ProblemVec.cc 36.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "ProblemVec.h"
#include "RecoveryEstimator.h"
#include "Serializer.h"
#include "AbstractFunction.h"
#include "Operator.h"
#include "SystemVector.h"
#include "DOFMatrix.h"
#include "FiniteElemSpace.h"
#include "Estimator.h"
#include "Marker.h"
#include "AdaptInfo.h"
#include "FileWriter.h"
#include "CoarseningManager.h"
#include "RefinementManager.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
15
#include "DualTraverse.h"
16
17
18
19
20
21
22
23
#include "Mesh.h"
#include "OEMSolver.h"
#include "Preconditioner.h"
#include "MatVecMultiplier.h"
#include "DirichletBC.h"
#include "RobinBC.h"
#include "PeriodicBC.h"
#include "Lagrange.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
24
#include "Flag.h"
Thomas Witkowski's avatar
Thomas Witkowski committed
25
#include "TraverseParallel.h"
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

namespace AMDiS {

  ProblemVec *ProblemVec::traversePtr_ = NULL;

  void ProblemVec::initialize(Flag initFlag,
			      ProblemVec *adoptProblem,
			      Flag adoptFlag)
  {
    FUNCNAME("ProblemVec::initialize()");
    
    // === create meshes ===
    if (meshes_.size() != 0) { 
      WARNING("meshes already created\n");
    } else {
      if (initFlag.isSet(CREATE_MESH) || 
	  ((!adoptFlag.isSet(INIT_MESH))&&
	   (initFlag.isSet(INIT_SYSTEM) || initFlag.isSet(INIT_FE_SPACE)))) {
	createMesh();
      } 
      if (adoptProblem && 
	  (adoptFlag.isSet(INIT_MESH) || 
	   adoptFlag.isSet(INIT_SYSTEM) ||
	   adoptFlag.isSet(INIT_FE_SPACE))) {
	meshes_ = adoptProblem->getMeshes();
Thomas Witkowski's avatar
Thomas Witkowski committed
51
	componentMeshes = adoptProblem->componentMeshes;
52
53
54
55
56
57
58
59
60
	refinementManager_ = adoptProblem->refinementManager_;
	coarseningManager_ = adoptProblem->coarseningManager_;
      }
    }

    if (meshes_.size() == 0) 
      WARNING("no mesh created\n");

    // === create fespace ===
61
    if (feSpaces.size() != 0) {
62
63
64
65
66
67
68
69
      WARNING("feSpaces already created\n");
    } else {
      if (initFlag.isSet(INIT_FE_SPACE) || 
	  (initFlag.isSet(INIT_SYSTEM)&&!adoptFlag.isSet(INIT_FE_SPACE))) {
	createFESpace();
      } 
      if (adoptProblem &&
	  (adoptFlag.isSet(INIT_FE_SPACE) || adoptFlag.isSet(INIT_SYSTEM))) {
70
71
	feSpaces = adoptProblem->getFESpaces();
	componentSpaces = adoptProblem->componentSpaces;
72
73
74
      }
    }

75
    if (feSpaces.size() == 0) 
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
      WARNING("no feSpace created\n");

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

    // === create marker ===
    if (initFlag.isSet(INIT_MARKER)) {
      createMarker();
    } 
    if (adoptProblem && adoptFlag.isSet(INIT_MARKER)) {
117
      marker = adoptProblem->getMarker();
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
    } 


    // === create file writer ===
    if (initFlag.isSet(INIT_FILEWRITER)) {
      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;
136
    std::string serializationFilename = "";
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
    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("")) {
      int readSerializationWithAdaptInfo = 0;

      GET_PARAMETER(0, name_ + "->input->read serialization", "%d", 
		    &readSerialization);
      GET_PARAMETER(0, name_ + "->input->serialization with adaptinfo", "%d",
		    &readSerializationWithAdaptInfo);

      // The serialization file is only read, if the adaptInfo part should not be used.
      // If the adaptInfo part should be also read, the serialization file will be read
      // in the constructor of the AdaptInstationary problem, because we do not have here
      // the adaptInfo object.
      if (readSerialization && !readSerializationWithAdaptInfo) {
	GET_PARAMETER(0, name_ + "->input->serialization filename", 
		      &serializationFilename);
	TEST_EXIT(serializationFilename != "")("no serialization file\n");

	MSG("Deserialization from file: %s\n", serializationFilename.c_str());
159
	std::ifstream in(serializationFilename.c_str());
160
161
162
	deserialize(in);
	in.close();
      } else {
163
164
165
166
	int globalRefinements = 0;
	GET_PARAMETER(0, meshes_[0]->getName() + "->global refinements", "%d", 
		      &globalRefinements);

167
168
169
170
171
	// Initialize the meshes if there is no serialization file.
	for (int i = 0; i < static_cast<int>(meshes_.size()); i++) {
	  if (initFlag.isSet(INIT_MESH) && 
	      meshes_[i] && 
	      !(meshes_[i]->isInitialized())) {
172
173
	    meshes_[i]->initialize();	    
	    refinementManager_->globalRefine(meshes_[i], globalRefinements);
174
175
176
177
178
179
180
181
182
183
184
185
	  }
	}	
      }
    }

    doOtherStuff();
  }

