28
Ingegneria Matematica ADVANCED PROGRAMMING FOR SCIENTIFIC COMPUTING Lecturers Prof. Luca Formaggia Prof. Carlo De Falco Implementation of a C++ code to solve the Allen-Cahn equation Advisor Prof. Marco Verani Student Antonio Nicoló 817177 Academic Year 2014-2015

Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

  • Upload
    others

  • View
    7

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Ingegneria Matematica

ADVANCED PROGRAMMING FORSCIENTIFIC COMPUTING

LecturersProf. Luca FormaggiaProf. Carlo De Falco

Implementation of a C++ codeto solve the Allen-Cahn equation

AdvisorProf. Marco Verani

StudentAntonio Nicoló

817177

Academic Year 2014-2015

Page 2: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Contents

1 The mathematical model 11.1 Model problem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11.2 The variational formulation . . . . . . . . . . . . . . . . . . . . . . . 2

2 Numerical approximation 32.1 Mesh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32.2 Implementation of linear finite element on a fixed mesh . . . . . . . . 62.3 Non linearity approximation . . . . . . . . . . . . . . . . . . . . . . . 142.4 Time stepping scheme and problem data . . . . . . . . . . . . . . . . 17

3 Numerical results 183.1 Concentric circle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183.2 Dumbbell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203.3 Non homogeneous problem . . . . . . . . . . . . . . . . . . . . . . . . 213.4 A different non linearity . . . . . . . . . . . . . . . . . . . . . . . . . 23

4 Instructions for use 25

Page 3: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Chapter 1

The mathematical model

1.1 Model problem

The Allen-Cahn equation is a mathematical model for certain phase separation pro-cess. It describes phase transition processes in which the volume fractions of thephases may change and the only stationary configurations represent single phases(melting processes). Let Ω ⊆ Rd, d = 2, 3, be a bounded, polygonal or polyhedralLipschitz domain. The outer unit normal on ∂Ω is denoted by n and ∂nv is the nor-mal derivative of a function v on ∂Ω. Given a positive parameter ε 1, u0 ∈ L2(Ω)

and T > 0, we seek a function u : [0, T ]× Ω→ R that solves∂tu−∆u+ ε−2f(u) = 0 in Ω× (0, T ),

∂nu = 0 on ∂Ω× (0, T ),

u = u0 in Ω× 0.

(1.1)

where f = F ′ for a nonnegative function F ∈ C1(R) satisfying F (±1) = 0. Weconsider F (u) = (u2 − 1)2/4 and f(u) = u3 − u, but other choices are possible.For this model problem we assume that |u0(x)| ≤ 1 for almost every x ∈ Ω. TheAllen-Cahn equation is the L2-gradient flow of the functional

Iε(u) =1

2

∫Ω

|∇u|2 dx+ ε−2

∫Ω

F (u) dx.

Solutions tend to decrease the energy and develop interfaces separting regions inwhich it is nearly constant with values close to the minima of F. We denote thezero level set of the function u as the interface, but this does not define a sharp

1

Page 4: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

1. The mathematical model

separation of the phases: more precisely, the phases are separated by a region ofwidth ε around the zero level set of u, called the diffuse interface. We observethat topological changes of the interface occur within temporal intervals of lenghtcomparable to ε2.

1.2 The variational formulation

In order to recast the problem (1.1) in a variational form, we introduce the followingnotation: for a Banach space X its dual is denoted X∗ and 〈·, ·〉 is the correspondingduality pairing. We define the functional space V := H1(Ω) = v ∈ L2(Ω) : ∇v ∈[L2(Ω)]d. The existence of a unique solution u follows from a discretization in timeand a subsequent passage to limit.

Theorem 1.1. For every u0 ∈ L2(Ω) and T > 0 there exists a weak solutionu ∈ H1([0, T ];V∗) ∩ L2([0, T ];V) that satisfies u(0) = u0 and

〈∂tu, v〉+ (∇u,∇v) + ε−2(f(u), v) = 0, (1.2)

for almost every t ∈ [0, T ] and every v ∈ V. If u0 ∈ V, then we have u ∈H1([0, T ];L2(Ω)) ∩ L∞([0, T ];V) and

Iε(u(t)) +

∫ t

0

‖∂tu‖2 dt ≤ Iε(u0),

for almost every t ∈ [0, T ].

2

Page 5: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Chapter 2

Numerical approximation

In this chapter we will see the numerical approximation of the Allen-Cahn equation(1.1): to this aim, we want that our code reads a triangulation defining the domainfrom some files, that implements linear finite elements on this fixed mesh and resolvesthe problem using Newton’s method to linearize the non linear function f(·).

2.1 Mesh

The first step to solve our problem is how to read a mesh. We need some usefulinformations about the triangulation: number of nodes, number of elements, coor-dinates of nodes with the respective Id and a connectivity matrix. We can easilyobtain these informations from any mesh generator (for example, pdetool in Matlab).Once we get these data, they will be stored in three different .txt-files: the first oneis called ne.txt and contains only the number of nodes and the number of elementsof the mesh; the second one, that contains the Id of the node with the correspondingcoordinates, is called vc.txt. Each node is stored in a different row of the file in as-cending order with respect to the Id, i.e. the first row coincides with Id equal to one,while the last row with the last Id. The third file contains the connectivity matrixand each element is stored in a different row of the matrix. We do not consider anumbering for the edges, because we are considering only Neumann boundary con-ditions. This file is called ev.txt.Now we can explain the code to get the mesh: we refer to reading.hpp file. Beforegoing on, we define the classes of nodes and elements. The first one is characterizedby the Id number and the coordinates of the node, while the second one is charac-terized by the Id numbers of the nodes that compose the triangle, as we can see in

