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
namespace AMDiS {

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

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

53
54
    initConstructor(problemStat, info, initialInfo, initialTimestampSet);
 }
55

Thomas Witkowski's avatar
Thomas Witkowski committed
56

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

68
    initConstructor(&problemStat, &info, &initialInfo, initialTimestampSet);
69
70
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
71

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

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

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

    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
94
      MSG("Deserialization from file: %s\n", queueSerializationFilename.c_str());
95

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

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

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

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

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

120
121
	Parameters::get((*problemStat).getName() + "->input->serialization filename", 
			serializationFilename);
122
123
124
	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
125
	std::ifstream in(serializationFilename.c_str());
126
127
128
129
130
131

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

132
133
134
135
136
137
	deserialize(in);
	in.close();
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
138

139
140
141
142
143
  void AdaptInstationary::explicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::explicitTimeStrategy()");

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


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

Thomas Witkowski's avatar
Thomas Witkowski committed
151
    problemTime->setTime(adaptInfo);
152

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
165

166
167
168
169
170
  void AdaptInstationary::implicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::implicitTimeStrategy()");

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

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

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
179
      adaptInfo->incTimestepIteration();
180

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

191

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


195
      // === Do only space iterations only if the maximum is higher than 0. === 
196

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

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

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

225

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

230
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
231
232
233
234

    // 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
235
236
    if (!fixedTimestep && adaptInfo->timeErrorLow()) {
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
237
238
239
240
241
242
243
      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
244
	if (fixedTimestep)
245
	  std::cout << "   fixedTimestep = true\n";	
Thomas Witkowski's avatar
Thomas Witkowski committed
246
	if (!adaptInfo->timeErrorLow())
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
247
	  adaptInfo->printTimeErrorLowInfo();
248
      }
249
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
250

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

Thomas Witkowski's avatar
Thomas Witkowski committed
258
259
260

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

    // 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());        
276
277
278
279
280
281

    if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
	std::cout << "=== in simpleAdaptiveTimeStrategy() ===\n";
	adaptInfo->printTimeErrorLowInfo();
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
284
285
286
287
288
289
290
291
292
293
294
    
    // 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);    
  }


295
296
  void AdaptInstationary::oneTimestep()
  {
297
    FUNCNAME("AdaptInstationary::oneTimestep()");
298

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
318

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
340
      problemTime->setTime(adaptInfo);
341
342

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
347
    while (!adaptInfo->reachedEndTime()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
348
      iterationTimestamp = time(NULL);
349

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

354
      if (breakWhenStable && (adaptInfo->getSolverIterations() == 0)) 
355
	break;
356

357
358
359
360
361
362
363
364
365
366
367
368
      // 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
369

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

378
379
380
381
382
    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);
383
    Parameters::get(aName + "->time adaptivity debug mode", dbgMode);
384
385
386
    Parameters::get(aName + "->queue->runtime", queueRuntime);
    Parameters::get(aName + "->queue->serialization filename", 
		    queueSerializationFilename);
387
388
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
389

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

Thomas Witkowski's avatar
Thomas Witkowski committed
398

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

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

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


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

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

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

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

      return true;
    }

    return false;
  }

}