AdaptInstationary.cc 14.2 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
167
168
169
170
    if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
	std::cout << "=== explicitTimeStrategy() ===\n";
	adaptInfo->printTimeErrorLowInfo();
    }

171
172
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
173

174
175
176
177
178
  void AdaptInstationary::implicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::implicitTimeStrategy()");

    do {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
179
      adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
180
      problemTime->setTime(adaptInfo);
181

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

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
187
      adaptInfo->incTimestepIteration();
188

Thomas Witkowski's avatar
Thomas Witkowski committed
189
      if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
190
	  !adaptInfo->timeToleranceReached() &&
191
	  adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
192
	  !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
193
194
195
196
  
	adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);
	continue;
197
      }
198

199

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
200
      adaptInfo->setSpaceIteration(0);
201
202


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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
205
      if (adaptInfo->getMaxSpaceIteration() > 0) {
206
    
207
	// === Space iterations. ===
208
	do {
Thomas Witkowski's avatar
Thomas Witkowski committed
209
	  problemIteration->beginIteration(adaptInfo);
210
211
212
213
214
215
216
217
	      if (dbgMode) {
		std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
		std::cout << "=== in implicitTimeStrategy() ===\n";
		std::cout << "=== space/time iteration "<< adaptInfo->getSpaceIteration()
			  <<" : "<< adaptInfo->getTimestepIteration() <<" ===\n";
		adaptInfo->printTimeErrorLowInfo();
	      }

Thomas Witkowski's avatar
Thomas Witkowski committed
218
219
	  if (problemIteration->oneIteration(adaptInfo, FULL_ITERATION)) {
	    if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
220
		!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
		!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
	      adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
	      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
	      problemIteration->endIteration(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
225
226
227
	      adaptInfo->incSpaceIteration();
	      break;
	    }	
228
229
	  }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
230
	  adaptInfo->incSpaceIteration();
Thomas Witkowski's avatar
Thomas Witkowski committed
231
	  problemIteration->endIteration(adaptInfo);
232
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
233
234
	} while (!adaptInfo->spaceToleranceReached() && 
		 adaptInfo->getSpaceIteration() <= adaptInfo->getMaxSpaceIteration());
235
236

      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
237
	problemIteration->endIteration(adaptInfo);
238
239
      }

240

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
241
    } while(!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
242
	    !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()) && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
243
	    adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration());  
244

245
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
246

247

248
249
250
    // 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
251
252
    if (!fixedTimestep && adaptInfo->timeErrorLow()) {
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
253
254
255
256
257
258
259
      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
260
	if (fixedTimestep)
261
	  std::cout << "   fixedTimestep = true\n";	
Thomas Witkowski's avatar
Thomas Witkowski committed
262
	if (!adaptInfo->timeErrorLow())
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
263
	  adaptInfo->printTimeErrorLowInfo();
264
      }
265
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
266

267
    // Second, check for decrease of timestep
Thomas Witkowski's avatar
Thomas Witkowski committed
268
    if (!fixedTimestep &&
269
	!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
270
271
	!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()))
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);    
272
273
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
274
275
276

  void AdaptInstationary::simpleAdaptiveTimeStrategy()
  {
277
    FUNCNAME("AdaptInstationary::simpleAdaptiveTimeStrategy()");
Thomas Witkowski's avatar
Thomas Witkowski committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291

    // 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());        
292
293
294
295
296
297

    if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
	std::cout << "=== in simpleAdaptiveTimeStrategy() ===\n";
	adaptInfo->printTimeErrorLowInfo();
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
298
299
300
301
302
303
304
305
306
307
308
309
310
    
    // 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);    
  }


