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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
119
120
121

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

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

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

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

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

170
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
171
    if (meshes.size() != 0) { 
172
173
174
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
175
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
176
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
177
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
178

179
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
180
	  (adoptFlag.isSet(INIT_MESH) ||
181
182
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
183
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
184
	componentMeshes = adoptProblem->componentMeshes;
185
186
187
      
	TEST_EXIT(adoptProblem->getNumAddComponents() + nAddComponents > 0)
	  ("Not yet implemented.\n");
188
189
	// 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
190
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
191
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
192
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
193
	  componentMeshes.resize(nComponents);
194
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
195
	    componentMeshes[i] = componentMeshes[0];
196
	}
197
198
199
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
200
    if (meshes.size() == 0) 
201
202
      WARNING("no mesh created\n");

203
    // === create refinement/corasening-manager ===
204
    if (refinementManager != NULL && coarseningManager != NULL) { 
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
      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;
      }
    }

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

224
    // === create fespace ===
225
    if (feSpaces.size() != 0) {
226
227
228
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
229
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
230
	createFeSpace(NULL);
231

232
233
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
234
	feSpaces = adoptProblem->getFeSpaces();
235
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
236
	traverseInfo = adoptProblem->traverseInfo;
237
238
239
      
	TEST_EXIT(adoptProblem->getNumAddComponents() + nAddComponents > 0)
	  ("Not yet implemented.\n");
240
241
242

	// 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
243
244
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
245
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
246
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
247
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
248
	    componentSpaces[i] = componentSpaces[0];
249
250
	}

251
252
253
      }
    }

254
    if (feSpaces.size() == 0) 
255
256
257
      WARNING("no feSpace created\n");

    // === create system ===
258
    if (initFlag.isSet(INIT_SYSTEM)) 
259
      createMatricesAndVectors();
260
    
261
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
262
      solution = adoptProblem->getSolution();
263
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
264
      systemMatrix = adoptProblem->getSystemMatrix();
265
266
267
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
268
    if (solver) {
269
270
      WARNING("solver already created\n");
    } else {
271
      if (initFlag.isSet(INIT_SOLVER))
272
	createSolver();
273
      
274
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
275
276
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
277
278
279
      }
    }

280
// #ifndef HAVE_PARALLEL_DOMAIN_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
281
    if (!solver) 
282
      WARNING("no solver created\n");
283
// #endif
284
285

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
286
    if (initFlag.isSet(INIT_ESTIMATOR))
287
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
288
289

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
290
      estimator = adoptProblem->getEstimators();
291
292

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
293
    if (initFlag.isSet(INIT_MARKER))
294
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
295

Thomas Witkowski's avatar
Thomas Witkowski committed
296
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
297
      marker = adoptProblem->getMarkers();
298
299

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
300
    if (initFlag.isSet(INIT_FILEWRITER))
301
302
303
304
305
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
306
307
308
309
    // 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.
310
311

    int readSerialization = 0;
312
313
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
314
315
316
317
318
319

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

320
321
322
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
323
324
325
326
327
328

      // 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) {
329
330
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
331
332
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

333
334
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
335
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
336
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
337
	ifstream in(serializationFilename.c_str());
338
339
340
341
342
343

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

344
345
	deserialize(in);
	in.close();
346
#endif
347
348

	deserialized = true;
349
      } else {
350
	int globalRefinements = 0;
351

352
	// If AMDiS is compiled for parallel computations, the global refinements are
353
354
355
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
356
357
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
358
#endif
359

360
	bool initMesh = initFlag.isSet(INIT_MESH);
361

Thomas Witkowski's avatar
Thomas Witkowski committed
362
363
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
364
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
365
	    meshes[i]->initialize();	    	
366
367

	// === read value file and use it for the mesh values ===
368
369
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
370
	if (valueFilename.length())
371
	  io::ValueReader::readValue(valueFilename,
372
373
374
375
376
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
377

Thomas Witkowski's avatar
Thomas Witkowski committed
378
	for (unsigned int i = 0; i < meshes.size(); i++)
379
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
380
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
381
382
383
384
385
386
      }
    }

    doOtherStuff();
  }

387

388
  void ProblemStatSeq::createMesh() 
389
  {
390
    FUNCNAME("ProblemStat::createMesh()");
391
    
392
393
394
395
396
397
    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());
398
399

    int dim = 0;
