Alessandro Morita Enjoying the thermodynamic limit

The math behind adjoints: PDE-constrained optimization for design

The math behind adjoints: PDE-constrained optimization for design

Introduction

This article introduces PDE-constrained optimization, a discipline with deep applications in engineering design. After all, any physical law is represented by a PDE, and engineering cares a lot about finding optimal designs — lower costs, higher safety, better performance, you name it.

We will navigate from good old Lagrange multipliers in Rn\mathbb R^n to infinite-dimensional functional derivatives. Buckle up.

In first-year calculus, we learn how to optimize a function, i.e. to find the maximum or minimum of a function f=f(x)f = f(x). Namely, we need to “differentiate and equal to zero”:

Find x such that dfdxx=x=0.\text{Find $x^*$ such that } \left.\frac{df}{dx}\right|_{x=x_\ast}=0.

A bit later, we learn that we may also add constraints to this optimization business: it may happen that xx cannot take all possible values, but only those implicitly defined by g(x)=0g(x) = 0. We then introduce the notion of a Lagrange multiplier, usually written as λ\lambda, and consider the modified function

L(x,λ)=f(x)+λg(x)L(x,\lambda) = f(x) + \lambda g(x)

and our goal becomes to now find the values x,λx_\ast, \lambda_\ast such that

Lxx=x,λ=λ=Lλx=x,λ=λ=0.\left.\frac{\partial L}{\partial x}\right|_{x=x_\ast, \lambda=\lambda_\ast} = \left.\frac{\partial L}{\partial \lambda}\right|_{x=x_\ast, \lambda=\lambda_\ast}=0.

There are various ways to generalize this framework, e.g. considering inequality constraints instead of equalities.

In applications, especially when dealing with physical systems, this framework may be too simple to capture the complexity of the real world. We need to generalize it.

A motivating example

Instead of starting with the abstract framework, let me first motivate it with an example.

An aerospace engineer is working a project to transport various types of gases to Mars. She is given a task of designing a container in the shape of a hollow aluminum spherical shell with a radius of 2 meters, that must withstand an internal pressure of 250 kPa while being transported in outer space (so the outer pressure is 0). This is the same pressure inside a can of soda in room temperature.

img

Like this, but larger. Picture from the internet.

There are two catches here:

  • Any solid, under pressure, will deform. Our project requirements specify that the maximum allowed deformation of the container is 3 mm.
  • Transporting massive objects in space is very expensive. Therefore, we want the mass of this container to be as small as possible.

What should our engineer do?

First, she parameterizes the problem. The geometry is fully determined by the radius of the sphere, RR, and its thickness hh, with the former being fixed at R=2R=2 meters. The design variable, analogous to the abstract variable xx in the previous section, is just the thickness hh.

How to map this variable to the total mass and maximum deformation of the shell? For the mass, the engineer know hows to proceed: since we are talking about a thin spherical shell (i.e. hRh \ll R), its volume is just the surface area of the sphere multiplied by the thickness, i.e.

Volume:4πR2h\mathrm{Volume:}\quad4\pi R^2h

and hence the total mass is just this volume multiplied by the density of aluminum:

Mass: m(h)=4πR2hρ.\text{Mass: }\quad m(h)=4\pi R^2h \rho.

Great - she knows what she wants to minimize mm as a function of hh. This is the equivalent of our optimization function ff discussed above.

But now she encounters an issue: how to write down the function gg describing the constraints to her problem? The constraints are the laws of physics themselves! In this particular case, she needs to solve the partial differential equation (PDE) for linear elasticity considering the boundary condition of 250 kPa pressure inside and zero outside.

The solution to this equation is a displacement field u(x)\vec u(\vec x) describing how a point x\vec x inside our massive body gets displaced to a new point x+u(x)\vec x + \vec u(\vec x). In this way, the displacement field describes how the body deforms under pressure. If she was given it, she could impose the requirement

u<3 mm.\vert \vec u \vert < 3\text{ mm}.

Notice how the flow of information goes, starting from the parameters describing the geometry:

Geometrical parameter hPDE for uConstraint for u.\text{Geometrical parameter $h$} \to \text{PDE for $\vec u$} \to \text{Constraint for $\vec u$}.

PDE-constrained optimization is a complex task that will be the focus of our discussion today.

Luckily for our engineer, she realizes that, in this case, spherical symmetry simplifies the problem significantly - browsing through her books, she finds an exact solution for the displacement of a spherical shell under internal pressure pp:

u=pR2(1ν)2Ehu = \frac{p R^2 (1-\nu)}{2Eh}

where E,νE, \nu are parameters (Young’s modulus and Poisson ratio) describing the stiffness of aluminum (this equation appears in Landau & Lifschitz Volume 7, Paragraph 7, Problem 2).

How lucky she is! She can finally write down her problem as follows: considering that, for aluminum,

  • Density: ρ=2710  kg/m3\rho = 2710 \;\mathrm{kg/m^3}
  • Young’s modulus: E=7×1010  PaE = 7 \times 10^{10}\;\mathrm{Pa}
  • Poisson ratio: ν=0.33\nu = 0.33

and that the pressure p=25×104 Pap = 25\times 10^4 \text{ Pa} for this problem, the optimization becomes

Minimize136,219hsubject to4.8×106h3×103\begin{align*} \text{Minimize} & \quad 136,219 h\\ \text{subject to} & \quad \frac{4.8\times 10^{-6}}{h} \leq 3\times10^{-3} \end{align*}

This is now easy to solve: since the function to minimize is linear, we want to pick the smallest possible value of hh satisfying the constraint. Since

h4.8×1063×103=0.00159 metersh \geq \frac{4.8 \times 10^{-6}}{3 \times 10^{-3}}=0.00159\text{ meters}

we conclude that the optimal value for hh is 1.59 millimeters, corresponding to a total mass of 218 kilograms.

Our engineer sighs in relief that the formula for the displacement of a spherical shell was available analytically. She wonders what she would have done if it wasn’t…

The general framework

Leaving the example, we now introduce the theoretical framework.

Our ingredients are fourfold:

Ingredient 1: a function u=u(x)u = u(x) which is a solution to a differential equation and its initial / boundary conditions. Both uu and its argument xx can be almost anything: vectors, scalars or tensors, real or complex.

