ProblemVec.h 17.9 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

    /** \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
292
293
294
295
296
297
298
299
300
301
302

    /** \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);
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
331
332
333
334
335
  
    // ===== 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
336
      TEST_EXIT(comp < static_cast<int>(componentMeshes.size()) && comp >= 0)
337
	("invalid component number\n");
Thomas Witkowski's avatar
Thomas Witkowski committed
338
      return componentMeshes[comp]; 
339
340
341
342
343
    };

    /** \brief
     * Returns \ref meshes_
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
344
    inline std::vector<Mesh*> getMeshes() {
345
346
347
348
349
350
351
352
      return meshes_; 
    };

    /** \brief
     * Returns \ref feSpace_.
     */
    inline FiniteElemSpace* getFESpace(int comp) { 
      FUNCNAME("ProblemVec::getFESpace()");
353
      TEST_EXIT(comp < static_cast<int>(componentSpaces.size()) && comp >= 0)
354
	("invalid component number\n");
355
      return componentSpaces[comp]; 
356
357
358
    };

    /** \brief
359
     * Returns \ref feSpaces.
360
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
361
    inline std::vector<FiniteElemSpace*> getFESpaces() { 
362
      return feSpaces; 
363
364
    };

365
    /** \brief
366
     * Returns \ref componentSpaces;
367
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
368
    inline std::vector<FiniteElemSpace*> getComponentFESpaces() {
369
      return componentSpaces;
370
371
    }

372
373
374
    /** \brief
     * Returns \ref estimator_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
375
    inline std::vector<Estimator*> getEstimator() { 
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
403
404
405
406
407
      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
408
     * Returns \ref marker.
409
410
     */
    inline Marker *getMarker(int comp) { 
411
      return marker[comp]; 
412
413
414
    };

    /** \brief
415
     * Returns \ref marker.
416
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
417
    inline std::vector<Marker*> getMarker() { 
418
      return marker; 
419
420
421
422
423
    };

    /** \brief
     * Returns \ref name_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
424
    inline virtual const std::string& getName() { 
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
455
456
457
458
459
      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
460
    inline void setEstimator(std::vector<Estimator*> est) { 
461
462
463
464
      estimator_ = est; 
    };

    inline void setFESpace(FiniteElemSpace *feSpace, int comp) {
465
      feSpaces[comp] = feSpace;
466
467
468
469
470
471
472
473
474
    };

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

475
476
    inline void setMarker(Marker* mark, int comp) { 
      marker[comp] = mark; 
477
478
479
480
481
    };

    /** \brief
     * Sets \ref solver_.
     */
482
483
484
    inline void setSolver(OEMSolver<SystemVector>* sol) { 
      solver_ = sol; 
    };
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499

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

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

500
501
502
503
    inline void setAssembleMatrixOnlyOnce(int i, int j, bool value = true) {
      assembleMatrixOnlyOnce_[i][j] = value;
    }

504
505
506
507
508
509
510
511
512
513
514
515
516
    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;
    }

517
518
    /** \} */

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

521
522
523
524
525
    // ===== Serializable implementation =====
  
    /** \brief
     * serialization
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
526
    virtual void serialize(std::ostream &out);
527
528
529
530

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

Thomas Witkowski's avatar
Thomas Witkowski committed
533
    std::vector<FileWriterInterface*> getFileWriterList() {
534
535
536
      return fileWriters_;
    };

Thomas Witkowski's avatar
Thomas Witkowski committed
537
538
539
540
541
542
543
  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);

544
545
546
547
  protected:
    /** \brief
     * Name of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
548
    std::string name_;
549
550
551
552

    /** \brief
     * number of problem components
     */
553
    int nComponents;
554
555
556
557

    /** \brief
     * fe spaces of this problem.
     */
558
    std::vector<FiniteElemSpace*> feSpaces;
559
560
561
562

    /** \brief
     * meshes of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
563
    std::vector<Mesh*> meshes_;
564
565
566
567

    /** \brief
     * Pointer to the fe spaces for the different problem components
     */
568
    std::vector<FiniteElemSpace*> componentSpaces;
569
570
571
572

    /** \brief
     * Pointer to the meshes for the different problem components
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
573
    std::vector<Mesh*> componentMeshes;
574
575
576
577

    /** \brief
     * Responsible for element marking.
     */
578
    std::vector<Marker*> marker;
579
580
581
582

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

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

605
606
607
608
609
610
611
    /** \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
612
    std::vector< std::vector< bool > > assembleMatrixOnlyOnce_;
613
614
615
616
617
618
619

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

622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
    /** \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
645
    std::vector<FileWriterInterface*> fileWriters_;
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
672
673
674
675
676

    /** \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_;
677
678
679
680
681
682
683
684
685
686
687
688
689

    /** \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;
690
691
692
693
  };
}

#endif