forked from AliceO2Group/AliceO2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoissonSolver.cxx
More file actions
1527 lines (1316 loc) · 67.2 KB
/
PoissonSolver.cxx
File metadata and controls
1527 lines (1316 loc) · 67.2 KB
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
// Copyright 2019-2020 CERN and copyright holders of ALICE O2.
// See https://alice-o2.web.cern.ch/copyright for details of the copyright holders.
// All rights not expressly granted are reserved.
//
// This software is distributed under the terms of the GNU General Public
// License v3 (GPL Version 3), copied verbatim in the file "COPYING".
//
// In applying this license CERN does not waive the privileges and immunities
// granted to it by virtue of its status as an Intergovernmental Organization
// or submit itself to any jurisdiction.
/// \file PoissonSolver.cxx
/// \brief This class provides implementation of Poisson Eq
/// solver by MultiGrid Method
///
///
///
/// \author Matthias Kleiner <mkleiner@ikf.uni-frankfurt.de>
/// \date Aug 21, 2020
#include "TPCSpaceCharge/PoissonSolver.h"
#include "TPCSpaceCharge/PoissonSolverHelpers.h"
#include "Framework/Logger.h"
#include <numeric>
#include <fmt/core.h>
#include "TPCSpaceCharge/Vector3D.h"
#include "TPCSpaceCharge/DataContainer3D.h"
#include "DataFormatsTPC/Defs.h"
#ifdef WITH_OPENMP
#include <omp.h>
#endif
using namespace o2::tpc;
template <typename DataT>
void PoissonSolver<DataT>::poissonSolver3D(DataContainer& matricesV, const DataContainer& matricesCharge, const int symmetry)
{
using timer = std::chrono::high_resolution_clock;
auto start = timer::now();
if (MGParameters::isFull3D) {
poissonMultiGrid3D(matricesV, matricesCharge, symmetry);
} else {
poissonMultiGrid3D2D(matricesV, matricesCharge, symmetry);
}
auto stop = timer::now();
std::chrono::duration<float> time = stop - start;
const float totalTime = time.count();
LOGP(detail, "poissonSolver3D took {}s", totalTime);
}
template <typename DataT>
void PoissonSolver<DataT>::poissonSolver2D(DataContainer& matricesV, const DataContainer& matricesCharge)
{
poissonMultiGrid2D(matricesV, matricesCharge);
}
template <typename DataT>
void PoissonSolver<DataT>::poissonMultiGrid2D(DataContainer& matricesV, const DataContainer& matricesCharge, const int iPhi)
{
/// Geometry of TPC -- should be use AliTPCParams instead
const DataT gridSpacingR = getSpacingR();
const DataT gridSpacingZ = getSpacingZ();
const DataT ratioZ = gridSpacingR * gridSpacingR / (gridSpacingZ * gridSpacingZ); // ratio_{Z} = gridSize_{r} / gridSize_{z}
int nGridRow = 0; // number grid
int nGridCol = 0; // number grid
int nnRow = mParamGrid.NRVertices;
while (nnRow >>= 1) {
++nGridRow;
}
int nnCol = mParamGrid.NZVertices;
while (nnCol >>= 1) {
++nGridCol;
}
// Check that number of mParamGrid.NRVertices and mParamGrid.NZVertices is suitable for multi grid
if (!isPowerOfTwo(mParamGrid.NRVertices - 1)) {
LOGP(error, "PoissonMultiGrid2D: PoissonMultiGrid - Error in the number of mParamGrid.NRVertices. Must be 2**M + 1");
return;
}
if (!isPowerOfTwo(mParamGrid.NZVertices - 1)) {
LOGP(error, "PoissonMultiGrid2D: PoissonMultiGrid - Error in the number of mParamGrid.NZVertices. Must be 2**N + 1");
return;
}
const int nLoop = std::max(nGridRow, nGridCol); // Calculate the number of nLoop for the binary expansion
LOGP(detail, "{}", fmt::format("PoissonMultiGrid2D: nGridRow={}, nGridCol={}, nLoop={}, nMGCycle={}", nGridRow, nGridCol, nLoop, MGParameters::nMGCycle));
unsigned int iOne = 1; // index
unsigned int jOne = 1; // index
int tnRRow = mParamGrid.NRVertices;
int tnZColumn = mParamGrid.NZVertices;
// Vector for storing multi grid array
std::vector<Vector> tvArrayV(nLoop); // potential <--> error
std::vector<Vector> tvChargeFMG(nLoop); // charge is restricted in full multiGrid
std::vector<Vector> tvCharge(nLoop); // charge <--> residue
std::vector<Vector> tvResidue(nLoop); // residue calculation
// Allocate memory for temporary grid
for (int count = 1; count <= nLoop; ++count) {
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
// if one just address to matrixV
const int index = count - 1;
tvResidue[index].resize(tnRRow, tnZColumn, 1);
tvChargeFMG[index].resize(tnRRow, tnZColumn, 1);
tvArrayV[index].resize(tnRRow, tnZColumn, 1);
tvCharge[index].resize(tnRRow, tnZColumn, 1);
if (count == 1) {
for (int iphi = iPhi; iphi <= iPhi; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
tvChargeFMG[index](ir, iz, iphi) = matricesCharge(iz, ir, iphi);
tvCharge[index](ir, iz, iphi) = matricesCharge(iz, ir, iphi);
tvArrayV[index](ir, iz, iphi) = matricesV(iz, ir, iphi);
}
}
}
} else {
restrict2D(tvChargeFMG[index], tvChargeFMG[count - 2], tnRRow, tnZColumn, 0);
}
iOne *= 2;
jOne *= 2;
}
/// full multi grid
if (MGParameters::cycleType == CycleType::FCycle) {
LOGP(detail, "PoissonMultiGrid2D: Do full cycle");
// FMG
// 1) Relax on the coarsest grid
iOne /= 2;
jOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
const DataT h = gridSpacingR * nLoop;
const DataT h2 = h * h;
const DataT tempRatio = ratioZ * iOne * iOne / (jOne * jOne);
const DataT tempFourth = 1 / (2 + 2 * tempRatio);
std::vector<DataT> coefficient1(tnRRow);
std::vector<DataT> coefficient2(tnRRow);
calcCoefficients2D(1, tnRRow - 1, h, coefficient1, coefficient2);
relax2D(tvArrayV[nLoop - 1], tvChargeFMG[nLoop - 1], tnRRow, tnZColumn, h2, tempFourth, tempRatio, coefficient1, coefficient2);
// Do VCycle from nLoop H to h
for (int count = nLoop - 2; count >= 0; --count) {
iOne /= 2;
jOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
interp2D(tvArrayV[count], tvArrayV[count + 1], tnRRow, tnZColumn, iPhi);
// Copy the relax charge to the tvCharge
tvCharge[count] = tvChargeFMG[count]; // copy
// Do V cycle
for (int mgCycle = 0; mgCycle < MGParameters::nMGCycle; ++mgCycle) {
vCycle2D(count + 1, nLoop, MGParameters::nPre, MGParameters::nPost, gridSpacingR, ratioZ, tvArrayV, tvCharge, tvResidue);
}
}
} else if (MGParameters::cycleType == CycleType::VCycle) {
// 2. VCycle
LOGP(detail, "PoissonMultiGrid2D: Do V cycle");
int gridFrom = 1;
int gridTo = nLoop;
// Do MGCycle
for (int mgCycle = 0; mgCycle < MGParameters::nMGCycle; ++mgCycle) {
vCycle2D(gridFrom, gridTo, MGParameters::nPre, MGParameters::nPost, gridSpacingR, ratioZ, tvArrayV, tvCharge, tvResidue);
}
} else if (MGParameters::cycleType == CycleType::WCycle) {
// 3. W Cycle (TODO:)
int gridFrom = 1;
int gridTo = nLoop;
// Do MGCycle
for (Int_t mgCycle = 0; mgCycle < MGParameters::nMGCycle; mgCycle++) {
wCycle2D(gridFrom, gridTo, MGParameters::gamma, MGParameters::nPre, MGParameters::nPost, gridSpacingR, ratioZ, tvArrayV, tvCharge, tvResidue);
}
}
// fill output
for (int iphi = iPhi; iphi <= iPhi; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
matricesV(iz, ir, iphi) = tvArrayV[0](ir, iz, iphi);
}
}
}
}
template <typename DataT>
void PoissonSolver<DataT>::poissonMultiGrid3D2D(DataContainer& matricesV, const DataContainer& matricesCharge, const int symmetry)
{
LOGP(detail, "{}", fmt::format("PoissonMultiGrid3D2D: in Poisson Solver 3D multiGrid semi coarsening mParamGrid.NRVertices={}, cols={}, mParamGrid.NPhiVertices={}", mParamGrid.NZVertices, mParamGrid.NRVertices, mParamGrid.NPhiVertices));
// Check that the number of mParamGrid.NRVertices and mParamGrid.NZVertices is suitable for a binary expansion
if (!isPowerOfTwo((mParamGrid.NRVertices - 1))) {
LOGP(error, "PoissonMultiGrid3D2D: Poisson3DMultiGrid - Error in the number of mParamGrid.NRVertices. Must be 2**M + 1");
return;
}
if (!isPowerOfTwo((mParamGrid.NZVertices - 1))) {
LOGP(error, "PoissonMultiGrid3D2D: Poisson3DMultiGrid - Error in the number of mParamGrid.NZVertices. Must be 2**N + 1");
return;
}
if (mParamGrid.NPhiVertices <= 3) {
LOGP(error, "PoissonMultiGrid3D2D: Poisson3DMultiGrid - Error in the number of mParamGrid.NPhiVertices. Must be larger than 3");
return;
}
const DataT gridSpacingR = getSpacingR();
const DataT gridSpacingZ = getSpacingZ();
const DataT gridSpacingPhi = getSpacingPhi();
const DataT ratioPhi = gridSpacingR * gridSpacingR / (gridSpacingPhi * gridSpacingPhi); // ratio_{phi} = gridSize_{r} / gridSize_{phi}
const DataT ratioZ = gridSpacingR * gridSpacingR / (gridSpacingZ * gridSpacingZ); // ratio_{Z} = gridSize_{r} / gridSize_{z}
// Solve Poisson's equation in cylindrical coordinates by multiGrid technique
// Allow for different size grid spacing in R and Z directions
int nGridRow = 0; // number grid
int nGridCol = 0; // number grid
int nnRow = mParamGrid.NRVertices;
int nnCol = mParamGrid.NZVertices;
while (nnRow >>= 1) {
++nGridRow;
}
while (nnCol >>= 1) {
++nGridCol;
}
const int maxVal = std::max(nGridRow, nGridCol); // Calculate the number of nLoop for the binary expansion
const size_t nLoop = (maxVal > MGParameters::maxLoop) ? MGParameters::maxLoop : maxVal;
unsigned int iOne = 1; // index i in gridSize r (original)
unsigned int jOne = 1; // index j in gridSize z (original)
std::vector<Vector> tvArrayV(nLoop); // potential <--> error
std::vector<Vector> tvChargeFMG(nLoop); // charge is restricted in full multiGrid
std::vector<Vector> tvCharge(nLoop); // charge <--> residue
std::vector<Vector> tvPrevArrayV(nLoop); // error calculation
std::vector<Vector> tvResidue(nLoop); // residue calculation
for (unsigned int count = 1; count <= nLoop; count++) {
const unsigned int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
const unsigned int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
const unsigned int index = count - 1;
tvResidue[index].resize(tnRRow, tnZColumn, mParamGrid.NPhiVertices);
tvPrevArrayV[index].resize(tnRRow, tnZColumn, mParamGrid.NPhiVertices);
// memory for the finest grid is from parameters
tvChargeFMG[index].resize(tnRRow, tnZColumn, mParamGrid.NPhiVertices);
tvArrayV[index].resize(tnRRow, tnZColumn, mParamGrid.NPhiVertices);
tvCharge[index].resize(tnRRow, tnZColumn, mParamGrid.NPhiVertices);
if (count == 1) {
for (int iphi = 0; iphi < mParamGrid.NPhiVertices; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
tvChargeFMG[index](ir, iz, iphi) = matricesCharge(iz, ir, iphi);
tvArrayV[index](ir, iz, iphi) = matricesV(iz, ir, iphi);
}
}
}
} else {
restrict3D(tvChargeFMG[index], tvChargeFMG[count - 2], tnRRow, tnZColumn, mParamGrid.NPhiVertices, mParamGrid.NPhiVertices);
restrictBoundary3D(tvArrayV[index], tvArrayV[count - 2], tnRRow, tnZColumn, mParamGrid.NPhiVertices, mParamGrid.NPhiVertices);
}
iOne *= 2; // doubling
jOne *= 2; // doubling
}
std::vector<DataT> coefficient1(mParamGrid.NRVertices); // coefficient1(mParamGrid.NRVertices) for storing (1 + h_{r}/2r_{i}) from central differences in r direction
std::vector<DataT> coefficient2(mParamGrid.NRVertices); // coefficient2(mParamGrid.NRVertices) for storing (1 + h_{r}/2r_{i}) from central differences in r direction
std::vector<DataT> coefficient3(mParamGrid.NRVertices); // coefficient3(mParamGrid.NRVertices) for storing (1/r_{i}^2) from central differences in phi direction
std::vector<DataT> coefficient4(mParamGrid.NRVertices); // coefficient4(mParamGrid.NRVertices) for storing 1/2
std::vector<DataT> inverseCoefficient4(mParamGrid.NRVertices); // inverse of coefficient4(mParamGrid.NRVertices)
// Case full multi grid (FMG)
if (MGParameters::cycleType == CycleType::FCycle) {
// 1) Relax on the coarsest grid
iOne /= 2;
jOne /= 2;
int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
const DataT h = getSpacingR() * iOne;
const DataT h2 = h * h;
const DataT iOne2 = iOne * iOne;
const DataT tempRatioPhi = ratioPhi * iOne2; // Used tobe divided by ( m_one * m_one ) when m_one was != 1
const DataT tempRatioZ = ratioZ * iOne2 / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
// relax on the coarsest level
relax3D(tvArrayV[nLoop - 1], tvChargeFMG[nLoop - 1], tnRRow, tnZColumn, mParamGrid.NPhiVertices, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
// 2) Do multiGrid v-cycle from coarsest to finest
for (int count = nLoop - 2; count >= 0; --count) {
// move to finer grid
iOne /= 2;
jOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
// 2) a) Interpolate potential for h -> 2h (coarse -> fine)
interp3D(tvArrayV[count], tvArrayV[count + 1], tnRRow, tnZColumn, mParamGrid.NPhiVertices, mParamGrid.NPhiVertices);
// 2) c) Copy the restricted charge to charge for calculation
tvCharge[count] = tvChargeFMG[count]; // copy
// 2) c) Do V cycle MGParameters::nMGCycle times at most
for (int mgCycle = 0; mgCycle < MGParameters::nMGCycle; ++mgCycle) {
// Copy the potential to temp array for convergence calculation
tvPrevArrayV[count] = tvArrayV[count];
// 2) c) i) Call V cycle from grid count+1 (current fine level) to nLoop (coarsest)
vCycle3D2D(symmetry, count + 1, nLoop, MGParameters::nPre, MGParameters::nPost, ratioZ, ratioPhi, tvArrayV, tvCharge, tvResidue, coefficient1, coefficient2, coefficient3, coefficient4, inverseCoefficient4);
const DataT convergenceError = getConvergenceError(tvArrayV[count], tvPrevArrayV[count]);
/// if already converge just break move to finer grid
if (convergenceError <= sConvergenceError) {
break;
}
}
}
}
// fill output
for (int iphi = 0; iphi < mParamGrid.NPhiVertices; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
matricesV(iz, ir, iphi) = tvArrayV[0](ir, iz, iphi);
}
}
}
}
template <typename DataT>
void PoissonSolver<DataT>::poissonMultiGrid3D(DataContainer& matricesV, const DataContainer& matricesCharge, const int symmetry)
{
const DataT gridSpacingR = getSpacingR();
const DataT gridSpacingZ = getSpacingZ();
const DataT ratioZ = gridSpacingR * gridSpacingR / (gridSpacingZ * gridSpacingZ); // ratio_{Z} = gridSize_{r} / gridSize_{z}
LOGP(detail, "{}", fmt::format("PoissonMultiGrid3D: in Poisson Solver 3D multi grid full coarsening mParamGrid.NRVertices={}, cols={}, mParamGrid.NPhiVertices={}", mParamGrid.NRVertices, mParamGrid.NZVertices, mParamGrid.NPhiVertices));
// Check that the number of mParamGrid.NRVertices and mParamGrid.NZVertices is suitable for a binary expansion
if (!isPowerOfTwo((mParamGrid.NRVertices - 1))) {
LOGP(error, "PoissonMultiGrid3D: Poisson3DMultiGrid - Error in the number of mParamGrid.NRVertices. Must be 2**M + 1");
return;
}
if (!isPowerOfTwo((mParamGrid.NZVertices - 1))) {
LOGP(error, "PoissonMultiGrid3D: Poisson3DMultiGrid - Error in the number of mParamGrid.NZVertices. Must be 2**N + 1");
return;
}
if (mParamGrid.NPhiVertices <= 3) {
LOGP(error, "PoissonMultiGrid3D: Poisson3DMultiGrid - Error in the number of mParamGrid.NPhiVertices. Must be larger than 3");
return;
}
// Solve Poisson's equation in cylindrical coordinates by multi grid technique
// Allow for different size grid spacing in R and Z directions
int nGridRow = 0; // number grid
int nGridCol = 0; // number grid
int nGridPhi = 0;
int nnRow = mParamGrid.NRVertices;
while (nnRow >>= 1) {
++nGridRow;
}
int nnCol = mParamGrid.NZVertices;
while (nnCol >>= 1) {
++nGridCol;
}
int nnPhi = mParamGrid.NPhiVertices;
while (nnPhi % 2 == 0) {
++nGridPhi;
nnPhi /= 2;
}
LOGP(detail, "{}", fmt::format("PoissonMultiGrid3D: nGridRow={}, nGridCol={}, nGridPhi={}", nGridRow, nGridCol, nGridPhi));
const int nLoop = std::max({nGridRow, nGridCol, nGridPhi}); // Calculate the number of nLoop for the binary expansion
// Vector for storing multi grid array
unsigned int iOne = 1; // index i in gridSize r (original)
unsigned int jOne = 1; // index j in gridSize z (original)
unsigned int kOne = 1; // index k in gridSize phi
int tnRRow = mParamGrid.NRVertices;
int tnZColumn = mParamGrid.NZVertices;
int tPhiSlice = mParamGrid.NPhiVertices;
// 1) Memory allocation for multi grid
std::vector<Vector> tvArrayV(nLoop); // potential <--> error
std::vector<Vector> tvChargeFMG(nLoop); // charge is restricted in full multiGrid
std::vector<Vector> tvCharge(nLoop); // charge <--> residue
std::vector<Vector> tvPrevArrayV(nLoop); // error calculation
std::vector<Vector> tvResidue(nLoop); // residue calculation
std::vector<DataT> coefficient1(mParamGrid.NRVertices); // coefficient1(mParamGrid.NRVertices) for storing (1 + h_{r}/2r_{i}) from central differences in r direction
std::vector<DataT> coefficient2(mParamGrid.NRVertices); // coefficient2(mParamGrid.NRVertices) for storing (1 + h_{r}/2r_{i}) from central differences in r direction
std::vector<DataT> coefficient3(mParamGrid.NRVertices); // coefficient3(mParamGrid.NRVertices) for storing (1/r_{i}^2) from central differences in phi direction
std::vector<DataT> coefficient4(mParamGrid.NRVertices); // coefficient4(mParamGrid.NRVertices) for storing 1/2
std::vector<DataT> inverseCoefficient4(mParamGrid.NRVertices); // inverse of coefficient4(mParamGrid.NRVertices)
for (int count = 1; count <= nLoop; ++count) {
// tnRRow,tnZColumn in new grid
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
// allocate memory for residue
const int index = count - 1;
tvResidue[index].resize(tnRRow, tnZColumn, tPhiSlice);
tvPrevArrayV[index].resize(tnRRow, tnZColumn, tPhiSlice);
tvChargeFMG[index].resize(tnRRow, tnZColumn, tPhiSlice);
tvArrayV[index].resize(tnRRow, tnZColumn, tPhiSlice);
tvCharge[index].resize(tnRRow, tnZColumn, tPhiSlice);
// memory for the finest grid is from parameters
if (count == 1) {
for (int iphi = 0; iphi < mParamGrid.NPhiVertices; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
tvChargeFMG[index](ir, iz, iphi) = matricesCharge(iz, ir, iphi);
tvArrayV[index](ir, iz, iphi) = matricesV(iz, ir, iphi);
}
}
}
tvCharge[index] = tvChargeFMG[index];
}
iOne *= 2; // doubling
jOne *= 2; // doubling
kOne *= 2;
}
// Case full multi grid (FMG)
if (MGParameters::cycleType == CycleType::FCycle) {
// Restrict the charge to coarser grid
iOne = 2;
jOne = 2;
kOne = 2;
int otPhiSlice = mParamGrid.NPhiVertices;
// 1) Restrict Charge and Boundary to coarser grid
for (int count = 2; count <= nLoop; ++count) {
// tnRRow,tnZColumn in new grid
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
LOGP(detail, "{}", fmt::format("PoissonMultiGrid3D: Restrict3D, tnRRow={}, tnZColumn={}, newPhiSlice={}, oldPhiSlice={}", tnRRow, tnZColumn, tPhiSlice, otPhiSlice));
restrict3D(tvChargeFMG[count - 1], tvChargeFMG[count - 2], tnRRow, tnZColumn, tPhiSlice, otPhiSlice);
// copy boundary values of V
restrictBoundary3D(tvArrayV[count - 1], tvArrayV[count - 2], tnRRow, tnZColumn, tPhiSlice, otPhiSlice);
otPhiSlice = tPhiSlice;
iOne *= 2; // doubling
jOne *= 2; // doubling
kOne *= 2;
}
// Relax on the coarsest grid
// FMG
// 2) Relax on the coarsest grid
// move to the coarsest + 1
iOne /= 2;
jOne /= 2;
kOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
otPhiSlice = tPhiSlice;
const DataT h = gridSpacingR * iOne;
const DataT h2 = h * h;
const DataT gridSizePhiInv = tPhiSlice * getGridSizePhiInv(); // h_{phi}
const DataT tempRatioPhi = h2 * gridSizePhiInv * gridSizePhiInv; // ratio_{phi} = gridSize_{r} / gridSize_{phi}
const DataT tempRatioZ = ratioZ * iOne * iOne / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
// 3) Relax on the coarsest grid
relax3D(tvArrayV[nLoop - 1], tvChargeFMG[nLoop - 1], tnRRow, tnZColumn, tPhiSlice, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
// 4) V Cycle from coarsest to finest
for (int count = nLoop - 2; count >= 0; --count) {
// move to finer grid
std::fill(std::begin(coefficient1), std::end(coefficient1), 0);
std::fill(std::begin(coefficient2), std::end(coefficient2), 0);
std::fill(std::begin(coefficient3), std::end(coefficient3), 0);
std::fill(std::begin(coefficient4), std::end(coefficient4), 0);
std::fill(std::begin(inverseCoefficient4), std::end(inverseCoefficient4), 0);
iOne /= 2;
jOne /= 2;
kOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
// 4) a) interpolate from 2h --> h grid
interp3D(tvArrayV[count], tvArrayV[count + 1], tnRRow, tnZColumn, tPhiSlice, otPhiSlice);
// Copy the relax charge to the tvCharge
if (count > 0) {
tvCharge[count] = tvChargeFMG[count];
}
using timer = std::chrono::high_resolution_clock;
auto start = timer::now();
for (int mgCycle = 0; mgCycle < MGParameters::nMGCycle; ++mgCycle) {
// copy to store previous potential
tvPrevArrayV[count] = tvArrayV[count];
vCycle3D(symmetry, count + 1, nLoop, MGParameters::nPre, MGParameters::nPost, ratioZ, tvArrayV, tvCharge, tvResidue, coefficient1, coefficient2, coefficient3, coefficient4, inverseCoefficient4);
// converge error
const DataT convergenceError = getConvergenceError(tvArrayV[count], tvPrevArrayV[count]);
// if already converge just break move to finer grid
if (convergenceError <= sConvergenceError) {
LOGP(detail, "Cycle converged. Continue to next cycle...");
break;
}
if (count <= 1 && !(mgCycle % 10)) {
auto stop = timer::now();
std::chrono::duration<float> time = stop - start;
const float totalTime = time.count();
const float timePerCycle = totalTime / (mgCycle + 1);
const float remaining = timePerCycle * (MGParameters::nMGCycle - mgCycle);
LOGP(detail, "Cycle {} out of {} for current V cycle {}. Processed time {}s with {}s per cycle. Max remaining time for current cycle {}s. Convergence {} > {}", mgCycle, MGParameters::nMGCycle, count, time.count(), timePerCycle, remaining, convergenceError, sConvergenceError);
}
if (mgCycle == (MGParameters::nMGCycle - 1)) {
LOGP(warning, "Cycle {} did not convergence! Current convergence error is larger than expected convergence error: {} > {}", mgCycle, convergenceError, sConvergenceError);
}
}
// keep old slice information
otPhiSlice = tPhiSlice;
}
} else if (MGParameters::cycleType == CycleType::VCycle) {
// V-cycle
int gridFrom = 1;
int gridTo = nLoop;
for (int mgCycle = 0; mgCycle < MGParameters::nMGCycle; ++mgCycle) {
// copy to store previous potential
tvPrevArrayV[0] = tvArrayV[0];
// Do V Cycle from the coarsest to finest grid
vCycle3D(symmetry, gridFrom, gridTo, MGParameters::nPre, MGParameters::nPost, ratioZ, tvArrayV, tvCharge, tvResidue, coefficient1, coefficient2, coefficient3, coefficient4, inverseCoefficient4);
// convergence error
const DataT convergenceError = getConvergenceError(tvArrayV[0], tvPrevArrayV[0]);
// if error already achieved then stop mg iteration
if (convergenceError <= sConvergenceError) {
break;
}
}
}
// fill output
for (int iphi = 0; iphi < mParamGrid.NPhiVertices; ++iphi) {
for (int ir = 0; ir < mParamGrid.NRVertices; ++ir) {
for (int iz = 0; iz < mParamGrid.NZVertices; ++iz) {
matricesV(iz, ir, iphi) = tvArrayV[0](ir, iz, iphi);
}
}
}
}
template <typename DataT>
void PoissonSolver<DataT>::wCycle2D(const int gridFrom, const int gridTo, const int gamma, const int nPre, const int nPost, const DataT gridSizeR, const DataT ratio,
std::vector<Vector>& tvArrayV, std::vector<Vector>& tvCharge, std::vector<Vector>& tvResidue)
{
unsigned int iOne = 1 << (gridFrom - 1);
unsigned int jOne = 1 << (gridFrom - 1);
int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
std::vector<DataT> coefficient1(mParamGrid.NRVertices);
std::vector<DataT> coefficient2(mParamGrid.NZVertices);
// 1) Go to coarsest level
for (int count = gridFrom; count <= gridTo - 2; ++count) {
const int index = count - 1;
const DataT h = gridSizeR * iOne;
const DataT h2 = h * h;
const DataT ih2 = 1 / h2;
const DataT tempRatio = ratio * iOne * iOne / (jOne * jOne);
const DataT tempFourth = 1 / (2 + 2 * tempRatio);
const DataT inverseTempFourth = 1 / tempFourth;
calcCoefficients2D(1, tnRRow - 1, h, coefficient1, coefficient2);
Vector matricesCurrentV = tvArrayV[index];
Vector matricesCurrentCharge = tvCharge[index];
Vector residue = tvResidue[index];
// 1) Pre-Smoothing: Gauss-Seidel Relaxation or Jacobi
for (int jPre = 1; jPre <= nPre; ++jPre) {
relax2D(matricesCurrentV, matricesCurrentCharge, tnRRow, tnZColumn, h2, tempFourth, tempRatio, coefficient1, coefficient2);
}
// 2) Residue calculation
residue2D(residue, matricesCurrentV, matricesCurrentCharge, tnRRow, tnZColumn, ih2, inverseTempFourth, tempRatio, coefficient1, coefficient2);
iOne *= 2;
jOne *= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
matricesCurrentCharge = tvCharge[count];
matricesCurrentV = tvArrayV[count];
// 3) Restriction
restrict2D(matricesCurrentCharge, residue, tnRRow, tnZColumn, 0);
}
// Do V cycle from: gridTo-1 to gridTo gamma times
for (int iGamma = 0; iGamma < gamma; ++iGamma) {
vCycle2D(gridTo - 1, gridTo, nPre, nPost, gridSizeR, ratio, tvArrayV, tvCharge, tvResidue);
}
// Go to finest grid
for (int count = gridTo - 2; count >= gridFrom; --count) {
iOne /= 2;
jOne /= 2;
const DataT h = gridSizeR * iOne;
const DataT h2 = h * h;
const DataT tempRatio = ratio * iOne * iOne / (jOne * jOne);
const DataT tempFourth = 1 / (2 + 2 * tempRatio);
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
const Vector matricesCurrentCharge = tvCharge[count - 1];
Vector matricesCurrentV = tvArrayV[count - 1];
const Vector matricesCurrentVC = tvArrayV[count];
// 6) Interpolation/Prolongation
addInterp2D(matricesCurrentV, matricesCurrentVC, tnRRow, tnZColumn, 0);
calcCoefficients2D(1, tnRRow - 1, h, coefficient1, coefficient2);
// 7) Post-Smoothing: Gauss-Seidel Relaxation
for (Int_t jPost = 1; jPost <= nPost; ++jPost) {
relax2D(matricesCurrentV, matricesCurrentCharge, tnRRow, tnZColumn, h2, tempFourth, tempRatio, coefficient1, coefficient2);
} // end post smoothing
}
}
template <typename DataT>
void PoissonSolver<DataT>::vCycle2D(const int gridFrom, const int gridTo, const int nPre, const int nPost, const DataT gridSizeR, const DataT ratio, std::vector<Vector>& tvArrayV,
std::vector<Vector>& tvCharge, std::vector<Vector>& tvResidue)
{
unsigned int iOne = 1 << (gridFrom - 1);
unsigned int jOne = 1 << (gridFrom - 1);
int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
std::vector<DataT> coefficient1(mParamGrid.NRVertices);
std::vector<DataT> coefficient2(mParamGrid.NZVertices);
// 1) Go to coarsest level
for (int count = gridFrom; count <= gridTo - 1; ++count) {
const int index = count - 1;
const DataT h = gridSizeR * iOne;
const DataT h2 = h * h;
const DataT ih2 = 1 / h2;
const DataT tempRatio = ratio * iOne * iOne / (jOne * jOne);
const DataT tempFourth = 1 / (2 + 2 * tempRatio);
const DataT inverseTempFourth = 1 / tempFourth;
calcCoefficients2D(1, tnRRow - 1, h, coefficient1, coefficient2);
for (int jPre = 1; jPre <= nPre; ++jPre) {
relax2D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, h2, tempFourth, tempRatio, coefficient1, coefficient2);
}
// 2) Residue calculation
residue2D(tvResidue[index], tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, ih2, inverseTempFourth, tempRatio, coefficient1, coefficient2);
iOne *= 2;
jOne *= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
// 3) Restriction
restrict2D(tvCharge[count], tvResidue[index], tnRRow, tnZColumn, 0);
// 4) Zeroing coarser V
std::fill(tvArrayV[count].begin(), tvArrayV[count].end(), 0); // is this necessary???
}
// 5) coarsest grid
const DataT h = gridSizeR * iOne;
const DataT h2 = h * h;
const DataT tempRatio = ratio * iOne * iOne / (jOne * jOne);
const DataT tempFourth = 1 / (2 + 2 * tempRatio);
calcCoefficients2D(1, tnRRow - 1, h, coefficient1, coefficient2);
relax2D(tvArrayV[gridTo - 1], tvCharge[gridTo - 1], tnRRow, tnZColumn, h2, tempFourth, tempRatio, coefficient1, coefficient2);
// Go to finest grid
for (int count = gridTo - 1; count >= gridFrom; count--) {
const int index = count - 1;
iOne /= 2;
jOne /= 2;
const DataT hTmp = gridSizeR * iOne;
const DataT h2Tmp = hTmp * hTmp;
const DataT tempRatioTmp = ratio * iOne * iOne / (jOne * jOne);
const DataT tempFourthTmp = 1 / (2 + 2 * tempRatioTmp);
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
// 6) Interpolation/Prolongation
addInterp2D(tvArrayV[index], tvArrayV[count], tnRRow, tnZColumn, 0);
calcCoefficients2D(1, tnRRow - 1, hTmp, coefficient1, coefficient2);
// 7) Post-Smoothing: Gauss-Seidel Relaxation
for (int jPost = 1; jPost <= nPost; ++jPost) {
relax2D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, h2Tmp, tempFourthTmp, tempRatioTmp, coefficient1, coefficient2);
} // end post smoothing
}
}
template <typename DataT>
void PoissonSolver<DataT>::vCycle3D2D(const int symmetry, const int gridFrom, const int gridTo, const int nPre, const int nPost, const DataT ratioZ, const DataT ratioPhi,
std::vector<Vector>& tvArrayV, std::vector<Vector>& tvCharge, std::vector<Vector>& tvResidue, std::vector<DataT>& coefficient1,
std::vector<DataT>& coefficient2, std::vector<DataT>& coefficient3, std::vector<DataT>& coefficient4, std::vector<DataT>& inverseCoefficient4) const
{
unsigned int iOne = 1 << (gridFrom - 1);
unsigned int jOne = 1 << (gridFrom - 1);
int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
for (int count = gridFrom; count <= gridTo - 1; ++count) {
const int index = count - 1;
const DataT h = getSpacingR() * iOne;
const DataT h2 = h * h;
const DataT ih2 = 1 / h2;
const int iOne2 = iOne * iOne;
const DataT tempRatioPhi = ratioPhi * iOne2; // Used tobe divided by ( m_one * m_one ) when m_one was != 1
const DataT tempRatioZ = ratioZ * iOne2 / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
for (unsigned int i = 1; i < tnRRow - 1; ++i) {
inverseCoefficient4[i] = 1 / coefficient4[i];
}
// Info("VCycle3D2D","Before Pre-smoothing");
// 1) Pre-Smoothing: Gauss-Seidel Relaxation or Jacobi
for (int jPre = 1; jPre <= nPre; ++jPre) {
relax3D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, mParamGrid.NPhiVertices, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
} // end pre smoothing
// 2) Residue calculation
residue3D(tvResidue[index], tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, mParamGrid.NPhiVertices, symmetry, ih2, tempRatioZ, coefficient1, coefficient2, coefficient3, inverseCoefficient4);
iOne *= 2;
jOne *= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
// 3) Restriction
restrict3D(tvCharge[count], tvResidue[index], tnRRow, tnZColumn, mParamGrid.NPhiVertices, mParamGrid.NPhiVertices);
// 4) Zeroing coarser V
std::fill(tvArrayV[count].begin(), tvArrayV[count].end(), 0);
}
// coarsest grid
const DataT hTmp = getSpacingR() * iOne;
const DataT h2Tmp = hTmp * hTmp;
const int iOne2Tmp = iOne * iOne;
const DataT tempRatioPhiTmp = ratioPhi * iOne2Tmp;
const DataT tempRatioZTmp = ratioZ * iOne2Tmp / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, hTmp, tempRatioZTmp, tempRatioPhiTmp, coefficient1, coefficient2, coefficient3, coefficient4);
// 3) Relax on the coarsest grid
relax3D(tvArrayV[gridTo - 1], tvCharge[gridTo - 1], tnRRow, tnZColumn, mParamGrid.NPhiVertices, symmetry, h2Tmp, tempRatioZTmp, coefficient1, coefficient2, coefficient3, coefficient4);
// back to fine
for (int count = gridTo - 1; count >= gridFrom; --count) {
const int index = count - 1;
iOne /= 2;
jOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
const DataT h = getSpacingR() * iOne;
const DataT h2 = h * h;
const int iOne2 = iOne * iOne;
const DataT tempRatioPhi = ratioPhi * iOne2; // Used tobe divided by ( m_one * m_one ) when m_one was != 1
const DataT tempRatioZ = ratioZ * iOne2 / (jOne * jOne);
// 4) Interpolation/Prolongation
addInterp3D(tvArrayV[index], tvArrayV[count], tnRRow, tnZColumn, mParamGrid.NPhiVertices, mParamGrid.NPhiVertices);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
// 5) Post-Smoothing: Gauss-Seidel Relaxation
for (int jPost = 1; jPost <= nPost; ++jPost) {
relax3D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, mParamGrid.NPhiVertices, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
} // end post smoothing
}
}
template <typename DataT>
void PoissonSolver<DataT>::vCycle3D(const int symmetry, const int gridFrom, const int gridTo, const int nPre, const int nPost, const DataT ratioZ, std::vector<Vector>& tvArrayV,
std::vector<Vector>& tvCharge, std::vector<Vector>& tvResidue, std::vector<DataT>& coefficient1, std::vector<DataT>& coefficient2, std::vector<DataT>& coefficient3,
std::vector<DataT>& coefficient4, std::vector<DataT>& inverseCoefficient4) const
{
const DataT gridSpacingR = getSpacingR();
unsigned int iOne = 1 << (gridFrom - 1);
unsigned int jOne = 1 << (gridFrom - 1);
unsigned int kOne = 1 << (gridFrom - 1);
int nnPhi = mParamGrid.NPhiVertices;
while (nnPhi % 2 == 0) {
nnPhi /= 2;
}
int tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
int tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
int tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
for (int count = gridFrom; count <= gridTo - 1; ++count) {
const int index = count - 1;
const int otPhiSlice = tPhiSlice;
const DataT h = gridSpacingR * iOne;
const DataT h2 = h * h;
const DataT ih2 = 1 / h2;
const DataT tempGridSizePhiInv = tPhiSlice * getGridSizePhiInv(); // phi now is multiGrid
const DataT tempRatioPhi = h2 * tempGridSizePhiInv * tempGridSizePhiInv; // ratio_{phi} = gridSize_{r} / gridSize_{phi}
const DataT tempRatioZ = ratioZ * iOne * iOne / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
for (int i = 1; i < tnRRow - 1; ++i) {
inverseCoefficient4[i] = 1 / coefficient4[i];
}
// 1) Pre-Smoothing: Gauss-Seidel Relaxation or Jacobi
for (int jPre = 1; jPre <= nPre; ++jPre) {
relax3D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, tPhiSlice, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
} // end pre smoothing
// 2) Residue calculation
residue3D(tvResidue[index], tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, tPhiSlice, symmetry, ih2, tempRatioZ, coefficient1, coefficient2, coefficient3, inverseCoefficient4);
iOne *= 2;
jOne *= 2;
kOne *= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
// 3) Restriction
restrict3D(tvCharge[count], tvResidue[index], tnRRow, tnZColumn, tPhiSlice, otPhiSlice);
// 4) Zeroing coarser V
std::fill(tvArrayV[count].begin(), tvArrayV[count].end(), 0);
}
// coarsest grid
const DataT hTmp = gridSpacingR * iOne;
const DataT h2Tmp = hTmp * hTmp;
const DataT tempGridSizePhiInvTmp = tPhiSlice * getGridSizePhiInv(); // phi now is multiGrid
const DataT tempRatioPhiTmp = h2Tmp * tempGridSizePhiInvTmp * tempGridSizePhiInvTmp; // ratio_{phi} = gridSize_{r} / gridSize_{phi}
const DataT tempRatioZTmp = ratioZ * iOne * iOne / (jOne * jOne);
calcCoefficients(1, tnRRow - 1, hTmp, tempRatioZTmp, tempRatioPhiTmp, coefficient1, coefficient2, coefficient3, coefficient4);
// 3) Relax on the coarsest grid
relax3D(tvArrayV[gridTo - 1], tvCharge[gridTo - 1], tnRRow, tnZColumn, tPhiSlice, symmetry, h2Tmp, tempRatioZTmp, coefficient1, coefficient2, coefficient3, coefficient4);
// back to fine
for (int count = gridTo - 1; count >= gridFrom; --count) {
const int index = count - 1;
const int otPhiSlice = tPhiSlice;
iOne /= 2;
jOne /= 2;
kOne /= 2;
tnRRow = iOne == 1 ? mParamGrid.NRVertices : mParamGrid.NRVertices / iOne + 1;
tnZColumn = jOne == 1 ? mParamGrid.NZVertices : mParamGrid.NZVertices / jOne + 1;
tPhiSlice = kOne == 1 ? mParamGrid.NPhiVertices : mParamGrid.NPhiVertices / kOne;
tPhiSlice = tPhiSlice < nnPhi ? nnPhi : tPhiSlice;
const DataT h = gridSpacingR * iOne;
const DataT h2 = h * h;
const DataT tempGridSizePhiInv = tPhiSlice * getGridSizePhiInv();
const DataT tempRatioPhi = h2 * tempGridSizePhiInv * tempGridSizePhiInv; // ratio_{phi} = gridSize_{r} / gridSize_{phi}
const DataT tempRatioZ = ratioZ * iOne * iOne / (jOne * jOne);
// 4) Interpolation/Prolongation
addInterp3D(tvArrayV[index], tvArrayV[count], tnRRow, tnZColumn, tPhiSlice, otPhiSlice);
calcCoefficients(1, tnRRow - 1, h, tempRatioZ, tempRatioPhi, coefficient1, coefficient2, coefficient3, coefficient4);
// 5) Post-Smoothing: Gauss-Seidel Relaxation
for (int jPost = 1; jPost <= nPost; ++jPost) {
relax3D(tvArrayV[index], tvCharge[index], tnRRow, tnZColumn, tPhiSlice, symmetry, h2, tempRatioZ, coefficient1, coefficient2, coefficient3, coefficient4);
}
}
}
template <typename DataT>
void PoissonSolver<DataT>::residue2D(Vector& residue, const Vector& matricesCurrentV, const Vector& matricesCurrentCharge, const int tnRRow, const int tnZColumn, const DataT ih2, const DataT inverseTempFourth,
const DataT tempRatio, std::vector<DataT>& coefficient1, std::vector<DataT>& coefficient2)
{
const int iPhi = 0;
#pragma omp parallel for num_threads(sNThreads)
for (int i = 1; i < tnRRow - 1; ++i) {
for (int j = 1; j < tnZColumn - 1; ++j) {
residue(i, j, iPhi) = ih2 * (coefficient1[i] * matricesCurrentV(i + 1, j, iPhi) + coefficient2[i] * matricesCurrentV(i - 1, j, iPhi) + tempRatio * (matricesCurrentV(i, j + 1, iPhi) + matricesCurrentV(i, j - 1, iPhi)) - inverseTempFourth * matricesCurrentV(i, j, iPhi)) + matricesCurrentCharge(i, j, iPhi);
} // end cols
} // end nRRow
// Boundary points.
for (int i = 0; i < tnRRow; ++i) {
residue(i, 0, iPhi) = residue(i, tnZColumn - 1, iPhi) = 0.0;
}
for (int j = 0; j < tnZColumn; ++j) {
residue(0, j, iPhi) = residue(tnRRow - 1, j, iPhi) = 0.0;
}
}
template <typename DataT>
void PoissonSolver<DataT>::residue3D(Vector& residue, const Vector& matricesCurrentV, const Vector& matricesCurrentCharge, const int tnRRow, const int tnZColumn, const int tnPhi, const int symmetry,
const DataT ih2, const DataT tempRatioZ, const std::vector<DataT>& coefficient1, const std::vector<DataT>& coefficient2, const std::vector<DataT>& coefficient3, const std::vector<DataT>& inverseCoefficient4) const
{
#pragma omp parallel for num_threads(sNThreads) // parallising this loop is possible - but using more than 2 cores makes it slower -
for (int m = 0; m < tnPhi; ++m) {
int mp1 = m + 1;
int signPlus = 1;
int mm1 = m - 1;
int signMinus = 1;
// Reflection symmetry in phi (e.g. symmetry at sector boundaries, or half sectors, etc.)
if (symmetry == 1) {
if (mp1 > tnPhi - 1) {
mp1 = tnPhi - 2;
}
if (mm1 < 0) {
mm1 = 1;
}
}
// Anti-symmetry in phi
else if (symmetry == -1) {
if (mp1 > tnPhi - 1) {
mp1 = tnPhi - 2;
signPlus = -1;
}
if (mm1 < 0) {
mm1 = 1;
signMinus = -1;
}
} else { // No Symmetries in phi, no boundaries, the calculation is continuous across all phi
if (mp1 > tnPhi - 1) {
mp1 = m + 1 - tnPhi;
}
if (mm1 < 0) {
mm1 = m - 1 + tnPhi;
}
}
for (int j = 1; j < tnZColumn - 1; ++j) {
for (int i = 1; i < tnRRow - 1; ++i) {
residue(i, j, m) = ih2 * (coefficient2[i] * matricesCurrentV(i - 1, j, m) + tempRatioZ * (matricesCurrentV(i, j - 1, m) + matricesCurrentV(i, j + 1, m)) + coefficient1[i] * matricesCurrentV(i + 1, j, m) +
coefficient3[i] * (signPlus * matricesCurrentV(i, j, mp1) + signMinus * matricesCurrentV(i, j, mm1)) - inverseCoefficient4[i] * matricesCurrentV(i, j, m)) +
matricesCurrentCharge(i, j, m);
} // end cols
} // end mParamGrid.NRVertices