AdaptInfo.h 18.6 KB
Newer Older
1 2 3 4
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
5
// ==  http://www.amdis-fem.org                                              ==
6 7
// ==                                                                        ==
// ============================================================================
8 9 10 11 12 13 14 15 16 17 18 19
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


20 21 22 23 24 25 26

/** \file AdaptInfo.h */

#ifndef AMDIS_ADAPTINFO_H
#define AMDIS_ADAPTINFO_H

#include "MatrixVector.h"
27
#include "Initfile.h"
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
#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:
48
      /// Constructor.
49
      ScalContent(std::string prefix)
50
	: est_sum(0.0),
51 52 53 54 55 56 57 58 59 60 61 62
	  est_t_sum(0.0),
	  est_max(0.0),
	  est_t_max(0.0),
	  fac_max(0.0),
	  fac_sum(1.0),
	  spaceTolerance(0.0),
	  timeTolerance(0.0),
	  timeErrLow(0.0),
	  coarsenAllowed(0),
	  refinementAllowed(1),
	  refineBisections(1),
	  coarseBisections(1)	  	
63
      {
64 65 66 67 68 69 70 71
	Parameters::get(prefix + "->tolerance", spaceTolerance);
	Parameters::get(prefix + "->time tolerance", timeTolerance);
	Parameters::get(prefix + "->coarsen allowed", coarsenAllowed);
	Parameters::get(prefix + "->refinement allowed", refinementAllowed);
	Parameters::get(prefix + "->refine bisections", refineBisections);
	Parameters::get(prefix + "->coarsen bisections", coarseBisections);
	Parameters::get(prefix + "->sum factor", fac_sum);
	Parameters::get(prefix + "->max factor", fac_max);
72

73
	timeErrLow = timeTolerance * 0.3;
74
      }
75

76
      /// Sum of all error estimates
77 78
      double est_sum;

79
      /// Sum of all time error estimates
80 81
      double est_t_sum;

82
      /// maximal local error estimate.
83 84
      double est_max;

85
      /// Maximum of all time error estimates
86
      double est_t_max;
87 88 89
      
      /// factors to combine max and integral time estimate
      double fac_max, fac_sum;
90

91
      /// Tolerance for the (absolute or relative) error
92 93
      double spaceTolerance;

94
      /// Time tolerance.
95 96
      double timeTolerance;

97
      /// Lower bound for the time error.
98 99
      double timeErrLow;

100
      /// true if coarsening is allowed, false otherwise.
101 102
      int coarsenAllowed;

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

      /** \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
       */                          
118
      int coarseBisections;    
119 120 121
    };

  public:
122
    /// Constructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
123
    AdaptInfo(std::string name_, int size = 1) 
124 125 126 127 128 129 130 131 132 133 134
      : 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),
135
	lastProcessedTimestep(0.0),
136 137 138
	minTimestep(0.0),
	maxTimestep(1.0),
	timestepNumber(0),
Thomas Witkowski's avatar
* Bla  
Thomas Witkowski committed
139
	nTimesteps(0),
140 141 142 143
	solverIterations(0),
	maxSolverIterations(0),
	solverTolerance(1e-8),
	solverResidual(0.0),
144
        scalContents(size),
145 146
	deserialized(false),
	rosenbrockMode(false)
147
    {
148
      init();
149 150
      char number[5];
      for (int i = 0; i < size; i++) {
151 152
	sprintf(number, "[%d]", i);
	scalContents[i] = new ScalContent(name + std::string(number));  
153
      }
154
    }
155

156
    /// Destructor.
Thomas Witkowski's avatar
Thomas Witkowski committed
157 158 159
    virtual ~AdaptInfo() 
    {
      for (unsigned int i = 0;  i < scalContents.size(); i++)
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  delete scalContents[i];
    }

    /// Sets initial values to time/timestep variables
    inline void init()
    {
      Parameters::get(name + "->start time", startTime);
      time = startTime;
      Parameters::get(name + "->timestep", timestep);
      Parameters::get(name + "->end time", endTime);
      Parameters::get(name + "->max iteration", maxSpaceIteration);
      Parameters::get(name + "->max timestep iteration", maxTimestepIteration);
      Parameters::get(name + "->max time iteration", maxTimeIteration);
      Parameters::get(name + "->min timestep", minTimestep);
      Parameters::get(name + "->max timestep", maxTimestep);
      Parameters::get(name + "->number of timesteps", nTimesteps);
176
    }
177

178
    /// Resets all variables to zero (or something equivalent)
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
      Parameters::get(name + "->timestep", timestep);
191
      lastProcessedTimestep=timestep;
192
    }
193

194
    /// Returns whether space tolerance is reached.
195 196
    virtual bool spaceToleranceReached() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
