-
Notifications
You must be signed in to change notification settings - Fork 1
/
porbcontinue.edp
497 lines (494 loc) · 22.1 KB
/
porbcontinue.edp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
//
// porbcontinue.edp
// Chris Douglas
//
// EXAMPLE USAGE:
// Continue input file along parameter without mesh adaptation:
// ff-mpirun -np 4 porbcontinue.edp -fi <FILEIN> -param <PARAM>
//
// Continue input file along parameter with mesh adaptation:
// ff-mpirun -np 4 porbcontinue.edp -fi <FILEIN> -param <PARAM> -mo <MESHOUT>
//
// NOTE: This file should not be changed unless you know what you're doing.
//
load "iovtk"
load "PETSc"
include "settings.idp"
include "macros_bifbox.idp"
// arguments
string meshin = getARGV("-mi", ""); // input meshfile with extension
string meshout = getARGV("-mo", "");
string filein = getARGV("-fi", "");
string fileout = getARGV("-fo", filein);
int select = getARGV("-select", 1);
bool zerofreq = getARGV("-zero", 0);
int count = getARGV("-count", 0);
int savecount = getARGV("-scount", 1);
int maxcount = getARGV("-maxcount", 100);
real h0 = getARGV("-h0", 1.0);
string param = getARGV("-param", "");
string adaptto = getARGV("-adaptto", "0");
real fmax = getARGV("-fmax", 2.0);
real kappamax = getARGV("-kmax", 0.5);
real deltamax = getARGV("-dmax", 4.0);
real anglemax = getARGV("-amax", 30.)*pi/180.0;
int monotone = getARGV("-mono", 1);
real eps = getARGV("-eps", 1e-7);
int snesmaxit = getARGV("-snes_max_it", 10);
string sneslinesearchtype = getARGV("-snes_linesearch_type","basic");
int Nh = getARGV("-Nh", 0); //if 0 will try to get from file, otherwise fail
int blocks = getARGV("-blocks", 1); //if blocks = 1, use monolithic LU; if blocks = N w/ 2 <= N <= Nh+1, use block preconditioner with N blocks
int refactor = getARGV("-refact", snesmaxit);
real paramtarget = getARGV("-paramtarget",1.0);
real[int] sym1(sym.n);
real omega;
bool stopflag = false;
bool forcesave = false;
// Load mesh, make FE basis
string fileroot, fileext = parsefilename(filein, fileroot); //extract file name and extension
parsefilename(fileout, fileout); // trim extension from output file, if given
if(meshin == "") meshin = readmeshname(workdir + filein); // get mesh file
string meshroot, meshext = parsefilename(meshin, meshroot);
parsefilename(meshout, meshroot); // trim extension from output mesh, if given
if(count > 0) {
fileroot = fileroot(0:fileroot.rfind("_" + count)-1); // get file root
meshroot = meshroot(0:meshroot.rfind("_" + count)-1); // get file root
}
assert(fileext == "porb" || fileext == "hopf" || fileext == "foho" || fileext == "hoho");
if (fileext != "porb") Nh = max(1, Nh);
Th = readmeshN(workdir + meshin);
Thg = Th;
buildDmesh(Th);
restu = restrict(XMh, XMhg, n2o);
XMh defu(ub), defu(yb);
XMh<complex> defu(um), defu(uma), defu(um2), defu(um3);
complex[int, int] uh(um[].n, Nh);
if(count == 0) {
if (fileext == "porb") {
ub[] = loadporb(fileroot, meshin, uh, sym1, omega, Nh);
}
else if(fileext == "hopf") {
complex[string] alpha;
complex beta;
complex[int] qma;
ub[] = loadhopf(fileroot, meshin, um[], qma, sym1, omega, alpha, beta);
uh(:, 0) = um[];
}
else if(fileext == "foho") {
real[string] alpha2;
real beta22, beta23, gamma22, gamma23;
complex[string] alpha;
complex beta;
complex gamma12, gamma13;
real[int] q2m, q2ma;
complex[int] qma;
ub[] = loadfoho(fileroot, meshin, um[], qma, q2m, q2ma, sym1, omega, alpha, alpha2, beta, beta22, beta23, gamma12, gamma13, gamma22, gamma23);
uh(:, 0) = um[];
}
else if(fileext == "hoho") {
real omegaN;
complex[string] alpha, alphaN;
complex beta, betaN, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23;
complex[int] qma, qNm, qNma;
if(select == 1){
ub[] = loadhoho(fileroot, meshin, um[], qma, qNm, qNma, sym1, sym, omega, omegaN, alpha, alphaN, beta, betaN, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23);
}
else if(select == 2){
ub[] = loadhoho(fileroot, meshin, qNm, qNma, um[], qma, sym, sym1, omegaN, omega, alphaN, alpha, betaN, beta, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23);
}
uh(:, 0) = um[];
}
saveporb(filein, (savecount > 0 ? fileout : ""), meshin, sym1, omega, Nh, false, false);
}
else {
ub[] = loadporb(fileroot + "_" + count, meshin, uh, sym1, omega, Nh);
}
Nh = max(1, Nh); // Nh must be at least 1, otherwise use basecompute.edp
real lambda = getlambda(param);
real paramdiff = lambda - paramtarget;
// Create distributed Mat
Mat J;
createMatu(Th, J, Pk);
complex[int] ik(sym.n), ik2(sym.n), ik3(sym.n);
complex iomega, iomega2, iomega3;
include "eqns.idp"
bool adapt = false;
if(meshout != "") adapt = true; // if output meshfile is given, adapt mesh
meshout = meshin; // if no adaptation
// Build bordered block matrix from only Mat components
Mat JHBa, JHB((1+2*Nh)*J.n, (1+2*Nh)*J.n), dwHB((1+2*Nh)*J.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
if(zerofreq) JHBa = JHB;
else JHBa = [[JHB, dwHB],[dwHB', 0]];
Mat JlPMa(JHBa.n, mpirank == 0 ? 1 : 0), yqPMa(JHBa.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
Mat JHBaa = [[JHBa, JlPMa],[yqPMa', -1.0]]; // make dummy Jacobian
real[int] RHB(JHB.n), yqP(JHBa.n), yqP0(JHBa.n), qap(JHBaa.n);
int it, internalit, adaptflag;
real f, kappa, cosalpha, res, resp, delta, deltap, maxdelta, omega0;
// FUNCTIONS
func real[int] funcRa(real[int]& qPETSc) {
ChangeNumbering(J, ub[], qPETSc(0:J.n-1), inverse = true, exchange = true);
for (int harm = 0; harm < Nh; harm++)
ChangeNumbering([J, J], [uh(:, harm).re, uh(:, harm).im], qPETSc((1+2*harm)*J.n:(3+2*harm)*J.n-1), inverse = true, exchange = true);
if(mpirank == 0) {
omega = zerofreq ? 0.0 : qPETSc(qPETSc.n-2);
lambda = qPETSc(qPETSc.n-1); // Extract parameter value from state vector on proc 0
}
broadcast(processor(0), omega);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
real[int] RPETSc(qPETSc.n);
ResidualHB(RHB, J, ub, uh, sym1, omega);
RPETSc = RHB;
if(mpirank == 0){
if(!zerofreq) RPETSc(RPETSc.n-2) = 0.0;
RPETSc(RPETSc.n-1) = 0.0;
}
StepAdaptMonitors(RPETSc, qPETSc, qap, yqP, yqP0);
if(mpirank == 0) cout << " " + text1 + ":\t||R|| = " << res << (it == 0 ? (",\th0 = " + h0) : (",\t||dx|| = " + delta + ",\tangle = " + (sign(cosalpha)*acos(abs(cosalpha))*180./pi))) << ",\t" + param + " = " << lambda << ",\tomega = " << omega << "." << endl;
return RPETSc;
}
func int funcJa(real[int]& qPETSc) {
++it;
internalit = 0;
qap = qPETSc;
resp = res;
deltap = delta;
ChangeNumbering(J, ub[], qPETSc(0:J.n-1), inverse = true, exchange = true);
for (int harm = 0; harm < Nh; harm++)
ChangeNumbering([J, J], [uh(:, harm).re, uh(:, harm).im], qPETSc((1+2*harm)*J.n:(3+2*harm)*J.n-1), inverse = true, exchange = true);
if(mpirank == 0) {
omega = zerofreq ? 0.0 : qPETSc(qPETSc.n-2);
lambda = qPETSc(qPETSc.n-1); // Extract parameter value from state vector on proc 0
}
broadcast(processor(0), omega);
broadcast(processor(0), lambda);
updatelambda(param, lambda + eps);
real[int] JlHB(RHB.n);
ResidualHB(JlHB, J, ub, uh, sym1, omega);
updatelambda(param, lambda);
JlHB -= RHB;
JlHB /= eps;
if (it == 1 | refactor >= it) JacobianHB(JHB, J, ub, uh, sym1, omega);
if (zerofreq) JHBa = JHB;
else {
real[int] Mtemp = 0, MqHB((1+2*Nh)*J.n);
for (int nh = 0; nh < Nh; nh++) {
sym = (1+nh)*sym1;
um[] = 1i*(1+nh)*uh(:, nh);
complex[int] Mc = vM(0, XMh, tgv = -10);
ChangeNumbering([J, J], [Mc.re, Mc.im], Mtemp);
MqHB((1+2*nh)*J.n:(3+2*nh)*J.n-1) = Mtemp;
}
matrix tempPms = [[MqHB]];
ChangeOperator(dwHB, tempPms, parent = JHBa); // send to Mat
}
if (mpirank == 0 && !zerofreq) {
JlHB.resize(JHBa.n);
JlHB(JlHB.n-1) = 0.0;
}
matrix tempPms = [[JlHB]]; // dense array to sparse matrix
ChangeOperator(JlPMa, tempPms, parent = JHBaa); // send to Mat
KSPSolve(JHBa, JlHB, yqP); // compute tangent vector in PETSc numbering
tempPms = [[yqP]]; // dense array to sparse matrix
ChangeOperator(yqPMa, tempPms, parent = JHBaa); // send to Mat
return 0;
}
// set up Mat parameters
real[int] fieldlabels((1+2*Nh)*J.n);
fieldlabels = 1.0;
blocks = min(blocks, 1+Nh);
for (int nh = 0; nh < Nh; nh++) fieldlabels((1+2*nh)*J.n:(3+2*nh)*J.n-1) = 1.0 + max(0, blocks + nh - Nh);
string HBKSPparams = KSPparams;
if (blocks > 1) HBKSPparams = "-ksp_type gmres -ksp_norm_type unpreconditioned -ksp_pc_side right -pc_type fieldsplit -pc_fieldsplit_type symmetric_multiplicative -fieldsplit_pc_type lu -ksp_gmres_cgs_refinement_type refine_ifneeded";
set(JHBaa, sparams = "-ksp_type preonly -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_precondition full -fieldsplit_1_ksp_type preonly -fieldsplit_1_pc_type redundant -fieldsplit_1_redundant_pc_type lu", setup = 1);
if (zerofreq) set(JHBa, sparams = "-prefix_push fieldsplit_0_ " + HBKSPparams + " -prefix_pop", prefix = "fieldsplit_0_", parent = JHBaa, fields = fieldlabels);
else {
set(JHBa, sparams = "-prefix_push fieldsplit_0_ -ksp_type preonly -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_precondition full -fieldsplit_1_ksp_type preonly -fieldsplit_1_pc_type redundant -fieldsplit_1_redundant_pc_type lu -prefix_pop", prefix = "fieldsplit_0_", parent = JHBaa, setup = 1);
set(JHB, sparams = "-prefix_push fieldsplit_0_fieldsplit_0_ " + HBKSPparams + " -prefix_pop", prefix = "fieldsplit_0_fieldsplit_0_", parent = JHBa, fields = fieldlabels);
}
// PREDICTOR
real[int] qa(JHB.n);
{
real[int] q;
ChangeNumbering(J, ub[], q);
qa(0:J.n-1) = q;
}
if (fileext != "hopf" && fileext != "foho" && fileext != "hoho") {
for (int harm = 0; harm < Nh; harm++){
real[int] qm;
ChangeNumbering([J, J], [uh(:, harm).re, uh(:, harm).im], qm);
qa((1+2*harm)*J.n:(3+2*harm)*J.n-1) = qm;
}
}
if(mpirank == 0){
qa.resize(qa.n+1+!zerofreq);
if(!zerofreq) qa(qa.n-2) = omega;
qa(qa.n-1) = lambda;
}
if (fileext != "hopf" && fileext != "foho" && fileext != "hoho"){
updatelambda(param, lambda + eps);
real[int] Jl(JHB.n), Jltemp(JHB.n);
ResidualHB(Jl, J, ub, uh, sym1, omega);
updatelambda(param, lambda);
ResidualHB(Jltemp, J, ub, uh, sym1, omega);
Jl -= Jltemp;
Jl /= eps;
JacobianHB(JHB, J, ub, uh, sym1, omega);
if (zerofreq) JHBa = JHB;
else {
real[int] Mtemp = 0, MqHB((1+2*Nh)*J.n);
for (int nh = 0; nh < Nh; nh++) {
sym = (1+nh)*sym1;
um[] = 1i*(1+nh)*uh(:, nh);
complex[int] Mc = vM(0, XMh, tgv = -10);
ChangeNumbering([J, J], [Mc.re, Mc.im], Mtemp);
MqHB((1+2*nh)*J.n:(3+2*nh)*J.n-1) = Mtemp;
}
matrix tempPms = [[MqHB]];
ChangeOperator(dwHB, tempPms, parent = JHBa); // send to Mat
}
if (!zerofreq && mpirank == 0) {
Jl.resize(JHBa.n);
Jl(Jl.n-1) = 0.0;
}
KSPSolve(JHBa, Jl, yqP);
yqP0 = yqP;
omega0 = omega;
}
else {
real[int] temp;
ChangeNumbering([J, J],[um[].re, um[].im], temp);
yqP = 0;
yqP(J.n:3*J.n-1) = temp;
yqP0 = yqP;
omega0 = omega;
}
while (!stopflag){
real[int] qa0 = qa;
real h, hl = (yqP'*yqP);
mpiAllReduce(hl, h, mpiCommWorld, mpiSUM);
h = h0/sqrt(h + 1.0);
qa(0:JHBa.n-1) -= (h*yqP);
if (mpirank == 0) {
if (!(count == 0 && (fileext == "hopf" || fileext == "foho" || fileext == "hoho"))) qa(qa.n-1) += h; // -= (-1.0*h)
omega = zerofreq ? 0.0 : qa(qa.n-2);
lambda = qa(qa.n-1);
}
broadcast(processor(0), omega);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
// CORRECTOR LOOP
int ret;
it = 0;
internalit = 0;
adaptflag = 0;
SNESSolve(JHBaa, funcJa, funcRa, qa, reason = ret,
sparams = "-snes_linesearch_type " + sneslinesearchtype + " -snes_converged_reason -options_left no -snes_max_it " + snesmaxit); // solve nonlinear problem with SNES
if (ret > 0) {
++count;
if (maxcount > 0) stopflag = (count >= maxcount);
else if ((lambda - paramtarget)*paramdiff <= 0) stopflag = true;
h0 /= f;
if (cosalpha < 0) {
h0 *= -1.0;
if (count == 1 && mpirank == 0) cout << "\tIncorrect initial orientation. Orientation reversed." << endl;
else if (count > 1 && mpirank == 0) {
cout << "\tFold bifurcation detected. Orientation reversed." << endl;
forcesave = true;
}
}
if (adapt && (count % savecount == 0)){
meshout = meshroot + "_" + count + "." + meshext;
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true);
for (int nh = 0; nh < Nh; nh++)
ChangeNumbering([J, J], [uh(:, nh).re, uh(:, nh).im], qa((1+2*nh)*J.n:(3+2*nh)*J.n-1), inverse = true);
if(mpirank == 0) {
omega = zerofreq ? 0.0 : qa(qa.n-2);
lambda = qa(qa.n-1);
}
broadcast(processor(0), omega);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
ChangeNumbering(J, yb[], yqP(0:J.n-1), inverse = true);
real yomega;
if(mpirank == 0 && !zerofreq) yomega = yqP(yqP.n-1);
XMhg defu(uG), defu(yG), defu(u1rG), defu(u1iG), defu(u2rG), defu(u2iG), defu(tempu), defu(tempu2);
real[int, int] yhrG(uG[].n, Nh), yhiG(uG[].n, Nh), uhrG(uG[].n, Nh), uhiG(uG[].n, Nh); // create private global FE functions
tempu[](restu) = ub[]; // populate local portion of global soln
mpiAllReduce(tempu[], uG[], mpiCommWorld, mpiSUM);
tempu[](restu) = yb[]; // populate local portion of global tangent vector
mpiAllReduce(tempu[], yG[], mpiCommWorld, mpiSUM);
for (int nh = 0; nh < Nh; nh++){
tempu[](restu) = uh(:, nh).re;
mpiAllReduce(tempu[], uhrG(:, nh), mpiCommWorld, mpiSUM);
tempu[](restu) = uh(:, nh).im;
mpiAllReduce(tempu[], uhiG(:, nh), mpiCommWorld, mpiSUM);
complex[int] temp(ub[].n);
ChangeNumbering([J, J], [temp.re, temp.im], yqP((1+2*nh)*J.n:(3+2*nh)*J.n-1), inverse = true);
tempu[](restu) = temp.re;
mpiAllReduce(tempu[], yhrG(:, nh), mpiCommWorld, mpiSUM);
tempu[](restu) = temp.im;
mpiAllReduce(tempu[], yhiG(:, nh), mpiCommWorld, mpiSUM);
}
u1rG[] = uhrG(:, 0);
u1iG[] = uhiG(:, 0);
if (Nh > 1){
u2rG[] = uhrG(:, 1);
u2iG[] = uhiG(:, 1);
}
if(mpirank == 0) {
IFMACRO(dimension,2)
if(adaptto == "0") Thg = adaptmesh(Thg, adaptu(uG), adaptmeshoptions);
else if(adaptto == "0y") Thg = adaptmesh(Thg, adaptu(uG), adaptu(yG), adaptmeshoptions);
else if(adaptto == "01") Thg = adaptmesh(Thg, adaptu(uG), adaptu(u1rG), adaptu(u1iG), adaptmeshoptions);
else if(Nh > 1 && adaptto == "02") Thg = adaptmesh(Thg, adaptu(uG), adaptu(u2rG), adaptu(u2iG), adaptmeshoptions);
else if(Nh > 1 && adaptto == "012") Thg = adaptmesh(Thg, adaptu(uG), adaptu(u1rG), adaptu(u1iG), adaptu(u2rG), adaptu(u2iG), adaptmeshoptions);
ENDIFMACRO
IFMACRO(dimension,3)
cout << "NOTE: 3D mesh adaptation is still under development." << endl;
load "mshmet"
load "mmg"
real anisomax = getARGV("-anisomax",1.0);
real[int] met((bool(anisomax > 1) ? 6 : 1)*Thg.nv);
if(adaptto == "0") met = mshmet(Thg, adaptu(uG), normalization = getARGV("-normalization",1), aniso = bool(anisomax > 1.0),hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), err = getARGV("-err", 1.0e-2));
else if(adaptto == "0y") met = mshmet(Thg, adaptu(uG), adaptu(yG), normalization = getARGV("-normalization",1), aniso = bool(anisomax > 1.0),hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), err = getARGV("-err", 1.0e-2));
else if(adaptto == "01") met = mshmet(Thg, adaptu(uG), adaptu(u1rG), adaptu(u1iG), normalization = getARGV("-normalization",1), aniso = bool(anisomax > 1.0), hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), err = getARGV("-err", 1.0e-2));
else if(Nh > 1 && adaptto == "02") met = mshmet(Thg, adaptu(uG), adaptu(u2rG), adaptu(u2iG), normalization = getARGV("-normalization",1), aniso = bool(anisomax > 1.0), hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), err = getARGV("-err", 1.0e-2));
else if(Nh > 1 && adaptto == "012") met = mshmet(Thg, adaptu(uG), adaptu(u1rG), adaptu(u1iG), adaptu(u2rG), adaptu(u2iG), normalization = getARGV("-normalization",1), aniso = bool(anisomax > 1.0), hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), err = getARGV("-err", 1.0e-2));
if(anisomax > 1.0) {
load "aniso"
boundaniso(6, met, anisomax);
}
Thg = mmg3d(Thg, metric = met, hmin = getARGV("-hmin", 1.0e-6), hmax = getARGV("-hmax", 1.0e+2), hgrad = -1, verbose = verbosity-(verbosity==0));
ENDIFMACRO
}
broadcast(processor(0), Thg);
defu(uG) = defu(uG);
defu(yG) = defu(yG);
real[int, int] yhrG2(uG[].n, Nh), yhiG2(uG[].n, Nh), uhrG2(uG[].n, Nh), uhiG2(uG[].n, Nh); // create private global FE functions
for (int nh = 0; nh < Nh; nh++){
tempu[] = uhrG(:, nh);
defu(tempu2) = defu(tempu);
uhrG2(:, nh) = tempu2[];
tempu[] = uhiG(:, nh);
defu(tempu2) = defu(tempu);
uhiG2(:, nh) = tempu2[];
tempu[] = yhrG(:, nh);
defu(tempu2) = defu(tempu);
yhrG2(:, nh) = tempu2[];
tempu[] = yhiG(:, nh);
defu(tempu2) = defu(tempu);
yhiG2(:, nh) = tempu2[];
}
Th = Thg;
Mat Adapt;
createMatu(Th, Adapt, Pk);
J = Adapt;
defu(ub) = initu(0.0);
defu(yb) = initu(0.0);
defu(um) = initu(0.0); // set local values to zero
defu(uma) = initu(0.0); // set local values to zero
defu(um2) = initu(0.0); // set local values to zero
defu(um3) = initu(0.0); // set local values to zero
uh.resize(um[].n, Nh);
yqP.resize((1+2*Nh)*J.n + ((!zerofreq && mpirank == 0) ? 1 : 0));
real[int] temp;
restu.resize(ub[].n); // Change size of restriction operator
restu = restrict(XMh, XMhg, n2o); // Compute new restriction from global mesh to local mesh
ub[] = uG[](restu);
yb[] = yG[](restu);
ChangeNumbering(J, yb[], temp);
yqP(0:J.n-1) = temp;
fieldlabels.resize((1+2*Nh)*J.n);
fieldlabels = 1.0;
for (int nh = 0; nh < Nh; nh++) {
tempu2[] = uhrG2(:, nh);
um[].re = tempu2[](restu);
tempu2[] = uhiG2(:, nh);
um[].im = tempu2[](restu);
uh(:, nh) = um[];
tempu2[] = yhrG2(:, nh);
real[int] temp1 = tempu2[](restu);
tempu2[] = yhiG2(:, nh);
real[int] temp2 = tempu2[](restu);
ChangeNumbering([J, J], [temp1, temp2], temp);
yqP((1+2*nh)*J.n:(3+2*nh)*J.n-1) = temp;
fieldlabels((1+2*nh)*J.n:(3+2*nh)*J.n-1) = 1.0 + max(0, blocks + nh - Nh);
}
Mat Adapt1((1+2*Nh)*J.n, (1+2*Nh)*J.n), Adapt2((1+2*Nh)*J.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
JHB = Adapt1;
dwHB = Adapt2;
if(zerofreq) JHBa = JHB;
else JHBa = [[JHB, dwHB],[dwHB', 0]];
Mat Adapt3(JHBa.n, mpirank == 0 ? 1 : 0), Adapt4(JHBa.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
JlPMa = Adapt3;
yqPMa = Adapt4;
JHBaa = [[JHBa, JlPMa],[yqPMa', -1.0]]; // make dummy Jacobian
if (zerofreq) set(JHBa, sparams = "-prefix_push fieldsplit_0_ " + HBKSPparams + " -prefix_pop", prefix = "fieldsplit_0_", parent = JHBaa, fields = fieldlabels);
else {
set(JHB, sparams = "-prefix_push fieldsplit_0_fieldsplit_0_ " + HBKSPparams + " -prefix_pop", prefix = "fieldsplit_0_fieldsplit_0_", parent = JHBa, fields = fieldlabels);
set(JHBa, sparams = "-prefix_push fieldsplit_0_ -ksp_type preonly -pc_type fieldsplit -pc_fieldsplit_type schur -pc_fieldsplit_schur_precondition full -fieldsplit_1_ksp_type preonly -fieldsplit_1_pc_type redundant -fieldsplit_1_redundant_pc_type lu -prefix_pop", prefix = "fieldsplit_0_", parent = JHBaa);
}
qa.resize(JHBaa.n);
{
real[int] q;
ChangeNumbering(J, ub[], q);
qa(0:J.n-1) = q;
}
for (int harm = 0; harm < Nh; harm++){
real[int] qm;
ChangeNumbering([J, J], [uh(:, harm).re, uh(:, harm).im], qm);
qa((1+2*harm)*J.n:(3+2*harm)*J.n-1) = qm;
}
yqP.resize(JHBa.n);
if(mpirank == 0) {
if(!zerofreq) {
qa(qa.n-2) = omega;
yqP(yqP.n-1) = yomega;
}
qa(qa.n-1) = lambda;
}
RHB.resize(JHB.n);
yqP0.resize(JHBa.n);
yqP0 = yqP;
qa0.resize(qa.n);
qap.resize(qa.n);
it = 0;
internalit = 0;
adaptflag = 1;
SNESSolve(JHBaa, funcJa, funcRa, qa, reason = ret,
sparams = "-snes_linesearch_type " + sneslinesearchtype + " -snes_converged_reason -options_left no"); // solve nonlinear problem with SNES
assert(ret > 0);
if(mpirank==0) { // Save adapted mesh
cout << " Saving adapted mesh '" + meshout + "' in '" + workdir + "'." << endl;
savemesh(Thg, workdir + meshout);
}
}
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true);
for (int harm = 0; harm < Nh; harm++)
ChangeNumbering([J, J], [uh(:, harm).re, uh(:, harm).im], qa((1+2*harm)*J.n:(3+2*harm)*J.n-1), inverse = true);
if (mpirank == 0) {
omega = zerofreq ? 0.0 : qa(qa.n-2);
lambda = qa(qa.n-1);
}
broadcast(processor(0), omega);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
saveporb(fileout + "_" + count + (forcesave ? "specialpt" : ""), (savecount > 0 ? fileout : ""), meshout, sym1, omega, Nh, ((count % savecount == 0) || forcesave || stopflag), true);
forcesave = false;
yqP0 = yqP;
omega0 = omega;
}
else {
if (mpirank == 0){
if(res*(monotone!=0) >= resp) cout << "\tResidual norm failed to decrease. Reattempting with smaller step." << endl;
if(kappa >= kappamax) cout << "\tContraction rate exceeds " << kappamax << ". Reattempting with smaller step." << endl;
if(it >= snesmaxit) cout << "\tFailed to converge within limit of " + snesmaxit + " iterations. Reattempting with smaller step." << endl;
if(maxdelta >= deltamax) cout << "\tStep size exceeds " << deltamax << "." << endl;
if(acos(abs(cosalpha)) >= anglemax) cout << "\tAngle exceeds " << (anglemax*180./pi) << " degrees." << endl;
}
h0 /= fmax;
qa = qa0;
}
}