ProblemStat.cc 50.1 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
  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);
    TEST_EXIT(nComponents > 0)("components not set!\n");    
    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);
93
94
    componentNames.resize(nComponents);

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
102
103
104

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

112
113
114
115
116
117
118
119
120
121
122
    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
123
124

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

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


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

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

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

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

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

181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
    // === 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");

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

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

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

226
227
228
      }
    }

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
255
    if (!solver) 
256
257
258
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
259
    if (initFlag.isSet(INIT_ESTIMATOR))
260
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
263
      estimator = adoptProblem->getEstimators();
264
265

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
266
    if (initFlag.isSet(INIT_MARKER))
267
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
268

Thomas Witkowski's avatar
Thomas Witkowski committed
269
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
270
      marker = adoptProblem->getMarkers();
271
272

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

    int readSerialization = 0;
285
286
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
287
288
289
290
291
292

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

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

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

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

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

317
318
	deserialize(in);
	in.close();
319
#endif
320
321

	deserialized = true;
322
      } else {
323
	int globalRefinements = 0;
324

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

333
	bool initMesh = initFlag.isSet(INIT_MESH);
334

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

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

	// === do global refinements ===
350

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

    doOtherStuff();
  }

360

361
  void ProblemStatSeq::createMesh() 
362
  {
363
    FUNCNAME("ProblemStat::createMesh()");
364

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

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

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


  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());
404
405

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

423

424
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
425
  {
426
    FUNCNAME("ProblemStat::createFeSpace()");
427

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

433
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
434
    traverseInfo.resize(nComponents);
435

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

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

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

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

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

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

475

476
  void ProblemStatSeq::createMatricesAndVectors()
477
  {
478
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
479
480
481

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

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

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

492
493
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
494

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

502

503
  void ProblemStatSeq::createSolver()
504
  {
505
    FUNCNAME("ProblemStat::createSolver()");
506
507

    // === create solver ===
508
    string solverType("0");
509
510
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
511
    OEMSolverCreator *solverCreator = 
512
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
513
    TEST_EXIT(solverCreator)("no solver type\n");
514
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
515
516
    solver = solverCreator->create();
    solver->initParameters();
517
518
  }

519

520
  void ProblemStatSeq::createEstimator()
521
  {
522
    FUNCNAME("ProblemStat::createEstimator()");
523
524

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

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

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


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

555

556
  void ProblemStatSeq::createMarker()
557
  {
558
    FUNCNAME("ProblemStat::createMarker()");
559

Thomas Witkowski's avatar
Thomas Witkowski committed
560
    int nMarkersCreated = 0;
561

562
    for (int i = 0; i < nComponents; i++) {
563
      marker[i] = Marker::createMarker
564
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
565

566
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
567
568
569
570
	nMarkersCreated++;

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

577

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
594
	solutionList[i] = solution->getDOFVector(i);
595
596
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
597
      fileWriters.push_back(new FileWriter(numberedName,
598
599
					   componentMeshes[0],
					   solutionList));
600
601
602
    }

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

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

    int writeSerialization = 0;
615
    Parameters::get(name + "->output->write serialization", writeSerialization);
616
    if (writeSerialization)
617
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
618
619
  }

620

621
  void ProblemStatSeq::doOtherStuff()
622
  {}
623

624

625
626
627
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
628
629
630
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
631
    if (!solver) {
632
633
634
635
636
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
637
638
639

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

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

644
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
645
646
647
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
648
649
  }

650

651
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
652
  {
653
    FUNCNAME("ProblemStat::estimate()");
654

655
656
657
658
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
659
660
#endif

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

672
673
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
674
675
676
677
678

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
679
	}
680
681
682
      }
    }

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

693

694
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
695
  {
696
    FUNCNAME("ProblemStat::markElements()");
697

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
709

710
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
711
  {
712
    FUNCNAME("ProblemStat::refineMesh()");
713

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

720
721
722
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
723

724
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
725
  {
726
    FUNCNAME("ProblemStat::coarsenMesh()");
727

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

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
737

738
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
739
  {
740
    FUNCNAME("ProblemStat::oneIteration()");
741

742
743
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
744
 	adaptInfo->allowRefinement(false, i);
745
      else
746
 	adaptInfo->allowRefinement(true, i);	
747
748
749
750

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

Thomas Witkowski's avatar
Thomas Witkowski committed
751

752
753
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
754
  {
755
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
756

757
    if (dualMeshTraverseRequired()) {
758
759
760
761
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

762
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
763
764
      return;
    }
765

766
767
768
769
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

770

Thomas Witkowski's avatar
Thomas Witkowski committed
771
772
773
774
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


775
776
777
778
779
780
781
782
783
#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
784

785
786
787

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
788
789
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
790
791
792
793
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
794
795
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
796
    if (useGetBound)
797
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
798