ProblemScal.cc 19.5 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
89
  }
  
  void ProblemScal::addDirichletBC(BoundaryType type, 
				   AbstractFunction<double, WorldVector<double> >* b) 
  {
    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
114
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       AbstractFunction<double, WorldVector<double> > *n,
			       AbstractFunction<double, WorldVector<double> > *r)
  {
    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
124
  }

  void ProblemScal::addNeumannBC(BoundaryType type, 
				 AbstractFunction<double, WorldVector<double> > *n)
  {
    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
133
  }

  void ProblemScal::addRobinBC(BoundaryType type, 
			       DOFVector<double> *n,
			       DOFVector<double> *r)
  {
    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
142
143
  }

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

144
145
146
147
    if (systemMatrix) 
      systemMatrix->getBoundaryManager()->addBoundaryCondition(periodic);
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(periodic);
148
149
150
151
152
153
154
  }

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
157
158
    GET_PARAMETER(0, name + "->info", "%d", &info_);
    GET_PARAMETER(0, name + "->mesh", &meshName);
159

160
    TEST_EXIT(meshName != "")("no mesh name specified\n");
161
162
163

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

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

Thomas Witkowski's avatar
Thomas Witkowski committed
170
    switch (dim) {
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
    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");
    }
  }

188
189
190

  void ProblemScal::setMeshFromProblemVec(ProblemVec* pv, int i) 
  { 
191
    mesh = pv->getMesh(i);
192
193
194
195
196
    coarseningManager_ = pv->getCoarseningManager(i);
    refinementManager_ = pv->getRefinementManager(i);
  }


197
198
  Flag ProblemScal::markElements(AdaptInfo *adaptInfo) 
  { 
199
200
    if (marker)
      return marker->markMesh(adaptInfo, mesh);
201
202
    else
      WARNING("no marker\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
203

204
205
206
    return 0;
  }

207

208
209
  Flag ProblemScal::refineMesh(AdaptInfo *adaptInfo) 
  { 
210
    return refinementManager_->refineMesh(mesh); 
211
212
  }

213

214
215
  Flag ProblemScal::coarsenMesh(AdaptInfo *adaptInfo) 
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
216
    if (adaptInfo->isCoarseningAllowed(0)) {
217
      return coarseningManager_->coarsenMesh(mesh);
Thomas Witkowski's avatar
Thomas Witkowski committed
218
    } else {
219
220
221
222
223
      WARNING("coarsening not allowed\n");
      return 0;
    }
  }

224
  void ProblemScal::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
