aboutsummaryrefslogtreecommitdiff
path: root/src/FlatBoundary.c
blob: 0b8bcb8768233ecc0905f361052ef5d7d1bf2d23 (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
 /*@@
   @file      FlatBoundary.c
   @date      Mon Mar 15 15:09:00 1999
   @author    Gerd Lanfermann, Gabrielle Allen
   @desc 
     Flat boundary conditions for a given GF group or single GF
   @enddesc 
 @@*/

/*#define DEBUG_BOUND*/

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdarg.h>
#include <string.h>

#include "cctk.h"
#include "cctk_Flesh.h"
#include "cctk_parameters.h"
#include "cctk_Comm.h"
#include "cctk_Groups.h"
#include "cctk_GHExtensions.h"
#include "CactusBase/CartGrid3D/src/Symmetry.h"
#include "cctk_Misc.h"
#include "cctk_WarnLevel.h"
#include "cctk_FortranString.h"

void ApplyFlat3D(cGH *GH,int *doBC,int *lsh,int *stencil_size,CCTK_REAL *var);

 /*@@
   @routine    ApplyFlatBC
   @date       Mon Mar 15 15:19:54 1999
   @author     Gerd Lanfermann
   @desc 
     Routine applies the flat (copying) BCs when called from C or 
     Fortran (via wrapper) with the groupname. The actual GF assignment is
     carried out in Fortran by FortranFlatBC.
   @enddesc 
   @calls      FortranFlatBC
   @history 
 
   @endhistory 

@@*/

/* Call with implementation::group notation, eg: ApplyFlatBC(admgerd::metric) */

void ApplyFlatBC(cGH *GH, int *stencil_size, char *name) {

  DECLARE_CCTK_PARAMETERS

  SymmetryGHex *sGHex;       /* the Symmetry GHextension                  */
  int first,last,index;      /* grid function indices                     */
  int type;                  /* type 0 for a group, type 1 for a variable */
  int num;                   /* index number of the group                 */
  int *doBC;                 /* flags if lower/upper BCs are 
				applied (1) or not (0)  
                                indexing is as in bbox: 0 lower 1 upper   */
      
  /* Get the pointer to the SymmetryGHextension */
  sGHex  = (SymmetryGHex*)GH->extensions[CCTK_GHExtensionHandle("Symmetry")];

  /* Allocate memory for doBC */
  doBC = (int *)malloc(2*(GH->cctk_dim)*sizeof(int));

  /* Decide if we have a group or a variable, and get the index */
  /* type = 1 (group), type = 0 (var), type = -1 (neither) */

  num  = CCTK_GroupIndex(name);

  if (num < 0) 
  {
    num = CCTK_VarIndex(name);
    if (num > 0)
    {
      type = 1; /* Variable */
    }
    else
    {
      type = -1;
      CCTK_WARN(0,"Name in ApplyFlatBC is neither a group nor a variable");
    }
  }
  else
  {
    type = 0; /* Group */
  }

  /* Find the first GF index, and the number of GFs */

  if (type == 0)
  {
    first   = CCTK_FirstVarIndexI(num);
    last    = first+CCTK_NumVarsInGroupI(num)-1;
    if (first == -1 || last == -1) 
      CCTK_WARN(0,"Invalid group number used");
  }
  else
  {
    first = num;
    last = num;
  }

  /* Loop over the all GFs */

  for (index=first; index<=last; index++) {

    /* ... check that we actually have a grid function (and not a scalar) */

    if (CCTK_GroupTypeFromVarI(index)==GROUP_GF) {

      /* The rule is: IF we have no symmetries for the lower grid boundary,
	 (GFSym==ESYM_NOSYM or ==ESYM_UNSET) AND we have a lower(upper) bound
	 AND we have enough gridpoints THEN apply BC  */
      int idim;

      for (idim=0;idim<GH->cctk_dim;idim++)
      {
	doBC[idim*2]=(((sGHex->GFSym[index][idim*2]==GFSYM_NOSYM)||
		  (sGHex->GFSym[index][idim*2]==GFSYM_UNSET)) &&
		 GH->cctk_lsh[idim]>1 && GH->cctk_bbox[idim*2]);
	doBC[idim*2+1] = (((sGHex->GFSym[index][idim*2+1]==GFSYM_NOSYM)||
		  (sGHex->GFSym[index][idim*2+1]==GFSYM_UNSET)) &&
		 GH->cctk_lsh[idim]>1 && GH->cctk_bbox[idim*2+1]);
      }


      if (GH->cctk_dim == 3)
      {
	ApplyFlat3D(GH,doBC,GH->cctk_lsh,stencil_size,GH->data[index][0]);
      }
      else
      {
        CCTKi_NotYetImplemented("Flat boundaries in other than 3D");
      }
 
    }
  }  

  if (doBC) free(doBC);
}  

void FMODIFIER FORTRAN_NAME(ApplyFlatBC)(cGH *GH, int *stencil_size, ONE_FORTSTRING_ARG) {

  ONE_FORTSTRING_CREATE(name)
  ApplyFlatBC(GH,stencil_size,name);
  free(name);

} 


void ApplyFlat3D(cGH *GH,int *doBC,int *lsh,int *stencil_size,CCTK_REAL *var)
{

  if (doBC[0] == 1)
  {

    int j,k,sw;

#ifdef DEBUG_BOUND
    printf("Applying flat boundary for lower x\n");
#endif

    for (k=0;k<lsh[2];k++)
    {
      for (j=0;j<lsh[1];j++)
      {
	for (sw=0;sw<stencil_size[0];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,sw,j,k)]
	    = var[CCTK_GFINDEX3D(GH,stencil_size[0],j,k)];
	}
      }
    }
  }
  
  if (doBC[1] == 1)
  {

    int j,k,sw;

#ifdef DEBUG_BOUND
    printf("Applying flat boundary for upper x\n");
#endif
      
    for (k=0;k<lsh[2];k++)
    {
      for (j=0;j<lsh[1];j++)
      {
	for (sw=0;sw<stencil_size[0];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,lsh[0]-sw-1,j,k)]
	    = var[CCTK_GFINDEX3D(GH,lsh[0]-stencil_size[0]-1,j,k)];
	}
      }
    }
  }

  if (doBC[2] == 1)
  {
    int i,k,sw;

#ifdef DEBUG_BOUND
    printf("Applying flat boundary for lower y\n");
#endif

    for (k=0;k<lsh[2];k++)
    {
      for (i=0;i<lsh[0];i++)
      {
	for (sw=0;sw<stencil_size[1];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,i,sw,k)]
	    = var[CCTK_GFINDEX3D(GH,i,stencil_size[1],k)];
	}
      }
    }
  }
  
  if (doBC[3] == 1)
  {
    int i,k,sw;

#ifdef DEBUG_BOUND
    printf("Applying flat boundary for upper y\n");
#endif
      
    for (k=0;k<lsh[2];k++)
    {
      for (i=0;i<lsh[0];i++)
      {
	for (sw=0;sw<stencil_size[1];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,i,lsh[1]-sw-1,k)]
	    = var[CCTK_GFINDEX3D(GH,i,lsh[1]-stencil_size[1]-1,k)];
	}
      }
    }
  }

  if (doBC[4] == 1)
  {
    int i,j,sw;

#ifdef DEBUG_BOUND
    printf("Applying flat boundary for lower z\n");
#endif

    for (j=0;j<lsh[1];j++)
    {
      for (i=0;i<lsh[0];i++)
      {
	for (sw=0;sw<stencil_size[2];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,i,j,sw)]
	    = var[CCTK_GFINDEX3D(GH,i,j,stencil_size[2])];
	}
      }
    }
  }
  
  if (doBC[5] == 1)
  {
    int i,j,sw;
      
#ifdef DEBUG_BOUND
    printf("Applying flat boundary for upper z\n");
#endif

    for (j=0;j<lsh[1];j++)
    {
      for (i=0;i<lsh[0];i++)
      {
	for (sw=0;sw<stencil_size[2];sw++)
	{
	  var[CCTK_GFINDEX3D(GH,i,j,lsh[2]-sw-1)]
	    = var[CCTK_GFINDEX3D(GH,i,j,lsh[2]-stencil_size[2]-1)];
	}
      }
    }
  }

}