  void ProblemVec::createMesh() 
  {
    FUNCNAME("ProblemVec::createMesh()");

Thomas Witkowski's avatar
Thomas Witkowski committed
186
    componentMeshes.resize(nComponents);
187
    std::map<int, Mesh*> meshForRefinementSet;
188
189
    char number[3];

190
    std::string meshName("");
191
    GET_PARAMETER(0, name_ + "->mesh", &meshName);
192
    TEST_EXIT(meshName != "")("no mesh name specified\n");
193
194
    int dim = 0;
    GET_PARAMETER(0, name_ + "->dim", "%d", &dim);
195
    TEST_EXIT(dim)("no problem dimension specified!\n");
196

197
    for (int i = 0; i < nComponents; i++) {
198
      sprintf(number, "%d", i);
199
200
201
      int refSet = -1;
      GET_PARAMETER(0, name_ + "->refinement set[" + number + "]", "%d", &refSet);
      if (refSet < 0) {
202
203
	refSet = 0;
      }
204
      if (meshForRefinementSet[refSet] == NULL) {
205
206
207
208
	Mesh *newMesh = NEW Mesh(meshName, dim);
	meshForRefinementSet[refSet] = newMesh;
	meshes_.push_back(newMesh);
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
209
      componentMeshes[i] = meshForRefinementSet[refSet];
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    }
    switch(dim) {
    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");
    }
  }

  void ProblemVec::createFESpace()
  {
    FUNCNAME("ProblemVec::createFESpace()");

    int degree = 1;
    char number[3];

236
    std::map< std::pair<Mesh*, int>, FiniteElemSpace*> feSpaceMap;
237
    int dim = -1;
238
    GET_PARAMETER(0, name_ + "->dim", "%d", &dim);
239
    TEST_EXIT(dim != -1)("no problem dimension specified!\n");
240

241
    componentSpaces.resize(nComponents, NULL);
242

243
    for (int i = 0; i < nComponents; i++) {
244
245
246
      sprintf(number, "%d", i);
      GET_PARAMETER(0, name_ + "->polynomial degree[" + number + "]","%d", &degree);

247
      TEST_EXIT(componentSpaces[i] == NULL)("feSpace already created\n");
248

Thomas Witkowski's avatar
Thomas Witkowski committed
249
      if (feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] == NULL) {
250
251
252
	FiniteElemSpace *newFESpace = 
	  FiniteElemSpace::provideFESpace(NULL,
					  Lagrange::getLagrange(dim, degree),
Thomas Witkowski's avatar
Thomas Witkowski committed
253
					  componentMeshes[i],
254
					  name_ + "->feSpace");
Thomas Witkowski's avatar
Thomas Witkowski committed
255
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)] = newFESpace;
256
	feSpaces.push_back(newFESpace);
257
      }
258
      componentSpaces[i] = 
Thomas Witkowski's avatar
Thomas Witkowski committed
259
	feSpaceMap[std::pair<Mesh*, int>(componentMeshes[i], degree)];
260
261
262
    }

    // create dof admin for vertex dofs if neccessary
263
    for (int i = 0; i < static_cast<int>(meshes_.size()); i++) {
264
265
266
267
268
269
270
271
272
273
274
275
276
277
      if (meshes_[i]->getNumberOfDOFs(VERTEX) == 0) {
	DimVec<int> ln_dof(meshes_[i]->getDim(), DEFAULT_VALUE, 0);
	ln_dof[VERTEX]= 1;
	meshes_[i]->createDOFAdmin("vertex dofs", ln_dof);      
      }
    }
  }

  void ProblemVec::createMatricesAndVectors()
  {
    FUNCNAME("ProblemVec::createMatricesAndVectors()");

    // === create vectors and system matrix ===

278
    systemMatrix_ = NEW Matrix<DOFMatrix*>(nComponents, nComponents);
279
    systemMatrix_->set(NULL);
280
281
    rhs_ = NEW SystemVector("rhs", componentSpaces, nComponents);
    solution_ = NEW SystemVector("solution", componentSpaces, nComponents);
282
283

    char number[10];
284
    std::string numberedName;
Thomas Witkowski's avatar
Thomas Witkowski committed
285
    for (int i = 0; i < nComponents; i++) {
286
287
      (*systemMatrix_)[i][i] = NEW DOFMatrix(componentSpaces[i], 
					     componentSpaces[i], "A_ii");
288
289
      (*systemMatrix_)[i][i]->setCoupleMatrix(false);
      sprintf(number, "[%d]", i);
290
      numberedName = "rhs" + std::string(number);
291
      rhs_->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], numberedName));