Ingredient 2: we further assume that something - maybe the geometrical domain Ω\Omega over which uu is defined, or its boundary conditions - can be parameterized with a set of parameters θ\theta, considered here as a finite vector. In the previous example θ\theta was a single vector [h][h] with the thickness of the shell.

Then, uu will be indirectly a function of θ\theta as well as of its variable xx. We may write this as u=u(x;θ)u = u(x;\theta) or uθ(x)u_\theta(x) when we want to explicitly communicate this dependence.

Ingredient 3: We are also given physical and/or design constraints that we want our solution to satisfy.

These can be human-chosen functions, but also the laws of physics themselves - in the previous example, we had both the upper limit on desired deformation of our shell but also the linear elastic equations. In all generality, write them as g(u)=0g(u)=0. As functions of uu, these will inherit the dependence on θ\theta.

Ingredient 4: finally, we are given a scalar functional, called a loss function or objective function, and often written as JJ, that takes in uu and returns a real number. JJ is what we will want to optimize for.

A functional is just a function of a function. In physics, to make the difference between a regular function ff and a functional JJ explicit, we use square brackets for their arguments instead of parenthesis. Hence, J[f]J[f] is a functional taking in a function, and returning e.g. a number (or maybe another function)

As a function of uu, JJ is indirectly a function of θ\theta; in some cases, we may also explicitly pass θ\theta as an argument to JJ, as was the case of the previous example in which JJ was the mass of the shell - it did not depend on uu, but depended on the thickness directly. Below, we will write

J=J[u] or J[uθ,θ]J = J[u] \text{ or } J[u_\theta,\theta]

depending on the context.

Examples of this framework are:

Designing a heat sink:

  1. uu is the temperature field over the heat sink, which solves the steady heat equation
  2. θ\theta specifies design parameters, such as fin spacing, height or thickness
  3. A design constraint may be that maximum temperature at specific points does not exceed some threshold
  4. JJ may be the heat flux integrated over the base of the heat sink, as a measure of its efficiency.

Designing an airfoil for a plane

  1. uu is actually a tuple (v,p)(v, p) describing the air velocity and pressure over the airfoil. The equation is the Navier-Stokes equation of fluid mechanics
  2. θ\theta can be any set of parameters describing the shape of the airfoil
  3. Design constraints may include target lift-to-drag ratios
  4. JJ may be the drag force, which we want to minimize.

As we can see, this framework is pretty general. Adjoint methods are one way of tackling this very broad family of problems.

The need for derivatives

In the literature for adjoint methods often focuses strictly on a slightly different problem:

How to calculate dJdθ?\text{How to calculate } \frac{dJ}{d\theta}?

That is, how can we precisely calculate the sensitivity of the objective JJ with respect to the parameters θ\theta?

Of course, computing derivatives and optimizing are related tasks - we discussed this in the beginning of this section. Many optimization methods, such as the famous gradient descent method and its variants, directly rely on computing gradients. Even for problems which are not strictly optimization tasks, such as solving a non-linear equation, can benefit from derivative information, as is the case with the Newton-Raphson method.

Therefore, knowing how to compute the derivative of our objective function is an important task on its own, and one that can be used directly in optimization. Therefore, this will be our focus from now on.

Aren’t finite differences enough?

A naive (but nonetheless useful and still very used in practice) way to compute the sensitivity of JJ with respect to θ\theta is to slightly perturb θ\theta and recompute JJ, approximating the derivatives via finite differences.

Namely, suppose one has access to a DifferentialEquationSolver\text{DifferentialEquationSolver} function that, given a vector θ\theta, yields the solution function uθu_\theta. Then, we can approximate the ii-th component of the vector dJ/dθdJ/d\theta, at a particular value θ0\theta_0, as

(dJdθθ=θ0)iJ[uθ0+ϵei,θ0+ϵei]J[uθ0,θ0]ϵ\left(\left.\frac{dJ}{d\theta}\right|_{\theta=\theta_0}\right)_i\approx \frac{J[u_{\theta_0 + \epsilon e_i},\theta_0+\epsilon e_i]-J[u_{\theta_0},\theta_0]}{\epsilon}

i.e. as the finite difference approximation along axis with unit vector eie_i (other approximation methods, such as central differences, can also be used).

This can get expensive very quickly: on top of calculating uθ0u_{\theta_0}, this involves O(p)O(p) calls to DifferentialEquationSolver\text{DifferentialEquationSolver} if θ\theta takes values in Rp\mathbb R^p . Calls to differential equation solvers (both ODE and PDE) are almost always expensive.

Therefore, especially when dealing with many parameters, there are better ways. We discuss them now.

Motivating the adjoint

For concreteness, assume for now that uu takes values in Rd\mathbb R^d and θ\theta takes values in Rp\mathbb R^p. Then, schematically,

dJdθ=Jθ+Jududθ\frac{dJ}{d\theta} = \frac{\partial J}{\partial \theta}+\frac{\partial J}{\partial u} \frac{du}{d\theta}

Both the LHS and the first term in the RHS are 1×p1\times p (row) vectors. The last, product, term, is the matrix product of a 1×d1 \times d (row) vector and a d×pd\times p matrix, yielding the correct dimensions.

The first term on the RHS is something that often will not depend on the solution of a PDE (for example, how mass depends on the geometrical parameters). It can be computed in a variety of ways, from automatic differentiation to finite differences.

Similarly, the J/u\partial J/\partial u term can often be calculated explicitly since JJ is a human-chosen metric. Therefore, the complexity lies in calculating du/dθdu/d\theta: how the solution to the PDE depends on its parameters.

Let us assume that a set of constraint equations

g(u,θ)=0g(u,\theta) = 0

are given, with gg taking values in Rm\mathbb R^m. If these equations hold identically, then in particular their total derivative with respect to θ\theta must be zero as well:

dgdθ=gududθ+gθ=0dudθ=(gu)1dgdθ\frac{dg}{d\theta}=\frac{ \partial g }{ \partial u } \frac{du}{d\theta}+\frac{ \partial g }{ \partial \theta } =0 \quad\Rightarrow\quad \frac{du}{d\theta}=-\left( \frac{ \partial g }{ \partial u } \right)^{-1} \frac{dg}{d\theta}

Plugging back into the expression for dJ/dθdJ/d\theta gives