3

Page 6: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

the code:

1 using I d = unsigned int ;2 class Nodes3 private :4 I d i d ;5 s t d : : a r r ay <double , ndim> coor ;6 public :7 Nodes ( )=default ;8 Nodes ( I d _id , double _x , double _y) ;9 I d g e t I d ( ) ;

10 s t d : : a r r ay <double , ndim> getCoor ( ) ;11 s t d : : a r r ay <double , ndim> MidPoint ( Nodes & v1 ) ;12 ~Nodes ( ) ;13 ;14 class Tr i a n g l e 15 private :16 s t d : : a r r ay <Id , v e r t > t r i a n g l e _ v e r t i c e s ;17 public :18 Tr i a n g l e ( )=default ;19 Tr i a n g l e ( I d _id1 , I d _id2 , I d _id3 ) ;20 s t d : : a r r ay <Id , v e r t > ge tVe r t ( ) ;21 ~T r i a n g l e ( ) ;22 ;

For class Nodes, getCoor returns the coordinates of the node, while MidPoint returnsthe coordinates of the midpoint between two nodes. Method getVert of class Triangle

returns the Id of the nodes that compose the element. Now we can define the classMesh2D, where there are the methods we need to read the mesh and construct thematrix of nodes and the connectivity matrix. The class is the following

1 using VecN = std : : v e c to r <Nodes >;2 using VecT = s td : : v e c to r <Tr i ang l e >;3 class Mesh2D4 private :5 unsigned int nNodes , nElem ;6 VecN ve r t e x_coo r d i n a t e s ;7 VecT e l em_ve r t i c e s ;8 public :9 Mesh2D ( )=default ;

10 Mesh2D(unsigned int nN , unsigned int nE) ;11 unsigned int getNodes ( ) ;12 unsigned int getElem ( ) ;

4

Page 7: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.1. Mesh

13 void setNodesElem ( ) ;14 void setMesh ( ) ;15 void setMesh_nN_nE ( ) ;16 VecN ge tVe r t e xCoo r d i n a t e s ( ) ;17 VecT ge tE l emVe r t i c e s ( ) ;18 ~Mesh2D ( ) ;19 ;

where nNodes and nElem denote the number of nodes and elements, respectively;vertex_coordinates contains the list of nodes, while elem_vertices is the connectivitymatrix. To read the mesh, we start setting the number of nodes and elements fromne.txt ; this is implemented in setNodesElem method:

1 void Mesh2D : : setNodesElem ( ) 2 s t d : : i f s t r e am n e_ f i l e ("mesh/ne . txt" ) ;3 n e_ f i l e >> nNodes >> nElem ;4 n e_ f i l e . c l o s e ( ) ;5 . . .6 ;

std :: ifstream requires that we have included fstream. To compute vertex_coordinates

and elem_vertices, we have to read from vc.txt and ev.txt, respectively:

1 void Mesh2D : : setMesh ( ) 2 setNodesElem ( ) ;3 v e r t e x_coo r d i n a t e s . r e s e r v e ( getNodes ( ) ) ;4 e l em_ve r t i c e s . r e s e r v e ( getElem ( ) ) ;5

6 I d tmp_id ;7 double tmp_x , tmp_y ;8 unsigned int v e r = 0 ;9 s t d : : i f s t r e am v c_ f i l e ("mesh/vc . txt" ) ;

10 while ( ! v c_ f i l e . e o f ( ) && ve r !=getNodes ( ) ) 11 v c_ f i l e >> tmp_id >> tmp_x >> tmp_y ;12 Nodes node ( tmp_id , tmp_x , tmp_y) ;13 v e r t e x_coo r d i n a t e s . emplace_back ( node ) ;14 v e r++;15 . . .16 17 v c_ f i l e . c l o s e ( ) ;18

19 I d tmp_id1 , tmp_id2 , tmp_id3 ;20 unsigned int e l = 0 ;21 s t d : : i f s t r e am e v_ f i l e ("mesh/ev . txt" ) ;

5

Page 8: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

22 while ( ! e v_ f i l e . e o f ( ) && e l !=getElem ( ) ) 23 e v_ f i l e >> tmp_id1 >> tmp_id2 >> tmp_id3 ;24 Tr i a n g l e t r i a ( tmp_id1 , tmp_id2 , tmp_id3 ) ;25 e l em_ve r t i c e s . emplace_back ( t r i a ) ;26 e l++;27 . . .28 29 e v_ f i l e . c l o s e ( ) ;30

getNodes and getElem return the number of nodes and the number of elements,respectively, while getVertexCoordinates returns the list of nodes and getElemVertices

returns the connectivity matrix. Note that, we can give the number of nodes andelements directly to the constructor of Mesh2D: in this case we do not need ne.txtand we can use the method setMesh_nN_nE, that is similar to setMesh. In thismanner, the mesh is read from files and we have defined the matrices of nodes andelements we need to proceed to the next step.

2.2 Implementation of linear finite element on a

fixed mesh

In this section, we present how to compute matrices we need to solve the Allen-Cahnequation implementing linear finite element on the mesh got at the previous step.This part of code is contained in matrices.hpp, where it has been defined the classMatrices in the following way:

1 class Mat r i c e s 2 private :3 unsigned int nNodes ;4 Spar seMat r i x<double> S t i f f n e s s ;5 Spar seMat r i x<double> Mass ;6 VectorXd RHS ;7 public :8 Mat r i c e s ( )=default ;9 Mat r i c e s (unsigned int _nN) ;

