ProblemStat.cc 54.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20
21


Thomas Witkowski's avatar
Thomas Witkowski committed
22
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
23
#include <boost/lexical_cast.hpp>
24
25

#include "ProblemStat.h"
26
27
28
29
30
31
32
33
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Marker.h"
#include "AdaptInfo.h"
34
#include "io/FileWriter.h"
35
36
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
37
#include "DualTraverse.h"
38
#include "Mesh.h"
39
#include "solver/LinearSolverInterface.h"
40
41
42
43
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
44
#include "Bubble.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
45
#include "Flag.h"
46
#include "est/Estimator.h"
47
48
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
49
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
50
#include "Debug.h"
51
52
53

namespace AMDiS {

54
  using namespace std;
55
56
  using boost::lexical_cast;

57
58
  ProblemStatSeq::ProblemStatSeq(string nameStr,
				 ProblemIterationInterface *problemIteration)
59
    : name(nameStr),
60
      nComponents(-1),
61
      nAddComponents(0),
62
63
      nMeshes(0),
      traverseInfo(0),
64
65
66
67
      solver(NULL),
      solution(NULL),
      rhs(NULL),
      systemMatrix(NULL),
68
      useGetBound(true),
69
70
      refinementManager(NULL),
      coarseningManager(NULL),
71
72
73
74
      info(10),
      deserialized(false),
      computeExactError(false),
      boundaryConditionSet(false),
75
76
77
      writeAsmInfo(false),
      solutionTime(0.0),
      buildTime(0.0)
78
79
  {
    Parameters::get(name + "->components", nComponents);
80
    Parameters::get(name + "->additional components", nAddComponents);
81
82
    TEST_EXIT(nComponents > 0)("No value set for parameter \"%s->components\"!\n",
			       name.c_str());    
83
84
    TEST_EXIT(nAddComponents >= 0)("Wrong parameter \"%s->additional components\"!\n",
				    name.c_str());
85
86
    estimator.resize(nComponents, NULL);
    marker.resize(nComponents, NULL);
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    
    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);
109
110
    componentNames.resize(nComponents);

111
112
113
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
Thomas Witkowski's avatar
Thomas Witkowski committed
114
115

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

Thomas Witkowski's avatar
Thomas Witkowski committed
118
119
120

