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#include <utility>
15
16#include <dune/vtk/function.hh>
17#include <dune/vtk/vtkwriter.hh>
18
20
21namespace Ikarus {
22namespace Impl {
23 struct DefaultUserFunction
24 {
25 };
26} // namespace Impl
27
48template <typename FE, template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction>
49class ResultFunction : public Dune::VTKFunction<typename FE::GridView>
50{
51public:
55 using ctype = typename GridView::ctype;
56 constexpr static int griddim = GridView::dimension;
57 using Entity = typename GridView::template Codim<0>::Entity;
58
69 double evaluate(int comp, const Entity& e, const Dune::FieldVector<ctype, griddim>& local) const override {
70 auto index = gridView_.indexSet().index(e);
71 return evaluateComponent(index, local, comp);
72 }
73
81 [[nodiscard]] int ncomps() const override {
82 if constexpr (std::is_same_v<UserFunction, Impl::DefaultUserFunction>) {
84 auto sigma = fes_->at(0).template calculateAt<RT>(feRequirements_, val).asVec();
85 return static_cast<int>(sigma.size());
86 } else
87 return userFunction_.ncomps();
88 }
89
97 [[nodiscard]] constexpr std::string name() const override {
98 if constexpr (requires { userFunction_.name(); })
99 return userFunction_.name();
100 else
101 return toString<RT>();
102 }
103
112 ResultFunction(std::vector<FiniteElement>* fes, const FERequirementType& req)
113 : gridView_{fes->at(0).localView().globalBasis().gridView()},
114 feRequirements_{req},
115 fes_{fes},
116 userFunction_{UserFunction{}} {}
117
118private:
119 double evaluateComponent(int eleID, const Dune::FieldVector<ctype, griddim>& local, int comp) const {
120 auto result = fes_->at(eleID).template calculateAt<RT>(feRequirements_, local).asVec();
121
122 if constexpr (!std::is_same_v<UserFunction, Impl::DefaultUserFunction>)
123 return userFunction_(result, comp);
124 else
125 return result(comp);
126 }
127
128 GridView gridView_;
129 FERequirementType feRequirements_;
130 std::vector<FiniteElement>* fes_;
131 [[no_unique_address]] std::string name_{};
132 UserFunction userFunction_;
133};
134
147template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction, typename FE>
148auto makeResultFunction(std::vector<FE>* fes, const typename FE::FERequirementType& req) {
149 return std::make_shared<ResultFunction<FE, RT, UserFunction>>(fes, req);
150}
151
168template <template <typename, int, int> class RT, typename UserFunction = Impl::DefaultUserFunction, typename FE>
169auto makeResultVtkFunction(std::vector<FE>* fes, const typename FE::FERequirementType& req) {
170 return Dune::Vtk::Function<typename FE::GridView>(std::make_shared<ResultFunction<FE, RT, UserFunction>>(fes, req));
171}
172
173} // namespace Ikarus
Definition of the LinearElastic class for finite element mechanics computations.
Definition: simpleassemblers.hh:22
auto makeResultVtkFunction(std::vector< FE > *fes, const typename FE::FERequirementType &req)
Function to create a ResultFunction as a gridfunction that can be used with dune-vtk.
Definition: resultfunction.hh:169
auto makeResultFunction(std::vector< FE > *fes, const typename FE::FERequirementType &req)
Function to create a ResultFunction as a shared_ptr.
Definition: resultfunction.hh:148
FE class is a base class for all finite elements.
Definition: febase.hh:81
typename Traits::GridView GridView
Type of the global view.
Definition: febase.hh:92
typename Traits::FERequirementType FERequirementType
Definition: febase.hh:95
Wrapper to evaluate results for a vtkwriter.
Definition: resultfunction.hh:50
typename FiniteElement::FERequirementType FERequirementType
Definition: resultfunction.hh:53
constexpr std::string name() const override
Get the name of the result type.
Definition: resultfunction.hh:97
int ncomps() const override
Get the number of components.
Definition: resultfunction.hh:81
typename GridView::ctype ctype
Definition: resultfunction.hh:55
static constexpr int griddim
Definition: resultfunction.hh:56
typename FiniteElement::GridView GridView
Definition: resultfunction.hh:54
ResultFunction(std::vector< FiniteElement > *fes, const FERequirementType &req)
Constructor for ResultFunction.
Definition: resultfunction.hh:112
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:69
typename GridView::template Codim< 0 >::Entity Entity
Definition: resultfunction.hh:57
Definition: utils/dirichletvalues.hh:30