aboutsummaryrefslogtreecommitdiff
path: root/src/RobinBoundary.c
blob: 0161d5956badca7d2c00e4422e5b27d66b30e2ee (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
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
 /*@@
   @file      RobinBoundary.c
   @date      July 6th 2000
   @author    Miguel Alcubierre, Gabrielle Allen, Gerd Lanfermann
   @desc
              Routines for Robin boundary conditions
   @enddesc
   @history
   @hdate     Tue 10 Apr 2001
   @hauthor   Thomas Radke
   @hdesc     BC routines generalized for applying to arbitrary CCTK data types
   @endhistory
   @version   $Id$
 @@*/

#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "cctk.h"
#include "util_Table.h"
#include "util_ErrorCodes.h"
#include "cctk_Parameters.h"
#include "cctk_FortranString.h"

#include "Boundary.h"

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

static int ApplyBndRobin (const cGH *GH,
                          const CCTK_INT *stencil,
                          CCTK_REAL finf,
                          int npow,
                          int first_var,
                          int num_vars);
static int OldApplyBndRobin (const cGH *GH,
                             const int *stencil,
                             CCTK_REAL finf,
                             int npow,
                             int first_var,
                             int num_vars);


/********************************************************************
 ********************    External Routines   ************************
 ********************************************************************/
/*@@
   @routine    BndRobin
   @date       14 Feb 2003
   @author     David Rideout
   @desc
               Top level function which is registered as handling
               this boundary condition
   @enddesc
   @calls      ApplyBndRobin
   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        num_vars
   @vdesc      number of variables passed in through var_indices[]
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        var_indices
   @vdesc      array of variable indicies to which to apply this boundary
               condition
   @vtype      CCTK_INT *
   @vio        in
   @endvar
   @var        faces
   @vdesc      array of set of faces to which to apply the bc
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        widths
   @vdesc      array of boundary widths for each variable
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @var        table_handles
   @vdesc      array of table handles which hold extra arguments
   @vtype      CCTK_INT
   @vio        in
   @endvar
   @returntype CCTK_INT
   @returndesc
               return code of @seeroutine ApplyBndRobin
               -21 error reading boundary width array from table
               -22 wrong size boundary width array in table
   @endreturndesc
@@*/

CCTK_INT BndRobin(const cGH *GH, CCTK_INT num_vars, CCTK_INT *vars,
                  CCTK_INT *faces, CCTK_INT *widths, CCTK_INT *tables)
{
  int i, j, k, gi, err, gdim, max_gdim, retval;

  /* variables to pass to ApplyBndRobin */
  CCTK_INT *width_alldirs; /* width of boundary in all directions */
  CCTK_REAL finf;     /* value of function at infinity */
  CCTK_INT npow;           /* decay rate */

#ifdef DEBUG
  printf("BndRobin(): got passed GH=%p, num_vars=%d, var_indices[0]=%d, table_handles[0]=%d\n", (const void *) GH, num_vars, var_indices[0], table_handles[0]);
#endif

  retval = 0; width_alldirs = NULL; max_gdim = 0;

  /* loop through variables, j at a time */
  for (i=0; i<num_vars; i+=j) {
    /* find other adjacent vars which are selected for identical bcs */
    j=1;
    /* Since GFs are allowed to have different staggering, the best we
       can do is find variables of the same group which are selected
       for identical bcs.  If all GFs had the same staggering then we
       could groups many GFs together. */
    gi = CCTK_GroupIndexFromVarI (vars[i]);
    while (i+j<num_vars && vars[i+j]==vars[i]+j &&
           CCTK_GroupIndexFromVarI(vars[i+j])==gi && tables[i+j]==tables[i]
           && faces[i+j]==faces[i] && widths[i+j]==widths[i])
    {
      ++j;
    }

    /* Check to see if faces specification is valid */
    if (faces[i] != CCTK_ALL_FACES)
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Faces specification %d for Robin boundary conditions on "
                 "%s is not implemented yet.  "
                 "Applying Robin bcs to all (external) faces.", faces[i],
                 CCTK_VarName(vars[i]));
    }

    /* Set up default arguments for ApplyBndRobin */
    finf = 0;
    npow = 1;

    /* Look on table for possible non-default arguments
     * (If any of these table look-ups fail, the value will be unchanged
     * from its default value)
     */
    /* Asymptotic value of function at infinity */
    err = Util_TableGetReal(tables[i], &finf, "FINF");
    if (err == UTIL_ERROR_BAD_HANDLE)
    {
      CCTK_VWarn(5, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Invalid table handle passed for Robin boundary "
                 "conditions for %s.  Using all default values.",
                 CCTK_VarName(vars[i]));
    } else
    {
      /* Decay power */
      Util_TableGetInt(tables[i], &npow, "DECAY_POWER");
    }

    /* Determine boundary width on all faces */
    /* allocate memory for buffer */
    gdim = CCTK_GroupDimI(gi);
    if (gdim > max_gdim)
    {
      width_alldirs = (CCTK_INT *) 
	realloc(width_alldirs, 2*gdim*sizeof(CCTK_INT));
      max_gdim = gdim;
    }

    /* fill it with values, either from table or the boundary_width
       parameter */
    if (widths[i]<0)
    {
      err = Util_TableGetIntArray(tables[i], gdim, width_alldirs,
                                  "BOUNDARY_WIDTH");
      if (err<0)
      {
        CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
                   "Error %d when reading boundary width array from table "
                   "for %s", err, CCTK_VarName(vars[i]));
        return -21;
      } else if (err!=2*gdim)
      {
        CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
                   "Boundary width array for %s has %d elements, but %d "
                   "expected", CCTK_VarName(vars[i]), err, 2*gdim);
        return -22;
      }
    } else
    {
      for (k=0; k<2*gdim; ++k)
      {
        width_alldirs[k] = widths[i];
      }
    }

    /* Apply the boundary condition */
    if ((retval = ApplyBndRobin(GH, width_alldirs, finf, npow, vars[i], j)) < 0)
    {
      CCTK_VWarn(1, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "ApplyBndRobin() returned %d", retval);
    }
  }
