aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/dh.cc
blob: c6b4ad3d6002ba67da955c24f98bbdd8bae27e1f (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
/***************************************************************************
                          dh.cc  -  Data Hierarchy
                          a grid hierarchy plus ghost zones
                             -------------------
    begin                : Sun Jun 11 2000
    copyright            : (C) 2000 by Erik Schnetter
    email                : schnetter@astro.psu.edu

    $Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetLib/src/dh.cc,v 1.6 2001/03/13 13:06:59 eschnett Exp $

 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#include <cassert>

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

#if !defined(TMPL_IMPLICIT) || !defined(DH_HH)
#  include "dh.hh"
#endif

#define DEBUG_OUTPUT



// Constructors
template<int D>
dh<D>::dh (gh<D>& h, const ivect& lghosts, const ivect& ughosts)
  : h(h), lghosts(lghosts), ughosts(ughosts)
{
  assert (all(lghosts>=0 && ughosts>=0));
  CHECKPOINT;
  h.add(this);
  recompose();
}

// Destructors
template<int D>
dh<D>::~dh () {
  CHECKPOINT;
  h.remove(this);
}

// Modifiers
template<int D>
void dh<D>::recompose () {
  CHECKPOINT;
  
  boxes.clear();
  
  boxes.resize(h.reflevels());
  for (int rl=0; rl<h.reflevels(); ++rl) {
    boxes[rl].resize(h.components(rl));
    for (int c=0; c<h.components(rl); ++c) {
      boxes[rl][c].resize(h.mglevels(rl,c));
      for (int ml=0; ml<h.mglevels(rl,c); ++ml) {
       	const ibbox intr = h.extents[rl][c][ml];
	
       	// Interior
       	// (the interior of the grid has the extent as specified by
       	// the user)
       	boxes[rl][c][ml].interior = intr;
	
       	// Exterior (add ghost zones)
       	// (the content of the exterior is completely determined by
       	// the interior of this or other components; the content of
       	// the exterior is redundant)
       	boxes[rl][c][ml].exterior = intr.expand(lghosts, ughosts);
	
       	// Boundaries (ghost zones only)
       	// (interior + boundaries = exterior)
       	boxes[rl][c][ml].boundaries = boxes[rl][c][ml].exterior - intr;
	
      } // for ml
    } // for c
  } // for rl
  
  for (int rl=0; rl<h.reflevels(); ++rl) {
    for (int c=0; c<h.components(rl); ++c) {
      for (int ml=0; ml<h.mglevels(rl,c); ++ml) {
	
      	// Sync boxes
      	const int cs = h.components(rl);
      	boxes[rl][c][ml].send_sync.resize(cs);
      	boxes[rl][c][ml].recv_sync.resize(cs);
      	
      	// Refinement boxes
      	if (rl>0) {
      	  const int csm1 = h.components(rl-1);
      	  boxes[rl][c][ml].send_ref_coarse.resize(csm1);
      	  boxes[rl][c][ml].recv_ref_coarse.resize(csm1);
      	  boxes[rl][c][ml].recv_ref_bnd_coarse.resize(csm1);
      	}
      	if (rl<h.reflevels()-1) {
      	  const int csp1 = h.components(rl+1);
      	  boxes[rl][c][ml].recv_ref_fine.resize(csp1);
      	  boxes[rl][c][ml].send_ref_fine.resize(csp1);
      	  boxes[rl][c][ml].send_ref_bnd_fine.resize(csp1);
      	}
	
      } // for ml
    } // for c
  } // for rl
  
  for (int rl=0; rl<h.reflevels(); ++rl) {
    for (int c=0; c<h.components(rl); ++c) {
      for (int ml=0; ml<h.mglevels(rl,c); ++ml) {
      	const ibbox intr = boxes[rl][c][ml].interior;
      	const ibbox extr = boxes[rl][c][ml].exterior;

      	const ibset bnds = boxes[rl][c][ml].boundaries;
	
      	// Sync boxes
      	for (int cc=0; cc<h.components(rl); ++cc) {
      	  assert (ml<h.mglevels(rl,cc));
      	  // intersect boundaries with interior of that component
      	  const ibset ovlp = bnds & boxes[rl][cc][ml].interior;
      	  for (ibset::const_iterator b=ovlp.begin(); b!=ovlp.end(); ++b) {
      	    boxes[rl][c ][ml].recv_sync[cc].push_back(*b);
      	    boxes[rl][cc][ml].send_sync[c ].push_back(*b);
      	  }
      	}
      	
      	// Multigrid boxes
      	if (ml>0) {
      	  const ibbox intrf = boxes[rl][c][ml-1].interior;
      	  const ibbox extrf = boxes[rl][c][ml-1].exterior;
      	  // Restriction (interior)
      	  {
      	    // (the restriction must fill all of the interior of the
      	    // coarse grid, and may use the exterior of the fine grid)
      	    const ibbox recv = intr;
      	    const ibbox send = recv.expanded_for(extrf);
      	    assert (send.is_contained_in(extrf));
      	    boxes[rl][c][ml-1].send_mg_coarse.push_back(send);
      	    boxes[rl][c][ml  ].recv_mg_fine  .push_back(recv);
      	  }
      	  // Prolongation (interior)
      	  {
      	    // (the prolongation may use the exterior of the coarse
      	    // grid, and may fill only the interior of the fine grid,
      	    // and the bbox must be as large as possible)
      	    const ibbox recv = extr.contracted_for(intrf) & intrf;
      	    const ibbox send = recv.expanded_for(extr);
      	    boxes[rl][c][ml-1].recv_mg_coarse.push_back(recv);
      	    boxes[rl][c][ml  ].send_mg_fine  .push_back(send);
      	  }
      	}
      	
      	// Refinement boxes
      	if (rl<h.reflevels()-1) {
      	  for (int cc=0; cc<h.components(rl+1); ++cc) {
      	    const ibbox intrf = boxes[rl+1][cc][ml].interior;
      	    const ibbox extrf = boxes[rl+1][cc][ml].exterior;
      	    // Restriction (interior)
      	    {
      	      // (the restriction may fill the interior of the of the
      	      // coarse grid, and may use the exterior of the fine
      	      // grid, and the bbox must be as large as possible)
      	      const ibbox recv = extrf.contracted_for(intr) & intr;
      	      const ibbox send = recv.expanded_for(extrf);
      	      boxes[rl+1][cc][ml].send_ref_coarse[c ].push_back(send);
      	      boxes[rl  ][c ][ml].recv_ref_fine  [cc].push_back(recv);
      	    }
      	    // Prolongation (interior)
      	    {
      	      // (the prolongation may use the exterior of the coarse
      	      // grid, and must fill all of the interior of the fine
      	      // grid)
      	      const ibbox recv = extr.contracted_for(intrf) & intrf;
      	      const ibbox send = recv.expanded_for(extr);
      	      assert (send.is_contained_in(extr));
      	      boxes[rl+1][cc][ml].recv_ref_coarse[c ].push_back(recv);
      	      boxes[rl  ][c ][ml].send_ref_fine  [cc].push_back(send);
      	    }
      	    // Prolongation (boundaries)
      	    {
      	      ibset bndsf = boxes[rl+1][cc][ml].boundaries;
      	      // coarsify boundaries of fine component
      	      for (ibset::const_iterator bi=bndsf.begin();
		   bi!=bndsf.end(); ++bi) {
		const ibbox& bndf = *bi;
		// (the prolongation may use the exterior of the
		// coarse grid, and must fill all of the boundary of
		// the fine grid)
		const ibbox recv = extr.contracted_for(bndf) & bndf;
		const ibbox send = recv.expanded_for(extr);
		assert (send.is_contained_in(extr));
		boxes[rl+1][cc][ml].recv_ref_bnd_coarse[c ].push_back(recv);
		boxes[rl  ][c ][ml].send_ref_bnd_fine  [cc].push_back(send);
      	      }
      	    }
      	
      	  }
      	}
	
      } // for ml
    } // for c
  } // for rl
  
  for (int rl=0; rl<h.reflevels(); ++rl) {
    for (int c=0; c<h.components(rl); ++c) {
      for (int ml=0; ml<h.mglevels(rl,c); ++ml) {
	
	// Boundaries that are neither synced nor prolonged from
	// coarser grids (outer boundaries)
	ibset& sync_not = boxes[rl][c][ml].sync_not;
	
	// The whole boundary
	sync_not = boxes[rl][c][ml].boundaries;
	
	// Subtract boxes received during synchronisation
	const iblistvect& recv_sync = boxes[rl][c][ml].recv_sync;
	for (iblistvect::const_iterator lvi=recv_sync.begin();
	     lvi!=recv_sync.end(); ++lvi) {
	  for (iblist::const_iterator li=lvi->begin();
	       li!=lvi->end(); ++li) {
	    sync_not -= *li;
	  }
	}
	
      } // for ml
    } // for c
  } // for rl
  
  bases.resize(h.reflevels());
  for (int rl=0; rl<h.reflevels(); ++rl) {
    if (h.components(rl)==0) {
      bases[rl].resize(0);
    } else {
      bases[rl].resize(h.mglevels(rl,0));
      for (int ml=0; ml<h.mglevels(rl,0); ++ml) {
	bases[rl][ml].exterior = ibbox();
	bases[rl][ml].interior = ibbox();
	for (int c=0; c<h.components(rl); ++c) {
	  bases[rl][ml].exterior
	    = (bases[rl][ml].exterior
	       .expanded_containing(boxes[rl][c][ml].exterior));
	  bases[rl][ml].interior
	    = (bases[rl][ml].interior
	       .expanded_containing(boxes[rl][c][ml].interior));
	}
	bases[rl][ml].boundaries
	  = bases[rl][ml].exterior - bases[rl][ml].interior;
      }
    }
  }
  
#ifdef DEBUG_OUTPUT
  for (int rl=0; rl<h.reflevels(); ++rl) {
    for (int c=0; c<h.components(rl); ++c) {
      for (int ml=0; ml<h.mglevels(rl,c); ++ml) {
      	cout << endl;
      	cout << "dh bboxes:" << endl;
      	cout << "rl=" << rl << " c=" << c << " ml=" << ml << endl;
      	cout << "exterior=" << boxes[rl][c][ml].exterior << endl;
      	cout << "interior=" << boxes[rl][c][ml].interior << endl;
      	cout << "send_mg_fine=" << boxes[rl][c][ml].send_mg_fine << endl;
      	cout << "send_mg_coarse=" << boxes[rl][c][ml].send_mg_coarse << endl;
      	cout << "recv_mg_fine=" << boxes[rl][c][ml].recv_mg_fine << endl;
      	cout << "recv_mg_coarse=" << boxes[rl][c][ml].recv_mg_coarse << endl;
      	cout << "send_ref_fine=" << boxes[rl][c][ml].send_ref_fine << endl;
      	cout << "send_ref_coarse=" << boxes[rl][c][ml].send_ref_coarse << endl;
      	cout << "recv_ref_fine=" << boxes[rl][c][ml].recv_ref_fine << endl;
      	cout << "recv_ref_coarse=" << boxes[rl][c][ml].recv_ref_coarse << endl;
      	cout << "send_sync=" << boxes[rl][c][ml].send_sync << endl;
      	cout << "send_ref_bnd_fine=" << boxes[rl][c][ml].send_ref_bnd_fine << endl;
      	cout << "boundaries=" << boxes[rl][c][ml].boundaries << endl;
      	cout << "recv_sync=" << boxes[rl][c][ml].recv_sync << endl;
      	cout << "recv_ref_bnd_coarse=" << boxes[rl][c][ml].recv_ref_bnd_coarse << endl;
      	cout << "sync_not=" << boxes[rl][c][ml].sync_not << endl;
      }
    }
  }
  for (int rl=0; rl<h.reflevels(); ++rl) {
    if (h.components(rl)>0) {
      for (int ml=0; ml<h.mglevels(rl,0); ++ml) {
      	cout << endl;
      	cout << "dh bases:" << endl;
      	cout << "rl=" << rl << " ml=" << ml << endl;
      	cout << "exterior=" << bases[rl][ml].exterior << endl;
      	cout << "interior=" << bases[rl][ml].interior << endl;
      	cout << "boundaries=" << bases[rl][ml].boundaries << endl;
      }
    }
  }
#endif
  
  for (list<generic_gf<D>*>::iterator f=gfs.begin(); f!=gfs.end(); ++f) {
    (*f)->recompose();
  }
}

// Grid function management
template<int D>
void dh<D>::add (generic_gf<D>* f) {
  CHECKPOINT;
  gfs.push_back(f);
}

template<int D>
void dh<D>::remove (generic_gf<D>* f) {
  CHECKPOINT;
  gfs.remove(f);
}



// Output
template<int D>
ostream& operator<< (ostream& os, const dh<D>& d) {
  os << "dh<" << D << ">:"
     << "ghosts=[" << d.lghosts << "," << d.ughosts << "],"
     << "gfs={";
  int cnt=0;
  for (list<generic_gf<D>*>::const_iterator f = d.gfs.begin();
       f != d.gfs.end(); ++f) {
    if (cnt++) os << ",";
    os << **f;
  }
  os << "}";
  return os;
}



#if defined(TMPL_EXPLICIT)
template class dh<1>;
template ostream& operator<< (ostream& os, const dh<1>& d);

template class dh<2>;
template ostream& operator<< (ostream& os, const dh<2>& d);

template class dh<3>;
template ostream& operator<< (ostream& os, const dh<3>& d);
#endif