ProblemVec.h 17.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
26
27
28
29
// ============================================================================
// ==                                                                        ==
// == AMDiS - Adaptive multidimensional simulations                          ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  crystal growth group                                                  ==
// ==                                                                        ==
// ==  Stiftung caesar                                                       ==
// ==  Ludwig-Erhard-Allee 2                                                 ==
// ==  53175 Bonn                                                            ==
// ==  germany                                                               ==
// ==                                                                        ==
// ============================================================================
// ==                                                                        ==
// ==  http://www.caesar.de/cg/AMDiS                                         ==
// ==                                                                        ==
// ============================================================================

/** \file ProblemVec.h */

#ifndef AMDIS_PROBLEMVEC_H
#define AMDIS_PROBLEMVEC_H

#include "ProblemStatBase.h"
#include "Parameters.h"
#include "Boundary.h"
#include "MatrixVector.h"
#include "StandardProblemIteration.h"
30
#include "ElementFileWriter.h"
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
61
62
63
#include <vector>
#include <list>

namespace AMDiS {

  template<typename t1, typename t2> class AbstractFunction;
  class Operator;
  class SystemVector;
  class DOFMatrix;
  class FiniteElemSpace;
  class Estimator;
  class Marker;
  class AdaptStationary;
  class AdaptInfo;
  class FileWriterInterface;
  class CoarseningManager;
  class RefinementManager;
  class Mesh;
  template<typename T> class OEMSolver;
  template<typename T> class Preconditioner;
  template<typename T> class MatVecMultiplier;

