AdaptInfo.h 17.7 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
// ============================================================================
// ==                                                                        ==
// == 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 "MatrixVector.h"
#include "Parameters.h"
#include "Serializable.h"

namespace AMDiS {

  /**
   * \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:
47
      /// Constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
48
      ScalContent(std::string prefix) 
49
50
51
52
53
54
55
56
57
58
	: 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),
59
  	  coarseBisections(1)
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
      {
	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;
80
      }
81

82
      /// Sum of all error estimates
83
84
      double est_sum;

85
      /// Sum of all time error estimates
86
87
      double est_t_sum;

88
      /// maximal local error estimate.
89
90
      double est_max;

91
      /// Maximum of all time error estimates
92
93
      double est_t_max;

94
      /// Tolerance for the (absolute or relative) error
95
96
      double spaceTolerance;

97
      /// Time tolerance.
98
99
      double timeTolerance;

100
      /// Lower bound for the time error.
101
102
      double timeErrLow;

103
      /// true if coarsening is allowed, false otherwise.
104
105
      int coarsenAllowed;

106
      /// true if refinement is allowed, false otherwise.
107
108
109
110
111
112
113
      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
       */
114
      int refineBisections;
115
116
117
118
119
120

      /** \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
       */                          
121
      int coarseBisections;    
122
123
124
    };

  public:
125
    /// Constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
126
    AdaptInfo(std::string name_, int size = 1) 
127
128
129
130
131
132
133
134
135
136
137
138
139
140
      : 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),
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
141
	nTimesteps(0),
142
143
144
145
	solverIterations(0),
	maxSolverIterations(0),
	solverTolerance(1e-8),
	solverResidual(0.0),
146
147
        scalContents(size),
	isDeserialized_(false)
148
149
150
151
152
153
154
155
156
157
158
159
    {
      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);

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
160
161
      GET_PARAMETER(0, name_ + "->number of timesteps", "%d", &nTimesteps);

162
163
164
165
166
167
      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
168
	  scalContents[i] = new ScalContent(name + std::string(number)); 
169
170
	}
      }
171
    }
172

173
    /// Destructor.
174
    virtual ~AdaptInfo() {
175
      for (int i = 0;  i < scalContents.getSize(); i++)
176
	delete scalContents[i];
177
    }
178
179
180
181
182
183
184
185
186
187
188

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

      GET_PARAMETER(0, name + "->timestep", "%f", &timestep);
191
    }
192

193
    /// Returns whether space tolerance is reached.
194
195
    virtual bool spaceToleranceReached() 
    {
196
      int size = scalContents.getSize();
197
198
      for (int i = 0; i < size; i++)
	if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance))
199
	  return false;
200

201
      return true;
202
    }
203

204
    /// Returns whether space tolerance of component i is reached.
205
206
    virtual bool spaceToleranceReached(int i) 
    {
207
      if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance))
208
	return false;
209
      else
210
	return true;
211
    }
212

213
    /// Returns whether time tolerance is reached.
214
215
    virtual bool timeToleranceReached() 
    {
216
      int size = scalContents.getSize();
217
218
      for (int i = 0; i < size; i++)
	if (!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance))
219
	  return false;
220

221
      return true;
222
    }
223

224
    /// Returns whether time tolerance of component i is reached.
225
226
    virtual bool timeToleranceReached(int i) 
    {
227
      if (!(scalContents[i]->est_t_sum < scalContents[i]->timeTolerance))
228
	return false;
229
      else
230
	return true;
231
    }
232

233
    /// Returns whether time error is under its lower bound.
234
235
    virtual bool timeErrorLow() 
    {
236
      int size = scalContents.getSize();
237
238
      for (int i = 0; i < size; i++)
	if (!(scalContents[i]->est_t_sum < scalContents[i]->timeErrLow))
239
	  return false;
240

241
      return true;
242
243
    }

244
    /// Print debug information about time error and its bound.
245
246
    void printTimeErrorLowInfo() 
    {
247
      for (int i = 0; i < scalContents.getSize(); i++)
248
249
250
	std::cout << "    Time error estimate = " << scalContents[i]->est_t_sum 
		  << "    Time error bound = " << scalContents[i]->timeErrLow << "\n";
    }
