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


Thomas Witkowski's avatar
Thomas Witkowski committed
13
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
14
#include <boost/lexical_cast.hpp>
15
16

#include "ProblemStat.h"
17
18
19
20
21
22
23
24
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Marker.h"
#include "AdaptInfo.h"
25
#include "io/FileWriter.h"
26
27
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
28
#include "DualTraverse.h"
29
30
31
32
33
34
#include "Mesh.h"
#include "OEMSolver.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
35
#include "Flag.h"
36
#include "est/Estimator.h"
37
38
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
39
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
40
#include "Debug.h"
41
42
43

namespace AMDiS {

44
  using namespace std;
45
46
  using boost::lexical_cast;

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  ProblemStatSeq::ProblemStatSeq(string nameStr,
				 ProblemIterationInterface *problemIteration)
    : StandardProblemIteration(this),
      name(nameStr),
      nComponents(-1),
      nMeshes(0),
      traverseInfo(0),
      solver(NULL),
      solution(NULL),
      rhs(NULL),
      systemMatrix(NULL),
      useGetBound(true),
      refinementManager(NULL),
      coarseningManager(NULL),
      info(10),
      deserialized(false),
      computeExactError(false),
      boundaryConditionSet(false),
      writeAsmInfo(false)
  {
    Parameters::get(name + "->components", nComponents);
68
69
    TEST_EXIT(nComponents > 0)("No value set for parameter \"%s->components\"!\n",
			       name.c_str());    
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    estimator.resize(nComponents, NULL);
    marker.resize(nComponents, NULL);
    
    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);
94
95
    componentNames.resize(nComponents);

96
97
98
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
Thomas Witkowski's avatar
Thomas Witkowski committed
99
100

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

Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
105

