AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
ProblemStat.hpp
1 #pragma once
2 
3 #include <list>
4 #include <map>
5 #include <memory>
6 #include <string>
7 #include <tuple>
8 #include <utility>
9 #include <vector>
10 
11 #include <dune/common/fvector.hh>
12 #include <dune/common/fmatrix.hh>
13 #include <dune/common/shared_ptr.hh>
14 
15 #include <dune/grid/common/grid.hh>
16 
17 #include <amdis/AdaptInfo.hpp>
18 #include <amdis/AdaptiveGrid.hpp>
19 #include <amdis/BiLinearForm.hpp>
20 #include <amdis/CreatorInterface.hpp>
21 #include <amdis/CreatorMap.hpp>
22 #include <amdis/DirichletBC.hpp>
23 #include <amdis/DOFVector.hpp>
24 //#include <amdis/Estimator.hpp>
25 #include <amdis/Flag.hpp>
26 #include <amdis/Initfile.hpp>
27 #include <amdis/LinearAlgebra.hpp>
28 #include <amdis/LinearForm.hpp>
29 #include <amdis/Marker.hpp>
30 #include <amdis/MeshCreator.hpp>
31 #include <amdis/OperatorList.hpp>
32 #include <amdis/PeriodicBC.hpp>
33 #include <amdis/ProblemStatBase.hpp>
34 #include <amdis/ProblemStatTraits.hpp>
35 #include <amdis/StandardProblemIteration.hpp>
36 #include <amdis/common/SharedPtr.hpp>
37 #include <amdis/common/TupleUtility.hpp>
38 #include <amdis/common/TypeTraits.hpp>
39 #include <amdis/GridFunctions.hpp>
40 #include <amdis/gridfunctions/DiscreteFunction.hpp>
41 #include <amdis/io/FileWriterBase.hpp>
42 #include <amdis/typetree/Traits.hpp>
43 #include <amdis/typetree/TreePath.hpp>
44 
45 namespace AMDiS
46 {
47  // forward declaration
48  template <class Traits>
49  class ProblemInstat;
50 
51  template <class Traits>
53  : public ProblemStatBase
54  , public StandardProblemIterationAdaptor<ProblemStat<Traits>>
55  {
56  using Self = ProblemStat;
57 
58  friend class ProblemInstat<Traits>;
59 
60  public: // typedefs and static constants
61 
62  using GlobalBasis = typename Traits::GlobalBasis;
63  using GridView = typename GlobalBasis::GridView;
65  using Element = typename GridView::template Codim<0>::Entity;
66  using WorldVector = typename Element::Geometry::GlobalCoordinate;
67  using WorldMatrix = FieldMatrix<typename WorldVector::field_type, WorldVector::dimension, WorldVector::dimension>;
68 
70  static constexpr int dim = Grid::dimension;
71 
73  static constexpr int dow = Grid::dimensionworld;
74 
75  private:
76  using LinAlgTraits = typename Traits::LinAlgTraits;
77  using Mat = typename LinAlgTraits::template MatrixImpl<typename Traits::CoefficientType>;
78  using Vec = typename LinAlgTraits::template VectorImpl<typename Traits::CoefficientType>;
79  using PartitionSet = typename LinAlgTraits::PartitionSet;
80 
81  public:
86 
87  public:
92  explicit ProblemStat(std::string const& name)
93  : name_(name)
94  {}
95 
98  template <class Grid_>
99  ProblemStat(std::string const& name, Grid_&& grid)
100  : ProblemStat(name)
101  {
102  adoptGrid(wrap_or_share(FWD(grid)));
103  }
104 
107  template <class Grid_, class Basis_, class B_ = Underlying_t<Basis_>,
108  REQUIRES(Concepts::GlobalBasis<B_>)>
109  ProblemStat(std::string const& name, Grid_&& grid, Basis_&& globalBasis)
110  : ProblemStat(name, FWD(grid))
111  {
112  adoptGlobalBasis(wrap_or_share(FWD(globalBasis)));
113  }
114 
117  template <class Grid_, class PBF_, class GV_ = typename Underlying_t<Grid_>::LeafGridView,
118  REQUIRES(Concepts::PreBasisFactory<PBF_, GV_, MultiIndex_t<PBF_>>)>
119  ProblemStat(std::string const& name, Grid_&& grid, PBF_ const& preBasisFactory)
120  : ProblemStat(name, FWD(grid))
121  {
122  adoptGlobalBasis(makeSharedPtr(GlobalBasis{grid_->leafGridView(), preBasisFactory}));
123  }
124 
126 
130  void initialize(Flag initFlag, Self* adoptProblem = nullptr, Flag adoptFlag = INIT_NOTHING);
131 
133 
138  void restore(Flag initFlag);
139 
140 
142 
143 
160  template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
161  void addMatrixOperator(Operator const& op, RowTreePath row = {}, ColTreePath col = {})
162  {
163  static constexpr bool isValidTreePath =
164  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, RowTreePath> &&
165  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, ColTreePath>;
166  static_assert(isValidTreePath, "Invalid row and/or col treepath passed to addMatrixOperator!");
167 
168  if constexpr (isValidTreePath)
169  systemMatrix_->addOperator(tag::element_operator<Element>{}, op, row, col);
170  }
171 
173 
191  template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
192  void addMatrixOperator(BoundaryType b, Operator const& op, RowTreePath row = {}, ColTreePath col = {})
193  {
194  using I = typename GridView::Intersection;
195  static constexpr bool isValidTreePath =
196  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, RowTreePath> &&
197  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, ColTreePath>;
198  static_assert(isValidTreePath, "Invalid row and/or col treepath passed to addMatrixOperator!");
199 
200  if constexpr (isValidTreePath)
201  systemMatrix_->addOperator(tag::boundary_operator<I>{*boundaryManager_,b}, op, row, col);
202  }
206 
208 
223  template <class Operator, class TreePath = RootTreePath>
224  void addVectorOperator(Operator const& op, TreePath path = {})
225  {
226  static constexpr bool isValidTreePath =
227  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, TreePath>;
228  static_assert(isValidTreePath, "Invalid treepath passed to addVectorOperator!");
229 
230  if constexpr (isValidTreePath)
231  rhs_->addOperator(tag::element_operator<Element>{}, op, path);
232  }
233 
235 
251  template <class Operator, class TreePath = RootTreePath>
252  void addVectorOperator(BoundaryType b, Operator const& op, TreePath path = {})
253  {
254  using I = typename GridView::Intersection;
255  static constexpr bool isValidTreePath =
256  Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, TreePath>;
257  static_assert(isValidTreePath, "Invalid treepath passed to addVectorOperator!");
258 
259  if constexpr (isValidTreePath)
260  rhs_->addOperator(tag::boundary_operator<I>{*boundaryManager_,b}, op, path);
261  }
265 
267 
287  template <class Predicate, class RowTreePath, class ColTreePath, class Values>
288  void addDirichletBC(Predicate const& predicate,
289  RowTreePath row, ColTreePath col,
290  Values const& values);
291 
292  template <class RowTreePath, class ColTreePath, class Values>
294  RowTreePath row, ColTreePath col,
295  Values const& values);
296 
297  template <class Identifier, class Values>
298  void addDirichletBC(Identifier&& id, Values&& values)
299  {
300  addDirichletBC(FWD(id), RootTreePath{}, RootTreePath{}, FWD(values));
301  }
302 
305  void addPeriodicBC(BoundaryType id, WorldMatrix const& A, WorldVector const& b);
309  public:
310 
312  Flag oneIteration(AdaptInfo& adaptInfo, Flag toDo = FULL_ITERATION) override
313  {
314  return StandardProblemIteration::oneIteration(adaptInfo, toDo);
315  }
316 
318  void buildAfterAdapt(AdaptInfo& adaptInfo,
319  Flag flag,
320  bool asmMatrix = true,
321  bool asmVector = true) override;
322 
325  void assemble(AdaptInfo& adaptInfo)
326  {
327  buildAfterAdapt(adaptInfo, Flag{0}, true, true);
328  }
329 
331  void solve(AdaptInfo& adaptInfo,
332  bool createMatrixData = true,
333  bool storeMatrixData = false) override;
334 
336  void estimate(AdaptInfo& /*adaptInfo*/) override { /* do nothing. */ }
337 
339  Flag adaptGrid(AdaptInfo& adaptInfo) override;
340 
342  Flag markElements(AdaptInfo& adaptInfo) override;
343 
345  Flag globalCoarsen(int n) override;
346 
348  Flag globalRefine(int n) override;
349 
351  void writeFiles(AdaptInfo& adaptInfo, bool force = false);
352 
353 
354  public: // get-methods
355 
357  std::string const& name() const override { return name_; }
358 
360  std::shared_ptr<Grid> grid() { return grid_; }
361  std::shared_ptr<Grid const> grid() const { return grid_; }
362 
364  GridView gridView() const { return globalBasis_->gridView(); }
365 
367  std::shared_ptr<BoundaryManager<Grid>> boundaryManager() { return boundaryManager_; }
368  std::shared_ptr<BoundaryManager<Grid> const> boundaryManager() const { return boundaryManager_; }
369 
371  std::shared_ptr<GlobalBasis> globalBasis() { return globalBasis_; }
372  std::shared_ptr<GlobalBasis const> globalBasis() const { return globalBasis_; }
373 
375  std::shared_ptr<LinearSolver> solver() { return linearSolver_; }
376  std::shared_ptr<LinearSolver const> solver() const { return linearSolver_; }
377 
379  std::shared_ptr<SystemMatrix> systemMatrix() { return systemMatrix_; }
380  std::shared_ptr<SystemMatrix const> systemMatrix() const { return systemMatrix_; }
381 
383  std::shared_ptr<SolutionVector> solutionVector() { return solution_; }
384  std::shared_ptr<SolutionVector const> solutionVector() const { return solution_; }
385 
387  std::shared_ptr<SystemVector> rhsVector() { return rhs_; }
388  std::shared_ptr<SystemVector const> rhsVector() const { return rhs_; }
389 
390 
392  template <class... Indices>
393  auto solution(Indices... ii)
394  {
395  assert(bool(solution_) && "You have to call initialize() before.");
396  return valueOf(*solution_, ii...);
397  }
398 
400  template <class... Indices>
401  auto solution(Indices... ii) const
402  {
403  assert(bool(solution_) && "You have to call initialize() before.");
404  return valueOf(*solution_, ii...);
405  }
406 
407 
408  public: // set-methods
409 
411  template <class Solver_>
412  void setSolver(Solver_&& solver)
413  {
414  linearSolver_ = wrap_or_share(FWD(solver));
415  }
416 
417 
421  template <class Grid_>
422  void setGrid(Grid_&& grid)
423  {
424  adoptGrid(wrap_or_share(FWD(grid)));
425  createGlobalBasis();
426  createMatricesAndVectors();
427  createMarker();
428  createFileWriter();
429  }
430 
431 
433 
436  template <class Marker_>
437  void addMarker(Marker_&& m)
438  {
439  auto marker = wrap_or_share(FWD(m));
440  auto it = marker_.emplace(marker->name(), marker);
441  if (marker_.size() > 1)
442  it.first->second->setMaximumMarking(true);
443  }
444 
446  void removeMarker(std::string name)
447  {
448  std::size_t num = marker_.erase(name);
449  test_warning(num == 1, "A marker with the given name '{}' does not exist.", name);
450  }
451 
453  void removeMarker(Marker<Grid> const& marker)
454  {
455  removeMarker(marker.name());
456  }
457 
458 
459  protected: // initialization methods
460 
461  void createGlobalBasis();
462  void createGrid();
463  void createMatricesAndVectors();
464  void createSolver();
465  void createMarker();
466  void createFileWriter();
467 
468  void adoptGlobalBasis(std::shared_ptr<GlobalBasis> globalBasis)
469  {
470  globalBasis_ = std::move(globalBasis);
471  initGlobalBasis();
472  }
473 
474  void adoptGrid(std::shared_ptr<Grid> const& grid,
475  std::shared_ptr<BoundaryManager<Grid>> const& boundaryManager)
476  {
477  grid_ = grid;
478  boundaryManager_ = boundaryManager;
479  Parameters::get(name_ + "->mesh", gridName_);
480  }
481 
482  void adoptGrid(std::shared_ptr<Grid> const& grid)
483  {
484  adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
485  }
486 
487  void adoptGrid(std::shared_ptr<typename Grid::HostGrid> const& hostGrid)
488  {
489  auto grid = std::make_shared<Grid>(hostGrid);
490  adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
491  }
492 
493  private:
494 
495  void createGlobalBasisImpl(std::true_type);
496  void createGlobalBasisImpl(std::false_type);
497 
498  void initGlobalBasis();
499 
500  private:
502  std::string name_;
503 
505  std::shared_ptr<Grid> grid_;
506 
508  std::string gridName_ = "mesh";
509 
511  std::shared_ptr<BoundaryManager<Grid>> boundaryManager_;
512 
514  std::shared_ptr<GlobalBasis> globalBasis_;
515 
517  std::list<std::shared_ptr<FileWriterInterface>> filewriter_;
518 
520  std::map<std::string, std::shared_ptr<Marker<Grid>>> marker_;
521 
523 // std::vector<Estimator*> estimator;
524 
526  std::shared_ptr<LinearSolver> linearSolver_;
527 
529  std::shared_ptr<SystemMatrix> systemMatrix_;
530 
532  std::shared_ptr<SolutionVector> solution_;
533 
536  std::shared_ptr<SystemVector> rhs_;
537 
540  std::map<std::string, std::vector<double>> estimates_;
541 
542  private: // some internal data-structures
544  };
545 
546 
547  namespace Impl
548  {
549  template <class Grid, class B, class = void>
550  struct DeducedProblemTraits;
551 
552  template <class Grid, class PB>
553  struct DeducedProblemTraits<Grid,GlobalBasis<PB>,void>
554  {
556  };
557 
558  template <class G, class PBF>
559  struct DeducedProblemTraits<G,PBF,
560  std::enable_if_t<Concepts::PreBasisFactory<PBF, typename G::LeafGridView, MultiIndex_t<PBF>>>>
561  {
562  using Grid = AdaptiveGrid_t<G>;
563  using GridView = typename Grid::LeafGridView;
564  using Basis = decltype(GlobalBasis{std::declval<GridView>(),std::declval<PBF>()});
565 
566  using type = DefaultProblemTraits<Basis>;
567  };
568 
569  template <class Grid, class Basis>
570  using DeducedProblemTraits_t = typename DeducedProblemTraits<Grid,Basis>::type;
571  }
572 
573 
574  // Deduction guide
575  template <class Grid, class Basis>
576  ProblemStat(std::string name, Grid&& grid, Basis&& globalBasis)
578 
579 
580  // mark templates as explicitly instantiated in cpp file
581  extern template class ProblemStat<YaspGridBasis<2,1>>;
582  extern template class ProblemStat<YaspGridBasis<2,2>>;
583 
584 } // end namespace AMDiS
585 
586 #include "ProblemStat.inc.hpp"
Flag oneIteration(AdaptInfo &adaptInfo, Flag toDo=FULL_ITERATION) override
Implementation of StandardProblemIteration::oneIteration.
Definition: ProblemStat.hpp:312
static constexpr int dow
Dimension of the world.
Definition: ProblemStat.hpp:73
std::string const & name() const override
Implementation of ProblemStatBase::name.
Definition: ProblemStat.hpp:357
void addPeriodicBC(BoundaryType id, WorldMatrix const &A, WorldVector const &b)
Definition: ProblemStat.inc.hpp:350
The Flag class encapsulates flags which represents simple information. Used e.g. while mesh traversal...
Definition: Flag.hpp:13
ProblemStat(std::string const &name, Grid_ &&grid)
Definition: ProblemStat.hpp:99
void addMatrixOperator(BoundaryType b, Operator const &op, RowTreePath row={}, ColTreePath col={})
Operator evaluated on the boundary of the domain with boundary index b
Definition: ProblemStat.hpp:192
void addDirichletBC(Predicate const &predicate, RowTreePath row, ColTreePath col, Values const &values)
Add boundary conditions to the system.
Definition: ProblemStat.inc.hpp:294
std::index_sequence< I... > Indices
class that represents a sequence of indices
Definition: Index.hpp:40
void addMarker(Marker_ &&m)
Store the shared_ptr and the name of the marker in the problem.
Definition: ProblemStat.hpp:437
typename Impl::AdaptiveGridImpl< HostGrid >::type AdaptiveGrid_t
Definition: AdaptiveGrid.hpp:367
void addMatrixOperator(Operator const &op, RowTreePath row={}, ColTreePath col={})
Add an operator to A.
Definition: ProblemStat.hpp:161
void estimate(AdaptInfo &) override
Implementation of ProblemStatBase::estimate.
Definition: ProblemStat.hpp:336
constexpr bool Predicate
A predicate is a function that returns a boolean.
Definition: Concepts.hpp:142
void setGrid(Grid_ &&grid)
Definition: ProblemStat.hpp:422
The basic container that stores a base vector and a corresponding basis.
Definition: LinearForm.hpp:25
ProblemStat(std::string const &name)
Constructor. Takes the name of the problem that is used to access values corresponding to this proble...
Definition: ProblemStat.hpp:92
The basic container that stores a base vector and a corresponding basis.
Definition: DOFVector.hpp:27
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
StandardProblemIteration when derived from ProblemStat.
Definition: StandardProblemIteration.hpp:71
Definition: OperatorList.hpp:16
Base class for all markers.
Definition: Marker.hpp:28
std::shared_ptr< LinearSolver > solver()
Return a reference to the linear solver, linearSolver.
Definition: ProblemStat.hpp:375
std::shared_ptr< SolutionVector > solutionVector()
Returns a reference to the solution vector, solution_.
Definition: ProblemStat.hpp:383
Wrapper around a global basis providing default traits.
Definition: ProblemStatTraits.hpp:77
std::shared_ptr< GlobalBasis > globalBasis()
Return the globalBasis_.
Definition: ProblemStat.hpp:371
Definition: ProblemStat.hpp:52
void initialize(Flag initFlag, Self *adoptProblem=nullptr, Flag adoptFlag=INIT_NOTHING)
Initialisation of the problem.
Definition: ProblemStat.inc.hpp:23
Standard implementation of ProblemTimeInterface for a time dependent problems.
Definition: ProblemInstat.hpp:22
TreeMatrix< BCData< Mat, Sol, Rhs >::template type, typename RB::LocalView::Tree, typename CB::LocalView::Tree > BoundaryConditions
Definition: BoundaryCondition.hpp:53
void restore(Flag initFlag)
Read the grid and solution from backup files and initialize the problem.
Definition: ProblemStat.inc.hpp:108
Definition: BiLinearForm.hpp:27
ProblemStat(std::string const &name, Grid_ &&grid, Basis_ &&globalBasis)
Constructor taking a grid and basis. Wraps both in shared pointers.
Definition: ProblemStat.hpp:109
std::shared_ptr< Grid > grid()
Return the grid_.
Definition: ProblemStat.hpp:360
auto solution(Indices... ii) const
Return a const view to a solution component.
Definition: ProblemStat.hpp:401
std::shared_ptr< SystemMatrix > systemMatrix()
Returns a reference to system-matrix, systemMatrix_.
Definition: ProblemStat.hpp:379
static std::optional< T > get(std::string const &key)
Get parameter-values from parameter-tree.
Definition: Initfile.hpp:25
static constexpr int dim
Dimension of the grid.
Definition: ProblemStat.hpp:70
void addVectorOperator(Operator const &op, TreePath path={})
Add an operator to rhs.
Definition: ProblemStat.hpp:224
std::shared_ptr< SystemVector > rhsVector()
Return a reference to the rhs system-vector, rhs.
Definition: ProblemStat.hpp:387
Flag markElements(AdaptInfo &adaptInfo) override
Implementation of ProblemStatBase::markElements.
Definition: ProblemStat.inc.hpp:391
void removeMarker(std::string name)
Remove a marker with the given name from the problem.
Definition: ProblemStat.hpp:446
Abstract base class for linear solvers.
Definition: LinearSolverInterface.hpp:27
Flag adaptGrid(AdaptInfo &adaptInfo) override
Implementation of ProblemStatBase::refineMesh.
Definition: ProblemStat.inc.hpp:453
auto valueOf(DiscreteFunction< Coeff, GB, Path... > &df, Indices... ii)
A Generator for the childs of a mutable DiscreteFunction.
Definition: DiscreteFunction.hpp:269
ProblemStat(std::string const &name, Grid_ &&grid, PBF_ const &preBasisFactory)
Constructor taking a grid and pre-basis factory to create a global basis on the fly.
Definition: ProblemStat.hpp:119
Interface for time independent problems. Concrete problems must override all pure virtual methods...
Definition: ProblemStatBase.hpp:58
void writeFiles(AdaptInfo &adaptInfo, bool force=false)
Writes output files. If force=true write even if timestep out of write rhythm.
Definition: ProblemStat.inc.hpp:530
void solve(AdaptInfo &adaptInfo, bool createMatrixData=true, bool storeMatrixData=false) override
Implementation of ProblemStatBase::solve.
Definition: ProblemStat.inc.hpp:362
typename PreBasis::GridView GridView
The grid view that the FE space is defined on.
Definition: GlobalBasis.hpp:66
GridView gridView() const
Return the gridView of the basis.
Definition: ProblemStat.hpp:364
Flag globalRefine(int n) override
Uniform global refinement by n level.
Definition: ProblemStat.inc.hpp:438
Holds adapt parameters and infos about the problem.
Definition: AdaptInfo.hpp:25
void addVectorOperator(BoundaryType b, Operator const &op, TreePath path={})
Operator evaluated on the boundary of the domain with boundary index b
Definition: ProblemStat.hpp:252
auto solution(Indices... ii)
Return a mutable view to a solution component.
Definition: ProblemStat.hpp:393
std::shared_ptr< T > wrap_or_share(T &t)
Definition: SharedPtr.hpp:22
std::shared_ptr< BoundaryManager< Grid > > boundaryManager()
Return the boundary manager to identify boundary segments.
Definition: ProblemStat.hpp:367
void buildAfterAdapt(AdaptInfo &adaptInfo, Flag flag, bool asmMatrix=true, bool asmVector=true) override
Implementation of ProblemStatBase::buildAfterCoarse.
Definition: ProblemStat.inc.hpp:468
Definition: OperatorList.hpp:14
constexpr bool GlobalBasis
A Dune::Functions::GlobalBasis type.
Definition: Concepts.hpp:190
void assemble(AdaptInfo &adaptInfo)
Assemble the linear system by calling buildAfterAdapt with asmMatrix and asmVector set to true...
Definition: ProblemStat.hpp:325
Manage boundary ids of boundary segments in a grid.
Definition: BoundaryManager.hpp:52
std::string const & name() const
Returns name_ of the Marker.
Definition: Marker.hpp:81
Flag globalCoarsen(int n) override
Uniform global grid coarsening by up to n level.
Definition: ProblemStat.inc.hpp:407
void setSolver(Solver_ &&solver)
Set a new linear solver for the problem.
Definition: ProblemStat.hpp:412
void removeMarker(Marker< Grid > const &marker)
Remove a marker from the problem.
Definition: ProblemStat.hpp:453
void test_warning(bool condition, std::string const &str, Args &&... args)
test for condition and in case of failure print message
Definition: Output.hpp:183
Flag oneIteration(AdaptInfo &adaptInfo, Flag toDo) override
Implementation of ProblemIterationInterface::oneIteration()
Definition: StandardProblemIteration.cpp:35