aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetRegrid/src/regrid.cc
blob: 979b96f395f5b30f4f899647dbacf2c8e30a961d (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
#include <assert.h>

#include <sstream>
#include <string>

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

#include "gh.hh"
#include "vect.hh"

#include "carpet.hh"
#include "regrid.hh"

extern "C" {
  static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/CarpetRegrid/src/regrid.cc,v 1.45 2004/05/31 19:04:00 schnetter Exp $";
  CCTK_FILEVERSION(Carpet_CarpetRegrid_regrid_cc);
}



namespace CarpetRegrid {
  
  using namespace std;
  using namespace Carpet;
  
  CCTK_INT CarpetRegrid_Regrid (CCTK_POINTER_TO_CONST const cctkGH_,
                                CCTK_POINTER const bbsss_,
                                CCTK_POINTER const obss_,
                                CCTK_POINTER const pss_)
  {
    DECLARE_CCTK_PARAMETERS;
    
    const cGH * const cctkGH = (const cGH *) cctkGH_;
    
    gh<dim>::rexts  & bbsss = * (gh<dim>::rexts  *) bbsss_;
    gh<dim>::rbnds  & obss  = * (gh<dim>::rbnds  *) obss_;
    gh<dim>::rprocs & pss   = * (gh<dim>::rprocs *) pss_;
    
    gh<dim> const & hh = *vhh.at(Carpet::map);
    
    assert (is_singlemap_mode());
    
    
    
    assert (regrid_every == -1 || regrid_every == 0
	    || regrid_every % maxmglevelfact == 0);
    
    // Return if no regridding is desired
    if (regrid_every == -1) return 0;
    
    // Return if we want to regrid during initial data only, and this
    // is not the time for initial data
    if (regrid_every == 0 && cctkGH->cctk_iteration != 0) return 0;

    // Return if we want to regrid regularly, but not at this time
    if (regrid_every > 0 && cctkGH->cctk_iteration != 0
	&& (cctkGH->cctk_iteration-1) % regrid_every != 0)
    {
      return 0;
    }
    
    
    
    // Steer parameters
    const int oldnumlevels = refinement_levels;
    if (CCTK_EQUALS(activate_levels_on_regrid, "none")) {
      
      // do nothing
      
    } else if (CCTK_EQUALS(activate_levels_on_regrid, "fixed")) {
      
      if (cctkGH->cctk_iteration-1 >= activate_next) {
        const int newnumlevels
          = min(refinement_levels + num_new_levels, maxreflevels);
        assert (newnumlevels>0 && newnumlevels<=maxreflevels);
        
        *const_cast<CCTK_INT*>(&activate_next) = cctkGH->cctk_iteration;
        ostringstream next;
        next << activate_next;
        CCTK_ParameterSet
          ("activate_next", "CarpetRegrid", next.str().c_str());
        
        *const_cast<CCTK_INT*>(&refinement_levels) = newnumlevels;
        ostringstream param;
        param << refinement_levels;
        CCTK_ParameterSet
          ("refinement_levels", "CarpetRegrid", param.str().c_str());
        
        if (verbose) {
          ostringstream buf1, buf2;
          buf1 << "Activating " << newnumlevels - oldnumlevels << " new refinement levels";
          buf2 << "There are now " << newnumlevels << " refinement levels";
          CCTK_INFO (buf1.str().c_str());
          CCTK_INFO (buf2.str().c_str());
        }
      }
      
    } else if (CCTK_EQUALS(activate_levels_on_regrid, "function")) {
      
      if (! CCTK_IsFunctionAliased("RegridLevel")) {
        CCTK_WARN (0, "No thorn has provided the function \"RegridLevel\"");
      }
      const int newnumlevels
        = RegridLevel (cctkGH, refinement_levels, maxreflevels);
      if (newnumlevels>0 && newnumlevels<=maxreflevels) {
        
        *const_cast<CCTK_INT*>(&refinement_levels) = newnumlevels;
        ostringstream param;
        param << refinement_levels;
        CCTK_ParameterSet
          ("refinement_levels", "CarpetRegrid", param.str().c_str());
        
        if (verbose) {
          ostringstream buf1, buf2;
          buf1 << "Activating " << newnumlevels - oldnumlevels << " new refinement levels";
          buf2 << "There are now " << newnumlevels << " refinement levels";
          CCTK_INFO (buf1.str().c_str());
          CCTK_INFO (buf2.str().c_str());
        }
        
      } else {
        CCTK_VWarn (1, __LINE__, __FILE__, CCTK_THORNSTRING,
                    "The aliased function \"RegridLevel\" returned an illegal number of refinement levels (%d).  No levels will be activated or deactivated.", newnumlevels);
      }
      
    } else {
      
      assert (0);
      
    }
    
    
    
    // Return if this is not during initial data generation, and if no
    // change in the grid structure is desired
    if (cctkGH->cctk_iteration != 0) {
      if (keep_same_grid_structure) return refinement_levels != oldnumlevels;
    }
    
    
    
    int do_recompose;
    
    if (CCTK_EQUALS(refined_regions, "none")) {
      
      do_recompose = BaseLevel (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "centre")) {
      
      do_recompose = Centre (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "manual-gridpoints")) {
      
      do_recompose
        = ManualGridpoints (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "manual-coordinates")) {
      
      do_recompose
        = ManualCoordinates (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "manual-gridpoint-list")) {
      
      do_recompose
        = ManualGridpointList (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "manual-coordinate-list")) {
      
      do_recompose
        = ManualCoordinateList (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "moving")) {
      
      do_recompose = Moving (cctkGH, hh, bbsss, obss, pss);
                 
    } else if (CCTK_EQUALS(refined_regions, "automatic")) {
      
      do_recompose = Automatic (cctkGH, hh, bbsss, obss, pss);
                 
    } else {
      assert (0);
    }
    
    return do_recompose;
  }
  
} // namespace CarpetRegrid