dJdθ=JθJu(gu)1()dgdθ\frac{dJ}{d\theta}=\frac{ \partial J }{ \partial \theta } \underbrace{-\frac{ \partial J }{ \partial u } \left( \frac{ \partial g }{ \partial u } \right)^{-1} }_{(*)}\frac{dg}{d\theta}

Let us give a name to the term ()(*): call it the adjoint

λT:=Ju(gu)1,\lambda^T :=-\frac{ \partial J }{ \partial u } \left( \frac{ \partial g}{ \partial u } \right)^{-1},

which can be seen to be the solution of a linear system of equations:

(gu)Tλ=(Ju)T\left( \frac{ \partial g }{ \partial u } \right)^{T} \lambda=-\left( \frac{ \partial J }{ \partial u } \right)^T

(again, check for matrix dimension consistency: (f/u)T(\partial f/\partial u)^T is a d×md \times m matrix, and the RHS is a d×1d \times 1 column vector, so λ\lambda must be a m×1m\times 1 column vector).

Then the total derivative w.r.t. parameters can be found as

dJdθ=Jθ+λTgθ\frac{dJ}{d\theta} = \frac{ \partial J }{ \partial \theta } + \lambda^T \frac{ \partial g}{ \partial \theta }

This is a very interesting result. It shows that, regardless of how uu is obtained (for example, as the solution to a complicated boundary value problem), once we have it, we can use it to compute the adjoint via its linear system; the solution can the be plugged into the expression above to immediately obtain sensitivity. This did not require many calls to the DifferentialEquationSolver!

Lagrange multipliers and adjoints

An alternative derivation of the adjoint equation utilizes the method of Lagrange multipliers. This approach provides additional insight by embedding the constraint g(u,θ)=0g(u, \theta) = 0 directly into the functional, allowing us to treat the solution process and optimization problem simultaneously.

We start by defining an augmented functional — the Lagrangian — that includes the constraint g(u,θ)=0g(u, \theta) = 0:

L(u,θ,λ)=J(u,θ)+λTg(u,θ),\mathcal{L}(u, \theta, \lambda) = J(u, \theta) + \lambda^T g(u, \theta),

where λ\lambda is a vector of Lagrange multipliers, also referred to as the adjoint variables, and takes values in Rm\mathbb{R}^m.

For the total derivative dJ/dθdJ/d\theta to be consistent with the constraint g(u,θ)=0g(u, \theta) = 0, we require stationarity of L\mathcal{L} with respect to all variables. This means that the partial derivatives of L\mathcal{L} with respect to uu, λ\lambda, and θ\theta must vanish:

  1. Stationarity with respect to λ\lambda enforces the constraint:
Lλ=g(u,θ)=0.\frac{\partial \mathcal{L}}{\partial \lambda} = g(u, \theta) = 0.
  1. Stationarity with respect to uu yields the adjoint equation:
Lu=Ju+λTgu=0(gu)Tλ=(Ju)T.\frac{\partial \mathcal{L}}{\partial u} = \frac{\partial J}{\partial u} + \lambda^T \frac{\partial g}{\partial u} = 0 \quad \Rightarrow \quad \left( \frac{\partial g}{\partial u} \right)^T \lambda = -\left( \frac{\partial J}{\partial u} \right)^T.

This is the same linear system for λ\lambda as derived earlier.

  1. Stationarity with respect to θ\theta yields:
Lθ=Jθ+λTgθ.\frac{\partial \mathcal{L}}{\partial \theta} = \frac{\partial J}{\partial \theta} + \lambda^T \frac{\partial g}{\partial \theta}.

Assume we have found uu_\ast, λ\lambda_\ast, θ\theta_\ast satisfying the required equations. Then the g=0g = 0 condition is satisfied; on this set of parameters, the Lagrangian just equals JJ itself, and

dJ/dθ=L/θ,dJ/d\theta = \partial \mathcal L/\partial \theta,

which is given by the equation above. Hence, we find the same expression for sensitivity as before!

Continuous adjoints

The Introduction motivated the construction of adjoints as auxiliary variables for calculating sensitivities of objective functions with respect to their parameters.

For this, we assumed all quantities were finite; in particular, that the constraints could be represented by a vector equation g(u,θ)=0g(u,\theta) = 0 with mm components. λ\lambda was then also a vector in Rm\mathbb R^m.

However, in the general case, we will be dealing with infinite-dimensional spaces; solutions of PDEs commonly live in Banach spaces. Therefore, we must generalize the discussion above to infinite dimensions.

As a spoiler, if you are only interested in finite element methods (FEM), then you can freely skip this section. FEM brings functions from infinite-dimensional spaces to finite-dimensional representations, and so the machinery of the previous section applies nicely - in particular, the laws of physics become a finite system of equations.

Gateaux derivatives

Writing a section with the right balance between mathematical rigor and intuition is always challenging. Since my main focus here is giving you the tools for computing things, I will provide a mostly operational definition of the necessary mathematical entities.

Recall that a functional FF is a function taking in another function, say uu, and returning a number. We may write F=F[u]F = F[u] in the same way we write f=f(x)f = f(x) for a function ff taking in an argument xx.

The Gateaux differential or Gateaux derivative of a functional FF is a linear operator defined by its action on any function ϕ\phi as

dF[u]du,ϕ:=ddtF[u+tϕ]t=0.\boxed{\left\langle \frac{dF[u]}{du},\phi\right\rangle:=\left.\frac{d}{dt}F[u+t\phi]\right|_{t=0}.}

A few comments are in order. First, notice that dF/dudF/du does not exist by itself; it is only defined when applied to another function ϕ\phi.

The ,\langle\cdot,\cdot\rangle is a pairing notation that is reminiscent of an inner product, but this is just notation. It is made to evoke the usual directional derivative in Euclidean space, namely vf(f)v\nabla_v f \equiv (\nabla f) \cdot v. There are alternative notations; see below.

The possible domain of functions ϕ\phi is the same of FF. More precisely, if X,YX, Y are Banach spaces and UXU \subseteq X is an open set, let us assume F:UYF: U \to Y. Then, dF[u]/du,ϕ\langle dF[u]/du, \phi\rangle is defined for any ϕX\phi \in X and uUu \in U.

Secondly, notice that we could equivalently write the RHS of this definition as

ddtF[u+tϕ]t=0=limt0+F[u+tϕ]F[u]t;\left.\frac{d}{dt} F[u + t\phi]\right|_{t=0}=\lim_{t \to 0^+} \frac{F[u+t\phi] - F[u]}{t};

