AdaptInfo.h 18.9 KB
Newer Older
1
2
3
4
5
6
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
7
// ==  TU Dresden                                                            ==
8
// ==                                                                        ==
9
10
11
// ==  Institut fr Wissenschaftliches Rechnen                               ==
// ==  Zellescher Weg 12-14                                                  ==
// ==  01069 Dresden                                                         ==
12
13
14
15
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
16
// ==  https://gforge.zih.tu-dresden.de/projects/amdis/                      ==
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
// ==                                                                        ==
// ============================================================================

/** \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
	: est_sum(0.0),
50
51
52
53
54
	est_t_sum(0.0),
	est_max(0.0),
	est_t_max(0.0),
	fac_max(0.0),
	fac_sum(1.0),
55
56
57
	spaceTolerance(0.0),
	timeTolerance(0.0),
	timeErrLow(0.0),
58
59
60
61
62
	coarsenAllowed(0),
	refinementAllowed(1),
	refineBisections(1),
	coarseBisections(1)
	
63
64
65
      {
	double timeTheta2 = 0.3;

66
67
68
69
	// TODO: obsolete parameters timeTheta2, relTimeErr, relSpaceErr

	GET_PARAMETER(0, prefix + "->tolerance", "%f", &spaceTolerance);
	GET_PARAMETER(0, prefix + "->time tolerance", "%f", &timeTolerance);
70
71
72
73
74
	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);
75
76
	GET_PARAMETER(0, prefix + "->sum factor", "%f", &fac_sum);
	GET_PARAMETER(0, prefix + "->max factor", "%f", &fac_max);
77

78
	timeErrLow = timeTolerance * timeTheta2;
79
      }
80

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

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

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

90
      /// Maximum of all time error estimates
91
      double est_t_max;
92
93
94
      
      /// factors to combine max and integral time estimate
      double fac_max, fac_sum;
95

96
      /// Tolerance for the (absolute or relative) error
97
98
      double spaceTolerance;

99
      /// Time tolerance.
100
101
      double timeTolerance;

102
      /// Lower bound for the time error.
103
104
      double timeErrLow;

105
      /// true if coarsening is allowed, false otherwise.
106
107
      int coarsenAllowed;

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

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

  public:
127
    /// Constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
128
    AdaptInfo(std::string name_, int size = 1) 
129
130
131
132
133
134
135
136
137
138
139
      : 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),
140
	lastProcessedTimestep(0.0),
141
142
143
	minTimestep(0.0),
	maxTimestep(1.0),
	timestepNumber(0),
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
144
	nTimesteps(0),
145
146
147
148
	solverIterations(0),
	maxSolverIterations(0),
	solverTolerance(1e-8),
	solverResidual(0.0),
149
150
        scalContents(size),
	isDeserialized_(false)
151
152
153
154
155
156
157
158
159
160
161
162
    {
      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
163
164
      GET_PARAMETER(0, name_ + "->number of timesteps", "%d", &nTimesteps);

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

176
    /// Destructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
177
178
179
    virtual ~AdaptInfo() 
    {
      for (unsigned int i = 0;  i < scalContents.size(); i++)
180
	delete scalContents[i];
181
    }
182
183
184
185
186
187
188
189
190
191
192

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

      GET_PARAMETER(0, name + "->timestep", "%f", &timestep);
195
      lastProcessedTimestep=timestep;
196
    }
197

198
    /// Returns whether space tolerance is reached.
199
200
    virtual bool spaceToleranceReached() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
201
202
203
204
      for (unsigned int i = 0; i < scalContents.size(); i++) {
	std::cout << "est_sum:" <<scalContents[i]->est_sum 
		  << " spaceTol: " << scalContents[i]->spaceTolerance 
		  << std::endl;
205
	if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance))
206
	  return false;
207
      }
208

209
      return true;
210
    }
211

212
    /// Returns whether space tolerance of component i is reached.
213
214
    virtual bool spaceToleranceReached(int i) 
    {
215
      if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance))
216
	return false;
217
      else
218
	return true;
219
    }
220

221
    /// Returns whether time tolerance is reached.
222
223
    virtual bool timeToleranceReached() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
224
      for (unsigned int i = 0; i < scalContents.size(); i++)
225
	if (!(getTimeEstCombined(i) < scalContents[i]->timeTolerance))
226
	  return false;
227

228
      return true;
229
    }
230

231
    /// Returns whether time tolerance of component i is reached.
232
233
    virtual bool timeToleranceReached(int i) 
    {
234
      if (!(getTimeEstCombined(i) < scalContents[i]->timeTolerance))
235
	return false;
236
      else
237
	return true;
238
    }
239

240
    /// Returns whether time error is under its lower bound.
241
242
    virtual bool timeErrorLow() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
243
      for (unsigned int i = 0; i < scalContents.size(); i++)
244
	if (!(getTimeEstCombined(i) < scalContents[i]->timeErrLow))
245
	  return false;
246

247
      return true;
248
    }
249
250
251
252
253
254
255
256
    /// Returns the time estimation as a combination 
    /// of maximal and integral time error 
    double getTimeEstCombined(unsigned i) const 
    { 
      return scalContents[i]->est_t_max*scalContents[i]->fac_max
	+scalContents[i]->est_t_sum*scalContents[i]->fac_sum; 
    }

257

258
    /// Print debug information about time error and its bound.
259
260
    void printTimeErrorLowInfo() 
    {
261
262
263
264
      for (unsigned int i = 0; i < scalContents.size(); i++){
	std::cout << "    Time error estimate  = " << getTimeEstCombined(i)
		  << "    Time error estimate sum = " << scalContents[i]->est_t_sum 
		  << "    Time error estimate max = " << scalContents[i]->est_t_max 
265
266
		  << "    Time error low bound = " << scalContents[i]->timeErrLow  
		  << "    Time error high bound = " << scalContents[i]->timeTolerance << "\n";
267
      }
268
    }
269

270
    /// Returns \ref spaceIteration.
271
272
    inline int getSpaceIteration() 
    { 
273
      return spaceIteration; 
274
    }
275

276
    /// Sets \ref spaceIteration.
277
278
    inline void setSpaceIteration(int it) 
    { 
279
      spaceIteration = it; 
280
    }
281
  
282
    /// Returns \ref maxSpaceIteration.
283
284
    inline int getMaxSpaceIteration() 
    { 
285
      return maxSpaceIteration;
286
    }
287

288
    /// Sets \ref maxSpaceIteration.
289
290
    inline void setMaxSpaceIteration(int it) 
    { 
291
      maxSpaceIteration = it; 
292
    }
293
  
294
    /// Increments \ref spaceIteration by 1;
295
296
    inline void incSpaceIteration() 
    { 
297
      spaceIteration++; 
298
    }
299

300
    /// Sets \ref timestepIteration.
301
302
    inline void setTimestepIteration(int it) 
    { 
303
      timestepIteration = it; 
304
    }
305
  
306
    /// Returns \ref timestepIteration.
307
308
    inline int getTimestepIteration() 
    { 
309
      return timestepIteration; 
310
    }
311

312
    /// Increments \ref timestepIteration by 1;
313
314
    inline void incTimestepIteration() 
    { 
315
      timestepIteration++; 
316
    }
317

318
    /// Returns \ref maxTimestepIteration.
319
320
    inline int getMaxTimestepIteration() 
    { 
321
      return maxTimestepIteration; 
322
    }
323

324
    /// Sets \ref maxTimestepIteration.
325
326
    inline void setMaxTimestepIteration(int it) 
    { 
327
      maxTimestepIteration = it; 
328
    }
329
  
330
    /// Sets \ref timeIteration.
331
332
    inline void setTimeIteration(int it) 
    { 
333
      timeIteration = it; 
334
    }
335
  
336
    /// Returns \ref timeIteration.
337
338
    inline int getTimeIteration() 
    { 
339
      return timeIteration; 
340
    }
341

342
    /// Increments \ref timesIteration by 1;
343
344
    inline void incTimeIteration() 
    { 
345
      timeIteration++; 
346
    }
347

348
    /// Returns \ref maxTimeIteration.
349
350
    inline int getMaxTimeIteration() 
    { 
351
      return maxTimeIteration; 
352
    }
353

354
    /// Sets \ref maxTimeIteration.
355
356
    inline void setMaxTimeIteration(int it) 
    { 
357
      maxTimeIteration = it; 
358
    }
359
  
360
    /// Returns \ref timestepNumber.
361
362
    inline int getTimestepNumber() 
    { 
363
      return timestepNumber; 
364
    }
365

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
366
    /// Returns \ref nTimesteps.
367
368
    inline int getNumberOfTimesteps() 
    {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
369
370
371
      return nTimesteps;
    }

372
    /// Increments \ref timestepNumber by 1;
373
374
    inline void incTimestepNumber() 
    { 
375
      timestepNumber++; 
376
    }
377

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

384
    /// Sets \ref est_max.
385
386
    inline void setEstMax(double e, int index) 
    {
387
      scalContents[index]->est_max = e;
388
    }
389

390
    /// Sets \ref est_max.
391
392
    inline void setTimeEstMax(double e, int index) 
    {
393
      scalContents[index]->est_t_max = e;
394
    }
395

396
    /// Sets \ref est_t_sum.
397
398
    inline void setTimeEstSum(double e, int index) 
    {
399
      scalContents[index]->est_t_sum = e;
400
    }
401

402
    /// Returns \ref est_sum.
403
404
    inline double getEstSum(int index) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
405
406
407
408
409
      FUNCNAME("AdaptInfo::getEstSum()");

      TEST_EXIT_DBG(static_cast<unsigned int>(index) < scalContents.size())
	("Wrong index for adaptInfo!\n");

410
      return scalContents[index]->est_sum; 
411
    }
412

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

419
    /// Returns \ref est_max.
420
421
    inline double getEstMax(int index) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
422
423
424
425
426
      FUNCNAME("AdaptInfo::getEstSum()");

      TEST_EXIT_DBG(static_cast<unsigned int>(index) < scalContents.size())
	("Wrong index for adaptInfo!\n");

427
      return scalContents[index]->est_max; 
428
    }
429

430
    /// Returns \ref est_max.
431
432
    inline double getTimeEstMax(int index) 
    { 
433
      return scalContents[index]->est_t_max; 
434
    }
435

436
    /// Returns \ref est_t_sum.
437
438
    inline double getTimeEstSum(int index) 
    { 
439
      return scalContents[index]->est_t_sum; 
440
    }
441

442
    /// Returns \ref spaceTolerance.
443
444
    inline double getSpaceTolerance(int index) 
    { 
445
      return scalContents[index]->spaceTolerance; 
446
    }  
447

448
    /// Sets \ref spaceTolerance.
449
450
    inline void setSpaceTolerance(int index, double tol) 
    { 
451
      scalContents[index]->spaceTolerance = tol; 
452
    }  
453

454
    /// Returns \ref timeTolerance.
455
456
    inline double getTimeTolerance(int index) 
    { 
457
      return scalContents[index]->timeTolerance; 
458
    }  
459

460
    /// Sets \ref time
461
462
    inline double setTime(double t) 
    { 
463
      time = t; 
464
465
466
467
468
      if (time > endTime) 
	time = endTime;
      if (time < startTime) 
	time = startTime;

469
      return time;
470
    }
471

472
    /// Gets \ref time
473
474
    inline double getTime() 
    { 
475
      return time; 
476
    }  
477

478
    /// Gets \ref &time
479
480
    inline double *getTimePtr() 
    { 
481
      return &time; 
482
    }  
483

484
    /// Sets \ref timestep
485
486
    inline double setTimestep(double t) 
    { 
487
      timestep = t; 
488
      if (timestep > maxTimestep)
489
	timestep = maxTimestep;
490
      if (timestep < minTimestep)
491
	timestep = minTimestep;
492
      if (time + timestep > endTime)
493
494
	timestep = endTime - time;
      
495
      return timestep;
496
    }
497
498
499
500
501
502
503
504
505
506
507
508
509
    /// Gets \ref timestep
    inline double getTimestep() 
    { 
      return timestep; 
    }

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

    inline double getLastProcessedTimestep(){
	return lastProcessedTimestep;
    } 
510

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
511
512
513
514
    /** \brief
     * Returns true, if the end time is reached and no more timestep
     * computations must be done.
     */
