aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetRegrid2/src/regrid.cc
blob: 765b1f758e31a5f454317b7bb210693b2a3a2c0c (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
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <vector>

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

#include "bbox.hh"
#include "bboxset.hh"
#include "defs.hh"
#include "dh.hh"
#include "gh.hh"
#include "vect.hh"

#include "carpet.hh"



namespace CarpetRegrid2 {
  
  using namespace std;
  using namespace Carpet;
  
  
  
  typedef bboxset <int, dim> ibboxset;
  typedef vect <CCTK_REAL, dim> rvect;
  typedef bbox <CCTK_REAL, dim> rbbox;
  
  
  
  struct centre_description {
    int                num_levels;
    rvect              position;
    vector <CCTK_REAL> radius;
    
    static int num_centres ();
    centre_description (int n);
  };
  
  
  
  int
  centre_description::
  num_centres ()
  {
    DECLARE_CCTK_PARAMETERS;
    return num_centres;
  }
  
  
  
  centre_description::
  centre_description (int const n)
  {
    DECLARE_CCTK_PARAMETERS;
    
    assert (n >= 0 and n < centre_description::num_centres ());
    switch (n) {
      
    case 0:
      num_levels = num_levels_1;
      position = rvect (position_x_1, position_y_1, position_z_1);
      radius.resize (num_levels);
      for (int rl = 0; rl < num_levels; ++ rl) {
        radius.at(rl) = radius_1[rl];
      }
      break;
      
    case 1:
      num_levels = num_levels_2;
      position = rvect (position_x_2, position_y_2, position_z_2);
      radius.resize (num_levels);
      for (int rl = 0; rl < num_levels; ++ rl) {
        radius.at(rl) = radius_2[rl];
      }
      break;
      
    case 2:
      num_levels = num_levels_3;
      position = rvect (position_x_3, position_y_3, position_z_3);
      radius.resize (num_levels);
      for (int rl = 0; rl < num_levels; ++ rl) {
        radius.at(rl) = radius_3[rl];
      }
      break;
      
    default:
      assert (0);
    }
    
    assert (num_levels <= maxreflevels);
  }
  
  
  
  // Convert a coordinate location to an index location
  ivect
  rpos2ipos (rvect const & rpos,
             rvect const & origin, rvect const & scale,
             gh const & hh, int const rl)
  {
    ivect const levfac = hh.reffacts.at(rl);
    assert (all (hh.baseextent.stride() % levfac == 0));
    ivect const istride = hh.baseextent.stride() / levfac;
    
    return (ivect (floor ((rpos - origin) * scale / rvect(istride) +
                          static_cast<CCTK_REAL> (0.5))) *
            istride);
  }
  
  // Convert a coordinate location to an index location, rounding in
  // the opposite manner as rpos2ipos
  ivect
  rpos2ipos1 (rvect const & rpos,
              rvect const & origin, rvect const & scale,
              gh const & hh, int const rl)
  {
    ivect const levfac = hh.reffacts.at(rl);
    assert (all (hh.baseextent.stride() % levfac == 0));
    ivect const istride = hh.baseextent.stride() / levfac;
    
    return (ivect (ceil ((rpos - origin) * scale / rvect(istride) -
                         static_cast<CCTK_REAL> (0.5))) *
            istride);
  }
  
  // Convert an index location to a coordinate location
  rvect
  ipos2rpos (ivect const & ipos,
             rvect const & origin, rvect const & scale,
             gh const & hh, int const rl)
  {
    return rvect(ipos) / scale + origin;
  }
  
  // Convert an index bbox to a coordinate bbox
  rbbox
  ibbox2rbbox (ibbox const & ib,
               rvect const & origin, rvect const & scale,
               gh const & hh, int const rl)
  {
    rvect const zero (0);
    return rbbox (ipos2rpos (ib.lower() , origin, scale, hh, rl),
                  ipos2rpos (ib.upper() , origin, scale, hh, rl),
                  ipos2rpos (ib.stride(), zero  , scale, hh, rl));
  }
  
  
  
  extern "C" {
    CCTK_INT
    CarpetRegrid2_Regrid (CCTK_POINTER_TO_CONST const cctkGH_,
                          CCTK_POINTER          const bbsss_,
                          CCTK_POINTER          const obss_,
                          CCTK_POINTER          const pss_,
                          CCTK_INT              const force);
    
    CCTK_INT
    CarpetRegrid2_RegridMaps (CCTK_POINTER_TO_CONST const cctkGH_,
                              CCTK_POINTER          const bbssss_,
                              CCTK_POINTER          const obsss_,
                              CCTK_POINTER          const psss_,
                              CCTK_INT              const force);
  }
  
  
  
  void
  Regrid (cGH const * const cctkGH,
          gh::rexts & bbss,
          gh::rbnds & obss)
  {
    DECLARE_CCTK_PARAMETERS;
    
    if (verbose) CCTK_INFO ("Regridding");
    
    assert (Carpet::is_singlemap_mode());
    gh const & hh = * Carpet::vhh.at (Carpet::map);
    dh const & dd = * Carpet::vdd.at (Carpet::map);
    
    //
    // Find extent of domain
    //
    
    // This requires that CoordBase is used
#warning "TODO: check this (for Carpet, and maybe also for CartGrid3D)"
#warning "TODO: (the check that these two are consistent should be in Carpet)"
    
    typedef vect<vect<CCTK_INT,2>,3> jjvect;
    jjvect nboundaryzones;
    jjvect is_internal;
    jjvect is_staggered;
    jjvect shiftout;
    {
      CCTK_INT const ierr = GetBoundarySpecification
        (2*dim,
         & nboundaryzones[0][0],
         & is_internal[0][0],
         & is_staggered[0][0],
         & shiftout[0][0]);
      assert (not ierr);
    }
    
    rvect physical_lower, physical_upper;
    rvect interior_lower, interior_upper;
    rvect exterior_lower, exterior_upper;
    rvect spacing;
    {
      CCTK_INT const ierr = GetDomainSpecification
        (dim,
         & physical_lower[0], & physical_upper[0],
         & interior_lower[0], & interior_upper[0],
         & exterior_lower[0], & exterior_upper[0],
         & spacing[0]);
      assert (not ierr);
    }
    
    // Adapt spacing for convergence level
    spacing *= ipow ((CCTK_REAL) Carpet::mgfact, Carpet::basemglevel);
    
    {
      CCTK_INT const ierr =
        ConvertFromPhysicalBoundary
        (dim,
         & physical_lower[0], & physical_upper[0],
         & interior_lower[0], & interior_upper[0],
         & exterior_lower[0], & exterior_upper[0],
         & spacing[0]);
      assert (not ierr);
    }
    
    //
    // Calculate the union of the bounding boxes for all levels
    //
    
    rvect const origin (exterior_lower);
    rvect const scale (rvect (hh.baseextent.stride()) / spacing);
    
    ivect const physical_ilower =
      rpos2ipos (physical_lower, origin, scale, hh, 0);
    ivect const physical_iupper =
      rpos2ipos1 (physical_upper, origin, scale, hh, 0);
    
    // The set of refined regions
    vector <ibboxset> regions (1);
    
    // Loop over all centres
    for (int n = 0; n < centre_description::num_centres(); ++ n) {
      centre_description centre (n);
      
      // Loop over all levels for this centre
      for (int rl = 1; rl < centre.num_levels; ++ rl) {
        
        // Calculate a bbox for this region
        rvect const rmin = centre.position - centre.radius.at(rl);
        rvect const rmax = centre.position + centre.radius.at(rl);
        
        // Convert to an integer bbox
        ivect const imin =
          rpos2ipos (rmin, origin, scale, hh, rl);
        ivect const imax =
          rpos2ipos1 (rmax, origin, scale, hh, rl);
        
        ivect const levfac = hh.reffacts.at(rl);
        assert (all (hh.baseextent.stride() % levfac == 0));
        ivect const istride = hh.baseextent.stride() / levfac;
        
        ibbox const region (imin, imax, istride);
        
        // Add this region to the list of regions
        if (static_cast <int> (regions.size()) < rl+1) regions.resize (rl+1);
        regions.at(rl) |= region;
        
      } // for rl
      
    } // for n
    
    // Ensure that the coarser grids contain the finer ones
    for (size_t rl = regions.size() - 1; rl >= 2; -- rl) {
      ibbox coarse = * regions.at(rl-1).begin();
      
      regions.at(rl).normalize();
      ibboxset coarsified;
      for (ibboxset::const_iterator ibb = regions.at(rl).begin();
           ibb != regions.at(rl).end();
           ++ ibb)
      {
        ivect const distance (min_distance);
        ibbox const fbb = * ibb;
        ibbox const cbb = fbb.expanded_for(coarse);
        ibbox const ebb = cbb.expand (distance, distance);
        coarsified |= ebb;
      }
      
      regions.at(rl-1) |= coarsified;
    }
    
    
    
    //
    // Clip at the outer boundary, and convert to (bbss, obss) pair
    //
    
    bbss.resize (regions.size());
    obss.resize (regions.size());
    
    for (size_t rl = 1; rl < regions.size(); ++ rl) {
      
      // Find the location of the outer boundary
      rvect const level_physical_lower = physical_lower;
      rvect const level_physical_upper = physical_upper;
      rvect const level_spacing = spacing / rvect (hh.reffacts.at(rl));
      rvect level_interior_lower, level_interior_upper;
      rvect level_exterior_lower, level_exterior_upper;
      {
        CCTK_INT const ierr =
          ConvertFromPhysicalBoundary
          (dim,
           & level_physical_lower[0], & level_physical_upper[0],
           & level_interior_lower[0], & level_interior_upper[0],
           & level_exterior_lower[0], & level_exterior_upper[0],
           & level_spacing[0]);
        assert (not ierr);
      }
      
      ivect const level_exterior_ilower =
        rpos2ipos (level_exterior_lower, origin, scale, hh, rl);
      ivect const level_exterior_iupper =
        rpos2ipos1 (level_exterior_upper, origin, scale, hh, rl);
      
      // Find the minimum necessary distance away from the outer
      // boundary due to buffer and ghost zones.  This is e.g. the
      // distance that the lower boundary of a bbox has to have from
      // the lower boundary.  This is in terms of grid points.
      i2vect const min_bnd_dist_away = dd.buffers + dd.ghosts;
      // Find the minimum necessary distance from the outer boundary
      // due to buffer and ghost zones.  This is e.g. the distance
      // that the upper boundary of a bbox has to have from the lower
      // boundary.  This is in terms of grid points.
      i2vect const min_bnd_dist_incl = dd.ghosts;
      
      // Clip at the outer boundary
      regions.at(rl).normalize();
      ibboxset clipped;
      for (ibboxset::const_iterator ibb = regions.at(rl).begin();
           ibb != regions.at(rl).end();
           ++ ibb)
      {
        ibbox const bb = * ibb;
        
        // Clip boxes that extend outside the boundary.  Enlarge boxes
        // that are inside but too close to the outer boundary.
        bvect const lower_is_outside_lower =
          bb.lower() - min_bnd_dist_away[0] * bb.stride() <= physical_ilower;
        // Remove bboxes that are completely outside.
        bvect const upper_is_outside_lower =
          bb.upper() < physical_ilower;
        // Enlarge bboxes that extend not far enough inwards.
        bvect const upper_is_almost_outside_lower =
          bb.upper() < physical_ilower + min_bnd_dist_incl[0] * bb.stride();
        
        // Ditto for the upper boundary.
        bvect const upper_is_outside_upper =
          bb.upper() + min_bnd_dist_away[1] * bb.stride() >= physical_iupper;
        bvect const lower_is_outside_upper =
          bb.lower() > physical_iupper;
        bvect const lower_is_almost_outside_upper =
          bb.lower() > physical_iupper - min_bnd_dist_incl[1] * bb.stride();
        
        assert (not any (lower_is_almost_outside_upper and
                         lower_is_outside_lower));
        assert (not any (upper_is_almost_outside_lower and
                         upper_is_outside_upper));
        
        if (any (upper_is_outside_lower or lower_is_outside_upper)) {
          // The box is completely outside.  Ignore it.
          continue;
        }
        
        ibbox const clipped_bb
          (either (lower_is_outside_lower,
                   level_exterior_ilower,
                   either (lower_is_almost_outside_upper,
                           physical_iupper - min_bnd_dist_incl[1] * bb.stride(),
                           bb.lower())),
           either (upper_is_outside_upper,
                   level_exterior_iupper,
                   either (upper_is_almost_outside_lower,
                           physical_ilower + min_bnd_dist_incl[0] * bb.stride(),
                           bb.upper())),
           bb.stride());
        
        clipped |= clipped_bb;
        
        if (symmetry_rotating90) {
          assert (0);           // There is also a paramwarn about this
        }
        
        if (symmetry_rotating180) {
          // Make the boxes rotating-180 symmetric
          if (lower_is_outside_lower[0]) {
            ivect const ilo = clipped_bb.lower();
            ivect const iup = clipped_bb.upper();
            ivect const istr = clipped_bb.stride();
            
            // Origin
            rvect const axis (physical_lower[0],
                              (physical_lower[1] + physical_upper[1]) / 2,
                              physical_lower[2]); // z component is unused
            ivect const iaxis0 = rpos2ipos  (axis, origin, scale, hh, rl);
            assert (all (iaxis0 % istr == 0));
            ivect const iaxis1 = rpos2ipos1 (axis, origin, scale, hh, rl);
            assert (all (iaxis1 % istr == 0));
            ivect const offset = iaxis1 - iaxis0;
            assert (all (offset % istr == 0));
            assert (all (offset >= 0 and offset < 2*istr));
            assert (all ((iaxis0 + iaxis1 - offset) % (2*istr) == 0));
            ivect const iaxis = (iaxis0 + iaxis1 - offset) / 2;
            
            // Mirror about the y axis, cutting off most of the extent
            // in the positive x direction
            ivect const new_ilo (ilo[0],
                                 (2*iaxis[1]+offset[1]) - iup[1],
                                 ilo[2]);
            ivect const new_iup ((2*iaxis[0]+offset[0]) - ilo[0],
                                 (2*iaxis[1]+offset[1]) - ilo[1],
                                 iup[2]);
            ivect const new_istr (istr);
            
            ibbox const new_bb (new_ilo, new_iup, new_istr);
            
            clipped |= new_bb;
          }
        }
      }
      
      regions.at(rl) = clipped;
      
      // Simplify the set of refined regions
      regions.at(rl).normalize();
      
      // Create a vector of bboxes for this level
      gh::cexts bbs;
      gh::cbnds obs;
      bbs.reserve (regions.at(rl).setsize());
      obs.reserve (regions.at(rl).setsize());
      for (ibboxset::const_iterator ibb = regions.at(rl).begin();
           ibb != regions.at(rl).end();
           ++ ibb)
      {
        ibbox bb = * ibb;
        
        bvect const lower_is_outer = bb.lower() <= physical_ilower;
        bvect const upper_is_outer = bb.upper() >= physical_iupper;
        
        bbvect ob;
        for (int d = 0; d < dim; ++ d) {
          ob[d][0] = lower_is_outer[d];
          ob[d][1] = upper_is_outer[d];
        }
        bbs.push_back (bb);
        obs.push_back (ob);
      }
      
      bbss.at(rl) = bbs;
      obss.at(rl) = obs;
      
    } // for rl
  }
  
  
  
  CCTK_INT
  CarpetRegrid2_Regrid (CCTK_POINTER_TO_CONST const cctkGH_,
                        CCTK_POINTER          const bbsss_,
                        CCTK_POINTER          const obss_,
                        CCTK_POINTER          const pss_,
                        CCTK_INT              const force)
  {
    DECLARE_CCTK_PARAMETERS;
    
    cGH const * const cctkGH = static_cast <cGH const *> (cctkGH_);
    
    assert (Carpet::is_singlemap_mode());
    
    // Decide whether to change the grid hierarchy
    // (We always do)
    bool do_recompose;
    if (force) {
      do_recompose = true;
    } else {
      if (regrid_every == -1) {
        do_recompose = false;
      } else if (regrid_every == 0) {
        do_recompose = cctkGH->cctk_iteration == 0;
      } else {
        do_recompose =
          cctkGH->cctk_iteration == 0 or
          (cctkGH->cctk_iteration > 0 and
           (cctkGH->cctk_iteration - 1) % regrid_every == 0);
      }
    }
    
    if (do_recompose) {
      
      gh::mexts  & bbsss = * static_cast <gh::mexts  *> (bbsss_);
      gh::rbnds  & obss  = * static_cast <gh::rbnds  *> (obss_ );
      gh::rprocs & pss   = * static_cast <gh::rprocs *> (pss_  );
      
      // Make multigrid unaware
      vector <vector <ibbox> > bbss = bbsss.at(0);
      
      Regrid (cctkGH, bbss, obss);
      
      // Make multiprocessor aware
      pss.resize (bbss.size());
      for (size_t rl = 0; rl < pss.size(); ++ rl) {
        Carpet::SplitRegions (cctkGH, bbss.at(rl), obss.at(rl), pss.at(rl));
      } // for rl
      
      // Make multigrid aware
      Carpet::MakeMultigridBoxes (cctkGH, bbss, obss, bbsss);
      
    } // if do_recompose
    
    return do_recompose;
  }
  
  
  
  CCTK_INT
  CarpetRegrid2_RegridMaps (CCTK_POINTER_TO_CONST const cctkGH_,
                            CCTK_POINTER          const bbssss_,
                            CCTK_POINTER          const obsss_,
                            CCTK_POINTER          const psss_,
                            CCTK_INT              const force)
  {
    DECLARE_CCTK_PARAMETERS;
    
    cGH const * const cctkGH = static_cast <cGH const *> (cctkGH_);
    
    assert (Carpet::is_level_mode());
    
    // Decide whether to change the grid hierarchy
    static int last_iteration = -1;
    bool do_recompose;
    if (force) {
      do_recompose = true;
    } else {
      if (regrid_every == -1) {
        do_recompose = false;
      } else if (regrid_every == 0) {
        do_recompose = cctkGH->cctk_iteration == 0;
      } else {
        do_recompose =
          (cctkGH->cctk_iteration == 0 or
           (cctkGH->cctk_iteration > 0 and
            (cctkGH->cctk_iteration - 1) % regrid_every == 0 and
            cctkGH->cctk_iteration > last_iteration));
      }
    }
    last_iteration = cctkGH->cctk_iteration;
    
    if (do_recompose) {
      
      vector <gh::mexts>  & bbssss =
        * static_cast <vector <gh::mexts>  *> (bbssss_);
      vector <gh::rbnds>  & obsss  =
        * static_cast <vector <gh::rbnds>  *> (obsss_ );
      vector <gh::rprocs> & psss   =
        * static_cast <vector <gh::rprocs> *> (psss_  );
      
      // Make multigrid unaware
      vector< vector <vector <ibbox> > > bbsss (maps);
      for (int m = 0; m < maps; ++ m) {
        bbsss.at(m) = bbssss.at(m).at(0);
      }
      
      for (int m = 0; m < maps; ++ m) {
        Regrid (cctkGH, bbsss.at(m), obsss.at(m));
      }
      
      // Count levels
      vector <int> rls (maps);
      for (int m = 0; m < maps; ++ m) {
        rls.at(m) = bbsss.at(m).size();
      }
      int maxrl = 0;
      for (int m = 0; m < maps; ++ m) {
        maxrl = max (maxrl, rls.at(m));
      }
      
      // Make multiprocessor aware
      for (int m = 0; m < maps; ++ m) {
        psss.at(m).resize (bbsss.at(m).size());
      }
      for (int rl = 0; rl < maxrl; ++ rl) {
        vector <vector <ibbox > > bbss (maps);
        vector <vector <bbvect> > obss (maps);
        vector <vector <int   > > pss  (maps);
        for (int m = 0; m < maps; ++ m) {
          if (rl < rls.at(m)) {
            bbss.at(m) = bbsss.at(m).at(rl);
            obss.at(m) = obsss.at(m).at(rl);
            pss .at(m) = psss .at(m).at(rl);
          }
        }
        Carpet::SplitRegionsMaps (cctkGH, bbss, obss, pss);
        for (int m = 0; m < maps; ++ m) {
          if (rl < rls.at(m)) {
            bbsss.at(m).at(rl) = bbss.at(m);
            obsss.at(m).at(rl) = obss.at(m);
            psss .at(m).at(rl) = pss .at(m);
          }
        }
      } // for rl
      
      // Make multigrid aware
      Carpet::MakeMultigridBoxesMaps (cctkGH, bbsss, obsss, bbssss);
      
    } // if do_recompose
    
    return do_recompose;
  }
  
  
  
} // namespace CarpetRegrid2