# Automated shape differentiation in the Unified Form Language

- 96 Downloads

## Abstract

We discuss automating the calculation of weak shape derivatives in the Unified Form Language (ACM TOMS 40(2):9:1–9:37 2014) by introducing an appropriate additional step in the pullback from physical to reference space that computes Gâteaux derivatives with respect to the coordinate field. We illustrate the ease of use with several examples.

## Keywords

Shape derivatives Finite elements## 1 Introduction

Physical models often involve functionals that assign real values to the solutions of partial differential equations (PDEs). For instance, the compliance of a structure is a function of the solution to the elasticity equations, and the drag of a rigid obstacle immersed in a fluid is a function of the solution to the Navier-Stokes equations.

This type of functional depends on the PDE parameters, and it is often possible to compute the derivative of a functional with respect to a chosen set of parameters. This derivative can in turn be used to perform sensitivity analysis and to run optimization algorithms with respect to parameters in the PDE.

The shape of the physical domain that is part the PDE-model (like the shape of the rigid obstacle mentioned above) is a parameter that is not straightforward to handle. Although we can compute the shape derivative of a functional following shape calculus rules (Delfour and Zolésio 2011), this differentiation exercise is often tedious and error prone. In Schmidt (2018), Schmidt introduces the open-source library FEMorph: an automatic shape differentiation toolbox for the Unified Form Language (UFL, Alnæs et al. 2014). The library FEMorph is based on refactoring UFL expressions and applying shape calculus differentiation rules recursively. It can compute first- and second-order shape derivatives (both in so-called weak and strong form), and it has been successfully employed to solve shape optimization problems (Schmidt et al. 2018).

This article presents an alternative approach to automated shape differentiation. The key idea is to rely solely on pullbacks and standard Gâteaux derivatives. This approach is more generic and robust, because it does not require handling of special cases. In particular, it circumvents the implementation of shape calculus rules, and required only a minor modification of UFL, because UFL supports Gâteaux derivatives with respect to functions. As a result, UFL is now capable of shape differentiating any integral that can be expressed in it.

This article is organized as follows. In Section 2, we revisit shape calculus and describe how to shape differentiate using standard finite element software. In Section 3, we consider three test cases and show how to compute shape derivatives using Firedrake and UFL. In Section 4, we describe code validation of this new UFL feature. In Section 5, we solve a PDE-constrained shape optimization test case with a descent algorithm implemented in Firedrake and UFL. Finally, in Section 6, we summarize the contribution of this article.

## 2 Shape differentiation on the reference element

A *shape functional* is a map \(\mathrm {J}:{\mathcal {U}}\to \mathbb {R}\) defined on the collection of domains \({\mathcal {U}}\) in \(\mathbb {R}^{d}\), where \(d\in \mathbb {N}\) denotes the space dimension.

We follow the perturbation of identity approach (Simon 1980) and for a vector field \({\mathbf {V}}\in W^{1,\infty }({\mathbb {R}^{d}},\mathbb {R}^{d})\), we consider the family of transformations {**P**_{s}}_{s≥ 0} defined by **P**_{s}(**x**) = **x** + *s***V**(**x**) for every \({\mathbf {x}}\in \mathbb {R}^{d}\). Note that **P**_{s} is a diffeomorphism for any sufficiently small \(s\in \mathbb {R}^{+}\).

_{s}: =

**P**

_{s}(Ω) and assume that \({\Omega }_{s}\in {\mathcal {U}}\) for

*s*sufficiently small. The

*shape directional derivative*of J at Ω in direction

**V**is the derivative

We say that J is *shape differentiable in Ω* if the directional derivative dJ(Ω)[**V**] exists for every direction \({\mathbf {V}}\in W^{1,\infty }({\mathbb {R}^{d}},\mathbb {R}^{d})\), and if the associated map \(\mathrm {d\mathrm {J}}({\Omega }): W^{1,\infty }({\mathbb {R}^{d}},\mathbb {R}^{d})\mapsto \mathbb {R}\) is linear and continuous. In this case, the linear operator dJ(Ω) is called the *shape derivative* of J in Ω.

^{1}The subscript

**P**

_{s}highlights the possible dependence of \(u_{{\mathbf {P}}_{s}}\) on the domain Ω

_{s}.

**D**

**P**

_{s}denotes the Jacobian matrix of

**P**

_{s}and \(u_{{\mathbf {P}}_{s}}\circ {\mathbf {P}}_{s}\) denotes the composition of \(u_{{\mathbf {P}}_{s}}\) with

**P**