292
      numberedName = name_ + std::string(number);
293
      solution_->setDOFVector(i, NEW DOFVector<double>(componentSpaces[i], 
294
295
296
297
298
299
300
301
302
303
304
305
306
307
						       numberedName));
      solution_->getDOFVector(i)->setCoarsenOperation(COARSE_INTERPOL);
      solution_->getDOFVector(i)->set(0.0);
    }

    // === create matVec ===
    matVec_ = NEW StandardMatVec<Matrix<DOFMatrix*>, SystemVector>(systemMatrix_);
  }

  void ProblemVec::createSolver()
  {
    FUNCNAME("ProblemVec::createSolver()");

    // === create solver ===
308
    std::string solverType("no");
309
310
311
312
313
314
315
316
317
318
319
320
    GET_PARAMETER(0, name_ + "->solver", &solverType);
    OEMSolverCreator<SystemVector> *solverCreator = 
      dynamic_cast<OEMSolverCreator<SystemVector>*>(
						    CreatorMap<OEMSolver<SystemVector> >
						    ::getCreator(solverType)
						    );
    TEST_EXIT(solverCreator)("no solver type\n");
    solverCreator->setName(name_ + "->solver");
    solver_ = solverCreator->create();
    solver_->initParameters();

    // === create preconditioners ===
321
    std::string preconType("no");
322
323

    PreconditionerScal *scalPrecon;
324
    PreconditionerVec *vecPrecon = NEW PreconditionerVec(nComponents);
325
326
327
328
329
330
331
332
333
334
335

    GET_PARAMETER(0, name_ + "->solver->left precon", &preconType);
    CreatorInterface<PreconditionerScal> *preconCreator =
      CreatorMap<PreconditionerScal>::getCreator(preconType);

    int i, j;

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

336
      for(i = 0; i < nComponents; i++) {
337
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
338
	  setSizeAndRow(nComponents, i);
339
340
    
	scalPrecon = preconCreator->create();
341
	for(j = 0; j < nComponents; j++) {
342
343
344
345
346
347
348
349
	  scalPrecon->setMatrix(&(*systemMatrix_)[i][j], j);
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
      leftPrecon_ = vecPrecon;
    }


350
    vecPrecon = NEW PreconditionerVec(nComponents);
351
352
353
354
355
356
357
358
359
360

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

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


361
      for(i = 0; i < nComponents; i++) {
362
	dynamic_cast<PreconditionerScalCreator*>(preconCreator)->
363
	  setSizeAndRow(nComponents, i);
364
365
    
	scalPrecon = preconCreator->create();
366
	for(j = 0; j < nComponents; j++) {
367
368
369
370
371
372
373
374
375
376
	  scalPrecon->setMatrix(&(*systemMatrix_)[i][j], j);
	}
	vecPrecon->setScalarPrecon(i, scalPrecon);
      }
      rightPrecon_ = vecPrecon;
    }


    // === create vector creator ===
    solver_->setVectorCreator(NEW SystemVector::Creator("temp",
377
							componentSpaces, 
378
							nComponents));
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
  }

  void ProblemVec::createEstimator()
  {
    FUNCNAME("ProblemVec::createEstimator()");

    int i, j;

    // create and set leaf data prototype
    for(i = 0; i < static_cast<int>(meshes_.size()); i++) {
      meshes_[i]->setElementDataPrototype
	(NEW LeafDataEstimatableVec(NEW LeafDataCoarsenableVec));
    }  

    char number[3];
394
    std::string estName;
395

396
    for(i = 0; i < nComponents; i++) {
397
398
      TEST_EXIT(estimator_[i] == NULL)("estimator already created\n");
      sprintf(number, "%d", i);
399
      estName = name_ + "->estimator[" + std::string(number) + "]";
400
401

      // === create estimator ===
402
      std::string estimatorType("no");
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
      GET_PARAMETER(0, estName, &estimatorType);
      EstimatorCreator *estimatorCreator = 
	dynamic_cast<EstimatorCreator*>(
					CreatorMap<Estimator>::getCreator(estimatorType));
      if(estimatorCreator) {
	estimatorCreator->setName(estName);
	estimatorCreator->setRow(i);
	if(estimatorType == "recovery") {
	  dynamic_cast<RecoveryEstimator::Creator*>(estimatorCreator)->
	    setSolution(solution_->getDOFVector(i));
	}
	estimator_[i] = estimatorCreator->create();
      }


      if(estimator_[i]) {
419
	for(j=0; j < nComponents; j++) {
420
421
422
423
424
425
426
427
428
429
430
431
	  estimator_[i]->addSystem((*systemMatrix_)[i][j], 
				   solution_->getDOFVector(j), 
				   rhs_->getDOFVector(j));
	}
      }
    }
  }

  void ProblemVec::createMarker()
  {
    FUNCNAME("ProblemVec::createMarker()");

432
    std::string numberedName;
433
434
    char number[10];
    int numMarkersCreated = 0;
435

436
    for (int i = 0; i < nComponents; i++) {
437
      sprintf(number, "[%d]", i);
438
      numberedName = name_ + "->marker" + std::string(number);
439
440
      marker[i] = Marker::createMarker(numberedName, i);
      if (marker[i]) {
441
442
	numMarkersCreated++;
	if (numMarkersCreated > 1)
443
	  marker[i]->setMaximumMarking(true);
444
445
446
447
448
449
450
451
452
453
      }
    }
  }

  void ProblemVec::createFileWriter()
  {
    FUNCNAME("ProblemVec::createFileWriter()");
  

    // Create one filewriter for all components of the problem
454
455
    std::string numberedName  = name_ + "->output";
    std::string filename = "";
456
457
458
    GET_PARAMETER(0, numberedName + "->filename", &filename);

    if (filename != "") {
459
      std::vector< DOFVector<double>* > solutionList(nComponents);
460

461
      for (int i = 0; i < nComponents; i++) {
Thomas Witkowski's avatar
Thomas Witkowski committed
462
	TEST_EXIT(componentMeshes[0] == componentMeshes[i])
463
464
465
466
467
468
	  ("All Meshes have to be equal to write a vector file.\n");

	solutionList[i] = solution_->getDOFVector(i);
      }

      fileWriters_.push_back(NEW FileWriter(numberedName,
Thomas Witkowski's avatar
Thomas Witkowski committed
469
					    componentMeshes[0],
470
471
472
473
474
475
					    solutionList));
    }


    // Create own filewriters for each components of the problem
    char number[10];
476
    for (int i = 0; i < nComponents; i++) {
477
      sprintf(number, "[%d]", i);
478
      numberedName  = name_ + "->output" + std::string(number);
479
480
481
482
483
      filename = "";
      GET_PARAMETER(0, numberedName + "->filename", &filename);

      if (filename != "") {
	fileWriters_.push_back(NEW FileWriter(numberedName, 
Thomas Witkowski's avatar
Thomas Witkowski committed
484
					      componentMeshes[i], 
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
					      solution_->getDOFVector(i)));
      }
    }


    // Check for serializer
    int writeSerialization = 0;
    GET_PARAMETER(0, name_ + "->write serialization", "%d", &writeSerialization);
    if (writeSerialization) {
      MSG("Use are using the obsolete parameter: %s->write serialization\n", name_.c_str());
      MSG("Please use instead the following parameter: %s->output->write serialization\n", name_.c_str());
      ERROR_EXIT("Usage of an obsolete parameter (see message above)!\n");
    }

    GET_PARAMETER(0, name_ + "->output->write serialization", "%d", &writeSerialization);
    if (writeSerialization) {
      fileWriters_.push_back(NEW Serializer<ProblemVec>(this));
    }
  }

  void ProblemVec::doOtherStuff()
  {
  }