10 Matr ix2d B_Mat(Mesh2D & mesh , T r i a n g l e & t r i a ) ;11 double e l_area ( Matr ix2d & B) ;12 Matr ix3d L o c a l S t i f f n e s s M a t r i x (Mesh2D & mesh , T r i a n g l e & t r i a ) ;13 Matr ix3d Loca lMassMatr i x (Mesh2D & mesh , T r i a n g l e & t r i a ) ;

6

Page 9: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.2. Implementation of linear finite element on a fixed mesh

14 Vector3d Loca lRhsVec to r (Mesh2D & mesh , T r i a n g l e & t r i a , double & t );

15 void c ompu t eS t i f f n e s s (Mesh2D & mesh ) ;16 void computeMass (Mesh2D & mesh ) ;17 void computeRHS (Mesh2D & mesh , double & t ) ;18 void computeAl l (Mesh2D & mesh ) ;19 Spar seMat r i x<double> g e t S t i f f n e s s ( ) ;20 Spar seMat r i x<double> getMass ( ) ;21 VectorXd getRHS ( ) ;22 ~Mat r i c e s ( ) ;23 ;

To define stiffness and mass matrices and the right-hand side vector, we use theEigen library, useful to solve linear systems (to use the utilities of Eigen, we haveto include the header Eigen/Dense and Eigen/Sparse). To better understand how tocompute these matrices, let us consider the following example

−∆u+ u = f in Ω, (2.1)

with homogeneous Neumann condition on the boundary ∂Ω. The weak formulationreads: find u ∈ H1(Ω) such that:∫

Ω

∇u · ∇v dx+

∫Ω

uv dx =

∫Ω

fv dx ∀v ∈ H1(Ω). (2.2)

We define the bilinear form L(·, ·) : H1(Ω)×H1(Ω)→ R as

L(u, v) =

∫Ω

∇u · ∇v dx+

∫Ω

uv dx,

and the linear functional F(v) : H1(Ω)→ R as

F(v) =

∫Ω

fv dx.

Consider now a triangulation T of Ω and let XT = v ∈ C(Ω) : v|κ ∈ P1(κ), ∀κ ∈ T ,where κ in a generic element of T . In this manner each function v ∈ XT is determinedby its value at all vertices. The finite element formulation reads: find uh ∈ XT suchthat L(uh, vh) = F(vh) ∀vh ∈ XT .Denoting with NT the total number of nodes in the triangulation, we consider now

7

Page 10: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

Figure 2.1: Tranformation map Fκ from the reference element κ to the genericelement κ.

a nodal basis φjNTj=1 of XT of functions such that

φj ∈ XT : φj(xi) = δij i, j = 1, ..., NT ,

where xi, i = 1, ..., NT denote the vertices of the triangulation. Writing u =∑NTj=1 ujφj, choosing v = φi, i = 1, ..., NT and exploiting the bilinearity of L, we

obtainNT∑j=1

ujL(φj, φi) = F(φi) i = 1, ..., NT ⇒ Lu = f ,

where L is a square matrix with entries Lij = L(φi, φj), f is a vector such thatfi = F(φi). Let us observe that:∫

Ω

∇φi∇φj dx =∑κ∈T

∫κ

∇φi∇φj,∫

Ω

φiφj dx =∑κ∈T

∫κ

φiφj,

∫Ω

fφi dx =∑κ∈T

∫κ

fφi dx,

where κ ⊂ supp(φi) ∩ supp(φj).Now we can start to assemble matrices and right-hand side: we have to loop overelements and for each element we compute all the integrals that are nonzero at theelement and add the computed integrals at the proper positions of the matrices.Before going on, we distinguish the global numbering from the local numbering: thefirst one is referred to the Id of the node, that we find in vertex_coordinates, while thesecond one is referred only on the current triangle and it is useful to assemble localmatrices and vectors: because we are managing with triangles the local numbering

8

Page 11: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.2. Implementation of linear finite element on a fixed mesh

of nodes will be 1, 2, 3 (see Figure 2.1).We want to compute now the stiffness matrix A ∈ RNT ×NT . Let viκ, i = 1, 2, 3,denote the vertex coordinates of the element κ with the local numbering. ThenFκ : κ→ κ is the map from the reference element κ to κ defined as:

Fκ = v1κ + x1(v2

κ − v1κ) + x2(v3

κ − v1κ) = v1

κ +[v2κ − v1

κ v3κ − v1

κ

] [x1

x2

].

We define the matrix B =[v2κ − v1

κ v3κ − v1

κ

]and we observe that 2|κ| = det(B),

so the element measure can be computed exploiting B. B_mat and el_area methodsprovide these informations

1 Matr ix2d Mat r i c e s : : B_Mat(Mesh2D & mesh , T r i a n g l e & t r i a ) 2 Nodes v1 , v2 , v3 ;3 v1 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 0 ] −1 ] ;4 v2 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 1 ] −1 ] ;5 v3 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 2 ] −1 ] ;6 Matr ix2d B;7 B << v2 . getCoor ( ) [0]− v1 . getCoor ( ) [ 0 ] , v3 . getCoor ( ) [0]− v1 . getCoor ( )

[ 0 ] ,8 v2 . getCoor ( ) [1]− v1 . getCoor ( ) [ 1 ] , v3 . getCoor ( ) [1]− v1 . getCoor ( ) [ 1 ] ;9 return B;

10 ;11

12 double Mat r i c e s : : e l_area ( Matr ix2d & B) 13 return 0 .5∗ s t d : : abs (B . de t e rm inan t ( ) ) ;14 ;

If we define the basis functions on the reference element φi : κ→ R as

