ProblemScal.cc 19.7 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
#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"
#include "FileWriter.h"
#include "RefinementManager.h"
#include "CoarseningManager.h"
#include "Lagrange.h"
#include "PeriodicBC.h"
#include "ValueReader.h"
24
#include "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
  }

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

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

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

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

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

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

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

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

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

206
207
208
209
210
    return 0;
  }

  Flag ProblemScal::refineMesh(AdaptInfo *adaptInfo) 
  { 
211
    return refinementManager->refineMesh(mesh); 
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

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

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

    debug::writeMatlabMatrix(*systemMatrix, "ac.dat");
    exit(0);
240
241
242

    SolverMatrix<DOFMatrix> solverMatrix;
    solverMatrix.setMatrix(*systemMatrix);
243
    solver->solveSystem(solverMatrix, *solution, *rhs);
244
245

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

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

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

    // === create mesh ===
267
    if (mesh) { 
268
269
      WARNING("mesh already created\n");
    } else {
Thomas Witkowski's avatar
Thomas Witkowski committed
270
271
272
      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
273
274
275
	createMesh();
      } 
      
Thomas Witkowski's avatar
Thomas Witkowski committed
276
277
278
279
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_MESH) || 
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
280
281
	TEST_EXIT(!mesh)("mesh already created\n");
	mesh = adoptProblem->getMesh();
282
283
	refinementManager = adoptProblem->refinementManager;
	coarseningManager = adoptProblem->coarseningManager;
Thomas Witkowski's avatar
Thomas Witkowski committed
284
      }
285
286
    }

287
    if (!mesh) 
288
      WARNING("no mesh created\n");
289
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
297
	createFESpace();
      } 
298

299
300
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
Thomas Witkowski's avatar
Thomas Witkowski committed
301
302
	TEST_EXIT(!feSpace)("feSpace already created");
	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
318
319
      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();
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
414
				 mesh,
				 solution,
				 mesh->getMacroFileInfo());
	  mesh->clearMacroFileInfo();
415
	}
416
	
417
	// === do global refinements ===
418
419
	if (initFlag.isSet(INIT_GLOBAL_REFINES)) {
	  int globalRefinements = 0;
420
421

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

425
	  refinementManager->globalRefine(mesh, globalRefinements);	
426
	}
427
428
429
430
431
432
433
434
      }
    }
  }

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

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

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

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

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

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

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

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

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

  void ProblemScal::createFileWriter()
  {
503
    fileWriters.push_back(new FileWriter(name + "->output", mesh, solution));
504
    int writeSerialization = 0;
505
506
507
508
509
    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
510
511
    if (writeSerialization)
      fileWriters.push_back(new Serializer<ProblemScal>(this));
512
#endif
513
514
515
516
517
518
  }

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

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

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

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

    clock_t first = clock();

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

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

548
    rhs->set(0.0);
549

550
551
552
553
554
555
    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
556
      nnz_per_row= 5;
557
558
559
560

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

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

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

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

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

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

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

617
    INFO(info, 8)("buildAfterCoarsen needed %.5f seconds\n", TIME_USED(first,clock()));
618
 
619
    createPrecon();
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);
    }
    
Thomas Witkowski's avatar
Thomas Witkowski committed
636
    ElementFileWriter fw(name, this->getFESpace(), vec);
637
638
639
    fw.writeFiles(adaptInfo, true);    
  }

640
641
  void ProblemScal::createPrecon()
  {
642
    std::string preconType("no");
643
644
645
646
647
648
649
650
651
652
653
654
655
656
    GET_PARAMETER(0, name + "->solver->left precon", &preconType);

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

    solver->setLeftPrecon( preconCreator->create(systemMatrix->getBaseMatrix()) );

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

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

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

661
662
    mesh->serialize(out);
    solution->serialize(out);
663
664
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
665
  void ProblemScal::deserialize(std::istream &in) 
666
667
668
  {
    FUNCNAME("ProblemScal::deserialize()");

669
670
    mesh->deserialize(in);    
    solution->deserialize(in);
671
672
673
  }

}