#ifdef DEBUG
  printf("BndRobin(): returning %d\n",retval);
#endif
  free(width_alldirs);

  return retval;
}

/* prototypes for external C routines are declared in header Boundary.h
   here only follow the fortran wrapper prototypes */
void CCTK_FCALL CCTK_FNAME (BndRobinGI)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            const int *gi);
void CCTK_FCALL CCTK_FNAME (BndRobinGN)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            ONE_FORTSTRING_ARG);
void CCTK_FCALL CCTK_FNAME (BndRobinVI)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL  *finf,
                            const int *npow,
                            const int *vi);
void CCTK_FCALL CCTK_FNAME (BndRobinVN)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            ONE_FORTSTRING_ARG);


/********************************************************************
 ********************    Internal Routines   ************************
 ********************************************************************/

/*@@
   @routine    BndRobinGI
   @date       Tue Jul 18 18:08:28 2000
   @author     Gerd Lanfermann
   @desc
               Apply Robin boundary conditions by group index
   @enddesc
   @calls      ApplyBndRobin

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        stencil
   @vdesc      stencil width array
   @vtype      int [ dimension of group ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infimum
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        gi
   @vdesc      index of group to apply boundary conditions to
   @vtype      int
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplyBndRobin
               -1 if invalid group index was given
   @endreturndesc
@@*/
int BndRobinGI (const cGH *GH,
                const int *stencil,
                CCTK_REAL finf,
                int npow,
                int gi)
{
  int first_vi, retval;


  first_vi = CCTK_FirstVarIndexI (gi);
  if (first_vi >= 0)
  {
    retval = OldApplyBndRobin (GH, stencil, finf, npow, first_vi,
                            CCTK_NumVarsInGroupI (gi));
  }
  else
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid group index %d in BndFlatGI", gi);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (BndRobinGI)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            const int *gi)
{
  *ierr = BndRobinGI (*GH, stencil, *finf, *npow, *gi);
}


/*@@
   @routine    BndRobinGN
   @date       Tue Jul 18 18:08:28 2000
   @author     Gerd Lanfermann
   @desc
               Apply Robin boundary conditions by group name
   @enddesc
   @calls      BndRobinGI

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        stencil
   @vdesc      stencil width array
   @vtype      int [ dimension of group ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infimum
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        gname
   @vdesc      name of group to apply boundary conditions to
   @vtype      const char *
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine BndRobinGI <BR>
               -1 if invalid group index was given
   @endreturndesc
@@*/
int BndRobinGN (const cGH *GH,
                const int *stencil,
                CCTK_REAL finf,
                int npow,
                const char *gname)
{
  int gi, retval;


  gi = CCTK_GroupIndex (gname);
  if (gi >= 0)
  {
    retval = BndRobinGI (GH, stencil, finf, npow, gi);
  }
  else
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid group name '%s' in BndRobinGN", gname);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (BndRobinGN)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE (gname)
  *ierr = BndRobinGN (*GH, stencil, *finf, *npow, gname);
  free (gname);
}


/*@@
   @routine    BndRobinVI
   @date       Tue Jul 18 18:08:28 2000
   @author     Gerd Lanfermann
   @desc
               Apply Robin boundary conditions by variable index
   @enddesc
   @calls      ApplyBndRobin

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        stencil
   @vdesc      stencil width array
   @vtype      int [ dimension of variable ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infimum
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        vi
   @vdesc      index of variable to apply boundary conditions to
   @vtype      const char *
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine ApplyBndRobin <BR>
               -1 if invalid group index was given
   @endreturndesc
@@*/
int BndRobinVI (const cGH *GH,
                const int *stencil,
                CCTK_REAL finf,
                int npow,
                int vi)
{
  int retval;


  if (vi >= 0 && vi < CCTK_NumVars ())
  {
    retval = OldApplyBndRobin (GH, stencil, finf, npow, vi, 1);
  }
  else
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "BndRobinVI: Invalid variable index %d", vi);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (BndRobinVI)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL  *finf,
                            const int *npow,
                            const int *vi)
{
  *ierr = BndRobinVI (*GH, stencil, *finf, *npow, *vi);
}


