version 0.4
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/vtkwriter.hh>
17
21
22namespace Ikarus {
23 namespace Impl {
24 struct DefaultUserFunction {};
25 } // namespace Impl
26
47 template <typename ElementType_, typename UserFunction = Impl::DefaultUserFunction>
48 class ResultFunction : public Dune::VTKFunction<typename ElementType_::GridView> {
49 public:
50 using ElementType = ElementType_;
51 using ResultRequirements = typename ElementType::ResultRequirementsType;
52 using GridView = typename ElementType::GridView;
53 using ctype = typename GridView::ctype;
54 constexpr static int griddim = GridView::dimension;
55 typedef typename GridView::template Codim<0>::Entity Entity;
56
67 double evaluate(int comp, const Entity& e, const Dune::FieldVector<ctype, griddim>& local) const override {
68 auto index = gridView.indexSet().index(e);
69 return evaluateComponent(index, local, comp);
70 }
71
79 [[nodiscard]] int ncomps() const override {
80 if constexpr (std::is_same_v<UserFunction, Impl::DefaultUserFunction>) {
82
83 fes_->at(0).calculateAt(resultRequirements_, val, resultTypeMap);
84 if (resultRequirements_.getRequestedResult() != resultTypeMap.getSingleResult().first)
85 DUNE_THROW(Dune::InvalidStateException, "The return result should be the requested one");
86
87 auto sigma = resultTypeMap.getSingleResult().second;
88
89 return static_cast<int>(sigma.rows() * sigma.cols());
90 } else
91 return userFunction_.ncomps();
92 }
93
101 [[nodiscard]] constexpr std::string name() const override {
102 if constexpr (std::is_same_v<UserFunction, Impl::DefaultUserFunction>)
103 return toString(resultRequirements_.getRequestedResult());
104 else
105 return userFunction_.name();
106 }
107
118 ResultFunction(std::vector<ElementType>* fes, const ResultRequirements& req, UserFunction userFunction = {})
119 : gridView{fes->at(0).localView().globalBasis().gridView()},
120 resultRequirements_{req},
121 fes_{fes},
122 userFunction_{userFunction} {
123 if constexpr (!std::is_same_v<UserFunction, Impl::DefaultUserFunction>) userFunction_ = userFunction;
124 }
125
126 private:
127 double evaluateComponent(int eleID, const Dune::FieldVector<ctype, griddim>& local, int comp) const {
128 if constexpr (!std::is_same_v<UserFunction, Impl::DefaultUserFunction>)
129 return userFunction_(fes_->at(eleID), resultRequirements_, local, comp);
130 else {
131 fes_->at(eleID).calculateAt(resultRequirements_, local, resultTypeMap);
132 auto result = resultTypeMap.getSingleResult().second;
133 return result(comp);
134 }
135 }
136
137 GridView gridView;
138 ResultRequirements resultRequirements_;
139 std::vector<ElementType>* fes_;
140 mutable ResultTypeMap<ctype> resultTypeMap;
141 [[no_unique_address]] std::string name_{};
142 UserFunction userFunction_;
143 };
144} // namespace Ikarus
Helper for transform between Dune linear algebra types and Eigen.
Definition of the LinearElastic class for finite element mechanics computations.
Ikarus Result Evaluators for Stress Analysis.
Definition: simpleassemblers.hh:21
constexpr std::string toString(ScalarAffordances _e)
Definition: ferequirements.hh:34
auto & getSingleResult()
Get the result array for a single result type.
Definition: ferequirements.hh:379
Definition: resultevaluators.hh:20
Wrapper to evaluate results for a vtkwriter.
Definition: resultfunction.hh:48
static constexpr int griddim
Definition: resultfunction.hh:54
typename GridView::ctype ctype
Definition: resultfunction.hh:53
int ncomps() const override
Get the number of components.
Definition: resultfunction.hh:79
typename ElementType::GridView GridView
Definition: resultfunction.hh:52
ResultFunction(std::vector< ElementType > *fes, const ResultRequirements &req, UserFunction userFunction={})
Constructor for ResultFunction.
Definition: resultfunction.hh:118
GridView::template Codim< 0 >::Entity Entity
Definition: resultfunction.hh:55
typename ElementType::ResultRequirementsType ResultRequirements
Definition: resultfunction.hh:51
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:67
constexpr std::string name() const override
Get the name of the result type.
Definition: resultfunction.hh:101
ElementType_ ElementType
Definition: resultfunction.hh:50