AdaptInstationary.cc 13.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


22
#include "AdaptInstationary.h"
23
#include "Initfile.h"
24
#include "est/Estimator.h"
25
26
27
28
#include "ProblemIterationInterface.h"
#include "ProblemTimeInterface.h"
#include "Serializer.h"

29
30
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "parallel/MeshDistributor.h"
31
#ifdef HAVE_PARALLEL_PETSC
32
33
#include <petsc.h>
#endif
Naumann, Andreas's avatar
Naumann, Andreas committed
34
#endif
35

36
37
using namespace std;

38
39
namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
40
  AdaptInstationary::AdaptInstationary(std::string name,
41
				       ProblemIterationInterface *problemStat,  
42
43
44
				       AdaptInfo *info,
				       ProblemTimeInterface *problemInstat,
				       AdaptInfo *initialInfo,
45
				       time_t initialTimestampSet)
46
    : AdaptBase(name, problemStat, info, problemInstat, initialInfo),
47
48
      breakWhenStable(0),
      dbgMode(false)
49
50
51
  {
    FUNCNAME("AdaptInstationary::AdaptInstationary()");

52
53
    MSG("You make use of the obsolete constructor AdaptInstationary::AdaptInstationary(...)!\n");
    MSG("Please use the constructor that uses references instead of pointers!\n");
54

55
56
    initConstructor(problemStat, info, initialInfo, initialTimestampSet);
 }
57

Thomas Witkowski's avatar
Thomas Witkowski committed
58

59
60
61
62
63
  AdaptInstationary::AdaptInstationary(std::string name,
				       ProblemIterationInterface &problemStat,  
				       AdaptInfo &info,
				       ProblemTimeInterface &problemInstat,
				       AdaptInfo &initialInfo,
64
				       time_t initialTimestampSet)
