ProblemStat.cc 54.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"
Praetorius, Simon's avatar
Praetorius, Simon committed
26
#include "io/FileWriter.hh"
27
28
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
29
#include "DualTraverse.h"
30
31
32
33
34
35
#include "Mesh.h"
#include "OEMSolver.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
36
#include "Flag.h"
37
#include "est/Estimator.h"
38
39
#include "io/VtkWriter.h"
#include "io/ValueReader.h"
40
#include "ProblemStatDbg.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
41
#include "Debug.h"
42
43
44

namespace AMDiS {

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

48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  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);
    TEST_EXIT(nComponents > 0)("components not set!\n");    
    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
99
100
    for (int i = 0; i < nComponents; i++)
      Parameters::get(name + "->name[" + lexical_cast<string>(i) + "]",
		      componentNames[i]);
  }

101
102
103
  void ProblemStatSeq::initialize(Flag initFlag,
				  ProblemStatSeq *adoptProblem,
				  Flag adoptFlag)
104
  {
105
    FUNCNAME("ProblemStat::initialize()");
106

107
    // === create meshes ===
Thomas Witkowski's avatar
Thomas Witkowski committed
108
    if (meshes.size() != 0) { 
109
110
111
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
Thomas Witkowski's avatar
Thomas Witkowski committed
112
	  (!adoptFlag.isSet(INIT_MESH) &&
Thomas Witkowski's avatar
Thomas Witkowski committed
113
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE))))
114
	createMesh();      
Thomas Witkowski's avatar
Thomas Witkowski committed
115

116
      if (adoptProblem && 
Thomas Witkowski's avatar
Thomas Witkowski committed
117
	  (adoptFlag.isSet(INIT_MESH) ||
118
119
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
120
	meshes = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
121
	componentMeshes = adoptProblem->componentMeshes;
122
123
124

	// 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
125
	if (adoptProblem->getNumComponents() < nComponents) {
Thomas Witkowski's avatar
Thomas Witkowski committed
126
	  TEST_EXIT(meshes.size() == 1)("Daran muss ich noch arbeiten!\n");
127
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
128
	  componentMeshes.resize(nComponents);
129
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
130
	    componentMeshes[i] = componentMeshes[0];
131
	}
132
133
134
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
135
    if (meshes.size() == 0) 
136
137
      WARNING("no mesh created\n");

138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    // === 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");

159
    // === create fespace ===
160
    if (feSpaces.size() != 0) {
161
162
163
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
164
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE)))
165
	createFeSpace(NULL);
166

167
168
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
169
	feSpaces = adoptProblem->getFeSpaces();
170
	componentSpaces = adoptProblem->componentSpaces;
Thomas Witkowski's avatar
Thomas Witkowski committed
171
	traverseInfo = adoptProblem->traverseInfo;
172
173
174

	// 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
175
176
	if (adoptProblem->getNumComponents() < nComponents) {
	  TEST_EXIT(feSpaces.size() == 1)("Daran muss ich noch arbeiten!\n");
177
	  
Thomas Witkowski's avatar
Thomas Witkowski committed
178
	  componentSpaces.resize(nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
179
	  for (int i = adoptProblem->getNumComponents(); i < nComponents; i++)
Thomas Witkowski's avatar
Thomas Witkowski committed
180
	    componentSpaces[i] = componentSpaces[0];
181
182
	}

183
184
185
      }
    }

186
    if (feSpaces.size() == 0) 
187
188
189
      WARNING("no feSpace created\n");

    // === create system ===
190
    if (initFlag.isSet(INIT_SYSTEM)) 
191
      createMatricesAndVectors();
192
    
193
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
194
      solution = adoptProblem->getSolution();
195
      rhs = adoptProblem->getRhs();
Thomas Witkowski's avatar
Thomas Witkowski committed
196
      systemMatrix = adoptProblem->getSystemMatrix();
197
198
199
    }

    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
200
    if (solver) {
201
202
203
204
205
206
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
Thomas Witkowski's avatar
Thomas Witkowski committed
207
208
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
209
210
211
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
212
    if (!solver) 
213
214
215
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
216
    if (initFlag.isSet(INIT_ESTIMATOR))
217
      createEstimator();
Thomas Witkowski's avatar
Thomas Witkowski committed
218
219

    if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR))
220
      estimator = adoptProblem->getEstimators();
221
222

    // === create marker ===
Thomas Witkowski's avatar
Thomas Witkowski committed
223
    if (initFlag.isSet(INIT_MARKER))
224
      createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
225

Thomas Witkowski's avatar
Thomas Witkowski committed
226
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER))
227
      marker = adoptProblem->getMarkers();