φ1(x1, x2) = 1− x1 − x2, φ1(x1, x2) = x1, φ1(x1, x2) = x2, (2.3)

then ϕiκ = φi F−1κ and φi = ϕi Fκ. By the chain rule we obtain

∂φi∂xj

=∑`

∂ϕi∂x`

∂Fκ,`∂xj

=∑`

∂ϕi∂x`

B`j = colj(B) · ∇ϕi.

Thus we can write the gradients as columns:

∇φi = BT∇ϕi or ∇ϕi = B−T∇φi.

9

Page 12: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

Then we can write

∇ϕj · ∇ϕi = ∇ϕTj∇ϕi = ∇φT

j B−1B−T∇φi,

and finally we obtain, as B is constant,∫κ

∇ϕj · ∇ϕi dx =

∫κ

∇φTj B−1B−T∇φi|det(B)| dx =

|det(B)|2

∇φTj B−1B−T∇φi

From (2.3) we can compute ∇φj, j = 1, 2, 3:

∇φ1 =

[−1

−1

], ∇φ2 =

[1

0

], ∇φ3 =

[0

1

]

Defining grd_bas_fcts =

[−1 1 0

−1 0 1

], we can compute the local stiffness matrix

LocalStiffMat =|det(B)|

2grd_bas_fctsT (B−1B−T) grd_bas_fcts,

that is a 3× 3 matrix.

1 Matr ix3d Mat r i c e s : : L o c a l S t i f f n e s s M a t r i x (Mesh2D & mesh , T r i a n g l e & t r i a )

2 Matr ix2d B, B_inv , B_inv_t ;3 B = B_Mat(mesh , t r i a ) ;4 B_inv = B. i n v e r s e ( ) ;5 B_inv_t = B_inv . t r a n s p o s e ( ) ;6 Matr ix<double ,2 ,3 > grd_bas_fcts ;7 grd_bas_fcts << −1.0 , 1 . 0 , 0 . 0 ,8 −1.0 , 0 . 0 , 1 . 0 ;9 Matr ix<double ,3 ,2 > grd_bas_fcts_t ;

10 grd_bas_fcts_t = grd_bas_fcts . t r a n s p o s e ( ) ;11 Matr ix3d LocS t i f fMa t ;12 LocS t i f fMa t = grd_bas_fcts_t ∗ ( B_inv ∗ B_inv_t ) ∗ grd_bas_fcts ∗

e l_area (B) ;13 return LocS t i f fMa t ;14 ;

Finally, we have to store the elements of LocalStiffMat at the right position in thestiffness matrix: the entry Aij is simply the sum of the various contributes fromlocal stiffness matrices; for example, let us consider the Figure 2.1: the element

10

Page 13: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.2. Implementation of linear finite element on a fixed mesh

(1,1) of the LocalStiffMatrix will go in position (48,48) of the A, while the element(2,3) of the local stiffness matrix will go in position (57,6) of the stiffness matrix.The implementation of this part is contained in computeStiffness method (keepingin mind that the stiffness matrix is set to zero when the constructor of Mesh2D iscalled; the same thing for the mass matrix)

1 void Mat r i c e s : : c ompu t eS t i f f n e s s (Mesh2D & mesh ) 2 Matr ix3d tmp ;3 for ( T r i a n g l e Tr i a : mesh . g e tE l emVe r t i c e s ( ) ) 4 tmp = L o c a l S t i f f n e s s M a t r i x (mesh , T r i a ) ;5 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) +=

tmp (0 , 0 ) ;6 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) +=

tmp (0 , 1 ) ;7 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) +=

tmp (1 , 0 ) ;8 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) +=

tmp (0 , 2 ) ;9 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) +=

tmp (2 , 0 ) ;10 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) +=

tmp (1 , 1 ) ;11 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) +=

tmp (1 , 2 ) ;12 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) +=

tmp (2 , 1 ) ;13 S t i f f n e s s . c o e f f R e f ( Tr i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) +=

tmp (2 , 2 ) ;14 15 ;

The mass matrix M ∈ RNT ×NT is easier to compute: we use the midpoint rule, thatmeans, for i, j = 1, 2, 3∫

κ

ϕiϕi dx =|κ|3

(1

2

1

2+

1

2

1

2+ 00

)=|κ|6,

∫κ

ϕiϕj dx =|κ|3

(1

2

1

2+

1

20 + 00

)=|κ|12

if i 6= j.

11

Page 14: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

Therefore, the local mass matrix is defined in the following way:

LocalMassMat = |κ|

16

112

112

112

16

112

112

112

16

,The implementation of the local mass matrix is the following

1 Matr ix3d Mat r i c e s : : Loca lMassMatr i x (Mesh2D & mesh , T r i a n g l e & t r i a ) 2 Matr ix2d B = B_Mat(mesh , t r i a ) ;3 Matr ix3d LocMassMat ;4 LocMassMat << 1 . 0 /6 . 0 , 1 . 0 /12 . 0 , 1 . 0 /12 . 0 ,5 1 . 0 /12 . 0 , 1 . 0 / 6 . 0 , 1 . 0 /12 . 0 ,6 1 . 0 /12 . 0 , 1 . 0 /12 . 0 , 1 . 0 / 6 . 0 ;7 LocMassMat ∗= el_area (B) ;8 return LocMassMat ;9 ;

As for the stiffness matrix, we are able to store the elements of LocalMassMat in M ,through the global numbering; the method is the following

1 void Mat r i c e s : : computeMass (Mesh2D & mesh ) 2 Matr ix3d tmp ;3 for ( T r i a n g l e Tr i a : mesh . g e tE l emVe r t i c e s ( ) ) 4 tmp = Loca lMassMatr i x (mesh , T r i a ) ;5 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) += tmp

