ProblemStat.cc 52.3 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
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
82
    if (meshes.size() == 0) 
83
84
      WARNING("no mesh created\n");

85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
    // === 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");

106
    // === create fespace ===
107
    if (feSpaces.size() != 0) {
108
109
110
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
111
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
112
	createFeSpace(NULL);
113

114
115
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
116
	feSpaces = adoptProblem->getFeSpaces();
117
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
118
	traverseInfo = adoptProblem->traverseInfo;
119
120
121

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

130
131
132
      }
    }

133
    if (feSpaces.size() == 0) 
134
135
136
      WARNING("no feSpace created\n");

    // === create system ===
137
    if (initFlag.isSet(INIT_SYSTEM)) 
138
      createMatricesAndVectors();
139
    
140
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
141
      solution = adoptProblem->getSolution();
142
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
143
      systemMatrix = adoptProblem->getSystemMatrix();
144
145
146
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
159
    if (!solver) 
160
161
162
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
163
    if (initFlag.isSet(INIT_ESTIMATOR))
164
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
165
166

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
167
      estimator = adoptProblem->getEstimators();
168
169

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
170
    if (initFlag.isSet(INIT_MARKER))
171
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
172

Thomas Witkowski's avatar
Thomas Witkowski committed
173
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
174
      marker = adoptProblem->getMarkers();
175
176

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

    int readSerialization = 0;
189
190
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
191
192
193
194
195
196

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

197
198
199
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
200
201
202
203
204
205

      // 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) {
206
207
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
208
209
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

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

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

221
222
	deserialize(in);
	in.close();
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
    string solverType("0");
413
414
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
415
    OEMSolverCreator *solverCreator = 
416
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
417
    TEST_EXIT(solverCreator)("no solver type\n");
418
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
419
420
    solver = solverCreator->create();
    solver->initParameters();
421
422
  }

423

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

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

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

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


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

459

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

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

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

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

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

481

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

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

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

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

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

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

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

    int writeSerialization = 0;
519
    Parameters::get(name + "->output->write serialization", writeSerialization);
520
    if (writeSerialization)
521
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
522
523
  }

524

525
  void ProblemStatSeq::doOtherStuff()
526
527
528
  {
  }

529

530
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool, bool)
531
532
533
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
534
    if (!solver) {
535
536
537
538
539
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
540
    
541
    solver->solveSystem(solverMatrix, *solution, *rhs);
542

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

546
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
547
548
549
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
550
551
  }

552

553
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
554
  {
555
    FUNCNAME("ProblemStat::estimate()");
556

557
558
559
560
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
561
562
#endif

563
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
564
      computeError(adaptInfo);
565
566
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
567
	Estimator *scalEstimator = estimator[i];
568
569
	
	if (scalEstimator) {
570
571
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
572
	  scalEstimator->estimate(adaptInfo->getTimestep());
573

574
575
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
576
577
578
579
580

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
581
	}
582
583
584
      }
    }

585
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
586
    MPI::COMM_WORLD.Barrier();
587
588
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
589
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
590
591
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
592
#endif
593
594
  }

595

596
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
597
  {
598
    FUNCNAME("ProblemStat::markElements()");
599

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

603
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
604
    for (int i = 0; i < nComponents; i++)
605
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
606
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
607
    
608
609
610
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
611

612
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
613
  {
614
    FUNCNAME("ProblemStat::refineMesh()");
615

Thomas Witkowski's avatar
Thomas Witkowski committed
616
    int nMeshes = static_cast<int>(meshes.size());
617
    Flag refineFlag = 0;
618
619
620
621
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

622
623
624
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
625

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

Thomas Witkowski's avatar
Thomas Witkowski committed
630
    int nMeshes = static_cast<int>(meshes.size());
631
    Flag coarsenFlag = 0;
632
633
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
634
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
635
636
637
638

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
639

640
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
641
  {
642
    FUNCNAME("ProblemStat::oneIteration()");
643

644
645
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
646
 	adaptInfo->allowRefinement(false, i);
647
      else
648
 	adaptInfo->allowRefinement(true, i);	
649
650
651
652

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

Thomas Witkowski's avatar
Thomas Witkowski committed
653

654
655
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
656
  {
657
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
658

659
660
661
//     buildAfterCoarsen_sebastianMode(adaptInfo, flag);
//     return;

662
    if (dualMeshTraverseRequired()) {
663
664
665
666
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

667
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
668
669
      return;
    }
670

671
672
673
674
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

675

Thomas Witkowski's avatar
Thomas Witkowski committed
676
677
678
679
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


680
681
682
683
684
685
686
687
688
#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
689

690
691
692

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
693
694
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
695
696
697
698
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
699
700
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
701
    if (useGetBound)
702
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
703
704

  
Thomas Witkowski's avatar
Thomas Witkowski committed
705
    traverseInfo.updateStatus();
706
707
708
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

711
      MSG("%d DOFs for %s\n", 
712
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
713
714
715
716
	  componentSpaces[i]->getName().c_str());

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

717
      for (int j = 0; j < nComponents; j++) {
718

719
720
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
721

722
723
724
725
	// 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).
726
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
727

728
	if (writeAsmInfo && matrix) {
729
	  for (vector<Operator*>::iterator it = matrix->getOperatorsBegin();
730
731
732
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
733
	      cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << endl;
734
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
735
	      cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << endl;
736
	    if (assembler->getFirstOrderAssembler(GRD_PHI)) 
737
	      cout << "FOA GRD_PHI: " << assembler->getFirstOrderAssembler(GRD_PHI)->getName() << endl;
738
	    if (assembler->getSecondOrderAssembler()) 
739
	      cout << "SOA: " << assembler->getSecondOrderAssembler()->getName() << endl;
740
741
742
	  }
	}

743
744
745
	if (matrix) 
	  matrix->calculateNnz();
	
746
747
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
748
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
749
	  assembleMatrix = false;
750
751
752
753
754
755
756
	} else if (matrix) {
	  matrix->getBaseMatrix().
	    change_dim(componentSpaces[i]->getAdmin()->getUsedSize(), 
		       componentSpaces[j]->getAdmin()->getUsedSize());

	  set_to_zero(matrix->getBaseMatrix());
	}