-
Notifications
You must be signed in to change notification settings - Fork 1
/
basecontinue.edp
309 lines (306 loc) · 12.6 KB
/
basecontinue.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
//
// basecontinue.edp
// Chris Douglas
//
// EXAMPLE USAGE:
// Continue input file along parameter without mesh adaptation:
// ff-mpirun -np 4 basecontinue.edp -fi <FILEIN> -param <PARAM>
//
// Continue input file along parameter with mesh adaptation:
// ff-mpirun -np 4 basecontinue.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 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", "b");
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", 1.0e-7);
int snesmaxit = getARGV("-snes_max_it", 10);
string sneslinesearchtype = getARGV("-snes_linesearch_type", "basic");
int refactor = getARGV("-refact", snesmaxit);
real paramtarget = getARGV("-paramtarget", 1.0);
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(fileext == "mode" || fileext == "resp" || fileext == "rslv" || fileext == "tdls") {
filein = readbasename(workdir + filein);
fileext = parsefilename(filein, fileroot);
}
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
}
Th = readmeshN(workdir + meshin);
Thg = Th;
buildDmesh(Th);
restu = restrict(XMh, XMhg, n2o);
XMh defu(ub), defu(um), defu(yb), defu(um2), defu(um3);
if(count == 0) {
if(fileext == "base") {
ub[] = loadbase(fileroot, meshin);
}
else if(fileext == "fold") {
real[string] alpha;
real beta;
real[int] qm, qma;
ub[] = loadfold(fileroot, meshin, qm, qma, alpha, beta);
}
else if(fileext == "hopf") {
real omega;
complex[string] alpha;
complex beta;
complex[int] qm, qma;
ub[] = loadhopf(fileroot, meshin, qm, qma, sym, omega, alpha, beta);
}
else if(fileext == "foho") {
real omega;
complex[string] alpha1;
complex beta1, gamma12, gamma13;
real[string] alpha2;
real beta22, beta23, gamma22, gamma23;
complex[int] q1m, q1ma;
real[int] q2m, q2ma;
ub[] = loadfoho(fileroot, meshin, q1m, q1ma, q2m, q2ma, sym, omega, alpha1, alpha2, beta1, beta22, beta23, gamma12, gamma13, gamma22, gamma23);
}
else if(fileext == "hoho") {
real[int] sym1(sym.n), sym2(sym.n);
real omega1, omega2;
complex[string] alpha1, alpha2;
complex beta1, beta2, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23;
complex[int] q1m, q1ma, q2m, q2ma;
ub[] = loadhoho(fileroot, meshin, q1m, q1ma, q2m, q2ma, sym1, sym2, omega1, omega2, alpha1, alpha2, beta1, beta2, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23);
}
savebase(filein, (savecount > 0 ? fileout : ""), meshin, false, false);
}
else {
ub[] = loadbase(fileroot + "_" + count, meshin);
}
real lambda = getlambda(param);
real paramdiff = lambda - paramtarget;
Mat J;
createMatu(Th, J, Pk);
sym = 0;
real[int] ik(sym.n), ik2(sym.n), ik3(sym.n);
real iomega = 0.0, iomega2 = 0.0, iomega3 = 0.0;
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 JlPM(J.n, mpirank == 0 ? 1 : 0), yqPM(J.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
Mat Ja = [[J, JlPM], [yqPM', -1.0]]; // make dummy Jacobian
real[int] R(ub[].n), yqP(J.n), yqP0(J.n), qap(Ja.n);
int it, internalit, adaptflag;
real f, kappa, cosalpha, res, resp, delta, deltap, maxdelta;
// FUNCTIONS
func real[int] funcRa(real[int]& qa) {
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true, exchange = true); // PETSc to FreeFEM
if(mpirank == 0) lambda = qa(qa.n-1); // Extract parameter value from state vector on proc 0
broadcast(processor(0), lambda);
updatelambda(param, lambda);
R = vR(0, XMh, tgv = -1);
real[int] Ra;
ChangeNumbering(J, R, Ra); // FreeFEM to PETSc
if(mpirank == 0) {
Ra.resize(J.n+1); // Append 0 to residual vector on proc 0
Ra(Ra.n-1) = 0.0;
}
StepAdaptMonitors(Ra, qa, 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 << "." << endl;
return Ra;
}
func int funcJa(real[int]& qa) {
++it;
internalit = 0;
qap = qa;
resp = res;
deltap = delta;
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true, exchange = true); // PETSc to FreeFEM
if(mpirank == 0) lambda = qa(qa.n-1); // Extract parameter value from state vector on proc 0
broadcast(processor(0), lambda);
updatelambda(param, lambda + eps);
real[int] Jl = vR(0, XMh, tgv = -1);
updatelambda(param, lambda);
Jl -= R;
Jl /= eps;
if (it == 1 | refactor >= it) J = vJ(XMh, XMh, tgv = -1);
ChangeNumbering(J, Jl, yqP); // FreeFEM to PETSc
matrix tempPms = [[yqP]]; // dense array to sparse matrix
ChangeOperator(JlPM, tempPms, parent = Ja); // send to Mat
KSPSolve(J, yqP, yqP); // compute tangent vector in PETSc numbering
tempPms = [[yqP]]; // dense array to sparse matrix
ChangeOperator(yqPM, tempPms, parent = Ja); // send to Mat
return 0;
}
// set up Mat parameters
IFMACRO(Jprecon) Jprecon(0); ENDIFMACRO
set(Ja, 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);
set(J, IFMACRO(Jsetargs) Jsetargs, ENDIFMACRO sparams = "-prefix_push fieldsplit_0_ " + KSPparams + " -prefix_pop", prefix = "fieldsplit_0_", parent = Ja);
// PREDICTOR
real[int] qa;
ChangeNumbering(J, ub[], qa);
if(mpirank == 0) {
qa.resize(J.n+1);
qa(qa.n-1) = lambda;
}
broadcast(processor(0), lambda);
{
updatelambda(param, lambda + eps);
real[int] Jl = vR(0, XMh, tgv = -1);
updatelambda(param, lambda);
R = vR(0, XMh, tgv = -1);
Jl -= R;
Jl /= eps;
J = vJ(XMh, XMh, tgv = -1);
ChangeNumbering(J, Jl, yqP); // FreeFEM to PETSc
KSPSolve(J, yqP, yqP);
yqP0 = yqP;
}
while (!stopflag){
real[int] qa0 = qa;
real h, hl = (yqP'*yqP);
mpiAllReduce(hl, h, mpiCommWorld, mpiSUM);
h = h0/sqrt(h + 1.0);
qa(0:J.n-1) -= (h*yqP);
if (mpirank == 0) {
qa(qa.n-1) += h; // -= (-1.0*h)
lambda = qa(qa.n-1);
}
broadcast(processor(0), lambda);
updatelambda(param, lambda);
// CORRECTOR LOOP
int ret;
it = 0;
internalit = 0;
adaptflag = 0;
SNESSolve(Ja, 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(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);
if(mpirank == 0) lambda = qa(qa.n-1);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
ChangeNumbering(J, yb[], yqP, inverse = true);
XMhg defu(uG), defu(yG), defu(tempu);
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);
if(mpirank == 0) {
IFMACRO(dimension,2)
if(adaptto == "b") Thg = adaptmesh(Thg, adaptu(uG), adaptmeshoptions);
else if(adaptto == "by") Thg = adaptmesh(Thg, adaptu(uG), adaptu(yG), 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 == "b") 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 == "by") 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));
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);
Th = Thg;
Mat Adapt;
createMatu(Th, Adapt, Pk);
J = Adapt;
defu(ub) = initu(0.0);
defu(yb) = initu(0.0);
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);
Mat Adapt1(J.n, mpirank == 0 ? 1 : 0), Adapt2(J.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
JlPM = Adapt1;
yqPM = Adapt2;
Ja = [[J, JlPM], [yqPM', -1.0]]; // make dummy Jacobian
IFMACRO(Jprecon) Jprecon(0); ENDIFMACRO
set(J, IFMACRO(Jsetargs) Jsetargs, ENDIFMACRO sparams = "-prefix_push fieldsplit_0_ " + KSPparams + " -prefix_pop", prefix = "fieldsplit_0_", parent = Ja);
qa.resize(J.n);
ChangeNumbering(J, ub[], qa);
if(mpirank == 0) {
qa.resize(J.n+1);
qa(qa.n-1) = lambda;
}
R.resize(ub[].n);
yqP.resize(J.n);
ChangeNumbering(J, yb[], yqP);
yqP0.resize(J.n);
yqP0 = yqP;
qa0.resize(qa.n);
qap.resize(qa.n);
it = 0;
internalit = 0;
adaptflag = 1;
SNESSolve(Ja, 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);
if(mpirank == 0) lambda = qa(qa.n-1);
broadcast(processor(0), lambda);
updatelambda(param, lambda);
savebase(fileout + "_" + count + (forcesave ? "specialpt" : ""), (savecount > 0 ? fileout : ""), meshout, ((count % savecount == 0) || forcesave || stopflag), true);
forcesave = false;
yqP0 = yqP;
IFMACRO(Jprecon) Jprecon(0); ENDIFMACRO
}
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;
}
}