251

252
    /// Returns \ref spaceIteration.
253
254
    inline int getSpaceIteration() 
    { 
255
      return spaceIteration; 
256
    }
257

258
    /// Sets \ref spaceIteration.
259
260
    inline void setSpaceIteration(int it) 
    { 
261
      spaceIteration = it; 
262
    }
263
  
264
    /// Returns \ref maxSpaceIteration.
265
266
    inline int getMaxSpaceIteration() 
    { 
267
      return maxSpaceIteration;
268
    }
269

270
    /// Sets \ref maxSpaceIteration.
271
272
    inline void setMaxSpaceIteration(int it) 
    { 
273
      maxSpaceIteration = it; 
274
    }
275
  
276
    /// Increments \ref spaceIteration by 1;
277
278
    inline void incSpaceIteration() 
    { 
279
      spaceIteration++; 
280
    }
281

282
    /// Sets \ref timestepIteration.
283
284
    inline void setTimestepIteration(int it) 
    { 
285
      timestepIteration = it; 
286
    }
287
  
288
    /// Returns \ref timestepIteration.
289
290
    inline int getTimestepIteration() 
    { 
291
      return timestepIteration; 
292
    }
293

294
    /// Increments \ref timestepIteration by 1;
295
296
    inline void incTimestepIteration() 
    { 
297
      timestepIteration++; 
298
    }
299

300
    /// Returns \ref maxTimestepIteration.
301
302
    inline int getMaxTimestepIteration() 
    { 
303
      return maxTimestepIteration; 
304
    }
305

306
    /// Sets \ref maxTimestepIteration.
307
308
    inline void setMaxTimestepIteration(int it) 
    { 
309
      maxTimestepIteration = it; 
310
    }
311
  
312
    /// Sets \ref timeIteration.
313
314
    inline void setTimeIteration(int it) 
    { 
315
      timeIteration = it; 
316
    }
317
  
318
    /// Returns \ref timeIteration.
319
320
    inline int getTimeIteration() 
    { 
321
      return timeIteration; 
322
    }
323

324
    /// Increments \ref timesIteration by 1;
325
326
    inline void incTimeIteration() 
    { 
327
      timeIteration++; 
328
    }
329

330
    /// Returns \ref maxTimeIteration.
331
332
    inline int getMaxTimeIteration() 
    { 
333
      return maxTimeIteration; 
334
    }
335

336
    /// Sets \ref maxTimeIteration.
337
338
    inline void setMaxTimeIteration(int it) 
    { 
339
      maxTimeIteration = it; 
340
    }
341
  
342
    /// Returns \ref timestepNumber.
343
344
    inline int getTimestepNumber() 
    { 
345
      return timestepNumber; 
346
    }
347

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
348
    /// Returns \ref nTimesteps.
349
350
    inline int getNumberOfTimesteps() 
    {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
351
352
353
      return nTimesteps;
    }

354
    /// Increments \ref timestepNumber by 1;
355
356
    inline void incTimestepNumber() 
    { 
357
      timestepNumber++; 
358
    }
359

360
    /// Sets \ref est_sum.
361
362
    inline void setEstSum(double e, int index) 
    {
363
      scalContents[index]->est_sum = e;
364
    }
365

366
    /// Sets \ref est_max.
367
368
    inline void setEstMax(double e, int index) 
    {
369
      scalContents[index]->est_max = e;
370
    }
371

372
    /// Sets \ref est_max.
373
374
    inline void setTimeEstMax(double e, int index) 
    {
375
      scalContents[index]->est_t_max = e;
376
    }
377

378
    /// Sets \ref est_t_sum.
379
380
    inline void setTimeEstSum(double e, int index) 
    {
381
      scalContents[index]->est_t_sum = e;
382
    }
383

384
    /// Returns \ref est_sum.
385
386
    inline double getEstSum(int index) 
    { 
387
      return scalContents[index]->est_sum; 
388
    }
389

390
    /// Returns \ref est_t_sum.
391
392
    inline double getEstTSum(int index) 
    { 
393
      return scalContents[index]->est_t_sum; 
394
    }
395

