ProblemStat.cc 52.8 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
25
26
#include "RecoveryEstimator.h"
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Estimator.h"
#include "Marker.h"
#include "AdaptInfo.h"
27
#include "io/FileWriter.h"
28
29
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
30
#include "DualTraverse.h"
31
32
33
34
35
36
#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
37
#include "Flag.h"
38
39
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
40
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
41
#include "Debug.h"
42
43
44

namespace AMDiS {

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

48
49
50
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
51
  {
52
    FUNCNAME("ProblemStat::initialize()");
53

54
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
55
    if (meshes.size() != 0) { 
56
57
58
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
59
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
60
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
61
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
62

63
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
64
	  (adoptFlag.isSet(INIT_MESH) ||
65
66
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
67
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
68
	componentMeshes = adoptProblem->componentMeshes;
69
70
71

	// 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
72
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
73
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
74
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
75
	  componentMeshes.resize(nComponents);
76
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
77
	    componentMeshes[i] = componentMeshes[0];
78
79
	}

80
81
82
83

	// If the problem adopts the mesh but creates an own FE space.
	if (initFlag.isSet(INIT_FE_SPACE)) {
	}
84
85
86
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
87
    if (meshes.size() == 0) 
88
89
      WARNING("no mesh created\n");

90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
    // === 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");

111
    // === create fespace ===
112
    if (feSpaces.size() != 0) {
113
114
115
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
116
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
117
	createFeSpace(NULL);
118

119
120
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
121
	feSpaces = adoptProblem->getFeSpaces();
122
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
123
	traverseInfo = adoptProblem->traverseInfo;
124
125
126

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

135
136
137
      }
    }

138
    if (feSpaces.size() == 0) 
139
140
141
      WARNING("no feSpace created\n");

    // === create system ===
142
    if (initFlag.isSet(INIT_SYSTEM)) 
143
      createMatricesAndVectors();
144
    
145
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
146
      solution = adoptProblem->getSolution();
147
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
148
      systemMatrix = adoptProblem->getSystemMatrix();
149
150
151
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
152
    if (solver) {
153
154
155
156
157
158
      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
159
160
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
161
162
163
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
164
    if (!solver) 
165
166
167
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
168
    if (initFlag.isSet(INIT_ESTIMATOR))
169
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
170
171

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
172
      estimator = adoptProblem->getEstimators();
173
174

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
175
    if (initFlag.isSet(INIT_MARKER))
176
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
177

Thomas Witkowski's avatar
Thomas Witkowski committed
178
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
179
      marker = adoptProblem->getMarkers();
180
181

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
182
    if (initFlag.isSet(INIT_FILEWRITER))
183
184
185
186
187
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
188
189
190
191
    // 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.
192
193

    int readSerialization = 0;
194
195
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
196
197
198
199
200
201

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

202
203
204
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
205
206
207
208
209
210

      // 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) {
211
212
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
213
214
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

215
216
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
217
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
218
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
219
	ifstream in(serializationFilename.c_str());
220
221
222
223
224
225

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

226
227
	deserialize(in);
	in.close();
228
#endif
229
230

	deserialized = true;
231
      } else {
232
	int globalRefinements = 0;
233

234
	// If AMDiS is compiled for parallel computations, the global refinements are
235
236
237
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
238
239
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
240
#endif
241

242
	bool initMesh = initFlag.isSet(INIT_MESH);
243

Thomas Witkowski's avatar
Thomas Witkowski committed
244
245
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
246
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
247
	    meshes[i]->initialize();	    	
248
249

	// === read value file and use it for the mesh values ===
250
251
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
252
	if (valueFilename.length())
253
254
255
256
257
258
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
259

Thomas Witkowski's avatar
Thomas Witkowski committed
260
	for (unsigned int i = 0; i < meshes.size(); i++)
261
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
262
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
263
264
265
266
267
268
      }
    }

    doOtherStuff();
  }

269

270
  void ProblemStatSeq::createMesh() 
271
  {
272
    FUNCNAME("ProblemStat::createMesh()");
273

Thomas Witkowski's avatar
Thomas Witkowski committed
274
    componentMeshes.resize(nComponents);
275
276
277
278
279
280
    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());
281
282

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

