ProblemVec.cc 42.2 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
#include <sstream>
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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
16
#include "DualTraverse.h"
17
18
19
20
21
22
#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
23
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
24
#include "TraverseParallel.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
25
#include "VtkWriter.h"
26
#include "ValueReader.h"
27
28
29
30
31
32
33
34
35
36

namespace AMDiS {

  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
    
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
37
    if (meshes.size() != 0) { 
38
39
40
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
41
	  (!adoptFlag.isSet(INIT_MESH) &&
42
43
44
45
46
47
48
	   (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
49
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
50
	componentMeshes = adoptProblem->componentMeshes;
Thomas Witkowski's avatar
Thomas Witkowski committed
51
52
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
53
54
55

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

64
65
66
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
67
    if (meshes.size() == 0) 
68
69
70
      WARNING("no mesh created\n");

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

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

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

94
95
96
      }
    }

97
    if (feSpaces.size() == 0) 
98
99
100
      WARNING("no feSpace created\n");

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
123
    if (!solver) 
124
125
126
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
127
    if (initFlag.isSet(INIT_ESTIMATOR))
128
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
129
130

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
Thomas Witkowski's avatar
Thomas Witkowski committed
131
      estimator = adoptProblem->getEstimator();
132
133

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

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
140
    if (initFlag.isSet(INIT_FILEWRITER))
141
142
143
144
145
146
147
148
149
150
151
      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;
152
    std::string serializationFilename = "";
153
154
155
156
157
158
159
    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
160
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
161
		    &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
162
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
163
164
165
166
167
168
169
		    &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
170
	GET_PARAMETER(0, name + "->input->serialization filename", 
171
172
173
174
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
175
	std::ifstream in(serializationFilename.c_str());
176
177
178
	deserialize(in);
	in.close();
      } else {
179
	int globalRefinements = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
180
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
181
182
		      &globalRefinements);

183
	// Initialize the meshes if there is no serialization file.
184
185
186
187
188
	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
189
	    meshes[i]->initialize();	    
190
	}
191
192
193
194

	// === 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
195
	if (valueFilename.length()) {
196
197
198
199
200
201
202
203
204
205
	  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
206
	    refinementManager->globalRefine(meshes[i], globalRefinements);
207
208
209
210
211
212
213
214
215
216
      }
    }

    doOtherStuff();
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
217
    componentMeshes.resize(nComponents);
218
    std::map<int, Mesh*> meshForRefinementSet;
219
220
    char number[3];

221
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
222
    GET_PARAMETER(0, name + "->mesh", &meshName);
223
    TEST_EXIT(meshName != "")("no mesh name specified\n");
224
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
225
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
226
    TEST_EXIT(dim)("no problem dimension specified!\n");
227

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

261
  void ProblemVec::createFESpace(DOFAdmin *admin)
262
263
264
  {
    FUNCNAME("ProblemVec::createFESpace()");

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

270
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
271
    traverseInfo.resize(nComponents);
272

273
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
274
      char number[3];
275
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
      int degree = 1;
      GET_PARAMETER(0, name + "->polynomial degree[" + number + "]","%d", &degree);
278

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

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

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

    for (int i = 0; i < nComponents; i++) {
295
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
296
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
297
      
Thomas Witkowski's avatar
Thomas Witkowski committed
298
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
299
300
301
    }

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
317
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
318
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
319
320
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
321

Thomas Witkowski's avatar
Thomas Witkowski committed
322
323
    char number[10] = "";
    std::string numberedName = "";
Thomas Witkowski's avatar
Thomas Witkowski committed
324
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
325
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
326
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
327
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
328

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

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

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

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

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

    char number[3];
366
    std::string estName;
367

Thomas Witkowski's avatar
Thomas Witkowski committed
368
369
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
370
      sprintf(number, "%d", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
371
      estName = name + "->estimator[" + std::string(number) + "]";
372
373

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


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

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

403
    std::string numberedName;
404
    char number[10];
Thomas Witkowski's avatar
Thomas Witkowski committed
405
    int nMarkersCreated = 0;
406

407
    for (int i = 0; i < nComponents; i++) {
408
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
409
      numberedName = name + "->marker" + std::string(number);
410
411
      marker[i] = Marker::createMarker(numberedName, i);
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
412
413
414
415
416
417
	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);
418
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
448
449
					    solutionList));
    }


    // Create own filewriters for each components of the problem
    char number[10];
