ProblemScal.cc 19.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include "ProblemScal.h"
#include "AbstractFunction.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "FixVec.h"
#include "Flag.h"
#include "Serializer.h"
#include "RecoveryEstimator.h"
#include "Operator.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Estimator.h"
#include "OEMSolver.h"
#include "Preconditioner.h"
#include "MatVecMultiplier.h"
#include "DOFVector.h"
#include "Marker.h"
#include "AdaptInfo.h"
#include "ElInfo.h"
#include "FileWriter.h"
#include "RefinementManager.h"
#include "CoarseningManager.h"
#include "Lagrange.h"
#include "PeriodicBC.h"
#include "ValueReader.h"
26
#include "ElementFileWriter.h"
27
28
29

namespace AMDiS {

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
  ProblemScal::~ProblemScal()
  {
    FUNCNAME("ProblemScal::~ProblemScal()");

    for (int i = 0; i < static_cast<int>(fileWriters.size()); i++) {
      DELETE fileWriters[i];
    }

    if (systemMatrix)
      DELETE systemMatrix;
    if (rhs) 
      DELETE rhs;
    if (solution)
      DELETE solution;
    if (estimator)
      DELETE estimator;
    if (marker)
      DELETE marker;
    if (solver)
      DELETE solver;
    if (leftPrecon)
      DELETE leftPrecon;
    if (rightPrecon)
      DELETE rightPrecon;
    if (mesh)
      DELETE mesh;

    FiniteElemSpace::clear();
    Lagrange::clear();
  }

61
  void ProblemScal::writeFiles(AdaptInfo *adaptInfo, bool force) {
62
63
    for (int i = 0; i < static_cast<int>(fileWriters.size()); i++) {
      fileWriters[i]->writeFiles(adaptInfo, force);
Thomas Witkowski's avatar
Thomas Witkowski committed
64
65
66
    }
  }

67
68
  void ProblemScal::interpolInitialSolution(AbstractFunction<double, WorldVector<double> > *fct) 
  {
69
    solution->interpol(fct);
70
71
72
73
74
75
  }

  void ProblemScal::addMatrixOperator(Operator *op, 
				      double *factor,
				      double *estFactor) 
  {
76
    systemMatrix->addOperator(op, factor, estFactor);
77
78
79
80
81
82
  }

  void ProblemScal::addVectorOperator(Operator *op, 
				      double *factor,
				      double *estFactor) 
  {
83
    rhs->addOperator(op, factor, estFactor);
84
85
86
87
88
  }
  
  void ProblemScal::addDirichletBC(BoundaryType type, 
				   AbstractFunction<double, WorldVector<double> >* b) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
89
    DirichletBC *dirichlet = new DirichletBC(type, b, feSpace);
90
91
92
93
94
95
    if (systemMatrix) 
      systemMatrix->getBoundaryManager()->addBoundaryCondition(dirichlet);
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(dirichlet);
    if (solution)
      solution->getBoundaryManager()->addBoundaryCondition(dirichlet);
96
97
98
99
100
101
  }

  void ProblemScal::addDirichletBC(BoundaryType type, 
				   DOFVector<double> *vec) 
  {
    DirichletBC *dirichlet = new DirichletBC(type, vec);
102
103
104
105
106
107
    if (systemMatrix) 
      systemMatrix->getBoundaryManager()->addBoundaryCondition(dirichlet);
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(dirichlet);
    if (solution)
      solution->getBoundaryManager()->addBoundaryCondition(dirichlet);
108
109
110
111
112
113
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       AbstractFunction<double, WorldVector<double> > *n,
			       AbstractFunction<double, WorldVector<double> > *r)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
114
    RobinBC *robin = new RobinBC(type, n, r, feSpace);
115
116
117
118
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(robin);
    if (systemMatrix)
      systemMatrix->getBoundaryManager()->addBoundaryCondition(robin);
119
120
121
122
123
  }

  void ProblemScal::addNeumannBC(BoundaryType type, 
				 AbstractFunction<double, WorldVector<double> > *n)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
124
    NeumannBC *neumann = new NeumannBC(type, n, feSpace);