  ProblemStatSeq::~ProblemStatSeq()
  {
121
122
    if (rhs)
      delete rhs;
123
    rhs = NULL;
124
125
    if (solution)
      delete solution;
126
    solution = NULL;
127
128
129
130
    
    if (solver)
      delete solver;
    solver = NULL;
Thomas Witkowski's avatar
Thomas Witkowski committed
131

132
133
134
135
136
    if (systemMatrix) {    
      for (int i = 0; i < nComponents; i++)
	for (int j = 0; j < nComponents; j++)
	  if ((*systemMatrix)[i][j]) {
	    delete (*systemMatrix)[i][j];
137
	    (*systemMatrix)[i][j] = NULL;
138
139
140
	  }

      delete systemMatrix;
141
      systemMatrix = NULL;
142
    }
Thomas Witkowski's avatar
Thomas Witkowski committed
143
144

    for (unsigned int i = 0; i < meshes.size(); i++)
145
146
      if (meshes[i]) {
// 	delete meshes[i];
147
// 	meshes[i] = NULL;
148
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
149
150
    
    for (unsigned int i = 0; i < estimator.size(); i++)
151
      if (estimator[i]) {
152
	delete estimator[i];
153
	estimator[i] = NULL;
154
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
155
156

    for (unsigned int i = 0; i < marker.size(); i++)
157
      if (marker[i]) {
158
	delete marker[i];
159
	marker[i] = NULL;
160
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
161
  }
162
  
163
164
165
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
166
  {
167
    FUNCNAME("ProblemStat::initialize()");
168

169
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
170
    if (meshes.size() != 0) { 
171
172
173
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
174
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
175
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
176
	createMesh();      
177
178
      // NOTE: why using CREATE_MESH with adoptFlag.isSet(INIT_MESH) ???
      // (since then meshes is replaced by the adoptProblem.meshes)
Thomas Witkowski's avatar
Thomas Witkowski committed
179

180
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
181
	  (adoptFlag.isSet(INIT_MESH) ||
182
	   adoptFlag.isSet(INIT_SYSTEM) ||
183
	   adoptFlag.isSet(INIT_FE_SPACE))) 
184
185
186
187
      {    
	if (meshes.size() != 0) { 
	  WARNING("meshes created, by using CREATE_MESH, but overwritten, by using adoptFlag.isSet(INIT_MESH)\n");
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
188
	meshes = adoptProblem->getMeshes();
189
	componentMeshes.clear();
190
191
192
	if (meshes.size() == 1)
	  componentMeshes.resize(nComponents, meshes[0]);
	else if (adoptProblem->getNumComponents() >= nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
193
	  componentMeshes.resize(nComponents);
194
195
196
197
198
199
200
201
202
	  std::copy(adoptProblem->componentMeshes.begin(), 
		    adoptProblem->componentMeshes.begin() + nComponents, 
		    componentMeshes.begin());
	} else {
	  componentMeshes.resize(nComponents, meshes[0]);
	  WARNING("componentMeshes may not be derived correctly from the adoptProblem. You have to do this manually!\n");
	}
	
	if (nAddComponents > 0) {
203
	  WARNING("Additional meshes can not be adopted from adoptProblem. You have to initialize these meshes manually!\n");
204
	}
205
206
207
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
208
    if (meshes.size() == 0) 
209
210
      WARNING("no mesh created\n");

211
    // === create refinement/corasening-manager ===
212
    if (refinementManager != NULL && coarseningManager != NULL) { 
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      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;
      }
    }

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

232
    // === create fespace ===
233
    if (feSpaces.size() != 0) {
234
235
236
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
237
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
238
	createFeSpace(NULL);
239

240
      if (adoptProblem &&
241
242
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) 
      {
243
	feSpaces = adoptProblem->getFeSpaces();
Thomas Witkowski's avatar
Thomas Witkowski committed
244
	traverseInfo = adoptProblem->traverseInfo;
245
	
246
	componentSpaces.clear();
247
248
249
	if (feSpaces.size() == 1)
	  componentSpaces.resize(nComponents, feSpaces[0]);
	else if (adoptProblem->getNumComponents() >= nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
250
	  componentSpaces.resize(nComponents);
251
252
253
254
255
256
257
258
259
260
	  std::copy(adoptProblem->componentSpaces.begin(), 
		    adoptProblem->componentSpaces.begin() + nComponents,
		    componentSpaces.begin());
	} else {
	  componentSpaces.resize(nComponents, feSpaces[0]);
	  WARNING("componentSpaces may not be derived correctly from the adoptProblem. You have to do this manually!\n");
	}
	
	if (nAddComponents > 0) {
	  WARNING("Additional feSpaces can not be adopted from adoptProblem. You have to initialize these fespaces manually!\n");
261
	}
262
263
264
      }
    }

265
    if (feSpaces.size() == 0) 
266
267
268
      WARNING("no feSpace created\n");

    // === create system ===
269
    if (initFlag.isSet(INIT_SYSTEM)) 
270
      createMatricesAndVectors();
271
    
272
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
273
      solution = adoptProblem->getSolution();
274
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
275
      systemMatrix = adoptProblem->getSystemMatrix();
276
277
278
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
279
    if (solver) {
280
281
      WARNING("solver already created\n");
    } else {
282
      if (initFlag.isSet(INIT_SOLVER))
283
	createSolver();
284
      
285
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
286
287
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
288
289
290
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
291
    if (!solver) 
292
293
294
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
295
    if (initFlag.isSet(INIT_ESTIMATOR))
296
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
297
298

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
299
      estimator = adoptProblem->getEstimators();
300
301

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
302
    if (initFlag.isSet(INIT_MARKER))
303
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
304

Thomas Witkowski's avatar
Thomas Witkowski committed
305
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
306
      marker = adoptProblem->getMarkers();
307
308

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
309
    if (initFlag.isSet(INIT_FILEWRITER))
310
311
312
313
314
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
315
316
317
318
    // 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.
319
320

    int readSerialization = 0;
321
322
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
323
324
325
326
327
328

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

329
330
331
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
332
333
334
335
336
337

      // 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) {
338
339
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
340
341
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

342
343
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
344
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
345
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
346
	ifstream in(serializationFilename.c_str());
347
348
349
350
351
352

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

353
354
	deserialize(in);
	in.close();
355
#endif
356
357

	deserialized = true;
358
      } else {
359
	int globalRefinements = 0;
360

361
	// If AMDiS is compiled for parallel computations, the global refinements are
362
363
364
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
365
366
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
367
#endif
368

369
	bool initMesh = initFlag.isSet(INIT_MESH);
370

Thomas Witkowski's avatar
Thomas Witkowski committed
371
372
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
373
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
374
	    meshes[i]->initialize();	    	
375
376

	// === read value file and use it for the mesh values ===
377
378
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
379
	if (valueFilename.length())
380
	  io::ValueReader::readValue(valueFilename,
381
382
383
384
385
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
386

Thomas Witkowski's avatar
Thomas Witkowski committed
387
	for (unsigned int i = 0; i < meshes.size(); i++)
388
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
389
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
390
391
392
393
394
395
      }
    }

    doOtherStuff();
  }

396

397
  void ProblemStatSeq::createMesh() 
398
  {
399
    FUNCNAME("ProblemStat::createMesh()");
400
    
401
402
403
404
405
406
    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());
407
408

    int dim = 0;
409
410
411
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
412
413
    
    componentMeshes.resize(nComponents + nAddComponents);
414

415
    for (int i = 0; i < nComponents + nAddComponents; i++) {
416
      int refSet = -1;
417
418
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
419
      if (refSet < 0)
420
	refSet = 0;
421
      
422
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
423
	Mesh *newMesh = new Mesh(meshName, dim);
424
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
425
426
	meshes.push_back(newMesh);
	nMeshes++;
427
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
428
      componentMeshes[i] = meshForRefinementSet[refSet];
429
    }
430
431
432
433
434
435
436
437
438
439
440
  }


  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());
441
442

    switch (dim) {
443
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
444
      refinementManager = new RefinementManager1d();
445
      coarseningManager = new CoarseningManager1d();
446
447
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
448
      refinementManager = new RefinementManager2d();
449
      coarseningManager = new CoarseningManager2d();
450
451
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
452
      refinementManager = new RefinementManager3d();
453
      coarseningManager = new CoarseningManager3d();
454
455
456
457
458
459
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

460

461
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
462
  {
463
    FUNCNAME("ProblemStat::createFeSpace()");
464

465
    map<pair<Mesh*, string>, FiniteElemSpace*> feSpaceMap;
466
    int dim = -1;
467
    Parameters::get(name + "->dim", dim);
468
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
469

470
    componentSpaces.resize(nComponents + nAddComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
471
    traverseInfo.resize(nComponents);
472

473
474
475
476
477
    for (int i = 0; i < nComponents + nAddComponents; i++) {
      if (componentSpaces[i] != NULL) {
	WARNING("feSpace already created\n");
	continue;
      }
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
      string componentString = "[" + boost::lexical_cast<string>(i) + "]";
      
      string feSpaceName = "";
      string initFileStr = name + "->feSpace" + componentString;
      Parameters::get(initFileStr, feSpaceName);
      
      // synonym for "feSpace"
      if (feSpaceName.size() == 0) {
	initFileStr = name + "->finite element space" + componentString;
	Parameters::get(initFileStr, feSpaceName);
      }
      
      // for backward compatibility also accept the old syntax
      if (feSpaceName.size() == 0) {
	int degree = 1;
	initFileStr = name + "->polynomial degree" + componentString;
	Parameters::get(initFileStr, degree);
	TEST_EXIT(degree > 0)
	  ("Poynomial degree in component %d must be larger than zero!\n", i);
	  
	feSpaceName = "Lagrange" + boost::lexical_cast<string>(degree);
      }
500

501
502
503
504
505
506
507
      if (feSpaceMap[pair<Mesh*, string>(componentMeshes[i], feSpaceName)] == NULL) {      
	BasisFunctionCreator *basisFctCreator = 
	dynamic_cast<BasisFunctionCreator*>(CreatorMap<BasisFunction>::getCreator(feSpaceName, initFileStr));
	TEST_EXIT(basisFctCreator)
	  ("No valid basisfunction type found in parameter \"%s\"\n", initFileStr.c_str());
	basisFctCreator->setDim(dim);
	
508
	FiniteElemSpace *newFeSpace = 
509
510
511
512
	  FiniteElemSpace::provideFeSpace(admin, basisFctCreator->create(),
					  componentMeshes[i], "FeSpace" + componentString + " (" + feSpaceName + ")");

	feSpaceMap[pair<Mesh*, string>(componentMeshes[i], feSpaceName)] = newFeSpace;
513
	feSpaces.push_back(newFeSpace);
514
      }
515

516
      componentSpaces[i] = feSpaceMap[pair<Mesh*, string>(componentMeshes[i], feSpaceName)];
Thomas Witkowski's avatar
Thomas Witkowski committed
517
518
519
    }

    for (int i = 0; i < nComponents; i++) {
520
      for (int j = 0; j < nComponents; j++)
521
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
522
      
523
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
524
525
526
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
527
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
528
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
529
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
530
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
531
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
532
533
534
535
      }
    }
  }

536

537
  void ProblemStatSeq::createMatricesAndVectors()
538
539
540
  {
    // === create vectors and system matrix ===

Thomas Witkowski's avatar
Thomas Witkowski committed
541
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
542
    systemMatrix->set((DOFMatrix*)(NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
543
544
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
545

Thomas Witkowski's avatar
Thomas Witkowski committed
546
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
547
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
548
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
549
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
550

551
      //set parallel synchronization later in ParalleProblemStat
552
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
553
						 "rhs[" + lexical_cast<string>(i) + "]", false));
554

555
      //set parallel synchronization later in ParalleProblemStat
Thomas Witkowski's avatar
Thomas Witkowski committed
556
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
557
      						      componentNames[i], false));
Thomas Witkowski's avatar
Thomas Witkowski committed
558
559
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
560
561
562
    }
  }

563

564
  void ProblemStatSeq::createSolver()
565
  {
566
    FUNCNAME("ProblemStat::createSolver()");
567
    
Praetorius, Simon's avatar
Praetorius, Simon committed
568
    // definition of standard-backends
569
570
571
572
#if defined HAVE_PARALLEL_PETSC
    string backend("p_petsc");
#elif defined HAVE_PARALLEL_MTL
    string backend("p_mtl");
573
#elif defined HAVE_SEQ_PETSC
574
575
576
577
578
579
580
581
    string backend("petsc");
#else
    string backend("mtl");
#endif
    
    // === read backend-name ===
    string initFileStr = name + "->solver";    
    Parameters::get(initFileStr + "->backend", backend);
582

583
    // === read solver-name ===
584
    string solverType("0");
585
    Parameters::get(initFileStr, solverType);
586
587
588
589
590
591
    
    if (backend != "0" && backend != "no" && backend != "")
      solverType = backend + "_" + solverType;
    
    // === create solver ===
    LinearSolverCreator *solverCreator = 
592
      dynamic_cast<LinearSolverCreator*>(CreatorMap<LinearSolverInterface>::getCreator(solverType, initFileStr));
593
594
    TEST_EXIT(solverCreator)
      ("No valid solver type found in parameter \"%s\"\n", initFileStr.c_str());
595
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
596
    solver = solverCreator->create();
597
598
  }

599

600
  void ProblemStatSeq::createEstimator()
601
  {
602
    FUNCNAME("ProblemStat::createEstimator()");
603
604

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

Thomas Witkowski's avatar
Thomas Witkowski committed
609
    for (int i = 0; i < nComponents; i++) {
610
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
611
612
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
613
614

      // === create estimator ===
615
616
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
617
      EstimatorCreator *estimatorCreator = 
618
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
619
      if (estimatorCreator) {
620
621
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
622
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
623
	estimator[i] = estimatorCreator->create();
624
625
626
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
627
628
629
630
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
631
				  rhs->getDOFVector(i));      // NOTE: hier eventuell (i) statt (j) ??? --> corrected
632
633
634
    }
  }

635

636
  void ProblemStatSeq::createMarker()
637
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
638
    int nMarkersCreated = 0;
639

640
    for (int i = 0; i < nComponents; i++) {
641
      marker[i] = Marker::createMarker
642
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
643

644
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
645
646
647
648
	nMarkersCreated++;

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

655

656
  void ProblemStatSeq::createFileWriter()
657
  {
658
    FUNCNAME("ProblemStat::createFileWriter()");
659
660
  
    // Create one filewriter for all components of the problem
661
662
663
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
664
665

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
672
	solutionList[i] = solution->getDOFVector(i);
673
674
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
675
      fileWriters.push_back(new FileWriter(numberedName,
676
677
					   componentMeshes[0],
					   solutionList));
678
679
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
680
    // Create own filewriters for each component of the problem
681
    for (int i = 0; i < nComponents; i++) {
682
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
683
      filename = "";
684
      Parameters::get(numberedName + "->filename", filename);
685

686
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
687
	fileWriters.push_back(new FileWriter(numberedName, 
688
					     componentMeshes[i], 
689
					     solution->getDOFVector(i)));      
690
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
691
    
692
    // create a filewrite for groups of components to write vector-valued output
Praetorius, Simon's avatar
Praetorius, Simon committed
693
694
695
696
697
698
699
700
701
702
703
    int nVectors = 0;
    Parameters::get(name + "->output->num vectors", nVectors);
    if (nVectors > 0) {
      for (int j = 0; j < nVectors; j++) {
	numberedName = name + "->output->vector[" + boost::lexical_cast<string>(j) + "]";
	
	filename = "";
	Parameters::get(numberedName + "->filename", filename);
	std::string componentName = "";
	Parameters::get(numberedName + "->name", componentName);
	std::vector<std::string> names; 
704
	if (componentName != "")
Praetorius, Simon's avatar
Praetorius, Simon committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
	  names.push_back(componentName);
	std::vector<int> comp;
	Parameters::get(numberedName + "->components", comp);
	
	if (filename != "" && comp.size() > 0) {
	  // Create own filewriters for each component of the problem
	  std::vector<DOFVector<double>*> vectors;
	  for (size_t i = 0; i < comp.size(); i++)
	    vectors.push_back(solution->getDOFVector(comp[i]));
	  
	  fileWriters.push_back(new FileWriter(numberedName, 
					      componentMeshes[comp[0]], 
					      vectors,
					      names
					      ));      
	  
	}
      }
    }
724
725

    int writeSerialization = 0;
726
    Parameters::get(name + "->output->write serialization", writeSerialization);
727
    if (writeSerialization)
728
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
729
730
  }

731

732
  void ProblemStatSeq::doOtherStuff()
733
  {}
734

735

736
737
738
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
739
740
741
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
742
    if (!solver) {
743
744
745
746
      WARNING("no solver\n");
      return;
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
747
    Timer t;
748
749
    solver->solveSystem(solverMatrix, *solution, *rhs, 
			createMatrixData, storeMatrixData);
750

Thomas Witkowski's avatar
Thomas Witkowski committed
751
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n", 
Praetorius, Simon's avatar
Praetorius, Simon committed
752
		  t.elapsed());
753
    solutionTime = t.elapsed();
754

755
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
756
757
758
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
759
760
  }

761

762
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
763
  {
764
    FUNCNAME("ProblemStat::estimate()");
765

Praetorius, Simon's avatar
Praetorius, Simon committed
766
    Timer t;
767

768
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
769
      computeError(adaptInfo);
770
771
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
772
	Estimator *scalEstimator = estimator[i];
773
774
	
	if (scalEstimator) {
775
776
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
777
	  scalEstimator->estimate(adaptInfo->getTimestep());
778

779
780
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
781
782
783
784
785

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
786
	}
787
788
789
      }
    }

790
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
791
    MPI::COMM_WORLD.Barrier();
792
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
793
794
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  t.elapsed());
795
796
  }

