ProblemScal.cc 19.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#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 "DOFVector.h"
#include "Marker.h"
#include "AdaptInfo.h"
#include "ElInfo.h"
18
#include "io/FileWriter.h"
19
20
21
22
#include "RefinementManager.h"
#include "CoarseningManager.h"
#include "Lagrange.h"
#include "PeriodicBC.h"
23
24
#include "io/ValueReader.h"
#include "io/ElementFileWriter.h"
25
#include "ProblemVec.h"
26
#include "Debug.h"
27
28
29

namespace AMDiS {

30
31
32
33
  ProblemScal::~ProblemScal()
  {
    FUNCNAME("ProblemScal::~ProblemScal()");

34
35
    for (int i = 0; i < static_cast<int>(fileWriters.size()); i++)
      delete fileWriters[i];
36
37

    if (systemMatrix)
38
      delete systemMatrix;
39
    if (rhs) 
40
      delete rhs;
41
    if (solution)
42
      delete solution;
43
    if (estimator)
44
      delete estimator;
45
    if (marker)
46
      delete marker;
47
    if (solver)
48
      delete solver;
49
    if (mesh)
50
      delete mesh;
51
52
53
54
55

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

56
57
58
  void ProblemScal::writeFiles(AdaptInfo *adaptInfo, bool force) 
  {
    for (int i = 0; i < static_cast<int>(fileWriters.size()); i++)
59
      fileWriters[i]->writeFiles(adaptInfo, force);
Thomas Witkowski's avatar
Thomas Witkowski committed
60
61
  }

62
63
64
65
66
  void ProblemScal::writeFiles(AdaptInfo &adaptInfo, bool force) 
  {
    writeFiles(&adaptInfo, force);
  }

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

72
  void ProblemScal::addMatrixOperator(Operator *op, double *factor, double *estFactor) 
73
  {
74
    systemMatrix->addOperator(op, factor, estFactor);
75
76
  }

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

  void ProblemScal::addVectorOperator(Operator *op, double *factor, double *estFactor) 
83
  {
84
    rhs->addOperator(op, factor, estFactor);
85
  }
86
87
88
89
90

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

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

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

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

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

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

148
149
150
151
    if (systemMatrix) 
      systemMatrix->getBoundaryManager()->addBoundaryCondition(periodic);
    if (rhs) 
      rhs->getBoundaryManager()->addBoundaryCondition(periodic);
152
153
  }

154

155
156
157
158
159
  void ProblemScal::createMesh()
  {
    TEST_EXIT(Parameters::initialized())("parameters not initialized\n");

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

Thomas Witkowski's avatar
Thomas Witkowski committed
162
    GET_PARAMETER(0, name + "->info", "%d", &info);
Thomas Witkowski's avatar
Thomas Witkowski committed
163
    GET_PARAMETER(0, name + "->mesh", &meshName);
164

165
    TEST_EXIT(meshName != "")("no mesh name specified\n");
166
167
168

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

    // create the mesh
173
    mesh = new Mesh(meshName, dim);
174

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

193
194
  void ProblemScal::setMeshFromProblemVec(ProblemVec* pv, int i) 
  { 
195
    mesh = pv->getMesh(i);
196
197
    coarseningManager = pv->getCoarseningManager(i);
    refinementManager = pv->getRefinementManager(i);
198
199
  }

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

207
208
209
210
211
    return 0;
  }

