ProblemVec.cc 49 KB
Newer Older
Thomas Witkowski's avatar
Thomas Witkowski committed
1
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
2
#include <boost/lexical_cast.hpp>
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "ProblemVec.h"
#include "RecoveryEstimator.h"
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Estimator.h"
#include "Marker.h"
#include "AdaptInfo.h"
#include "FileWriter.h"
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
17
#include "DualTraverse.h"
18
19
20
21
22
23
#include "Mesh.h"
#include "OEMSolver.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
24
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
25
#include "TraverseParallel.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
26
#include "VtkWriter.h"
27
#include "ValueReader.h"
28
#include "ProblemVecDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
29
#include "Debug.h"
30
31
32

namespace AMDiS {

33
34
  using boost::lexical_cast;

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

41
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
42
    if (meshes.size() != 0) { 
43
44
45
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
46
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
47
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
48
	createMesh();
Thomas Witkowski's avatar
Thomas Witkowski committed
49

50
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
51
	  (adoptFlag.isSet(INIT_MESH) ||
52
53
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
54
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
55
	componentMeshes = adoptProblem->componentMeshes;
Thomas Witkowski's avatar
Thomas Witkowski committed
56
57
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
58
59
60

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

69
70
71
      }
    }

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

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

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

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

99
100
101
      }
    }

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

    // === create system ===
106
    if (initFlag.isSet(INIT_SYSTEM)) 
107
      createMatricesAndVectors();
108
    
109
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
110
      solution = adoptProblem->getSolution();
111
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
112
      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
      WARNING("no solver created\n");

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

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
136
      estimator = adoptProblem->getEstimators();
137
138

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

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
145
    if (initFlag.isSet(INIT_FILEWRITER))
146
147
148
149
150
151
152
153
154
155
156
      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;
157
    std::string serializationFilename = "";
158
159
160
161
162
163
164
    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;

165
      GET_PARAMETER(0, name + "->input->read serialization", "%d", &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
166
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
167
168
169
170
171
172
173
		    &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
174
	GET_PARAMETER(0, name + "->input->serialization filename", 
175
176
177
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

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

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
#endif
187
188

	deserialized = true;
189
      } else {
190
	int globalRefinements = 0;
191
192
193
194
195

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

200
	// Initialize the meshes if there is no serialization file.
201
202
203
204
205
	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
206
	    meshes[i]->initialize();	    
207
	}
208
209
210
211

	// === 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
212
	if (valueFilename.length()) {
213
214
215
216
217
218
219
220
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());
	  meshes[0]->clearMacroFileInfo();
	}

	// === do global refinements ===
221

222
223
224
225
226
	for (unsigned int i = 0; i < meshes.size(); i++) {
	  bool initMesh = initFlag.isSet(INIT_MESH) ||
	    (adoptProblem && adoptFlag.isSet(INIT_MESH));

	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
227
	    refinementManager->globalRefine(meshes[i], globalRefinements);
228
	}
229
230
231
232
233
234
      }
    }

    doOtherStuff();
  }

235

236
237
238
239
  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
240
    componentMeshes.resize(nComponents);
241
    std::map<int, Mesh*> meshForRefinementSet;
242

243
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
244
    GET_PARAMETER(0, name + "->mesh", &meshName);
245
    TEST_EXIT(meshName != "")("no mesh name specified\n");
246
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
247
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
248
    TEST_EXIT(dim)("no problem dimension specified!\n");
249

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

283

284
  void ProblemVec::createFeSpace(DOFAdmin *admin)
285
  {
286
    FUNCNAME("ProblemVec::createFeSpace()");
287

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

293
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
294
    traverseInfo.resize(nComponents);
295

296
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
297
      int degree = 1;
298
299
      GET_PARAMETER(0, name + "->polynomial degree[" + 
		    boost::lexical_cast<std::string>(i) + "]","%d", &degree);
300

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

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

307
308
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
309
					  componentMeshes[i], s.str());
