AdaptInstationary.cc 9.73 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#include "AdaptInstationary.h"
#include "Parameters.h"
#include "Estimator.h"
#include "TecPlotWriter.h"
#include "ProblemIterationInterface.h"
#include "ProblemTimeInterface.h"
#include "Serializer.h"

namespace AMDiS {

11
  AdaptInstationary::AdaptInstationary(const char *name,
12
				       ProblemIterationInterface *problemStat,  
13
14
15
16
				       AdaptInfo *info,
				       ProblemTimeInterface *problemInstat,
				       AdaptInfo *initialInfo,
				       time_t initialTimestamp)
17
    : AdaptBase(name, problemStat, info, problemInstat, initialInfo),
18
19
      breakWhenStable(0),
      dbgMode(false)
20
21
22
23
24
25
26
27
28
29
30
31
32
33
  {
    FUNCNAME("AdaptInstationary::AdaptInstationary()");

    initialize(name_);

    fixedTimestep_ = (info->getMinTimestep() == info->getMaxTimestep());

    if (initialTimestamp == 0) {
      initialTimestamp_ = time(NULL);
    } else {
      initialTimestamp_ = initialTimestamp;
    }

    // Check if the problem should be deserialized because of the -rs parameter.
Thomas Witkowski's avatar
Thomas Witkowski committed
34
    std::string serializationFilename = "";
35
36
37
38
39
40
41
    GET_PARAMETER(0, "argv->rs", &serializationFilename);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
42
      std::ifstream in(queueSerializationFilename_.c_str());
43
44
45
      deserialize(in);
      in.close();

46
47
      info->setIsDeserialized(true);
      initialInfo->setIsDeserialized(true);
48
49
50
51
52
53
54
55
56
57
    } else {
      int readSerialization = 0;
      int readSerializationWithAdaptInfo = 0;

      GET_PARAMETER(0, (*problemStat).getName() + "->input->read serialization", "%d", 
		    &readSerialization);
      GET_PARAMETER(0, (*problemStat).getName() + "->input->serialization with adaptinfo", "%d",
		    &readSerializationWithAdaptInfo);

      if (readSerialization && readSerializationWithAdaptInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	std::string serializationFilename = "";
59
60
61
62
63
64

	GET_PARAMETER(0, (*problemStat).getName() + "->input->serialization filename", 
		      &serializationFilename);
	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
65
	std::ifstream in(serializationFilename.c_str());
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
	deserialize(in);
	in.close();
      }
    }
  }

  AdaptInstationary::~AdaptInstationary()
  {
  }

  void AdaptInstationary::explicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::explicitTimeStrategy()");

    // estimate before first adaption
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
81
82
    if (adaptInfo->getTime() <= adaptInfo->getStartTime()) {
      problemIteration_->oneIteration(adaptInfo, ESTIMATE);
83
84
85
    }

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
88
    problemTime_->setTime(adaptInfo);
89
90

    INFO(info_,6)("time = %e, timestep = %e\n",
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
91
		  adaptInfo->getTime(), adaptInfo->getTimestep());
92

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
93
    adaptInfo->setSpaceIteration(0);
94
95
  
    // do the iteration
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
96
97
98
    problemIteration_->beginIteration(adaptInfo);
    problemIteration_->oneIteration(adaptInfo, FULL_ITERATION);
    problemIteration_->endIteration(adaptInfo);
