ProblemStat.cc 50 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
      if (meshes[i])
	delete meshes[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
127
128
    
    for (unsigned int i = 0; i < estimator.size(); i++)
129
130
      if (estimator[i])
	delete estimator[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
131
132

    for (unsigned int i = 0; i < marker.size(); i++)
133
134
      if (marker[i])
	delete marker[i];
Thomas Witkowski's avatar
Thomas Witkowski committed
135
136
137
  }


138
139
140
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
141
  {
142
    FUNCNAME("ProblemStat::initialize()");
143

144
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
145
    if (meshes.size() != 0) { 
146
147
148
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
149
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
150
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
151
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
152

153
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
154
	  (adoptFlag.isSet(INIT_MESH) ||
155
156
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
157
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
158
	componentMeshes = adoptProblem->componentMeshes;
159
160
161

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

Thomas Witkowski's avatar
Thomas Witkowski committed
172
    if (meshes.size() == 0) 
173
174
      WARNING("no mesh created\n");

175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
    // === 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");

196
    // === create fespace ===
197
    if (feSpaces.size() != 0) {
198
199
200
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
201
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
202
	createFeSpace(NULL);
203

204
205
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
206
	feSpaces = adoptProblem->getFeSpaces();
207
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
208
	traverseInfo = adoptProblem->traverseInfo;
209
210
211

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

220
221
222
      }
    }

223
    if (feSpaces.size() == 0) 
224
225
226
      WARNING("no feSpace created\n");

    // === create system ===
227
    if (initFlag.isSet(INIT_SYSTEM)) 
228
      createMatricesAndVectors();
229
    
230
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
231
      solution = adoptProblem->getSolution();
232
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
233
      systemMatrix = adoptProblem->getSystemMatrix();
234
235
236
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
237
    if (solver) {
238
239
      WARNING("solver already created\n");
    } else {
240
      if (initFlag.isSet(INIT_SOLVER))
241
	createSolver();
242
      
243
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
246
247
248
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
249
    if (!solver) 
250
251
252
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
253
    if (initFlag.isSet(INIT_ESTIMATOR))
254
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
255
256

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
257
      estimator = adoptProblem->getEstimators();
258
259

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
260
    if (initFlag.isSet(INIT_MARKER))
261
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
262

Thomas Witkowski's avatar
Thomas Witkowski committed
263
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
264
      marker = adoptProblem->getMarkers();
265
266

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

    int readSerialization = 0;
279
280
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
281
282
283
284
285
286

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

287
288
289
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
290
291
292
293
294
295

      // 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) {
296
297
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
298
299
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

300
301
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
302
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
303
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
304
	ifstream in(serializationFilename.c_str());
305
306
307
308
309
310

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

311
312
	deserialize(in);
	in.close();
313
#endif
314
315

	deserialized = true;
316
      } else {
317
	int globalRefinements = 0;
318

319
	// If AMDiS is compiled for parallel computations, the global refinements are
320
321
322
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
323
324
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
325
#endif
326

327
	bool initMesh = initFlag.isSet(INIT_MESH);
328

Thomas Witkowski's avatar
Thomas Witkowski committed
329
330
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
331
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
332
	    meshes[i]->initialize();	    	
333
334

	// === read value file and use it for the mesh values ===
335
336
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
337
	if (valueFilename.length())
338
339
340
341
342
343
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
344

Thomas Witkowski's avatar
Thomas Witkowski committed
345
	for (unsigned int i = 0; i < meshes.size(); i++)
346
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
347
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
348
349
350
351
352
353
      }
    }

    doOtherStuff();
  }

354

355
  void ProblemStatSeq::createMesh() 
356
  {
357
    FUNCNAME("ProblemStat::createMesh()");
358

Thomas Witkowski's avatar
Thomas Witkowski committed
359
    componentMeshes.resize(nComponents);
360
361
362
363
364
365
    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());
366
367

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

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


  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());
398
399

    switch (dim) {
400
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
401
      refinementManager = new RefinementManager1d();
402
      coarseningManager = new CoarseningManager1d();
403
404
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
405
      refinementManager = new RefinementManager2d();
406
      coarseningManager = new CoarseningManager2d();
407
408
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
409
      refinementManager = new RefinementManager3d();
410
      coarseningManager = new CoarseningManager3d();
411
412
413
414
415
416
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

417

418
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
419
  {
420
    FUNCNAME("ProblemStat::createFeSpace()");
421

422
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
423
    int dim = -1;
424
    Parameters::get(name + "->dim", dim);
425
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
426

427
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
428
    traverseInfo.resize(nComponents);
429

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

438
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
439
440
441
	stringstream s;
	s << name << "->feSpace[" << i << "]";

442
443
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
444
					  componentMeshes[i], s.str());
445
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
446
	feSpaces.push_back(newFeSpace);
447
      }
448

449
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
450
451
452
    }

    for (int i = 0; i < nComponents; i++) {
453
      for (int j = 0; j < nComponents; j++)
454
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
455
      
456
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
457
458
459
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
460
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
461
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
462
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
463
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
464
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
465
466
467
468
      }
    }
  }

469

470
  void ProblemStatSeq::createMatricesAndVectors()