  Flag ProblemScal::refineMesh(AdaptInfo *adaptInfo) 
  { 
212
    return refinementManager->refineMesh(mesh); 
213
214
215
216
  }

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

225
  void ProblemScal::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
226
227
  {
    FUNCNAME("Problem::solve()");
228

229
    if (!solver) {
230
231
232
233
234
235
236
      WARNING("no solver\n");
      return;
    }

#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif
237
238
    clock_t first = clock();    

239
240
    SolverMatrix<DOFMatrix> solverMatrix;
    solverMatrix.setMatrix(*systemMatrix);
241
    solver->solveSystem(solverMatrix, *solution, *rhs);
242
243

#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
244
    INFO(info, 8)("solution of discrete system needed %.5f seconds system time / %.5f seconds wallclock time\n",
245
246
247
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
Thomas Witkowski's avatar
Thomas Witkowski committed
248
    INFO(info, 8)("solution of discrete system needed %.5f seconds\n",
249
250
251
		   TIME_USED(first, clock()));
#endif

252
    adaptInfo->setSolverIterations(solver->getIterations());
253
254
255
    adaptInfo->setMaxSolverIterations(solver->getMaxIterations());
    adaptInfo->setSolverTolerance(solver->getTolerance());
    adaptInfo->setSolverResidual(solver->getResidual());
256
257
258
259
260
261
262
263
  }

  void ProblemScal::initialize(Flag initFlag,
			       ProblemScal *adoptProblem,
			       Flag adoptFlag) 
  {
    FUNCNAME("Problem::initialize()");
    // === create mesh ===
264
    if (mesh) { 
265
266
      WARNING("mesh already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
267
268
269
      if (initFlag.isSet(CREATE_MESH) || 
	  ((!adoptFlag.isSet(INIT_MESH))&&
	   (initFlag.isSet(INIT_SYSTEM)||initFlag.isSet(INIT_FE_SPACE)))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
270
271
272
	createMesh();
      } 
      
Thomas Witkowski's avatar
Thomas Witkowski committed
273
274
275
276
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_MESH) || 
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
277
278
	TEST_EXIT(!mesh)("mesh already created\n");
	mesh = adoptProblem->getMesh();
279
280
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
Thomas Witkowski's avatar
Thomas Witkowski committed
281
      }
282
283
    }

284
    if (!mesh) 
285
      WARNING("no mesh created\n");
286

287
288
289
    if (refinementManager == NULL)
	WARNING("refinement manager not set\n");

290
    // === create fespace ===
Thomas Witkowski's avatar
Thomas Witkowski committed
291
    if (feSpace) {
292
293
      WARNING("feSpace already created\n");
    } else {
294
295
      if (initFlag.isSet(INIT_FE_SPACE) || 
	  (initFlag.isSet(INIT_SYSTEM) && !adoptFlag.isSet(INIT_FE_SPACE))) {
296
	createFeSpace();
297
      } 
298

299
300
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
301
	TEST_EXIT(!feSpace)("feSpace already created");
302
	feSpace = dynamic_cast<ProblemScal*>(adoptProblem)->getFeSpace();
Thomas Witkowski's avatar
Thomas Witkowski committed
303
      }
304
305
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
306
    if (!feSpace) 
307
      WARNING("no feSpace created\n");
308
309
310
311
312
313

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

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

335
    if (!solver) 
336
337
338
      WARNING("no solver created\n");

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

351
    if (!estimator) 
352
      WARNING("no estimator created\n");
353
354

    // === create marker ===
355
    if (marker) {
356
357
      WARNING("marker already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
358
      if (initFlag.isSet(INIT_MARKER))
359
	createMarker();
Thomas Witkowski's avatar
Thomas Witkowski committed
360
361

      if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
362
363
	TEST_EXIT(!marker)("marker already created\n");
	marker = adoptProblem->getMarker();
364
365
366
      } 
    }

367
    if (!marker) 
368
      WARNING("no marker created\n");
369
370

    // === create file writer ===
Thomas Witkowski's avatar
Thomas Witkowski committed
371
    if (initFlag.isSet(INIT_FILEWRITER))
372
      createFileWriter();
Thomas Witkowski's avatar
Thomas Witkowski committed
373
 
374
375
376
377
378
379
380
381
382
383

    // === 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
384
    std::string serializationFilename = "";
385
386
387
388
389
    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
390
      GET_PARAMETER(0, name + "->input->read serialization", "%d", 
391
392
		    &readSerialization);
      if (readSerialization) {
Thomas Witkowski's avatar
Thomas Witkowski committed
393
	GET_PARAMETER(0, name + "->input->serialization filename", 
394
395
396
397
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

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

#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
420
	  GET_PARAMETER(0, mesh->getName() + "->global refinements", "%d", &globalRefinements);
421
422
#endif

423
	  refinementManager->globalRefine(mesh, globalRefinements);	
424
	}
425
426
      }
    }
427
	
428
429
  }

430
  void ProblemScal::createFeSpace()
