src/grid/multigrid.h

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
  
#define GRIDNAME "Multigrid"
  #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;
  } Multigrid;

  struct _Point {
    int i;
  #if dimension > 1
    int j;
  #endif
  #if dimension > 2
    int k;
  #endif
    int level, n;
  };
  static Point last_point;

  #define multigrid ((Multigrid *)grid)

  #if dimension == 1
  # define dimpower(n) (n)
  #elif dimension == 2
  # define dimpower(n) sq(n)
  #elif dimension == 3
  # define dimpower(n) cube(n)
  #endif

  static size_t _size (size_t l)
  {
    size_t n = (1 << l) + 2*GHOSTS;
    return dimpower(n);
  }

  #define CELL(m,level,i)  (*((Cell *) &m[level][(i)*datasize]))

  /***** Cartesian macros *****/
  #if dimension == 1
  @def data(k,l,m)
    ((double *)&multigrid->d[point.level][(point.i + k)*datasize + (l) - (l)]) @
  #elif dimension == 2
  @def data(k,l,m)
    ((double *)&multigrid->d[point.level][((point.i + k)*((1 << point.level) +
  							2*GHOSTS) +
  					 (point.j + l))*datasize]) @
  #elif dimension == 3
  @def data(l,m,o)
    ((double *)&multigrid->d[point.level][((point.i + l)*sq((1 << point.level) +
  							  2*GHOSTS) +
  					 (point.j + m)*((1 << point.level) +
  							2*GHOSTS) +
  					 (point.k + o))*datasize]) @
  #endif

  @define allocated(...) true
  @define allocated_child(...) true

  /***** Multigrid variables and macros *****/
  @define depth()       (grid->depth)
  #if dimension == 1
  @def fine(a,k,l,m)
    ((double *)
     &multigrid->d[point.level+1][(2*point.i-GHOSTS+k)*datasize])[a.i]
  @
    @def coarse(a,k,l,m)
    ((double *)
     &multigrid->d[point.level-1][((point.i+GHOSTS)/2+k)*datasize])[a.i]
  @
  @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)
    ((double *)
     &multigrid->d[point.level+1][((2*point.i-GHOSTS+k)*2*((1 << point.level) +
  							 GHOSTS) +
  			    (2*point.j-GHOSTS+l))*datasize])[a.i]
  @
  @def coarse(a,k,l,m)
    ((double *)
     &multigrid->d[point.level-1][(((point.i+GHOSTS)/2+k)*((1 << point.level)/2 +
  							 2*GHOSTS) +
  			    (point.j+GHOSTS)/2+l)*datasize])[a.i]
  @
  @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)
  ((double *)
   &multigrid->d[point.level+1][((2*point.i-GHOSTS+l)*sq(2*((1 << point.level) +
  							  GHOSTS)) +
  			       (2*point.j-GHOSTS+m)*2*((1 << point.level) +
  						       GHOSTS) +
  			       (2*point.k-GHOSTS+o))*datasize])[a.i]
  @
  @def coarse(a,l,m,o)
  ((double *)
   &multigrid->d[point.level-1][(((point.i+GHOSTS)/2+l)*sq((1 << point.level)/2 +
  							 2*GHOSTS) +
  			       ((point.j+GHOSTS)/2+m)*((1 << point.level)/2 +
  						       2*GHOSTS) +
  			       (point.k+GHOSTS)/2+o)*datasize])[a.i]
  @
  @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;
    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;
    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;
    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() true
  				 
  // 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() (point.j < point.n + GHOSTS)
  @define is_face_y() (point.i < point.n + GHOSTS)

  @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() (point.j < point.n + GHOSTS && point.k < point.n + GHOSTS)
  @define is_face_y() (point.i < point.n + GHOSTS && point.k < point.n + GHOSTS)
  @define is_face_z() (point.i < point.n + GHOSTS && point.j < point.n + GHOSTS)

  @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;
    Point p;
    p.level = depth(); p.n = 1 << p.level;
    for (; p.level >= 0; p.n /= 2, p.level--)
      for (int i = 0; i < dimpower(p.n + 2*GHOSTS); i++)
        for (scalar s in list)
  	if (!is_constant(s))
  	  ((double *)(&multigrid->d[p.level][i*datasize]))[s.i] = 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;
    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})
  @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;
    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()
      }
    }
  }
  @

  @define neighbor(o,p,q) ((Point){point.i+o, point.j+p, point.level, point.n})
  @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;
    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})
  @
  @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)
    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);
  			
  static void box_boundary_level (const Boundary * b, scalar * scalars, int l)
  {
    disable_fpe (FE_DIVBYZERO|FE_INVALID);
    for (int layer = 1; layer <= BGHOSTS; layer++)
      for (int d = 0; d < 2*dimension; d++) {

        scalar * list = NULL, * listb = NULL;
        for (scalar s in scalars)
  	if (!is_constant(s)) {
  	  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) {
  	      // normal component of face vector
  	      if (layer == 1)
  		s[(ig + 1)/2,(jg + 1)/2,(kg + 1)/2] =
  		  sb.boundary[d] (point, neighborp(ig,jg,kg), s);
  	    }
  	    else
  	      // tangential component of face vector or centered
  	      s[layer*ig,layer*jg,layer*kg] =
  		sb.boundary[d] (neighborp((1 - layer)*ig,
  					  (1 - layer)*jg,
  					  (1 - layer)*kg),
  				neighborp(layer*ig,layer*jg,layer*kg), s);
  	  }
  	}
  	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.boundary[right] == periodic_bc)
        list1 = list_add (list1, s);
    if (!list1)
      return;

    if (l == 0) {
      foreach_level(0)
        for (scalar s in list1) {
  	double v = s[];
  	foreach_neighbor()
  	  s[] = 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)
        s[i] = s[i + point.n];
    for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
      for (scalar s in list1)
        s[i] = s[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)) {
        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) {
  	double v = s[];
  	foreach_neighbor()
  	  s[] = v;
        }
      free (list1);
      return;
    }

    OMP_PARALLEL() {
      Point point;
      point.level = l < 0 ? depth() : l; point.n = 1 << point.level;
  #if dimension == 2  
      point.i = point.j = 0;
      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)
  	    s[i,j] = s[i + point.n,j];
  	for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
  	  for (scalar s in list1)
  	    s[i,j] = s[i - point.n,j];
        }
  #else // dimension == 3
      point.i = point.j = point.k = 0;
      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)
  	      s[i,j,k] = s[i + point.n,j,k];
  	  for (int i = point.n + GHOSTS; i < point.n + 2*GHOSTS; i++)
  	    for (scalar s in list1)
  	      s[i,j,k] = s[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;
    for (int l = 0; l <= depth(); l++)
      free (m->d[l]);
    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
    m->d = (char **) malloc(sizeof(Point *)*(depth() + 1));
    for (int l = 0; l <= depth(); l++) {
      size_t len = _size(l)*datasize;
      m->d[l] = (char *) malloc (len);
      /* trash the data just to make sure it's either explicitly
         initialised or never touched */
      double * v = (double *) m->d[l];
      for (int i = 0; i < len/sizeof(double); i++)
        v[i] = undefined;
    }
    reset (all, 0.);
  }

  void realloc_scalar (void)
  {
    Multigrid * p = multigrid;
    size_t oldatasize = datasize - sizeof(double);
    for (int l = 0; l <= depth(); l++) {
      size_t len = _size(l);
      qrealloc (p->d[l], len*datasize, char);
      char * data = p->d[l] + (len - 1)*oldatasize;
      for (int i = len - 1; i > 0; i--, data -= oldatasize)
        memmove (data + i*sizeof(double), data, oldatasize);  
    }
  }

  #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

  struct _locate { double x, y, z; };

  Point locate (struct _locate p)
  {
    Point point = { .level = -1, .n = 1 << depth() };
  #if _MPI
    point.i = (p.x - 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 = (p.y - 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 = (p.z - 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 = (p.x - X0)/L0*point.n + GHOSTS;
    if (point.i < GHOSTS || point.i >= point.n + GHOSTS)
      return point;
  #if dimension >= 2
    point.j = (p.y - Y0)/L0*point.n + GHOSTS;
    if (point.j < GHOSTS || point.j >= point.n + GHOSTS)
      return point;
  #endif
  #if dimension >= 3
    point.k = (p.z - Z0)/L0*point.n + GHOSTS;
    if (point.k < GHOSTS || point.k >= point.n + GHOSTS)
      return point;
  #endif  
  #endif // !_MPI
    point.level = depth();
    return point;
  }

  #include "multigrid-common.h"

  struct Dimensions {
    int nx, ny, nz;
  };

  void dimensions (struct Dimensions p)
  {
  #if _MPI
    for (int i = 0; i < dimension; i++)
      mpi_dims[i] = (&p.nx)[i];
  #endif
  }

  #if _MPI

  @if dimension == 1

  @def foreach_slice_x(start, end, l) {
    Point point;
    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;
    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;
    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;
    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;
    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;
    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