ProblemVec.cc 41.9 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
195
196
197
198
199
200
201
202
203
204
205

	// === 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
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
322

    char number[10];
323
    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], 
326
					     componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
327
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
328
      sprintf(number, "[%d]", i);
329
      numberedName = "rhs" + std::string(number);
Thomas Witkowski's avatar
Thomas Witkowski committed
330
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
331
      numberedName = name + std::string(number);
Thomas Witkowski's avatar
Thomas Witkowski committed
332
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
333
						       numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
334
335
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
336
337
338
339
340
341
342
343
    }
  }

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

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

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

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

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

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

      // === create estimator ===
373
      std::string estimatorType("0");
374
375
376
377
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
	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()");

578
579
    //    if (adaptInfo->getTimestepNumber() == 9) return 0;

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

586
587
588
589
590
591
592
    return refineFlag;
  }

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

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

    return coarsenFlag;
  }

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

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

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

624
625
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
626
627
628
629
630
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

631
632
633
634
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

635
    for (int i = 0; i < static_cast<int>(meshes.size()); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
636
      meshes[i]->dofCompress();
637
638
639

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

Thomas Witkowski's avatar
Thomas Witkowski committed
648
    if (useGetBound)
649
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
650

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

Thomas Witkowski's avatar
Thomas Witkowski committed
656
      rhs->getDOFVector(i)->set(0.0);
657
      for (int j = 0; j < nComponents; j++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
658
	if ((*systemMatrix)[i][j]) {
659
660
661
662
663
	  DOFMatrix*                   dof_matrix= (*systemMatrix)[i][j];
	  DOFMatrix::base_matrix_type& base_matrix= dof_matrix->getBaseMatrix();

	  int nnz_per_row= 0;
	  if (num_rows(base_matrix) != 0)
Thomas Witkowski's avatar
Thomas Witkowski committed
664
	    nnz_per_row= int(double(base_matrix.nnz()) / num_rows(base_matrix) * 1.2); 
665
	  if (nnz_per_row < 5) 
Thomas Witkowski's avatar
Thomas Witkowski committed
666
	    nnz_per_row= 5;
667
668
669
670

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

	  set_to_zero(base_matrix);
673
674
	  
	  // Reuse old sparsity information (if available) or default
675
	  //	  dof_matrix->startInsertion(nnz_per_row);
676
	}
677
678
679
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
680
681
    traverseInfo.updateStatus();

Thomas Witkowski's avatar
Thomas Witkowski committed
682
    for (int i = 0; i < nComponents; i++) {
683
      for (int j = 0; j < nComponents; j++) {
684

685
686
687
688
	// 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
689
	DOFMatrix *matrix = (*systemMatrix)[i][j];
690

691
692
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
693
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j])
694
	  assembleMatrix = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
695
696

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

700
701
702
703
704
705
706
	// 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;
	}

707
708
709
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

Thomas Witkowski's avatar
Thomas Witkowski committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
	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
729
	    // least one another aux fe space. 
Thomas Witkowski's avatar
Thomas Witkowski committed
730
731
732
733
734
735

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

736
737
	    assembleOnDifMeshes2(componentSpaces[i], 
				 traverseInfo.getAuxFESpace(i, j),
Thomas Witkowski's avatar
Thomas Witkowski committed
738
739
				 assembleFlag,
				 NULL,
740
				 ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
741
742
743
744
745
746
747

	  } 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
748
749
750
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
751
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
752
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
753
754
755
756
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
757
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
758
759
760
761

	} 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
762
763
764
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
765
766
767
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  
	} else {
	  ERROR_EXIT("Not yet implemented!\n");
768
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
769

770
771
772
	// TODO: ExitMatrix should be called after finishInsertion!
 	if (assembleMatrix && matrix->getBoundaryManager())
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	  
773
	
Thomas Witkowski's avatar
Thomas Witkowski committed
774
	assembledMatrix[i][j] = true;
775
776
      }

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

784
785
    int nnz = 0;

786
787
788
    // Finish insertion
    for (int i = 0; i < nComponents; i++) 
      for (int j = 0; j < nComponents; j++) 
789
 	if ((*systemMatrix)[i][j]) {
790
 	  (*systemMatrix)[i][j]->finishInsertion(); 
791
	  nnz += (*systemMatrix)[i][j]->getBaseMatrix().nnz();
792
	}    
793

794
    solverMatrix.setMatrix(*systemMatrix);
795

Thomas Witkowski's avatar
Thomas Witkowski committed
796
    createPrecon();
797

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

800
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
801
802
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
803
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
804
805
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
Thomas Witkowski's avatar
Thomas Witkowski committed
806
#endif     
807
808
  }

809
810
  void ProblemVec::createPrecon()
  {
811
    std::string preconType("no");
812
813
814
815
816
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);

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

Thomas Witkowski's avatar
Thomas Witkowski committed
817
    solver->setLeftPrecon(preconCreator->create(solverMatrix.getMatrix()));
818
819
820
821
822

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

    preconCreator = CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);
Thomas Witkowski's avatar
Thomas Witkowski committed
823
    solver->setRightPrecon(preconCreator->create(solverMatrix.getMatrix()));
824
825
826
  }


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

831
832
833
834
835
836
837
    clock_t first = clock();

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

    int i;
Thomas Witkowski's avatar
Thomas Witkowski committed
838
    int size = static_cast<int>(fileWriters.size());
839
840
841
842
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 1)
#endif
    for (i = 0; i < size; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
843
      fileWriters[i]->writeFiles(adaptInfo, force);
844
    }
845
846
    
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
847
848
849
    INFO(info, 8)("writeFiles needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()),
		  omp_get_wtime() - wtime);
850
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
851
852
    INFO(info, 8)("writeFiles needed %.5f seconds\n",
		  TIME_USED(first, clock()));
853
#endif
854
855
  }

856
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
857
858
859
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

Thomas Witkowski's avatar
Thomas Witkowski committed
860
    solution->interpol(fct);
861
862
863
864
865
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
866
867
				     double *estFactor,
				     bool fake)
868
869
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");
Thomas Witkowski's avatar
Thomas Witkowski committed
870
   
Thomas Witkowski's avatar
Thomas Witkowski committed
871
    if (!(*systemMatrix)[i][j]) {
872
      TEST_EXIT(i != j)("should have been created already\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
873
      (*systemMatrix)[i][j] = new DOFMatrix(componentSpaces[i], componentSpaces[j], "");
Thomas Witkowski's avatar
Thomas Witkowski committed
874
875
876
      (*systemMatrix)[i][j]->setCoupleMatrix(true);
      (*systemMatrix)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix)[i][i]->getBoundaryManager()->
877
				getBoundaryConditionMap());
Thomas Witkowski's avatar
Thomas Witkowski committed
878
879
880

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

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

885
886
887
888
889
890
891
892
893
894
895
    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;