ProblemStat.cc 49.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
//
// Software License for AMDiS
//
// Copyright (c) 2010 Dresden University of Technology 
// All rights reserved.
// Authors: Simon Vey, Thomas Witkowski et al.
//
// This file is part of AMDiS
//
// See also license.opensource.txt in the distribution.


Thomas Witkowski's avatar
Thomas Witkowski committed
13
#include <sstream>
Thomas Witkowski's avatar
Thomas Witkowski committed
14
#include <boost/lexical_cast.hpp>
15
16

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

namespace AMDiS {

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

47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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
  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);
93
94
    componentNames.resize(nComponents);

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

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

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

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

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

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

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

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

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

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

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

184
185
186
      }
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	// === do global refinements ===
308

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

    doOtherStuff();
  }

318

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

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

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

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


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

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

381

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

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

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

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

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

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

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

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

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

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

432

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

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

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

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

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

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

459

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

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

476

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

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

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

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


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

512

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

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

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

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

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

534

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

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

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

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

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

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

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

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

577

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

581

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

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

    clock_t first = clock();
594
595
596

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

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

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

607

608
  void ProblemStatSeq::estimate(AdaptInfo *adaptInfo) 
609
  {
610
    FUNCNAME("ProblemStat::estimate()");
611

612
613
614
615
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
    double first = MPI::Wtime();
#else
    clock_t first = clock();
616
617
#endif

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

629
630
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
631
632
633
634
635

	  if (adaptInfo->getRosenbrockMode() == false) {
	    adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	    adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	  }
636
	}
637
638
639
      }
    }

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

650

651
  Flag ProblemStatSeq::markElements(AdaptInfo *adaptInfo) 
652
  {
653
    FUNCNAME("ProblemStat::markElements()");
654

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
666

667
  Flag ProblemStatSeq::refineMesh(AdaptInfo *adaptInfo) 
668
  {
669
    FUNCNAME("ProblemStat::refineMesh()");
670

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

677
678
679
    return refineFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
680

681
  Flag ProblemStatSeq::coarsenMesh(AdaptInfo *adaptInfo) 
682
  {
683
    FUNCNAME("ProblemStat::coarsenMesh()");
684

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

    return coarsenFlag;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
694

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

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
708

709
710
  void ProblemStatSeq::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
					 bool asmMatrix, bool asmVector)
711
  {
712
    FUNCNAME("ProblemStat::buildAfterCoarsen()");
713

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 rowComponent = 0; rowComponent < nComponents; rowComponent++) {
762

763
      MSG("%d DOFs for %s\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
764
765
	  componentSpaces[rowComponent]->getAdmin()->getUsedDofs(), 
	  componentSpaces[rowComponent]->getName().c_str());
766

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

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

771
	if (writeAsmInfo)
Thomas Witkowski's avatar
Thomas Witkowski committed
772
	  MSG("--------- %d %d -------------\n", rowComponent, colComponent);
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).
Thomas Witkowski's avatar
Thomas Witkowski committed
778
779
	DOFMatrix *matrix = 
	  (asmMatrix ? (*systemMatrix)[rowComponent][colComponent] : NULL);
780

781
	if (writeAsmInfo && matrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
782
	  MSG("  -> matrix has %d operators\n", matrix->getOperators().size());
783
	  for (ve