ProblemVec.cc 49.5 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
	if (estimatorType == "recovery")
412
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
413
	    setSolution(solution->getDOFVector(i));       
Thomas Witkowski's avatar
Thomas Witkowski committed
414
	estimator[i] = estimatorCreator->create();
415
416
417
      }


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

426

427
428
429
430
  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

Thomas Witkowski's avatar
Thomas Witkowski committed
431
    int nMarkersCreated = 0;
432

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

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

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

448

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

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

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

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

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

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

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

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

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

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

500

501
502
503
504
  void ProblemVec::doOtherStuff()
  {
  }

505

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

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

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

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

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

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

537

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

    clock_t first = clock();

544
545
546
547
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

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

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

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

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

579

580
581
582
583
  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
598

599
600
601
602
  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

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

609
610
611
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
612

613
614
615
616
  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

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

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
626

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
640

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

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

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

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


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

663
664
665

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

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

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

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

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

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

690
      for (int j = 0; j < nComponents; j++) {
691

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

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

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

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

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

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

742
	  continue;
743
	}
744

745
746
747
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

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

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

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

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

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

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

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

791
792
793
794
795

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

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

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

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

807
808
    TEST_EXIT(asmVector)("Not yet implemented!\n");

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


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

Thomas Witkowski's avatar
Thomas Witkowski committed
818
    
819
820
821
822
    Flag assembleFlag = 
      flag | 
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
823
824
825
826
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
827
828
829
830
      Mesh::FILL_NEIGH;

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

832
833
    traverseInfo.updateStatus();

834
835
836
837
838
839
840
841
842
843
844
845
    if (writeAsmInfo) {
      MSG("TraverseInfo:\n");
      for (int i = 0; i < nComponents; i++) {
	MSG("  component %d:   difAuxSpace = %d\n", i, traverseInfo.difAuxSpace(i));
	
	for (int j = 0; j < nComponents; j++) {
	  MSG("  component %d-%d:  difAuxSpace = %d\n", 
	      i, j, traverseInfo.difAuxSpace(i, j));
	}
      }
    }

846
847
848
849
850
851
852
853
854
855
856
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

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

      for (int j = 0; j < nComponents; j++) {
857
	
858
859
860
861
862
863
864
	if (writeAsmInfo)
	  std::cout << "-------" << i << " " << j << "----------------" << std::endl;

	// Only if this variable is true, the current matrix will be assembled.	
	bool assembleMatrix = true;
	// The DOFMatrix which should be assembled (or not, if assembleMatrix
	// will be set to false).
865
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
866
867
868
869
870
871
872
873
874
875
876
877
878

	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;
Thomas Witkowski's avatar
Thomas Witkowski committed
879
	  }
880
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
881

882
883
884
885
886
887
888
889
890
891
892
	if (matrix) 
	  matrix->calculateNnz();
	
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
	if (assembleMatrixOnlyOnce[i][j] && assembledMatrix[i][j]) {
	  assembleMatrix = false;
	} else if (matrix) {
	  matrix->getBaseMatrix().
	    change_dim(componentSpaces[i]->getAdmin()->getUsedSize(), 
		       componentSpaces[j]->getAdmin()->getUsedSize());
893

894
895
	  set_to_zero(matrix->getBaseMatrix());
	  matrix->startInsertion(matrix->getNnz());
896

897
898
899
	  if (matrix->getBoundaryManager())
	    matrix->getBoundaryManager()->initMatrix(matrix);
	}
900

901
902
903
	// If there is no DOFMatrix, e.g., if it is completly 0, do not assemble.
	if (!matrix || !assembleMatrix)
	  assembleMatrix = false;
Thomas Witkowski's avatar
Thomas Witkowski committed
904

905
906
907
908
909
910
	// If the matrix should not be assembled, the rhs vector has to be considered.
	// This will be only done, if i == j. So, if both is not true, we can jump
	// to the next matrix.
	if (!assembleMatrix && i != j)
	  if (matrix)
	    nnz += matrix->getBaseMatrix().nnz();
911
912
913
914

	if (matrix && !assembleMatrix) {
	  ERROR_EXIT("Not yet implemented!\n");
	}
915
916
      }
    }
917

918
    TEST_EXIT(meshes.size() == 2)("There is something wrong!\n");