396
    /// Returns \ref est_max.
397
398
    inline double getEstMax(int index) 
    { 
399
      return scalContents[index]->est_max; 
400
    }
401

402
    /// Returns \ref est_max.
403
404
    inline double getTimeEstMax(int index) 
    { 
405
      return scalContents[index]->est_t_max; 
406
    }
407

408
    /// Returns \ref est_t_sum.
409
410
    inline double getTimeEstSum(int index) 
    { 
411
      return scalContents[index]->est_t_sum; 
412
    }
413

414
    /// Returns \ref spaceTolerance.
415
416
    inline double getSpaceTolerance(int index) 
    { 
417
      return scalContents[index]->spaceTolerance; 
418
    }  
419

420
    /// Sets \ref spaceTolerance.
421
422
    inline void setSpaceTolerance(int index, double tol) 
    { 
423
      scalContents[index]->spaceTolerance = tol; 
424
    }  
425

426
    /// Returns \ref timeTolerance.
427
428
    inline double getTimeTolerance(int index) 
    { 
429
      return scalContents[index]->timeTolerance; 
430
    }  
431

432
    /// Sets \ref time
433
434
    inline double setTime(double t) 
    { 
435
      time = t; 
436
437
438
439
440
      if (time > endTime) 
	time = endTime;
      if (time < startTime) 
	time = startTime;

441
      return time;
442
    }
443

444
    /// Gets \ref time
445
446
    inline double getTime() 
    { 
447
      return time; 
448
    }  
449

450
    /// Gets \ref &time
451
452
    inline double *getTimePtr() 
    { 
453
      return &time; 
454
    }  
455

456
    /// Sets \ref timestep
457
458
    inline double setTimestep(double t) 
    { 
459
      timestep = t; 
460
      if (timestep > maxTimestep)
461
	timestep = maxTimestep;
462
      if (timestep < minTimestep)
463
	timestep = minTimestep;
464
      if (time + timestep > endTime)
465
466
	timestep = endTime - time;
      
467
      return timestep;
468
    }
469

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
470
471
472
473
    /** \brief
     * Returns true, if the end time is reached and no more timestep
     * computations must be done.
     */
474
475
    inline bool reachedEndTime() 
    {
476
477
478
479
      if (nTimesteps > 0) 
	return !(timestepNumber < nTimesteps);

      return !(time < endTime - DBL_TOL);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
480
481
    }

482
    /// Gets \ref timestep
483
484
    inline double getTimestep() 
    { 
485
      return timestep; 
486
    }
487

488
    /// Sets \ref minTimestep
489
490
    inline void setMinTimestep(double t) 
    { 
491
      minTimestep = t; 
492
    }
493

494
    /// Gets \ref minTimestep
495
496
    inline double getMinTimestep() 
    { 
497
      return minTimestep; 
498
    }  
499

500
    /// Sets \ref maxTimestep
501
502
    inline void setMaxTimestep(double t) 
    { 
503
      maxTimestep = t; 
504
    }
505

506
    /// Gets \ref maxTimestep
507
508
    inline double getMaxTimestep() 
    { 
509
      return maxTimestep; 
510
    }  
511

512
    /// Gets \ref &timestep
513
514
    inline double *getTimestepPtr() 
    { 
515
      return &timestep; 
516
    }  
517

518
    /// Sets \ref startTime = time
519
520
    inline void setStartTime(double time) 
    { 
521
      startTime = time; 
522
    }
523

524
    /// Sets \ref endTime = time
525
526
    inline void setEndTime(double time) 
    { 
527
      endTime = time; 
528
    }
529

530
    /// Returns \ref startTime
531
532
    inline double getStartTime() 
    { 
533
      return startTime; 
534
    }
535

536
    /// Returns \ref endTime
537
538
    inline double getEndTime() 
    { 
539
      return endTime; 
540
    }
541

542
    /// Returns \ref timeErrLow.
543
544
    inline double getTimeErrLow(int index) 
    { 
545
      return scalContents[index]->timeErrLow; 
546
    }  
547

548
    /// Returns whether coarsening is allowed or not.
549
550
    inline bool isCoarseningAllowed(int index) 
    {
551
      return (scalContents[index]->coarsenAllowed == 1);
552
    }
