AdaptInfo.h 19.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file AdaptInfo.h */

#ifndef AMDIS_ADAPTINFO_H
#define AMDIS_ADAPTINFO_H

#include "MemoryManager.h"
#include "MatrixVector.h"
#include "Parameters.h"
#include "Serializable.h"

namespace AMDiS {

  // ===========================================================================
  // ===== class AdaptInfo =====================================================
  // ===========================================================================

  /**
   * \ingroup Adaption
   * 
   * \brief
   * Holds adapt parameters and infos about the problem. Base class
   * for AdaptInfoScal and AdaptInfoVec.
   */
  class AdaptInfo : public Serializable
  {
  protected:
    /** \brief
     * Stores adapt infos for a scalar problem or for one component of a 
     * vector valued problem.
     */
    class ScalContent {
    public:
      /** \brief
       * Constructor.
       */
Thomas Witkowski's avatar
Thomas Witkowski committed
55
      ScalContent(std::string prefix) 
56
57
58
59
60
61
62
63
64
65
	: est_sum(0.0),
	  est_t_sum(0.0),
	  est_max(0.0),
	  est_t_max(0.0),
	  spaceTolerance(1.0),
	  timeTolerance(1.0),
	  timeErrLow(1.0),
	  coarsenAllowed(0),
	  refinementAllowed(1),
	  refineBisections(1),
66
  	  coarseBisections(1)
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
      {
	double totalTol = 1.0;
	double relSpaceErr = 1.0;
	double relTimeErr = 0.5;
	double timeTheta1 = 1.0;
	double timeTheta2 = 0.3;

	GET_PARAMETER(0, prefix + "->tolerance", "%f", &totalTol);
	GET_PARAMETER(0, prefix + "->rel space error", "%f", &relSpaceErr);
	GET_PARAMETER(0, prefix + "->rel time error", "%f", &relTimeErr);
	GET_PARAMETER(0, prefix + "->time theta 1", "%f", &timeTheta1);
	GET_PARAMETER(0, prefix + "->time theta 2", "%f", &timeTheta2);
	GET_PARAMETER(0, prefix + "->coarsen allowed", "%d", &coarsenAllowed);
	GET_PARAMETER(0, prefix + "->refinement allowed", "%d", &refinementAllowed);
	GET_PARAMETER(0, prefix + "->refine bisections", "%d", &refineBisections);
	GET_PARAMETER(0, prefix + "->coarsen bisections", "%d", &coarseBisections);

	spaceTolerance = totalTol * relSpaceErr;
	timeTolerance = totalTol * relTimeErr * timeTheta1;
	timeErrLow = totalTol * relTimeErr * timeTheta2;
87
      }
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152

      /** \brief
       * Sum of all error estimates
       */
      double est_sum;

      /** \brief
       * Sum of all time error estimates
       */
      double est_t_sum;

      /** \brief
       * maximal local error estimate.
       */
      double est_max;

      /** \brief
       * Maximum of all time error estimates
       */
      double est_t_max;

      /** \brief
       * Tolerance for the (absolute or relative) error
       */
      double spaceTolerance;

      /** \brief
       * Time tolerance.
       */
      double timeTolerance;

      /** \brief
       * Lower bound for the time error.
       */
      double timeErrLow;

      /** \brief
       * true if coarsening is allowed, false otherwise.
       */
      int coarsenAllowed;

      /** \brief
       * true if refinement is allowed, false otherwise.
       */
      int refinementAllowed;

      /** \brief
       * parameter to tell the marking strategy how many bisections should be 
       * performed when an element is marked for refinement; usually the value is
       * 1 or DIM
       */
      int  refineBisections;

      /** \brief
       * parameter to tell the marking strategy how many bisections should
       * be undone when an element is marked for coarsening; usually the value is 
       * 1 or DIM
       */                          
      int  coarseBisections;    
    };