Thomas Witkowski's avatar
Thomas Witkowski committed
125
    if (rhs) 
126
      rhs->getBoundaryManager()->addBoundaryCondition(neumann);
127
128
129
130
131
132
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       DOFVector<double> *n,
			       DOFVector<double> *r)
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
133
    RobinBC *robin = new RobinBC(type, n, r, feSpace);
134
135
136
137
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(robin);
    if (systemMatrix)
      systemMatrix->getBoundaryManager()->addBoundaryCondition(robin);
138
139
140
141
  }

  void ProblemScal::addPeriodicBC(BoundaryType type) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
142
    PeriodicBC *periodic = new PeriodicBC(type, feSpace);
143

144
145
146
147
    if (systemMatrix) 
      systemMatrix->getBoundaryManager()->addBoundaryCondition(periodic);
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(periodic);
148
149
  }

150
#ifdef HAVE_PARALLEL_AMDIS
Thomas Witkowski's avatar
Thomas Witkowski committed
151
  void ProblemScal::useApplicationOrdering(AO *ao) 
152
153
154
155
156
157
158
159
  {
    applicationOrdering = ao;
    systemMatrix->useApplicationOrdering(ao);
    rhs->useApplicationOrdering(ao);
  }
#endif


160
161
162
163
164
  void ProblemScal::createMesh()
  {
    TEST_EXIT(Parameters::initialized())("parameters not initialized\n");

    // === create problems mesh ===
Thomas Witkowski's avatar
Thomas Witkowski committed
165
    std::string meshName("");
166

Thomas Witkowski's avatar
Thomas Witkowski committed
167
    GET_PARAMETER(0, name + "->info", "%d", &info);
Thomas Witkowski's avatar
Thomas Witkowski committed
168
    GET_PARAMETER(0, name + "->mesh", &meshName);
169

170
    TEST_EXIT(meshName != "")("no mesh name specified\n");
171
172
173

    // get problem dimension
    int dim = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
174
    GET_PARAMETER(0, name + "->dim", "%d", &dim);
175
    TEST_EXIT(dim)("no problem dimension specified!\n");
176
177

    // create the mesh
178
    mesh = NEW Mesh(meshName, dim);
179

Thomas Witkowski's avatar
Thomas Witkowski committed
180
    switch (dim) {
181
    case 1:
182
183
      coarseningManager = NEW CoarseningManager1d();
      refinementManager = NEW RefinementManager1d();
184
185
      break;
    case 2:
186
187
      coarseningManager = NEW CoarseningManager2d();
      refinementManager = NEW RefinementManager2d();
188
189
      break;
    case 3:
190
191
      coarseningManager = NEW CoarseningManager3d();
      refinementManager = NEW RefinementManager3d();
192
193
194
195
196
197
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

198
199
200

  void ProblemScal::setMeshFromProblemVec(ProblemVec* pv, int i) 
  { 
201
    mesh = pv->getMesh(i);
202
203
    coarseningManager = pv->getCoarseningManager(i);
    refinementManager = pv->getRefinementManager(i);
204
205
206
  }


207
208
  Flag ProblemScal::markElements(AdaptInfo *adaptInfo) 
  { 
209
210
    if (marker)
      return marker->markMesh(adaptInfo, mesh);
211
212
    else
      WARNING("no marker\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
213

214
215
216
    return 0;
  }

217

218
219
  Flag ProblemScal::refineMesh(AdaptInfo *adaptInfo) 
  { 
220
    return refinementManager->refineMesh(mesh); 
221
222
  }

223

224
225
  Flag ProblemScal::coarsenMesh(AdaptInfo *adaptInfo) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
226
    if (adaptInfo->isCoarseningAllowed(0)) {
227
      return coarseningManager->coarsenMesh(mesh);
Thomas Witkowski's avatar
Thomas Witkowski committed
228
    } else {
229
230
231
232
233
      WARNING("coarsening not allowed\n");
      return 0;
    }
  }

234
  void ProblemScal::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
235
236
  {
    FUNCNAME("Problem::solve()");
237
    if (!solver) {
238
239
240
241
242
243
244
245
246
      WARNING("no solver\n");
      return;
    }

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

    clock_t first = clock();
Thomas Witkowski's avatar
Thomas Witkowski committed
247
    int iter = solver->solve(matVec, solution, rhs, leftPrecon, rightPrecon); 
248
249

#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
250
    INFO(info, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
251
252
253
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
254
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n",
255
256
257
258
		   TIME_USED(first, clock()));
#endif

    adaptInfo->setSolverIterations(iter);
259
260
261
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
262
263
264
265
266
267
268
269
270
  }

  void ProblemScal::initialize(Flag initFlag,
			       ProblemScal *adoptProblem,
			       Flag adoptFlag) 
  {
    FUNCNAME("Problem::initialize()");

    // === create mesh ===
271
    if (mesh) { 
272
273
      WARNING("mesh already created\n");
    } else {
274
      if (initFlag.isSet(CREATE_MESH) || 
275
	 ((!adoptFlag.isSet(INIT_MESH))&&
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
278
279
	  (initFlag.isSet(INIT_SYSTEM)||initFlag.isSet(INIT_FE_SPACE)))) {
	createMesh();
      } 
      
280
      if (adoptProblem && 
281
282
	 (adoptFlag.isSet(INIT_MESH) || 
	  adoptFlag.isSet(INIT_SYSTEM) ||
Thomas Witkowski's avatar
Thomas Witkowski committed
283
	  adoptFlag.isSet(INIT_FE_SPACE))) {
284
285
	TEST_EXIT(!mesh)("mesh already created\n");
	mesh = adoptProblem->getMesh();
286
287
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
Thomas Witkowski's avatar
Thomas Witkowski committed
288
      }
289
290
    }

291
    if (!mesh) 
292
      WARNING("no mesh created\n");
293
294

    // === create fespace ===
Thomas Witkowski's avatar
Thomas Witkowski committed
295
    if (feSpace) {
296
297
      WARNING("feSpace already created\n");
    } else {
298
299
      if (initFlag.isSet(INIT_FE_SPACE) || 
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
300
301
	createFESpace();
      } 
302
303
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
304
305
	TEST_EXIT(!feSpace)("feSpace already created");
	feSpace = dynamic_cast<ProblemScal*>(adoptProblem)->getFESpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
306
      }
307
308
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
309
    if (!feSpace) 
310
      WARNING("no feSpace created\n");
311
312
313
314
315
316

    // === create system ===
    if (initFlag.isSet(INIT_SYSTEM)) {
      createMatricesAndVectors();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
317
318
319
320
321
322
      TEST_EXIT(!solution)("solution already created\n");
      TEST_EXIT(!rhs)("rhs already created\n");
      TEST_EXIT(!systemMatrix)("systemMatrix already created\n");
      solution = adoptProblem->getSolution();
      rhs = adoptProblem->getRHS();
      systemMatrix  = adoptProblem->getSystemMatrix();
323
324
325
    }

    // === create solver ===
326
    if (solver) {
327
328
329
330
331
332
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
333
334
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
335
336
337
      }
    }

338
    if (!solver) 
339
340
341
      WARNING("no solver created\n");

    // === create estimator ===
342
    if (estimator) {
343
344
      WARNING("estimator already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
345
      if (initFlag.isSet(INIT_ESTIMATOR)) {
346
347
	createEstimator();
      } 
Thomas Witkowski's avatar
Thomas Witkowski committed
348
      if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR)) {
349
350
	TEST_EXIT(!estimator)("estimator already created\n");
	estimator = adoptProblem->getEstimator();
351
352
353
      } 
    }

354
    if (!estimator) 
355
      WARNING("no estimator created\n");
356
357

    // === create marker ===
358
    if (marker) {
359
360
      WARNING("marker already created\n");
    } else {
361
      if (initFlag.isSet(INIT_MARKER)) {
362
363
	createMarker();
      } 
364
      if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
365
366
	TEST_EXIT(!marker)("marker already created\n");
	marker = adoptProblem->getMarker();
367
368
369
      } 
    }