553

554
    /// Returns whether coarsening is allowed or not.
555
556
    inline bool isRefinementAllowed(int index) 
    {
557
      return (scalContents[index]->refinementAllowed == 1);
558
    }
559

560
    ///
561
562
    inline void allowRefinement(bool allow, int index) 
    {
563
      scalContents[index]->refinementAllowed = allow;
564
    }
565

566
    ///
567
568
    inline void allowCoarsening(bool allow, int index) 
    {
569
      scalContents[index]->coarsenAllowed = allow;
570
    }
571

572
    /// Returns \ref refineBisections
573
574
    inline const int getRefineBisections(int index) const 
    {
575
      return scalContents[index]->refineBisections;
576
    }
577

578
    /// Returns \ref coarseBisections
579
580
    inline const int getCoarseBisections(int index) const 
    {
581
      return scalContents[index]->coarseBisections;
582
    }
583

584
585
    inline int getSize() 
    { 
586
      return scalContents.getSize(); 
587
    }
588

589
590
    inline void setSolverIterations(int it) 
    {
591
      solverIterations = it;
592
    }
593
  
594
595
    inline int getSolverIterations() 
    {
596
      return solverIterations;
597
    }
598
  
599
600
    inline void setMaxSolverIterations(int it) 
    {
601
      maxSolverIterations = it;
602
    }
603
  
604
605
    inline int getMaxSolverIterations() 
    {
606
      return maxSolverIterations;
607
    }
608
  
609
610
    inline void setSolverTolerance(double tol) 
    {
611
      solverTolerance = tol;
612
    }
613
  
614
615
    inline double getSolverTolerance() 
    {
616
      return solverTolerance;
617
    }
618
  
619
620
    inline void setSolverResidual(double res) 
    {
621
      solverResidual = res;
622
    }
623
  
624
625
    inline double getSolverResidual() 
    {
626
      return solverResidual;
627
    }
628

629
    /// Returns true, if the adaptive procedure was deserialized from a file.
630
631
    const bool isDeserialized() const 
    {
632
633
634
      return isDeserialized_;
    }

635
636
    inline void setIsDeserialized(bool b) 
    {
637
638
639
      isDeserialized_ = b;
    }

640
    /// Creates new scalContents with the given size.
641
642
    void setScalContents(int newSize);

643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
    /** \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;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
658
    void serialize(std::ostream& out);
659

Thomas Witkowski's avatar
Thomas Witkowski committed
660
    void deserialize(std::istream& in);
661
662

  protected:
663
    /// Name.
Thomas Witkowski's avatar
Thomas Witkowski committed
664
    std::string name;
665

666
    /// Current space iteration
667
668
669
670
671
672
673
674
    int spaceIteration;

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

675
    /// Current timestep iteration
676
677
    int timestepIteration;

678
    /// Maximal number of iterations for choosing a timestep
679
680
    int maxTimestepIteration;

681
    /// Current time iteration
682
683
    int timeIteration;

684
    /// Maximal number of time iterations
685
686
    int maxTimeIteration;

687
    /// Actual time, end of time interval for current time step
688
689
    double time;

690
    /// Initial time
691
692
    double startTime;

693
    /// Final time
694
695
    double endTime;

696
    /// Current time step size
697
698
    double timestep;

699
    /// Minimal step size
700
701
    double minTimestep;

702
    /// Maximal step size
703
704
    double maxTimestep;

705
    /// Number of current time step
706
    int timestepNumber;
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
707
708
709
710
711
712
713

    /** \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;
714
  
715
    /// number of iterations needed of linear or nonlinear solver
716
717
    int solverIterations;

718
    /// maximal number of iterations needed of linear or nonlinear solver
719
720
    int maxSolverIterations;

721
    ///
722
723
    double solverTolerance;

724
    ///
725
726
    double solverResidual;

727
    /// Scalar adapt infos.
728
    Vector<ScalContent*> scalContents;
729

730
    /// Is true, if the adaptive procedure was deserialized from a file.
731
    bool isDeserialized_;
732
733
734
735
736
  };

}

#endif //  AMDIS_ADAPTINFO_H