(0 , 0 ) ;6 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) += tmp

(0 , 1 ) ;7 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) += tmp

(1 , 0 ) ;8 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [0 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) += tmp

(0 , 2 ) ;9 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [0 ]−1) += tmp

(2 , 0 ) ;10 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) += tmp

(1 , 1 ) ;11 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [1 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) += tmp

(1 , 2 ) ;12 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [1 ]−1) += tmp

(2 , 1 ) ;13 Mass . c o e f f R e f ( T r i a . g e tVe r t ( ) [2 ]−1 , Tr i a . g e tVe r t ( ) [2 ]−1) += tmp

(2 , 2 ) ;

12

Page 15: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.2. Implementation of linear finite element on a fixed mesh

14 15 ;

The method computeAll provide to compute both stiffness and mass matrix duringthe same loop over elements.For the right-hand side, we use the quadrature formula of the midpoint rule, i.e.

f 1κ =|κ|3

[1

2f(m12) + 0f(m23) +

1

2f(m31)

],

f 2κ =|κ|3

[1

2f(m12) +

1

2f(m23) + 0f(m31)

],

f 3κ =|κ|3

[0f(m12) +

1

2f(m23) +

1

2f(m31)

],

where mij represents the midpoint between node i and j, for i, j = 1, 2, 3 (usinglocal numbering) and |κ| is the measure of the element κ. In LocalRhsVector methodwe have:

1 Vector3d Mat r i c e s : : Loca lRhsVec to r (Mesh2D & mesh , T r i a n g l e & t r i a ,double & t )

2 Nodes v1 , v2 , v3 ;3 v1 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 0 ] −1 ] ;4 v2 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 1 ] −1 ] ;5 v3 = mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ t r i a . g e tVe r t ( ) [ 2 ] −1 ] ;6 s t d : : a r r ay <double , ndim> m12 , m13 , m23 ;7 m12 = v1 . MidPoint ( v2 ) ;8 m13 = v1 . MidPoint ( v3 ) ;9 m23 = v2 . MidPoint ( v3 ) ;

10 Matr ix2d B = B_Mat(mesh , t r i a ) ;11 double g1 = ( g (m12 , t )+g (m13 , t ) ) ∗ 0 . 5 ;12 double g2 = ( g (m12 , t )+g (m23 , t ) ) ∗ 0 . 5 ;13 double g3 = ( g (m13 , t )+g (m23 , t ) ) ∗ 0 . 5 ;14 Vector3d LocalRHS ;15 LocalRHS << g1 , g2 , g3 ;16 LocalRHS ∗= el_area (B) / 3 . 0 ;17 return LocalRHS ;18 ;

With this method we compute the local vector. Now we want to store it in the realright-hand side, so we implement the following method:

1 void Mat r i c e s : : computeRHS (Mesh2D & mesh , double & t ) 2 RHS. s e tZe r o ( ) ;

13

Page 16: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

3 Vector3d tmp ;4 for ( T r i a n g l e Tr i a : mesh . g e tE l emVe r t i c e s ( ) ) 5 tmp = Loca lRhsVec to r (mesh , Tr ia , t ) ;6 RHS. c o e f f R e f ( T r i a . g e tVe r t ( ) [0]−1) += tmp (0) ;7 RHS. c o e f f R e f ( T r i a . g e tVe r t ( ) [1]−1) += tmp (1) ;8 RHS. c o e f f R e f ( T r i a . g e tVe r t ( ) [2]−1) += tmp (2) ;9

10 ;

Note that, everytime computeRHS is called, the elements of right-hand side are setto zero, otherwise we sum the local right-hand side on the right-hand side computedat the previous time step and so on (this will be more clear later on).

2.3 Non linearity approximation

We go back now to the Allen-Cahn equation. The code described in this section iscontained in nonlinear.hpp. Here, it is defined the class Newton:

1 class Newton2 protected :3 double t o l l e r a n c e ;4 unsigned int max_iter ;5 bool conve rged ;6 public :7 Newton ( ) ;8 Newton (double _to l l ) ;9 Newton (double _to l l , unsigned int _max_iter ) ;

10 ~Newton ( ) ;11 double Re s i d u a l ( VectorXd & v1 , VectorXd & v2 ) ;12 bool Convergence ( VectorXd & v1 , VectorXd & v2 ) ;13 ;

Default values for tollerance and maximum number of iterations of Newton’s methodare assigned, but it is also possible to give different values through different con-structors. Methods Residual and Convergence are defined in the following way:

1 double Newton : : R e s i d u a l ( VectorXd & v1 , VectorXd & v2 ) 2 return s t d : : s q r t ( ( v1−v2 ) . dot ( v1−v2 ) ) ;3 ;4 bool Newton : : Convergence ( VectorXd & v1 , VectorXd & v2 ) 5 conve rged = Re s i d u a l ( v1 , v2 )<t o l l e r a n c e ? true : fa l se ;6 return conve rged ;

14

Page 17: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.3. Non linearity approximation

7 ;

Residual allows to compute the residual between two vectors and Convergence returnstrue if the residual is less than the tollerance.Starting from (1.2) and choosing vh ∈ XT , we have the following semi-implicitapproximation: given u0

h ∈ XT , find ukh(t) ∈ XT for every t ∈ [0, T ] such that

〈∂tukh(t), vh〉+ (∇ukh(t),∇vh) + ε−2(G(ukh(t), uk−1h ), vh) = 0, ∀vh ∈ XT , (2.4)

