ProblemVec.h 16.4 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
74
75
	solver_(NULL),
	solution_(NULL),
	rhs_(NULL),
	systemMatrix_(NULL),
	matVec_(NULL),
	leftPrecon_(NULL),
	rightPrecon_(NULL),
	useGetBound_(true),
	info_(10),
	allowFirstRef_(false)
    {
76
77
78
79
80
81
82
83
84
85
86
87
      GET_PARAMETER(0, name_ + "->components", "%d", &nComponents);
      TEST_EXIT(nComponents > 0)("components not set!\n");    

      estimator_.resize(nComponents, NULL);
      marker_.resize(nComponents, NULL);

      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++) {
88
89
90
91
	  assembleMatrixOnlyOnce_[i][j] = false;
	  assembledMatrix_[i][j] = false;
	}
      }
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
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
    };

    /** \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() { 
213
      return nComponents; 
214
215
216
217
218
219
    }; 

    /** \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
220
221
222
    virtual ProblemStatBase *getProblem(int number = 0) { 
      return this; 
    };
223
224
225
226
227
228

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

Thomas Witkowski's avatar
Thomas Witkowski committed
229
230
231
232
233
    /** \brief
     * Startes parallel output writing;
     */
    void writeDelayedFiles();

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

239
240
241
    /** \brief
     * Interpolates fct to \ref solution.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
242
    void interpolInitialSolution(std::vector<AbstractFunction<double, WorldVector<double> >*> *fct);
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
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
325
326
327
328
329

    /** \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()");
      TEST_EXIT(comp < static_cast<int>(componentMeshes_.size()) && comp >= 0)
	("invalid component number\n");
      return componentMeshes_[comp]; 
    };

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

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

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

351
352
353
    /** \brief
     * Returns \ref componentSpaces_;
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
354
    inline std::vector<FiniteElemSpace*> getComponentFESpaces() {
355
356
357
      return componentSpaces_;
    }

358
359
360
    /** \brief
     * Returns \ref estimator_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
361
    inline std::vector<Estimator*> getEstimator() { 
362
363
364
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
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
     * Returns \ref marker_.
     */
    inline Marker *getMarker(int comp) { 
      return marker_[comp]; 
    };

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

    /** \brief
     * Returns \ref name_.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
410
    inline virtual const std::string& getName() { 
411
412
413
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
      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
446
    inline void setEstimator(std::vector<Estimator*> est) { 
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
      estimator_ = est; 
    };

    inline void setFESpace(FiniteElemSpace *feSpace, int comp) {
      feSpaces_[comp] = feSpace;
    };

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

    inline void setMarker(Marker* marker, int comp) { 
      marker_[comp] = marker; 
    };

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

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

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

486
487
488
489
    inline void setAssembleMatrixOnlyOnce(int i, int j, bool value = true) {
      assembleMatrixOnlyOnce_[i][j] = value;
    }

490
491
    /** \} */

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

494
495
496
497
498
    // ===== Serializable implementation =====
  
    /** \brief
     * serialization
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
499
    virtual void serialize(std::ostream &out);
500
501
502
503

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

Thomas Witkowski's avatar
Thomas Witkowski committed
506
    std::vector<FileWriterInterface*> getFileWriterList() {
507
508
509
510
511
512
513
      return fileWriters_;
    };

  protected:
    /** \brief
     * Name of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
514
    std::string name_;
515
516
517
518

    /** \brief
     * number of problem components
     */
519
    int nComponents;
520
521
522
523

    /** \brief
     * fe spaces of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
524
    std::vector<FiniteElemSpace*> feSpaces_;
525
526
527
528

    /** \brief
     * meshes of this problem.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
529
    std::vector<Mesh*> meshes_;
530
531
532
533

    /** \brief
     * Pointer to the fe spaces for the different problem components
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
534
    std::vector<FiniteElemSpace*> componentSpaces_;
535
536
537
538

    /** \brief
     * Pointer to the meshes for the different problem components
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
539
    std::vector<Mesh*> componentMeshes_;
540
541
542
543

    /** \brief
     * Responsible for element marking.
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
544
    std::vector<Marker*> marker_;
545
546
547
548

    /** \brief
     * Estimator of this problem. Used in \ref estimate().
     */
Thomas Witkowski's avatar
Thomas Witkowski committed
549
    std::vector<Estimator*> estimator_;
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570

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

571
572
573
574
575
576
577
    /** \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
578
    std::vector< std::vector< bool > > assembleMatrixOnlyOnce_;
579
580
581
582
583
584
585

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

588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
    /** \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
611
    std::vector<FileWriterInterface*> fileWriters_;
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646

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

#endif