_{s}, that is, \((u_{{\mathbf {P}}_{s}}\circ {\mathbf {P}}_{s})({\mathbf {x}}) =u_{{\mathbf {P}}_{s}}({\mathbf {P}}_{s}({\mathbf {x}}))\) for every

**x**∈Ω. Note that \(\det ({\mathbf {D}}{\mathbf {P}}_{s})>0\) for

*s*sufficiently small. Then, by linearity of the integral, the shape derivative dJ is given by

*s*at

*s*= 0. The term \(\text{d}_{\text{s}}(u_{{\mathbf {P}}_{s}}\circ {\mathbf {P}}_{s})\) is often called the

*material derivative*(Berggren 2010). Its explicit formula depends on whether the function \(u_{{\mathbf {P}}_{s}}\) does or does not dependent on Ω

_{s}(see Section 3).

*K*

_{i}are non-overlapping. Additionally, let \(\{{\mathbf {F}}_i\}_{i\in \mathcal {I}}\) be a family of diffeomorphisms such that \({\mathbf {F}}_i(\hat {K}) = K_{i}\) for every \(i\in \mathcal {I}\), where \(\hat {K}\) denotes a reference element. This induces a partition \(\{{\mathbf {P}}_{s}(K_{i})\}_{i\in \mathcal {I}}\) of Ω

_{s}. To evaluate (1), standard finite element software rewrites it as

**F**

_{i}, that is, \({\mathbf {F}}_{i}^{-1}({\mathbf {F}}_{i}({\mathbf {x}}))= {\mathbf {x}}\) for every \({\mathbf {x}}\in \hat {K}\), and \({\mathbf {F}}_{i}({\mathbf {F}}_{i}^{-1}({\mathbf {x}}))= {\mathbf {x}}\) for every

**x**∈

*K*

_{i}. Since \({\mathbf {P}}_{s} = ({\mathbf {P}}_{s}\circ {\mathbf {F}}_{i})\circ {\mathbf {F}}_{i}^{-1}\) and

**P**

_{s}∘

**F**

_{i}=

**F**

_{i}+

*s*

**V**∘

**F**

_{i}, we can rewrite (4) as follows:

*s*implies

**V**] by performing the steps necessary for the assembly of J(Ω) and, at the appropriate time, differentiating the maps \(\{g_{i}\}_{i\in \mathcal {I}}\). To be precise, this differentiation corresponds to a standard Gâteaux directional derivative, because the integrand in (6) corresponds to the following limit

*g*

_{i}at

**T**=

**F**

_{i}in the direction

**V**∘

**F**

_{i}(Hinze et al. 2009, Def. 1.29). This viewpoint is important to correctly implement this differentiation step in the existing pipeline in UFL (see Fig. 1). We emphasize that this also enables computing higher order shape derivatives by simply taking higher order Gâteaux derivatives in (6).

### *Remark 1*

Lagrange finite element global basis functions are obtained by gluing local parametric basis functions, that is, basis functions \(\{{b_{m}^{i}}\}_{m\in {}}\) defined only on *K*_{i} and of the form \(b_{m} = \hat {b}_m\circ {\mathbf {F}}_i^{-1}\), where \(\{\hat {b}_m\}_{m\in {}}\) is the set of reference local basis functions, which are defined only on the reference element \(\hat {K}\). If **V** lives in a Lagrange finite element space built on the partitioning \(\{K_{i}\}_{i\in \mathcal {I}}\), it is possible to evaluate dJ(**T**)[**V**] by computing the Gâteaux derivative in (6) in the direction of the reference local basis functions \(\{\hat {b}_m\}_{m\in {}}\) (instead of in the direction **V** ∘**F**_{i}) and summing these values. This allows us to fully rely on the symbolic differentiation capabilities of UFL.

### *Remark 2*

The approach does not rely on the element being affinely mapped, but extends to elements that are mapped using a Piola transform such as the Raviart-Thomas or Nedelec elements. However, the current implementation does not work for elements such as the Hermite element that require different pullbacks for point evaluation and derivative degrees of freedom.

## 3 Examples

In this section, we consider three examples based on (1) that cover most applications. For these examples, we give explicit expressions of dJ using (3) and (6) and show how to compute dJ with the finite element software Firedrake^{2} (Rathgeber et al. 2016). To shorten the notation, we define **V**_{i} : = **V** ∘**F**_{i}.

### *Example 1*

