ProblemVec.cc 42.1 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
178
	// We AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem definition object.
#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;
Thomas Witkowski's avatar
Thomas Witkowski committed
187
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
188
189
		      &globalRefinements);

190
	// Initialize the meshes if there is no serialization file.
191
192
193
194
195
	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
196
	    meshes[i]->initialize();	    
197
	}
198
199
200
201

	// === 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
202
	if (valueFilename.length()) {
203
204
205
206
207
208
209
210
211
212
	  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
213
	    refinementManager->globalRefine(meshes[i], globalRefinements);
214
215
216
217
218
219
220
221
222
223
      }
    }

    doOtherStuff();
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
224
    componentMeshes.resize(nComponents);
225
    std::map<int, Mesh*> meshForRefinementSet;
226

227
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
228
    GET_PARAMETER(0, name + "->mesh", &meshName);
229
    TEST_EXIT(meshName != "")("no mesh name specified\n");
230
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
231
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
232
    TEST_EXIT(dim)("no problem dimension specified!\n");
233

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

267
  void ProblemVec::createFESpace(DOFAdmin *admin)
268
269
270
  {
    FUNCNAME("ProblemVec::createFESpace()");

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

276
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
277
    traverseInfo.resize(nComponents);
278

279
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
280
      int degree = 1;
281
282
      GET_PARAMETER(0, name + "->polynomial degree[" + 
		    boost::lexical_cast<std::string>(i) + "]","%d", &degree);
283

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

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

290
	FiniteElemSpace *newFESpace = 
291
	  FiniteElemSpace::provideFESpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
292
					  componentMeshes[i], s.str());
Thomas Witkowski's avatar
Thomas Witkowski committed
293
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFESpace;
294
	feSpaces.push_back(newFESpace);
295
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
296
297
298
299
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

    for (int i = 0; i < nComponents; i++) {
300
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
301
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
302
      
Thomas Witkowski's avatar
Thomas Witkowski committed
303
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
304
305
306
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
307
308
309
    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);
310
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
311
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
312
313
314
315
316
317
318
319
320
321
      }
    }
  }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
322
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
323
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
326

Thomas Witkowski's avatar
Thomas Witkowski committed
327
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
328
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
329
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
330
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
331

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
366
367
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
368
369
      std::string estName = 
	name + "->estimator[" + boost::lexical_cast<std::string>(i) + "]";
370
371

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


Thomas Witkowski's avatar
Thomas Witkowski committed
387
      if (estimator[i]) {
388
	for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
389
390
391
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				   solution->getDOFVector(j), 
				   rhs->getDOFVector(j));
392
393
394
395
396
397
398
399
      }
    }
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
400
    int nMarkersCreated = 0;
401

402
    for (int i = 0; i < nComponents; i++) {
403
404
405
      marker[i] = Marker::createMarker
	(name + "->marker[" + boost::lexical_cast<std::string>(i) + "]", i);

406
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
407
408
409
410
	nMarkersCreated++;

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
433
	solutionList[i] = solution->getDOFVector(i);
434
435
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
436
      fileWriters.push_back(new FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
437
					    componentMeshes[0],
438
439
440
441
					    solutionList));
    }

    // Create own filewriters for each components of the problem
442
    for (int i = 0; i < nComponents; i++) {
443
      numberedName = name + "->output[" + boost::lexical_cast<std::string>(i) + "]";
444
445
446
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

447
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
448
	fileWriters.push_back(new FileWriter(numberedName, 
449
450
					     componentMeshes[i], 
					     solution->getDOFVector(i)));
451
452
453
454
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
462
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
463
464
465
466

    // 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
467
    if (writeSerialization)
Thomas Witkowski's avatar
Thomas Witkowski committed
468
      fileWriters.push_back(new Serializer<ProblemVec>(this));
469
#endif
470
471
472
473
474
475
  }

  void ProblemVec::doOtherStuff()
  {
  }

476
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
477
478
479
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
480
    if (!solver) {
481
482
483
484
485
486
487
488
489
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
490
491
    int iter = solver->solveSystem(solverMatrix, *solution, *rhs);

492
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
493
494
    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);
495
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
496
497
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
498
499
#endif

500
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
501
502
503
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
504
505
506
507
508
509
510
511
  }

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

    clock_t first = clock();

512
513
514
515
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

516
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
517
      computeError(adaptInfo);
518
519
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
520
	Estimator *scalEstimator = estimator[i];
521
522
523
524
525
526
527
528
529
530
	
	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);
	}
531
532
533
      }
    }

534
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
535
536
    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);
537
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
538
539
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
540
541
542

#endif

543
544
545
546
547
548
549
550
551
552
553
  }

  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;
554
    for (int i = 0; i < nComponents; i++) {
555
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
556
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
557
558
559
560
      } else {
	WARNING("no marker for component %d\n", i);
      }
    }
561
    
562
563
564
565
566
567
568
    return markFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
569
    int nMeshes = static_cast<int>(meshes.size());
570
    Flag refineFlag = 0;
571
572
573
574
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

575
576
577
578
579
580
581
    return refineFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
582
    int nMeshes = static_cast<int>(meshes.size());
583
    Flag coarsenFlag = 0;
584
585
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
586
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
587
588
589
590
591
592
593
594

    return coarsenFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
