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

277
278
279
    /// Backup the grid
    void backup(std::string const& filename) const;

280
281
282
    /// Implements \ref ProblemStatBase::backup
    void backup(AdaptInfo& adaptInfo) override;

283
284
285
    /// Retore the grid
    auto restore(std::string const& filename);

286
287
288
    /// Implements \ref ProblemStatBase::restore
    void restore(Flag initFlag) override;

Praetorius, Simon's avatar
Praetorius, Simon committed
289

290
  public: // get-methods
291

Praetorius, Simon's avatar
Praetorius, Simon committed
292
    /// Implementation of \ref ProblemStatBase::name
293
    std::string const& name() const override { return name_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
294
295
296


    /// Return a reference to the grid, \ref grid
297
298
    std::shared_ptr<Grid>       grid()       { return grid_; }
    std::shared_ptr<Grid const> grid() const { return grid_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
299
300

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

303
    /// Return the boundary manager to identify boundary segments
304
305
    std::shared_ptr<BoundaryManager<Grid>>       boundaryManager()       { return boundaryManager_; }
    std::shared_ptr<BoundaryManager<Grid> const> boundaryManager() const { return boundaryManager_; }
306

Praetorius, Simon's avatar
Praetorius, Simon committed
307
    /// Return the \ref globalBasis_
308
309
    std::shared_ptr<GlobalBasis>       globalBasis()       { return globalBasis_; }
    std::shared_ptr<GlobalBasis const> globalBasis() const { return globalBasis_; }
Praetorius, Simon's avatar
Praetorius, Simon committed
310
311

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

315
    /// Returns a reference to system-matrix, \ref systemMatrix_
316
317
    std::shared_ptr<SystemMatrix>       systemMatrix()       { return systemMatrix_; }
    std::shared_ptr<SystemMatrix const> systemMatrix() const { return systemMatrix_; }
318
319

    /// Returns a reference to the solution vector, \ref solution_
320
321
    std::shared_ptr<SystemVector>       solutionVector()       { return solution_; }
    std::shared_ptr<SystemVector const> solutionVector() const { return solution_; }
322
323

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

327
328
329

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

337
338
    /// Return a const view to a solution component
    template <class TreePath = RootTreePath>
Praetorius, Simon's avatar
Praetorius, Simon committed
339
    auto solution(TreePath path = {}) const
340
    {
341
      assert(bool(solution_) && "You have to call initialize() before.");
342
      auto&& tp = makeTreePath(path);
343
      return makeDiscreteFunction(*solution_, tp);
344
    }
345
346


Praetorius, Simon's avatar
Praetorius, Simon committed
347
  public: // set-methods
348

349
    /// Set a new linear solver for the problem
Praetorius, Simon's avatar
Praetorius, Simon committed
350
    void setSolver(std::shared_ptr<LinearSolverType> const& solver)
351
    {
352
      linearSolver_ = solver;
353
354
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
355
356
357
358
359
    void setSolver(LinearSolverType& solver)
    {
      setSolver(Dune::stackobject_to_shared_ptr(solver));
    }

360

361
    /// Set the grid. Stores pointer and initializes feSpaces
Praetorius, Simon's avatar
Praetorius, Simon committed
362
    /// matrices and vectors, as well as markers and file-writers.
363
    void setGrid(std::shared_ptr<Grid> const& grid)
364
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
365
      adoptGrid(grid);
366
      createGlobalBasis();
367
      createMatricesAndVectors();
Praetorius, Simon's avatar
Praetorius, Simon committed
368
      createMarker();
369
370
371
      createFileWriter();
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
372
373
374
375
376
    void setGrid(Grid& grid)
    {
      setGrid(Dune::stackobject_to_shared_ptr(grid));
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
377
378
379
380
381
382
383
384
385
386
387
388
    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));
    }

389

390
  protected: // initialization methods
391

Praetorius, Simon's avatar
Praetorius, Simon committed
392
393
394
395
396
397
    void createGlobalBasis();
    void createGrid();
    void createMatricesAndVectors();
    void createSolver();
    void createMarker();
    void createFileWriter();
398

Praetorius, Simon's avatar
Praetorius, Simon committed
399
    void adoptGlobalBasis(std::shared_ptr<GlobalBasis> const& globalBasis)
400
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
401
      globalBasis_ = globalBasis;
402
403
404
      initGlobalBasis(*globalBasis_);
    }

Praetorius, Simon's avatar
Praetorius, Simon committed
405
    void adoptGrid(std::shared_ptr<Grid> const& grid)