431
432
433
  {
    // create finite element space
    int degree = 1;
Thomas Witkowski's avatar
Thomas Witkowski committed
434
    GET_PARAMETER(0, name + "->polynomial degree" ,"%d", &degree);
435
    feSpace = FiniteElemSpace::provideFeSpace(NULL,
436
437
					       Lagrange::getLagrange(mesh->getDim(), degree),
					       mesh,
Thomas Witkowski's avatar
Thomas Witkowski committed
438
					       name + "->feSpace");  
439
440

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

  void ProblemScal::createMatricesAndVectors()
  {
    // === create vectors and system matrix ===
451
452
453
    systemMatrix = new DOFMatrix(feSpace, feSpace, "system matrix");
    rhs = new DOFVector<double>(feSpace, "rhs");
    solution = new DOFVector<double>(feSpace, "solution");
454

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

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

  void ProblemScal::createEstimator()
  {
    // create and set leaf data prototype
475
    mesh->setElementDataPrototype(new LeafDataEstimatable(new LeafDataCoarsenable));
476
477

    // === create estimator ===
478
    std::string estimatorType("0");
Thomas Witkowski's avatar
Thomas Witkowski committed
479
    GET_PARAMETER(0, name + "->estimator", &estimatorType);
480
481
482
    EstimatorCreator *estimatorCreator = 
      dynamic_cast<EstimatorCreator*>(
				      CreatorMap<Estimator>::getCreator(estimatorType));
Thomas Witkowski's avatar
Thomas Witkowski committed
483
    if (estimatorCreator) {
Thomas Witkowski's avatar
Thomas Witkowski committed
484
      estimatorCreator->setName(name + "->estimator");
Thomas Witkowski's avatar
Thomas Witkowski committed
485
      if (estimatorType == "recovery") {
486
	dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->setSolution(solution);
487
      }
488
      estimator = estimatorCreator->create();
489
490

      // init estimator
491
      estimator->addSystem(systemMatrix, solution, rhs);
492
493
494
495
496
    }
  }

  void ProblemScal::createMarker()
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
497
    marker = dynamic_cast<Marker*>(Marker::createMarker(name + "->marker", -1));
498
499
500
501
  }

  void ProblemScal::createFileWriter()
  {
502
    fileWriters.push_back(new FileWriter(name + "->output", mesh, solution));
503
    int writeSerialization = 0;
504
505
506
507
508
    GET_PARAMETER(0, name + "->output->write serialization", "%d", &writeSerialization);

    // Serialization is not allowed to be done by the problem, if its part of a parallel
    // problem definition. Than, the parallel problem object must be serialized.
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
509
510
    if (writeSerialization)
      fileWriters.push_back(new Serializer<ProblemScal>(this));
511
#endif
512
513
514
515
516
517
  }

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

518
    if (estimator) {
519
      clock_t first = clock();
520
      estimator->estimate(adaptInfo->getTimestep());
Thomas Witkowski's avatar
Thomas Witkowski committed
521
      INFO(info,8)("estimation of the error needed %.5f seconds\n",
522
523
		    TIME_USED(first,clock()));

524
      adaptInfo->setEstSum(estimator->getErrorSum(), 0);
525
526
527
      adaptInfo->setTimeEstSum(estimator->getTimeEst(), 0);
      adaptInfo->setEstMax(estimator->getErrorMax(), 0);
      adaptInfo->setTimeEstMax(estimator->getTimeEstMax(), 0);
528
529
530
531
532
    } else {
      WARNING("no estimator\n");
    }
  }

533
534
  void ProblemScal::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				      bool assembleMatrix, bool assembleVector)
535
  {
536
    FUNCNAME("ProblemScal::buildAfterCoarsen()");
537
538
539

    clock_t first = clock();

540
#ifndef HAVE_PARALLEL_DOMAIN_AMDIS
541
    mesh->dofCompress();
Thomas Witkowski's avatar
Thomas Witkowski committed
542
#endif
543
544

    MSG("%d DOFs for %s\n", 
545
	feSpace->getAdmin()->getUsedDofs(), feSpace->getName().c_str());
546

547
    rhs->set(0.0);
548

549
550
551
552
553
554
    DOFMatrix::base_matrix_type& base_matrix= systemMatrix->getBaseMatrix(); 

    int nnz_per_row= 0;
    if (num_rows(base_matrix) != 0)
      nnz_per_row= int(double(base_matrix.nnz()) / num_rows(base_matrix) * 1.2); 
    if (nnz_per_row < 5) 
Thomas Witkowski's avatar
Thomas Witkowski committed
555
      nnz_per_row= 5;
556
557
558
559

    // Correct dimensionality of matrix
    base_matrix.change_dim(feSpace->getAdmin()->getUsedSize(), 
			   feSpace->getAdmin()->getUsedSize());
560
    set_to_zero(base_matrix);
561
562
563
564
   
    // Reuse old sparsity information (if available) or default
    systemMatrix->startInsertion(nnz_per_row);

565
    // fill boundary conditions
566
567
568
569
570
571
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->initMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->initVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->initVector(solution);
572

573
    BoundaryType *bound;
574
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
575
576
577
578
579
580
    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);
