AMDiS 2.11-git
The Adaptive Multi-Dimensional Simulation Toolbox
 
Loading...
Searching...
No Matches
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/Assembler.hpp>
20#include <amdis/BiLinearForm.hpp>
21#include <amdis/CreatorInterface.hpp>
22#include <amdis/CreatorMap.hpp>
23#include <amdis/DirichletBC.hpp>
24#include <amdis/SymmetricDirichletBC.hpp>
25#include <amdis/DOFVector.hpp>
26//#include <amdis/Estimator.hpp>
27#include <amdis/Flag.hpp>
28#include <amdis/Initfile.hpp>
29#include <amdis/LinearAlgebra.hpp>
30#include <amdis/LinearForm.hpp>
31#include <amdis/Marker.hpp>
32#include <amdis/MeshCreator.hpp>
33#include <amdis/PeriodicBC.hpp>
34#include <amdis/ProblemStatBase.hpp>
35#include <amdis/ProblemStatTraits.hpp>
36#include <amdis/StandardProblemIteration.hpp>
37#include <amdis/common/SharedPtr.hpp>
38#include <amdis/common/TupleUtility.hpp>
39#include <amdis/common/TypeTraits.hpp>
40#include <amdis/GridFunctions.hpp>
41#include <amdis/gridfunctions/DiscreteFunction.hpp>
42#include <amdis/io/FileWriterBase.hpp>
43#include <amdis/typetree/Concepts.hpp>
44#include <amdis/typetree/TreePath.hpp>
45
46namespace AMDiS
47{
48 // forward declaration
49 template <class Traits>
50 class ProblemInstat;
51
52 template <class Traits>
54 : public ProblemStatBase
55 , public StandardProblemIterationAdaptor<ProblemStat<Traits>>
56 {
57 using Self = ProblemStat;
58
59 friend class ProblemInstat<Traits>;
60
61 public: // typedefs and static constants
62
63 using GlobalBasis = typename Traits::GlobalBasis;
64 using GridView = typename GlobalBasis::GridView;
65 using Grid = AdaptiveGrid_t<typename GridView::Grid>;
66 using Element = typename GridView::template Codim<0>::Entity;
67 using WorldVector = typename Element::Geometry::GlobalCoordinate;
68 using WorldMatrix = FieldMatrix<typename WorldVector::field_type, WorldVector::dimension, WorldVector::dimension>;
69
71 static constexpr int dim = Grid::dimension;
72
74 static constexpr int dow = Grid::dimensionworld;
75
76 using value_type = typename Traits::CoefficientType;
77
78 using Mat = typename Traits::Backend::template Matrix<GlobalBasis,GlobalBasis>::template Impl<value_type>;
79 using Vec = typename Traits::Backend::template Vector<GlobalBasis>::template Impl<value_type>;
80
83
87
88 public:
93 explicit ProblemStat(std::string const& name)
94 : name_(name)
95 {}
96
99 template <class Grid_>
100 ProblemStat(std::string const& name, Grid_&& grid)
102 {
103 adoptGrid(wrap_or_share(FWD(grid)));
104 }
105
108 template <class Grid_, class Basis_, class B_ = Underlying_t<Basis_>,
109 REQUIRES(Concepts::GlobalBasis<B_>)>
110 ProblemStat(std::string const& name, Grid_&& grid, Basis_&& globalBasis)
111 : ProblemStat(name, FWD(grid))
112 {
113 adoptGlobalBasis(wrap_or_share(FWD(globalBasis)));
114 }
115
118 template <class Grid_, class PBF_, class GV_ = typename Underlying_t<Grid_>::LeafGridView,
119 REQUIRES(Concepts::PreBasisFactory<PBF_, GV_>)>
120 ProblemStat(std::string const& name, Grid_&& grid, PBF_ const& preBasisFactory)
121 : ProblemStat(name, FWD(grid))
122 {
123 adoptGlobalBasis(makeSharedPtr(GlobalBasis{grid_->leafGridView(), preBasisFactory}));
124 }
125
127
131 void initialize(Flag initFlag, Self* adoptProblem = nullptr, Flag adoptFlag = INIT_NOTHING);
132
134
139 void restore(Flag initFlag);
140
141
143
145
161 template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
162 void addMatrixOperator(Operator const& op, RowTreePath row = {}, ColTreePath col = {})
163 {
164 static constexpr bool isValidTreePath =
165 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, RowTreePath> &&
166 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, ColTreePath>;
167 static_assert(isValidTreePath, "Invalid row and/or col treepath passed to addMatrixOperator!");
168
169 if constexpr (isValidTreePath)
170 systemMatrix_->addOperator(tag::element_operator<Element>{}, op, row, col);
171 }
172
174
192 template <class Operator, class RowTreePath = RootTreePath, class ColTreePath = RootTreePath>
193 void addMatrixOperator(BoundaryType b, Operator const& op, RowTreePath row = {}, ColTreePath col = {})
194 {
195 using I = typename GridView::Intersection;
196 static constexpr bool isValidTreePath =
197 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, RowTreePath> &&
198 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, ColTreePath>;
199 static_assert(isValidTreePath, "Invalid row and/or col treepath passed to addMatrixOperator!");
200
201 if constexpr (isValidTreePath)
202 systemMatrix_->addOperator(BoundarySubset<I>{*boundaryManager_,b}, op, row, col);
203 }
208
210
224 template <class Operator, class TreePath = RootTreePath>
225 void addVectorOperator(Operator const& op, TreePath path = {})
226 {
227 static constexpr bool isValidTreePath =
228 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, TreePath>;
229 static_assert(isValidTreePath, "Invalid treepath passed to addVectorOperator!");
230
231 if constexpr (isValidTreePath)
232 rhs_->addOperator(tag::element_operator<Element>{}, op, path);
233 }
234
236
252 template <class Operator, class TreePath = RootTreePath>
253 void addVectorOperator(BoundaryType b, Operator const& op, TreePath path = {})
254 {
255 using I = typename GridView::Intersection;
256 static constexpr bool isValidTreePath =
257 Concepts::ValidTreePath<typename GlobalBasis::LocalView::Tree, TreePath>;
258 static_assert(isValidTreePath, "Invalid treepath passed to addVectorOperator!");
259
260 if constexpr (isValidTreePath)
261 rhs_->addOperator(BoundarySubset<I>{*boundaryManager_,b}, op, path);
262 }
267
269
288 template <class Predicate, class RowTreePath, class ColTreePath, class Values>
289 void addDirichletBC(Predicate const& predicate,
290 RowTreePath row, ColTreePath col,
291 Values const& values);
292
293 template <class RowTreePath, class ColTreePath, class Values>
294 void addDirichletBC(BoundaryType id,
295 RowTreePath row, ColTreePath col,
296 Values const& values);
297
298
299 template <class Identifier, class Values>
300 void addDirichletBC(Identifier&& id, Values&& values)
301 {
302 addDirichletBC(FWD(id), RootTreePath{}, RootTreePath{}, FWD(values));
303 }
304
305 template <class Predicate, class Path, class Values>
306 void addSymmetricDirichletBC(Predicate const& predicate,
307 Path path,
308 Values const& values);
309
310 template <class Path, class Values>
311 void addSymmetricDirichletBC(BoundaryType id,
312 Path path,
313 Values const& values);
314
315 template <class Identifier, class Values>
316 void addSymmetricDirichletBC(Identifier&& id, Values&& values)
317 {
318 addSymmetricDirichletBC(FWD(id), RootTreePath{}, FWD(values));
319 }
320
323 void addPeriodicBC(BoundaryType id, WorldMatrix const& A, WorldVector const& b);
326 void addConstraint(BoundaryCondition<SystemMatrix, SolutionVector, SystemVector> constraint)
327 {
328 constraints_.push_back(constraint);
329 }
330
331 public:
332
334 Flag oneIteration(AdaptInfo& adaptInfo, Flag toDo = FULL_ITERATION) override
335 {
336 return StandardProblemIteration::oneIteration(adaptInfo, toDo);
337 }
338
340 void buildAfterAdapt(AdaptInfo& adaptInfo,
341 Flag flag,
342 bool asmMatrix = true,
343 bool asmVector = true) override;
344
347 void assemble(AdaptInfo& adaptInfo)
348 {
349 buildAfterAdapt(adaptInfo, Flag{0}, true, true);
350 }
351
353 void solve(AdaptInfo& adaptInfo,
354 bool createMatrixData = true,
355 bool storeMatrixData = false) override;
356
358 void estimate(AdaptInfo& /*adaptInfo*/) override { /* do nothing. */ }
359
361 Flag adaptGrid(AdaptInfo& adaptInfo) override;
362
364 Flag markElements(AdaptInfo& adaptInfo) override;
365
367 Flag globalCoarsen(int n) override;
368
370 Flag globalRefine(int n) override;
371
373 void writeFiles(AdaptInfo& adaptInfo, bool force = false);
374
375
376 public: // get-methods
377
379 std::string const& name() const override { return name_; }
380
382 std::shared_ptr<Grid> grid() { return grid_; }
383 std::shared_ptr<Grid const> grid() const { return grid_; }
384
386 GridView gridView() const { return globalBasis_->gridView(); }
387
389 std::shared_ptr<BoundaryManager<Grid>> boundaryManager() { return boundaryManager_; }
390 std::shared_ptr<BoundaryManager<Grid> const> boundaryManager() const { return boundaryManager_; }
391
393 std::shared_ptr<GlobalBasis> globalBasis() { return globalBasis_; }
394 std::shared_ptr<GlobalBasis const> globalBasis() const { return globalBasis_; }
395
397 std::shared_ptr<LinearSolverInterface> solver() { return linearSolver_; }
398 std::shared_ptr<LinearSolverInterface const> solver() const { return linearSolver_; }
399
401 std::shared_ptr<SystemMatrix> systemMatrix() { return systemMatrix_; }
402 std::shared_ptr<SystemMatrix const> systemMatrix() const { return systemMatrix_; }
403
405 std::shared_ptr<SolutionVector> solutionVector() { return solution_; }
406 std::shared_ptr<SolutionVector const> solutionVector() const { return solution_; }
407
409 std::shared_ptr<SystemVector> rhsVector() { return rhs_; }
410 std::shared_ptr<SystemVector const> rhsVector() const { return rhs_; }
411
412
414
418 template <class Range = void, class... Indices>
419 auto solution(Indices... ii)
420 {
421 assert(bool(solution_) && "You have to call initialize() before.");
422 return valueOf<Range>(*solution_, ii...);
423 }
424
426
430 template <class Range = void, class... Indices>
431 auto solution(Indices... ii) const
432 {
433 assert(bool(solution_) && "You have to call initialize() before.");
434 return valueOf<Range>(*solution_, ii...);
435 }
436
437
438 public: // set-methods
439
441 template <class Solver_>
442 void setSolver(Solver_&& solver)
443 {
444 linearSolver_ = wrap_or_share(FWD(solver));
445 }
446
447
451 template <class Grid_>
452 void setGrid(Grid_&& grid)
453 {
454 adoptGrid(wrap_or_share(FWD(grid)));
455 createGlobalBasis();
456 createMatricesAndVectors();
457 createMarker();
458 createFileWriter();
459 }
460
461
463
466 template <class Marker_>
467 void addMarker(Marker_&& m)
468 {
469 auto marker = wrap_or_share(FWD(m));
470 auto it = marker_.emplace(marker->name(), marker);
471 if (marker_.size() > 1)
472 it.first->second->setMaximumMarking(true);
473 }
474
476 void removeMarker(std::string name)
477 {
478 std::size_t num = marker_.erase(name);
479 test_warning(num == 1, "A marker with the given name '{}' does not exist.", name);
480 }
481
483 void removeMarker(Marker<Grid> const& marker)
484 {
485 removeMarker(marker.name());
486 }
487
489 template<class FileWriter_>
490 void addFileWriter(FileWriter_&& f)
491 {
492 filewriter_.push_back(wrap_or_share(FWD(f)));
493 }
494
497 {
498 filewriter_.clear();
499 }
500
501 protected: // initialization methods
502
503 void createGlobalBasis();
504 void createGrid();
505 void createMatricesAndVectors();
506 void createSolver();
507 void createMarker();
508 void createFileWriter();
509
510 void adoptGlobalBasis(std::shared_ptr<GlobalBasis> globalBasis)
511 {
512 globalBasis_ = std::move(globalBasis);
513 initGlobalBasis();
514 }
515
516 void adoptGrid(std::shared_ptr<Grid> const& grid,
517 std::shared_ptr<BoundaryManager<Grid>> const& boundaryManager)
518 {
519 grid_ = grid;
520 boundaryManager_ = boundaryManager;
521 Parameters::get(name_ + "->mesh", gridName_);
522 }
523
524 void adoptGrid(std::shared_ptr<Grid> const& grid)
525 {
526 adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
527 }
528
529 void adoptGrid(std::shared_ptr<typename Grid::HostGrid> const& hostGrid)
530 {
531 auto grid = std::make_shared<Grid>(hostGrid);
532 adoptGrid(grid, std::make_shared<BoundaryManager<Grid>>(grid));
533 }
534
535 private:
536
537 void createGlobalBasisImpl(std::true_type);
538 void createGlobalBasisImpl(std::false_type);
539
540 void initGlobalBasis();
541
542 protected:
543
545 std::string name_;
546
548 std::shared_ptr<Grid> grid_;
549
551 std::string gridName_ = "mesh";
552
554 std::shared_ptr<BoundaryManager<Grid>> boundaryManager_;
555
557 std::shared_ptr<GlobalBasis> globalBasis_;
558
560 std::list<std::shared_ptr<FileWriterInterface>> filewriter_;
561
563 std::map<std::string, std::shared_ptr<Marker<Grid>>> marker_;
564
566// std::vector<Estimator*> estimator;
567
569 std::shared_ptr<LinearSolverInterface> linearSolver_;
570
572 std::shared_ptr<SystemMatrix> systemMatrix_;
573
575 std::shared_ptr<SolutionVector> solution_;
576
579 std::shared_ptr<SystemVector> rhs_;
580
583 std::map<std::string, std::vector<double>> estimates_;
584
586 std::list<BoundaryCondition<SystemMatrix, SolutionVector, SystemVector>> constraints_;
587 };
588
589
590 namespace Impl
591 {
592 template <class Grid, class B, class = void>
593 struct DeducedProblemTraits;
594
595 template <class Grid, class PB>
596 struct DeducedProblemTraits<Grid,GlobalBasis<PB>,void>
597 {
599 };
600
601 template <class Grid, class PB>
602 struct DeducedProblemTraits<Grid,const GlobalBasis<PB>,void>
603 {
604 using type = DefaultProblemTraits<GlobalBasis<PB>>;
605 };
606
607 template <class G, class PBF>
608 struct DeducedProblemTraits<G,PBF,
609 std::enable_if_t<Concepts::PreBasisFactory<PBF, typename G::LeafGridView>>>
610 {
611 using Grid = AdaptiveGrid_t<G>;
612 using GridView = typename Grid::LeafGridView;
613 using Basis = decltype(GlobalBasis{std::declval<GridView>(),std::declval<PBF>()});
614
615 using type = DefaultProblemTraits<Basis>;
616 };
617
618 template <class Grid, class Basis>
619 using DeducedProblemTraits_t = typename DeducedProblemTraits<Grid,Basis>::type;
620 }
621
622
623 // Deduction guide
624 template <class Grid, class Basis>
625 ProblemStat(std::string name, Grid&& grid, Basis&& globalBasis)
626 -> ProblemStat<Impl::DeducedProblemTraits_t<Underlying_t<Grid>,Underlying_t<Basis>>>;
627
628} // end namespace AMDiS
629
630#include "ProblemStat.inc.hpp"
Holds adapt parameters and infos about the problem.
Definition AdaptInfo.hpp:26
Definition BiLinearForm.hpp:31
Class defining a subset of a domain boundary.
Definition BoundarySubset.hpp:24
The basic container that stores a base vector and a corresponding basis.
Definition DOFVector.hpp:43
The Flag class encapsulates flags which represents simple information. Used e.g. while mesh traversal...
Definition Flag.hpp:14
Global basis defined on a pre-basis.
Definition GlobalBasis.hpp:47
typename PreBasis::GridView GridView
The grid view that the FE space is defined on.
Definition GlobalBasis.hpp:56
The basic container that stores a base vector and a corresponding basis.
Definition LinearForm.hpp:29
Definition LinearSolverInterface.hpp:9
Implementation of LinearSolverInterface for EIGEN solvers.
Definition LinearSolver.hpp:16
Base class for all markers.
Definition Marker.hpp:29
std::string const & name() const
Returns name_ of the Marker.
Definition Marker.hpp:81
The base class for an operator to be used in an Assembler.
Definition Operator.hpp:80
Standard implementation of ProblemTimeInterface for a time dependent problems.
Definition ProblemInstat.hpp:26
Interface for time independent problems. Concrete problems must override all pure virtual methods....
Definition ProblemStatBase.hpp:59
Definition ProblemStat.hpp:56
void setSolver(Solver_ &&solver)
Set a new linear solver for the problem.
Definition ProblemStat.hpp:442
void removeMarker(Marker< Grid > const &marker)
Remove a marker from the problem.
Definition ProblemStat.hpp:483
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:93
void addFileWriter(FileWriter_ &&f)
Add another filewriter to the problem.
Definition ProblemStat.hpp:490
std::shared_ptr< Grid > grid_
Grid of this problem.
Definition ProblemStat.hpp:548
static constexpr int dim
Dimension of the grid.
Definition ProblemStat.hpp:71
void addMatrixOperator(Operator const &op, RowTreePath row={}, ColTreePath col={})
Add an operator to A.
Definition ProblemStat.hpp:162
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:120
std::shared_ptr< SystemMatrix > systemMatrix()
Returns a reference to system-matrix, systemMatrix_.
Definition ProblemStat.hpp:401
void addMarker(Marker_ &&m)
Store the shared_ptr and the name of the marker in the problem.
Definition ProblemStat.hpp:467
void removeMarker(std::string name)
Remove a marker with the given name from the problem.
Definition ProblemStat.hpp:476
Flag oneIteration(AdaptInfo &adaptInfo, Flag toDo=FULL_ITERATION) override
Implementation of StandardProblemIteration::oneIteration.
Definition ProblemStat.hpp:334
ProblemStat(std::string const &name, Grid_ &&grid)
Definition ProblemStat.hpp:100
std::shared_ptr< BoundaryManager< Grid > > boundaryManager()
Return the boundary manager to identify boundary segments.
Definition ProblemStat.hpp:389
std::string name_
Name of this problem.
Definition ProblemStat.hpp:545
std::list< BoundaryCondition< SystemMatrix, SolutionVector, SystemVector > > constraints_
List of constraints to apply to matrix, solution and rhs.
Definition ProblemStat.hpp:586
void addVectorOperator(BoundaryType b, Operator const &op, TreePath path={})
Operator evaluated on the boundary of the domain with boundary index b
Definition ProblemStat.hpp:253
void initialize(Flag initFlag, Self *adoptProblem=nullptr, Flag adoptFlag=INIT_NOTHING)
Initialisation of the problem.
Definition ProblemStat.inc.hpp:23
void estimate(AdaptInfo &) override
Implementation of ProblemStatBase::estimate.
Definition ProblemStat.hpp:358
std::shared_ptr< GlobalBasis > globalBasis_
FE space of this problem.
Definition ProblemStat.hpp:557
void setGrid(Grid_ &&grid)
Definition ProblemStat.hpp:452
ProblemStat(std::string const &name, Grid_ &&grid, Basis_ &&globalBasis)
Constructor taking a grid and basis. Wraps both in shared pointers.
Definition ProblemStat.hpp:110
void assemble(AdaptInfo &adaptInfo)
Assemble the linear system by calling buildAfterAdapt with asmMatrix and asmVector set to true.
Definition ProblemStat.hpp:347
void restore(Flag initFlag)
Read the grid and solution from backup files and initialize the problem.
Definition ProblemStat.inc.hpp:108
std::list< std::shared_ptr< FileWriterInterface > > filewriter_
A FileWriter object.
Definition ProblemStat.hpp:560
GridView gridView() const
Return the gridView of the basis.
Definition ProblemStat.hpp:386
std::shared_ptr< SystemMatrix > systemMatrix_
Matrix that is filled during assembling.
Definition ProblemStat.hpp:572
std::shared_ptr< Grid > grid()
Return the grid_.
Definition ProblemStat.hpp:382
std::shared_ptr< LinearSolverInterface > solver()
Return a reference to the linear solver, linearSolver.
Definition ProblemStat.hpp:397
auto solution(Indices... ii)
Return a mutable view to a solution component.
Definition ProblemStat.hpp:419
std::string const & name() const override
Implementation of ProblemStatBase::name.
Definition ProblemStat.hpp:379
std::shared_ptr< SolutionVector > solution_
Vector with the solution components.
Definition ProblemStat.hpp:575
auto solution(Indices... ii) const
Return a const view to a solution component.
Definition ProblemStat.hpp:431
void addVectorOperator(Operator const &op, TreePath path={})
Add an operator to rhs.
Definition ProblemStat.hpp:225
static constexpr int dow
Dimension of the world.
Definition ProblemStat.hpp:74
std::map< std::string, std::shared_ptr< Marker< Grid > > > marker_
Pointer to the adaptation markers.
Definition ProblemStat.hpp:563
std::shared_ptr< SystemVector > rhs_
Definition ProblemStat.hpp:579
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:193
void clearFileWriter()
Deletes all filewriters.
Definition ProblemStat.hpp:496
std::shared_ptr< LinearSolverInterface > linearSolver_
Pointer to the estimators for this problem.
Definition ProblemStat.hpp:569
std::map< std::string, std::vector< double > > estimates_
Definition ProblemStat.hpp:583
std::shared_ptr< SolutionVector > solutionVector()
Returns a reference to the solution vector, solution_.
Definition ProblemStat.hpp:405
std::shared_ptr< BoundaryManager< Grid > > boundaryManager_
Management of boundary conditions.
Definition ProblemStat.hpp:554
std::shared_ptr< SystemVector > rhsVector()
Return a reference to the rhs system-vector, rhs.
Definition ProblemStat.hpp:409
std::shared_ptr< GlobalBasis > globalBasis()
Return the globalBasis_.
Definition ProblemStat.hpp:393
StandardProblemIteration when derived from ProblemStat.
Definition StandardProblemIteration.hpp:73
constexpr bool GlobalBasis
A Dune::Functions::GlobalBasis type.
Definition Concepts.hpp:189
Wrapper around a global basis providing default traits.
Definition ProblemStatTraits.hpp:77
Definition Assembler.hpp:15