AdaptInfo.hpp 18.3 KB
Newer Older
1
2
3
#pragma once

// std c++ headers
4
#include <algorithm>
5
#include <cmath>
6
#include <forward_list>
7
#include <limits>
8
#include <map>
9
#include <string>
10
#include <utility>
11
12
13
#include <vector>

// AMDiS includes
14
#include <amdis/Output.hpp>
15
#include <amdis/common/ConceptsBase.hpp>
16
#include <amdis/common/Math.hpp>
17
#include <amdis/utility/TreePath.hpp>
18
19
20
21
22
23
24
25

namespace AMDiS
{

  /**
   * \ingroup Adaption
   *
   * \brief
26
   * Holds adapt parameters and infos about the problem.
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
   */
  class AdaptInfo
  {
  protected:
    /** \brief
     * Stores adapt infos for a scalar problem or for one component of a
     * vector valued problem.
     */
    class ScalContent
    {
    public:
      /// Constructor.
      explicit ScalContent(std::string prefix);

      /// Sum of all error estimates
      double est_sum = 0.0;

      /// Sum of all time error estimates
      double est_t_sum = 0.0;

      /// maximal local error estimate.
      double est_max = 0.0;

      /// Maximum of all time error estimates
      double est_t_max = 0.0;

      /// factors to combine max and integral time estimate
      double fac_max = 0.0, fac_sum = 1.0;

      /// Tolerance for the (absolute or relative) error
      double spaceTolerance = 0.0;

      /// Time tolerance.
      double timeTolerance = 0.0;

      /// Relative time tolerance
      double timeRelativeTolerance = 0.0;

      /// Lower bound for the time error.
      double timeErrLow = 0.0;

      /// true if coarsening is allowed, false otherwise.
      int coarsenAllowed = 0;

      /// true if refinement is allowed, false otherwise.
      int refinementAllowed = 1;
    };

  public:
    /// Constructor.
77
    explicit AdaptInfo(std::string name);
78
79

    /// Destructor.
80
    virtual ~AdaptInfo() {}
81
82
83
84
85
86
87

    /// Resets all variables to zero (or something equivalent)
    void reset();

    /// Returns whether space tolerance is reached.
    virtual bool spaceToleranceReached() const
    {
88
      for (auto it = scalContents.begin(); it != scalContents.end(); it++)      {
89
        if (!(it->second.est_sum < it->second.spaceTolerance))
90
91
92
93
94
95
          return false;
      }

      return true;
    }

96
97
    /// Returns whether space tolerance of component associated with key is reached.
    virtual bool spaceToleranceReached(std::string key) const
98
    {
99
      if (!(getScalContent(key).est_sum < getScalContent(key).spaceTolerance))
100
101
102
103
104
        return false;
      else
        return true;
    }

105
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
106
    bool spaceToleranceReached(const TP& tp) const
107
108
109
110
    {
      return spaceToleranceReached(to_string(tp));
    }

111
112
113
    /// Returns whether time tolerance is reached.
    virtual bool timeToleranceReached() const
    {
114
      for (auto it = scalContents.begin(); it != scalContents.end(); it++)
115
        if (!(getTimeEstCombined(it->first) < it->second.timeTolerance))
116
117
118
119
120
          return false;

      return true;
    }

121
122
    /// Returns whether time tolerance of component associated with key is reached.
    virtual bool timeToleranceReached(std::string key) const
123
    {
124
      if (!(getTimeEstCombined(key) < getScalContent(key).timeTolerance))
125
126
127
128
129
        return false;
      else
        return true;
    }

130
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
131
    bool timeToleranceReached(const TP& tp) const
132
133
134
135
    {
      return timeToleranceReached(to_string(tp));
    }

136
137
138
    /// Returns whether time error is under its lower bound.
    virtual bool timeErrorLow() const
    {
139
      for (auto it = scalContents.begin(); it != scalContents.end(); it++)
140
        if (!(getTimeEstCombined(it->first) < it->second.timeErrLow))
141
142
143
144
          return false;

      return true;
    }
145

146
147
    /// Returns the time estimation as a combination
    /// of maximal and integral time error
148
    double getTimeEstCombined(std::string key) const
149
150
    {
      return
151
152
        getScalContent(key).est_t_max * getScalContent(key).fac_max +
        getScalContent(key).est_t_sum * getScalContent(key).fac_sum;
153
154
    }

155
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
156
    double getTimeEstCombined(const TP& tp) const
157
158
159
    {
      return getTimeEstCombined(to_string(tp));
    }
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284