370
    if (!marker) 
371
      WARNING("no marker created\n");
372
373

    // === create file writer ===
374
    if (initFlag.isSet(INIT_FILEWRITER)) {
375
376
377
378
379
380
381
382
383
384
385
386
387
388
      createFileWriter();
    }

  

    // === read serialization and init mesh ===

    // There are two possiblities where the user can define a serialization
    // 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.

    int readSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
389
    std::string serializationFilename = "";
390
391
392
393
394
    GET_PARAMETER(0, "argv->rs", &serializationFilename);

    // 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("")) {
Thomas Witkowski's avatar
Thomas Witkowski committed
395
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
396
397
		    &readSerialization);
      if (readSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
398
	GET_PARAMETER(0, name + "->input->serialization filename", 
399
400
401
402
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
403
	std::ifstream in(serializationFilename.c_str());
404
405
406
	deserialize(in);
	in.close();
      } else {
407
408
	if (initFlag.isSet(INIT_MESH) && mesh && !mesh->isInitialized()) {
	  mesh->initialize();
409
410
411
	}
    
	// === read value file and use it for the mesh values ===
Thomas Witkowski's avatar
Thomas Witkowski committed
412
	std::string valueFilename("");
413
	GET_PARAMETER(0, mesh->getName() + "->value file name", &valueFilename);     
414
415
	if (valueFilename.length()) {     
	  ValueReader::readValue(valueFilename.c_str(),
416
417
418
419
				 mesh,
				 solution,
				 mesh->getMacroFileInfo());
	  mesh->clearMacroFileInfo();
420
421
	}
	
422
		
423
	// === do global refinements ===
424
425
	if (initFlag.isSet(INIT_GLOBAL_REFINES)) {
	  int globalRefinements = 0;
426
	  GET_PARAMETER(0, mesh->getName() + "->global refinements", "%d", &globalRefinements);
427
	  refinementManager->globalRefine(mesh, globalRefinements);	
428
	}
429
430
431
432
433
434
435
436
      }
    }
  }

  void ProblemScal::createFESpace()
  {
    // create finite element space
    int degree = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
437
    GET_PARAMETER(0, name + "->polynomial degree" ,"%d", &degree);
Thomas Witkowski's avatar
Thomas Witkowski committed
438
    feSpace = FiniteElemSpace::provideFESpace(NULL,
439
440
					       Lagrange::getLagrange(mesh->getDim(), degree),
					       mesh,
Thomas Witkowski's avatar
Thomas Witkowski committed
441
					       name + "->feSpace");  
442
443

    // create dof admin for vertex dofs if neccessary
444
445
    if (mesh->getNumberOfDOFs(VERTEX) == 0) {
      DimVec<int> ln_dof(mesh->getDim(), DEFAULT_VALUE, 0);
446
      ln_dof[VERTEX]= 1;
447
      mesh->createDOFAdmin("vertex dofs", ln_dof);
448
449
450
451
452
453
    }
  }

  void ProblemScal::createMatricesAndVectors()
  {
    // === create vectors and system matrix ===
Thomas Witkowski's avatar
Thomas Witkowski committed
454
455
456
    systemMatrix = NEW DOFMatrix(feSpace, feSpace, "system matrix");
    rhs = NEW DOFVector<double>(feSpace, "rhs");
    solution = NEW DOFVector<double>(feSpace, "solution");
457

458
459
    solution->setCoarsenOperation(COARSE_INTERPOL);
    solution->set(0.0);      /*  initialize u_h  !                      */
460
461

    // === create matVec ===
Thomas Witkowski's avatar
Thomas Witkowski committed
462
    matVec = NEW StandardMatVec<DOFMatrix, DOFVector<double> >(systemMatrix);
463
464
465
466
467
  }

  void ProblemScal::createSolver()
  {
    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
468
    std::string solverType("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
469
    GET_PARAMETER(0, name + "->solver", &solverType);
470
    OEMSolverCreator<DOFVector<double> > *solverCreator = 
Thomas Witkowski's avatar
Thomas Witkowski committed
471
      dynamic_cast<OEMSolverCreator<DOFVector<double> >*>(CreatorMap<OEMSolver<DOFVector<double> > >::getCreator(solverType));
472
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
473
    solverCreator->setName(name + "->solver");
474
475
    solver = solverCreator->create();
    solver->initParameters();
476
477

    // === create preconditioners ===
Thomas Witkowski's avatar
Thomas Witkowski committed
478
    std::string preconType("no");
479
    Preconditioner<DOFVector<double> > *precon;
Thomas Witkowski's avatar
Thomas Witkowski committed
480
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);
481
482
483
484
485
    CreatorInterface<PreconditionerScal> *preconCreator = 
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
Thomas Witkowski's avatar
Thomas Witkowski committed
486
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name + "->solver->left precon");
487
488
489
490
491
    }
  
    precon = preconCreator->create();

    if (precon) {
492
493
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix);
      leftPrecon = precon;
