version 0.4.1
io/vtkwriter.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 "vtkdatatag.hh"
14
15#include <array>
16#include <tuple>
17
18#include "dune/functions/functionspacebases/powerbasis.hh"
19#include "dune/functions/functionspacebases/subspacebasis.hh"
20#include <dune/common/fvector.hh>
21#include <dune/functions/gridfunctions/discreteglobalbasisfunction.hh>
22#include <dune/grid/yaspgrid.hh>
23#include <dune/vtk/vtkwriter.hh>
24#include <dune/vtk/writers/unstructuredgridwriter.hh>
25
28
29namespace Ikarus::Vtk {
30
31namespace Impl {
32 template <typename Container>
33 constexpr auto sizeOfContainer = []() {
34 if constexpr (requires { std::tuple_size<Container>::value; })
35 return std::tuple_size<Container>::value;
36 else
37 return 1ul;
38 }();
39
40 template <class PreBasis>
41 struct ResultContainerPre
42 {
43 using type = double;
44 };
45
46 template <class Basis>
47 struct ResultContainer
48 {
49 using type = ResultContainerPre<typename Basis::PreBasis>::type;
50 };
51
52 template <class RB, class PP>
53 struct ResultContainer<Dune::Functions::SubspaceBasis<RB, PP>>
54 {
55 using type = double;
56 };
57
58 template <class Basis>
59 using ResultContainer_t = typename ResultContainer<Basis>::type;
60
61 // specialization for power basis
62 template <class IMS, class SPB, std::size_t size>
63 struct ResultContainerPre<Dune::Functions::PowerPreBasis<IMS, SPB, size>>
64 {
65 using type = std::array<typename ResultContainerPre<SPB>::type, size>;
66 };
67
68} // namespace Impl
69
78template <typename AS, typename DC, typename Base>
79requires(Concepts::FlatAssembler<AS> && Concepts::DataCollector<DC>)
80struct Writer : public Base
81{
82public:
83 using Assembler = AS;
84 using GridView = typename Assembler::GridView;
85 using FERequirement = typename Assembler::FERequirement;
86 using FEContainer = typename Assembler::FEContainer;
87 using FEType = typename std::remove_cvref_t<FEContainer>::value_type;
88
89 using DataCollector = DC;
90 using VTKWriter = Base;
91
98 template <class... Args>
99 Writer(std::shared_ptr<AS> assembler, Args... args)
100 : Base(assembler->gridView(), std::forward<Args>(args)...),
101 assembler_(assembler) {}
102
110 template <typename DC_, class... Args>
112 Writer(std::shared_ptr<AS> assembler, DC_&& dc, Args... args)
113 : Base(std::forward<std::decay_t<DC_>>(dc), std::forward<Args>(args)...),
114 assembler_(assembler) {}
115
123 template <typename RF>
124 void addResultFunction(RF&& resultFunction, DataTag dataTag) {
125 if (dataTag == DataTag::asCellData or dataTag == DataTag::asCellAndPointData)
126 Base::addCellData(std::forward<RF>(resultFunction));
127 if (dataTag == DataTag::asPointData or dataTag == DataTag::asCellAndPointData)
128 Base::addPointData(std::forward<RF>(resultFunction));
129 }
130
137 template <template <typename, int, int> class RT>
140 auto resFunction = makeResultVtkFunction<RT>(assembler_);
141 addResultFunction(std::move(resFunction), dataTag);
142 }
143
150 using ResultTuple = typename FEType::SupportedResultTypes;
151
152 Dune::Hybrid::forEach(ResultTuple(), [&]<typename RT>(RT i) { addResult<RT::template Rebind>(dataTag); });
153 }
154
168 template <typename Basis, typename R>
169 void addInterpolation(R&& vals, const Basis& basis, const std::string& name, DataTag dataTag = DataTag::asPointData) {
170 using Container = Impl::ResultContainer_t<Basis>;
171
172 auto gridFunction = Dune::Functions::makeDiscreteGlobalBasisFunction<Container>(basis, std::forward<R>(vals));
173 auto fieldInfo = Dune::Vtk::FieldInfo(name, Impl::sizeOfContainer<Container>);
174
175 if (dataTag == DataTag::asCellData or dataTag == DataTag::asCellAndPointData)
176 Base::addCellData(std::move(gridFunction), fieldInfo);
177 if (dataTag == DataTag::asPointData or dataTag == DataTag::asCellAndPointData)
178 Base::addPointData(std::move(gridFunction), fieldInfo);
179 }
180
181private:
182 std::shared_ptr<Assembler> assembler_;
183};
184
190template <typename G>
191struct IsStructured : std::false_type
192{
193};
194
198template <int dim, typename Coordinates>
199struct IsStructured<Dune::YaspGrid<dim, Coordinates>> : std::true_type
200{
201};
202
208template <typename GV>
211{
214 std::conditional_t<isStructured, Dune::Vtk::YaspDataCollector<GV>, Dune::Vtk::ContinuousDataCollector<GV>>;
215
216 template <typename DC = DefaultDataCollector>
217 using DefaultVTKWriter = std::conditional_t<isStructured, Dune::Vtk::RectilinearGridWriter<typename DC::GridView, DC>,
218 Dune::Vtk::UnstructuredGridWriter<typename DC::GridView, DC>>;
219};
220
221// Class template argument deduction guides for VTK::Writer
222
223template <typename AS, class... Args>
225Writer(std::shared_ptr<AS>,
228
229template <typename AS, typename DC, class... Args, Dune::Vtk::IsDataCollector<std::decay_t<DC>> = true>
231Writer(std::shared_ptr<AS>, DC&&, Args...)
233 typename DefaultVTKWriterManager<typename AS::GridView>::template DefaultVTKWriter<std::decay_t<DC>>>;
234
235} // namespace Ikarus::Vtk
Several concepts.
Ikarus Result Function for Stress and other finite element results.
STL namespace.
Definition: vtkdatatag.hh:8
Writer(std::shared_ptr< AS >, Args...) -> Writer< AS, typename DefaultVTKWriterManager< typename AS::GridView >::DefaultDataCollector, typename DefaultVTKWriterManager< typename AS::GridView >::template DefaultVTKWriter<> >
DataTag
Tag enum indicating cell data or point data.
Definition: vtkdatatag.hh:13
Definition: utils/dirichletvalues.hh:30
def basis(gv, tree)
Definition: basis.py:10
Manages writing results using VTK, based on assembler and data collector.
Definition: io/vtkwriter.hh:81
void addAllResults(DataTag dataTag=DataTag::asPointData)
Adds all results for the given data tag.
Definition: io/vtkwriter.hh:149
Writer(std::shared_ptr< AS > assembler, DC_ &&dc, Args... args)
Constructor with assembler, data collector, and additional arguments.
Definition: io/vtkwriter.hh:112
typename std::remove_cvref_t< FEContainer >::value_type FEType
Definition: io/vtkwriter.hh:87
void addInterpolation(R &&vals, const Basis &basis, const std::string &name, DataTag dataTag=DataTag::asPointData)
Adds interpolation data for the given basis and container.
Definition: io/vtkwriter.hh:169
AS Assembler
Definition: io/vtkwriter.hh:83
DC DataCollector
Definition: io/vtkwriter.hh:89
Base VTKWriter
Definition: io/vtkwriter.hh:90
Writer(std::shared_ptr< AS > assembler, Args... args)
Constructor with assembler and additional arguments.
Definition: io/vtkwriter.hh:99
void addResult(DataTag dataTag=DataTag::asPointData)
Adds a result for the given data tag.
Definition: io/vtkwriter.hh:139
void addResultFunction(RF &&resultFunction, DataTag dataTag)
Adds a result function for the given data tag.
Definition: io/vtkwriter.hh:124
typename Assembler::FEContainer FEContainer
Definition: io/vtkwriter.hh:86
typename Assembler::GridView GridView
Definition: io/vtkwriter.hh:84
typename Assembler::FERequirement FERequirement
Definition: io/vtkwriter.hh:85
Meta type to check whether a grid is structured, inherits from false_type.
Definition: io/vtkwriter.hh:192
Manages the default template parameter for the Vtk::Writer
Definition: io/vtkwriter.hh:211
static constexpr bool isStructured
Definition: io/vtkwriter.hh:212
std::conditional_t< isStructured, Dune::Vtk::RectilinearGridWriter< typename DC::GridView, DC >, Dune::Vtk::UnstructuredGridWriter< typename DC::GridView, DC > > DefaultVTKWriter
Definition: io/vtkwriter.hh:218
std::conditional_t< isStructured, Dune::Vtk::YaspDataCollector< GV >, Dune::Vtk::ContinuousDataCollector< GV > > DefaultDataCollector
Definition: io/vtkwriter.hh:214
A concept to check if a template type satisfies the ResultType requirements.
Definition: concepts.hh:488
Concept representing the requirements for a FlatAssembler.A type T satisfies FlatAssembler if it prov...
Definition: concepts.hh:500
Definition: concepts.hh:572
Definition: concepts.hh:581