version 0.4.1
resultfunction.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2024 The Ikarus Developers mueller@ibb.uni-stuttgart.de
2// SPDX-License-Identifier: LGPL-3.0-or-later
3
11#pragma once
12
13#include <type_traits>
14
15#include <dune/vtk/function.hh>
16#include <dune/vtk/vtkwriter.hh>
17
20
21namespace Ikarus {
22namespace Impl {
23 struct DefaultUserFunction
24 {
25 };
26} // namespace Impl
27
48template <typename AS, template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction>
49requires(Concepts::FlatAssembler<AS> and Concepts::ResultType<RT>)
50class ResultFunction : public Dune::VTKFunction<typename AS::GridView>
51{
52public:
53 using Assembler = AS;
54 using GridView = typename Assembler::GridView;
55 using FERequirementType = typename Assembler::FERequirement;
56 using FEContainer = typename Assembler::FEContainer;
57 using FiniteElement = typename std::remove_cvref_t<FEContainer>::value_type;
58
59 using ctype = typename GridView::ctype;
60 constexpr static int griddim = GridView::dimension;
61 using Entity = typename GridView::template Codim<0>::Entity;
62
73 double evaluate(int comp, const Entity& e, const Dune::FieldVector<ctype, griddim>& local) const override {
74 const auto index = gridView().indexSet().index(e);
75 return evaluateComponent(index, local, comp);
76 }
77
85 [[nodiscard]] int ncomps() const override {
86 if constexpr (std::is_same_v<UserFunction, Impl::DefaultUserFunction>) {
88 auto sigma = finiteElements().at(0).template calculateAt<RT>(requirement(), val).asVec();
89 return static_cast<int>(sigma.size());
90 } else
91 return userFunction_.ncomps();
92 }
93
101 [[nodiscard]] constexpr std::string name() const override {
102 if constexpr (requires { userFunction_.name(); })
103 return userFunction_.name();
104 else
105 return toString<RT>();
106 }
107
116 Dune::VTK::Precision precision() const override { return prec_; }
117
128 ResultFunction(std::shared_ptr<Assembler> assembler, Dune::VTK::Precision prec = Dune::VTK::Precision::float64)
129 : assembler_(assembler),
130 prec_{prec},
131 userFunction_{UserFunction{}} {}
132
133private:
134 double evaluateComponent(int eleID, const Dune::FieldVector<ctype, griddim>& local, int comp) const {
135 auto result = finiteElements().at(eleID).template calculateAt<RT>(requirement(), local).asVec();
136
137 if constexpr (!std::is_same_v<UserFunction, Impl::DefaultUserFunction>)
138 return userFunction_(result, comp);
139 else
140 return result(comp);
141 }
142
143 const FEContainer& finiteElements() const { return assembler_->finiteElements(); }
144 const FERequirementType& requirement() const { return assembler_->requirement(); }
145 const GridView& gridView() const { return assembler_->gridView(); }
146
147 std::shared_ptr<Assembler> assembler_;
148
149 Dune::VTK::Precision prec_;
150 [[no_unique_address]] std::string name_{};
151 UserFunction userFunction_;
152};
153
166template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction, typename AS>
167auto makeResultFunction(std::shared_ptr<AS> assembler, Dune::VTK::Precision prec = Dune::VTK::Precision::float64) {
168 return std::make_shared<ResultFunction<AS, RT, UserFunction>>(assembler, prec);
169}
170
186template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction, typename AS>
187auto makeResultVtkFunction(std::shared_ptr<AS> assembler) {
188 return Dune::Vtk::Function<typename AS::GridView>(std::make_shared<ResultFunction<AS, RT, UserFunction>>(assembler));
189}
190
191} // namespace Ikarus
Several concepts.
Definition of the LinearElastic class for finite element mechanics computations.
Definition: assemblermanipulatorbuildingblocks.hh:22
auto makeResultVtkFunction(std::shared_ptr< AS > assembler)
Function to create a ResultFunction as a gridfunction that can be used with dune-vtk.
Definition: resultfunction.hh:187
auto makeResultFunction(std::shared_ptr< AS > assembler, Dune::VTK::Precision prec=Dune::VTK::Precision::float64)
Function to create a ResultFunction as a shared_ptr.
Definition: resultfunction.hh:167
Wrapper to evaluate results for a vtkwriter.
Definition: resultfunction.hh:51
typename Assembler::FERequirement FERequirementType
Definition: resultfunction.hh:55
int ncomps() const override
Get the number of components.
Definition: resultfunction.hh:85
typename Assembler::FEContainer FEContainer
Definition: resultfunction.hh:56
typename GridView::template Codim< 0 >::Entity Entity
Definition: resultfunction.hh:61
typename std::remove_cvref_t< FEContainer >::value_type FiniteElement
Definition: resultfunction.hh:57
typename GridView::ctype ctype
Definition: resultfunction.hh:59
Dune::VTK::Precision precision() const override
Get the precision used for this result.
Definition: resultfunction.hh:116
constexpr std::string name() const override
Get the name of the result type.
Definition: resultfunction.hh:101
AS Assembler
Definition: resultfunction.hh:53
ResultFunction(std::shared_ptr< Assembler > assembler, Dune::VTK::Precision prec=Dune::VTK::Precision::float64)
Constructor for ResultFunction.
Definition: resultfunction.hh:128
double evaluate(int comp, const Entity &e, const Dune::FieldVector< ctype, griddim > &local) const override
Evaluate the component at a given entity and local coordinates.
Definition: resultfunction.hh:73
typename Assembler::GridView GridView
Definition: resultfunction.hh:54
Definition: utils/dirichletvalues.hh:32