this is indeed how many books define this derivative. I find the notation we used more useful for most applications, but both are equivalent.

A word on notation

The notation above is not unique. It is closest to that of reference [3] with the square brackets coming from the convention in physics for functionals.

The following are other equivalent notations observed in the literature:

dF(u;ϕ)(in math texts)δF[u](in physics texts, with δu instead of ϕ)\begin{align*} dF(u;\phi) & \quad\text{(in math texts)} \\ \delta F[u] & \quad \text{(in physics texts, with $\delta u$ instead of $\phi$)} \end{align*}

Physics tests further define the so-called functional derivative as the integral kernel of the Gateaux derivative, i.e. a Gateaux derivative “density”. More precisely, for a functional F=F[u]F = F[u] given by an integral

F[u]=Ω(some function of u and derivatives)dxF[u] = \int_\Omega \text{(some function of $u$ and derivatives)} dx

the functional derivative is the quantity δF/δf(x)\delta F/\delta f(x) defined via

dFdu,δuδF[u]=ΩδFδu(x)δu(x)dx\left\langle \frac{d F}{du},\delta u\right\rangle\equiv\delta F[u] = \int_\Omega \frac{\delta F}{\delta u(x)} \delta u(x)\,dx

where we have written δu\delta u instead of ϕ\phi.

Physicists’ notation is slightly more compact and generalizes easily for more than one functional. For example, if F=F[f,g]F = F[f,g] then its total Gateaux derivative is just

δF[f,g]=Ω(δFδf(x)δf(x)+δFδg(x)δg(x))dx\delta F[f,g]=\int_\Omega \left(\frac{\delta F}{\delta f(x)} \delta f(x) + \frac{\delta F}{\delta g(x)} \delta g(x)\right) dx

Imposing, for instance, that δF=0\delta F = 0 would require both integrands to be zero for any variations δf,δg\delta f, \delta g.

Example calculation

We now provide an example to make the calculation of Gateaux derivatives more concrete. Let ΩRn\Omega \subset \mathbb R^n. Consider functions which are square-integrable over Ω\Omega, taking real values. For some smooth function L:RRL: \mathbb R \to\mathbb R, define

F[f]=ΩL(f(x))dxF[f]= \int_{\Omega} L(f(x)) dx

Let us calculate its Gateaux derivative:

F[f+tϕ]=ΩL(f(x)+tϕ(x))dxddtF[f+tϕ]=ΩddtL(f(x)+tϕ(x))dx=ΩL(f(x)+tϕ(x))ϕ(x)dx(by chain rule)ddtF[f+tϕ]t=0=ΩL(f(x))ϕ(x)dx\begin{align*} F[f+t\phi]& =\int_\Omega L(f(x) + t\phi(x)) dx\\ \frac{d}{dt}F[f+t\phi] &=\int_\Omega \frac{d}{dt}L(f(x) + t\phi(x))dx\\ &=\int_\Omega L'(f(x) + t\phi(x)) \phi(x)dx\quad\text{(by chain rule)}\\ \left.\frac{d}{dt}F[f+t\phi]\right|_{t=0} &=\int_\Omega L'(f(x)) \phi(x) dx \end{align*}

which shows that the Gateaux derivative is the linear functional given by

dF[f]df,ϕ=ΩL(f(x))ϕ(x)dx.\left\langle \frac{dF[f]}{df},\phi\right\rangle =\int_\Omega L'(f(x)) \phi(x) dx.

Exercise: generalizing the case above for L=L(f(x),f(x))L = L(f(x), \nabla f(x)), show that

dF[f]df,ϕ=Ω(Lf(x)ϕ(x)+Lf(x)ϕ(x))dx\left\langle \frac{dF[f]}{df}, \phi\right\rangle= \int_\Omega \left(\frac{\partial L}{\partial f(x)} \phi(x) + \frac{\partial L}{\partial \nabla f(x)}\cdot\nabla\phi(x)\right)dx

Exercise: in the case above, specializing to test functions ϕ\phi which vanish on the boundary Ω\partial\Omega, show that requiring dF/df=0dF/df = 0 for all ϕ\phi implies the Euler-Lagrange equation:

Lf(x)(Lf(x))=0.\frac{\partial L}{\partial f(x)}-\nabla\cdot\left(\frac{\partial L}{\partial\nabla f(x)} \right) = 0.

Optimization for the heat equation

Our working example will be that of the heat equation. We do this to avoid excess abstraction in the theory, while providing a concrete application which you can use as inspiration for other problems.

Weak form

Weak formulation of the heat equation in Rd\mathbb R^d with mixed Neumann and Dirichlet boundary conditions: the equation is, in its full glory,

(ku)=qin Ωu=0on ΓDkun=g0on ΓN\begin{align*} -\nabla\cdot(k \nabla u) &= q\quad\text{in }\Omega\\ u&=0\quad \text{on } \Gamma_D\\ -k\frac{\partial u}{\partial n} &= g_0 \quad\text{on } \Gamma_N \end{align*}

(with ΓNΓD=Ω\Gamma_N \cup \Gamma_D = \partial\Omega). Here,

  • uu denotes temperature, in whatever chosen units (e.g. Kelvin)
  • kk is the thermal conductivity, in units of power per unit length, per unit temperature
  • qq is some internal energy source density, in units of power per unit volume
  • g0g_0 is some prescribed flux, in units of power per unit length.

By multiplying by a test function vv and integrating by parts, we are able to rewrite the problem in its weak (continuous Galerkin) form as: find uVu \in V such that

a(u,v)=l(v),vVa(u,v) = l(v),\quad\forall v \in V

where

a(u,v)=Ωkuvdx,l(v)=Ωqvds+ΓNg0vdsa(u,v)=\int_\Omega k \nabla u \cdot \nabla v\,dx, \quad l(v) = \int_\Omega qv\,ds + \int_{\Gamma_N} g_0 v\,ds

and where we have defined the following subspace of L2(Ω)L^2(\Omega)

V=H01(Ω)={wL2(Ω):w[L2(Ω)]d,wΓD=0}V =H_0^1(\Omega)=\{w \in L^2(\Omega): \nabla w \in [L^2(\Omega)]^d, w\vert_{\Gamma_D}=0 \}