400
401
402
    Parameters::get(name + "->dim", dim);
    TEST_EXIT(dim)("No problem dimension specified for \"%s->dim\"!\n",
		   name.c_str());
403
404
    
    componentMeshes.resize(nComponents + nAddComponents);
405

406
    for (int i = 0; i < nComponents + nAddComponents; i++) {
407
      int refSet = -1;
408
409
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
410
      if (refSet < 0)
411
	refSet = 0;
412
      
413
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
414
	Mesh *newMesh = new Mesh(meshName, dim);
415
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
416
417
	meshes.push_back(newMesh);
	nMeshes++;
418
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
419
      componentMeshes[i] = meshForRefinementSet[refSet];
420
    }
421
422
423
424
425
426
427
428
429
430
431
  }


  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());
432
433

    switch (dim) {
434
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
435
      refinementManager = new RefinementManager1d();
436
      coarseningManager = new CoarseningManager1d();
437
438
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
439
      refinementManager = new RefinementManager2d();
440
      coarseningManager = new CoarseningManager2d();
441
442
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
443
      refinementManager = new RefinementManager3d();
444
      coarseningManager = new CoarseningManager3d();
445
446
447
448
449
450
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

451

452
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
453
  {
454
    FUNCNAME("ProblemStat::createFeSpace()");
455

456
    map<pair<Mesh*, string>, FiniteElemSpace*> feSpaceMap;
457
    int dim = -1;
458
    Parameters::get(name + "->dim", dim);
459
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
460

461
    componentSpaces.resize(nComponents + nAddComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
462
    traverseInfo.resize(nComponents);
463

464
465
466
467
468
    for (int i = 0; i < nComponents + nAddComponents; i++) {
      if (componentSpaces[i] != NULL) {
	WARNING("feSpace already created\n");
	continue;
      }
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
      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);
      }
491

492
493
494
495
496
497
498
      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);
	
499
	FiniteElemSpace *newFeSpace = 
500
501
502
503
	  FiniteElemSpace::provideFeSpace(admin, basisFctCreator->create(),
					  componentMeshes[i], "FeSpace" + componentString + " (" + feSpaceName + ")");

	feSpaceMap[pair<Mesh*, string>(componentMeshes[i], feSpaceName)] = newFeSpace;
504
	feSpaces.push_back(newFeSpace);
505
      }
506

507
      componentSpaces[i] = feSpaceMap[pair<Mesh*, string>(componentMeshes[i], feSpaceName)];
Thomas Witkowski's avatar
Thomas Witkowski committed
508
509
510
    }

    for (int i = 0; i < nComponents; i++) {
511
      for (int j = 0; j < nComponents; j++)
512
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
513
      
514
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
515
516
517
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
518
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
519
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
520
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
521
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
522
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
523
524
525
526
      }
    }
  }

527

528
  void ProblemStatSeq::createMatricesAndVectors()
529
530
531
  {
    // === create vectors and system matrix ===

Thomas Witkowski's avatar
Thomas Witkowski committed
532
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
533
    systemMatrix->set((DOFMatrix*)(NULL));
Thomas Witkowski's avatar
Thomas Witkowski committed
534
535
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
536

Thomas Witkowski's avatar
Thomas Witkowski committed
537
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
538
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
539
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
540
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
541

542
      //set parallel synchronization later in ParalleProblemStat
543
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
544
						 "rhs[" + lexical_cast<string>(i) + "]", false));
545

546
      //set parallel synchronization later in ParalleProblemStat
Thomas Witkowski's avatar
Thomas Witkowski committed
547
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
548
      						      componentNames[i], false));
Thomas Witkowski's avatar
Thomas Witkowski committed
549
550
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
551
552
553
    }
  }

554

555
  void ProblemStatSeq::createSolver()
556
  {
557
    FUNCNAME("ProblemStat::createSolver()");
558
    
Praetorius, Simon's avatar
Praetorius, Simon committed
559
    // definition of standard-backends
560
561
562
563
#if defined HAVE_PARALLEL_PETSC
    string backend("p_petsc");
#elif defined HAVE_PARALLEL_MTL
    string backend("p_mtl");
564
#elif defined HAVE_SEQ_PETSC
565
566
567
568
569
570
571
572
    string backend("petsc");
#else
    string backend("mtl");
#endif
    
    // === read backend-name ===
    string initFileStr = name + "->solver";    
    Parameters::get(initFileStr + "->backend", backend);
573

574
    // === read solver-name ===
575
    string solverType("0");
576
    Parameters::get(initFileStr, solverType);
577
578
579
580
581
582
    
    if (backend != "0" && backend != "no" && backend != "")
      solverType = backend + "_" + solverType;
    
    // === create solver ===
    LinearSolverCreator *solverCreator = 
583
      dynamic_cast<LinearSolverCreator*>(CreatorMap<LinearSolverInterface>::getCreator(solverType, initFileStr));
584
585
    TEST_EXIT(solverCreator)
      ("No valid solver type found in parameter \"%s\"\n", initFileStr.c_str());
586
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
587
    solver = solverCreator->create();
588
589
  }

