-
Notifications
You must be signed in to change notification settings - Fork 1
/
hohocompute.edp
1071 lines (1054 loc) · 43 KB
/
hohocompute.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// hohocompute.edp
// Chris Douglas
//
// EXAMPLE USAGE:
// Initialize with two mode or hopf files, solve on same mesh
// ff-mpirun -np 4 hohocompute.edp -param <PARAM1> -param2 <PARAM2> -fi <FILEIN> -fi2 <FILEIN2> -fo <FILEOUT1>
//
// Initialize with hoho from file, adapt mesh to solution
// ff-mpirun -np 4 hohocompute.edp -param <PARAM1> -param2 <PARAM2> -fi <FILEIN> -fo <FILEOUT1> -mo <MESHOUT>
//
// NOTE: This file should not be changed unless you know what you're doing.
//
/*
This script computes the normal form at a non-degenerate Hopf-Hopf point.
The normal form is written for the complex amplitudes Y = A1*exp(1i*omega1*t) and Z = A2*exp(1i*omega2*t) as:
dY/dt = alpha1*dlambda*Y + 1i*omega1*Y + beta1*Y*|Y|^2 + gamma1*Y*|Z|^2 + gamma12*Z^2 + gamma13*Z^3
dZ/dt = alpha2*dlambda*Z + 1i*omega2*Z + beta2*Z*|Z|^2 + gamma2*Z*|Y|^2 + gamma22*Y*Z^* + gamma23*Y*(Z^*)^2
where:
alpha are the coefficients for the terms from parameter changes,
beta are the coefficients for the terms from self-coupling harmonic interactions,
gamma are the coefficients for the terms from cross-coupling harmonic interactions.
*/
load "iovtk"
load "PETSc-complex"
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 filein2 = getARGV("-fi2", "");
string basefilein = getARGV("-bfi", "");
string fileout = getARGV("-fo", "");
bool normalform = getARGV("-nf", 1);
bool wnlsave = getARGV("-wnl", 0);
int select = getARGV("-select", 1);
int select2 = getARGV("-select2", 1);
bool zerofreq = getARGV("-zero", 0);
bool zerofreq2 = getARGV("-zero2", 0);
int res1x = getARGV("-res1x", 0);
string param = getARGV("-param", "");
string param2 = getARGV("-param2", "");
string adaptto = getARGV("-adaptto", "b");
real eps = getARGV("-eps", 1e-7);
real eps2 = getARGV("-eps2", 1e-7);
string sneslinesearchtype = getARGV("-snes_linesearch_type","basic");
real[int] sym1(sym.n), sym2(sym.n);
real omega, omega1, omega2;
complex[string] alpha1, alpha2;
complex beta1, beta2, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23;
// Load mesh, make FE basis
string fileroot1, fileext1 = parsefilename(filein, fileroot1); //extract file name and extension
string fileroot2, fileext2 = parsefilename(filein2, fileroot2); //extract file name and extension
parsefilename(fileout, fileout); // trim extension from output file, if given
if((fileext1 == "mode" || fileext1 == "resp" || fileext1 == "rslv" || fileext1 == "tdls") && basefilein == "") basefilein = readbasename(workdir + filein);
string basefileroot, basefileext = parsefilename(basefilein, basefileroot);
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
Th = readmeshN(workdir + meshin);
Thg = Th;
buildDmesh(Th);
restu = restrict(XMh, XMhg, n2o);
XMh<complex> defu(ub), defu(um), defu(uma), defu(um2), defu(um3);
if (fileext2 == "hopf") {
ub[].re = loadhopf(fileroot2, meshin, um2[], um3[], sym2, omega2, alpha2, beta2);
}
else if (fileext2 == "foho") {
real[string] alphaN;
real beta22, beta23, gamma22, gamma23;
complex gamma12, gamma13;
real[int] q2m, q2ma;
ub[].re = loadfoho(fileroot2, meshin, um2[], um3[], q2m, q2ma, sym2, omega2, alpha2, alphaN, beta2, beta22, beta23, gamma12, gamma13, gamma22, gamma23);
}
else if (fileext2 == "hoho") {
real omegaN;
complex[string] alphaN;
complex betaN, gammaN, gamma12, gamma13, gamma22, gamma23;
complex[int] qNm, qNma;
if(select2 == 1){
ub[].re = loadhoho(fileroot2, meshin, um2[], um3[], qNm, qNma, sym2, sym, omega2, omegaN, alpha2, alphaN, beta2, betaN, gammaN, gamma2, gamma12, gamma13, gamma22, gamma23);
}
else if(select2 == 2){
ub[].re = loadhoho(fileroot2, meshin, qNm, qNma, um2[], um3[], sym, sym2, omegaN, omega2, alphaN, alpha2, betaN, beta2, gamma2, gammaN, gamma12, gamma13, gamma22, gamma23);
}
}
else if (fileext2 == "mode") {
complex eigenvalue;
um2[] = loadmode(fileroot2, meshin, sym2, eigenvalue);
omega2 = imag(eigenvalue);
}
else if (fileext2 == "resp") {
um2[] = loadresp(fileroot2, meshin, sym2, omega2);
}
else if (fileext2 == "rslv") {
real gain;
complex[int] fm;
um2[] = loadrslv(fileroot2, meshin, fm, sym2, omega2, gain);
}
else if(fileext2 == "porb") {
int Nh=1;
complex[int, int] qh(um[].n, Nh);
ub[].re = loadporb(fileroot2, meshin, qh, sym2, omega2, Nh);
um2[] = qh(:, 0);
}
else if (fileext2 != "") assert(false); // invalid input filetype
if (fileext1 == "hoho" && fileext2 == "") {
ub[].re = loadhoho(fileroot1, meshin, um[], uma[], um2[], um3[], sym1, sym2, omega1, omega2, alpha1, alpha2, beta1, beta2, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23);
}
else if (fileext1 == "hoho" && fileext2 != "") {
real omegaN;
complex[string] alphaN;
complex betaN, gammaN, gamma12, gamma13, gamma22, gamma23;
complex[int] qNm, qNma;
if(select == 1){
ub[].re = loadhoho(fileroot1, meshin, um[], uma[], qNm, qNma, sym1, sym, omega1, omegaN, alpha1, alphaN, beta1, betaN, gamma1, gammaN, gamma12, gamma13, gamma22, gamma23);
}
else if(select == 2){
ub[].re = loadhoho(fileroot1, meshin, qNm, qNma, um[], uma[], sym, sym1, omegaN, omega1, alphaN, alpha1, betaN, beta1, gammaN, gamma1, gamma12, gamma13, gamma22, gamma23);
}
}
else if (fileext1 == "foho") {
real[string] alphaN;
real beta22, beta23, gamma22, gamma23;
complex gamma12, gamma13;
real[int] q2m, q2ma;
ub[].re = loadfoho(fileroot1, meshin, um[], uma[], q2m, q2ma, sym1, omega1, alpha1, alphaN, beta1, beta22, beta23, gamma12, gamma13, gamma22, gamma23);
}
else if (fileext1 == "hopf") {
ub[].re = loadhopf(fileroot1, meshin, um[], uma[], sym1, omega1, alpha1, beta1);
}
else if (fileext1 == "mode") {
complex eigenvalue;
um[] = loadmode(fileroot1, meshin, sym1, eigenvalue);
omega1 = imag(eigenvalue);
}
else if (fileext1 == "resp") {
um[] = loadresp(fileroot1, meshin, sym1, omega1);
}
else if (fileext1 == "rslv") {
real gain;
complex[int] fm;
um[] = loadrslv(fileroot1, meshin, fm, sym1, omega1, gain);
}
else if(fileext1 == "porb") {
int Nh=1;
complex[int, int] qh(um[].n, Nh);
ub[].re = loadporb(fileroot1, meshin, qh, sym1, omega, Nh);
um[] = qh(:, 0);
}
else assert(false); // invalid input filetype
if (basefileext == "base") {
ub[].re = loadbase(basefileroot, meshin);
}
else if(basefileext == "fold") {
real[string] alpha;
real beta;
real[int] qm, qma;
ub[].re = loadfold(basefileroot, meshin, qm, qma, alpha, beta);
}
else if(basefileext == "hopf") {
real omega;
complex[string] alpha;
complex beta;
complex[int] qm, qma;
ub[].re = loadhopf(basefileroot, meshin, qm, qma, sym, omega, alpha, beta);
}
else if(basefileext == "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[].re = loadfoho(basefileroot, meshin, q1m, q1ma, q2m, q2ma, sym, omega, alpha1, alpha2, beta1, beta22, beta23, gamma12, gamma13, gamma22, gamma23);
}
else if(basefileext == "hoho") {
real[int] symN(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[].re = loadhoho(basefileroot, meshin, q1m, q1ma, q2m, q2ma, sym, symN, omega1, omega2, alpha1, alpha2, beta1, beta2, gamma1, gamma2, gamma12, gamma13, gamma22, gamma23);
}
else if(basefileext == "tdns") {
real time;
ub[].re = loadtdns(basefileroot, meshin, time);
}
else if(basefileext == "porb") {
int Nh=1;
real omega;
complex[int, int] qh(um[].n, Nh);
ub[].re = loadporb(basefileroot, meshin, qh, sym, omega, Nh);
}
real lambda1 = getlambda(param);
real lambda2 = getlambda(param2);
// Create distributed Mat
Mat<complex> J;
createMatu(Th, J, Pk);
// MESH ADAPTATION
bool adapt = false;
if(meshout == "") meshout = meshin; // if no adaptation
else { // if output meshfile is given, adapt mesh
adapt = true;
meshout = meshout + "." + meshext;
complex[int] q;
ChangeNumbering(J, ub[], q);
ChangeNumbering(J, ub[], q, inverse = true);
ChangeNumbering(J, um[], q);
ChangeNumbering(J, um[], q, inverse = true);
ChangeNumbering(J, uma[], q);
ChangeNumbering(J, uma[], q, inverse = true);
ChangeNumbering(J, um2[], q);
ChangeNumbering(J, um2[], q, inverse = true);
ChangeNumbering(J, um3[], q);
ChangeNumbering(J, um3[], q, inverse = true);
XMhg defu(uG), defu(umrG), defu(umiG), defu(umarG), defu(umaiG), defu(tempu), defu(um2rG), defu(um2iG), defu(um3rG), defu(um3iG);
tempu[](restu) = ub[].re; // populate local portion of global soln
mpiAllReduce(tempu[], uG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um[].re; // populate local portion of global soln
mpiAllReduce(tempu[], umrG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um[].im; // populate local portion of global soln
mpiAllReduce(tempu[], umiG[], mpiCommWorld, mpiSUM);
tempu[](restu) = uma[].re; // populate local portion of global soln
mpiAllReduce(tempu[], umarG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um[].im; // populate local portion of global soln
mpiAllReduce(tempu[], umaiG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um2[].re; // populate local portion of global soln
mpiAllReduce(tempu[], um2rG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um2[].im; // populate local portion of global soln
mpiAllReduce(tempu[], um2iG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um3[].re; // populate local portion of global soln
mpiAllReduce(tempu[], um3rG[], mpiCommWorld, mpiSUM);
tempu[](restu) = um3[].im; // populate local portion of global soln
mpiAllReduce(tempu[], um3iG[], mpiCommWorld, mpiSUM);
if(mpirank == 0) { // Perform mesh adaptation (serially) on processor 0
IFMACRO(dimension,2)
if(adaptto == "b") Thg = adaptmesh(Thg, adaptu(uG), adaptmeshoptions);
else if(adaptto == "bd") Thg = adaptmesh(Thg, adaptu(uG), adaptu(umrG), adaptu(umiG), adaptu(um2rG), adaptu(um2iG), adaptmeshoptions);
else if(adaptto == "ba") Thg = adaptmesh(Thg, adaptu(uG), adaptu(umarG), adaptu(umaiG), adaptu(um3rG), adaptu(um3iG), adaptmeshoptions);
else if(adaptto == "bda") Thg = adaptmesh(Thg, adaptu(uG), adaptu(umrG), adaptu(umiG), adaptu(umarG), adaptu(umaiG), adaptu(um2rG), adaptu(um2iG), adaptu(um3rG), adaptu(um3iG), 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 == "bd") met = mshmet(Thg, adaptu(uG), adaptu(umrG), adaptu(umiG), adaptu(um2rG), adaptu(um2iG), 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 == "ba") met = mshmet(Thg, adaptu(uG), adaptu(umarG), adaptu(umaiG), adaptu(um3rG), adaptu(um3iG), 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 == "bda") met = mshmet(Thg, adaptu(uG), adaptu(umrG), adaptu(umiG), adaptu(umarG), adaptu(umaiG), adaptu(um2rG), adaptu(um2iG), adaptu(um3rG), adaptu(um3iG), 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
} // TODO: add adaptation to endogeneity and structural sensitivity
broadcast(processor(0), Thg);
defu(uG) = defu(uG);
defu(umrG) = defu(umrG);
defu(umiG) = defu(umiG);
defu(umarG) = defu(umarG);
defu(umaiG) = defu(umaiG);
defu(um2rG) = defu(um2rG);
defu(um2iG) = defu(um2iG);
defu(um3rG) = defu(um3rG);
defu(um3iG) = defu(um3iG);
Th = Thg;
Mat<complex> Adapt;
createMatu(Th, Adapt, Pk);
J = Adapt;
defu(ub) = initu(0.0);
defu(um) = initu(0.0);
defu(uma) = initu(0.0);
defu(um2) = initu(0.0);
defu(um3) = 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[].re = uG[](restu);
um[].re = umrG[](restu);
um[].im = umiG[](restu);
uma[].re = umarG[](restu);
uma[].im = umaiG[](restu);
um2[].re = um2rG[](restu);
um2[].im = um2iG[](restu);
um3[].re = um3rG[](restu);
um3[].im = um3iG[](restu);
}
complex[int] ik(sym.n), ik2(sym.n), ik3(sym.n);
complex iomega, iomega2 = 0.0, iomega3 = 0.0;
include "eqns.idp"
// Build bordered block matrix from only Mat components
Mat<complex> JlPM(J.n, mpirank == 0 ? (4-zerofreq-zerofreq2) : 0), gqPM(J.n, mpirank == 0 ? (4-zerofreq-zerofreq2) : 0), glPM(mpirank == 0 ? (4-zerofreq-zerofreq2) : 0, mpirank == 0 ? (4-zerofreq-zerofreq2) : 0); // Initialize Mat objects for bordered matrix
Mat<complex> H(J), Ja = [[J, JlPM], [gqPM', glPM]]; // make dummy Jacobian
complex[int] R(ub[].n), q1m(J.n), q1ma(J.n), p1P(J.n), q1P(J.n), q2m(J.n), q2ma(J.n), p2P(J.n), q2P(J.n);
// FUNCTIONS
func complex[int] funcRa(complex[int]& qa) {
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true, exchange = true); // PETSc to FreeFEM
if(mpirank == 0) {
lambda1 = real(qa(qa.n-(4-zerofreq-zerofreq2))); // Extract parameter value from state vector on proc 0
omega1 = zerofreq ? 0.0 : real(qa(qa.n-(3-zerofreq2))); // Extract frequency value from state vector on proc 0
lambda2 = real(qa(qa.n-(2-zerofreq2))); // Extract parameter value from state vector on proc 0
omega2 = zerofreq2 ? 0.0 : real(qa(qa.n-1)); // Extract frequency value from state vector on proc 0
}
broadcast(processor(0), lambda1);
broadcast(processor(0), omega1);
broadcast(processor(0), lambda2);
broadcast(processor(0), omega2);
updatelambda(param, lambda1);
updatelambda(param2, lambda2);
sym = 0;
R = vR(0, XMh, tgv = -1);
complex[int] Ra;
ChangeNumbering(J, R, Ra); // FreeFEM to PETSc
iomega = 1i*omega1;
ik.im = sym1;
sym = sym1;
J = vJ(XMh, XMh, tgv = -2);
KSPSolve(J, p1P, q1m);
KSPSolveHermitianTranspose(J, q1P, q1ma);
complex ginv, ginvl = (q1P'*q1m);
mpiAllReduce(ginvl, ginv, mpiCommWorld, mpiSUM);
q1m /= ginv; // rescale direct mode
q1ma /= conj(ginv); // rescale adjoint mode
iomega = 1i*omega2;
ik.im = sym2;
sym = sym2;
J = vJ(XMh, XMh, tgv = -2);
KSPSolve(J, p2P, q2m);
KSPSolveHermitianTranspose(J, q2P, q2ma);
complex hinv, hinvl = (q2P'*q2m);
mpiAllReduce(hinvl, hinv, mpiCommWorld, mpiSUM);
q2m /= hinv; // rescale direct mode
q2ma /= conj(hinv); // rescale adjoint mode
if(mpirank == 0) {
Ra.resize(Ra.n+(4-zerofreq-zerofreq2)); // Append 0 to residual vector on proc 0
Ra(Ra.n-(4-zerofreq-zerofreq2)) = real(1.0/ginv);
if(!zerofreq) Ra(Ra.n-(3-zerofreq2)) = imag(1.0/ginv);
Ra(Ra.n-(2-zerofreq2)) = real(1.0/hinv);
if(!zerofreq2) Ra(Ra.n-1) = imag(1.0/hinv);
}
return Ra;
}
func int funcJa(complex[int]& qa) {
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true, exchange = true); // PETSc to FreeFEM
if(mpirank == 0) {
lambda1 = real(qa(qa.n-(4-zerofreq-zerofreq2))); // Extract parameter value from state vector on proc 0
omega1 = zerofreq ? 0.0 : real(qa(qa.n-(3-zerofreq2))); // Extract frequency value from state vector on proc 0
lambda2 = real(qa(qa.n-(2-zerofreq2))); // Extract parameter value from state vector on proc 0
omega2 = zerofreq2 ? 0.0 : real(qa(qa.n-1)); // Extract frequency value from state vector on proc 0
}
broadcast(processor(0), lambda1);
broadcast(processor(0), omega1);
broadcast(processor(0), lambda2);
broadcast(processor(0), omega2);
ChangeNumbering(J, uma[], q1ma, inverse = true);
ChangeNumbering(J, um3[], q2ma, inverse = true);
updatelambda(param, lambda1 + eps);
updatelambda(param2, lambda2);
sym = 0;
complex[int] Jl1 = vR(0, XMh, tgv = -1);
sym = sym1;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
complex[int] Hl1 = vJ(0, XMh, tgv = -10);
sym = sym2;
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
complex[int] Kl1 = vJ(0, XMh, tgv = -10);
updatelambda(param, lambda1);
updatelambda(param2, lambda2 + eps2);
sym = 0;
complex[int] Jl2 = vR(0, XMh, tgv = -1);
sym = sym1;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
complex[int] Hl2 = vJ(0, XMh, tgv = -10);
sym = sym2;
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
complex[int] temp1(J.n), temp2(J.n), temp3(J.n), temp4(J.n), Kl2 = vJ(0, XMh, tgv = -10);
updatelambda(param2, lambda2);
Jl1 -= R;
Jl2 -= R;
Jl1 /= eps;
Jl2 /= eps2;
sym = sym1;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
R = vJ(0, XMh, tgv = -10);
Hl1 -= R;
Hl2 -= R;
Hl1 /= eps;
Hl2 /= eps2;
sym = sym2;
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
R = vJ(0, XMh, tgv = -10);
Kl1 -= R;
Kl2 -= R;
Kl1 /= eps;
Kl2 /= eps2;
ChangeNumbering(J, Jl1, temp1); // FreeFEM to PETSc
ChangeNumbering(J, Jl2, temp3); // FreeFEM to PETSc
matrix<complex> tempPms;
if (zerofreq && zerofreq2) tempPms = [[temp1, temp3]];
else if (!zerofreq && zerofreq2) tempPms = [[temp1, 0, temp3]];
else if (zerofreq && !zerofreq2) tempPms = [[temp1, temp3, 0]];
else tempPms = [[temp1, 0, temp3, 0]]; // dense array to sparse matrix
ChangeOperator(JlPM, tempPms, parent = Ja); // send to Mat
complex gl1 = J(uma[], Hl1);
complex gl2 = J(uma[], Hl2);
complex hl1 = J(um3[], Kl1);
complex hl2 = J(um3[], Kl2);
if (zerofreq && zerofreq2) tempPms = [[real(gl1), real(gl2)], [real(hl1), real(hl2)]];
else if (!zerofreq && zerofreq2) {
sym = sym1;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
R = vM(0, XMh, tgv = -10);
complex gw = J(uma[], R);
tempPms = [[real(gl1), -imag(gw), real(gl2) ],
[imag(gl1), real(gw), imag(gl2) ],
[real(hl1), 0 , real(hl2) ]];
}
else if (zerofreq && !zerofreq2) {
sym = sym2;
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
R = vM(0, XMh, tgv = -10);
complex hw = J(um3[], R);
tempPms = [[real(gl1), real(gl2), 0 ],
[real(hl1), real(hl2), -imag(hw) ],
[imag(hl1), imag(hl2), real(hw) ]];
}
else {
sym = sym1;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
R = vM(0, XMh, tgv = -10);
complex gw = J(uma[], R);
sym = sym2;
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
R = vM(0, XMh, tgv = -10);
complex hw = J(um3[], R);
tempPms = [[real(gl1), -imag(gw), real(gl2), 0 ],
[imag(gl1), real(gw), imag(gl2), 0 ],
[real(hl1), 0 , real(hl2), -imag(hw) ],
[imag(hl1), 0 , imag(hl2), real(hw) ]];
}
ChangeOperator(glPM, tempPms, parent = Ja); // send to Mat
sym = 0;
ik.im = sym1;
iomega = 1i*omega1;
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
H = vH(XMh, XMh, tgv = 0); // form the matrix (dL/dq*w)
MatMultHermitianTranspose(H, q1ma, temp1); // gqr,i
ik.im = sym2;
iomega = 1i*omega2;
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
H = vH(XMh, XMh, tgv = 0); // form the matrix (dL/dq*w)
MatMultHermitianTranspose(H, q2ma, temp3); // gqr,i
if(!zerofreq) temp2.re = -temp1.im;
temp1.im = 0.0;
if(!zerofreq2) temp4.re = -temp3.im;
temp3.im = 0.0;
if (zerofreq && zerofreq2) tempPms = [[temp1, temp3]];
else if (!zerofreq && zerofreq2) tempPms = [[temp1, temp2, temp3]];
else if (zerofreq && !zerofreq2) tempPms = [[temp1, temp3, temp4]];
else tempPms = [[temp1, temp2, temp3, temp4]]; // dense array to sparse matrix
ChangeOperator(gqPM, tempPms, parent = Ja); // send to Mat
ik = 0.0;
iomega = 0.0;
sym = 0;
J = vJ(XMh, XMh, tgv = -1);
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);
// Initialize
complex[int] qa;
ChangeNumbering(J, ub[], qa);
if(mpirank == 0) {
qa.resize(qa.n+(4-zerofreq-zerofreq2));
qa(qa.n-(4-zerofreq-zerofreq2)) = lambda1;
if(!zerofreq) qa(qa.n-(3-zerofreq2)) = omega1;
qa(qa.n-(2-zerofreq2)) = lambda2;
if(!zerofreq2) qa(qa.n-1) = omega2;
}
{
sym = sym1;
complex[int] Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q1m);
ChangeNumbering(J, um[], q1m, inverse = true);
real Mnorm = sqrt(real(J(um[], Mq)));
Mq /= Mnorm;
ChangeNumbering(J, Mq, q1P);
if (fileext1 == "hopf" || fileext1 == "foho" || fileext1 == "hoho") um[] = uma[];
else {
ik.im = sym1;
iomega = 1i*omega1;
J = vJ(XMh, XMh, tgv = -3);
KSPSolveHermitianTranspose(J, q1P, q1ma);
ChangeNumbering(J, um[], q1ma, inverse = true, exchange = true);
}
Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q1m, inverse = true);
Mq *= (Mnorm/J(um[], Mq)); // so that <uma[],M*um[]> = 1
ChangeNumbering(J, Mq, p1P);
um[] = um2[];
sym = sym2;
Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q2m);
ChangeNumbering(J, um[], q2m, inverse = true);
Mnorm = sqrt(real(J(um[], Mq)));
Mq /= Mnorm;
ChangeNumbering(J, Mq, q2P);
if (fileext2 == "hopf" || fileext2 == "foho" || fileext2 == "hoho") um[] = um3[];
else {
ik.im = sym2;
iomega = 1i*omega2;
J = vJ(XMh, XMh, tgv = -3);
KSPSolveHermitianTranspose(J, q2P, q2ma);
ChangeNumbering(J, um[], q2ma, inverse = true, exchange = true);
}
Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q2m, inverse = true);
Mq *= (Mnorm/J(um[], Mq)); // so that <uma[],M*um[]> = 1
ChangeNumbering(J, Mq, p2P);
}
// solve nonlinear problem with SNES
int ret;
SNESSolve(Ja, funcJa, funcRa, qa, reason = ret,
sparams = "-snes_linesearch_type " + sneslinesearchtype + " -snes_monitor -snes_converged_reason -options_left no");
if (ret > 0) { // Save solution if solver converged and output file is given
ChangeNumbering(J, ub[], qa(0:J.n-1), inverse = true, exchange = true); // PETSc to FreeFEM
if(mpirank == 0) {
lambda1 = real(qa(qa.n-(4-zerofreq-zerofreq2))); // Extract parameter value from state vector on proc 0
omega1 = zerofreq ? 0.0 : real(qa(qa.n-(3-zerofreq2))); // Extract frequency value from state vector on proc 0
lambda2 = real(qa(qa.n-(2-zerofreq2))); // Extract parameter value from state vector on proc 0
omega2 = zerofreq2 ? 0.0 : real(qa(qa.n-1)); // Extract frequency value from state vector on proc 0
}
broadcast(processor(0), lambda1);
broadcast(processor(0), omega1);
broadcast(processor(0), lambda2);
broadcast(processor(0), omega2);
updatelambda(param, lambda1);
updatelambda(param2, lambda2);
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
sym = sym1;
complex[int] Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q1m, inverse = true);
ChangeNumbering(J, uma[], q1ma, inverse = true);
real Mnorm = sqrt(real(J(um[], Mq)));
um[] /= Mnorm; // so that <um[],M*um[]> = 1
uma[] *= (Mnorm/J(Mq, uma[])); // so that <uma[],M*um[]> = 1
ChangeNumbering(J, um[], q1m);
ChangeNumbering(J, uma[], q1ma);
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
sym = sym2;
Mq = vM(0, XMh, tgv = -10);
ChangeNumbering(J, um2[], q2m, inverse = true);
ChangeNumbering(J, um3[], q2ma, inverse = true);
Mnorm = sqrt(real(J(um2[], Mq)));
um2[] /= Mnorm; // so that <um[],M*um[]> = 1
um3[] *= (Mnorm/J(Mq, um3[])); // so that <uma[],M*um[]> = 1
ChangeNumbering(J, um2[], q2m);
ChangeNumbering(J, um3[], q2ma);
if (normalform){
complex[int] qAAs(J.n), qABs(J.n), qBBs(J.n), qAA(J.n), qAB(J.n), qBB(J.n), tempP(J.n), temp(um[].n), temp1(um[].n);
complex[int,int] qDa(paramnames.n, J.n);
Mat<complex> qPM(J.n, mpirank == 0 ? 1 : 0), pPM(J.n, mpirank == 0 ? 1 : 0); // Initialize Mat objects for bordered matrix
Mat<complex> Ja = [[J, qPM], [pPM', 0]]; // make dummy Jacobian
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);
IFMACRO(Jprecon) Jprecon(0); ENDIFMACRO
set(J, IFMACRO(Jsetargs) Jsetargs, ENDIFMACRO sparams = "-prefix_push fieldsplit_0_ " + KSPparams + " -prefix_pop", prefix = "fieldsplit_0_", parent = Ja);
if (res1x == 1){
omega2 = (omega2 + omega1)/2.0;
omega1 = omega2;
}
else if (res1x == 2){
omega2 = (2.0*omega2 + omega1)/4.0;
omega1 = 2.0*omega2;
}
else if (res1x == 3){
omega2 = (3.0*omega2 + omega1)/6.0;
omega1 = 3.0*omega2;
}
// 2nd-order
// A: base modifications due to parameter changes
ik = 0.0;
ik2 = 0.0;
iomega = 0.0;
iomega2 = 0.0;
sym = 0;
J = vJ(XMh, XMh, tgv = -1);
if(paramnames[0] != ""){
temp1 = vR(0, XMh, tgv = -1);
for (int k = 0; k < paramnames.n; ++k){
lambda1 = getlambda(paramnames[k]);
updatelambda(paramnames[k], lambda1 + eps);
temp = vR(0, XMh, tgv = -1);
updatelambda(paramnames[k], lambda1);
temp -= temp1;
temp /= -eps;
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
KSPSolve(J, tempP, qAAs);
qDa(k, :) = qAAs;
}
}
// B: base modifications due to quadratic nonlinear interactions
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
um2[] = conj(um[]);
ik.im = sym1;
ik2.im = -sym1;
iomega = 1i*omega1;
iomega2 = -1i*omega1;
temp = vH(0, XMh, tgv = -10);
temp.re *= -1.0; // -2.0/2.0
temp.im = 0.0;
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
KSPSolve(J, tempP, qAAs);
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
um2[] = conj(um[]);
ik.im = sym2;
ik2.im = -sym2;
iomega = 1i*omega2;
iomega2 = -1i*omega2;
temp = vH(0, XMh, tgv = -10);
temp.re *= -1.0; // -2.0/2.0
temp.im = 0.0;
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
KSPSolve(J, tempP, qBBs);
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ChangeNumbering(J, um2[], q2m, inverse = true, exchange = true);
um2[] = conj(um2[]);
ik.im = sym1;
ik2.im = -sym2;
iomega = 1i*omega1;
iomega2 = -1i*omega2;
sym = sym1 - sym2;
temp = vH(0, XMh, tgv = -10);
temp *= -1.0; // -2.0/2.0
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
ik.im = (sym1 - sym2);
iomega = 1i*(omega1 - omega2);
J = vJ(XMh, XMh, tgv = -1);
if (res1x != 2) KSPSolve(J, tempP, qABs);
else {
complex[int] tempPa = tempP, sol;
if(mpirank == 0) {
tempPa.resize(tempPa.n+1);
tempPa(tempPa.n-1) = 0.0;
}
ChangeNumbering(J, um[], q2ma, inverse = true, exchange = true);
temp = vM(0, XMh, tgv = -10);
ChangeNumbering(J, temp, tempP);
matrix<complex> tempPms = [[tempP]]; // dense array to sparse matrix
ChangeOperator(pPM, tempPms, parent = Ja); // send to Mat
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
temp = vM(0, XMh, tgv = -10);
ChangeNumbering(J, temp, tempP);
tempPms = [[tempP]]; // dense array to sparse matrix
ChangeOperator(qPM, tempPms, parent = Ja); // send to Mat
KSPSolve(Ja, tempPa, sol);
qABs = sol(0:J.n-1);
if(mpirank == 0) gamma22 = sol(sol.n-1);
broadcast(processor(0), gamma22);
}
// C: harmonics generation due to quadratic nonlinear interactions
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
um2[] = um[];
ik.im = sym1;
ik2.im = sym1;
iomega = 1i*omega1;
iomega2 = 1i*omega1;
sym = 2.0*sym1;
temp = vH(0, XMh, tgv = -10);
temp *= -0.5; // -1.0/2.0
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
ik.im = 2.0*sym1;
iomega = 2i*omega1;
J = vJ(XMh, XMh, tgv = -1);
KSPSolve(J, tempP, qAA);
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ChangeNumbering(J, um2[], q2m, inverse = true, exchange = true);
ik.im = sym1;
ik2.im = sym2;
iomega = 1i*omega1;
iomega2 = 1i*omega2;
sym = sym1 + sym2;
temp = vH(0, XMh, tgv = -10);
temp *= -1.0; // -2.0/2.0
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
ik.im = (sym1 + sym2);
iomega = 1i*(omega1 + omega2);
J = vJ(XMh, XMh, tgv = -1);
KSPSolve(J, tempP, qAB);
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
um2[] = um[];
ik.im = sym2;
ik2.im = sym2;
iomega = 1i*omega2;
iomega2 = 1i*omega2;
sym = 2.0*sym2;
temp = vH(0, XMh, tgv = -10);
temp *= -0.5; // -1.0/2.0
ChangeNumbering(J, temp, tempP); // FreeFEM to PETSc
ik.im = 2*sym2;
iomega = 2i*omega2;
J = vJ(XMh, XMh, tgv = -1);
if (res1x != 2) KSPSolve(J, tempP, qBB);
else {
complex[int] tempPa = tempP, sol;
if(mpirank == 0) {
tempPa.resize(tempPa.n+1);
tempPa(tempPa.n-1) = 0.0;
}
ChangeNumbering(J, um[], q1ma, inverse = true, exchange = true);
temp = vM(0, XMh, tgv = -10);
ChangeNumbering(J, temp, tempP);
matrix<complex> tempPms = [[tempP]]; // dense array to sparse matrix
ChangeOperator(pPM, tempPms, parent = Ja); // send to Mat
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
temp = vM(0, XMh, tgv = -10);
ChangeNumbering(J, temp, tempP);
tempPms = [[tempP]]; // dense array to sparse matrix
ChangeOperator(qPM, tempPms, parent = Ja); // send to Mat
KSPSolve(Ja, tempPa, sol);
qBB = sol(0:J.n-1);
if(mpirank == 0) gamma12 = sol(sol.n-1);
broadcast(processor(0), gamma12);
}
// 3rd-order
// fundamental modifications due to parameter changes and quadratic interaction of fundamental with 2nd order base modification.
// A
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ChangeNumbering(J, uma[], q1ma, inverse = true);
sym = sym1;
ik.im = sym1;
ik2 = 0.0;
iomega = 1i*omega1;
iomega2 = 0.0;
if(paramnames[0] != ""){
temp1 = vJ(0, XMh, tgv = -10);
for (int k = 0; k < paramnames.n; ++k){
lambda1 = getlambda(paramnames[k]);
updatelambda(paramnames[k], lambda1 + eps);
temp = vJ(0, XMh, tgv = -10);
updatelambda(paramnames[k], lambda1);
temp -= temp1;
temp /= eps;
ChangeNumbering(J, um2[], qDa(k, :), inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10); // 2.0/2.0
alpha1[paramnames[k]] = -J(uma[], temp);
}
}
// B
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
ChangeNumbering(J, uma[], q2ma, inverse = true);
sym = sym2;
ik.im = sym2;
ik2 = 0.0;
iomega = 1i*omega2;
iomega2 = 0.0;
if(paramnames[0] != ""){
temp1 = vJ(0, XMh, tgv = -10);
for (int k = 0; k < paramnames.n; ++k){
lambda1 = getlambda(paramnames[k]);
updatelambda(paramnames[k], lambda1 + eps);
temp = vJ(0, XMh, tgv = -10);
updatelambda(paramnames[k], lambda1);
temp -= temp1;
temp /= eps;
ChangeNumbering(J, um2[], qDa(k, :), inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10); // 2.0/2.0
alpha2[paramnames[k]] = -J(uma[], temp);
}
}
// A|A|^2
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
um2[] = um[];
um3[] = conj(um[]);
ChangeNumbering(J, uma[], q1ma, inverse = true);
sym = sym1;
ik.im = sym1;
ik2.im = sym1;
ik3.im = -sym1;
iomega = 1i*omega1;
iomega2 = 1i*omega1;
iomega3 = -1i*omega1;
temp = vT(0, XMh, tgv = -10);
temp *= 0.5; //3.0/6.0
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ik.im = sym1;
ik2 = 0.0;
iomega = 1i*omega1;
iomega2 = 0.0;
ChangeNumbering(J, um2[], qAAs, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
// D: fundamental modification due to quadratic interaction of fundamental with 2nd order modification C
ik.im = -sym1;
ik2.im = 2.0*sym1;
iomega = -1i*omega1;
iomega2 = 2i*omega1;
um[] = um3[];
ChangeNumbering(J, um2[], qAA, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
beta1 = -J(uma[], temp);
// B|B|^2
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
um2[] = um[];
um3[] = conj(um[]);
ChangeNumbering(J, uma[], q2ma, inverse = true);
sym = sym2;
ik.im = sym2;
ik2.im = sym2;
ik3.im = -sym2;
iomega = 1i*omega2;
iomega2 = 1i*omega2;
iomega3 = -1i*omega2;
temp = vT(0, XMh, tgv = -10);
temp *= 0.5; //3.0/6.0
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ik.im = sym2;
ik2 = 0.0;
iomega = 1i*omega2;
iomega2 = 0.0;
ChangeNumbering(J, um2[], qBBs, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
// D: fundamental modification due to quadratic interaction of fundamental with 2nd order modification C
ik.im = -sym2;
ik2.im = 2.0*sym2;
iomega = -1i*omega2;
iomega2 = 2i*omega2;
um[] = um3[];
ChangeNumbering(J, um2[], qBB, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
beta2 = -J(uma[], temp);
// A|B|^2
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ChangeNumbering(J, um2[], q2m, inverse = true, exchange = true);
um3[] = conj(um2[]);
ChangeNumbering(J, uma[], q1ma, inverse = true);
sym = sym1;
ik.im = sym1;
ik2.im = sym2;
ik3.im = -sym2;
iomega = 1i*omega1;
iomega2 = 1i*omega2;
iomega3 = -1i*omega2;
temp = vT(0, XMh, tgv = -10);
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ik.im = sym1;
ik2 = 0.0;
iomega = 1i*omega1;
iomega2 = 0.0;
ChangeNumbering(J, um2[], qBBs, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
ik.im = sym2;
ik2.im = sym1 - sym2;
iomega = 1i*omega2;
iomega2 = 1i*(omega1 - omega2);
ChangeNumbering(J, um2[], qABs, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
// D: fundamental modification due to quadratic interaction of fundamental with 2nd order modification C
um[] = um3[];
ik.im = -sym2;
ik2.im = sym1 + sym2;
iomega = -1i*omega2;
iomega2 = 1i*(omega1 + omega2);
ChangeNumbering(J, um2[], qAB, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
gamma1 = -J(uma[], temp);
// B|A|^2
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
ChangeNumbering(J, um2[], q1m, inverse = true, exchange = true);
um3[] = conj(um2[]);
ChangeNumbering(J, uma[], q2ma, inverse = true);
sym = sym2;
ik.im = sym2;
ik2.im = sym1;
ik3.im = -sym1;
iomega = 1i*omega2;
iomega2 = 1i*omega1;
iomega3 = -1i*omega1;
temp = vT(0, XMh, tgv = -10);
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ik.im = sym2;
ik2 = 0.0;
iomega = 1i*omega2;
iomega2 = 0.0;
ChangeNumbering(J, um2[], qAAs, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ik.im = sym1;
ik2.im = sym2 - sym1;
iomega = 1i*omega1;
iomega2 = 1i*(omega2 - omega1);
ChangeNumbering(J, um2[], qABs, inverse = true, exchange = true); // FreeFEM to PETSc
um2[] = conj(um2[]);
temp += vH(0, XMh, tgv = -10);
// D: fundamental modification due to quadratic interaction of fundamental with 2nd order modification C
um[] = um3[];
ik.im = -sym1;
ik2.im = sym1 + sym2;
iomega = -1i*omega1;
iomega2 = 1i*(omega1 + omega2);
ChangeNumbering(J, um2[], qAB, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
gamma2 = -J(uma[], temp);
if (res1x == 3){
// B^3
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
um2[] = um[];
um3[] = um[];
ChangeNumbering(J, uma[], q1ma, inverse = true);
sym = sym1;
ik.im = sym2;
ik2.im = sym2;
ik3.im = sym2;
iomega = 1i*omega2;
iomega2 = 1i*omega2;
iomega3 = 1i*omega2;
temp = vT(0, XMh, tgv = -10);
temp *= 1.0/6.0;
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ChangeNumbering(J, um[], q2m, inverse = true, exchange = true);
ik.im = sym2;
ik2.im = 2.0*sym2;
iomega = 1i*omega2;
iomega2 = 2i*omega2;
ChangeNumbering(J, um2[], qBB, inverse = true, exchange = true); // FreeFEM to PETSc
temp += vH(0, XMh, tgv = -10);
gamma13 = -J(uma[], temp);
// A*(B^*)^2
// B: fundamental modification due to cubic self-interaction of fundamental
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ChangeNumbering(J, um2[], q2m, inverse = true, exchange = true);
um2[] = conj(um2[]);
um3[] = um2[];
ChangeNumbering(J, uma[], q2ma, inverse = true);
sym = sym1 - 2.0*sym2;
ik.im = sym1;
ik2.im = -sym2;
ik3.im = -sym2;
iomega = 1i*omega1;
iomega2 = -1i*omega2;
iomega3 = -1i*omega2;
temp = vT(0, XMh, tgv = -10);
temp *= 0.5;
// C: fundamental modification due to quadratic interaction of fundamental with 2nd order modification B
ChangeNumbering(J, um[], q1m, inverse = true, exchange = true);
ik.im = sym1;
ik2.im = -2.0*sym2;
iomega = 1i*omega1;
iomega2 = -2i*omega2;
ChangeNumbering(J, um2[], qBB, inverse = true, exchange = true); // FreeFEM to PETSc
um2[] = conj(um2[]);
temp += vH(0, XMh, tgv = -10);