  public:
    /** \brief
     * Constructor.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
153
    AdaptInfo(std::string name_, int size = 1) 
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
      : name(name_), 
	spaceIteration(-1),
	maxSpaceIteration(-1),
	timestepIteration(0),
	maxTimestepIteration(30),
	timeIteration(0),
	maxTimeIteration(30),
	time(0.0),
	startTime(0.0),
	endTime(1.0),
	timestep(0.0),
	minTimestep(0.0),
	maxTimestep(1.0),
	timestepNumber(0),
	solverIterations(0),
	maxSolverIterations(0),
	solverTolerance(1e-8),
	solverResidual(0.0),
172
173
        scalContents(size),
	isDeserialized_(false)
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
    {
      GET_PARAMETER(0, name_ + "->start time", "%f", &startTime);
      time = startTime;
      GET_PARAMETER(0, name_ + "->timestep", "%f", &timestep);
      GET_PARAMETER(0, name_ + "->end time", "%f", &endTime);
      GET_PARAMETER(0, name_ + "->max iteration", "%d", &maxSpaceIteration);
      GET_PARAMETER(0, name_ + "->max timestep iteration", "%d", &maxTimestepIteration);
      GET_PARAMETER(0, name_ + "->max time iteration", "%d", &maxTimeIteration);

      GET_PARAMETER(0, name_ + "->min timestep", "%f", &minTimestep);
      GET_PARAMETER(0, name_ + "->max timestep", "%f", &maxTimestep);

      if (size == 1) {
	scalContents[0] = new ScalContent(name); 
      } else {
	char number[5];
	for (int i = 0; i < size; i++) {
	  sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
192
	  scalContents[i] = new ScalContent(name + std::string(number)); 
193
194
	}
      }
195
    }
196
197
198
199
200

    /** \brief
     * Destructor.
     */
    virtual ~AdaptInfo() {
201
      for (int i = 0;  i < scalContents.getSize(); i++) {
202
203
	delete scalContents[i];
      }
204
    }
205
206
207
208
209
210
211
212
213
214
215

    inline void reset() 
    {
      spaceIteration = -1;
      timestepIteration = 0;
      timeIteration = 0;
      time = 0.0;
      timestep = 0.0;
      timestepNumber = 0;
      solverIterations = 0;
      solverResidual = 0.0;
216
217

      GET_PARAMETER(0, name + "->timestep", "%f", &timestep);
218
    }
219
220
221
222
223

    /** \brief
     * Returns whether space tolerance is reached.
     */
    virtual bool spaceToleranceReached() {
224
225
226
      int size = scalContents.getSize();
      for (int i = 0; i < size; i++) {
	if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance)) {
227
228
229
230
	  return false;
	}
      }
      return true;
231
    }
232
233
234
235
236

    /** \brief
     * Returns whether space tolerance of component i is reached.
     */
    virtual bool spaceToleranceReached(int i) {
237
      if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance)) {
238
239
240
241
	return false;
      } else {
	return true;
      }
242
    }
243
244
245
246
247

    /** \brief
     * Returns whether time tolerance is reached.
     */
    virtual bool timeToleranceReached() {
248
249
250
      int size = scalContents.getSize();
      for (int i = 0; i < size; i++) {
	if (!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance)) {
251
252
253
254
	  return false;
	}
      }
      return true;
255
    }
256
257
258
259
260

    /** \brief
     * Returns whether time tolerance of component i is reached.
     */
    virtual bool timeToleranceReached(int i) {
261
      if (!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance)) {
262
263
264
265
	return false;
      } else {
	return true;
      }
266
    }
267
268
269
270
271
272
273
274
275
276
277
278

    /** \brief
     * Returns whether time error is under its lower bound.
     */
    virtual bool timeErrorLow() {
      int size = scalContents.getSize();
      for (int i = 0; i < size; i++) {
	if (!(scalContents[i]->est_t_sum < scalContents[i]->timeErrLow)) {
	  return false;
	}
      }
      return true;
279
280
281
282
283
284
285
286
287
288
289
    }

    /** \brief
     * Print debug information about time error and its bound.
     */
    void printTimeErrorLowInfo() {
      for (int i = 0; i < scalContents.getSize(); i++) {
	std::cout << "    Time error estimate = " << scalContents[i]->est_t_sum 
		  << "    Time error bound = " << scalContents[i]->timeErrLow << "\n";
      }
    }
290
291
292
293
294
295

    /** \brief
     * Returns \ref spaceIteration.
     */
    inline int getSpaceIteration() { 
      return spaceIteration; 
296
    }
297
298
299
300
301
302

    /** \brief
     * Sets \ref spaceIteration.
     */
    inline void setSpaceIteration(int it) { 
      spaceIteration = it; 
303
    }
