ProblemStat.cc 57.5 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
#include "TraverseParallel.h"
39
40
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
41
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
42
#include "Debug.h"
43
44
45

namespace AMDiS {

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

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

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

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

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

83
84
85
86

	// If the problem adopts the mesh but creates an own FE space.
	if (initFlag.isSet(INIT_FE_SPACE)) {
	}
87
88
89
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
90
    if (meshes.size() == 0) 
91
92
93
      WARNING("no mesh created\n");

    // === create fespace ===
94
    if (feSpaces.size() != 0) {
95
96
97
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
98
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
99
	createFeSpace(NULL);
100

101
102
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
103
	feSpaces = adoptProblem->getFeSpaces();
104
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
105
	traverseInfo = adoptProblem->traverseInfo;
106
107
108

	// 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
109
110
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
111
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
112
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
113
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
114
	    componentSpaces[i] = componentSpaces[0];
115
116
	}

117
118
119
      }
    }

120
    if (feSpaces.size() == 0) 
121
122
123
      WARNING("no feSpace created\n");

    // === create system ===
124
    if (initFlag.isSet(INIT_SYSTEM)) 
125
      createMatricesAndVectors();
126
    
127
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
128
      solution = adoptProblem->getSolution();
129
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
130
      systemMatrix = adoptProblem->getSystemMatrix();
131
132
133
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
134
    if (solver) {
135
136
137
138
139
140
      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
141
142
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
143
144
145
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
146
    if (!solver) 
147
148
149
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
150
    if (initFlag.isSet(INIT_ESTIMATOR))
151
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
152
153

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
154
      estimator = adoptProblem->getEstimators();
155
156

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
157
    if (initFlag.isSet(INIT_MARKER))
158
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
159

Thomas Witkowski's avatar
Thomas Witkowski committed
160
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
161
      marker = adoptProblem->getMarkers();
162
163

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
164
    if (initFlag.isSet(INIT_FILEWRITER))
165
166
167
168
169
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
170
171
172
173
    // 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.
174
175

    int readSerialization = 0;
176
177
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
178
179
180
181
182
183

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

184
185
186
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
187
188
189
190
191
192

      // 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) {
193
194
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
195
196
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

197
198
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
199
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
200
	ifstream in(serializationFilename.c_str());
201
202
	deserialize(in);
	in.close();
203
204
205

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
#endif
206
207

	deserialized = true;
208
      } else {
209
	int globalRefinements = 0;
210

211
	// If AMDiS is compiled for parallel computations, the global refinements are
212
213
214
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
215
216
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
217
#endif
218

219
	bool initMesh = initFlag.isSet(INIT_MESH);
220

Thomas Witkowski's avatar
Thomas Witkowski committed
221
222
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
223
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	    meshes[i]->initialize();	    	
225
226

	// === read value file and use it for the mesh values ===
227
228
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
229
	if (valueFilename.length())
230
231
232
233
234
235
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
236

Thomas Witkowski's avatar
Thomas Witkowski committed
237
	for (unsigned int i = 0; i < meshes.size(); i++)
238
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
239
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
240
241
242
243
244
245
      }
    }

    doOtherStuff();
  }

246

247
  void ProblemStatSeq::createMesh() 
248
  {
249
    FUNCNAME("ProblemStat::createMesh()");
250

Thomas Witkowski's avatar
Thomas Witkowski committed
251
    componentMeshes.resize(nComponents);
252
253
254
255
256
257
    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());
258
259

    int dim = 0;
260
261
262
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
263

264
    for (int i = 0; i < nComponents; i++) {
265
      int refSet = -1;
266
267
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
268
      if (refSet < 0)
269
	refSet = 0;
270
      
271
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
272
	Mesh *newMesh = new Mesh(meshName, dim);
273
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
274
275
	meshes.push_back(newMesh);
	nMeshes++;
276
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
277
      componentMeshes[i] = meshForRefinementSet[refSet];
278
    }
