ProblemVec.cc 49.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


Thomas Witkowski's avatar
Thomas Witkowski committed
13
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
14
#include <boost/lexical_cast.hpp>
15
16
17
18
19
20
21
22
23
24
25
#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"
26
#include "io/FileWriter.h"
27
28
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
29
#include "DualTraverse.h"
30
31
32
33
34
35
#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
36
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
37
#include "TraverseParallel.h"
38
39
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
40
#include "ProblemVecDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
41
#include "Debug.h"
42
43
44

namespace AMDiS {

45
46
  using boost::lexical_cast;

47
48
49
50
51
  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
52

53
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
54
    if (meshes.size() != 0) { 
55
56
57
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
58
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
59
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
60
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
61

62
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
63
	  (adoptFlag.isSet(INIT_MESH) ||
64
65
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
66
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
67
	componentMeshes = adoptProblem->componentMeshes;
Thomas Witkowski's avatar
Thomas Witkowski committed
68
69
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
70
71
72

	// 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
73
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
74
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
75
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
76
	  componentMeshes.resize(nComponents);
77
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
78
	    componentMeshes[i] = componentMeshes[0];
79
80
	}

81
82
83
84

	// If the problem adopts the mesh but creates an own FE space.
	if (initFlag.isSet(INIT_FE_SPACE)) {
	}
85
86
87
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
88
    if (meshes.size() == 0) 
89
90
91
      WARNING("no mesh created\n");

    // === create fespace ===
92
    if (feSpaces.size() != 0) {
93
94
95
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
96
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
97
	createFeSpace(NULL);
98

99
100
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
101
	feSpaces = adoptProblem->getFeSpaces();
102
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
103
	traverseInfo = adoptProblem->traverseInfo;
104
105
106

	// 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
107
108
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
109
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
110
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
111
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
112
	    componentSpaces[i] = componentSpaces[0];
113
114
	}

115
116
117
      }
    }

118
    if (feSpaces.size() == 0) 
119
120
121
      WARNING("no feSpace created\n");

    // === create system ===
122
    if (initFlag.isSet(INIT_SYSTEM)) 
123
      createMatricesAndVectors();
124
    
125
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
126
      solution = adoptProblem->getSolution();
127
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
128
      systemMatrix = adoptProblem->getSystemMatrix();
129
130
131
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
132
    if (solver) {
133
134
135
136
137
138
      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
139
140
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
141
142
143
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
144
    if (!solver) 
145
146
147
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
148
    if (initFlag.isSet(INIT_ESTIMATOR))
149
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
150
151

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
152
      estimator = adoptProblem->getEstimators();
153
154

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
155
    if (initFlag.isSet(INIT_MARKER))
156
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
157

Thomas Witkowski's avatar
Thomas Witkowski committed
158
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
159
      marker = adoptProblem->getMarkers();
160
161

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
162
    if (initFlag.isSet(INIT_FILEWRITER))
163
164
165
166
167
168
169
170
171
172
173
      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;
174
    std::string serializationFilename = "";
175
176
177
178
179
180
181
    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;

182
      GET_PARAMETER(0, name + "->input->read serialization", "%d", &readSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
183
      GET_PARAMETER(0, name + "->input->serialization with adaptinfo", "%d",
184
185
186
187
188
189
190
		    &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
191
	GET_PARAMETER(0, name + "->input->serialization filename", 
192
193
194
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

195
196
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
197
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
198
	std::ifstream in(serializationFilename.c_str());
199
200
	deserialize(in);
	in.close();
201
202
203

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

	deserialized = true;
206
      } else {
207
	int globalRefinements = 0;
208

209
	// If AMDiS is compiled for parallel computations, the global refinements are
210
211
212
	// 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
213
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
214
		      &globalRefinements);
215
#endif
216

217
	bool initMesh = initFlag.isSet(INIT_MESH);
218

Thomas Witkowski's avatar
Thomas Witkowski committed
219
220
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
221
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
222
	    meshes[i]->initialize();	    	
223
224
225
226

	// === read value file and use it for the mesh values ===
	std::string valueFilename("");
	GET_PARAMETER(0, meshes[0]->getName() + "->value file name", &valueFilename); 
227
	if (valueFilename.length())
228
229
230
231
232
233
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
234

Thomas Witkowski's avatar
Thomas Witkowski committed
235
	for (unsigned int i = 0; i < meshes.size(); i++)
236
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
237
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
238
239
240
241
242
243
      }
    }

    doOtherStuff();
  }

244

245
246
247
248
  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
249
    componentMeshes.resize(nComponents);
250
    std::map<int, Mesh*> meshForRefinementSet;
251

252
    std::string meshName("");
Thomas Witkowski's avatar
Thomas Witkowski committed
253
    GET_PARAMETER(0, name + "->mesh", &meshName);
254
    TEST_EXIT(meshName != "")("no mesh name specified\n");
255
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
256
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
257
    TEST_EXIT(dim)("no problem dimension specified!\n");
258