where G(·, ·) : R × R → R is a continuos function and it is assumed to provide aconsistent approximation of the nonlinear function f(·), in the sense that G(u, u) =

f(u). Carrying out one iteration of a Newton scheme in every time step of the Eulerimplicit scheme with initial guess uk−1

h corresponds to the linearization

Glin(uk, uk−1) = f(uk−1) + f ′(uk−1)(uk − uk−1).

Using this approximation in (2.4) we obtain the following scheme:

〈∂tukh(t), vh〉+ (∇ukh(t),∇vh) + ε−2(f ′(ukh(t)), vh) =

= −ε−2(f(uk−1h (t)), vh) + ε−2(f ′(uk−1

h (t))ukh(t), vh).(2.5)

Now we can define the class AllenCahn_Linearized from the class Newton:

1 class Al l enCahn_L inea r i z ed : protected Newton2 private :3 double eps ;4 double de l t a_t ;5 public :6 Al l enCahn_L inea r i z ed ( )=default ;7 Al l enCahn_L inea r i z ed (double _eps , double _delta_t ) ;8 Al l enCahn_L inea r i z ed (double _eps , double _delta_t , double _to l l ) ;9 Al l enCahn_L inea r i z ed (double _eps , double _delta_t , unsigned int

_max_iter ) ;10 Al l enCahn_L inea r i z ed (double _eps , double _delta_t , double _to l l ,

unsigned int _max_iter ) ;11 VectorXd Newton_AC(Mesh2D & mesh , Mat r i c e s & mat , VectorXd & uh0 ,

double & t ) ;12 ~A l l enCahn_L inea r i z ed ( ) ;13 ;

15

Page 18: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2. Numerical approximation

The attributes of this class are the parameter ε and ∆t; as for the class Newton, itis possible to give in input of the constructor also the tollerance and the maximumnumber of iterations. The Newton’s method of problem (2.5) is implemented inNewton_AC as follow:

1 VectorXd A l l enCahn_L inea r i z ed : : Newton_AC(Mesh2D & mesh , Mat r i c e s & mat ,VectorXd & uh0 , double & t )

2 unsigned int i t e r = 0 ;3 mat . computeRHS (mesh , t ) ;4

5 VectorXd u_n(mesh . getNodes ( ) ) , u_n1(mesh . getNodes ( ) ) , uo ld (mesh .getNodes ( ) ) ;

6 Spar seMat r i x<double> A(mesh . getNodes ( ) ,mesh . getNodes ( ) ) ;7 VectorXd b (mesh . getNodes ( ) ) ;8 VectorXd tmp1 (mesh . getNodes ( ) ) , tmp2 (mesh . getNodes ( ) ) ;9 u_n = uh0 ; u_n1 . f i l l ( 0 ) ; uo ld = u_n ;

10 conve rged = fa l se ;11

12 while ( ! Convergence ( uold , u_n1) && i t e r <max_iter ) 13 i t e r ++;14 tmp1 = df (u_n) ; tmp2 = ElemWiseProd ( tmp1 , u_n) ;15 A = mat . getMass ( ) / de l t a_t + mat . g e t S t i f f n e s s ( ) + mat . getMass ( ) ∗

d i ag ( tmp1 ) /( eps ∗ eps ) ;16 b = mat . getMass ( ) ∗uh0/ de l t a_t − mat . getMass ( ) ∗ f (u_n) /( eps ∗ eps ) +

mat . getMass ( ) ∗tmp2/( eps ∗ eps ) + mat . getRHS ( ) ;17

18 UmfPackLU<Spar seMat r i x<double> > s o l v e r ;19 s o l v e r . compute (A) ;20 i f ( s o l v e r . i n f o ( ) !=Succe s s )21 throw s t d : : r un t ime_er ro r ("Decomposition fa i led" ) ;22 u_n1 = s o l v e r . s o l v e ( b ) ;23 i f ( s o l v e r . i n f o ( ) !=Succe s s )24 throw s t d : : r un t ime_er ro r ("Solving fa i led" ) ;25

26 uo ld = u_n ;27 u_n = u_n1 ;28 29 return u_n1 ;30 ;

Here u_n denotes the old solution of Newton’s iterations, while u_n1 is the vector ofunknowns that we have to compute in every iteration of Newton’s scheme. Note that,

16

Page 19: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

2.4. Time stepping scheme and problem data

if we do not have an homogeneous equation, but there is a function f(x, t) differentfrom zero, for every time step we have to compute the vector of right-hand side (thisis the reason for which in method computeRHS we set the line RHS.setZero();). Tosolve the linear system, we use decomposition LU with UmfPack solver providedby the Eigen library. At the end of Newton’s step, we have to update the oldsolution (we use the variable uold to be able to compute the residual when testingthe convergence).

2.4 Time stepping scheme and problem data

Starting from (2.5), in main.cpp we write the time stepping scheme in the followingway:

1 Al l enCahn_L inea r i z ed AC_L( eps , d e l t a t ) ;2 VectorXd u_k(mesh . getNodes ( ) ) ;3 for (double t = t0+d e l t a t ; t <= T+d e l t a t /2 ; t += d e l t a t ) 4 u_k = AC_L. Newton_AC(mesh , Mat , uh0 , t ) ;5 uh0 = u_k ;6 . . .7

where u_k represents the solution at time t computed with Newton’s iterations anduh0 is the initial condition that is updated at every time step. t0, T and deltat

are global variables defined in the file data.cpp, where there are all the data of theproblem. It is possible to define also a value for tollerance and maximun numberof iteration if we do not want to use the default values. In this file, there are alsocontained a function for the initial condition and a function for the right-hand side.Moreover, it is possible to modify the non linearity of the problem and then its firstderivative to make the code more generic for other different problems.