228
229

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
230
    if (initFlag.isSet(INIT_FILEWRITER))
231
232
233
234
235
      createFileWriter();
    
    // === read serialization and init mesh ===
    
    // There are two possiblities where the user can define a serialization
236
237
238
239
    // 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.
240
241

    int readSerialization = 0;
242
243
    string serializationFilename = "";
    Parameters::get("argv->rs", serializationFilename);
244
245
246
247
248
249

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

250
251
252
      Parameters::get(name + "->input->read serialization", readSerialization);
      Parameters::get(name + "->input->serialization with adaptinfo",
		      readSerializationWithAdaptInfo);
253
254
255
256
257
258

      // 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) {
259
260
	Parameters::get(name + "->input->serialization filename", 
			serializationFilename);
261
262
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

263
264
	// If AMDiS is compiled for parallel computations, the deserialization is
	// controlled by the parallel problem object.
265
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
266
	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
267
	ifstream in(serializationFilename.c_str());
268
269
270
271
272
273

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

274
275
	deserialize(in);
	in.close();
276
#endif
277
278

	deserialized = true;
279
      } else {
280
	int globalRefinements = 0;
281

282
	// If AMDiS is compiled for parallel computations, the global refinements are
283
284
285
	// ignored here. Later, each rank will add the global refinements to its 
	// private mesh.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
286
287
	Parameters::get(meshes[0]->getName() + "->global refinements", 
			globalRefinements);
288
#endif
289

290
	bool initMesh = initFlag.isSet(INIT_MESH);
291

Thomas Witkowski's avatar
Thomas Witkowski committed
292
293
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes.size()); i++)
294
	  if (initMesh && meshes[i] && !(meshes[i]->isInitialized()))
Thomas Witkowski's avatar
Thomas Witkowski committed
295
	    meshes[i]->initialize();	    	
296
297

	// === read value file and use it for the mesh values ===
298
299
	string valueFilename("");
	Parameters::get(meshes[0]->getName() + "->value file name", valueFilename); 
300
	if (valueFilename.length())
301
302
303
304
305
306
	  ValueReader::readValue(valueFilename,
				 meshes[0],
				 solution->getDOFVector(0),
				 meshes[0]->getMacroFileInfo());

	// === do global refinements ===
307

Thomas Witkowski's avatar
Thomas Witkowski committed
308
	for (unsigned int i = 0; i < meshes.size(); i++)
309
	  if (initMesh && meshes[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
310
	    refinementManager->globalRefine(meshes[i], globalRefinements);	
311
312
313
314
315
316
      }
    }

    doOtherStuff();
  }

317

318
  void ProblemStatSeq::createMesh() 
319
  {
320
    FUNCNAME("ProblemStat::createMesh()");
321

Thomas Witkowski's avatar
Thomas Witkowski committed
322
    componentMeshes.resize(nComponents);
323
324
325
326
327
328
    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());
329
330

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

335
    for (int i = 0; i < nComponents; i++) {
336
      int refSet = -1;
337
338
      Parameters::get(name + "->refinement set[" + lexical_cast<string>(i) + "]", 
		      refSet);
339
      if (refSet < 0)
340
	refSet = 0;
341
      
342
      if (meshForRefinementSet[refSet] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
343
	Mesh *newMesh = new Mesh(meshName, dim);
344
	meshForRefinementSet[refSet] = newMesh;
Thomas Witkowski's avatar
Thomas Witkowski committed
345
346
	meshes.push_back(newMesh);
	nMeshes++;
347
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
348
      componentMeshes[i] = meshForRefinementSet[refSet];
349
    }
350
351
352
353
354
355
356
357
358
359
360
  }


  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());