304
305
306
307
308
309
  
    /** \brief
     * Returns \ref maxSpaceIteration.
     */
    inline int getMaxSpaceIteration() { 
      return maxSpaceIteration;
310
    }
311
312
313
314
315
316

    /** \brief
     * Sets \ref maxSpaceIteration.
     */
    inline void setMaxSpaceIteration(int it) { 
      maxSpaceIteration = it; 
317
    }
318
319
320
321
322
323
  
    /** \brief
     * Increments \ref spaceIteration by 1;
     */
    inline void incSpaceIteration() { 
      spaceIteration++; 
324
    }
325
326
327
328
329
330

    /** \brief
     * Sets \ref timestepIteration.
     */
    inline void setTimestepIteration(int it) { 
      timestepIteration = it; 
331
    }
332
333
334
335
336
337
  
    /** \brief
     * Returns \ref timestepIteration.
     */
    inline int getTimestepIteration() { 
      return timestepIteration; 
338
    }
339
340
341
342
343
344

    /** \brief
     * Increments \ref timestepIteration by 1;
     */
    inline void incTimestepIteration() { 
      timestepIteration++; 
345
    }
346
347
348
349
350
351

    /** \brief
     * Returns \ref maxTimestepIteration.
     */
    inline int getMaxTimestepIteration() { 
      return maxTimestepIteration; 
352
    }
353
354
355
356
357
358

    /** \brief
     * Sets \ref maxTimestepIteration.
     */
    inline void setMaxTimestepIteration(int it) { 
      maxTimestepIteration = it; 
359
    }
360
361
362
363
364
365
  
    /** \brief
     * Sets \ref timeIteration.
     */
    inline void setTimeIteration(int it) { 
      timeIteration = it; 
366
    }
367
368
369
370
371
372
  
    /** \brief
     * Returns \ref timeIteration.
     */
    inline int getTimeIteration() { 
      return timeIteration; 
373
    }
374
375
376
377
378
379

    /** \brief
     * Increments \ref timesIteration by 1;
     */
    inline void incTimeIteration() { 
      timeIteration++; 
380
    }
381
382
383
384
385
386

    /** \brief
     * Returns \ref maxTimeIteration.
     */
    inline int getMaxTimeIteration() { 
      return maxTimeIteration; 
387
    }
388
389
390
391
392
393

    /** \brief
     * Sets \ref maxTimeIteration.
     */
    inline void setMaxTimeIteration(int it) { 
      maxTimeIteration = it; 
394
    }
395
396
397
398
399
400
  
    /** \brief
     * Returns \ref timestepNumber.
     */
    inline int getTimestepNumber() { 
      return timestepNumber; 
401
    }
402
403
404
405
406
407

    /** \brief
     * Increments \ref timestepNumber by 1;
     */
    inline void incTimestepNumber() { 
      timestepNumber++; 
408
    }
409
410
411
412
413
414

    /** \brief
     * Sets \ref est_sum.
     */
    inline void setEstSum(double e, int index) {
      scalContents[index]->est_sum = e;
415
    }
416
417
418
419
420
421

    /** \brief
     * Sets \ref est_max.
     */
    inline void setEstMax(double e, int index) {
      scalContents[index]->est_max = e;
422
    }
423
424
425
426
427
428

    /** \brief
     * Sets \ref est_max.
     */
    inline void setTimeEstMax(double e, int index) {
      scalContents[index]->est_t_max = e;
429
    }
430
431
432
433
434
435

    /** \brief
     * Sets \ref est_t_sum.
     */
    inline void setTimeEstSum(double e, int index) {
      scalContents[index]->est_t_sum = e;
436
    }
437
438
439
440
441
442

    /** \brief
     * Returns \ref est_sum.
     */
    inline double getEstSum(int index) { 
      return scalContents[index]->est_sum; 
443
    }
444

445
446
447
448
449
    /** \brief
     * Returns \ref est_t_sum.
     */
    inline double getEstTSum(int index) { 
      return scalContents[index]->est_t_sum; 
450
    }
451

452
453
454
455
456
    /** \brief
     * Returns \ref est_max.
     */
    inline double getEstMax(int index) { 
      return scalContents[index]->est_max; 
457
    }
