Debian Codegraph @

idp/ffddm_geneoCS_saddlepoint.idp

raw
1 macro ffddmgeneofullsetup(pr,fgeneoA1,fgeneoB)
2
3 pr#bCM = 0;
4
5 if (pr#prfe#prmesh#isincomm)
6 {
7 NewMacro pr#geneofullpattern()1 EndMacro
8 func pr#geneofullpatternOP = fgeneoB;
9
10 pr#corr = ffddmcorrection;
11
12 int nev = ffddmnu;
13
14 IFMACRO(!pr#withhpddm)
15
16 if (!bpara)
17 pr#Z.resize(nev*pr#prfe#prmesh#npart);
18 else if (!pr#prfe#prmesh#excluded)
19 pr#Z.resize(nev);
20
21 pr#si=0;
22 pr#prfe#prmesh#tic(pr#tloc)
23
24 int[int] sizel(pr#prfe#prmesh#npart);
25 int[int] sizelT(pr#prfe#prmesh#npart);
26
27 for(int i=0;i<pr#prfe#prmesh#npart;++i)
28 if ((!pr#prfe#prmesh#excluded) && ((!bpara) || (i == mpiRank(pr#prfe#prmesh#commddm)))) {
29 pr#prfe#prmesh#Thi=pr#prfe#prmesh#aTh[i];
30
31 pr#prfe#K[int,int] eV(pr#prfe#Dk[i].n,nev);
32 pr#prfe#K[int] ev(nev);
33
34 int[int] done(pr#prfe#prmesh#numberIntersection);
35 done = 0;
36
37 pr#prfe#K[int] buffi((1+pr#prfe#prmesh#numberIntersection)*pr#prfe#Dk[i].n);
38 pr#prfe#K[int] buffo((1+pr#prfe#prmesh#numberIntersection)*pr#prfe#Dk[i].n);
39
40 int cptB = 0;
41 func pr#prfe#K[int] ffullgeneoB(pr#prfe#K[int] &u) {
42 cptB++;
43 pr#prfe#K[int] Du = pr#prfe#Dk[i] .* u;
44 mpiRequest[int] rq(2*pr#prfe#prmesh#numberIntersection);
45 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
46 pr#prfe#snd[j] = pr#prfe#restrictionIntersection[j]*Du;
47 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
48 Isend(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq[j*2]),pr#prfe#snd[j]);
49 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
50 if (!done[j])
51 Irecv(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq[j*2+1]),pr#prfe#rcv[j]);
52
53 for(int j=0;j<pr#prfe#prmesh#numberIntersection*2;++j)
54 int k = mpiWaitAny(rq);
55
56 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
57 if (!done[j] && pr#prfe#rcv[j][0] == 1e+30)
58 done[j] = 1;
59
60 mpiRequest[int] rq2(2*pr#prfe#prmesh#numberIntersection);
61
62 int cpt = 1;
63 buffi(0:pr#prfe#Dk[i].n-1) = Du;
64 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
65 if (!done[j]) {
66 buffi(cpt*pr#prfe#Dk[i].n:(cpt+1)*pr#prfe#Dk[i].n-1) = pr#prfe#restrictionIntersection[j]'*pr#prfe#rcv[j];
67 cpt++;
68 }
69
70 buffo = fgeneoB(buffi, cpt);
71
72 cpt = 1;
73 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
74 if (!done[j]) {
75 pr#prfe#snd[j] = pr#prfe#restrictionIntersection[j]*buffo(cpt*pr#prfe#Dk[i].n:(cpt+1)*pr#prfe#Dk[i].n-1);
76 cpt++;
77 }
78
79 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
80 if (!done[j])
81 Isend(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq2[j*2]),pr#prfe#snd[j]);
82 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
83 Irecv(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq2[j*2+1]),pr#prfe#rcv[j]);
84
85 for(int j=0;j<pr#prfe#prmesh#numberIntersection*2;++j)
86 int k = mpiWaitAny(rq2);
87
88 pr#prfe#K[int] res = buffo(0:pr#prfe#Dk[i].n-1);
89 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
90 res += pr#prfe#restrictionIntersection[j]'*pr#prfe#rcv[j];
91 res = pr#prfe#Dk[i] .* res;
92 return res;
93 }
94
95 int nok = 0;
96
97 if (pr#prfe#Dk[i].n > 0) {
98 NewMacro localmacroK pr#prfe#K EndMacro
99 IFMACRO(localmacroK,real)
100 int kk = EigenValue(pr#prfe#Dk[i].n, A1=fgeneoA1, B=ffullgeneoB, mode=3, sym=1, tol=1e-1,sigma=0,value=ev,rawvector=eV,ncv=max(20,2*nev));
101 ENDIFMACRO
102 IFMACRO(localmacroK,complex)
103 int kk = complexEigenValue(pr#prfe#Dk[i].n, A1=fgeneoA1, B=ffullgeneoB, mode=3, tol=1e-3,sigma=0,value=ev,rawvector=eV,ncv=max(20,3*nev));
104 ENDIFMACRO
105
106 for(int j=0;j<nev;++j) {
107 if (j == 0 || ffddmtau == 0 || real(ev[j]) < ffddmtau) {
108 /*if (mpirank == 0) cout << i << " " << pr#si << " " << ev[j] << endl;*/
109 pr#Z[pr#si].resize(pr#prfe#Dk[i].n);
110 pr#Z[pr#si] = pr#prfe#Dk[i].*eV(:,j);
111 if (pr#Z[pr#si].sum < 0) pr#Z[pr#si] *= -1;
112 pr#si+=1;
113 nok+=1;
114 }
115 }
116 }
117
118 sizel[i] = nok; /* number of ev for subdomain i */
119
120 mpiRequest[int] rqd(pr#prfe#prmesh#numberIntersection);
121
122 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
123 pr#prfe#snd[j][0] = 1e+30;
124 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
125 Isend(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rqd[j]),pr#prfe#snd[j]);
126
127 int wit = 0;
128 while (done.min == 0) {
129 mpiRequest[int] rq(pr#prfe#prmesh#numberIntersection);
130 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
131 if (!done[j])
132 Irecv(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq[j]),pr#prfe#rcv[j]);
133
134 if (wit++ == 0)
135 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
136 int k = mpiWaitAny(rqd);
137 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
138 int k = mpiWaitAny(rq);
139
140 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
141 if (!done[j] && pr#prfe#rcv[j][0] == 1e+30)
142 done[j] = 1;
143
144 int cpt = 0;
145 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
146 if (!done[j]) {
147 buffi(cpt*pr#prfe#Dk[i].n:(cpt+1)*pr#prfe#Dk[i].n-1) = pr#prfe#restrictionIntersection[j]'*pr#prfe#rcv[j];
148 cpt++;
149 }
150
151 if (cpt) buffo = fgeneoB(buffi,cpt);
152
153 cpt = 0;
154 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
155 if (!done[j]) {
156 pr#prfe#snd[j] = pr#prfe#restrictionIntersection[j]*buffo(cpt*pr#prfe#Dk[i].n:(cpt+1)*pr#prfe#Dk[i].n-1);
157 cpt++;
158 }
159
160 mpiRequest[int] rq2(pr#prfe#prmesh#numberIntersection);
161
162 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
163 if (!done[j])
164 Isend(processor(pr#prfe#prmesh#arrayIntersection[j],pr#prfe#prmesh#commddm,rq2[j]),pr#prfe#snd[j]);
165
166 for(int j=0;j<pr#prfe#prmesh#numberIntersection;++j)
167 int k = mpiWaitAny(rq2);
168 }
169 }
170
171 pr#prfe#prmesh#toc(pr#tloc,"local eigenvalue problems",pr#teigenv)
172
173 /* pr#sizelg stores the number of ev for every subdomain */
174 if (!bpara)
175 pr#sizelg = sizel;
176 else {
177 pr#sizelg = 0;
178 mpiAllReduce(sizel,pr#sizelg,pr#prfe#prmesh#mpicomm,mpiSUM);
179 }
180
181 ffddmbuildZTAZ(pr,pr,null)
182
183 ENDIFMACRO
184 }
185 // EOM
186
187 macro ffddmSPsetup(SP,prA,prBtilde,prCtilde,Ctildei)
188
189 matrix SP#Btildei, SP#localBlocki;
190
191 /* matrix-vector product with local Schur complement */
192 func real[int] SP#Simatvec(real[int] &l) {
193 real[int] t1 = SP#Btildei'*l;
194 real[int] t2 = prA#aR[mpiRank(prA#prfe#prmesh#commddm)]^-1*t1;
195 real[int] t3 = SP#Btildei*t2;
196 real[int] t4 = Ctildei * l;
197 t3 += t4;
198 return t3;
199 }
200
201 /* inverse of local Schur complement through the solution of the augmented sparse local saddle point system */
202 func real[int] SP#Sisolve(real[int] &l) {
203 real[int] bi(prA#prfe#Vhi.ndof + prCtilde#prfe#Vhi.ndof);
204 bi(prA#prfe#Vhi.ndof:bi.n-1) = -l;
205 real[int] ui = SP#localBlocki^-1*bi;
206 return ui(prA#prfe#Vhi.ndof:bi.n-1);
207 }
208
209 /* distributed matrix-vector product with B from prA#prfe#Vhi to prCtilde#prfe#Vhi */
210 func real[int] SP#Bmatvec(real[int] &l) {
211 real[int] Dltilde(prA#prfe#AugVhi.ndof);
212 /* go from prA#prfe#Vhi to prA#prfe#AugVhi: */
213 if (!prA#prfe#prmesh#excluded) {
214 real[int] Dl = prA#prfe#Dk[mpiRank(prA#prfe#prmesh#commddm)].*l; /* multiplty by PoU */
215 Dltilde = prA#prfe#Auginterp'*Dl; /* extend */
216 prA#prfe#Augupdate(Dltilde,false); /* sum */
217 }
218 real[int] res = prBtilde#A(Dltilde);
219 return res;
220 }
221
222 /* distributed matrix-vector product with B^T from prCtilde#prfe#Vhi to prA#prfe#Vhi */
223 func real[int] SP#BTmatvec(real[int] &l) {
224 real[int] BTtilde = prBtilde#AT(l); /* B^T */
225 real[int] res(prA#prfe#Vhi.ndof);
226 if (!prA#prfe#prmesh#excluded) res = prA#prfe#Auginterp*BTtilde; /* restrict from prA#prfe#AugVhi to prA#prfe#Vhi */
227 return res;
228 }
229
230 /* matrix-vector product with S_1 defined as custom operator for prefix S1tilde */
231 NewMacro SP#S1tildemyA
232 func real[int] SP#S1tildeA(real[int] &l)
233 {
234 real[int] s(l.n);
235 s = 0;
236
237 prA#prfe#prmesh#Augtic(SP#S1tildetmvi)
238
239 if (!prA#prfe#prmesh#Augexcluded){
240 s = SP#Simatvec(l);
241 prCtilde#prfe#update(s,false);
242 prCtilde#prfe#update(s,true);
243 }
244 prA#prfe#prmesh#Augtoc(SP#S1tildetmvi,"",SP#S1tildetmv)
245
246 return s;
247 }
248 EndMacro
249
250 /* one-level preconditioner M_{S_1,1}^-1 defined as custom preconditioner for prefix S1tilde */
251 NewMacro SP#S1tildemyPREC1
252 func real[int] SP#S1tildePREC1(real[int] &l)
253 {
254 real[int] s(l.n);
255 s = 0;
256
257 prA#prfe#prmesh#Augtic(SP#S1tildetpreci)
258
259 if (!prA#prfe#prmesh#Augexcluded){
260 real[int] aux = l;
261 if(SP#S1tildeprec == "soras")
262 aux = aux .* prCtilde#prfe#Dk[mpiRank(prA#prfe#prmesh#Augcommddm)];
263
264 s = SP#Sisolve(aux);
265
266 if(SP#S1tildeprec != "asm"){
267 prCtilde#prfe#update(s,true);
268 }
269 else{
270 prCtilde#prfe#update(s,false);
271 prCtilde#prfe#update(s,true);
272 }
273 }
274 prA#prfe#prmesh#Augtoc(SP#S1tildetpreci,"",SP#S1tildetprec)
275
276 return s;
277 }
278 EndMacro
279
280 /* operator (I + M_{S_1}^-1 S_0) for the solution of eq. (25) to apply N_S^-1
281 defined as custom operator for prefix NStilde */
282 NewMacro SP#NStildemyA
283 func real[int] SP#NStildeA(real[int] &u) {
284 prA#prfe#prmesh#Augtic(SP#NStildetmvi)
285 real[int] res(u.n);
286
287 /* recall that S_0 = B R_0^T (R_0 A R_0^T)^-1 R_0 B^T */
288
289 real[int] BTu = SP#BTmatvec(u); /* B^T */
290
291 real[int] R0TA01R0BTu = prA#Q(BTu); /* Q = R_0^T (R_0 A R_0^T)^-1 R_0 */
292
293 /* extend from prA#prfe#Vhi to prA#prfe#AugVhi : */
294 real[int] El(prA#prfe#AugVhi.ndof);
295 if (!prA#prfe#prmesh#excluded) {
296 real[int] Dl = prA#prfe#Dk[mpiRank(prA#prfe#prmesh#commddm)].*R0TA01R0BTu; /* multiplty by PoU */
297 El = prA#prfe#Auginterp'*Dl; /* extend */
298 prA#prfe#Augupdate(El,false); /* sum */
299 }
300
301 real[int] BR0TA01R0BTu = prBtilde#A(El); /* B */
302
303 real[int] MS11 = SP#S1tildePREC(BR0TA01R0BTu); /* M_{S_1}^-1 */
304 res = u + MS11;
305 prA#prfe#prmesh#Augtoc(SP#NStildetmvi,"",SP#NStildetmv)
306 return res;
307 }
308 EndMacro
309
310 /* matrix-vector product with Schur complement S = C + B A^-1 B^T for step 3 of Algorithm 2
311 defined as custom operator for prefix SP */
312 NewMacro SP#myA
313 func real[int] SP#A(real[int] &l)
314 {
315 prA#prfe#prmesh#Augtic(SP#tmvi)
316 real[int] BTl(prA#prfe#Vhi.ndof), s(prA#prfe#Vhi.ndof), x0(prA#prfe#Vhi.ndof);
317
318 BTl = SP#BTmatvec(l); /* B^T */
319
320 x0 = 0;
321 s = prA#fGMRES(x0, BTl, gtol, 100, "right"); /* A^-1 */
322
323 real[int] res = SP#Bmatvec(s); /* B */
324
325 res += prCtilde#A(l); /* + C */
326
327 prA#prfe#prmesh#Augtoc(SP#tmvi,"",SP#tmv)
328 return res;
329 }
330 EndMacro
331
332 /* matrix-vector product with N_S^-1 (Algorithm 1, i.e. solve eq. (25))
333 defined as custom preconditioner for prefix SP */
334 NewMacro SP#myPREC
335 func real[int] SP#PREC(real[int] &G) {
336 prA#prfe#prmesh#Augtic(SP#tpreci)
337 real[int] Gprime(G.n), res(G.n);
338
339 /* solve (I + M_{S_1}^-1 S_0) P = M_{S_1}^-1 G */
340 Gprime = SP#S1tildePREC(G);
341 res = SP#NStildefGMRES(res, Gprime, 1.e-2, 100, "right");
342
343 prA#prfe#prmesh#Augtoc(SP#tpreci,"",SP#tprec)
344 return res;
345 }
346 EndMacro
347
348 ffddmsetupinit(SP#S1tilde,prCtilde#prfe); /* define Operator for S_1 (S1tildeA with one-level preconditioner S1tildePREC1) */
349 SP#S1tildeprec = "soras";
350
351 ffddmsetupinit(SP#NStilde,prCtilde#prfe); /* define operator for N_S (NStildeA = I + M_{S_1}^-1 S_0, no preconditioner) */
352 SP#NStildeprec = "none";
353 SP#NStildeverbosity = 1;
354
355 ffddmsetupinit(SP,prCtilde#prfe); /* define Operator for S (SPA = S := C + B A^-1 B^T with preconditioner SPPREC = N_S^-1) */
356
357 /* build Btildei and localBlocki : */
358 prA#prfe#prmesh#Augtic(SP#tfactoi)
359 if (!prA#prfe#prmesh#excluded) {
360 SP#Btildei = prBtilde#aRd[mpiRank(prA#prfe#prmesh#commddm)]*prA#prfe#Auginterp'; /* Btilde_i of eq. (9) */
361
362 matrix mC = -Ctildei;
363 SP#localBlocki = [[prA#aR[mpiRank(prA#prfe#prmesh#commddm)],SP#Btildei'],[SP#Btildei,mC]];
364 set(SP#localBlocki,solver=sparsesolver,sym=1,factorize=3,commworld=mpiCommSelf);
365 }
366 prA#prfe#prmesh#Augtoc(SP#tfactoi,"",SP#tfacto)
367
368 /* local contribution to lhs sum of eigenvalue problem (18) for Arpack ;
369 blocking forward-backward substitutions for A_i^{-1} for mu right-hand sides to accelerate computations,
370 as we will also have input vectors from neighboring subdomains */
371 func real[int] SP#Simatvecblock(real[int] &l, int mu) {
372 real[int] res(l.n);
373 real[int] buffi(prA#prfe#Vhi.ndof * mu), buffo(prA#prfe#Vhi.ndof * mu);
374 for (int i=0; i < mu; i++)
375 buffi(i*prA#prfe#Vhi.ndof:(i+1)*prA#prfe#Vhi.ndof-1) = SP#Btildei'*l(i*prCtilde#prfe#Vhi.ndof:(i+1)*prCtilde#prfe#Vhi.ndof-1);
376
377 buffo = prA#aR[mpiRank(prA#prfe#prmesh#commddm)]^-1*buffi;
378
379 for (int i=0; i < mu; i++) {
380 real[int] t1 = SP#Btildei*buffo(i*prA#prfe#Vhi.ndof:(i+1)*prA#prfe#Vhi.ndof-1);
381 /*
382 real[int] t2 = prCtilde#prfe#Dk[mpiRank(prA#prfe#prmesh#commddm)].*l(i*prCtilde#prfe#Vhi.ndof:(i+1)*prCtilde#prfe#Vhi.ndof-1);
383 real[int] t3 = CtildeaRd[mpiRank(prA#prfe#prmesh#commddm)]*t2;
384 res(i*prCtilde#prfe#Vhi.ndof:(i+1)*prCtilde#prfe#Vhi.ndof-1) = t1 + t3;
385 */
386 real[int] t2 = Ctildei*l(i*prCtilde#prfe#Vhi.ndof:(i+1)*prCtilde#prfe#Vhi.ndof-1);
387 res(i*prCtilde#prfe#Vhi.ndof:(i+1)*prCtilde#prfe#Vhi.ndof-1) = t1 + t2;
388
389 }
390 return res;
391 }
392
393 /* build the second level of M_{S_1}^-1 (section 3.2.2) ;
394 Sisolve corresponds to the inverse of rhs of eigenvalue problem (18) for Arpack ;
395 Simatvecblock corresponds to the local Schur complement, i.e. the local contribution
396 to the S_1 sum -- and to the lhs sum of (18) */
397 ffddmgeneofullsetup(SP#S1tilde,SP#Sisolve,SP#Simatvecblock);
398
399 SP#S1tildecorr = "BNN";
400
401 /******************* Block version (prefix 'Block') with preconditioner P eq. (28) *******************/
402 NewMacro SP#Blockmyscalprod
403 func real SP#Blockscalprod(real[int]& va, real[int]& vb)
404 {
405 real resf = prA#prfe#scalprod(va(0:prA#prfe#Vhi.ndof-1),vb(0:prA#prfe#Vhi.ndof-1));
406 real resb = prCtilde#prfe#scalprod(va(prA#prfe#Vhi.ndof:va.n-1),vb(prA#prfe#Vhi.ndof:va.n-1));
407 return resf+resb;
408 }
409 EndMacro
410
411 ffddminitDfespacef(SP#Block, prA#prfe#prmesh#Aug, real, def, init, P1, def, init, P1)
412
413 NewMacro SP#BlockmyA
414 func real[int] SP#BlockA(real[int] &u)
415 {
416 real[int] res(u.n);
417 res(0:prA#prfe#Vhi.ndof-1) = prA#A(u(0:prA#prfe#Vhi.ndof-1));
418 res(prA#prfe#Vhi.ndof:u.n-1) = -prCtilde#A(u(prA#prfe#Vhi.ndof:u.n-1));
419
420 real[int] r1 = SP#BTmatvec(u(prA#prfe#Vhi.ndof:u.n-1));
421
422 res(0:prA#prfe#Vhi.ndof-1) += r1;
423
424 real[int] r2 = SP#Bmatvec(u(0:prA#prfe#Vhi.ndof-1));
425
426 res(prA#prfe#Vhi.ndof:u.n-1) += r2;
427
428 return res;
429 }
430 EndMacro
431
432 NewMacro SP#BlockmyPREC1
433 func real[int] SP#BlockPREC1(real[int] &u)
434 {
435 real[int] res(u.n), resb(u.n);
436
437 resb(0:prA#prfe#Vhi.ndof-1) = prA#PREC(u(0:prA#prfe#Vhi.ndof-1));
438
439 resb(prA#prfe#Vhi.ndof:u.n-1) = -SP#PREC(u(prA#prfe#Vhi.ndof:u.n-1));
440
441 real[int] r2 = SP#Bmatvec(resb(0:prA#prfe#Vhi.ndof-1));
442
443 real[int] s2(r2.n);
444
445 s2 = SP#PREC(r2);
446
447 resb(prA#prfe#Vhi.ndof:u.n-1) += s2;
448
449 res(0:prA#prfe#Vhi.ndof-1) = (resb(0:prA#prfe#Vhi.ndof-1));
450 res(prA#prfe#Vhi.ndof:u.n-1) = (resb(prA#prfe#Vhi.ndof:u.n-1));
451
452 real[int] r1 = SP#BTmatvec(resb(prA#prfe#Vhi.ndof:u.n-1));
453
454 real[int] s1(r1.n);
455
456 s1 = prA#PREC(r1);
457
458 res(0:prA#prfe#Vhi.ndof-1) -= s1;
459
460 return res;
461 }
462 EndMacro
463
464 ffddmsetupinit(SP#Block,SP#Block);
465 SP#Blockprec = "ras";
466 //