ProblemVec.cc 49.3 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
#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"
14
#include "io/FileWriter.h"
15 16
#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"
26 27
#include "io/VtkWriter.h"
#include "io/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 72

	// If the problem adopts the mesh but creates an own FE space.
	if (initFlag.isSet(INIT_FE_SPACE)) {
	}
73 74 75
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
76
    if (meshes.size() == 0) 
77 78 79
      WARNING("no mesh created\n");

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

87 88
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
89
	feSpaces = adoptProblem->getFeSpaces();
90
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
91
	traverseInfo = adoptProblem->traverseInfo;
92 93 94

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

103 104 105
      }
    }

106
    if (feSpaces.size() == 0) 
107 108 109
      WARNING("no feSpace created\n");

    // === create system ===
110
    if (initFlag.isSet(INIT_SYSTEM)) 
111
      createMatricesAndVectors();
112
    
113
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
114
      solution = adoptProblem->getSolution();
115
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
116
      systemMatrix = adoptProblem->getSystemMatrix();
117 118 119
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
120
    if (solver) {
121 122 123 124 125 126
      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
127 128
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
129 130 131
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
132
    if (!solver) 
133 134 135
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
136
    if (initFlag.isSet(INIT_ESTIMATOR))
137
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
138 139

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
140
      estimator = adoptProblem->getEstimators();
141 142

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
143
    if (initFlag.isSet(INIT_MARKER))
144
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
145

Thomas Witkowski's avatar
Thomas Witkowski committed
146
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
147
      marker = adoptProblem->getMarkers();
148 149

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

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

183 184
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
185
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
186
	std::ifstream in(serializationFilename.c_str());
187 188
	deserialize(in);
	in.close();
189 190 191

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

	deserialized = true;
194
      } else {
195
	int globalRefinements = 0;
196

197
	// If AMDiS is compiled for parallel computations, the global refinements are
198 199 200
	// 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
201
	GET_PARAMETER(0, meshes[0]->getName() + "->global refinements", "%d", 
202
		      &globalRefinements);
203
#endif
204

205
	bool initMesh = initFlag.isSet(INIT_MESH);
206

Thomas Witkowski's avatar
Thomas Witkowski committed
207 208
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
209
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
210
	    meshes[i]->initialize();	    	
211 212 213 214

	// === read value file and use it for the mesh values ===
	std::string valueFilename("");
	GET_PARAMETER(0, meshes[0]->getName() + "->value file name", &valueFilename); 
215
	if (valueFilename.length())
216 217 218 219 220 221
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
222

Thomas Witkowski's avatar
Thomas Witkowski committed
223
	for (unsigned int i = 0; i < meshes.size(); i++)
224
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
225
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
226 227 228 229 230 231
      }
    }

    doOtherStuff();
  }

232

233 234 235 236
  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
237
    componentMeshes.resize(nComponents);
238
    std::map<int, Mesh*> meshForRefinementSet;
239

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

247
    for (int i = 0; i < nComponents; i++) {
248
      int refSet = -1;
249 250
      GET_PARAMETER(0, name + "->refinement set[" + 
		       lexical_cast<std::string>(i) + "]", "%d", &refSet);
251
      if (refSet < 0)
252
	refSet = 0;
253
      
254
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
255
	Mesh *newMesh = new Mesh(meshName, dim);
256
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
257 258
	meshes.push_back(newMesh);
	nMeshes++;
259
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
260
      componentMeshes[i] = meshForRefinementSet[refSet];
261
    }
262 263

    switch (dim) {
264
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
265 266
      coarseningManager = new CoarseningManager1d();
      refinementManager = new RefinementManager1d();
267 268
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
269 270
      coarseningManager = new CoarseningManager2d();
      refinementManager = new RefinementManager2d();
271 272
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
273 274
      coarseningManager = new CoarseningManager3d();
      refinementManager = new RefinementManager3d();
275 276 277 278 279 280
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

281

282
  void ProblemVec::createFeSpace(DOFAdmin *admin)
283
  {
284
    FUNCNAME("ProblemVec::createFeSpace()");
285

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

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

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

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
312 313 314 315
      componentSpaces[i] = feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
    }

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

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
323
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
324
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
325
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
326
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
327
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
328 329 330 331
      }
    }
  }

332

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

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

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

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

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

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

360

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

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

376

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

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

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

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


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

414

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

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

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

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

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

436

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

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

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

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

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

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

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

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

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

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

488

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

493

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

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

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

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

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

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

525

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

    clock_t first = clock();

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

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

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

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

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

567

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
586

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

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

597 598 599
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
600

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

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

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
614

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
628

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

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

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

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


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

651 652 653

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

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

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

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

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

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

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

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

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

689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
	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;
	  }
	}

704 705 706
	if (matrix) 
	  matrix->calculateNnz();
	
707 708
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
709
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
710
	  assembleMatrix = false;
711 712 713 714 715 716 717
	} 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
718 719

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

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

730
	  continue;
731
	}
732

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

736 737 738 739 740 741 742 743
	// 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
744

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

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

756 757
      // And now assemble boundary conditions on the vectors
      assembleBoundaryConditions(rhs->getDOFVector(i),
Thomas Witkowski's avatar
Thomas Witkowski committed
758 759 760
 				 solution->getDOFVector(i),
 				 componentMeshes[