aboutsummaryrefslogtreecommitdiff
path: root/Carpet/CarpetLib/src/th.cc
blob: 8bd4163e50731616b017f2a79c6247bd3dcc4750 (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
#include <cctk.h>

#include <cassert>
#include <cmath>
#include <iostream>
#include <vector>

#include "defs.hh"
#include "gh.hh"

#include "th.hh"

using namespace std;



set<th*> th::allth;



// Constructors
th::th (gh& h_,
        bool const time_interpolation_during_regridding_)
  : h(h_),
    time_interpolation_during_regridding
    (time_interpolation_during_regridding_),
    timelevels(0)
{
  reffacts.resize(1, 1);
  allth.insert(this);
  h.insert(this);
}

th::th (gh& h_, int const timelevels_, vector<int> const& reffacts_,
        bool const time_interpolation_during_regridding_)
  : h(h_),
    time_interpolation_during_regridding
    (time_interpolation_during_regridding_),
    timelevels(timelevels_), reffacts(reffacts_)
{
  assert (timelevels_ > 0);
  assert (reffacts.size() >= 1);
  assert (reffacts.front() == 1);
  for (size_t n = 1; n < reffacts.size(); ++ n) {
    assert (reffacts.AT(n) >= reffacts.AT(n-1));
    assert (reffacts.AT(n) % reffacts.AT(n-1) == 0);
  }
  allth.insert(this);
  h.insert(this);
}

// Destructors
th::~th ()
{
  h.erase(this);
  allth.erase(this);
}

// Modifiers
void th::regrid ()
{
  CCTK_REAL const basetime  = 0.0;
  CCTK_REAL const basedelta = 1.0;
  
  const int old_mglevels = times.size();
  times.resize(h.mglevels());
  deltas.resize(h.mglevels());
  for (int ml=0; ml<h.mglevels(); ++ml) {
    const int old_reflevels = times.AT(ml).size();
    times.AT(ml).resize(h.reflevels());
    deltas.AT(ml).resize(h.reflevels());
    for (int rl=0; rl<h.reflevels(); ++rl) {
      if (ml==0) {
	deltas.AT(ml).AT(rl) = basedelta / reffacts.AT(rl);
      } else {
	deltas.AT(ml).AT(rl) = deltas.AT(ml-1).AT(rl) * h.mgfact;
      }
      if (old_mglevels==0) {
        times.AT(ml).AT(rl).resize(timelevels);
        for (int tl=0; tl<timelevels; ++tl) {
          times.AT(ml).AT(rl).AT(tl) = basetime - tl * deltas.AT(ml).AT(rl);
        }
      } else if (rl < old_reflevels) {
        // do nothing
      } else if (rl == 0) {
        assert (0);
        times.AT(ml).AT(rl).resize(timelevels);
        for (int tl=0; tl<timelevels; ++tl) {
          times.AT(ml).AT(rl).AT(tl) = basetime - tl * deltas.AT(ml).AT(rl);
        }
      } else {
        if (time_interpolation_during_regridding) {
          // We probably don't want to do this, but it is nice for
          // compatibility
          times.AT(ml).AT(rl).resize(timelevels);
          for (int tl=0; tl<timelevels; ++tl) {
            // linear interpolation between the two surrounding coarse
            // grid times
            assert (reffacts.AT(rl) % reffacts.AT(rl-1) == 0);
            int const rf = reffacts.AT(rl) / reffacts.AT(rl-1);
            int const ctl = tl / rf;
            int const mtl = tl % rf;
            if (mtl == 0) {
              times.AT(ml).AT(rl).AT(tl) = times.AT(ml).AT(rl-1).AT(ctl);
            } else {
              assert (ctl+1<timelevels);
              CCTK_REAL const alpha = (CCTK_REAL)mtl / rf;
              assert (alpha>0 and alpha<1);
              times.AT(ml).AT(rl).AT(tl) =
                (1-alpha) * times.AT(ml).AT(rl-1).AT(ctl  ) +
                (  alpha) * times.AT(ml).AT(rl-1).AT(ctl+1);
            }
          }
        } else {
          times.AT(ml).AT(rl) = times.AT(ml).AT(rl-1);
        }
      }
    }
  }
  for (int ml=0; ml<h.mglevels(); ++ml) {
    for (int rl=0; rl<h.reflevels(); ++rl) {
      for (int tl=1; tl<timelevels; ++tl) {
        assert (times.AT(ml).AT(rl).AT(tl) < times.AT(ml).AT(rl).AT(tl-1));
      }
    }
  }
  for (int ml=0; ml<h.mglevels(); ++ml) {
    for (int rl=0; rl<h.reflevels(); ++rl) {
      // assert (isfinite(deltas.AT(ml).AT(rl)));
      for (int tl=0; tl<timelevels; ++tl) {
        // assert (isfinite(times.AT(ml).AT(rl).AT(tl)));
      }
    }
  }
}

void th::regrid_free ()
{
}



void th::advance_time (int const ml, int const rl)
{
  for (int tl=timelevels-1; tl>0; --tl) {
    set_time (ml,rl,tl, get_time(ml,rl,tl-1));
  }
  set_time (ml,rl,0, get_time(ml,rl,0) + get_delta(ml,rl));
}

void th::retreat_time (int const ml, int const rl)
{
  CCTK_REAL const t = get_time(ml,rl,0);
  for (int tl=0; tl<timelevels-1; ++tl) {
    set_time (ml,rl,tl, get_time(ml,rl,tl+1));
  }
  set_time (ml,rl,timelevels-1, t);
}

void th::flip_timelevels (int const ml, int const rl)
{
  for (int tl=1; tl<(timelevels+1)/2; ++tl) {
    int const tl2 = timelevels - tl;
    CCTK_REAL const t  = get_time(ml,rl,tl );
    CCTK_REAL const t2 = get_time(ml,rl,tl2);
    set_time (ml,rl,tl ,t2);
    set_time (ml,rl,tl2,t );
  }
  set_delta (ml,rl, -get_delta(ml,rl));
}



// Memory usage
size_t
th::
memory ()
  const
{
  return
    memoryof (reffacts) +
    memoryof (times) +
    memoryof (deltas);
}

size_t
th::
allmemory ()
{
  size_t mem = memoryof(allth);
  for (set<th*>::const_iterator
         thi = allth.begin(); thi != allth.end(); ++ thi)
  {
    mem += memoryof(**thi);
  }
  return mem;
}



// Input
istream& th::input (istream& is)
{
  skipws (is);
  consume (is, "th:{");
  consume (is, "timelevels=");
  is >> timelevels;
  consume (is, ",");
  consume (is, "reffacts=");
  is >> reffacts;
  consume (is, ",");
  consume (is, "times=");
  is >> times;
  consume (is, ",");
  consume (is, "deltas=");
  is >> deltas;
  consume (is, "}");
  return is;
}

// Output
ostream& th::output (ostream& os) const
{
  os << "th:{"
     << "timelevels=" << timelevels << ","
     << "reffacts=" << reffacts << ","
     << "times=" << times << ","
     << "deltas=" << deltas << "}";
  return os;
}