/*@@
   @routine    BndRobinVN
   @date       Tue Jul 18 18:08:28 2000
   @author     Gerd Lanfermann
   @desc
               Apply Robin boundary conditions by variable name
   @enddesc
   @calls      BndRobinVI

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        stencil
   @vdesc      stencil width array
   @vtype      int [ dimension of variable ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infimum
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        vname
   @vdesc      name of variable to apply boundary conditions to
   @vtype      const char *
   @vio        in
   @endvar

   @returntype int
   @returndesc
               return code of @seeroutine BndRobinVI <BR>
               -1 if invalid group index was given
   @endreturndesc
@@*/
int BndRobinVN (const cGH *GH,
                const int *stencil,
                CCTK_REAL finf,
                int npow,
                const char *vname)
{
  int vi, retval;


  vi = CCTK_VarIndex (vname);
  if (vi >= 0)
  {
    retval = BndRobinVI (GH, stencil, finf, npow, vi);
  }
  else
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Invalid variable name '%s' in BndRobinVN", vname);
    retval = -1;
  }

  return (retval);
}

void CCTK_FCALL CCTK_FNAME (BndRobinVN)
                           (int *ierr,
                            const cGH **GH,
                            const int *stencil,
                            const CCTK_REAL *finf,
                            const int *npow,
                            ONE_FORTSTRING_ARG)
{
  ONE_FORTSTRING_CREATE (vname)
  *ierr = BndRobinVN (*GH, stencil, *finf, *npow, vname);
  free (vname);
}


/********************************************************************
 *********************     Local Routines   *************************
 ********************************************************************/

/* maximum dimension we can deal with */
#define MAXDIM  3

/* macro to compute x*x */
#define SQR(x)  ((x) * (x))


/*@@
   @routine    SET_LINEAR_INDICES
   @date       Thu 7 June 2001
   @author     Thomas Radke
   @desc
               Macro to set the linear indices for the source and destination
               element of the current grid variable
   @enddesc

   @var        i
   @vdesc      x index to use
   @vtype      int
   @vio        in
   @endvar
@@*/
#define SET_LINEAR_INDICES(i)                                                 \
{                                                                             \
  dst = CCTK_GFINDEX3D (GH, i, j, k);                                         \
  src = CCTK_GFINDEX3D (GH, i+dx, j+dy, k+dz);                                \
  distance = dist[abs(dx) + 2*abs (dy) + 4*abs (dz)];                         \
}


/*@@
   @routine    ROBIN_BOUNDARY_TYPED_3D
   @date       Thu 7 June 2001
   @author     Thomas Radke
   @desc
               Macro to apply Robin boundary conditions to a 3D variable
               of given datatype
   @enddesc

   @var        cctk_type
   @vdesc      CCTK datatype of the variable
   @vtype      <cctk_type>
   @vio        in
   @endvar
@@*/
#define ROBIN_BOUNDARY_TYPED_3D(cctk_type)                                    \
{                                                                             \
  cctk_type *data;                                                            \
  double u_src, u_dst, aux;                                                   \
                                                                              \
                                                                              \
  /* avoid the else branch with the expensive sqrt() operation if possible */ \
  if (abs (dx) + abs (dy) + abs (dz) == 1)                                    \
  {                                                                           \
    u_dst = fabs ((double) (dx ? x[dst] : (dy ? y[dst] : z[dst])));           \
    u_src = fabs ((double) (dx ? x[src] : (dy ? y[src] : z[src])));           \
  }                                                                           \
  else                                                                        \
  {                                                                           \
    u_dst = sqrt (SQR (dx * x[dst]) + SQR (dy * y[dst]) + SQR (dz * z[dst])); \
    u_src = sqrt (SQR (dx * x[src]) + SQR (dy * y[src]) + SQR (dz * z[src])); \
  }                                                                           \
                                                                              \
  aux = decay * distance * (u_src + u_dst) / SQR (r[src] + r[dst]);           \
                                                                              \
  data = (cctk_type *) GH->data[var][0];                                      \
  data[dst] = (cctk_type) ((2*aux*finf + data[src]*(1 - aux)) / (1 + aux));   \
}


/*@@
   @routine    ROBIN_BOUNDARY
   @date       Thu 7 June 2001
   @author     Thomas Radke
   @desc
               Macro to apply Robin boundary conditions to a variable
               of a given datatype in all directions
               Currently it is limited up to 3D variables only.
   @enddesc
   @calls      SET_LINEAR_INDICES
               ROBIN_BOUNDARY_TYPED_3D

   @var        cctk_type
   @vdesc      CCTK datatype of the variable
   @vtype      <cctk_type>
   @vio        in
   @endvar
@@*/
#define ROBIN_BOUNDARY(cctk_type)                                             \
{                                                                             \
  int i, j, k;                                                                \
  int dx, dy, dz;                                                             \
  int src, dst;                                                               \
  double distance;                                                            \
                                                                              \
                                                                              \
  /* check the dimensionality */                                              \
  if (gdim != 3)                                                              \
  {                                                                           \
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,                      \
                "ApplyBndRobin: variable dimension of %d not supported",      \
                gdim);                                                        \
    return (-5);                                                              \
  }                                                                           \
                                                                              \
  /* outermost loop over all z points */                                      \
  for (k = 0; k < GH->cctk_lsh[2]; k++)                                       \
  {                                                                           \
    dz = 0;                                                                   \
    if (k == 0 && doBC[4])                                                    \
    {                                                                         \
      dz = +1;                                                                \
    }                                                                         \
    else if (k == GH->cctk_lsh[2]-1 && doBC[5])                               \
    {                                                                         \
      dz = -1;                                                                \
    }                                                                         \
                                                                              \
    /* middle loop over all y points */                                       \
    for (j = 0; j < GH->cctk_lsh[1]; j++)                                     \
    {                                                                         \
      dy = 0;                                                                 \
      if (j == 0 && doBC[2])                                                  \
      {                                                                       \
        dy = +1;                                                              \
      }                                                                       \
      else if (j == GH->cctk_lsh[1]-1 && doBC[3])                             \
      {                                                                       \
        dy = -1;                                                              \
      }                                                                       \
                                                                              \
      /* lower x */                                                           \
      dx = 0;                                                                 \
      if (doBC[0])                                                            \
      {                                                                       \
        dx = +1;                                                              \
      }                                                                       \
      if (dx || dy || dz)                                                     \
      {                                                                       \
        SET_LINEAR_INDICES (0);                                               \
        ROBIN_BOUNDARY_TYPED_3D (cctk_type);                                  \
      }                                                                       \
                                                                              \
      /* lower/upper y and/or z */                                            \
      if (dy || dz)                                                           \
      {                                                                       \
        dx = 0;                                                               \
        SET_LINEAR_INDICES (1);                                               \
        for (i = 1; i < GH->cctk_lsh[0]-1; i++, src++, dst++)                 \
        {                                                                     \
          ROBIN_BOUNDARY_TYPED_3D (cctk_type);                                \
        }                                                                     \
      }                                                                       \
                                                                              \
      /* upper x */                                                           \
      dx = 0;                                                                 \
      if (doBC[1])                                                            \
      {                                                                       \
        dx = -1;                                                              \
      }                                                                       \
      if (dx || dy || dz)                                                     \
      {                                                                       \
        SET_LINEAR_INDICES (GH->cctk_lsh[0]-1);                               \
        ROBIN_BOUNDARY_TYPED_3D (cctk_type);                                  \
      }                                                                       \
    }                                                                         \
  }                                                                           \
}


