ProblemVec.cc 42.2 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
2
#include <sstream>

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
24
25
#include "Mesh.h"
#include "OEMSolver.h"
#include "Preconditioner.h"
#include "MatVecMultiplier.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
26
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
27
#include "TraverseParallel.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
28
#include "VtkWriter.h"
29
#include "ValueReader.h"
30
31
32
33
34
35
36
37
38
39

namespace AMDiS {

  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
    
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
40
    if (meshes.size() != 0) { 
41
42
43
44
45
46
47
48
49
50
51
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
	  ((!adoptFlag.isSet(INIT_MESH))&&
	   (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
63
64
	  componentMeshes.resize(nComponents);
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++) {
	    componentMeshes[i] = componentMeshes[0];
65
66
67
	  }
	}

68
69
70
      }
    }

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

    // === create fespace ===
75
    if (feSpaces.size() != 0) {
76
77
78
79
80
81
82
83
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
	  (initFlag.isSet(INIT_SYSTEM)&&!adoptFlag.isSet(INIT_FE_SPACE))) {
	createFESpace();
      } 
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
84
85
	feSpaces = adoptProblem->getFESpaces();
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
86
	traverseInfo = adoptProblem->traverseInfo;
87
88
89

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

99
100
101
      }
    }

102
    if (feSpaces.size() == 0) 
103
104
105
106
107
108
109
      WARNING("no feSpace created\n");

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
128
    if (!solver) 
129
130
131
132
133
134
135
      WARNING("no solver created\n");

    // === create estimator ===
    if (initFlag.isSet(INIT_ESTIMATOR)) {
      createEstimator();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
136
      estimator = adoptProblem->getEstimator();
137
138
139
140
141
142
143
    } 

    // === create marker ===
    if (initFlag.isSet(INIT_MARKER)) {
      createMarker();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
144
      marker = adoptProblem->getMarker();
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
    } 


    // === create file writer ===
    if (initFlag.isSet(INIT_FILEWRITER)) {
      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;
163
    std::string serializationFilename = "";
164
165
166
167
168
169
170
    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;

Thomas Witkowski's avatar
Thomas Witkowski committed
171
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
172
		    &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
173
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
174
175
176
177
178
179
180
		    &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
181
	GET_PARAMETER(0, name + "->input->serialization filename", 
182
183
184
185
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
186
	std::ifstream in(serializationFilename.c_str());
187
188
189
	deserialize(in);
	in.close();
      } else {
190
	int globalRefinements = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
191
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
192
193
		      &globalRefinements);

194
	// Initialize the meshes if there is no serialization file.
195
196
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
	  if (initFlag.isSet(INIT_MESH) && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
197
	    meshes[i]->initialize();	    
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

	// === read value file and use it for the mesh values ===
	std::string valueFilename("");
	GET_PARAMETER(0, meshes[0]->getName() + "->value file name", &valueFilename); 
	if (valueFilename.length()) {     
	  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
    char number[3];

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

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

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

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

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

280
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
281
      char number[3];
282
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
283
284
      int degree = 1;
      GET_PARAMETER(0, name + "->polynomial degree[" + number + "]","%d", &degree);
285

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

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

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

    for (int i = 0; i < nComponents; i++) {
      for (int j = 0; j < nComponents; j++) {
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
      }
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
306
307
308
    }

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

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

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

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

    char number[10];
330
    std::string numberedName;
Thomas Witkowski's avatar
Thomas Witkowski committed
331
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
332
      (*systemMatrix)[i][i] = NEW DOFMatrix(componentSpaces[i], 
333
					     componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
334
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
335
      sprintf(number, "[%d]", i);
336
      numberedName = "rhs" + std::string(number);
Thomas Witkowski's avatar
Thomas Witkowski committed
337
338
339
      rhs->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], numberedName));
      numberedName = name + std::string(number);
      solution->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], 
340
						       numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
341
342
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
343
344
345
    }

    // === create matVec ===
Thomas Witkowski's avatar
Thomas Witkowski committed
346
    matVec = NEW StandardMatVec<Matrix<DOFMatrix*>, SystemVector>(systemMatrix);
347
348
349
350
351
352
353
  }

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

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

    // === create preconditioners ===
367
    std::string preconType("no");
368
369

    PreconditionerScal *scalPrecon;
370
    PreconditionerVec *vecPrecon = NEW PreconditionerVec(nComponents);
371

Thomas Witkowski's avatar
Thomas Witkowski committed
372
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);
373
374
375
376
377
    CreatorInterface<PreconditionerScal> *preconCreator =
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
378
	setName(name + "->solver->left precon");
379

