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

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

#include "Carpet/CarpetLib/src/dist.hh"
#include "Carpet/CarpetLib/src/ggf.hh"
#include "Carpet/CarpetLib/src/vect.hh"

#include "carpet.hh"

static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/Carpet/src/SetupGH.cc,v 1.2 2001/07/09 09:00:10 schnetter Exp $";



namespace Carpet {
  
  using namespace std;
  
  
  
  void* SetupGH (tFleshConfig* fc, int convLevel, cGH* cgh)
  {
    DECLARE_CCTK_PARAMETERS;
    
    assert (cgh->cctk_dim == dim);
    
    // Not sure what to do with that
    assert (convLevel==0);
    
    dist::pseudoinit();
    Checkpoint ("starting SetupGH...");
    
    // Refinement information
    maxreflevels = max_refinement_levels;
    maxreflevelfact
      = (int)floor(pow((double)refinement_factor, maxreflevels-1) + 0.5);
    
    // 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);
    }
    
    const vect<int,dim> str(stride);
    const vect<int,dim> lb(lghosts * str);
    const vect<int,dim> ub = (npoints - ughosts - 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(hh, maxreflevelfact);
    
    // Allocate data hierarchy
    dd = new dh<dim>(*hh, lghosts, ughosts, prolongation_order_space);
    
    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 grid hierarchy for scalars
    const vect<int,dim> lb0(0);
    const vect<int,dim> ub0(0);
    const bbox<int,dim> baseext0(lb0, ub0, str);
    hh0 = new gh<dim>(refinement_factor, vertex_centered,
		      multigrid_factor, vertex_centered,
		      baseext0);
    
    // Allocate time hierarchy for scalars
    tt0 = new th(hh0, maxreflevelfact);
    
    // Allocate data hierarchy for scalars
    const vect<int,dim> lghosts0(0);
    const vect<int,dim> ughosts0(0);
    dd0 = new dh<dim>(*hh0, lghosts0, ughosts0, prolongation_order_space);
    
    // 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) {
      
      switch (CCTK_GroupTypeI(group)) {
	
      case CCTK_SCALAR: {
	arrdata[group].info.dim = 0;
	arrdata[group].hh = hh0;
	arrdata[group].tt = tt0;
	arrdata[group].dd = dd0;
	break;
      }
	
      case CCTK_ARRAY: {
	cGroup gp;
	CCTK_GroupData (group, &gp);
	
	assert (gp.dim>=1 || gp.dim<=dim);
	arrdata[group].info.dim = gp.dim;
	
	switch (gp.disttype) {
	case CCTK_DISTRIB_CONSTANT:
	case CCTK_DISTRIB_DEFAULT:
	  break;
	default:
	  abort();
	}
	
	const CCTK_INT * const * const sz  = CCTK_GroupSizesI(group);
	const CCTK_INT * const * const gsz = CCTK_GroupGhostsizesI(group);
	vect<int,dim> sizes(1), ghostsizes(0);
	for (int d=0; d<gp.dim; ++d) {
	  if (sz) sizes[d] = (*sz)[d];
	  if (gsz) ghostsizes[d] = (*gsz)[d];
	}
	
	vect<int,dim> alb(0), aub(stride), astr(stride);
	for (int d=0; d<gp.dim; ++d) {
	  if (gp.disttype==CCTK_DISTRIB_CONSTANT && d==gp.dim-1) {
	    aub[d] = (CCTK_nProcs(cgh) * sizes[d]
		      - (CCTK_nProcs(cgh)-1) * ghostsizes[d]);
	  } else {
	    aub[d] = sizes[d];
	  }
	}
	const bbox<int,dim> arrext(alb, aub-astr, astr);
	
	arrdata[group].hh = new gh<dim>(refinement_factor, vertex_centered,
					multigrid_factor, vertex_centered,
					arrext);
	
	arrdata[group].tt = new th(arrdata[group].hh, maxreflevelfact);
	
	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,
			prolongation_order_space);
	
	if (max_refinement_levels > 1) {
	  const int prolongation_stencil_size
	    = arrdata[group].dd->prolongation_stencil_size();
	  const int min_nghosts
	    = ((prolongation_stencil_size + refinement_factor - 2)
	       / (refinement_factor-1));
	  if (any(min(alghosts,aughosts) < min_nghosts)) {
	    CCTK_VWarn (0, __LINE__, __FILE__, CCTK_THORNSTRING,
			"There are not enough ghost zones for the desired spatial prolongation order for the grid function group \"%s\".  With Carpet::prolongation_order_space=%d, you need at least %d ghost zones.",
			CCTK_GroupName(group),
			prolongation_order_space, min_nghosts);
	  }
	}
	break;
      }
	
      case CCTK_GF: {
	assert (CCTK_GroupDimI(group) == dim);
	arrdata[group].info.dim = dim;
	arrdata[group].hh = hh;
	arrdata[group].tt = tt;
	arrdata[group].dd = dd;
	break;
      }
	
      default:
	abort();
      }
      
      arrdata[group].info.gsh         = (int*)malloc(  dim * sizeof(int));
      arrdata[group].info.lsh         = (int*)malloc(  dim * sizeof(int));
      arrdata[group].info.lbnd        = (int*)malloc(  dim * sizeof(int));
      arrdata[group].info.ubnd        = (int*)malloc(  dim * sizeof(int));
      arrdata[group].info.bbox        = (int*)malloc(2*dim * sizeof(int));
      arrdata[group].info.nghostzones = (int*)malloc(  dim * sizeof(int));
      
      arrdata[group].data.resize(CCTK_NumVarsInGroupI(group));
      for (int var=0; var<(int)arrdata[group].data.size(); ++var) {
	arrdata[group].data[var] = 0;
      }
    }
    
    // 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) {
	cgh->cctk_nghostzones[d] = arrdata[group].dd->lghosts[d];
      }
    }
    
    // Initialise current position
    reflevel  = 0;
    mglevel   = 0;
    component = -1;
    
    // Recompose grid hierarchy
    Recompose (cgh); 
    
    // Initialise time step on coarse grid
    base_delta_time = 0;
    
    // Current iteration
    iteration.resize(maxreflevels, 0);
    
    // Set current position (this time for real)
    set_reflevel  (cgh, 0);
    set_mglevel   (cgh, 0);
    set_component (cgh, -1);
    
    // Enable storage for all groups if desired
    // XXX
    if (true || enable_all_storage) {
      for (int group=0; group<CCTK_NumGroups(); ++group) {
	EnableGroupStorage (cgh, CCTK_GroupName(group));
      }
    }
    
    Checkpoint ("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