361
362

    switch (dim) {
363
    case 1:
Thomas Witkowski's avatar
Thomas Witkowski committed
364
      refinementManager = new RefinementManager1d();
365
      coarseningManager = new CoarseningManager1d();
366
367
      break;
    case 2:
Thomas Witkowski's avatar
Thomas Witkowski committed
368
      refinementManager = new RefinementManager2d();
369
      coarseningManager = new CoarseningManager2d();
370
371
      break;
    case 3:
Thomas Witkowski's avatar
Thomas Witkowski committed
372
      refinementManager = new RefinementManager3d();
373
      coarseningManager = new CoarseningManager3d();
374
375
376
377
378
379
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

380

381
  void ProblemStatSeq::createFeSpace(DOFAdmin *admin)
382
  {
383
    FUNCNAME("ProblemStat::createFeSpace()");
384

385
    map<pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
386
    int dim = -1;
387
    Parameters::get(name + "->dim", dim);
388
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
389

390
    componentSpaces.resize(nComponents, NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
391
    traverseInfo.resize(nComponents);
392

393
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
394
      int degree = 1;
395
396
      Parameters::get(name + "->polynomial degree[" + 
		      boost::lexical_cast<string>(i) + "]", degree);
397

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

400
      if (feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
Thomas Witkowski's avatar
Thomas Witkowski committed
401
402
403
	stringstream s;
	s << name << "->feSpace[" << i << "]";

404
405
	FiniteElemSpace *newFeSpace = 
	  FiniteElemSpace::provideFeSpace(admin, Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
406
					  componentMeshes[i], s.str());
407
	feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)] = newFeSpace;
408
	feSpaces.push_back(newFeSpace);
409
      }
410

411
      componentSpaces[i] = feSpaceMap[pair<Mesh*, int>(componentMeshes[i], degree)];
Thomas Witkowski's avatar
Thomas Witkowski committed
412
413
414
    }

    for (int i = 0; i < nComponents; i++) {
415
      for (int j = 0; j < nComponents; j++)
416
	traverseInfo.getMatrix(i, j).setFeSpace(componentSpaces[i], componentSpaces[j]);
417
      
418
      traverseInfo.getVector(i).setFeSpace(componentSpaces[i]);
419
420
421
    }

    // create dof admin for vertex dofs if neccessary
Thomas Witkowski's avatar
Thomas Witkowski committed
422
    for (int i = 0; i < static_cast<int>(meshes.size()); i++) {
423
      if (meshes[i]->getNumberOfDofs(VERTEX) == 0) {
Thomas Witkowski's avatar
Thomas Witkowski committed
424
	DimVec<int> ln_dof(meshes[i]->getDim(), DEFAULT_VALUE, 0);
425
	ln_dof[VERTEX] = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
426
	meshes[i]->createDOFAdmin("vertex dofs", ln_dof);      
427
428
429
430
      }
    }
  }

431

432
  void ProblemStatSeq::createMatricesAndVectors()
433
  {
434
    FUNCNAME("ProblemStat::createMatricesAndVectors()");
435
436
437

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

Thomas Witkowski's avatar
Thomas Witkowski committed
438
    systemMatrix = new Matrix<DOFMatrix*>(nComponents, nComponents);
Thomas Witkowski's avatar
Thomas Witkowski committed
439
    systemMatrix->set(NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
440
441
    rhs = new SystemVector("rhs", componentSpaces, nComponents);
    solution = new SystemVector("solution", componentSpaces, nComponents);
442

Thomas Witkowski's avatar
Thomas Witkowski committed
443
    for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
444
      (*systemMatrix)[i][i] = new DOFMatrix(componentSpaces[i], 
Thomas Witkowski's avatar
Thomas Witkowski committed
445
					    componentSpaces[i], "A_ii");
Thomas Witkowski's avatar
Thomas Witkowski committed
446
      (*systemMatrix)[i][i]->setCoupleMatrix(false);
Thomas Witkowski's avatar
Thomas Witkowski committed
447

448
449
      rhs->setDOFVector(i, new DOFVector<double>(componentSpaces[i], 
						 "rhs[" + lexical_cast<string>(i) + "]"));
450

Thomas Witkowski's avatar
Thomas Witkowski committed
451
      solution->setDOFVector(i, new DOFVector<double>(componentSpaces[i],
452
      						      componentNames[i]));
Thomas Witkowski's avatar
Thomas Witkowski committed
453
454
      solution->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution->getDOFVector(i)->set(0.0);
455
456
457
    }
  }

458

459
  void ProblemStatSeq::createSolver()
460
  {
461
    FUNCNAME("ProblemStat::createSolver()");
462
463

    // === create solver ===
464
    string solverType("0");
465
466
    string initFileStr = name + "->solver";
    Parameters::get(initFileStr, solverType);
467
    OEMSolverCreator *solverCreator = 
468
      dynamic_cast<OEMSolverCreator*>(CreatorMap<OEMSolver>::getCreator(solverType, initFileStr));
469
    TEST_EXIT(solverCreator)("no solver type\n");
470
    solverCreator->setName(initFileStr);
Thomas Witkowski's avatar
Thomas Witkowski committed
471
472
    solver = solverCreator->create();
    solver->initParameters();
473
474
  }

475

476
  void ProblemStatSeq::createEstimator()