1 VectorXd f ( VectorXd & u ) 2 return Cube ( u ) − u ;3 4 VectorXd d f ( VectorXd & u ) 5 VectorXd one ( u . s i z e ( ) ) ;6 one . f i l l ( 1 . 0 ) ;7 return 3 .0∗ Square ( u )−one ;8

where Square and Cube return the power two and three of the vector u component-wise, repectively; these functions are provided in file Algebra.hpp.

17

Page 20: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Chapter 3

Numerical results

3.1 Concentric circle

In this example, we consider as initial condition u0 a function costituted of twocircles centered at the origin. Let Ω := (−2, 2)2, set r1 = 0.4 and r2 = 1 and definedj(x) = |x| − rj for x ∈ Ω and j = 1, 2. For given ε > 0 and x ∈ Ω let

u0(x) := −tanh(d(x)√

), d(x) := max−d1(x), d2(x).

In data.cpp we used the following function

1 VectorXd u_h_0(Mesh2D & mesh ) 2 VectorXd uh0 (mesh . getNodes ( ) ) ; uh0 . s e tZe r o ( ) ;3 double r1 = 0 . 4 ; double r2 = 1 . 0 ;4 double d1 = 0 . 0 ; double d2 = 0 . 0 ; double d = 0 . 0 ;5 for (unsigned int i = 0 ; i<mesh . getNodes ( ) ; i++)6 d1 = −(norm (mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] ) − r1 ) ;7 d2 = norm (mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] ) − r2 ;8 d = s td : : max( d1 , d2 ) ;9 uh0 [ i ] = −s t d : : tanh ( d/( s td : : s q r t (2 ) ∗ eps ) ) ;

10 11 return uh0 ;12

Snapshots of the evolution defined by the previous initial data for ε = 1/16 areshown in Figure 3.1. We can compare these results with those obtained by Bartels

18

Page 21: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3.1. Concentric circle

et al.1: we can see that the interface’s evolutions are very similar in both cases at thesame times. The mesh used in this case consist of 10145 nodes and 19968 elements,while the time step is ∆t = 10−4, that is good for the numerical stability.

Figure 3.1: Snapshot of the evolution for t = 0.0, 0.06, 0.12, 0.29, 0.48, 0.5 andε = 1/16 (from left to the righ and top to bottom). At the top the numericalsolution obtained with the C++ code, while at the bottom the results obtained byBartels et al.

1Bartels, S., Müller, R., Ortner, C. (2011). Robust a priori and a posteriori error analysis forthe approximation of Allen-Cahn and Ginzburg-Landau equations past topological changes. SIAMJournal on Numerical Analysis, 49(1), 110-134.

19

Page 22: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3. Numerical results

3.2 Dumbbell

In this second example, let Ω := (−2, 2)2, define m1 = [0, 2], m2 = [0, 0] and m3 =

[0,−2]. For given ε > 0, let r1 = r3 := 2−3ε/2, r2 = 1 and set dj(x) = |x−mj|− rjfor x ∈ Ω and j = 1, 2, 3. For x ∈ Ω let

u0(x) := −tanh(d(x)√

), d(x) := max−d1(x), d2(x),−d3(x).

In data.cpp we defined the initial condition as

1 VectorXd u_h_0(Mesh2D & mesh ) 2 VectorXd uh0 (mesh . getNodes ( ) ) ; uh0 . s e tZe r o ( ) ;3 double r1 = 2.0−1.5∗ eps ; double r2 = 1 . 0 ; double r3 = 2.0−1.5∗ eps ;4 s t d : : a r r ay <double ,2> m1 = 0 ,2 ; s t d : : a r r ay<double ,2> m3 = 0 ,−2;5 double d1 = 0 . 0 ; double d2 = 0 . 0 ; double d3 = 0 . 0 ; double d = 0 . 0 ;6 for (unsigned int i = 0 ; i<mesh . getNodes ( ) ; i++)7 d1 = −( no rmd i f f (mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] ,m1) − r1 ) ;8 d2 = norm (mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] ) − r2 ;9 d3 = −( no rmd i f f (mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] ,m3) − r3 ) ;

10 d = s td : : max( s td : : max( d1 , d2 ) , d3 ) ;11 uh0 [ i ] = −s t d : : tanh ( d/( s td : : s q r t (2 ) ∗ eps ) ) ;12 13 return uh0 ;14

In Figure 3.2 we display snapshots of the evolution: within a short time interval, theinitially connected interface splits into two curves; then the two component of theinterface develop circular shapes and eventually the diameters of the two particlesdecrease to zero until they collapse. Also here it is possible to see that the twosolutions are very similar at fixed times. For this example, we used the same meshof the previous case and the same ∆t.

20

Page 23: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3.3. Non homogeneous problem

Figure 3.2: Snapshot of the evolution for t = 0.0, 0.012, 0.024, 0.073, 0.122, 0.146and ε = 1/16 (from left to the righ and top to bottom). At the top the numericalsolution obtained with the C++ code, while at the bottom the results obtained byBartels et al.

3.3 Non homogeneous problem

In this example we want to test the validity of the code: let Ω := (−2, 2)2, we choosethe following analitycal solution

u(x, t) := e−100tcos(πx)cos(πy),

which has homogeneous Neumann condition on the boundary and provides the fol-lowing right-hand side:

g(x, t) = e−100tcos(πx)cos(πy)[−100 + 2π2 + ε−2(e−200tcos(πx)2cos(πy)2 − 1)

].

In data.cpp we have