509
  void ProblemVec::solve(AdaptInfo *adaptInfo, bool fixedMatrix)
510
511
512
513
514
515
516
517
518
519
520
521
522
  {
    FUNCNAME("Problem::solve()");

    if (!solver_) {
      WARNING("no solver\n");
      return;
    }

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

    clock_t first = clock();
523
524
    int iter = solver_->solve(matVec_, solution_, rhs_, 
			      leftPrecon_, rightPrecon_, fixedMatrix);
525
    
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#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 ProblemVec::estimate(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::estimate()");

    clock_t first = clock();

548
549
550
551
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

552
    if (computeExactError) {
Thomas Witkowski's avatar
Thomas Witkowski committed
553
      computeError(adaptInfo);
554
555
556
557
558
559
560
561
562
563
564
565
566
    } else {
      for (int i = 0; i < nComponents; i++) {
	Estimator *scalEstimator = estimator_[i];
	
	if (scalEstimator) {
	  scalEstimator->estimate(adaptInfo->getTimestep());
	  adaptInfo->setEstSum(scalEstimator->getErrorSum(), i);
	  adaptInfo->setEstMax(scalEstimator->getErrorMax(), i);
	  adaptInfo->setTimeEstSum(scalEstimator->getTimeEst(), i);
	  adaptInfo->setTimeEstMax(scalEstimator->getTimeEstMax(), i);
	} else {
	  WARNING("no estimator for component %d\n" , i);
	}
567
568
569
      }
    }

570
571
572
573
574
575
576
577
578
579
#ifdef _OPENMP
    INFO(info_, 8)("estimation of the error needed %.5f seconds system time / %.5f seconds wallclock time\n",
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
    INFO(info_, 8)("estimation of the error needed %.5f seconds\n",
		   TIME_USED(first, clock()));

#endif

580
581
582
583
584
585
586
587
588
589
590
  }

  Flag ProblemVec::markElements(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::markElements()");

    // to enforce albert-like behavior: refinement even if space tolerance
    // here is reached already because of time adaption
    allowFirstRefinement();

    Flag markFlag = 0;
591
    for (int i = 0; i < nComponents; i++) {
592
      if (marker[i]) {
Thomas Witkowski's avatar
Thomas Witkowski committed
593
	markFlag |= marker[i]->markMesh(adaptInfo, componentMeshes[i]);
594
595
596
597
      } else {
	WARNING("no marker for component %d\n", i);
      }
    }
598
    
599
600
601
602
603
604
605
    return markFlag;
  }

  Flag ProblemVec::refineMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::refineMesh()");

606
    int nMeshes = static_cast<int>(meshes_.size());
607
    Flag refineFlag = 0;
608
    for (int i = 0; i < nMeshes; i++) {
609
610
611
612
613
614
615
616
617
      refineFlag |= refinementManager_->refineMesh(meshes_[i]);
    }
    return refineFlag;
  }

  Flag ProblemVec::coarsenMesh(AdaptInfo *adaptInfo) 
  {
    FUNCNAME("ProblemVec::coarsenMesh()");

618
    int nMeshes = static_cast<int>(meshes_.size());
619
    Flag coarsenFlag = 0;
620
621
    for (int i = 0; i < nMeshes; i++) {
      if (adaptInfo->isCoarseningAllowed(i)) {
622
623
624
625
626
627
628
629
630
631
632
633
634
	coarsenFlag |= coarseningManager_->coarsenMesh(meshes_[i]);

	WARNING("coarsening for component %d no allowed\n", i);
      }
    }
    return coarsenFlag;
  }

  Flag ProblemVec::oneIteration(AdaptInfo *adaptInfo, Flag toDo)
  {
    FUNCNAME("ProblemVec::oneIteration()");

    if (allowFirstRef_) {
635
      for (int i = 0; i < nComponents; i++) {
636
637
638
639
	adaptInfo->allowRefinement(true, i);
      }
      allowFirstRef_ = false;
    } else {
640
      for (int i = 0; i < nComponents; i++) {
641
642
643
644
645
646
647
648
649
650
651
	if (adaptInfo->spaceToleranceReached(i)) {
	  adaptInfo->allowRefinement(false, i);
	} else {
	  adaptInfo->allowRefinement(true, i);	
	}
      }
    }

    return StandardProblemIteration::oneIteration(adaptInfo, toDo);
  }

