ProblemVec.cc 42.4 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
#include <sstream>
2
#include "boost/lexical_cast.hpp"
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "ProblemVec.h"
#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"
#include "FileWriter.h"
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
17
#include "DualTraverse.h"
18
19
20
21
22
23
#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
24
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
25
#include "TraverseParallel.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
26
#include "VtkWriter.h"
27
#include "ValueReader.h"
28
29
30

namespace AMDiS {

31
32
  using boost::lexical_cast;

33
34
35
36
37
  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
38

39
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
40
    if (meshes.size() != 0) { 
41
42
43
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
44
	  (!adoptFlag.isSet(INIT_MESH) &&
45
46
47
48
49
50
51
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE)))) {
	createMesh();
      } 
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_MESH) || 
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
52
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
53
	componentMeshes = adoptProblem->componentMeshes;
Thomas Witkowski's avatar
Thomas Witkowski committed
54
55
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
56
57
58

	// 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
59
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
60
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
61
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
62
	  componentMeshes.resize(nComponents);
63
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
64
	    componentMeshes[i] = componentMeshes[0];
65
66
	}

67
68
69
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
70
    if (meshes.size() == 0) 
71
72
73
      WARNING("no mesh created\n");

    // === create fespace ===
74
    if (feSpaces.size() != 0) {
75
76
77
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
78
79
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
	createFESpace(NULL);
80

81
82
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
83
84
	feSpaces = adoptProblem->getFESpaces();
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
85
	traverseInfo = adoptProblem->traverseInfo;
86
87
88

	// 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
89
90
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
91
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
92
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
93
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
94
	    componentSpaces[i] = componentSpaces[0];
95
96
	}

97
98
99
      }
    }

100
    if (feSpaces.size() == 0) 
101
102
103
      WARNING("no feSpace created\n");

    // === create system ===
104
    if (initFlag.isSet(INIT_SYSTEM)) 
105
      createMatricesAndVectors();
106
    
107
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
108
109
110
      solution = adoptProblem->getSolution();
      rhs = adoptProblem->getRHS();
      systemMatrix = adoptProblem->getSystemMatrix();
111
112
113
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
114
    if (solver) {
115
116
117
118
119
120
      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
121
122
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
123
124
125
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
126
    if (!solver) 
127
128
129
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
130
    if (initFlag.isSet(INIT_ESTIMATOR))
131
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
132
133

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
Thomas Witkowski's avatar
Thomas Witkowski committed
134
      estimator = adoptProblem->getEstimator();
135
136

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
137
    if (initFlag.isSet(INIT_MARKER))
138
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
139
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
140
      marker = adoptProblem->getMarker();
141
142

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
143
    if (initFlag.isSet(INIT_FILEWRITER))
144
145
146
147
148
149
150
151
152
153
154
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
    // 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.

    int readSerialization = 0;
155
    std::string serializationFilename = "";
156
157
158
159
160
161
162
    GET_PARAMETER(0, "argv->rs", &serializationFilename);

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

163
      GET_PARAMETER(0, name + "->input->read serialization", "%d", &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
164
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
165
166
167
168
169
170
171
		    &readSerializationWithAdaptInfo);

      // 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) {
Thomas Witkowski's avatar
Thomas Witkowski committed
172
	GET_PARAMETER(0, name + "->input->serialization filename", 
173
174
175
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

176
177
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
178
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
179
	std::ifstream in(serializationFilename.c_str());
180
181
	deserialize(in);
	in.close();
182
183
184

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
#endif
185
      } else {
186
	int globalRefinements = 0;
187
188
189
190
191

	// If AMDiS is compied for parallel computations, the global refinements are
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
192
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
193
		      &globalRefinements);
194
#endif
195

196
	// Initialize the meshes if there is no serialization file.
197
198
199
200
201
	for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
	  bool initMesh = initFlag.isSet(INIT_MESH) ||
	    (adoptProblem && adoptFlag.isSet(INIT_MESH));

	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
202
	    meshes[i]->initialize();	    
203
	}
204
205
206
207

	// === read value file and use it for the mesh values ===
	std::string valueFilename("");
	GET_PARAMETER(0, meshes[0]->getName() + "->value file name", &valueFilename); 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
