ProblemVec.h 17.8 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
232
233
234

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

    /** \brief
     * Interpolates fct to \ref solution.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
235
    void interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct);
236
237
238
239
240
241
242
243
244
245
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

    /** \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;
    }; 
Thomas Witkowski's avatar
Thomas Witkowski committed
282
283
284
285
286
287
288
289
290
291
292

    /** \brief
     * This function assembles a DOFMatrix and a DOFVector for the case,
     * the meshes from row and col FE-space are equal.
     */
    void assembleOnOneMesh(FiniteElemSpace *feSpace, Flag assembleFlag,
			   DOFMatrix *matrix, DOFVector<double> *vector);

    void assembleOnDifMeshes(FiniteElemSpace *rowFeSpace, FiniteElemSpace *colFeSpace,
			     Flag assembleFlag,
			     DOFMatrix *matrix, DOFVector<double> *vector);
293
294
295
296
297

    void assembleBoundaryConditions(DOFVector<double> *rhs,
				    DOFVector<double> *solution,
				    Mesh *mesh,
				    Flag assembleFlag);
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
325
326
327
328
329
330
  
    // ===== 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
331
      TEST_EXIT(comp < static_cast<int>(componentMeshes.size()) && comp >= 0)
332
	("invalid component number\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
333
      return componentMeshes[comp]; 
334
335
336
337
338
    };

    /** \brief
     * Returns \ref meshes_
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
339
    inline std::vector<Mesh*> getMeshes() {
340
341
342
343
344
345
346
347
      return meshes_; 
    };

    /** \brief
     * Returns \ref feSpace_.
     */
    inline FiniteElemSpace* getFESpace(int comp) { 
      FUNCNAME("ProblemVec::getFESpace()");
348
      TEST_EXIT(comp < static_cast<int>(componentSpaces.size()) && comp >= 0)
349
	("invalid component number\n");
350
      return componentSpaces[comp]; 
351
352
353
    };

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

360
    /** \brief
361
     * Returns \ref componentSpaces;
362
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
363
    inline std::vector<FiniteElemSpace*> getComponentFESpaces() {
364
      return componentSpaces;
365
366
    }

367
368
369
    /** \brief
     * Returns \ref estimator_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
370
    inline std::vector<Estimator*> getEstimator() { 
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
397
398
399
400
401
402
      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
403
     * Returns \ref marker.
404
405
     */
    inline Marker *getMarker(int comp) { 
406
      return marker[comp]; 
407
408
409
    };

    /** \brief
410
     * Returns \ref marker.
411
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
412
    inline std::vector<Marker*> getMarker() { 
413
      return marker; 
414
415
416
417
418
    };

    /** \brief
     * Returns \ref name_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
419
    inline virtual const std::string& getName() { 
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
449
450
451
452
453
454
      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
455
    inline void setEstimator(std::vector<Estimator*> est) { 
456
457
458
459
      estimator_ = est; 
    };

    inline void setFESpace(FiniteElemSpace *feSpace, int comp) {
460
      feSpaces[comp] = feSpace;
461
462
463
464
465
466
467
468
469
    };

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

470
471
    inline void setMarker(Marker* mark, int comp) { 
      marker[comp] = mark; 
472
473
474
475
476
    };

    /** \brief
     * Sets \ref solver_.
     */
477
478
479
    inline void setSolver(OEMSolver<SystemVector>* sol) { 
      solver_ = sol; 
    };
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494

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

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

495
496
497
498
    inline void setAssembleMatrixOnlyOnce(int i, int j, bool value = true) {
      assembleMatrixOnlyOnce_[i][j] = value;
    }

499
500
501
502
503
504
505
506
507
508
509
510
511
    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;
    }

512
513
    /** \} */

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

516
517
518
519
520
    // ===== Serializable implementation =====
  
    /** \brief
     * serialization
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
521
    virtual void serialize(std::ostream &out);
522
523
524
525

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

Thomas Witkowski's avatar
Thomas Witkowski committed
528
    std::vector<FileWriterInterface*> getFileWriterList() {
529
530
531
      return fileWriters_;
    };

Thomas Witkowski's avatar
Thomas Witkowski committed
532
533
534
535
536
537
538
  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);

539
540
541
542
  protected:
    /** \brief
     * Name of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
543
    std::string name_;
544
545
546
547

    /** \brief
     * number of problem components
     */
548
    int nComponents;
549
550
551
552

    /** \brief
     * fe spaces of this problem.
     */
553
    std::vector<FiniteElemSpace*> feSpaces;
554
555
556
557

    /** \brief
     * meshes of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
558
    std::vector<Mesh*> meshes_;
559
560
561
562

    /** \brief
     * Pointer to the fe spaces for the different problem components
     */
563
    std::vector<FiniteElemSpace*> componentSpaces;
564
565
566
567

    /** \brief
     * Pointer to the meshes for the different problem components
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
568
    std::vector<Mesh*> componentMeshes;
569
570
571
572

    /** \brief
     * Responsible for element marking.
     */
573
    std::vector<Marker*> marker;
574
575
576
577

    /** \brief
     * Estimator of this problem. Used in \ref estimate().
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
578
    std::vector<Estimator*> estimator_;
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599

    /** \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_;

600
601
602
603
604
605
606
    /** \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
607
    std::vector< std::vector< bool > > assembleMatrixOnlyOnce_;
608
609
610
611
612
613
614

    /** \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
615
    std::vector< std::vector< bool > > assembledMatrix_;
616

617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
    /** \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
640
    std::vector<FileWriterInterface*> fileWriters_;
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
666
667
668
669
670
671

    /** \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_;
672
673
674
675
676
677
678
679
680
681
682
683
684

    /** \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;
685
686
687
688
  };
}

#endif