    /// Print debug information about time error and its bound.
    void printTimeErrorLowInfo() const;

    /// Returns \ref spaceIteration.
    int getSpaceIteration() const
    {
      return spaceIteration;
    }

    /// Sets \ref spaceIteration.
    void setSpaceIteration(int it)
    {
      spaceIteration = it;
    }

    /// Returns \ref maxSpaceIteration.
    int getMaxSpaceIteration() const
    {
      return maxSpaceIteration;
    }

    /// Sets \ref maxSpaceIteration.
    void setMaxSpaceIteration(int it)
    {
      maxSpaceIteration = it;
    }

    /// Increments \ref spaceIteration by 1;
    void incSpaceIteration()
    {
      spaceIteration++;
    }

    /// Sets \ref timestepIteration.
    void setTimestepIteration(int it)
    {
      timestepIteration = it;
    }

    /// Returns \ref timestepIteration.
    int getTimestepIteration() const
    {
      return timestepIteration;
    }

    /// Increments \ref timestepIteration by 1;
    void incTimestepIteration()
    {
      timestepIteration++;
    }

    /// Returns \ref maxTimestepIteration.
    int getMaxTimestepIteration() const
    {
      return maxTimestepIteration;
    }

    /// Sets \ref maxTimestepIteration.
    void setMaxTimestepIteration(int it)
    {
      maxTimestepIteration = it;
    }

    /// Sets \ref timeIteration.
    void setTimeIteration(int it)
    {
      timeIteration = it;
    }

    /// Returns \ref timeIteration.
    int getTimeIteration() const
    {
      return timeIteration;
    }

    /// Increments \ref timesIteration by 1;
    void incTimeIteration()
    {
      timeIteration++;
    }

    /// Returns \ref maxTimeIteration.
    int getMaxTimeIteration() const
    {
      return maxTimeIteration;
    }

    /// Sets \ref maxTimeIteration.
    void setMaxTimeIteration(int it)
    {
      maxTimeIteration = it;
    }

    /// Returns \ref timestepNumber.
    int getTimestepNumber() const
    {
      return timestepNumber;
    }

    /// Sets \ref timestepNumber.
    void setTimestepNumber(int num)
    {
      timestepNumber = std::min(nTimesteps, num);
    }

    /// Returns \ref nTimesteps.
    int getNumberOfTimesteps() const
    {
      return nTimesteps;
    }

    /// Sets \ref nTimesteps.
    void setNumberOfTimesteps(int num)
    {
      nTimesteps = std::max(0, num);
    }

    /// Increments \ref timestepNumber by 1;
    void incTimestepNumber()
    {
      timestepNumber++;
    }

