ProblemVec.cc 44.8 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
81
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
	createFESpace(NULL);
82

83
84
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
85
86
	feSpaces = adoptProblem->getFESpaces();
	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
111
112
      solution = adoptProblem->getSolution();
      rhs = adoptProblem->getRHS();
      systemMatrix = adoptProblem->getSystemMatrix();
113
114
115
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
116
    if (solver) {
117
118
119
120
121
122
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
123
124
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
125
126
127
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
128
    if (!solver) 
129
130
131
      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))
Thomas Witkowski's avatar
Thomas Witkowski committed
136
      estimator = adoptProblem->getEstimator();
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->getMarker();
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
      } else {
188
	int globalRefinements = 0;
189
190
191
192
193

	// 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
194
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
195
		      &globalRefinements);
196
#endif
197

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

	// === 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
210
	if (valueFilename.length()) {
211
212
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 ===
	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
221
	    refinementManager->globalRefine(meshes[i], globalRefinements);
222
223
224
225
226
227
228
229
230
231
      }
    }

    doOtherStuff();
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
232
    componentMeshes.resize(nComponents);
233
    std::map<int, Mesh*> meshForRefinementSet;
234

235
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
236
    GET_PARAMETER(0, name + "->mesh", &meshName);
237
    TEST_EXIT(meshName != "")("no mesh name specified\n");
238
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
239
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
240
    TEST_EXIT(dim)("no problem dimension specified!\n");
241

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

275
  void ProblemVec::createFESpace(DOFAdmin *admin)
276
277
278
  {
    FUNCNAME("ProblemVec::createFESpace()");

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

284
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
285
    traverseInfo.resize(nComponents);
286

287
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
288
      int degree = 1;
289
290
      GET_PARAMETER(0, name + "->polynomial degree[" + 
		    boost::lexical_cast<std::string>(i) + "]","%d", &degree);
291

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

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

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

    for (int i = 0; i < nComponents; i++) {
308
      for (int j = 0; j < nComponents; j++)
Thomas Witkowski's avatar
Thomas Witkowski committed
309
	traverseInfo.getMatrix(i, j).setFESpace(componentSpaces[i], componentSpaces[j]);
310
      
Thomas Witkowski's avatar
Thomas Witkowski committed
311
      traverseInfo.getVector(i).setFESpace(componentSpaces[i]);
312
313
314
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
315
316
317
    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);
318
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
319
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
320
321
322
323
324
325
326
327
328
329
      }
    }
  }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
330
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
331
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
332
333
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
334

Thomas Witkowski's avatar
Thomas Witkowski committed
335
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
336
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
337
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
338
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
339

340
      std::string numberedName = "rhs[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
341
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
342
343

      numberedName = "solution[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
344
345
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
346
347
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
348
349
350
351
352
353
354
355
    }
  }

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

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

366

367
368
369
370
371
  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
376
377
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
378
379
      std::string estName = 
	name + "->estimator[" + boost::lexical_cast<std::string>(i) + "]";
380
381

      // === create estimator ===
382
      std::string estimatorType("0");
383
384
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
385
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
386
      if (estimatorCreator) {
387
388
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
389
	if (estimatorType == "recovery")
390
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
391
	    setSolution(solution->getDOFVector(i));       
Thomas Witkowski's avatar
Thomas Witkowski committed
392
	estimator[i] = estimatorCreator->create();
393
394
395
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
396
      if (estimator[i]) {
397
398
399
400
401
402
403
404
405
406
	MSG("Estimator fake to test different meshes ...\n");
	WAIT_REALLY;
	estimator[i]->addSystem((*systemMatrix)[i][i], 
				solution->getDOFVector(i), 
				rhs->getDOFVector(i));

	// for (int j = 0; j < nComponents; j++)
	//   estimator[i]->addSystem((*systemMatrix)[i][j], 
	// 			   solution->getDOFVector(j), 
	// 			   rhs->getDOFVector(j));
407
408
409
410
      }
    }
  }

411

412
413
414
415
  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

Thomas Witkowski's avatar
Thomas Witkowski committed
416
    int nMarkersCreated = 0;
417

418
    for (int i = 0; i < nComponents; i++) {
419
420
421
      marker[i] = Marker::createMarker
	(name + "->marker[" + boost::lexical_cast<std::string>(i) + "]", i);

422
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
423
424
425
426
	nMarkersCreated++;

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
449
	solutionList[i] = solution->getDOFVector(i);
450
451
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
452
      fileWriters.push_back(new FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
453
					    componentMeshes[0],
454
455
456
457
					    solutionList));
    }

    // Create own filewriters for each components of the problem
458
    for (int i = 0; i < nComponents; i++) {
459
      numberedName = name + "->output[" + boost::lexical_cast<std::string>(i) + "]";
460
461
462
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

463
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
464
	fileWriters.push_back(new FileWriter(numberedName, 
465
466
					     componentMeshes[i], 
					     solution->getDOFVector(i)));
467
468
469
470
    }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
478
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);
479
480
481
482

    // Serialization is not allowed to be done by the problem, if its part of a parallel
    // problem definition. Than, the parallel problem object must be serialized.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
483
    if (writeSerialization)
