aboutsummaryrefslogtreecommitdiff
path: root/src/apply.c
blob: 353361f0b0f8bc60f42d90099f2d05f142817d1f (plain)
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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"

#include "util_ErrorCodes.h"
#include "util_Table.h"

#include "reflection.h"



#define COPY_PRE(VARTYPE)                                               \
  static void                                                           \
  copy_##VARTYPE (VARTYPE const * restrict const srcvar,                \
                  VARTYPE * restrict const dstvar,                      \
                  int const ni, int const nj, int const nk,             \
                  int const imin, int const jmin, int const kmin,       \
                  int const imax, int const jmax, int const kmax,       \
                  int const ioff, int const joff, int const koff,       \
                  int const idir, int const jdir, int const kdir,       \
                  int const parity)                                     \
  {                                                                     \
    assert (abs(idir)==1);                                              \
    assert (abs(jdir)==1);                                              \
    assert (abs(kdir)==1);                                              \
    int const iioff = ioff + (1 - idir) * imin;                         \
    int const jjoff = joff + (1 - jdir) * jmin;                         \
    int const kkoff = koff + (1 - kdir) * kmin;                         \
    int const iimin = iioff + idir * imin;                              \
    int const jjmin = jjoff + jdir * jmin;                              \
    int const kkmin = kkoff + kdir * kmin;                              \
    int const iimax = iioff + idir * imax;                              \
    int const jjmax = jjoff + jdir * jmax;                              \
    int const kkmax = kkoff + kdir * kmax;                              \
    assert (imin>=0 && imax<=ni);                                       \
    assert (jmin>=0 && jmax<=nj);                                       \
    assert (kmin>=0 && kmax<=nk);                                       \
    assert (iimin>=0 && iimax<=ni);                                     \
    assert (jjmin>=0 && jjmax<=nj);                                     \
    assert (kkmin>=0 && kkmax<=nk);                                     \
    assert (iimax>=-1 && iimin<ni);                                     \
    assert (jjmax>=-1 && jjmin<nj);                                     \
    assert (kkmax>=-1 && kkmin<nk);

#define COPY_LOOP(VARTYPE)                                              \
    for (int k=kmin; k<kmax; ++k) {                                     \
      for (int j=jmin; j<jmax; ++j) {                                   \
        for (int i=imin; i<imax; ++i) {                                 \
          int const dstind = i + ni * (j + nj * k);                     \
          int const ii = iioff + idir * i;                              \
          int const jj = jjoff + jdir * j;                              \
          int const kk = kkoff + kdir * k;                              \
          int const srcind = ii + ni * (jj + nj * kk);                  \
          REAL(dstvar[dstind] RE = parity * srcvar[srcind] RE;)         \
          IMAG(dstvar[dstind] IM = parity * srcvar[srcind] IM;)         \
        }                                                               \
      }                                                                 \
    }                                                                   \

#define COPY_POST(VARTYPE)                      \
  }

#define REAL(x) x
#define IMAG(x) /* nothing */
#define RE /* nothing */
#define IM ERROR ERROR ERROR

#ifdef HAVE_CCTK_INT1
COPY_PRE(CCTK_INT1)
#pragma omp parallel for
COPY_LOOP(CCTK_INT1)
COPY_POST(CCTK_INT1)
#endif

#ifdef HAVE_CCTK_INT2
COPY_PRE(CCTK_INT2)
#pragma omp parallel for
COPY_LOOP(CCTK_INT2)
COPY_POST(CCTK_INT2)
#endif

#ifdef HAVE_CCTK_INT4
COPY_PRE(CCTK_INT4)
#pragma omp parallel for
COPY_LOOP(CCTK_INT4)
COPY_POST(CCTK_INT4)
#endif

#ifdef HAVE_CCTK_INT8
COPY_PRE(CCTK_INT8)
#pragma omp parallel for
COPY_LOOP(CCTK_INT8)
COPY_POST(CCTK_INT8)
#endif

#ifdef HAVE_CCTK_REAL4
COPY_PRE(CCTK_REAL4)
#pragma omp parallel for
COPY_LOOP(CCTK_REAL4)
COPY_POST(CCTK_REAL4)
#endif

