ProblemStat.cc 53.7 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
93
94
95
96
97
  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);
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
  }

98
99
100
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
101
  {
102
    FUNCNAME("ProblemStat::initialize()");
103

104
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
105
    if (meshes.size() != 0) { 
106
107
108
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
109
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
110
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
111
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
112

113
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
114
	  (adoptFlag.isSet(INIT_MESH) ||
115
116
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
117
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
118
	componentMeshes = adoptProblem->componentMeshes;
119
120
121

	// 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
122
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
123
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
124
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
125
	  componentMeshes.resize(nComponents);
126
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
127
	    componentMeshes[i] = componentMeshes[0];
128
	}
129
130
131
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
132
    if (meshes.size() == 0) 
133
134
      WARNING("no mesh created\n");

135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
    // === 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");

156
    // === create fespace ===
157
    if (feSpaces.size() != 0) {
158
159
160
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
161
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
162
	createFeSpace(NULL);
163

164
165
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
166
	feSpaces = adoptProblem->getFeSpaces();
167
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
168
	traverseInfo = adoptProblem->traverseInfo;
169
170
171

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

180
181
182
      }
    }

183
    if (feSpaces.size() == 0) 
184
185
186
      WARNING("no feSpace created\n");

    // === create system ===
187
    if (initFlag.isSet(INIT_SYSTEM)) 
188
      createMatricesAndVectors();
189
    
190
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
191
      solution = adoptProblem->getSolution();
192
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
193
      systemMatrix = adoptProblem->getSystemMatrix();
194
195
196
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
197
    if (solver) {
198
199
200
201
202
203
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
204
205
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
206
207
208
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
209
    if (!solver) 
210
211
212
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
213
    if (initFlag.isSet(INIT_ESTIMATOR))
214
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
215
216

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
217
      estimator = adoptProblem->getEstimators();
218
219

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
220
    if (initFlag.isSet(INIT_MARKER))
221
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
222

Thomas Witkowski's avatar
Thomas Witkowski committed
223
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
224
      marker = adoptProblem->getMarkers();
225
226

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
227
    if (initFlag.isSet(INIT_FILEWRITER))
228
229
230
231
232
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
233
234
235
236
    // 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.
237
238

    int readSerialization = 0;
239
240
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
241
242
243
244
245
246

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

247
248
249
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
250
251
252
253
254
255

      // 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) {
256
257
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
258
259
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

260
261
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
262
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
263
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
264
	ifstream in(serializationFilename.c_str());
265
266
267
268
269
270

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

271
272
	deserialize(in);
	in.close();
273
#endif
274
275

	deserialized = true;
276
      } else {
277
	int globalRefinements = 0;
278

279
	// If AMDiS is compiled for parallel computations, the global refinements are
280
281
282
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
283
284
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
285
#endif
286

287
	bool initMesh = initFlag.isSet(INIT_MESH);
288

Thomas Witkowski's avatar
Thomas Witkowski committed
289
290
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
291
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
292
	    meshes[i]->initialize();	    	
293
294

	// === read value file and use it for the mesh values ===
295
296
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
297
	if (valueFilename.length())
298
299
300
301
302
303
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
304

Thomas Witkowski's avatar
Thomas Witkowski committed
305
	for (unsigned int i = 0; i < meshes.size(); i++)
306
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
307
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
308
309
310
311
312
313
      }
    }

    doOtherStuff();
  }

314

315
  void ProblemStatSeq::createMesh() 
316
  {
317
    FUNCNAME("ProblemStat::createMesh()");
318

Thomas Witkowski's avatar
Thomas Witkowski committed
319
    componentMeshes.resize(nComponents);
320
321
322
323
324
325
    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());
326
327

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

332
    for (int i = 0; i < nComponents; i++) {
333
      int refSet = -1;
334
335
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
336
      if (refSet < 0)
337
	refSet = 0;
338
      
339
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
340
	Mesh *newMesh = new Mesh(meshName, dim);
341
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
	meshes.push_back(newMesh);
	nMeshes++;
344
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
345
      componentMeshes[i] = meshForRefinementSet[refSet];
346
    }