225
226
  {
    FUNCNAME("Problem::solve()");
227
    if (!solver) {
228
229
230
231
232
233
234
235
236
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
Thomas Witkowski's avatar
Thomas Witkowski committed
237
    int iter = solver->solve(matVec, solution, rhs, leftPrecon, rightPrecon); 
238
239
240
241
242
243
244
245
246
247
248

#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);
249
250
251
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
252
253
254
255
256
257
258
259
260
  }

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

    // === create mesh ===
261
    if (mesh) { 
262
263
      WARNING("mesh already created\n");
    } else {
264
      if (initFlag.isSet(CREATE_MESH) || 
265
	 ((!adoptFlag.isSet(INIT_MESH))&&
Thomas Witkowski's avatar
Thomas Witkowski committed
266
267
268
269
	  (initFlag.isSet(INIT_SYSTEM)||initFlag.isSet(INIT_FE_SPACE)))) {
	createMesh();
      } 
      
270
      if (adoptProblem && 
271
272
	 (adoptFlag.isSet(INIT_MESH) || 
	  adoptFlag.isSet(INIT_SYSTEM) ||
Thomas Witkowski's avatar
Thomas Witkowski committed
273
	  adoptFlag.isSet(INIT_FE_SPACE))) {
274
275
	TEST_EXIT(!mesh)("mesh already created\n");
	mesh = adoptProblem->getMesh();
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
278
	refinementManager_ = adoptProblem->refinementManager_;
	coarseningManager_ = adoptProblem->coarseningManager_;
      }
279
280
    }

281
    if (!mesh) 
282
      WARNING("no mesh created\n");
283
284

    // === create fespace ===
285
    if (feSpace_) {
286
287
      WARNING("feSpace already created\n");
    } else {
288
      if (initFlag.isSet(INIT_FE_SPACE) || (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
289
290
	createFESpace();
      } 
Thomas Witkowski's avatar
Thomas Witkowski committed
291
292
293
294
      if (adoptProblem && (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
	TEST_EXIT(!feSpace_)("feSpace already created");
	feSpace_ = dynamic_cast<ProblemScal*>(adoptProblem)->getFESpace();
      }
295
296
    }

297
298
    if (!feSpace_) 
      WARNING("no feSpace created\n");
299
300
301
302
303
304

    // === create system ===
    if (initFlag.isSet(INIT_SYSTEM)) {
      createMatricesAndVectors();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_SYSTEM)) {
305
306
307
308
309
310
      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();
311
312
313
    }

    // === create solver ===
314
    if (solver) {
315
316
317
318
319
320
      WARNING("solver already created\n");
    } else {
      if (initFlag.isSet(INIT_SOLVER)) {
	createSolver();
      } 
      if (adoptProblem && adoptFlag.isSet(INIT_SOLVER)) {
321
322
	TEST_EXIT(!solver)("solver already created\n");
	solver = adoptProblem->getSolver();
323
324
325
      }
    }

326
    if (!solver) 
327
328
329
      WARNING("no solver created\n");

    // === create estimator ===
330
    if (estimator) {
331
332
      WARNING("estimator already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
333
      if (initFlag.isSet(INIT_ESTIMATOR)) {
334
335
	createEstimator();
      } 
Thomas Witkowski's avatar
Thomas Witkowski committed
336
      if (adoptProblem && adoptFlag.isSet(INIT_ESTIMATOR)) {
337
338
	TEST_EXIT(!estimator)("estimator already created\n");
	estimator = adoptProblem->getEstimator();
339
340
341
      } 
    }

342
    if (!estimator) 
343
      WARNING("no estimator created\n");
344
345

    // === create marker ===
346
    if (marker) {
347
348
      WARNING("marker already created\n");
    } else {
349
      if (initFlag.isSet(INIT_MARKER)) {
350
351
	createMarker();
      } 
352
      if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
353
354
	TEST_EXIT(!marker)("marker already created\n");
	marker = adoptProblem->getMarker();
355
356
357
      } 
    }

358
    if (!marker) 
359
      WARNING("no marker created\n");
360
361

    // === create file writer ===
362
    if (initFlag.isSet(INIT_FILEWRITER)) {
363
364
365
366
367
368
369
370
371
372
373
374
375
376
      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
377
    std::string serializationFilename = "";
378
379
380
381
382
    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
383
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
384
385
		    &readSerialization);
      if (readSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
386
	GET_PARAMETER(0, name + "->input->serialization filename", 
387
388
389
390
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
Thomas Witkowski's avatar
Thomas Witkowski committed
391
	std::ifstream in(serializationFilename.c_str());
392
393
394
	deserialize(in);
	in.close();
      } else {
395
396
	if (initFlag.isSet(INIT_MESH) && mesh && !mesh->isInitialized()) {
	  mesh->initialize();
397
398
399
	}
    
	// === read value file and use it for the mesh values ===
Thomas Witkowski's avatar
Thomas Witkowski committed
400
	std::string valueFilename("");
401
	GET_PARAMETER(0, mesh->getName() + "->value file name", &valueFilename);     
402
403
	if (valueFilename.length()) {     
	  ValueReader::readValue(valueFilename.c_str(),
404
405
406
407
				 mesh,
				 solution,
				 mesh->getMacroFileInfo());
	  mesh->clearMacroFileInfo();
408
409
	}
	
410
		
411
	// === do global refinements ===
412
413
	if (initFlag.isSet(INIT_GLOBAL_REFINES)) {
	  int globalRefinements = 0;
414
415
	  GET_PARAMETER(0, mesh->getName() + "->global refinements", "%d", &globalRefinements);
	  refinementManager_->globalRefine(mesh, globalRefinements);	
416
	}
417
418
419
420
421
422
423
424
      }
    }
  }

  void ProblemScal::createFESpace()
  {
    // create finite element space
    int degree = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
425
    GET_PARAMETER(0, name + "->polynomial degree" ,"%d", &degree);
426
    feSpace_ = FiniteElemSpace::provideFESpace(NULL,
427
428
					       Lagrange::getLagrange(mesh->getDim(), degree),
					       mesh,
Thomas Witkowski's avatar
Thomas Witkowski committed
429
					       name + "->feSpace");  
430
431

    // create dof admin for vertex dofs if neccessary
432
433
    if (mesh->getNumberOfDOFs(VERTEX) == 0) {
      DimVec<int> ln_dof(mesh->getDim(), DEFAULT_VALUE, 0);
434
      ln_dof[VERTEX]= 1;
435
      mesh->createDOFAdmin("vertex dofs", ln_dof);
436
437
438
439
440
441
    }
  }

  void ProblemScal::createMatricesAndVectors()
  {
    // === create vectors and system matrix ===
442
443
444
    systemMatrix = NEW DOFMatrix(feSpace_, feSpace_, "system matrix");
    rhs = NEW DOFVector<double>(feSpace_, "rhs");
    solution = NEW DOFVector<double>(feSpace_, "solution");
445

446
447
    solution->setCoarsenOperation(COARSE_INTERPOL);
    solution->set(0.0);      /*  initialize u_h  !                      */
448
449

    // === create matVec ===
Thomas Witkowski's avatar
Thomas Witkowski committed
450
    matVec = NEW StandardMatVec<DOFMatrix, DOFVector<double> >(systemMatrix);
451
452
453
454
455
  }

  void ProblemScal::createSolver()
  {
    // === create solver ===
Thomas Witkowski's avatar
Thomas Witkowski committed
456
    std::string solverType("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
457
    GET_PARAMETER(0, name + "->solver", &solverType);
458
    OEMSolverCreator<DOFVector<double> > *solverCreator = 
Thomas Witkowski's avatar
Thomas Witkowski committed
459
      dynamic_cast<OEMSolverCreator<DOFVector<double> >*>(CreatorMap<OEMSolver<DOFVector<double> > >::getCreator(solverType));
460
    TEST_EXIT(solverCreator)("no solver type\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
461
    solverCreator->setName(name + "->solver");
462
463
    solver = solverCreator->create();
    solver->initParameters();
464
465

    // === create preconditioners ===
Thomas Witkowski's avatar
Thomas Witkowski committed
466
    std::string preconType("no");
467
    Preconditioner<DOFVector<double> > *precon;
Thomas Witkowski's avatar
Thomas Witkowski committed
468
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);
469
470
471
472
473
    CreatorInterface<PreconditionerScal> *preconCreator = 
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
Thomas Witkowski's avatar
Thomas Witkowski committed
474
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name + "->solver->left precon");
475
476
477
478
479
    }
  
    precon = preconCreator->create();

    if (precon) {
480
481
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix);
      leftPrecon = precon;
482
483
484
    }

    preconType.assign("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
485
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);
486
487
488
489
    preconCreator = CreatorMap<PreconditionerScal>::getCreator(preconType);

    if (!preconCreator->isNullCreator()) {
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setSizeAndRow(1, 0);
Thomas Witkowski's avatar
Thomas Witkowski committed
490
      dynamic_cast<PreconditionerScalCreator*>(preconCreator)->setName(name + "->solver->left precon");
491
492
493
494
    }

    precon = preconCreator->create();
    if (precon) {
495
496
      dynamic_cast<PreconditionerScal*>(precon)->setMatrix(&systemMatrix);
      rightPrecon = precon;
497
498
499
    }

    // === create vector creator ===