65
66
67
68
69
    : AdaptBase(name, &problemStat, &info, &problemInstat, &initialInfo),
      breakWhenStable(0),
      dbgMode(false)
  {

70
    initConstructor(&problemStat, &info, &initialInfo, initialTimestampSet);
71
72
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
73

74
75
76
  void AdaptInstationary::initConstructor(ProblemIterationInterface *problemStat,  
					  AdaptInfo *info,
					  AdaptInfo *initialInfo,
77
					  time_t initialTimestampSet)
78
  {
79
    FUNCNAME("AdaptInstationary::initConstructor()");
Thomas Witkowski's avatar
Thomas Witkowski committed
80
    initialize(name);
81

Thomas Witkowski's avatar
Thomas Witkowski committed
82
    fixedTimestep = (info->getMinTimestep() == info->getMaxTimestep());
83
84
 
    if (initialTimestampSet == 0)
85
      initialTimestamp = time(0);
86
    else
87
88
      initialTimestamp = initialTimestampSet;

89
    // Check if the problem should be deserialized because of the -rs parameter.
Thomas Witkowski's avatar
Thomas Witkowski committed
90
    std::string serializationFilename = "";
91
    Parameters::get("argv->rs", serializationFilename);
92
93
94
95

    if (serializationFilename.compare("")) {
      // The value of the -rs argument is ignored, because we want to use the 
      // serialization file mentioned in the used init file.
Thomas Witkowski's avatar
Thomas Witkowski committed
96
      MSG("Deserialization from file: %s\n", queueSerializationFilename.c_str());
97

98
      std::ifstream in(queueSerializationFilename.c_str() , ios::in);
99
100
101
102
103
104

      // Read the revision number of the AMDiS version which was used to create 
      // the serialization file.
      int revNumber = -1;
      SerUtil::deserialize(in, revNumber);
      
105
106
107
      deserialize(in);
      in.close();

108
109
      info->setIsDeserialized(true);
      initialInfo->setIsDeserialized(true);
110
111
112
113
    } else {
      int readSerialization = 0;
      int readSerializationWithAdaptInfo = 0;

114
115
116
117
      Parameters::get((*problemStat).getName() + "->input->read serialization",
		      readSerialization);
      Parameters::get((*problemStat).getName() + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
118
119

      if (readSerialization && readSerializationWithAdaptInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
120
	std::string serializationFilename = "";
121

122
123
	Parameters::get((*problemStat).getName() + "->input->serialization filename", 
			serializationFilename);
124
125
126
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization with AdaptInfo from file: %s\n", serializationFilename.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
127
	std::ifstream in(serializationFilename.c_str());
128
129
130
131
132
133

	// Read the revision number of the AMDiS version which was used to create 
	// the serialization file.
	int revNumber = -1;
	SerUtil::deserialize(in, revNumber);

134
135
136
137
138
139
	deserialize(in);
	in.close();
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
140

141
142
143
144
145
  void AdaptInstationary::explicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::explicitTimeStrategy()");

    // estimate before first adaption
Thomas Witkowski's avatar
Thomas Witkowski committed
146
    if (adaptInfo->getTime() <= adaptInfo->getStartTime())
Thomas Witkowski's avatar
Thomas Witkowski committed
147
      problemIteration->oneIteration(adaptInfo, ESTIMATE);
Thomas Witkowski's avatar
Thomas Witkowski committed
148
149


150
    // increment time
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
151
    adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
152

Thomas Witkowski's avatar
Thomas Witkowski committed
153
    problemTime->setTime(adaptInfo);
154

155
    INFO(info, 6)("time = %e, timestep = %e\n",
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
156
		  adaptInfo->getTime(), adaptInfo->getTimestep());
157

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
158
    adaptInfo->setSpaceIteration(0);
159
160
  
    // do the iteration
Thomas Witkowski's avatar
Thomas Witkowski committed
161
162
163
    problemIteration->beginIteration(adaptInfo);
    problemIteration->oneIteration(adaptInfo, FULL_ITERATION);
    problemIteration->endIteration(adaptInfo);
164
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
165
166
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
167

168
169
170
171
172
  void AdaptInstationary::implicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::implicitTimeStrategy()");

    do {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
173
      adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
174
      problemTime->setTime(adaptInfo);
175

Thomas Witkowski's avatar
Thomas Witkowski committed
176
177
      INFO(info,6)("time = %e, try timestep = %e\n",
		   adaptInfo->getTime(), adaptInfo->getTimestep());
178

Thomas Witkowski's avatar
Thomas Witkowski committed
179
      problemIteration->oneIteration(adaptInfo, NO_ADAPTION);
180

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
181
      adaptInfo->incTimestepIteration();
182

Thomas Witkowski's avatar
Thomas Witkowski committed
183
      if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
184
	  !adaptInfo->timeToleranceReached() &&
185
	  adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
186
	  !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
187
188
189
190
  
	adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);
	continue;
191
      }
192

193

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
194
      adaptInfo->setSpaceIteration(0);
195
196


Praetorius, Simon's avatar
Praetorius, Simon committed
197
      // === Do space iterations only if the maximum is higher than 0. === 
198

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
199
      if (adaptInfo->getMaxSpaceIteration() > 0) {
200
    
201
	// === Space iterations. ===
202
	do {
Thomas Witkowski's avatar
Thomas Witkowski committed
203
	  problemIteration->beginIteration(adaptInfo);
204
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
205
206
	  if (problemIteration->oneIteration(adaptInfo, FULL_ITERATION)) {
	    if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
207
		!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
208
209
		!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
	      adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
210
211
	      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
	      problemIteration->endIteration(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
212
213
214
	      adaptInfo->incSpaceIteration();
	      break;
	    }	
215
216
	  }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
217
	  adaptInfo->incSpaceIteration();
Thomas Witkowski's avatar
Thomas Witkowski committed
218
	  problemIteration->endIteration(adaptInfo);
219
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
220
221
	} while (!adaptInfo->spaceToleranceReached() && 
		 adaptInfo->getSpaceIteration() <= adaptInfo->getMaxSpaceIteration());
222
223

      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	problemIteration->endIteration(adaptInfo);
225
226
      }

227

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
228
    } while(!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
229
	    !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()) && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
230
	    adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration());  
231

232
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
233
234
235
236

    // After successful iteration/timestep the timestep will be changed according 
    // adaption rules for next timestep. 
    // First, check for increase of timestep
Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
    if (!fixedTimestep && adaptInfo->timeErrorLow()) {
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
239
240
241
242
243
244
245
      if (dbgMode) {
	// print information about timestep increase
      }
    } else {
      if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE ===\n";
	std::cout << " Do not increase timestep: \n";
Thomas Witkowski's avatar
Thomas Witkowski committed
246
	if (fixedTimestep)
247
	  std::cout << "   fixedTimestep = true\n";	
Thomas Witkowski's avatar
Thomas Witkowski committed
248
	if (!adaptInfo->timeErrorLow())
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
249
	  adaptInfo->printTimeErrorLowInfo();
250
      }
251
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
252

253
    // Second, check for decrease of timestep
Thomas Witkowski's avatar
Thomas Witkowski committed
254
    if (!fixedTimestep &&
255
	!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
256
257
	!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()))
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);    
258
259
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
262

  void AdaptInstationary::simpleAdaptiveTimeStrategy()
  {
263
    FUNCNAME("AdaptInstationary::simpleAdaptiveTimeStrategy()");
Thomas Witkowski's avatar
Thomas Witkowski committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277

    // estimate before first adaption
    if (adaptInfo->getTime() <= adaptInfo->getStartTime())
      problemIteration->oneIteration(adaptInfo, ESTIMATE);

    adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
    problemTime->setTime(adaptInfo);
    
    INFO(info,6)("time = %e, timestep = %e\n",
		 adaptInfo->getTime(), adaptInfo->getTimestep());
    
    problemIteration->oneIteration(adaptInfo, FULL_ITERATION);

    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep());        
