Examples and tutorials » Density optimization examples » Temperature minimization

Assemble a state, adjoint and regularized density update for heat conduction.

Domain, design and objective

On the unit square, the material density $\gamma\in[0,1]$ controls the conductivity

\[ a(\gamma)=a_{\min}+(a_{\max}-a_{\min})\gamma^3, \qquad a_{\min}=10^{-4},\quad a_{\max}=1. \]

The positive lower bound keeps the state equation elliptic at zero density. With unit heat source, the temperature satisfies

\[ -\nabla\cdot(a(\gamma)\nabla u)=1,\qquad u=0\text{ on }\Gamma_D,\qquad a(\gamma)\partial_nu=0\text{ on the remaining boundary}. \]

The objective combines mean temperature and material usage.

\[ J(\gamma)=\frac{1}{|\Omega|}\int_\Omega u_\gamma\,dx +\ell\int_\Omega\gamma\,dx,\qquad\ell=4. \]

Material usage is penalized with a fixed weight. The algorithm uses projected regularized gradient steps.

Discrete setup

Parameters

The state, adjoint and density use scalar continuous P1 spaces. The initial density is 0.9, the step coefficient is $\mu=0.01$ and the gradient regularization coefficient is $\alpha=0.05$ . The default iteration budget is 5,000.

Mesh and sink boundary

A 32-by-32 vertex grid gives 1,024 vertices and 1,922 triangles. Boundary edges are selected as heat sinks when at least one endpoint is on the left boundary and within 0.1 of its midpoint. This endpoint-based rule defines the actual mesh-aligned sink interval.

  constexpr size_t n = 32;
  constexpr Geometry::Polytope::Type g = Geometry::Polytope::Type::Triangle;

  Alert::Info() << "Generating uniform grid..." << Alert::Raise;
  Mesh mesh = Mesh<Context::Local>::UniformGrid(g, { n, n });

  mesh.getConnectivity().compute(1, 2);
  mesh.scale(1.0 / (n - 1));

  // Boundary labeling
  for (auto it = mesh.getBoundary(); it; ++it)
  {
    bool onGammaD = false;
    for (auto vit = it->getVertex(); vit; ++vit)
    {
      const auto& vertex = *vit;
      const Real x = vertex.x();
      const Real y = vertex.y();

      if (std::abs(x) < 1e-12 && y > 0.5 - radius && y < 0.5 + radius)
      {
        onGammaD = true;
        break;
      }
    }
    mesh.setAttribute(it.key(), onGammaD ? GammaD : Gamma0);
  }

State, adjoint and update

State equation

At the current density, the state equation is assembled and solved. Factorization or solution failure terminates the program.

    Problem poisson(u, v);
    poisson = Integral(a * Grad(u), Grad(v))
            - Integral(f * v)
            + DirichletBC(u, RealFunction(0.0)).on(GammaD);
    Solver::SparseLU stateSolver(poisson);
    stateSolver.solve();
    if (!stateSolver.success())
      return EXIT_FAILURE;

Adjoint and sensitivity

Using the Lagrangian convention in this program, the adjoint satisfies

\[ \int_\Omega a(\gamma)\nabla p\cdot\nabla q\,dx =-\frac{1}{|\Omega|}\int_\Omega q\,dx. \]

It has the same homogeneous essential boundary data as the state. The objective derivative in density direction $\eta$ is

\[ J'(\gamma)[\eta]=\int_\Omega \left(\ell+3(a_{\max}-a_{\min})\gamma^2\nabla u\cdot\nabla p\right)\eta\,dx. \]
    Problem adjoint(p, q);
    adjoint = Integral(a * Grad(p), Grad(q))
            + Integral(RealFunction(1.0 / vol), q)
            + DirichletBC(p, RealFunction(0.0)).on(GammaD);
    Solver::SparseLU adjointSolver(adjoint);
    adjointSolver.solve();
    if (!adjointSolver.success())
      return EXIT_FAILURE;

Regularized gradient

The derivative density is represented through

\[ \alpha\int_\Omega\nabla g\cdot\nabla w\,dx +\int_\Omega gw\,dx=J'(\gamma)[w]. \]

Zero gradient values on the sink keep the design's sink-boundary nodal values fixed during these updates. The coefficient convention gives a smoothing length $\sqrt{\alpha}$ .

    Problem hilbert(gfun, w);
    hilbert = Integral(alpha * Grad(gfun), Grad(w))
            + Integral(gfun, w)
            - Integral(
                ell
                + 3 * (gmax - gmin) * Pow(gamma, 2)
                  * Dot(Grad(u.getSolution()), Grad(p.getSolution())),
                w)
            + DirichletBC(gfun, RealFunction(0.0)).on(GammaD);
    Solver::SparseLU gradientSolver(hilbert);
    gradientSolver.solve();
    if (!gradientSolver.success())
      return EXIT_FAILURE;

Project the density

The update is $\gamma^{k+1}=\Pi_{[0,1]}(\gamma^k-\mu g^k)$ . The current state, adjoint, gradient and density are exported together before the density changes, so a snapshot represents one consistent iterate.

    step = mu * gfun.getSolution();

    // Snapshot the state, adjoint and descent computed for this same density.
    xdmf.write(Real(i)).flush();
    gamma -= step;
    gamma = Min(1.0, Max(0.0, gamma));

The fixed step and fixed iteration budget define this demonstration. Assess a run through its objective, material usage and density fields. Objective-controlled step selection and an optimality stopping test are additional algorithm choices.

Inspect the results

Image
Computed P1 material density at the last recorded iterate.
Image
Computed temperature on the same design.

In the checked 5,000-state run, the objective decreased from approximately 4.47838 to 3.16329, with no increases in the recorded history. Material usage decreased from 0.9 to approximately 0.53165. Each state, adjoint and gradient solve succeeded. These observations describe the fixed-step run.

TemperatureMinimization.csv records mean temperature, material usage and their weighted objective. The XDMF output contains temperature u, adjoint p**, regularized gradient g, density gamma and update step. Open TemperatureMinimization.xdmf in ParaView and select Apply.

Build and run

Build TemperatureMinimization. The executable generates its mesh, so no resource file is required. Run it in a scratch directory; an optional positive integer argument selects the iteration budget. Omitting it runs 5,000 updates. The program checks each sparse solve and each recorded objective for failure.

Complete source code contains the boundary selection and numerical update.

Continue