515
516
    inline bool reachedEndTime() 
    {
517
518
519
520
      if (nTimesteps > 0) 
	return !(timestepNumber < nTimesteps);

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

523

524
    /// Sets \ref minTimestep
525
526
    inline void setMinTimestep(double t) 
    { 
527
      minTimestep = t; 
528
    }
529

530
    /// Gets \ref minTimestep
531
532
    inline double getMinTimestep() 
    { 
533
      return minTimestep; 
534
    }  
535

536
    /// Sets \ref maxTimestep
537
538
    inline void setMaxTimestep(double t) 
    { 
539
      maxTimestep = t; 
540
    }
541

542
    /// Gets \ref maxTimestep
543
544
    inline double getMaxTimestep() 
    { 
545
      return maxTimestep; 
546
    }  
547

548
    /// Gets \ref &timestep
549
550
    inline double *getTimestepPtr() 
    { 
551
      return &timestep; 
552
    }  
553

554
    /// Sets \ref startTime = time
555
556
    inline void setStartTime(double time) 
    { 
557
      startTime = time; 
558
    }
559

560
    /// Sets \ref endTime = time
561
562
    inline void setEndTime(double time) 
    { 
563
      endTime = time; 
564
    }
565

566
    /// Returns \ref startTime
567
568
    inline double getStartTime() 
    { 
569
      return startTime; 
570
    }
571

572
    /// Returns \ref endTime
573
574
    inline double getEndTime() 
    { 
575
      return endTime; 
576
    }
577

578
    /// Returns \ref timeErrLow.
579
580
    inline double getTimeErrLow(int index) 
    { 
581
      return scalContents[index]->timeErrLow; 
582
    }  
583

584
    /// Returns whether coarsening is allowed or not.
585
586
    inline bool isCoarseningAllowed(int index) 
    {
587
      return (scalContents[index]->coarsenAllowed == 1);
588
    }
589

590
    /// Returns whether coarsening is allowed or not.
591
592
    inline bool isRefinementAllowed(int index) 
    {
593
      return (scalContents[index]->refinementAllowed == 1);
594
    }
595

596
    ///
597
598
    inline void allowRefinement(bool allow, int index) 
    {
599
      scalContents[index]->refinementAllowed = allow;
600
    }
601

602
    ///
603
604
    inline void allowCoarsening(bool allow, int index) 
    {
605
      scalContents[index]->coarsenAllowed = allow;
606
    }
607

608
    /// Returns \ref refineBisections
609
610
    inline const int getRefineBisections(int index) const 
    {
611
      return scalContents[index]->refineBisections;
612
    }
613

614
    /// Returns \ref coarseBisections
615
616
    inline const int getCoarseBisections(int index) const 
    {
617
      return scalContents[index]->coarseBisections;
618
    }
619

620
621
    inline int getSize() 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
622
      return scalContents.size(); 
623
    }