Thomas Witkowski's avatar
Thomas Witkowski committed
380
      for (int i = 0; i < nComponents; i++) {
381
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
382
	  setSizeAndRow(nComponents, i);
383
384
    
	scalPrecon = preconCreator->create();
Thomas Witkowski's avatar
Thomas Witkowski committed
385
386
	for (int j = 0; j < nComponents; j++) {
	  scalPrecon->setMatrix(&(*systemMatrix)[i][j], j);
387
388
389
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
390
      leftPrecon = vecPrecon;
391
392
393
    }


394
    vecPrecon = NEW PreconditionerVec(nComponents);
395

Thomas Witkowski's avatar
Thomas Witkowski committed
396
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);
397
398
399
    preconCreator = 
      CreatorMap<PreconditionerScal>::getCreator(preconType);

Thomas Witkowski's avatar
Thomas Witkowski committed
400
    if (!preconCreator->isNullCreator()) {
401
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
402
	setName(name + "->solver->left precon");
403
404


Thomas Witkowski's avatar
Thomas Witkowski committed
405
      for (int i = 0; i < nComponents; i++) {
406
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
407
	  setSizeAndRow(nComponents, i);
408
409
    
	scalPrecon = preconCreator->create();
Thomas Witkowski's avatar
Thomas Witkowski committed
410
411
	for (int j = 0; j < nComponents; j++) {
	  scalPrecon->setMatrix(&(*systemMatrix)[i][j], j);
412
413
414
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
415
      rightPrecon = vecPrecon;
416
417
418
419
    }


    // === create vector creator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
420
    solver->setVectorCreator(NEW SystemVector::Creator("temp",
421
							componentSpaces, 
422
							nComponents));
423
424
425
426
427
428
429
  }

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

    // create and set leaf data prototype
Thomas Witkowski's avatar
Thomas Witkowski committed
430
431
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->setElementDataPrototype
432
433
434
435
	(NEW LeafDataEstimatableVec(NEW LeafDataCoarsenableVec));
    }  

    char number[3];
436
    std::string estName;
437

Thomas Witkowski's avatar
Thomas Witkowski committed
438
439
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
440
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
441
      estName = name + "->estimator[" + std::string(number) + "]";
442
443

      // === create estimator ===
444
      std::string estimatorType("no");
445
446
447
448
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
	dynamic_cast<EstimatorCreator*>(
					CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
449
      if (estimatorCreator) {
450
451
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
Thomas Witkowski's avatar
Thomas Witkowski committed
452
	if (estimatorType == "recovery") {
453
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
Thomas Witkowski's avatar
Thomas Witkowski committed
454
	    setSolution(solution->getDOFVector(i));
455
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
456
	estimator[i] = estimatorCreator->create();
457
458
459
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
460
461
462
463
464
      if (estimator[i]) {
	for (int j = 0; j < nComponents; j++) {
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				   solution->getDOFVector(j), 
				   rhs->getDOFVector(j));
465
466
467
468
469
470
471
472
473
	}
      }
    }
  }

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

474
    std::string numberedName;
475
    char number[10];
Thomas Witkowski's avatar
Thomas Witkowski committed
476
    int nMarkersCreated = 0;
477

478
    for (int i = 0; i < nComponents; i++) {
479
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
480
      numberedName = name + "->marker" + std::string(number);
481
482
      marker[i] = Marker::createMarker(numberedName, i);
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
483
484
485
486
487
488
	nMarkersCreated++;

	// If there is more than one marker, and all components are defined
	// on the same mesh, the maximum marking has to be enabled.
 	if ((nMarkersCreated > 1) && (nMeshes == 1))
 	  marker[i]->setMaximumMarking(true);
489
490
491
492
493
494
495
496
497
498
      }
    }
  }

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

    // Create one filewriter for all components of the problem
Thomas Witkowski's avatar
Thomas Witkowski committed
499
    std::string numberedName  = name + "->output";
500
    std::string filename = "";
501
502
503
    GET_PARAMETER(0, numberedName + "->filename", &filename);

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
510
	solutionList[i] = solution->getDOFVector(i);
511
512
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
513
      fileWriters.push_back(NEW FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
514
					    componentMeshes[0],
515
516
517
518
519
520
					    solutionList));
    }


    // Create own filewriters for each components of the problem
    char number[10];
521
    for (int i = 0; i < nComponents; i++) {
522
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
523
      numberedName  = name + "->output" + std::string(number);
524
525
526
527
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

      if (filename != "") {
Thomas Witkowski's avatar
Thomas Witkowski committed
528
	fileWriters.push_back(NEW FileWriter(numberedName, 
Thomas Witkowski's avatar
Thomas Witkowski committed
529
					      componentMeshes[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
530
					      solution->getDOFVector(i)));
531
532
533
534
535
536
      }
    }


    // Check for serializer
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
537
    GET_PARAMETER(0, name + "->write serialization", "%d", &writeSerialization);
538
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
539
540
      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());
