ProblemStat.hpp 17.2 KB
Newer Older
1
2
3
4
#pragma once

#include <list>
#include <map>
5
6
7
8
9
#include <memory>
#include <string>
#include <tuple>
#include <utility>
#include <vector>
10

11
12
#include <dune/common/fvector.hh>
#include <dune/common/fmatrix.hh>
13
14
#include <dune/grid/common/grid.hh>

15
16
#include <amdis/AdaptInfo.hpp>
#include <amdis/CreatorInterface.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
17
#include <amdis/CreatorMap.hpp>
18
#include <amdis/DirichletBC.hpp>
Praetorius, Simon's avatar
Praetorius, Simon committed
19
//#include <amdis/Estimator.hpp>
20
21
22
#include <amdis/Flag.hpp>
#include <amdis/Initfile.hpp>
#include <amdis/LinearAlgebra.hpp>
23
#include <amdis/OperatorList.hpp>
24
#include <amdis/Marker.hpp>
25
#include <amdis/Mesh.hpp>
26
#include <amdis/PeriodicBC.hpp>
27
28
29
30
31
#include <amdis/ProblemStatBase.hpp>
#include <amdis/ProblemStatTraits.hpp>
#include <amdis/StandardProblemIteration.hpp>

#include <amdis/common/TupleUtility.hpp>
32
#include <amdis/common/TypeTraits.hpp>
33
34

#include <amdis/GridFunctions.hpp>
35
#include <amdis/gridfunctions/DiscreteFunction.hpp>
36
37
#include <amdis/gridfunctions/DOFVectorView.hpp>

38
#include <amdis/FileWriterInterface.hpp>
39

40
41
#include <amdis/typetree/TreeData.hpp>
#include <amdis/typetree/TreePath.hpp>
42

43
namespace AMDiS
44
{
45
46
47
48
  // forward declaration
  template <class Traits>
  class ProblemInstat;

49
  template <class Traits>
50
  class ProblemStat
Praetorius, Simon's avatar
Praetorius, Simon committed
51
      : public ProblemStatBase
52
      , public StandardProblemIteration
53
  {
54
    using Self = ProblemStat;
55

56
57
    friend class ProblemInstat<Traits>;

58
  public: // typedefs and static constants
59

60
    using GlobalBasis = typename Traits::GlobalBasis;
Praetorius, Simon's avatar
Praetorius, Simon committed
61
62
63
    using GridView    = typename GlobalBasis::GridView;
    using Grid        = typename GridView::Grid;
    using Element     = typename GridView::template Codim<0>::Entity;
64
    using WorldVector = typename Element::Geometry::GlobalCoordinate;
65
    using WorldMatrix = FieldMatrix<typename WorldVector::field_type, WorldVector::dimension, WorldVector::dimension>;
66

67
    /// Dimension of the grid
68
    static constexpr int dim = Grid::dimension;
69

70
    /// Dimension of the world
71
    static constexpr int dow = Grid::dimensionworld;
72

73
    using SystemMatrix = DOFMatrix<GlobalBasis, GlobalBasis, double>;
74
    using SystemVector = DOFVector<GlobalBasis, double>;
75

76
    using LinearSolverType = LinearSolverInterface<typename SystemMatrix::BaseMatrix, typename SystemVector::BaseVector>;
77

78
  public:
79
80
    /**
     * \brief Constructor. Takes the name of the problem that is used to
81
     * access values corresponding to this problem in the parameter file.
82
     **/
83
    explicit ProblemStat(std::string const& name)
84
      : StandardProblemIteration(dynamic_cast<ProblemStatBase&>(*this))
85
      , name_(name)
86
    {}
87

88
89
    /// Constructor taking additionally a reference to a grid that is used
    /// instead of the default created grid, \ref ProblemStat
90
91
    ProblemStat(std::string const& name, Grid& grid)
      : ProblemStat(name)
92
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
93
      adoptGrid(Dune::stackobject_to_shared_ptr(grid));
94
95
96
97
    }

    /// \brief Constructor taking a grid reference and a basis reference.
    /// Stores pointers to both.
98
99
    ProblemStat(std::string const& name, Grid& grid, GlobalBasis& globalBasis)
      : ProblemStat(name, grid)
100
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
101
      adoptGlobalBasis(Dune::stackobject_to_shared_ptr(globalBasis));
102
    }
Praetorius, Simon's avatar
Praetorius, Simon committed
103

104

105
    /**
106
     * \brief Initialisation of the problem.
107
     *
108
109
110
     * Parameters read in initialize() for problem with name 'PROB'
     *   MESH[0]->global refinements:  nr of initial global refinements
     **/