  ProblemStatSeq::~ProblemStatSeq()
  {
106
107
    if (rhs)
      delete rhs;
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    rhs = NULL;
109
110
    if (solution)
      delete solution;
Thomas Witkowski's avatar
Thomas Witkowski committed
111
112
    solution = NULL;

113
114
115
116
117
118
119
120
121
122
123
    if (systemMatrix) {    
      for (int i = 0; i < nComponents; i++)
	for (int j = 0; j < nComponents; j++)
	  if ((*systemMatrix)[i][j]) {
	    delete (*systemMatrix)[i][j];
	    (*systemMatrix)[i][j] = NULL;
	  }

      delete systemMatrix;
      systemMatrix = NULL;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
124
125

    for (unsigned int i = 0; i < meshes.size(); i++)
126
127
128
129
      if (meshes[i]) {
// 	delete meshes[i];
// 	meshes[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
130
131
    
    for (unsigned int i = 0; i < estimator.size(); i++)
132
      if (estimator[i]) {
133
	delete estimator[i];
134
135
	estimator[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
136
137

    for (unsigned int i = 0; i < marker.size(); i++)
138
      if (marker[i]) {
139
	delete marker[i];
140
141
	marker[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
142
143
144
  }


145
146
147
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
148
  {
149
    FUNCNAME("ProblemStat::initialize()");
150

151
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
152
    if (meshes.size() != 0) { 
153
154
155
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
156
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
157
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
158
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
159

160
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
161
	  (adoptFlag.isSet(INIT_MESH) ||
162
163
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
164
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
165
	componentMeshes = adoptProblem->componentMeshes;
166
167
168

	// 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
169
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
170
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
171
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
172
	  componentMeshes.resize(nComponents);
173
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
174
	    componentMeshes[i] = componentMeshes[0];
175
	}
176
177
178
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
179
    if (meshes.size() == 0) 
180
181
      WARNING("no mesh created\n");

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    // === create refinement/corasening-manager ===
    if (refinementManager != NULL && coarseningManager != NULL) { 
      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;
      }
    }

    if (refinementManager == NULL || coarseningManager == NULL) 
      WARNING("no refinement-/coarseningmanager created\n");

203
    // === create fespace ===
204
    if (feSpaces.size() != 0) {
205
206
207
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
208
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
209
	createFeSpace(NULL);
210

211
212
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
213
	feSpaces = adoptProblem->getFeSpaces();
214
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
215
	traverseInfo = adoptProblem->traverseInfo;
216
217
218

	// 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
219
220
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
221
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
222
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
223
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	    componentSpaces[i] = componentSpaces[0];
225
226
	}

227
228
229
      }
    }

230
    if (feSpaces.size() == 0) 
231
232
233
      WARNING("no feSpace created\n");

    // === create system ===
234
    if (initFlag.isSet(INIT_SYSTEM)) 
235
      createMatricesAndVectors();
236
    
237
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
238
      solution = adoptProblem->getSolution();
239
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
240
      systemMatrix = adoptProblem->getSystemMatrix();
241
242
243
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
244
    if (solver) {
245
246
      WARNING("solver already created\n");
    } else {
247
      if (initFlag.isSet(INIT_SOLVER))
248
	createSolver();
249
      
250
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
251
252
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
253
254
255
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
256
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
257
    if (!solver) 
258
      WARNING("no solver created\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
259
#endif
260
261

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
262
    if (initFlag.isSet(INIT_ESTIMATOR))
263
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
264
265

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
266
      estimator = adoptProblem->getEstimators();
267
268

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
269
    if (initFlag.isSet(INIT_MARKER))
270
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
271

Thomas Witkowski's avatar
Thomas Witkowski committed
272
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
273
      marker = adoptProblem->getMarkers();
274
275

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
276
    if (initFlag.isSet(INIT_FILEWRITER))
277
278
279
280
281
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
282
283
284
285
    // 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.
286
287

    int readSerialization = 0;
288
289
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
290
291
292
293
294
295

    // 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;

296
297
298
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
299
300
301
302
303
304

      // 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) {
305
306
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
307
308
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

309
310
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
311
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
312
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
313
	ifstream in(serializationFilename.c_str());
314
315
316
317
318
319

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

320
321
	deserialize(in);
	in.close();
322
#endif
323
324

	deserialized = true;
325
      } else {
326
	int globalRefinements = 0;
327

328
	// If AMDiS is compiled for parallel computations, the global refinements are
329
330
331
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
332
333
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
334
#endif
335

336
	bool initMesh = initFlag.isSet(INIT_MESH);
337

Thomas Witkowski's avatar
Thomas Witkowski committed
338
339
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
340
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
341
	    meshes[i]->initialize();	    	
342
343

	// === read value file and use it for the mesh values ===
344
345
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
346
	if (valueFilename.length())
347
348
349
350
351
352
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
353

Thomas Witkowski's avatar
Thomas Witkowski committed
354
	for (unsigned int i = 0; i < meshes.size(); i++)
355
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
356
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
357
358
359
360
361
362
      }
    }

    doOtherStuff();
  }

363

364
  void ProblemStatSeq::createMesh() 
365
  {
366
    FUNCNAME("ProblemStat::createMesh()");
367

Thomas Witkowski's avatar
Thomas Witkowski committed
368
    componentMeshes.resize(nComponents);
369
370
371
372
373
374
    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());
375
376

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

381
    for (int i = 0; i < nComponents; i++) {
382
      int refSet = -1;
383
384
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
385
      if (refSet < 0)
386
	refSet = 0;
387
      
388
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
389
	Mesh *newMesh = new Mesh(meshName, dim);
390
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
391
392
	meshes.push_back(newMesh);
	nMeshes++;
393
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
394
      componentMeshes[i] = meshForRefinementSet[refSet];
395
    }
396
397
398
399
400
401
402
403
404
405
406
  }


  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());
407
408

    switch (dim) {
409
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
410
      refinementManager = new RefinementManager1d();
411
      coarseningManager = new CoarseningManager1d();
412
413
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
414
      refinementManager = new RefinementManager2d();
415
      coarseningManager = new CoarseningManager2d();
416
417
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
418
      refinementManager = new RefinementManager3d();
419
      coarseningManager = new CoarseningManager3d();
420
421
422
423
424
425
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

426

427
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
428
  {
429
    FUNCNAME("ProblemStat::createFeSpace()");
430

431
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
432
    int dim = -1;
433
    Parameters::get(name + "->dim", dim);
434
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
435

436
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
437
    traverseInfo.resize(nComponents);
438

439
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
440
      int degree = 1;
441
442
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
443
444
      TEST_EXIT(degree > 0)
	("Poynomial degree in component %d must be larger than zero!\n", i);
445
      TEST_EXIT(componentSpaces[i] == NULL)("feSpace already created\n");
446

447
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
448
449
450
	stringstream s;
	s << name << "->feSpace[" << i << "]";

451
452
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
453
					  componentMeshes[i], s.str());
454
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
455
	feSpaces.push_back(newFeSpace);
456
      }
457

458
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
459
460
461
    }

    for (int i = 0; i < nComponents; i++) {
462
      for (int j = 0; j < nComponents; j++)
463
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
464
      
465
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
466
467
468
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
469
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
470
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
471
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
472
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
473
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
474
475
476
477
      }
    }
  }

478

479
  void ProblemStatSeq::createMatricesAndVectors()
480
  {
481
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
482
483
484

    // === create vectors and system matrix ===

Thomas Witkowski's avatar
Thomas Witkowski committed
485
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
486
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
487
488
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
489

Thomas Witkowski's avatar
Thomas Witkowski committed
490
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
491
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
492
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
493
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
494

495
496
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
497

Thomas Witkowski's avatar
Thomas Witkowski committed
498
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
499
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
500
501
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
502
503
504
    }
  }

505

506
  void ProblemStatSeq::createSolver()
507
  {
508
    FUNCNAME("ProblemStat::createSolver()");
509
510

    // === create solver ===
511
    string solverType("0");
512
513
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
514
    OEMSolverCreator *solverCreator = 
515
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
516
517
    TEST_EXIT(solverCreator)
      ("No valid solver type found in parameter \"%s\"\n", initFileStr.c_str());
518
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
519
520
    solver = solverCreator->create();
    solver->initParameters();
521
522
  }