  class ProblemVec : public ProblemStatBase,
		     public StandardProblemIteration
  {
  public:
    /** \brief
     * constructor
     */
    ProblemVec(const char* name,
	       ProblemIterationInterface *problemIteration = NULL)
      : StandardProblemIteration(this),
	name_(name),
64
	nComponents(-1),
65
66
67
68
69
70
71
72
73
	solver_(NULL),
	solution_(NULL),
	rhs_(NULL),
	systemMatrix_(NULL),
	matVec_(NULL),
	leftPrecon_(NULL),
	rightPrecon_(NULL),
	useGetBound_(true),
	info_(10),
74
75
	allowFirstRef_(false),
	computeExactError(false)
76
    {
77
78
79
80
      GET_PARAMETER(0, name_ + "->components", "%d", &nComponents);
      TEST_EXIT(nComponents > 0)("components not set!\n");    

      estimator_.resize(nComponents, NULL);
81
      marker.resize(nComponents, NULL);
82
83
84
85
86
87
88

      assembleMatrixOnlyOnce_.resize(nComponents);
      assembledMatrix_.resize(nComponents);
      for (int i = 0; i < nComponents; i++) {
	assembleMatrixOnlyOnce_[i].resize(nComponents);
	assembledMatrix_[i].resize(nComponents);
	for (int j = 0; j < nComponents; j++) {
89
90
91
92
	  assembleMatrixOnlyOnce_[i][j] = false;
	  assembledMatrix_[i][j] = false;
	}
      }
93
94

      exactSolutionFcts.resize(nComponents);
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
    };

    /** \brief
     * destructor
     */
    virtual ~ProblemVec() {};

    /** \brief
     * Initialisation of the problem.
     */
    virtual void initialize(Flag initFlag,
			    ProblemVec *adoptProblem = NULL,
			    Flag adoptFlag = INIT_NOTHING);


    /** \brief
     * Used in \ref initialize().
     */
    virtual void createMesh();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createFESpace();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createMatricesAndVectors();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createSolver();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createEstimator();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createMarker();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void createFileWriter();

    /** \brief
     * Used in \ref initialize().
     */
    virtual void doOtherStuff();

    /** \brief
     * Implementation of ProblemStatBase::solve(). Deligates the solving
     * of problems system to \ref solver.
     */
    virtual void solve(AdaptInfo *adaptInfo);

    /** \brief
     * Implementation of ProblemStatBase::estimate(). Deligates the estimation
     * to \ref estimator.
     */
    virtual void estimate(AdaptInfo *adaptInfo);

    /** \brief
     * Implementation of ProblemStatBase::markElements().
     * Deligated to \ref adapt.
     */
    virtual Flag markElements(AdaptInfo *adaptInfo);

    /** \brief
     * Implementation of ProblemStatBase::refineMesh(). Deligated to the
     * RefinementManager of \ref adapt.
     */
    virtual Flag refineMesh(AdaptInfo *adaptInfo);

    /** \brief
     * Implementation of ProblemStatBase::coarsenMesh(). Deligated to the
     * CoarseningManager of \ref adapt.
     */
    virtual Flag coarsenMesh(AdaptInfo *adaptInfo);

    /** \brief
     * Implementation of ProblemStatBase::buildBeforeRefine().
     * Does nothing here.
     */
    virtual void buildBeforeRefine(AdaptInfo *adaptInfo, Flag) {};

    /** \brief
     * Implementation of ProblemStatBase::buildBeforeCoarsen().
     * Does nothing here.
     */
    virtual void buildBeforeCoarsen(AdaptInfo *adaptInfo, Flag) {};

    /** \brief
     * Implementation of ProblemStatBase::buildAfterCoarsen().
     * Assembles \ref A and \ref rhs.
     */
    virtual void buildAfterCoarsen(AdaptInfo *adaptInfo, Flag flag);

    /** \brief
     * Determines the execution order of the single adaption steps. If adapt is
     * true, mesh adaption will be performed. This allows to avoid mesh adaption,
     * e.g. in timestep adaption loops of timestep adaptive strategies.
     */
    virtual Flag oneIteration(AdaptInfo *adaptInfo, Flag toDo = FULL_ITERATION);

    /** \brief
     * Returns number of managed problems
     */
    virtual int getNumProblems() { 
      return 1; 
    };

    /** \brief
     * Implementation of ProblemStatBase::getNumComponents()
     */
    virtual int getNumComponents() { 
216
      return nComponents; 
217
218
219
220
221
222
    }; 

    /** \brief
     * Returns the problem with the given number. If only one problem
     * is managed by this master problem, the number hasn't to be given.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
223
224
225
    virtual ProblemStatBase *getProblem(int number = 0) { 
      return this; 
    };
226
227
228
229
230
231

    /** \brief
     * Writes output files.
     */
    void writeFiles(AdaptInfo *adaptInfo, bool force);

Thomas Witkowski's avatar
Thomas Witkowski committed
232
233
234
235
236
    /** \brief
     * Startes parallel output writing;
     */
    void writeDelayedFiles();

Thomas Witkowski's avatar
Thomas Witkowski committed
237
238
239
240
241
    /** \brief
     * Returns true, if there is calculation waiting to be started.
     */
    bool existsDelayedCalculation();

242
243
244
    /** \brief
     * Interpolates fct to \ref solution.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
245
    void interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct);
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324

    /** \brief
     * Adds an Operator to \ref A.
     */
    void addMatrixOperator(Operator *op, int i, int j,
			   double *factor = NULL,
			   double *estFactor = NULL);

    /** \brief
     * Adds an Operator to \ref rhs.
     */
    void addVectorOperator(Operator *op, int i,
			   double *factor = NULL,
			   double *estFactor = NULL);

    /** \brief
     * Adds dirichlet boundary conditions.
     */
    virtual void addDirichletBC(BoundaryType type, int system,
				AbstractFunction<double, WorldVector<double> > *b);

    /** \brief
     * Adds neumann boundary conditions.
     */
    virtual void addNeumannBC(BoundaryType type, int row, int col, 
			      AbstractFunction<double, WorldVector<double> > *n);

    /** \brief
     * Adds robin boundary conditions.
     */
    virtual void addRobinBC(BoundaryType type, int row, int col, 
			    AbstractFunction<double, WorldVector<double> > *n,
			    AbstractFunction<double, WorldVector<double> > *r);

    /** \brief
     * Adds periodic boundary conditions.
     */
    virtual void addPeriodicBC(BoundaryType type, int row, int col);


    /** \brief
     * Implementation of ProblemStatBase::allowFirstRefinement().
     */
    inline void allowFirstRefinement() {
      allowFirstRef_ = true;
    }; 
  
    // ===== getting-methods ======================================================

    /** \name getting methods
     * \{ 
     */

    /** \brief
     * Returns \ref solution_.
     */
    inline SystemVector* getSolution() { 
      return solution_; 
    };

    /** \brief
     * Returns \ref rhs_.
     */
    inline SystemVector* getRHS() { 
      return rhs_; 
    };

    /** \brief
     * Returns \ref systemMatrix_.
     */
    inline Matrix<DOFMatrix*> *getSystemMatrix() { 
      return systemMatrix_; 
    };

    /** \brief
     * Returns mesh of given component
     */
    inline Mesh* getMesh(int comp) {
      FUNCNAME("ProblemVec::getMesh()");
Thomas Witkowski's avatar
Thomas Witkowski committed
325
      TEST_EXIT(comp < static_cast<int>(componentMeshes.size()) && comp >= 0)
326
	("invalid component number\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
327
      return componentMeshes[comp]; 
328
329
330
331
332
    };

    /** \brief
     * Returns \ref meshes_
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
333
    inline std::vector<Mesh*> getMeshes() {
334
335
336
337
338
339
340
341
      return meshes_; 
    };

    /** \brief
     * Returns \ref feSpace_.
     */
    inline FiniteElemSpace* getFESpace(int comp) { 
      FUNCNAME("ProblemVec::getFESpace()");
342
      TEST_EXIT(comp < static_cast<int>(componentSpaces.size()) && comp >= 0)
343
	("invalid component number\n");
344
      return componentSpaces[comp]; 
345
346
347
    };

    /** \brief
348
     * Returns \ref feSpaces.
349
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
350
    inline std::vector<FiniteElemSpace*> getFESpaces() { 
351
      return feSpaces; 
352
353
    };

354
    /** \brief
355
     * Returns \ref componentSpaces;
356
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
357
    inline std::vector<FiniteElemSpace*> getComponentFESpaces() {
358
      return componentSpaces;
359
360
    }

361
362
363
    /** \brief
     * Returns \ref estimator_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
364
    inline std::vector<Estimator*> getEstimator() { 
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
      return estimator_; 
    };

    /** \brief
     * Returns \ref estimator_.
     */
    inline Estimator* getEstimator(int comp) { 
      return estimator_[comp]; 
    };

    /** \brief
     * Returns \ref refinementManager_.
     */
    inline RefinementManager* getRefinementManager(int comp) { 
      return refinementManager_; 
    };

    /** \brief
     * Returns \ref refinementManager_.
     */
    inline CoarseningManager* getCoarseningManager(int comp) { 
      return coarseningManager_; 
    };

    /** \brief
     * Returns \ref solver_.
     */
    inline OEMSolver<SystemVector>* getSolver() { 
      return solver_; 
    };

    /** \brief
397
     * Returns \ref marker.
398
399
     */
    inline Marker *getMarker(int comp) { 
400
      return marker[comp]; 
401
402
403
    };

    /** \brief
404
     * Returns \ref marker.
405
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
406
    inline std::vector<Marker*> getMarker() { 
407
      return marker; 
408
409
410
411
412
    };

    /** \brief
     * Returns \ref name_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
413
    inline virtual const std::string& getName() { 
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
      return name_; 
    };

    /** \brief
     * Returns \ref useGetBound_.
     */
    inline bool getBoundUsed() { 
      return useGetBound_; 
    };

    /** \brief
     * Returns \ref leftPrecon_.
     */
    inline Preconditioner<SystemVector> *getLeftPrecon() { 
      return leftPrecon_; 
    };

    /** \brief
     * Returns \ref rightPrecon_.
     */
    inline Preconditioner<SystemVector> *getRightPrecon() { 
      return rightPrecon_; 
    };

    /** \} */

    // ===== setting-methods ======================================================

    /** \name setting methods
     * \{ 
     */

    /** \brief
     * Sets \ref estimator_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
449
    inline void setEstimator(std::vector<Estimator*> est) { 
450
451
452
453
      estimator_ = est; 
    };

    inline void setFESpace(FiniteElemSpace *feSpace, int comp) {
454
      feSpaces[comp] = feSpace;
455
456
457
458
459
460
461
462
463
    };

    /** \brief
     * Sets \ref estimator_.
     */
    inline void setEstimator(Estimator* est, int comp) { 
      estimator_[comp] = est; 
    };

464
465
    inline void setMarker(Marker* mark, int comp) { 
      marker[comp] = mark; 
466
467
468
469
470
    };

    /** \brief
     * Sets \ref solver_.
     */
471
472
473
    inline void setSolver(OEMSolver<SystemVector>* sol) { 
      solver_ = sol; 
    };
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

    /** \brief
     * Sets \ref leftPrecon_.
     */
    inline void setLeftPrecon(Preconditioner<SystemVector> *p) {
      leftPrecon_ = p;
    };

    /** \brief
     * Sets \ref rightPrecon_.
     */
    inline void setRightPrecon(Preconditioner<SystemVector> *p) {
      rightPrecon_ = p;
    };

489
490
491
492
    inline void setAssembleMatrixOnlyOnce(int i, int j, bool value = true) {
      assembleMatrixOnlyOnce_[i][j] = value;
    }

493
494
495
496
497
498
499
500
501
502
503
504
505
    void setExactSolutionFct(AbstractFunction<double, WorldVector<double> > *fct,
			     int component) {
      exactSolutionFcts[component] = fct;
    }

    AbstractFunction<double, WorldVector<double> > *getExactSolutionFct(int component) {
      return exactSolutionFcts[component];
    }

    void setComputeExactError(bool v) {
      computeExactError = v;
    }

506
507
    /** \} */

Thomas Witkowski's avatar
Thomas Witkowski committed
508
    void writeResidualMesh(AdaptInfo *adaptInfo, const std::string name);
509

510
511
512
513
514
    // ===== Serializable implementation =====
  
    /** \brief
     * serialization
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
515
    virtual void serialize(std::ostream &out);
516
517
518
519

    /** \brief
     * deserialization
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
520
    virtual void deserialize(std::istream &in);
521

Thomas Witkowski's avatar
Thomas Witkowski committed
522
    std::vector<FileWriterInterface*> getFileWriterList() {
523
524
525
      return fileWriters_;
    };

Thomas Witkowski's avatar
Thomas Witkowski committed
526
527
528
529
530
531
532
  protected:
    /** \brief
     * If the exact solution is known, the problem can compute the exact
     * error instead of the error estimation. This is done in this function.
     */
    void computeError(AdaptInfo *adaptInfo);

533
534
535
536
  protected:
    /** \brief
     * Name of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
537
    std::string name_;
538
539
540
541

    /** \brief
     * number of problem components
     */
542
    int nComponents;
543
544
545
546

    /** \brief
     * fe spaces of this problem.
     */
547
    std::vector<FiniteElemSpace*> feSpaces;
548
549
550
551

    /** \brief
     * meshes of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
552
    std::vector<Mesh*> meshes_;
553
554
555
556

    /** \brief
     * Pointer to the fe spaces for the different problem components
     */
557
    std::vector<FiniteElemSpace*> componentSpaces;
558
559
560
561

    /** \brief
     * Pointer to the meshes for the different problem components
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
562
    std::vector<Mesh*> componentMeshes;
563
564
565
566

    /** \brief
     * Responsible for element marking.
     */
567
    std::vector<Marker*> marker;
568
569
570
571

    /** \brief
     * Estimator of this problem. Used in \ref estimate().
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
572
    std::vector<Estimator*> estimator_;
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593

    /** \brief
     * Linear solver of this problem. Used in \ref solve().
     */
    OEMSolver<SystemVector> *solver_;

    /** \brief
     * system vector  storing the calculated solution of the problem.
     */
    SystemVector *solution_;

    /** \brief
     * system vector for the right hand side 
     */
    SystemVector *rhs_;

    /** \brief
     * System matrix
     */
    Matrix<DOFMatrix*> *systemMatrix_;

594
595
596
597
598
599
600
    /** \brief
     * Some DOFMatrices of the systemMatrix may be assembled only once (for
     * example if they are independent of the time or older solutions). If
     * [i][j] of this field is set to true, the corresponding DOFMatrix will
     * be assembled only once. All other matrices will be assembled at every
     * time step.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
601
    std::vector< std::vector< bool > > assembleMatrixOnlyOnce_;
602
603
604
605
606
607
608

    /** \brief
     * If [i][j] of this field is set to true, the corresponding DOFMatrix of
     * the systemMatrix_ has been assembled at least once. This field is used
     * to determine, if assembling of a matrix can be ommitted, if it is set
     * to be assembled only once.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
609
    std::vector< std::vector< bool > > assembledMatrix_;
610

611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
    /** \brief
     * Matrix-vector multiplication
     */
    MatVecMultiplier<SystemVector> *matVec_;

    /** \brief
     * Left preconditioner. Used in \ref solver.
     */
    Preconditioner<SystemVector> *leftPrecon_;

    /** \brief
     * Right preconditioner. Used in \ref solver.
     */
    Preconditioner<SystemVector> *rightPrecon_;

    /** \brief
     * Determines whether domain boundaries should be considered at assembling.
     */
    bool useGetBound_;

    /** \brief
     * Writes the meshes and solution after the adaption loop.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
634
    std::vector<FileWriterInterface*> fileWriters_;
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665

    /** \brief
     * All actions of mesh refinement are performed by refinementManager.
     * If new refinement algorithms should be realized, one has to override
     * RefinementManager and give one instance of it to AdaptStationary.
     */
    RefinementManager *refinementManager_;

    /** \brief
     * All actions of mesh coarsening are performed by coarseningManager.
     * If new coarsening algorithms should be realized, one has to override
     * CoarseningManager and give one instance of it to AdaptStationary.
     */
    CoarseningManager *coarseningManager_;
  
    /** \brief
     * Info level.
     */
    int info_;

    /** \brief
     * Allows one refinement although the adapt tolerance is reached already.
     */
    bool allowFirstRef_;

    ProblemIterationInterface *problemIteration_;

    /** \brief
     * Used for mesh traversal.
     */
    static ProblemVec *traversePtr_;
666
667
668
669
670
671
672
673
674
675
676
677
678

    /** \brief
     * This vectors stores pointers to functions defining the exact solution of
     * the problem. This may be used to compute the real error of the computed
     * solution.
     */
    std::vector<AbstractFunction<double, WorldVector<double> >*> exactSolutionFcts;

    /** \brief
     * If true, the error is not estimated but computed from the exact solution
     * defined by \ref exactSolutionFcts.
     */
    bool computeExactError;
679
680
681
682
  };
}

#endif