AdaptInstationary.cc 12.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// 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.


13
#include "AdaptInstationary.h"
14
#include "Initfile.h"
15
16
17
18
19
#include "Estimator.h"
#include "ProblemIterationInterface.h"
#include "ProblemTimeInterface.h"
#include "Serializer.h"

20
21
#if HAVE_PARALLEL_DOMAIN_AMDIS
#include "parallel/MeshDistributor.h"
Naumann, Andreas's avatar
Naumann, Andreas committed
22
#ifndef HAVE_PARALLEL_MTL4
23
24
#include <petsc.h>
#endif
Naumann, Andreas's avatar
Naumann, Andreas committed
25
#endif
26

27
28
namespace AMDiS {

Thomas Witkowski's avatar
Thomas Witkowski committed
29
  AdaptInstationary::AdaptInstationary(std::string name,
30
				       ProblemIterationInterface *problemStat,  
31
32
33
				       AdaptInfo *info,
				       ProblemTimeInterface *problemInstat,
				       AdaptInfo *initialInfo,
34
				       time_t initialTimestampSet)
35
    : AdaptBase(name, problemStat, info, problemInstat, initialInfo),
36
37
      breakWhenStable(0),
      dbgMode(false)
38
39
40
  {
    FUNCNAME("AdaptInstationary::AdaptInstationary()");

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

44
45
    initConstructor(problemStat, info, initialInfo, initialTimestampSet);
 }
46

Thomas Witkowski's avatar
Thomas Witkowski committed
47

48
49
50
51
52
  AdaptInstationary::AdaptInstationary(std::string name,
				       ProblemIterationInterface &problemStat,  
				       AdaptInfo &info,
				       ProblemTimeInterface &problemInstat,
				       AdaptInfo &initialInfo,
53
				       time_t initialTimestampSet)
54
55
56
57
58
59
    : AdaptBase(name, &problemStat, &info, &problemInstat, &initialInfo),
      breakWhenStable(0),
      dbgMode(false)
  {
    FUNCNAME("AdaptInstationary::AdaptInstationary()");

60
    initConstructor(&problemStat, &info, &initialInfo, initialTimestampSet);
61
62
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
63

64
65
66
  void AdaptInstationary::initConstructor(ProblemIterationInterface *problemStat,  
					  AdaptInfo *info,
					  AdaptInfo *initialInfo,
67
					  time_t initialTimestampSet)
68
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
69
    initialize(name);
70

Thomas Witkowski's avatar
Thomas Witkowski committed
71
    fixedTimestep = (info->getMinTimestep() == info->getMaxTimestep());
72
73
 
    if (initialTimestampSet == 0)
Thomas Witkowski's avatar
Thomas Witkowski committed
74
      initialTimestamp = time(NULL);
75
    else
76
77
      initialTimestamp = initialTimestampSet;

78
    // Check if the problem should be deserialized because of the -rs parameter.
Thomas Witkowski's avatar
Thomas Witkowski committed
79
    std::string serializationFilename = "";
80
    Parameters::get("argv->rs", serializationFilename);
81
82
83
84

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

87
      std::ifstream in(queueSerializationFilename.c_str() , ios::in);
88
89
90
      deserialize(in);
      in.close();

91
92
      info->setIsDeserialized(true);
      initialInfo->setIsDeserialized(true);
93
94
95
96
    } else {
      int readSerialization = 0;
      int readSerializationWithAdaptInfo = 0;

97
98
99
100
      Parameters::get((*problemStat).getName() + "->input->read serialization",
		      readSerialization);
      Parameters::get((*problemStat).getName() + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
101
102

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

105
106
	Parameters::get((*problemStat).getName() + "->input->serialization filename", 
			serializationFilename);
107
108
109
	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
110
	std::ifstream in(serializationFilename.c_str());
111
112
113
114
115
116
	deserialize(in);
	in.close();
      }
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
117

118
119
120
121
122
  void AdaptInstationary::explicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::explicitTimeStrategy()");

    // estimate before first adaption
Thomas Witkowski's avatar
Thomas Witkowski committed
123
    if (adaptInfo->getTime() <= adaptInfo->getStartTime())
Thomas Witkowski's avatar
Thomas Witkowski committed
124
      problemIteration->oneIteration(adaptInfo, ESTIMATE);
Thomas Witkowski's avatar
Thomas Witkowski committed
125
126


127
    // increment time
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
128
    adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
129

Thomas Witkowski's avatar
Thomas Witkowski committed
130
    problemTime->setTime(adaptInfo);
131

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
135
    adaptInfo->setSpaceIteration(0);
136
137
  
    // do the iteration
Thomas Witkowski's avatar
Thomas Witkowski committed
138
139
140
    problemIteration->beginIteration(adaptInfo);
    problemIteration->oneIteration(adaptInfo, FULL_ITERATION);
    problemIteration->endIteration(adaptInfo);
141
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
142
143
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
144

145
146
147
148
149
  void AdaptInstationary::implicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::implicitTimeStrategy()");

    do {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
150
      adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
151
      problemTime->setTime(adaptInfo);
152

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

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
158
      adaptInfo->incTimestepIteration();
159

Thomas Witkowski's avatar
Thomas Witkowski committed
160
      if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
161
	  !adaptInfo->timeToleranceReached() &&
162
	  adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
163
	  !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
164
165
166
167
  
	adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);
	continue;
168
      }
169

170

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
171
      adaptInfo->setSpaceIteration(0);
172
173


174
      // === Do only space iterations only if the maximum is higher than 0. === 
175

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
176
      if (adaptInfo->getMaxSpaceIteration() > 0) {
177
    
178
	// === Space iterations. ===
179
	do {
Thomas Witkowski's avatar
Thomas Witkowski committed
180
	  problemIteration->beginIteration(adaptInfo);
181
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
182
183
	  if (problemIteration->oneIteration(adaptInfo, FULL_ITERATION)) {
	    if (!fixedTimestep && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
184
		!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
185
186
		!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
	      adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
187
188
	      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
	      problemIteration->endIteration(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
189
190
191
	      adaptInfo->incSpaceIteration();
	      break;
	    }	
192
193
	  }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
194
	  adaptInfo->incSpaceIteration();
Thomas Witkowski's avatar
Thomas Witkowski committed
195
	  problemIteration->endIteration(adaptInfo);
196
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
197
198
	} while (!adaptInfo->spaceToleranceReached() && 
		 adaptInfo->getSpaceIteration() <= adaptInfo->getMaxSpaceIteration());
199
200

      } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
201
	problemIteration->endIteration(adaptInfo);
202
203
      }

204

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
205
    } while(!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
206
	    !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()) && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
207
	    adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration());  
208

209
    adaptInfo->setLastProcessedTimestep(adaptInfo->getTimestep()); 
210
211
212
213

    // 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
214
215
    if (!fixedTimestep && adaptInfo->timeErrorLow()) {
      adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta2);
216
217
218
219
220
221
222
      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
223
	if (fixedTimestep)
224
	  std::cout << "   fixedTimestep = true\n";	
Thomas Witkowski's avatar
Thomas Witkowski committed
225
	if (!adaptInfo->timeErrorLow())
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
226
	  adaptInfo->printTimeErrorLowInfo();
227
      }
228
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
229

230
    // Second, check for decrease of timestep
Thomas Witkowski's avatar
Thomas Witkowski committed
231
    if (!fixedTimestep &&
232
	!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
233
234
	!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()))
	adaptInfo->setTimestep(adaptInfo->getTimestep() * timeDelta1);    
235
236
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
239

  void AdaptInstationary::simpleAdaptiveTimeStrategy()
  {
240
    FUNCNAME("AdaptInstationary::simpleAdaptiveTimeStrategy()");
Thomas Witkowski's avatar
Thomas Witkowski committed
241
242
243
244
245
246
247
248
249
250
251
252
253
254

    // 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());        
255
256
257
258
259
260

    if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE           ===\n";
	std::cout << "=== in simpleAdaptiveTimeStrategy() ===\n";
	adaptInfo->printTimeErrorLowInfo();
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
263
264
265
266
267
268
269
270
271
272
273
    
    // 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);    
  }


274
275
  void AdaptInstationary::oneTimestep()
  {
276
    FUNCNAME("AdaptInstationary::oneTimestep()");
277

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

280
281
282
283
284
285
286
    switch (strategy) {
    case 0:
      explicitTimeStrategy();
      break;
    case 1:
      implicitTimeStrategy();
      break;
Thomas Witkowski's avatar
Thomas Witkowski committed
287
288
289
    case 2:
      simpleAdaptiveTimeStrategy();
      break;
290
    default:
Thomas Witkowski's avatar
Thomas Witkowski committed
291
      ERROR_EXIT("Unknown strategy = %d!\n", strategy);
292
    }
293

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
294
    adaptInfo->incTimestepNumber();
295
296
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
297

298
299
300
301
302
  int AdaptInstationary::adapt()
  {
    FUNCNAME("AdaptInstationary::adapt()");
    int errorCode = 0;

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
303
    TEST_EXIT(adaptInfo->getTimestep() >= adaptInfo->getMinTimestep())
304
      ("timestep < min timestep\n");
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
305
    TEST_EXIT(adaptInfo->getTimestep() <= adaptInfo->getMaxTimestep())
306
307
      ("timestep > max timestep\n");

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

310
311
312
313
#if HAVE_PARALLEL_DOMAIN_AMDIS
    MeshDistributor::globalMeshDistributor->initParallelization(); 
#endif

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
314
315
    if (adaptInfo->getTimestepNumber() == 0) {
      adaptInfo->setTime(adaptInfo->getStartTime());
Thomas Witkowski's avatar
Thomas Witkowski committed
316
317
      initialAdaptInfo->setStartTime(adaptInfo->getStartTime());
      initialAdaptInfo->setTime(adaptInfo->getStartTime());
318

Thomas Witkowski's avatar
Thomas Witkowski committed
319
      problemTime->setTime(adaptInfo);
320
321

      // initial adaption
Thomas Witkowski's avatar
Thomas Witkowski committed
322
323
      problemTime->solveInitialProblem(initialAdaptInfo);
      problemTime->transferInitialSolution(adaptInfo);
324
325
    }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
326
    while (!adaptInfo->reachedEndTime()) {
Thomas Witkowski's avatar
Thomas Witkowski committed
327
      iterationTimestamp = time(NULL);
328

Thomas Witkowski's avatar
Thomas Witkowski committed
329
      problemTime->initTimestep(adaptInfo);
330
      oneTimestep();
Thomas Witkowski's avatar
Thomas Witkowski committed
331
      problemTime->closeTimestep(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
332

333
      if (breakWhenStable && (adaptInfo->getSolverIterations() == 0)) 
334
	break;
335

336
337
338
339
340
341
342
343
344
345
346
347
      // 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
348

Thomas Witkowski's avatar
Thomas Witkowski committed
349
  void AdaptInstationary::initialize(std::string aName)
350
351
352
353
  {
    FUNCNAME("AdaptInstationary::initialize()");

    strategy = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
354
355
356
357
    timeDelta1 = 0.7071;
    timeDelta2 = 1.4142;
    queueRuntime = -1;
    queueSerializationFilename = "__serialized_problem.ser";
358

359
360
361
362
363
    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);
364
    Parameters::get(aName + "->time adaptivity debug mode", dbgMode);
365
366
367
    Parameters::get(aName + "->queue->runtime", queueRuntime);
    Parameters::get(aName + "->queue->serialization filename", 
		    queueSerializationFilename);
368
369
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
370

Thomas Witkowski's avatar
Thomas Witkowski committed
371
  void AdaptInstationary::serialize(std::ostream &out)
372
373
374
  {
    FUNCNAME("AdaptInstationary::serialize()");

375
    SerUtil::serialize(out, amdisRevisionNumber);
Thomas Witkowski's avatar
Thomas Witkowski committed
376
    problemIteration->serialize(out);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
377
    adaptInfo->serialize(out);
Thomas Witkowski's avatar
Thomas Witkowski committed
378
379
    if (problemTime)
      problemTime->serialize(out);    
380
381
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
382

Thomas Witkowski's avatar
Thomas Witkowski committed
383
  void AdaptInstationary::deserialize(std::istream &in)
384
385
  {
    FUNCNAME("AdaptInstationary::deserialize()");
386
387
    if(in.fail())
      ERROR_EXIT("File not found for deserialization  \n");
Thomas Witkowski's avatar
Thomas Witkowski committed
388
    problemIteration->deserialize(in);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
389
    adaptInfo->deserialize(in);
Thomas Witkowski's avatar
Thomas Witkowski committed
390
391
    if (problemTime)
      problemTime->deserialize(in);    
392
393
394
395
396
397
  }


  bool AdaptInstationary::checkQueueRuntime()
  {
    // If there is no time limited runtime queue, there is also nothing to check.
Thomas Witkowski's avatar
Thomas Witkowski committed
398
    if (queueRuntime == -1) {
399
400
401
402
403
404
      return false;
    }
    // Get the current time.
    time_t currentTimestamp = time(NULL);

    // Update list with the last iteration runtimes.
Thomas Witkowski's avatar
Thomas Witkowski committed
405
    lastIterationsDuration.push(currentTimestamp - iterationTimestamp);
406
    // The list should not contain more than 5 elements. If so, delete the oldest one.
Thomas Witkowski's avatar
Thomas Witkowski committed
407
408
    if (lastIterationsDuration.size() > 5)
      lastIterationsDuration.pop();    
409
410

    // Calculate the avarage of the last iterations.
Thomas Witkowski's avatar
Thomas Witkowski committed
411
    std::queue<int> tmpQueue = lastIterationsDuration;
412
413
    int avrgLastIterations = 0;
    while (!tmpQueue.empty()) {
414
415
	avrgLastIterations += tmpQueue.front();
	tmpQueue.pop();
416
    } 
Thomas Witkowski's avatar
Thomas Witkowski committed
417
    avrgLastIterations /= lastIterationsDuration.size();
418

419
    // Check if there is enough time for a further iteration.
Thomas Witkowski's avatar
Thomas Witkowski committed
420
421
    if (initialTimestamp + queueRuntime - currentTimestamp < avrgLastIterations * 2) {
      std::ofstream out(queueSerializationFilename.c_str());
422
423
424
425
426
427
428
429
430
431
      serialize(out);
      out.close();

      return true;
    }

    return false;
  }

}