ExtendedRosenbrockStationary.cc 11.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//
// 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.


#include "time/ExtendedRosenbrockStationary.h"
#include "io/VtkWriter.h"
#include "SystemVector.h"
#include "OEMSolver.h"
#include "Debug.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
18
#include "POperators_ZOT.h"
Praetorius, Simon's avatar
Praetorius, Simon committed
19
#include "VectorOperations.h"
20
21
22
23
24
25
26

#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
#include "parallel/MeshDistributor.h"
#endif

using namespace AMDiS;

Praetorius, Simon's avatar
Praetorius, Simon committed
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
  void ExtendedRosenbrockStationary::initialize(Flag initFlag,
						ProblemStat *adoptProblem,
						Flag adoptFlag)
  { FUNCNAME_DBG("ExtendedRosenbrockStationary::initialize()");
    
    super::initialize(initFlag, adoptProblem, adoptFlag);
    
    // create rosenbrock method
    std::string str("");
    std::string initFileStr(name + "->rosenbrock->method");
    Parameters::get(initFileStr, str);
    RosenbrockMethodCreator *creator = 
      dynamic_cast<RosenbrockMethodCreator*>(CreatorMap<RosenbrockMethod>::getCreator(str, initFileStr));
    rm = creator->create();
    TEST_EXIT_DBG(rm)("No Rosenbrock method created!\n");
    
    init();
    
    // read error weights
    Parameters::get(name + "->rosenbrock->error weights", errorEstWeights);
    if (errorEstWeights.size() == 0) {
      WARNING("No weights for time-error estimation given.\n");
    }
50

Praetorius, Simon's avatar
Praetorius, Simon committed
51
52
53
54
55
56
57
58
59
60
61
62
63
    errorEstWeights.resize(super::getNumComponents(), 0.0);
    double SumErrorEstWeights = vector_operations::sum(errorEstWeights);
    if (SumErrorEstWeights<DBL_TOL) {
      WARNING("Using equal weights for all components!\n");
      for (size_t i = 0; i < errorEstWeights.size(); i++)
	errorEstWeights[i] = 1.0/static_cast<double>(super::getNumComponents());
    } else {
      for (size_t i = 0; i < errorEstWeights.size(); i++)
	errorEstWeights[i] *= 1.0/SumErrorEstWeights;
    }
    
    std::cout << "errorEstWeights = " << errorEstWeights << "\n";
  }