279
280

    switch (dim) {
281
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
284
285
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
288
289
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
290
291
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
292
293
294
295
296
297
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

298

299
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
300
  {
301
    FUNCNAME("ProblemStat::createFeSpace()");
302

303
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
304
    int dim = -1;
305
    Parameters::get(name + "->dim", dim);
306
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
307

308
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
309
    traverseInfo.resize(nComponents);
310

311
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
312
      int degree = 1;
313
314
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
315

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

318
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
319
320
321
	stringstream s;
	s << name << "->feSpace[" << i << "]";

322
323
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
324
					  componentMeshes[i], s.str());
325
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
326
	feSpaces.push_back(newFeSpace);
327
      }
328

329
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
332
    }

    for (int i = 0; i < nComponents; i++) {
333
      for (int j = 0; j < nComponents; j++)
334
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
335
      
336
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
337
338
339
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
340
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
341
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
342
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
343
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
344
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
345
346
347
348
      }
    }
  }

349

350
  void ProblemStatSeq::createMatricesAndVectors()
351
  {
352
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
353
354
355

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

Thomas Witkowski's avatar
Thomas Witkowski committed
356
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
357
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
358
359
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
360

Thomas Witkowski's avatar
Thomas Witkowski committed
361
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
362
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
363
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
364
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
365

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

369
      numberedName = "solution[" + boost::lexical_cast<string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
370
371
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
372
373
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
374
375
376
    }
  }

377

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

    // === create solver ===
383
384
    string solverType("0");
    Parameters::get(name + "->solver", solverType);
385
386
    OEMSolverCreator *solverCreator = 
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType));
387
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
388
389
390
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
391
392
  }

393

394
  void ProblemStatSeq::createEstimator()
395
  {
396
    FUNCNAME("ProblemStat::createEstimator()");
397
398

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

Thomas Witkowski's avatar
Thomas Witkowski committed
403
404
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
405
406
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
407
408

      // === create estimator ===
409
410
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
411
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.    
Thomas Witkowski committed
412
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
413
      if (estimatorCreator) {
414
415
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
416
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
417
	estimator[i] = estimatorCreator->create();
418
419
420
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
421
422
423
424
425
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
426
427
428
    }
  }

429

430
  void ProblemStatSeq::createMarker()
431
  {
432
    FUNCNAME("ProblemStat::createMarker()");
433

Thomas Witkowski's avatar
Thomas Witkowski committed
434
    int nMarkersCreated = 0;
435

436
    for (int i = 0; i < nComponents; i++) {
437
      marker[i] = Marker::createMarker
438
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
439

440
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
441
442
443
444
	nMarkersCreated++;

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

451

452
  void ProblemStatSeq::createFileWriter()
453
  {
454
    FUNCNAME("ProblemStat::createFileWriter()");
455
456
  
    // Create one filewriter for all components of the problem
457
458
459
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
460
461

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
468
	solutionList[i] = solution->getDOFVector(i);
469
470
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
471
      fileWriters.push_back(new FileWriter(numberedName,
472
473
					   componentMeshes[0],
					   solutionList));
474
475
476
    }

    // Create own filewriters for each components of the problem
477
    for (int i = 0; i < nComponents; i++) {
478
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
479
      filename = "";
480
      Parameters::get(numberedName + "->filename", filename);
481

482
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
483
	fileWriters.push_back(new FileWriter(numberedName, 
484
					     componentMeshes[i], 
485
					     solution->getDOFVector(i)));      
486
487
488
489
    }

    // Check for serializer
    int writeSerialization = 0;
490
    Parameters::get(name + "->write serialization", writeSerialization);
491
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
492
493
      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());
494
495
496
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

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

499
    if (writeSerialization)
500
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
501
502
  }

503

504
  void ProblemStatSeq::doOtherStuff()
505
506
507
  {
  }

508

509
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
510
511
512
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
513
    if (!solver) {
514
515
516
517
      WARNING("no solver\n");
      return;
    }