#ifdef HAVE_CCTK_REAL8
COPY_PRE(CCTK_REAL8)
#pragma omp parallel for
COPY_LOOP(CCTK_REAL8)
COPY_POST(CCTK_REAL8)
#endif

#ifdef HAVE_CCTK_REAL16
COPY_PRE(CCTK_REAL16)
#pragma omp parallel for
COPY_LOOP(CCTK_REAL16)
COPY_POST(CCTK_REAL16)
#endif

#undef REAL
#undef IMAG
#undef RE
#undef IM

#define REAL(x) x
#define IMAG(x) x
#define RE .Re
#define IM .Im

#ifdef HAVE_CCTK_COMPLEX8
COPY_PRE(CCTK_COMPLEX8)
#pragma omp parallel for
COPY_LOOP(CCTK_COMPLEX8)
COPY_POST(CCTK_COMPLEX8)
#endif

#ifdef HAVE_CCTK_COMPLEX16
COPY_PRE(CCTK_COMPLEX16)
#pragma omp parallel for
COPY_LOOP(CCTK_COMPLEX16)
COPY_POST(CCTK_COMPLEX16)
#endif

#ifdef HAVE_CCTK_COMPLEX32
COPY_PRE(CCTK_COMPLEX32)
#pragma omp parallel for
COPY_LOOP(CCTK_COMPLEX32)
COPY_POST(CCTK_COMPLEX32)
#endif

#undef REAL
#undef IMAG
#undef RE
#undef IM

#undef COPY_PRE
#undef COPY_LOOP
#undef COPY_POST



