Dune Core Modules (2.5.0)

functionwriter.hh
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3
4#ifndef DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
5#define DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
6
7#include <cstddef>
8#include <memory>
9#include <string>
10#include <typeinfo>
11#include <vector>
12
15
16#include <dune/geometry/referenceelements.hh>
17
20#include <dune/grid/io/file/vtk/pvtuwriter.hh>
21#include <dune/grid/io/file/vtk/vtuwriter.hh>
22
23namespace Dune
24{
27
28 namespace VTK {
29
31 template<typename Cell_>
33 typedef typename Cell_::Geometry::ctype DF;
34 static const unsigned mydim = Cell_::mydimension;
36
37 public:
39 typedef Cell_ Cell;
40
42 virtual std::string name() const = 0;
43
45 virtual unsigned ncomps() const = 0;
46
48 virtual void addArray(PVTUWriter& writer) = 0;
50 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) = 0;
52
57 virtual void write(const Cell& cell, const Domain& xl) {
58 DUNE_THROW(NotImplemented, "FunctionWriterBase::write(const Cell&, "
59 "const Domain&): Either the derived class " <<
60 typeid(*this).name() << " failed to implement this method "
61 "or this method is not meant to be called on the derived "
62 "class and was called in error.");
63 }
65
69 virtual void write(const Cell& cell, unsigned cornerIndex) {
70 write(cell,
71 Refelems::general(cell.type()).position(cornerIndex, mydim));
72 }
74 virtual void endWrite() = 0;
77 };
78
80 //
81 // A Generic Function writer for VTKFunctions
82 //
83
85 template<typename Func>
87 : public FunctionWriterBase<typename Func::Entity>
88 {
90 std::shared_ptr<const Func> func;
91 std::shared_ptr<DataArrayWriter<float> > arraywriter;
92
93 public:
94 VTKFunctionWriter(const std::shared_ptr<const Func>& func_)
95 : func(func_)
96 { }
97
99 virtual std::string name() const { return func->name(); }
100
102 virtual unsigned ncomps() const {
103 if(func->ncomps() == 2) return 3;
104 else return func->ncomps();
105 }
106
108 virtual void addArray(PVTUWriter& writer) {
109 writer.addArray<float>(name(), ncomps());
110 }
111
113 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
114 arraywriter.reset(writer.makeArrayWriter<float>(name(), ncomps(),
115 nitems));
116 return !arraywriter->writeIsNoop();
117 }
118
120 virtual void write(const typename Base::Cell& cell,
121 const typename Base::Domain& xl) {
122 for(int d = 0; d < func->ncomps(); ++d)
123 arraywriter->write(func->evaluate(d, cell, xl));
124 for(unsigned d = func->ncomps(); d < ncomps(); ++d)
125 arraywriter->write(0);
126 }
127
129 virtual void endWrite() {
130 arraywriter.reset();
131 }
132 };
133
135 //
136 // Writers for the grid information
137 //
138
140 template<typename Cell>
142 : public FunctionWriterBase<Cell>
143 {
145
146 std::shared_ptr<DataArrayWriter<float> > arraywriter;
147
148 public:
149
151 virtual std::string name() const { return "Coordinates"; }
152
154 virtual unsigned ncomps() const { return 3; }
155
157 virtual void addArray(PVTUWriter& writer) {
158 writer.addArray<float>(name(), ncomps());
159 }
160
162 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
163 arraywriter.reset(writer.makeArrayWriter<float>(name(), ncomps(),
164 nitems));
165 return !arraywriter->writeIsNoop();
166 }
168 virtual void write(const typename Base::Cell& cell,
169 const typename Base::Domain& xl) {
171 = cell.geometry().global(xl);
172 for(unsigned d = 0; d < 3 && d < Base::Cell::Geometry::coorddimension; ++d)
173 arraywriter->write(xg[d]);
174 for(unsigned d = Base::Cell::Geometry::coorddimension; d < 3; ++d)
175 arraywriter->write(0);
176 }
178 virtual void endWrite() {
179 arraywriter.reset();
180 }
181 };
182
184 template<typename IteratorFactory>
186 : public FunctionWriterBase<typename IteratorFactory::Cell>
187 {
189 static const unsigned mydim = Base::Cell::mydimension;
190
191 const IteratorFactory& factory;
192 std::shared_ptr<DataArrayWriter<unsigned> > arraywriter;
193 std::vector<unsigned> pointIndices;
194
195 public:
197 ConformingConnectivityWriter(const IteratorFactory& factory_)
198 : factory(factory_)
199 { }
200
202 virtual std::string name() const { return "connectivity"; }
203
205 virtual unsigned ncomps() const { return 1; }
206
208 virtual void addArray(PVTUWriter& writer) {
209 writer.addArray<unsigned>(name(), ncomps());
210 }
211
213 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
214 arraywriter.reset(writer.makeArrayWriter<unsigned>(name(), ncomps(),
215 nitems));
216 if(arraywriter->writeIsNoop())
217 return false;
218
220 pointIndices.resize(factory.indexSet().size(mydim));
221 const typename IteratorFactory::PointIterator& pend =
222 factory.endPoints();
223 typename IteratorFactory::PointIterator pit = factory.beginPoints();
224 unsigned counter = 0;
225 while(pit != pend) {
226 pointIndices[factory.indexSet().subIndex
227 (pit->cell(), pit->duneIndex(), mydim)] = counter;
228 ++counter;
229 ++pit;
230 }
231 return true;
232 }
234 virtual void write(const typename Base::Cell& cell, unsigned cornerIndex)
235 {
236 // if pointIndices is empty, we're in writeIsNoop mode
237 if(pointIndices.size() == 0)
238 return;
239 arraywriter->write(pointIndices[factory.indexSet().subIndex
240 (cell, cornerIndex, mydim)]);
241 }
243 virtual void endWrite() {
244 arraywriter.reset();
245 pointIndices.clear();
246 }
247 };
248
250 template<typename Cell>
252 : public FunctionWriterBase<Cell>
253 {
254 std::shared_ptr<DataArrayWriter<unsigned> > arraywriter;
255 unsigned counter;
256
257 public:
259 virtual std::string name() const { return "connectivity"; }
260
262 virtual unsigned ncomps() const { return 1; }
263
265 virtual void addArray(PVTUWriter& writer) {
266 writer.addArray<unsigned>(name(), ncomps());
267 }
268
270 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
271 arraywriter.reset(writer.makeArrayWriter<unsigned>(name(), ncomps(),
272 nitems));
273 counter = 0;
274 return !arraywriter->writeIsNoop();
275 }
277 virtual void write(const Cell& cell, unsigned cornerIndex)
278 {
279 arraywriter->write(counter);
280 ++counter;
281 }
283 virtual void endWrite() {
284 arraywriter.reset();
285 }
286 };
287
289 template<typename Cell>
291 : public FunctionWriterBase<Cell>
292 {
294
295 std::shared_ptr<DataArrayWriter<unsigned> > arraywriter;
296 unsigned offset;
297
298 public:
300 virtual std::string name() const { return "offsets"; }
301
303 virtual unsigned ncomps() const { return 1; }
304
306 virtual void addArray(PVTUWriter& writer) {
307 writer.addArray<unsigned>(name(), ncomps());
308 }
309
311 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
312 arraywriter.reset(writer.makeArrayWriter<unsigned>(name(), ncomps(),
313 nitems));
314 offset = 0;
315 return !arraywriter->writeIsNoop();
316 }
318 virtual void write(const Cell& cell, const typename Base::Domain&) {
319 offset += cell.geometry().corners();
320 arraywriter->write(offset);
321 }
323 virtual void endWrite() {
324 arraywriter.reset();
325 }
326 };
327
329 template<typename Cell>
331 : public FunctionWriterBase<Cell>
332 {
334
335 std::shared_ptr<DataArrayWriter<unsigned char> > arraywriter;
336
337 public:
339 virtual std::string name() const { return "types"; }
340
342 virtual unsigned ncomps() const { return 1; }
343
345 virtual void addArray(PVTUWriter& writer) {
346 writer.addArray<unsigned char>(name(), ncomps());
347 }
348
350 virtual bool beginWrite(VTUWriter& writer, std::size_t nitems) {
351 arraywriter.reset(writer.makeArrayWriter<unsigned char>
352 ( name(), ncomps(), nitems));
353 return !arraywriter->writeIsNoop();
354 }
356 virtual void write(const Cell& cell, const typename Base::Domain&) {
357 arraywriter->write(geometryType(cell.type()));
358 }
360 virtual void endWrite() {
361 arraywriter.reset();
362 }
363 };
364
365 } // namespace VTK
366
368
369} // namespace Dune
370
371#endif // DUNE_GRID_IO_FILE_VTK_FUNCTIONWRITER_HH
vector space out of a tensor product of fields.
Definition: fvector.hh:93
Default exception for dummy implementations.
Definition: exceptions.hh:261
writer for the connectivity array in conforming mode
Definition: functionwriter.hh:187
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:208
virtual std::string name() const
return name
Definition: functionwriter.hh:202
virtual void write(const typename Base::Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:234
ConformingConnectivityWriter(const IteratorFactory &factory_)
create a writer with the given iteratorfactory
Definition: functionwriter.hh:197
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:205
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:213
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:243
writer for the Coordinates array
Definition: functionwriter.hh:143
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:168
virtual std::string name() const
return name
Definition: functionwriter.hh:151
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:178
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:154
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:157
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:162
Base class for function writers.
Definition: functionwriter.hh:32
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 &cell, const Domain &xl)
write at the given position
Definition: functionwriter.hh:57
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:76
virtual void write(const Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:69
writer for the connectivity array in nonconforming mode
Definition: functionwriter.hh:253
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:262
virtual std::string name() const
return name
Definition: functionwriter.hh:259
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:265
virtual void write(const Cell &cell, unsigned cornerIndex)
write at the given corner
Definition: functionwriter.hh:277
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:270
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:283
writer for the offsets array
Definition: functionwriter.hh:292
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:306
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:323
virtual std::string name() const
return name
Definition: functionwriter.hh:300
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:318
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:311
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:303
Dump a .vtu/.vtp files contents to a stream.
Definition: pvtuwriter.hh:60
void addArray(const std::string &name, unsigned ncomps)
Add an array to the output file.
Definition: pvtuwriter.hh:205
writer for the types array
Definition: functionwriter.hh:332
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:342
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:350
virtual std::string name() const
return name
Definition: functionwriter.hh:339
virtual void write(const Cell &cell, const typename Base::Domain &)
write at the given position
Definition: functionwriter.hh:356
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:345
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:360
Base class for function writers.
Definition: functionwriter.hh:88
virtual std::string name() const
return name
Definition: functionwriter.hh:99
virtual unsigned ncomps() const
return number of components of the vector
Definition: functionwriter.hh:102
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)
start writing with the given writer
Definition: functionwriter.hh:113
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition: functionwriter.hh:108
virtual void write(const typename Base::Cell &cell, const typename Base::Domain &xl)
write at the given position
Definition: functionwriter.hh:120
virtual void endWrite()
signal end of writing
Definition: functionwriter.hh:129
Dump a .vtu/.vtp files contents to a stream.
Definition: vtuwriter.hh:96
DataArrayWriter< T > * makeArrayWriter(const std::string &name, unsigned ncomps, unsigned nitems)
acquire a DataArrayWriter
Definition: vtuwriter.hh:379
Common stuff for the VTKWriter.
GeometryType geometryType(const Dune::GeometryType &t)
mapping from GeometryType to VTKGeometryType
Definition: common.hh:197
Data array writers for the VTKWriter.
A few common exception classes.
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:216
Dune namespace.
Definition: alignment.hh:11
Class providing access to the singletons of the reference elements.
Definition: referenceelements.hh:752
static const ReferenceElement< ctype, dim > & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:757
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Nov 23, 23:29, 2024)