310
311
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
	feSpaces.push_back(newFeSpace);
312
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
313
314
315
316
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

    for (int i = 0; i < nComponents; i++) {
317
      for (int j = 0; j < nComponents; j++)
318
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
319
      
320
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
321
322
323
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
326
    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);
327
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
328
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
329
330
331
332
      }
    }
  }

333

334
335
336
337
338
339
  void ProblemVec::createMatricesAndVectors()
  {
    FUNCNAME("ProblemVec::createMatricesAndVectors()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
340
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
341
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
342
343
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
344

Thomas Witkowski's avatar
Thomas Witkowski committed
345
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
346
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
347
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
348
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
349

350
      std::string numberedName = "rhs[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
351
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
352
353

      numberedName = "solution[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
354
355
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
356
357
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
358
359
360
    }
  }

361

362
363
364
365
366
  void ProblemVec::createSolver()
  {
    FUNCNAME("ProblemVec::createSolver()");

    // === create solver ===
367
    std::string solverType("0");
Thomas Witkowski's avatar
Thomas Witkowski committed
368
    GET_PARAMETER(0, name + "->solver", &solverType);
369
370
    OEMSolverCreator *solverCreator = 
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType));
371
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
372
373
374
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
375
376
  }

377

378
379
380
381
382
  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

    // create and set leaf data prototype
Thomas Witkowski's avatar
Thomas Witkowski committed
383
    for (unsigned int i = 0; i < meshes.size(); i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
384
      meshes[i]->setElementDataPrototype
Thomas Witkowski's avatar
Thomas Witkowski committed
385
	(new LeafDataEstimatableVec(new LeafDataCoarsenableVec));
386

Thomas Witkowski's avatar
Thomas Witkowski committed
387
388
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
389
390
      std::string estName = 
	name + "->estimator[" + boost::lexical_cast<std::string>(i) + "]";
391
392

      // === create estimator ===
393
      std::string estimatorType("0");
394
395
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
396
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
397
      if (estimatorCreator) {
398
399
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
400
	if (estimatorType == "recovery")
401
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
402
	    setSolution(solution->getDOFVector(i));       
Thomas Witkowski's avatar
Thomas Witkowski committed
403
	estimator[i] = estimatorCreator->create();
404
405
406
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
407
408
409
410
411
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
412
413
414
    }
  }

415

416
417
418
419
  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

Thomas Witkowski's avatar
Thomas Witkowski committed
420
    int nMarkersCreated = 0;
421

422
    for (int i = 0; i < nComponents; i++) {
423
424
425
      marker[i] = Marker::createMarker
	(name + "->marker[" + boost::lexical_cast<std::string>(i) + "]", i);

426
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
427
428
429
430
	nMarkersCreated++;

	// If there is more than one marker, and all components are defined
	// on the same mesh, the maximum marking has to be enabled.
431
 	if (nMarkersCreated > 1 && nMeshes == 1)
Thomas Witkowski's avatar
Thomas Witkowski committed
432
 	  marker[i]->setMaximumMarking(true);
433
434
435
436
      }
    }
  }

437

438
439
440
441
442
  void ProblemVec::createFileWriter()
  {
    FUNCNAME("ProblemVec::createFileWriter()");
  
    // Create one filewriter for all components of the problem
Thomas Witkowski's avatar
Thomas Witkowski committed
443
    std::string numberedName  = name + "->output";
444
    std::string filename = "";
445
446
447
    GET_PARAMETER(0, numberedName + "->filename", &filename);

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
454
	solutionList[i] = solution->getDOFVector(i);
455
456
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
457
      fileWriters.push_back(new FileWriter(numberedName,
458
459
					   componentMeshes[0],
					   solutionList));
460
461
462
    }

    // Create own filewriters for each components of the problem