1 VectorXd u_h_0(Mesh2D & mesh ) 2 VectorXd uh0 (mesh . getNodes ( ) ) ; uh0 . s e tZe r o ( ) ;3 for (unsigned int i = 0 ; i<mesh . getNodes ( ) ; i++)

21

Page 24: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3. Numerical results

4 uh0 [ i ] = s td : : cos (M_PI∗mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] . getCoor ( ) [ 0 ] )∗ s t d : : cos (M_PI∗mesh . g e tVe r t e xCoo r d i n a t e s ( ) [ i ] . getCoor ( ) [ 1 ] ) ;

5 return uh0 ;6 7 double g ( s td : : a r r ay <double ,2> & x , double & t ) 8 return s t d : : exp (−100.0∗ t ) ∗ s t d : : cos (M_PI∗x [ 0 ] ) ∗ s t d : : cos (M_PI∗x [ 1 ] )

∗(−100.0+2.0∗M_PI∗M_PI+( s td : : exp (−200.0∗ t ) ∗ s t d : : cos (M_PI∗x [ 0 ] ) ∗ s t d: : cos (M_PI∗x [ 0 ] ) ∗ s t d : : cos (M_PI∗x [ 1 ] ) ∗ s t d : : cos (M_PI∗x [ 1 ] ) −1.0) /( eps ∗eps ) ) ;

9

We considered a mesh with 10145 nodes and 19968 elements, with ∆t = 10−4 andε = 0.2. As we can see from Figure 3.3, the approximation of the exact solution isvery good for each time step. The maximum error at the final step, computed withthe sup-norm, is w 3 · 10−3 after 500 time steps.

Figure 3.3: Snapshot of the evolution for t = 0.0, 0.01, 0.02, 0.03, 0.04, 0.05 andε = 0.2 (from left to the right and top to bottom). Blue points represent thenumerical solution, while red star the exact one.

22

Page 25: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3.4. A different non linearity

3.4 A different non linearity

In this last example, we want to test the generality of this code changing the nonlinear function f(·). We choose F (u) = (u2 − 1)3/6 and f(u) = u5 − 2u3 + u.The solution is the same of the previous example, while the right-hand side is thefollowing:

g(x, t) = e−100tcos(πx)cos(πy)[− 100 + 2π2 + ε−2(e−400tcos(πx)4cos(πy)4+

− 2e−200tcos(πx)2cos(πy)2 + 1)].

In file data.cpp we substitute the code seen at the end of the previous chapter withthe following

1 VectorXd f ( VectorXd & u ) 2 return F i f t h ( u ) − 2 .0∗Cube ( u ) + u ;3 4 VectorXd d f ( VectorXd & u ) 5 VectorXd one ( u . s i z e ( ) ) ;6 one . f i l l ( 1 . 0 ) ;7 return 5 .0∗ Fourth ( u ) − 6 .0∗ Square ( u ) + one ;8

where Fourth and Fifth return the power four and five of the vector u componentwise,repectively (these functions are provided in file Algebra.hpp). The mesh consideredin this case is the same of the previous example: also here we obtain maximumerror, computed with the sup-norm, of w 3 · 10−3 in 500 time steps.

23

Page 26: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

3. Numerical results

Figure 3.4: Snapshot of the evolution for t = 0.0, 0.01, 0.02, 0.03, 0.04, 0.05 andε = 0.2 (from left to the right and top to bottom). Blue points represent thenumerical solution, while red star the exact one.

24

Page 27: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

Chapter 4

Instructions for use

In this chapter, we will see how to use the code to solve Allen-Cahn equation withdifferent input data. The whole program is constituted by:

• main.cpp: this is the file that will be launched by the Makefile; it contains thevariables of the problem for the mesh and matrices of finite element method.There is also the loop over time and for each time step the solution will beprinted in a file named output.txt, which is necessary for the post-processingof the results.

• data.cpp: in this file we have all the data of the problem (excluding meshdata, which are apart); in this file we can set the initial and the final time ofthe simulation (t0 and T), the time step ∆t and also the tollerance and themaximum number of iteration for Newton’s scheme (toll and max_num_iter).We have to define also the parameter ε of the problem, that is called eps.Moreover, we have to set the non linearity f(u) and its first derivative ∂f

∂u

through the functions f and df. Finally, we have to set the initial condition andthe right-hand side of the equation with the functions uh0 and g, respectively.

• mesh: this is a folder where there are three files for the mesh. As we haveexplained in chapter 1, ne.txt contains the number of nodes and elements,vc.txt is a list of nodes and ev.txt is the connectivity matrix.

• include: this folder contains the four header files of the program: we haveseen reading.hpp, which allows to read the mesh from files and store it invertex_coordinates (list of nodes) and elem_vertices (connectivity matrix);matrices.hpp allows to compute the matrices of the linear finite element such as

25

Page 28: Implementation of a C++ code to solve the Allen-Cahn equationforma/Didattica/ProgettiPacs/Nicolo... · 2020. 4. 22. · Ingegneria Matematica ADVANCEDPROGRAMMINGFOR SCIENTIFICCOMPUTING

4. Instructions for use

the stiffness and the mass matrix and the right-hand side vector; nonlinear.hpp

is the header where is defined the Newton’s method described in chapter 2;algebra.hpp is a header file where there defined useful mathematical functions,such as the componentwise exponentiation of vectors, the elementwise prod-uct between two vectors, the norm of a vector and a function that provides adiagonal matrix which has as components the elements of the vector given ininput.

In this manner, properly setting the parameters contained in data.cpp, we are ableto solve the Allen-Cahn equation with different initial conditions and different nonlinearities.

26