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
30
31
32

namespace AMDiS {

  ProblemScal *ProblemScal::traversePtr_ = NULL;

  void ProblemScal::writeFiles(AdaptInfo *adaptInfo, bool force) {
Thomas Witkowski's avatar
Thomas Witkowski committed
33
34
35
36
37
38
39
40
41
    for (int i = 0; i < static_cast<int>(fileWriters_.size()); i++) {
      fileWriters_[i]->writeFiles(adaptInfo, force);
    }
  }

  void ProblemScal::writeDelayedFiles()
  {
    for (int i = 0; i < static_cast<int>(fileWriters_.size()); i++) {
      fileWriters_[i]->writeDelayedFiles();
42
43
44
    }
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
45
46
47
48
49
50
51
52
53
54
  bool ProblemScal::existsDelayedCalculation()
  {
    for (int i = 0; i < static_cast<int>(fileWriters_.size()); i++) {
      if (fileWriters_[i]->isWritingDelayed())
	return true;   
    }

    return false;
  }

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  void ProblemScal::interpolInitialSolution(AbstractFunction<double, WorldVector<double> > *fct) 
  {
    solution_->interpol(fct);
  }

  void ProblemScal::addMatrixOperator(Operator *op, 
				      double *factor,
				      double *estFactor) 
  {
    systemMatrix_->addOperator(op, factor, estFactor);
  }

  void ProblemScal::addVectorOperator(Operator *op, 
				      double *factor,
				      double *estFactor) 
  {
    rhs_->addOperator(op, factor, estFactor);
  }
  
  void ProblemScal::addDirichletBC(BoundaryType type, 
				   AbstractFunction<double, WorldVector<double> >* b) 
  {
    DirichletBC *dirichlet = new DirichletBC(type, b, feSpace_);
Thomas Witkowski's avatar
Thomas Witkowski committed
78
    if (systemMatrix_) 
79
      systemMatrix_->getBoundaryManager()->addBoundaryCondition(dirichlet);
Thomas Witkowski's avatar
Thomas Witkowski committed
80
    if (rhs_) 
81
      rhs_->getBoundaryManager()->addBoundaryCondition(dirichlet);
Thomas Witkowski's avatar
Thomas Witkowski committed
82
    if (solution_)
83
84
85
86
87
88
89
      solution_->getBoundaryManager()->addBoundaryCondition(dirichlet);
  }

  void ProblemScal::addDirichletBC(BoundaryType type, 
				   DOFVector<double> *vec) 
  {
    DirichletBC *dirichlet = new DirichletBC(type, vec);
Thomas Witkowski's avatar
Thomas Witkowski committed
90
    if (systemMatrix_) 
91
      systemMatrix_->getBoundaryManager()->addBoundaryCondition(dirichlet);
Thomas Witkowski's avatar
Thomas Witkowski committed
92
    if (rhs_) 
93
      rhs_->getBoundaryManager()->addBoundaryCondition(dirichlet);
Thomas Witkowski's avatar
Thomas Witkowski committed
94
    if (solution_)
95
96
97
98
99
100
101
102
      solution_->getBoundaryManager()->addBoundaryCondition(dirichlet);
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       AbstractFunction<double, WorldVector<double> > *n,
			       AbstractFunction<double, WorldVector<double> > *r)
  {
    RobinBC *robin = new RobinBC(type, n, r, feSpace_);
Thomas Witkowski's avatar
Thomas Witkowski committed
103
    if (rhs_) 
104
      rhs_->getBoundaryManager()->addBoundaryCondition(robin);
Thomas Witkowski's avatar
Thomas Witkowski committed
105
    if (systemMatrix_)
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      systemMatrix_->getBoundaryManager()->addBoundaryCondition(robin);
  }

  void ProblemScal::addNeumannBC(BoundaryType type, 
				 AbstractFunction<double, WorldVector<double> > *n)
  {
    NeumannBC *neumann = new NeumannBC(type, n, feSpace_);
    if(rhs_) 
      rhs_->getBoundaryManager()->addBoundaryCondition(neumann);
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       DOFVector<double> *n,
			       DOFVector<double> *r)
  {
    RobinBC *robin = new RobinBC(type, n, r, feSpace_);
Thomas Witkowski's avatar
Thomas Witkowski committed
122
    if (rhs_) 
123
      rhs_->getBoundaryManager()->addBoundaryCondition(robin);
Thomas Witkowski's avatar
Thomas Witkowski committed
124
    if (systemMatrix_)
125
126
127
128
129
130
131
      systemMatrix_->getBoundaryManager()->addBoundaryCondition(robin);
  }

  void ProblemScal::addPeriodicBC(BoundaryType type) 
  {
    PeriodicBC *periodic = new PeriodicBC(type, feSpace_);

Thomas Witkowski's avatar
Thomas Witkowski committed
132
    if (systemMatrix_) 
133
      systemMatrix_->getBoundaryManager()->addBoundaryCondition(periodic);
Thomas Witkowski's avatar
Thomas Witkowski committed
134
    if (rhs_) 
135
136
137
138
139
140
141
142
      rhs_->getBoundaryManager()->addBoundaryCondition(periodic);
  }

  void ProblemScal::createMesh()
  {
    TEST_EXIT(Parameters::initialized())("parameters not initialized\n");

    // === create problems mesh ===
Thomas Witkowski's avatar
Thomas Witkowski committed
143
    std::string meshName("");
144
145
146
147

    GET_PARAMETER(0, name_ + "->info", "%d", &info_);
    GET_PARAMETER(0, name_ + "->mesh", &meshName);

148
    TEST_EXIT(meshName != "")("no mesh name specified\n");
149
150
151
152

    // get problem dimension
    int dim = 0;
    GET_PARAMETER(0, name_ + "->dim", "%d", &dim);
153
    TEST_EXIT(dim)("no problem dimension specified!\n");
154
155
156
157

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

Thomas Witkowski's avatar
Thomas Witkowski committed
158
    switch (dim) {
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
    case 1:
      coarseningManager_ = NEW CoarseningManager1d();
      refinementManager_ = NEW RefinementManager1d();
      break;
    case 2:
      coarseningManager_ = NEW CoarseningManager2d();
      refinementManager_ = NEW RefinementManager2d();
      break;
    case 3:
      coarseningManager_ = NEW CoarseningManager3d();
      refinementManager_ = NEW RefinementManager3d();
      break;
    default:
      ERROR_EXIT("invalid dim!\n");
    }
  }

176
177
178
179
180
181
182
183
184

  void ProblemScal::setMeshFromProblemVec(ProblemVec* pv, int i) 
  { 
    mesh_ = pv->getMesh(i);
    coarseningManager_ = pv->getCoarseningManager(i);
    refinementManager_ = pv->getRefinementManager(i);
  }


185
186
  Flag ProblemScal::markElements(AdaptInfo *adaptInfo) 
  { 
Thomas Witkowski's avatar
Thomas Witkowski committed
187
    if (marker_)
188
189
190
      return marker_->markMesh(adaptInfo, mesh_);
    else
      WARNING("no marker\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
191

192
193
194
    return 0;
  }

195

196
197
198
199
200
  Flag ProblemScal::refineMesh(AdaptInfo *adaptInfo) 
  { 
    return refinementManager_->refineMesh(mesh_); 
  }

201

202
203
  Flag ProblemScal::coarsenMesh(AdaptInfo *adaptInfo) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
204
    if (adaptInfo->isCoarseningAllowed(0)) {
205
      return coarseningManager_->coarsenMesh(mesh_);
Thomas Witkowski's avatar
Thomas Witkowski committed
206
    } else {
207
208
209
210
211
212
213
214
      WARNING("coarsening not allowed\n");
      return 0;
    }
  }

  void ProblemScal::solve(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("Problem::solve()");
Thomas Witkowski's avatar
Thomas Witkowski committed
215
    if (!solver_) {
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
    int iter = solver_->solve(matVec_, solution_, rhs_, leftPrecon_, rightPrecon_); 

#ifdef _OPENMP
    INFO(info_, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
    INFO(info_, 8)("solution of discrete system needed %.5f seconds\n",
		   TIME_USED(first, clock()));
#endif

    adaptInfo->setSolverIterations(iter);
    adaptInfo->setMaxSolverIterations(solver_->getMaxIterations());
    adaptInfo->setSolverTolerance(solver_->getTolerance());
    adaptInfo->setSolverResidual(solver_->getResidual());
  }

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

    // === create mesh ===
249
    if (mesh_) { 
250
251
      WARNING("mesh already created\n");
    } else {
252
      if (initFlag.isSet(CREATE_MESH) || 
253
	 ((!adoptFlag.isSet(INIT_MESH))&&
Thomas Witkowski's avatar
Thomas Witkowski committed
254
255
256
257
	  (initFlag.isSet(INIT_SYSTEM)||initFlag.isSet(INIT_FE_SPACE)))) {
	createMesh();
      } 
      
258
      if (adoptProblem && 
259
260
	 (adoptFlag.isSet(INIT_MESH) || 
	  adoptFlag.isSet(INIT_SYSTEM) ||
Thomas Witkowski's avatar
Thomas Witkowski committed
261
262
263
264
265
266
	  adoptFlag.isSet(INIT_FE_SPACE))) {
	TEST_EXIT(!mesh_)("mesh already created\n");
	mesh_ = adoptProblem->getMesh();
	refinementManager_ = adoptProblem->refinementManager_;
	coarseningManager_ = adoptProblem->coarseningManager_;
      }
267
268
    }

269
270
    if (!mesh_) 
      WARNING("no mesh created\n");
271
272

    // === create fespace ===
273
    if (feSpace_) {
274
275
      WARNING("feSpace already created\n");
    } else {
276
      if (initFlag.isSet(INIT_FE_SPACE) || (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
277
278
	createFESpace();
      } 
Thomas Witkowski's avatar
Thomas Witkowski committed
279
280
281
282
      if (adoptProblem && (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
	TEST_EXIT(!feSpace_)("feSpace already created");
	feSpace_ = dynamic_cast<ProblemScal*>(adoptProblem)->getFESpace();
      }
283
284
    }

285
286
    if (!feSpace_) 
      WARNING("no feSpace created\n");
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317

    // === create system ===
    if (initFlag.isSet(INIT_SYSTEM)) {
      createMatricesAndVectors();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
      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();
    }

    // === create solver ===
    if (solver_) {
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
	TEST_EXIT(!solver_)("solver already created\n");
	solver_ = adoptProblem->getSolver();
      }
    }

    if (!solver_) 
      WARNING("no solver created\n");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
318
    if (estimator_) {
319
320
      WARNING("estimator already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
321
      if (initFlag.isSet(INIT_ESTIMATOR)) {
322
323
	createEstimator();
      } 
Thomas Witkowski's avatar
Thomas Witkowski committed
324
      if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR)) {
325
326
327
328
329
	TEST_EXIT(!estimator_)("estimator already created\n");
	estimator_ = adoptProblem->getEstimator();
      } 
    }

330
331
    if (!estimator_) 
      WARNING("no estimator created\n");
332
333

    // === create marker ===
334
    if (marker_) {
335
336
      WARNING("marker already created\n");
    } else {
337
      if (initFlag.isSet(INIT_MARKER)) {
338
339
	createMarker();
      } 
340
      if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
341
342
343
344
345
	TEST_EXIT(!marker_)("marker already created\n");
	marker_ = adoptProblem->getMarker();
      } 
    }

346
347
    if (!marker_) 
      WARNING("no marker created\n");
348
349

    // === create file writer ===
350
    if (initFlag.isSet(INIT_FILEWRITER)) {
351
352
353
354
355
356
357
358
359
360
361
362
363
364
      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
365
    std::string serializationFilename = "";
366
367
368
369
370
371
372
373
374
375
376
377
378
    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("")) {
      GET_PARAMETER(0, name_ + "->input->read serialization", "%d", 
		    &readSerialization);
      if (readSerialization) {
	GET_PARAMETER(0, name_ + "->input->serialization filename", 
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
379
	std::ifstream in(serializationFilename.c_str());
380
381
382
383
384
385
386
387
	deserialize(in);
	in.close();
      } else {
	if (initFlag.isSet(INIT_MESH) && mesh_ && !mesh_->isInitialized()) {
	  mesh_->initialize();
	}
    
	// === read value file and use it for the mesh values ===
Thomas Witkowski's avatar
Thomas Witkowski committed
388
	std::string valueFilename("");
389
390
391
392
393
394
395
396
397
	GET_PARAMETER(0, mesh_->getName() + "->value file name", &valueFilename);     
	if (valueFilename.length()) {     
	  ValueReader::readValue(valueFilename.c_str(),
				 mesh_,
				 solution_,
				 mesh_->getMacroFileInfo());
	  mesh_->clearMacroFileInfo();
	}
	
398
		
399
	// === do global refinements ===
400
401
402
403
404
	if (initFlag.isSet(INIT_GLOBAL_REFINES)) {
	  int globalRefinements = 0;
	  GET_PARAMETER(0, mesh_->getName() + "->global refinements", "%d", &globalRefinements);
	  refinementManager_->globalRefine(mesh_, globalRefinements);	
	}
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
      }
    }
  }

  void ProblemScal::createFESpace()
  {
    // create finite element space
    int degree = 1;
    GET_PARAMETER(0, name_ + "->polynomial degree" ,"%d", &degree);
    feSpace_ = FiniteElemSpace::provideFESpace(NULL,
					       Lagrange::getLagrange(mesh_->getDim(), degree),
					       mesh_,
					       name_ + "->feSpace");  

    // create dof admin for vertex dofs if neccessary
    if (mesh_->getNumberOfDOFs(VERTEX) == 0) {
      DimVec<int> ln_dof(mesh_->getDim(), DEFAULT_VALUE, 0);
      ln_dof[VERTEX]= 1;
      mesh_->createDOFAdmin("vertex dofs", ln_dof);
    }
  }

  void ProblemScal::createMatricesAndVectors()
  {
    // === create vectors and system matrix ===
    systemMatrix_ = NEW DOFMatrix(feSpace_, feSpace_, "system matrix");
    rhs_          = NEW DOFVector<double>(feSpace_, "rhs");
    solution_     = NEW DOFVector<double>(feSpace_, "solution");

    solution_->refineInterpol(true);
    solution_->setCoarsenOperation(COARSE_INTERPOL);
    solution_->set(0.0);      /*  initialize u_h  !                      */

    // === create matVec ===
    matVec_ = NEW StandardMatVec<DOFMatrix, DOFVector<double> >(systemMatrix_);
  }

  void ProblemScal::createSolver()
  {
    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
445
    std::string solverType("no");
446
447
448
449
450
451
452
453
454
455
    GET_PARAMETER(0, name_ + "->solver", &solverType);
    OEMSolverCreator<DOFVector<double> > *solverCreator = 
      dynamic_cast<OEMSolverCreator<DOFVector<double> >*>(
							  CreatorMap<OEMSolver<DOFVector<double> > >::getCreator(solverType));
    TEST_EXIT(solverCreator)("no solver type\n");
    solverCreator->setName(name_ + "->solver");
    solver_ = solverCreator->create();
    solver_->initParameters();

    // === create preconditioners ===
Thomas Witkowski's avatar
Thomas Witkowski committed
456
    std::string preconType("no");
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
    Preconditioner<DOFVector<double> > *precon;
    GET_PARAMETER(0, name_ + "->solver->left precon", &preconType);
    CreatorInterface<PreconditionerScal> *preconCreator = 
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name_ + "->solver->left precon");
    }
  
    precon = preconCreator->create();

    if (precon) {
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix_);
      leftPrecon_ = precon;
    }

    preconType.assign("no");
    GET_PARAMETER(0, name_ + "->solver->right precon", &preconType);
    preconCreator = CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name_ + "->solver->left precon");
    }

    precon = preconCreator->create();
    if (precon) {
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix_);
      rightPrecon_ = precon;
    }

    // === create vector creator ===
    solver_->setVectorCreator(new DOFVector<double>::Creator(feSpace_));

  }

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

    // create estimator
    //estimator = NEW ResidualEstimator(name + "->estimator");

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
504
    std::string estimatorType("no");
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
    GET_PARAMETER(0, name_ + "->estimator", &estimatorType);
    EstimatorCreator *estimatorCreator = 
      dynamic_cast<EstimatorCreator*>(
				      CreatorMap<Estimator>::getCreator(estimatorType));
    if(estimatorCreator) {
      estimatorCreator->setName(name_ + "->estimator");
      if(estimatorType == "recovery") {
	dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->setSolution(solution_);
      }
      estimator_ = estimatorCreator->create();

      // init estimator
      estimator_->addSystem(systemMatrix_, solution_, rhs_);
    }
  }

  void ProblemScal::createMarker()
  {
    marker_ = dynamic_cast<Marker*>(Marker::createMarker(name_ + "->marker", -1));
  }

  void ProblemScal::createFileWriter()
  {
    fileWriters_.push_back(NEW FileWriter(name_ + "->output", mesh_, solution_));
    int writeSerialization = 0;
    GET_PARAMETER(0, name_ + "->output->write serialization", "%d", 
		  &writeSerialization);
    if(writeSerialization) {
      fileWriters_.push_back(NEW Serializer<ProblemScal>(this));
    }
  }

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

    if(estimator_) {
      clock_t first = clock();
      estimator_->estimate(adaptInfo->getTimestep());
      INFO(info_,8)("estimation of the error needed %.5f seconds\n",
		    TIME_USED(first,clock()));

      adaptInfo->setEstSum(estimator_->getErrorSum(), 0);

      adaptInfo->
	setTimeEstSum(estimator_->getTimeEst(), 0);

      adaptInfo->
	setEstMax(estimator_->getErrorMax(), 0);

      adaptInfo->
	setTimeEstMax(estimator_->getTimeEstMax(), 0);

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

  void ProblemScal::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag)
  {
565
    FUNCNAME("ProblemScal::buildAfterCoarsen()");
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604

    clock_t first = clock();

    mesh_->dofCompress();

    MSG("%d DOFs for %s\n", 
	feSpace_->getAdmin()->getUsedSize(), 
	feSpace_->getName().c_str());

    Flag assembleFlag = 
      flag | 
      systemMatrix_->getAssembleFlag() | 
      rhs_->getAssembleFlag();

    if (useGetBound_)
      assembleFlag |= Mesh::FILL_BOUND;

    systemMatrix_->clear();
    rhs_->set(0.0);

    traversePtr_ = this;
    mesh_->traverse(-1, assembleFlag, &buildAfterCoarsenFct);

    // fill boundary conditions
    if (systemMatrix_->getBoundaryManager())
      systemMatrix_->getBoundaryManager()->initMatrix(systemMatrix_);
    if (rhs_->getBoundaryManager())
      rhs_->getBoundaryManager()->initVector(rhs_);
    if (solution_->getBoundaryManager())
      solution_->getBoundaryManager()->initVector(solution_);

    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh_, -1, 
					 Mesh::CALL_LEAF_EL | 
					 Mesh::FILL_BOUND |
					 Mesh::FILL_COORDS |
					 Mesh::FILL_DET |
					 Mesh::FILL_GRD_LAMBDA |
					 Mesh::FILL_NEIGH);