208
	if (valueFilename.length()) {
209
210
211
212
213
214
215
216
217
218
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());
	  meshes[0]->clearMacroFileInfo();
	}

	// === do global refinements ===
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
	  if (initFlag.isSet(INIT_MESH) && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
219
	    refinementManager->globalRefine(meshes[i], globalRefinements);
220
221
222
223
224
225
226
227
228
229
      }
    }

    doOtherStuff();
  }

  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
230
    componentMeshes.resize(nComponents);
231
    std::map<int, Mesh*> meshForRefinementSet;
232

233
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
234
    GET_PARAMETER(0, name + "->mesh", &meshName);
235
    TEST_EXIT(meshName != "")("no mesh name specified\n");
236
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
237
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
238
    TEST_EXIT(dim)("no problem dimension specified!\n");
239

240
    for (int i = 0; i < nComponents; i++) {
241
      int refSet = -1;
242
243
      GET_PARAMETER(0, name + "->refinement set[" + 
		       lexical_cast<std::string>(i) + "]", "%d", &refSet);
244
      if (refSet < 0)
245
	refSet = 0;
246
      
247
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
248
	Mesh *newMesh = new Mesh(meshName, dim);
249
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
250
251
	meshes.push_back(newMesh);
	nMeshes++;
252
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
253
      componentMeshes[i] = meshForRefinementSet[refSet];
254
255
256
    }
    switch(dim) {
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
257
258
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
259
260
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
263
264
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
265
266
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
267
268
269
270
271
272
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

273
  void ProblemVec::createFESpace(DOFAdmin *admin)
274
275
276
  {
    FUNCNAME("ProblemVec::createFESpace()");

277
    std::map<std::pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
278
    int dim = -1;
Thomas Witkowski's avatar
Thomas Witkowski committed
279
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
280
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
281

282
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
283
    traverseInfo.resize(nComponents);
284

285
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
286
      int degree = 1;
287
288
      GET_PARAMETER(0, name + "->polynomial degree[" + 
		    boost::lexical_cast<std::string>(i) + "]","%d", &degree);
289

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

Thomas Witkowski's avatar
Thomas Witkowski committed
292
      if (feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
293
294
295
	stringstream s;
	s << name << "->feSpace[" << i << "]";

296
	FiniteElemSpace *newFESpace = 
297
	  FiniteElemSpace::provideFESpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
298
					  componentMeshes[i], s.str());
Thomas Witkowski's avatar
Thomas Witkowski committed
299
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFESpace;
300
	feSpaces.push_back(newFESpace);
301
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
302
303
304
305
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

    for (int i = 0; i < nComponents; i++) {
306
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
307
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
308
      
Thomas Witkowski's avatar
Thomas Witkowski committed
309
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
310
311
312
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
313
314
315
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      if (meshes[i]->getNumberOfDOFs(VERTEX) == 0) {
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
316
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
317
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
318
319
320
321
322
323
324
325
326
327
      }
    }
  }

  void ProblemVec::createMatricesAndVectors()
  {
    FUNCNAME("ProblemVec::createMatricesAndVectors()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
328
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
329
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
330
331
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
332

Thomas Witkowski's avatar
Thomas Witkowski committed
333
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
334
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
335
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
336
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
337

338
      std::string numberedName = "rhs[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
339
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
340
      numberedName = name + boost::lexical_cast<std::string>(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
343
344
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
345
346
347
348
349
350
351
352
    }
  }

  void ProblemVec::createSolver()
  {
    FUNCNAME("ProblemVec::createSolver()");

    // === create solver ===
353
    std::string solverType("0");
Thomas Witkowski's avatar
Thomas Witkowski committed
354
    GET_PARAMETER(0, name + "->solver", &solverType);
355
356
    OEMSolverCreator *solverCreator = 
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType));
357
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
358
359
360
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
361
362
363
364
365
366
367
  }

  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

    // create and set leaf data prototype
368
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
369
      meshes[i]->setElementDataPrototype
Thomas Witkowski's avatar
Thomas Witkowski committed
370
	(new LeafDataEstimatableVec(new LeafDataCoarsenableVec));
371

Thomas Witkowski's avatar
Thomas Witkowski committed
372
373
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
374
375
      std::string estName = 
	name + "->estimator[" + boost::lexical_cast<std::string>(i) + "]";
376
377

      // === create estimator ===
378
      std::string estimatorType("0");
