25
Noname manuscript No. (will be inserted by the editor) Fully and Semi-Automated Shape Differentiation in NGSolve Peter Gangl · Kevin Sturm · Michael Neunteufel · Joachim Sch¨ oberl Received: date / Accepted: date Abstract In this paper we present a framework for auto- mated shape differentiation in the finite element software NGSolve. Our approach combines the mathematical Lagrangian approach for differentiating PDE constrained shape func- tions with the automated differentiation capabilities of NGSolve. The user can decide which degree of automatisation is re- quired, thus allowing for either a more custom-like or black- box-like behaviour of the software. We discuss the auto- matic generation of first and second order shape derivatives for unconstrained model problems as well as for more re- alistic problems that are constrained by different types of partial differential equations. We consider linear as well as nonlinear problems and also problems which are posed on surfaces. In numerical experiments we verify the accuracy of the computed derivatives via a Taylor test. Finally we present first and second order shape optimisation algorithms and illustrate them for several numerical optimisation exam- ples ranging from nonlinear elasticity to Maxwell’s equa- tions. Keywords shape optimisation, shape derivative, automated differentiation, shape Newton method P. Gangl (corresponding author) TU Graz, Steyrergasse 30, 8010 Graz, Austria E-mail: gangl(at)math.tugraz.at K. Sturm TU Wien, Wiedner Hauptstr. 8-10, 1040 Vienna E-mail: kevin.sturm(at)tuwien.ac.at M. Neunteufel TU Wien, Wiedner Hauptstr. 8-10, 1040 Vienna E-mail: michael.neunteufel(at)tuwien.ac.at J. Sch ¨ oberl TU Wien, Wiedner Hauptstr. 8-10, 1040 Vienna E-mail: joachim.schoeberl(at)tuwien.ac.at 1 Introduction Numerical simulation and shape optimisation tools to solve the problems have become an integral part in the design process of many products. Starting out from an initial de- sign, non-parametric shape optimisation techniques based on first and second order shape derivatives can assist in find- ing shapes of a product which are optimal with respect to a given objective function. Examples include the optimal de- sign of aircrafts [37, 38], optimal inductor design [23], opti- misation of microlenses [32], the optimal design of electric motors [16], applications to mechanical engineering [4, 27], multiphysics problems [15] or electrical impedance tomog- raphy (EIT) in medical sciences to name only a few [20]. Shape optimisation algorithms are based on the concept of shape derivatives. Let P(R d ) denote the set of all subsets of R d . Further let A P(R d ) be a set of admissible shapes and J : A R be a shape function. Given an admissible shape Ω A and a sufficiently smooth vector field V , we define the perturbed domain Ω t :=(Id + tV )(Ω ) for a small perturbation parameter t > 0. The shape derivative is defined as DJ (Ω )( V ) := d dt J (Ω t ) t =0 = lim t &0 J (Ω t ) - J (Ω ) t . (1) Remark 1 We remark that a frequently used definition of shape differentiability is to require the mapping V 7J ((Id + V )(Ω )) being Fr´ echet differentiable in V = 0; see [1, 19, 29]. This stronger notion of differentiability implies that the limit defined in (1) exists. In most practically relevant applications, the objective functional depends on the shape of a (sub-)domain via the solution to a partial differential equation (PDE). Thus, one is facing a problem of PDE-constrained shape optimisation arXiv:2004.06783v2 [math.OC] 23 Oct 2020

Fully and Semi-Automated Shape Differentiation in NGSolve · 6 P. Gangl, J. Schöberl, K. Sturm and M. Neunteufel 2.2 Automatic Differentiation in NGSolve In NGSolve, symbolic expressions

  • Upload
    others

  • View
    0

  • Download
    0

Embed Size (px)

