aboutsummaryrefslogtreecommitdiff
path: root/Carpet/Carpet/src/Evolve.cc
blob: 672d0b16088c8076760056e571149fd8d6729a3a (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
#include <assert.h>
#include <stdlib.h>

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

#ifdef HAVE_TIME_GETTIMEOFDAY
#  ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#  endif
#  include <unistd.h>
#endif

#include "dist.hh"
#include "th.hh"

#include "carpet.hh"

extern "C" {
  static const char* rcsid = "$Header: /home/eschnett/C/carpet/Carpet/Carpet/Carpet/src/Evolve.cc,v 1.28 2003/08/10 21:59:51 schnetter Exp $";
  CCTK_FILEVERSION(Carpet_Carpet_Evolve_cc);
}



namespace Carpet {
  
  using namespace std;
  
  
  
  static double initial_time;
  
  
  
  static bool do_terminate (const cGH *cgh,
                            const CCTK_REAL time, const int iteration)
  {
    DECLARE_CCTK_PARAMETERS;
    
    bool term;
    
    // Early shortcut
    if (terminate_next || CCTK_TerminationReached(cgh)) {
      
      term = true;
      
    } else {
      
      const bool term_iter = iteration >= cctk_itlast;
      const bool term_time = ((cctk_initial_time < cctk_final_time
                              ? time >= cctk_final_time
                              : time <= cctk_final_time)
                              && iteration % maxreflevelfact == 0);
      double runtime;
#ifdef HAVE_TIME_GETTIMEOFDAY
      // get the current time
      struct timeval tv;
      gettimeofday (&tv, 0);
      runtime = (tv.tv_sec + tv.tv_usec / 1e6) - initial_time;
#else
      runtime = 0;
#endif
      const bool term_runtime = (max_runtime > 0
                                 && runtime >= 60.0 * max_runtime);
      
      if (CCTK_Equals(terminate, "never")) {
        term = false;
      } else if (CCTK_Equals(terminate, "iteration")) {
        term = term_iter;
      } else if (CCTK_Equals(terminate, "time")) {
        term = term_time;
      } else if (CCTK_Equals(terminate, "runtime")) {
        term = term_runtime;
      } else if (CCTK_Equals(terminate, "any")) {
        term = term_iter || term_time || term_runtime;
      } else if (CCTK_Equals(terminate, "all")) {
        term = term_iter && term_time && term_runtime;
      } else if (CCTK_Equals(terminate, "either")) {
        term = term_iter || term_time;
      } else if (CCTK_Equals(terminate, "both")) {
        term = term_iter && term_time;
      } else {
        assert (0);
      }
      
    }
    
    {
      int local, global;
      local = term;
      MPI_Allreduce (&local, &global, 1, MPI_INT, MPI_LOR, dist::comm);
      term = global;
    }
    
    return term;
  }
  
  
  
  int Evolve (tFleshConfig* fc)
  {
    DECLARE_CCTK_PARAMETERS;
    
    Waypoint ("starting Evolve...");
    
    const int convlev = 0;
    cGH* cgh = fc->GH[convlev];
    
#ifdef HAVE_TIME_GETTIMEOFDAY
    // get the starting time
    struct timeval tv;
    gettimeofday (&tv, 0);
    initial_time = tv.tv_sec + tv.tv_usec / 1e6;
#else
    initial_time = 0;
#endif
    
    int next_global_mode_iter_loop1 = 0;
    int next_global_mode_iter_loop2 = 0;
    int next_global_mode_iter_loop3 = 0;
    
    // Main loop
    while (! do_terminate(cgh, refleveltimes[0], cgh->cctk_iteration)) {
      
      // Advance time
      ++cgh->cctk_iteration;
      
      Waypoint ("Evolving iteration %d...", cgh->cctk_iteration);
      
      BEGIN_REFLEVEL_LOOP(cgh) {
	const int do_every = maxreflevelfact/reflevelfact;
	if ((cgh->cctk_iteration-1) % do_every == 0) {
	  
	  BEGIN_MGLEVEL_LOOP(cgh) {
	    const int do_every = mglevelfact * maxreflevelfact/reflevelfact;
	    if ((cgh->cctk_iteration-1) % do_every == 0) {
	      
              do_global_mode
                = cgh->cctk_iteration >= next_global_mode_iter_loop1;
              next_global_mode_iter_loop1 = cgh->cctk_iteration + 1;
              
	      // Advance level times
	      tt->advance_time (reflevel, mglevel);
              cgh->cctk_time = (cctk_initial_time
                                + (tt->time (0, reflevel, mglevel)
                                   * cgh->cctk_delta_time));
	      
	      Waypoint ("%*sCurrent time is %g, delta is %g%s", 2*reflevel, "",
			cgh->cctk_time,
                        cgh->cctk_delta_time / cgh->cctk_timefac,
                        do_global_mode ? "   (global time)" : "");
	      
	      // Cycle time levels
	      CycleTimeLevels (cgh);
	      
	      // Checking
	      CalculateChecksums (cgh, allbutcurrenttime);
	      Poison (cgh, currenttimebutnotifonly);
	      
	      // Evolve
	      Waypoint ("%*sScheduling PRESTEP", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("CCTK_PRESTEP", cgh, CallFunction);
	      Waypoint ("%*sScheduling EVOL", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("CCTK_EVOL", cgh, CallFunction);
	      Waypoint ("%*sScheduling POSTSTEP", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("CCTK_POSTSTEP", cgh, CallFunction);
              
	      // Checking
	      PoisonCheck (cgh, currenttime);
	      
	    }
	  } END_MGLEVEL_LOOP;
	  
	  // Regrid
	  Waypoint ("%*sRegrid", 2*reflevel, "");
	  Regrid (cgh);
          
	}
      } END_REFLEVEL_LOOP;
      
      
      
      BEGIN_REVERSE_REFLEVEL_LOOP(cgh) {
	const int do_every = maxreflevelfact/reflevelfact;
	if (cgh->cctk_iteration % do_every == 0) {
	  
	  BEGIN_MGLEVEL_LOOP(cgh) {
	    const int do_every = mglevelfact * maxreflevelfact/reflevelfact;
	    if (cgh->cctk_iteration % do_every == 0) {
	      
              do_global_mode
                = cgh->cctk_iteration >= next_global_mode_iter_loop2;
              next_global_mode_iter_loop2 = cgh->cctk_iteration + 1;
              
	      // Restrict
	      Waypoint ("%*sCurrent time is %g, delta is %g%s", 2*reflevel, "",
			cgh->cctk_time,
                        cgh->cctk_delta_time / cgh->cctk_timefac,
                        do_global_mode ? "   (global time)" : "");
	      Restrict (cgh);
              
	      Waypoint ("%*sScheduling PostRestrict", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("PostRestrict", cgh, CallFunction);
	      
	      // Checking
	      CalculateChecksums (cgh, currenttime);
	      
	    }
	  } END_MGLEVEL_LOOP;
	  
	}
      } END_REVERSE_REFLEVEL_LOOP;
      
      
      
      BEGIN_REFLEVEL_LOOP(cgh) {
	const int do_every = maxreflevelfact/reflevelfact;
	if (cgh->cctk_iteration % do_every == 0) {
	  
	  BEGIN_MGLEVEL_LOOP(cgh) {
	    const int do_every = mglevelfact * maxreflevelfact/reflevelfact;
	    if (cgh->cctk_iteration % do_every == 0) {
	      
              do_global_mode
                = cgh->cctk_iteration >= next_global_mode_iter_loop3;
              next_global_mode_iter_loop3 = cgh->cctk_iteration + 1;
	      
	      Waypoint ("%*sCurrent time is %g, delta is %g%s", 2*reflevel, "",
			cgh->cctk_time,
                        cgh->cctk_delta_time / cgh->cctk_timefac,
                        do_global_mode ? "   (global time)" : "");
              
	      // Checkpoint
	      Waypoint ("%*sScheduling CHECKPOINT", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("CCTK_CHECKPOINT", cgh, CallFunction);
	      
	      // Analysis
	      Waypoint ("%*sScheduling ANALYSIS", 2*reflevel, "");
	      CCTK_ScheduleTraverse ("CCTK_ANALYSIS", cgh, CallFunction);
	      
	      // Output
	      Waypoint ("%*sOutputGH", 2*reflevel, "");
	      CCTK_OutputGH (cgh);
	      
	      // Checking
	      CheckChecksums (cgh, alltimes);
	      
	    }
	  } END_MGLEVEL_LOOP;
	  
	}
      } END_REFLEVEL_LOOP;
      
    } // main loop
    
    Waypoint ("done with Evolve.");
    
    return 0;
  }
  
} // namespace Carpet