vtkGraphics.hh

Go to the documentation of this file.
1 
8 #ifndef VtkGraphics_HH_
9 #define VtkGraphics_HH_
10 #include "basics.hh"
11 #include "denseMatrixGraphics.hh"
12 #include "basis.hh"
13 
14 namespace graphics {
15 
20  class VtkGraphics: public OutputBase, public DenseMatrixGraphics {
21  public:
23 
30  template<class F, class G>
31  VtkGraphics(const concepts::Space<G>& spc, const std::string filename,
32  const concepts::Vector<F>& sol,
33  const concepts::ElementFunction<F, G>* fun = 0);
34 
40  template<class F, class G>
41  VtkGraphics(const concepts::Space<G>& spc, const std::string filename,
43 
48  template<class G>
49  VtkGraphics(const concepts::Space<G>& spc, const std::string filename);
50 
56  VtkGraphics(concepts::Mesh& msh, const std::string filename,
57  const uint points = 5);
58 
66  template<class G, class F>
67  void addSolution(const concepts::Space<G>& spc, const std::string name,
68  const concepts::Vector<F>& sol,
69  const concepts::ElementFunction<F, G>* fun = 0) {
70 
72  spc, sol, fun);
73 
74  //store matrix
75  std::ostringstream stream;
76  writeValues_(stream, function, name);
77  *ofs_ << stream.str();
78  }
79  ;
80 
87  template<class F, class G>
88  void addFunction(const concepts::Space<G>& spc, const std::string name,
89  const concepts::ElementFormula<F, G>& frm) {
90 
92  spc, frm);
93  //store matrix
94  std::ostringstream stream;
95 
96  writeValues_(stream, function, name);
97  *ofs_ << stream.str();
98  }
99 
100  virtual ~VtkGraphics();
101 
102  private:
103 
105  template<class G, class F>
106  void storeData_(bool data, std::string filename,
107  DenseMatrixCollection<G>& dense_ptr_,
108  concepts::RCP<concepts::ElementMatrix<F> > functionValue =
110 
112  template<class G>
113  void writeHead_(std::string&, std::ostringstream& stream,
114  DenseMatrixCollection<G>& dense_ptr_);
115  template<class G, class F>
116 
118  void writePoints_(std::ostringstream& stream,
119  DenseMatrixCollection<G>& dense_ptr_,
120  concepts::RCP<concepts::ElementMatrix<F> > functionValue =
122 
124  template<class G>
125  void writeMesh_(std::ostringstream& stream,
126  DenseMatrixCollection<G>& dense_ptr_);
127 
129  template<class G>
130  void writeEdgeMesh_(std::ostringstream& stream,
131  DenseMatrixCollection<G>& dense_ptr_);
132 
134  template<class F>
135  void writeValues_(std::ostringstream& stream,
136  concepts::RCP<concepts::ElementMatrix<F> > functionValue = 0,
137  const std::string name = "solution");
138 
140  template<class G>
141  void writeWeightsAndAttr_(std::ostringstream& stream,
142  DenseMatrixCollection<G>& dense_ptr_);
143 
145  template<class G>
146  void cellType_(std::ostringstream& stream,
147  DenseMatrixCollection<G>& dense_ptr_);
148 
152  std::string vtkEnding_(const std::string& filename) {
153  uint size = filename.size();
154  if (size < 4 || std::string(filename, size - 4, 4) != ".vtk")
155  return (filename + ".vtk");
156  return filename;
157  }
158 
159  // Boolean for destructor
160  uint data_;
161 
162  // dimension of the output: 1 if output is solution | dim_ if output is gradient
164 
165  };
166 
167  template<class F, class G>
169  const std::string filename, const concepts::Vector<F>& sol,
170  const concepts::ElementFunction<F, G>* fun) :
171  OutputBase(vtkEnding_(filename)) {
172 
173  DenseMatrixCollection<G> dmc = this->getSpace(spc);
175  this->getSolution(spc, sol, fun);
176 
177  storeData_(true, filename, dmc, functionValues);
178  }
179 
180  template<class F, class G>
182  const std::string filename, const concepts::ElementFormula<F, G>& frm) :
183  OutputBase(vtkEnding_(filename)) {
184 
185  DenseMatrixCollection<G> dmc = this->getSpace(spc);
187  this->getFunction(spc, frm);
188 
189  storeData_(true, filename, dmc, functionValues);
190  }
191 
192  template<class G>
194  const std::string filename) :
195  OutputBase(vtkEnding_(filename)) {
196 
197  outputDimension_ = 0;
198  DenseMatrixCollection<G> dmc = this->getSpace(spc);
199  storeData_<G, concepts::Real>(true, filename, dmc);
200 
201  }
202 
203  //Save the matrices that hold informations
204  template<class G, class F>
205  void VtkGraphics::storeData_(bool data, std::string filename,
206  DenseMatrixCollection<G>& dense_ptr_,
207  concepts::RCP<concepts::ElementMatrix<F> > functionValue) {
208 
209  std::ostringstream stream;
210 
211  //write head of vtk file
212  writeHead_(filename, stream, dense_ptr_);
213 
214  //write the figure-points in the vtk file
215  writePoints_(stream, dense_ptr_, functionValue);
216 
217  //writes the mesh in the vtk file
218  //(i.e. the quads/hexahedra that builds the mesh)
219  writeMesh_(stream, dense_ptr_);
220 
221  //if our mesh consits of hexahedra we have to specify the cell type
222  if ((dense_ptr_.find("z") != dense_ptr_.end()) && (dense_ptr_["msh"]->m() >= 8))
223  cellType_(stream, dense_ptr_);
224 
225  //writes data in the vtk file. The value/gradient/laplace of the solution,
226  //the integration weights of the integration rule and attributes
227  if (data) {
228  writeWeightsAndAttr_(stream, dense_ptr_);
229  if (functionValue)
230  writeValues_(stream, functionValue);
231  }
232 
233  //store the written data in ofs
234  *ofs_ << stream.str();
235  }
236 
237  template<class G>
238  void VtkGraphics::writeMesh_(std::ostringstream& stream,
239  DenseMatrixCollection<G>& dense_ptr_) {
240 
241  //write lines in case of line graphics
242  if (dense_ptr_["msh"]->m() < 4) {
243  stream << "LINES" << " " << dense_ptr_["msh"]->n() << " "
244  << dense_ptr_["msh"]->n() * 3 << "\n";
245 
246  for (uint i = 0; i < dense_ptr_["msh"]->n(); ++i)
247  stream << "2 " << dense_ptr_["msh"]->operator()(0, i) - 1 << " "
248  << dense_ptr_["msh"]->operator()(1, i) - 1 << '\n';
249  }
250 
251  //write polygons in case of Quad2D and Quad3D
252  else if ((((dense_ptr_.find("z") == dense_ptr_.end())
253  && (dense_ptr_.find("y") != dense_ptr_.end()))
254  )||
255  ((dense_ptr_.find("z") != dense_ptr_.end())
256  && (dense_ptr_["msh"]->m() < 8)))
257  {
258  stream << "POLYGONS" << " " << dense_ptr_["msh"]->n() << " "
259  << dense_ptr_["msh"]->n() * 5 << "\n";
260 
261  for (uint i = 0; i < dense_ptr_["msh"]->n(); ++i)
262  stream << "4 " << dense_ptr_["msh"]->operator()(0, i) - 1 << " "
263  << dense_ptr_["msh"]->operator()(1, i) - 1 << " "
264  << dense_ptr_["msh"]->operator()(2, i) - 1 << " "
265  << dense_ptr_["msh"]->operator()(3, i) - 1 << '\n';
266  }
267 
268  //write cells (of cell type 12) in case of hexahedra
269  else if (dense_ptr_.find("z") != dense_ptr_.end()) {
270  stream << "CELLS" << " " << dense_ptr_["msh"]->n() << " "
271  << dense_ptr_["msh"]->n() * 9 << "\n";
272  for (uint i = 0; i < dense_ptr_["msh"]->n(); ++i)
273  stream << "8 " << dense_ptr_["msh"]->operator()(0, i) - 1 << " "
274  << dense_ptr_["msh"]->operator()(1, i) - 1 << " "
275  << dense_ptr_["msh"]->operator()(2, i) - 1 << " "
276  << dense_ptr_["msh"]->operator()(3, i) - 1 << " "
277  << dense_ptr_["msh"]->operator()(4, i) - 1 << " "
278  << dense_ptr_["msh"]->operator()(5, i) - 1 << " "
279  << dense_ptr_["msh"]->operator()(6, i) - 1 << " "
280  << dense_ptr_["msh"]->operator()(7, i) - 1 << '\n';
281  }
282  }
283 
284  template<class G>
285  void VtkGraphics::writeEdgeMesh_(std::ostringstream& stream,
286  DenseMatrixCollection<G>& dense_ptr_) {
287  stream << "LINES" << " " << dense_ptr_["edgemsh"]->n() << " "
288  << dense_ptr_["edgemsh"]->n() * 3 << "\n";
289 
290  for (uint i = 0; i < dense_ptr_["edgemsh"]->n(); ++i)
291  stream << "2 " << dense_ptr_["edgemsh"]->operator()(0, i) - 1 << " "
292  << dense_ptr_["edgemsh"]->operator()(1, i) - 1 << "\n";
293  }
294 
295  template<class G>
296  void VtkGraphics::writeWeightsAndAttr_(std::ostringstream& stream,
297  DenseMatrixCollection<G>& dense_ptr_) {
298 
299  //Adds the integration weights and the pointAttributes as Point_Data
300  stream << "POINT_DATA " << dense_ptr_["w"]->n() << "\n";
301  stream << "SCALARS weights FLOAT\n";
302  stream << "LOOKUP_TABLE default\n";
303  for (uint i = 0; i < dense_ptr_["w"]->n(); ++i)
304  stream << dense_ptr_["w"]->operator()(0, i) << "\n";
305 
306  stream << "SCALARS pointAttributes FLOAT\n";
307  stream << "LOOKUP_TABLE default\n";
308  for (uint i = 0; i < dense_ptr_["pattr"]->n(); ++i)
309  stream << dense_ptr_["pattr"]->operator()(0, i) << "\n";
310  }
311 
312  template<class G>
313  void VtkGraphics::cellType_(std::ostringstream& stream,
314  DenseMatrixCollection<G>& dense_ptr_) {
315 
316  //In Vtk files the Cell Type no. 12 stands for hexahedra
317  stream << "CELL_TYPES " << dense_ptr_["msh"]->n() << "\n";
318  for (uint i = 0; i < dense_ptr_["msh"]->n(); ++i)
319  stream << 12 << "\n";
320 
321  }
322 
323  template<class G, class F>
324  void VtkGraphics::writePoints_(std::ostringstream& stream,
325  DenseMatrixCollection<G>& dense_ptr_,
326  concepts::RCP<concepts::ElementMatrix<F> > functionValue) {
327 
328  stream << "POINTS " << dense_ptr_["x"]->n() << " float\n";
329  for (uint i = 0; i < dense_ptr_["x"]->n(); ++i) {
330  stream << dense_ptr_["x"]->operator()(0, i) << " "
331  << dense_ptr_["y"]->operator()(0, i) << " ";
332  //in case of two dimensions add 0 as third coordinate, the z- value else
333  if (dense_ptr_.find("z") == dense_ptr_.end())
334  stream << 0 << "\n";
335  else
336  stream << dense_ptr_["z"]->operator()(0, i) << "\n";
337  }
338  }
339 
340  template<class G>
341  void VtkGraphics::writeHead_(std::string& filename,
342  std::ostringstream& stream, DenseMatrixCollection<G>& dense_ptr_) {
343  stream << "# vtk DataFile Version 2.0\n";
344  stream << filename;
345  stream << "\nASCII\n";
346 
347  //write dataset for line graphics, Quad2D and Quad3D
348  if (dense_ptr_["msh"]->m() < 8)
349  stream << "DATASET POLYDATA\n";
350 
351  //write dataset for hexahedra
352  else
353  stream << "DATASET UNSTRUCTURED_GRID\n";
354  }
355 
356 } // namespace graphics
357 
358 #endif /* VtkGraphics_HH_ */
An abstract class for a function in a FE space.
std::unique_ptr< std::ofstream > ofs_
Stream for output file.
Definition: basis.hh:83
void writeMesh_(std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_)
Writes the mesh/geometry in the vtk file.
Definition: vtkGraphics.hh:238
concepts::RCP< concepts::ElementMatrix< F > > getSolution(const concepts::Space< G > &spc, const concepts::Vector< F > &sol, const concepts::ElementFunction< F, G > *fun=0)
Returns a vector with the value of the solution on the quadrature-points of a given space using a giv...
void writePoints_(std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_, concepts::RCP< concepts::ElementMatrix< F > > functionValue=concepts::RCP< concepts::ElementMatrix< F > >(0))
writes the points in the vtk file
Definition: vtkGraphics.hh:324
void cellType_(std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_)
Adds the cellType (in the 3 d case)
Definition: vtkGraphics.hh:313
Base class for graphical output to a file.
Definition: basis.hh:74
void writeHead_(std::string &, std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_)
writes the header of an ASCII vtk file
Definition: vtkGraphics.hh:341
void storeData_(bool data, std::string filename, DenseMatrixCollection< G > &dense_ptr_, concepts::RCP< concepts::ElementMatrix< F > > functionValue=concepts::RCP< concepts::ElementMatrix< F > >(0))
Store data as vtk file.
Definition: vtkGraphics.hh:205
Graphics.
Definition: basis.hh:33
Interface for a formula defined element by element.
void writeValues_(std::ostringstream &stream, concepts::RCP< concepts::ElementMatrix< F > > functionValue=0, const std::string name="solution")
writes function values in a vtk file
Reference-counting pointer.
Definition: bf_iddiv.hh:15
Class that allows to store graphical infomations in .vtk files to use them in paraview.
Definition: vtkGraphics.hh:20
void addSolution(const concepts::Space< G > &spc, const std::string name, const concepts::Vector< F > &sol, const concepts::ElementFunction< F, G > *fun=0)
Adds a solution vector to the current matfile.
Definition: vtkGraphics.hh:67
DenseMatrixCollection< G > getSpace(const concepts::Space< G > &spc)
Returns a graphics::DenseMatrixCollection<G> that contains informations for graphical output of the g...
std::string vtkEnding_(const std::string &filename)
Adds ".vtk" to a string if it dosn't end with ".vtk" the string itself else.
Definition: vtkGraphics.hh:152
void writeWeightsAndAttr_(std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_)
Writes attribues and integration weights as pointdata in the vtk file.
Definition: vtkGraphics.hh:296
Element matrix.
Definition: linearForm.hh:18
Class that collects data for graphical output and stores it in concepts::ElementMatrices and graphics...
An abstract class for meshes.
Definition: mesh.hh:76
void addFunction(const concepts::Space< G > &spc, const std::string name, const concepts::ElementFormula< F, G > &frm)
Adds a solution vector to the current matfile.
Definition: vtkGraphics.hh:88
void writeEdgeMesh_(std::ostringstream &stream, DenseMatrixCollection< G > &dense_ptr_)
Writes the Edgmesh in the vtk file.
Definition: vtkGraphics.hh:285
concepts::RCP< concepts::ElementMatrix< typename concepts::Datatype< F >::type > > getFunction(const concepts::Space< G > &spc, const concepts::ElementFormula< F, G > &frm)
Returns a vector with the value of the function on the quadrature-points of a given space using the g...
Class that hold and manage a std::map with concepts::ElementMatrices for graphical output,...
VtkGraphics(concepts::Mesh &msh, const std::string filename, const uint points=5)
Constructor for output of a mesh.
Page URL: http://wiki.math.ethz.ch/bin/view/Concepts/WebHome
21 August 2020
© 2020 Eidgenössische Technische Hochschule Zürich