518
519
520
521
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

522
    clock_t first = clock();
523
    
524
    solver->solveSystem(solverMatrix, *solution, *rhs);
525

526
527
528
529
#ifdef _OPENMP
    INFO(info, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
530
531
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
532
#endif
533

534
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
535
536
537
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
538
539
  }

540

541
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
542
  {
543
    FUNCNAME("ProblemStat::estimate()");
544

545
546
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
547
548
549
#else
#ifdef _OPENMP
    double first = omp_get_wtime();
550
551
#else
    clock_t first = clock();
552
#endif
553
554
#endif

555
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
556
      computeError(adaptInfo);
557
558
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
559
	Estimator *scalEstimator = estimator[i];
560
561
	
	if (scalEstimator) {
562
563
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
564
	  scalEstimator->estimate(adaptInfo->getTimestep());
565

566
567
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
568
569
570
571
572

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
573
	}
574
575
576
      }
    }

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

592

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

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

600
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
601
    for (int i = 0; i < nComponents; i++) {
602
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
603
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
604
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
605
606
	WARNING("No marker for component %d\n", 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
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    MPI::COMM_WORLD.Barrier();
    INFO(info, 8)("dof compression needed %.5f seconds\n", 
		  MPI::Wtime() - first);

    first = MPI::Wtime();
686
687
688
#else
#ifdef _OPENMP
    double first = omp_get_wtime();
689
690
691
#else
    clock_t first = clock();
#endif
692
#endif
693

694
695
696

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

Thomas Witkowski's avatar
Thomas Witkowski committed
705
    if (useGetBound)
706
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
707
708

  
Thomas Witkowski's avatar
Thomas Witkowski committed
709
    traverseInfo.updateStatus();
710
711
712
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

715
      MSG("%d DOFs for %s\n", 
716
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
717
718
719
720
	  componentSpaces[i]->getName().c_str());

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

721
      for (int j = 0; j < nComponents; j++) {
722

723
724
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
725

726
727
728
729
	// 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).
730
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
731

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

747
748
749
	if (matrix) 
	  matrix->calculateNnz();
	
750
751
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
752
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
753
	  assembleMatrix = false;
754
755
756
757
758
759
760
	} 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
761
762

	// If there is no DOFMatrix, e.g., if it is completly 0, do not assemble.
763
	if (!matrix || !assembleMatrix)
764
765
	  assembleMatrix = false;

766
767
768
	// If the matrix should not be assembled, the rhs vector has to be considered.
	// This will be only done, if i == j. So, if both is not true, we can jump
	// to the next matrix.
769
770
771
772
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

773
	  continue;
774
	}
775

776
777
778
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

779
780
781
782
783
784
785
786
	// The simplest case: either the right hand side has no operaters, no aux
	// fe spaces, or all aux fe spaces are equal to the row and col fe space.
	assembleOnOneMesh(componentSpaces[i],
			  assembleFlag,
			  assembleMatrix ? matrix : NULL,
			  ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
      
	assembledMatrix[i][j] = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
787

788
789
	if (assembleMatrix)
	  matrix->finishInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
790

791
792
 	if (assembleMatrix && matrix->getBoundaryManager())
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
793

794
	if (matrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
795
	  nnz += matrix->getBaseMatrix().nnz();
796
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
797

798
799
      // And now assemble boundary conditions on the vectors
      assembleBoundaryConditions(rhs->getDOFVector(i),
Thomas Witkowski's avatar
Thomas Witkowski committed
800
801
802
 				 solution->getDOFVector(i),
 				 componentMeshes[i],
 				 assembleFlag);     
803
    }  
804

805
    if (asmMatrix) {
806
      solverMatrix.setMatrix(*systemMatrix);
807
      createPrecon();
Thomas Witkowski's avatar
Thomas Witkowski committed
808

809
      INFO(info, 8)("fillin of assembled matrix: %d\n", nnz);
810
    }
811