406
407
408
409
410
411
    {
      adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
    }

    void adoptGrid(std::shared_ptr<Grid> const& grid,
                   std::shared_ptr<BoundaryManager<Grid>> const& boundaryManager)
412
    {
Praetorius, Simon's avatar
Praetorius, Simon committed
413
      grid_ = grid;
414
      boundaryManager_ = boundaryManager;
415
      Parameters::get(name_ + "->mesh", gridName_);
416
    }
417

Praetorius, Simon's avatar
Praetorius, Simon committed
418
  private:
419

Praetorius, Simon's avatar
Praetorius, Simon committed
420
421
    void createGlobalBasisImpl(std::true_type);
    void createGlobalBasisImpl(std::false_type);
422

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

Praetorius, Simon's avatar
Praetorius, Simon committed
425

426
427
  public: // implementation of iteration interface methods

Praetorius, Simon's avatar
Praetorius, Simon committed
428
    /// Implementation of \ref StandardProblemIteration::oneIteration.
429
    Flag oneIteration(AdaptInfo& adaptInfo, Flag toDo = FULL_ITERATION) override
430
431
432
433
434
    {
      return StandardProblemIteration::oneIteration(adaptInfo, toDo);
    }

    /// Implementation of \ref ProblemStatBase::estimate.
435
    void estimate(AdaptInfo& adaptInfo) override { /* do nothing. */ }
436
437

    /// Implementation of \ref ProblemStatBase::refineMesh.
438
    Flag adaptGrid(AdaptInfo& adaptInfo) override;
439
440

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

443
    /// Uniform global grid coarsening by up to n level
444
    Flag globalCoarsen(int n) override;
445
446

    /// Uniform global refinement by n level
447
    Flag globalRefine(int n) override;
448

449
  private:
450
    /// Name of this problem.
451
    std::string name_;
452

453
    /// Grid of this problem.
Praetorius, Simon's avatar
Praetorius, Simon committed
454
    std::shared_ptr<Grid> grid_;
455

456
    /// Name of the grid
457
    std::string gridName_ = "mesh";
458

459
460
461
    /// Management of boundary conditions
    std::shared_ptr<BoundaryManager<Grid>> boundaryManager_;

462
    /// FE spaces of this problem.
463
    std::shared_ptr<GlobalBasis> globalBasis_;
464

465
    /// A FileWriter object
466
    std::list<std::shared_ptr<FileWriterInterface>> filewriter_;
467

468
    /// Pointer to the adaptation markers
Praetorius, Simon's avatar
Praetorius, Simon committed
469
    std::list<std::shared_ptr<Marker<Grid>>> marker_;
Praetorius, Simon's avatar
Praetorius, Simon committed
470
471
472

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

474
    /// An object of the linearSolver Interface
475
    std::shared_ptr<LinearSolverType> linearSolver_;
476

Praetorius, Simon's avatar
Praetorius, Simon committed
477
    /// Matrix that is filled during assembling
478
    std::shared_ptr<SystemMatrix> systemMatrix_;
479

Praetorius, Simon's avatar
Praetorius, Simon committed
480
    /// Vector with the solution components
481
    std::shared_ptr<SystemVector> solution_;
482

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

Praetorius, Simon's avatar
Praetorius, Simon committed
487
488
489
490
    /// 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_;

491

492
493
  private: // some internal data-structures

494
495
    DirichletBCs<GlobalBasis, GlobalBasis> dirichletBCs_;
    PeriodicBCs<GlobalBasis, GlobalBasis> periodicBCs_;
496
  };
497

Praetorius, Simon's avatar
Praetorius, Simon committed
498

499
500
501
#if DUNE_HAVE_CXX_CLASS_TEMPLATE_ARGUMENT_DEDUCTION
  // Deduction rule
  template <class Grid, class GlobalBasis>
502
  ProblemStat(std::string const& name, Grid& grid, GlobalBasis& globalBasis)
503
504
505
506
507
508
    -> ProblemStat<DefaultProblemTraits<GlobalBasis>>;
#endif

  // Generator for ProblemStat with given Grid and GlobalBasis
  template <class Grid, class GlobalBasis>
  ProblemStat<DefaultProblemTraits<GlobalBasis>>
509
  makeProblemStat(std::string const& name, Grid& grid, GlobalBasis& globalBasis)
510
  {
511
    return {name, grid, globalBasis};
512
513
  }

514
515
516
517
  // mark templates as explicitly instantiated in cpp file
  extern template class ProblemStat<YaspGridBasis<2,1>>;
  extern template class ProblemStat<YaspGridBasis<2,2>>;

518
} // end namespace AMDiS
519
520

#include "ProblemStat.inc.hpp"