652
653
  void ProblemVec::buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag,
				     bool asmMatrix, bool asmVector)
654
655
656
657
658
  {
    FUNCNAME("ProblemVec::buildAfterCoarsen()");

    clock_t first = clock();

659
660
661
662
#ifdef _OPENMP
    double wtime = omp_get_wtime();
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
663
    for (int i = 0; i < static_cast<int>(meshes_.size()); i++) {
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
      meshes_[i]->dofCompress();
    }

    Flag assembleFlag = 
      flag | 
      (*systemMatrix_)[0][0]->getAssembleFlag() | 
      rhs_->getDOFVector(0)->getAssembleFlag()   |
      Mesh::CALL_LEAF_EL                        | 
      Mesh::FILL_COORDS                         |
      Mesh::FILL_DET                            |
      Mesh::FILL_GRD_LAMBDA |
      Mesh::FILL_NEIGH;

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

Thomas Witkowski's avatar
Thomas Witkowski committed
681

682
    for (int i = 0; i < nComponents; i++) {
683
      MSG("%d DOFs for %s\n", 
684
685
	  componentSpaces[i]->getAdmin()->getUsedSize(), 
	  componentSpaces[i]->getName().c_str());
686
687

      rhs_->getDOFVector(i)->set(0.0);
688
      for (int j = 0; j < nComponents; j++) {
689
690
691
	if ((*systemMatrix_)[i][j]) {
	  // The matrix should not be deleted, if it was assembled before
	  // and it is marked to be assembled only once.
692
	  if (!(assembleMatrixOnlyOnce_[i][j] && assembledMatrix_[i][j]) && asmMatrix) {
693
694
695
	    (*systemMatrix_)[i][j]->clear();
	  }
	}
696
697
698
      }
    }

Thomas Witkowski's avatar
Thomas Witkowski committed
699
    for (int i = 0; i < nComponents; i++) {
700
      for (int j = 0; j < nComponents; j++) {
701
702
703
704
	// Only if this variable is true, the current matrix will be assembled.	
	bool assembleMatrix = true;
	// The DOFMatrix which should be assembled (or not, if assembleMatrix
	// will be set to false).
705
706
	DOFMatrix *matrix = (*systemMatrix_)[i][j];

707
708
709
710
711
712
713
714
715
716
	// If the matrix was assembled before and it is marked to be assembled
	// only once, it will not be assembled.
	if (assembleMatrixOnlyOnce_[i][j] && assembledMatrix_[i][j]) {
	  assembleMatrix = false;
	}
	// If there is no DOFMatrix (e.g. if it is completly 0), do not assemble.
	if (!matrix) {
	  assembleMatrix = false;
	}

717
718
719
720
	if (!asmMatrix) {
	  assembleMatrix = false;
	}

721
722
723
724
725
726
727
	// If the matrix should not be assembled, the rhs vector has to be considered.
	// This will be only done, if i == j. So, if both is not true, we can jump
	// to the next matrix.
	if (!assembleMatrix && i != j) {
	  continue;
	}

728
729
730
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->initMatrix(matrix);

Thomas Witkowski's avatar
Thomas Witkowski committed
731
732
733
734
	if (componentSpaces[i] == componentSpaces[j]) {
	  assembleOnOneMesh(componentSpaces[i],
			    assembleFlag,
			    assembleMatrix ? matrix : NULL,
735
			    ((i == j) && asmVector) ? rhs_->getDOFVector(i) : NULL);
Thomas Witkowski's avatar
Thomas Witkowski committed
736
737
738
739
	} else {
	  assembleOnDifMeshes(componentSpaces[i], componentSpaces[j],
			      assembleFlag,
			      assembleMatrix ? matrix : NULL,
740
			      ((i == j) && asmVector) ? rhs_->getDOFVector(i) : NULL);	  
741
742
743
744
	  TEST_EXIT(matrix->getUsedSize() == componentSpaces[i]->getAdmin()->getUsedSize())
	    ("Assembled matrix has wrong dimension!\n");
	  TEST_EXIT(matrix->getNumCols() == componentSpaces[j]->getAdmin()->getUsedSize())
	    ("Assembled matrix has wrong dimension!\n");
745
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
746

747
748
749
	if (assembleMatrix && matrix->getBoundaryManager())
	  matrix->getBoundaryManager()->exitMatrix(matrix);	  
	
750
	assembledMatrix_[i][j] = true;
751
752
      }

753
754
755
756
757
      // And now assemble boundary conditions on the vectors
      assembleBoundaryConditions(rhs_->getDOFVector(i),
				 solution_->getDOFVector(i),
				 componentMeshes[i],
				 assembleFlag);
758
    }
759
760
761
762
763
764

#ifdef _OPENMP
    INFO(info_, 8)("buildAfterCoarsen needed %.5f seconds system time / %.5f seconds wallclock time\n",
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
765
    INFO(info_, 8)("buildAfterCoarsen needed %.5f seconds\n",
Thomas Witkowski's avatar
Thomas Witkowski committed
766
		   TIME_USED(first, clock()));
767
#endif
768
769
770
771
772
773
  }

  void ProblemVec::writeFiles(AdaptInfo *adaptInfo, bool force) 
  {
    FUNCNAME("ProblemVec::writeFiles()");

774
775
776
777
778
779
780
781
782
783
784
785
786
    clock_t first = clock();

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

    int i;
    int size = static_cast<int>(fileWriters_.size());
#ifdef _OPENMP
#pragma omp parallel for schedule(static, 1)
#endif
    for (i = 0; i < size; i++) {
      fileWriters_[i]->writeFiles(adaptInfo, force);
787
    }
788
789
790
791
792
793
794
795
796
    
#ifdef _OPENMP
    INFO(info_, 8)("writeFiles needed %.5f seconds system time / %.5f seconds wallclock time\n",
		   TIME_USED(first, clock()),
		   omp_get_wtime() - wtime);
#else
    INFO(info_, 8)("writeFiles needed %.5f seconds\n",
		   TIME_USED(first, clock()));
#endif
797
798
  }

799
  void ProblemVec::interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct) 
800
801
802
803
804
805
806
807
808
809
810
811
812
  {
    FUNCNAME("ProblemVec::interpolInitialSolution()");

    solution_->interpol(fct);
  }

  void ProblemVec::addMatrixOperator(Operator *op, 
				     int i, int j,
				     double *factor,
				     double *estFactor)
  {
    FUNCNAME("ProblemVec::addMatrixOperator()");

813
    if (!(*systemMatrix_)[i][j]) {
814
      TEST_EXIT(i != j)("should have been created already\n");
815
816
      (*systemMatrix_)[i][j] = NEW DOFMatrix(componentSpaces[i],
					     componentSpaces[j],
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
					     "");
      (*systemMatrix_)[i][j]->setCoupleMatrix(true);

      (*systemMatrix_)[i][j]->getBoundaryManager()->
	setBoundaryConditionMap((*systemMatrix_)[i][i]->getBoundaryManager()->
				getBoundaryConditionMap());
    }    
    (*systemMatrix_)[i][j]->addOperator(op, factor, estFactor);
  }

  void ProblemVec::addVectorOperator(Operator *op, int i,
				     double *factor,
				     double *estFactor)
  {
    FUNCNAME("ProblemVec::addVectorOperator()");

    rhs_->getDOFVector(i)->addOperator(op, factor, estFactor);
  }

  void ProblemVec::addDirichletBC(BoundaryType type, int system,
				  AbstractFunction<double, WorldVector<double> >* b)
  {
    FUNCNAME("ProblemVec::addDirichletBC()");

    DirichletBC *dirichlet = new DirichletBC(type, 
					     b, 
843
					     componentSpaces[system]);
844
    for (int i = 0; i < nComponents; i++) {
845
846
847
848
      if (systemMatrix_ && (*systemMatrix_)[system][i]) {
	(*systemMatrix_)[system][i]->getBoundaryManager()->addBoundaryCondition(dirichlet);
      }
    }
849

850
851
    if (rhs_)
      rhs_->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);
852

853
854
855
856
857
858
859
860
861
862
863
    if (solution_)
      solution_->getDOFVector(system)->getBoundaryManager()->addBoundaryCondition(dirichlet);
  }

  void ProblemVec::addNeumannBC(BoundaryType type, int row, int col, 
				AbstractFunction<double, WorldVector<double> > *n)
  {
    FUNCNAME("ProblemVec::addNeumannBC()");

    NeumannBC *neumann = 
      new NeumannBC(type, n, 
864
865
		    componentSpaces[row], 
		    componentSpaces[col]);
866
    if (rhs_)
867
868
869
870
871
872
873
874
875
876
877
      rhs_->getDOFVector(row)->getBoundaryManager()->addBoundaryCondition(neumann);
  }

  void ProblemVec::addRobinBC(BoundaryType type, int row, int col, 
			      AbstractFunction<double, WorldVector<double> > *n,
			      AbstractFunction<double, WorldVector<double> > *r)
  {
    FUNCNAME("ProblemVec::addRobinBC()");

    RobinBC *robin = 
      new RobinBC(type, n, r, 
878
879
		  componentSpaces[row], 
		  componentSpaces[col]);
880
    if (rhs_)
881
      rhs_->getDOFVector(row)->getBoundaryManager()->addBoundaryCondition(robin);
882
883

    if (systemMatrix_ && (*systemMatrix_)[row][col]) {
884
885
886
887
888
889
890
891
      (*systemMatrix_)[row][col]->getBoundaryManager()->addBoundaryCondition(robin);
    }
  }

  void ProblemVec::addPeriodicBC(BoundaryType type, int row, int col) 
  {
    FUNCNAME("ProblemVec::addPeriodicBC()");

892
    FiniteElemSpace *feSpace = componentSpaces[row];
893
894
895

    PeriodicBC *periodic = new PeriodicBC(type, feSpace);

896
    if (systemMatrix_ && (*systemMatrix_)[row][col]) 
897
      (*systemMatrix_)[row][col]->getBoundaryManager()->addBoundaryCondition(periodic);
898
899

    if (rhs_) 
900
901
902
903
      rhs_->getDOFVector(row)->getBoundaryManager()->
	addBoundaryCondition(periodic);
  }