static int
BndReflectVI (cGH const * restrict const cctkGH,
              int const vi)
{
  DECLARE_CCTK_PARAMETERS;
  
  int gi;
  cGroup group;
  cGroupDynamicData data;
  int firstvar, numvars;
  char * restrict fullname;
  
  void * restrict varptr;
  
  int table;
  char tensortypealias[1000];
  enum tensortype { UNKNOWN,
                    SCALAR, VECTOR, SYMTENSOR, SYMTENSOR3, TENSOR,
                    WEYLSCALARS_REAL, MANUALCARTESIAN };
  enum tensortype ttype;
  CCTK_INT tensorparity;
  int tcomponent;
  
  int do_reflection[6];
  int do_stagger[6];
  
  int dir, face;
  
  int lsh[3], imin[3], imax[3], ioff[3], idir[3];
  
  int parity;
  int manual_parities[3];

  int d;
  
  int ierr;
  
  
  
  /* Check arguments */
  if (! cctkGH) {
    CCTK_WARN (0, "Argument cctkGH is NULL");
  }
  if (vi < 0 || vi >= CCTK_NumVars()) {
    CCTK_WARN (0, "Illegal variable index");
  }
  
  if (verbose) {
    fullname = CCTK_FullName (vi);
    if (! fullname)   {
      CCTK_WARN (0, "Internal error in CCTK_FullName");
    }
    CCTK_VInfo (CCTK_THORNSTRING,
                "Applying reflection boundary conditions to \"%s\"",
                fullname);
    free (fullname);
  }
  
  
  
  /* Get and check group information */
  gi = CCTK_GroupIndexFromVarI (vi);
  if (gi < 0 || gi > CCTK_NumGroups()) {
    CCTK_WARN (0, "Internal error in CCTK_GroupIndexFromVarI");
  }
  
  ierr = CCTK_GroupData (gi, &group);
  assert (!ierr);
  assert (group.grouptype == CCTK_GF);
  assert (group.disttype == CCTK_DISTRIB_DEFAULT);
  assert (group.stagtype == 0);
  
  firstvar = CCTK_FirstVarIndexI (gi);
  assert (firstvar>=0 && firstvar<CCTK_NumVars());
  numvars = CCTK_NumVarsInGroupI (gi);
  assert (numvars>=0);
  
  ierr = CCTK_GroupDynamicData (cctkGH, gi, &data);
  assert (!ierr);
  
  table = CCTK_GroupTagsTableI(gi);
  assert (table>=0);
  
  varptr = CCTK_VarDataPtrI (cctkGH, 0, vi);
  assert (varptr);
  
  
  
  /* Get and check tensor type information */
  ierr = Util_TableGetString
    (table, sizeof tensortypealias, tensortypealias, "tensortypealias");
  if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
    /* assume a scalar */
    if (numvars != 1) {
      static int * restrict didwarn = 0;
      if (! didwarn) {
        didwarn = calloc (CCTK_NumGroups(), sizeof *didwarn);
      }
      if (! didwarn[gi]) {
        didwarn[gi] = 1;
        {
          char * groupname = CCTK_GroupName(gi);
          assert (groupname);
          CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                      "Group \"%s\" has no tensor type and contains more than one element -- treating these as \"scalar\"",
                      groupname);
          free (groupname);
        }
      }
    }
    strcpy (tensortypealias, "scalar");
  } else if (ierr<0) {
    char * groupname = CCTK_GroupName(gi);
    assert (groupname);
    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Error in tensor type alias declaration for group \"%s\"",
                groupname);
    free (groupname);
  }
  
  ttype = UNKNOWN;
  tcomponent = 0;
  if (CCTK_EQUALS (tensortypealias, "scalar")) {
    /* scalar */
    ttype = SCALAR;
    tcomponent = 0;
  } else if (CCTK_EQUALS (tensortypealias, "4scalar")) {
    /* 4-scalar */
    ttype = SCALAR;
    tcomponent = 0;
  } else if (CCTK_EQUALS (tensortypealias, "u")
             || CCTK_EQUALS (tensortypealias, "d"))
  {
    /* vector */
    assert (numvars == 3);
    ttype = VECTOR;
    tcomponent = vi - firstvar;
  } else if (CCTK_EQUALS (tensortypealias, "4u")
             || CCTK_EQUALS (tensortypealias, "4d"))
  {
    /* 4-vector */
    assert (numvars == 4);
    if (vi == firstvar) {
      ttype = SCALAR;
      tcomponent = 0;
    } else {
      ttype = VECTOR;
      tcomponent = vi - firstvar - 1;
    }
  } else if (CCTK_EQUALS (tensortypealias, "uu_sym")
             || CCTK_EQUALS (tensortypealias, "dd_sym"))
  {
    /* symmetric tensor */
    assert (numvars == 6);
    ttype = SYMTENSOR;
    tcomponent = vi - firstvar;
  } else if (CCTK_EQUALS (tensortypealias, "uu")
             || CCTK_EQUALS (tensortypealias, "ud")
             || CCTK_EQUALS (tensortypealias, "du")
             || CCTK_EQUALS (tensortypealias, "dd"))
  {
    /* non-symmetric tensor */
    assert (numvars == 9);
    ttype = TENSOR;
    tcomponent = vi - firstvar;
  } else if (CCTK_EQUALS (tensortypealias, "4uu_sym")
             || CCTK_EQUALS (tensortypealias, "4dd_sym"))
  {
    /* symmetric 4-tensor */
    assert (numvars == 10);
    if (vi == firstvar) {
      ttype = SCALAR;
      tcomponent = 0;
    } else if (vi <= firstvar+3) {
      ttype = VECTOR;
      tcomponent = vi - firstvar - 1;
    } else {
      ttype = SYMTENSOR;
      tcomponent = vi - firstvar - 4;
    }
  } else if (CCTK_EQUALS (tensortypealias, "ddd_sym")) {
    /* 3rd rank tensor, symmetric in last 2 indices */
    assert (numvars == 18);
    ttype = SYMTENSOR3;
    tcomponent = vi - firstvar;
  } else if (CCTK_EQUALS (tensortypealias, "weylscalars_real")) {
    /* Weyl scalars, stored as 10 real values.  NOTE: This assumes
       that Psi_0 comes first, which is NOT the default with
       PsiKadelia.  */
    assert (numvars == 10);
    ttype = WEYLSCALARS_REAL;
    tcomponent = vi - firstvar;
  } else if (CCTK_EQUALS (tensortypealias, "ManualCartesian")) {
      /* Reflection symmetries specified by hand */
      ttype = MANUALCARTESIAN;
      tcomponent = vi - firstvar;
  } else {
    char * groupname = CCTK_GroupName(gi);
    assert (groupname);
    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Illegal tensor type alias for group \"%s\"",
                groupname);
    free (groupname);
  }
  
  switch (ttype) {
  case SCALAR:
    assert (tcomponent>=0 && tcomponent<1);
    break;
  case VECTOR:
    assert (tcomponent>=0 && tcomponent<3);
    break;
  case SYMTENSOR:
    assert (tcomponent>=0 && tcomponent<6);
    break;
  case SYMTENSOR3:
    assert (tcomponent>=0 && tcomponent<18);
    break;
  case TENSOR:
    assert (tcomponent>=0 && tcomponent<9);
    break;
  case WEYLSCALARS_REAL:
    assert (tcomponent>=0 && tcomponent<10);
    break;
  case MANUALCARTESIAN:
    /* No restriction on number of components */
    ReflectionSymmetry_GetManualParities(table, gi, manual_parities);
    break;

  default:
    assert (0);
  }
  
  ierr = Util_TableGetInt (table, & tensorparity, "tensorparity");
  if (ierr == UTIL_ERROR_TABLE_NO_SUCH_KEY) {
    tensorparity = +1;
  } else if (ierr<0) {
    char * groupname = CCTK_GroupName(gi);
    assert (groupname);
    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Error in tensor parity declaration for group \"%s\"",
                groupname);
    free (groupname);
  }
  
  
  
  /* Reflection symmetry information */
  do_reflection[0] = reflection_x;
  do_reflection[1] = reflection_upper_x;
  do_reflection[2] = reflection_y;
  do_reflection[3] = reflection_upper_y;
  do_reflection[4] = reflection_z;
  do_reflection[5] = reflection_upper_z;
  
  do_stagger[0] = avoid_origin_x;
  do_stagger[1] = avoid_origin_upper_x;
  do_stagger[2] = avoid_origin_y;
  do_stagger[3] = avoid_origin_upper_y;
  do_stagger[4] = avoid_origin_z;
  do_stagger[5] = avoid_origin_upper_z;
  
  
  
  /* Loop over all directions and faces */
  for (dir=0; dir<3; ++dir) {
    for (face=0; face<2; ++face) {
      /* If there is a reflection symmetry on that face */
      if (do_reflection[2*dir+face]) {
        /* If we have the outer boundary of that face */
        if (cctkGH->cctk_bbox[2*dir+face]) {
          
          /* Find parity */
          parity = tensorparity;
          switch (ttype) {
          case SCALAR:
            parity *= +1;
            break;
          case VECTOR:
            parity *= dir == tcomponent ? -1 : +1;
            break;
          case SYMTENSOR:
            switch (tcomponent) {
            case 0: parity *= +1; break;
            case 1: parity *= (dir == 0 || dir == 1) ? -1 : +1; break;
            case 2: parity *= (dir == 0 || dir == 2) ? -1 : +1; break;
            case 3: parity *= +1; break;
            case 4: parity *= (dir == 1 || dir == 2) ? -1 : +1; break;
            case 5: parity *= +1; break;
            default: assert (0);
            }
            break;
          case SYMTENSOR3:
            switch (tcomponent % 6) {
            case 0: parity *= +1; break;
            case 1: parity *= (dir == 0 || dir == 1) ? -1 : +1; break;
            case 2: parity *= (dir == 0 || dir == 2) ? -1 : +1; break;
            case 3: parity *= +1; break;
            case 4: parity *= (dir == 1 || dir == 2) ? -1 : +1; break;
            case 5: parity *= +1; break;
            default: assert (0);
            }
            switch (tcomponent / 6) {
            case 0: parity *= dir == 0 ? -1 : +1; break;
            case 1: parity *= dir == 1 ? -1 : +1; break;
            case 2: parity *= dir == 2 ? -1 : +1; break;
            default: assert (0);
            }
            break;
          case TENSOR:
            switch (tcomponent) {
            case 0: parity *= +1; break;
            case 1: parity *= (dir == 0 || dir == 1) ? -1 : +1; break;
            case 2: parity *= (dir == 0 || dir == 2) ? -1 : +1; break;
            case 3: parity *= (dir == 1 || dir == 0) ? -1 : +1; break;
            case 4: parity *= +1; break;
            case 5: parity *= (dir == 1 || dir == 2) ? -1 : +1; break;
            case 6: parity *= (dir == 2 || dir == 0) ? -1 : +1; break;
            case 7: parity *= (dir == 2 || dir == 1) ? -1 : +1; break;
            case 8: parity *= +1; break;
            default: assert (0);
            }
            break;
          case WEYLSCALARS_REAL: {
            static int const weylparities[10][3] =
              {{+1,+1,+1},
               {-1,-1,-1},
               {+1,+1,+1},
               {-1,-1,-1},
               {+1,+1,+1},
               {-1,-1,-1},
               {+1,+1,+1},
               {-1,-1,-1},
               {+1,+1,+1},
               {-1,-1,-1}};
            parity *= weylparities[tcomponent][dir];
            break;
          }
          case MANUALCARTESIAN:
            parity = manual_parities[dir];
            break;
          default:
            assert (0);
          }
          
          /* Find region extent */
          for (d=0; d<3; ++d) {
            lsh[d] = cctkGH->cctk_lsh[d];
            imin[d] = 0;
            imax[d] = cctkGH->cctk_lsh[d];
            ioff[d] = 0;
            idir[d] = 1;
          }
          if (face == 0) {
            imax[dir] = cctkGH->cctk_nghostzones[dir];
            ioff[dir] = (+ 2*cctkGH->cctk_nghostzones[dir] - 1 
                         + (do_stagger[2*dir+face] ? 0 : 1));
            idir[dir] = -1;
          } else {
            imin[dir] = cctkGH->cctk_lsh[dir] - cctkGH->cctk_nghostzones[dir];
            ioff[dir] = (- 2*cctkGH->cctk_nghostzones[dir] + 1
                         - (do_stagger[2*dir+face] ? 0 : 1));
            idir[dir] = -1;
          }
          
          /* Ensure that there are sufficient interior zones, since
             this thorn does not support filling symmetry zones from
             other symmetry zones */
          {
            int const have_points = cctkGH->cctk_gsh[dir];
            int const need_points =
              3 * cctkGH->cctk_nghostzones[dir]
              + !do_stagger[2*dir] + !do_stagger[2*dir+1];
            if (need_points > have_points) {
              CCTK_VWarn (CCTK_WARN_ABORT, __LINE__, __FILE__, CCTK_THORNSTRING,
                          "Cannot apply symmetry boundary zones in the %s %c direction, since there seem to be more symmetry zones than interior zones",
                          (face==0 ? "lower" : "upper"),
                          "xyz"[dir]);
            }
          }
          
          /* Copy region */
          switch (group.vartype) {
            
#define ARGS  varptr, varptr,                   \
              lsh[0], lsh[1], lsh[2],           \
              imin[0], imin[1], imin[2],        \
              imax[0], imax[1], imax[2],        \
              ioff[0], ioff[1], ioff[2],        \
              idir[0], idir[1], idir[2],        \
              parity

#ifdef HAVE_CCTK_INT1
          case CCTK_VARIABLE_INT1:
#ifdef CCTK_INTEGER_PRECISION_1
          case CCTK_VARIABLE_INT:
#endif
            copy_CCTK_INT1 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_INT2
          case CCTK_VARIABLE_INT2:
#ifdef CCTK_INTEGER_PRECISION_2
          case CCTK_VARIABLE_INT:
#endif
            copy_CCTK_INT2 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_INT4
          case CCTK_VARIABLE_INT4:
#ifdef CCTK_INTEGER_PRECISION_4
          case CCTK_VARIABLE_INT:
#endif
            copy_CCTK_INT4 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_INT8
          case CCTK_VARIABLE_INT8:
#ifdef CCTK_INTEGER_PRECISION_8
          case CCTK_VARIABLE_INT:
#endif
            copy_CCTK_INT8 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_REAL4
          case CCTK_VARIABLE_REAL4:
#ifdef CCTK_REAL_PRECISION_4
          case CCTK_VARIABLE_REAL:
#endif
            copy_CCTK_REAL4 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_REAL8
          case CCTK_VARIABLE_REAL8:
#ifdef CCTK_REAL_PRECISION_8
          case CCTK_VARIABLE_REAL:
#endif
            copy_CCTK_REAL8 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_REAL16
          case CCTK_VARIABLE_REAL16:
#ifdef CCTK_REAL_PRECISION_16
          case CCTK_VARIABLE_REAL:
#endif
            copy_CCTK_REAL16 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_COMPLEX8
          case CCTK_VARIABLE_COMPLEX8:
#ifdef CCTK_COMPLEX_PRECISION_8
          case CCTK_VARIABLE_COMPLEX:
#endif
            copy_CCTK_COMPLEX8 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_COMPLEX16
          case CCTK_VARIABLE_COMPLEX16:
#ifdef CCTK_COMPLEX_PRECISION_16
          case CCTK_VARIABLE_COMPLEX:
#endif
            copy_CCTK_COMPLEX16 (ARGS);
            break;
#endif
            
#ifdef HAVE_CCTK_COMPLEX32
          case CCTK_VARIABLE_COMPLEX32:
#ifdef CCTK_COMPLEX_PRECISION_32
          case CCTK_VARIABLE_COMPLEX:
#endif
            copy_CCTK_COMPLEX32 (ARGS);
            break;
#endif
            
#undef ARGS
            
          default:
            CCTK_WARN (0, "Unsupported variable type");
          }
          
        } /* if cctk_bbox */
      } /* if do_reflection */
    } /* for face */
  } /* for dir */
  
  /* Success */
  return 0;
}