Praetorius, Simon's avatar
Praetorius, Simon committed
111
    void initialize(Flag initFlag, Self* adoptProblem = nullptr, Flag adoptFlag = INIT_NOTHING);
112

113

Praetorius, Simon's avatar
Praetorius, Simon committed
114
    /// Add an operator to \ref A.
115
    /** @{ */
Praetorius, Simon's avatar
Praetorius, Simon committed
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
    /// Operator evaluated on the whole element
    /**
     * Adds an operator to the list of element operators to be assembled in
     * quadrature points inside the element.
     *
     * \param op   A (pre-) local operator, \see LocalOperator, \see GridFunctionOperator
     * \param row  TreePath identifying the sub-basis in the global basis tree
     *             corresponding to the row basis. \see treepath()
     * \param col  TreePath identifying the sub-basis in the global basis tree
     *             corresponding to the column basis. \see treepath()
     *
     * Example:
     * ```
     * auto op = makeOperator(tag::test_trial{}, 1.0/tau);
     * prob.addMatrixOperator(op, _0, _0);
     * ```
     **/
Praetorius, Simon's avatar
Praetorius, Simon committed
133
    template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
134
135
136
137
    void addMatrixOperator(Operator const& op, RowTreePath row = {}, ColTreePath col = {})
    {
      systemMatrix_->addOperator(tag::element_operator<Element>{}, op, row, col);
    }
138

Praetorius, Simon's avatar
Praetorius, Simon committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
    /// Operator evaluated on the boundary of the domain with boundary index `b`
    /**
     * Adds an operator to the list of boundary operators to be assembled in
     * quadrature points on the boundary intersections.
     *
     * \param b    Boundary indentifier where to assemble this operator. Can be
     *             constructed from an integer. \see BoundaryType
     * \param op   A (pre-) local operator, \see LocalOperator, \see GridFunctionOperator
     * \param row  TreePath identifying the sub-basis in the global basis tree
     *             corresponding to the row basis. \see treepath()
     * \param col  TreePath identifying the sub-basis in the global basis tree
     *             corresponding to the column basis. \see treepath()
     *
     * Example:
     * ```
     * auto op = makeOperator(tag::test_trial{}, alpha);
     * prob.addMatrixOperator(BoundaryType{1}, op, _0, _0);
     * ```
     **/
Praetorius, Simon's avatar
Praetorius, Simon committed
158
    template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
159
160
    void addMatrixOperator(BoundaryType b, Operator const& op, RowTreePath row = {}, ColTreePath col = {})
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
161
      using I = typename GridView::Intersection;
162
      systemMatrix_->addOperator(tag::boundary_operator<I>{boundaryManager_,b}, op, row, col);
163
    }
164
    /** @} */
165
166


Praetorius, Simon's avatar
Praetorius, Simon committed
167
    /// Add an operator to \ref rhs.
168
    /** @{ */
Praetorius, Simon's avatar
Praetorius, Simon committed
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
    /// Operator evaluated on the whole element
    /**
     * Adds an operator to the list of element operators to be assembled in
     * quadrature points inside the element.
     *
     * \param op    A (pre-) local operator, \see LocalOperator, \see GridFunctionOperator
     * \param path  TreePath identifying the sub-basis in the global basis tree
     *              corresponding to the row basis. \see treepath()
     *
     * Example:
     * ```
     * auto op = makeOperator(tag::test{}, probInstat.getOldSolution(0) / tau);
     * prob.addVectorOperator(op, _0);
     * ```
     **/
Praetorius, Simon's avatar
Praetorius, Simon committed
184
    template <class Operator, class TreePath = RootTreePath>
185
186
187
188
    void addVectorOperator(Operator const& op, TreePath path = {})
    {
      rhs_->addOperator(tag::element_operator<Element>{}, op, path);
    }
189

Praetorius, Simon's avatar
Praetorius, Simon committed
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
    /// Operator evaluated on the boundary of the domain with boundary index `b`
    /**
     * Adds an operator to the list of boundary operators to be assembled in
     * quadrature points on the boundary intersections.
     *
     * \param b     Boundary indentifier where to assemble this operator. Can be
     *              constructed from an integer. \see BoundaryType
     * \param op    A (pre-) local operator, \see LocalOperator, \see GridFunctionOperator
     * \param path  TreePath identifying the sub-basis in the global basis tree
     *              corresponding to the row basis. \see treepath()
     *
     * Example:
     * ```
     * auto op = makeOperator(tag::test{}, [g](auto const& x) { return g(x); });
     * prob.addVectorOperator(BoundaryType{1}, op, _0);
     * ```
     **/
