aboutsummaryrefslogtreecommitdiff
path: root/src/Symmetry.c
blob: 1cf8544b1a7d260fb4827d7bd2b576957caef0af (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
 /*@@
   @file      SymmetryWrappers.c
   @date      April 2000
   @author    Gerd Lanfermann
   @desc
              Routines to apply the 1/2/3D Symmetries for
              all symmetry domains (octant/bitant/quadrant).
   @enddesc
   @history
   @hdate     Sat 02 Nov 2002
   @hauthor   Thomas Radke
   @hdesc     routines generalized for applying to arbitrary CCTK data types
   @endhistory
   @version   $Id$
 @@*/


#include <stdlib.h>

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

#include "Symmetry.h"

/* the rcs ID and its dummy function to use it */
static const char *rcsid = "$Header$";
CCTK_FILEVERSION(CactusBase_CartGrid3D_Symmetry_c)


/********************************************************************
 ********************* Local Routine Prototypes *********************
 ********************************************************************/
static int ApplySymmetry (const cGH *GH, int gindex, int first_vindex,
                          int numvars);


/********************************************************************
 ******************* Fortran Wrapper Prototypes *********************
 ********************************************************************/
void CCTK_FCALL CCTK_FNAME (CartSymGI) (int *ierr, const cGH **GH, int *gindex);
void CCTK_FCALL CCTK_FNAME (CartSymGN)
                           (int *ierr, const cGH **GH, ONE_FORTSTRING_ARG);
void CCTK_FCALL CCTK_FNAME (CartSymVI) (int *ierr, const cGH **GH, int *vindex);
void CCTK_FCALL CCTK_FNAME (CartSymVN)
                           (int *ierr, const cGH **GH, ONE_FORTSTRING_ARG);


/********************************************************************
 ****************** External Routine Prototypes *********************
 ********************************************************************/
void DecodeSymParameters3D (int sym[6]);
void CartGrid3D_ApplyBC (CCTK_ARGUMENTS);


/*@@
   @routine    CartSymGI
   @date       April 2000
   @author     Gerd Lanfermann
   @desc
               Apply symmetry boundary routines by group index
   @enddesc
   @calls      ApplySymmetry

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        gindex
   @vdesc      index of group to apply symmetry BC
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplySymmetry <BR>
               -1 if invalid group index was given
   @endreturndesc
@@*/
int CartSymGI (const cGH *GH, int gindex)
{
  int numvars, first_vindex, retval;


  numvars = CCTK_NumVarsInGroupI (gindex);
  first_vindex = CCTK_FirstVarIndexI (gindex);
  if (numvars > 0 && first_vindex >= 0)
  {
    char * groupname = CCTK_GroupName (gindex);
    if (!groupname)
      CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error returned from function CCTK_GroupName");
    CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,
                "You should not call the symmetry boundary condition routines for the group \"%s\" through the CartSym* routines any more.  The symmetry boundary conditions are now applied automatically when a physical boundary condition is applied.", groupname);
    free (groupname);
    retval = ApplySymmetry (GH, gindex, first_vindex, numvars);
  }
  else
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid group index %d in CartSymGI", gindex);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (CartSymGI) (int *ierr, const cGH **GH, int *gindex)
{
  *ierr = CartSymGI (*GH, *gindex);
}


/*@@
   @routine    CartSymGN
   @date       April 2000
   @author     Gerd Lanfermann
   @desc
               Apply symmetry boundary routines by group name
   @enddesc
   @calls      ApplySymmetry

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        gname
   @vdesc      name of group to apply symmetry BC
   @vtype      const char *
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplySymmetry <BR>
               -1 if invalid group name was given
   @endreturndesc
@@*/
int CartSymGN (const cGH *GH, const char *gname)
{
  int gindex, retval;


  gindex = CCTK_GroupIndex (gname);
  if (gindex >= 0)
  {
    retval = CartSymGI (GH, gindex);
  }
  else
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid group name '%s' in CartSymGN", gname);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (CartSymGN)
                           (int *ierr, const cGH **GH, ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE (gname)
  *ierr = CartSymGN (*GH, gname);
  free (gname);
}


/*@@
   @routine    CartSymVI
   @date       April 2000
   @author     Gerd Lanfermann
   @desc
               Apply symmetry boundary routines by variable index
   @enddesc
   @calls      ApplySymmetry

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        gindex
   @vdesc      index of variable to apply symmetry BC
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplySymmetry <BR>
               -1 if invalid variable index was given
   @endreturndesc
@@*/
int CartSymVI (const cGH *GH, int vindex)
{
  int retval, gindex;


  gindex = CCTK_GroupIndexFromVarI (vindex);
  if (gindex >= 0)
  {
    char * fullname = CCTK_FullName (vindex);
    if (!fullname)
      CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error returned from function CCTK_FullName");
    CCTK_VWarn (3, __LINE__, __FILE__, CCTK_THORNSTRING,
                "You should not call the symmetry boundary condition routines for the variable \"%s\" through the CartSym* routines any more.  The symmetry boundary conditions are now applied automatically when a physical boundary condition is applied.", fullname);
    free (fullname);
    retval = ApplySymmetry (GH, gindex, vindex, 1);
  }
  else
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid variable index %d in CartSymVI", vindex);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (CartSymVI) (int *ierr, const cGH **GH, int *vindex)
{
  *ierr = CartSymVI (*GH, *vindex);
}


/*@@
   @routine    CartSymVN
   @date       April 2000
   @author     Gerd Lanfermann
   @desc
               Apply symmetry boundary routines by variable name
   @enddesc
   @calls      ApplySymmetry

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        gname
   @vdesc      name of variable to apply symmetry BC
   @vtype      const char *
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplySymmetry <BR>
               -1 if invalid variable name was given
   @endreturndesc
@@*/
int CartSymVN (const cGH *GH, const char *vname)
{
  int vindex, retval;


  vindex = CCTK_VarIndex (vname);
  if (vindex >= 0)
  {
    retval = CartSymVI (GH, vindex);
  }
  else
  {
    CCTK_VWarn (1,__LINE__,__FILE__,CCTK_THORNSTRING,
                "Invalid variable name '%s' in CartSymVN", vname);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (CartSymVN)
                           (int *ierr, const cGH **GH, ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE (vname)
  *ierr = CartSymVN (*GH, vname);
  free (vname);
}


/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/
/* macro to compute the linear index of a 3D point */
#define INDEX_3D(ash, i, j, k)      ((i) + (ash)[0]*((j) + (ash)[1]*(k)))


/*@@
   @routine    SYMMETRY_BOUNDARY
   @date       Sat 02 Nov 2002
   @author     Thomas Radke
   @desc
               Macro to apply symmetry boundary conditions to a variable
               of given datatype
               Currently it is limited up to 3D variables only.
   @enddesc

   @var        cctk_type
   @vdesc      CCTK datatype of the variable
   @vtype      <cctk_type>
   @vio        in
   @endvar
   @var        itype
   @vdesc      integral CCTK datatype of the variable (used for typecasting)
   @vtype      <cctk_type>
   @vio        in
   @endvar
@@*/
#define APPLY_LOWER(dir, ii, jj, kk, jjend, kkend, iii, jjj, kkk, itype)      \
{                                                                             \
  for (kk = 0; kk < kkend; kk++)                                              \
  {                                                                           \
    for (jj = 0; jj < jjend; jj++)                                            \
    {                                                                         \
      for (ii = 0; ii < gdata.nghostzones[dir/2]; ii++)                       \
      {                                                                       \
        _var[INDEX_3D (ash, i, j, k)] NUMBER_PART                             \
                    = (itype)(GFSym[vindex][dir]                              \
                      * _var[INDEX_3D (ash, iii, jjj, kkk)] NUMBER_PART);     \
      }                                                                       \
    }                                                                         \
  }                                                                           \
}

#define APPLY_UPPER(dir, ii, jj, kk, jjend, kkend, iii, jjj, kkk, itype)      \
{                                                                             \
  for (kk = 0; kk < kkend; kk++)                                              \
  {                                                                           \
    for (jj = 0; jj < jjend; jj++)                                            \
    {                                                                         \
      for (ii = lsh[dir/2]-gdata.nghostzones[dir/2]; ii < lsh[dir/2]; ii++)   \
      {                                                                       \
        _var[INDEX_3D (ash, i, j, k)] NUMBER_PART                             \
                     = (itype)(GFSym[vindex][dir]                             \
                       * _var[INDEX_3D (ash, iii, jjj, kkk)] NUMBER_PART);    \
      }                                                                       \
    }                                                                         \
  }                                                                           \
}


#define SYMMETRY_BOUNDARY(cctk_type, itype)                                   \
{                                                                             \
  cctk_type *_var = GH->data[vindex][0];                                      \
                                                                              \
  switch (group_static_data.dim)                                              \
  {                                                                           \
    case 3:                                                                   \
  /* apply symmetry to the z faces */                                         \
  if (doSym[4] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_LOWER (4, k, j, i, lsh[1], lsh[0], i, j, offset[4]-k, itype);       \
  }                                                                           \
  if (doSym[5] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_UPPER (5, k, j, i, lsh[1], lsh[0], i, j, offset[5]-k, itype);       \
  }                                                                           \
  if (doSym[4] == GFSYM_ROTATION_X)                                           \
  {                                                                           \
    APPLY_LOWER (4, k, j, i, lsh[1], lsh[0], i, lsh[1]-j-1, offset[4]-k, itype);\
  }                                                                           \
  if (doSym[5] == GFSYM_ROTATION_X)                                           \
  {                                                                           \
    APPLY_UPPER (5, k, j, i, lsh[1], lsh[0], i, lsh[1]-j-1, offset[5]-k, itype);\
  }                                                                           \
  if (doSym[4] == GFSYM_ROTATION_Y)                                           \
  {                                                                           \
    APPLY_LOWER (4, k, j, i, lsh[1], lsh[0], lsh[0]-i-1, j, offset[4]-k, itype);\
  }                                                                           \
  if (doSym[5] == GFSYM_ROTATION_Y)                                           \
  {                                                                           \
    APPLY_UPPER (5, k, j, i, lsh[1], lsh[0], lsh[0]-i-1, j, offset[5]-k, itype);\
  }                                                                           \
  /* FALL THROUGH */                                                          \
    case 2:                                                                   \
  /* apply symmetry to the y faces */                                         \
  if (doSym[2] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_LOWER (2, j, k, i, lsh[2], lsh[0], i, offset[2]-j, k, itype);       \
  }                                                                           \
  if (doSym[3] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_UPPER (3, j, k, i, lsh[2], lsh[0], i, offset[3]-j, k, itype);       \
  }                                                                           \
  if (doSym[2] == GFSYM_ROTATION_Z)                                           \
  {                                                                           \
    APPLY_LOWER (2, j, k, i, lsh[2], lsh[0], lsh[0]-i-1, offset[2]-j, k, itype);\
  }                                                                           \
  if (doSym[3] == GFSYM_ROTATION_Z)                                           \
  {                                                                           \
    APPLY_UPPER (3, j, k, i, lsh[2], lsh[0], lsh[0]-i-1, offset[3]-j, k, itype);\
  }                                                                           \
  if (group_static_data.dim > 2)                                              \
  {                                                                           \
    if (doSym[2] == GFSYM_ROTATION_X)                                         \
    {                                                                         \
      APPLY_LOWER (2, j, k, i, lsh[2], lsh[0], i, offset[2]-j, lsh[2]-k-1, itype);\
    }                                                                         \
    if (doSym[3] == GFSYM_ROTATION_X)                                         \
    {                                                                         \
      APPLY_UPPER (3, j, k, i, lsh[2], lsh[0], i, offset[3]-j, lsh[2]-k-1, itype);\
    }                                                                         \
  }                                                                           \
  /* FALL THROUGH */                                                          \
    case 1:                                                                   \
  /* apply symmetry to the x faces */                                         \
  if (doSym[0] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_LOWER (0, i, j, k, lsh[1], lsh[2], offset[0]-i, j, k, itype);       \
  }                                                                           \
  if (doSym[1] == GFSYM_REFLECTION)                                           \
  {                                                                           \
    APPLY_UPPER (1, i, j, k, lsh[1], lsh[2], offset[1]-i, j, k, itype);       \
  }                                                                           \
  if (group_static_data.dim > 1)                                              \
  {                                                                           \
    if (doSym[0] == GFSYM_ROTATION_Z)                                         \
    {                                                                         \
      APPLY_LOWER (0, i, j, k, lsh[1], lsh[2], offset[0]-i, lsh[1]-j-1, k, itype);\
    }                                                                         \
    if (doSym[1] == GFSYM_ROTATION_Z)                                         \
    {                                                                         \
      APPLY_UPPER (1, i, j, k, lsh[1], lsh[2], offset[1]-i, lsh[1]-j-1, k, itype);\
    }                                                                         \
  }                                                                           \
  if (group_static_data.dim > 2)                                              \
  {                                                                           \
    if (doSym[0] == GFSYM_ROTATION_Y)                                         \
    {                                                                         \
      APPLY_LOWER (0, i, j, k, lsh[1], lsh[2], offset[0]-i, j, lsh[2]-k-1, itype);\
    }                                                                         \
    if (doSym[1] == GFSYM_ROTATION_Y)                                         \
    {                                                                         \
      APPLY_UPPER (1, i, j, k, lsh[1], lsh[2], offset[1]-i, j, lsh[2]-k-1, itype);\
    }                                                                         \
  }                                                                           \
  /* FALL THROUGH */                                                          \
    default:                                                                  \
      ;                                                                       \
  }                                                                           \
}

/* Function to apply above macros. */
#define SYMMETRY_FUNCTION(cctk_type,integral_type,SUFFIX)                     \
  static void cctk_type ## _SymBC ## SUFFIX(const cGH *GH, const int vindex, const int *doSym, const int *offset, const int *lsh, const int *ash, const cGroup group_static_data, const cGroupDynamicData gdata, int **GFSym) { int i,j,k; SYMMETRY_BOUNDARY(cctk_type, integral_type); }

/* Create functions to apply macros.
 * This is much easier for the optiser to deal with.
 * E.g. on some machines we can't compile this file if we use the macros
 * directly in the switch statement in ApplySymmetry.
 */

#define NUMBER_PART .Re
SYMMETRY_FUNCTION(CCTK_COMPLEX,CCTK_REAL,R)
#ifdef HAVE_CCTK_COMPLEX8
SYMMETRY_FUNCTION(CCTK_COMPLEX8,CCTK_REAL4,R)
#endif
#ifdef HAVE_CCTK_COMPLEX16
SYMMETRY_FUNCTION(CCTK_COMPLEX16,CCTK_REAL8,R)
#endif
#ifdef HAVE_CCTK_COMPLEX32
SYMMETRY_FUNCTION(CCTK_COMPLEX32,CCTK_REAL16,R)
#endif
#undef NUMBER_PART

#define NUMBER_PART .Im
SYMMETRY_FUNCTION(CCTK_COMPLEX,CCTK_REAL,I)
#ifdef HAVE_CCTK_COMPLEX8
SYMMETRY_FUNCTION(CCTK_COMPLEX8,CCTK_REAL4,I)
#endif
#ifdef HAVE_CCTK_COMPLEX16
SYMMETRY_FUNCTION(CCTK_COMPLEX16,CCTK_REAL8,I)
#endif
#ifdef HAVE_CCTK_COMPLEX32
SYMMETRY_FUNCTION(CCTK_COMPLEX32,CCTK_REAL16,I)
#endif
#undef NUMBER_PART

#define NUMBER_PART 
SYMMETRY_FUNCTION(CCTK_BYTE,CCTK_BYTE,R)
SYMMETRY_FUNCTION(CCTK_INT,CCTK_INT,R)
#ifdef HAVE_CCTK_INT1
SYMMETRY_FUNCTION(CCTK_INT1,CCTK_INT1,R)
#endif
#ifdef HAVE_CCTK_INT2
SYMMETRY_FUNCTION(CCTK_INT2,CCTK_INT2,R)
#endif
#ifdef HAVE_CCTK_INT4
SYMMETRY_FUNCTION(CCTK_INT4,CCTK_INT4,R)
#endif
#ifdef HAVE_CCTK_INT8
SYMMETRY_FUNCTION(CCTK_INT8,CCTK_INT8,R)
#endif
SYMMETRY_FUNCTION(CCTK_REAL,CCTK_REAL,R)
#ifdef HAVE_CCTK_REAL4
SYMMETRY_FUNCTION(CCTK_REAL4,CCTK_REAL4,R)
#endif
#ifdef HAVE_CCTK_REAL8
SYMMETRY_FUNCTION(CCTK_REAL8,CCTK_REAL8,R)
#endif
#ifdef HAVE_CCTK_REAL16
SYMMETRY_FUNCTION(CCTK_REAL16,CCTK_REAL16,R)
#endif
#undef NUMBER_PART

#define CALL_SYMBC(cctk_type,SUFFIX) cctk_type ## _SymBC ## SUFFIX(GH, vindex, doSym, offset, lsh, ash, group_static_data, gdata, GFSym)

/*@@
   @routine    ApplySymmetry
   @date       Thu Mar  2 11:02:10 2000
   @author     Gerd Lanfermann
   @desc
               Apply symmetry boundary conditions to a group of grid variables
               This routine is called by the various CartSymXXX wrappers.
   @enddesc

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        gindex
   @vdesc      group index of the variables to apply symmetry BCs
   @vtype      int
   @vio        in
   @endvar
   @var        first_var
   @vdesc      index of first variable to apply symmetry BCs
   @vtype      int
   @vio        in
   @endvar
   @var        num_vars
   @vdesc      number of variables
   @vtype      int
   @vio        in
   @endvar

   @calls      CCTK_GroupData
               CCTK_GroupDynamicData
               SYMMETRY_BOUNDARY
   @history
   @hdate      Sat 02 Nov 2002
   @hauthor    Thomas Radke
   @hdesc      Merged separate routines for 1D, 2D, and 3D
               into a single generic routine
   @endhistory

   @returntype int
   @returndesc
                0 for success, or<BR>
               -1 if group dimension is not supported<BR>
               -2 if group datatype is not supported
   @endreturndesc
@@*/
static int ApplySymmetry (const cGH *GH, int gindex, int first_vindex,
                          int numvars)
{
  int i, dim, vindex, retval;
  int **GFSym;
  int domainsym[2*MAX_DIM], doSym[2*MAX_DIM], offset[2*MAX_DIM];
  int lsh[MAX_DIM], ash[MAX_DIM], cntstag[MAX_DIM];
  cGroup group_static_data;
  cGroupDynamicData gdata;
  DECLARE_CCTK_PARAMETERS


  DecodeSymParameters3D (domainsym);

  /* check if any symmetries are to be applied */
  for (i = 0; i < 2*MAX_DIM; i++)
  {
    if (domainsym[i])
    {
      break;
    }
  }
  if (i == 2*MAX_DIM)
  {
    return (0);
  }

  /* get the group's static and dynamic data structure */
  CCTK_GroupData (gindex, &group_static_data);
  CCTK_GroupDynamicData (GH, gindex, &gdata);
  if (group_static_data.dim <= 0 || group_static_data.dim > MAX_DIM)
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "ApplySymmetry: group dimension must 1, 2, or 3");
    return (-1);
  }

  /* Avoid origin?  Default is yes */
  cntstag[0] = no_origin && no_originx && avoid_origin && avoid_originx;
  cntstag[1] = no_origin && no_originy && avoid_origin && avoid_originy;
  cntstag[2] = no_origin && no_originz && avoid_origin && avoid_originz;

  /* initialize array for variables with less dimensions than MAX_DIM
     so that we can use the INDEX_3D macro later on */
  for (i = 0; i < MAX_DIM; i++)
  {
    if (i < group_static_data.dim)
    {
      lsh[i] = gdata.lsh[i];
      ash[i] = gdata.ash[i];
    }
    else
    {
      lsh[i] = 1;
      ash[i] = 1;
    }
    offset[2*i+0] = 2*gdata.nghostzones[i] - cntstag[i];
    offset[2*i+1] = 2*(lsh[i]-1) - offset[2*i+0];
  }

  GFSym = ((SymmetryGHex *) CCTK_GHExtension (GH, "Symmetry"))->GFSym;

  /* Apply Symmetries if:
     + the Symmetry is activated (== NOT NOSYM)
     + the Symmetry is set (== NOT UNSET)
     + the length in the direction is more than 1 grid point
     + the processor has a lower/upper physical boundary.
     Whether a grid allows a symmetry along a direction (e.g. octant=all)
     is part if the Symmetry Setup process.
  */
  retval = 0;
  for (vindex = first_vindex; vindex < first_vindex + numvars; vindex++)
  {
    for (dim = 0; dim < 2*group_static_data.dim; dim++)
    {
      if (GFSym[vindex][dim] == GFSYM_UNSET)
      {
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Symmetries unspecified for '%s'", CCTK_VarName (vindex));
      }

      doSym[dim] = (GFSym[vindex][dim] != GFSYM_NOSYM &&
                    GFSym[vindex][dim] != GFSYM_UNSET &&
                    lsh[dim/2] > 1 && GH->cctk_bbox[dim]) ? domainsym[dim] : 0;
    }

    switch (group_static_data.vartype)
    {
      case CCTK_VARIABLE_BYTE:
        CALL_SYMBC(CCTK_BYTE,R); break;

      case CCTK_VARIABLE_INT:
        CALL_SYMBC(CCTK_INT,R); break;

      case CCTK_VARIABLE_REAL:
        CALL_SYMBC(CCTK_REAL,R); break;

      case CCTK_VARIABLE_COMPLEX:
        CALL_SYMBC(CCTK_COMPLEX,R);
        CALL_SYMBC(CCTK_COMPLEX,I); break;
        
#ifdef HAVE_CCTK_INT1
      case CCTK_VARIABLE_INT1:
        CALL_SYMBC(CCTK_INT1,R); break;
#endif

#ifdef HAVE_CCTK_INT2
      case CCTK_VARIABLE_INT2:
        CALL_SYMBC(CCTK_INT2,R); break;
#endif

#ifdef HAVE_CCTK_INT4
      case CCTK_VARIABLE_INT4:
        CALL_SYMBC(CCTK_INT4,R); break;
#endif

#ifdef HAVE_CCTK_INT8
      case CCTK_VARIABLE_INT8:
        CALL_SYMBC(CCTK_INT8,R); break;
#endif

#ifdef HAVE_CCTK_REAL4
      case CCTK_VARIABLE_REAL4:
        CALL_SYMBC(CCTK_REAL4,R); break;

      case CCTK_VARIABLE_COMPLEX8:
        CALL_SYMBC(CCTK_COMPLEX8,R);
        CALL_SYMBC(CCTK_COMPLEX8,I); break;
#endif

#ifdef HAVE_CCTK_REAL8
      case CCTK_VARIABLE_REAL8:
        CALL_SYMBC(CCTK_REAL8,R); break;

      case CCTK_VARIABLE_COMPLEX16:
        CALL_SYMBC(CCTK_COMPLEX16,R);
        CALL_SYMBC(CCTK_COMPLEX16,I); break;
#endif

#ifdef HAVE_CCTK_REAL16
      case CCTK_VARIABLE_REAL16:
        CALL_SYMBC(CCTK_REAL16,R); break;

      case CCTK_VARIABLE_COMPLEX32:
        CALL_SYMBC(CCTK_COMPLEX32,R);
        CALL_SYMBC(CCTK_COMPLEX32,I); break;
#endif

      default:
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Unsupported variable type %d for variable '%s'",
                    CCTK_VarTypeI (vindex), CCTK_VarName (vindex));
        retval = -2;
    }
  }

  return (retval);
}


/*@@
   @routine    CartGrid3D_ApplyBC
   @date       Sat Feb 07
   @author     Erik Schnetter
   @desc       Apply the symmetry boundary conditions
   @enddesc 
@@*/

void CartGrid3D_ApplyBC (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 gi;
  int i;
  int ierr;
  
  if (!cctkGH)
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "cctkGH undeclared");
  
  nvars = Boundary_SelectedGVs (cctkGH, 0, 0, 0, 0, 0, 0);
  if (nvars<0)
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error returned from function Boundary_selectedGVs");
  
  indices = malloc (nvars * sizeof *indices);
  if(! (nvars==0 || indices))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  faces = malloc (nvars * sizeof *faces);
  if(! (nvars==0 || faces))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  widths = malloc (nvars * sizeof *widths);
  if(! (nvars==0 || widths))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  tables = malloc (nvars * sizeof *tables);
  if(! (nvars==0 || tables))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  
  ierr =  Boundary_SelectedGVs
    (cctkGH, nvars, indices, faces, widths, tables, 0);
  if(! (ierr == nvars))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  
  for (i=0; i<nvars; ++i) {
    vi = indices[i];
    if(! (vi>=0 && vi<CCTK_NumVars()))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
    
    gi = CCTK_GroupIndexFromVarI (vi);
    if( (gi<0))
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
    
    ierr = ApplySymmetry (cctkGH, gi, vi, 1);
    if(ierr)
    CCTK_VWarn ( 0, __LINE__, __FILE__, "CartGrid3D", "error in function CartGrid3D_ApplyBC");
  }
  
  free (indices);
  free (faces);
  free (widths);
  free (tables);
}