624

625
626
    inline void setSolverIterations(int it) 
    {
627
      solverIterations = it;
628
    }
629
  
630
631
    inline int getSolverIterations() 
    {
632
      return solverIterations;
633
    }
634
  
635
636
    inline void setMaxSolverIterations(int it) 
    {
637
      maxSolverIterations = it;
638
    }
639
  
640
641
    inline int getMaxSolverIterations() 
    {
642
      return maxSolverIterations;
643
    }
644
  
645
646
    inline void setSolverTolerance(double tol) 
    {
647
      solverTolerance = tol;
648
    }
649
  
650
651
    inline double getSolverTolerance() 
    {
652
      return solverTolerance;
653
    }
654
  
655
656
    inline void setSolverResidual(double res) 
    {
657
      solverResidual = res;
658
    }
659
  
660
661
    inline double getSolverResidual() 
    {
662
      return solverResidual;
663
    }
664

665
    /// Returns true, if the adaptive procedure was deserialized from a file.
666
667
    const bool isDeserialized() const 
    {
668
669
670
      return isDeserialized_;
    }

671
672
    inline void setIsDeserialized(bool b) 
    {
673
674
675
      isDeserialized_ = b;
    }

676
    /// Creates new scalContents with the given size.
677
678
    void setScalContents(int newSize);

