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
| #if SINGLE_PRECISION
typedef float real;
#else
typedef double real;
#endif
#ifndef GRIDNAME
# define GRIDNAME "Multigrid"
#endif
#define GHOSTS 2
/* By default only one layer of ghost cells is used on the boundary to
optimise the cost of boundary conditions. */
#ifndef BGHOSTS
@ define BGHOSTS 1
#endif
#define _I (point.i - GHOSTS)
#define _J (point.j - GHOSTS)
#define _K (point.k - GHOSTS)
#define _DELTA (1./(1 << point.level))
typedef struct {
Grid g;
char * d;
size_t field_size;
} Multigrid;
struct _Point {
int i;
#if dimension > 1
int j;
#endif
#if dimension > 2
int k;
#endif
int level, n;
@ifdef foreach_block
int l;
@define _BLOCK_INDEX , point.l
@else
@define _BLOCK_INDEX
@endif
};
static Point last_point;
#define multigrid ((Multigrid *)grid)
// see /src/notes/index.tm
#if dimension == 1
# define _shift(l) ((1 << (l)) - 1 + 2*GHOSTS*(l))
#elif dimension == 2
# define _shift(l) (((1 << 2*(l)) - 1)/3 + 4*GHOSTS*((1 << (l)) - 1 + GHOSTS*(l)))
#elif dimension == 3
# define _shift(l) (((1 << 3*(l)) - 1)/7 + 2*GHOSTS*((1 << 2*(l)) - 1) + \
12*sq(GHOSTS)*((1 << (l)) - 1) + 8*cube(GHOSTS)*(l))
#endif
#define CELL(m,level,i) (*((Cell *) &m[level][(i)*datasize]))
/***** Cartesian macros *****/
#if dimension == 1
@undef val
@def val(a,k,l,m) (((real *)multigrid->d)[point.i + (k) +
_shift (point.level) +
_index(a,m)*multigrid->field_size])
@
#elif dimension == 2
@undef val
@def val(a,k,l,m) (((real *)multigrid->d)[point.j + (l) +
(point.i + (k))*((1 << point.level) + 2*GHOSTS) +
_shift (point.level) +
_index(a,m)*multigrid->field_size])
@
#elif dimension == 3
@undef val
@def val(a,l,m,o) (((real *)multigrid->d)[point.k + (o) +
((1 << point.level) + 2*GHOSTS)*
(point.j + (m) +
(point.i + (l))*((1 << point.level) + 2*GHOSTS)) +
_shift (point.level) +
_index(a,0)*multigrid->field_size])
@
#endif
/* low-level memory management */
#if dimension == 1
# if BGHOSTS == 1
@define allocated(...) true
# else // BGHOST != 1
@define allocated(k,l,m) (point.i+(k) >= 0 && point.i+(k) < (1 << point.level) + 2*GHOSTS)
# endif // BGHOST != 1
@def allocated_child(k,l,m) (level < depth() &&
point.i > 0 && point.i <= (1 << point.level) + 2)
@
#elif dimension == 2
# if BGHOSTS == 1
@define allocated(...) true
# else // BGHOST != 1
@def allocated(k,l,m) (point.i+(k) >= 0 && point.i+(k) < (1 << point.level) + 2*GHOSTS &&
point.j+(l) >= 0 && point.j+(l) < (1 << point.level) + 2*GHOSTS)
@
# endif // BGHOST != 1
@def allocated_child(k,l,m) (level < depth() &&
point.i > 0 && point.i <= (1 << point.level) + 2 &&
point.j > 0 && point.j <= (1 << point.level) + 2)
@
#else // dimension == 3
# if BGHOSTS == 1
@define allocated(...) true
#else // BGHOST != 1
@def allocated(a,l,m) (point.i+(a) >= 0 &&
point.i+(a) < (1 << point.level) + 2*GHOSTS &&
point.j+(l) >= 0 &&
point.j+(l) < (1 << point.level) + 2*GHOSTS &&
point.k+(m) >= 0 &&
point.k+(m) < (1 << point.level) + 2*GHOSTS)
@
#endif // BGHOST != 1
@def allocated_child(a,l,m) (level < depth() &&
point.i > 0 && point.i <= (1 << point.level) + 2 &&
point.j > 0 && point.j <= (1 << point.level) + 2 &&
point.k > 0 && point.k <= (1 << point.level) + 2)
@
#endif // dimension == 3
/***** Multigrid variables and macros *****/
@define depth() (grid->depth)
#if dimension == 1
@def fine(a,k,l,m)
(((real *)multigrid->d)[2*point.i - GHOSTS + (k) +
_shift (point.level + 1) +
_index(a,m)*multigrid->field_size])
@
@def coarse(a,k,l,m)
(((real *)multigrid->d)[(point.i + GHOSTS)/2 + (k) +
_shift (point.level - 1) +
_index(a,m)*multigrid->field_size])
@
@def POINT_VARIABLES
VARIABLES
int level = point.level; NOT_UNUSED(level);
struct { int x; } child = { 2*((point.i+GHOSTS)%2)-1 }; NOT_UNUSED(child);
Point parent = point; NOT_UNUSED(parent);
parent.level--;
parent.i = (point.i + GHOSTS)/2;
@
#elif dimension == 2
@def fine(a,k,l,m)
(((real *)multigrid->d)[2*point.j - GHOSTS + (l) +
(2*point.i - GHOSTS + (k))*((1 << point.level)*2 + 2*GHOSTS) +
_shift (point.level + 1) +
_index(a,m)*multigrid->field_size])
@
@def coarse(a,k,l,m)
(((real *)multigrid->d)[(point.j + GHOSTS)/2 + (l) +
((point.i + GHOSTS)/2 + (k))*((1 << point.level)/2 + 2*GHOSTS) +
_shift (point.level - 1) +
_index(a,m)*multigrid->field_size])
@
@def POINT_VARIABLES
VARIABLES
int level = point.level; NOT_UNUSED(level);
struct { int x, y; } child = {
2*((point.i+GHOSTS)%2)-1, 2*((point.j+GHOSTS)%2)-1
}; NOT_UNUSED(child);
Point parent = point; NOT_UNUSED(parent);
parent.level--;
parent.i = (point.i + GHOSTS)/2; parent.j = (point.j + GHOSTS)/2;
@
#elif dimension == 3
@def fine(a,l,m,o)
(((real *)multigrid->d)[2*point.k - GHOSTS + (o) +
((1 << point.level)*2 + 2*GHOSTS)*
(2*point.j - GHOSTS + (m) +
(2*point.i - GHOSTS + (l))*((1 << point.level)*2 + 2*GHOSTS)) +
_shift (point.level + 1) +
_index(a,0)*multigrid->field_size])
@
@def coarse(a,l,m,o)
(((real *)multigrid->d)[(point.k + GHOSTS)/2 + (o) +
((1 << point.level)/2 + 2*GHOSTS)*
((point.j + GHOSTS)/2 + (m) +
((point.i + GHOSTS)/2 + (l))*((1 << point.level)/2 + 2*GHOSTS)) +
_shift (point.level - 1) +
_index(a,0)*multigrid->field_size])
@
@def POINT_VARIABLES
VARIABLES
int level = point.level; NOT_UNUSED(level);
struct { int x, y, z; } child = {
2*((point.i + GHOSTS)%2) - 1,
2*((point.j + GHOSTS)%2) - 1,
2*((point.k + GHOSTS)%2) - 1
}; NOT_UNUSED(child);
Point parent = point; NOT_UNUSED(parent);
parent.level--;
parent.i = (point.i + GHOSTS)/2;
parent.j = (point.j + GHOSTS)/2;
parent.k = (point.k + GHOSTS)/2;
@
#endif
@def foreach_level(l)
OMP_PARALLEL() {
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = l; point.n = 1 << point.level;
int _k;
OMP(omp for schedule(static))
for (_k = GHOSTS; _k < point.n + GHOSTS; _k++) {
point.i = _k;
#if dimension > 1
for (point.j = GHOSTS; point.j < point.n + GHOSTS; point.j++)
#if dimension > 2
for (point.k = GHOSTS; point.k < point.n + GHOSTS; point.k++)
#endif
{
#endif
POINT_VARIABLES
@
@def end_foreach_level()
#if dimension > 1
}
#endif
}
}
@
@def foreach()
OMP_PARALLEL() {
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = depth(); point.n = 1 << point.level;
int _k;
OMP(omp for schedule(static))
for (_k = GHOSTS; _k < point.n + GHOSTS; _k++) {
point.i = _k;
#if dimension > 1
for (point.j = GHOSTS; point.j < point.n + GHOSTS; point.j++)
#if dimension > 2
for (point.k = GHOSTS; point.k < point.n + GHOSTS; point.k++)
#endif
{
#endif
POINT_VARIABLES
@
@def end_foreach()
#if dimension > 1
}
#endif
}
}
@
@define is_active(cell) (true)
@define is_leaf(cell) (level == depth())
@define is_local(cell) (true)
@define leaf 2
@def refine_cell(...) do {
fprintf (stderr, "grid depths do not match. Aborting.\n");
assert (0);
} while (0)
@
@define tree multigrid
#include "foreach_cell.h"
@def foreach_face_generic()
OMP_PARALLEL() {
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = depth(); point.n = 1 << point.level;
int _k;
OMP(omp for schedule(static))
for (_k = GHOSTS; _k <= point.n + GHOSTS; _k++) {
point.i = _k;
#if dimension > 1
for (point.j = GHOSTS; point.j <= point.n + GHOSTS; point.j++)
#if dimension > 2
for (point.k = GHOSTS; point.k <= point.n + GHOSTS; point.k++)
#endif
{
#endif
POINT_VARIABLES
@
@def end_foreach_face_generic()
#if dimension > 1
}
#endif
}
}
@
@def foreach_vertex()
foreach_face_generic() {
x -= Delta/2.;
#if dimension > 1
y -= Delta/2.;
#endif
#if dimension > 2
z -= Delta/2.;
#endif
@
@define end_foreach_vertex() } end_foreach_face_generic()
@define is_coarse() (point.level < depth())
#if dimension == 1
@define is_face_x() { int ig = -1; VARIABLES; {
@define end_is_face_x() }}
// foreach_edge?
@def foreach_child() {
int _i = 2*point.i - GHOSTS;
point.level++;
point.n *= 2;
for (int _k = 0; _k < 2; _k++) {
point.i = _i + _k;
POINT_VARIABLES;
@
@def end_foreach_child()
}
point.i = (_i + GHOSTS)/2;
point.level--;
point.n /= 2;
}
@
@define foreach_child_break() _k = 2
#elif dimension == 2
#define foreach_edge() foreach_face(y,x)
@define is_face_x() { int ig = -1; VARIABLES; if (point.j < point.n + GHOSTS) {
@define end_is_face_x() }}
@define is_face_y() { int jg = -1; VARIABLES; if (point.i < point.n + GHOSTS) {
@define end_is_face_y() }}
@def foreach_child() {
int _i = 2*point.i - GHOSTS, _j = 2*point.j - GHOSTS;
point.level++;
point.n *= 2;
for (int _k = 0; _k < 2; _k++)
for (int _l = 0; _l < 2; _l++) {
point.i = _i + _k; point.j = _j + _l;
POINT_VARIABLES;
@
@def end_foreach_child()
}
point.i = (_i + GHOSTS)/2; point.j = (_j + GHOSTS)/2;
point.level--;
point.n /= 2;
}
@
@define foreach_child_break() _k = _l = 2
#elif dimension == 3
@def foreach_vertex_aux()
foreach_vertex() {
struct { int x, y, z; } _a = {point.i, point.j, point.k};
@
@define end_foreach_vertex_aux() } end_foreach_vertex()
#define foreach_edge() \
foreach_vertex_aux() \
foreach_dimension() \
if (_a.x < point.n + GHOSTS)
@define is_face_x() { int ig = -1; VARIABLES; if (point.j < point.n + GHOSTS && point.k < point.n + GHOSTS) {
@define end_is_face_x() }}
@define is_face_y() { int jg = -1; VARIABLES; if (point.i < point.n + GHOSTS && point.k < point.n + GHOSTS) {
@define end_is_face_y() }}
@define is_face_z() { int kg = -1; VARIABLES; if (point.i < point.n + GHOSTS && point.j < point.n + GHOSTS) {
@define end_is_face_z() }}
@def foreach_child() {
int _i = 2*point.i - GHOSTS;
int _j = 2*point.j - GHOSTS;
int _k = 2*point.k - GHOSTS;
point.level++;
point.n *= 2;
for (int _l = 0; _l < 2; _l++)
for (int _m = 0; _m < 2; _m++)
for (int _n = 0; _n < 2; _n++) {
point.i = _i + _l; point.j = _j + _m; point.k = _k + _n;
POINT_VARIABLES;
@
@def end_foreach_child()
}
point.i = (_i + GHOSTS)/2;
point.j = (_j + GHOSTS)/2;
point.k = (_k + GHOSTS)/2;
point.level--;
point.n /= 2;
}
@
@define foreach_child_break() _l = _m = _n = 2
#endif
@if TRASH
@ undef trash
@ define trash(list) reset(list, undefined)
@endif
#include "neighbors.h"
void reset (void * alist, double val)
{
scalar * list = (scalar *) alist;
for (scalar s in list)
if (!is_constant(s))
for (int b = 0; b < s.block; b++) {
real * data = (real *) multigrid->d;
data += (s.i + b)*multigrid->field_size;
for (int i = 0; i < multigrid->field_size; i++, data++)
*data = val;
}
}
// Boundaries
#if dimension == 1
@def foreach_boundary_dir(l,d)
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = l < 0 ? depth() : l;
point.n = 1 << point.level;
if (d == left) {
point.i = GHOSTS;
ig = -1;
}
else if (d == right) {
point.i = point.n + GHOSTS - 1;
ig = 1;
}
{
POINT_VARIABLES
@
@define end_foreach_boundary_dir() }
@define neighbor(o,p,q) ((Point){point.i+o, point.level, point.n _BLOCK_INDEX})
@define is_boundary(point) (point.i < GHOSTS || point.i >= point.n + GHOSTS)
#elif dimension == 2
@def foreach_boundary_dir(l,d)
OMP_PARALLEL() {
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = l < 0 ? depth() : l;
point.n = 1 << point.level;
int * _i = &point.j;
if (d == left) {
point.i = GHOSTS;
ig = -1;
}
else if (d == right) {
point.i = point.n + GHOSTS - 1;
ig = 1;
}
else if (d == bottom) {
point.j = GHOSTS;
_i = &point.i;
jg = -1;
}
else if (d == top) {
point.j = point.n + GHOSTS - 1;
_i = &point.i;
jg = 1;
}
int _l;
OMP(omp for schedule(static))
for (_l = 0; _l < point.n + 2*GHOSTS; _l++) {
*_i = _l;
{
POINT_VARIABLES
@
@def end_foreach_boundary_dir()
}
}
}
@
@def neighbor(o,p,q)
((Point){point.i+o, point.j+p, point.level, point.n _BLOCK_INDEX})
@
@def is_boundary(point) (point.i < GHOSTS || point.i >= point.n + GHOSTS ||
point.j < GHOSTS || point.j >= point.n + GHOSTS)
@
#elif dimension == 3
@def foreach_boundary_dir(l,d)
OMP_PARALLEL() {
int ig = 0, jg = 0, kg = 0; NOT_UNUSED(ig); NOT_UNUSED(jg); NOT_UNUSED(kg);
Point point = {0};
point.level = l < 0 ? depth() : l;
point.n = 1 << point.level;
int * _i = &point.j, * _j = &point.k;
if (d == left) {
point.i = GHOSTS;
ig = -1;
}
else if (d == right) {
point.i = point.n + GHOSTS - 1;
ig = 1;
}
else if (d == bottom) {
point.j = GHOSTS;
_i = &point.i;
jg = -1;
}
else if (d == top) {
point.j = point.n + GHOSTS - 1;
_i = &point.i;
jg = 1;
}
else if (d == back) {
point.k = GHOSTS;
_i = &point.i; _j = &point.j;
kg = -1;
}
else if (d == front) {
point.k = point.n + GHOSTS - 1;
_i = &point.i; _j = &point.j;
kg = 1;
}
int _l;
OMP(omp for schedule(static))
for (_l = 0; _l < point.n + 2*GHOSTS; _l++) {
*_i = _l;
for (int _m = 0; _m < point.n + 2*GHOSTS; _m++) {
*_j = _m;
POINT_VARIABLES
@
@def end_foreach_boundary_dir()
}
}
}
@
@def neighbor(o,p,q)
((Point){point.i+o, point.j+p, point.k+q, point.level, point.n _BLOCK_INDEX})
@
@def is_boundary(point) (point.i < GHOSTS || point.i >= point.n + GHOSTS ||
point.j < GHOSTS || point.j >= point.n + GHOSTS ||
point.k < GHOSTS || point.k >= point.n + GHOSTS)
@
#endif // dimension == 3
@def foreach_boundary(b)
if (default_scalar_bc[b] != periodic_bc)
foreach_boundary_dir (depth(), b)
if (!is_boundary(point)) {
@
@define end_foreach_boundary() } end_foreach_boundary_dir()
@define neighborp(k,l,o) neighbor(k,l,o)
static double periodic_bc (Point point, Point neighbor, scalar s, bool * data);
static void box_boundary_level (const Boundary * b, scalar * scalars, int l)
{
extern double (* default_scalar_bc[]) (Point, Point, scalar, bool *);
disable_fpe (FE_DIVBYZERO|FE_INVALID);
for (int d = 0; d < 2*dimension; d++)
if (default_scalar_bc[d] == periodic_bc)
for (scalar s in scalars)
if (!is_constant(s) && s.block > 0) {
if (is_vertex_scalar (s))
s.boundary[d] = s.boundary_homogeneous[d] = NULL;
else if (s.face) {
vector v = s.v;
v.x.boundary[d] = v.x.boundary_homogeneous[d] = NULL;
}
}
for (int bghost = 1; bghost <= BGHOSTS; bghost++)
for (int d = 0; d < 2*dimension; d++) {
scalar * list = NULL, * listb = NULL;
for (scalar s in scalars)
if (!is_constant(s) && s.block > 0) {
scalar sb = s;
#if dimension > 1
if (s.v.x.i >= 0) {
// vector component
int j = 0;
while ((&s.v.x)[j].i != s.i) j++;
sb = (&s.v.x)[(j - d/2 + dimension) % dimension];
}
#endif
if (sb.boundary[d] && sb.boundary[d] != periodic_bc) {
list = list_append (list, s);
listb = list_append (listb, sb);
}
}
if (list) {
foreach_boundary_dir (l, d) {
scalar s, sb;
for (s,sb in list,listb) {
if ((s.face && sb.i == s.v.x.i) || is_vertex_scalar (s)) {
// normal component of face vector, or vertex scalar
if (bghost == 1)
foreach_block()
s[(ig + 1)/2,(jg + 1)/2,(kg + 1)/2] =
sb.boundary[d] (point, neighborp(ig,jg,kg), s, NULL);
}
else
// tangential component of face vector or centered
foreach_block()
s[bghost*ig,bghost*jg,bghost*kg] =
sb.boundary[d] (neighborp((1 - bghost)*ig,
(1 - bghost)*jg,
(1 - bghost)*kg),
neighborp(bghost*ig,bghost*jg,bghost*kg),
s, NULL);
}
}
free (list);
free (listb);
}
}
enable_fpe (FE_DIVBYZERO|FE_INVALID);
}
/* Periodic boundaries */
#if !_MPI
#if dimension == 1
static void periodic_boundary_level_x (const Boundary * b, scalar * list, int l)
{
scalar * list1 = NULL;
for (scalar s in list)
if (!is_constant(s) && s.block > 0 && s.boundary[right] == periodic_bc)
list1 = list_add (list1, s);
if (!list1)
return;
if (l == 0) {
foreach_level(0)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
real v = sb[];
foreach_neighbor()
sb[] = v;
}
free (list1);
return;
}
Point point = {0};
point.level = l < 0 ? depth() : l; point.n = 1 << point.level;
for (int i = 0; i < GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i] = sb[i + point.n];
}
for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i] = sb[i - point.n];
}
free (list1);
}
#else // dimension != 1
@define VT _attribute[s.i].v.y
foreach_dimension()
static void periodic_boundary_level_x (const Boundary * b, scalar * list, int l)
{
scalar * list1 = NULL;
for (scalar s in list)
if (!is_constant(s) && s.block > 0) {
if (s.face) {
scalar vt = VT;
if (vt.boundary[right] == periodic_bc)
list1 = list_add (list1, s);
}
else if (s.boundary[right] == periodic_bc)
list1 = list_add (list1, s);
}
if (!list1)
return;
if (l == 0) {
foreach_level(0)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
real v = sb[];
foreach_neighbor()
sb[] = v;
}
free (list1);
return;
}
OMP_PARALLEL() {
Point point = {0};
point.level = l < 0 ? depth() : l; point.n = 1 << point.level;
#if dimension == 2
int j;
OMP(omp for schedule(static))
for (j = 0; j < point.n + 2*GHOSTS; j++) {
for (int i = 0; i < GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i,j] = sb[i + point.n,j];
}
for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i,j] = sb[i - point.n,j];
}
}
#else // dimension == 3
int j;
OMP(omp for schedule(static))
for (j = 0; j < point.n + 2*GHOSTS; j++)
for (int k = 0; k < point.n + 2*GHOSTS; k++) {
for (int i = 0; i < GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i,j,k] = sb[i + point.n,j,k];
}
for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
for (scalar s in list1)
for (int b = 0; b < s.block; b++) {
scalar sb = {s.i + b};
sb[i,j,k] = sb[i - point.n,j,k];
}
}
#endif
}
free (list1);
}
@undef VT
#endif // dimension != 1
#endif // !_MPI
void free_grid (void)
{
if (!grid)
return;
free_boundaries();
Multigrid * m = multigrid;
free (m->d);
free (m);
grid = NULL;
}
int log_base2 (int n) {
int m = n, r = 0;
while (m > 1)
m /= 2, r++;
return (1 << r) < n ? r + 1 : r;
}
void init_grid (int n)
{
free_grid();
Multigrid * m = qmalloc (1, Multigrid);
grid = (Grid *) m;
grid->depth = grid->maxdepth = log_base2(n);
N = 1 << depth();
// mesh size
grid->n = grid->tn = 1 << dimension*depth();
// box boundaries
Boundary * b = qcalloc (1, Boundary);
b->level = box_boundary_level;
add_boundary (b);
#if _MPI
Boundary * mpi_boundary_new();
mpi_boundary_new();
#else
// periodic boundaries
foreach_dimension() {
Boundary * b = qcalloc (1, Boundary);
b->level = periodic_boundary_level_x;
add_boundary (b);
}
#endif
// allocate grid: this must be after mpi_boundary_new() since this modifies depth()
m->field_size = _shift (depth() + 1);
m->d = (char *) malloc(m->field_size*datasize);
reset (all, 0.);
}
void realloc_scalar (int size)
{
Multigrid * p = multigrid;
datasize += size;
qrealloc (p->d, p->field_size*datasize, char);
}
#if _MPI
int mpi_dims[dimension], mpi_coords[dimension];
#undef _DELTA
#undef _I
#undef _J
#undef _K
#define _DELTA (1./(1 << point.level)/mpi_dims[0])
#define _I (point.i - GHOSTS + mpi_coords[0]*(1 << point.level))
#define _J (point.j - GHOSTS + mpi_coords[1]*(1 << point.level))
#define _K (point.k - GHOSTS + mpi_coords[2]*(1 << point.level))
#endif
Point locate (double xp = 0, double yp = 0, double zp = 0)
{
Point point = {0};
point.level = -1, point.n = 1 << depth();
#if _MPI
point.i = (xp - X0)/L0*point.n*mpi_dims[0] + GHOSTS - mpi_coords[0]*point.n;
if (point.i < GHOSTS || point.i >= point.n + GHOSTS)
return point;
#if dimension >= 2
point.j = (yp - Y0)/L0*point.n*mpi_dims[0] + GHOSTS - mpi_coords[1]*point.n;
if (point.j < GHOSTS || point.j >= point.n + GHOSTS)
return point;
#endif
#if dimension >= 3
point.k = (zp - Z0)/L0*point.n*mpi_dims[0] + GHOSTS - mpi_coords[2]*point.n;
if (point.k < GHOSTS || point.k >= point.n + GHOSTS)
return point;
#endif
#else // !_MPI
point.i = (xp - X0)/L0*point.n + GHOSTS;
if (point.i < GHOSTS || point.i >= point.n + GHOSTS)
return point;
#if dimension >= 2
point.j = (yp - Y0)/L0*point.n + GHOSTS;
if (point.j < GHOSTS || point.j >= point.n + GHOSTS)
return point;
#endif
#if dimension >= 3
point.k = (zp - Z0)/L0*point.n + GHOSTS;
if (point.k < GHOSTS || point.k >= point.n + GHOSTS)
return point;
#endif
#endif // !_MPI
point.level = depth();
return point;
}
#if !_GPU
# include "multigrid-common.h"
#endif
void dimensions (int nx = 0, int ny = 0, int nz = 0)
{
#if _MPI
int p[] = {nx, ny, nz};
for (int i = 0; i < dimension; i++)
mpi_dims[i] = p[i];
#endif
}
#if _MPI
@if dimension == 1
@def foreach_slice_x(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = start; point.i < end; point.i++)
@
@define end_foreach_slice_x() }
@elif dimension == 2
@def foreach_slice_x(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = start; point.i < end; point.i++)
for (point.j = 0; point.j < point.n + 2*GHOSTS; point.j++)
@
@define end_foreach_slice_x() }
@def foreach_slice_y(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = 0; point.i < point.n + 2*GHOSTS; point.i++)
for (point.j = start; point.j < end; point.j++)
@
@define end_foreach_slice_y() }
@elif dimension == 3
@def foreach_slice_x(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = start; point.i < end; point.i++)
for (point.j = 0; point.j < point.n + 2*GHOSTS; point.j++)
for (point.k = 0; point.k < point.n + 2*GHOSTS; point.k++)
@
@define end_foreach_slice_x() }
@def foreach_slice_y(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = 0; point.i < point.n + 2*GHOSTS; point.i++)
for (point.j = start; point.j < end; point.j++)
for (point.k = 0; point.k < point.n + 2*GHOSTS; point.k++)
@
@define end_foreach_slice_y() }
@def foreach_slice_z(start, end, l) {
Point point = {0};
point.level = l; point.n = 1 << point.level;
for (point.i = 0; point.i < point.n + 2*GHOSTS; point.i++)
for (point.j = 0; point.j < point.n + 2*GHOSTS; point.j++)
for (point.k = start; point.k < end; point.k++)
@
@define end_foreach_slice_z() }
@endif // dimension == 3
#include "multigrid-mpi.h"
#endif // _MPI
|