581

582
    while (elInfo) {
583
      if (useGetBound) {
Thomas Witkowski's avatar
Thomas Witkowski committed
584
	bound = new BoundaryType[feSpace->getBasisFcts()->getNumber()];
Thomas Witkowski's avatar
Thomas Witkowski committed
585
	feSpace->getBasisFcts()->getBound(elInfo, bound);
586
587
588
      } else {
	bound = NULL;
      }
589

590
591
592
      systemMatrix->assemble(1.0, elInfo, bound);
      rhs->assemble(1.0, elInfo, bound);
      
Thomas Witkowski's avatar
Thomas Witkowski committed
593
      if (useGetBound)
Thomas Witkowski's avatar
Thomas Witkowski committed
594
	delete [] bound;
595
      
596
597
598
599
600
601
      if (systemMatrix->getBoundaryManager()) 
	systemMatrix->getBoundaryManager()->fillBoundaryConditions(elInfo, systemMatrix);
      if (rhs->getBoundaryManager())
	rhs->getBoundaryManager()->fillBoundaryConditions(elInfo, rhs);
      if (solution->getBoundaryManager())
	solution->getBoundaryManager()->fillBoundaryConditions(elInfo, solution);
602
603
604
      elInfo = stack.traverseNext(elInfo);
    }

605
    systemMatrix->removeRowsWithDBC(systemMatrix->getApplyDBCs());
606
    systemMatrix->finishInsertion();
607

608
    // TODO: ExitMatrix should be called after finishInsertion!
609
610
611
612
613
614
    if (systemMatrix->getBoundaryManager())
      systemMatrix->getBoundaryManager()->exitMatrix(systemMatrix);
    if (rhs->getBoundaryManager())
      rhs->getBoundaryManager()->exitVector(rhs);
    if (solution->getBoundaryManager())
      solution->getBoundaryManager()->exitVector(solution);
615

616
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", TIME_USED(first,clock()));
617
 
618
    createPrecon();
619
620
  }

621

Thomas Witkowski's avatar
Thomas Witkowski committed
622
  void ProblemScal::writeResidualMesh(AdaptInfo *adaptInfo, std::string name)
623
  {
Thomas Witkowski's avatar
Thomas Witkowski committed
624
    FUNCNAME("ProblemScal::writeResidualMesh()");
625

Thomas Witkowski's avatar
Thomas Witkowski committed
626
    std::map<int, double> vec;    
627
    TraverseStack stack;
Thomas Witkowski's avatar
Thomas Witkowski committed
628
629
    ElInfo *elInfo = stack.traverseFirst(this->getMesh(),  -1, 
					 Mesh::CALL_LEAF_EL | Mesh::FILL_COORDS);
630
631
    
    while (elInfo) {		  
Thomas Witkowski's avatar
Thomas Witkowski committed
632
      vec[elInfo->getElement()->getIndex()] = elInfo->getElement()->getEstimation(0);
633
634
635
      elInfo = stack.traverseNext(elInfo);
    }
    
636
    ElementFileWriter fw(name, this->getMesh(), vec);
637
638
639
    fw.writeFiles(adaptInfo, true);    
  }

640

641
642
  void ProblemScal::createPrecon()
  {
643
    std::string preconType("no");
644
645
646
647
648
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);

    CreatorInterface<ITL_BasePreconditioner> *preconCreator = 
      CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);

649
    solver->setLeftPrecon(preconCreator->create(systemMatrix->getBaseMatrix()));
650
651
652
653
654

    preconType= "no";
    GET_PARAMETER(0, name + "->solver->right precon", &preconType);

    preconCreator = CreatorMap<ITL_BasePreconditioner>::getCreator(preconType);
655
    solver->setRightPrecon(preconCreator->create(systemMatrix->getBaseMatrix()));
656
657
  }

658

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

663
664
    mesh->serialize(out);
    solution->serialize(out);
665
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
  }

}