AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
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 if (nTimesteps_)
264 timestepNumber_ = std::min(nTimesteps_, num);
265 else
266 timestepNumber_ = num;
267 }
268
271 {
272 return nTimesteps_;
273 }
274
277 {
278 nTimesteps_ = std::max(0, num);
279 }
280
283 {
285 }
286
288 void setEstSum(double e, Key key)
289 {
290 scalContent(key).est_sum = e;
291 }
292
293 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
294 void setEstSum(double e, const TP& tp)
295 {
296 setEstSum(e, to_string(tp));
297 }
298
300 void setEstMax(double e, Key key)
301 {
302 scalContent(key).est_max = e;
303 }
304
305 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
306 void setEstMax(double e, const TP& tp)
307 {
308 setEstMax(e, to_string(tp));
309 }
310
312 void setTimeEstMax(double e, Key key)
313 {
314 scalContent(key).est_t_max = e;
315 }
316
317 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
318 void setTimeEstMax(double e, const TP& tp)
319 {
320 setTimeEstMax(e, to_string(tp));
321 }
322
324 void setTimeEstSum(double e, Key key)
325 {
326 scalContent(key).est_t_sum = e;
327 }
328
329 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
330 void setTimeEstSum(double e, const TP& tp)
331 {
332 setTimeEstSum(e, to_string(tp));
333 }
334
336 double estSum(Key key) const
337 {
338 return scalContent(key).est_sum;
339 }
340
341 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
342 double estSum(const TP& tp)
343 {
344 return estSum(to_string(tp));
345 }
346
348 double estTSum(Key key) const
349 {
350 return scalContent(key).est_t_sum;
351 }
352
353 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
354 double estTSum(const TP& tp)
355 {
356 return estTSum(to_string(tp));
357 }
358
360 double estMax(Key key) const
361 {
362 return scalContent(key).est_max;
363 }
364
365 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
366 double estMax(const TP& tp)
367 {
368 return estMax(to_string(tp));
369 }
370
372 double timeEstMax(Key key) const
373 {
374 return scalContent(key).est_t_max;
375 }
376
377 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
378 double timeEstmax(const TP& tp)
379 {
380 return timeEstMax(to_string(tp));
381 }
382
384 double timeEstSum(Key key) const
385 {
386 return scalContent(key).est_t_sum;
387 }
388
389 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
390 double timeEstSum(const TP& tp)
391 {
392 return timeEstSum(to_string(tp));
393 }
394
396 double timeEst() const
397 {
398 return timeEst_;
399 }
400
401 void setTimeEst(double value)
402 {
403 timeEst_ = value;
404 }
405
407 double spaceTolerance(Key key) const
408 {
409 return scalContent(key).spaceTolerance;
410 }
411
412 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
413 double spaceTolerance(const TP& tp)
414 {
415 return spaceTolerance(to_string(tp));
416 }
417
419 void setSpaceTolerance(Key key, double tol)
420 {
421 scalContent(key).spaceTolerance = tol;
422 }
423
424 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
425 void setSpaceTolerance(const TP& tp, double tol)
426 {
427 return setSpaceTolerance(to_string(tp), tol);
428 }
429
431 double timeTolerance(Key key) const
432 {
433 return scalContent(key).timeTolerance;
434 }
435
436 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
437 double timeTolerance(const TP& tp)
438 {
439 return timeTolerance(to_string(tp));
440 }
441
443 double timeRelativeTolerance(Key key) const
444 {
445 return scalContent(key).timeRelativeTolerance;
446 }
447
448 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
449 double timeRelativeTolerance(const TP& tp)
450 {
451 return timeRelativeTolerance(to_string(tp));
452 }
453
455 double setTime(double t)
456 {
457 time_ = t;
458 if (time_ > endTime_)
459 time_ = endTime_;
460 if (time_ < startTime_)
462
463 return time_;
464 }
465
467 double const& time() const
468 {
469 return time_;
470 }
471
473 double setTimestep(double t)
474 {
475 timestep_ = t;
480 if (time_ + timestep_ > endTime_)
482
483 return timestep_;
484 }
486 double const& timestep() const
487 {
488 return timestep_;
489 }
490
491 void setLastProcessedTimestep(double t)
492 {
494 }
495
496 double lastProcessedTimestep() const
497 {
499 }
500
503 bool reachedEndTime() const
504 {
505 if (nTimesteps_ > 0)
506 return !(timestepNumber_ < nTimesteps_);
507
508 return !(std::abs(time_ - endTime_) > std::numeric_limits<double>::epsilon());
509 }
510
511
513 void setMinTimestep(double t)
514 {
515 minTimestep_ = t;
516 }
517
519 double minTimestep() const
520 {
521 return minTimestep_;
522 }
523
525 void setMaxTimestep(double t)
526 {
527 maxTimestep_ = t;
528 }
529
531 double maxTimestep() const
532 {
533 return maxTimestep_;
534 }
535
537 void setStartTime(double time)
538 {
540 }
541
543 void setEndTime(double time)
544 {
545 endTime_ = time;
546 }
547
549 double startTime() const
550 {
551 return startTime_;
552 }
553
555 double endTime() const
556 {
557 return endTime_;
558 }
559
561 double timeErrLow(Key key) const
562 {
563 return scalContent(key).timeErrLow;
564 }
565
566 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
567 double timeErrLow(const TP& tp)
568 {
569 return timeErrLow(to_string(tp));
570 }
571
573 bool isCoarseningAllowed(Key key) const
574 {
575 return (scalContent(key).coarsenAllowed == 1);
576 }
577
578 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
579 bool isCoarseningAllowed(const TP& tp)
580 {
581 return isCoarseningAllowed(to_string(tp));
582 }
583
585 bool isRefinementAllowed(Key key) const
586 {
587 return (scalContent(key).refinementAllowed == 1);
588 }
589
590 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
591 bool isRefinementAllowed(const TP& tp)
592 {
593 return isRefinementAllowed(to_string(tp));
594 }
595
597 void allowRefinement(bool allow, Key key)
598 {
599 scalContent(key).refinementAllowed = allow;
600 }
601
602 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
603 void allowRefinement(bool allow, const TP& tp)
604 {
605 return allowRefinement(allow, to_string(tp));
606 }
607
609 void allowCoarsening(bool allow, Key key)
610 {
611 scalContent(key).coarsenAllowed = allow;
612 }
613
614 template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
615 void allowCoarsening(bool allow, const TP& tp)
616 {
617 return allowCoarsening(allow, to_string(tp));
618 }
619
620 int size() const
621 {
622 return int(scalContents_.size());
623 }
624
625 void setSolverIterations(int it)
626 {
628 }
629
630 int solverIterations() const
631 {
632 return solverIterations_;
633 }
634
635 void setMaxSolverIterations(int it)
636 {
638 }
639
640 int maxSolverIterations() const
641 {
643 }
644
645 void setSolverTolerance(double tol)
646 {
647 solverTolerance_ = tol;
648 }
649
650 double solverTolerance() const
651 {
652 return solverTolerance_;
653 }
654
655 void setSolverResidual(double res)
656 {
657 solverResidual_ = res;
658 }
659
660 double solverResidual() const
661 {
662 return solverResidual_;
663 }
664
665 void setGlobalTimeTolerance(double tol)
666 {
668 }
669
670 double globalTimeTolerance() const
671 {
673 }
674
675
680 void resetTimeValues(double newTimeStep,
681 double newStartTime,
682 double newEndTime)
683 {
684 time_ = newStartTime;
685 startTime_ = newStartTime;
686 endTime_ = newEndTime;
687 timestep_ = newTimeStep;
688 timestepNumber_ = 0;
689 }
690
691 private:
692 ScalContent& scalContent(Key key) const
693 {
694 auto result = scalContents_.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(name_ + "[" + key + "]") );
695 return result.first->second;
696 }
697
698 protected:
700 std::string name_;
701
704
710
713
716
719
722
724 double time_ = 0.0;
725
727 double startTime_ = 0.0;
728
730 double endTime_ = 1.0;
731
733 double timestep_ = 0.0;
734
737
739 double minTimestep_ = std::sqrt(std::numeric_limits<double>::epsilon());
740
742 double maxTimestep_ = std::sqrt(std::numeric_limits<double>::max());
743
746
752 int nTimesteps_ = 0;
753
756
759
761 double solverTolerance_ = 1.e-8;
762
764 double solverResidual_ = 0.0;
765
768
770 mutable std::map<Key, ScalContent> scalContents_;
771
773 double timeEst_ = 0.0;
774 };
775
776} // 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
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:543
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:537
double const & timestep() const
Gets timestep_.
Definition AdaptInfo.hpp:486
void incTimestepNumber()
Increments timestepNumber_ by 1;.
Definition AdaptInfo.hpp:282
double timeErrLow(Key key) const
Returns timeErrLow.
Definition AdaptInfo.hpp:561
std::map< Key, ScalContent > scalContents_
Scalar adapt infos.
Definition AdaptInfo.hpp:770
bool isCoarseningAllowed(Key key) const
Returns whether coarsening is allowed or not.
Definition AdaptInfo.hpp:573
double setTimestep(double t)
Sets timestep_.
Definition AdaptInfo.hpp:473
int maxTimeIteration_
Maximal number of time iterations.
Definition AdaptInfo.hpp:721
double endTime() const
Returns endTime_.
Definition AdaptInfo.hpp:555
void setTimeIteration(int it)
Sets timeIteration_.
Definition AdaptInfo.hpp:225
int timestepIteration_
Current timestep iteration.
Definition AdaptInfo.hpp:712
virtual bool spaceToleranceReached() const
Returns whether space tolerance is reached.
Definition AdaptInfo.hpp:86
bool reachedEndTime() const
Definition AdaptInfo.hpp:503
double timeRelativeTolerance(Key key) const
Returns timeRelativeTolerance.
Definition AdaptInfo.hpp:443
bool isRefinementAllowed(Key key) const
Returns whether coarsening is allowed or not.
Definition AdaptInfo.hpp:585
int maxSpaceIteration_
maximal allowed number of iterations of the adaptive procedure; if maxIteration <= 0,...
Definition AdaptInfo.hpp:709
double time_
Actual time, end of time interval for current time step.
Definition AdaptInfo.hpp:724
double endTime_
Final time.
Definition AdaptInfo.hpp:730
std::string name_
Name.
Definition AdaptInfo.hpp:700
void incTimeIteration()
Increments timesIteration_ by 1;.
Definition AdaptInfo.hpp:237
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:519
double spaceTolerance(Key key) const
Returns spaceTolerance.
Definition AdaptInfo.hpp:407
int timeIteration() const
Returns timeIteration_.
Definition AdaptInfo.hpp:231
double startTime() const
Returns startTime_.
Definition AdaptInfo.hpp:549
double timeEst() const
Returns timeEst_ the estimated overall time error.
Definition AdaptInfo.hpp:396
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:733
int maxTimeIteration() const
Returns maxTimeIteration_.
Definition AdaptInfo.hpp:243
double timeEstMax(Key key) const
Returns est_max.
Definition AdaptInfo.hpp:372
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:455
int timestepIteration() const
Returns timestepIteration_.
Definition AdaptInfo.hpp:201
int timestepNumber_
Number of current time step.
Definition AdaptInfo.hpp:745
double maxTimestep() const
Gets maxTimestep_.
Definition AdaptInfo.hpp:531
double globalTimeTolerance_
tolerance for the overall time error
Definition AdaptInfo.hpp:767
void setEstMax(double e, Key key)
Sets est_max.
Definition AdaptInfo.hpp:300
int maxSolverIterations_
maximal number of iterations needed of linear or nonlinear solver
Definition AdaptInfo.hpp:758
double timeEst_
overall time error estimate
Definition AdaptInfo.hpp:773
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:680
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:336
int maxSpaceIteration() const
Returns maxSpaceIteration_.
Definition AdaptInfo.hpp:177
double lastProcessedTimestep_
Last processed time step size of finished iteration.
Definition AdaptInfo.hpp:736
void setEstSum(double e, Key key)
Sets est_sum.
Definition AdaptInfo.hpp:288
void setMaxTimestepIteration(int it)
Sets maxTimestepIteration.
Definition AdaptInfo.hpp:219
void setTimeEstSum(double e, Key key)
Sets est_t_sum.
Definition AdaptInfo.hpp:324
double timeEstCombined(Key key) const
Definition AdaptInfo.hpp:148
int spaceIteration_
Current space iteration.
Definition AdaptInfo.hpp:703
double estMax(Key key) const
Returns est_max.
Definition AdaptInfo.hpp:360
double minTimestep_
Minimal step size.
Definition AdaptInfo.hpp:739
double timeTolerance(Key key) const
Returns timeTolerance.
Definition AdaptInfo.hpp:431
double startTime_
Initial time.
Definition AdaptInfo.hpp:727
void setTimeEstMax(double e, Key key)
Sets est_max.
Definition AdaptInfo.hpp:312
void setNumberOfTimesteps(int num)
Sets nTimesteps.
Definition AdaptInfo.hpp:276
double const & time() const
Gets time_.
Definition AdaptInfo.hpp:467
int numberOfTimesteps() const
Returns nTimesteps_.
Definition AdaptInfo.hpp:270
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:752
void reset()
Resets all variables to zero (or something equivalent)
Definition AdaptInfo.cpp:67
void setMinTimestep(double t)
Sets minTimestep_.
Definition AdaptInfo.hpp:513
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:718
int solverIterations_
number of iterations needed of linear or nonlinear solver
Definition AdaptInfo.hpp:755
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:419
int maxTimestepIteration_
Maximal number of iterations for choosing a timestep.
Definition AdaptInfo.hpp:715
double maxTimestep_
Maximal step size.
Definition AdaptInfo.hpp:742
double timeEstSum(Key key) const
Returns est_t_sum.
Definition AdaptInfo.hpp:384
void incTimestepIteration()
Increments timestepIteration_ by 1;.
Definition AdaptInfo.hpp:207
void setMaxTimestep(double t)
Sets maxTimestep_.
Definition AdaptInfo.hpp:525
double estTSum(Key key) const
Returns est_t_sum.
Definition AdaptInfo.hpp:348