477
  {
478
    FUNCNAME("ProblemStat::createEstimator()");
479
480

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

Thomas Witkowski's avatar
Thomas Witkowski committed
485
486
    for (int i = 0; i < nComponents; i++) {
      TEST_EXIT(estimator[i] == NULL)("estimator already created\n");
487
488
      string estName = 
	name + "->estimator[" + boost::lexical_cast<string>(i) + "]";
489
490

      // === create estimator ===
491
492
      string estimatorType("0");
      Parameters::get(estName, estimatorType);
493
      EstimatorCreator *estimatorCreator = 
494
	dynamic_cast<EstimatorCreator*>(CreatorMap<Estimator>::getCreator(estimatorType, estName));
Thomas Witkowski's avatar
Thomas Witkowski committed
495
      if (estimatorCreator) {
496
497
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
498
	estimatorCreator->setSolution(solution->getDOFVector(i));
Thomas Witkowski's avatar
Thomas Witkowski committed
499
	estimator[i] = estimatorCreator->create();
500
501
502
      }


Thomas Witkowski's avatar
Thomas Witkowski committed
503
504
505
506
507
      if (estimator[i])
	for (int j = 0; j < nComponents; j++)
	  estimator[i]->addSystem((*systemMatrix)[i][j], 
				  solution->getDOFVector(j), 
				  rhs->getDOFVector(j));      
508
509
510
    }
  }

511

512
  void ProblemStatSeq::createMarker()
513
  {
514
    FUNCNAME("ProblemStat::createMarker()");
515

Thomas Witkowski's avatar
Thomas Witkowski committed
516
    int nMarkersCreated = 0;
517

518
    for (int i = 0; i < nComponents; i++) {
519
      marker[i] = Marker::createMarker
520
	(name + "->marker[" + boost::lexical_cast<string>(i) + "]", i);
521

522
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
523
524
525
526
	nMarkersCreated++;

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

533

534
  void ProblemStatSeq::createFileWriter()
535
  {
536
    FUNCNAME("ProblemStat::createFileWriter()");
537
538
  
    // Create one filewriter for all components of the problem
539
540
541
    string numberedName  = name + "->output";
    string filename = "";
    Parameters::get(numberedName + "->filename", filename);
542
543

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
550
	solutionList[i] = solution->getDOFVector(i);
551
552
      }

Thomas Witkowski's avatar
Thomas Witkowski committed
553
      fileWriters.push_back(new FileWriter(numberedName,
554
555
					   componentMeshes[0],
					   solutionList));
556
557
558
    }

    // Create own filewriters for each components of the problem
559
    for (int i = 0; i < nComponents; i++) {
560
      numberedName = name + "->output[" + boost::lexical_cast<string>(i) + "]";
561
      filename = "";
562
      Parameters::get(numberedName + "->filename", filename);
563

564
      if (filename != "")
Thomas Witkowski's avatar
Thomas Witkowski committed
565
	fileWriters.push_back(new FileWriter(numberedName, 
566
					     componentMeshes[i], 
567
					     solution->getDOFVector(i)));      
568
569
570
    }

    int writeSerialization = 0;
571
    Parameters::get(name + "->output->write serialization", writeSerialization);
572
    if (writeSerialization)
573
      fileWriters.push_back(new Serializer<ProblemStatSeq>(this));
574
575
  }

576

577
  void ProblemStatSeq::doOtherStuff()
578
579
580
  {
  }

581

582
  void ProblemStatSeq::solve(AdaptInfo *adaptInfo, bool, bool)
583
584
585
  {
    FUNCNAME("Problem::solve()");

Thomas Witkowski's avatar
Thomas Witkowski committed
586
    if (!solver) {
587
588
589
590
591
      WARNING("no solver\n");
      return;
    }

    clock_t first = clock();
592
    
593
    solver->solveSystem(solverMatrix, *solution, *rhs);
594

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

598
    adaptInfo->setSolverIterations(solver->getIterations());
Thomas Witkowski's avatar
Thomas Witkowski committed
599
600
601
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
602
603
  }

604

605
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
606
  {
607
    FUNCNAME("ProblemStat::estimate()");
608

609
610
611
612
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
613
614
#endif

615
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
616
      computeError(adaptInfo);
617
618
    } else {
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
619
	Estimator *scalEstimator = estimator[i];
620
621
	
	if (scalEstimator) {
622
623
	  traverseInfo.updateStatus();
	  scalEstimator->setTraverseInfo(traverseInfo);
624
	  scalEstimator->estimate(adaptInfo->getTimestep());
625

626
627
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
628
629
630
631
632

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
633
	}
634
635
636
      }
    }

637
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
638
    MPI::COMM_WORLD.Barrier();
639
640
    INFO(info, 8)("estimation of the error needed %.5f seconds\n", 
		  MPI::Wtime() - first);