Setting up the Lagrangian

Let us assume that we want to minimize the following functional, sometimes called heat transport potential capacity (see reference [2]):

J[u]=Ωk2u2dxJ[u]=\int_\Omega \frac k2 |\nabla u|^2dx

The intuition behind this quantity is that it measures how well temperature flows around the body.

Great, we have an objective to optimize for. Now, what are our parameters θ\theta?

In this functional framework we are in, we want to be able to ask questions such as “how would my objective function change if I perturbed this function?”. A common function to choose in the heat scenario is kk itself — the thermal conductivity. This is particularly true in topology optimization scenarios in which we want to let kk become zero in some regions, effectively introducing holes in the medium.

Therefore, let us work with this assumption: our parameter θ\theta in this case will be the thermal conductivity kk itself. This is a good example since it appears both in our objective JJ and in our constraint equations.

We need only add a Lagrange multiplier and the associated constraint to this functional, and we will have in our hands a Lagrangian that we can optimize. Gateaux derivatives are the tool of choice for optimization when we are dealing with infinite-dimensional functionals, analogous to how the regular derivative d/dxd/dx was our tool for finite-dimensional optimization.

I assert that

L[u,v,k]=J[u]+a(u,v)l(v)=Ωk2u2dx+ΩkuvdxΩqvds+ΓNg0vds\begin{align*} \mathcal L[u,v,k] &= J[u] + a(u,v) - l(v)\\ &=\int_\Omega \frac k2 \vert\nabla u\vert^2 dx + \int_\Omega k \nabla u \cdot \nabla v\,dx - \int_\Omega qv\,ds + \int_{\Gamma_N} g_0 v\,ds \end{align*}

is the Lagrangian we want, with the test function vv that appears in the weak formulation being our continuous Lagrange multiplier.

It does not explicitly appear in the neat form Lagrange multiplier times constraint that we are familiar with, but this is purely aesthetical: remember that the weak form was obtained by multiplying the strong form L(u)fL(u) - f by vv and integrating. That is,

a(u,v)l(v)=v,L(v)fa(u,v) - l(v) = \langle v, L(v) - f \rangle

where ,\langle \cdot, \cdot \rangle here is the L2(Ω)L^2(\Omega) inner product

f,g=Ωf(x)g(x)dx.\langle f,g\rangle = \int_\Omega f(x) g(x) dx.

Therefore, what we have above is a sum (integral) of the Lagrange multiplier v(x)v(x) times the constraint (L(v)f)(x)=(k(x)u(x))f(x)(L(v)-f)(x) = -\nabla \cdot (k(x) \nabla u(x)) - f(x) at every point xx.

Calculating derivatives

For completeness, I repeat here what our Lagrangian is:

L[u,v,k]=Ωk2u2dx(I)+Ωkuvdx(II)Ωqvds(III)+ΓNg0vds(IV)\boxed{\mathcal L[u,v,k] = \underbrace{\int_\Omega \frac k2 \vert\nabla u\vert^2 dx}_{(\mathrm{I})} + \underbrace{\int_\Omega k \nabla u \cdot \nabla v\,dx}_{(\mathrm{II})} - \underbrace{\int_\Omega qv\,ds}_{(\mathrm{III})} + \underbrace{\int_{\Gamma_N} g_0 v\,ds}_{(\mathrm{IV})}}

Things are about to get a bit mechanical since we are entering the domain of “sit down and calculate”, but we can foresee what is going to happen. Remember from the first chapter that, for a simple Lagrangian (renaming λ\lambda to vv to match current notation)

L(u,v,θ)=J(u,θ)+vTg(u,θ)\mathcal{L}(u, v, \theta) = J(u, \theta) + v^T g(u, \theta)

we had that:

  • Stationarity w.r.t. vv gave us the constraint equation g(u,θ)=0g(u,\theta) = 0
  • Stationarity w.r.t uu gave us the adjoint equation, i.e. that solved by vv;
  • Stationarity w.r.t. θ\theta gave us the desired equation for the sensitivity of JJ with respect to the parameters

The same thing will happen now. We will first be very explicit with the Gateaux derivatives, to help you get used to the machinery.

If we were using physicists’ notation as discussed in the previous section, we could just take δL=0\delta \mathcal L = 0 with an understanding that this would generate 3 sets of terms: those proportional to δu\delta u, δv\delta v and δθ\delta \theta, respectively, all which would have to be zero independently.

Stationarity with respect to the Lagrange multiplier

Only terms containing vv and its derivatives are non-zero, so we can ignore (I). Starting with (II):

ddvΩkuvdx,ϕ=ddtΩku(v+tϕ)dxt=0=Ωkuϕdx.\begin{align*} \left\langle \frac{d}{dv} \int_\Omega k\nabla u\cdot \nabla v\,dx,\phi \right\rangle &=\frac{d}{dt} \left.\int_\Omega k \nabla u\cdot\nabla(v + t\phi)\, dx \right\vert_{t=0}\\ &=\int_\Omega k\nabla u\cdot \nabla \phi\,dx. \end{align*}

You may realize that, whenever the functional is linear in vv and its derivatives, the Gateaux derivative formally just replaces vv with ϕ\phi. For example, (III) yields

ddvΩqv,ϕ=Ωqϕ.\left\langle \frac{d}{dv} \int_\Omega qv, \phi \right\rangle = \int_\Omega q\phi.

In fact, we can find ourselves a nice general result beyond the heat equation here. Recall that our Lagrangian is, on a high level,

L[u,v,θ]=J[u]+a(u,v)l(v);\mathcal L[u,v,\theta] = J[u] + a(u,v) - l(v);

since the J[u]J[u] term does not count for the derivative with respect to vv, and since both a(u,v)a(u,v) and l(v)l(v) are linear forms with respect to vv, we immediately conclude that

dLdv,ϕ=a(u,ϕ)l(ϕ)=!0ϕ.\left\langle \frac{d \mathcal L}{dv},\phi\right\rangle=a(u,\phi)-l(\phi) \overset{!}{=} 0\quad\forall \phi.

But a(u,ϕ)=l(ϕ)a(u,\phi) = l(\phi) is literally the weak form of our problem. Therefore, we have just retrieved the constraint equation, as expected.

Stationarity with respect to the solution

We now go back to the derivative with respect to uu. The derivative of the objective function JJ (term (I)) is:

dduΩk2u2dx,ψ=ddtΩk2u+tψ2dxt=0=ddtΩk2(u+tψ)(u+tψ)dxt=0=Ωkuψdx.\begin{align*} \left\langle \frac{d}{du} \int_\Omega \frac k2 \vert\nabla u\vert^2 dx,\psi\right\rangle &= \left.\frac{d}{dt} \int_\Omega \frac k2 \vert\nabla u+t \nabla \psi\vert^2 dx \right\vert_{t=0}\\ &=\left.\frac{d}{dt} \int_\Omega \frac k2 (\nabla u+t \nabla \psi)\cdot (\nabla u+t \nabla \psi) dx\right\vert_{t=0}\\ &= \int_\Omega k \nabla u \cdot\nabla\psi\,dx. \end{align*}

Notice the test function is now ψ\psi; for every stationarity condition, we use a new test function. This is more easily seen in physicists’ notation, in which ψ\psi would be δu\delta u.

For term (II), we notice it is a linear function of uu and, from the discussion in the previous section, we know that

dduΩkuvdx,ψ=Ωkψvdx.\left\langle \frac{d}{du} \int_\Omega k \nabla u\cdot \nabla v\, dx, \psi\right\rangle=\int_\Omega k\nabla \psi\cdot \nabla v\,dx.

Finally, terms (III) and (IV) are not functions of uu, so they can be ignored. Putting everything together, we find that

dLdu,ψ=Ωkuψdx+Ωkψvdx=!0ψ.\left\langle \frac{d \mathcal L}{du},\psi\right\rangle=\int_\Omega k \nabla u\cdot\nabla \psi\,dx + \int_\Omega k\nabla \psi\cdot \nabla v\,dx \overset{!}{=} 0\quad\forall \psi.

Notice that this can be written as

Ωkψ(u+v)dx=0ψ\int_\Omega k \nabla \psi\cdot\nabla(u+v)dx=0\quad\forall\psi

which, in turn, implies the surprising result

v=u\boxed{v = -u}

i.e., for the particular case of the objective function given by the heat potential capacity, the adjoint vv is equal to uu up to a sign.

Can you see why this would not be the case if we had chosen any other JJ?

An addendum: the strong form of the adjoint problem

Although we found ourselves a nice result v=uv=-u in this case, it would not be like this had we chosen other objective functions.

Let us pretend we didn’t see that, and instead do something different: starting from the weak-form adjoint equation,

Find vH01(Ω) such that Ωkuψ+Ωkψvdx=0,ψH01(Ω)\text{Find } v \in H_0^1(\Omega) \text{ such that }\int_\Omega k \nabla u\cdot\nabla \psi + \int_\Omega k\nabla \psi\cdot \nabla v\,dx = 0,\quad\forall \psi\in H_0^1(\Omega)

let us show that we can actually reverse-engineer the strong form PDE for the adjoint vv.

The trick is to integrate by parts so that ψ\psi appears without any derivatives. This procedure will result in bulk and boundary integrals which must all independently vanish. Let us see how.

Writing

Ωk(u+v)ψdx=0\int_\Omega k (\nabla u + \nabla v)\cdot\nabla\psi\,dx = 0

and integrating by parts, we get

ΓNψk(un+vn)ds+ΓDψk(un+vn)dsΩψ[(kv)+(ku)]dx=0.\begin{align*} \int_{\Gamma_N} \psi k\left(\frac{\partial u}{\partial n} + \frac{\partial v}{\partial n}\right)\,ds &+ \int_{\Gamma_D} \psi k\left(\frac{\partial u}{\partial n}+ \frac{\partial v}{\partial n}\right) \, ds \\ &- \int_\Omega \psi[\nabla\cdot(k\nabla v) + \nabla\cdot(k\nabla u)]\,dx=0. \end{align*}

We can simplify this expression in several ways:

  • The ΓD\Gamma_D integral vanishes since ψH01(Ω)\psi \in H_0^1(\Omega), i.e. vanishes over ΓD\Gamma_D
  • On ΓN\Gamma_N, ku/n=g0-k\, \partial u/\partial n = g_0
  • In Ω\Omega, the strong form of the primal problem is (ku)=q-\nabla \cdot (k \nabla u) = q

Hence, the equation becomes

ΓNψ(kvng0)dsΩψ[(kv)q]dx=0.\int_{\Gamma_N} \psi \left(k\frac{\partial v}{\partial n} - g_0 \right) ds - \int_\Omega \psi[\nabla\cdot(k\nabla v)-q]dx=0.

For both integrals to vanish, we obtain the differential equation

(kv)=qin Ω-\nabla\cdot(k\nabla v)=-q\quad\text{in }\Omega

and boundary conditions

