version 0.4.1
resultfunction.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2021-2025 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 {
32 template <typename R, typename FiniteElement, int dim>
33 double operator()(const R& resultArray, const Dune::FieldVector<double, dim>& pos, const FiniteElement& fe,
34 const int comp) const {
35 return resultArray[comp];
36 };
37 };
38} // namespace Impl
39
60template <typename AS, template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction>
61requires(Concepts::FlatAssembler<AS> and Concepts::ResultType<RT>)
62class ResultFunction : public Dune::VTKFunction<typename AS::GridView>
63{
64public:
65 using Assembler = AS;
66 using GridView = typename Assembler::GridView;
67 using FERequirementType = typename Assembler::FERequirement;
68 using FEContainer = typename Assembler::FEContainer;
69 using FiniteElement = typename std::remove_cvref_t<FEContainer>::value_type;
70
71 using ctype = typename GridView::ctype;
72 constexpr static int griddim = GridView::dimension;
73 using Entity = typename GridView::template Codim<0>::Entity;
74
85 double evaluate(int comp, const Entity& e, const Dune::FieldVector<ctype, griddim>& local) const override {
86 const auto index = gridView().indexSet().index(e);
87 return evaluateComponent(index, local, comp);
88 }
89
97 [[nodiscard]] int ncomps() const override {
98 if constexpr (std::is_same_v<UserFunction, Impl::DefaultUserFunction>) {
100 auto sigma = finiteElements().at(0).template calculateAt<RT>(requirement(), val).asVec();
101 return static_cast<int>(sigma.size());
102 } else
103 return userFunction_.ncomps();
104 }
105
113 [[nodiscard]] constexpr std::string name() const override {
114 if constexpr (requires { userFunction_.name(); })
115 return userFunction_.name();
116 else
117 return toString<RT>();
118 }
119
128 Dune::VTK::Precision precision() const override { return prec_; }
129
140 template <typename UF = UserFunction>
141 ResultFunction(std::shared_ptr<Assembler> assembler, Dune::VTK::Precision prec = Dune::VTK::Precision::float64,
142 UF&& userFunction = {})
143 : assembler_(assembler),
144 prec_{prec},
145 userFunction_{std::forward<UF>(userFunction)} {}
146
147private:
148 double evaluateComponent(int eleID, const Dune::FieldVector<ctype, griddim>& local, int comp) const {
149 const auto& fe = finiteElements().at(eleID);
150 auto result = fe.template calculateAt<RT>(requirement(), local).asVec();
151
152 return userFunction_(result, local, fe, comp);
153 }
154
155 const FEContainer& finiteElements() const { return assembler_->finiteElements(); }
156 const FERequirementType& requirement() const { return assembler_->requirement(); }
157 const GridView& gridView() const { return assembler_->gridView(); }
158
159 std::shared_ptr<Assembler> assembler_;
160
161 Dune::VTK::Precision prec_;
162 UserFunction userFunction_;
163};
164
178template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction,
179 Concepts::FlatAssembler AS>
180auto makeResultFunction(std::shared_ptr<AS> assembler, Dune::VTK::Precision prec = Dune::VTK::Precision::float64,
181 UserFunction&& userFunction = {}) {
182 return std::make_shared<ResultFunction<AS, RT, UserFunction>>(assembler, prec,
183 std::forward<UserFunction>(userFunction));
184}
185
196template <template <typename, int, int> class RT, typename UserFunction, Concepts::FlatAssembler AS>
197auto makeResultFunction(std::shared_ptr<AS> assembler, UserFunction&& userFunction) {
198 return makeResultFunction<RT>(assembler, Dune::VTK::Precision::float64, std::forward<UserFunction>(userFunction));
199}
200
217template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction,
218 Concepts::FlatAssembler AS>
219auto makeResultVtkFunction(std::shared_ptr<AS> assembler, UserFunction&& userFunction = {}) {
220 return Dune::Vtk::Function<typename AS::GridView>(std::make_shared<ResultFunction<AS, RT, UserFunction>>(
221 assembler, Dune::VTK::Precision::float64, std::forward<UserFunction>(userFunction)));
222}
223
224} // namespace Ikarus
Several concepts.
Definition of the LinearElastic class for finite element mechanics computations.
Definition: assemblermanipulatorbuildingblocks.hh:22
auto makeResultFunction(std::shared_ptr< AS > assembler, Dune::VTK::Precision prec=Dune::VTK::Precision::float64, UserFunction &&userFunction={})
Function to create a ResultFunction as a shared_ptr.
Definition: resultfunction.hh:180
auto makeResultVtkFunction(std::shared_ptr< AS > assembler, UserFunction &&userFunction={})
Function to create a ResultFunction as a gridfunction that can be used with dune-vtk.
Definition: resultfunction.hh:219
Wrapper to evaluate results for a vtkwriter.
Definition: resultfunction.hh:63
typename Assembler::FERequirement FERequirementType
Definition: resultfunction.hh:67
int ncomps() const override
Get the number of components.
Definition: resultfunction.hh:97
typename Assembler::FEContainer FEContainer
Definition: resultfunction.hh:68
typename GridView::template Codim< 0 >::Entity Entity
Definition: resultfunction.hh:73
typename std::remove_cvref_t< FEContainer >::value_type FiniteElement
Definition: resultfunction.hh:69
typename GridView::ctype ctype
Definition: resultfunction.hh:71
Dune::VTK::Precision precision() const override
Get the precision used for this result.
Definition: resultfunction.hh:128
ResultFunction(std::shared_ptr< Assembler > assembler, Dune::VTK::Precision prec=Dune::VTK::Precision::float64, UF &&userFunction={})
Construct a new Result Function object.
Definition: resultfunction.hh:141
constexpr std::string name() const override
Get the name of the result type.
Definition: resultfunction.hh:113
AS Assembler
Definition: resultfunction.hh:65
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:85
typename Assembler::GridView GridView
Definition: resultfunction.hh:66
Definition: utils/dirichletvalues.hh:32