450
    for (int i = 0; i < nComponents; i++) {
451
      sprintf(number, "[%d]", i);
Thomas Witkowski's avatar
Thomas Witkowski committed
452
      numberedName  = name + "->output" + std::string(number);
453
454
455
456
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

      if (filename != "") {
Thomas Witkowski's avatar
Thomas Witkowski committed
457
	fileWriters.push_back(new FileWriter(numberedName, 
Thomas Witkowski's avatar
Thomas Witkowski committed
458
					      componentMeshes[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
459
					      solution->getDOFVector(i)));
460
461
462
463
464
465
      }
    }


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

Thomas Witkowski's avatar
Thomas Witkowski committed
473
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
474
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
475
      fileWriters.push_back(new Serializer<ProblemVec>(this));
476
477
478
479
480
481
482
    }
  }

  void ProblemVec::doOtherStuff()
  {
  }

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

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

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

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

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

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

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

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

519
520
    clock_t first = clock();

521
522
523
524
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

525
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
526
      computeError(adaptInfo);
527
528
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
529
	Estimator *scalEstimator = estimator[i];
530
531
532
533
534
535
536
537
538
539
	
	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);
	}
540
541
542
      }
    }

543
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
544
545
    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);
546
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
547
548
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
549
550
551

#endif

552
553
554
555
556
557
558
559
560
561
562
  }

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

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

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

584
585
586
587
588
589
590
    return refineFlag;
  }

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

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

    return coarsenFlag;
  }

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

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

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

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

620
621
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
622
623
624
625
626
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

627
628
629
630
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
644
    if (useGetBound)
645
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
646

647
    for (int i = 0; i < nComponents; i++) {
648
      MSG("%d DOFs for %s\n", 
649
650
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());
651

Thomas Witkowski's avatar
Thomas Witkowski committed
652
      rhs->getDOFVector(i)->set(0.0);
653
      for (int j = 0; j < nComponents; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
654
	if ((*systemMatrix)[i][j]) {
655
656
657
	  DOFMatrix*                   dof_matrix= (*systemMatrix)[i][j];
	  DOFMatrix::base_matrix_type& base_matrix= dof_matrix->getBaseMatrix();

Thomas Witkowski's avatar
Thomas Witkowski committed
658
	  dof_matrix->calculateNnz();
659
660
661
662

	  // Correct dimensionality of matrix
	  base_matrix.change_dim(componentSpaces[i]->getAdmin()->getUsedSize(), 
				 componentSpaces[j]->getAdmin()->getUsedSize());
Thomas Witkowski's avatar
Thomas Witkowski committed
663
664

	  set_to_zero(base_matrix);
665
	}
666
667
668
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
669
670
    traverseInfo.updateStatus();

Thomas Witkowski's avatar
Thomas Witkowski committed
671
    for (int i = 0; i < nComponents; i++) {
672
      for (int j = 0; j < nComponents; j++) {
673

674
675
676
677
	// 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
678
	DOFMatrix *matrix = (*systemMatrix)[i][j];
679

680
681
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
682
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j])
683
	  assembleMatrix = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
684
685

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

689
690
691
692
693
694
695
	// 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;
	}

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

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

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

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

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

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

759
760
761
	// TODO: ExitMatrix should be called after finishInsertion!
 	if (assembleMatrix && matrix->getBoundaryManager())
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	  
762
	
Thomas Witkowski's avatar
Thomas Witkowski committed
763
	assembledMatrix[i][j] = true;
764
765
      }

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

773
774
    int nnz = 0;

775
776
777
    // Finish insertion
    for (int i = 0; i < nComponents; i++) 
      for (int j = 0; j < nComponents; j++) 
778
 	if ((*systemMatrix)[i][j]) {
779
 	  (*systemMatrix)[i][j]->finishInsertion(); 
780
	  nnz += (*systemMatrix)[i][j]->getBaseMatrix().nnz();
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
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
846
847
848
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
849
    solution->interpol(fct);
850
851
852
853
854
  }

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

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

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

874
875
876
877
878
879
880
881
882
883
884
    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;
	}
      }    
    } 
885
886
887
888
  }

  void ProblemVec::addVectorOperator(Operator