590

591
  void ProblemStatSeq::createEstimator()
592
  {
593
    FUNCNAME("ProblemStat::createEstimator()");
594
595

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

Thomas Witkowski's avatar
Thomas Witkowski committed
600
    for (int i = 0; i < nComponents; i++) {
601
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
602
603
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
604
605

      // === create estimator ===
606
607
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
608
      EstimatorCreator *estimatorCreator = 
609
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
610
      if (estimatorCreator) {
611
612
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
613
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
614
	estimator[i] = estimatorCreator->create();
615
616
617
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
618
619
620
621
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
Praetorius, Simon's avatar
Praetorius, Simon committed
622
				  rhs->getDOFVector(j));      // TODO: hier eventuell (i) statt (j) ???
623
624
625
    }
  }

626

627
  void ProblemStatSeq::createMarker()
628
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
629
    int nMarkersCreated = 0;
630

631
    for (int i = 0; i < nComponents; i++) {
632
      marker[i] = Marker::createMarker
633
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
634

635
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
636
637
638
639
	nMarkersCreated++;

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

646

647
  void ProblemStatSeq::createFileWriter()
648
  {
649
    FUNCNAME("ProblemStat::createFileWriter()");
650
651
  
    // Create one filewriter for all components of the problem
652
653
654
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
655
656

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
663
	solutionList[i] = solution->getDOFVector(i);
664
665
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
666
      fileWriters.push_back(new FileWriter(numberedName,
667
668
					   componentMeshes[0],
					   solutionList));
669
670
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
671
    // Create own filewriters for each component of the problem
672
    for (int i = 0; i < nComponents; i++) {
673
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
674
      filename = "";
675
      Parameters::get(numberedName + "->filename", filename);
676

677
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
678
	fileWriters.push_back(new FileWriter(numberedName, 
679
					     componentMeshes[i], 
680
					     solution->getDOFVector(i)));      
681
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
682
    
683
    // create a filewrite for groups of components to write vector-valued output
Praetorius, Simon's avatar
Praetorius, Simon committed
684
685
686
687
688
689
690
691
692
693
694
    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; 
695
	if (componentName != "")
Praetorius, Simon's avatar
Praetorius, Simon committed
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
	  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
					      ));      
	  
	}
      }
    }
715
716

    int writeSerialization = 0;
717
    Parameters::get(name + "->output->write serialization", writeSerialization);
718
    if (writeSerialization)
719
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
720
721
  }

722

723
  void ProblemStatSeq::doOtherStuff()
724
  {}
725

726

727
728
729
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, 	       
			     bool createMatrixData,
			     bool storeMatrixData)
730
731
732
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
733
    if (!solver) {
734
735
736
737
      WARNING("no solver\n");
      return;
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
738
    Timer t;
739
740
    solver->solveSystem(solverMatrix, *solution, *rhs, 
			createMatrixData, storeMatrixData);
741

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

746
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
747
748
749
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
750
751
  }

752

753
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
754
  {
755
    FUNCNAME("ProblemStat::estimate()");
756

Praetorius, Simon's avatar
Praetorius, Simon committed
757
    Timer t;
758

759
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
760
      computeError(adaptInfo);
761
762
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
763
	Estimator *scalEstimator = estimator[i];
764
765
	
	if (scalEstimator) {
766
767
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
768
	  scalEstimator->estimate(adaptInfo->getTimestep());
769

770
771
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
772
773
774
775
776

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
777
	}
778
779
780
      }
    }

781
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
782
    MPI::COMM_WORLD.Barrier();
783
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
784
785
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  t.elapsed());
786
787
  }

788

789
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
790
  {
791
    FUNCNAME_DBG("ProblemStat::markElements()");
792

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

796
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
797
    for (int i = 0; i < nComponents; i++)
798
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
799
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
800
    
801
802
803
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
804