541
542
543
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
544
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
545
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
546
      fileWriters.push_back(NEW Serializer<ProblemVec>(this));
547
548
549
550
551
552
553
    }
  }

  void ProblemVec::doOtherStuff()
  {
  }

554
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
555
556
557
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
558
    if (!solver) {
559
560
561
562
563
564
565
566
567
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
Thomas Witkowski's avatar
Thomas Witkowski committed
568
    int iter = solver->solve(matVec, solution, rhs, leftPrecon, rightPrecon, fixedMatrix);
569
    
570
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
571
572
    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);
573
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
574
575
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
576
577
578
579
#endif


    adaptInfo->setSolverIterations(iter);
Thomas Witkowski's avatar
Thomas Witkowski committed
580
581
582
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
583
584
585
586
587
588
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
589
590
    VtkWriter::writeFile(solution->getDOFVector(0), "test.vtu");

591
592
    clock_t first = clock();

593
594
595
596
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

597
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
598
      computeError(adaptInfo);
599
600
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
601
	Estimator *scalEstimator = estimator[i];
602
603
604
605
606
607
608
609
610
611
	
	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);
	}
612
613
614
      }
    }

615
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
616
617
    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);
618
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
619
620
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
621
622
623

#endif

624
625
626
627
628
629
630
631
632
633
634
  }

  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;
635
    for (int i = 0; i < nComponents; i++) {
636
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
637
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
638
639
640
641
      } else {
	WARNING("no marker for component %d\n", i);
      }
    }
642
    
643
644
645
646
647
648
649
    return markFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
650
    int nMeshes = static_cast<int>(meshes.size());
651
    Flag refineFlag = 0;
652
    for (int i = 0; i < nMeshes; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
653
      refineFlag |= refinementManager->refineMesh(meshes[i]);
654
655
656
657
658
659
660
661
    }
    return refineFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
662
    int nMeshes = static_cast<int>(meshes.size());
663
    Flag coarsenFlag = 0;
664
665
    for (int i = 0; i < nMeshes; i++) {
      if (adaptInfo->isCoarseningAllowed(i)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
666
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
667
668
669
670
671
672
673
674
675
676
677

	WARNING("coarsening for component %d no allowed\n", i);
      }
    }
    return coarsenFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
678
    if (allowFirstRef) {
679
      for (int i = 0; i < nComponents; i++) {
680
681
	adaptInfo->allowRefinement(true, i);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
682
      allowFirstRef = false;
683
    } else {
684
      for (int i = 0; i < nComponents; i++) {
685
686
687
688
689
690
691
692
693
694
695
	if (adaptInfo->spaceToleranceReached(i)) {
	  adaptInfo->allowRefinement(false, i);
	} else {
	  adaptInfo->allowRefinement(true, i);	
	}
      }
    }

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

696
697
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
698
699
700
701
702
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

703
704
705
706
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
707
708
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
      meshes[i]->dofCompress();
709
710
711
712
    }

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
713
714
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
715
716
717
718
719
720
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
721
    if (useGetBound) {
722
723
724
      assembleFlag |= Mesh::FILL_BOUND;
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
725

726
    for (int i = 0; i < nComponents; i++) {
727
      MSG("%d DOFs for %s\n", 
728
729
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());
730

Thomas Witkowski's avatar
Thomas Witkowski committed
731
      rhs->getDOFVector(i)->set(0.0);
732
      for (int j = 0; j < nComponents; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
733
	if ((*systemMatrix)[i][j]) {
734
735
	  // The matrix should not be deleted, if it was assembled before
	  // and it is marked to be assembled only once.
Thomas Witkowski's avatar
Thomas Witkowski committed
736
737
	  if (!(assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) && asmMatrix) {
	    (*systemMatrix)[i][j]->clear();
738
739
	  }
	}
740
741
742
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
743
744
    traverseInfo.updateStatus();

Thomas Witkowski's avatar
Thomas Witkowski committed
745
    for (int i = 0; i < nComponents; i++) {
746
      for (int j = 0; j < nComponents; j++) {
747
748
749
750
	// 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
751
	DOFMatrix *matrix = (*systemMatrix)[i][j];
752

753
754
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
Thomas Witkowski's avatar
Thomas Witkowski committed
755
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
756
757
	  assembleMatrix = false;
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
758
759

	// If there is no DOFMatrix, e.g., if it is completly 0, do not assemble.
760
761
762
763
	if (!matrix) {
	  assembleMatrix = false;
	}

764
765
766
767
	if (!asmMatrix) {
	  assembleMatrix = false;
	}

768
769
770
771
772
773
774
	// 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.
	if (!assembleMatrix && i != j) {
	  continue;
	}

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

Thomas Witkowski's avatar
Thomas Witkowski committed
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
	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
797
	    // least one another aux fe space. 
Thomas Witkowski's avatar
Thomas Witkowski committed
798
799
800
801
802
803

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

804
805
	    assembleOnDifMeshes2(componentSpaces[i], 
				 traverseInfo.getAuxFESpace(i, j),
Thomas Witkowski's avatar
Thomas Witkowski committed
806
807
				 assembleFlag,
				 NULL,
808
				 ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
809
810
811
812
813
814
815

	  } 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
816
817
818
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
819
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
820
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
821
822
823
824
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
825
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
826
827
828
829

	} 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
830
831
832
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
833
834
835
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  

	  TEST_EXIT_DBG(matrix->getUsedSize() == componentSpaces[i]->getAdmin()->getUsedSize())
836
	    ("Assembled matrix has wrong dimension!\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
837
838
839
840
841
	  TEST_EXIT_DBG(matrix->getNumCols() == componentSpaces[j]->getAdmin()->getUsedSize())
	    ("Assembled matrix has wrong dimension!\n");	  

	} else {
	  ERROR_EXIT("Not yet implemented!\n");
842
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
843

844
845
846
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->exitMatrix(matrix);	  
	
Thomas Witkowski's avatar
Thomas Witkowski committed
847
	assembledMatrix[i][j] = true;
848
849
      }

850
      // And now assemble boundary conditions on the vectors
Thomas Witkowski's avatar
Thomas Witkowski committed
851
852
      assembleBoundaryConditions(rhs->getDOFVector(i),
				 solution->getDOFVector(i),
853
854
				 componentMeshes[i],
				 assembleFlag);
855
    }
856
857

#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
858
859
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
860
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
861
862
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
863
#endif
864
865
866
867
868
869
  }

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

870
871
872
873
874
875
876
    clock_t first = clock();

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

    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
877
    int size = static_cast<int>(fileWriters.size());
878
879
880
881
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 1)
#endif
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
882
      fileWriters[i]->writeFiles(adaptInfo, force);
883
    }