278
279
280
281
282
283

    if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
	std::cout << "=== in simpleAdaptiveTimeStrategy() ===\n";
	adaptInfo->printTimeErrorLowInfo();
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
284
285
286
287
288
289
290
291
292
293
294
295
296
    
    // First, check for increase of timestep
    if (!fixedTimestep && adaptInfo->timeErrorLow())
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
    
    // Second, check for decrease of timestep
    if (!fixedTimestep &&
	!adaptInfo->timeToleranceReached() &&
	!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()))
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);    
  }


297
298
  void AdaptInstationary::oneTimestep()
  {
299
    FUNCNAME("AdaptInstationary::oneTimestep()");
300

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
301
    adaptInfo->setTimestepIteration(0);
302

303
304
305
306
307
308
309
    switch (strategy) {
    case 0:
      explicitTimeStrategy();
      break;
    case 1:
      implicitTimeStrategy();
      break;
Thomas Witkowski's avatar
Thomas Witkowski committed
310
311
312
    case 2:
      simpleAdaptiveTimeStrategy();
      break;
313
    default:
Thomas Witkowski's avatar
Thomas Witkowski committed
314
      ERROR_EXIT("Unknown strategy = %d!\n", strategy);
315
    }
316

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
317
    adaptInfo->incTimestepNumber();
318
319
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
320

321
322
323
324
325
  int AdaptInstationary::adapt()
  {
    FUNCNAME("AdaptInstationary::adapt()");
    int errorCode = 0;

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
326
    TEST_EXIT(adaptInfo->getTimestep() >= adaptInfo->getMinTimestep())
327
      ("timestep < min timestep\n");
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
328
    TEST_EXIT(adaptInfo->getTimestep() <= adaptInfo->getMaxTimestep())
329
330
      ("timestep > max timestep\n");

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
331
    TEST_EXIT(adaptInfo->getTimestep() > 0)("timestep <= 0!\n");
332

333
#if HAVE_PARALLEL_DOMAIN_AMDIS
334
    Parallel::MeshDistributor::globalMeshDistributor->initParallelization(); 
335
336
#endif

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
337
338
    if (adaptInfo->getTimestepNumber() == 0) {
      adaptInfo->setTime(adaptInfo->getStartTime());
Thomas Witkowski's avatar
Thomas Witkowski committed
339
340
      initialAdaptInfo->setStartTime(adaptInfo->getStartTime());
      initialAdaptInfo->setTime(adaptInfo->getStartTime());
341

Thomas Witkowski's avatar
Thomas Witkowski committed
342
      problemTime->setTime(adaptInfo);
343
344

      // initial adaption
Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
      problemTime->solveInitialProblem(initialAdaptInfo);
      problemTime->transferInitialSolution(adaptInfo);
347
348
    }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
349
    while (!adaptInfo->reachedEndTime()) {
350
      iterationTimestamp = time(0);
351

Thomas Witkowski's avatar
Thomas Witkowski committed
352
      problemTime->initTimestep(adaptInfo);
353
      oneTimestep();
Thomas Witkowski's avatar
Thomas Witkowski committed
354
      problemTime->closeTimestep(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
355

356
      if (breakWhenStable && (adaptInfo->getSolverIterations() == 0)) 
357
	break;
358

359
360
361
362
363
364
365
366
367
368
369
370
      // Check if there is a runtime limitation. If there is a runtime limitation
      // and there is no more time for a next adaption loop, than return the error
      // code for rescheduling the problem and break the adaption loop.
      if (checkQueueRuntime()) {
	errorCode = RescheduleErrorCode;
	break;
      }
    }

    return errorCode;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
371

Thomas Witkowski's avatar
Thomas Witkowski committed
372
  void AdaptInstationary::initialize(std::string aName)
373
374
  {
    strategy = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
375
376
377
378
    timeDelta1 = 0.7071;
    timeDelta2 = 1.4142;
    queueRuntime = -1;
    queueSerializationFilename = "__serialized_problem.ser";
379

380
381
382
383
384
    Parameters::get(aName + "->strategy", strategy);
    Parameters::get(aName + "->time delta 1", timeDelta1);
    Parameters::get(aName + "->time delta 2", timeDelta2);
    Parameters::get(aName + "->info", info);
    Parameters::get(aName + "->break when stable", breakWhenStable);
385
    Parameters::get(aName + "->time adaptivity debug mode", dbgMode);
386
387
388
    Parameters::get(aName + "->queue->runtime", queueRuntime);
    Parameters::get(aName + "->queue->serialization filename", 
		    queueSerializationFilename);
389
390
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
391

Thomas Witkowski's avatar
Thomas Witkowski committed
392
  void AdaptInstationary::serialize(std::ostream &out)
393
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
394
    problemIteration->serialize(out);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
395
    adaptInfo->serialize(out);
Thomas Witkowski's avatar
Thomas Witkowski committed
396
397
    if (problemTime)
      problemTime->serialize(out);    
398
399
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
400

Thomas Witkowski's avatar
Thomas Witkowski committed
401
  void AdaptInstationary::deserialize(std::istream &in)
402
403
  {
    FUNCNAME("AdaptInstationary::deserialize()");
404
405
406
407

    if (in.fail())
      ERROR_EXIT("File not found for deserialization!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
408
    problemIteration->deserialize(in);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
409
    adaptInfo->deserialize(in);
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
    if (problemTime)
      problemTime->deserialize(in);    
412
413
414
415
416
417
  }


  bool AdaptInstationary::checkQueueRuntime()
  {
    // If there is no time limited runtime queue, there is also nothing to check.
Thomas Witkowski's avatar
Thomas Witkowski committed
418
    if (queueRuntime == -1)
419
      return false;
Thomas Witkowski's avatar
Thomas Witkowski committed
420
    
421
    // Get the current time.
422
    time_t currentTimestamp = time(0);
423
424

    // Update list with the last iteration runtimes.
Thomas Witkowski's avatar
Thomas Witkowski committed
425
    lastIterationsDuration.push(currentTimestamp - iterationTimestamp);
426
    // The list should not contain more than 5 elements. If so, delete the oldest one.
Thomas Witkowski's avatar
Thomas Witkowski committed
427
428
    if (lastIterationsDuration.size() > 5)
      lastIterationsDuration.pop();    
429
430

    // Calculate the avarage of the last iterations.
Thomas Witkowski's avatar
Thomas Witkowski committed
431
    std::queue<int> tmpQueue = lastIterationsDuration;
432
433
    int avrgLastIterations = 0;
    while (!tmpQueue.empty()) {
434
435
	avrgLastIterations += tmpQueue.front();
	tmpQueue.pop();
436
    } 
Thomas Witkowski's avatar
Thomas Witkowski committed
437
    avrgLastIterations /= lastIterationsDuration.size();
438

439
    // Check if there is enough time for a further iteration.
Thomas Witkowski's avatar
Thomas Witkowski committed
440
441
    if (initialTimestamp + queueRuntime - currentTimestamp < avrgLastIterations * 2) {
      std::ofstream out(queueSerializationFilename.c_str());
442
443
444
445
446
447
448
449
450
451
      serialize(out);
      out.close();

      return true;
    }

    return false;
  }

}