ProblemStat.cc 52.6 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
	deserialize(in);
	in.close();
222
223

#endif
224
225

	deserialized = true;
226
      } else {
227
	int globalRefinements = 0;
228

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

237
	bool initMesh = initFlag.isSet(INIT_MESH);
238

Thomas Witkowski's avatar
Thomas Witkowski committed
239
240
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
241
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
242
	    meshes[i]->initialize();	    	
243
244

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

	// === do global refinements ===
254

Thomas Witkowski's avatar
Thomas Witkowski committed
255
	for (unsigned int i = 0; i < meshes.size(); i++)
256
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
257
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
258
259
260
261
262
263
      }
    }

    doOtherStuff();
  }

264

265
  void ProblemStatSeq::createMesh() 
266
  {
267
    FUNCNAME("ProblemStat::createMesh()");
268

Thomas Witkowski's avatar
Thomas Witkowski committed
269
    componentMeshes.resize(nComponents);
270
271
272
273
274
275
    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());
276
277

    int dim = 0;
278
279
280
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
281

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


  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());
308
309

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

327

328
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
329
  {
330
    FUNCNAME("ProblemStat::createFeSpace()");
331

332
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
333
    int dim = -1;
334
    Parameters::get(name + "->dim", dim);
335
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
336

337
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
338
    traverseInfo.resize(nComponents);
339

340
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
341
      int degree = 1;
342
343
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
344

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

347
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
348
349
350
	stringstream s;
	s << name << "->feSpace[" << i << "]";

351
352
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
353
					  componentMeshes[i], s.str());
354
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
355
	feSpaces.push_back(newFeSpace);
356
      }
357

358
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
359
360
361
    }

    for (int i = 0; i < nComponents; i++) {
362
      for (int j = 0; j < nComponents; j++)
363
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
364
      
365
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
366
367
368
    }

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

378

379
  void ProblemStatSeq::createMatricesAndVectors()
380
  {
381
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
382
383
384

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

Thomas Witkowski's avatar
Thomas Witkowski committed
385
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
386
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
387
388
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
389

Thomas Witkowski's avatar
Thomas Witkowski committed
390
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
391
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
392
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
393
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
394

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

398
      numberedName = "solution[" + boost::lexical_cast<string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
399
400
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
401
402
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
403
404
405
    }
  }

406

407
  void ProblemStatSeq::createSolver()
408
  {
409
    FUNCNAME("ProblemStat::createSolver()");
410
411

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

422

423
  void ProblemStatSeq::createEstimator()
424
  {
425
    FUNCNAME("ProblemStat::createEstimator()");
426
427

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

Thomas Witkowski's avatar
Thomas Witkowski committed
432
433
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
434
435
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
436
437

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


Thomas Witkowski's avatar
Thomas Witkowski committed
450
451
452
453
454
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
455
456
457
    }
  }

458

459
  void ProblemStatSeq::createMarker()
460
  {
461
    FUNCNAME("ProblemStat::createMarker()");
462

Thomas Witkowski's avatar
Thomas Witkowski committed
463
    int nMarkersCreated = 0;
464

465
    for (int i = 0; i < nComponents; i++) {
466
      marker[i] = Marker::createMarker
467
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
468

469
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
470
471
472
473
	nMarkersCreated++;

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

480

481
  void ProblemStatSeq::createFileWriter()
482
  {
483
    FUNCNAME("ProblemStat::createFileWriter()");
484
485
  
    // Create one filewriter for all components of the problem
486
487
488
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
489
490

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
497
	solutionList[i] = solution->getDOFVector(i);
498
499
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
500
      fileWriters.push_back(new FileWriter(numberedName,
501
502
					   componentMeshes[0],
					   solutionList));
503
504
505
    }

    // Create own filewriters for each components of the problem
506
    for (int i = 0; i < nComponents; i++) {
507
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
508
      filename = "";
509
      Parameters::get(numberedName + "->filename", filename);
510

511
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
512
	fileWriters.push_back(new FileWriter(numberedName, 
513
					     componentMeshes[i], 
514
					     solution->getDOFVector(i)));      
515
516
517
518
    }

    // Check for serializer
    int writeSerialization = 0;
519
    Parameters::get(name + "->write serialization", writeSerialization);
520
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
521
522
      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());
523
524
525
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

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

528
    if (writeSerialization)
529
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
530
531
  }

532

533
  void ProblemStatSeq::doOtherStuff()
534
535
536
  {
  }

537

538
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
539
540
541
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
542
    if (!solver) {
543
544
545
546
547
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
548
    
549
    solver->solveSystem(solverMatrix, *solution, *rhs);
550

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

554
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
555
556
557
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
558
559
  }

560

561
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
562
  {
563
    FUNCNAME("ProblemStat::estimate()");
564

565
566
567
568
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
569
570
#endif

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

582
583
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
584
585
586
587
588

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
589
	}
590
591
592
      }
    }

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

603

604
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
605
  {
606
    FUNCNAME("ProblemStat::markElements()");
607

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

611
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
612
    for (int i = 0; i < nComponents; i++)
613
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
614
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
615
    
616
617
618
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
619

620
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
621
  {
622
    FUNCNAME("ProblemStat::refineMesh()");
623

Thomas Witkowski's avatar
Thomas Witkowski committed
624
    int nMeshes = static_cast<int>(meshes.size());
625
    Flag refineFlag = 0;
626
627
628
629
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

630
631
632
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
633

634
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
635
  {
636
    FUNCNAME("ProblemStat::coarsenMesh()");
637

Thomas Witkowski's avatar
Thomas Witkowski committed
638
    int nMeshes = static_cast<int>(meshes.size());
639
    Flag coarsenFlag = 0;
640
641
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
642
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
643
644
645
646

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
647

648
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
649
  {
650
    FUNCNAME("ProblemStat::oneIteration()");
651

652
653
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
654
 	adaptInfo->allowRefinement(false, i);
655
      else
656
 	adaptInfo->allowRefinement(true, i);	
657
658
659
660

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

Thomas Witkowski's avatar
Thomas Witkowski committed
661

662
663
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
664
  {
665
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
666

667
668
669
//     buildAfterCoarsen_sebastianMode(adaptInfo, flag);
//     return;

670
    if (dualMeshTraverseRequired()) {
671
672
673
674
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

675
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
676
677
      return;
    }
678

679
680
681
682
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

683

Thomas Witkowski's avatar
Thomas Witkowski committed
684
685
686
687
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


688
689
690
691
692
693
694
695
696
#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
697

698
699
700

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
701
702
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
703
704
705
706
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
707
708
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
709
    if (useGetBound)
710
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
711
712

  
Thomas Witkowski's avatar
Thomas Witkowski committed
713
    traverseInfo.updateStatus();
714
715
716
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

719
      MSG("%d DOFs for %s\n", 
720
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
721
722
723
724
	  componentSpaces[i]->getName().c_str());

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

725
      for (int j = 0; j < nComponents; j++) {
726

727
728
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
729

730
731
732
733
	// 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).
734
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
735

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

751
752
753
	if (matrix) 
	  matrix->calculateNnz();
	
754
755
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
756
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
757
	  assembleMatrix = false;
758
759
760
761
762
763
764
	} else if (matrix) {
	  matrix->getBaseMatrix().
	    change_dim(componentSpaces[i]->getAdmin()->getUsedSize(),