641
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
642
643
    INFO(info, 8)("estimation of the error needed %.5f seconds\n",
		  TIME_USED(first, clock()));
644
#endif
645
646
  }

647

648
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
649
  {
650
    FUNCNAME("ProblemStat::markElements()");
651

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

655
    Flag markFlag = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
656
    for (int i = 0; i < nComponents; i++)
657
      if (marker[i])
Thomas Witkowski's avatar
Thomas Witkowski committed
658
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
659
    
660
661
662
    return markFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
663

664
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
665
  {
666
    FUNCNAME("ProblemStat::refineMesh()");
667

Thomas Witkowski's avatar
Thomas Witkowski committed
668
    int nMeshes = static_cast<int>(meshes.size());
669
    Flag refineFlag = 0;
670
671
672
673
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isRefinementAllowed(i))
	refineFlag |= refinementManager->refineMesh(meshes[i]);

674
675
676
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
677

678
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
679
  {
680
    FUNCNAME("ProblemStat::coarsenMesh()");
681

Thomas Witkowski's avatar
Thomas Witkowski committed
682
    int nMeshes = static_cast<int>(meshes.size());
683
    Flag coarsenFlag = 0;
684
685
    for (int i = 0; i < nMeshes; i++)
      if (adaptInfo->isCoarseningAllowed(i))
Thomas Witkowski's avatar
Thomas Witkowski committed
686
	coarsenFlag |= coarseningManager->coarsenMesh(meshes[i]);
687
688
689
690

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
691

692
  Flag ProblemStatSeq::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
693
  {
694
    FUNCNAME("ProblemStat::oneIteration()");
695

696
697
    for (int i = 0; i < nComponents; i++)
      if (adaptInfo->spaceToleranceReached(i))
698
 	adaptInfo->allowRefinement(false, i);
699
      else
700
 	adaptInfo->allowRefinement(true, i);	
701
702
703
704

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

Thomas Witkowski's avatar
Thomas Witkowski committed
705

706
707
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
708
  {
709
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
710

711
712
713
//     buildAfterCoarsen_sebastianMode(adaptInfo, flag);
//     return;

714
    if (dualMeshTraverseRequired()) {
715
716
717
718
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
      ERROR_EXIT("Dual mesh assemble does not work in parallel code!\n");
#endif

719
      dualAssemble(adaptInfo, flag, asmMatrix, asmVector);
720
721
      return;
    }
722

723
724
725
726
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#endif

727

Thomas Witkowski's avatar
Thomas Witkowski committed
728
729
730
731
    for (unsigned int i = 0; i < meshes.size(); i++)
      meshes[i]->dofCompress();


732
733
734
735
736
737
738
739
740
#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
741

742
743
744

    Flag assembleFlag = 
      flag | 
Thomas Witkowski's avatar
Thomas Witkowski committed
745
746
      (*systemMatrix)[0][0]->getAssembleFlag() | 
      rhs->getDOFVector(0)->getAssembleFlag()  |
747
748
749
750
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
751
752
      Mesh::FILL_NEIGH;

Thomas Witkowski's avatar
Thomas Witkowski committed
753
    if (useGetBound)
754
      assembleFlag |= Mesh::FILL_BOUND;
Thomas Witkowski's avatar
Thomas Witkowski committed
755
756

  
Thomas Witkowski's avatar
Thomas Witkowski committed
757
    traverseInfo.updateStatus();
758
759
760
    // Used to calculate the overall number of non zero entries.
    int nnz = 0;

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

763
      MSG("%d DOFs for %s\n", 
764
	  componentSpaces[i]->getAdmin()->getUsedDofs(), 
765
766
767
768
	  componentSpaces[i]->getName().c_str());

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

769
      for (int j = 0; j < nComponents; j++) {
770

771
	if (writeAsmInfo)
772
	  MSG("--------- %d %d -------------\n", i, j);
773

774
775
776
777
	// 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).
778
	DOFMatrix *matrix = (asmMatrix ? (*systemMatrix)[i][j] : NULL);
779

780
	if (writeAsmInfo && matrix) {
781
	  for (vector<Operator*>::iterator it = matrix->getOperatorsBegin();
782
783
784
	       it != matrix->getOperatorsEnd(); ++it) {
	    Assembler *assembler = (*it)->getAssembler();
	    if (assembler->getZeroOrderAssembler()) 
785
	      cout << "ZOA: " << assembler->getZeroOrderAssembler()->getName() << endl;
786
	    if (assembler->getFirstOrderAssembler(GRD_PSI)) 
Thomas Witkowski's avatar