494
495
496
    }

    preconType.assign("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
497
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);
498
499
500
501
    preconCreator = CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
Thomas Witkowski's avatar
Thomas Witkowski committed
502
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name + "->solver->left precon");
503
504
505
506
    }

    precon = preconCreator->create();
    if (precon) {
507
508
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix);
      rightPrecon = precon;
509
510
511
    }

    // === create vector creator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
512
    solver->setVectorCreator(new DOFVector<double>::Creator(feSpace));
513
514
515
516
517
518

  }

  void ProblemScal::createEstimator()
  {
    // create and set leaf data prototype
519
    mesh->setElementDataPrototype(NEW LeafDataEstimatable(NEW LeafDataCoarsenable));
520
521

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
522
    std::string estimatorType("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
523
    GET_PARAMETER(0, name + "->estimator", &estimatorType);
524
525
526
    EstimatorCreator *estimatorCreator = 
      dynamic_cast<EstimatorCreator*>(
				      CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
527
    if (estimatorCreator) {
Thomas Witkowski's avatar
Thomas Witkowski committed
528
      estimatorCreator->setName(name + "->estimator");
Thomas Witkowski's avatar
Thomas Witkowski committed
529
      if (estimatorType == "recovery") {
530
	dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->setSolution(solution);
531
      }
532
      estimator = estimatorCreator->create();
533
534

      // init estimator
535
      estimator->addSystem(systemMatrix, solution, rhs);
536
537
538
539
540
    }
  }

  void ProblemScal::createMarker()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
541
    marker = dynamic_cast<Marker*>(Marker::createMarker(name + "->marker", -1));
542
543
544
545
  }

  void ProblemScal::createFileWriter()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
546
    fileWriters.push_back(NEW FileWriter(name + "->output", mesh, solution));
547
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
548
    GET_PARAMETER(0, name + "->output->write serialization", "%d", 
549
		  &writeSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
550
    if (writeSerialization) {
551
      fileWriters.push_back(NEW Serializer<ProblemScal>(this));
552
553
554
555
556
557
558
    }
  }

  void ProblemScal::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("Problem::estimate()");

559
    if (estimator) {
560
      clock_t first = clock();
561
      estimator->estimate(adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
562
      INFO(info,8)("estimation of the error needed %.5f seconds\n",
563
564
		    TIME_USED(first,clock()));

565
      adaptInfo->setEstSum(estimator->getErrorSum(), 0);
566
567

      adaptInfo->
568
	setTimeEstSum(estimator->getTimeEst(), 0);
569
570

      adaptInfo->
571
	setEstMax(estimator->getErrorMax(), 0);
572
573

      adaptInfo->
574
	setTimeEstMax(estimator->getTimeEstMax(), 0);
575
576
577
578
579
580

    } else {
      WARNING("no estimator\n");
    }
  }

581
582
  void ProblemScal::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				      bool assembleMatrix, bool assembleVector)