595
    if (allowFirstRef) {
Thomas Witkowski's avatar
Thomas Witkowski committed
596
      for (int i = 0; i < nComponents; i++)
597
	adaptInfo->allowRefinement(true, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
598

Thomas Witkowski's avatar
Thomas Witkowski committed
599
      allowFirstRef = false;
600
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
601
602
      for (int i = 0; i < nComponents; i++)
	if (adaptInfo->spaceToleranceReached(i))
603
	  adaptInfo->allowRefinement(false, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
604
	else
605
606
607
608
609
610
	  adaptInfo->allowRefinement(true, i);	
    }

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

611
612
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
613
614
615
616
617
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

618
619
620
621
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

622
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
623
      meshes[i]->dofCompress();
624
625
626

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
627
628
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
629
630
631
632
633
634
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
635
    if (useGetBound)
636
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
637

Thomas Witkowski's avatar
Thomas Witkowski committed
638
639
    traverseInfo.updateStatus();

640
641
642
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
643
    for (int i = 0; i < nComponents; i++) {
644
645
646
647
648
649
650
651

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

652
653
654
655
656
657
      MSG("%d DOFs for %s\n", 
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());

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

658
      for (int j = 0; j < nComponents; j++) {
659

660
661
662
663
	// 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
664
	DOFMatrix *matrix = (*systemMatrix)[i][j];
665

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

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

685
686
687
	// 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.
688
689
690
691
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

692
	  continue;
693
	}
694

695
696
697
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

Thomas Witkowski's avatar
Thomas Witkowski committed
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
	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
717
	    // least one another aux fe space. 
Thomas Witkowski's avatar
Thomas Witkowski committed
718
719
720
721
722
723

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

724
725
	    assembleOnDifMeshes2(componentSpaces[i], 
				 traverseInfo.getAuxFESpace(i, j),
Thomas Witkowski's avatar
Thomas Witkowski committed
726
727
				 assembleFlag,
				 NULL,
728
				 ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
729
730
731
732
733
734
735

	  } 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
736
737
738
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
739
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
740
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
741
742
743
744
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
745
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
746
747
748
749

	} 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
750
751
752
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
753
754
755
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  
	} else {
	  ERROR_EXIT("Not yet implemented!\n");
756
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
757

Thomas Witkowski's avatar
Thomas Witkowski committed
758
	assembledMatrix[i][j] = true;
759
760
761

	if (assembleMatrix)
	  matrix->finishInsertion();
762
763

 	if (assembleMatrix && matrix->getBoundaryManager())
764
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
765
766
	
	if (matrix)
767
	  nnz += matrix->getBaseMatrix().nnz();	
768
769
      }

770
      // And now assemble boundary conditions on the vectors
Thomas Witkowski's avatar
Thomas Witkowski committed
771
772
      assembleBoundaryConditions(rhs->getDOFVector(i),
				 solution->getDOFVector(i),
773
				 componentMeshes[i],
774
				 assembleFlag);     
775
    }
776

777
    solverMatrix.setMatrix(*systemMatrix);
778

Thomas Witkowski's avatar
Thomas Witkowski committed
779
    createPrecon();
780

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

783
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
784
785
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
786
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
787
788
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
Thomas Witkowski's avatar
Thomas Witkowski committed
789
#endif     
790
791
  }

792
793
  void ProblemVec::createPrecon()
  {
794
    std::string preconType("no");
795
796
797
798
799
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
800
    solver->setLeftPrecon(preconCreator->create(solverMatrix.getMatrix()));
801
802
803
804
805

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

    preconCreator = CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);
Thomas Witkowski's avatar
Thomas Witkowski committed
806
    solver->setRightPrecon(preconCreator->create(solverMatrix.getMatrix()));
807
808
809
  }


810
811
812
813
  void ProblemVec::writeFiles(AdaptInfo *adaptInfo, bool force) 
  {
    FUNCNAME("ProblemVec::writeFiles()");

814
815
816
817
818
819
820
    clock_t first = clock();

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

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

839
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
840
841
842
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
843
    solution->interpol(fct);
844
845
846
847
848
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
849
850
				     double *estFactor,
				     bool fake)
851
852
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");
Thomas Witkowski's avatar
Thomas Witkowski committed
853
   
Thomas Witkowski's avatar
Thomas Witkowski committed
854
    if (!(*systemMatrix)[i][j]) {
855
      TEST_EXIT(i != j)("should have been created already\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
856
      (*systemMatrix)[i][j] = new DOFMatrix(componentSpaces[i], componentSpaces[j], "");
Thomas Witkowski's avatar
Thomas Witkowski committed
857
858
859
      (*systemMatrix)[i][j]->setCoupleMatrix(true);
      (*systemMatrix)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix)[i][i]->getBoundaryManager()->
860
				getBoundaryConditionMap());
Thomas Witkowski's avatar
Thomas Witkowski committed
861
862
863

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

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

868
869
870
871
872
873
874
875
876
877
878
    if (!fake) {    
      traverseInfo.getMatrix(i, j).setAuxFESpaces(op->getAuxFESpaces()); 
      
      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(true);
	  break;
	}
      }    
    } 
879
880
881
882
  }

  void ProblemVec::addVectorOperator(Operator *op, int i,
				     double *factor,
883
884
				     double *estFactor,
				     bool fake)
885
886
887
  {
    FUNCNAME("ProblemVec::addVectorOperator()");

Thomas Witkowski's avatar
Thomas Witkowski committed
888
889
    rhs->getDOFVector(i)->addOperator(op, factor, estFactor);

890
891
892
893
894
895
896
897
898
899
    if (!fake) {
      traverseInfo.getVector(i).setAuxFESpaces(op->getAuxFESpaces()); 
      
      for (int j = 0; j < static_cast<int>(op->getAuxFESpaces().size()); j++) {
	if ((op->getAuxFESpaces())[j]->getMesh()