458
459
460
461
462
463

    /** \brief
     * Returns \ref est_max.
     */
    inline double getTimeEstMax(int index) { 
      return scalContents[index]->est_t_max; 
464
    }
465
466
467
468
469
470

    /** \brief
     * Returns \ref est_t_sum.
     */
    inline double getTimeEstSum(int index) { 
      return scalContents[index]->est_t_sum; 
471
    }
472
473
474
475
476
477

    /** \brief
     * Returns \ref spaceTolerance.
     */
    inline double getSpaceTolerance(int index) { 
      return scalContents[index]->spaceTolerance; 
478
    }  
479
480
481
482
483
484

    /** \brief
     * Sets \ref spaceTolerance.
     */
    inline void setSpaceTolerance(int index, double tol) { 
      scalContents[index]->spaceTolerance = tol; 
485
    }  
486
487
488
489
490
491

    /** \brief
     * Returns \ref timeTolerance.
     */
    inline double getTimeTolerance(int index) { 
      return scalContents[index]->timeTolerance; 
492
    }  
493
494
495
496
497
498

    /** \brief
     * Sets \ref time
     */
    inline double setTime(double t) { 
      time = t; 
499
500
501
502
503
      if (time > endTime) 
	time = endTime;
      if (time < startTime) 
	time = startTime;

504
      return time;
505
    }
506
507
508
509
510
511

    /** \brief
     * Gets \ref time
     */
    inline double getTime() { 
      return time; 
512
    }  
513
514
515
516
517
518

    /** \brief
     * Gets \ref &time
     */
    inline double *getTimePtr() { 
      return &time; 
519
    }  
520
521
522
523
524
525

    /** \brief
     * Sets \ref timestep
     */
    inline double setTimestep(double t) { 
      timestep = t; 
526
      if (timestep > maxTimestep) {
527
	timestep = maxTimestep;
528
529
      }
      if (timestep < minTimestep) {
530
	timestep = minTimestep;
531
532
      }
      if (time + timestep > endTime) {
533
	timestep = endTime - time;
534
      }
535
      
536
      return timestep;
537
    }
538
539
540
541
542
543

    /** \brief
     * Gets \ref timestep
     */
    inline double getTimestep() { 
      return timestep; 
544
    }
545
546
547
548
549
550

    /** \brief
     * Sets \ref minTimestep
     */
    inline void setMinTimestep(double t) { 
      minTimestep = t; 
551
    }
552
553
554
555
556
557

    /** \brief
     * Gets \ref minTimestep
     */
    inline double getMinTimestep() { 
      return minTimestep; 
558
    }  
559
560
561
562
563
564

    /** \brief
     * Sets \ref maxTimestep
     */
    inline void setMaxTimestep(double t) { 
      maxTimestep = t; 
565
    }
566
567
568
569
570
571

    /** \brief
     * Gets \ref maxTimestep
     */
    inline double getMaxTimestep() { 
      return maxTimestep; 
572
    }  
573
574
575
576
577
578

    /** \brief
     * Gets \ref &timestep
     */
    inline double *getTimestepPtr() { 
      return &timestep; 
579
    }  
580
581
582
583
584
585

    /** \brief
     * Sets \ref startTime = time
     */
    inline void setStartTime(double time) { 
      startTime = time; 
586
    }
587
588
589
590
591
592

    /** \brief
     * Sets \ref endTime = time
     */
    inline void setEndTime(double time) { 
      endTime = time; 
593
    }
594
595
596
597
598
599

    /** \brief
     * Returns \ref startTime
     */
    inline double getStartTime() { 
      return startTime; 
600
    }
601
602
603
604
605
606

    /** \brief
     * Returns \ref endTime
     */
    inline double getEndTime() { 
      return endTime; 
607
    }
608
609
610
611
612
613

    /** \brief
     * Returns \ref timeErrLow.
     */
    inline double getTimeErrLow(int index) { 
      return scalContents[index]->timeErrLow; 
614
    }  
615
616
617
618
619
620

    /** \brief
     * Returns whether coarsening is allowed or not.
     */
    inline bool isCoarseningAllowed(int index) {
      return (scalContents[index]->coarsenAllowed == 1);
621
    }