679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
    /** \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
694
    void serialize(std::ostream& out);
695

Thomas Witkowski's avatar
Thomas Witkowski committed
696
    void deserialize(std::istream& in);
697
698

  protected:
699
    /// Name.
Thomas Witkowski's avatar
Thomas Witkowski committed
700
    std::string name;
701

702
    /// Current space iteration
703
704
705
706
707
708
709
710
    int spaceIteration;

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

711
    /// Current timestep iteration
712
713
    int timestepIteration;

714
    /// Maximal number of iterations for choosing a timestep
715
716
    int maxTimestepIteration;

717
    /// Current time iteration
718
719
    int timeIteration;

720
    /// Maximal number of time iterations
721
722
    int maxTimeIteration;

723
    /// Actual time, end of time interval for current time step
724
725
    double time;

726
    /// Initial time
727
728
    double startTime;

729
    /// Final time
730
731
    double endTime;

732
    ///Time step size to be used
733
734
    double timestep;

735
736
737
    /// Last processed time step size of finished iteration
    double lastProcessedTimestep;

738
    /// Minimal step size
739
740
    double minTimestep;

741
    /// Maximal step size
742
743
    double maxTimestep;

744
    /// Number of current time step
745
    int timestepNumber;
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
746
747
748
749
750
751
752

    /** \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;
753
  
754
    /// number of iterations needed of linear or nonlinear solver
755
756
    int solverIterations;

757
    /// maximal number of iterations needed of linear or nonlinear solver
758
759
    int maxSolverIterations;

760
    ///
761
762
    double solverTolerance;

763
    ///
764
765
    double solverResidual;

766
    /// Scalar adapt infos.
Thomas Witkowski's avatar
Thomas Witkowski committed
767
    std::vector<ScalContent*> scalContents;
768

769
    /// Is true, if the adaptive procedure was deserialized from a file.
770
    bool isDeserialized_;
771
772
773
774
775
  };

}

#endif //  AMDIS_ADAPTINFO_H