Praetorius, Simon's avatar
Praetorius, Simon committed
207
    template <class Operator, class TreePath = RootTreePath>
208
209
    void addVectorOperator(BoundaryType b, Operator const& op, TreePath path = {})
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
210
      using I = typename GridView::Intersection;
211
      rhs_->addOperator(tag::boundary_operator<I>{boundaryManager_,b}, op, path);
212
    }
213
    /** @} */
214

215

Praetorius, Simon's avatar
Praetorius, Simon committed
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
    /// Add boundary conditions to the system
    /** @{ */
    /// Dirichlet boundary condition
    /**
     * Enforce Dirichlet boundary values for the solution vector on boundary
     * regions identified by the predicate.
     *
     * \param predicate  Functor `bool(WorldVector)` returning true for all
     *                   DOFs on the boundary that should be assigned a value.
     * \param row        TreePath identifying the sub-basis in the global basis tree
     *                   corresponding to the row basis. \see treepath()
     * \param col        TreePath identifying the sub-basis in the global basis tree
     *                   corresponding to the column basis. \see treepath()
     * \param values     Functor `Range(WorldVector)` or any \ref GridFunction
     *                   that is evaluated in the DOFs identified by the predicate.
     *
     * Example:
     * ```
     * prob.addDirichletBC([](auto const& x) { return x[0] < 1.e-8; }, 0, 0,
     *                     [](auto const& x) { return 0.0; });
     * ```
     **/
238
    template <class Predicate, class RowTreePath, class ColTreePath, class Values>
239
    void addDirichletBC(Predicate const& predicate,
240
                        RowTreePath row, ColTreePath col,
Praetorius, Simon's avatar
Praetorius, Simon committed
241
                        Values const& values);
242
243
244
245
246
247
248
249
250

    template <class RowTreePath, class ColTreePath, class Values>
    void addDirichletBC(BoundaryType id,
                        RowTreePath row, ColTreePath col,
                        Values const& values);

    /// Add a periodic boundary conditions to the system, by specifying a face transformation
    /// y = A*x + b of coordinates. We assume, that A is orthonormal.
    void addPeriodicBC(BoundaryType id, WorldMatrix const& A, WorldVector const& b);
Praetorius, Simon's avatar
Praetorius, Simon committed
251
252
    /** @} */

253

254
  public:
255

256
    /// Implementation of \ref ProblemStatBase::solve
257
258
259
    void solve(AdaptInfo& adaptInfo,
               bool createMatrixData = true,
               bool storeMatrixData = false) override;
260

261
    /// Implementation of \ref ProblemStatBase::buildAfterCoarse
262
263
264
265
    void buildAfterAdapt(AdaptInfo& adaptInfo,
                         Flag flag,
                         bool asmMatrix = true,
                         bool asmVector = true) override;
266

Praetorius, Simon's avatar
Praetorius, Simon committed
267
268
269
270
271
272
273
    /// \brief Assemble the linear system by calling \ref buildAfterAdapt with
    /// `asmMatrix` and `asmVector` set to true.
    void assemble(AdaptInfo& adaptInfo)
    {
      buildAfterAdapt(adaptInfo, Flag{0}, true, true);
    }

274
    /// Writes output files.
275
    void writeFiles(AdaptInfo& adaptInfo, bool force = false);
276

Praetorius, Simon's avatar
Praetorius, Simon committed
277

278
  public: // get-methods
279

Praetorius, Simon's avatar
Praetorius, Simon committed
280
    /// Implementation of \ref ProblemStatBase::name