523

524
  void ProblemStatSeq::createEstimator()
525
  {
526
    FUNCNAME("ProblemStat::createEstimator()");
527
528

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

Thomas Witkowski's avatar
Thomas Witkowski committed
533
534
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
535
536
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
537
538

      // === create estimator ===
539
540
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
541
      EstimatorCreator *estimatorCreator = 
542
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
543
      if (estimatorCreator) {
544
545
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
546
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
547
	estimator[i] = estimatorCreator->create();
548
549
550
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
551
552
553
554
555
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
556
557
558
    }
  }

559

560
  void ProblemStatSeq::createMarker()
561
  {
562
    FUNCNAME("ProblemStat::createMarker()");
563

Thomas Witkowski's avatar
Thomas Witkowski committed
564
    int nMarkersCreated = 0;
565

566
    for (int i = 0; i < nComponents; i++) {
567
      marker[i] = Marker::createMarker
568
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
569

570
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
571
572
573
574
	nMarkersCreated++;

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

581

582
  void ProblemStatSeq::createFileWriter()
583
  {
584
    FUNCNAME("ProblemStat::createFileWriter()");
585
586
  
    // Create one filewriter for all components of the problem
587
588
589
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
590
591

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
598
	solutionList[i] = solution->getDOFVector(i);
599
600
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
601
      fileWriters.push_back(new FileWriter(numberedName,
602
603
					   componentMeshes[0],
					   solutionList));
604
605
606
    }

    // Create own filewriters for each components of the problem
607
    for (int i = 0; i < nComponents; i++) {
608
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
609
      filename = "";
610
      Parameters::get(numberedName + "->filename", filename);
611

612
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
613
	fileWriters.push_back(new FileWriter(numberedName, 
614
					     componentMeshes[i], 
615
					     solution->getDOFVector(i)));      
616
617
618
    }

    int writeSerialization = 0;
619
    Parameters::get(name + "->output->write serialization", writeSerialization);
620
    if (writeSerialization)
621
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
622
623
  }

624

625
  void ProblemStatSeq::doOtherStuff()
626
  {}
627

628

629
630
631
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
632
633
634
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
635
    if (!solver) {
636
637
638
639
640
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
641
642
643

    solver->solveSystem(solverMatrix, *solution, *rhs, 
			createMatrixData, storeMatrixData);
644

Thomas Witkowski's avatar
Thomas Witkowski committed
645
646
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
647

648
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
649
650
651
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
652
653
  }

654

655
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
656
  {
657
    FUNCNAME("ProblemStat::estimate()");
658

659
660
661
662
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
663
664
#endif

665
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
666
      computeError(adaptInfo);
667
668
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
669
	Estimator *scalEstimator = estimator[i];
670
671
	
	if (scalEstimator) {
672
673
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
674
	  scalEstimator->estimate(adaptInfo->getTimestep());
675

676
677
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
678
679
680
681
682

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
683
	}
684
685
686
      }
    }

687
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
688
    MPI::COMM_WORLD.Barrier();
689
690
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
691
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
692
693
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
694
#endif
695
696
  }

697

698
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
699
  {
700
    FUNCNAME("ProblemStat::markElements()");
701

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

705
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
706
    for (int i = 0; i < nComponents; i++)
707
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
708
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
709
    
710
711
712
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
713

714
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
715
  {
716
    FUNCNAME("ProblemStat::refineMesh()");
717

Thomas Witkowski's avatar
Thomas Witkowski committed
718
    int nMeshes = static_cast<int>(meshes.size());
719
    Flag refineFlag = 0;
720
721
722
723
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

724
725
726
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
727

728
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
729
  {
730
    FUNCNAME("ProblemStat::coarsenMesh()");
731

Thomas Witkowski's avatar
Thomas Witkowski committed
732
    int nMeshes = static_cast<int>(meshes.size());
733
    Flag coarsenFlag = 0;
734
735
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
736
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
737
738
739
740

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
741

742
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
743
  {
744
    FUNCNAME("ProblemStat::oneIteration()");
745

746
747
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
748
 	adaptInfo->allowRefinement(false, i);
749
      else
750
 	adaptInfo->allowRefinement(true, i);	
751
752
753
754

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

Thomas Witkowski's avatar
Thomas Witkowski committed
755

756
757
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
758
  {
759
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
760

761
    if (dualMeshTraverseRequired()) {
762
763
764
765
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

766
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
767
768
      return;
    }
769

770
771
772
773
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

774

Thomas Witkowski's avatar
Thomas Witkowski committed
775
776
777
778
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


779
780
781
782
783
784
785
786
787
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    MPI::COMM_WORLD.Barrier();
    INFO(info, 8)("dof compression needed %.5f seconds\n", 
		  MPI::Wtime() - first);

    first = MPI::Wtime();
#else
    clock_t first = clock();
#endif
788

789
790
791

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
792
793
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |