ProblemStat.hpp 17.4 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


    /// Return a reference to the grid, \ref grid
285
286
    std::shared_ptr<Grid>       grid()       { return grid_; }
    std::shared_ptr<Grid const> grid() const { return grid_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
287
288

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

291
    /// Return the boundary manager to identify boundary segments
292
293
    std::shared_ptr<BoundaryManager<Grid>>       boundaryManager()       { return boundaryManager_; }
    std::shared_ptr<BoundaryManager<Grid> const> boundaryManager() const { return boundaryManager_; }
294

Praetorius, Simon's avatar
Praetorius, Simon committed
295
    /// Return the \ref globalBasis_
296
297
    std::shared_ptr<GlobalBasis>       globalBasis()       { return globalBasis_; }
    std::shared_ptr<GlobalBasis const> globalBasis() const { return globalBasis_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
298
299

    /// Return a reference to the linear solver, \ref linearSolver
300
301
    std::shared_ptr<LinearSolverType>       solver()       { return linearSolver_; }
    std::shared_ptr<LinearSolverType const> solver() const { return linearSolver_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
302

303
    /// Returns a reference to system-matrix, \ref systemMatrix_
304
305
    std::shared_ptr<SystemMatrix>       systemMatrix()       { return systemMatrix_; }
    std::shared_ptr<SystemMatrix const> systemMatrix() const { return systemMatrix_; }
306
307

    /// Returns a reference to the solution vector, \ref solution_
308
309
    std::shared_ptr<SystemVector>       solutionVector()       { return solution_; }
    std::shared_ptr<SystemVector const> solutionVector() const { return solution_; }
310
311

    /// Return a reference to the rhs system-vector, \ref rhs
312
313
    std::shared_ptr<SystemVector>       rhsVector()       { return rhs_; }
    std::shared_ptr<SystemVector const> rhsVector() const { return rhs_; }
314

315
316
317

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

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


Praetorius, Simon's avatar
Praetorius, Simon committed
335
  public: // set-methods
336

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

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

348

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
365
366
367
368
369
370
371
372
373
374
375
376
    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));
    }

377

378
  protected: // initialization methods
379

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

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

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
406
  private:
407

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

411
    void initGlobalBasis();
412

Praetorius, Simon's avatar
Praetorius, Simon committed
413

414
415
  public: // implementation of iteration interface methods

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

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

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

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

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

    /// Uniform global refinement by n level
435
    Flag globalRefine(int n) override;
436

437
  private:
438
    /// Name of this problem.
439
    std::string name_;
440

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

444
    /// Name of the grid
445
    std::string gridName_ = "mesh";
446

447
448
449
    /// Management of boundary conditions
    std::shared_ptr<BoundaryManager<Grid>> boundaryManager_;

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

453
    /// A FileWriter object
454
    std::list<std::shared_ptr<FileWriterInterface>> filewriter_;
455

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

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

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

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

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

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

Praetorius, Simon's avatar
Praetorius, Simon committed
475
476
477
478
    /// 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_;

479

480
481
  private: // some internal data-structures

482
483
    DirichletBCs<GlobalBasis, GlobalBasis> dirichletBCs_;
    PeriodicBCs<GlobalBasis, GlobalBasis> periodicBCs_;
484
  };
485

Praetorius, Simon's avatar
Praetorius, Simon committed
486

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

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

502
503
504
505
  // mark templates as explicitly instantiated in cpp file
  extern template class ProblemStat<YaspGridBasis<2,1>>;
  extern template class ProblemStat<YaspGridBasis<2,2>>;

506
} // end namespace AMDiS
507
508

#include "ProblemStat.inc.hpp"