Dune Core Modules (unstable)

functionwriter.hh
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4// vi: set et ts=4 sw=2 sts=2:
5
6#ifndef DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
7#define DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
8
9#include <cstddef>
10#include <memory>
11#include <string>
12#include <typeinfo>
13#include <vector>
14
17
18#include <dune/geometry/referenceelements.hh>
19
22#include <dune/grid/io/file/vtk/pvtuwriter.hh>
23#include <dune/grid/io/file/vtk/vtuwriter.hh>
24
25namespace Dune
26{
29
30 namespace VTK {
31
33 template<typename Cell_>
35 typedef typename Cell_::Geometry::ctype DF;
36 static const unsigned mydim = Cell_::mydimension;
38
39 public:
41 typedef Cell_ Cell;
42
44 virtual std::string name() const = 0;
45
47 virtual unsigned ncomps() const = 0;
48
50 virtual void addArray(PVTUWriter& writer) = 0;
52 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) = 0;
54
59 virtual void write(const Cell& /* cell */, const Domain& /* xl */) {
60 DUNE_THROW(NotImplemented, "FunctionWriterBase::write(const Cell&, "
61 "const Domain&): Either the derived class " <<
62 typeid(*this).name() << " failed to implement this method "
63 "or this method is not meant to be called on the derived "
64 "class and was called in error.");
65 }
67
71 virtual void write(const Cell& cell, unsigned cornerIndex) {
72 write(cell,
73 Refelems::general(cell.type()).position(cornerIndex, mydim));
74 }
76 virtual void endWrite() = 0;
79 };
80
82 //
83 // A Generic Function writer for VTKFunctions
84 //
85
87 template<typename Func>
89 : public FunctionWriterBase<typename Func::Entity>
90 {
92 std::shared_ptr<const Func> func;
93 VTK::Precision precision_;
94 std::shared_ptr<DataArrayWriter> arraywriter;
95
96 public:
97 VTKFunctionWriter(const std::shared_ptr<const Func>& func_,
98 VTK::Precision prec = VTK::Precision::float32)
99 : func(func_), precision_(prec)
100 { }
101
103 virtual std::string name() const { return func->name(); }
104
106 virtual unsigned ncomps() const {
107 if(func->ncomps() == 2) return 3;
108 else return func->ncomps();
109 }
110
112 virtual void addArray(PVTUWriter& writer) {
113 writer.addArray(name(), ncomps(), precision_);
114 }
115
117 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
118 arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
119 nitems, precision_));
120 return !arraywriter->writeIsNoop();
121 }
122
124 virtual void write(const typename Base::Cell& cell,
125 const typename Base::Domain& xl) {
126 for(int d = 0; d < func->ncomps(); ++d)
127 arraywriter->write(func->evaluate(d, cell, xl));
128 for(unsigned d = func->ncomps(); d < ncomps(); ++d)
129 arraywriter->write(0);
130 }
131
133 virtual void endWrite() {
134 arraywriter.reset();
135 }
136 };
137
139 //
140 // Writers for the grid information
141 //
142
144 template<typename Cell>
146 : public FunctionWriterBase<Cell>
147 {
149
150 VTK::Precision precision_;
151 std::shared_ptr<DataArrayWriter> arraywriter;
152
153 public:
154 explicit CoordinatesWriter(VTK::Precision prec = VTK::Precision::float32)
155 : precision_(prec)
156 {}
157
159 virtual std::string name() const { return "Coordinates"; }
160
162 virtual unsigned ncomps() const { return 3; }
163
165 virtual void addArray(PVTUWriter& writer) {
166 writer.addArray(name(), ncomps(), precision_);
167 }
168
170 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
171 arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
172 nitems, precision_));
173 return !arraywriter->writeIsNoop();
174 }
176 virtual void write(const typename Base::Cell& cell,
177 const typename Base::Domain& xl) {
179 = cell.geometry().global(xl);
180 for(unsigned d = 0; d < 3 && d < Base::Cell::Geometry::coorddimension; ++d)
181 arraywriter->write(xg[d]);
182 for(unsigned d = Base::Cell::Geometry::coorddimension; d < 3; ++d)
183 arraywriter->write(0);
184 }
186 virtual void endWrite() {
187 arraywriter.reset();
188 }
189 };
190
192 template<typename IteratorFactory>
194 : public FunctionWriterBase<typename IteratorFactory::Cell>
195 {
197 static const unsigned mydim = Base::Cell::mydimension;
198
199 const IteratorFactory& factory;
200 std::shared_ptr<DataArrayWriter> arraywriter;
201 std::vector<unsigned> pointIndices;
202
203 public:
205 ConformingConnectivityWriter(const IteratorFactory& factory_)
206 : factory(factory_)
207 { }
208
210 virtual std::string name() const { return "connectivity"; }
211
213 virtual unsigned ncomps() const { return 1; }
214
216 virtual void addArray(PVTUWriter& writer) {
217 writer.addArray(name(), ncomps(), Precision::int32);
218 }
219
221 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
222 arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
223 nitems, Precision::int32));
224 if(arraywriter->writeIsNoop())
225 return false;
226
228 pointIndices.resize(factory.indexSet().size(mydim));
229 const typename IteratorFactory::PointIterator& pend =
230 factory.endPoints();
231 typename IteratorFactory::PointIterator pit = factory.beginPoints();
232 unsigned counter = 0;
233 while(pit != pend) {
234 pointIndices[factory.indexSet().subIndex
235 (pit->cell(), pit->duneIndex(), mydim)] = counter;
236 ++counter;
237 ++pit;
238 }
239 return true;
240 }
242 virtual void write(const typename Base::Cell& cell, unsigned cornerIndex)
243 {
244 // if pointIndices is empty, we're in writeIsNoop mode
245 if(pointIndices.size() == 0)
246 return;
247 arraywriter->write(pointIndices[factory.indexSet().subIndex
248 (cell, cornerIndex, mydim)]);
249 }
251 virtual void endWrite() {
252 arraywriter.reset();
253 pointIndices.clear();
254 }
255 };
256
258 template<typename Cell>
260 : public FunctionWriterBase<Cell>
261 {
262 std::shared_ptr<DataArrayWriter> arraywriter;
263 unsigned counter;
264
265 public:
267 virtual std::string name() const { return "connectivity"; }
268
270 virtual unsigned ncomps() const { return 1; }
271
273 virtual void addArray(PVTUWriter& writer) {
274 writer.addArray(name(), ncomps(), Precision::int32);
275 }
276
278 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
279 arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
280 nitems, Precision::int32));
281 counter = 0;
282 return !arraywriter->writeIsNoop();
283 }
285 virtual void write(const Cell& /* cell */, unsigned /* cornerIndex */)
286 {
287 arraywriter->write(counter);
288 ++counter;
289 }
291 virtual void endWrite() {
292 arraywriter.reset();
293 }
294 };
295
297 template<typename Cell>
299 : public FunctionWriterBase<Cell>
300 {
302
303 std::shared_ptr<DataArrayWriter> arraywriter;
304 unsigned offset;
305
306 public:
308 virtual std::string name() const { return "offsets"; }
309
311 virtual unsigned ncomps() const { return 1; }
312
314 virtual void addArray(PVTUWriter& writer) {
315 writer.addArray(name(), ncomps(), Precision::int32);
316 }
317
319 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
320 arraywriter.reset(writer.makeArrayWriter(name(), ncomps(),
321 nitems, Precision::int32));
322 offset = 0;
323 return !arraywriter->writeIsNoop();
324 }
326 virtual void write(const Cell& cell, const typename Base::Domain&) {
327 offset += cell.geometry().corners();
328 arraywriter->write(offset);
329 }
331 virtual void endWrite() {
332 arraywriter.reset();
333 }
334 };
335
337 template<typename Cell>
339 : public FunctionWriterBase<Cell>
340 {
342
343 std::shared_ptr<DataArrayWriter> arraywriter;
344
345 public:
347 virtual std::string name() const { return "types"; }
348
350 virtual unsigned ncomps() const { return 1; }
351
353 virtual void addArray(PVTUWriter& writer) {
354 writer.addArray(name(), ncomps(), Precision::uint8);
355 }
356
358 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
359 arraywriter.reset(writer.makeArrayWriter
360 ( name(), ncomps(), nitems, Precision::uint8));
361 return !arraywriter->writeIsNoop();
362 }
364 virtual void write(const Cell& cell, const typename Base::Domain&) {
365 arraywriter->write(geometryType(cell.type()));
366 }
368 virtual void endWrite() {
369 arraywriter.reset();
370 }
371 };
372
373 } // namespace VTK
374
376
377} // namespace Dune
378
379#endif // DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:91
Default exception for dummy implementations.
Definition: exceptions.hh:263
writer for the connectivity array in conforming mode
Definition: functionwriter.hh:195
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:216
virtual std::string name() const
return name
Definition: functionwriter.hh:210
virtual void write(const typename Base::Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:242
ConformingConnectivityWriter(const IteratorFactory &factory_)
create a writer with the given iteratorfactory
Definition: functionwriter.hh:205
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:213
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:221
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:251
writer for the Coordinates array
Definition: functionwriter.hh:147
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:176
virtual std::string name() const
return name
Definition: functionwriter.hh:159
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:186
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:162
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:165
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:170
Base class for function writers.
Definition: functionwriter.hh:34
virtual std::string name() const =0
return name
virtual unsigned ncomps() const =0
return number of components of the vector
virtual void write(const Cell &, const Domain &)
write at the given position
Definition: functionwriter.hh:59
virtual void addArray(PVTUWriter &writer)=0
add this field to the given parallel writer
virtual void endWrite()=0
signal end of writing
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)=0
start writing with the given writer
virtual ~FunctionWriterBase()
destructor
Definition: functionwriter.hh:78
virtual void write(const Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:71
writer for the connectivity array in nonconforming mode
Definition: functionwriter.hh:261
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:270
virtual std::string name() const
return name
Definition: functionwriter.hh:267
virtual void write(const Cell &, unsigned)
write at the given corner
Definition: functionwriter.hh:285
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:273
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:278
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:291
writer for the offsets array
Definition: functionwriter.hh:300
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:314
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:331
virtual std::string name() const
return name
Definition: functionwriter.hh:308
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:326
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:319
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:311
Dump a .vtu/.vtp files contents to a stream.
Definition: pvtuwriter.hh:62
void addArray(const std::string &name, unsigned ncomps, Precision prec)
Add an array to the output file.
Definition: pvtuwriter.hh:207
writer for the types array
Definition: functionwriter.hh:340
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:350
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:358
virtual std::string name() const
return name
Definition: functionwriter.hh:347
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:364
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:353
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:368
Base class for function writers.
Definition: functionwriter.hh:90
virtual std::string name() const
return name
Definition: functionwriter.hh:103
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:106
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:117
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:112
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:124
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:133
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:98
DataArrayWriter * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems, Precision prec)
acquire a DataArrayWriter
Definition: vtuwriter.hh:380
Data array writers for the VTKWriter.
A few common exception classes.
Common stuff for the VTKWriter.
Precision
which precision to use when writing out data to vtk files
Definition: common.hh:271
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:151
Implements a vector constructed from a given type representing a field and a compile-time given size.
#define DUNE_THROW(E, m)
Definition: exceptions.hh:218
Dune namespace.
Definition: alignedallocator.hh:13
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:128
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:156
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 21, 23:30, 2024)