*u*. Then, the chain rule implies that \(\text{d}_{\text{s}}(u_{{\mathbf {P}}_{s}}\circ {\mathbf {P}}_{s})=\text{d}_{\text{s}}(u\circ {\mathbf {P}}_{s})=\nabla u\cdot {\mathbf {V}}\). Recalling \(\text{d}_{\text{s}}(\det {\mathbf {D}}{\mathbf {P}}_{s}) = \text {div}({\mathbf {V}})\), we conclude that (3) becomes

*u*≡ 1, to enforce volume constraints in shape optimization.

### *Example 2*

*V*

_{h}(Ω

_{s}) are of the form \({B_{s}^{i}} = B^{i}\circ {\mathbf {P}}_{s}^{-1}\), where \(\{B^{i}\}_{i}\) are basis functions of

*V*

_{h}(Ω) and \({\mathbf {P}}_{s}^{-1}\) is the inverse of \({\mathbf {P}}_{s}\), that is, \({\mathbf {P}}_{s}^{-1}({\mathbf {P}}_{s}({\mathbf {x}}))= {\mathbf {P}}_{s}({\mathbf {P}}_{s}^{-1}({\mathbf {x}}))={\mathbf {x}}\) for every \({\mathbf {x}}\in \mathbb {R}^{d}\). Let \(v_{{\mathbf {P}}_{s}}\in V_{h}({\Omega }_{s})\) and \(u_{{\mathbf {P}}_{s}}=v_{{\mathbf {P}}_{s}}+\| \nabla v_{{\mathbf {P}}_{s}} \|^{2}\). Since

*v*

_{i}is a linear combination of the local basis functions \(\{\hat {b}_m\}_{m\in {}}\) defined on the reference element \(\hat K\). Therefore,

### *Example 3*

*u*is the solution to (15) for

*s*= 0 and

*p*∈

*V*

_{h}(Ω) is the solution to an adjoint boundary value problem. The shape derivative of \(\mathrm {L}_{s}(u\circ {\mathbf {P}}_{s}^{-1},p\circ {\mathbf {P}}_{s}^{-1})\) can be computed as in example 2. The result is

### *Remark 3*

With appropriate modifications, the same code can be use for functionals constrained to boundary value problems with Neumann or Dirichlet boundary conditions. For the Neumann case, it is sufficient to add the Neumann forcing term in line 6 of Listing 3. For the Dirichlet case, one needs to replace u with u+g in lines 6 and 7 (where g is the function defined in terms of X that describes the Dirichlet boundary condition) and impose homogeneous Dirichlet boundary conditions in lines 8 and 9.

### *Remark 4*

To evaluate the action of the shape Hessian of a PDE-constrained functional, one can follow the instructions given in Hinze et al. (2009, p. 65). Note that by computing shape derivatives as in (6), it is straightforward to combine shape derivatives of \(\mathrm {L}_{s}(u\circ {\mathbf {P}}_{s}^{-1},p\circ {\mathbf {P}}_{s}^{-1})\) with standard Gâteaux derivatives with respect to \(u\circ {\mathbf {P}}_{s}^{-1}\).

## 4 Code validation

_{1}and δ

_{2}for

*s*= 2

^{− 1},2

^{− 2}, \(\dots , 2^{-10}\), and J as in examples 1 and 3 from the previous section (we denote these functionals J

_{1}and J

_{2}respectively). The vector field

**V**is chosen randomly. This experiment clearly displays the asymptotic rates predicted by (17).

We have repeated this numerical experiment for many other test cases, including functionals that are not linear in *u*, functionals given by integrals over *∂*Ω involving the normal **n**, and functionals that are constrained to linear and nonlinear boundary value problems with nonconstant right-hand sides and nonconstant Neumann and Dirichlet boundary conditions. In every instance, we have observed the asymptotic rates predicted by (17). The code for these numerical experiments is available at “Software used in ‘Automated shape differentiation in the Unified Form Language’ ()”.

## 5 Shape optimization of a pipe

In this section, we show how to use Firedrake and the new UFL capability to code a PDE-constrained shape optimization algorithm. As test case, we consider the optimization of a pipe to minimize the dissipation of kinetic energy of the fluid into heat. This example is taken from Schmidt (2010, Sect. 6.2.3). To simplify the exposition, we use a very simple optimization strategy. At the end of the section, we will comment on possible improvements.

*ν*), which flows in from the left and is modeled using the incompressible Navier-Stokes equations. To be precise, let Ω be the shape of the pipe, Γ ⊂

*∂*Ω be the outflow boundary of the pipe (that is, the end of the pipe on the right), and

**u**and