287
    for (int i = 0; i < nComponents; i++) {
288
      int refSet = -1;
289
290
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
291
      if (refSet < 0)
292
	refSet = 0;
293
      
294
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
295
	Mesh *newMesh = new Mesh(meshName, dim);
296
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
297
298
	meshes.push_back(newMesh);
	nMeshes++;
299
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
300
      componentMeshes[i] = meshForRefinementSet[refSet];
301
    }
302
303
304
305
306
307
308
309
310
311
312
  }


  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());
313
314

    switch (dim) {
315
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
316
      refinementManager = new RefinementManager1d();
317
      coarseningManager = new CoarseningManager1d();
318
319
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
320
      refinementManager = new RefinementManager2d();
321
      coarseningManager = new CoarseningManager2d();
322
323
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      refinementManager = new RefinementManager3d();
325
      coarseningManager = new CoarseningManager3d();
326
327
328
329
330
331
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

332

333
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
334
  {
335
    FUNCNAME("ProblemStat::createFeSpace()");
336

337
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
338
    int dim = -1;
339
    Parameters::get(name + "->dim", dim);
340
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
341

342
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
343
    traverseInfo.resize(nComponents);
344

345
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
346
      int degree = 1;
347
348
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
349

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

352
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
353
354
355
	stringstream s;
	s << name << "->feSpace[" << i << "]";

356
357
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
358
					  componentMeshes[i], s.str());
359
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
360
	feSpaces.push_back(newFeSpace);
361
      }
362

363
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
364
365
366
    }

    for (int i = 0; i < nComponents; i++) {
367
      for (int j = 0; j < nComponents; j++)
368
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
369
      
370
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
371
372
373
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
374
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
375
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
376
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
377
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
378
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
379
380
381
382
      }
    }
  }

383

384
  void ProblemStatSeq::createMatricesAndVectors()
385
  {
386
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
387
388
389

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

Thomas Witkowski's avatar
Thomas Witkowski committed
390
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
391
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
392
393
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
394

Thomas Witkowski's avatar
Thomas Witkowski committed
395
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
396
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
397
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
398
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
399

400
      string numberedName = "rhs[" + boost::lexical_cast<string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
401
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
402

403
      numberedName = "solution[" + boost::lexical_cast<string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
404
405
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
406
407
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
408
409
410
    }
  }

411

412
  void ProblemStatSeq::createSolver()
413
  {
414
    FUNCNAME("ProblemStat::createSolver()");
415
416

    // === create solver ===
417
418
    string solverType("0");
    Parameters::get(name + "->solver", solverType);
419
420
    OEMSolverCreator *solverCreator = 
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType));
421
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
422
423
424
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
425
426
  }

427

428
  void ProblemStatSeq::createEstimator()
429
  {
430
    FUNCNAME("ProblemStat::createEstimator()");
431
432

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

Thomas Witkowski's avatar
Thomas Witkowski committed
437
438
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
439
440
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
441
442

      // === create estimator ===
443
444
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
445
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
446
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
447
      if (estimatorCreator) {
448
449
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
450
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
451
	estimator[i] = estimatorCreator->create();
452
453
454
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
455
456
457
458
459
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
460
461
462
    }
  }

463

464
  void ProblemStatSeq::createMarker()
465
  {
466
    FUNCNAME("ProblemStat::createMarker()");
467

Thomas Witkowski's avatar
Thomas Witkowski committed
468
    int nMarkersCreated = 0;
469

470
    for (int i = 0; i < nComponents; i++) {
471
      marker[i] = Marker::createMarker
472
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
473

474
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
475
476
477
478
	nMarkersCreated++;

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

485

486
  void ProblemStatSeq::createFileWriter()
487
  {
488
    FUNCNAME("ProblemStat::createFileWriter()");
489
490
  
    // Create one filewriter for all components of the problem
491
492
493
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
494
495

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
502
	solutionList[i] = solution->getDOFVector(i);
503
504
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
505
      fileWriters.push_back(new FileWriter(numberedName,
506
507
					   componentMeshes[0],
					   solutionList));
508
509
510
    }

    // Create own filewriters for each components of the problem
511
    for (int i = 0; i < nComponents; i++) {
512
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
513
      filename = "";
514
      Parameters::get(numberedName + "->filename", filename);
515

516
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
517
	fileWriters.push_back(new FileWriter(numberedName, 
518
					     componentMeshes[i], 
519
					     solution->getDOFVector(i)));      
520
521
522
523
    }

    // Check for serializer
    int writeSerialization = 0;
524
    Parameters::get(name + "->write serialization", writeSerialization);
525
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
526
527
      MSG("Use are using the obsolete parameter: %s->write serialization\n", name.c_str());
      MSG("Please use instead the following parameter: %s->output->write serialization\n", name.c_str());
528
529
530
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

531
    Parameters::get(name + "->output->write serialization", writeSerialization);
532

533
    if (writeSerialization)
534
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
535
536
  }

537

538
  void ProblemStatSeq::doOtherStuff()
539
540
541
  {
  }

542

543
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool, bool)
544
545
546
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
547
    if (!solver) {
548
549
550
551
552
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
553
    
554
    solver->solveSystem(solverMatrix, *solution, *rhs);
555

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

559
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
560
561
562
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
563
564
  }

