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

namespace AMDiS {

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

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

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

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

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

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

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

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

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

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

129
130
131
      }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

220
221
	deserialize(in);
	in.close();
222
#endif
223
224

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

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

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

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

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

	// === do global refinements ===
253

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

    doOtherStuff();
  }

263

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

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

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

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


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

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

326

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

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

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

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

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

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

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

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

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

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

377

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

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

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

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

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

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

405

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

    // === create solver ===
411
    string solverType("0");
412
413
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
414
    OEMSolverCreator *solverCreator = 
415
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
416
    TEST_EXIT(solverCreator)("no solver type\n");
417
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
418
419
    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 = 
441
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
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
    }

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

523

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

528

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

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

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

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

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

551

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

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

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

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

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

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

594

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
610

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

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

621
622
623
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
624

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

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

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
638

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
652

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

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

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

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

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

674

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


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

689
690
691

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

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

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

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

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

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

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

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

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

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

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

	  set_to_zero(matrix->getBaseMatrix());
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
756
757

	// If there is no DOFMatrix, e.g., if it is completly 0, do not assemble.