311
312
  void AdaptInstationary::oneTimestep()
  {
313
    FUNCNAME("AdaptInstationary::oneTimestep()");
314

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

317
318
319
320
321
322
323
    switch (strategy) {
    case 0:
      explicitTimeStrategy();
      break;
    case 1:
      implicitTimeStrategy();
      break;
Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
326
    case 2:
      simpleAdaptiveTimeStrategy();
      break;
327
    default:
Thomas Witkowski's avatar
Thomas Witkowski committed
328
      ERROR_EXIT("Unknown strategy = %d!\n", strategy);
329
    }
330

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
331
    adaptInfo->incTimestepNumber();
332
333
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
334

335
336
337
338
339
  int AdaptInstationary::adapt()
  {
    FUNCNAME("AdaptInstationary::adapt()");
    int errorCode = 0;

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
340
    TEST_EXIT(adaptInfo->getTimestep() >= adaptInfo->getMinTimestep())
341
      ("timestep < min timestep\n");
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
342
    TEST_EXIT(adaptInfo->getTimestep() <= adaptInfo->getMaxTimestep())
343
344
      ("timestep > max timestep\n");

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

347
#if HAVE_PARALLEL_DOMAIN_AMDIS
348
    Parallel::MeshDistributor::globalMeshDistributor->initParallelization(); 
349
350
#endif

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
351
352
    if (adaptInfo->getTimestepNumber() == 0) {
      adaptInfo->setTime(adaptInfo->getStartTime());
Thomas Witkowski's avatar
Thomas Witkowski committed
353
354
      initialAdaptInfo->setStartTime(adaptInfo->getStartTime());
      initialAdaptInfo->setTime(adaptInfo->getStartTime());
355

Thomas Witkowski's avatar
Thomas Witkowski committed
356
      problemTime->setTime(adaptInfo);
357
358

      // initial adaption
Thomas Witkowski's avatar
Thomas Witkowski committed
359
360
      problemTime->solveInitialProblem(initialAdaptInfo);
      problemTime->transferInitialSolution(adaptInfo);
361
362
    }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
363
    while (!adaptInfo->reachedEndTime()) {
364
      iterationTimestamp = time(0);
365

Thomas Witkowski's avatar
Thomas Witkowski committed
366
      problemTime->initTimestep(adaptInfo);
367
      oneTimestep();
Thomas Witkowski's avatar
Thomas Witkowski committed
368
      problemTime->closeTimestep(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
369

370
      if (breakWhenStable && (adaptInfo->getSolverIterations() == 0)) 
371
	break;
372

373
374
375
376
377
378
379
380
381
382
383
384
      // 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
385

Thomas Witkowski's avatar
Thomas Witkowski committed
386
  void AdaptInstationary::initialize(std::string aName)
387
388
  {
    strategy = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
389
390
391
392
    timeDelta1 = 0.7071;
    timeDelta2 = 1.4142;
    queueRuntime = -1;
    queueSerializationFilename = "__serialized_problem.ser";
393

394
395
396
397
398
    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);
399
    Parameters::get(aName + "->time adaptivity debug mode", dbgMode);
400
401
402
    Parameters::get(aName + "->queue->runtime", queueRuntime);
    Parameters::get(aName + "->queue->serialization filename", 
		    queueSerializationFilename);
403
404
405
406
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    // debug output only on rank 0
    if (MPI::COMM_WORLD.Get_rank() != 0) dbgMode=false;
#endif
407
408
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
409

Thomas Witkowski's avatar
Thomas Witkowski committed
410
  void AdaptInstationary::serialize(std::ostream &out)
411
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
412
    problemIteration->serialize(out);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
413
    adaptInfo->serialize(out);
Thomas Witkowski's avatar
Thomas Witkowski committed
414
415
    if (problemTime)
      problemTime->serialize(out);    
416
417
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
418

Thomas Witkowski's avatar
Thomas Witkowski committed
419
  void AdaptInstationary::deserialize(std::istream &in)
420
421
  {
    FUNCNAME("AdaptInstationary::deserialize()");
422
423
424
425

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

Thomas Witkowski's avatar
Thomas Witkowski committed
426
    problemIteration->deserialize(in);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
427
    adaptInfo->deserialize(in);
Thomas Witkowski's avatar
Thomas Witkowski committed
428
429
    if (problemTime)
      problemTime->deserialize(in);    
430
431
432
433
434
435
  }


  bool AdaptInstationary::checkQueueRuntime()
  {
    // If there is no time limited runtime queue, there is also nothing to check.
Thomas Witkowski's avatar
Thomas Witkowski committed
436
    if (queueRuntime == -1)
437
      return false;
Thomas Witkowski's avatar
Thomas Witkowski committed
438
    
439
    // Get the current time.
440
    time_t currentTimestamp = time(0);
441
442

    // Update list with the last iteration runtimes.
Thomas Witkowski's avatar
Thomas Witkowski committed
443
    lastIterationsDuration.push(currentTimestamp - iterationTimestamp);
444
    // The list should not contain more than 5 elements. If so, delete the oldest one.
Thomas Witkowski's avatar
Thomas Witkowski committed
445
446
    if (lastIterationsDuration.size() > 5)
      lastIterationsDuration.pop();    
447
448

    // Calculate the avarage of the last iterations.
Thomas Witkowski's avatar
Thomas Witkowski committed
449
    std::queue<int> tmpQueue = lastIterationsDuration;
450
451
    int avrgLastIterations = 0;
    while (!tmpQueue.empty()) {
452
453
	avrgLastIterations += tmpQueue.front();
	tmpQueue.pop();
454
    } 
Thomas Witkowski's avatar
Thomas Witkowski committed
455
    avrgLastIterations /= lastIterationsDuration.size();
456

457
    // Check if there is enough time for a further iteration.
Thomas Witkowski's avatar
Thomas Witkowski committed
458
459
    if (initialTimestamp + queueRuntime - currentTimestamp < avrgLastIterations * 2) {
      std::ofstream out(queueSerializationFilename.c_str());
460
461
462
463
464
465
466
467
468
469
      serialize(out);
      out.close();

      return true;
    }

    return false;
  }

}