3.1-git
DUNE for Multi-{Phase, Component, Scale, Physics, ...} flow and transport in porous media
timemanager.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
3/*****************************************************************************
4 * See the file COPYING for full copying permissions. *
5 * *
6 * This program is free software: you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation, either version 3 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 *****************************************************************************/
24#ifndef DUMUX_TIME_MANAGER_HH
25#define DUMUX_TIME_MANAGER_HH
26
27#warning "This file is deprecated. Use the TimeLoop class in common/timeloop.hh"
28
29#include <algorithm>
30
31#include <dune/common/float_cmp.hh>
32#include <dune/common/timer.hh>
33#include <dune/common/parallel/mpihelper.hh>
34
35#include "properties.hh"
36#include "parameters.hh"
37
38namespace Dumux {
39
61template <class TypeTag>
63{
66
68 {}
69public:
70
71 TimeManager(bool verbose = true)
72 {
73 verbose_ =
74 verbose &&
75 Dune::MPIHelper::getCollectiveCommunication().rank() == 0;
76
77 episodeIndex_ = 0;
78 episodeStartTime_ = 0;
79
80 time_ = 0.0;
81 endTime_ = -1e100;
82
83 timeStepSize_ = 1.0;
84 previousTimeStepSize_ = timeStepSize_;
85 timeStepIdx_ = 0;
86 finished_ = false;
87
88 episodeLength_ = 1e100;
89 }
90
101 void init(Problem &problem,
102 Scalar tStart,
103 Scalar dtInitial,
104 Scalar tEnd,
105 bool restart = false)
106 {
107 timer_.reset();
108
109 problem_ = &problem;
110 time_ = tStart;
111 timeStepSize_ = dtInitial;
112 previousTimeStepSize_ = dtInitial;
113 endTime_ = tEnd;
114
115 if (verbose_)
116 std::cout << "Initializing problem '" << problem_->name() << "'\n";
117
118 // initialize the problem
119 problem_->init();
120
121 // restart problem if necessary
122 if(restart)
123 problem_->restart(tStart);
124 else
125 {
126 // write initial condition (if problem is not restarted)
127 time_ -= timeStepSize_;
128 if (problem_->shouldWriteOutput())
129 problem_->writeOutput();
130 time_ += timeStepSize_;
131 }
132
133 if (verbose_) {
134 int numProcesses = Dune::MPIHelper::getCollectiveCommunication().size();
135 std::cout << "Initialization took " << timer_.elapsed() <<" seconds on "
136 << numProcesses << " processes.\n"
137 << "The cumulative CPU time was " << timer_.elapsed()*numProcesses << " seconds.\n";
138 }
139
140 }
141
153 void setTime(Scalar t)
154 { time_ = t; }
155
162 void setTime(Scalar t, int stepIdx)
163 { time_ = t; timeStepIdx_ = stepIdx; }
164
170 Scalar time() const
171 { return time_; }
172
176 Scalar endTime() const
177 { return endTime_; }
178
184 void setEndTime(Scalar t)
185 { endTime_ = t; }
186
190 double wallTime() const
191 { return timer_.elapsed(); }
192
203 void setTimeStepSize(Scalar dt)
204 {
205 using std::min;
206 timeStepSize_ = min(dt, maxTimeStepSize());
207 }
208
214 Scalar timeStepSize() const
215 { return timeStepSize_; }
216
220 Scalar previousTimeStepSize() const
221 { return previousTimeStepSize_; }
222
227 int timeStepIndex() const
228 { return timeStepIdx_; }
229
237 void setFinished(bool yesno = true)
238 { finished_ = yesno; }
239
246 bool finished() const
247 { return finished_ || time() >= endTime(); }
248
253 bool willBeFinished() const
254 { return finished_ || time() + timeStepSize() >= endTime(); }
255
260 Scalar maxTimeStepSize() const
261 {
262 if (finished())
263 return 0.0;
264
265 using std::max;
266 using std::min;
267 return min(min(episodeMaxTimeStepSize(),
268 problem_->maxTimeStepSize()),
269 max<Scalar>(0.0, endTime() - time()));
270 }
271
272 /*
273 * @}
274 */
275
276
289 void startNextEpisode(Scalar tStart,
290 Scalar len,
291 const std::string &description = "")
292 {
293 ++ episodeIndex_;
294 episodeStartTime_ = tStart;
295 episodeLength_ = len;
296 episodeDescription_ = description;
297 }
298
305 void startNextEpisode(Scalar len = 1e100)
306 {
307 ++ episodeIndex_;
308 episodeStartTime_ = time_;
309 episodeLength_ = len;
310 }
311
317 int episodeIndex() const
318 { return episodeIndex_; }
319
324 Scalar episodeStartTime() const
325 { return episodeStartTime_; }
326
331 Scalar episodeLength() const
332 { return episodeLength_; }
333
338 bool episodeIsFinished() const
339 { return time() >= episodeStartTime_ + episodeLength(); }
340
346 { return time() + timeStepSize() >= episodeStartTime_ + episodeLength(); }
347
353 {
354 // if the current episode is over and the simulation
355 // wants to give it some extra time, we will return
356 // the time step size it suggested instead of trying
357 // to align it to the end of the episode.
358 if (episodeIsFinished())
359 return 0.0;
360
361 // make sure that we don't exceed the end of the
362 // current episode.
363 using std::max;
364 return max<Scalar>(0.0, episodeLength() - (time() - episodeStartTime()));
365 }
366
367 /*
368 * @}
369 */
370
377 void run()
378 {
379 timer_.reset();
380
381 // do the time steps
382 while (!finished())
383 {
384 // pre-process the current solution
385 problem_->preTimeStep();
386
387 // execute the time integration scheme
388 problem_->timeIntegration();
389 Scalar dt = timeStepSize();
390
391 // post-process the current solution
392 problem_->postTimeStep();
393
394 // write the result to disk
395 if (problem_->shouldWriteOutput())
396 problem_->writeOutput();
397
398 // prepare the model for the next time integration
399 problem_->advanceTimeLevel();
400
401 // write restart file if mandated by the problem
402 if (problem_->shouldWriteRestartFile())
403 problem_->serialize();
404
405 // advance the simulated time by the current time step size
406 time_ += dt;
407 ++timeStepIdx_;
408
409 if (verbose_) {
410 std::cout
411 << "Time step "<<timeStepIndex()<<" done. "
412 << "Wall time:"<<timer_.elapsed()
413 <<", time:"<<time()
414 <<", time step size:"<<dt
415 <<"\n";
416 }
417
418 // notify the problem if an episode is finished
419 if (episodeIsFinished()) {
420 //define what to do at the end of an episode in the problem
421 problem_->episodeEnd();
422
423 //check if a time step size was explicitly defined in problem->episodeEnd()
424 if (Dune::FloatCmp::eq<Scalar>(dt, timeStepSize()))
425 {
426 // set the initial time step size of a an episode to the last real time step size before the episode
427 using std::max;
428 Scalar nextDt = max(previousTimeStepSize_, timeStepSize());
429 previousTimeStepSize_ = nextDt;
430 setTimeStepSize(nextDt);
431 }
432 }
433 else
434 {
435 // notify the problem that the time step is done and ask it
436 // for a suggestion for the next time step size
437 // set the time step size for the next step
438 previousTimeStepSize_ = timeStepSize();
439 setTimeStepSize(problem_->nextTimeStepSize(dt));
440 }
441 }
442
443 if (verbose_) {
444 int numProcesses = Dune::MPIHelper::getCollectiveCommunication().size();
445 std::cout << "Simulation took " << timer_.elapsed() <<" seconds on "
446 << numProcesses << " processes.\n"
447 << "The cumulative CPU time was " << timer_.elapsed()*numProcesses << " seconds.\n";
448 }
449 }
450
462 template <class Restarter>
463 void serialize(Restarter &res)
464 {
465 res.serializeSectionBegin("TimeManager");
466 res.serializeStream() << episodeIndex_ << " "
467 << episodeStartTime_ << " "
468 << episodeLength_ << " "
469 << time_ + timeStepSize() << " "
470 << timeStepIdx_ + 1 << " ";
471 res.serializeSectionEnd();
472 }
473
481 template <class Restarter>
482 void deserialize(Restarter &res)
483 {
484 res.deserializeSectionBegin("TimeManager");
485 res.deserializeStream() >> episodeIndex_
486 >> episodeStartTime_
487 >> episodeLength_
488 >> time_
489 >> timeStepIdx_;
490 res.deserializeSectionEnd();
491 }
492
493 /*
494 * @}
495 */
496
497private:
498 Problem *problem_;
499 int episodeIndex_;
500 Scalar episodeStartTime_;
501 Scalar episodeLength_;
502 std::string episodeDescription_;
503
504 Dune::Timer timer_;
505 Scalar time_;
506 Scalar endTime_;
507
508 Scalar timeStepSize_;
509 Scalar previousTimeStepSize_;
510 int timeStepIdx_;
511 bool finished_;
512 bool verbose_;
513};
514}
515
516#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
make the local view function available whenever we use the grid geometry
Definition: adapt.hh:29
typename Properties::Detail::GetPropImpl< TypeTag, Property >::type::type GetPropType
get the type alias defined in the property (equivalent to old macro GET_PROP_TYPE(....
Definition: propertysystem.hh:149
Manages the handling of time dependent problems.
Definition: timemanager.hh:63
bool episodeWillBeFinished() const
Returns true if the current episode will be finished after the current time step.
Definition: timemanager.hh:345
Scalar timeStepSize() const
Returns the suggested time step length so that we don't miss the beginning of the next episode or cr...
Definition: timemanager.hh:214
TimeManager(bool verbose=true)
Definition: timemanager.hh:71
Scalar maxTimeStepSize() const
Aligns dt to the episode boundary or the end time of the simulation.
Definition: timemanager.hh:260
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: timemanager.hh:227
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition: timemanager.hh:324
void deserialize(Restarter &res)
Read the time manager's state from a restart file.
Definition: timemanager.hh:482
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: timemanager.hh:253
double wallTime() const
Returns the current wall time (cpu time).
Definition: timemanager.hh:190
void serialize(Restarter &res)
Write the time manager's state to a restart file.
Definition: timemanager.hh:463
void setTimeStepSize(Scalar dt)
Set the current time step size to a given value.
Definition: timemanager.hh:203
void startNextEpisode(Scalar len=1e100)
Start the next episode, but don't change the episode identifier.
Definition: timemanager.hh:305
Scalar episodeMaxTimeStepSize() const
Aligns the time step size to the episode boundary if the current time step crosses the boundary of th...
Definition: timemanager.hh:352
bool episodeIsFinished() const
Returns true if the current episode is finished at the current time.
Definition: timemanager.hh:338
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: timemanager.hh:184
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: timemanager.hh:162
void run()
Runs the simulation using a given problem class.
Definition: timemanager.hh:377
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition: timemanager.hh:331
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition: timemanager.hh:237
bool finished() const
Returns true if the simulation is finished.
Definition: timemanager.hh:246
int episodeIndex() const
Returns the index of the current episode.
Definition: timemanager.hh:317
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: timemanager.hh:153
Scalar previousTimeStepSize() const
Returns the size of the previous time step .
Definition: timemanager.hh:220
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: timemanager.hh:176
void init(Problem &problem, Scalar tStart, Scalar dtInitial, Scalar tEnd, bool restart=false)
Initialize the model and problem and write the initial condition to disk.
Definition: timemanager.hh:101
void startNextEpisode(Scalar tStart, Scalar len, const std::string &description="")
Change the current episode of the simulation.
Definition: timemanager.hh:289
Scalar time() const
Return the time before the time integration. To get the time after the time integration you have to ...
Definition: timemanager.hh:170
Base file for properties related to sequential models.