259
    for (int i = 0; i < nComponents; i++) {
260
      int refSet = -1;
261
262
      GET_PARAMETER(0, name + "->refinement set[" + 
		       lexical_cast<std::string>(i) + "]", "%d", &refSet);
263
      if (refSet < 0)
264
	refSet = 0;
265
      
266
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
267
	Mesh *newMesh = new Mesh(meshName, dim);
268
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
269
270
	meshes.push_back(newMesh);
	nMeshes++;
271
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
272
      componentMeshes[i] = meshForRefinementSet[refSet];
273
    }
274
275

    switch (dim) {
276
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
277
278
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
279
280
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
281
282
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
283
284
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
285
286
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
287
288
289
290
291
292
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

293

294
  void ProblemVec::createFeSpace(DOFAdmin *admin)
295
  {
296
    FUNCNAME("ProblemVec::createFeSpace()");
297

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

303
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
304
    traverseInfo.resize(nComponents);
305

306
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
307
      int degree = 1;
308
309
      GET_PARAMETER(0, name + "->polynomial degree[" + 
		    boost::lexical_cast<std::string>(i) + "]","%d", &degree);
310

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

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

317
318
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
319
					  componentMeshes[i], s.str());
320
321
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
	feSpaces.push_back(newFeSpace);
322
      }
323

Thomas Witkowski's avatar
Thomas Witkowski committed
324
325
326
327
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

    for (int i = 0; i < nComponents; i++) {
328
      for (int j = 0; j < nComponents; j++)
329
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
330
      
331
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
332
333
334
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
335
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
336
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
337
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
338
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
339
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
340
341
342
343
      }
    }
  }

344

345
346
347
348
349
350
  void ProblemVec::createMatricesAndVectors()
  {
    FUNCNAME("ProblemVec::createMatricesAndVectors()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
351
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
352
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
353
354
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
355

Thomas Witkowski's avatar
Thomas Witkowski committed
356
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
357
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
358
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
359
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
360

361
      std::string numberedName = "rhs[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
362
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], numberedName));
363
364

      numberedName = "solution[" + boost::lexical_cast<std::string>(i) + "]";
Thomas Witkowski's avatar
Thomas Witkowski committed
365
366
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
      						      numberedName));
Thomas Witkowski's avatar
Thomas Witkowski committed
367
368
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
369
370
371
    }
  }

372

373
374
375
376
377
  void ProblemVec::createSolver()
  {
    FUNCNAME("ProblemVec::createSolver()");

    // === create solver ===
378
    std::string solverType("0");
Thomas Witkowski's avatar
Thomas Witkowski committed
379
    GET_PARAMETER(0, name + "->solver", &solverType);
380
381
    OEMSolverCreator *solverCreator = 
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType));
382
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
383
384
385
    solverCreator->setName(name + "->solver");
    solver = solverCreator->create();
    solver->initParameters();
386
387
  }

388

389
390
391
392
393
  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
398
399
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
400
401
      std::string estName = 
	name + "->estimator[" + boost::lexical_cast<std::string>(i) + "]";
402
403

      // === create estimator ===
404
      std::string estimatorType("0");
405
406
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
Thomas Witkowski's avatar
nix.  
Thomas Witkowski committed
407
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
408
      if (estimatorCreator) {
409
410
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
411
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
412
	estimator[i] = estimatorCreator->create();
413
414
415
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
416
417
418
419
420
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
421
422
423
    }
  }

424

425
426
427
428
  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

Thomas Witkowski's avatar
Thomas Witkowski committed
429
    int nMarkersCreated = 0;
430

431
    for (int i = 0; i < nComponents; i++) {
432
433
434
      marker[i] = Marker::createMarker
	(name + "->marker[" + boost::lexical_cast<std::string>(i) + "]", i);

435
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
436
437
438
439
	nMarkersCreated++;

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

446

447
448
449
450
451
  void ProblemVec::createFileWriter()
  {
    FUNCNAME("ProblemVec::createFileWriter()");
  
    // Create one filewriter for all components of the problem
Thomas Witkowski's avatar
Thomas Witkowski committed
452
    std::string numberedName  = name + "->output";
453
    std::string filename = "";
454
455
456
    GET_PARAMETER(0, numberedName + "->filename", &filename);

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
463
	solutionList[i] = solution->getDOFVector(i);
464
465
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
466
      fileWriters.push_back(new FileWriter(numberedName,
467
468
					   componentMeshes[0],
					   solutionList));
469
470
471
    }

    // Create own filewriters for each components of the problem
472
    for (int i = 0; i < nComponents; i++) {
473
      numberedName = name + "->output[" + boost::lexical_cast<std::string>(i) + "]";
474
475
476
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

477
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
478
	fileWriters.push_back(new FileWriter(numberedName, 
479
					     componentMeshes[i], 
480
					     solution->getDOFVector(i)));      
481
482
483
484
    }

    // Check for serializer
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
485
    GET_PARAMETER(0, name + "->write serialization", "%d", &writeSerialization);
486
    if (writeSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
487
488
      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());
489
490
491
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

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

494
    if (writeSerialization)
Thomas Witkowski's avatar
Thomas Witkowski committed
495
      fileWriters.push_back(new Serializer<ProblemVec>(this));
496
497
  }

498

