aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/SetupGH.cc
blob: 89d73343e005869048ccd2738b1a95b896a400c1 (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
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

#include <iostream>
#include <sstream>
#include <vector>

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

#include "bbox.hh"
#include "defs.hh"
#include "dist.hh"
#include "ggf.hh"
#include "gh.hh"
#include "vect.hh"

#include "carpet.hh"

extern "C" {
  static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/Carpet/src/SetupGH.cc,v 1.53 2003/09/19 16:08:37 schnetter Exp $";
  CCTK_FILEVERSION(Carpet_Carpet_SetupGH_cc);
}



namespace Carpet {
  
  using namespace std;
  
  
  
  static bool CanTransferVariableType (cGH* cgh, const int group)
  {
    // Find out which types correspond to the default types
#if CCTK_INTEGER_PRECISION_1
#  define CCTK_DEFAULT_INTEGER_TYPE CCTK_VARIABLE_INT1
#elif CCTK_INTEGER_PRECISION_2
#  define CCTK_DEFAULT_INTEGER_TYPE CCTK_VARIABLE_INT2
#elif CCTK_INTEGER_PRECISION_4
#  define CCTK_DEFAULT_INTEGER_TYPE CCTK_VARIABLE_INT4
#elif CCTK_INTEGER_PRECISION_8
#  define CCTK_DEFAULT_INTEGER_TYPE CCTK_VARIABLE_INT8
#else
#  error "Unsupported default integer type"
#endif
    
#if CCTK_REAL_PRECISION_4
#  define CCTK_DEFAULT_REAL_TYPE CCTK_VARIABLE_REAL4
#  define CCTK_DEFAULT_COMPLEX_TYPE CCTK_VARIABLE_COMPLEX8
#elif CCTK_REAL_PRECISION_8
#  define CCTK_DEFAULT_REAL_TYPE CCTK_VARIABLE_REAL8
#  define CCTK_DEFAULT_COMPLEX_TYPE CCTK_VARIABLE_COMPLEX16
#elif CCTK_REAL_PRECISION_16
#  define CCTK_DEFAULT_REAL_TYPE CCTK_VARIABLE_REAL16
#  define CCTK_DEFAULT_COMPLEX_TYPE CCTK_VARIABLE_COMPLEX32
#else
#  error "Unsupported default real type"
#endif
    
    if (CCTK_NumVarsInGroupI(group) == 0) return true;
    
    const int var0 = CCTK_FirstVarIndexI(group);
    const int type0 = CCTK_VarTypeI(var0);
    int type1;
    switch (type0) {
    case CCTK_VARIABLE_INT:
      type1 = CCTK_DEFAULT_INTEGER_TYPE;
      break;
    case CCTK_VARIABLE_REAL:
      type1 = CCTK_DEFAULT_REAL_TYPE;
      break;
    case CCTK_VARIABLE_COMPLEX:
      type1 = CCTK_DEFAULT_COMPLEX_TYPE;
      break;
    default:
      type1 = type0;
    }
    switch (type1) {
      
#ifdef CCTK_REAL8
    case CCTK_VARIABLE_REAL8:
      // This type is supported.
      return true;
#endif
      
#ifdef CCTK_REAL4
    case CCTK_VARIABLE_REAL4:
#endif
#ifdef CCTK_REAL16
    case CCTK_VARIABLE_REAL16:
#endif
#ifdef CCTK_REAL4 /* CCTK_COMPLEX8 */
    case CCTK_VARIABLE_COMPLEX8:
#endif
#ifdef CCTK_REAL8 /* CCTK_COMPLEX16 */
    case CCTK_VARIABLE_COMPLEX16:
#endif
#ifdef CCTK_REAL16 /* CCTK_COMPLEX32 */
    case CCTK_VARIABLE_COMPLEX32:
#endif
      // This type is not supported, but could be.
      return false;
      
    case CCTK_VARIABLE_BYTE:
#ifdef CCTK_INT1
    case CCTK_VARIABLE_INT1:
#endif
#ifdef CCTK_INT2
    case CCTK_VARIABLE_INT2:
#endif
#ifdef CCTK_INT4
    case CCTK_VARIABLE_INT4:
#endif
#ifdef CCTK_INT8
    case CCTK_VARIABLE_INT8:
#endif
      // This type is not supported, and cannot be.
      return false;
      
    default:
      {
        CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "Internal error: encountered variable type %d (%s) for group %d (%s)",
                    type1, CCTK_VarTypeName(type1),
                    group, CCTK_GroupName(group));
      }
    }
    
    // not reached
    return false;
  }
  
  
  
  void* SetupGH (tFleshConfig* fc, int convLevel, cGH* cgh)
  {
    DECLARE_CCTK_PARAMETERS;
    
    int ierr;
    
    assert (cgh->cctk_dim == dim);
    
    // Not sure what to do with that
    assert (convLevel==0);
    
    dist::pseudoinit();
    
    CCTK_VInfo (CCTK_THORNSTRING,
		"Carpet is running on %d processors", CCTK_nProcs(cgh));
    
    Waypoint ("starting SetupGH...");
    
    // Refinement information
    maxreflevels = max_refinement_levels;
    reffact = refinement_factor;
    maxreflevelfact = ipow(reffact, maxreflevels-1);
    
    // Multigrid information
    mglevels = multigrid_levels;
    mgfact = multigrid_factor;
    maxmglevelfact = ipow(mgfact, mglevels-1);
    
    // Ghost zones
    vect<int,dim> lghosts, ughosts;
    if (ghost_size == -1) {
      lghosts = vect<int,dim>(ghost_size_x, ghost_size_y, ghost_size_z);
      ughosts = vect<int,dim>(ghost_size_x, ghost_size_y, ghost_size_z);
    } else {
      lghosts = vect<int,dim>(ghost_size, ghost_size, ghost_size);
      ughosts = vect<int,dim>(ghost_size, ghost_size, ghost_size);
    }
    
    // Grid size
    const int stride = maxreflevelfact;
    vect<int,dim> npoints;
    if (global_nsize == -1) {
      npoints = vect<int,dim>(global_nx, global_ny, global_nz);
    } else {
      npoints = vect<int,dim>(global_nsize, global_nsize, global_nsize);
    }
    // Sanity check
    // (if this fails, someone requested an insane amount of memory)
    assert (all(npoints <= INT_MAX));
    {
      int max = INT_MAX;
      for (int d=0; d<dim; ++d) {
        assert (npoints[d] <= max);
        max /= npoints[d];
      }
    }
    
    const vect<int,dim> str(stride);
    const vect<int,dim> lb(0);
    const vect<int,dim> ub((npoints - 1) * str);
    
    const bbox<int,dim> baseext(lb, ub, str);
    
    // Allocate grid hierarchy
    hh = new gh<dim>(refinement_factor, vertex_centered,
		     multigrid_factor, vertex_centered,
		     baseext);
    
    // Allocate time hierarchy
    tt = new th<dim>(hh, 1.0);
    
    // Allocate data hierarchy
    dd = new dh<dim>(*hh, lghosts, ughosts,
                     prolongation_order_space, buffer_width);
    
    if (max_refinement_levels > 1) {
      const int prolongation_stencil_size = dd->prolongation_stencil_size();
      const int min_nghosts
	= ((prolongation_stencil_size + refinement_factor - 1)
	   / (refinement_factor-1));
      if (any(min(lghosts,ughosts) < min_nghosts)) {
	CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
		    "There are not enough ghost zones for the desired spatial prolongation order.  With Carpet::prolongation_order_space=%d, you need at least %d ghost zones.",
		    prolongation_order_space, min_nghosts);
      }
    }
    
    // Allocate space for groups
    arrdata.resize(CCTK_NumGroups());
    
    // Allocate space for variables in group (but don't enable storage
    // yet)
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      
      cGroup gp;
      ierr = CCTK_GroupData (group, &gp);
      assert (!ierr);
      
      switch (gp.grouptype) {
	
      case CCTK_SCALAR:
      case CCTK_ARRAY: {
	
	int disttype;
	vect<int,dim> sizes(1), ghostsizes(0);
	
	switch (gp.grouptype) {
	  
	case CCTK_SCALAR:
	  // treat scalars as DIM=0, DISTRIB=const arrays
	  arrdata[group].info.dim = 0;
	  disttype = CCTK_DISTRIB_CONSTANT;
	  break;
	  
	case CCTK_ARRAY: {
	  assert (gp.dim>=1 || gp.dim<=dim);
	  arrdata[group].info.dim = gp.dim;
	  disttype = gp.disttype;
	  const CCTK_INT * const * const sz  = CCTK_GroupSizesI(group);
	  const CCTK_INT * const * const gsz = CCTK_GroupGhostsizesI(group);
	  for (int d=0; d<gp.dim; ++d) {
	    if (sz) sizes[d] = *sz[d];
	    if (gsz) ghostsizes[d] = *gsz[d];
	  }
	  break;
	}
	  
	default:
	  assert (0);
	}
	
	assert (disttype==CCTK_DISTRIB_CONSTANT
		|| disttype==CCTK_DISTRIB_DEFAULT);
	
	if (disttype==CCTK_DISTRIB_CONSTANT) {
	  sizes[dim-1] = (sizes[dim-1] - 2*ghostsizes[dim-1]) * CCTK_nProcs(cgh) + 2*ghostsizes[dim-1];
	}
	
	const vect<int,dim> alb(0);
	const vect<int,dim> aub(sizes-1);
	const vect<int,dim> astr(1);
	const bbox<int,dim> arrext(alb, aub, astr);
	
	arrdata[group].hh = new gh<dim>(refinement_factor, vertex_centered,
					multigrid_factor, vertex_centered,
					arrext);
	
	arrdata[group].tt = new th<dim>(arrdata[group].hh, 1.0);
	
	vect<int,dim> alghosts(0), aughosts(0);
	for (int d=0; d<gp.dim; ++d) {
	  alghosts[d] = ghostsizes[d];
	  aughosts[d] = ghostsizes[d];
	}
	
	arrdata[group].dd
	  = new dh<dim>(*arrdata[group].hh, alghosts, aughosts, 0, 0);
	
	// Set refinement structure for scalars and arrays:
	
	// Set the basic extent
	vector<bbox<int,dim> >          bbs;
	vector<vect<vect<bool,2>,dim> > obs;
	bbs.push_back (arrdata[group].hh->baseextent);
	obs.push_back (vect<vect<bool,2>,dim>(vect<bool,2>(true)));
	
	// Split it into components, one for each processor
	SplitRegions_AlongZ (cgh, bbs, obs);
	
	// For all refinement levels (but there is only one)
	vector<vector<bbox<int,dim> > >          bbss(1);
	vector<vector<vect<vect<bool,2>,dim> > > obss(1);
	bbss[0] = bbs;
	obss[0] = obs;
	
	// For all multigrid levels
	gh<dim>::rexts bbsss;
	bbsss = hh->make_multigrid_boxes(bbss, mglevels);
	
	// Distribute onto processors
	vector<vector<int> > pss;
	MakeProcessors (cgh, bbsss, pss);
	
	// And recompose.  Done.
        char * groupname = CCTK_GroupName (group);
        assert (groupname);
        Checkpoint ("Recomposing grid array group %s", groupname);
        free (groupname);
	arrdata[group].hh->recompose (bbsss, obss, pss);
	
	break;
      }
	
      case CCTK_GF: {
	assert (gp.dim == dim);
	arrdata[group].info.dim = dim;
	arrdata[group].hh = hh;
	arrdata[group].tt = tt;
	arrdata[group].dd = dd;
	break;
      }
	
      default:
	assert (0);
      }
      
      arrdata[group].info.gsh         = new int [dim];
      arrdata[group].info.lsh         = new int [dim];
      arrdata[group].info.lbnd        = new int [dim];
      arrdata[group].info.ubnd        = new int [dim];
      arrdata[group].info.bbox        = new int [2*dim];
      arrdata[group].info.nghostzones = new int [dim];
      
      arrdata[group].data.resize(CCTK_NumVarsInGroupI(group));
      for (int var=0; var<(int)arrdata[group].data.size(); ++var) {
	arrdata[group].data[var] = 0;
      }
      
      arrdata[group].do_transfer = CanTransferVariableType (cgh, group);
      
    } // for group
    
    
    
    // Initialise cgh
    for (int d=0; d<dim; ++d) {
      cgh->cctk_nghostzones[d] = dd->lghosts[d];
    }
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      for (int d=0; d<dim; ++d) {
        ((int*)arrdata[group].info.nghostzones)[d] = arrdata[group].dd->lghosts[d];
      }
    }
    
    for (int group=0; group<CCTK_NumGroups(); ++group) {
      if (CCTK_GroupTypeI(group) != CCTK_GF) {
        
        const int rl = 0;
        const int ml = 0;
        const int c = CCTK_MyProc(cgh);
        
        const bbox<int,dim>& base = arrdata[group].hh->baseextent;
        const vect<vect<bool,2>,dim>& obnds = arrdata[group].hh->outer_boundaries[rl][c];
	const bbox<int,dim>& ext  = arrdata[group].dd->boxes[rl][c][ml].exterior;
        
        for (int d=0; d<dim; ++d) {
          ((int*)arrdata[group].info.gsh )[d] = (base.shape() / base.stride())[d];
          ((int*)arrdata[group].info.lsh )[d] = (ext.shape() / ext.stride())[d];
          ((int*)arrdata[group].info.lbnd)[d] = (ext.lower() / ext.stride())[d];
          ((int*)arrdata[group].info.ubnd)[d] = (ext.upper() / ext.stride())[d];
          for (int f=0; f<2; ++f) {
            ((int*)arrdata[group].info.bbox)[2*d+f] = obnds[d][f];
          }
          
          assert (arrdata[group].info.lsh[d]>=0);
          assert (arrdata[group].info.lsh[d]<=arrdata[group].info.gsh[d]);
          assert (arrdata[group].info.lbnd[d]>=0);
          assert (arrdata[group].info.lbnd[d]<=arrdata[group].info.ubnd[d]+1);
          assert (arrdata[group].info.ubnd[d]<arrdata[group].info.gsh[d]);
          assert (arrdata[group].info.lbnd[d] + arrdata[group].info.lsh[d] - 1
                  == arrdata[group].info.ubnd[d]);
          assert (arrdata[group].info.lbnd[d]<=arrdata[group].info.ubnd[d]+1);
        }
        
        const int numvars = CCTK_NumVarsInGroupI (group);
        if (numvars>0) {
          const int firstvar = CCTK_FirstVarIndexI (group);
          assert (firstvar>=0);
          const int num_tl = CCTK_NumTimeLevelsFromVarI (firstvar);
          
          assert (rl>=0 && rl<(int)arrdata[group].dd->boxes.size());
          assert (c>=0 && c<(int)arrdata[group].dd->boxes[rl].size());
          assert (ml>=0 && ml<(int)arrdata[group].dd->boxes[rl][c].size());
          assert (arrdata[group].hh->is_local(rl,c));
          
          assert (group<(int)arrdata.size());
          for (int var=0; var<numvars; ++var) {
            assert (var<(int)arrdata[group].data.size());
            for (int tl=0; tl<num_tl; ++tl) {
              cgh->data[firstvar+var][tl] = 0;
            }
          }
        }
        
      } // if grouptype
    } // for group
    
    
    
    // Initialise current position
    reflevel  = -1;
    mglevel   = -1;
    component = -1;
    
    // Enable prolongating
    do_prolongate = true;
    
    
    
    // Set initial refinement structure
    vector<bbox<int,dim> > bbs;
    vector<vect<vect<bool,2>,dim> > obs;
    if (strcmp(base_extents, "") == 0) {
      // default: one grid component covering everything
      bbs.push_back (hh->baseextent);
      obs.push_back (vect<vect<bool,2>,dim>(vect<bool,2>(true)));
    } else {
      // explicit grid components
      istringstream ext_str(base_extents);
      ext_str >> bbs;
      CCTK_VInfo (CCTK_THORNSTRING, "Using %d grid patches", bbs.size());
      cout << "grid-patches-are " << bbs << endl;
      if (bbs.size()<=0) {
	CCTK_WARN (0, "Cannot evolve with 0 grid patches");
      }
      istringstream ob_str (base_outerbounds);
      ob_str >> obs;
      cout << "outer-boundaries-are " << obs << endl;
      assert (obs.size() == bbs.size());
    }
    
    SplitRegions (cgh, bbs, obs);
    
    vector<vector<bbox<int,dim> > > bbss(1);
    vector<vector<vect<vect<bool,2>,dim> > > obss(1);
    bbss[0] = bbs;
    obss[0] = obs;
    
    gh<dim>::rexts bbsss;
    bbsss = hh->make_multigrid_boxes(bbss, mglevels);
    
    gh<dim>::rprocs pss;
    MakeProcessors (cgh, bbsss, pss);
    
    // Recompose grid hierarchy
    Checkpoint ("Recomposing grid functions");
    Recompose (cgh, bbsss, obss, pss);
    
    
    
    // Initialise time step on coarse grid
    cgh->cctk_timefac = 1;
    for (int d=0; d<dim; ++d) {
      cgh->cctk_levoff[d] = 0;
      cgh->cctk_levoffdenom[d] = 1;
    }
    
    refleveltimes.resize (maxreflevels);
    cgh->cctk_time = 0.0;
    delta_time = 1.0;
    cgh->cctk_delta_time = 1.0;
    
    
    
    // Enable storage for all groups if desired
    if (true || enable_all_storage) {
      BEGIN_REFLEVEL_LOOP(cgh) {
	BEGIN_MGLEVEL_LOOP(cgh) {
	  for (int group=0; group<CCTK_NumGroups(); ++group) {
            char * groupname = CCTK_GroupName(group);
	    EnableGroupStorage (cgh, groupname);
            free (groupname);
	  }
	} END_MGLEVEL_LOOP;
      } END_REFLEVEL_LOOP;
    }
    
    Waypoint ("done with SetupGH.");
    
    // We register only once, ergo we get only one handle, ergo there
    // is only one grid hierarchy for us.  We store that statically,
    // so there is no need to pass anything to Cactus.
    return 0;
  }
  
} // namespace Carpet