Thomas Witkowski's avatar
Thomas Witkowski committed
484
      fileWriters.push_back(new Serializer<ProblemVec>(this));
485
#endif
486
487
488
489
490
491
  }

  void ProblemVec::doOtherStuff()
  {
  }

492
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
493
494
495
  {
    FUNCNAME("Problem::solve()");

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

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

    clock_t first = clock();
506
    
507
    solver->solveSystem(solverMatrix, *solution, *rhs);
508

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

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

523

524
525
526
527
528
529
  void ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

    clock_t first = clock();

530
531
532
533
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

534
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
535
      computeError(adaptInfo);
536
537
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
538
	Estimator *scalEstimator = estimator[i];
539
540
541
542
543
544
545
546
	
	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 {
547
	  WARNING("No estimator for component %d\n" , i);
548
	}
549
550
551
      }
    }

552
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
553
554
    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);
555
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
556
557
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
558
#endif
559
560
  }

561

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

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

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

590
591
592
593
594
595
596
    return refineFlag;
  }

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

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

    return coarsenFlag;
  }

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

Thomas Witkowski's avatar
Thomas Witkowski committed
610
    if (allowFirstRef) {
Thomas Witkowski's avatar
Thomas Witkowski committed
611
      for (int i = 0; i < nComponents; i++)
612
	adaptInfo->allowRefinement(true, i);
Thomas Witkowski's avatar
Thomas Witkowski committed
613

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

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

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

631
    //    printOpenmpTraverseInfo(this, true);
632

633
    clock_t first = clock();
634
635
636
637
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
651
    if (useGetBound)
652
      assembleFlag |= Mesh::FILL_BOUND;
653
   
Thomas Witkowski's avatar
Thomas Witkowski committed
654
655
    traverseInfo.updateStatus();

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

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

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

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

667
      for (int j = 0; j < nComponents; j++) {
668

669
670
	std::cout << "-------" << i << " " << j << "----------------" << std::endl;

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

677
678
679
	if (matrix) 
	  matrix->calculateNnz();
	
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;
684
685
686
687
688
689
690
	} 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
691
692

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

696
697
698
	// 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.
699
700
701
702
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

703
	  continue;
704
	}
705

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

Thomas Witkowski's avatar
Thomas Witkowski committed
709
710
711
712
713
714
715
716
717
718
719
	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.

720
	    //	    std::cout << "ASM 1" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
721
722
723
724
725
726
727
728
	    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
736
737
738
739
740
741
742
743
744
	    //	    std::cout << "ASM 2" << std::endl;

	    if (assembleMatrix)
	      assembleOnOneMesh(componentSpaces[i],
				assembleFlag,
				assembleMatrix ? matrix : NULL,
				((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
	    
	    if (i == j && asmVector)
	      assembleOnDifMeshes2(componentSpaces[i], 
				   traverseInfo.getAuxFESpace(i),
				   assembleFlag,
				   NULL,
				   rhs->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
745
746
747
748
749
	  } else {
	    ERROR_EXIT("Possible? If yes, not yet implemented!\n");
	  }

	} else if (traverseInfo.getStatus(i, j) == SingleComponentInfo::EQ_SPACES_WITH_DIF_AUX) {
750
	  //	  	    std::cout << "ASM 4" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
751
752
753
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
754
			    ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
755
	  //	  	    std::cout << "ASM 5" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
756
757
758
759
	  assembleOnDifMeshes2(componentSpaces[i],
			       traverseInfo.getAuxFESpace(i, j),
			       assembleFlag,
			       assembleMatrix ? matrix : NULL,
760
			       ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
761
762
763

	} else if (traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_NO_AUX ||
		   traverseInfo.getStatus(i, j) ==  SingleComponentInfo::DIF_SPACES_WITH_AUX) {
764
	  //	    std::cout << "ASM 6" << std::endl;
Thomas Witkowski's avatar
Thomas Witkowski committed
765
766
767
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
Thomas Witkowski's avatar
Thomas Witkowski committed
768
769
770
			      ((i == j) && asmVector) ? rhs->getDOFVector(i) : NULL);	  
	} else {
	  ERROR_EXIT("Not yet implemented!\n");
771
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
772

Thomas Witkowski's avatar
Thomas Witkowski committed
773
	assembledMatrix[i][j] = true;
774
775
776

	if (assembleMatrix)
	  matrix->finishInsertion();
777
778

 	if (assembleMatrix && matrix->getBoundaryManager())
779
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
780
781
	
	if (matrix)
782
	  nnz += matrix->getBaseMatrix().nnz();	  	
783
784
      }

785
      // And now assemble boundary conditions on the vectors
Thomas Witkowski's avatar
Thomas Witkowski committed
786
787
      assembleBoundaryConditions(rhs->getDOFVector(i),
				 solution->getDOFVector(i),
788
				 componentMeshes[i],
789
				 assembleFlag);     
790
    }
791

792
793
    //    exit(0);

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
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", 
805
		  TIME_USED(first, clock()));    
806
#endif    
807
808
  }

809

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

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

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

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

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


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

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

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

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

857

858
859
860
861
862
  void ProblemVec::writeFiles(AdaptInfo &adaptInfo, bool force) 
  {
    writeFiles(&adaptInfo, force);
  }

863