*p*be the velocity and the pressure of the fluid, respectively. Then,

**u**and

*p*satisfy

**g**is given by a Poiseuille flow at the inlet and is zero on the walls of the pipe

### Description of Listing 4

In lines 2–4, we load the finite element mesh pipe.msh and extract the vertex coordinates. This mesh is generated with Gmsh (Geuzaine and Remacle 2009) and is available as part of “Software used in ‘Automated shape differentiation in the Unified Form Language’ (2019)”. Lines 5–8 define the Gramian matrix of the inner product employed to compute descent directions. In lines 9–14, we define the space of P2-P1 Taylor-Hood finite elements, which we use to discretize the weak formulation of the Navier-Stokes equations, and set up the functions containing the solutions to the state and adjoint equation as well as the test functions for the weak form. In lines 15–22, we define the weak formulation of the Navier-Stokes equations and certain parameters to prescribe the use of the MUMPS direct solver (Amestoy et al. 2000) to solve the linearized equations. In lines 23–29, we define the shape functional *J*, the functional describing the volume of the shape, as well as the Lagrangian and its derivative. In particular, note that the shape derivative of the Lagrangian can be computed with the simple command dL = derivative(L, X) in line 28. Without the new automatic shape differentiation capability in UFL, line 28 would have to be replaced with the following formula

**u**are stored in the file u.pvd (which can visualized using Paraview (Ahrens et al. 2005)). Finally, lines 36–46 contain the optimization algorithm: for 100 iterations, we compute the shape derivative and penalize volume changes (lines 38–40), compute the descent direction (lines 41–42), update the domain (line 45), and update the state and adjoint solutions (line 46).

### *Remark 5*

The optimization algorithm of Listing 4 is based on a simple optimization strategy and can be improved in several ways, at the mere cost of adding lines of code. For instance, instead of using a fixed step size and a fixed number of iterations, one could implement an adaptive step-size selection and stopping criteria. Additionally, one could experiment with different inner products to define descent directions (Iglesias et al. 2017), as well as compute second-order derivatives of *J* and implement (Quasi-)Newton methods (Schmidt 2018). Despite the room for improvement, we would like to stress that Listing 4 can be readily used for a 3D problem by simply passing a 3D mesh and changing the inflow boundary condition in line 18.

## 6 Discussion

We have presented a new and equivalent formulation of shape derivatives in the context of finite elements as Gâteaux derivatives on the reference element. While the formulation applies to finite elements in general, we have implemented this new approach in UFL due to its extensive support for symbolic calculations. This new UFL capability allows computing shape derivatives of functionals that are defined as volume or boundary integrals, and that are constrained to linear and nonlinear PDEs. During shape differentiation, our code treats finite element functions and global functions differently. This behavior is correct and necessary to handle PDE-constraints properly. In combination with a finite element software package, such as FEniCS or Firedrake, that takes as input UFL, this enables the entirely automated shape differentiation of functionals subject to boundary value problems. This notably simplifies tackling PDE-constrained shape optimization problems.

Compared to the existing shape differentiation toolbox FEMorph, our code does not compute shape derivatives in strong form because it neither relies on shape calculus differentiation rules nor performs integration by parts. However, in practice, we do not consider this a limitation as it has been shown in Hiptmair et al. (2015), Berggren (2010), and Zhu (2018) that the weak form is superior when the state and the adjoint equations are discretized by finite elements.

## 7 Replication of results

The code for the numerical experiments is available at Software used in ‘Automated shape differentiation in the Unified Form Language’ (2019).

## Footnotes

## Notes

### Funding information

DAH is supported by the Natural Environment Research Council (grant no. NE/K008951/1). LM is supported by the Engineering and Physical Sciences Research Council (grant no. EP/L000407/1). FW is supported by the EPSRC Centre For Doctoral Training in Industrially Focused Mathematical Modelling (grant no. EP/L015803/1).

### Compliance with ethical standards

### **Conflict of interest**

The authors declare that they have no conflict of interest.

## References