Thomas Witkowski's avatar
Thomas Witkowski committed
904
905
906
907
908
  void ProblemVec::assembleOnOneMesh(FiniteElemSpace *feSpace, Flag assembleFlag,
				     DOFMatrix *matrix, DOFVector<double> *vector)
  {
    Mesh *mesh = feSpace->getMesh();
    const BasisFunction *basisFcts = feSpace->getBasisFcts();
909
910

#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
911
    TraverseParallelStack stack;
912
913
914
#else
    TraverseStack stack;
#endif   
Thomas Witkowski's avatar
Thomas Witkowski committed
915

916
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
917
#pragma omp parallel
918
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
919
920
921
922
923
924
925
    {
      BoundaryType *bound = useGetBound_ ? GET_MEMORY(BoundaryType, basisFcts->getNumber()) : NULL;

      // Create for every thread its private matrix and vector, on that
      // the thread will assemble its part of the mesh.
      DOFMatrix *tmpMatrix = NULL;
      DOFVector<double> *tmpVector = NULL; 
Thomas Witkowski's avatar
Thomas Witkowski committed
926
927

      if (matrix) {
Thomas Witkowski's avatar
Thomas Witkowski committed
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
	tmpMatrix = NEW DOFMatrix(matrix->getRowFESpace(),
				  matrix->getColFESpace(),
				  "tmp");

	// Copy the global matrix to the private matrix, because we need the
	// operators defined on the global matrix in the private one. Only the
	// values have to be set to zero.
	*tmpMatrix = *matrix;
	tmpMatrix->clear();
      }

      if (vector) {
	tmpVector = NEW DOFVector<double>(vector->getFESpace(), "tmp");

	// Copy the global vector to the private vector, because we need the
	// operatirs defined on the global vector in the private one. But set
	// the values to zero of the private vector after copying.
	*tmpVector = *vector;
	tmpVector->set(0.0);
      }

949
950
951
952
      // Because we are using the parallel traverse stack, each thread will
      // traverse only a part of the mesh.
      ElInfo *elInfo = stack.traverseFirst(mesh, -1, assembleFlag);

953
954
955
956
957
958
959
      // After creating privat copies of the DOFMatrix and the DOFVector, all threads
      // have to wait at this barrier. Especially for small problems this is required,
      // because otherwise one thread may be finished with assembling, before another
      // has made his private copy.
#ifdef _OPENMP
#pragma omp barrier
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
      while (elInfo) {
	if (useGetBound_) {
	  basisFcts->getBound(elInfo, bound);
	}
	
	if (matrix) {
	  tmpMatrix->assemble(1.0, elInfo, bound);
	  
	  // Take the matrix boundary manager from the public matrix,
	  // but assemble the boundary conditions on the thread private matrix.
	  if (matrix->getBoundaryManager()) {
	    matrix->getBoundaryManager()->
	      fillBoundaryConditions(elInfo, tmpMatrix);
	  }		      
	}
	
	if (vector) {
	  tmpVector->assemble(1.0, elInfo, bound);
	}
Thomas Witkowski's avatar
Thomas Witkowski committed
979
	
Thomas Witkowski's avatar
Thomas Witkowski committed
980
	elInfo = stack.traverseNext(elInfo);
Thomas Witkowski's avatar
Thomas Witkowski committed
981
      }
Thomas Witkowski's avatar
Thomas Witkowski committed
982
983
984
985
986
987
988

      // After mesh traverse, all thread have to added their private matrices and
      // vectors to the global public matrix and public vector. Therefore, this is 
      // a critical section, which is allowed to be executed by on thread only at 
      // the same time.

      if (matrix) {
989
#ifdef _OPENMP
Thomas Witkowski's avatar
Thomas Witkowski committed
990
#pragma omp critical
991
#endif
Thomas Witkowski's avatar
Thomas Witkowski committed
992
993
994
995
996
997
998
999
1000
	{
	  addDOFMatrix(matrix, tmpMatrix);

	  // Remove rows corresponding to DOFs on a Dirichlet boundary.
	  matrix->removeRowsWithDBC(tmpMatrix->getApplyDBCs());
	}

	DELETE tmpMatrix;
      }
For faster browsing, not all history is shown. View entire blame