347
348
349
350
351
352
353
354
355
356
357
  }


  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());
358
359

    switch (dim) {
360
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
361
      refinementManager = new RefinementManager1d();
362
      coarseningManager = new CoarseningManager1d();
363
364
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
365
      refinementManager = new RefinementManager2d();
366
      coarseningManager = new CoarseningManager2d();
367
368
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
369
      refinementManager = new RefinementManager3d();
370
      coarseningManager = new CoarseningManager3d();
371
372
373
374
375
376
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

377

378
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
379
  {
380
    FUNCNAME("ProblemStat::createFeSpace()");
381

382
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
383
    int dim = -1;
384
    Parameters::get(name + "->dim", dim);
385
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
386

387
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
388
    traverseInfo.resize(nComponents);
389

390
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
391
      int degree = 1;
392
393
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
394

395
      TEST_EXIT(componentSpaces[i] == NULL)("feSpace already created\n");
396

397
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
398
399
400
	stringstream s;
	s << name << "->feSpace[" << i << "]";

401
402
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
403
					  componentMeshes[i], s.str());
404
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
405
	feSpaces.push_back(newFeSpace);
406
      }
407

408
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
409
410
411
    }

    for (int i = 0; i < nComponents; i++) {
412
      for (int j = 0; j < nComponents; j++)
413
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
414
      
415
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
416
417
418
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
419
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
420
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
421
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
422
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
423
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
424
425
426
427
      }
    }
  }

428

429
  void ProblemStatSeq::createMatricesAndVectors()
430
  {
431
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
432
433
434

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

Thomas Witkowski's avatar
Thomas Witkowski committed
435
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
436
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
437
438
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
439

Thomas Witkowski's avatar
Thomas Witkowski committed
440
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
441
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
442
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
443
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
444

445
446
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
447

Thomas Witkowski's avatar
Thomas Witkowski committed
448
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
449
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
450
451
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
452
453
454
    }
  }

455

456
  void ProblemStatSeq::createSolver()
457
  {
458
    FUNCNAME("ProblemStat::createSolver()");
459
460

    // === create solver ===
461
    string solverType("0");
462
463
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
464
    OEMSolverCreator *solverCreator = 
465
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
466
    TEST_EXIT(solverCreator)("no solver type\n");
467
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
468
469
    solver = solverCreator->create();
    solver->initParameters();
470
471
  }

472

473
  void ProblemStatSeq::createEstimator()
474
  {
475
    FUNCNAME("ProblemStat::createEstimator()");
476
477

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

Thomas Witkowski's avatar
Thomas Witkowski committed
482
483
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
484
485
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
486
487

      // === create estimator ===
488
489
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
490
      EstimatorCreator *estimatorCreator = 
491
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
492
      if (estimatorCreator) {
493
494
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
495
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
496
	estimator[i] = estimatorCreator->create();
497
498
499
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
500
501
502
503
504
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
505
506
507
    }
  }

508

509
  void ProblemStatSeq::createMarker()
510
  {
511
    FUNCNAME("ProblemStat::createMarker()");
512

Thomas Witkowski's avatar
Thomas Witkowski committed
513
    int nMarkersCreated = 0;
514

515
    for (int i = 0; i < nComponents; i++) {
516
      marker[i] = Marker::createMarker
517
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
518

519
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
520
521
522
523
	nMarkersCreated++;

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

530

531
  void ProblemStatSeq::createFileWriter()
532
  {
533
    FUNCNAME("ProblemStat::createFileWriter()");
534
535
  
    // Create one filewriter for all components of the problem
536
537
538
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
539
540

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
547
	solutionList[i] = solution->getDOFVector(i);
548
549
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
550
      fileWriters.push_back(new FileWriter(numberedName,
551
552
					   componentMeshes[0],
					   solutionList));
553
554
555
    }

    // Create own filewriters for each components of the problem
556
    for (int i = 0; i < nComponents; i++) {
557
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
558
      filename = "";
559
      Parameters::get(numberedName + "->filename", filename);
560

561
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
562
	fileWriters.push_back(new FileWriter(numberedName, 
563
					     componentMeshes[i], 
564
					     solution->getDOFVector(i)));      
565
566
567
    }

    int writeSerialization = 0;
568
    Parameters::get(name + "->output->write serialization", writeSerialization);
569
    if (writeSerialization)
570
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
571
572
  }

573

574
  void ProblemStatSeq::doOtherStuff()
575
576
577
  {
  }

578

579
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool, bool)
580
581
582
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
583
    if (!solver) {
584
585
586
587
588
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
589
    
590
    solver->solveSystem(solverMatrix, *solution, *rhs);
591

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

595
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
596
597
598
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
599
600
  }