/*@@
   @routine    ApplyBndRobin
   @date       Tue Jul 18 18:08:28 2000
   @author     Gerd Lanfermann
   @desc
               Apply Robin boundary conditions to a group of grid functions
               given by their indices
               This routine is called by the various BndRobinXXX wrappers.

               Although it is currently limited to handle 3D variables only
               it can easily be extended for higher dimensions
               by adapting the appropriate macros.
   @enddesc

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        in_widths
   @vdesc      boundary width array
   @vtype      CCTK_INT [ dimension of variable ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infinity
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        first_var
   @vdesc      index of first variable to apply boundary conditions to
   @vtype      int
   @vio        in
   @endvar
   @var        num_vars
   @vdesc      number of variables
   @vtype      int
   @vio        in
   @endvar

   @calls      CCTK_VarTypeI
               CCTK_GroupDimFromVarI
               ROBIN_BOUNDARY
   @history
   @hdate      Tue 10 Apr 2001
   @hauthor    Thomas Radke
   @hdesc      Merged separate routines for 1D, 2D, and 3D
               into a single generic routine
   @endhistory

   @returntype int
   @returndesc
                0 for success
               -1 if variable dimension is not supported
               -2 if NULL pointer passed as boundary width array
               -3 if stencil width is other than 1
               -4 if variable type is not supported
               -5 if variable dimension is other than 3D
               -6 if no coordinate information is available
   @endreturndesc
@@*/
static int ApplyBndRobin (const cGH *GH,
                          const CCTK_INT *in_widths,
                          CCTK_REAL finf,
                          int npow,
                          int first_var,
                          int num_vars)
{
  int var, vtype, dim, gdim;
  int doBC[2*MAXDIM];
  CCTK_INT symtable;
  CCTK_INT symbnd[2*MAXDIM];
  CCTK_INT is_physical[2*MAXDIM];
  CCTK_INT ierr;
  char coord_system_name[20];
  double decay;
  const CCTK_REAL *x, *y, *z, *r;
  double dist[8];


  /* get the number of dimensions and the variables' type */
  gdim  = CCTK_GroupDimI (CCTK_GroupIndexFromVarI (first_var));
  vtype = CCTK_VarTypeI (first_var);

  /* make sure we can deal with this number of dimensions */
  if (gdim > MAXDIM)
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "ApplyBndRobin: Variable dimension of %d not supported", gdim);
    return (-1);
  }

  /* check the boundary width */
  if (! in_widths)
  {
    CCTK_WARN (1, "ApplyBndRobin: NULL pointer passed for boundary width "
               "array");
    return (-2);
  }

  for (dim = 0; dim < 2*gdim; dim++)
  {
    if (in_widths[dim] != 1)
    {
      CCTK_WARN (1, "ApplyBndRobin: Stencil width must be 1 "
                    "for Robin boundary conditions");
      return (-3);
    }
  }

  /* Robin boundaries need the underlying grid coordinates */
  sprintf (coord_system_name, "cart%dd", gdim);
  if (CCTK_CoordSystemHandle (coord_system_name) < 0)
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "ApplyBndRobin: Couldn't get coordinates from '%s'",
                coord_system_name);
    return (-6);
  }
  x = GH->data[CCTK_CoordIndex (-1, "x", coord_system_name)][0];
  y = GH->data[CCTK_CoordIndex (-1, "y", coord_system_name)][0];
  z = GH->data[CCTK_CoordIndex (-1, "z", coord_system_name)][0];

  sprintf (coord_system_name, "spher%dd", gdim);
  if (CCTK_CoordSystemHandle (coord_system_name) < 0)
  {
    CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                "ApplyBndRobin: Couldn't get coordinates from '%s'",
                coord_system_name);
    return (-6);
  }
  r = GH->data[CCTK_CoordIndex (-1, "r", coord_system_name)][0];

  /* see if we have a physical boundary */
  symtable = SymmetryTableHandleForGrid (GH);
  if (symtable < 0) CCTK_WARN (0, "internal error");
  ierr = Util_TableGetIntArray (symtable, 2 * gdim, symbnd, "symmetry_handle");
  if (ierr != 2 * gdim) CCTK_WARN (0, "internal error");
  for (dim = 0; dim < 2 * gdim; dim++)
  {
    is_physical[dim] = symbnd[dim] < 0;
  }

  /* get the decay rate as a double */
  decay = (double) npow;

  /* precompute the distance to all 8 neighbors in a 3D grid */
  dist[0] = 0;                                    /* not used */
  dist[1] = GH->cctk_delta_space[0]/GH->cctk_levfac[0];
  dist[2] = GH->cctk_delta_space[1]/GH->cctk_levfac[1];
  dist[3] = sqrt (SQR (dist[1]) + SQR (dist[2]));
  dist[4] = GH->cctk_delta_space[2]/GH->cctk_levfac[2];
  dist[5] = sqrt (SQR (dist[1]) + SQR (dist[4]));
  dist[6] = sqrt (SQR (dist[2]) + SQR (dist[4]));
  dist[7] = sqrt (SQR (dist[1]) + SQR (dist[2]) + SQR (dist[4]));

  /* now loop over all variables */
  for (var = first_var; var < first_var + num_vars; var++)
  {
    /* Apply condition if:
       + boundary is a physical boundary
       + boundary is an outer boundary
       + have enough grid points
    */
    for (dim = 0; dim < 2 * gdim; dim++)
    {
      doBC[dim] = is_physical[dim];
    }
    for (dim = 0; dim < gdim; dim++)
    {
      doBC[dim*2]   &= GH->cctk_lsh[dim] > 1 && GH->cctk_bbox[dim*2];
      doBC[dim*2+1] &= GH->cctk_lsh[dim] > 1 && GH->cctk_bbox[dim*2+1];
    }

    switch (vtype)
    {
      case CCTK_VARIABLE_REAL:
        ROBIN_BOUNDARY (CCTK_REAL); break;

#ifdef CCTK_REAL4
      case CCTK_VARIABLE_REAL4:
        ROBIN_BOUNDARY (CCTK_REAL4); break;
#endif

#ifdef CCTK_REAL8
      case CCTK_VARIABLE_REAL8:
        ROBIN_BOUNDARY (CCTK_REAL8); break;
#endif

#ifdef CCTK_REAL16
      case CCTK_VARIABLE_REAL16:
        ROBIN_BOUNDARY (CCTK_REAL16); break;
#endif

      default:
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "ApplyBndRobin: Unsupported variable type %d for "
                    "variable '%s'", CCTK_VarTypeI (var), CCTK_VarName (var));
        return (-4);
    }
  }

  return(0);
}


