3.2-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
60template <class TypeTag>
62{
65
67 {}
68public:
69
70 TimeManager(bool verbose = true)
71 {
72 verbose_ =
73 verbose &&
74 Dune::MPIHelper::getCollectiveCommunication().rank() == 0;
75
76 episodeIndex_ = 0;
77 episodeStartTime_ = 0;
78
79 time_ = 0.0;
80 endTime_ = -1e100;
81
82 timeStepSize_ = 1.0;
83 previousTimeStepSize_ = timeStepSize_;
84 timeStepIdx_ = 0;
85 finished_ = false;
86
87 episodeLength_ = 1e100;
88 }
89
100 void init(Problem &problem,
101 Scalar tStart,
102 Scalar dtInitial,
103 Scalar tEnd,
104 bool restart = false)
105 {
106 timer_.reset();
107
108 problem_ = &problem;
109 time_ = tStart;
110 timeStepSize_ = dtInitial;
111 previousTimeStepSize_ = dtInitial;
112 endTime_ = tEnd;
113
114 if (verbose_)
115 std::cout << "Initializing problem '" << problem_->name() << "'\n";
116
117 // initialize the problem
118 problem_->init();
119
120 // restart problem if necessary
121 if(restart)
122 problem_->restart(tStart);
123 else
124 {
125 // write initial condition (if problem is not restarted)
126 time_ -= timeStepSize_;
127 if (problem_->shouldWriteOutput())
128 problem_->writeOutput();
129 time_ += timeStepSize_;
130 }
131
132 if (verbose_) {
133 int numProcesses = Dune::MPIHelper::getCollectiveCommunication().size();
134 std::cout << "Initialization took " << timer_.elapsed() <<" seconds on "
135 << numProcesses << " processes.\n"
136 << "The cumulative CPU time was " << timer_.elapsed()*numProcesses << " seconds.\n";
137 }
138
139 }
140
152 void setTime(Scalar t)
153 { time_ = t; }
154
161 void setTime(Scalar t, int stepIdx)
162 { time_ = t; timeStepIdx_ = stepIdx; }
163
169 Scalar time() const
170 { return time_; }
171
175 Scalar endTime() const
176 { return endTime_; }
177
183 void setEndTime(Scalar t)
184 { endTime_ = t; }
185
189 double wallTime() const
190 { return timer_.elapsed(); }
191
202 void setTimeStepSize(Scalar dt)
203 {
204 using std::min;
205 timeStepSize_ = min(dt, maxTimeStepSize());
206 }
207
213 Scalar timeStepSize() const
214 { return timeStepSize_; }
215
219 Scalar previousTimeStepSize() const
220 { return previousTimeStepSize_; }
221
226 int timeStepIndex() const
227 { return timeStepIdx_; }
228
236 void setFinished(bool yesno = true)
237 { finished_ = yesno; }
238
245 bool finished() const
246 { return finished_ || time() >= endTime(); }
247
252 bool willBeFinished() const
253 { return finished_ || time() + timeStepSize() >= endTime(); }
254
259 Scalar maxTimeStepSize() const
260 {
261 if (finished())
262 return 0.0;
263
264 using std::max;
265 using std::min;
266 return min(min(episodeMaxTimeStepSize(),
267 problem_->maxTimeStepSize()),
268 max<Scalar>(0.0, endTime() - time()));
269 }
270
271 /*
272 * @}
273 */
274
275
288 void startNextEpisode(Scalar tStart,
289 Scalar len,
290 const std::string &description = "")
291 {
292 ++ episodeIndex_;
293 episodeStartTime_ = tStart;
294 episodeLength_ = len;
295 episodeDescription_ = description;
296 }
297
304 void startNextEpisode(Scalar len = 1e100)
305 {
306 ++ episodeIndex_;
307 episodeStartTime_ = time_;
308 episodeLength_ = len;
309 }
310
316 int episodeIndex() const
317 { return episodeIndex_; }
318
323 Scalar episodeStartTime() const
324 { return episodeStartTime_; }
325
330 Scalar episodeLength() const
331 { return episodeLength_; }
332
337 bool episodeIsFinished() const
338 { return time() >= episodeStartTime_ + episodeLength(); }
339
345 { return time() + timeStepSize() >= episodeStartTime_ + episodeLength(); }
346
352 {
353 // if the current episode is over and the simulation
354 // wants to give it some extra time, we will return
355 // the time step size it suggested instead of trying
356 // to align it to the end of the episode.
357 if (episodeIsFinished())
358 return 0.0;
359
360 // make sure that we don't exceed the end of the
361 // current episode.
362 using std::max;
363 return max<Scalar>(0.0, episodeLength() - (time() - episodeStartTime()));
364 }
365
366 /*
367 * @}
368 */
369
376 void run()
377 {
378 timer_.reset();
379
380 // do the time steps
381 while (!finished())
382 {
383 // pre-process the current solution
384 problem_->preTimeStep();
385
386 // execute the time integration scheme
387 problem_->timeIntegration();
388 Scalar dt = timeStepSize();
389
390 // post-process the current solution
391 problem_->postTimeStep();
392
393 // write the result to disk
394 if (problem_->shouldWriteOutput())
395 problem_->writeOutput();
396
397 // prepare the model for the next time integration
398 problem_->advanceTimeLevel();
399
400 // write restart file if mandated by the problem
401 if (problem_->shouldWriteRestartFile())
402 problem_->serialize();
403
404 // advance the simulated time by the current time step size
405 time_ += dt;
406 ++timeStepIdx_;
407
408 if (verbose_) {
409 std::cout
410 << "Time step "<<timeStepIndex()<<" done. "
411 << "Wall time:"<<timer_.elapsed()
412 <<", time:"<<time()
413 <<", time step size:"<<dt
414 <<"\n";
415 }
416
417 // notify the problem if an episode is finished
418 if (episodeIsFinished()) {
419 //define what to do at the end of an episode in the problem
420 problem_->episodeEnd();
421
422 //check if a time step size was explicitly defined in problem->episodeEnd()
423 if (Dune::FloatCmp::eq<Scalar>(dt, timeStepSize()))
424 {
425 // set the initial time step size of a an episode to the last real time step size before the episode
426 using std::max;
427 Scalar nextDt = max(previousTimeStepSize_, timeStepSize());
428 previousTimeStepSize_ = nextDt;
429 setTimeStepSize(nextDt);
430 }
431 }
432 else
433 {
434 // notify the problem that the time step is done and ask it
435 // for a suggestion for the next time step size
436 // set the time step size for the next step
437 previousTimeStepSize_ = timeStepSize();
438 setTimeStepSize(problem_->nextTimeStepSize(dt));
439 }
440 }
441
442 if (verbose_) {
443 int numProcesses = Dune::MPIHelper::getCollectiveCommunication().size();
444 std::cout << "Simulation took " << timer_.elapsed() <<" seconds on "
445 << numProcesses << " processes.\n"
446 << "The cumulative CPU time was " << timer_.elapsed()*numProcesses << " seconds.\n";
447 }
448 }
449
461 template <class Restarter>
462 void serialize(Restarter &res)
463 {
464 res.serializeSectionBegin("TimeManager");
465 res.serializeStream() << episodeIndex_ << " "
466 << episodeStartTime_ << " "
467 << episodeLength_ << " "
468 << time_ + timeStepSize() << " "
469 << timeStepIdx_ + 1 << " ";
470 res.serializeSectionEnd();
471 }
472
480 template <class Restarter>
481 void deserialize(Restarter &res)
482 {
483 res.deserializeSectionBegin("TimeManager");
484 res.deserializeStream() >> episodeIndex_
485 >> episodeStartTime_
486 >> episodeLength_
487 >> time_
488 >> timeStepIdx_;
489 res.deserializeSectionEnd();
490 }
491
492 /*
493 * @}
494 */
495
496private:
497 Problem *problem_;
498 int episodeIndex_;
499 Scalar episodeStartTime_;
500 Scalar episodeLength_;
501 std::string episodeDescription_;
502
503 Dune::Timer timer_;
504 Scalar time_;
505 Scalar endTime_;
506
507 Scalar timeStepSize_;
508 Scalar previousTimeStepSize_;
509 int timeStepIdx_;
510 bool finished_;
511 bool verbose_;
512};
513}
514
515#endif
The infrastructure to retrieve run-time parameters from Dune::ParameterTrees.
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:62
bool episodeWillBeFinished() const
Returns true if the current episode will be finished after the current time step.
Definition: timemanager.hh:344
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:213
TimeManager(bool verbose=true)
Definition: timemanager.hh:70
Scalar maxTimeStepSize() const
Aligns dt to the episode boundary or the end time of the simulation.
Definition: timemanager.hh:259
int timeStepIndex() const
Returns number of time steps which have been executed since the beginning of the simulation.
Definition: timemanager.hh:226
Scalar episodeStartTime() const
Returns the absolute time when the current episode started .
Definition: timemanager.hh:323
void deserialize(Restarter &res)
Read the time manager's state from a restart file.
Definition: timemanager.hh:481
bool willBeFinished() const
Returns true if the simulation is finished after the time level is incremented by the current time st...
Definition: timemanager.hh:252
double wallTime() const
Returns the current wall time (cpu time).
Definition: timemanager.hh:189
void serialize(Restarter &res)
Write the time manager's state to a restart file.
Definition: timemanager.hh:462
void setTimeStepSize(Scalar dt)
Set the current time step size to a given value.
Definition: timemanager.hh:202
void startNextEpisode(Scalar len=1e100)
Start the next episode, but don't change the episode identifier.
Definition: timemanager.hh:304
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:351
bool episodeIsFinished() const
Returns true if the current episode is finished at the current time.
Definition: timemanager.hh:337
void setEndTime(Scalar t)
Set the time of simulated seconds at which the simulation runs.
Definition: timemanager.hh:183
void setTime(Scalar t, int stepIdx)
Set the current simulated time and the time step index.
Definition: timemanager.hh:161
void run()
Runs the simulation using a given problem class.
Definition: timemanager.hh:376
Scalar episodeLength() const
Returns the length of the current episode in simulated time .
Definition: timemanager.hh:330
void setFinished(bool yesno=true)
Specify whether the simulation is finished.
Definition: timemanager.hh:236
bool finished() const
Returns true if the simulation is finished.
Definition: timemanager.hh:245
int episodeIndex() const
Returns the index of the current episode.
Definition: timemanager.hh:316
void setTime(Scalar t)
Set the current simulated time, don't change the current time step index.
Definition: timemanager.hh:152
Scalar previousTimeStepSize() const
Returns the size of the previous time step .
Definition: timemanager.hh:219
Scalar endTime() const
Returns the number of (simulated) seconds which the simulation runs.
Definition: timemanager.hh:175
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:100
void startNextEpisode(Scalar tStart, Scalar len, const std::string &description="")
Change the current episode of the simulation.
Definition: timemanager.hh:288
Scalar time() const
Return the time before the time integration. To get the time after the time integration you have to ...
Definition: timemanager.hh:169
Base file for properties related to sequential models.