601

602
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
603
  {
604
    FUNCNAME("ProblemStat::estimate()");
605

606
607
608
609
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
610
611
#endif

612
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
613
      computeError(adaptInfo);
614
615
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
616
	Estimator *scalEstimator = estimator[i];
617
618
	
	if (scalEstimator) {
619
620
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
621
	  scalEstimator->estimate(adaptInfo->getTimestep());
622

623
624
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
625
626
627
628
629

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
630
	}
631
632
633
      }
    }

634
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
635
    MPI::COMM_WORLD.Barrier();
636
637
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
638
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
639
640
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
641
#endif
642
643
  }

644

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

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

652
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
653
    for (int i = 0; i < nComponents; i++)
654
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
655
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
656
    
657
658
659
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
660

661
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
662
  {
663
    FUNCNAME("ProblemStat::refineMesh()");
664

Thomas Witkowski's avatar
Thomas Witkowski committed
665
    int nMeshes = static_cast<int>(meshes.size());
666
    Flag refineFlag = 0;
667
668
669
670
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

671
672
673
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
674

675
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
676
  {
677
    FUNCNAME("ProblemStat::coarsenMesh()");
678

Thomas Witkowski's avatar
Thomas Witkowski committed
679
    int nMeshes = static_cast<int>(meshes.size());
680
    Flag coarsenFlag = 0;
681
682
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
683
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
684
685
686
687

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
688

689
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
690
  {
691
    FUNCNAME("ProblemStat::oneIteration()");
692

693
694
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
695
 	adaptInfo->allowRefinement(false, i);
696
      else
697
 	adaptInfo->allowRefinement(true, i);	
698
699
700
701

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

Thomas Witkowski's avatar
Thomas Witkowski committed
702

703
704
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
705
  {
706
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
707

708
709
710
//     buildAfterCoarsen_sebastianMode(adaptInfo, flag);
//     return;

711
    if (dualMeshTraverseRequired()) {
712
713
714
715
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

716
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
717
718
      return;
    }
719

720
721
722
723
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

724

Thomas Witkowski's avatar
Thomas Witkowski committed
725
726
727
728
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


729
730
731
732
733
734
735
736
737
#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
738

739
740
741

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
742
743
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
744
745
746
747
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
748
749
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
750
    if (useGetBound)
751
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
752
753

  
Thomas Witkowski's avatar
Thomas Witkowski committed
754
    traverseInfo.updateStatus();
755
756
757
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
758
    for (int i = 0; i < nComponents; i++) {
759

760
      MSG("%d DOFs for %s\n", 
761
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
762
763
764
765
	  componentSpaces[i]->getName().c_str());

      rhs->getDOFVector(i)->set(0.0);

766
      for (int j = 0; j < nComponents; j++) {
767

768
769
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
770

771
772
773
774
	// Only if this variable is true, the current matrix will be assembled.	
	bool assembleMatrix = true;
	// The DOFMatrix which should be assembled (or not, if assembleMatrix
	// will be set to false).
775
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
776

777
	if (writeAsmInfo && matrix) {
778
	  for (vector<Operator*>::iterator it = matrix->getOperatorsBegin();
779
780
781
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
782
	      cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << endl;
783
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
784
	      cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << endl;
785
	    if (assembler->getFirstOrderAssembler(GRD_PHI)) 
786
	      cout << "FOA GRD_PHI: " << assembler->getFirstOrderAssembler(GRD_PHI)->getName() << endl;