197
      for (unsigned int i = 0; i < scalContents.size(); i++) {
198
	if (!(scalContents[i]->est_sum < scalContents[i]->spaceTolerance))
199
	  return false;
200
      }
201

202
      return true;
203
    }
204

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

214
    /// Returns whether time tolerance is reached.
215 216
    virtual bool timeToleranceReached() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
217
      for (unsigned int i = 0; i < scalContents.size(); i++)
218
	if (!(getTimeEstCombined(i) < 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 (!(getTimeEstCombined(i) < 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() 
    {
Thomas Witkowski's avatar
Thomas Witkowski committed
236
      for (unsigned int i = 0; i < scalContents.size(); i++)
237
	if (!(getTimeEstCombined(i) < scalContents[i]->timeErrLow))
238
	  return false;
239

240
      return true;
241
    }
242 243 244 245
    /// Returns the time estimation as a combination 
    /// of maximal and integral time error 
    double getTimeEstCombined(unsigned i) const 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
246 247 248
      return 
	scalContents[i]->est_t_max * scalContents[i]->fac_max +
	scalContents[i]->est_t_sum * scalContents[i]->fac_sum; 
249 250
    }

251

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

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

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

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

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

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

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

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

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

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

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

365 366 367
    /// Sets \ref timestepNumber.
    inline void setTimestepNumber(int num) 
    {
368
      timestepNumber = std::min(nTimesteps, num);
369 370
    }
    
Thomas Witkowski's avatar
* Bla  
Thomas Witkowski committed
371
    /// Returns \ref nTimesteps.
372 373
    inline int getNumberOfTimesteps() 
    {
Thomas Witkowski's avatar
* Bla  
Thomas Witkowski committed
374 375 376
      return nTimesteps;
    }

377 378 379
    /// Sets \ref nTimesteps.
    inline void setNumberOfTimesteps(int num) 
    {
380
      nTimesteps = std::max(0, num);
381 382
    }

383
    /// Increments \ref timestepNumber by 1;
384 385
    inline void incTimestepNumber() 
    { 
386
      timestepNumber++; 
387
    }
388

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

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

401
    /// Sets \ref est_max.
402 403
    inline void setTimeEstMax(double e, int index) 
    {
404
      scalContents[index]->est_t_max = e;
405
    }
406

407
    /// Sets \ref est_t_sum.
408 409
    inline void setTimeEstSum(double e, int index) 
    {
410
      scalContents[index]->est_t_sum = e;
411
    }
412

413
    /// Returns \ref est_sum.
414 415
    inline double getEstSum(int index) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
416 417 418 419 420
      FUNCNAME("AdaptInfo::getEstSum()");

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

421
      return scalContents[index]->est_sum; 
422
    }
423

424
    /// Returns \ref est_t_sum.
425 426
    inline double getEstTSum(int index) 
    { 
427
      return scalContents[index]->est_t_sum; 
428
    }
429

430
    /// Returns \ref est_max.
431 432
    inline double getEstMax(int index) 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
433 434 435 436 437
      FUNCNAME("AdaptInfo::getEstSum()");

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

438
      return scalContents[index]->est_max; 
439
    }
440

441
    /// Returns \ref est_max.
442 443
    inline double getTimeEstMax(int index) 
    { 
444
      return scalContents[index]->est_t_max; 
445
    }
446

447
    /// Returns \ref est_t_sum.
448 449
    inline double getTimeEstSum(int index) 
    { 
450
      return scalContents[index]->est_t_sum; 
451
    }
452

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

459
    /// Sets \ref spaceTolerance.
460 461
    inline void setSpaceTolerance(int index, double tol) 
    { 
462
      scalContents[index]->spaceTolerance = tol; 
463
    }  
464

465
    /// Returns \ref timeTolerance.
466 467
    inline double getTimeTolerance(int index) 
    { 
468
      return scalContents[index]->timeTolerance; 
469
    }  
470

471
    /// Sets \ref time
472 473
    inline double setTime(double t) 
    { 
474
      time = t; 
475 476 477 478 479
      if (time > endTime) 
	time = endTime;
      if (time < startTime) 
	time = startTime;

480
      return time;
481
    }
482

483
    /// Gets \ref time
484 485
    inline double getTime() 
    { 
486
      return time; 
487
    }  
488

489
    /// Gets \ref &time
490 491
    inline double *getTimePtr() 
    { 
492
      return &time; 
493
    }  
494

495
    /// Sets \ref timestep
496 497
    inline double setTimestep(double t) 
    { 
498
      timestep = t; 
499
      if (timestep > maxTimestep)
500
	timestep = maxTimestep;
501
      if (timestep < minTimestep)
502
	timestep = minTimestep;
503
      if (time + timestep > endTime)
504 505
	timestep = endTime - time;
      
506
      return timestep;
507
    }
508 509 510 511 512 513
    /// Gets \ref timestep
    inline double getTimestep() 
    { 
      return timestep; 
    }

514 515 516
    inline void setLastProcessedTimestep(double t)
    {
      lastProcessedTimestep = t;
517 518
    } 

519 520
    inline double getLastProcessedTimestep()
    {
521 522
	return lastProcessedTimestep;
    } 
523

524 525
    /// Returns true, if the end time is reached and no more timestep
    /// computations must be done.
526 527
    inline bool reachedEndTime() 
    {
528 529 530
      if (nTimesteps > 0) 
	return !(timestepNumber < nTimesteps);

531
      return !(fabs(time - endTime) > DBL_TOL);
Thomas Witkowski's avatar
* Bla  
Thomas Witkowski committed
532 533
    }

534

535
    /// Sets \ref minTimestep
536 537
    inline void setMinTimestep(double t) 
    { 
538
      minTimestep = t; 
539
    }
540

541
    /// Gets \ref minTimestep
542 543
    inline double getMinTimestep() 
    { 
544
      return minTimestep; 
545
    }  
546

547
    /// Sets \ref maxTimestep
548 549
    inline void setMaxTimestep(double t) 
    { 
550
      maxTimestep = t; 
551
    }
552

553
    /// Gets \ref maxTimestep
554 555
    inline double getMaxTimestep() 
    { 
556
      return maxTimestep; 
557
    }  
558
    
559
    /// Gets \ref &timestep
560 561
    inline double *getTimestepPtr() 
    { 
562
      return &timestep; 
563
    }  
564

565
    /// Sets \ref startTime = time
566 567
    inline void setStartTime(double time) 
    { 
568
      startTime = time; 
569
    }
570

571
    /// Sets \ref endTime = time
572 573
    inline void setEndTime(double time) 
    { 
574
      endTime = time; 
575
    }
576

577
    /// Returns \ref startTime
578 579
    inline double getStartTime() 
    { 
580
      return startTime; 
581
    }
582

583
    /// Returns \ref endTime
584 585
    inline double getEndTime() 
    { 
586
      return endTime; 
587
    }
588

589
    /// Returns \ref timeErrLow.
590 591
    inline double getTimeErrLow(int index) 
    { 
592
      return scalContents[index]->timeErrLow; 
593
    }  
594

595
    /// Returns whether coarsening is allowed or not.
596 597
    inline bool isCoarseningAllowed(int index) 
    {
598
      return (scalContents[index]->coarsenAllowed == 1);
599
    }
600

601
    /// Returns whether coarsening is allowed or not.
602 603
    inline bool isRefinementAllowed(int index) 
    {
604
      return (scalContents[index]->refinementAllowed == 1);
605
    }
606

607
    ///
608 609
    inline void allowRefinement(bool allow, int index) 
    {
610
      scalContents[index]->refinementAllowed = allow;
611
    }
612

613
    ///
614 615
    inline void allowCoarsening(bool allow, int index) 
    {
616
      scalContents[index]->coarsenAllowed = allow;
617
    }
618

619
    /// Returns \ref refineBisections
620 621
    inline const int getRefineBisections(int index) const 
    {
622
      return scalContents[index]->refineBisections;
623
    }
624

625
    /// Returns \ref coarseBisections
626 627
    inline const int getCoarseBisections(int index) const 
    {
628
      return scalContents[index]->coarseBisections;
629
    }    
630

631 632
    inline int getSize() 
    { 
Thomas Witkowski's avatar
Thomas Witkowski committed
633
      return scalContents.size(); 
634
    }
635

636 637 638 639 640
    inline bool getRosenbrockMode()
    {
      return rosenbrockMode;
    }

641 642
    inline void setSolverIterations(int it) 
    {
643
      solverIterations = it;
644
    }
645
  
646 647
    inline int getSolverIterations() 
    {
648
      return solverIterations;
649
    }
650
  
651 652
    inline void setMaxSolverIterations(int it) 
    {
653
      maxSolverIterations = it;
654
    }
655
  
656 657
    inline int getMaxSolverIterations() 
    {
658
      return maxSolverIterations;
659
    }
660
  
661 662
    inline void setSolverTolerance(double tol) 
    {
663
      solverTolerance = tol;
664
    }
665
  
666 667
    inline double getSolverTolerance() 
    {
668
      return solverTolerance;
669
    }
670
  
671 672
    inline void setSolverResidual(double res) 
    {
673
      solverResidual = res;
674
    }
675
  
676 677
    inline double getSolverResidual() 
    {
678
      return solverResidual;
679
    }
680

681
    /// Returns true, if the adaptive procedure was deserialized from a file.
682 683
    const bool isDeserialized() const 
    {
684
      return deserialized;
685 686
    }

687 688
    inline void setIsDeserialized(bool b