kvn=g0on ΓN-k\frac{\partial v}{\partial n}=-g_0\quad \text{on }\Gamma_N v=0on ΓD (coming from v being in H01(Ω)v = 0\quad\text{on }\Gamma_D \text{ (coming from $v$ being in $H_0^1(\Omega)$}

Notice that formally replacing v=uv = -u yields exactly the strong form problem for uu, which reinforces that this is the solution for the adjoint in this formulation.

Going back to derivatives

Stationarity with respect to parameters

Finally, we calculate the derivative with respect to θ\theta, or kk in this particular example. Only terms (I) and (II) depend on kk, which appears linearly in both, so we immediately find

dLdk,η=Ω12u2ηdx+Ωuvηdx=Ω(12u2+uv)ηdx.\begin{align*} \left\langle \frac{d\mathcal L}{d k}, \eta \right\rangle &= \int_\Omega \frac 12 \vert\nabla u\vert^2 \eta\,dx + \int_\Omega\nabla u\cdot\nabla v\,\eta\,dx\\ &=\int_\Omega \left(\frac 12 \vert \nabla u\vert^2 + \nabla u \cdot \nabla v \right)\eta \,dx. \end{align*}

But we know that, for our problem, we have a nice analytical result: v=uv = -u, which we can substitute to obtain

dLdk,η=Ω12u2ηdx\boxed{\left\langle \frac{d\mathcal L}{d k}, \eta \right\rangle = -\int_\Omega \frac 12 \vert\nabla u\vert^2 \eta\,dx}

showing that, for an everywhere non-negative change to kk (specified via η\eta), our objective function will become smaller.

Does this make sense, physically? Assume a very simple case, that of a solid beam-like structure. We set u=0u=0 in one end (say, at x=0x=0) and specify the flux g0g_0 at the other end (say x=Lx=L).

Since the problem is 1D, the heat equation is just u=0u'' = 0, with solution u=g0x/ku = -g_0 x/k matching the boundary conditions. The higher kk is, the less of a temperature drop between x=0x=0 and x=Lx=L we observe - good conductors spread temperature across them more effectively.

The heat potential capacity can be calculated analytically to be

J=0L12k(g0k)2dx=g02L2kJ=\int_0^L \frac 12 k \left(\frac{g_0}{k} \right)^2 dx=\frac{g_0^2L}{2k}

Indeed, if we let kk increase, JJ decreases, in accordance to what we just found:

dJ=g02L2k2dk<0dJ =-\frac{g_0^2 L}{2k^2} dk <0

Joining everything

We are done:

  • We pick an objective function JJ, here being

    J[u,k]=Ωku22dx.J[u,k]=\int_\Omega \frac{k \vert\nabla u\vert^2}{2}dx.
  • We solve the primal problem to find uH01(Ω)u \in H_0^1(\Omega) satisfying

    a(u,ϕ)=l(ϕ)ϕH01(Ω)a(u,\phi) = l(\phi)\quad\forall \phi \in H_0^1(\Omega)

    where

    a(u,ϕ)=Ωkuϕdx,l(ϕ)=Ωqϕdx+ΓNg0ϕdsa(u,\phi)=\int_\Omega k \nabla u \cdot \nabla \phi\,dx, \quad l(\phi) = \int_\Omega q\phi\,dx + \int_{\Gamma_N} g_0 \phi\,ds

    and the relevant space is

    H01(Ω)={wL2(Ω):w[L2(Ω)]d,wΓD=0}H_0^1(\Omega)=\{w \in L^2(\Omega): \nabla w \in [L^2(\Omega)]^d, w\vert_{\Gamma_D}=0 \}
  • Then, we solve the adjoint problem, which depends on JJ and the solution of the primal problem. In this case

    v=uv = -u
  • With these two in hand, we can compute the sensitivity of our loss with respect to the thermal conductivity kk along any “direction” η\eta, interpreted as a perturbation to kk via

    dLdk,η=Ω12u2ηdx\left\langle \frac{d\mathcal L}{d k}, \eta \right\rangle = -\int_\Omega \frac 12 \vert \nabla u\vert^2\eta \,dx

Discrete adjoints and FEM

In practice, despite the continuous formalism’s elegance, for computational purposes we need to restrict ourselves to finite dimensions.

Lightning review of FEM

This is the whole deal with the Finite Element Method (FEM): starting from the continuous formulation on an infinite-dimensional vector space HH, namely

Find uH such that a(u,v)=l(v) for all vH,\text{Find } u \in H \text{ such that } \quad a(u,v) = l(v) \quad\text{ for all } v \in H,

the finite element method invites us to find a finite subspace HhHH_h \subset H, spanned by a basis {Ni}i=1n\{N_i\}_{i=1}^n, such that we can write

u(x)=i=1nuiNi(x),v(x)=i=1nviNi(x).u(x) = \sum_{i=1}^n u_i N_i(x),\quad v(x) = \sum_{i=1}^n v_i N_i(x).

If the form aa is bilinear and vv is linear, we can plug these expressions in and write

i=1nj=1nuivja(Ni,Nj)=j=1nvjl(Nj).\sum_{i=1}^n \sum_{j=1}^n u_i v_j a(N_i, N_j)=\sum_{j=1}^n v_j l(N_j).

For this to hold for any vjv_j‘s, it must be true that (calling aija(Ni,Nj)a_{ij} \equiv a(N_i, N_j) and lj=l(Nj)l_j = l(N_j)):

i=1naijui=ljj{1,,n}\sum_{i=1}^n a_{ij} u_i=l_j\quad\forall j\in\{1,\ldots, n\}

or, in matrix form

Au=b\boxed{A u = b}

with ARn×nA \in \mathbb R^{n\times n} being a matrix whose i,ji,j component is ajia_{ji}, and u,bRnu, b\in \mathbb R^n being column vectors.

Therefore, making use of a FEM-like basis expansion, we are able to reduce our infinite-dimensional, continuous approach into a matrix equation.

Optimization problem revisited

This program of converting our quantities into matrix objects can be expanded to the objective function as well. For instance, consider our objective

J=Ωku22dxJ = \int_\Omega \frac{k\vert\nabla u\vert^2}{2}dx

In FEM, we discretize the domain Ω\Omega into a mesh ThT_h of, say, tetrahedra - the elements. The basis functions NiN_i are then constructed so that they are localized, each NiN_i being non-zero only over a few such elements.

img Example of a 2D domain being meshed to increasingly finer resolutions. From COMSOL documentation

img Example of two basis functions and their overlap. Notice that, outside the highlighted areas, the basis functions are identically zero. From COMSOL documentation

Assume we are given such a mesh, consisting of NN tetrahedral elements ee. We will make the assumption that kk can be approximated well enough element-wise, i.e. kk is a constant per element.

This greatly simplifies the integral. Since the gradient can be written as

u(x)i=1nuiNi(x),\nabla u(x)\approx\sum_{i=1}^n u_i \nabla N_i(x),

we can approximate the integral as an element-wise sum:

JeTheke2i,j=1nuiujNi(x)Nj(x)dx.J \approx \sum_{e \in T_h} \int_e \frac{k_e}{2} \sum_{i,j=1}^n u_i u_j \nabla N_i(x)\cdot\nabla N_j(x)\,dx.

Defining the stiffness matrix

Kij:=eThekeNi(x)Nj(x)dx;\boxed{K_{ij} := \sum_{e \in T_h} \int_e k_e \nabla N_i(x)\cdot\nabla N_j(x)\,dx;}

although initially a O(n2N)O(n^2 N) operation, it is actually much faster to calculate since each NiN_i has a small support, making KijK_{ij} a sparse matrix. We finally find

J12ijKijuiuj=12uTKu\boxed{J \approx \frac 12 \sum_{ij}K_{ij} u_i u_j = \frac 12 u^ T K u}

where the matrices uu and KK are defined naturally.

Hence, our full optimization problem becomes

Minimize12uTKufor uRnsubject toAu=b\boxed{\begin{align*} \text{Minimize} & \quad \frac 12 u^T K u & \text{for } u \in\mathbb R^ n\\ \text{subject to} & \quad Au=b \end{align*}}

This is a properly-defined quadratic optimization problem, which can be naturally tackled via Lagrange multipliers as explained in the first chapter.

The FEM heat equation

Since we did so much work for the heat equation in the continuous case, let us see that we get essentially the same equations for the discretized case.

Write our Lagrangian as

L(u,v,k)=12uTKu+vT(Aub)\mathcal L(u, v, k)=\frac 12 u^T K u + v^T(Au-b)

where we should explicitly calculate the relevant matrices. We defined

Aij=a(Nj,Ni)=ΩkNjNidx.A_{ij} = a(N_j, N_i)= \int_\Omega k \nabla N_j \cdot\nabla N_i\,dx.

Using an element-wise constant kk, this boils down to

Aij=eThkeeNiNjdx.A_{ij} = \sum_{e\in T_h} k_e \int _e \nabla N_i \cdot \nabla N_j\,dx.

Similarly, we defined

bi=l(Ni)=ΩqNidx+ΓNg0Nidsb_i=l(N_i)= \int_\Omega qN_i\,dx+\int_{\Gamma_N} g_0 N_i\,ds

which can also be converted to an element-wise sum over 3D and 2D elements if we choose to represent qq and g0g_0 as element-wise constant.

Finally, we recall that we defined

Kij=eThkeeNi(x)Nj(x)dx.K_{ij}=\sum_{e \in T_h} k_e\int_e \nabla N_i(x)\cdot\nabla N_j(x)\,dx.

Oh, but wait — this is exactly the expression for AijA_{ij} that we found before. Again, the fact that we chose a very specific JJ has helped us, since it gives the nice result

A=K.A = K.

It is trivial to see that taking the derivative L/v=0\partial\mathcal L/\partial v = 0 yields the constraint equation Au=bAu = b (notice that L/v\partial L/\partial v is a row vector, since vv is defined as a column vector). Let us then compute the derivative with respect to uu. Everything is more easily done in index notation, since

L(u,v,k)=ij[12Kijuiuj+vi(Aijujbi)]\mathcal L(u,v,k)=\sum_{ij} \left[ \frac 12 K_{ij} u_i u_j + v_i(A_{ij} u_j - b_i)\right]

Then the ii-th component of the derivative with respect to uu is easily seen to be

(Lu)i=j[Kijuj+Kijvj]\left(\frac{\partial\mathcal L}{\partial u}\right)_i = \sum_j [K_{ij} u_j + K_{ij}v_j]

where we have replaced AijA_{ij} with KijK_{ij}. This can be shifted back to matrix notation:

Lu=Ku+Kv\frac{\partial \mathcal L}{\partial u}=Ku+Kv

which, when set to zero, gives

v=uv = -u

as before!

Finally, we can take the derivative with respect to kk - or, more precisely, against the vector [ke]e=1N[k_e]_{e=1}^N whose number of components is the number of mesh elements.

We have

Lke=12uTKkeu+vTKku\frac{\partial \mathcal L}{\partial k_e}= \frac 12 u^ T \frac{\partial K}{\partial k_e} u +v^T\frac{\partial K}{\partial k} u

Using that v=uv=-u yields

Lke=12uTKkeu.\frac{\partial \mathcal L}{\partial k_e}=-\frac 12 u^T \frac{\partial K}{\partial k_e} u.

Now,

Kij=eThkeeNi(x)Nj(x)dxKijke=eNiNjdxK_{ij}=\sum_{e \in T_h} k_e\int_e \nabla N_i(x)\cdot\nabla N_j(x)\,dx\quad\Rightarrow\quad\frac{\partial K_{ij}}{\partial k_e} = \int_e \nabla N_i\cdot\nabla N_j dx

and the matrix product becomes

12uTKkeu=12ijuiujeNiNjdx=12e(iuiNi)(jujNj)dx=12eu2.\begin{align*} -\frac 12 u^T \frac{\partial K}{\partial k_e} u &= -\frac 12 \sum_{ij} u_i u_j \int_e \nabla N_i \cdot\nabla N_j dx\\ &=-\frac 12 \int_e\nabla (\sum_i u_i N_i)\cdot \nabla(\sum_j u_j N_j)dx\\ &=-\frac 12 \int_e \vert\nabla u\vert^2. \end{align*}

Therefore, we conclude that L/k\partial \mathcal L/\partial k is a NN-dimensional vector (one component per mesh element) whose components are the quantities

e12u2dx\int_e \frac{1}{2}\vert\nabla u\vert^2 dx

for each ee. Convince yourself that this is the discrete analog of the Gateaux derivative

dLdk,η=Ω12u2ηdx\left\langle \frac{d\mathcal L}{d k}, \eta \right\rangle = -\int_\Omega \frac 12 \vert \nabla u\vert^2\eta \,dx

obtained in the continuous case.

Future to-dos

This is getting pretty long as it is, so we will call it a day for now. After motivating the importance of PDE-constrained optimization, we started with Lagrange multipliers in finite dimensions; went up a layer of complexity to study adjoints in the continuous case; and then came back to finite dimensions with the help of FEM. As we saw, the constructions are equivalent in the sense that, when projected to finite dimensions, they yield the same equations.

As next steps:

  • Add FEniCSx implementation of an adjoint
  • Add more examples and exercises, especially with other objective functions

References

  1. Bradley, Andrew M. “PDE-Constrained Optimization and the Adjoint Method.” Published July 7, 2024. Originally November 16, 2010. https://cs.stanford.edu/~ambrad/adjoint_tutorial.pdf.

  2. Guo, Zengyuan, Cheng Xinguang, and Xia Zaizhong. “Least Dissipation Principle of Heat Transport Potential Capacity and Its Application in Heat Conduction Optimization.” Chinese Science Bulletin, vol. 48, no. 4, 2003, pp. 406-410.

  3. Allaire, Grégoire, François Jouve, and Anca-Maria Toader. “Structural Optimization Using Sensitivity Analysis and a Level-Set Method.” Journal of Computational Physics, vol. 194, no. 1, 2004, pp. 363–93. https://doi.org/10.1016/j.jcp.2003.09.032.