884
885
    
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
886
887
888
    INFO(info, 8)("writeFiles needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()),
		  omp_get_wtime() - wtime);
889
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
890
891
    INFO(info, 8)("writeFiles needed %.5f seconds\n",
		  TIME_USED(first, clock()));
892
#endif
893
894
  }

895
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
896
897
898
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
899
    solution->interpol(fct);
900
901
902
903
904
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
905
906
				     double *estFactor,
				     bool fake)
907
908
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");
Thomas Witkowski's avatar
Thomas Witkowski committed
909
   
Thomas Witkowski's avatar
Thomas Witkowski committed
910
    if (!(*systemMatrix)[i][j]) {
911
      TEST_EXIT(i != j)("should have been created already\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
912
913
914
915
      (*systemMatrix)[i][j] = NEW DOFMatrix(componentSpaces[i], componentSpaces[j], "");
      (*systemMatrix)[i][j]->setCoupleMatrix(true);
      (*systemMatrix)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix)[i][i]->getBoundaryManager()->
916
917
				getBoundaryConditionMap());
    }    
Thomas Witkowski's avatar
Thomas Witkowski committed
918

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

921
922
923
924
925
926
927
928
929
930
931
    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;
	}
      }    
    } 
932
933
934
935
  }

  void ProblemVec::addVectorOperator(Operator *op, int i,
				     double *factor,
936
937
				     double *estFactor,
				     bool fake)
938
939
940
  {
    FUNCNAME("ProblemVec::addVectorOperator()");

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

943
944
945
946
947
948
949
950
951
952
    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() != componentSpaces[i]->getMesh()) {
	  op->setNeedDualTraverse(true);
	  break;
	}
      }    
    }
953
954
955
956
957
958
959
960
961
  }

  void ProblemVec::addDirichletBC(BoundaryType type, int system,
				  AbstractFunction<double, WorldVector<double> >* b)
  {
    FUNCNAME("ProblemVec::addDirichletBC()");

    DirichletBC *dirichlet = new DirichletBC(type, 
					     b, 
962
					     componentSpaces[system]);
963
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
964
965
      if (systemMatrix && (*systemMatrix)[system][i]) {
	(*systemMatrix)[system][i]->getBoundaryManager()->addBoundaryCondition(dirichlet);
966
967
      }
    }
968

Thomas Witkowski's avatar
Thomas Witkowski committed
969
970
    if (rhs)
      rhs->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);
971

Thomas Witkowski's avatar
Thomas Witkowski committed
972
973
    if (solution)
      solution->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);