281
    std::string const& name() const override { return name_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
282
283
284
285
286
287
288


    /// Return a reference to the grid, \ref grid
    Grid&       grid()       { return *grid_; }
    Grid const& grid() const { return *grid_; }

    /// Return the gridView of the leaf-level
289
    GridView const& gridView() const { return globalBasis_->gridView(); }
Praetorius, Simon's avatar
Praetorius, Simon committed
290

291
292
293
294
295
296
    /// Return the boundary manager to identify boundary segments
    BoundaryManager<Grid>&       boundaryManager()       { return *boundaryManager_; }
    BoundaryManager<Grid> const& boundaryManager() const { return *boundaryManager_; }

    auto const& boundaryManagerPtr() { return boundaryManager_; }

Praetorius, Simon's avatar
Praetorius, Simon committed
297
298
299
300
301
302
303
304
    /// Return the \ref globalBasis_
    GlobalBasis&       globalBasis()       { return *globalBasis_; }
    GlobalBasis const& globalBasis() const { return *globalBasis_; }

    /// Return a reference to the linear solver, \ref linearSolver
    LinearSolverType&       solver()       { return *linearSolver_; }
    LinearSolverType const& solver() const { return *linearSolver_; }

305
    /// Returns a reference to system-matrix, \ref systemMatrix_
Praetorius, Simon's avatar
Praetorius, Simon committed
306
307
    SystemMatrix&       systemMatrix()       { return *systemMatrix_; }
    SystemMatrix const& systemMatrix() const { return *systemMatrix_; }
308
309

    /// Returns a reference to the solution vector, \ref solution_
Praetorius, Simon's avatar
Praetorius, Simon committed
310
311
    SystemVector&       solutionVector()       { return *solution_; }
    SystemVector const& solutionVector() const { return *solution_; }
312
313

    /// Return a reference to the rhs system-vector, \ref rhs
Praetorius, Simon's avatar
Praetorius, Simon committed
314
315
    SystemVector&       rhsVector()       { return *rhs_; }
    SystemVector const& rhsVector() const { return *rhs_; }
316

317
318
319

    /// Return a mutable view to a solution component
    template <class TreePath = RootTreePath>
Praetorius, Simon's avatar
Praetorius, Simon committed
320
    auto solution(TreePath path = {})
321
    {
322
      assert(bool(solution_) && "You have to call initialize() before.");
323
      auto&& tp = makeTreePath(path);
324
      return makeDOFVectorView(*solution_, tp);
325
    }
326

327
328
    /// Return a const view to a solution component
    template <class TreePath = RootTreePath>
Praetorius, Simon's avatar
Praetorius, Simon committed
329
    auto solution(TreePath path = {}) const
330
    {
331
      assert(bool(solution_) && "You have to call initialize() before.");
332
      auto&& tp = makeTreePath(path);
333
      return makeDiscreteFunction(*solution_, tp);
334
    }
335
336


Praetorius, Simon's avatar
Praetorius, Simon committed
337
  public: // set-methods
338

339
    /// Set a new linear solver for the problem
Praetorius, Simon's avatar
Praetorius, Simon committed
340
    void setSolver(std::shared_ptr<LinearSolverType> const& solver)
341
    {
342
      linearSolver_ = solver;
343
344
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
345
346
347
348
349
    void setSolver(LinearSolverType& solver)
    {
      setSolver(Dune::stackobject_to_shared_ptr(solver));
    }

350

351
    /// Set the grid. Stores pointer and initializes feSpaces
Praetorius, Simon's avatar
Praetorius, Simon committed
352
    /// matrices and vectors, as well as markers and file-writers.
353
    void setGrid(std::shared_ptr<Grid> const& grid)
354
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
355
      adoptGrid(grid);
356
      createGlobalBasis();
357
      createMatricesAndVectors();
Praetorius, Simon's avatar
Praetorius, Simon committed
358
      createMarker();
359
360
361
      createFileWriter();
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
362
363
364
365
366
    void setGrid(Grid& grid)
    {
      setGrid(Dune::stackobject_to_shared_ptr(grid));
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
367
368
369
370
371
372
373
374
375
376
377
378
    void addMarker(std::shared_ptr<Marker<Grid>> const& marker)
    {
      marker_.push_back(marker);
      if (marker_.size() > 1)
        marker_.back()->setMaximumMarking(true);
    }

    void addMarker(Marker<Grid>& marker)
    {
      addMarker(Dune::stackobject_to_shared_ptr(marker));
    }

379

380
  protected: // initialization methods
381

Praetorius, Simon's avatar
Praetorius, Simon committed
382
383
384
385
386
387
    void createGlobalBasis();
    void createGrid();
    void createMatricesAndVectors();
    void createSolver();
    void createMarker();
    void createFileWriter();
388

Praetorius, Simon's avatar
Praetorius, Simon committed
389
    void adoptGlobalBasis(std::shared_ptr<GlobalBasis> const& globalBasis)
390
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
391
      globalBasis_ = globalBasis;
392
393
394
      initGlobalBasis(*globalBasis_);
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
395
    void adoptGrid(std::shared_ptr<Grid> const& grid)
396
397
398
399
400
401
    {
      adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
    }

    void adoptGrid(std::shared_ptr<Grid> const& grid,
                   std::shared_ptr<BoundaryManager<Grid>> const& boundaryManager)
402
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
403
      grid_ = grid;
404
      boundaryManager_ = boundaryManager;
405
      Parameters::get(name_ + "->mesh", gridName_);
406
    }
407

Praetorius, Simon's avatar
Praetorius, Simon committed
408
  private:
409

Praetorius, Simon's avatar
Praetorius, Simon committed
410
411
    void createGlobalBasisImpl(std::true_type);
    void createGlobalBasisImpl(std::false_type);
412

Praetorius, Simon's avatar
Praetorius, Simon committed
413
    void initGlobalBasis(GlobalBasis const& globalBasis);
414

Praetorius, Simon's avatar
Praetorius, Simon committed
415

416
417
  public: // implementation of iteration interface methods

Praetorius, Simon's avatar
Praetorius, Simon committed
418
    /// Implementation of \ref StandardProblemIteration::oneIteration.
419
    Flag oneIteration(AdaptInfo& adaptInfo, Flag toDo = FULL_ITERATION) override
420
421
422
423
424
    {
      return StandardProblemIteration::oneIteration(adaptInfo, toDo);
    }

    /// Implementation of \ref ProblemStatBase::estimate.
425
    void estimate(AdaptInfo& adaptInfo) override { /* do nothing. */ }
426
427

    /// Implementation of \ref ProblemStatBase::refineMesh.
428
    Flag adaptGrid(AdaptInfo& adaptInfo) override;
429
430

    /// Implementation of \ref ProblemStatBase::markElements.
431
    Flag markElements(AdaptInfo& adaptInfo) override;
432

433
    /// Uniform global grid coarsening by up to n level
434
    Flag globalCoarsen(int n) override;
435
436

    /// Uniform global refinement by n level
437
    Flag globalRefine(int n) override;
438

439
  private:
440
    /// Name of this problem.
441
    std::string name_;
442

443
    /// Grid of this problem.
Praetorius, Simon's avatar
Praetorius, Simon committed
444
    std::shared_ptr<Grid> grid_;
445

446
    /// Name of the grid
447
    std::string gridName_ = "mesh";
448

449
450
451
    /// Management of boundary conditions
    std::shared_ptr<BoundaryManager<Grid>> boundaryManager_;

452
    /// FE spaces of this problem.
453
    std::shared_ptr<GlobalBasis> globalBasis_;
454

455
    /// A FileWriter object
456
    std::list<std::shared_ptr<FileWriterInterface>> filewriter_;
457

458
    /// Pointer to the adaptation markers
Praetorius, Simon's avatar
Praetorius, Simon committed
459
    std::list<std::shared_ptr<Marker<Grid>>> marker_;
Praetorius, Simon's avatar
Praetorius, Simon committed
460
461
462

    /// Pointer to the estimators for this problem
//    std::vector<Estimator*> estimator;
463

464
    /// An object of the linearSolver Interface
465
    std::shared_ptr<LinearSolverType> linearSolver_;
466

Praetorius, Simon's avatar
Praetorius, Simon committed
467
    /// Matrix that is filled during assembling
468
    std::shared_ptr<SystemMatrix> systemMatrix_;
469

Praetorius, Simon's avatar
Praetorius, Simon committed
470
    /// Vector with the solution components
471
    std::shared_ptr<SystemVector> solution_;
472

Praetorius, Simon's avatar
Praetorius, Simon committed
473
    /// Vector (load-vector) corresponding to the right-hand side
474
    /// of the equation, filled during assembling
475
    std::shared_ptr<SystemVector> rhs_;
476

Praetorius, Simon's avatar
Praetorius, Simon committed
477
478
479
480
    /// A vector with the local element error estimates
    /// for each node in the basis tree, indexed by [to_string(treePath)][element index]
    std::map<std::string, std::vector<double>> estimates_;

481

482
483
  private: // some internal data-structures

484
485
    DirichletBCs<GlobalBasis, GlobalBasis> dirichletBCs_;
    PeriodicBCs<GlobalBasis, GlobalBasis> periodicBCs_;
486
  };
487

Praetorius, Simon's avatar
Praetorius, Simon committed
488

489
490
491
#if DUNE_HAVE_CXX_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
  // Deduction rule
  template <class Grid, class GlobalBasis>
492
  ProblemStat(std::string const& name, Grid& grid, GlobalBasis& globalBasis)
493
494
495
496
497
498
    -> ProblemStat<DefaultProblemTraits<GlobalBasis>>;
#endif

  // Generator for ProblemStat with given Grid and GlobalBasis
  template <class Grid, class GlobalBasis>
  ProblemStat<DefaultProblemTraits<GlobalBasis>>
499
  makeProblemStat(std::string const& name, Grid& grid, GlobalBasis& globalBasis)
500
  {
501
    return {name, grid, globalBasis};
502
503
  }

504
} // end namespace AMDiS
505
506
507

#include "ProblemStat.inc.hpp"