463
    for (int i = 0; i < nComponents; i++) {
464
      numberedName = name + "->output[" + boost::lexical_cast<std::string>(i) + "]";
465
466
467
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

468
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
469
	fileWriters.push_back(new FileWriter(numberedName, 
470
					     componentMeshes[i], 
471
					     solution->getDOFVector(i)));      
472
473
474
475
    }

    // Check for serializer
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
476
    GET_PARAMETER(0, name + "->write serialization", "%d", &writeSerialization);
477
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
478
479
      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());
480
481
482
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
483
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
484

485
    if (writeSerialization)
Thomas Witkowski's avatar
Thomas Witkowski committed
486
      fileWriters.push_back(new Serializer<ProblemVec>(this));
487
488
  }

489

490
491
492
493
  void ProblemVec::doOtherStuff()
  {
  }

494

495
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
496
497
498
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
499
    if (!solver) {
500
501
502
503
504
505
506
507
508
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
509
    
510
    solver->solveSystem(solverMatrix, *solution, *rhs);
511

512
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
513
514
    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);
515
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
516
517
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
518
519
#endif

520
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
521
522
523
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
524
525
  }

526

527
528
529
530
531
532
  void ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

    clock_t first = clock();

533
534
535
536
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

537
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
538
      computeError(adaptInfo);
539
540
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
541
	Estimator *scalEstimator = estimator[i];
542
543
	
	if (scalEstimator) {
544
545
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
546
	  scalEstimator->estimate(adaptInfo->getTimestep());
547

548
549
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
550
551
552
553
554

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
555
	}
556
557
558
      }
    }

559
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
560
561
    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);
562
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
563
564
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
565
#endif
566
567
  }

568

569
570
571
572
  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

573
    TEST_EXIT_DBG(static_cast<unsigned int>(nComponents) == marker.size())
Thomas Witkowski's avatar
Thomas Witkowski committed
574
575
      ("Wrong number of markers!\n");

576
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
577
    for (int i = 0; i < nComponents; i++) {
578
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
579
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
580
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
581
582
	WARNING("No marker for component %d\n", i);              
    }
583
    
584
585
586
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
587

588
589
590
591
  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
592
    int nMeshes = static_cast<int>(meshes.size());
593
    Flag refineFlag = 0;
594
595
596
597
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

598
599
600
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
601

602
603
604
605
  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
606
    int nMeshes = static_cast<int>(meshes.size());
607
    Flag coarsenFlag = 0;
608
609
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
610
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
611
612
613
614

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
615

616
617
618
619
  Flag ProblemVec::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    FUNCNAME("ProblemVec::oneIteration()");

620
621
622
623
624
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
	adaptInfo->allowRefinement(false, i);
      else
	adaptInfo->allowRefinement(true, i);	
625
626
627
628

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

Thomas Witkowski's avatar
Thomas Witkowski committed
629

630
631
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
632
633
634
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

635
    if (dualMeshTraverseRequired()) {
636
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
637
638
      return;
    }
639

640
    //    printOpenmpTraverseInfo(this, true);
641
642
    //    std::cout << "ElInfo = " << ElInfo::subElemMatrices.size() << std::endl;

Thomas Witkowski's avatar
Thomas Witkowski committed
643
644
645
646
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


647
    clock_t first = clock();
648
649
650
651
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

652
653
654

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
655
656
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
657
658
659
660
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
661
662
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
663
    if (useGetBound)
664
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
665
666

  
Thomas Witkowski's avatar
Thomas Witkowski committed
667
    traverseInfo.updateStatus();
668
669
670
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

673
674
675
676
677
678
      MSG("%d DOFs for %s\n", 
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());

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