583
  {
584
    FUNCNAME("ProblemScal::buildAfterCoarsen()");
585
586
587

    clock_t first = clock();

Thomas Witkowski's avatar
Thomas Witkowski committed
588
#ifndef HAVE_PARALLEL_AMDIS
589
    mesh->dofCompress();
Thomas Witkowski's avatar
Thomas Witkowski committed
590
#endif
591
592

    MSG("%d DOFs for %s\n", 
Thomas Witkowski's avatar
Thomas Witkowski committed
593
	feSpace->getAdmin()->getUsedDOFs(), feSpace->getName().c_str());
594
595
596

    Flag assembleFlag = 
      flag | 
597
598
      systemMatrix->getAssembleFlag() | 
      rhs->getAssembleFlag();
599

600
    if (useGetBound)
601
602
      assembleFlag |= Mesh::FILL_BOUND;

603
604
    systemMatrix->clear();
    rhs->set(0.0);
605
606

    // fill boundary conditions
607
608
609
610
611
612
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->initMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->initVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->initVector(solution);
613

614
    BoundaryType *bound;
615
    TraverseStack stack;
616
617
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, assembleFlag);

618
    while (elInfo) {
619
      if (useGetBound) {
Thomas Witkowski's avatar
Thomas Witkowski committed
620
621
	bound = GET_MEMORY(BoundaryType, feSpace->getBasisFcts()->getNumber());
	feSpace->getBasisFcts()->getBound(elInfo, bound);
622
623
624
625
626
627
628
629
      } else {
	bound = NULL;
      }
      
      systemMatrix->assemble(1.0, elInfo, bound);
      rhs->assemble(1.0, elInfo, bound);
      
      if (useGetBound) {
Thomas Witkowski's avatar
Thomas Witkowski committed
630
	FREE_MEMORY(bound, BoundaryType, feSpace->getBasisFcts()->getNumber());	    
631
632
      }
      
633
634
635
636
637
638
      if (systemMatrix->getBoundaryManager()) 
	systemMatrix->getBoundaryManager()->fillBoundaryConditions(elInfo, systemMatrix);
      if (rhs->getBoundaryManager())
	rhs->getBoundaryManager()->fillBoundaryConditions(elInfo, rhs);
      if (solution->getBoundaryManager())
	solution->getBoundaryManager()->fillBoundaryConditions(elInfo, solution);
639
640
641
      elInfo = stack.traverseNext(elInfo);
    }

