Debian Codegraph @

examples/hpddm/coupled-physics-2d-PETSc.md

raw symbols SCIP
1 ---
2 name: coupledphysics
3 category: Coupled Physics
4 layout: mpi
5 ---
6
7 ## Coupled-physics problem in 2D
8
9 In this example, we will solve a coupled-physics problem with two domains in 2D using domain decomposition, Krylov solvers and LU. The setup consists of two domains that share an interface. On each domain a reaction-diffusion equation is defined,
10
11 $$
12 -\nabla u_1 + \eta u_1 = f_1\text{ on }\Omega_1\\
13 -\nabla u_2 + \eta u_2 = f_2\text{ on }\Omega_2
14 $$
15
16 where $u_1$ and $u_2$ correspond to the local solution on the two domains, $\Omega_1$ and $\Omega_2$, and $f_1$ and $f_2$ are the two corresponding right hand sides. For this example, we assume that $f_1 = f_2 = f$. In addition to the local equations on the two domains, we also define some cross-interface conditions for how the two domains interact,
17
18 $$
19 \left(\frac{1}{\alpha}p_1 + u_1\right) + \left(\frac{1}{\alpha}p_2 - u_2\right) = 0 \text{ on } \Gamma_1\\
20 \left(\frac{1}{\alpha}p_12+ u_2\right) + \left(\frac{1}{\alpha}p_1 - u_1\right) = 0 \text{ on } \Gamma_2
21 $$
22
23 with $\alpha$ a fixed constant Robin parameter, and $p_1$ and $p_2$ are the solutions defined on the interface $\Gamma_1$ and $\Gamma_2$ respectively. The $\alpha$ value is calculated according to the formula,
24
25 $$
26 \alpha = \left[\left(\left(\frac{\pi}{L}\right)^2 + 1\right) \left(\left(\frac{\pi}{h}\right)^2 + 1\right)\right]^{\frac{1}{4}}
27 $$
28
29 ### Weak form
30
31 We turn this into its weak form by using the usual test function approach, resulting in the definition of the local-to-domain problem,
32
33 $$
34 \mathscr{A}_i = \begin{bmatrix}L_{ii} & B_{ii}\\B_{ii}^T & C_{ii}\end{bmatrix}
35 $$
36
37 for $i=1,2$ as given by
38
39 $$
40 L_{ii} \leftarrow \int\int_{\Omega_i}(\nabla u_i \nabla v_i + \eta u_i v_i)\\
41 B_{ii} \leftarrow -\int_{\Gamma_i}p_i v_i \\
42 B_{ii}^T \leftarrow -\int_{\Gamma_i} u_i q_i \\
43 C_{ii} \leftarrow -\frac{1}{\alpha}\int_{\Gamma_i}u_i v_i.
44 $$
45
46 Similarly, the weak form for the cross-interface matrices between $\Omega_1$ and $\Omega_2$ are defined as
47
48 $$
49 B_{12} \leftarrow - \int_{\Gamma_1} p_2v_1\\
50 B_{21} \leftarrow - \int_{\Gamma_2} p_1v_2\\
51 C_{12} \leftarrow - \frac{1}{\alpha} \int_{\Gamma_1} p_2q_1\\
52 C_{21} \leftarrow - \frac{1}{\alpha} \int_{\Gamma_2} p_1q_2.
53 $$
54
55 ### Preparing constants and building the mesh
56
57 First, we include the necessary headers and define some global constants and compute $\alpha$,
58
59 ~~~freefem
60 include "getARGV.idp"
61 load "PETSc"
62 // Controlling the number of dof's
63 int nn = getARGV("-n", 200);
64 // domain 1
65 func Pku1 = P1; func Pkp1 = P1;
66 // domain 2
67 func Pku2 = P1; func Pkp2 = P1;
68 // alpha
69 real tmp1 = pow(pi/2,2) + 1;
70 real tmp2 = pow((nn-1)*pi, 2) + 1;
71 real tmp3 = tmp1*tmp2;
72 real alpha = pow(tmp3, 0.25);
73 ~~~
74
75 Next, we build a mesh (a star in this example) and split it into two halves along the line $x=0$. We also extract the interface meshes along the split:
76
77 ~~~freefem
78 border star(t=0, 2*pi){
79 x = 1.0*(1 + 0.3*sin(5*t))*cos(t);
80 y = 1.0*(1 + 0.3*sin(5*t))*sin(t);
81 }
82 mesh Th = buildmesh(star(nn));
83 mesh Th1 = trunc(Th, x<=0, split=1, label=123);
84 mesh Th2 = trunc(Th, x>=0, split=1, label=123);
85 int[int] labs = [123];
86 meshL ThI1 = extract(Th1, label=labs);
87 meshL ThI2 = extract(Th2, label=labs);
88 ~~~
89
90 ### Domain decomposition
91
92 In order to use domain decomposition, we will take advantage of PETSc's capabilities through the `macro_ddm` script in FreeFEM. However, the datatype `mesh` requires `macro_ddm` to be set up for a 2D volume mesh, whereas the `meshL` datatype requires a 3D line mesh setup. To accommodate this, we will scope the relevant parts of the code doing the decomposition. To prepare for this, we need to prepare some variables that will get defined inside of those scoped regions but are also needed outside of them:
93
94 ~~~freefem
95 Mat L11, L22;
96 Mat C11, C22;
97 Mat interpI12, interpI21;
98
99 fespace Ph1(Th1, P0);
100 fespace Ph2(Th2, P0);
101 Ph1 part1;
102 Ph2 part2;
103 ~~~
104
105 The `fespace`s defined will hold the decomposition which will allow us to reuse the decomposition of the surface mesh in the line mesh along the interface. Next, we decompose our surface meshes using a custom partitioning performed with `metis`, storing the partitioning in `part1` and `part2`:
106
107 ~~~freefem
108 {
109 macro dimension()2// EOM
110 macro meshtype()V// EOM
111 macro partitioner()metis//
112 include "macro_ddm.idp"
113 partitionerSeq(part1[], Th1, mpisize);
114 partitionerSeq(part2[], Th2, mpisize);
115 partitionerPar(part1[], Th1, mpiCommWorld, mpisize);
116 partitionerPar(part2[], Th2, mpiCommWorld, mpisize);
117 macro Th1UserPartitioning()part1[]// EOM
118 macro Th2UserPartitioning()part2[]// EOM
119 MatCreate(Th1, L11, Pku1);
120 MatCreate(Th2, L22, Pku2);
121 }
122 ~~~
123
124 Following this, `L11` and `L22` are distributed PETSc matrices, according to the decomposition, that we will later fill with the relevant data. We now will interpolate the decomposition along the interface in order to have matching decompositions along the interface meshes:
125
126 ~~~freefem
127 fespace PhI1(ThI1, P0);
128 fespace PhI2(ThI2, P0);
129 PhI1 partI1 = part1;
130 PhI2 partI2 = part2;
131 ~~~
132
133 There are also certain operations that are not defined if a local subdomain does not include any part of the interface,
134
135 ~~~freefem
136 int[int] labs1 = labels(Th1);
137 int[int] labs2 = labels(Th2);
138 int hasI1 = 0, hasI2 = 0;
139 for(int i = 0; i < labs1.n; i++) if(labs1[i] == 123) hasI1 = 1;
140 for(int i = 0; i < labs2.n; i++) if(labs2[i] == 123) hasI2 = 1;
141 ~~~
142
143 We can now decompose the interface meshes in a similar way as we did with the surface meshes. Since we already have a partitioning, however, we don't need to create a new one,
144
145 ~~~freefem
146 {
147 macro dimension()3// EOM
148 macro meshtype()L// EOM
149 include "macro_ddm.idp"
150 macro ThI1UserPartitioning()partI1[]// EOM
151 macro ThI2UserPartitioning()partI2[]// EOM
152 MatCreate(ThI1, C11, Pkp1);
153 MatCreate(ThI2, C22, Pkp2);
154 MatInterpolate(ThI1, Pkp1, C11, ThI2, Pkp2, C22, interpI21);
155 MatInterpolate(ThI2, Pkp2, C22, ThI1, Pkp1, C11, interpI12);
156 }
157 ~~~
158
159 In this scoped section, we also set up two interpolation matrices in order to move from the interface mesh in one of $\Omega_1$ or $\Omega_2$ to the other. Since the decompositions of the two domains is not assumed to match along the interface, we will need to set up our cross-interface matrices in one of the domains and project it across the interface as we will see later.
160
161 With all the meshes decomposed, we can now set up our distributed finite element spaces,
162
163 ~~~freefem
164 fespace Vh1(Th1, Pku1);
165 fespace Vh2(Th2, Pku2);
166 fespace VhI1(ThI1, Pkp1);
167 fespace VhI2(ThI2, Pkp2);
168 ~~~
169
170 ### Problem specification
171
172 We define our problem, including the corresponding right-hand side, in the usual FreeFEM manner,
173
174 ~~~freefem
175 func f = x^2*y*100;
176 macro grad(u) [dx(u),dy(u)] // EOM
177 varf ProbL1(u, v) = int2d(Th1)(grad(u)'*grad(v) + 1e-1*u*v) - int2d(Th1)(f * v);
178 varf ProbB1(u, v) = int1d(ThI1)(u*v);
179 varf ProbC1(u, v) = -int1d(ThI1)(1./alpha * u*v);
180 varf ProbL2(u, v) = int2d(Th2)(grad(u)'*grad(v) + 1e-1*u*v) - int2d(Th2)(f * v);
181 varf ProbB2(u, v) = int1d(ThI2)(u*v);
182 varf ProbC2(u, v) = -int1d(ThI2)(1./alpha * u*v);
183 real[int] rhs(Vh1.ndof + Vh2.ndof + VhI1.ndof + VhI2.ndof);
184 rhs = 0;
185 rhs(0:Vh1.ndof-1) = ProbL1(0,Vh1);
186 rhs(Vh1.ndof+VhI1.ndof:Vh1.ndof+VhI1.ndof+Vh2.ndof-1) = ProbL2(0,Vh2);
187 ~~~
188
189 For simplicity, we define the same problem on both domains and use the same $f$ for both, however, this can be anything here.
190
191 ### Constructing distributed matrices
192
193 We are now ready to set up and compose the various distributed block matrices that will make up the full system definition. We start with the matrices defining the problem local to $\Omega_1$ and $\Omega_2$,
194
195 ~~~freefem
196 // L11 and L22
197 matrix mL11 = ProbL1(Vh1, Vh1, solver=GMRES);
198 matrix mL22 = ProbL2(Vh2, Vh2, solver=GMRES);
199 L11 = mL11;
200 L22 = mL22;
201 // B11 and B22
202 matrix mB11(Vh1.ndof, VhI1.ndof);
203 matrix mB22(Vh2.ndof, VhI2.ndof);
204 if(hasI1) mB11 = ProbB1(VhI1, Vh1, solver=GMRES);
205 if(hasI2) mB22 = ProbB2(VhI2, Vh2, solver=GMRES);
206 Mat B11(L11, C11);
207 Mat B22(L22, C22);
208 B11 = mB11;
209 B22 = mB22;
210 // C11 and C22
211 matrix mC11(VhI1.ndof,VhI1.ndof);
212 matrix mC22(VhI2.ndof,VhI2.ndof);
213 if(hasI1) mC11 = ProbC1(VhI1, VhI1, solver=GMRES);
214 if(hasI2) mC22 = ProbC2(VhI2, VhI2, solver=GMRES);
215 C11 = mC11;
216 C22 = mC22;
217 ~~~
218
219 Note that the value of the `solver` parameter is completely irrelevant as we will solve the system through PETSc later-on. However, the default solver in distributed FreeFEM includes collective MPI operations, which does not allow us to only conditionally fill `B11`, `B22`, `C11`, and `C22`, as otherwise the execution would be stalled as soon as one of the subdomains does not include any part of the boundary.
220
221 The cross-interface matrices will be set up on the source domain only, as we cannot assume that we have matching decompositions on the other side of the interface:
222
223 ~~~freefem
224 // BT12 and BT21
225 matrix mB12(Vh1.ndof, VhI1.ndof), mB21(Vh2.ndof, VhI2.ndof);
226 if(hasI1) mB12 = ProbB1(VhI1, Vh1, solver=GMRES);
227 if(hasI2) mB21 = ProbB2(VhI2, Vh2, solver=GMRES);
228 matrix mBT21 = -mB21';
229 matrix mBT12 = -mB12';
230 Mat BT12(C11, L11);
231 Mat BT21(C22, L22);
232 BT12 = mBT12;
233 BT21 = mBT21;
234 // C12 and C21
235 matrix mC12(VhI1.ndof,VhI1.ndof), mC21(VhI2.ndof, VhI2.ndof);
236 if(hasI1) mC12 = ProbC1(VhI1, VhI1, solver=GMRES);
237 if(hasI2) mC21 = ProbC2(VhI2, VhI2, solver=GMRES);
238 Mat C12(C11, C11);
239 Mat C21(C22, C22);
240 C12 = mC12;
241 C21 = mC21;
242 ~~~
243
244 In order to project the target domain across the interface onto the right subdomains of that domain, we make use of the interpolation matrices set up initially. PETSc allows us to define a block matrix constructed out of block matrices and block matrix products, which allows us to “fix” the target domains of the cross-interface block matrices while defining the full system block matrix:
245
246 ~~~freefem
247 Mat A = [[ L11 , B11 , 0 , 0 ],
248 [ B11' , C11 , interpI12*BT21, interpI12*C21],
249 [ 0 , 0 , L22 , B22 ],
250 [interpI21*BT12, interpI21*C12, B22' , C22 ]];
251 ~~~
252
253 ### Solving the system
254
255 For solving the problem we use PETSc's `fgmres` solver on the global system preconditioned by a solve of the two 2x2 diagonal blocks, which define each a saddle point problem local to domain $\Omega_1$ and $\Omega_2$ respectively. These two saddle point systems are solved with PETSc's `gmres` solver preconditioned in turn by `lu`:
256
257 ~~~freefem
258 set(A, sparams=" -ksp_type fgmres" +
259 " -ksp_pc_side right" +
260 " -pc_type fieldsplit" +
261 " -pc_fieldsplit_type additive" +
262 " -pc_fieldsplit_0_fields 0,1" +
263 " -pc_fieldsplit_1_fields 2,3" +
264 " -fieldsplit_0_ksp_type preonly" +
265 " -fieldsplit_0_pc_type lu" +
266 " -fieldsplit_1_ksp_type preonly" +
267 " -fieldsplit_1_pc_type lu" +
268 " -ksp_max_it 100" +
269 " -ksp_gmres_restart 100" +
270 " -ksp_monitor_true_residual" +
271 " -ksp_converged_reason" +
272 " -ksp_gmres_modifiedgramschmidt");
273 ~~~
274
275 The `fieldsplit` preconditioner in PETSc allows us to define a block-Jacobi type splitting, with each split containing the fields corresponding to the respective 2x2 block matrix. We are now ready to solve the problem and plot the solution:
276
277 ~~~freefem
278 real[int] sol = A^-1 * rhs;
279 macro dimension()2// EOM
280 macro meshtype()V// EOM
281 include "macro_ddm.idp"
282 macro def(u)u//
283 Vh1 u1;
284 Vh2 u2;
285 u1[] = sol(0:Vh1.ndof-1);
286 u2[] = sol(Vh1.ndof+VhI1.ndof:Vh1.ndof+VhI1.ndof+Vh2.ndof-1);
287 plotMPI(Th1, u1, Pku1, def, real, cmm="Global velocity (u1)");
288 plotMPI(Th2, u2, Pku2, def, real, cmm="Global velocity (u2)");
289 ~~~
290
291 For 16 MPI processes, a possible combined plot is shown below:
292
293 ![][_solution]
294
295 [_solution]: https://raw.githubusercontent.com/FreeFem/FreeFem-markdown-figures/main/examples/examples/coupledphysics/solution.png