797

798
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
799
  {
800
    FUNCNAME_DBG("ProblemStat::markElements()");
801

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

805
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
806
    for (int i = 0; i < nComponents; i++)
807
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
808
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
809
    
810
811
812
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
813

814
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
815
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
816
    int nMeshes = static_cast<int>(meshes.size());
817
    Flag refineFlag = 0;
818
    for (int i = 0; i < nMeshes; i++)
819
      if (i >= nComponents || adaptInfo->isRefinementAllowed(i))
820
821
	refineFlag |= refinementManager->refineMesh(meshes[i]);

822
823
824
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
825

826
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
827
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
828
    int nMeshes = static_cast<int>(meshes.size());
829
    Flag coarsenFlag = 0;
830
    for (int i = 0; i < nMeshes; i++)
831
      if (i >= nComponents || adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
832
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
833
834
835
836

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
837

838
839
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
840
  {
841
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
842

843
    if (dualMeshTraverseRequired()) {
844
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
845
846
      return;
    }
847

848
    Timer t;
849

Thomas Witkowski's avatar
Thomas Witkowski committed
850
851
852
853
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


854
855
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    MPI::COMM_WORLD.Barrier();
856
    INFO(info, 8)("dof compression needed %.5f seconds\n", t.elapsed());
857
#endif
858
    t.reset();
859
860
861

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
862
863
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
864
865
866
867
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
868
869
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
870
    if (useGetBound)
Peter Gottschling's avatar