499
500
501
502
  void ProblemVec::doOtherStuff()
  {
  }

503

504
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
505
506
507
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
508
    if (!solver) {
509
510
511
512
513
514
515
516
517
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
518
    
519
    solver->solveSystem(solverMatrix, *solution, *rhs);
520

521
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
522
523
    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);
524
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
525
526
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
527
528
#endif

529
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
530
531
532
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
533
534
  }

535

536
537
538
539
540
541
  void ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

    clock_t first = clock();

542
543
544
545
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

546
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
547
      computeError(adaptInfo);
548
549
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
550
	Estimator *scalEstimator = estimator[i];
551
552
	
	if (scalEstimator) {
553
554
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
555
	  scalEstimator->estimate(adaptInfo->getTimestep());
556

557
558
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
559
560
561
562
563

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
564
	}
565
566
567
      }
    }

568
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
569
570
    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);
571
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
572
573
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
574
#endif
575
576
  }

577

578
579
580
581
  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

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

585
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
586
    for (int i = 0; i < nComponents; i++) {
587
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
588
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
589
      else
Thomas Witkowski's avatar
Thomas Witkowski committed
590
591
	WARNING("No marker for component %d\n", i);              
    }
592
    
593
594
595
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
596

597
598
599
600
  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
601
    int nMeshes = static_cast<int>(meshes.size());
602
    Flag refineFlag = 0;
603
604
605
606
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

607
608
609
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
610

611
612
613
614
  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
615
    int nMeshes = static_cast<int>(meshes.size());
616
    Flag coarsenFlag = 0;
617
618
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
619
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
620
621
622
623

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
624

625
626
627
628
  Flag ProblemVec::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    FUNCNAME("ProblemVec::oneIteration()");

629
630
631
632
633
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
	adaptInfo->allowRefinement(false, i);
      else
	adaptInfo->allowRefinement(true, i);	
634
635
636
637

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

Thomas Witkowski's avatar
Thomas Witkowski committed
638

639
640
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
641
642
643
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

644
    if (dualMeshTraverseRequired()) {
645
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
646
647
      return;
    }
648

649
    //    printOpenmpTraverseInfo(this, true);
650
651
    //    std::cout << "ElInfo = " << ElInfo::subElemMatrices.size() << std::endl;

Thomas Witkowski's avatar
Thomas Witkowski committed
652
653
654
655
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


656
    clock_t first = clock();
657
658
659
660
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

661
662
663

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
664
665
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
666
667
668
669
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
670
671
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
672
    if (useGetBound)
673
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
674
675

  
Thomas Witkowski's avatar
Thomas Witkowski committed
676
    traverseInfo.updateStatus();
677
678
679
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

682
      MSG("%d DOFs for %s\n", 
683
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
684
685
686
687
	  componentSpaces[i]->getName().c_str());

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

688
      for (int j = 0; j < nComponents; j++) {
689

690
691
	if (writeAsmInfo)
	  MSG("--------- %d %d -------------\n", i, j);
692

693
694
695
696
	// 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).
697
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
698

699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
	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;
	  }
	}

714
715
716
	if (matrix) 
	  matrix->calculateNnz();
	
717
718
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
719
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
720
	  assembleMatrix = false;
721
722
723
724
725
726
727
	} 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
728
729

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

733
734
735
	// 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.
736
737
738
739
	if (!assembleMatrix && i != j) {
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();

740
	  continue;
741
	}
742

743
744
745
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

746
747
748
749
750
751
752
753
	// 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
754

Thomas Witkowski's avatar
Thomas Witkowski committed
755
	
756
757
	if (assembleMatrix)
	  matrix->finishInsertion();
Thomas Witkowski's avatar
Thomas Witkowski committed
758

759
760
761
762
 	if (assembleMatrix && matrix->getBoundaryManager())
 	  matrix->getBoundaryManager()->exitMatrix(matrix);	
	
	if (matrix)
Thomas Witkowski's avatar
Thomas Witkowski committed
763
	  nnz += matrix->getBaseMatrix().nnz();
764
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
765

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

773
    if (asmMatrix) {
774
      solverMatrix.setMatrix(*systemMatrix);
775
      createPrecon();
Thomas Witkowski's avatar
Thomas Witkowski committed
776

777
      INFO(info, 8)("fillin of assembled matrix: %d\n", nnz);
778
    }
779

780
781
782
783
784
785
786
787
#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    
  }
788

789
790
791
792
793

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

794
    TEST_EXIT(meshes.size() <= 2)("More than two meshes are not yet supported!\n");
795
796
797
798
799

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

800
801
  void ProblemVec::dualAssemble(AdaptInfo *adaptInfo, Flag flag, 
				bool asmMatrix, bool asmVector)
802
803
804
  {
    FUNCNAME("ProblemVec::dualAssemble()");

805
806
    TEST_EXIT(asmVector)("Not yet implemented!\n");

Thomas Witkowski's avatar
Thomas Witkowski committed
807
808
809
810
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


811
812
813
814
815
    clock_t first = clock();
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
816
    
817
818
819
820
    Flag assembleFlag = 
      flag | 
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
821
822
823
824
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |