Tools/VTKData.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef TOOLS_VTKDATA_H
6 #define TOOLS_VTKDATA_H
7 
8 #include <fstream>
9 #include <iostream>
10 #include <string>
11 #include <sstream>
12 #include <functional>
13 #include <map>
14 #include <vector>
15 #include <type_traits>
16 #include <cmath>
17 
18 //Namespace Detail contains black magic.
19 namespace Detail {
23  template<typename T>
25  {
27  public:
33  : name_(fieldName)
34  {
35  }
36 
38  {
39  }
40 
46  virtual void emit(std::ostream& out, const T& t) const = 0;
51  virtual std::string getTypeName() const = 0;
56  {
57  return name_;
58  }
62  virtual std::size_t getNumberOfComponents() const = 0;
63  };
64 
66  template<typename V>
69  {
70  return "Float32";
71  }
72 
73  template<typename V>
76  {
77  return "UInt32";
78  }
79 
80  template<typename V>
83  {
84  return "Int32";
85  }
86 
87  template<typename V>
90  {
92  }
93 
94  template<typename V>
95  V round(V in) {
96  return in;
97  }
98 
99  template<>
100  double round(double in) {
101  //logger.assert_debug(fabs(in)>1e-33 or fabs(in)==0, "Detected underflow: corrected double value % to 0", in);
102  logger.assert_debug(std::isfinite(in), "Detected nans!");
103  return (fabs(in)<1e-33)?0.0:in;
104  }
105 
107  template<typename T, typename V>
109  emitProxy(std::ostream& out, const T& t, std::size_t nComponents, V T::*member)
110  {
111  for (std::size_t i = 0; i < nComponents; i++)
112  {
113  out << round((t.*member)[i]) << " ";
114  }
115  }
116 
117  template<typename T, typename V>
118  typename std::enable_if<!(std::is_array<V>::value || std::is_pointer<V>::value), void>::type
119  emitProxy(std::ostream& out, const T& t, std::size_t nComponents, V T::*member)
120  {
121  out << round(t.*member) << " ";
122  }
123 
127  template<typename T, typename V>
129  {
130  const V T::* member_;
131  const std::size_t nComponents_;
132  public:
133  VTKPointDescriptorEntryImpl(std::string name, V T::*member, std::size_t nComponents)
134  : VTKPointDescriptorEntry<T>(name), member_(member), nComponents_(nComponents)
135  { }
136 
137  std::string getTypeName() const override
138  {
139  return toVTKDataType<V>();
140  }
141 
142  void emit(std::ostream& out, const T& t) const override
143  {
145  }
146 
147  std::size_t getNumberOfComponents() const override
148  {
149  return nComponents_;
150  }
151  };
152 }
153 
162 template<typename T>
164 {
165  std::vector< Detail::VTKPointDescriptorEntry<T>* > entries_;
167  public:
169  {
170  }
171 
173  {
174  for (auto * d : entries_)
175  delete d;
176  }
177 
178  template<typename DATATYPE>
180  addProperty(std::string name, DATATYPE T::*m, bool isPrimary = false)
181  {
184  entries_.push_back(data);
185  if (isPrimary)
186  positionEntry_ = entries_.back();
187 
188  return *this;
189  }
190 
191  template<typename DATATYPE>
193  addProperty(std::string name, DATATYPE T::*m, bool isPrimary = false)
194  {
197  entries_.push_back(data);
198  if (isPrimary)
199  positionEntry_ = entries_.back();
200 
201  return *this;
202  }
203 
204  template<typename DATATYPE>
205  VTKPointDescriptor& addProperty(std::string name, DATATYPE T::*m, std::size_t nComponents, bool isPrimary = false)
206  {
209  entries_.push_back(data);
210  if (isPrimary)
211  positionEntry_ = entries_.back();
212 
213  return *this;
214  }
215 
216 
217  template<typename VT>
218  friend class VTKUnstructuredGrid;
219 };
220 
221 /* \brief VTKCollection output file writer
222 *
223 * This is templated by the VTKDescriptor DESCR.
224 * \param T the backing output type
225 */
227 {
228  std::ofstream outFile_;
231  std::size_t recordID_;
232 
233  void writeHeader()
234  {
235  outFile_ <<
236  "<?xml version=\"1.0\"?>\n"
237  "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"LittleEndian\">\n"
238  " <Collection>\n";
239  hasHeader_ = true;
240  }
241 
242  void writeFooter()
243  {
244  outFile_ <<
245  " </Collection>\n"
246  "</VTKFile>\n";
247  hasFooter_ = true;
248  }
249 
250  public:
252  : outFile_(fileName), hasHeader_(false), hasFooter_(false), recordID_(0)
253  {
254  }
255 
257  {
258  if (hasHeader_ && !hasFooter_)
259  writeFooter();
260  }
261 
262  /* Returns true if the output file is valid and no error bits are set. */
263  operator bool() const
264  {
265  return outFile_.good();
266  }
267 
268  /* Appends the (relative) path to this index file. */
270  {
271  if (!hasHeader_)
272  writeHeader();
273 
274  outFile_ <<
275  "<DataSet group=\"\" part=\"0\" timestep=\"" << recordID_ << "\" file=\"" << filename << "\" />\n";
276 
277  recordID_++;
278  }
279 };
280 
285 template<typename T>
287 {
289  std::ofstream outFile_;
290  public:
296  : descriptor_(descr), outFile_(filename)
297  {
298  }
299 
301  operator bool() const
302  {
303  return outFile_.good();
304  }
305 
311  template<typename C>
312  void write(const C& container)
313  {
314  outFile_ <<
315  "<?xml version=\"1.0\"?>\n"
316  "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n"
317  " <UnstructuredGrid>\n"
318  " <Piece NumberOfPoints=\"" << container.size() << "\" NumberOfCells=\"0\">\n"
319  " <Cells>\n"
320  " <DataArray type=\"Int32\" name=\"connectivity\" format=\"ascii\">\n"
321  " 0\n"
322  " </DataArray>\n"
323  " <DataArray type=\"Float32\" name=\"offset\" format=\"ascii\">\n"
324  " 0\n"
325  " </DataArray>\n"
326  " <DataArray type=\"UInt8\" name=\"types\" format=\"ascii\">\n"
327  " 1\n"
328  " </DataArray>\n"
329  " </Cells>\n"
330  " <Points>\n";
331  Detail::VTKPointDescriptorEntry<T>* pDescr = descriptor_->positionEntry_;
332  outFile_ << "<DataArray type=\"" << pDescr->getTypeName() << "\" "
333  "NumberOfComponents=\"" << pDescr->getNumberOfComponents() << "\" "
334  "format=\"ascii\">\n";
335  for (const T& mem : container)
336  {
337  pDescr->emit(outFile_, mem);
338  }
339  outFile_ << "\n</DataArray>\n";
340 
341  outFile_ <<
342  " </Points>\n"
343  " <PointData>\n";
344 
345  for (Detail::VTKPointDescriptorEntry<T>* descr : descriptor_->entries_)
346  {
347  outFile_ << "<DataArray type=\"" << descr->getTypeName() << "\" "
348  "Name=\"" << descr->getName() << "\" "
349  "NumberOfComponents=\"" << descr->getNumberOfComponents() << "\" "
350  "format=\"ascii\">\n";
351  for (const T& mem : container)
352  {
353  descr->emit(outFile_, mem);
354  }
355  outFile_ << "\n</DataArray>\n";
356  }
357  outFile_ <<
358  " </PointData>\n"
359  " <CellData/>\n"
360  " </Piece>\n"
361  " </UnstructuredGrid>\n"
362  "</VTKFile>\n";
363  }
364 };
365 
366 #endif
int i
Definition: BiCGSTAB_step_by_step.cpp:9
Array< double, 1, 3 > e(1./3., 0.5, 2.)
MatrixXcd V
Definition: EigenSolver_EigenSolver_MatrixType.cpp:15
Eigen::Triplet< double > T
Definition: EigenUnitTest.cpp:11
Logger< MERCURYDPM_LOGLEVEL > logger("MercuryKernel")
Definition of different loggers with certain modules. A user can define its own custom logger here.
int data[]
Definition: Map_placement_new.cpp:1
Definition: Tools/VTKData.h:129
const std::size_t nComponents_
Definition: Tools/VTKData.h:131
std::size_t getNumberOfComponents() const override
Returns the number of components in this type.
Definition: Tools/VTKData.h:147
void emit(std::ostream &out, const T &t) const override
writes this VTKData to the given output stream for a single T
Definition: Tools/VTKData.h:142
std::string getTypeName() const override
Gives the VTKDataType for VTK.
Definition: Tools/VTKData.h:137
VTKPointDescriptorEntryImpl(std::string name, V T::*member, std::size_t nComponents)
Definition: Tools/VTKData.h:133
const V T::* member_
Definition: Tools/VTKData.h:130
Definition: Tools/VTKData.h:25
std::string name_
Definition: Tools/VTKData.h:26
virtual ~VTKPointDescriptorEntry()
Definition: Tools/VTKData.h:37
std::string getName() const
Returns the name associated with this field.
Definition: Tools/VTKData.h:55
virtual void emit(std::ostream &out, const T &t) const =0
writes this VTKData to the given output stream for a single T
virtual std::size_t getNumberOfComponents() const =0
Returns the number of components in this type.
VTKPointDescriptorEntry(std::string fieldName)
initialises this data member with given name.
Definition: Tools/VTKData.h:32
virtual std::string getTypeName() const =0
Gives the VTKDataType for VTK.
Definition: Tools/VTKData.h:227
VTKCollection(std::string fileName)
Definition: Tools/VTKData.h:251
std::ofstream outFile_
Definition: Tools/VTKData.h:228
bool hasHeader_
Definition: Tools/VTKData.h:229
~VTKCollection()
Definition: Tools/VTKData.h:256
void writeFooter()
Definition: Tools/VTKData.h:242
std::size_t recordID_
Definition: Tools/VTKData.h:231
bool hasFooter_
Definition: Tools/VTKData.h:230
void append(std::string filename)
Definition: Tools/VTKData.h:269
void writeHeader()
Definition: Tools/VTKData.h:233
Definition: Tools/VTKData.h:164
std::enable_if<!std::is_array< DATATYPE >::value, VTKPointDescriptor & >::type addProperty(std::string name, DATATYPE T::*m, bool isPrimary=false)
Definition: Tools/VTKData.h:193
Detail::VTKPointDescriptorEntry< T > * positionEntry_
Definition: Tools/VTKData.h:166
VTKPointDescriptor()
Definition: Tools/VTKData.h:168
~VTKPointDescriptor()
Definition: Tools/VTKData.h:172
std::vector< Detail::VTKPointDescriptorEntry< T > * > entries_
Definition: Tools/VTKData.h:165
VTKPointDescriptor & addProperty(std::string name, DATATYPE T::*m, std::size_t nComponents, bool isPrimary=false)
Definition: Tools/VTKData.h:205
std::enable_if< std::is_array< DATATYPE >::value, VTKPointDescriptor & >::type addProperty(std::string name, DATATYPE T::*m, bool isPrimary=false)
Definition: Tools/VTKData.h:180
Definition: Tools/VTKData.h:287
const VTKPointDescriptor< T > * descriptor_
Definition: Tools/VTKData.h:288
VTKUnstructuredGrid(std::string filename, const VTKPointDescriptor< T > *descr)
Definition: Tools/VTKData.h:295
std::ofstream outFile_
Definition: Tools/VTKData.h:289
void write(const C &container)
Definition: Tools/VTKData.h:312
Definition: matrices.h:74
int * m
Definition: level2_cplx_impl.h:294
#define isfinite(X)
Definition: main.h:111
Definition: MpiContainer.h:70
V round(V in)
Definition: Tools/VTKData.h:95
std::enable_if< std::is_floating_point< V >::value, std::string >::type toVTKDataType()
Definition: Tools/VTKData.h:68
std::enable_if< std::is_array< V >::value||std::is_pointer< V >::value, void >::type emitProxy(std::ostream &out, const T &t, std::size_t nComponents, V T::*member)
This function actually writes the correct datatype to ostream.
Definition: Tools/VTKData.h:109
squared absolute value
Definition: GlobalFunctions.h:87
string filename
Definition: MergeRestartFiles.py:39
string fileName
Definition: UniformPSDSelfTest.py:10
Real fabs(const Real &a)
Definition: boostmultiprec.cpp:117
type
Definition: compute_granudrum_aor.py:141
std::string string(const unsigned &i)
Definition: oomph_definitions.cc:286
string name
Definition: plotDoE.py:33
t
Definition: plotPSD.py:36
std::ofstream out("Result.txt")