/* When CoordBase is used to specify the location of the boundary
   points, then ensure that the CoordBase parameters and this thorn's
   parameters are consistent.  */
static
void
CheckBoundaryParameters (cGH const * restrict const cctkGH,
                         int const vi,
                         int const * restrict const stencil)
{
  DECLARE_CCTK_PARAMETERS;
  
  static int did_check = 0;
  
  int type;                     /* Parameter type */
  void const * ptr;             /* Pointer to parameter value */
  char const * coordtype;       /* CartGrid3D::type */
  
  int dim;                      /* Number of dimensions of vi */
  
  CCTK_INT * restrict nboundaryzones; /* CoordBase boundary location */
  CCTK_INT * restrict is_internal;
  CCTK_INT * restrict is_staggered;
  CCTK_INT * restrict shiftout;
  
  int do_reflection[6];         /* This thorn's parameters */
  int do_stagger[6];
  
  int d;
  int ierr;
  
  
  
  /* Check only once to save time */
  if (did_check) return;
  
  /* Check only for grid functions */
  if (CCTK_GroupTypeFromVarI (vi) != CCTK_GF) return;
  
  did_check = 1;
  
  /* Check whether CartGrid3D is active */
  if (! CCTK_IsThornActive ("CartGrid3D")) return;
  
  /* Check whether CoordBase is used */
  ptr = CCTK_ParameterGet ("type", "CartGrid3D", & type);
  assert (ptr != 0);
  assert (type == PARAMETER_KEYWORD);
  coordtype = * (char const * const *) ptr;
  if (! CCTK_EQUALS (coordtype, "coordbase")) return;
  
  /* Get the boundary specification */
  dim = CCTK_GroupDimFromVarI (vi);
  assert (dim >= 0);
  nboundaryzones = malloc (2*dim * sizeof *nboundaryzones);
  is_internal = malloc (2*dim * sizeof *is_internal);
  is_staggered = malloc (2*dim * sizeof *is_staggered);
  shiftout = malloc (2*dim * sizeof *shiftout);
  ierr = GetBoundarySpecification
    (2*dim, nboundaryzones, is_internal, is_staggered, shiftout);
  assert (! ierr);
  
  /* Reflection symmetry information */
  assert (dim == 3);
  do_reflection[0] = reflection_x;
  do_reflection[1] = reflection_upper_x;
  do_reflection[2] = reflection_y;
  do_reflection[3] = reflection_upper_y;
  do_reflection[4] = reflection_z;
  do_reflection[5] = reflection_upper_z;
  
  do_stagger[0] = avoid_origin_x;
  do_stagger[1] = avoid_origin_upper_x;
  do_stagger[2] = avoid_origin_y;
  do_stagger[3] = avoid_origin_upper_y;
  do_stagger[4] = avoid_origin_z;
  do_stagger[5] = avoid_origin_upper_z;
  
  /* Check the boundary sizes */
  for (d=0; d<6; ++d) {
    if (do_reflection[d]) {
      char const dir = "xyz"[d/2];
      char const * const face = (d%2==0) ? "lower" : "upper";
      if (stencil[d/2] != nboundaryzones[d]) {
        CCTK_VWarn (CCTK_WARN_ABORT,
                    __LINE__, __FILE__, CCTK_THORNSTRING,
                    "The %s %c face is a symmetry boundary.  Since there are %d ghost zones in the %c direction, the corresponding CoordBase boundary width must also be %d.  The boundary width is currently %d.",
                    face, dir,
                    stencil[d/2], dir, stencil[d/2],
                    (int) nboundaryzones[d]);
      }
      if (is_internal[d]) {
        CCTK_VWarn (CCTK_WARN_ABORT,
                    __LINE__, __FILE__, CCTK_THORNSTRING,
                    "The %s %c face is a symmetry boundary.  The corresponding CoordBase boundary must not be internal.",
                    face, dir);
      }
      if (do_stagger[d] != is_staggered[d]) {
        CCTK_VWarn (CCTK_WARN_ABORT,
                    __LINE__, __FILE__, CCTK_THORNSTRING,
                    "The %s %c face is a symmetry boundary.  The symmetry condition and the corresponding CoordBase boundary must either be both staggered or both not staggered.",
                    face, dir);
      }
      if ((do_stagger[d] ? 0 : 1) != shiftout[d]) {
        CCTK_VWarn (CCTK_WARN_ABORT,
                    __LINE__, __FILE__, CCTK_THORNSTRING,
                    "The %s %c face is a symmetry boundary.  If the symmetry condition is staggered, then the corresponding CoordBase shiftout must be 0; otherwise it must be 1.",
                    face, dir);
      }
    }
  }
  
  /* Free memory */
  free (nboundaryzones);
  free (is_internal);
  free (is_staggered);
  free (shiftout);
}