471
  {
472
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
473
474
475

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

Thomas Witkowski's avatar
Thomas Witkowski committed
476
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
477
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
478
479
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
480

Thomas Witkowski's avatar
Thomas Witkowski committed
481
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
482
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
483
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
484
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
485

486
487
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
488

Thomas Witkowski's avatar
Thomas Witkowski committed
489
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
490
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
491
492
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
493
494
495
    }
  }

496

497
  void ProblemStatSeq::createSolver()
498
  {
499
    FUNCNAME("ProblemStat::createSolver()");
500
501

    // === create solver ===
502
    string solverType("0");
503
504
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
505
    OEMSolverCreator *solverCreator = 
506
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
507
    TEST_EXIT(solverCreator)("no solver type\n");
508
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
509
510
    solver = solverCreator->create();
    solver->initParameters();
511
512
  }

513

514
  void ProblemStatSeq::createEstimator()
515
  {
516
    FUNCNAME("ProblemStat::createEstimator()");
517
518

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

Thomas Witkowski's avatar
Thomas Witkowski committed
523
524
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
525
526
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
527
528

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


Thomas Witkowski's avatar
Thomas Witkowski committed
541
542
543
544
545
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
546
547
548
    }
  }

549

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

Thomas Witkowski's avatar
Thomas Witkowski committed
554
    int nMarkersCreated = 0;
555

556
    for (int i = 0; i < nComponents; i++) {
557
      marker[i] = Marker::createMarker
558
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
559

560
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
561
562
563
564
	nMarkersCreated++;

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

571

572
  void ProblemStatSeq::createFileWriter()
573
  {
574
    FUNCNAME("ProblemStat::createFileWriter()");
575
576
  
    // Create one filewriter for all components of the problem
577
578
579
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
580
581

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
588
	solutionList[i] = solution->getDOFVector(i);
589
590
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
591
      fileWriters.push_back(new FileWriter(numberedName,
592
593
					   componentMeshes[0],
					   solutionList));
594
595
596
    }

    // Create own filewriters for each components of the problem
597
    for (int i = 0; i < nComponents; i++) {
598
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
599
      filename = "";
600
      Parameters::get(numberedName + "->filename", filename);
601

602
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
603
	fileWriters.push_back(new FileWriter(numberedName, 
604
					     componentMeshes[i], 
605
					     solution->getDOFVector(i)));      
606
607
608
    }

    int writeSerialization = 0;
609
    Parameters::get(name + "->output->write serialization", writeSerialization);
610
    if (writeSerialization)
611
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
612
613
  }

614

615
  void ProblemStatSeq::doOtherStuff()
616
  {}
617

618

619
620
621
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
622
623
624
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
625
    if (!solver) {
626
627
628
629
630
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
631
632
633

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

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

638
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
639
640
641
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
642
643
  }

644

645
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
646
  {
647
    FUNCNAME("ProblemStat::estimate()");
648

649
650
651
652
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
653
654
#endif

655
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
656
      computeError(adaptInfo);
657
658
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
659
	Estimator *scalEstimator = estimator[i];
660
661
	
	if (scalEstimator) {
662
663
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
664
	  scalEstimator->estimate(adaptInfo->getTimestep());
665

666
667
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
668
669
670
671
672

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
673
	}
674
675
676
      }
    }

677
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
678
    MPI::COMM_WORLD.Barrier();
679
680
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
681
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
682
683
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
684
#endif
685
686
  }

687

688
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
689
  {
690
    FUNCNAME("ProblemStat::markElements()");
691

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

695
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
696
    for (int i = 0; i < nComponents; i++)
697
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
698
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
699
    
700
701
702
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
703

704
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
705
  {
706
    FUNCNAME("ProblemStat::refineMesh()");
707

Thomas Witkowski's avatar
Thomas Witkowski committed
708
    int nMeshes = static_cast<int>(meshes.size());
709
    Flag refineFlag = 0;
710
711
712
713
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

714
715
716
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
717

718
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
719
  {
720
    FUNCNAME("ProblemStat::coarsenMesh()");
721

Thomas Witkowski's avatar
Thomas Witkowski committed
722
    int nMeshes = static_cast<int>(meshes.size());
723
    Flag coarsenFlag = 0;
724
725
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
726
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
727
728
729
730

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
731

732
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
733
  {
734
    FUNCNAME("ProblemStat::oneIteration()");
735

736
737
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
738
 	adaptInfo->allowRefinement(false, i);
739
      else
740
 	adaptInfo->allowRefinement(true, i);	
741
742
743
744

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

Thomas Witkowski's avatar
Thomas Witkowski committed
745

746
747
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
748
  {
749
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
750

751
    if (dualMeshTraverseRequired()) {
752
753
754
755
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

756
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
757
758
      return;
    }
759

760
761
762
763
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

764

Thomas Witkowski's avatar
Thomas Witkowski committed
765
766
767
768
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


769
770
771
772
773
774
775
776
777
#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
778

779
780
781

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
782
783
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
784
785
786
787
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
788
789
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
790
    if (useGetBound)
791
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
792
793

  
Thomas Witkowski's avatar
Thomas Witkowski committed
794
    traverseInfo.updateStatus();
795
796
797
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
798
    for (int rowComponent = 0; rowComponent < nComponents; rowComponent++) {
799