KASKADE 7 development version
Loading...
Searching...
No Matches
vtk.hh
Go to the documentation of this file.
1/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2/* */
3/* This file is part of the library KASKADE 7 */
4/* https://www.zib.de/research/projects/kaskade7-finite-element-toolbox */
5/* */
6/* Copyright (C) 2002-2025 Zuse Institute Berlin */
7/* */
8/* KASKADE 7 is distributed under the terms of the ZIB Academic License. */
9/* see $KASKADE/academic.txt */
10/* */
11/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
12
13#ifndef VTK_HH
14#define VTK_HH
15
17#include "io/iobase.hh"
18#include "io/vtkwriter.hh"
19#include "fem/variables.hh"
21
37
38//-------------------------------------------------------------------------------------------------
39
40namespace Kaskade
41{
42
54 template <class Function>
55 void writeVTK(Function const& f, std::string const& filename, IoOptions options, std::string fname)
56 {
57 using namespace boost::fusion;
58
59 if (fname.empty())
60 fname = filename;
61
62 // Construct a variable set (trivially consisting of only one function) to be handed to the
63 // VTK writer.
64 auto vsDescription = makeVariableSetDescription(
65 makeSpaceList(&f.space()),
66 make_vector(Variable<SpaceIndex<0>,
68 auto fs = vsDescription.variableSet();
69 component<0>(fs) = f;
70
71 // write the VTK file
72 writeVTK(fs,filename,options);
73 }
74
75 //-----------------------------------------------------------------------------------------------
76
78 {
79 template <class Spaces, class Variables, class Functions>
80 auto makeVariableSet(Spaces spaces, Variables vars, Functions fs)
81 {
82 // Create variable set description and variable set itself
83 auto varSetDesc = makeVariableSetDescription(spaces,vars);
84
85
86 return std::pair(varSetDesc,fs);
87 }
88
89 template <class Spaces, class Variables, class Functions, class Function, typename... Args>
90 auto makeVariableSet(Spaces spaces, Variables vars, Functions fs,
91 Function const& f, std::string name, Args const&... args)
92 {
93 namespace bf = boost::fusion;
94
95 // We create a trivial space list here: Every function has its own space. This
96 // prevents some evaluation optimizations (a single evaluator used for multiple functions
97 // on the same space), but this is not exploited in VTK writer anyways. Thus, the
98 // variable id and its space index always coincide.
99 auto newFs = bf::as_vector(bf::push_back(fs,&f));
100
101 auto newSpaces = bf::as_vector(bf::push_back(spaces,&f.space()));
102
103 constexpr int index = bf::size(spaces);
105 auto newVars = bf::as_vector(bf::push_back(vars,newVar));
106
107 return makeVariableSet(newSpaces,newVars,newFs,args...);
108 }
109 }
110
111 template <class Function, typename ...Args>
112 void writeVTK(std::string const& filename, IoOptions options,
113 Function const& f, std::string name, Args const&... args)
114 {
115 namespace bf = boost::fusion;
116
117 // Create variable set description and function list
118 auto [varSetDesc,fs] = VTKWriterDetail::makeVariableSet(bf::vector<>(),bf::vector<>(),
119 bf::vector<>(),
120 f, std::move(name), args...);
121 auto fsCopy = fs; // required for passing to lambda below
122
123 using VarSetDesc = decltype(varSetDesc);
124
125 // Create variable set itself
126 auto varSet = varSetDesc.variableSet();
127
128 // Assign functions to variable set
129 bf::for_each(typename VarSetDesc::Variables(),[&](auto const& var)
130 {
131 component<var.id>(varSet) = *bf::at_c<var.id>(fsCopy);
132 });
133
134 // Perform the VTK output
135 writeVTK(varSet,filename,options);
136 }
137
138 //-----------------------------------------------------------------------------------------------
139
149 {
150 public:
151 TimeSeriesVTKWriter(std::string const& basename, IoOptions options_= ioOptions_default)
152 : base(basename), pvd((basename+".pvd").c_str()), count(0), options(options_)
153 {
154 pvd << "<?xml version=\"1.0\"?>\n"
155 << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << VTKWriterDetail::vtkEndianness() << "\">\n"
156 << " <Collection>\n";
157 }
158
162 template <class VariableSet>
163 void add(double time, VariableSet const& vars)
164 {
165 auto fname = base+"-"+paddedString(count,4);
166 writeVTK(vars,fname,options);
167
168 pvd << " <DataSet timestep=\"" << time << "\" file=\"" << fname << ".vtu\" />\n";
169 ++count;
170 }
171
173 {
174 pvd << " </Collection>\n"
175 << "</VTKFile>\n";
176 }
177
178 private:
179 std::string base;
180 std::ofstream pvd;
181 int count;
182 IoOptions options;
183 };
184
185
186 //---------------------------------------------------------------------
187 //---------------------------------------------------------------------
188
189
190
191
192} // end namespace Kaskade
193//---------------------------------------------------------------------
194
195#endif
Function is the interface for evaluatable functions .
Definition concepts.hh:324
void add(double time, VariableSet const &vars)
Adds frame with values given by vars at time time to the series.
Definition vtk.hh:163
TimeSeriesVTKWriter(std::string const &basename, IoOptions options_=ioOptions_default)
Definition vtk.hh:151
A class for storing a heterogeneous collection of FunctionSpaceElement s.
Definition variables.hh:337
int count(Cell const &cell, int codim)
Computes the number of subentities of codimension c of a given entity.
Definition fixdune.hh:667
std::string paddedString(int n, int places=3)
creates a zero-padded string representation of the given number
void writeVTK(Function const &f, std::string const &filename, IoOptions options, std::string fname)
Writes a single finite element function to a VTK file.
Definition vtk.hh:55
auto makeSpaceList(Spaces *... spaces)
Creates a list of finite element spaces in the required format. Usage example (for Stokes flow):
Definition variables.hh:923
VariableSetDescription< SpaceList, VariableList > makeVariableSetDescription(SpaceList const &spaces, VariableList const &vars)
Creates a variable set description from given spaces and variables.
Definition variables.hh:942
Output of mesh and solution for visualization software.
auto makeVariableSet(Spaces spaces, Variables vars, Functions fs)
Definition vtk.hh:80
IoOptions ioOptions_default
auto & component(LinearProductSpace< Scalar, Sequence > &x)
Helper class for specifying the number of components of a variable.
Definition variables.hh:100
options for VTK/AMIRA output
Definition iobase.hh:77
Helper class for specifying the FE space index of a variable.
Definition variables.hh:92
A class defining elementary information about a single variable.
Definition variables.hh:121
Variables and their descriptions.