aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Requirements/src/Requirements.cc
blob: f94e8bf41acf82b573744b8b8374dc902ec23d0f (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
#include <Requirements.hh>

#include <cctk.h>
#include <cctk_Parameters.h>
#include <cctk_Functions.h>
#include <cctk_Schedule.h>
#include <cctki_GHExtensions.h>
#include <cctki_Schedule.h>
#include <util_String.h>

#include <algorithm>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>

#include <all_clauses.hh>
#include <clause.hh>
#include <clauses.hh>
#include <location.hh>
#include <util.hh>
#include <gridpoint.hh>
#include <all_state.hh>

using namespace std;

namespace Requirements {
   
  // Rules:
  //
  // 1. Everything that is required by a routine must be provided by
  //    another routine which is scheduled earlier.
  //
  // 2. Things can be provided only once, not multiple times.
  //    Except when they are also required.
    
  inline ostream& operator<< (ostream& os, const all_clauses_t& a) {
    a.output(os);
    return os;
  }
  
  all_clauses_t all_clauses;
  all_state_t all_state;
  
  void Setup(int const maps)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Setup maps=%d", maps);
      }
      all_state.setup(maps);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  void ChangeStorage(vector<int> const& groups,
                     vector<int> const& timelevels,
                     int const reflevel)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        std::ostringstream stream;
        stream << "groups: " << groups << " timelevels: " << timelevels;
        CCTK_VInfo(CCTK_THORNSTRING,
                   "ChangeStorage reflevel=%d %s",
                   reflevel, stream.str().c_str());
      }
      all_state.change_storage(groups, timelevels, reflevel);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }

  void Regrid(int const reflevels)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Regrid reflevels=%d", reflevels);
      }
      all_state.regrid(reflevels);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }

  void Recompose(int const reflevel, valid::valid_t const where)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Recompose reflevel=%d where=%s",
                   reflevel,
                   where == valid::nowhere    ? "nowhere"    :
                   where == valid::interior   ? "interior"   :
                   where == valid::everywhere ? "everywhere" :
                   NULL);
      }
      all_state.recompose(reflevel, where);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }

  void RegridFree()
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "RegridFree");
      }
      all_state.regrid_free();
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  void Cycle(int const reflevel)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Cycle reflevel=%d", reflevel);
      }
      all_state.cycle(reflevel);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
   
  void BeforeRoutine(cFunctionData const* const function_data,
                     int const reflevel, int const map,
                     int const timelevel, int const timelevel_offset)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      all_state.before_routine(function_data, all_clauses,
                               reflevel, map, timelevel, timelevel_offset);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  void AfterRoutine(cFunctionData const* const function_data, CCTK_INT cctk_iteration,
                    int const reflevel, int const map,
                    int const timelevel, int const timelevel_offset)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      all_state.after_routine(function_data, all_clauses, cctk_iteration,
                              reflevel, map, timelevel, timelevel_offset);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  void Sync(cFunctionData const* const function_data,
            CCTK_INT cctk_iteration,
            vector<int> const& groups,
            int const reflevel, int const timelevel)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Sync reflevel=%d timelevel=%d",
                   reflevel, timelevel);
      }
      all_state.sync(function_data, cctk_iteration, groups, reflevel, timelevel);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  void Restrict(vector<int> const& groups, CCTK_INT const cctk_iteration, int const reflevel)
  {
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      if (verbose) {
        CCTK_VInfo(CCTK_THORNSTRING,
                   "Restrict reflevel=%d",
                   reflevel);
      }
      all_state.restrict1(groups, cctk_iteration, reflevel);
    }
    if (inconsistencies_are_fatal and gridpoint_t::there_was_an_error) {
      CCTK_WARN(CCTK_WARN_ABORT,
                "Aborting because schedule clauses were not satisfied");
    }
  }
  
  ////////////////////////////////////////////////////////////////////////////
  
  
  // Check that the grid is in the correct state, i.e. all necessary
  // parts are valid, for the "current" function. 
  extern "C" 
  void Carpet_Requirements_CheckReads(CCTK_POINTER_TO_CONST const cctkGH_,
                                      CCTK_INT const nvars,
                                      CCTK_INT const* const varinds,
                                      char const* const clause)
  {
    cGH const* const cctkGH = static_cast<cGH const*>(cctkGH_);
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      // TODO: come up with a scheme to avoid constructing and destroying clauses
      cFunctionData const* const function_data = 
          CCTK_ScheduleQueryCurrentFunction(cctkGH);
      int const reflevel = GetRefinementLevel(cctkGH);
      int const map = GetMap(cctkGH);
      int const timelevel = GetTimeLevel(cctkGH);
      int const timelevel_offset = GetTimeLevelOffset(cctkGH);
      // TODO: design an interface to all_state.before_routine that operates
      //       on indices and clauses directly
      for (int v=0; v<nvars; ++v) { 
        cFunctionData temp_function_data = *function_data;
        char* const fullname = CCTK_FullName(varinds[v]);
        char* reads;
        int const len_written =
          Util_asprintf(&reads, "%s(%s)", fullname, clause);
        assert(len_written > 0);
        temp_function_data.n_WritesClauses = 0;
        temp_function_data.WritesClauses = NULL;
        temp_function_data.n_ReadsClauses = 1;
        temp_function_data.ReadsClauses = (char const**)&reads;
        all_clauses.get_clauses(&temp_function_data);
        BeforeRoutine(&temp_function_data,
                      reflevel, map, timelevel, timelevel_offset);
        all_clauses.remove_clauses(&temp_function_data);
        free(fullname);
        free(reads);
      }
    }
  }
  
  // Register the fact that certain parts of the grid have been
  // written in certain variables due to executing the "current"
  // function.
  extern "C" 
  void Carpet_Requirements_NotifyWrites(CCTK_POINTER_TO_CONST const cctkGH_,
                                        CCTK_INT const nvars,
                                        CCTK_INT const* const varinds,
                                        char const* const clause)
  {
    cGH const* const cctkGH = static_cast<cGH const*>(cctkGH_);
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      // TODO: come up with a scheme to avoid constructing and destroying clauses
      cFunctionData const* const function_data = 
          CCTK_ScheduleQueryCurrentFunction(cctkGH);
      int const reflevel = GetRefinementLevel(cctkGH);
      int const map = GetMap(cctkGH);
      int const timelevel = GetTimeLevel(cctkGH);
      int const timelevel_offset = GetTimeLevelOffset(cctkGH);
      // TODO: design an interface to all_state.before_routine that operates
      //       on indices and claues directly
      for (int v=0; v<nvars; ++v) { 
        cFunctionData temp_function_data = *function_data;
        char* const fullname = CCTK_FullName(varinds[v]);
        char* writes;
        int const len_written =
          Util_asprintf(&writes, "%s(%s)", fullname, clause);
        assert(len_written > 0);
        temp_function_data.n_WritesClauses = 1;
        temp_function_data.WritesClauses = (char const**)&writes;
        temp_function_data.n_ReadsClauses = 0;
        temp_function_data.ReadsClauses = NULL;
        all_clauses.get_clauses(&temp_function_data);
        AfterRoutine(&temp_function_data, cctkGH->cctk_iteration,
                     reflevel, map, timelevel, timelevel_offset);
        all_clauses.remove_clauses(&temp_function_data);
        free(fullname);
        free(writes);
      }
    }
  }
  
  extern "C"
  void Carpet_Requirements_Invalidate(CCTK_POINTER_TO_CONST const cctkGH_,
                                      CCTK_INT const nvars,
                                      CCTK_INT const* const varinds)
  {
    cGH const* const cctkGH = static_cast<cGH const*>(cctkGH_);
    DECLARE_CCTK_PARAMETERS;
    if (check_requirements) {
      vector<int> vars(nvars);
      for (int v=0; v<nvars; ++v) {
        vars.AT(v) = varinds[v];
      }
      int const reflevel = GetRefinementLevel(cctkGH);
      int const map = GetMap(cctkGH);
      int const timelevel = GetTimeLevel(cctkGH);
      all_state.invalidate(vars, reflevel, map, timelevel);
    }
  }
  
  
  
  ////////////////////////////////////////////////////////////////////////////
  
  
  
  // scheduled routines to handle boundary and symmetry conditions
  extern "C"
  void CarpetCheckReadsBeforeBoundary(CCTK_ARGUMENTS)
  {
    DECLARE_CCTK_ARGUMENTS;
    int num_vars, err;
    vector<CCTK_INT> vars, faces, widths, tables;

    num_vars = Boundary_SelectedGVs(cctkGH, 0, NULL, NULL, NULL, NULL, NULL);
    if (num_vars < 0) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error retrieving number of selected GVs: %d", num_vars);
    }
    vars.resize(num_vars);
    faces.resize(num_vars);
    widths.resize(num_vars);
    tables.resize(num_vars);

    /* get selected vars for all bc */
    err = Boundary_SelectedGVs(cctkGH, num_vars, &vars[0], &faces[0], &widths[0], &tables[0],
                                    NULL);
    if (err<0) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error in Boundary_SelectedGVs for all boundary conditions");
    } else if (err != num_vars) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Boundary_SelectedGVs returned %d selected variables for "
                 "all boundary conditions, but %d expected\n", err,
                 num_vars);
    }

    Requirements_CheckReads(cctkGH, num_vars, &vars[0], "interior");
  }

  extern "C"
  void CarpetNotifyWritesAfterBoundary(CCTK_ARGUMENTS)
  {
    DECLARE_CCTK_ARGUMENTS;
    int num_vars, err;
    vector<CCTK_INT> vars, faces, widths, tables;

    num_vars = Boundary_SelectedGVs(cctkGH, 0, NULL, NULL, NULL, NULL, NULL);
    if (num_vars < 0) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error retrieving number of selected GVs: %d", num_vars);
    }
    vars.resize(num_vars);
    faces.resize(num_vars);
    widths.resize(num_vars);
    tables.resize(num_vars);

    /* get selected vars for all bc */
    err = Boundary_SelectedGVs(cctkGH, num_vars, &vars[0], &faces[0], &widths[0], &tables[0],
                                    NULL);
    if (err<0) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Error in Boundary_SelectedGVs for all boundary conditions");
    } else if (err != num_vars) {
      CCTK_VWarn(0, __LINE__, __FILE__, CCTK_THORNSTRING,
                 "Boundary_SelectedGVs returned %d selected variables for "
                 "all boundary conditions, but %d expected\n", err,
                 num_vars);
    }

    Requirements_NotifyWrites(cctkGH, num_vars, &vars[0], "boundary;boundary_ghostzones");
  }
  
} // namespace Requirements