379
380
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
381
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
382
      if (estimatorCreator) {
383
384
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
385
	if (estimatorType == "recovery") {
386
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
387
	    setSolution(solution->getDOFVector(i));
388
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
389
	estimator[i] = estimatorCreator->create();
390
391
392
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
393
      if (estimator[i]) {
394
	for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
395
396
397
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				   solution->getDOFVector(j), 
				   rhs->getDOFVector(j));
398
399
400
401
402
403
404
405
      }
    }
  }

  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

Thomas Witkowski's avatar
Thomas Witkowski committed
406
    int nMarkersCreated = 0;
407

408
    for (int i = 0; i < nComponents; i++) {
409
410
411
      marker[i] = Marker::createMarker
	(name + "->marker[" + boost::lexical_cast<std::string>(i) + "]", i);

412
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
413
414
415
416
	nMarkersCreated++;

	// If there is more than one marker, and all components are defined
	// on the same mesh, the maximum marking has to be enabled.
417
 	if (nMarkersCreated > 1 && nMeshes == 1)
Thomas Witkowski's avatar
Thomas Witkowski committed
418
 	  marker[i]->setMaximumMarking(true);
419
420
421
422
423
424
425
426
427
      }
    }
  }

  void ProblemVec::createFileWriter()
  {
    FUNCNAME("ProblemVec::createFileWriter()");
  
    // Create one filewriter for all components of the problem
Thomas Witkowski's avatar
Thomas Witkowski committed
428
    std::string numberedName  = name + "->output";
429
    std::string filename = "";
430
431
432
    GET_PARAMETER(0, numberedName + "->filename", &filename);

    if (filename != "") {
433
      std::vector< DOFVector<double>* > solutionList(nComponents);
434

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

Thomas Witkowski's avatar
Thomas Witkowski committed
439
	solutionList[i] = solution->getDOFVector(i);
440
441
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
442
      fileWriters.push_back(new FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
443
					    componentMeshes[0],
444
445
446
447
					    solutionList));
    }

    // Create own filewriters for each components of the problem
448
    for (int i = 0; i < nComponents; i++) {
449
      numberedName = name + "->output[" + boost::lexical_cast<std::string>(i) + "]";
450
451
452
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

453
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
454
	fileWriters.push_back(new FileWriter(numberedName, 
455
456
					     componentMeshes[i], 
					     solution->getDOFVector(i)));
457
458
459
460
    }

    // Check for serializer
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
461
    GET_PARAMETER(0, name + "->write serialization", "%d", &writeSerialization);
462
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
463
464
      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());
465
466
467
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
468
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
469
470
471
472

    // Serialization is not allowed to be done by the problem, if its part of a parallel
    // problem definition. Than, the parallel problem object must be serialized.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
473
    if (writeSerialization)
Thomas Witkowski's avatar
Thomas Witkowski committed
474
      fileWriters.push_back(new Serializer<ProblemVec>(this));
475
#endif
476
477
478
479
480
481
  }

  void ProblemVec::doOtherStuff()
  {
  }

482
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
483
484
485
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
486
    if (!solver) {
487
488
489
490
491
492
493
494
495
      WARNING("no solver\n");
      return;
    }

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

    clock_t first = clock();
496
    solver->solveSystem(solverMatrix, *solution, *rhs);
497

498
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
499
500
    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);
501
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
502
503
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
504
505
#endif

506
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
507
508
509
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
510
511
512
513
514
515
516
517
  }

  void ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

    clock_t first = clock();

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

522
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
523
      computeError(adaptInfo);
524
525
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
526
	Estimator *scalEstimator = estimator[i];
527
528
529
530
531
532
533
534
535
536
	
	if (scalEstimator) {
	  scalEstimator->estimate(adaptInfo->getTimestep());
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
	  adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	  adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	} else {
	  WARNING("no estimator for component %d\n" , i);
	}
537
538
539
      }
    }

540
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
541
542
    INFO(info, 8)("estimation of the error needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
543
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
544
545
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
546
547
548

#endif

549
550
551
552
553
554
555
556
557
558
559
  }

  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

    // to enforce albert-like behavior: refinement even if space tolerance
    // here is reached already because of time adaption
    allowFirstRefinement();

    Flag markFlag = 0;
560
    for (int i = 0; i < nComponents; i++) {
561
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
562
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
563
564
565
566
      } else {
	WARNING("no marker for component %d\n", i);
      }
    }