642
    systemMatrix->removeRowsWithDBC(systemMatrix->getApplyDBCs());
Thomas Witkowski's avatar
Thomas Witkowski committed
643

644
645
646
647
648
649
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->exitMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->exitVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->exitVector(solution);
650

Thomas Witkowski's avatar
Thomas Witkowski committed
651
652
653
654
655
656
657
658
659
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", TIME_USED(first,clock()));

#ifdef HAVE_PARALLEL_AMDIS
//     PetscErrorCode ierr;
//     Mat A;

//     ierr = MatCreate(PETSC_COMM_WORLD, &A); CHKERRQ(ierr);
#endif
    
660
661
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
662
  void ProblemScal::writeResidualMesh(AdaptInfo *adaptInfo, const std::string name)
663
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
664
    FUNCNAME("ProblemScal::writeResidualMesh()");
665

Thomas Witkowski's avatar
Thomas Witkowski committed
666
    std::map<int, double> vec;    
667
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
668
669
    ElInfo *elInfo = stack.traverseFirst(this->getMesh(),  -1, 
					 Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
670
671
    
    while (elInfo) {		  
Thomas Witkowski's avatar
Thomas Witkowski committed
672
      vec[elInfo->getElement()->getIndex()] = elInfo->getElement()->getEstimation(0);
673
674
675
      elInfo = stack.traverseNext(elInfo);
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
676
    ElementFileWriter fw(name, this->getFESpace(), vec);
677
678
679
    fw.writeFiles(adaptInfo, true);    
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
680
  void ProblemScal::serialize(std::ostream &out) 
681
682
683
  {
    FUNCNAME("ProblemScal::serialize()");

684
685
    mesh->serialize(out);
    solution->serialize(out);
686
687
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
688
  void ProblemScal::deserialize(std::istream &in) 
689
690
691
  {
    FUNCNAME("ProblemScal::deserialize()");

692
693
    mesh->deserialize(in);    
    solution->deserialize(in);
694
695
696
  }

}