565

566
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
567
  {
568
    FUNCNAME("ProblemStat::estimate()");
569

570
571
572
573
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
574
575
#endif

576
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
577
      computeError(adaptInfo);
578
579
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
580
	Estimator *scalEstimator = estimator[i];
581
582
	
	if (scalEstimator) {
583
584
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
585
	  scalEstimator->estimate(adaptInfo->getTimestep());
586

587
588
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
589
590
591
592
593

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
594
	}
595
596
597
      }
    }

598
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
599
    MPI::COMM_WORLD.Barrier();
600
601
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
602
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
603
604
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
605
#endif
606
607
  }

608

609
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
610
  {
611
    FUNCNAME("ProblemStat::markElements()");
612

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

616
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
617
    for (int i = 0; i < nComponents; i++)
618
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
619
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
620
    
621
622
623
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
624

625
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
626
  {
627
    FUNCNAME("ProblemStat::refineMesh()");
628

Thomas Witkowski's avatar
Thomas Witkowski committed
629
    int nMeshes = static_cast<int>(meshes.size());
630
    Flag refineFlag = 0;
631
632
633
634
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

635
636
637
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
638

639
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
640
  {
641
    FUNCNAME("ProblemStat::coarsenMesh()");
642

Thomas Witkowski's avatar
Thomas Witkowski committed
643
    int nMeshes = static_cast<int>(meshes.size());
644
    Flag coarsenFlag = 0;
645
646
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
647
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
648
649
650
651

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
652

653
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
654
  {
655
    FUNCNAME("ProblemStat::oneIteration()");
656

657
658
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
659
 	adaptInfo->allowRefinement(false, i);
660
      else
661
 	adaptInfo->allowRefinement(true, i);	
662
663
664
665

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

Thomas Witkowski's avatar
Thomas Witkowski committed
666

667
668
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
669
  {
670
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
671

672
673
674
//     buildAfterCoarsen_sebastianMode(adaptInfo, flag);
//     return;

675
    if (dualMeshTraverseRequired()) {
676
677
678
679
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

680
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
681
682
      return;
    }
683

684
685
686
687
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

688

Thomas Witkowski's avatar
Thomas Witkowski committed
689
690
691
692
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


693
694
695
696
697
698
699
700
701
#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
702

703
704
705

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
706
707
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
708
709
710
711
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
712
713
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
714
    if (useGetBound)
715
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
716
717

  
Thomas Witkowski's avatar
Thomas Witkowski committed
718
    traverseInfo.updateStatus();
719
720
721
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

724
      MSG("%d DOFs for %s\n", 
725
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
726
727
728
729
	  componentSpaces[i]->getName().c_str());

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

730
      for (int j = 0; j < nComponents; j++) {
731

732
733
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
734

735
736
737
738
	// 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).
739
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
740

741
	if (writeAsmInfo && matrix) {
742
	  for (vector<Operator*>::iterator it = matrix->getOperatorsBegin();
743
744
745
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
746
	      cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << endl;
747
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
748
	      cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << endl;
749
	    if (assembler->getFirstOrderAssembler(GRD_PHI)) 
750
	      cout << "FOA GRD_PHI: " << assembler->getFirstOrderAssembler(GRD_PHI)->getName() << endl;
751
	    if (assembler->getSecondOrderAssembler()) 
752
	      cout << "SOA: " << assembler->getSecondOrderAssembler()->getName() << endl;
753
754
755
	  }
	}

756
757
758
	if (matrix) 
	  matrix->calculateNnz();
	
759
760
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.