567
    
568
569
570
571
572
573
574
    return markFlag;
  }

  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
575
    int nMeshes = static_cast<int>(meshes.size());
576
    Flag refineFlag = 0;
577
578
579
580
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

581
582
583
584
585
586
587
    return refineFlag;
  }

  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
588
    int nMeshes = static_cast<int>(meshes.size());
589
    Flag coarsenFlag = 0;
590
591
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
592
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
593
594
595
596
597
598
599
600

    return coarsenFlag;
  }

  Flag ProblemVec::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    FUNCNAME("ProblemVec::oneIteration()");

Thomas Witkowski's avatar
Thomas Witkowski committed
601
    if (allowFirstRef) {
Thomas Witkowski's avatar
Thomas Witkowski committed
602
      for (int i = 0; i < nComponents; i++)
603
	adaptInfo->allowRefinement(true, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
604

Thomas Witkowski's avatar
Thomas Witkowski committed
605
      allowFirstRef = false;
606
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
607
608
      for (int i = 0; i < nComponents; i++)
	if (adaptInfo->spaceToleranceReached(i))
609
	  adaptInfo->allowRefinement(false, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
610
	else
611
612
613
614
615
616
	  adaptInfo->allowRefinement(true, i);	
    }

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

617
618
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
619
620
621
622
623
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

624
625
626
627
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

628
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
629
      meshes[i]->dofCompress();
630
631
632

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
633
634
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
635
636
637
638
639
640
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
641
    if (useGetBound)
642
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
643

Thomas Witkowski's avatar
Thomas Witkowski committed
644
645
    traverseInfo.updateStatus();

646
647
648
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
649
    for (int i = 0; i < nComponents; i++) {
650
651
652
653
654
655
656
657

#if 0
      std::vector<int> nnzInRow;
      int overallNnz, averageNnz;
      componentSpaces[i]->getMesh()->computeMatrixFillin(componentSpaces[i],
							 nnzInRow, overallNnz, averageNnz);     
#endif

658
659
660
661
662
663
      MSG("%d DOFs for %s\n", 
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());

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

664
      for (int j = 0; j < nComponents; j++) {
665

666
667
668
669
	// 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).
Thomas Witkowski's avatar
Thomas Witkowski committed
670
	DOFMatrix *matrix = (*systemMatrix)[i][j];
671

672
673
674
	if (matrix) 
	  matrix->calculateNnz();
	
675
676
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
677
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
678
	  assembleMatrix = false;
679
680
681
682
683
684
685
	} 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
686
687

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

691
692
693
	// 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.
694
695
696
697
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

698
	  continue;
699
	}
700

701
702
703
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

Thomas Witkowski's avatar
Thomas Witkowski committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
	if (traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_NO_AUX ||
	    traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_WITH_AUX) {

	  // Row fe space and col fe space are both equal

	  if (traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_NO_AUX ||
	      traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_WITH_AUX) {

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

	  } else if (traverseInfo.getStatus(i) == SingleComponentInfo::EQ_SPACES_WITH_DIF_AUX) {

	    // Row fe space and col fe space are both equal, but right hand side has at
723
	    // least one another aux fe space. 
Thomas Witkowski's avatar
Thomas Witkowski committed
724
725
726
727
728
729

	    assembleOnOneMesh(componentSpaces[i],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);

730
731
	    assembleOnDifMeshes2(componentSpaces[i], 
				 traverseInfo.getAuxFESpace(i, j),
Thomas Witkowski's avatar
Thomas Witkowski committed
732
733
				 assembleFlag,
				 NULL,
734
				 ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
735
736
737
738
739
740
741

	  } else {
	    ERROR_EXIT("Possible? If yes, not yet implemented!\n");
	  }

	} else if (traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_WITH_DIF_AUX) {
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
742
743
744
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
745
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
746
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
747
748
749
750
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
751
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
752
753
754
755

	} else if (traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_NO_AUX ||
		   traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_WITH_AUX) {

Thomas Witkowski's avatar
Thomas Witkowski committed
756
757
758
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
759
760
761
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  
	} else {
	  ERROR_EXIT("Not yet implemented!\n");
762
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
763

Thomas Witkowski's avatar
Thomas Witkowski committed
764
	assembledMatrix[i][j] = true;
765
766
767

	if (assembleMatrix)
	  matrix->finishInsertion();
768
769

 	if (assembleMatrix && matrix->getBoundaryManager())
770
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
771
772
	
	if (matrix)
773
	  nnz += matrix->getBaseMatrix().nnz();	  
774
775
      }

776
      // And now assemble boundary conditions on the vectors
Thomas Witkowski's avatar
Thomas Witkowski committed
777
778
      assembleBoundaryConditions(rhs->getDOFVector(i),
				 solution->getDOFVector(i),
779
				 componentMeshes[i],
780
				 assembleFlag);     
781
    }
782

783
    solverMatrix.setMatrix(*systemMatrix);
784

Thomas Witkowski's avatar
Thomas Witkowski committed
785
    createPrecon();
786

787
    INFO(info, 8)("fillin of assembled matrix: %d\n", nnz);
788

789
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
790
791
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
792
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
793
794
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
Thomas Witkowski's avatar
Thomas Witkowski committed
795
#endif     
796
797
  }

798
799
  void ProblemVec::createPrecon()
  {
800
    std::string preconType("no");
801
802
803
804
805
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);

    CreatorInterface<ITL_BasePreconditioner> *preconCreator = 
      CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);