void
ReflectionSymmetry_Apply (CCTK_ARGUMENTS)
{
  DECLARE_CCTK_ARGUMENTS;
  
  int nvars;
  CCTK_INT * restrict indices;
  CCTK_INT * restrict faces;
  CCTK_INT * restrict widths;
  CCTK_INT * restrict tables;
  int vi;
  int dim;
  int * restrict stencil;
  int i;
  int istat;
  int ierr;
  
  if (! cctkGH) {
    CCTK_WARN (0, "Argument cctkGH is NULL");
  }
  
  nvars = Boundary_SelectedGVs (cctkGH, 0, NULL, NULL, NULL, NULL, NULL);
  if (nvars < 0) {
    CCTK_WARN (0, "Internal error in Boundary_SelectedGVs");
  }
  
  if (nvars == 0) {
    /* Nothing to do */
    return;
  }
  
  indices = malloc (nvars * sizeof *indices);
  if (! indices) {
    CCTK_WARN (0, "Out of memory");
  }
  faces = malloc (nvars * sizeof *faces);
  if (! faces) {
    CCTK_WARN (0, "Out of memory");
  }
  widths = malloc (nvars * sizeof *widths);
  if (! widths) {
    CCTK_WARN (0, "Out of memory");
  }
  tables = malloc (nvars * sizeof *tables);
  if (! tables) {
    CCTK_WARN (0, "Out of memory");
  }
  
  istat =  Boundary_SelectedGVs
    (cctkGH, nvars, indices, faces, widths, tables, 0);
  if (istat != nvars) {
    CCTK_WARN (0, "Internal error in Boundary_SelectedGVs");
  }
  
  for (i=0; i<nvars; ++i) {
    vi = indices[i];
    if (vi < 0 || vi >= CCTK_NumVars()) {
      CCTK_WARN (0, "Illegal variable index");
    }
    
    if (widths[i] < 0) {
      CCTK_WARN (0, "Illegal boundary width");
    }
    
    dim = CCTK_GroupDimFromVarI (vi);
    if (dim < 0) {
      CCTK_WARN (0, "Illegal dimension");
    }
    
    stencil = malloc (dim * sizeof *stencil);
    if (! stencil) {
      CCTK_WARN (0, "Out of memory");
    }
    ierr = CCTK_GroupnghostzonesVI (cctkGH, dim, stencil, vi);
    if (ierr) {
      CCTK_WARN (0, "Internal error in CCTK_GroupnghostzonesVI");
    }
    
    CheckBoundaryParameters (cctkGH, vi, stencil);
    
    ierr = BndReflectVI (cctkGH, vi);
    if (ierr) {
      CCTK_WARN (0, "Internal error in BndReflectVI");
    }
    
    free (stencil);
  }
  
  free (indices);
  free (faces);
  free (widths);
  free (tables);
}