679
      for (int j = 0; j < nComponents; j++) {
680

681
682
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
683

684
685
686
687
	// 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).
688
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
689

690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
	if (writeAsmInfo && matrix) {
	  for (std::vector<Operator*>::iterator it = matrix->getOperatorsBegin();
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
	      std::cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << std::endl;
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
	      std::cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << std::endl;
	    if (assembler->getFirstOrderAssembler(GRD_PHI)) 
	      std::cout << "FOA GRD_PHI: " << assembler->getFirstOrderAssembler(GRD_PHI)->getName() << std::endl;
	    if (assembler->getSecondOrderAssembler()) 
	      std::cout << "SOA: " << assembler->getSecondOrderAssembler()->getName() << std::endl;
	  }
	}

705
706
707
	if (matrix) 
	  matrix->calculateNnz();
	
708
709
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
710
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
711
	  assembleMatrix = false;
712
713
714
715
716
717
718
	} else if (matrix) {
	  matrix->getBaseMatrix().
	    change_dim(componentSpaces[i]->getAdmin()->getUsedSize(), 
		       componentSpaces[j]->getAdmin()->getUsedSize());

	  set_to_zero(matrix->getBaseMatrix());
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
719
720

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

724
725
726
	// 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.
727
728
729
730
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

731
	  continue;
732
	}
733

734
735
736
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

737
738
739
740
741
742
743
744
	// 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);
      
	assembledMatrix[i][j] = true;
Thomas Witkowski's avatar
Thomas Witkowski committed
745

Thomas Witkowski's avatar
Thomas Witkowski committed
746
	
747
748
	if (assembleMatrix)
	  matrix->finishInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
749

750
751
752
753
 	if (assembleMatrix && matrix->getBoundaryManager())
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
	
	if (matrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
754
	  nnz += matrix->getBaseMatrix().nnz();
755
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
756

757
758
      // And now assemble boundary conditions on the vectors
      assembleBoundaryConditions(rhs->getDOFVector(i),
Thomas Witkowski's avatar
Thomas Witkowski committed
759
760
761
 				 solution->getDOFVector(i),
 				 componentMeshes[i],
 				 assembleFlag);     
762
    }  
763

764
    if (asmMatrix) {
765
      solverMatrix.setMatrix(*systemMatrix);
766
      createPrecon();
Thomas Witkowski's avatar
Thomas Witkowski committed
767

768
769
      INFO(info, 8)("fillin of assembled matrix: %d\n", nnz);
    }
770

771
772
773
774
775
776
777
778
#ifdef _OPENMP
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		  TIME_USED(first, clock()), omp_get_wtime() - wtime);
#else
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
		  TIME_USED(first, clock()));    
#endif    
  }
779

780
781
782
783
784
785
786
787
788
789
790

  bool ProblemVec::dualMeshTraverseRequired()
  {
    FUNCNAME("ProblemVec::dualMeshTraverseRequired()");

    TEST_EXIT(meshes.size() <= 2)("More than two mneshes are not supported yet!\n");    

    return (meshes.size() == 2);
  }
  

791
792
  void ProblemVec::dualAssemble(AdaptInfo *adaptInfo, Flag flag, 
				bool asmMatrix, bool asmVector)
793
794
795
  {
    FUNCNAME("ProblemVec::dualAssemble()");

796
797
    TEST_EXIT(asmVector)("Not yet implemented!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
798
799
800
801
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


802
803
804
805
806
    clock_t first = clock();
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
807
    
808
809
810
811
    Flag assembleFlag = 
      flag | 
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
812
813
814
815
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
816
817
818
819
      Mesh::FILL_NEIGH;

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

821
822
823
824
825
826
827
828
829
830
831
832
833
834
    traverseInfo.updateStatus();

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

    for (int i = 0; i < nComponents; i++) {

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

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

      for (int j = 0; j < nComponents; j++) {
835
	
836
837
838
839
840
841
842
	if (writeAsmInfo)
	  std::cout << "-------" << i << " " << j << "----------------" << std::endl;

	// 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).
843
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
844
845
846
847
848
849
850
851
852
853
854
855
856

	if (writeAsmInfo && matrix) {
	  for (std::vector<Operator*>::iterator it = matrix->getOperatorsBegin();
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
	      std::cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << std::endl;
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
	      std::cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << std::endl;
	    if (assembler->getFirstOrderAssembler(