Thomas Witkowski's avatar
Thomas Witkowski committed
806
    solver->setLeftPrecon(preconCreator->create(solverMatrix.getMatrix()));
807
808
809
810
811

    preconType= "no";
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);

    preconCreator = CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);
Thomas Witkowski's avatar
Thomas Witkowski committed
812
    solver->setRightPrecon(preconCreator->create(solverMatrix.getMatrix()));
813
814
815
  }


816
817
818
819
  void ProblemVec::writeFiles(AdaptInfo *adaptInfo, bool force) 
  {
    FUNCNAME("ProblemVec::writeFiles()");

820
821
822
823
824
825
826
    clock_t first = clock();

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
827
    int size = static_cast<int>(fileWriters.size());
828
829
830
831
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 1)
#endif
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
832
      fileWriters[i]->writeFiles(adaptInfo, force);
833
    }
834
835
    
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
836
837
838
    INFO(info, 8)("writeFiles needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()),
		  omp_get_wtime() - wtime);
839
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
840
841
    INFO(info, 8)("writeFiles needed %.5f seconds\n",
		  TIME_USED(first, clock()));
842
#endif
843
844
  }

845
846
847
848
849
  void ProblemVec::writeFiles(AdaptInfo &adaptInfo, bool force) 
  {
    writeFiles(&adaptInfo, force);
  }

850
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
851
852
853
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
854
    solution->interpol(fct);
855
856
857
858
859
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
860
				     double *estFactor)
861
862
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");
Thomas Witkowski's avatar
Thomas Witkowski committed
863
   
Thomas Witkowski's avatar
Thomas Witkowski committed
864
    if (!(*systemMatrix)[i][j]) {
865
      TEST_EXIT(i != j)("should have been created already\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
866
      (*systemMatrix)[i][j] = new DOFMatrix(componentSpaces[i], componentSpaces[j], "");
Thomas Witkowski's avatar
Thomas Witkowski committed
867
868
869
      (*systemMatrix)[i][j]->setCoupleMatrix(true);
      (*systemMatrix)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix)[i][i]->getBoundaryManager()->
870
				getBoundaryConditionMap());
Thomas Witkowski's avatar
Thomas Witkowski committed
871
872
873

      if (estimator[i])
	estimator[i]->setNewMatrix(j, (*systemMatrix)[i][j]);
874
    }    
Thomas Witkowski's avatar
Thomas Witkowski committed
875

876
    (*systemMatrix)[i][j]->addOperator(op, factor, estFactor);
Thomas Witkowski's avatar
Thomas Witkowski committed
877

878
    traverseInfo.getMatrix(i, j).setAuxFESpaces(op->getAuxFESpaces()); 
879
      
880
881
882
883
884
885
    for (int k = 0; k < static_cast<int>(op->getAuxFESpaces().size()); k++) {
      if ((op->getAuxFESpaces())[k]->getMesh() != componentSpaces[i]->getMesh() ||
	  (op->getAuxFESpaces())[k]->getMesh() != componentSpaces[j]->getMesh()) {
	op->setNeedDualTraverse(