Thomas Witkowski's avatar
Thomas Witkowski committed
605

606
607
    // for all elements ...
    while (elInfo) {
Thomas Witkowski's avatar
Thomas Witkowski committed
608
      if (systemMatrix_->getBoundaryManager()) 
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
	systemMatrix_->getBoundaryManager()->fillBoundaryConditions(elInfo, systemMatrix_);
      if (rhs_->getBoundaryManager())
	rhs_->getBoundaryManager()->fillBoundaryConditions(elInfo, rhs_);
      if (solution_->getBoundaryManager())
	solution_->getBoundaryManager()->fillBoundaryConditions(elInfo, solution_);
      elInfo = stack.traverseNext(elInfo);
    }

    if (systemMatrix_->getBoundaryManager())
      systemMatrix_->getBoundaryManager()->exitMatrix(systemMatrix_);
    if (rhs_->getBoundaryManager())
      rhs_->getBoundaryManager()->exitVector(rhs_);
    if (solution_->getBoundaryManager())
      solution_->getBoundaryManager()->exitVector(solution_);

    INFO(info_, 8)("buildAfterCoarsen needed %.5f seconds\n",
		   TIME_USED(first,clock()));

    return;
  }

  int ProblemScal::buildAfterCoarsenFct(ElInfo *elInfo) 
  {
    const BoundaryType *bound;

634
    if (traversePtr_->getBoundUsed()) {
635
      bound = traversePtr_->getFESpace()->getBasisFcts()->getBound(elInfo, NULL);
636
    } else {
637
      bound = NULL;
638
    }
639
640
641
642
643
644
645
  
    traversePtr_->getSystemMatrix()->assemble(1.0, elInfo, bound);
    traversePtr_->getRHS()->assemble(1.0, elInfo, bound);

    return 0;
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
646
  void ProblemScal::writeResidualMesh(AdaptInfo *adaptInfo, const std::string name)
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
  {
    FUNCNAME("ProblemVec::writeResidualMesh()");

    Mesh *mesh = this->getMesh();
    FiniteElemSpace *fe = this->getFESpace();
    
    std::map<int, double> vec;
    
    TraverseStack stack;
    ElInfo *elInfo = stack.traverseFirst(mesh,
					 -1, 
					 Mesh::CALL_LEAF_EL | 
					 Mesh::FILL_COORDS);
    
    while (elInfo) {		  
      Element *el = elInfo->getElement();
      double lError = el->getEstimation(0);
      
      vec[elInfo->getElement()->getIndex()] = lError;
      elInfo = stack.traverseNext(elInfo);
    }
    
    ElementFileWriter fw(name, mesh, fe, vec);
    fw.writeFiles(adaptInfo, true);    
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
673
  void ProblemScal::serialize(std::ostream &out) 
674
675
676
677
678
679
680
  {
    FUNCNAME("ProblemScal::serialize()");

    mesh_->serialize(out);
    solution_->serialize(out);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
681
  void ProblemScal::deserialize(std::istream &in) 
682
683
684
685
686
687
688
689
  {
    FUNCNAME("ProblemScal::deserialize()");

    mesh_->deserialize(in);    
    solution_->deserialize(in);
  }

}