- Ahrens J, Geveci B, Law C (2005) Paraview: an end-user tool for large data visualization. The visualization handbook, 717Google Scholar
- Alnæs MS, Logg A, Ølgaard KB, Rognes ME, Wells GN (2014) Unified form language: a domain-specific language for weak formulations of partial differential equations. ACM Trans Math Softw 40(2):9:1–9,37. https://doi.org/10.1145/2566630 MathSciNetCrossRefzbMATHGoogle Scholar
- Amestoy PR, Duff IS, L’Excellent JY, Koster J (2000) Mumps: a general purpose distributed memory sparse solver. In: International workshop on applied parallel computing. Springer, pp 121–130Google Scholar
- Berggren M (2010) A unified discrete-continuous sensitivity analysis method for shape optimization. In: Applied and numerical partial differential equations, Comput. Methods Appl. Sci, vol 15. Springer, New York, pp 25–39. https://doi.org/10.1007/978-90-481-3239-3_4
- Delfour MC, Zolésio JP (2011) Shapes and geometries. Metrics, analysis, differential calculus, and optimization advances in design and control, vol 22, 2nd edn. Society for Industrial and Applied Mathematics (SIAM), Philadelphia. https://doi.org/10.1137/1.9780898719826
- Geuzaine C, Remacle JF (2009) Gmsh: a 3-D finite element mesh generator with built-in pre-and post-processing facilities. Int J Numer Methods Eng 79(11):1309–1331MathSciNetCrossRefzbMATHGoogle Scholar
- Hintermüller M, Ring W (2004) A second order shape optimization approach for image segmentation. SIAM J Appl Math 64(2):442–467. https://doi.org/10.1137/S0036139902403901 MathSciNetCrossRefzbMATHGoogle Scholar
- Hinze M, Pinnau R, Ulbrich M, Ulbrich S (2009) Optimization with PDE constraints mathematical modelling: theory and applications, vol 23. Springer, New YorkGoogle Scholar
- Hiptmair R, Paganini A, Sargheini S (2015) Comparison of approximate shape gradients. BIT 55 (2):459–485. https://doi.org/10.1007/s10543-014-0515-z MathSciNetCrossRefzbMATHGoogle Scholar
- Iglesias JA, Sturm K, Wechsung F (2017) Two-dimensional shape optimization with nearly conformal transformations. SIAM J Sci Comput 40(6):A3807–A3830. https://epubs.siam.org/doi/abs/10.1137/17M1152711 MathSciNetCrossRefzbMATHGoogle Scholar
- Logg A, Wells GN (2010) DOLFIN: automated finite element computing. ACM Trans Math Softw 37 (2):20:1–20,28. https://doi.org/10.1145/1731022.1731030 MathSciNetCrossRefzbMATHGoogle Scholar
- Logg A, Mardal KA, Wells GN (eds) (2012) Automated solution of differential equations by the finite element method: the FEniCS book. SpringerGoogle Scholar
- Rathgeber F, Ham DA, Mitchell L, Lange M, Luporini F, McRae ATT, Bercea GT, Markall GR, Kelly PHJ (2016) Firedrake: automating the finite element method by composing abstractions. ACM Trans Math Softw 43(3):24:1–24,27. https://doi.org/10.1145/2998441 MathSciNetCrossRefzbMATHGoogle Scholar
- Schmidt S (2010) Efficient large scale aerodynamic design based on shape calculus. Ph.D. thesis, Universität TrierGoogle Scholar
- Schmidt S (2018) Weak and strong form shape Hessians and their automatic generation. SIAM J Sci Comput 40(2):C210–C233. https://doi.org/10.1137/16M1099972. Software freely available at https://bitbucket.org/Epoxid/femorph MathSciNetCrossRefzbMATHGoogle Scholar
- Schmidt S, Schütte M, Walther A (2018) Efficient numerical solution of geometric inverse problems involving Maxwell’s equations using shape derivatives and automatic code generation. SIAM J Sci Comput 40(2):B405–B428. https://doi.org/10.1137/16M110602X MathSciNetCrossRefzbMATHGoogle Scholar
- Simon J (1980) Differentiation with respect to the domain in boundary value problems. Numer Funct Anal Optim 2(7-8):649–687. https://doi.org/10.1080/01630563.1980.10120631 MathSciNetCrossRefzbMATHGoogle Scholar
- Software used in ‘Automated shape differentiation in the Unified Form Language’ (2019). https://doi.org/10.5281/zenodo.2621254
- Zhu S (2018) Effective shape optimization of laplace eigenvalue problems using domain expressions of Eulerian derivatives. J Optim Theory Appl 176(1):17–34. https://doi.org/10.1007/s10957-017-1198-9 MathSciNetCrossRefzbMATHGoogle Scholar

## Copyright information

**Open Access** This article is distributed under the terms of the Creative Commons Attribution 4.0 International License (http://creativecommons.org/licenses/by/4.0/), which permits unrestricted use, distribution, and reproduction in any medium, provided you give appropriate credit to the original author(s) and the source, provide a link to the Creative Commons license, and indicate if changes were made.