64
65
66
67
68
69
70
71

  void ExtendedRosenbrockStationary::init()
  {
    stageSolution = new SystemVector(*solution);
    unVec = new SystemVector(*solution);
    timeRhsVec = new SystemVector(*solution);
    newUn = new SystemVector(*solution);
    tmp = new SystemVector(*solution);
72
73
    if (!fixedTimestep)
      lowSol = new SystemVector(*solution);    
74
75
76
77
78
79
80
81
82

    stageSolution->set(0.0);
    unVec->set(0.0);
    
    stageSolutions.resize(rm->getStages());
    for (int i = 0; i < rm->getStages(); i++) {
      stageSolutions[i] = new SystemVector(*solution);
      stageSolutions[i]->set(0.0);
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
83
    order = rm->getOrder();
84
85
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
  Flag ExtendedRosenbrockStationary::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    order = rm->getOrder();
    Flag flag = 0, markFlag = 0;
    

    if (toDo.isSet(MARK))
      markFlag = problem->markElements(adaptInfo);
    else
      markFlag = 3;

    // refine
    if (toDo.isSet(ADAPT) && markFlag.isSet(MESH_REFINED))
      flag = problem->refineMesh(adaptInfo);

    // coarsen
    if (toDo.isSet(ADAPT) && markFlag.isSet(MESH_COARSENED))
      flag |= problem->coarsenMesh(adaptInfo);
    
    if (toDo.isSet(BUILD) || toDo.isSet(SOLVE)) {
      flag = stageIteration(adaptInfo, toDo, true, true);
      estimateTimeError(adaptInfo);
    }
    
    if (toDo.isSet(ESTIMATE))
      problem->estimate(adaptInfo);
112

Praetorius, Simon's avatar
Praetorius, Simon committed
113
114
115
116
    return flag;
  }
  
  Flag ExtendedRosenbrockStationary::stageIteration(AdaptInfo *adaptInfo, Flag flag,
117
					       bool asmMatrix, bool asmVector)
Praetorius, Simon's avatar
Praetorius, Simon committed
118
119
  {
    FUNCNAME("ExtendedRosenbrockStationary::stageIteration()");
120
121
122
123
124
125
126
127
128

    TEST_EXIT(tauPtr)("No tau pointer defined in stationary problem!\n");

    if (first) {
      first = false;
      *unVec = *solution;
    }
    
    *newUn = *unVec;    
129
130
    if (!fixedTimestep)
      *lowSol = *unVec;
Praetorius, Simon's avatar
Praetorius, Simon committed
131
132
    
    invTauGamma = 1.0 / ((*tauPtr) * rm->getGamma());
133

Praetorius, Simon's avatar
Praetorius, Simon committed
134
135
    for (int i = 0; i < rm->getStages(); i++) {
      
Praetorius, Simon's avatar
Praetorius, Simon committed
136
      stageTime = oldTime + rm->getAlphaI(i) * (*tauPtr);
Praetorius, Simon's avatar
Praetorius, Simon committed
137
138
      tauGammaI = rm->getGammaI(i) * (*tauPtr);
      
139
      // stage-solution: u_s(i) = u_old + sum_{j=0}^{i-1} a_ij*U_j
140
141
142
143
144
145
146
      *stageSolution = *unVec;
      for (int j = 0; j < i; j++) {
	*tmp = *(stageSolutions[j]);
	*tmp *= rm->getA(i, j);
	*stageSolution += *tmp;
      }

147
148
149
      // Dirichlet-BC implemented as additional algebraic equation u = g(x,t) on boundary
      // => U_i = -u_s(i) + g(x,t_s(i)) + tau*gamma_i* d_t(g)(t_old) on boundary
      // where u_s(i) = ith stage-solution, t_s(i) = ith stage-time
150
151
      for (unsigned int j = 0; j < boundaries.size(); j++) {
	boundaries[j].vec->interpol(boundaries[j].fct);
152
153
	*(boundaries[j].vec) -= *(stageSolution->getDOFVector(boundaries[j].col));
	
Praetorius, Simon's avatar
Praetorius, Simon committed
154
	if (boundaries[j].fctDt != NULL) {
155
156
	// time derivative of dirichlet bc is given
	  DOFVector<double>* tmpDt = new DOFVector<double>(getFeSpace(boundaries[j].col), "tmp");
Praetorius, Simon's avatar
Praetorius, Simon committed
157
158
	  tmpDt->interpol(boundaries[j].fctDt);
	  *tmpDt *= tauGammaI;
159
	  *(boundaries[j].vec) += *tmpDt; // alternativ transformDOF(...) benutzen, für x = x + a*y 
Praetorius, Simon's avatar
Praetorius, Simon committed
160
161
	  delete tmpDt;
	}
162
163
164
165
166
167
168
169
170
      }

      timeRhsVec->set(0.0);
      for (int j = 0; j < i; j++) {
	*tmp = *(stageSolutions[j]);
	*tmp *= (rm->getC(i, j) / *tauPtr);
	*timeRhsVec += *tmp;
      }

171
172
173
      super::buildAfterCoarsen(adaptInfo, flag | UPDATE_DIRICHLET_BC, (i == 0), true);
      
      super::solve(adaptInfo, i == 0, true);
174
175
176
177
178
179
180

      *(stageSolutions[i]) = *solution;
      
      *tmp = *solution;
      *tmp *= rm->getM1(i);
      *newUn += *tmp;

181
182
183
184
185
186
187
      if (!fixedTimestep) {
	// lower order approximation, with coefficients of embedded method
	// used for local error estimator
	*tmp = *solution;
	*tmp *= rm->getM2(i);
	*lowSol += *tmp;
      }
188
189
    }
    
Praetorius, Simon's avatar
Praetorius, Simon committed
190
    stageTime = oldTime + (*tauPtr);
Praetorius, Simon's avatar
Praetorius, Simon committed
191
    
Praetorius, Simon's avatar
Praetorius, Simon committed
192
193
194
195
196
197
198
199
200
    Flag flag_;
    return flag_;
  }

  
  double ExtendedRosenbrockStationary::estimateTimeError(AdaptInfo* adaptInfo)
  {
    oldErrorEst = errorEst;
    errorEst = 0.0;
201
    if (!fixedTimestep) {
Praetorius, Simon's avatar
Praetorius, Simon committed
202
      for (int i = 0; i < lowSol->getSize(); i++) {
203
204
	(*(lowSol->getDOFVector(i))) -= (*(newUn->getDOFVector(i)));
	adaptInfo->setTimeEstSum(lowSol->getDOFVector(i)->l2norm(), i+componentShift);
Praetorius, Simon's avatar
Praetorius, Simon committed
205
	errorEst += errorEstWeights[i] * adaptInfo->getTimeEstSum(i+componentShift);
206
207
      }
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
208
209
210
    
    adaptInfo->setTimeEst(errorEst);
    return errorEst;
211
212
  }

Praetorius, Simon's avatar
Praetorius, Simon committed
213
214
215
216
217
218
  void ExtendedRosenbrockStationary::acceptTimestep(AdaptInfo* adaptInfo)
  {
    *solution = *newUn;
    *unVec = *newUn;
    oldTime = adaptInfo->getTime();
  }
219

Praetorius, Simon's avatar
Praetorius, Simon committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
      
  double ExtendedRosenbrockStationary::getNewTimestep(double tol, double tau, bool restrict)
  {
    double fac = pow((tol / errorEst), 1.0 / order);
    if (restrict)
      fac = std::min(3.0, fac);
    return 0.95 * (fac * tau);
  }
  
  double ExtendedRosenbrockStationary::getNewTimestep(double tol, double oldTau, double tau, bool restrict)
  {
    double fac = tau / oldTau * pow((tol * oldErrorEst / sqr(errorEst)), 1.0 / order);
    if (restrict)
      fac = std::min(3.0, fac);
    return 0.95 * (fac * tau);
  }
  
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
  void ExtendedRosenbrockStationary::addOperator(Operator &op, int row, int col,
					 double *factor, double *estFactor)
  {
    FUNCNAME("ExtendedRosenbrockStationary::addOperator()");

    TEST_EXIT(op.getUhOld() == NULL)("UhOld is not allowed to be set!\n");

    op.setUhOld(stageSolution->getDOFVector(col));
    super::addVectorOperator(op, row, factor, estFactor);
  }
  

  void ExtendedRosenbrockStationary::addJacobianOperator(Operator &op, int row, int col,
						 double *factor, double *estFactor)
  {
    FUNCNAME("ExtendedRosenbrockStationary::addJacobianOperator()");
    
    TEST_EXIT(factor == NULL)("Not yet implemented!\n");
    TEST_EXIT(estFactor == NULL)("Not yet implemented!\n");

    super::addMatrixOperator(op, row, col, &minusOne, &minusOne);
  }


Praetorius, Simon's avatar
Praetorius, Simon committed
261
  void ExtendedRosenbrockStationary::addTimeOperator(int row, int col, double factor)
262
263
264
265
  {
    FUNCNAME("ExtendedRosenbrockStationary::addTimeOperator()");

    Operator *op = new Operator(componentSpaces[row], componentSpaces[col]);
266
    op->addTerm(new Simple_ZOT(factor));
Praetorius, Simon's avatar
Praetorius, Simon committed
267
    super::addMatrixOperator(op, row, col, &invTauGamma, &invTauGamma);
268
269

    Operator *opRhs = new Operator(componentSpaces[row]);
270
    opRhs->addTerm(new Phase_ZOT(timeRhsVec->getDOFVector(col), factor));
Praetorius, Simon's avatar
Praetorius, Simon committed
271
    super::addVectorOperator(opRhs, row);
272
273
274
275
  }

  
  void ExtendedRosenbrockStationary::addDirichletBC(BoundaryType type, int row, int col,
Praetorius, Simon's avatar
Praetorius, Simon committed
276
277
					    AbstractFunction<double, WorldVector<double> > *fct,
					    AbstractFunction<double, WorldVector<double> > *fctDt)
278
279
280
  {
    FUNCNAME("ExtendedRosenbrockStationary::addDirichletBC()");

281
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
282
    MyRosenbrockBoundary bound(fct, fctDt, vec, row, col);
283
284
285
286
    boundaries.push_back(bound);

    super::addDirichletBC(type, row, col, vec);
  }
Praetorius, Simon's avatar
Praetorius, Simon committed
287
  
288
289

  void ExtendedRosenbrockStationary::addSingularDirichletBC(WorldVector<double> &pos, int row, int col,
Praetorius, Simon's avatar
Praetorius, Simon committed
290
							    AbstractFunction<double, WorldVector<double> > *fct)
291
  {
292
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
293
    MyRosenbrockBoundary bound(fct, NULL, vec, row, col);
294
295
296
297
298
299
300
    boundaries.push_back(bound);

    super::addSingularDirichletBC(pos, row, col, *vec);
  }


  void ExtendedRosenbrockStationary::addSingularDirichletBC(DegreeOfFreedom idx, int row, int col,
Praetorius, Simon's avatar
Praetorius, Simon committed
301
							    AbstractFunction<double, WorldVector<double> > *fct)
302
  {
303
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
304
    MyRosenbrockBoundary bound(fct, NULL, vec, row, col);
305
306
307
308
309
310
311
312
313
    boundaries.push_back(bound);

    super::addSingularDirichletBC(idx, row, col, *vec);
  }
  

  void ExtendedRosenbrockStationary::addImplicitDirichletBC(AbstractFunction<double, WorldVector<double> > &signedDist, int row, int col,
							    AbstractFunction<double, WorldVector<double> > &fct)
  {
314
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
315
    MyRosenbrockBoundary bound(&fct, NULL, vec, row, col);
316
317
318
319
320
321
322
323
324
    boundaries.push_back(bound);

    super::addImplicitDirichletBC(signedDist, row, col, *vec);
  }


  void ExtendedRosenbrockStationary::addImplicitDirichletBC(DOFVector<double> &signedDist, int row, int col,
							    AbstractFunction<double, WorldVector<double> > &fct)
  {
325
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
326
    MyRosenbrockBoundary bound(&fct, NULL, vec, row, col);
327
328
329
330
331
332
333
334
335
336
    boundaries.push_back(bound);

    super::addImplicitDirichletBC(signedDist, row, col, *vec);
  }


  void ExtendedRosenbrockStationary::addManualDirichletBC(BoundaryType nr, AbstractFunction<bool, WorldVector<double> >* meshIndicator,
							  int row, int col,
							  AbstractFunction<double, WorldVector<double> > &fct)
  {
337
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
338
    MyRosenbrockBoundary bound(&fct, NULL, vec, row, col);
339
340
341
342
343
344
345
346
347
348
    boundaries.push_back(bound);

    super::addManualDirichletBC(nr, meshIndicator, row, col, *vec);
  }


  void ExtendedRosenbrockStationary::addManualDirichletBC(AbstractFunction<bool, WorldVector<double> >* meshIndicator,
							  int row, int col,
							  AbstractFunction<double, WorldVector<double> > &fct)
  {
349
    DOFVector<double>* vec = new DOFVector<double>(componentSpaces[col], "vec");
Praetorius, Simon's avatar
Praetorius, Simon committed
350
    MyRosenbrockBoundary bound(&fct, NULL, vec, row, col);
351
352
353
354
    boundaries.push_back(bound);

    super::addManualDirichletBC(meshIndicator, row, col, *vec);
  }