    /// Sets \ref est_sum.
285
    void setEstSum(double e, std::string key)
286
    {
287
      getScalContent(key).est_sum = e;
288
289
    }

290
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
291
    void setEstSum(double e, const TP& tp)
292
293
294
295
    {
      setEstSum(e, to_string(tp));
    }

296
    /// Sets \ref est_max.
297
    void setEstMax(double e, std::string key)
298
    {
299
      getScalContent(key).est_max = e;
300
301
    }

302
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
303
    void setEstMax(double e, const TP& tp)
304
305
306
307
    {
      setEstMax(e, to_string(tp));
    }

308
    /// Sets \ref est_max.
309
    void setTimeEstMax(double e, std::string key)
310
    {
311
      getScalContent(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
318
319
    {
      setTimeEstMax(e, to_string(tp));
    }

320
    /// Sets \ref est_t_sum.
321
    void setTimeEstSum(double e, std::string key)
322
    {
323
      getScalContent(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
330
331
    {
      setTimeEstSum(e, to_string(tp));
    }

332
    /// Returns \ref est_sum.
333
    double getEstSum(std::string key) const
334
    {
335
      AMDIS_FUNCNAME_DBG("AdaptInfo::getEstSum()");
336
      test_exit_dbg(scalContents.count(key) == 1, "Wrong key for adaptInfo!\n");
337

338
      return getScalContent(key).est_sum;
339
340
    }

341
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
342
    double getEstSum(const TP& tp)
343
344
345
346
    {
      return getEstSum(to_string(tp));
    }

347
    /// Returns \ref est_t_sum.
348
    double getEstTSum(std::string key) const
349
    {
350
      return getScalContent(key).est_t_sum;
351
352
    }

353
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
354
    double getEstTSum(const TP& tp)
355
356
357
358
    {
      return getEstTSum(to_string(tp));
    }

359
    /// Returns \ref est_max.
360
    double getEstMax(std::string key) const
361
    {
362
      AMDIS_FUNCNAME_DBG("AdaptInfo::getEstSum()");
363
      test_exit_dbg(scalContents.count(key) == 1, "Wrong key for adaptInfo!\n");
364

365
      return getScalContent(key).est_max;
366
367
    }

368
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
369
    double getEstMax(const TP& tp)
370
371
372
373
    {
      return getEstMax(to_string(tp));
    }

374
    /// Returns \ref est_max.
375
    double getTimeEstMax(std::string key) const
376
    {
377
      return getScalContent(key).est_t_max;
378
379
    }

380
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
381
    double getTimeEstmax(const TP& tp)
382
383
384
385
    {
      return getTimeEstMax(to_string(tp));
    }

386
    /// Returns \ref est_t_sum.
387
    double getTimeEstSum(std::string key) const
388
    {
389
      return getScalContent(key).est_t_sum;
390
391
    }

392
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
393
    double getTimeEstSum(const TP& tp)
394
395
396
397
    {
      return getTimeEstSum(to_string(tp));
    }

398
399
400
401
402
403
404
405
406
407
408
409
    /// Returns \ref est_t the estimated overall time error
    double getTimeEst() const
    {
      return est_t;
    }

    void setTimeEst(double value)
    {
      est_t = value;
    }

    /// Returns \ref spaceTolerance.
410
    double getSpaceTolerance(std::string key) const
411
    {
412
      return getScalContent(key).spaceTolerance;
413
414
    }

415
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
416
    double getSpaceTolerance(const TP& tp)
417
418
419
420
    {
      return getSpaceTolerance(to_string(tp));
    }

421
    /// Sets \ref spaceTolerance.
422
    void setSpaceTolerance(std::string key, double tol)
423
    {
424
      getScalContent(key).spaceTolerance = tol;
425
426
    }

427
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
428
    void setSpaceTolerance(const TP& tp, double tol)
429
430
431
432
    {
      return setSpaceTolerance(to_string(tp), tol);
    }

433
    /// Returns \ref timeTolerance.
434
    double getTimeTolerance(std::string key) const
435
    {
436
      return getScalContent(key).timeTolerance;
437
438
    }

439
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
440
    double getTimeTolerance(const TP& tp)
441
442
443
444
    {
      return getTimeTolerance(to_string(tp));
    }

445
    /// Returns \ref timeRelativeTolerance.
446
    double getTimeRelativeTolerance(std::string key) const
447
    {
448
      return getScalContent(key).timeRelativeTolerance;
449
450
    }

451
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
452
    double getTimeRelativeTolerance(const TP& tp)
453
454
455
456
    {
      return getTimeRelativeTolerance(to_string(tp));
    }

457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
    /// Sets \ref time
    double setTime(double t)
    {
      time = t;
      if (time > endTime)
        time = endTime;
      if (time < startTime)
        time = startTime;

      return time;
    }

    /// Gets \ref time
    double getTime() const
    {
      return time;
    }

    /// Gets \ref &time
    double* getTimePtr()
    {
      return &time;
    }

    /// Sets \ref timestep
    double setTimestep(double t)
    {
      timestep = t;
      if (timestep > maxTimestep)
        timestep = maxTimestep;
      if (timestep < minTimestep)
        timestep = minTimestep;
      if (time + timestep > endTime)
        timestep = endTime - time;

      return timestep;
    }
    /// Gets \ref timestep
    double getTimestep() const
    {
      return timestep;
    }

    void setLastProcessedTimestep(double t)
    {
      lastProcessedTimestep = t;
    }

    double getLastProcessedTimestep() const
    {
      return lastProcessedTimestep;
    }

    /// Returns true, if the end time is reached and no more timestep
    /// computations must be done.
    bool reachedEndTime() const
    {
      if (nTimesteps > 0)
        return !(timestepNumber < nTimesteps);

517
      return !(std::abs(time - endTime) > std::numeric_limits<double>::epsilon());
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
    }


    /// Sets \ref minTimestep
    void setMinTimestep(double t)
    {
      minTimestep = t;
    }

    /// Gets \ref minTimestep
    double getMinTimestep() const
    {
      return minTimestep;
    }

    /// Sets \ref maxTimestep
    void setMaxTimestep(double t)
    {
      maxTimestep = t;
    }

    /// Gets \ref maxTimestep
    double getMaxTimestep() const
    {
      return maxTimestep;
    }

    /// Gets \ref &timestep
    double* getTimestepPtr()
    {
      return &timestep;
    }

    /// Sets \ref startTime = time
    void setStartTime(double time)
    {
      startTime = time;
    }

    /// Sets \ref endTime = time
    void setEndTime(double time)
    {
      endTime = time;
    }

    /// Returns \ref startTime
    double getStartTime() const
    {
      return startTime;
    }

    /// Returns \ref endTime
    double getEndTime() const
    {
      return endTime;
    }

    /// Returns \ref timeErrLow.
576
    double getTimeErrLow(std::string key) const
577
    {
578
      return getScalContent(key).timeErrLow;
579
580
    }

581
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
582
    double getTimeErrLow(const TP& tp)
583
584
585
586
    {
      return getTimeErrLow(to_string(tp));
    }

587
    /// Returns whether coarsening is allowed or not.
588
    bool isCoarseningAllowed(std::string key) const
589
    {
590
      return (getScalContent(key).coarsenAllowed == 1);
591
592
    }

593
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
594
    bool isCoarseningAllowed(const TP& tp)
595
596
597
598
    {
      return isCoarseningAllowed(to_string(tp));
    }

599
    /// Returns whether coarsening is allowed or not.
600
    bool isRefinementAllowed(std::string key) const
601
    {
602
      return (getScalContent(key).refinementAllowed == 1);
603
604
    }

605
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
606
    bool isRefinementAllowed(const TP& tp)
607
608
609
610
    {
      return isRefinementAllowed(to_string(tp));
    }

611
    ///
612
    void allowRefinement(bool allow, std::string key)
613
    {
614
      getScalContent(key).refinementAllowed = allow;
615
616
    }

617
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
618
    void allowRefinement(bool allow, const TP& tp)
619
620
621
622
    {
      return allowRefinement(allow, to_string(tp));
    }

623
    ///
624
    void allowCoarsening(bool allow, std::string key)
625
    {
626
      getScalContent(key).coarsenAllowed = allow;
627
628
    }

629
    template <class TP, REQUIRES( Concepts::PreTreePath<TP> )>
630
    void allowCoarsening(bool allow, const TP& tp)
631
632
633
634
    {
      return allowCoarsening(allow, to_string(tp));
    }

635
636
    int getSize() const
    {
637
      return int(scalContents.size());
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
    }

    // TODO: remove from AdaptInfo
    bool getRosenbrockMode() const
    {
      return rosenbrockMode;
    }

    void setSolverIterations(int it)
    {
      solverIterations = it;
    }

    int getSolverIterations() const
    {
      return solverIterations;
    }

    void setMaxSolverIterations(int it)
    {
      maxSolverIterations = it;
    }

    int getMaxSolverIterations() const
    {
      return maxSolverIterations;
    }

    void setSolverTolerance(double tol)
    {
      solverTolerance = tol;
    }

    double getSolverTolerance() const
    {
      return solverTolerance;
    }

    void setSolverResidual(double res)
    {
      solverResidual = res;
    }

    double getSolverResidual() const
    {
      return solverResidual;
    }

    void setGlobalTimeTolerance(double tol)
    {
      globalTimeTolerance = tol;
    }

    double getGlobalTimeTolerance() const
    {
      return globalTimeTolerance;
    }

    // TODO: remove from AdaptInfo
    void setRosenbrockMode(bool b)
    {
      rosenbrockMode = b;
    }

    /** \brief
     * Resets timestep, current time and time boundaries without
     * any check. Is used by the parareal algorithm.
     */
    void resetTimeValues(double newTimeStep,
                         double newStartTime,
                         double newEndTime)
    {
      time = newStartTime;
      startTime = newStartTime;
      endTime = newEndTime;
      timestep = newTimeStep;
      timestepNumber = 0;
    }

717
718
719
720
721
722
723
  private:
    ScalContent& getScalContent(std::string key) const
    {
      auto result = scalContents.emplace(std::piecewise_construct, std::forward_as_tuple(key), std::forward_as_tuple(name + "[" + key + "]") );
      return result.first->second;
    }

724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
  protected:
    /// Name.
    std::string name;

    /// Current space iteration
    int spaceIteration = -1;

    /** \brief
     * maximal allowed number of iterations of the adaptive procedure; if
     * maxIteration <= 0, no iteration bound is used
     */
    int maxSpaceIteration = -1;

    /// Current timestep iteration
    int timestepIteration = 0;

    /// Maximal number of iterations for choosing a timestep
    int maxTimestepIteration = 30;

    /// Current time iteration
    int timeIteration = 0;

    /// Maximal number of time iterations
    int maxTimeIteration = 30;

    /// Actual time, end of time interval for current time step
    double time = 0.0;

    /// Initial time
    double startTime = 0.0;

    /// Final time
    double endTime = 1.0;

758
    /// Time step size to be used
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
    double timestep = 0.0;

    /// Last processed time step size of finished iteration
    double lastProcessedTimestep = 0.0;

    /// Minimal step size
    double minTimestep = 0.0;

    /// Maximal step size
    double maxTimestep = 1.0;

    /// Number of current time step
    int timestepNumber = 0;

    /** \brief
     * Per default this value is 0 and not used. If it is set to a non-zero value,
     * the computation of the stationary problem is done nTimesteps times with a
     * fixed timestep.
     */
    int nTimesteps = 0;

    /// number of iterations needed of linear or nonlinear solver
    int solverIterations = 0;

    /// maximal number of iterations needed of linear or nonlinear solver
    int maxSolverIterations = 0;

    ///
    double solverTolerance = 1.e-8;

    ///
    double solverResidual = 0.0;

    /// tolerance for the overall time error
    double globalTimeTolerance = 1.0;

795
796
    /// Scalar adapt infos
    mutable std::map<std::string, ScalContent> scalContents;
797
798
799
800
801
802
803
804
805
806
807
808

    /// Is true, if the adaptive procedure was deserialized from a file. TODO: remove deserialization
    bool deserialized = false;

    /// Is true, if the time adaption is controlled by a Rosenbrock Method.
    bool rosenbrockMode = false;

    /// overall time error estimate
    double est_t = 0.0;
  };

} // end namespace AMDiS