aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetEvolutionMask/src/evolution_mask.cc
blob: b355310f074e18bd3c5008c41959c24539a8ae66 (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
#include <cassert>
#include <sstream>

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

#include "carpet.hh"

#include "evolution_mask.hh"



namespace CarpetEvolutionMask {
  
  using namespace std;
  using namespace Carpet;
  
  
  void
  CarpetEvolutionMaskSetup (CCTK_ARGUMENTS)
  {
    DECLARE_CCTK_PARAMETERS;
    
    // ask the flesh what the value of carpet::buffer_width is...
    // this should be fixed at some point...

    CCTK_INT use_outer_buffer_zones = *(CCTK_INT*) CCTK_ParameterGet
      ("use_outer_buffer_zones","Carpet",0);

    CCTK_INT buffer_width;
    if (use_outer_buffer_zones) {
      buffer_width = 0;
    } else {
      buffer_width = *(CCTK_INT*) CCTK_ParameterGet
        ("buffer_width","Carpet",0);
    }
    // cout << "buffer width: " << buffer_width << endl;
    
    if (! is_singlemap_mode()) {
      CCTK_WARN (0, "This routine may only be called in singlemap mode");
    }
    
    if (reflevel > 0) {
      
      ivect const izero = ivect(0);
      ivect const ione  = ivect(1);
      
      gh const & hh = *vhh.at(Carpet::map);
      dh const & dd = *vdd.at(Carpet::map);
      
      ibbox const & base = hh.bases().at(mglevel).at(reflevel).expand(100,100);
      ibbox const & coarsebase = hh.bases().at(mglevel).at(reflevel-1).expand(100,100);
      
      ivect const reffact
        = spacereffacts.at(reflevel) / spacereffacts.at(reflevel-1);
      assert (all (reffact == 2));
      
      // cout << "base: " << base << endl;
      // cout << "coarsebase: " << coarsebase << endl;
      
      // Calculate the union of all refined regions
      ibset refined;
      for (int c=0; c<hh.components(reflevel); ++c) {
	ibbox refcomp = hh.extents().at(mglevel).at(reflevel).at(c);
	bbvect outer_boundary = hh.outer_boundary(reflevel,c);
	ivect expand_right, expand_left;
	for (int d=0;d<dim;d++) {
	  if (outer_boundary[d][0]) {
	    expand_left[d] = 50;
	  } else {
	    expand_left[d] = 0;
	  }
	  if (outer_boundary[d][1]) {
	    expand_right[d] = 50;
	  } else {
	    expand_right[d] = 0;
	  }
	}
	//	cout << "rl,c,expand_left,expand_right" << " " << reflevel << " " << c
	//	     << " " << expand_left << " " << expand_right << endl;
	refcomp = refcomp.expand(expand_left,expand_right);
	refined |= refcomp;
      }
      refined.normalize();
      

      //      cout << "refined: " << refined << endl;

      // calculate inverse set of refined regions on the current level
      ibset antirefined = base - refined;


      //      cout << "antirefined: " << antirefined << endl;

      // now make antibase larger
      // make refined regions SMALLER
      ivect antishrinkby;
      for (int d=0; d<dim;d++) {
	antishrinkby[d] = cctkGH->cctk_nghostzones[d] + buffer_width;
      }
      ibset antishrunk;
      for (ibset::const_iterator bi = antirefined.begin();
             bi != antirefined.end();
             ++bi)
        {
          antishrunk |= (*bi).expand(antishrinkby, antishrinkby).expanded_for(coarsebase);
        }
      antishrunk.normalize();

      //      cout << "antishrunk1: " << antishrunk << endl;

      // now cut away dangling edges
      antishrunk &= coarsebase;

      //      cout << "antishrunk2: " << antishrunk << endl;

      // cut holes into coarsebase
      ibset shrunk = coarsebase - antishrunk;

      //      cout << "shrunk: " << shrunk << endl;

      // Calculate the union of all coarse regions
      ibset parent;
      for (int c=0; c<hh.components(reflevel-1); ++c) {
        parent |= hh.extents().at(mglevel).at(reflevel-1).at(c);
      }
      parent.normalize();

      //      cout << "parent: " << parent << endl;
      
      // Subtract the refined region
      ibset notrefined = parent - shrunk;
      notrefined.normalize();

      //      cout << "notrefined: " << notrefined << endl;

      ivect enlargeby;
      int stencil_size = dd.prolongation_stencil_size();
      for (int d=0; d<dim;d++) {
	enlargeby[d] = cctkGH->cctk_nghostzones[d] + buffer_width +
	  stencil_size;
      }
      ibset enlarged;
      for (ibset::const_iterator bi = notrefined.begin();
             bi != notrefined.end();
             ++bi)
        {
          enlarged |= (*bi).expand(enlargeby, enlargeby);
        }
      enlarged.normalize();

      //      cout << "enlarged: " << enlarged << endl;
      
      // Intersect with the original union      
      ibset evolveon = parent & enlarged;
      evolveon.normalize();

      ibset notevolveon = parent - evolveon;
      notevolveon.normalize();

      //      cout << "notevolveon: " << notevolveon << endl;
      
      // Set not evolved region on next coarser level
      {
        int const oldreflevel = reflevel;
        int const oldgrouptype = mc_grouptype;
        int const oldmap = Carpet::map;
        leave_singlemap_mode (cctkGH);
        leave_level_mode (cctkGH);
        enter_level_mode (cctkGH, oldreflevel-1);
        enter_singlemap_mode (cctkGH, oldmap, oldgrouptype);
        
        BEGIN_LOCAL_COMPONENT_LOOP (cctkGH, CCTK_GF) {
          
          DECLARE_CCTK_ARGUMENTS;
          
          ibbox const & ext
            = dd.boxes.at(mglevel).at(reflevel).at(component).exterior;
          
          for (ibset::const_iterator bi = notevolveon.begin();
               bi != notevolveon.end();
               ++bi)
          {
            
            ibbox const & box = (*bi) & ext;
            if (! box.empty()) {
              
              assert (all ((box.lower() - ext.lower()               ) >= 0));
              assert (all ((box.upper() - ext.lower() + ext.stride()) >= 0));
              assert (all ((box.lower() - ext.lower()               ) % ext.stride() == 0));
              assert (all ((box.upper() - ext.lower() + ext.stride()) % ext.stride() == 0));
              ivect const imin = (box.lower() - ext.lower()               ) / ext.stride();
              ivect const imax = (box.upper() - ext.lower() + ext.stride()) / ext.stride();
              assert (all (izero <= imin));
              assert (box.empty() || all (imin <= imax));
              assert (all (imax <= ivect::ref(cctk_lsh)));
              
              if (verbose) {
                ostringstream buf;
                buf << "Setting restricted region on level " << reflevel << " to weight 0: " << imin << ":" << imax-ione;
                CCTK_INFO (buf.str().c_str());
              }
              
              // Set mask in the restricted region to 0
              assert (dim == 3);
              for (int k=imin[2]; k<imax[2]; ++k) {
                for (int j=imin[1]; j<imax[1]; ++j) {
                  for (int i=imin[0]; i<imax[0]; ++i) {
                    int const ind = CCTK_GFINDEX3D (cctkGH, i, j, k);
                    evolution_mask[ind] = 0;
                  }
                }
              }
              
            } // if box not empty
              
          } // for box
          
        } END_LOCAL_COMPONENT_LOOP;
        
        leave_singlemap_mode (cctkGH);
        leave_level_mode (cctkGH);
        enter_level_mode (cctkGH, oldreflevel);
        enter_singlemap_mode (cctkGH, oldmap, oldgrouptype);
      }
      
    } // if reflevel>0
  }
  
} // namespace CarpetEvolutionMask