Citation preview

  • Noname manuscript No.(will be inserted by the editor)

    Fully and Semi-Automated Shape Differentiation in NGSolve

    Peter Gangl · Kevin Sturm · Michael Neunteufel · Joachim Schöberl

    Received: date / Accepted: date

    Abstract In this paper we present a framework for auto-mated shape differentiation in the finite element softwareNGSolve. Our approach combines the mathematical Lagrangianapproach for differentiating PDE constrained shape func-tions with the automated differentiation capabilities of NGSolve.The user can decide which degree of automatisation is re-quired, thus allowing for either a more custom-like or black-box-like behaviour of the software. We discuss the auto-matic generation of first and second order shape derivativesfor unconstrained model problems as well as for more re-alistic problems that are constrained by different types ofpartial differential equations. We consider linear as well asnonlinear problems and also problems which are posed onsurfaces. In numerical experiments we verify the accuracyof the computed derivatives via a Taylor test. Finally wepresent first and second order shape optimisation algorithmsand illustrate them for several numerical optimisation exam-ples ranging from nonlinear elasticity to Maxwell’s equa-tions.

    Keywords shape optimisation, shape derivative, automateddifferentiation, shape Newton method

    P. Gangl (corresponding author)TU Graz, Steyrergasse 30, 8010 Graz, AustriaE-mail: gangl(at)math.tugraz.at

    K. SturmTU Wien, Wiedner Hauptstr. 8-10, 1040 ViennaE-mail: kevin.sturm(at)tuwien.ac.at

    M. NeunteufelTU Wien, Wiedner Hauptstr. 8-10, 1040 ViennaE-mail: michael.neunteufel(at)tuwien.ac.at

    J. SchöberlTU Wien, Wiedner Hauptstr. 8-10, 1040 ViennaE-mail: joachim.schoeberl(at)tuwien.ac.at

    1 Introduction

    Numerical simulation and shape optimisation tools to solvethe problems have become an integral part in the designprocess of many products. Starting out from an initial de-sign, non-parametric shape optimisation techniques basedon first and second order shape derivatives can assist in find-ing shapes of a product which are optimal with respect to agiven objective function. Examples include the optimal de-sign of aircrafts [37, 38], optimal inductor design [23], opti-misation of microlenses [32], the optimal design of electricmotors [16], applications to mechanical engineering [4, 27],multiphysics problems [15] or electrical impedance tomog-raphy (EIT) in medical sciences to name only a few [20].

    Shape optimisation algorithms are based on the conceptof shape derivatives. Let P(Rd) denote the set of all subsetsof Rd . Further let A ⊂P(Rd) be a set of admissible shapesand J : A → R be a shape function. Given an admissibleshape Ω ∈ A and a sufficiently smooth vector field V , wedefine the perturbed domain Ωt := (Id+ tV )(Ω) for a smallperturbation parameter t > 0. The shape derivative is definedas

    DJ (Ω)(V ) :=(

    ddt

    J (Ωt))∣∣∣∣

    t=0= lim

    t↘0

    J (Ωt)−J (Ω)t

    .

    (1)

    Remark 1 We remark that a frequently used definition ofshape differentiability is to require the mapping V 7→ J((Id+V )(Ω)) being Fréchet differentiable in V = 0; see [1,19,29].This stronger notion of differentiability implies that the limitdefined in (1) exists.

    In most practically relevant applications, the objectivefunctional depends on the shape of a (sub-)domain via thesolution to a partial differential equation (PDE). Thus, oneis facing a problem of PDE-constrained shape optimisation

    arX

    iv:2

    004.

    0678

    3v2

    [m

    ath.

    OC

    ] 2

    3 O

    ct 2

    020

  • 2 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    of the form

    min(Ω ,u)∈A×Y

    J(Ω ,u)

    s.t. (Ω ,u) ∈A ×Y : e(Ω ;u,v) = 0 for all v ∈ Y.(2)

    Here, the second line represents the constraining boundaryvalue problem posed on a Hilbert space Y , which we assumeto be uniquely solvable for all admissible Ω ∈A . Denotingthe unique solution for a given Ω ∈A by uΩ , we introducethe notation for the reduced functional

    J (Ω) := J(Ω ,uΩ ).

    In order to be able to apply a shape optimisation algorithmto a given problem of this kind, the shape derivative (1) hasto be computed, see the standard literature [9, 42] or [44]for an overview of different approaches. In the followingwe focus on computing the so-called volume form of theshape derivative which in a finite element context is knownto give a better approximation compared to the boundaryform; see [6, 22].

    The convergence of shape optimisation algorithms canbe speeded up by using second order shape derivatives. Giventwo sufficiently smooth vector fields V , W and an admissi-ble shape Ω ∈A , let Ωs,t := (Id+ sV + tW )(Ω) be the per-turbed domain. Then, the second order shape derivative isdefined as

    D2J (Ω)(V )(W ) :=(

    d2

    dsdtJ (Ωs,t)

    )∣∣∣∣s,t=0

    . (3)

    Second order information in Newton-type algorithms hasbeen explored in the articles [2, 13, 31, 33, 40]. Since thecomputation of second order shape derivatives is more in-volved and error prone, several authors have employed au-tomatic differentiation (AD) tools, see e.g. [36] and [18]for two approaches based on the Unified Form Language(UFL) [5]. In [18], the authors present a fully automatedshape differentiation software which uses the transforma-tion properties on the finite element level. In [36] (see alsothe earlier work [35]) the automated derivatives are com-puted using UFL. The strategies of [18] and [36] differ inthat, for the latter, the software computes an unsymmetricshape Hessian since it involves the term DJ (Ω)(∂VW ).Optionally the software allows to make the shape Hessiansymmetric by requiring ∂VW = 0. We will discuss the sub-tle difference and the relation between the two possible waysof defining shape Hessians in Remark 3 of Section 3.2. Letus also mention [12] where automated shape derivatives fortransient PDEs in FEniCS and Firedrake are presented.

    In this paper we present an alternative framework for ADof PDE constrained problems of type (2). There exist severalapproaches for the rigorous derivation of the shape deriva-tive of PDE-constrained shape functionals, see [45] for anoverview. The main idea, however, is always similar. After

    transforming the perturbed setting back to the original do-main, shape differentiation in the direction of a given vec-tor field reduces to the differentiation with respect to thescalar parameter t which now enters via the correspondingtransformation and its gradient. It is shown in [44] that theshape derivative for a nonlinear PDE-constrained shape op-timisation problem can be computed as the derivative of theLagrangian with respect to the perturbation parameter. Wewill illustrate this systematic procedure for a number of dif-ferent applications and utilise symbolic differentiation pro-vided by the finite element software package NGSolve [39]to obtain the shape derivative for different classes of PDE-constrained optimisation problems. NGSolve allows for thefast and efficient numerical solution of a large number ofdifferent boundary value problems. The aim of this paper isto extend NGSolve by the possibility of semi-automatic andfully automatic shape differentiation and optimisation.

    Distinctly from previous approaches we cover the fol-lowing two points:

    – a fully automated setting requiring as input the weak for-mulation of the constraint and the cost function,

    – a semi-automated setting which offers a highly customis-able user interface, but requires mathematical backgroundknowledge.

    Structure of the paper. In Section 2 we give a brief introduc-tion on how to solve a PDE in NGSolve and present its built-in auto-differentiation capabilities. The introduced syntaxwill also lay the foundation for the following sections. InSection 3 we present a first unconstrained shape optimisa-tion problem and show how to solve it in NGSolve. Forthis purpose we show how to compute the first and secondorder shape derivative in a semi-automated way. Section 4extends the preceding section by incorporating a PDE con-straint. The strategy is illustrated by means of a simple Pois-son equation. We also show how to treat the computationof shape derivatives when the PDE is defined on surfaces.While the semi-automated shape differentiation presented inSections 3 and 4 requires mathematical background knowl-edge, in Section 5 we show how the shape derivatives canbe computed in a fully automated fashion. In the last sectionof the paper we verify the computed formulas by a Taylortest, discuss optimisation algorithms and present several nu-merical optimisation examples including nonlinear elastic-ity, Maxwell’s equations and Helmholtz’s equation.

    2 A brief introduction to NGSolve

    In this section, we give a brief overview of the main con-cepts of the finite element software NGSolve [39]. We firstdescribe the main principles for numerically solving bound-ary value problems in NGSolve before focusing on its built-in automatic differentiation capabilities. In the subsequent

  • Fully and Semi-Automated Shape Differentiation in NGSolve 3

    sections of this paper, these ingredients will be combined toimplement the shape derivative of unconstrained and PDE-constrained shape optimisation problems in an automatedway.

    2.1 Solving PDEs with finite elements in NGSolve

    In this section, we illustrate the syntax of NGSolve usingthe python programming language for the Poisson equationwith homogeneous Dirichlet conditions as a model problem.We refer the reader to the online documentation

    https://ngsolve.org/docu/latest/

    for a more detailed description of the many features of thispackage.

    Given a domain Ω ⊂ Rd and a right hand side f , weconsider the model problem to find u satisfying

    −∆u = f in Ω ,u = 0 on ∂Ω .

    The weak form of the model problem reads

    Find u∈H10 (Ω) :∫

    Ω∇u ·∇w dx=

    ∫Ω

    f w dx ∀w∈H10 (Ω).

    (4)

    We consider a ball of radius 12 in two space dimensions cen-tered at the point (0.5,0.5)>, i.e. Ω = B((0.5,0.5)>,0.5),and the right hand side is defined by f (x1,x2) = 2x2(1−x2)+2x1(1− x1). We will go through the steps for numeri-cally solving this problem by the finite element method.

    We begin by importing the necessary functionalities andsetting up a finite element mesh.

    1 from n g s o l v e i m p o r t *2 from n e t g e n . geom2d i m p o r t Sp l ineGeomet ry3

    4 geo = Sp l ineGeomet ry ( )5 geo . AddCi rc l e ( ( 0 . 5 , 0 . 5 ) , 0 . 5 , bc=” c i r c l e ” )6

    7 mesh = Mesh ( geo . Genera teMesh ( maxh = 0 . 2 ) )8 mesh . Curve ( 3 )

    The first line imports all modules from the package NGSolve.The second line includes the SplineGeometry function whichenables us to define a mesh via a geometric description, inour case a circle centered at (0.5,0.5)> of radius 0.5. Fi-nally the mesh is generated in line 7 and in line 8 we spec-ify that we want to use a curved finite element mesh for amore accurate approximation of the geometry. For that pur-pose, a projection-based interpolation procedure is used, seee.g. [11].

    Next in line 9 we define an H1 conforming finite elementspace of polynomial degree 3 and include Dirichlet bound-ary conditions on the boundary of the domain ∂Ω (refer-enced by the string ‘‘circle’’ that we assigned in line 5).

    On this space we define a trial function u in line 11 and atest function w in line 12. These are purely symbolic objectswhich are used to define boundary value problems in weakform.

    9 f e s = H1 ( mesh , o r d e r =3 , d i r i c h l e t =” c i r c l e ” )10

    11 u = f e s . T r i a l F u n c t i o n ( )12 w = f e s . T e s t F u n c t i o n ( )

    For a more compact presentation later on, we define a coef-ficient function X which combines the three spatial compo-nents:

    13 X = C o e f f i c i e n t F u n c t i o n ( ( x , y , z ) )

    Now, the left and right hand sides of problem (4) can be con-veniently defined as a bilinear or linear form, respectively,on the finite element space fes by the following lines.

    14 L = LinearForm ( f e s )15 f1 = (2*X[ 1 ] * ( 1 −X[ 1 ] ) +2*X[ 0 ] * ( 1 −X[ 0 ] ) )16 L += f1 * w * dx17

    18 a = B i l i n e a r F o r m ( f e s , symmet r i c =True )19 a += grad ( u ) * g rad (w) *dx

    We assemble the system matrix coming from the bilinearform a and the load vector coming from L and solve thecorresponding system of linear equations.

    20 a . Assemble ( )21 L . Assemble ( )22

    23 gfu = G r i d F u n c t i o n ( f e s )24 gfu . vec . d a t a = a . mat . I n v e r s e ( f e s . F reeDofs ( ) ,

    i n v e r s e =” s p a r s e c h o l e s k y ” ) * L . vec25

    26 Draw ( gfu , mesh , ” s t a t e ” )

    Here, gfu is defined as a GridFunction over the finite el-ement space fes. A GridFunction object is used to savethe results by containing the corresponding finite elementcoefficient vectors. Further, it can evaluate the stored finiteelement solution at a given mesh point. The Dirichlet con-ditions are incorporated into the direct solution of the linearsystem and the numerical solution is drawn in the graphi-cal user interface. The numerical solution is depicted in Fig-ure 1.

    2.2 Automatic Differentiation in NGSolve

    In NGSolve, symbolic expressions are stored in expressiontrees, see Figure 2 for an example. It is possible to differ-entiate an expression expr with respect to a variable var ap-pearing in expr into a direction dir by the command

    expr.Diff(var, dir).

    Mathematically this line corresponds to the directional deriva-tive of g:=expr at x := var in direction v := dir, that is,

    Dg(x)(v). (5)

    https://ngsolve.org/docu/latest/

  • 4 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    Fig. 1 Solution of problem (4) by code fragments of Section 2.1 with29 nodes, 40 (curved) triangular elements and polynomial order 3.

    When calling the Diff command for expr, the expressiontree of expr is gone through node by node, and for each nodethe corresponding differentiation rules such as product ruleor chain rule are applied. When a node represents the vari-able with respect to which the differentiation is carried out,it is replaced by the direction dir of differentiation.

    Figure 2 shows the differentiation of the expression expr=2x*x+3y with respect to x into the direction given by v:

    27 v = P a r a m e t e r ( 1 )28 exp r = 2*x*x+3*y29 dexpr = exp r . D i f f ( x , v )30 p r i n t ( exp r )31 p r i n t ( dexpr )

    The output of print(expr) reads

    coef binary operation ’+’, real

    coef binary operation ’*’, real

    coef scale 2, real

    coef coordinate x, real

    coef coordinate x, real

    coef scale 3, real

    coef coordinate y, real

    which translates to 2x∗x+3y and corresponds to the expres-sion tree depicted in Figure 2(a). The output of print(dexpr)reads

    coef binary operation ’+’, real

    coef binary operation ’+’, real

    coef binary operation ’*’, real

    coef scale 2, real

    coef N5ngfem28ParameterCoefficientFunctionE, real

    coef coordinate x, real

    coef binary operation ’*’, real

    coef scale 2, real

    coef coordinate x, real

    coef N5ngfem28ParameterCoefficientFunctionE, real

    coef scale 3, real

    coef 0, real

    which translates to (2v∗x+2x∗v)+3∗0 and corresponds tothe expression tree depicted in Figure 2(b). The coefficient

    N5ngfem28ParameterCoefficientFunctionE

    +

    3y*

    2x x

    +

    3*0+

    *

    2v x

    *

    2x v

    (a) (b)

    Fig. 2 Illustration of Diff command for example expr= 2x*x+3y. (a)Expression tree for expr. (b) Expression tree for expression obtainedby call of expr.Diff(x, v).

    appearing therein is the C++ internal class name of the Pythonobject Parameter.

    NGSolve trial and test functions are purely symbolic ob-jects used for defining bilinear and linear forms. Therefore,they do not depend on the spatial variables x, y, z as canbe seen by differentiating them. NGSolve GridFunctionson the other hand represent functions in the finite elementspace. However, also for these objects, the space dependencyis omitted when performing symbolic differentiation. Thecode segments

    32 u = f e s . T r i a l F u n c t i o n ( ) # s y m b o l i c o b j e c t33 w = f e s . T e s t F u n c t i o n ( ) # s y m b o l i c o b j e c t34 gf = G r i d F u n c t i o n ( f e s )35 gf . S e t ( x*x*y )36

    37 p r i n t ( ” D i f f u w. r . t . x ” , u . D i f f ( x ) )38 p r i n t ( ” D i f f w w. r . t . x ” , w. D i f f ( x ) )39 p r i n t ( ” D i f f g f w. r . t . x ” , g f . D i f f ( x ) )

    will give the following output:

    Diff u w.r.t. x: ConstantCF, val = 0

    Diff w w.r.t. x: ConstantCF, val = 0

    Diff gf w.r.t. x: ConstantCF, val = 0

    Here, the GridFunction.Setmethod takes a CoefficientFunctionobject and performs a (local) L2 best-approximation into theunderlying finite element space with respect to its naturalnorm and stores the resulting coefficient vector.

    3 Semi-automatic shape differentiation withoutconstraints

    We will illustrate the steps to be taken in order to obtainthe shape derivative of a shape function in a semi-automaticway for a simple shape optimisation problem. For Ω ⊂ Rdbounded and open and a continuously differentiable func-tion f ∈ C1(Rd), we consider the shape differentiation ofthe shape function

    J (Ω) =∫

    Ωf (x)dx. (6)

  • Fully and Semi-Automated Shape Differentiation in NGSolve 5

    Clearly the minimiser of J over all measurable sets in Rdis given by Ω ∗ = {x ∈Rd : f (x)< 0}. We also refer to [34]for the computations of first and second order variations offunctions of type (6) where Ω is a submanifold of Rd .

    3.1 First order shape derivative

    Henceforth we denote by C0,1(Rd)d the space of boundedand Lipschitz continuous vector fields V : Rd→Rd . In viewof Rademachers’ theorem [14, Thm.6, p.296] the space C0,1(Rd)dcorresponds to the Sobolev space W 1,∞(Rd)d .

    Given a vector field V ∈C0,1(Rd)d , we define the trans-formation

    Tt(x) := (Id+ t V )(x), x ∈ Rd , t ≥ 0.

    Definition 1 The first order shape derivative of a shape func-tion J at Ω in direction V ∈C0,1(Rd)d is defined by

    DJ (Ω)(V ) = limt↘0

    J (Tt(Ω))−J (Ω)t

    . (7)

    3.1.1 Shape differentiation of unconstrained volumeintegrals

    Using the transformation y = Tt(x) and the notation Ft :=∂Tt = I + t∂V for the Jacobian of the transformation Tt , weget for J as in (6),

    J (Ωt) =∫

    Ωtf (x′) dx′ =

    ∫Ω( f ◦Tt)(x)det(Ft(x))dx. (8)

    Now let us explain how to compute the shape derivativeof J . Denoting

    G(Tt ,Ft) :=∫

    Ω( f ◦Tt)(x)det(Ft(x))dx, (9)

    the chain rule gives (formally)

    ddt

    J (Ωt)∣∣∣∣t=0

    =ddt

    G(Tt ,Ft)∣∣∣∣t=0

    =

    (dGdTt

    dTtdt

    +dGdFt

    dFtdt

    )∣∣∣∣t=0

    . (10)

    Using that dTtdt (x) =V (x) anddFtdt (x) = ∂V (x), we get for the

    shape derivative

    DJ (Ω)(V ) =ddt

    J (Ωt)∣∣∣∣t=0

    =

    (dGdTt

    V +dGdFt

    ∂V)∣∣∣∣

    t=0.

    This is the form we use for defining the first order shapederivative in NGSolve. Note that a Lipschitz vector fieldis differentiable almost everywhere and hence ∂V (x) is de-fined almost everywhere and bounded.

    Given the function f (x1,x2) = (x1 − 0.5)2/a2 + (x2 −0.5)2/b2−R2 with a = 1.3, b = 1/a and R = 0.5, we im-plement the transformed cost function (8) as follows:

    40 f = ( (X[ 0 ] − 0 . 5 ) / 1 . 3 ) * * 2 + ( 1 . 3 * (X[ 1 ] − 0 . 5 ) ) **2 −0 .5**2

    41

    42 F = Id ( 2 ) # s y m b o l i c i d e n t i t ym a t r i x

    43 G f = f * Det ( F ) * dx # F on ly a c t s a s adummy v a r i a b l e

    Here, we introduce the symbol F and assign to it the value ofthe identity matrix in line 42. This allows us to differentiatewith respect to F. Then we define the function G of (9) inline 43. The shape derivative is a bounded linear functionalon a space of vector fields. We introduce a vector-valued fi-nite element space VEC and define the object representing theshape derivative dJOmega f as a linear functional on VEC. Inline 48, we differentiate with respect to the spatial variablesin the direction given by V. Note that X is the coefficientfunction we introduced in line 13. In line 49, we deal withthe differentiation with respect to F.

    Remark 2 Defining ξt := det(Ft) and using ddt ξt |t=0 = divV ,it holds

    dGdFt

    dFtdt

    ∣∣∣∣t=0

    =dGdξt

    dξtdFt

    dFtdt

    ∣∣∣∣t=0

    =dGdξt

    dξtdt

    ∣∣∣∣t=0

    =dGdξt

    divV∣∣∣∣t=0

    =∫

    Ωf divV dx.

    Therefore, we obtain for the first order shape derivative thewell-known formula

    DJ (Ω)(V ) =∫

    Ω∇ f ·V + f divV dx. (11)

    Finally if Ω is smooth enough (for instance C1), it followsby integration by parts in (11) that the shape derivative isgiven by

    DJ (Ω)(V ) =∫

    ∂ΩfV ·n ds, (12)

    where n denotes the outward pointing normal along ∂Ω .

    44 VEC = VectorH1 ( mesh , o r d e r =1 , d i r i c h l e t =” ” ) #v e c t o r i a l FE s p a c e o f o r d e r 1

    45 V = VEC. T e s t F u n c t i o n ( )46

    47 dJOmega f = LinearForm (VEC)48 dJOmega f += G f . D i f f (X, V)49 dJOmega f += G f . D i f f ( F , g r ad (V) )

    3.1.2 Shape differentiation of unconstrained boundaryintegrals

    For Ω and f as in the previous section we consider

    Jbnd(Ω) =∫

    ∂Ωf (x) dx. (13)

  • 6 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    Then we get

    Jbnd(Ωt) =∫

    ∂Ωtf (x′) dsx′ (14)

    =∫

    ∂Ω( f ◦Tt)(x)det(Ft(x))|Ft(x)−>n(x)|dsx,

    (15)

    see e.g. [42, Prop. 2.47], with the outer unit normal vectorn and | · | denoting the Euclidean norm. It is shown in [42,Prop. 2.50] that the shape derivative of (13) is given by

    DJbnd(Ω)(V ) =∫

    ∂Ω∇ f ·V + f (divV −n>∂V n)dsx.

    Again, we can compute the shape derivative in NGSolveas the total derivative of expression (15) with respect to theparameter t. In NGSolve, the only difference lies in the ne-cessity to use the trace of the gradient of a test vector fieldV.

    50 G f bnd = f * Det ( F ) * Norm ( Inv ( F ) . t r a n s *s p e c i a l c f . normal ( 2 ) ) * ds

    51

    52 dJOmega f bnd = LinearForm (VEC)53 dJOmega f bnd += G f bnd . D i f f (X, V)

    # no t r a c e needed54 dJOmega f bnd += G f bnd . D i f f ( F , g r ad (V) . Trace ( ) )

    # t r a c e needed

    Note that the trace operator for gradients on the bound-ary is obligatory in NGSolve, whereas for direct evaluationof H1 trial and test functions itself it is optional.

    3.2 Second order shape derivatives

    For second order shape derivatives, we consider perturba-tions of the form

    Ts,t(x) = (Id+ sV + tW )(x), x ∈ Rd ,

    for s, t ≥ 0 and define Ωs,t := Ts,t(Ω).

    Definition 2 The second order shape derivative of a shapefunction J at Ω in direction (V,W )∈C0,1(Rd)d×C0,1(Rd)dis defined by

    D2J (Ω)(V )(W ) =d2

    dsdtJ (Ωs,t)

    ∣∣∣∣s=t=0

    . (16)

    Remark 3 We remark that if J is smooth enough, the sec-ond order derivative as defined in (16) is symmetric by defi-nition:

    D2J (Ω)(V )(W ) = D2J (Ω)(W )(V ). (17)

    We stress that this derivative is not the same as the shapederivative obtained by repeated shape differentiation, that is,it does not coincide with (see, e.g., [10, Chap. 9, Sec. 6])

    d2J (Ω)(V )(W ) := limt↘0

    DJ (TWt (Ω))(V )−DJ (Ω)(V )t

    (18)

    which is in general asymmetric.The derivative defined in (18) is only symmetric if ∂VW =

    0 since it holds

    d2J (Ω)(V )(W ) = D2J (Ω)(V )(W )+DJ (Ω)(∂VW ),(19)

    see also the early work of Simon [41] on this topic. How-ever, in NGSolve, when repeating the shape differentiationprocedure introduced in Section 3.1, we compute directlythe second order shape derivative as defined in (16). Here,we exploit the fact that trial functions are independent ofthe spatial coordinates, see also Section 2.2 and the examplebelow.

    Let us now exemplify the computation of the second or-der shape derivative for the shape function J defined in (6).Similarly to the computations of the first derivative, we usethe notation Fs,t := ∂Ts,t = I + s∂V + t∂W . Then we get

    d2

    dsdtJ (Ωs,t)

    ∣∣∣∣s=t=0

    =d2

    dsdt

    ∫Ωs,t

    f (x)dx∣∣∣∣s=t=0

    =d2

    dsdt

    ∫Ω( f ◦Ts,t)(x)det(Fs,t(x))dx

    ∣∣∣∣s=t=0

    .

    Again, using the notation

    G(Ts,t ,Fs,t) =∫

    Ω( f ◦Ts,t)(x)det(Fs,t(x))dx,

    we get

    d2

    dsdtJ (Ωs,t)

    ∣∣∣∣s=t=0

    =d2

    dsdtG(Ts,t ,Fs,t)

    ∣∣∣∣s=t=0

    =dds

    (dG

    dTs,t

    dTs,tdt

    +dG

    dFs,t

    dFs,tdt

    )∣∣∣∣s=t=0

    .

    Using that d2Ts,t

    dsdt = 0 andd2Fs,tdsdt = 0, we get further

    d2

    dsdtJ (Ωs,t)

    ∣∣∣∣s=t=0

    =dds

    (dG

    dTs,t

    )dTs,tdt

    +dds

    (dG

    dFs,t

    )dFs,tdt

    ∣∣∣∣s=t=0

    =

    (d2GdT 2s,t

    dTs,tds

    +d2G

    dFs,tdTs,t

    dFs,tds

    )dTs,tdt

    +

    (d2G

    dTs,tdFs,t

    dTs,tds

    +d2GdF2s,t

    dFs,tds

    )dFs,tdt

    ∣∣∣∣s=t=0

    . (20)

    Formula (20) is used for the automatic derivation of the sec-ond order shape derivative in NGSolve. Using dTs,tds (x) =

  • Fully and Semi-Automated Shape Differentiation in NGSolve 7

    V (x), dTs,tdt (x)=W (x) anddFs,tds (x)= ∂V (x),

    dFs,tdt (x)= ∂W (x),

    we get

    d2

    dsdtJ (Ωs,t)

    ∣∣∣∣s=t=0

    =

    (d2GdT 2s,t

    V +d2G

    dFs,tdTs,t∂V

    )W

    +

    (d2G

    dTs,tdFs,tV +

    d2GdF2s,t

    ∂V

    )∂W

    ∣∣∣∣s=t=0

    .

    (21)

    Remark 4 We remark that the formula (21) can be evaluatedexplicitly and reads

    D2J (Ω)(V,W ) =∫

    Ω∇2 fV ·W +∇ f ·W divV +∇ f ·V divW

    + f divV divW − f ∂V> : ∂W dx.

    Formula (21) can be implemented in NGSolve as follows:

    55 d2JOmega f = B i l i n e a r F o r m (VEC)56 W = VEC. T r i a l F u n c t i o n ( )57

    58 d2JOmega f +=( G f . D i f f (X,W) +G f . D i f f ( F , g r ad (W) )) . D i f f (X,V) f

    59 d2JOmega f +=( G f . D i f f (X,W) +G f . D i f f ( F , g r ad (W) )) . D i f f ( F , g r ad (V) )

    Notice that since W is a trial function it is not affected by thedifferentiation with respect to X, see Section 2.2. Therefore,the terms coming from differentiating W with respect to thespatial coordinates X into the direction of V disappear andthus, although code lines 58–59 look like the “derivative ofthe derivative”, we actually compute formula (16) and not(18).

    In the same fashion, second order derivatives of bound-ary integrals of the form (13) can be computed.

    60 d2JOmega f bnd = B i l i n e a r F o r m (VEC)61

    62 d2JOmega f bnd +=( G f bnd . D i f f (X, W) + G f bnd .D i f f ( F , g r ad (W) . Trace ( ) ) ) . D i f f (X, V)

    63 d2JOmega f bnd +=( G f bnd . D i f f (X, W) + G f bnd .D i f f ( F , g r ad (W) . Trace ( ) ) ) . D i f f ( F , g r ad (V) .Trace ( ) )

    Again note that the trace operator is necessary when dealingwith gradients on the boundary.

    4 Semi-automatic shape differentiation with PDEconstraints

    In this section, we describe the automatic computation ofthe shape derivative for the following type of equality con-strained shape optimisation problems:

    min(Ω ,u)

    J(Ω ,u) (22)

    subject to (Ω ,u) ∈A ×Y solves

    e(Ω ,u) = 0, (23)

    where e : A ×Y → Y ∗ with e(Ω , ·) : Y (Ω)→ Y (Ω)∗ rep-resents an abstract PDE constraint with Y = ∪Ω∈A Y (Ω)being the union of Banach spaces Y (Ω) and A a set ofadmissible shapes. For any given Ω ∈ A we assume thePDE constraint (23) to admit a unique solution which wedenote by uΩ . Moreover, let J (Ω) := J(Ω ,uΩ ) denote thereduced cost functional. By introducing a Lagrangian func-tion, we can henceforth deal with an unconstrained shapefunction L rather than a shape function J and a PDE con-straint. We introduce the Lagrangian

    L (Ω ,u, p) := J(Ω ,u)+ 〈e(Ω ,u), p〉. (24)

    Now an initial shape Ω is perturbed by a family of trans-formations Tt , resulting in a new shape Ωt := Tt(Ω). Trans-forming back to the initial shape Ω leads to the Lagrangian:

    G(t,u, p) := L (Tt(Ω),Φt(u),Φt(p)), u, p ∈ Y (Ω), (25)

    where Φt : Y (Ω)→ Y (Ωt) is a bijective mapping. Here thetransformation Φt depends on the differential operator in-volved. For instance

    – if Y (Ω) = H10 (Ω), then Φt(u) = u◦T−1

    t ,– if Y (Ω) = H(curl,Ω), then Φt(u) = ∂T−>t (u◦T−1t ),– if Y (Ω)=H(div,Ω), then Φt(u)= 1det(∂Tt )∂Tt(u◦T

    −1t ).

    Intuitively the transformations Φt are chosen in such a waythat the transformed function Φt(u) still belongs to the samespace, but on a different domain. For the above three ex-amples this essentially requires to check how the differen-tial operators ∇, curl and div transform under the change ofvariables Tt , respectively. In fact one can check that

    (∇u)◦Tt = ∂T−>t ∇(u◦Tt), u ∈ H10 (Ω),

    (curlu)◦Tt =1

    ξ (t)∂Ttcurl

    (∂T>t (u◦Tt)

    ), u ∈ H(curl,Ω),

    (divu)◦Tt =1

    ξ (t)div(ξ (t)∂T−1t (u◦Tt)

    ), u ∈ H(div,Ω),

    where ξ (t) := det(∂Tt), see also [28, Section 3.9]. The trans-formation rules are precisely given by the respective Φt . Wealso note that for smooth functions this can be checked bydirect computation.

    Now the shape differentiability of (22)–(23) is reducedto proving that (see [45])

    DJ (Ω)(V ) =ddt

    G(t,ut ,0)|t=0 = ∂tG(0,u, p), (26)

    where ut := ut ◦Tt and ut ∈ Y (Ωt) solves e(Ωt ,ut) = 0 andp is the solution to the adjoint equation

    p ∈ Y (Ω), ∂uG(0,u, p)(ϕ) = 0 for all ϕ ∈ Y (Ω). (27)

    We stress that the choice of p as the solution of the ad-joint equation is important in order for the second equal-ity in (26) to hold. The verification of this equality depends

  • 8 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    on the specific PDE under consideration and can be accom-plished by different methods. We refer the reader to [45]for an overview and remark that (26) holds for a large classof nonlinear PDE constrained shape optimisation problems;see [44].

    The rest of this section is organised as follows: We in-troduce a model problem, which is the minimisation of atracking-type cost functional subject to Poisson’s equationin Section 4.1. We illustrate how the first and second or-der shape derivative for this PDE-constrained model prob-lem can be obtained in NGSolve in Sections 4.2 and 4.3.Finally, we also briefly discuss the extension to partial dif-ferentiation equations on surfaces.

    4.1 PDE-constrained model problem

    We will illustrate the derivation of the first and second ordershape derivative for the minimisation of a tracking-type costfunctional subject to Poisson’s equation on the unknown do-main Ω . Let d = 2 or 3, f ,ud ∈H1(Rd) and A ⊂P(Rd) bea set of admissible shapes. Here, P(Rd) denotes the powerset of all subsets of Rd . We consider the problem

    min(Ω ,u)

    J(Ω ,u) =∫

    Ω|u−ud |2 dx (28a)

    subject to (Ω ,u) ∈A ×H10 (Ω) solves

    〈e(Ω ,u),ψ〉 :=∫

    Ω∇u ·∇ψ dx−

    ∫Ω

    f ψ dx = 0 (28b)

    for all ψ ∈ H10 (Ω). The Lagrangian is given by

    L (Ω ,ϕ,ψ) :=∫

    Ω|ϕ−ud |2 dx+

    ∫Ω

    ∇ϕ ·∇ψ dx−∫

    Ωf ψ dx.

    (29)

    Given an admissible shape Ω , a vector field V ∈C0,1(Rd)dand t > 0 small, let Ωt := (Id + tV )(Ω) be the perturbeddomain. Therefore the parametrised Lagrangian is given by

    G(t,ϕ,ψ) :=L (Tt(Ω),ϕ ◦T−1t ,ψ ◦T−1t ), ϕ,ψ ∈H10 (Ω).(30)

    Changing variables yields

    G(t,ϕ,ψ) =∫

    Ω|ϕ−utd |2 det(Ft)dx

    +∫

    Ω(F−>t ∇ϕ) · (F−>t ∇ψ)det(Ft)dx−

    ∫Ω

    f tψ det(Ft)dx

    (31)

    =: G̃(Tt ,Ft ,ϕ,ψ),

    where utd = ud ◦Tt and f t = f ◦Tt . Here we also transformedthe gradient according to (∇w)◦Tt = F−>t ∇(w◦Tt) for w ∈

    H10 (Ω). Recall that, for a given Ω ∈A , uΩ denotes the cor-responding unique solution to (28b) and J (Ω) the reducedcost functional, J (Ω) := J(Ω ,uΩ ). Let ut ∈H10 (Ω) be thesolution of the perturbed state equation brought back to theoriginal domain Ω , that is, ut ∈ H10 (Ω) is the unique solu-tion to

    ∂ψ G(t,ut ,0)(ψ) = 0 for all ψ ∈ H10 (Ω). (32)

    Note that, for ut defined by (32), it holds J (Ωt)=G(t,ut ,ψ)for all ψ ∈H10 (Ω) and therefore also DJ (Ω)(V )=

    ddt G(t,u

    t ,ψ)for all ψ ∈ H10 (Ω).

    It can easily be shown that (26) holds and thus the shapederivative in the direction of a vector field V ∈ C0,1(R)d isgiven by

    DJ (Ω)(V ) = ∂tG(0,u, p),

    where p ∈H10 (Ω) denotes the adjoint state and is defined asthe unique solution p ∈ H10 (Ω) to

    ∂ϕ G(0,u, p)(ϕ̂) = 0 for all ϕ̂ ∈ H10 (Ω), (33)

    or explicitly∫Ω

    ∇ϕ̂ ·∇pdx =−2∫

    Ω(u−ud)ϕ̂ dx for all ϕ̂ ∈ H10 (Ω).

    (34)

    4.2 First order shape derivative

    By the discussion above, the first order shape derivative isgiven by ∂tG(0,u, p) with G defined in (31) and u and p theunique solutions to the boundary value problems (28b) and(34), respectively.

    Writing G̃(Tt ,Ft) := G̃(Tt ,Ft ,u, p) =G(t,u, p) we obtainin analogy to the unconstrained problem

    DJ (Ω)(V ) =ddt

    J (Ωt)∣∣∣∣t=0

    =

    (dG̃dTt

    V +dG̃dFt

    ∂V)∣∣∣∣

    t=0.

    We can compute explicitly

    dG̃dFt|t=0∂V =

    ∫Ω

    div(V )(u−ud)2− (∂V +∂V>)∇u ·∇p

    −div(V )∇u ·∇p− f pdiv(V ) dx, (35)dG̃dTt|t=0V =

    ∫Ω−2(u−ud)∇ud ·V −∇ f ·V p dx. (36)

    Now we are in a position to compute the first order shapederivative for the PDE-constrained shape optimisation prob-lem (28) in NGSolve. After solving the state equation asshown in Section 2.1, the adjoint equation can be solved asfollows.

  • Fully and Semi-Automated Shape Differentiation in NGSolve 9

    64 ud = X[ 0 ] * ( 1 −X[ 0 ] ) *X[ 1 ] * ( 1 −X[ 1 ] )65 d e f Cos t ( u ) :66 r e t u r n ( u−ud ) **2 * Det ( F ) * dx67

    68 # s o l v e a d j o i n t e q u a t i o n69 gfp = G r i d F u n c t i o n ( f e s )70 dCostdu = LinearForm ( f e s )71 dCostdu += Cos t ( g fu ) . D i f f ( gfu , w)72 dCostdu . Assemble ( )73 gfp . vec . d a t a = −a . mat . I n v e r s e ( f e s . F reeDofs ( ) ,

    i n v e r s e =” s p a r s e c h o l e s k y ” ) . T * dCostdu . vec74

    75 Draw ( gfp , mesh , ” a d j o i n t ” )

    We can now define the Lagrangian (31) such that the shapederivative can be obtained by the same procedure as in theunconstrained setting. Note that lines 82–83 coincide withlines 48–49.

    76 d e f E q u a t i o n ( u ,w) :77 r e t u r n ( ( Inv ( F ) . t r a n s * g rad ( u ) ) * ( Inv ( F )

    . t r a n s * g rad (w) ) − f *w) * Det ( F ) *dx78

    79 G pde = Cos t ( g fu ) + E q u a t i o n ( gfu , g fp )80

    81 dJOmega pde = LinearForm (VEC)82 dJOmega pde += G pde . D i f f (X, V)83 dJOmega pde += G pde . D i f f ( F , g r ad (V) )

    4.3 Second order shape derivative

    Let us introduce the notation

    〈EV,W (s, t)ϕ,ψ〉 :=∫

    Ω(F−>s,t ∇ϕ) · (F−>s,t ∇ψ)det(Fs,t) dx

    −∫

    Ωf ◦Ts,tψ det(Fs,t) dx (37)

    JV,W (s, t;ϕ) :=∫

    Ω|ϕ−ud ◦Ts,t |2 det(Fs,t)dx (38)

    and

    GV,W (s, t,u, p) :=〈EV,W (s, t)u, p〉+ JV,W (s, t;u), (39)

    where Ts,t(x) = x+ sV (x)+ tW (x) and Fs,t := ∂Ts,t . We ob-serve that

    J (Ts,t(Ω)) = GV,W (s, t,us,t , ps,t) (40)

    with (us,t , ps,t) ∈ H10 (Ω)×H10 (Ω) being the solution to

    ∂pGV,W (s, t,us,t ,0)(ϕ) = 0 for all ϕ ∈ H10 (Ω), (41)∂uGV,W (s, t,us,t , ps,t)(ψ) = 0 for all ψ ∈ H10 (Ω) (42)

    for s, t ≥ 0. In case t = 0 we write us := us,t |t=0 and ps :=ps,t |t=0 and similarly for t = s = 0 we write u := us,t |s=t=0and p := ps,t |s=t=0. Therefore, consecutive differentiation of

    (40) first with respect to t at zero and then with respect to sat zero yields

    D2J (Ω)(V )(W ) =d2

    dsdtGV,W (s, t,us,t , ps,t)|s=t=0

    =dds

    ∂tGV,W (s,0,us, ps)|s=0

    =∂s∂tGV,W (0,0,u, p)+∂u∂tGV,W (0,0,u, p)(∂su0)

    +∂p∂tGV,W (0,0,u, p)(∂s p0), (43)

    where ∂su0 ∈H10 (Ω) solves the material derivative equation

    ∂u∂pGV,W (0,0,u,0)(ψ)(∂su0) =−∂s∂pGV,W (0,0,u,0)(ψ)(44)

    for all ψ ∈ H10 (Ω) or, equivalently

    〈∂uEV,W (0,0)(∂su0),ψ〉=−〈∂sEV,W (0,0)u,ψ〉 (45)

    for all ψ ∈H10 (Ω). Note that (45) is obtained by differentiat-ing (41) with respect to s and setting s = t = 0. Similarly thefunction ∂s p0 ∈ H10 (Ω) solves the material derivative equa-tion obtained by differentiating (42) with respect to s fors = t = 0,

    ∂p∂uGV,W (0,0,u, p)(ψ)(∂s p0) =−∂ 2u GV,W (0,0,u, p)(ψ)(∂su0)−∂s∂uGV,W (0,0,u, p)(ψ)

    (46)

    for all ψ ∈ H10 (Ω). The introduction of the adjoint variablep is analogous to the computation of the first order shapederivative. However in contrast to the first order derivativethe evaluation of D2J (Ω)(V )(W ) requires the computa-tion of the material derivatives ∂su0 and ∂s p0.

    Formally (44) and (46) can be written as an operatorequation with x = (0,0,u, p),(

    ∂ 2u GV,W (x) ∂p∂uGV,W (x)∂u∂pGV,W (x) 0

    )(∂su0∂s p0

    )=−

    (∂s∂uGV,W (x)∂s∂pGV,W (x)

    ).

    (47)

    So to evaluate the second derivative (43) in some direction(V,W ) we have to solve the system (47).

    This is realised in NGSolve by setting up a combinedfinite element space which we denote by X2. We define trialand test functions as well as grid functions representing thedeformation vector fields V and W , which we initialise withsome functions.

    84 X2 = FESpace ( [ f e s , f e s ] )85 dsu , dsp = X2 . T r i a l F u n c t i o n ( )86 uTes t , p T e s t = X2 . T e s t F u n c t i o n ( )87 gfV = G r i d F u n c t i o n (VEC)88 gfW = G r i d F u n c t i o n (VEC)89 gfV . S e t ( (X[ 0 ] *X[ 0 ] *X[ 1 ] * exp (X[ 1 ] ) ,X[ 1 ] *X[ 1 ] *X

    [ 0 ] * exp (X[ 0 ] ) ) )

  • 10 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    90 gfW . S e t ( (X[ 1 ] *X[ 1 ] *X[ 0 ] * exp (X[ 0 ] ) ,X[ 0 ] *X[ 0 ] *X[ 1 ] * exp (X[ 1 ] ) ) )

    We define a 2×2 block bilinear form as well as a 2×1 blocklinear form which will represent the left and right hand sidesof (47), respectively. The operator equation in (47) can beconveniently defined by differentiating the Lagrangian withrespect to the corresponding variables.

    91 shapeHessLag2 = B i l i n e a r F o r m ( X2 )92 shapeGradLag2 = LinearForm ( X2 )93

    94 shapeHessLag2 += ( G pde . D i f f ( gfu , u T e s t ) ) . D i f f( gfu , dsu ) # b l o c k ( 1 , 1 )

    95 shapeHessLag2 += ( G pde . D i f f ( gfu , u T e s t ) ) . D i f f( gfp , dsp ) # b l o c k ( 1 , 2 )

    96 shapeHessLag2 += ( G pde . D i f f ( gfp , p T e s t ) ) . D i f f( gfu , dsu ) # b l o c k ( 2 , 1 )

    97

    98 # l i n e 199 shapeGradLag2 += ( G pde . D i f f ( gfu , u T e s t ) ) . D i f f

    ( F , g r ad ( gfV ) )100 shapeGradLag2 += ( G pde . D i f f ( gfu , u T e s t ) ) . D i f f

    (X, gfV )101

    102 # l i n e 2103 shapeGradLag2 += ( G pde . D i f f ( gfp , p T e s t ) ) . D i f f (

    F , g r ad ( gfV ) )104 shapeGradLag2 += ( G pde . D i f f ( gfp , p T e s t ) ) . D i f f (

    X, gfV )

    We can solve this combined system for ∂su0 and ∂s p0 andaccess and visualise the two components in the followingway:

    105 gfCombined2 = G r i d F u n c t i o n ( X2 )106 shapeHessLag2 . Assemble ( )107 shapeGradLag2 . Assemble ( )108 gfCombined2 . vec . d a t a = shapeHessLag2 . mat .

    I n v e r s e ( X2 . FreeDofs ( ) , i n v e r s e = ” umfpack ”) * shapeGradLag2 . vec

    109

    110 g f d s u = G r i d F u n c t i o n ( f e s )111 g f d s p = G r i d F u n c t i o n ( f e s )112 g f d s u . vec . d a t a = gfCombined2 . components [ 0 ] . vec113 g f d s p . vec . d a t a = gfCombined2 . components [ 1 ] . vec114

    115 Draw ( gfdsu , mesh , ” dsu ” )116 Draw ( gfdsp , mesh , ” dsp ” )

    In order to obtain the second order shape derivative in the di-rection given by (V,W ), it remains to evaluate the term (43).We define the three terms of (43) as bilinear forms, assemblethem and perform vector-matrix-vector multiplications:

    117 w1 = f e s . T r i a l F u n c t i o n ( )118 q1 = f e s . T r i a l F u n c t i o n ( )119

    120 shapeHess11 = B i l i n e a r F o r m (VEC)121 shapeHess11 += ( G pde . D i f f ( F , g r ad (W) ) +G pde .

    D i f f (X, W) ) . D i f f ( F , g r ad (V) )122 shapeHess11 += ( G pde . D i f f ( F , g r ad (W) ) +G pde .

    D i f f (X, W) ) . D i f f (X, V)123 shapeHess11 . Assemble ( )124

    125 shapeHess12 = B i l i n e a r F o r m ( t r i a l s p a c e = f e s ,t e s t s p a c e = VEC)

    126 shapeHess12 += ( G pde . D i f f ( F , g r ad (V) ) + G pde. D i f f (X, V) ) . D i f f ( gfu , w1 )

    127 shapeHess12 . Assemble ( )128

    129 shapeHess13 = B i l i n e a r F o r m ( t r i a l s p a c e = f e s ,t e s t s p a c e = VEC)

    130 shapeHess13 += ( G pde . D i f f ( F , g r ad (V) ) + G pde. D i f f (X, V) ) . D i f f ( gfp , q1 )

    131 shapeHess13 . Assemble ( )132

    133 av = gfV . vec . C r e a t e V e c t o r ( )134 av . d a t a = shapeHess11 . mat * gfV . vec135

    136 adsu = gfV . vec . C r e a t e V e c t o r ( )137 adsu . d a t a = shapeHess12 . mat * g f d s u . vec138

    139 adsp = gfV . vec . C r e a t e V e c t o r ( )140 adsp . d a t a = shapeHess13 . mat * g f d s p . vec141

    142 d2J = I n n e r P r o d u c t ( gfW . vec , av ) +I n n e r P r o d u c t ( gfW . vec , adsu ) + I n n e r P r o d u c t( gfW . vec , adsp )

    4.4 PDEs on surfaces

    The automated shape differentiation is not restricted to par-tial differential equations on domains Ω , but is readily ex-tended to surface PDEs. We consider a two dimensional closedsurface M ⊂ R3 and denote by n the normal field along M.Let ud ∈ H1(Rd) be given and define

    J(M,u) =∫

    M|u−ud |2 ds, (48)

    where u ∈ H1(M) solves the surface equation

    ∫M

    ∇Mu ·∇Mψ +uψ ds =∫

    Mf ψ ds for all ψ ∈ H1(M),

    (49)

    where ∇Mψ denotes the tangential gradient of ψ; see [10,p.493, Def.5.1]. We assume that the function f ∈ H1(R3) isgiven. The Lagrangian is given by

    L (M,ϕ,ψ) :=∫

    M|ϕ−ud |2 ds+

    ∫M

    ∇Mϕ ·∇Mψ +ϕψ ds

    −∫

    Mf ψ ds.

    As in the previous section we fix an admissible shape Mand let Mt := (Id+ tV )(M) be a small perturbation of M bymeans of a vector field V ∈ C1(Rd)d for t > 0 small. Theparametrised Lagrangian is given by

    G(t,ϕ,ψ) :=L (Tt(M),ϕ ◦T−1t ,ψ ◦T−1t ), ϕ,ψ ∈H1(M).(50)

  • Fully and Semi-Automated Shape Differentiation in NGSolve 11

    Define the density ω(Ft) := det(Ft)|F−>t n|. Changing vari-ables and using

    (∇Mt ϕ)◦Tt =B(Ft)∇M(ϕ ◦Tt),

    B(Ft) =

    (I− F

    −>t n|F−>t n|

    ⊗ F−>

    t n|F−>t n|

    )F−>t , (51)

    yields

    G(t,ϕ,ψ) =∫

    M|ϕ−utd |2 ω(Ft) ds

    +∫

    M((B(Ft)∇ϕ) · (B(Ft)∇ψ)+ϕψ)ω(Ft) ds

    −∫

    Mf tψ ω(Ft) ds,

    (52)

    where utd = ud ◦Tt and f t = f ◦Tt .Writing G̃(Tt ,Ft) := G(t,u, p) we obtain in analogy to

    the domain case

    DJ (Ω)(V ) =(

    dG̃dTt

    V +dG̃dFt

    ∂V)∣∣∣∣

    t=0. (53)

    We can compute explicitly

    dG̃dFt|t=0V =

    ∫M

    divM(V )(u−ud)2

    − (∂ MV +∂ MV>)∇Mu ·∇M p+divM(V )(∇Mu ·∇M p+up)− f pdivM(V ) ds, (54)

    dG̃dTt|t=0∂V =

    ∫M−2(u−ud)∇ud ·V −∇ f ·V p ds, (55)

    where ∂ MV denotes the tangential Jacobian of V definedby (∂ MV )i j := (∇MVi) j for i, j = 1, . . . ,d, and divM(V ) :=∂ MV : I the tangential divergence, which is defined as thetrace of the tangential Jacobian; see [10, p.495].

    The implementation is analogous to the previous sec-tions. We will only illustrate first order derivatives here. Wefirst define the geometry of the unit sphere, create a surfacemesh and define a finite element space on the surface mesh:

    143 from n e t g e n . csg i m p o r t *144 from n e t g e n . meshing i m p o r t *145 from n g s o l v e . i n t e r n a l i m p o r t v i s o p t i o n s146 from n g s o l v e i m p o r t *147

    148 g e o s u r f = CSGeometry ( )149 s p h e r e = Sphere ( Pn t ( 0 , 0 , 0 ) , 1 ) . bc ( ” o u t e r ” )150 g e o s u r f . Add ( s p h e r e )151 m e s h s u r f = Mesh ( g e o s u r f . Genera teMesh (

    p e r f s t e p s e n d = MeshingStep .MESHSURFACE,o p t s t e p s 2 d =3 , maxh = 0 . 2 ) )

    152 m e s h s u r f . Curve ( 3 )153 f e s s u r f = H1 ( mesh su r f , o r d e r = 3)

    Next we define the transformed cost function and partial dif-ferential equation needed for setting up the Lagrangian (52).Here, we again make use of a symbolic object F to which weassign the identity matrix. We define the tangential determi-nant ω and the matrix B defined in (51) as functions of thedeformation gradient Ft .

    154 X = C o e f f i c i e n t F u n c t i o n ( ( x , y , z ) )155 f unc = C o e f f i c i e n t F u n c t i o n (X[ 0 ] *X[ 1 ] *X[ 2 ] )156 F = Id ( 3 )157 t a n g D e t = Det ( F ) * Norm ( Inv ( F ) . t r a n s *

    s p e c i a l c f . normal ( 3 ) )158 Bmat = ( Id ( 3 ) − 1 / Norm ( Inv ( F ) . t r a n s * s p e c i a l c f .

    normal ( 3 ) ) **2 * O u t e r P r o d u c t ( Inv ( F ) . t r a n s *s p e c i a l c f . normal ( 3 ) , Inv ( F ) . t r a n s *s p e c i a l c f . normal ( 3 ) ) ) * Inv ( F ) . t r a n s

    159

    160 d e f E q u a t i o n s u r f ( u ,w) :161 r e t u r n ( ( Bmat* g rad ( u ) . T race ( ) ) * ( Bmat*

    g rad (w) . Trace ( ) ) + u*w − func * w) *t a n g D e t * ds

    162

    163 d e f C o s t s u r f ( u ) :164 r e t u r n u**2 * t a n g D e t * ds

    Now we can define the bilinear form and solve the stateequation. Here, the right hand side of the equation is in-cluded in the bilinear form and the boundary value problem– although linear – is solved by Newton’s method (whichterminates after only one iteration) for convenience.

    165 # s e t up and s o l v e s t a t e e q u a t i o n166 u s u r f , w s u r f = f e s s u r f . TnT ( )167 a = B i l i n e a r F o r m ( f e s s u r f )168 a += E q u a t i o n s u r f ( u s u r f , w s u r f )169 g f u s u r f = G r i d F u n c t i o n ( f e s s u r f )170 s o l v e r s . Newton ( a , g f u s u r f , p r i n t i n g = F a l s e )171 Draw ( g f u s u r f , mesh su r f , ” g f u s u r f ” )

    Using Newton’s method for solving the linear boundary valueproblem allows us to define both the left and right hand sideof the PDE using only one BilinearForm a (which, strictlyspeaking, is not bilinear any more). This way, we can reuseEquation surf as defined in lines 160–161 to define theboundary value problem in line 168.

    The adjoint equation is solved as usual:

    172 # s o l v e a d j o i n t e q u a t i o n173 l f c o s t s u r f = LinearForm ( f e s s u r f )174 l f c o s t s u r f += C o s t s u r f ( g f u s u r f ) . D i f f (

    g f u s u r f , w s u r f )175 l f c o s t s u r f . Assemble ( )176 i n v a = a . mat . I n v e r s e ( f e s s u r f . F reeDofs ( ) ,

    i n v e r s e =” s p a r s e c h o l e s k y ” )177 g f p s u r f = G r i d F u n c t i o n ( f e s s u r f )178 g f p s u r f . vec . d a t a = − i n v a . T * l f c o s t s u r f . vec179 Draw ( g f p s u r f , mesh su r f , ” g f p s u r f ” )

    The shape derivative is obtained as in the case of PDEsposed on volumes by the evaluation of (53):

    180 G s u r f = C o s t s u r f ( g f u s u r f ) + E q u a t i o n s u r f (g f u s u r f , g f p s u r f )

    181

  • 12 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    182 VEC3d = VectorH1 ( mesh su r f , o r d e r =1)183 V3d = VEC3d . T e s t F u n c t i o n ( )184 dJOmega sur f = LinearForm ( VEC3d )185 dJOmega sur f += G s u r f . D i f f (X, V3d ) + G s u r f .

    D i f f ( F , Grad ( V3d ) . Trace ( ) )

    5 Fully automated shape differentiation

    In the previous sections we used the automatic differentia-tion capabilities of NGSolve to alleviate the shape differenti-ation procedure. However, so far we still had to include someknowledge about the problems at hand. So far, it was neces-sary to define the objective function or Lagrangian G in thecorrect way, accounting for the correct transformation rulesbetween perturbed and unperturbed domain. In this section,we will show that also this step can be automated since allnecessary information is already included in the functionalsetting. The fully automated shape differentiation is incor-porated by the command

    DiffShape(...).

    In particular, in the fully automated setting it is enough toset up the cost function or Lagrangian for the unperturbedsetting. For a shape function of the type (6) we can define theshape derivative of the cost function in the following way:

    186 G f 0 = f * dx187 dJOmega f 0 = LinearForm (VEC)188 dJOmega f 0 += G f 0 . D i f f S h a p e (V)

    Note that there is no term of the form Det(F) showing upin line 186. Here, the transformation of the domain is takencare of automatically. It can be checked that this really givesthe same result as dJOmega f defined in lines 48–49.

    189 dJOmega f . Assemble ( )190 dJOmega f 0 . Assemble ( )191 d i f f e r e n c e V e c = dJOmega f . vec . C r e a t e V e c t o r ( )192 d i f f e r e n c e V e c . d a t a = dJOmega f . vec −

    dJOmega f 0 . vec193 p r i n t ( ” | dJOmega f − dJOmega f 0 | = ” , Norm (

    d i f f e r e n c e V e c ) )

    The above code gives the output

    |dJOmega_f - dJOmega_f_0| = 1.571008573810619e-17

    which confirms our claim. The same holds true for secondorder shape derivatives. The lines 58–59 can be replaced bya repeated call of DiffShape(...):

    194 d2JOmega f 0 = B i l i n e a r F o r m (VEC)195 d2JOmega f 0 += G f 0 . D i f f S h a p e (V) . D i f f S h a p e (W)

    Again, it can be verified that d2JOmega f 0 coincideswith the previously defined quantity d2JOmega f. Note thatslightly different results may occur due to different integra-tion rules used. This can be cured by enforcing an integra-tion rule of higher order for G f, i.e. by replacing the symboldx in the definition of G f with dx(bonus intorder=2).

    In the more general setting of PDE-constrained shapeoptimisation, the procedure is very similar. Here the idea ex-ploited in the implementation of the command DiffShape(...)is to just differentiate the general expression (25) with re-spect to the parameter t. The transformations Φt appearingin (25), which depend on the functional setting of the PDE,are identified automatically from the finite element spacefrom which the corresponding functions originate. The shapederivative of lines 82–83 can be obtained by the followingcode.

    196 d e f C o s t 0 ( u ) :197 r e t u r n ( u−ud ) **2 * dx198

    199 d e f E q u a t i o n 0 ( u ,w) :200 r e t u r n ( g r ad ( u ) * g rad (w) − f1 *w) *dx201

    202 G pde 0 = C o s t 0 ( g fu ) + E q u a t i o n 0 ( gfu , g fp )203

    204 dJOmega pde 0 = LinearForm (VEC)205 dJOmega pde 0 += G pde 0 . D i f f S h a p e (V)

    Here, gfu and gfp represent the solutions to the state andadjoint equation, respectively, and must have been computedpreviously. The bilinear form shapeHess11 used in Section4.3 (see lines 121–122) can be obtained similarly:

    206 shapeHess11 0 = B i l i n e a r F o r m (VEC)207 shapeHess11 0 += G pde 0 . D i f f S h a p e (W) .

    D i f f S h a p e (V)

    The same holds true for boundary integrals

    208 G f b n d 0 = f * ds209 dJOmega f bnd 0 = LinearForm (VEC)210 dJOmega f bnd 0 += G f b n d 0 . D i f f S h a p e (V)

    and surface PDEs

    211 d e f C o s t s u r f 0 ( u ) :212 r e t u r n u**2 * ds213 d e f E q u a t i o n s u r f 0 ( u ,w) :214 r e t u r n ( g r ad ( u ) . T race ( ) * g rad (w) . Trace ( ) +

    u*w − func * w) * ds215 G s u r f 0 = C o s t s u r f 0 ( g f u s u r f ) +

    E q u a t i o n s u r f 0 ( g f u s u r f , g f p s u r f )216 dJOmega su r f 0 = LinearForm ( VEC3d )217 dJOmega su r f 0 += G s u r f 0 . D i f f S h a p e ( V3d )

    as well as their respective second order derivatives.

    Remark 5 We remark that the fully automated differentia-tion using DiffShape(...) should be seen to complementthe semi-automated shape differentiation techniques intro-duced in Sections 3 and 4 rather than to replace them. Usingthe semi-automated differentiation, the user has the possibil-ity to, on the one hand, keep control over the involved terms,and on the other hand also to adjust the shape differentiationto their custom problems which may be non-standard. Asan example where the semi-automated differentiation maybe beneficial compared to the fully automated differentia-tion we mention the case of time-dependent PDE constraints

  • Fully and Semi-Automated Shape Differentiation in NGSolve 13

    considered in a space-time setting when a shape deformationis only desired in the spatial coordinates, see Section 7.8. Ofcourse, when one is interested in the shape derivative for amore standard problem, the fully automated way appears tobe more convenient and less error prone.

    Remark 6 We have seen that the command DiffShape(...)allows to compute the shape derivative of unconstrained shapeoptimisation problems in a fully automated way without spec-ifying any transformation rules, see line 188. For the practi-cally more relevant case of PDE-constrained shape optimi-sation problems, the state and adjoint equations have to besolved beforehand also in the fully automated context us-ing DiffShape(...). We remark that this can be easilyachieved by defining a custom function solvePDE() as itis done for the case of a linear PDE in lines 227–234. Sincethe purpose of this paper is to illustrate a convenient way ofcomputing shape derivatives and performing shape optimi-sation rather than to provide a tool for black-box optimisa-tion, this step is left to the user and is not automated, leavingmore freedom in the choice of, e.g., solvers for the arisinglinear systems.

    6 Optimisation algorithms

    In this section we discuss how to use optimisation algo-rithms in conjunction with the automated shape differenti-ation explained in the previous sections. The starting pointof our discussion is a fixed initial shape Ω . Then we con-sider the mapping

    V 7→ g(V ) := J ((Id+V )(Ω)) (56)

    defined on a suitable space of vector fields Θ ⊂ C0,1(D)d .Since the mapping g is defined on an open subset Θ of theBanach space C0,1(D)d we can employ standard algorithmsto minimise g over Θ . The only constraint we must imposeis that Id+V remains invertible, which can be difficult inpractice. In view of g(V + tW ) = J ((Id+V + tW )(Ω)) =J ((Id+ tW ◦ (Id+V )−1)((Id+V )(Ω))) for V,W ∈Θ andt small, we find by differentiating with respect to t at t = 0,that

    ∂g(V )(W ) = DJ ((Id+V )(Ω))(W ◦ (Id+V )−1) (57)

    for V,W ∈Θ and Id+V invertible.

    6.1 Gradient computation

    The gradient of ∂g(V ) in a Hilbert space H ⊂ C0,1(D)d isdefined by

    ∂g(V )(W ) = (∇Hg(V ),W )H for all W ∈ H. (58)

    Typical choices for H are

    H = H10 (D)d , (W,V )H :=

    ∫D

    ∂W : ∂V +V ·W dx, (59)

    H = H10 (D)d , (W,V )H :=

    ∫D

    ε(W ) : ε(V )+V ·W dx, (60)

    H = H10 (D)d , (W,V )H :=

    ∫D

    ε(W ) : ε(V )+V ·W

    + γCRBV ·BW dx, (61)

    where ε(V ) := 12 (∂V +∂V>), γCR > 0 and

    B :=(−∂x ∂y∂y ∂x

    ). (62)

    The last choice, which is restricted to the spatial dimensiond = 2, corresponds to a penalised Cauchy-Riemann gradientand results in a gradient which is approximately conformaland hence preserves good mesh quality. We refer to [24] fora detailed description. We also refer to [7, 8] and [3] for theuse of different inner products.

    6.2 Basic algorithm

    Let Ω be an initial shape and let H ⊂C0,1(D)d be a Hilbertspace. Then a basic shape optimisation algorithm reads asfollows.

    Algorithm 1 gradient algorithm1: Input: domain Ω0, n = 0, Nmax > 0, ε > 0, γ ≥ 02: Output: optimal shape Ω ∗3: while n≤ Nmax and |∇J (Ωn)|> ε do4: if J ((Id − α∇J (Ωn))(Ωn)) < J (Ωn) − γα|∇J (Ωn)|2

    then5: Ωn+1← (Id−α∇J (Ωn))(Ωn)6: n← n+17: increase α8: else9: reduce α

    10: end if11: end while

    We present and explain the numerical realisation of Al-gorithm 1 in NGSolve for the case of a PDE-constrainedshape optimisation problem in two space dimensions. Thesimpler case of an unconstrained shape optimisation prob-lem or the case of three space dimensions can be realised bysmall modifications of the presented code.

    First of all, we mention that we realise shape modifi-cations in NGSolve by means of deformation vector fieldswithout actually modifying the coordinates of the underly-ing finite element grid. Recall the vector-valued finite ele-ment space VEC over a given mesh as introduced in code line44. We define a vector-valued GridFunctionwith the namegfset which will represent the current shape. We initialise

  • 14 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    it with some vector-valued coefficient function V (x1,x2) =(x21 x2,x

    22 x1)

    > and obtain the deformed shape (Id+V )(Ω)by the command mesh.SetDeformation(gfset):

    218 g f s e t = G r i d F u n c t i o n (VEC)219 Draw ( g f s e t , mesh , ” g f s e t ” )220 S e t V i s u a l i z a t i o n ( d e f o r m a t i o n =True )221 g f s e t . S e t ( (X[ 0 ] *X[ 0 ] *X[ 1 ] ,X[ 1 ] *X[ 1 ] *X[ 0 ] ) )222 mesh . S e t D e f o r m a t i o n ( g f s e t )223 Redraw ( )

    Any operation involving the mesh such as integration or as-sembling of matrices is now carried out for the deformedconfiguration. To be more precise, a change of variables isperformed internally by accounting for the correspondingJacobi determinant and transforming the derivatives accord-ingly with the Jacobian of the deformation. Therefore, all re-sulting coefficient vectors (which are stored in GridFunctions)correspond to the shape functions in reference configuration.The deformation can be unset by the commandmesh.UnsetDeformation(). Integrating the constant func-tion over the mesh in the perturbed and unperturbed setting,

    224 p r i n t ( I n t e g r a t e ( 1 , mesh ) )225 mesh . U n s e t D e f o r m a t i o n ( )226 p r i n t ( I n t e g r a t e ( 1 , mesh ) )

    gives the output1.7924529046862627

    0.7854072970684544

    respectively.In the course of the optimisation algorithm the state equa-

    tion as well as the adjoint equation have to be solved forevery new shape. We define the following function, whichcomputes the state and adjoint state for a linear PDE con-straint:

    227 d e f solvePDE ( ) :228 a . Assemble ( )229 L . Assemble ( )230 dCostdu . Assemble ( )231

    232 i n v a = a . mat . I n v e r s e ( f e s . F reeDofs ( ) ,i n v e r s e =” s p a r s e c h o l e s k y ” )

    233 gfu . vec . d a t a = i n v a * L . vec234 gfp . vec . d a t a = − i n v a . T * dCostdu . vec

    The shape derivative dJOmega for some problem at handcan be defined as illustrated in Sections 4.1 and Section 5.Finally, we need to define the shape gradient, which is thesolution to a boundary value problem of the form (58). Wechoose the bilinear form defined in (61) with γCR = 10:

    235 d e f eps ( u ) :236 r e t u r n 1 / 2 * ( g rad ( u ) + g rad ( u ) . t r a n s )237

    238 aX = B i l i n e a r F o r m (VEC)239 W, V = VEC. TnT ( ) # d e f i n e t r i a l f u n c t i o n W

    and t e s t f u n c t i o n V240

    241 aX += I n n e r P r o d u c t ( eps (W) , eps (V) ) *dx +I n n e r P r o d u c t (W, V) *dx

    242 aX += 10 * ( g rad (W) [ 1 , 1 ] − g rad (W) [ 0 , 0 ] ) * ( g rad(V) [ 1 , 1 ] − g rad (V) [ 0 , 0 ] ) *dx

    243 aX += 10 * ( g rad (W) [ 1 , 0 ] + g rad (W) [ 0 , 1 ] ) * ( g rad(V) [ 1 , 0 ] + g rad (V) [ 0 , 1 ] ) *dx

    Now we can run Algorithm 1 for problem (28):

    244 a l p h a = 1245 a l p h a i n c r f a c t o r = 1 . 2246 gamma = 1e −4247 Nmax = 100248 e p s i l o n = 1e −7249

    250 i s C o n v e r g e d = F a l s e251 g f s e t . S e t ( ( 0 , 0 ) )252 gfX = G r i d F u n c t i o n (VEC)253 gfse tTemp = G r i d F u n c t i o n (VEC)254

    255 solvePDE ( )256 Jnew = I n t e g r a t e ( Cos t ( g fu ) , mesh )257 J o l d = Jnew258

    259 f o r k i n r a n g e (Nmax) :260 mesh . S e t D e f o r m a t i o n ( g f s e t )261 aX . Assemble ( )262 dJOmega pde . Assemble ( )263 invaX = aX . mat . I n v e r s e (VEC. FreeDofs ( ) ,

    i n v e r s e =” s p a r s e c h o l e s k y ” )264 gfX . vec . d a t a = invaX * dJOmega pde . vec265 currentNormGFX = Norm ( gfX . vec )266

    267 w h i l e True :268 i f currentNormGFX < e p s i l o n :269 i s C o n v e r g e d = True270 b r e a k271

    272 gfse tTemp . vec . d a t a = g f s e t . vec − a l p h a* gfX . vec

    273 mesh . S e t D e f o r m a t i o n ( gfse tTemp )274 solvePDE ( )275 Jnew = I n t e g r a t e ( Cos t ( g fu ) , mesh )276 mesh . U n s e t D e f o r m a t i o n ( )277 i f Jnew < J o l d − gamma * a l p h a *

    currentNormGFX **2 :278 J o l d = Jnew279 g f s e t . vec . d a t a = gfse tTemp . vec280 a l p h a *= a l p h a i n c r f a c t o r281 b r e a k282 e l s e :283 a l p h a = a l p h a / 2284 Redraw ( b l o c k i n g =True )

    Mesh movement and mesh optimisation As an alternative torealizing the deformations via mesh.SetDeformation(...),where the underlying mesh is not modified, one could alsojust move every mesh node in the direction of the given de-scent vector field by changing its coordinates. This can berealised by invoking the following method:

    285 d e f moveNGmesh2D ( d i s p l , mesh ) :286 f o r p i n mesh . ngmesh . P o i n t s ( ) :287 v = d i s p l ( mesh ( p [ 0 ] , p [ 1 ] ) )288 p [ 0 ] += v [ 0 ]289 p [ 1 ] += v [ 1 ]290 mesh . ngmesh . Update ( )

  • Fully and Semi-Automated Shape Differentiation in NGSolve 15

    Fig. 3 Before and after mesh optimisation bymesh.ngmesh.OptimizeMesh2d().

    Here, the displacement vector field displ, which is of typeGridFunction, is evaluated for each mesh node and, subse-quently, the mesh nodes are updated. At the end of the pro-cedure, the mesh structure needs to be updated, see line 290.Note that GridFunctions can only be evaluated at pointsinside the mesh (but not necessarily vertices of the mesh).Therefore, in order to evaluate displ at the point given bythe coordinates p[0], p[1], we need to pass mesh(p[0],p[1])in line 287.

    One advantage of this strategy is that a ill-shaped meshcan easily be repaired by a call of the methodmesh.ngmesh.OptimizeMesh2d() followed bymesh.ngmesh.Update(). Figure 3 shows a ill-shaped meshand the result of a call of mesh.ngmesh.OptimizeMesh2d().

    6.3 Newton’s method for unconstrained problems

    The particular choice H = H10 (D)d and

    (V,W )H := D2J (Ω)(V )(W ), (63)

    for a given shape function J leads to Newton’s method.We refer to [2, 13, 31, 33] where shape Newton methodswere used previously and to [21, Chapter 2] and [25, Chap-ter 5] for Newton’s method in an optimal control setting.This bilinear form is only positive semi-definite on H10 (D)

    d

    since D2J (Ω)(V )(W ) = 0 for V,W with V = W = 0 on∂Ω . Moreover, from the structure theorem for second shapederivatives proved in [30] we know that at a stationary pointΩ , that is, DJ (Ω)(V ) = 0 for all V ∈C0,1(D)d , we have

    D2J (Ω)(V )(W ) = `Ω (V ·n,W ·n), (64)

    where `Ω : C0(∂Ω)×C0(∂Ω)→ R is a bilinear function.Hence we also have D2J (Ω)(V )(W ) = 0 for all V,W suchthat V ·n =W ·n = 0. As a result the gradient

    (∇J (Ω),V )H = DJ (Ω)(V ) for all V ∈ H10 (D)d (65)

    according to (63) is not uniquely determined. To get aroundthis difficulty, the shape Hessian is often regularised by anH1 term, i.e. (63) is replaced by

    D2J (Ω)(V )(W )+δ∫

    Ω∂V : ∂W +V ·W dx, (66)

    see, e.g. [36], which, however, impairs the convergence speedof Newton’s method.

    Alternative regularisation strategy. Here, we propose the fol-lowing strategy: We regularise the shape Hessian only onthe boundary ∂Ω and only in tangential direction, i.e., wechoose

    (V,W )H := D2J (Ω)(V )(W )+δ∫

    ∂Ω(V · τ)(W · τ) (67)

    with a regularisation parameter δ . To exclude the part of thekernel corresponding to interior deformations, we solve the(regularised) Newton equation (65) only on the boundary∂Ω . This is realised by setting Dirichlet boundary condi-tions for all degrees of freedom except those on the bound-ary.

    291 VEC2 = VectorH1 ( mesh , o r d e r =1 , d i r i c h l e t = ”c i r c l e ” ) # a u x i l i a r y s p a c e f o r boundaryc o n d i t i o n s

    292 aX = B i l i n e a r F o r m (VEC)293 aX += G f 0 . D i f f S h a p e (W) . D i f f S h a p e (V)294 aX += 100 * I n n e r P r o d u c t (W, s p e c i a l c f .

    t a n g e n t i a l ( 2 ) ) * I n n e r P r o d u c t (V, s p e c i a l c f. t a n g e n t i a l ( 2 ) ) * ds

    295 aX . Assemble ( )296 invAX = aX . mat . I n v e r s e ( ˜ VEC2 . FreeDofs ( ) ,

    i n v e r s e =” umfpack ” )297

    298 gfX bnd = G r i d F u n c t i o n (VEC)299 gfX bnd . vec . d a t a = invAX * dJOmega f 0 . vec

    As a result, we get a shape gradient ˜∇J (Ω) which is nonzeroonly on the boundary. We extend this vector field to the inte-rior by solving an additional boundary value problem (of lin-earised elasticity type), where we use the deformation givenby ˜∇J (Ω) as Dirichlet boundary conditions.

    300 d e f g e t E x t e n s i o n ( gfX bnd , f r e e d o f s , g f X e x t ) :301 u , v = VEC. TnT ( )302 aX ex t = B i l i n e a r F o r m (VEC)303 aX ex t += I n n e r P r o d u c t ( g r ad ( u ) + g rad ( u ) .

    t r a n s , g r ad ( v ) ) *dx+ I n n e r P r o d u c t ( u , v ) *dx304

    305 g f X e x t . S e t ( gfX bnd )306 aX ex t . Assemble ( )307

    308 r = gfX bnd . vec . C r e a t e V e c t o r ( )309 r . d a t a = ( −1) * aX ex t . mat * g f X e x t . vec310

    311 g f X e x t . vec . d a t a += aX ex t . mat . I n v e r s e (f r e e d o f s = f r e e d o f s ) * r

    312

    313 g e t E x t e n s i o n ( gfX bnd , VEC2 . FreeDofs ( ) , gfX )314 g f s e t . S e t ( ( 0 , 0 ) )315 g f s e t . vec . d a t a = g f s e t . vec − 1 * gfX . vec

  • 16 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    The Newton algorithm reads as follows.

    Algorithm 2 Newton algorithm1: Input: domain Ω0, n = 0, Nmax > 0, ε > 02: Output: optimal shape Ω ∗3: while n≤ Nmax and |∇J (Ωn)|> ε do4: solve (65) to get ∇J (Ωn)5: Ωn+1← (Id−∇J (Ωn))(Ωn)6: n← n+17: end while

    6.4 Newton’s method for PDE-constrained problems

    We consider the PDE-constrained model problem of Section4.1 which is subject to the Poisson equation. The unregu-larised Newton system reads

    D2J (Ω)(V )(W ) =−DJ (Ω)(V ) for all V ∈ H10 (Ω).(68)

    In Subsection 4.3 we discussed how the second order shapederivative can be evaluated along a fixed given direction. Inthis section, we want to assemble the whole shape Hessianand eventually solve a regularised version of (68). Recall-ing that DJ (Ω)(V ) = ∂sGV,0(x) with x= (0,0,u, p) we seethat (47) and (43) lead to

    H

    Ṽ∂su0∂s p0

    =−∂tGV,W (x)0

    0

    . (69)with

    H (x) =

    ∂s∂tGV,W (x) ∂u∂tGV,W (x) ∂p∂tGV,W (x)∂s∂uGV,W (x) ∂ 2u GV,W (x) ∂p∂uGV,W (x)∂s∂pGV,W (x) ∂u∂pGV,W (x) 0

    .(70)

    The component Ṽ then represents the direction which weuse for the shape Newton optimisation step. The matrix in(69) can be realised in NGSolve by using a combined finiteelement space X3 consisting of three components as follows:

    316 X3 = FESpace ( [ VEC, f e s , f e s ] )317 PHI , u1 , p1= X3 . T r i a l F u n c t i o n ( )318 PSI , uTes t1 , pTe s t 1 = X3 . T e s t F u n c t i o n ( )319

    320 shapeHessLag3 = B i l i n e a r F o r m ( X3 )321 shapeHessLag3 += G pde 0 . D i f f S h a p e ( PHI ) .

    D i f f S h a p e ( PSI ) # b l o c k ( 1 , 1 )322 shapeHessLag3 += G pde 0 . D i f f S h a p e ( PSI ) . D i f f (

    gfu , u1 ) # b l o c k ( 1 , 2 )323 shapeHessLag3 += G pde 0 . D i f f S h a p e ( PSI ) . D i f f (

    gfp , p1 ) # b l o c k ( 1 , 3 )324 shapeHessLag3 += G pde 0 . D i f f ( gfu , u Te s t1 ) .

    D i f f S h a p e ( PHI ) # b l o c k ( 2 , 1 )325 shapeHessLag3 += ( G pde 0 . D i f f ( gfu , u Te s t1 ) ) .

    D i f f ( gfu , u1 ) # b l o c k ( 2 , 2 )

    326 shapeHessLag3 += ( G pde 0 . D i f f ( gfu , u Te s t 1 ) ) .D i f f ( gfp , p1 ) # b l o c k ( 2 , 3 )

    327 shapeHessLag3 += G pde 0 . D i f f ( gfp , p Te s t 1 ) .D i f f S h a p e ( PHI ) # b l o c k ( 3 , 1 )

    328 shapeHessLag3 += ( G pde 0 . D i f f ( gfp , p Te s t 1 ) ) .D i f f ( gfu , u1 ) # b l o c k ( 3 , 2 )

    The right hand side of (69) can be defined as follows:

    329 shapeGradLag3 = LinearForm ( X3 )330 shapeGradLag3 += ( −1) * G pde 0 . D i f f S h a p e ( PSI )

    Recall that the system (65) has a nontrivial kernel as dis-cussed in Section 6.3. This problem can be circumvented byproceeding like in the unconstrained case. We add a regular-isation only on the boundary,

    331 d e l t a = 1332 shapeHessLag3 += d e l t a * I n n e r P r o d u c t ( PHI ,

    s p e c i a l c f . t a n g e n t i a l ( 2 ) ) * I n n e r P r o d u c t (PSI , s p e c i a l c f . t a n g e n t i a l ( 2 ) ) * ds

    and exclude the interior degrees of freedom in the first rowand column of the 3×3 block system. This can be realised bysetting Dirichlet boundary conditions for the interior degreesof freedom, i.e. by dealing with the free degrees of freedom,

    333 # copy of VEC wi th D i r i c h l e t boundaryc o n d i t i o n s on whole boundary :

    334 VEC2 = VectorH1 ( mesh , o r d e r = 1 , d i r i c h l e t = ”. * ” )

    335 f reeDofsCombined = B i t A r r a y (VEC2 . ndof + 2* f e s .ndof )

    336 f o r i i n r a n g e (VEC2 . ndof ) :337 f reeDofsCombined [ i ] = n o t VEC2 . FreeDofs ( ) [

    i ]338 f o r i i n r a n g e ( f e s . ndof ) :339 f reeDofsCombined [VEC2 . ndof + i ] = f e s .

    F reeDofs ( ) [ i ]340 f reeDofsCombined [VEC2 . ndof + f e s . ndof + i ] =

    f e s . F reeDofs ( ) [ i ]

    and solving the regularised system using these free dofs:

    341 gfCombined3 = G r i d F u n c t i o n ( X3 )342 shapeHessLag3 . Assemble ( )343 shapeGradLag3 . Assemble ( )344 gfCombined3 . vec . d a t a = shapeHessLag3 . mat .

    I n v e r s e ( f r e e d o f s = freeDofsCombined , i n v e r s e=” umfpack ” ) * shapeGradLag3 . vec

    The newton direction is then given as the first of the threecomponents of the obtained solution.

    345 V t i l d e b n d = G r i d F u n c t i o n (VEC)346 V t i l d e = G r i d F u n c t i o n (VEC)347 V t i l d e b n d . vec . d a t a = gfCombined3 . components

    [ 0 ] . vec348 g e t E x t e n s i o n ( V t i l d e b n d , VEC2 . FreeDofs ( ) ,

    V t i l d e )349

    350 g f s e t . vec . d a t a = g f s e t . vec + 1 * V t i l d e . vec

  • Fully and Semi-Automated Shape Differentiation in NGSolve 17

    7 Numerical Experiments

    In this section we first verify the copmuted shape deriva-tives by performing a Taylor test, and then apply the au-tomated shape differentiation and the numerical algorithmsintroduced in the preceding sections in numerical examples.

    7.1 Code verification

    We verify the expressions that we obtained in a semi-automaticor fully automatic way for the first and second order shapederivatives by looking at the Taylor expansions of the per-turbed shape functionals. We illustrate our findings in twoexamples in R2. On the one hand, we consider a shape func-tion as introduced in (6) with an additional boundary integralas in (13), henceforth denoted by J1; on the other hand, weconsider the PDE-constrained shape optimisation problemdefined by (28), the reduced form of which will be denotedby J2(Ω). More precisely, we consider

    J1(Ω) =∫

    Ωf (x) dx+

    ∫∂Ω

    f (x) ds, (71)

    J2(Ω) =∫

    Ω|uΩ −ud |2 dx where uΩ solves (28b). (72)

    In the case of J1, we used the function

    f (x1,x2) =(

    0.5+√

    x21 + x22

    )2(0.5−

    √x21 + x

    22

    )2and for J2, we used ud(x1,x2) = x1(1− x1)x2(1− x2) andf (x1,x2) = 2x2(1− x2)+ 2x1(1− x1) for the function f inthe PDE constraint (28b).

    For the test of the first order shape derivatives DJi(Ω)(V )we choose a fixed shape Ω and a vector field V ∈C0,1(R2)2and observe the quantity

    δ1(Ji, t) := |Ji((Id+ tV )(Ω))−Ji(Ω)− t DJi(Ω)(V )| ,(73)

    for t ↘ 0. Likewise, for the second order shape derivative,we consider the remainder

    δ2(Ji, t) :=∣∣∣∣Ji((Id+ tV )(Ω))−Ji(Ω)− t DJi(Ω)(V )

    − 12

    t2D2Ji(Ω)(V )(V )∣∣∣∣

    as t ↘ 0. By the definition of first and second order shapederivatives, it must hold that

    δ1(Ji, t) = O(t2) and δ2(Ji, t) = O(t3) as t↘ 0.(74)

    This behavior can be observed in Figure 4(a) for J1 and inFigure 4(b) for J2, where we used V (x1,x2)= (x21x2e

    x2 ,x22x1ex1)

    in both cases.

    10-3

    10-2

    10-1

    10-18

    10-16

    10-14

    10-12

    10-10

    10-8

    10-6

    10-4

    10-3

    10-2

    10-1

    10-12

    10-10

    10-8

    10-6

    10-4

    10-2

    100

    (a) (b)

    Fig. 4 Taylor test for functions J1 and J2.

    The experiments for shape function J1 was conductedon a mesh consisting of 13662 vertices, 26946 elements andwith polynomial order 2 (resulting in 54269 degrees of free-dom), and the experiment for J2 with 95556 vertices and190062 elements and polynomial degree 1 (95556 degreesof freedom). We conducted these experiments for a num-ber of different problems with different vector fields V , inparticular with different PDE constraints and boundary con-ditions, and obtained similar results in all instances provideda sufficiently fine mesh was used.

    7.2 A first shape optimisation problem

    In this section, we revisit problem (6) introduced in Section3, i.e. the problem of finding a shape Ω such that the costfunction J (Ω) =

    ∫Ω f (x) dx is minimised.

    7.2.1 First order methods

    We illustrate our first order methods in a problem which wasalso considered in [24] and reproduce the results obtainedthere. We choose the function

    f (x1,x2) =(√

    (x1−a)2 +bx22−1)(√

    (x1 +a)2 +bx22−1)

    ·(√

    bx21 +(x2−a)2−1)(√

    bx21 +(x2 +a)2−1

    )− ε

    (75)

    with a = 45 , b = 2 and ε = 0.001. Recall that the optimalshape is given by {(x1,x2) ∈ R2 : f (x1,x2) < 0} which isdepicted in Figure 5 (right). We start our optimisation algo-rithm with the unit disk, Ω 0 = B1(0) as an initial design.Note that the optimal design cannot be reached by means ofshape optimisation using boundary perturbations. However,we expect the outer curve of the optimal shape to be reachedvery closely.

    We apply Algorithm 1 with the shape gradient ∇J as-sociated to the H1 inner product (59), to the bilinear form oflinearised elasticity (60) and including the additional Cauchy-Riemann term (61). We chose the algorithmic parameters

  • 18 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    Fig. 5 Initial domain Ω0 and optimal domain Ω ∗ for problem (6) withf chosen according to (75).

    Fig. 6 Results of problem (6) with f as in (75) and the shape gradientassociated to the H1 inner product (59).

    Fig. 7 Results of problem (6) with f as in (75) and the shape gradientassociated to the elasticity bilinear form (60).

    γ = 1e− 4, ε = 1e− 7, a mesh consisting of 2522 verticesand 4886 elements and a globally continuous vector-valuedfinite element space VEC of order 3. The results can be seenin Figures 6, 7 and 8, respectively.

    7.2.2 Second order method

    Since Newton’s method converges quadratically only in aneighborhood of the optimal solution, we choose a simpleroptimal design here. We choose

    f (x1,x2) =x2

    a2+

    y2

    b2−1 (76)

    which yields an ellipse with the lengths of the two semi-axes a and b. We choose a = 1.3 and b = 1/a and again startthe optimisation with the unit disk as initial shape. Figure

    Fig. 8 Results of problem (6) with f as in (75) and the shape gradientassociated to the elasticity bilinear form with Cauchy-Riemann term(61).

    Fig. 9 Numerical results for problem (67) with f as in (76) using sec-ond order method. Left: Initial design. Center: Optimised design aftersix iterations using (65)/(67). Right: Objective value J and norm ofshape gradient ‖∇J (Ω)‖ in the course of second order optimisationusing (66) with δ = 0.5 and (67) with δ = 100.

    9 shows the initial and optimised design after only six it-erations of Algorithm 2 with (·, ·)H chosen as in (67) withδ = 100. A comparison of the convergence histories be-tween the choice (67) with δ = 100 and (66) with δ = 0.5is shown in the right picture of Figure 9. In both cases,we tested a range of different values for δ and comparedthe convergence histories for the values which yielded thefastest convergence. The experiments were conducted on afinite element mesh consisting of 2522 nodes and 4886 tri-angular elements with a finite element space VEC of order 3,with the algorithmic parameter ε = 10−7.

    7.3 Shape optimisation subject to the Poisson equation

    In this section, we revisit the model problem introduced inSection 4.1 with f (x1,x2) = 2x2(1− x2)+ 2x1(1− x1) andud(x1,x2) = x1(1− x1)x2(1− x2). Note that the data is cho-sen in such a way that, for Ω ∗ = (0,1)2 it holds J (Ω ∗) = 0and thus Ω ∗ is a global minimiser of J . We show resultsobtained by first and second order shape optimisation meth-ods exploiting automated differentiation.

    We ran the optimisation algorithm in three versions. Onthe one hand, we applied a first order method with constantstep size α = 1. On the other hand, we applied two secondorder methods with the two different regularisation strate-gies for the shape Hessian in (65) introduced in (66) and(67). We chose the regularisation parameters δ empiricallysuch that the method performs as well as possible. In the

  • Fully and Semi-Automated Shape Differentiation in NGSolve 19

    0 50 100 150 200

    Iterations

    10-9

    10-8

    10-7

    10-6

    10-5

    10-4

    J

    0 50 100 150 200

    Iterations

    10-5

    10-4

    10-3

    10-2

    10-1

    100

    101

    (a) (b)

    Fig. 10 Convergence behaviour for shape optimisation problem (28)with proposed regularisation strategies (67) and (66) as well as firstorder method with constant step size α = 1. (a) Behaviour of objectivefunction J . (b) Behaviour of norm of shape gradient ‖∇J (Ω)‖.

    Fig. 11 Shape optimisation for problem (28). Left: Initial design.Right: Improved design after 200 iterations of second order algorithmwith regularisation as proposed in (67). Objective value was reducedfrom 5.297 ·10−5 to 1.0317 ·10−9. Color shows solution of constrain-ing PDE (28b).

    case of (66) we chose δ = 0.001 and in the case of (67)δ = 1. The experiments were conducted on a finite elementmesh consisting of 4886 elements with 2522 vertices andpolynomial degree 1. In Figure 10, we can observe the de-crease of the objective function as well as of the norm of theshape gradient over 200 iterations for these three algorith-mic settings.

    Figure 10 shows the initial design as well as the designafter 200 iterations of the second order method with reg-ularisation strategy (67). Note that the improved design isvery close to Ω ∗ = (0,1)2, which is a global solution. Theinitial design was chosen as the disk of radius 12 centered

    at the point( 1

    2 ,12

    )>. The objective value was reduced from

    5.297 ·10−5 to 1.0317 ·10−9.

    7.4 Nonlinear elasticity

    Here, we illustrate the applicability of the automated shapedifferentiation and optimisation in the more realistic and morecomplicated setting of nonlinear elasticity in two space di-mensions using a Saint Venant–Kirchhoff material with Young’smodulus E = 1000 and Poisson ratio ν = 0.3. We considera two-dimensional cantilever which is clamped on the upperand lower left parts of the boundary, Γ 1l = {0}× (0.88,1)and Γ 2l = {0}× (0,0.12), respectively, and is subject to asurface force gN = (0,−100)> on Γr = {1}× (0.45,0.55).

    The initial geometry with 3 holes is depicted in Figure 12(a). Let Γl := Γ 1l ∪Γ 2l and H1Γl (Ω)

    2 the subspace of H1(Ω)2

    with vanishing trace on Γl . The displacement u ∈ H1Γl (Ω)2

    under the surface force gN is given as the solution to theboundary value problem∫

    ΩS(u) : ∇v dx =

    ∫Γr

    gN · v ds (77)

    for all v ∈ H1Γl (Ω)2. Here, S(u) denotes the Saint Venant–

    Kirchhoff stress tensor

    S(u) = (I2 +∇u)[

    λTr(

    12(C(u)− I2)

    )I2 +µ(C(u)− I2)

    ],

    (78)

    where C(u) = (I2+∇u)>(I2+∇u) and I2 is the identity ma-trix, see also [4, Sec. 8], and λ and µ denote the Lamé con-stants,

    λ =Eν

    (1+ν)(1−2ν), µ =

    E2(1+ν)

    . (79)

    We minimise the functional

    J(Ω ,u) =∫

    ΩS(u) : ∇u dx+α

    ∫Ω

    1 dx (80)

    with α = 2.5 subject to (77) which amounts to maximisingthe structure’s stiffness while bounding the allowed amountof material used.

    We remark that the well-posedness of (77) is not clear,see also the discussion in [4, Sec. 8]. Nevertheless, applica-tion of the automated shape differentiation and optimisationyields a significant improvement of the initial design. Thehighly nonlinear PDE constraint (77) is solved by Newton’smethod. In order to have good starting values, a load step-ping strategy is employed, i.e., the load on the right handside is gradually increased, the PDE is solved and the solu-tion is used as an initial guess for the next load step. Thisis repeated until the full load is applied. With these ingre-dients at hand, Algorithm 1 (i.e. code lines 244–284) canbe run. We chose the algorithmic parameters alpha = 0.1(as an initial value), alpha incr factor = 1 (i.e. no in-crease), gamma = 1e-4 and epsilon = 1e-7. Moreover,we used (59) with an additional Cauchy-Riemann term as in(61) with weight γCR = 10. The objective value was reducedfrom 3.125 to 2.635 (volume term from 1.290 to 1.096) in15 iterations of Algorithm 1. The results were obtained on amesh consisting of 10614 elements and 5540 vertices usingpiecewise linear, globally continuous finite elements.

    7.5 Helmholtz equation

    In this section, we consider the problem of finding the opti-mal shape of a scattering object. More precisely, we consider

  • 20 P. Gangl, K. Sturm, M. Neunteufel, J. Schöberl

    (a) (b) (c)

    Fig. 12 Initial and optimised geometry of cantilever under verticalforce on right hand side using St. Venant–Kirchhoff model in nonlin-ear elasticity. (a) Initial geometry. (b) Optimised geometry (referenceconfiguration). (c) Optimised geometry (deformed configuration).

    the minimisation of the functional∫Γr

    uu ds (81)

    subject to the Helmholtz equation with impedance boundaryconditions on the outer boundary: Find u ∈ H1(Ω ,C) suchthat∫

    Ω[∇u ·∇w̄−ω2uw̄

    ]dx− iω

    ∫Γ

    uw̄ds =∫

    Ωf w̄ (82)

    for all w ∈ H1(Ω ,C). Here, w denotes the complex conju-gate of a complex-valued function w, ω denotes the wavenumber, i denotes the complex unit and the function f onthe right hand side is chosen as

    f (x1,x2) = 103 · e−9((x1−0.2)2+(x2−0.5)2), (83)

    see Figure 13(a). Furthermore,

    Ω = B((0.5,0.5)>,1)\B((0.75,0.5)>,0.15)

    denotes the domain of interest, Γ = {(x1,x2) : x21 + x22 = 1}the outer boundary and Γr = {(x1,x2) : x21 + x22 = 1,x1 ≥ 0}the right half of the outer boundary. Here, only the innerboundary ∂Ω \Γ is subject to the shape optimisation. Thus,the aim of this model problem is to find a shape of the scat-tering object such that the waves are reflected away from Γr.

    Figure 13 (b) and (c) show the initial and final shape ofthe scattering object, respectively. Figure 14 shows the normof the state for the initial configuration (circular shape ofscattering object) and for the optimised configuration. Theobjective value was reduced from 3.44 ·10−3 to 3.31 ·10−3.The forward simulations were performed using piecewiselinear finite elements on a triangular grid consisting of with34803 degrees of freedom. The optimisation stopped after12 iterations.

    7.6 Application to an Electric Machine

    In this section, we consider the setting of three-dimensionalnonlinear magnetostatics in H(curl,D) as it appears in the

    (a) (b) (c)

    Fig. 13 (a) Geometry with right hand side f . (b) Initial shape ofscatterer (zoom of geometry in (a)). (c) Optimised shape of scatterer(zoom).

    (a) (b)

    Fig. 14 (a) Absolute value of state u for initial configuration. (b) Ab-solute value of state u for optimised configuration.

    simulation of electric machines. Let D⊂R3 denote the com-putational domain, which consists of ferromagnetic mate-rial, air regions and permanent magnets, see Figure 15. Ouraim is to minimise the functional∫

    Ωg|curlu ·n−Bnd |2 dx, (84)

    where Ωg denotes the air gap region of the machine, n de-notes an extension of the normal vector to the interior of Ωg,Bnd : Ωg→R3 is a given smooth function and u∈H0(curl,D)is the solution to the boundary value problem∫

    DνΩ (|curlu|)curlu · curlw+δu ·w dx =

    ∫Ωm

    M · curlw dx

    (85)

    for all w ∈ H0(curl,D). Here, Ω ⊂ D denotes the union ofthe ferromagnetic parts of the electric machine, Ωm denotesthe permanent magnets subdomain and

    νΩ = χΩ (x)ν̂(|curlu|)+χD\Ω (x)ν0 (86)

    denotes the magnetic reluctivity, which is a nonlinear func-tion ν̂ inside the ferromagnetic regions and equal to a con-stant ν0 elsewhere. Further, δ > 0 is a small regularisationparameter and M : D → R3 denotes the magnetisation inthe permanent magnets. The nonlinear function ν̂ satisfiesa Lipschitz condition and a strong monotonicity conditionsuch that problem (85) is well-posed. The goal of minimis-ing the cost function (84) is to obtain a design which ex-hibits a smooth rotation pattern. Note that in this particu-lar example we do not consider rotation of the machine, butrather a fixed rotor position, and there are no electric cur-rents present. We refer the reader to [17, Sec. 6] for a moredetailed description of the problem and to [16] for a 2D ver-sion of the same problem.

  • Fully and Semi-Automated Shape Differentiation in NGSolve 21

    Fig. 15 Geometry of electric motor with subdomains in 2D cross sec-tion. The ferromagnetic subdomains Ω are depicted in red, Ωm corre-sponds to the permanent magnets. The rest of the computational do-main represents air. F