/*@@
   @routine    OldApplyBndRobin
   @date       5 May 2003
   @author     David Rideout
   @desc
               The new boundary API expects a 2d-element array for the
               boundary_widths (d=dimension of grid variable), while
               the old API expects a d-element array.  This function
               converts the old array to the new format.
   @enddesc

   @var        GH
   @vdesc      Pointer to CCTK grid hierarchy
   @vtype      const cGH *
   @vio        in
   @endvar
   @var        in_widths
   @vdesc      boundary width array
   @vtype      int [ dimension of variable ]
   @vio        in
   @endvar
   @var        finf
   @vdesc      value of f at infinity
   @vtype      CCTK_REAL
   @vio        in
   @endvar
   @var        npow
   @vdesc      power of decay rate
   @vtype      int
   @vio        in
   @endvar
   @var        first_var
   @vdesc      index of first variable to apply boundary conditions to
   @vtype      int
   @vio        in
   @endvar
   @var        num_vars
   @vdesc      number of variables
   @vtype      int
   @vio        in
   @endvar

   @calls      CCTK_GroupIndexFromVarI
               ApplyBndRobin
   @returntype int
   @returndesc
               returncode from @seeroutine ApplyBndRobin
   @endreturndesc
@@*/
static int OldApplyBndRobin (const cGH *GH,
                             const int *in_widths,
                             CCTK_REAL finf,
                             int npow,
                             int first_var,
                             int num_vars)
{
  int i, dim, retval;
  CCTK_INT *boundary_widths;
  static int warned;

  /* Convert stencil_alldirs to new format */
  dim = CCTK_GroupDimFromVarI(first_var);
  boundary_widths = malloc(2*dim*sizeof(CCTK_INT));
  for (i=0; i<2*dim; ++i)
  {
    boundary_widths[i] = in_widths[i/2];
  }

  /* Bug people for using the old interface */
  if (!warned)
  {
    CCTK_VWarn (2, __LINE__, __FILE__, CCTK_THORNSTRING,
                "Copied older d-element array of boundary widths into the "
                "newer 2d-element format.  Please use the new boundary "
                "interface to avoid this.");
    warned = 1;
  }

  /* Call ApplyBnd... with new boundary width array */
  retval = ApplyBndRobin(GH, boundary_widths, finf, npow, first_var, num_vars);

  free(boundary_widths);
  return retval;
}