500
    solver->setVectorCreator(new DOFVector<double>::Creator(feSpace_));
501
502
503
504
505
506

  }

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

    // === create estimator ===
Thomas Witkowski's avatar
Thomas Witkowski committed
510
    std::string estimatorType("no");
Thomas Witkowski's avatar
Thomas Witkowski committed
511
    GET_PARAMETER(0, name + "->estimator", &estimatorType);
512
513
514
    EstimatorCreator *estimatorCreator = 
      dynamic_cast<EstimatorCreator*>(
				      CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
515
    if (estimatorCreator) {
Thomas Witkowski's avatar
Thomas Witkowski committed
516
      estimatorCreator->setName(name + "->estimator");
Thomas Witkowski's avatar
Thomas Witkowski committed
517
      if (estimatorType == "recovery") {
518
	dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->setSolution(solution);
519
      }
520
      estimator = estimatorCreator->create();
521
522

      // init estimator
523
      estimator->addSystem(systemMatrix, solution, rhs);
524
525
526
527
528
    }
  }

  void ProblemScal::createMarker()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
529
    marker = dynamic_cast<Marker*>(Marker::createMarker(name + "->marker", -1));
530
531
532
533
  }

  void ProblemScal::createFileWriter()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
534
    fileWriters.push_back(NEW FileWriter(name + "->output", mesh, solution));
535
    int writeSerialization = 0;
Thomas Witkowski's avatar
Thomas Witkowski committed
536
    GET_PARAMETER(0, name + "->output->write serialization", "%d", 
537
		  &writeSerialization);
