ProblemStat.cc 49.5 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


Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
23
#include <boost/lexical_cast.hpp>
24
25

#include "ProblemStat.h"
26
27
28
29
30
31
32
33
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Marker.h"
#include "AdaptInfo.h"
34
#include "io/FileWriter.h"
35
36
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
37
#include "DualTraverse.h"
38
#include "Mesh.h"
39
#include "solver/LinearSolver.h"
40
41
42
43
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
44
#include "Flag.h"
45
#include "est/Estimator.h"
46
47
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
48
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
49
#include "Debug.h"
50
51
52

namespace AMDiS {

53
  using namespace std;
54
55
  using boost::lexical_cast;

56
57
58
59
60
61
62
  ProblemStatSeq::ProblemStatSeq(string nameStr,
				 ProblemIterationInterface *problemIteration)
    : StandardProblemIteration(this),
      name(nameStr),
      nComponents(-1),
      nMeshes(0),
      traverseInfo(0),
63
64
65
66
      solver(nullptr),
      solution(nullptr),
      rhs(nullptr),
      systemMatrix(nullptr),
67
      useGetBound(true),
68
69
      refinementManager(nullptr),
      coarseningManager(nullptr),
70
71
72
73
74
75
76
      info(10),
      deserialized(false),
      computeExactError(false),
      boundaryConditionSet(false),
      writeAsmInfo(false)
  {
    Parameters::get(name + "->components", nComponents);
77
78
    TEST_EXIT(nComponents > 0)("No value set for parameter \"%s->components\"!\n",
			       name.c_str());    
79
80
    estimator.resize(nComponents, nullptr);
    marker.resize(nComponents, nullptr);
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    
    assembleMatrixOnlyOnce.resize(nComponents);
    assembledMatrix.resize(nComponents);
    for (int i = 0; i < nComponents; i++) {
      assembleMatrixOnlyOnce[i].resize(nComponents);
      assembledMatrix[i].resize(nComponents);
      for (int j = 0; j < nComponents; j++) {
	assembleMatrixOnlyOnce[i][j] = false;
	assembledMatrix[i][j] = false;
      }
    }
    
    exactSolutionFcts.resize(nComponents);


    // === Initialize name of components. ===

    componentNames.resize(nComponents, "");
    for (int i = 0; i < nComponents; i++)
      componentNames[i] = "solution[" + lexical_cast<string>(i) + "]";

    Parameters::get(name + "->name", componentNames);
103
104
    componentNames.resize(nComponents);

105
106
107
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
Thomas Witkowski's avatar
Thomas Witkowski committed
108
109

    Parameters::get("debug->write asm info", writeAsmInfo);
110
111
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
112
113
114

  ProblemStatSeq::~ProblemStatSeq()
  {
115
116
    if (rhs)
      delete rhs;
117
    rhs = nullptr;
118
119
    if (solution)
      delete solution;
120
    solution = nullptr;
Thomas Witkowski's avatar
Thomas Witkowski committed
121

122
123
124
125
126
    if (systemMatrix) {    
      for (int i = 0; i < nComponents; i++)
	for (int j = 0; j < nComponents; j++)
	  if ((*systemMatrix)[i][j]) {
	    delete (*systemMatrix)[i][j];
127
	    (*systemMatrix)[i][j] = nullptr;
128
129
130
	  }

      delete systemMatrix;
131
      systemMatrix = nullptr;
132
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
133
134

    for (unsigned int i = 0; i < meshes.size(); i++)
135
136
      if (meshes[i]) {
// 	delete meshes[i];
137
// 	meshes[i] = nullptr;
138
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
139
140
    
    for (unsigned int i = 0; i < estimator.size(); i++)
141
      if (estimator[i]) {
142
	delete estimator[i];
143
	estimator[i] = nullptr;
144
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
145
146

    for (unsigned int i = 0; i < marker.size(); i++)
147
      if (marker[i]) {
148
	delete marker[i];
149
	marker[i] = nullptr;
150
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
151
152
153
  }


154
155
156
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
157
  {
158
    FUNCNAME("ProblemStat::initialize()");
159

160
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
161
    if (meshes.size() != 0) { 
162
163
164
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
165
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
166
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
167
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
168

169
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
170
	  (adoptFlag.isSet(INIT_MESH) ||
171
172
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
173
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
174
	componentMeshes = adoptProblem->componentMeshes;
175
176
177

	// If the adopt problem has fewer components than this problem, but only one
	// mesh for all component, than scal up the componentMeshes array.
Thomas Witkowski's avatar
Thomas Witkowski committed
178
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
179
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
180
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
181
	  componentMeshes.resize(nComponents);
182
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
183
	    componentMeshes[i] = componentMeshes[0];
184
	}
185
186
187
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
188
    if (meshes.size() == 0) 
189
190
      WARNING("no mesh created\n");

191
    // === create refinement/corasening-manager ===
192
    if (refinementManager != nullptr && coarseningManager != nullptr) { 
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
      WARNING("refinement-/coarseningmanager already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
          (!adoptFlag.isSet(INIT_MESH) &&
           (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
        createRefCoarseManager();

      if (adoptProblem && 
          (adoptFlag.isSet(INIT_MESH) ||
           adoptFlag.isSet(INIT_SYSTEM) ||
           adoptFlag.isSet(INIT_FE_SPACE))) {
        refinementManager = adoptProblem->refinementManager;
        coarseningManager = adoptProblem->coarseningManager;
      }
    }

209
    if (refinementManager == nullptr || coarseningManager == nullptr) 
210
211
      WARNING("no refinement-/coarseningmanager created\n");

212
    // === create fespace ===
213
    if (feSpaces.size() != 0) {
214
215
216
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
217
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
218
	createFeSpace(nullptr);
219

220
221
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
222
	feSpaces = adoptProblem->getFeSpaces();
223
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	traverseInfo = adoptProblem->traverseInfo;
225
226
227

	// If the adopt problem has fewer components than this problem, but only one
	// fe space for all component, than scal up the componentSpaces array.
Thomas Witkowski's avatar
Thomas Witkowski committed
228
229
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
230
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
231
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
232
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
233
	    componentSpaces[i] = componentSpaces[0];
234
235
	}

236
237
238
      }
    }

239
    if (feSpaces.size() == 0) 
240
241
242
      WARNING("no feSpace created\n");

    // === create system ===
243
    if (initFlag.isSet(INIT_SYSTEM)) 
244
      createMatricesAndVectors();
245
    
246
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
247
      solution = adoptProblem->getSolution();
248
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
249
      systemMatrix = adoptProblem->getSystemMatrix();
250
251
252
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
253
    if (solver) {
254
255
      WARNING("solver already created\n");
    } else {
256
      if (initFlag.isSet(INIT_SOLVER))
257
	createSolver();
258
      
259
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
260
261
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
262
263
264
      }
    }

265
// #ifndef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
266
    if (!solver) 
267
      WARNING("no solver created\n");
268
// #endif
269
270

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
271
    if (initFlag.isSet(INIT_ESTIMATOR))
272
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
273
274

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
275
      estimator = adoptProblem->getEstimators();
276
277

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
278
    if (initFlag.isSet(INIT_MARKER))
279
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
280

Thomas Witkowski's avatar
Thomas Witkowski committed
281
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
282
      marker = adoptProblem->getMarkers();
283
284

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
285
    if (initFlag.isSet(INIT_FILEWRITER))
286
287
288
289
290
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
291
292
293
294
    // to be read from disk. Either by providing the parameter -rs when 
    // executing the program or in the init file. The -rs parameter is always
    // checked first, because it can be added automatically when rescheduling
    // the program before timeout of the runqueue.
295
296

    int readSerialization = 0;
297
298
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
299
300
301
302
303
304

    // If the parameter -rs is set, we do nothing here, because the problem will be
    // deserialized in the constructor of a following AdaptInstationary initialization.
    if (!serializationFilename.compare("")) {
      int readSerializationWithAdaptInfo = 0;

305
306
307
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
308
309
310
311
312
313

      // The serialization file is only read, if the adaptInfo part should not be used.
      // If the adaptInfo part should be also read, the serialization file will be read
      // in the constructor of the AdaptInstationary problem, because we do not have here
      // the adaptInfo object.
      if (readSerialization && !readSerializationWithAdaptInfo) {
314
315
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
316
317
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

318
319
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
320
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
321
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
322
	ifstream in(serializationFilename.c_str());
323
324
325
326
327
328

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

329
330
	deserialize(in);
	in.close();
331
#endif
332
333

	deserialized = true;
334
      } else {
335
	int globalRefinements = 0;
336

337
	// If AMDiS is compiled for parallel computations, the global refinements are
338
339
340
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
341
342
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
343
#endif
344

345
	bool initMesh = initFlag.isSet(INIT_MESH);
346

Thomas Witkowski's avatar
Thomas Witkowski committed
347
348
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
349
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
350
	    meshes[i]->initialize();	    	
351
352

	// === read value file and use it for the mesh values ===
353
354
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
355
	if (valueFilename.length())
356
357
358
359
360
361
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
362

Thomas Witkowski's avatar
Thomas Witkowski committed
363
	for (unsigned int i = 0; i < meshes.size(); i++)
364
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
365
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
366
367
368
369
370
371
      }
    }

    doOtherStuff();
  }

372

373
  void ProblemStatSeq::createMesh() 
374
  {
375
    FUNCNAME("ProblemStat::createMesh()");
376

Thomas Witkowski's avatar
Thomas Witkowski committed
377
    componentMeshes.resize(nComponents);
378
379
380
381
382
383
    map<int, Mesh*> meshForRefinementSet;

    string meshName("");
    Parameters::get(name + "->mesh", meshName);
    TEST_EXIT(meshName != "")("No mesh name specified for \"%s->mesh\"!\n", 
			      name.c_str());
384
385

    int dim = 0;
386
387
388
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
389

390
    for (int i = 0; i < nComponents; i++) {
391
      int refSet = -1;
392
393
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
394
      if (refSet < 0)
395
	refSet = 0;
396
      
397
      if (meshForRefinementSet[refSet] == nullptr) {
Thomas Witkowski's avatar
Thomas Witkowski committed
398
	Mesh *newMesh = new Mesh(meshName, dim);
399
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
400
401
	meshes.push_back(newMesh);
	nMeshes++;
402
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
403
      componentMeshes[i] = meshForRefinementSet[refSet];
404
    }
405
406
407
408
409
410
411
412
413
414
415
  }


  void ProblemStatSeq::createRefCoarseManager() 
  {
    FUNCNAME("ProblemStat::createRefCoarseManager()");

    int dim = 0;
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
                   name.c_str());
416
417

    switch (dim) {
418
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
419
      refinementManager = new RefinementManager1d();
420
      coarseningManager = new CoarseningManager1d();
421
422
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
423
      refinementManager = new RefinementManager2d();
424
      coarseningManager = new CoarseningManager2d();
425
426
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
427
      refinementManager = new RefinementManager3d();
428
      coarseningManager = new CoarseningManager3d();
429
430
431
432
433
434
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

435

436
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
437
  {
438
    FUNCNAME("ProblemStat::createFeSpace()");
439

440
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
441
    int dim = -1;
442
    Parameters::get(name + "->dim", dim);
443
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
444

445
    componentSpaces.resize(nComponents, nullptr);
Thomas Witkowski's avatar
Thomas Witkowski committed
446
    traverseInfo.resize(nComponents);
447

448
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
449
      int degree = 1;
450
451
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
452
453
      TEST_EXIT(degree > 0)
	("Poynomial degree in component %d must be larger than zero!\n", i);
454
      TEST_EXIT(componentSpaces[i] == nullptr)("feSpace already created\n");
455

456
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == nullptr) {
Thomas Witkowski's avatar
Thomas Witkowski committed
457
458
459
	stringstream s;
	s << name << "->feSpace[" << i << "]";

460
461
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
462
					  componentMeshes[i], s.str());
463
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
464
	feSpaces.push_back(newFeSpace);
465
      }
466

467
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
468
469
470
    }

    for (int i = 0; i < nComponents; i++) {
471
      for (int j = 0; j < nComponents; j++)
472
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
473
      
474
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
475
476
477
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
478
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
479
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
480
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
481
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
482
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
483
484
485
486
      }
    }
  }

487

488
  void ProblemStatSeq::createMatricesAndVectors()
489
490
491
  {
    // === create vectors and system matrix ===

Thomas Witkowski's avatar
Thomas Witkowski committed
492
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
493
    systemMatrix->set(nullptr);
Thomas Witkowski's avatar
Thomas Witkowski committed
494
495
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
496

Thomas Witkowski's avatar
Thomas Witkowski committed
497
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
498
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
499
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
500
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
501

502
503
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
504

Thomas Witkowski's avatar
Thomas Witkowski committed
505
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
506
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
507
508
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
509
510
511
    }
  }

512

513
  void ProblemStatSeq::createSolver()
514
  {
515
    FUNCNAME("ProblemStat::createSolver()");
516
    
Praetorius, Simon's avatar
Praetorius, Simon committed
517
    // definition of standard-backends
518
519
520
521
522
523
524
525
526
527
528
529
530
#if defined HAVE_PARALLEL_PETSC
    string backend("p_petsc");
#elif defined HAVE_PARALLEL_MTL
    string backend("p_mtl");
#elif defined HAVE_PETSC
    string backend("petsc");
#else
    string backend("mtl");
#endif
    
    // === read backend-name ===
    string initFileStr = name + "->solver";    
    Parameters::get(initFileStr + "->backend", backend);
531

532
    // === read solver-name ===
533
    string solverType("0");
534
    Parameters::get(initFileStr, solverType);
535
536
537
538
539
540
541
    
    if (backend != "0" && backend != "no" && backend != "")
      solverType = backend + "_" + solverType;
    
    // === create solver ===
    LinearSolverCreator *solverCreator = 
      dynamic_cast<LinearSolverCreator*>(CreatorMap<LinearSolver>::getCreator(solverType, initFileStr));
542
543
    TEST_EXIT(solverCreator)
      ("No valid solver type found in parameter \"%s\"\n", initFileStr.c_str());
544
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
545
    solver = solverCreator->create();
546
547
  }

548

549
  void ProblemStatSeq::createEstimator()
550
  {
551
    FUNCNAME("ProblemStat::createEstimator()");
552
553

    // create and set leaf data prototype
Thomas Witkowski's avatar
Thomas Witkowski committed
554
    for (unsigned int i = 0; i < meshes.size(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
555
      meshes[i]->setElementDataPrototype
Thomas Witkowski's avatar
Thomas Witkowski committed
556
	(new LeafDataEstimatableVec(new LeafDataCoarsenableVec));
557

Thomas Witkowski's avatar
Thomas Witkowski committed
558
    for (int i = 0; i < nComponents; i++) {
559
      TEST_EXIT(estimator[i] == nullptr)("estimator already created\n");
560
561
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
562
563

      // === create estimator ===
564
565
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
566
      EstimatorCreator *estimatorCreator = 
567
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
568
      if (estimatorCreator) {
569
570
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
571
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
572
	estimator[i] = estimatorCreator->create();
573
574
575
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
576
577
578
579
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
Praetorius, Simon's avatar
Praetorius, Simon committed
580
				  rhs->getDOFVector(j));      // TODO: hier eventuell (i) statt (j) ???
581
582
583
    }
  }

584

585
  void ProblemStatSeq::createMarker()
586
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
587
    int nMarkersCreated = 0;
588

589
    for (int i = 0; i < nComponents; i++) {
590
      marker[i] = Marker::createMarker
591
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
592

593
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
594
595
596
597
	nMarkersCreated++;

	// If there is more than one marker, and all components are defined
	// on the same mesh, the maximum marking has to be enabled.
598
 	if (nMarkersCreated > 1 && nMeshes == 1)
Thomas Witkowski's avatar
Thomas Witkowski committed
599
 	  marker[i]->setMaximumMarking(true);
600
601
602
603
      }
    }
  }

604

605
  void ProblemStatSeq::createFileWriter()
606
  {
607
    FUNCNAME("ProblemStat::createFileWriter()");
608
609
  
    // Create one filewriter for all components of the problem
610
611
612
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
613
614

    if (filename != "") {
615
      vector< DOFVector<double>* > solutionList(nComponents);
616

617
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
618
	TEST_EXIT(componentMeshes[0] == componentMeshes[i])
619
620
	  ("All Meshes have to be equal to write a vector file.\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
621
	solutionList[i] = solution->getDOFVector(i);
622
623
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
624
      fileWriters.push_back(new FileWriter(numberedName,
625
626
					   componentMeshes[0],
					   solutionList));
627
628
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
629
    // Create own filewriters for each component of the problem
630
    for (int i = 0; i < nComponents; i++) {
631
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
632
      filename = "";
633
      Parameters::get(numberedName + "->filename", filename);
634

635
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
636
	fileWriters.push_back(new FileWriter(numberedName, 
637
					     componentMeshes[i], 
638
					     solution->getDOFVector(i)));      
639
640
641
    }

    int writeSerialization = 0;
642
    Parameters::get(name + "->output->write serialization", writeSerialization);
643
    if (writeSerialization)
644
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
645
646
  }

647

648
  void ProblemStatSeq::doOtherStuff()
649
  {}
650

651

652
653
654
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
655
656
657
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
658
    if (!solver) {
659
660
661
662
      WARNING("no solver\n");
      return;
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
663
    Timer t;
664
665
    solver->solveSystem(solverMatrix, *solution, *rhs, 
			createMatrixData, storeMatrixData);
666

Thomas Witkowski's avatar
Thomas Witkowski committed
667
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
Praetorius, Simon's avatar
Praetorius, Simon committed
668
		  t.elapsed());
669

670
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
671
672
673
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
674
675
  }

676

677
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
678
  {
679
    FUNCNAME("ProblemStat::estimate()");
680

Praetorius, Simon's avatar
Praetorius, Simon committed
681
    Timer t;
682

683
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
684
      computeError(adaptInfo);
685
686
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
687
	Estimator *scalEstimator = estimator[i];
688
689
	
	if (scalEstimator) {
690
691
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
692
	  scalEstimator->estimate(adaptInfo->getTimestep());
693

694
695
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
696
697
698
699
700

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
701
	}
702
703
704
      }
    }

705
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
706
    MPI::COMM_WORLD.Barrier();
707
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
708
709
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  t.elapsed());
710
711
  }

712

713
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
714
  {
715
    FUNCNAME_DBG("ProblemStat::markElements()");
716

717
    TEST_EXIT_DBG(static_cast<unsigned int>(nComponents) == marker.size())
Thomas Witkowski's avatar
Thomas Witkowski committed
718
719
      ("Wrong number of markers!\n");

720
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
721
    for (int i = 0; i < nComponents; i++)
722
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
723
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
724
    
725
726
727
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
728

729
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
730
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
731
    int nMeshes = static_cast<int>(meshes.size());
732
    Flag refineFlag = 0;
733
734
735
736
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

737
738
739
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
740

741
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
742
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
743
    int nMeshes = static_cast<int>(meshes.size());
744
    Flag coarsenFlag = 0;
745
746
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
747
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
748
749
750
751

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
752

753
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
754
  {
755
756
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
757
 	adaptInfo->allowRefinement(false, i);
758
      else
759
 	adaptInfo->allowRefinement(true, i);	
760
761
762
763

    return StandardProblemIteration::oneIteration(adaptInfo, toDo);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
764

765
766
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
767
  {
768
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
769

770
    if (dualMeshTraverseRequired()) {