AMDiS 2.10
The Adaptive Multi-Dimensional Simulation Toolbox
AdaptInfo.hpp
1#pragma once
2
3// std c++ headers
4#include <algorithm>
5#include <cmath>
6#include <limits>
7#include <map>
8#include <string>
9#include <utility>
10
11// AMDiS includes
12#include <amdis/Output.hpp>
13#include <amdis/common/ConceptsBase.hpp>
14#include <amdis/typetree/TreePath.hpp>
15
16namespace AMDiS
17{
18
26 {
27 public:
28 using Key = std::string;
29
30 protected:
36 {
37 public:
39 explicit ScalContent(std::string const& prefix);
40
42 double est_sum = 0.0;
43
45 double est_t_sum = 0.0;
46
48 double est_max = 0.0;
49
51 double est_t_max = 0.0;
52
54 double fac_max = 0.0, fac_sum = 1.0;
55
57 double spaceTolerance = 0.0;
58
60 double timeTolerance = 0.0;
61
64
66 double timeErrLow = 0.0;
67
70
73 };
74
75 public:
77 explicit AdaptInfo(std::string const& name);
78
80 virtual ~AdaptInfo() = default;
81
83 void reset();
84
86 virtual bool spaceToleranceReached() const
87 {
88 for (auto const& scalContent : scalContents_) {
89 if (!(scalContent.second.est_sum < scalContent.second.spaceTolerance))
90 return false;
91 }
92
93 return true;
94 }
95
97 virtual bool spaceToleranceReached(Key key) const
98 {
99 if (!(scalContent(key).est_sum < scalContent(key).spaceTolerance))
100 return false;
101 else
102 return true;
103 }
104
105 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
106 bool spaceToleranceReached(const TP& tp) const
107 {
108 return spaceToleranceReached(to_string(tp));
109 }
110
112 virtual bool timeToleranceReached() const
113 {
114 for (auto const& scalContent : scalContents_)
115 if (!(timeEstCombined(scalContent.first) < scalContent.second.timeTolerance))
116 return false;
117
118 return true;
119 }
120
122 virtual bool timeToleranceReached(Key key) const
123 {
124 if (!(timeEstCombined(key) < scalContent(key).timeTolerance))
125 return false;
126 else
127 return true;
128 }
129
130 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
131 bool timeToleranceReached(const TP& tp) const
132 {
133 return timeToleranceReached(to_string(tp));
134 }
135
137 virtual bool timeErrorLow() const
138 {
139 for (auto const& scalContent : scalContents_)
140 if (!(timeEstCombined(scalContent.first) < scalContent.second.timeErrLow))
141 return false;
142
143 return true;
144 }
145
148 double timeEstCombined(Key key) const
149 {
150 return
151 scalContent(key).est_t_max * scalContent(key).fac_max +
152 scalContent(key).est_t_sum * scalContent(key).fac_sum;
153 }
154
155 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
156 double timeEstCombined(const TP& tp) const
157 {
158 return tTimeEstCombined(to_string(tp));
159 }
160
162 void printTimeErrorLowInfo() const;
163
165 int spaceIteration() const
166 {
167 return spaceIteration_;
168 }
169
171 void setSpaceIteration(int it)
172 {
173 spaceIteration_ = it;
174 }
175
178 {
179 return maxSpaceIteration_;
180 }
181
183 void maxSpaceIteration(int it)
184 {
186 }
187
190 {
192 }
193
196 {
198 }
199
202 {
203 return timestepIteration_;
204 }
205
208 {
210 }
211
214 {
216 }
217
220 {
222 }
223
225 void setTimeIteration(int it)
226 {
227 timeIteration_ = it;
228 }
229
231 int timeIteration() const
232 {
233 return timeIteration_;
234 }
235
238 {
240 }
241
244 {
245 return maxTimeIteration_;
246 }
247
250 {
252 }
253
255 int timestepNumber() const
256 {
257 return timestepNumber_;
258 }
259
261 void setTimestepNumber(int num)
262 {
263 timestepNumber_ = std::min(nTimesteps_, num);
264 }
265
268 {
269 return nTimesteps_;
270 }
271
274 {
275 nTimesteps_ = std::max(0, num);
276 }
277
280 {
282 }
283
285 void setEstSum(double e, Key key)
286 {
287 scalContent(key).est_sum = e;
288 }
289
290 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
291 void setEstSum(double e, const TP& tp)
292 {
293 setEstSum(e, to_string(tp));
294 }
295
297 void setEstMax(double e, Key key)
298 {
299 scalContent(key).est_max = e;
300 }
301
302 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
303 void setEstMax(double e, const TP& tp)
304 {
305 setEstMax(e, to_string(tp));
306 }
307
309 void setTimeEstMax(double e, Key key)
310 {
311 scalContent(key).est_t_max = e;
312 }
313
314 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
315 void setTimeEstMax(double e, const TP& tp)
316 {
317 setTimeEstMax(e, to_string(tp));
318 }
319
321 void setTimeEstSum(double e, Key key)
322 {
323 scalContent(key).est_t_sum = e;
324 }
325
326 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
327 void setTimeEstSum(double e, const TP& tp)
328 {
329 setTimeEstSum(e, to_string(tp));
330 }
331
333 double estSum(Key key) const
334 {
335 return scalContent(key).est_sum;
336 }
337
338 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
339 double estSum(const TP& tp)
340 {
341 return estSum(to_string(tp));
342 }
343
345 double estTSum(Key key) const
346 {
347 return scalContent(key).est_t_sum;
348 }
349
350 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
351 double estTSum(const TP& tp)
352 {
353 return estTSum(to_string(tp));
354 }
355
357 double estMax(Key key) const
358 {
359 return scalContent(key).est_max;
360 }
361
362 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
363 double estMax(const TP& tp)
364 {
365 return estMax(to_string(tp));
366 }
367
369 double timeEstMax(Key key) const
370 {
371 return scalContent(key).est_t_max;
372 }
373
374 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
375 double timeEstmax(const TP& tp)
376 {
377 return timeEstMax(to_string(tp));
378 }
379
381 double timeEstSum(Key key) const
382 {
383 return scalContent(key).est_t_sum;
384 }
385
386 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
387 double timeEstSum(const TP& tp)
388 {
389 return timeEstSum(to_string(tp));
390 }
391
393 double timeEst() const
394 {
395 return timeEst_;
396 }
397
398 void setTimeEst(double value)
399 {
400 timeEst_ = value;
401 }
402
404 double spaceTolerance(Key key) const
405 {
406 return scalContent(key).spaceTolerance;
407 }
408
409 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
410 double spaceTolerance(const TP& tp)
411 {
412 return spaceTolerance(to_string(tp));
413 }
414
416 void setSpaceTolerance(Key key, double tol)
417 {
418 scalContent(key).spaceTolerance = tol;
419 }
420
421 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
422 void setSpaceTolerance(const TP& tp, double tol)
423 {
424 return setSpaceTolerance(to_string(tp), tol);
425 }
426
428 double timeTolerance(Key key) const
429 {
430 return scalContent(key).timeTolerance;
431 }
432
433 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
434 double timeTolerance(const TP& tp)
435 {
436 return timeTolerance(to_string(tp));
437 }
438
440 double timeRelativeTolerance(Key key) const
441 {
442 return scalContent(key).timeRelativeTolerance;
443 }
444
445 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
446 double timeRelativeTolerance(const TP& tp)
447 {
448 return timeRelativeTolerance(to_string(tp));
449 }
450
452 double setTime(double t)
453 {
454 time_ = t;
455 if (time_ > endTime_)
456 time_ = endTime_;
457 if (time_ < startTime_)
459
460 return time_;
461 }
462
464 double const& time() const
465 {
466 return time_;
467 }
468
470 double setTimestep(double t)
471 {
472 timestep_ = t;
477 if (time_ + timestep_ > endTime_)
479
480 return timestep_;
481 }
483 double const& timestep() const
484 {
485 return timestep_;
486 }
487
488 void setLastProcessedTimestep(double t)
489 {
491 }
492
493 double lastProcessedTimestep() const
494 {
496 }
497
500 bool reachedEndTime() const
501 {
502 if (nTimesteps_ > 0)
503 return !(timestepNumber_ < nTimesteps_);
504
505 return !(std::abs(time_ - endTime_) > std::numeric_limits<double>::epsilon());
506 }
507
508
510 void setMinTimestep(double t)
511 {
512 minTimestep_ = t;
513 }
514
516 double minTimestep() const
517 {
518 return minTimestep_;
519 }
520
522 void setMaxTimestep(double t)
523 {
524 maxTimestep_ = t;
525 }
526
528 double maxTimestep() const
529 {
530 return maxTimestep_;
531 }
532
534 void setStartTime(double time)
535 {
537 }
538
540 void setEndTime(double time)
541 {
542 endTime_ = time;
543 }
544
546 double startTime() const
547 {
548 return startTime_;
549 }
550
552 double endTime() const
553 {
554 return endTime_;
555 }
556
558 double timeErrLow(Key key) const
559 {
560 return scalContent(key).timeErrLow;
561 }
562
563 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
564 double timeErrLow(const TP& tp)
565 {
566 return timeErrLow(to_string(tp));
567 }
568
570 bool isCoarseningAllowed(Key key) const
571 {
572 return (scalContent(key).coarsenAllowed == 1);
573 }
574
575 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
576 bool isCoarseningAllowed(const TP& tp)
577 {
578 return isCoarseningAllowed(to_string(tp));
579 }
580
582 bool isRefinementAllowed(Key key) const
583 {
584 return (scalContent(key).refinementAllowed == 1);
585 }
586
587 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
588 bool isRefinementAllowed(const TP& tp)
589 {
590 return isRefinementAllowed(to_string(tp));
591 }
592
594 void allowRefinement(bool allow, Key key)
595 {
596 scalContent(key).refinementAllowed = allow;
597 }
598
599 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
600 void allowRefinement(bool allow, const TP& tp)
601 {
602 return allowRefinement(allow, to_string(tp));
603 }
604
606 void allowCoarsening(bool allow, Key key)
607 {
608 scalContent(key).coarsenAllowed = allow;
609 }
610
611 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
612 void allowCoarsening(bool allow, const TP& tp)
613 {
614 return allowCoarsening(allow, to_string(tp));
615 }
616
617 int size() const
618 {
619 return int(scalContents_.size());
620 }
621
622 void setSolverIterations(int it)
623 {
625 }
626
627 int solverIterations() const
628 {
629 return solverIterations_;
630 }
631
632 void setMaxSolverIterations(int it)
633 {
635 }
636
637 int maxSolverIterations() const
638 {
640 }
641
642 void setSolverTolerance(double tol)
643 {
644 solverTolerance_ = tol;
645 }
646
647 double solverTolerance() const
648 {
649 return solverTolerance_;
650 }
651
652 void setSolverResidual(double res)
653 {
654 solverResidual_ = res;
655 }
656
657 double solverResidual() const
658 {
659 return solverResidual_;
660 }
661
662 void setGlobalTimeTolerance(double tol)
663 {
665 }
666
667 double globalTimeTolerance() const
668 {
670 }
671
672
677 void resetTimeValues(double newTimeStep,
678 double newStartTime,
679 double newEndTime)
680 {
681 time_ = newStartTime;
682 startTime_ = newStartTime;
683 endTime_ = newEndTime;
684 timestep_ = newTimeStep;
685 timestepNumber_ = 0;
686 }
687
688 private:
689 ScalContent& scalContent(Key key) const
690 {
691 auto result = scalContents_.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(name_ + "[" + key + "]") );
692 return result.first->second;
693 }
694
695 protected:
697 std::string name_;
698
701
707
710
713
716
719
721 double time_ = 0.0;
722
724 double startTime_ = 0.0;
725
727 double endTime_ = 1.0;
728
730 double timestep_ = 0.0;
731
734
736 double minTimestep_ = std::sqrt(std::numeric_limits<double>::epsilon());
737
739 double maxTimestep_ = std::sqrt(std::numeric_limits<double>::max());
740
743
749 int nTimesteps_ = 0;
750
753
756
758 double solverTolerance_ = 1.e-8;
759
761 double solverResidual_ = 0.0;
762
765
767 mutable std::map<Key, ScalContent> scalContents_;
768
770 double timeEst_ = 0.0;
771 };
772
773} // end namespace AMDiS
Stores adapt infos for a scalar problem or for one component of a vector valued problem.
Definition: AdaptInfo.hpp:36
int coarsenAllowed
true if coarsening is allowed, false otherwise.
Definition: AdaptInfo.hpp:69
double timeErrLow
Lower bound for the time error.
Definition: AdaptInfo.hpp:66
double est_t_sum
Sum of all time error estimates.
Definition: AdaptInfo.hpp:45
double est_t_max
Maximum of all time error estimates.
Definition: AdaptInfo.hpp:51
double spaceTolerance
Tolerance for the (absolute or relative) error.
Definition: AdaptInfo.hpp:57
double timeRelativeTolerance
Relative time tolerance.
Definition: AdaptInfo.hpp:63
double est_max
maximal local error estimate.
Definition: AdaptInfo.hpp:48
double timeTolerance
Time tolerance.
Definition: AdaptInfo.hpp:60
double est_sum
Sum of all error estimates.
Definition: AdaptInfo.hpp:42
ScalContent(std::string const &prefix)
Constructor.
Definition: AdaptInfo.cpp:14
double fac_max
factors to combine max and integral time estimate
Definition: AdaptInfo.hpp:54
int refinementAllowed
true if refinement is allowed, false otherwise.
Definition: AdaptInfo.hpp:72
Holds adapt parameters and infos about the problem.
Definition: AdaptInfo.hpp:26
void setEndTime(double time)
Sets endTime_ = time.
Definition: AdaptInfo.hpp:540
void setMaxTimeIteration(int it)
Sets maxTimeIteration_.
Definition: AdaptInfo.hpp:249
int spaceIteration() const
Returns spaceIteration_.
Definition: AdaptInfo.hpp:165
void incSpaceIteration()
Increments spaceIteration_ by 1;.
Definition: AdaptInfo.hpp:189
void setSpaceIteration(int it)
Sets spaceIteration_.
Definition: AdaptInfo.hpp:171
int maxTimestepIteration() const
Returns maxTimestepIteration_.
Definition: AdaptInfo.hpp:213
void setStartTime(double time)
Sets startTime_ = time.
Definition: AdaptInfo.hpp:534
double const & timestep() const
Gets timestep_.
Definition: AdaptInfo.hpp:483
void incTimestepNumber()
Increments timestepNumber_ by 1;.
Definition: AdaptInfo.hpp:279
double timeErrLow(Key key) const
Returns timeErrLow.
Definition: AdaptInfo.hpp:558
std::map< Key, ScalContent > scalContents_
Scalar adapt infos.
Definition: AdaptInfo.hpp:767
bool isCoarseningAllowed(Key key) const
Returns whether coarsening is allowed or not.
Definition: AdaptInfo.hpp:570
double setTimestep(double t)
Sets timestep_.
Definition: AdaptInfo.hpp:470
int maxTimeIteration_
Maximal number of time iterations.
Definition: AdaptInfo.hpp:718
double endTime() const
Returns endTime_.
Definition: AdaptInfo.hpp:552
void setTimeIteration(int it)
Sets timeIteration_.
Definition: AdaptInfo.hpp:225
int timestepIteration_
Current timestep iteration.
Definition: AdaptInfo.hpp:709
virtual bool spaceToleranceReached() const
Returns whether space tolerance is reached.
Definition: AdaptInfo.hpp:86
bool reachedEndTime() const
Definition: AdaptInfo.hpp:500
double timeRelativeTolerance(Key key) const
Returns timeRelativeTolerance.
Definition: AdaptInfo.hpp:440
bool isRefinementAllowed(Key key) const
Returns whether coarsening is allowed or not.
Definition: AdaptInfo.hpp:582
int maxSpaceIteration_
maximal allowed number of iterations of the adaptive procedure; if maxIteration <= 0,...
Definition: AdaptInfo.hpp:706
double time_
Actual time, end of time interval for current time step.
Definition: AdaptInfo.hpp:721
double endTime_
Final time.
Definition: AdaptInfo.hpp:727
std::string name_
Name.
Definition: AdaptInfo.hpp:697
void incTimeIteration()
Increments timesIteration_ by 1;.
Definition: AdaptInfo.hpp:237
AdaptInfo(std::string const &name)
Constructor.
Definition: AdaptInfo.cpp:30
virtual bool spaceToleranceReached(Key key) const
Returns whether space tolerance of component associated with key is reached.
Definition: AdaptInfo.hpp:97
double minTimestep() const
Gets minTimestep_.
Definition: AdaptInfo.hpp:516
double spaceTolerance(Key key) const
Returns spaceTolerance.
Definition: AdaptInfo.hpp:404
int timeIteration() const
Returns timeIteration_.
Definition: AdaptInfo.hpp:231
double startTime() const
Returns startTime_.
Definition: AdaptInfo.hpp:546
double timeEst() const
Returns timeEst_ the estimated overall time error.
Definition: AdaptInfo.hpp:393
int timestepNumber() const
Returns timestepNumber_.
Definition: AdaptInfo.hpp:255
void printTimeErrorLowInfo() const
Print debug information about time error and its bound.
Definition: AdaptInfo.cpp:49
double timestep_
Time step size to be used.
Definition: AdaptInfo.hpp:730
int maxTimeIteration() const
Returns maxTimeIteration_.
Definition: AdaptInfo.hpp:243
double timeEstMax(Key key) const
Returns est_max.
Definition: AdaptInfo.hpp:369
virtual bool timeToleranceReached() const
Returns whether time tolerance is reached.
Definition: AdaptInfo.hpp:112
void setTimestepIteration(int it)
Sets timestepIteration_.
Definition: AdaptInfo.hpp:195
double setTime(double t)
Sets time_.
Definition: AdaptInfo.hpp:452
int timestepIteration() const
Returns timestepIteration_.
Definition: AdaptInfo.hpp:201
int timestepNumber_
Number of current time step.
Definition: AdaptInfo.hpp:742
double maxTimestep() const
Gets maxTimestep_.
Definition: AdaptInfo.hpp:528
double globalTimeTolerance_
tolerance for the overall time error
Definition: AdaptInfo.hpp:764
void setEstMax(double e, Key key)
Sets est_max.
Definition: AdaptInfo.hpp:297
int maxSolverIterations_
maximal number of iterations needed of linear or nonlinear solver
Definition: AdaptInfo.hpp:755
double timeEst_
overall time error estimate
Definition: AdaptInfo.hpp:770
void resetTimeValues(double newTimeStep, double newStartTime, double newEndTime)
Resets timestep, current time and time boundaries without any check. Is used by the parareal algorith...
Definition: AdaptInfo.hpp:677
virtual bool timeToleranceReached(Key key) const
Returns whether time tolerance of component associated with key is reached.
Definition: AdaptInfo.hpp:122
void setTimestepNumber(int num)
Sets timestepNumber.
Definition: AdaptInfo.hpp:261
double estSum(Key key) const
Returns est_sum.
Definition: AdaptInfo.hpp:333
int maxSpaceIteration() const
Returns maxSpaceIteration_.
Definition: AdaptInfo.hpp:177
double lastProcessedTimestep_
Last processed time step size of finished iteration.
Definition: AdaptInfo.hpp:733
void setEstSum(double e, Key key)
Sets est_sum.
Definition: AdaptInfo.hpp:285
void setMaxTimestepIteration(int it)
Sets maxTimestepIteration.
Definition: AdaptInfo.hpp:219
void setTimeEstSum(double e, Key key)
Sets est_t_sum.
Definition: AdaptInfo.hpp:321
double timeEstCombined(Key key) const
Definition: AdaptInfo.hpp:148
int spaceIteration_
Current space iteration.
Definition: AdaptInfo.hpp:700
double estMax(Key key) const
Returns est_max.
Definition: AdaptInfo.hpp:357
double minTimestep_
Minimal step size.
Definition: AdaptInfo.hpp:736
double timeTolerance(Key key) const
Returns timeTolerance.
Definition: AdaptInfo.hpp:428
double startTime_
Initial time.
Definition: AdaptInfo.hpp:724
void setTimeEstMax(double e, Key key)
Sets est_max.
Definition: AdaptInfo.hpp:309
void setNumberOfTimesteps(int num)
Sets nTimesteps.
Definition: AdaptInfo.hpp:273
double const & time() const
Gets time_.
Definition: AdaptInfo.hpp:464
int numberOfTimesteps() const
Returns nTimesteps_.
Definition: AdaptInfo.hpp:267
int nTimesteps_
Per default this value is 0 and not used. If it is set to a non-zero value, the computation of the st...
Definition: AdaptInfo.hpp:749
void reset()
Resets all variables to zero (or something equivalent)
Definition: AdaptInfo.cpp:67
void setMinTimestep(double t)
Sets minTimestep_.
Definition: AdaptInfo.hpp:510
virtual bool timeErrorLow() const
Returns whether time error is under its lower bound.
Definition: AdaptInfo.hpp:137
int timeIteration_
Current time iteration.
Definition: AdaptInfo.hpp:715
int solverIterations_
number of iterations needed of linear or nonlinear solver
Definition: AdaptInfo.hpp:752
virtual ~AdaptInfo()=default
Destructor.
void maxSpaceIteration(int it)
Sets maxSpaceIteration_.
Definition: AdaptInfo.hpp:183
void setSpaceTolerance(Key key, double tol)
Sets spaceTolerance.
Definition: AdaptInfo.hpp:416
int maxTimestepIteration_
Maximal number of iterations for choosing a timestep.
Definition: AdaptInfo.hpp:712
double maxTimestep_
Maximal step size.
Definition: AdaptInfo.hpp:739
double timeEstSum(Key key) const
Returns est_t_sum.
Definition: AdaptInfo.hpp:381
void incTimestepIteration()
Increments timestepIteration_ by 1;.
Definition: AdaptInfo.hpp:207
void setMaxTimestep(double t)
Sets maxTimestep_.
Definition: AdaptInfo.hpp:522
double estTSum(Key key) const
Returns est_t_sum.
Definition: AdaptInfo.hpp:345