ProblemStat.cc 50.1 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

#include "ProblemStat.h"
17
18
19
20
21
22
23
24
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Marker.h"
#include "AdaptInfo.h"
25
#include "io/FileWriter.h"
26
27
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
28
#include "DualTraverse.h"
29
30
31
32
33
34
#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
35
#include "Flag.h"
36
#include "est/Estimator.h"
37
38
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
39
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
40
#include "Debug.h"
41
42
43

namespace AMDiS {

44
  using namespace std;
45
46
  using boost::lexical_cast;

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
  ProblemStatSeq::ProblemStatSeq(string nameStr,
				 ProblemIterationInterface *problemIteration)
    : StandardProblemIteration(this),
      name(nameStr),
      nComponents(-1),
      nMeshes(0),
      traverseInfo(0),
      solver(NULL),
      solution(NULL),
      rhs(NULL),
      systemMatrix(NULL),
      useGetBound(true),
      refinementManager(NULL),
      coarseningManager(NULL),
      info(10),
      deserialized(false),
      computeExactError(false),
      boundaryConditionSet(false),
      writeAsmInfo(false)
  {
    Parameters::get(name + "->components", nComponents);
68
69
    TEST_EXIT(nComponents > 0)("No value set for parameter \"%s->components\"!\n",
			       name.c_str());    
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
    estimator.resize(nComponents, NULL);
    marker.resize(nComponents, NULL);
    
    assembleMatrixOnlyOnce.resize(nComponents);
    assembledMatrix.resize(nComponents);
    for (int i = 0; i < nComponents; i++) {
      assembleMatrixOnlyOnce[i].resize(nComponents);
      assembledMatrix[i].resize(nComponents);
      for (int j = 0; j < nComponents; j++) {
	assembleMatrixOnlyOnce[i][j] = false;
	assembledMatrix[i][j] = false;
      }
    }
    
    exactSolutionFcts.resize(nComponents);


    // === Initialize name of components. ===

    componentNames.resize(nComponents, "");
    for (int i = 0; i < nComponents; i++)
      componentNames[i] = "solution[" + lexical_cast<string>(i) + "]";

    Parameters::get(name + "->name", componentNames);
94
95
    componentNames.resize(nComponents);

96
97
98
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
Thomas Witkowski's avatar
Thomas Witkowski committed
99
100

    Parameters::get("debug->write asm info", writeAsmInfo);
101
102
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
103
104
105

  ProblemStatSeq::~ProblemStatSeq()
  {
106
107
    if (rhs)
      delete rhs;
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    rhs = NULL;
109
110
    if (solution)
      delete solution;
Thomas Witkowski's avatar
Thomas Witkowski committed
111
112
    solution = NULL;

113
114
115
116
117
118
119
120
121
122
123
    if (systemMatrix) {    
      for (int i = 0; i < nComponents; i++)
	for (int j = 0; j < nComponents; j++)
	  if ((*systemMatrix)[i][j]) {
	    delete (*systemMatrix)[i][j];
	    (*systemMatrix)[i][j] = NULL;
	  }

      delete systemMatrix;
      systemMatrix = NULL;
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
124
125

    for (unsigned int i = 0; i < meshes.size(); i++)
126
127
128
129
      if (meshes[i]) {
// 	delete meshes[i];
// 	meshes[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
130
131
    
    for (unsigned int i = 0; i < estimator.size(); i++)
132
      if (estimator[i]) {
133
	delete estimator[i];
134
135
	estimator[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
136
137

    for (unsigned int i = 0; i < marker.size(); i++)
138
      if (marker[i]) {
139
	delete marker[i];
140
141
	marker[i] = NULL;
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
142
143
144
  }


145
146
147
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
148
  {
149
    FUNCNAME("ProblemStat::initialize()");
150

151
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
152
    if (meshes.size() != 0) { 
153
154
155
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
156
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
157
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
158
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
159

160
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
161
	  (adoptFlag.isSet(INIT_MESH) ||
162
163
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
164
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
165
	componentMeshes = adoptProblem->componentMeshes;
166
167
168

	// 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
169
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
170
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
171
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
172
	  componentMeshes.resize(nComponents);
173
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
174
	    componentMeshes[i] = componentMeshes[0];
175
	}
176
177
178
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
179
    if (meshes.size() == 0) 
180
181
      WARNING("no mesh created\n");

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
    // === create refinement/corasening-manager ===
    if (refinementManager != NULL && coarseningManager != NULL) { 
      WARNING("refinement-/coarseningmanager already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
          (!adoptFlag.isSet(INIT_MESH) &&
           (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
        createRefCoarseManager();

      if (adoptProblem && 
          (adoptFlag.isSet(INIT_MESH) ||
           adoptFlag.isSet(INIT_SYSTEM) ||
           adoptFlag.isSet(INIT_FE_SPACE))) {
        refinementManager = adoptProblem->refinementManager;
        coarseningManager = adoptProblem->coarseningManager;
      }
    }

    if (refinementManager == NULL || coarseningManager == NULL) 
      WARNING("no refinement-/coarseningmanager created\n");

203
    // === create fespace ===
204
    if (feSpaces.size() != 0) {
205
206
207
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
208
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
209
	createFeSpace(NULL);
210

211
212
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
213
	feSpaces = adoptProblem->getFeSpaces();
214
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
215
	traverseInfo = adoptProblem->traverseInfo;
216
217
218

	// 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
219
220
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
221
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
222
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
223
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
224
	    componentSpaces[i] = componentSpaces[0];
225
226
	}

227
228
229
      }
    }

230
    if (feSpaces.size() == 0) 
231
232
233
      WARNING("no feSpace created\n");

    // === create system ===
234
    if (initFlag.isSet(INIT_SYSTEM)) 
235
      createMatricesAndVectors();
236
    
237
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
238
      solution = adoptProblem->getSolution();
239
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
240
      systemMatrix = adoptProblem->getSystemMatrix();
241
242
243
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
244
    if (solver) {
245
246
      WARNING("solver already created\n");
    } else {
247
      if (initFlag.isSet(INIT_SOLVER))
248
	createSolver();
249
      
250
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
251
252
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
253
254
255
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
256
    if (!solver) 
257
258
259
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
260
    if (initFlag.isSet(INIT_ESTIMATOR))
261
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
262
263

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
264
      estimator = adoptProblem->getEstimators();
265
266

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
267
    if (initFlag.isSet(INIT_MARKER))
268
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
269

Thomas Witkowski's avatar
Thomas Witkowski committed
270
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
271
      marker = adoptProblem->getMarkers();
272
273

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
274
    if (initFlag.isSet(INIT_FILEWRITER))
275
276
277
278
279
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
280
281
282
283
    // 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.
284
285

    int readSerialization = 0;
286
287
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
288
289
290
291
292
293

    // 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;

294
295
296
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
297
298
299
300
301
302

      // 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) {
303
304
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
305
306
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

307
308
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
309
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
310
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
311
	ifstream in(serializationFilename.c_str());
312
313
314
315
316
317

	// Read the revision number of the AMDiS version which was used to create 
	// the serialization file.
	int revNumber = -1;
	SerUtil::deserialize(in, revNumber);

318
319
	deserialize(in);
	in.close();
320
#endif
321
322

	deserialized = true;
323
      } else {
324
	int globalRefinements = 0;
325

326
	// If AMDiS is compiled for parallel computations, the global refinements are
327
328
329
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
330
331
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
332
#endif
333

334
	bool initMesh = initFlag.isSet(INIT_MESH);
335

Thomas Witkowski's avatar
Thomas Witkowski committed
336
337
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
338
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
339
	    meshes[i]->initialize();	    	
340
341

	// === read value file and use it for the mesh values ===
342
343
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
344
	if (valueFilename.length())
345
346
347
348
349
350
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
351

Thomas Witkowski's avatar
Thomas Witkowski committed
352
	for (unsigned int i = 0; i < meshes.size(); i++)
353
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
354
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
355
356
357
358
359
360
      }
    }

    doOtherStuff();
  }

361

362
  void ProblemStatSeq::createMesh() 
363
  {
364
    FUNCNAME("ProblemStat::createMesh()");
365

Thomas Witkowski's avatar
Thomas Witkowski committed
366
    componentMeshes.resize(nComponents);
367
368
369
370
371
372
    map<int, Mesh*> meshForRefinementSet;

    string meshName("");
    Parameters::get(name + "->mesh", meshName);
    TEST_EXIT(meshName != "")("No mesh name specified for \"%s->mesh\"!\n", 
			      name.c_str());
373
374

    int dim = 0;
375
376
377
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
378

379
    for (int i = 0; i < nComponents; i++) {
380
      int refSet = -1;
381
382
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
383
      if (refSet < 0)
384
	refSet = 0;
385
      
386
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
387
	Mesh *newMesh = new Mesh(meshName, dim);
388
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
389
390
	meshes.push_back(newMesh);
	nMeshes++;
391
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
392
      componentMeshes[i] = meshForRefinementSet[refSet];
393
    }
394
395
396
397
398
399
400
401
402
403
404
  }


  void ProblemStatSeq::createRefCoarseManager() 
  {
    FUNCNAME("ProblemStat::createRefCoarseManager()");

    int dim = 0;
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
                   name.c_str());
405
406

    switch (dim) {
407
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
408
      refinementManager = new RefinementManager1d();
409
      coarseningManager = new CoarseningManager1d();
410
411
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
412
      refinementManager = new RefinementManager2d();
413
      coarseningManager = new CoarseningManager2d();
414
415
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
416
      refinementManager = new RefinementManager3d();
417
      coarseningManager = new CoarseningManager3d();
418
419
420
421
422
423
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

424

425
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
426
  {
427
    FUNCNAME("ProblemStat::createFeSpace()");
428

429
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
430
    int dim = -1;
431
    Parameters::get(name + "->dim", dim);
432
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
433

434
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
435
    traverseInfo.resize(nComponents);
436

437
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
438
      int degree = 1;
439
440
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
441
442
      TEST_EXIT(degree > 0)
	("Poynomial degree in component %d must be larger than zero!\n", i);
443
      TEST_EXIT(componentSpaces[i] == NULL)("feSpace already created\n");
444

445
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
446
447
448
	stringstream s;
	s << name << "->feSpace[" << i << "]";

449
450
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
451
					  componentMeshes[i], s.str());
452
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
453
	feSpaces.push_back(newFeSpace);
454
      }
455

456
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
457
458
459
    }

    for (int i = 0; i < nComponents; i++) {
460
      for (int j = 0; j < nComponents; j++)
461
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
462
      
463
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
464
465
466
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
467
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
468
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
469
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
470
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
471
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
472
473
474
475
      }
    }
  }

476

477
  void ProblemStatSeq::createMatricesAndVectors()
478
  {
479
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
480
481
482

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

Thomas Witkowski's avatar
Thomas Witkowski committed
483
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
484
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
485
486
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
487

Thomas Witkowski's avatar
Thomas Witkowski committed
488
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
489
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
490
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
491
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
492

493
494
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
495

Thomas Witkowski's avatar
Thomas Witkowski committed
496
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
497
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
498
499
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
500
501
502
    }
  }

503

504
  void ProblemStatSeq::createSolver()
505
  {
506
    FUNCNAME("ProblemStat::createSolver()");
507
508

    // === create solver ===
509
    string solverType("0");
510
511
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
512
    OEMSolverCreator *solverCreator = 
513
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
514
    TEST_EXIT(solverCreator)("no solver type\n");
515
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
516
517
    solver = solverCreator->create();
    solver->initParameters();
518
519
  }

520

521
  void ProblemStatSeq::createEstimator()
522
  {
523
    FUNCNAME("ProblemStat::createEstimator()");
524
525

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

Thomas Witkowski's avatar
Thomas Witkowski committed
530
531
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
532
533
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
534
535

      // === create estimator ===
536
537
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
538
      EstimatorCreator *estimatorCreator = 
539
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
540
      if (estimatorCreator) {
541
542
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
543
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
544
	estimator[i] = estimatorCreator->create();
545
546
547
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
548
549
550
551
552
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
553
554
555
    }
  }

556

557
  void ProblemStatSeq::createMarker()
558
  {
559
    FUNCNAME("ProblemStat::createMarker()");
560

Thomas Witkowski's avatar
Thomas Witkowski committed
561
    int nMarkersCreated = 0;
562

563
    for (int i = 0; i < nComponents; i++) {
564
      marker[i] = Marker::createMarker
565
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
566

567
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
568
569
570
571
	nMarkersCreated++;

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

578

579
  void ProblemStatSeq::createFileWriter()
580
  {
581
    FUNCNAME("ProblemStat::createFileWriter()");
582
583
  
    // Create one filewriter for all components of the problem
584
585
586
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
587
588

    if (filename != "") {
589
      vector< DOFVector<double>* > solutionList(nComponents);
590

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

Thomas Witkowski's avatar
Thomas Witkowski committed
595
	solutionList[i] = solution->getDOFVector(i);
596
597
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
598
      fileWriters.push_back(new FileWriter(numberedName,
599
600
					   componentMeshes[0],
					   solutionList));
601
602
603
    }

    // Create own filewriters for each components of the problem
604
    for (int i = 0; i < nComponents; i++) {
605
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
606
      filename = "";
607
      Parameters::get(numberedName + "->filename", filename);
608

609
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
610
	fileWriters.push_back(new FileWriter(numberedName, 
611
					     componentMeshes[i], 
612
					     solution->getDOFVector(i)));      
613
614
615
    }

    int writeSerialization = 0;
616
    Parameters::get(name + "->output->write serialization", writeSerialization);
617
    if (writeSerialization)
618
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
619
620
  }

621

622
  void ProblemStatSeq::doOtherStuff()
623
  {}
624

625

626
627
628
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
629
630
631
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
632
    if (!solver) {
633
634
635
636
637
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
638
639
640

    solver->solveSystem(solverMatrix, *solution, *rhs, 
			createMatrixData, storeMatrixData);
641

Thomas Witkowski's avatar
Thomas Witkowski committed
642
643
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
		  TIME_USED(first, clock()));
644

645
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
646
647
648
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
649
650
  }

651

652
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
653
  {
654
    FUNCNAME("ProblemStat::estimate()");
655

656
657
658
659
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
660
661
#endif

662
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
663
      computeError(adaptInfo);
664
665
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
666
	Estimator *scalEstimator = estimator[i];
667
668
	
	if (scalEstimator) {
669
670
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
671
	  scalEstimator->estimate(adaptInfo->getTimestep());
672

673
674
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
675
676
677
678
679

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
680
	}
681
682
683
      }
    }

684
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
685
    MPI::COMM_WORLD.Barrier();
686
687
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
688
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
689
690
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
691
#endif
692
693
  }

694

695
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
696
  {
697
    FUNCNAME("ProblemStat::markElements()");
698

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

702
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
703
    for (int i = 0; i < nComponents; i++)
704
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
705
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
706
    
707
708
709
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
710

711
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
712
  {
713
    FUNCNAME("ProblemStat::refineMesh()");
714

Thomas Witkowski's avatar
Thomas Witkowski committed
715
    int nMeshes = static_cast<int>(meshes.size());
716
    Flag refineFlag = 0;
717
718
719
720
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

721
722
723
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
724

725
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
726
  {
727
    FUNCNAME("ProblemStat::coarsenMesh()");
728

Thomas Witkowski's avatar
Thomas Witkowski committed
729
    int nMeshes = static_cast<int>(meshes.size());
730
    Flag coarsenFlag = 0;
731
732
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
733
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
734
735
736
737

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
738

739
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
740
  {
741
    FUNCNAME("ProblemStat::oneIteration()");
742

743
744
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
745
 	adaptInfo->allowRefinement(false, i);
746
      else
747
 	adaptInfo->allowRefinement(true, i);	
748
749
750
751

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

Thomas Witkowski's avatar
Thomas Witkowski committed
752

753
754
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
755
  {
756
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
757

758
    if (dualMeshTraverseRequired()) {
759
760
761
762
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

763
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
764
765
      return;
    }
766

767
768
769
770
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

771

Thomas Witkowski's avatar
Thomas Witkowski committed
772
773
774
775
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


776
777
778
779
780
781
782
783
784
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    MPI::COMM_WORLD.Barrier();
    INFO(info, 8)("dof compression needed %.5f seconds\n", 
		  MPI::Wtime() - first);

    first = MPI::Wtime();
#else
    clock_t first = clock();
#endif
785

786
787
788

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
789
790
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
791
792
793
794
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
795
796
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
797
    if (useGetBound)
798
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
799
800

  
Thomas Witkowski's avatar
Thomas Witkowski committed
801
    traverseInfo.updateStatus();
802
803
804
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

Thomas Witkowski's avatar
Thomas Witkowski committed
805
    for (int rowComponent = 0; rowComponent < nComponents; rowComponent++) {
806

807
      MSG("%d DOFs for %s\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
808
809
	  componentSpaces[rowComponent]->getAdmin()->getUsedDofs(), 
	  componentSpaces[rowComponent]->getName().c_str());
810

Thomas Witkowski's avatar
Thomas Witkowski committed
811
      rhs->getDOFVector(rowComponent)->set(0.0);
812

Thomas Witkowski's avatar
Thomas Witkowski committed
813
      for (int colComponent = 0; colComponent < nComponents; colComponent++) {
814

815
	if (writeAsmInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
816
	  MSG("--------- %d %d -------------\n", rowComponent, colComponent);
817

818
819
820
821
	// Only if this variable is true, the current matrix will be assembled.	
	bool assembleMatrix = true;
	// The DOFMatrix which should be assembled (or not, if assembleMatrix
	// will be set to false).
Thomas Witkowski's avatar
Thomas Witkowski committed
822
823
	DOFMatrix *matrix = 
	  (asmMatrix ? (*systemMatrix)[rowComponent][colComponent] : NULL);
824

825
	if (writeAsmInfo && matrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
826
	  MSG("  -> matrix has %d operators\n", matrix->getOperators().size());
827
	  for (vector<Operator*>::iterator it = matrix->getOperatorsBegin();
828
829
830
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
831
	      cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << endl;
832
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
833
	      cout << "FOA GRD_PSI: " << assembler->getFirstOrderAssembler(GRD_PSI)->getName() << endl;
834
	    if (assembler->getFirstOrderAssembler(GRD_PHI)) 
835
	      cout << "FOA GRD_PHI: " << assembler->getFirstOrderAssembler(GRD_PHI)->getName() << endl;
836
	    if (assembler->getSecondOrderAssembler()) 
837
	      cout << "SOA: " << assembler->getSecondOrderAssembler()->getName() << endl;
838
839
840
	  }
	}

841
842
843
	if (matrix) 
	  matrix->calculateNnz();
	
844
845
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
Thomas Witkowski's avatar
Thomas Witkowski committed
846
847
	if (assembleMatrixOnlyOnce[rowComponent][colComponent] && 
	    assembledMatrix[rowComponent][colComponent]) {
848
	  assembleMatrix = false;
849
850
	} else if (matrix)