99
100
101
102
103
104
105
  }

  void AdaptInstationary::implicitTimeStrategy()
  {
    FUNCNAME("AdaptInstationary::implicitTimeStrategy()");

    do {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
106
107
      adaptInfo->setTime(adaptInfo->getTime() + adaptInfo->getTimestep());
      problemTime_->setTime(adaptInfo);
108
109

      INFO(info_,6)("time = %e, try timestep = %e\n",
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
110
		    adaptInfo->getTime(), adaptInfo->getTimestep());
111
    
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
112
      problemIteration_->oneIteration(adaptInfo, NO_ADAPTION);
113

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
114
      adaptInfo->incTimestepIteration();
115

116
      if (!fixedTimestep_ && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
117
	  !adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
118
	  !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) {
119

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
120
121
	adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
	adaptInfo->setTimestep(adaptInfo->getTimestep() * time_delta_1);
122
123
	continue;
      }
124

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
125
      adaptInfo->setSpaceIteration(0);
126
127
128
129


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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
130
      if (adaptInfo->getMaxSpaceIteration() > 0) {
131
    
132
133
	/* === Space iterations === */
	do {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
134
	  problemIteration_->beginIteration(adaptInfo);
135
	  
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
136
	  if (problemIteration_->oneIteration(adaptInfo, FULL_ITERATION)) {
137
	    if (!fixedTimestep_ && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
138
		!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
139
		!(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep())) 
140
	      {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
141
142
143
144
		adaptInfo->setTime(adaptInfo->getTime() - adaptInfo->getTimestep());
		adaptInfo->setTimestep(adaptInfo->getTimestep() * time_delta_1);
		problemIteration_->endIteration(adaptInfo);
		adaptInfo->incSpaceIteration();
145
146
147
148
		break;
	      }	
	  }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
149
150
	  adaptInfo->incSpaceIteration();
	  problemIteration_->endIteration(adaptInfo);
151
	  
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
152
153
	} while(!adaptInfo->spaceToleranceReached() && 
		adaptInfo->getSpaceIteration() <= adaptInfo->getMaxSpaceIteration());
154
155

      } else {
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
156
	problemIteration_->endIteration(adaptInfo);
157
158
      }

159

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
160
    } while(!adaptInfo->timeToleranceReached() &&
Thomas Witkowski's avatar
Thomas Witkowski committed
161
	    !(adaptInfo->getTimestep() <= adaptInfo->getMinTimestep()) && 
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
162
	    adaptInfo->getTimestepIteration() <= adaptInfo->getMaxTimestepIteration());  
163

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
164
165
    if (!fixedTimestep_ && adaptInfo->timeErrorLow()) {
      adaptInfo->setTimestep(adaptInfo->getTimestep() *time_delta_2);
166
167
168
169
170
171
172
173
174
175
      if (dbgMode) {
	// print information about timestep increase
      }
    } else {
      if (dbgMode) {
	std::cout << "=== ADAPT INFO DEBUG MODE ===\n";
	std::cout << " Do not increase timestep: \n";
	if (fixedTimestep_) {
	  std::cout << "   fixedTimestep = true\n";	
	}
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
176
177
	if (!adaptInfo->timeErrorLow()) {
	  adaptInfo->printTimeErrorLowInfo();
178
179
	}
      }
180
181
182
183
184
    }
  }

  void AdaptInstationary::oneTimestep()
  {
185
    FUNCNAME("AdaptInstationary::oneTimestep()");
186

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

189
190
191
192
193
194
195
196
197
198
199
    switch (strategy) {
    case 0:
      explicitTimeStrategy();
      break;
    case 1:
      implicitTimeStrategy();
      break;
    default:
      MSG("unknown strategy = %d; use explicit strategy\n", strategy);
      explicitTimeStrategy();
    }
200

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
201
    adaptInfo->incTimestepNumber();
202
203
204
205
206
207
208
209
  }

  int AdaptInstationary::adapt()
  {
    FUNCNAME("AdaptInstationary::adapt()");

    int errorCode = 0;

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
210
    TEST_EXIT(adaptInfo->getTimestep() >= adaptInfo->getMinTimestep())
211
      ("timestep < min timestep\n");
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
212
    TEST_EXIT(adaptInfo->getTimestep() <= adaptInfo->getMaxTimestep())
213
214
      ("timestep > max timestep\n");

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

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
217
218
219
220
    if (adaptInfo->getTimestepNumber() == 0) {
      adaptInfo->setTime(adaptInfo->getStartTime());
      initialAdaptInfo_->setStartTime(adaptInfo->getStartTime());
      initialAdaptInfo_->setTime(adaptInfo->getStartTime());
221

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
222
      problemTime_->setTime(adaptInfo);
223
224
225

      // initial adaption
      problemTime_->solveInitialProblem(initialAdaptInfo_);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
226
      problemTime_->transferInitialSolution(adaptInfo);
227
228
    }

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
229
    while (!adaptInfo->reachedEndTime()) {
230
231
      iterationTimestamp_ = time(NULL);

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
232
      problemTime_->initTimestep(adaptInfo);
233
      oneTimestep();
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
234
      problemTime_->closeTimestep(adaptInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
235

Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
236
      if (breakWhenStable && (adaptInfo->getSolverIterations() == 0)) {
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
	break;
      }

      // 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
252
  void AdaptInstationary::initialize(const std::string& aName)
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  {
    FUNCNAME("AdaptInstationary::initialize()");

    strategy = 0;
    time_delta_1 = 0.7071;
    time_delta_2 = 1.4142;
    queueRuntime_ = -1;
    queueSerializationFilename_ = "__serialized_problem.ser";

    GET_PARAMETER(0, aName + "->strategy", "%d", &strategy);
    GET_PARAMETER(0, aName + "->time delta 1", "%f", &time_delta_1);
    GET_PARAMETER(0, aName + "->time delta 2", "%f", &time_delta_2);
    GET_PARAMETER(0, aName + "->info", "%d", &info_);
    GET_PARAMETER(0, aName + "->break when stable", "%d", &breakWhenStable);
    GET_PARAMETER(0, aName + "->queue->runtime", "%d", &queueRuntime_);
    GET_PARAMETER(0, aName + "->queue->serialization filename", &queueSerializationFilename_);

    return;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
273
  void AdaptInstationary::serialize(std::ostream &out)
274
275
276
277
  {
    FUNCNAME("AdaptInstationary::serialize()");

    problemIteration_->serialize(out);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
278
    adaptInfo->serialize(out);
279
280
281
282
283
    if (problemTime_) {
      problemTime_->serialize(out);
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
284
  void AdaptInstationary::deserialize(std::istream &in)
285
286
287
288
  {
    FUNCNAME("AdaptInstationary::deserialize()");

    problemIteration_->deserialize(in);
Thomas Witkowski's avatar
* Bla    
Thomas Witkowski committed
289
    adaptInfo->deserialize(in);
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
    if (problemTime_) {
      problemTime_->deserialize(in);
    }
  }


  bool AdaptInstationary::checkQueueRuntime()
  {
    // If there is no time limited runtime queue, there is also nothing to check.
    if (queueRuntime_ == -1) {
      return false;
    }

    // Get the current time.
    time_t currentTimestamp = time(NULL);

    // Update list with the last iteration runtimes.
307
    lastIterationsDuration_.push(currentTimestamp - iterationTimestamp_);
308
    // The list should not contain more than 5 elements. If so, delete the oldest one.
309
310
    if (lastIterationsDuration_.size() > 5) {
      lastIterationsDuration_.pop();
311
312
313
    }

    // Calculate the avarage of the last iterations.
Thomas Witkowski's avatar
Thomas Witkowski committed
314
    std::queue<int> tmpQueue = lastIterationsDuration_;
315
316
317
318
319
    int avrgLastIterations = 0;
    while (!tmpQueue.empty()) {
      avrgLastIterations += tmpQueue.front();
      tmpQueue.pop();
    } 
320
    avrgLastIterations /= lastIterationsDuration_.size();
321
322
323
    
    // Check if there is enough time for a further iteration.
    if (initialTimestamp_ + queueRuntime_ - currentTimestamp < avrgLastIterations * 2) {
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      std::ofstream out(queueSerializationFilename_.c_str());
325
326
327
328
329
330
331
332
333
334
      serialize(out);
      out.close();

      return true;
    }

    return false;
  }

}