AdaptInfo.h 17.8 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
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
153
154
155
156
157
158
159
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
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
517
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
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
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
717
718
719
720
721
722
723
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
758
759
760
761
762
763
764
765
766
767
768
769
770
771
// ============================================================================
// ==                                                                        ==
// == 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.
       */
      ScalContent(::std::string prefix) 
	: 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),
	  coarseBisections(1)
      {
	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;
      };

      /** \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.
     */
    AdaptInfo(::std::string name_, int size = 1) 
      : 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),
	scalContents(size)
    {
      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);
	  scalContents[i] = new ScalContent(name + ::std::string(number)); 
	}
      }
    };

    /** \brief
     * Destructor.
     */
    virtual ~AdaptInfo() {
      int i;
      for(i = 0;  i < scalContents.getSize(); i++) {
	delete scalContents[i];
      }
    };

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

    /** \brief
     * Returns whether space tolerance is reached.
     */
    virtual bool spaceToleranceReached() {
      int i, size = scalContents.getSize();
      for(i = 0; i < size; i++) {
	if(!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance)) {
	  return false;
	}
      }
      return true;
    };

    /** \brief
     * Returns whether space tolerance of component i is reached.
     */
    virtual bool spaceToleranceReached(int i) {
      if(!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance)) {
	return false;
      } else {
	return true;
      }
    };

    /** \brief
     * Returns whether time tolerance is reached.
     */
    virtual bool timeToleranceReached() {
      //if(timestep <= minTimestep) return true;
      int i, size = scalContents.getSize();
      for(i = 0; i < size; i++) {
	if(!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance)) {
	  return false;
	}
      }
      return true;
    };

    /** \brief
     * Returns whether time tolerance of component i is reached.
     */
    virtual bool timeToleranceReached(int i) {
      if(!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance)) {
	return false;
      } else {
	return true;
      }
    };

    /** \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;
    };

    /** \brief
     * Returns \ref spaceIteration.
     */
    inline int getSpaceIteration() { 
      return spaceIteration; 
    };

    /** \brief
     * Sets \ref spaceIteration.
     */
    inline void setSpaceIteration(int it) { 
      spaceIteration = it; 
    };
  
    /** \brief
     * Returns \ref maxSpaceIteration.
     */
    inline int getMaxSpaceIteration() { 
      return maxSpaceIteration;
    };

    /** \brief
     * Sets \ref maxSpaceIteration.
     */
    inline void setMaxSpaceIteration(int it) { 
      maxSpaceIteration = it; 
    };
  
    /** \brief
     * Increments \ref spaceIteration by 1;
     */
    inline void incSpaceIteration() { 
      spaceIteration++; 
    };

    /** \brief
     * Sets \ref timestepIteration.
     */
    inline void setTimestepIteration(int it) { 
      timestepIteration = it; 
    };
  
    /** \brief
     * Returns \ref timestepIteration.
     */
    inline int getTimestepIteration() { 
      return timestepIteration; 
    };

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

    /** \brief
     * Returns \ref maxTimestepIteration.
     */
    inline int getMaxTimestepIteration() { 
      return maxTimestepIteration; 
    };

    /** \brief
     * Sets \ref maxTimestepIteration.
     */
    inline void setMaxTimestepIteration(int it) { 
      maxTimestepIteration = it; 
    };
  
    /** \brief
     * Sets \ref timeIteration.
     */
    inline void setTimeIteration(int it) { 
      timeIteration = it; 
    };
  
    /** \brief
     * Returns \ref timeIteration.
     */
    inline int getTimeIteration() { 
      return timeIteration; 
    };

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

    /** \brief
     * Returns \ref maxTimeIteration.
     */
    inline int getMaxTimeIteration() { 
      return maxTimeIteration; 
    };

    /** \brief
     * Sets \ref maxTimeIteration.
     */
    inline void setMaxTimeIteration(int it) { 
      maxTimeIteration = it; 
    };
  
    /** \brief
     * Returns \ref timestepNumber.
     */
    inline int getTimestepNumber() { 
      return timestepNumber; 
    };

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

    /** \brief
     * Sets \ref est_sum.
     */
    inline void setEstSum(double e, int index) {
      scalContents[index]->est_sum = e;
    };

    /** \brief
     * Sets \ref est_max.
     */
    inline void setEstMax(double e, int index) {
      scalContents[index]->est_max = e;
    };

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

    /** \brief
     * Sets \ref est_t_sum.
     */
    inline void setTimeEstSum(double e, int index) {
      scalContents[index]->est_t_sum = e;
    };

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

    /** \brief
     * Returns \ref est_max.
     */
    inline double getEstMax(int index) { 
      return scalContents[index]->est_max; 
    };

    /** \brief
     * Returns \ref est_max.
     */
    inline double getTimeEstMax(int index) { 
      return scalContents[index]->est_t_max; 
    };

    /** \brief
     * Returns \ref est_t_sum.
     */
    inline double getTimeEstSum(int index) { 
      return scalContents[index]->est_t_sum; 
    };

    /** \brief
     * Returns \ref spaceTolerance.
     */
    inline double getSpaceTolerance(int index) { 
      return scalContents[index]->spaceTolerance; 
    };  

    /** \brief
     * Sets \ref spaceTolerance.
     */
    inline void setSpaceTolerance(int index, double tol) { 
      scalContents[index]->spaceTolerance = tol; 
    };  

    /** \brief
     * Returns \ref timeTolerance.
     */
    inline double getTimeTolerance(int index) { 
      return scalContents[index]->timeTolerance; 
    };  

    /** \brief
     * Sets \ref time
     */
    inline double setTime(double t) { 
      time = t; 
      if (time > endTime) time = endTime;
      if (time < startTime) time = startTime;
      return time;
    };

    /** \brief
     * Gets \ref time
     */
    inline double getTime() { 
      return time; 
    };  

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

    /** \brief
     * Sets \ref timestep
     */
    inline 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;
    };

    /** \brief
     * Gets \ref timestep
     */
    inline double getTimestep() { 
      return timestep; 
    };

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

    /** \brief
     * Gets \ref minTimestep
     */
    inline double getMinTimestep() { 
      return minTimestep; 
    };  

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

    /** \brief
     * Gets \ref maxTimestep
     */
    inline double getMaxTimestep() { 
      return maxTimestep; 
    };  

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

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

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

    /** \brief
     * Returns \ref startTime
     */
    inline double getStartTime() { 
      return startTime; 
    };

    /** \brief
     * Returns \ref endTime
     */
    inline double getEndTime() { 
      return endTime; 
    };

    /** \brief
     * Returns \ref timeErrLow.
     */
    inline double getTimeErrLow(int index) { 
      return scalContents[index]->timeErrLow; 
    };  

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

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

    /** \brief
     * 
     */
    inline void allowRefinement(bool allow, int index) {
      scalContents[index]->refinementAllowed = allow;
    };

    /** \brief
     * 
     */
    inline void allowCoarsening(bool allow, int index) {
      scalContents[index]->coarsenAllowed = allow;
    };

    /** \brief
     * Returns \ref refineBisections
     */
    inline const int getRefineBisections(int index) const {
      return scalContents[index]->refineBisections;
    };

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

    inline int getSize() { 
      return scalContents.getSize(); 
    };

    inline void setSolverIterations(int it) {
      solverIterations = it;
    };
  
    inline int getSolverIterations() {
      return solverIterations;
    };
  
    inline void setMaxSolverIterations(int it) {
      maxSolverIterations = it;
    };
  
    inline int getMaxSolverIterations() {
      return maxSolverIterations;
    };
  
    inline void setSolverTolerance(double tol) {
      solverTolerance = tol;
    };
  
    inline double getSolverTolerance() {
      return solverTolerance;
    };
  
    inline void setSolverResidual(double res) {
      solverResidual = res;
    };
  
    inline double getSolverResidual() {
      return solverResidual;
    };

    /** \brief
     * Creates new scalContents with the given size.
     */
    void setScalContents(int newSize);

    // ===== Serialiazable implementation =====
    void serialize(::std::ostream& out);

    void deserialize(::std::istream& in);

  protected:
    /** \brief
     * Name.
     */
    ::std::string name;

    /** \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;

    double solverTolerance;

    double solverResidual;

    /** \brief
     * Scalar adapt infos.
     */
    Vector<ScalContent*> scalContents;
  };

}

#endif //  AMDIS_ADAPTINFO_H