622
623
624
625
626
627

    /** \brief
     * Returns whether coarsening is allowed or not.
     */
    inline bool isRefinementAllowed(int index) {
      return (scalContents[index]->refinementAllowed == 1);
628
    }
629
630
631
632
633
634

    /** \brief
     * 
     */
    inline void allowRefinement(bool allow, int index) {
      scalContents[index]->refinementAllowed = allow;
635
    }
636
637
638
639
640
641

    /** \brief
     * 
     */
    inline void allowCoarsening(bool allow, int index) {
      scalContents[index]->coarsenAllowed = allow;
642
    }
643
644
645
646
647
648

    /** \brief
     * Returns \ref refineBisections
     */
    inline const int getRefineBisections(int index) const {
      return scalContents[index]->refineBisections;
649
    }
650
651
652
653
654
655

    /** \brief
     * Returns \ref coarseBisections
     */
    inline const int getCoarseBisections(int index) const {
      return scalContents[index]->coarseBisections;
656
    }
657
658
659

    inline int getSize() { 
      return scalContents.getSize(); 
660
    }
661
662
663

    inline void setSolverIterations(int it) {
      solverIterations = it;
664
    }
665
666
667
  
    inline int getSolverIterations() {
      return solverIterations;
668
    }
669
670
671
  
    inline void setMaxSolverIterations(int it) {
      maxSolverIterations = it;
672
    }
673
674
675
  
    inline int getMaxSolverIterations() {
      return maxSolverIterations;
676
    }
677
678
679
  
    inline void setSolverTolerance(double tol) {
      solverTolerance = tol;
680
    }
681
682
683
  
    inline double getSolverTolerance() {
      return solverTolerance;
684
    }
685
686
687
  
    inline void setSolverResidual(double res) {
      solverResidual = res;
688
    }
689
690
691
  
    inline double getSolverResidual() {
      return solverResidual;
692
    }
693

694
695
696
697
698
699
700
701
702
703
704
    /** \brief
     * Returns true, if the adaptive procedure was deserialized from a file.
     */
    const bool isDeserialized() const {
      return isDeserialized_;
    }

    inline void setIsDeserialized(bool b) {
      isDeserialized_ = b;
    }

705
706
707
708
709
    /** \brief
     * Creates new scalContents with the given size.
     */
    void setScalContents(int newSize);

710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
    /** \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;
    }

725
    // ===== Serialiazable implementation =====
Thomas Witkowski's avatar
Thomas Witkowski committed
726
    void serialize(std::ostream& out);
727

Thomas Witkowski's avatar
Thomas Witkowski committed
728
    void deserialize(std::istream& in);
729
730
731
732
733

  protected:
    /** \brief
     * Name.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
734
    std::string name;
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
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
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812

    /** \brief
     * Current space iteration
     */
    int spaceIteration;

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

    /** \brief
     * Current timestep iteration
     */
    int timestepIteration;

    /** \brief
     * Maximal number of iterations for choosing a timestep
     */
    int maxTimestepIteration;

    /** \brief
     * Current time iteration
     */
    int timeIteration;

    /** \brief
     * Maximal number of time iterations
     */
    int maxTimeIteration;

    /** \brief
     * Actual time, end of time interval for current time step
     */
    double time;

    /** \brief
     * Initial time
     */
    double startTime;

    /** \brief
     * Final time
     */
    double endTime;

    /** \brief
     * Current time step size
     */
    double timestep;

    /** \brief
     * Minimal step size
     */
    double minTimestep;

    /** \brief
     * Maximal step size
     */
    double maxTimestep;

    /** \brief
     * Number of current time step
     */
    int timestepNumber;
  
    /** \brief
     * number of iterations needed of linear or nonlinear solver
     */
    int solverIterations;


    /** \brief
     * maximal number of iterations needed of linear or nonlinear solver
     */
    int maxSolverIterations;

813
814
815
    /** \brief
     *
     */
816
817
    double solverTolerance;

818
819
820
    /** \brief
     *
     */
821
822
823
824
825
826
    double solverResidual;

    /** \brief
     * Scalar adapt infos.
     */
    Vector<ScalContent*> scalContents;
827
828
829
830
831

    /** \brief
     * Is true, if the adaptive procedure was deserialized from a file.
     */
    bool isDeserialized_;
832
833
834
835
836
  };

}

#endif //  AMDIS_ADAPTINFO_H