Thomas Witkowski's avatar
Thomas Witkowski committed
538
    if (writeSerialization) {
539
      fileWriters.push_back(NEW Serializer<ProblemScal>(this));
540
541
542
543
544
545
546
    }
  }

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

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

553
      adaptInfo->setEstSum(estimator->getErrorSum(), 0);
554
555

      adaptInfo->
556
	setTimeEstSum(estimator->getTimeEst(), 0);
557
558

      adaptInfo->
559
	setEstMax(estimator->getErrorMax(), 0);
560
561

      adaptInfo->
562
	setTimeEstMax(estimator->getTimeEstMax(), 0);
563
564
565
566
567
568

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

569
570
  void ProblemScal::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				      bool assembleMatrix, bool assembleVector)
571
  {
572
    FUNCNAME("ProblemScal::buildAfterCoarsen()");
573
574
575

    clock_t first = clock();

576
    mesh->dofCompress();
577
578
579
580
581
582
583

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

    Flag assembleFlag = 
      flag | 
584
585
      systemMatrix->getAssembleFlag() | 
      rhs->getAssembleFlag();
586

587
    if (useGetBound)
588
589
      assembleFlag |= Mesh::FILL_BOUND;

590
591
    systemMatrix->clear();
    rhs->set(0.0);
592
593

    // fill boundary conditions
594
595
596
597
598
599
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->initMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->initVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->initVector(solution);
600

601
    BoundaryType *bound;
602
    TraverseStack stack;
603
604
    ElInfo *elInfo = stack.traverseFirst(mesh, -1, assembleFlag);

605
    while (elInfo) {
606
607
608
609
610
611
612
613
614
615
616
617
618
619
	if (useGetBound) {
	    bound = GET_MEMORY(BoundaryType, feSpace_->getBasisFcts()->getNumber());
	    feSpace_->getBasisFcts()->getBound(elInfo, bound);
	} else {
	    bound = NULL;
	}

	systemMatrix->assemble(1.0, elInfo, bound);
	rhs->assemble(1.0, elInfo, bound);

	if (useGetBound) {
	    FREE_MEMORY(bound, BoundaryType, feSpace_->getBasisFcts()->getNumber());	    
	}

620
621
622
623
624
625
      if (systemMatrix->getBoundaryManager()) 
	systemMatrix->getBoundaryManager()->fillBoundaryConditions(elInfo, systemMatrix);
      if (rhs->getBoundaryManager())
	rhs->getBoundaryManager()->fillBoundaryConditions(elInfo, rhs);
      if (solution->getBoundaryManager())
	solution->getBoundaryManager()->fillBoundaryConditions(elInfo, solution);
626
627
628
      elInfo = stack.traverseNext(elInfo);
    }

629
    systemMatrix->removeRowsWithDBC(systemMatrix->getApplyDBCs());
Thomas Witkowski's avatar
Thomas Witkowski committed
630

631
632
633
634
635
636
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->exitMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->exitVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->exitVector(solution);
637
638
639
640
641

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

Thomas Witkowski's avatar
Thomas Witkowski committed
642
  void ProblemScal::writeResidualMesh(AdaptInfo *adaptInfo, const std::string name)
643
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
644
    FUNCNAME("ProblemScal::writeResidualMesh()");
645

Thomas Witkowski's avatar
Thomas Witkowski committed
646
    std::map<int, double> vec;    
647
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
648
649
    ElInfo *elInfo = stack.traverseFirst(this->getMesh(),  -1, 
					 Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
650
651
    
    while (elInfo) {		  
Thomas Witkowski's avatar
Thomas Witkowski committed
652
      vec[elInfo->getElement()->getIndex()] = elInfo->getElement()->getEstimation(0);
653
654
655
      elInfo = stack.traverseNext(elInfo);
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
656
    ElementFileWriter fw(name, this->getFESpace(), vec);
657
658
659
    fw.writeFiles(adaptInfo, true);    
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
660
  void ProblemScal::serialize(std::ostream &out) 
661
662
663
  {
    FUNCNAME("ProblemScal::serialize()");

664
665
    mesh->serialize(out);
    solution->serialize(out);
666
667
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
668
  void ProblemScal::deserialize(std::istream &in) 
669
670
671
  {
    FUNCNAME("ProblemScal::deserialize()");

672
673
    mesh->deserialize(in);    
    solution->deserialize(in);
674
675
676
  }

}