MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WallVTKWriter.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2020, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
27 #include "DPMBase.h"
28 
29 
31 {
32  // set capacity of points and cells based on the previous time step
33  // the initial values are based on the minimum, which is one triangle per wall
34  static unsigned int capacityPoints = 3*handler_.getSize();
35  static unsigned int capacityTriangleStrips = handler_.getSize();
36  vtk.triangleStrips.reserve(capacityPoints);
37  vtk.points.reserve(capacityTriangleStrips);
38 
39  //add all wall data to the point and cell arrays
40  for (const auto& w: handler_)
41  {
42  w->renderWall(vtk);
43  }
44 
45 // logger(INFO, "size (capacity) of points: % (%), cells: % (%)", vtk.points.size(), capacityPoints, vtk.triangleStrips.size(), capacityTriangleStrips);
46 
47  //store from previous time step
48  capacityPoints = vtk.points.size();
49  capacityTriangleStrips = vtk.triangleStrips.size();
50 }
51 
53 {
54  if (PROCESSOR_ID!=0) return;
55  std::fstream file = makeVTKFileWithHeader();
56 
57  VTKContainer vtk;
58  getVTKData(vtk);
59  file << "<Piece NumberOfPoints=\"" << vtk.points.size()
60  << "\" NumberOfCells=\"" << vtk.triangleStrips.size()
61  << "\">\n"
62  << "<Points>\n";
63  writeVTKPoints(file, vtk);
64  file << "</Points>\n";
65  // this cannot be done right now since we can't math Points/Cells and Walls
66  // file << "<PointData Vectors=\"vector\">\n";
67  // write(file,"indSpecies",[](BaseWall* w){return w->getIndSpecies();});
68  // file << "</PointData>\n";
69  file << "<Cells>\n";
70  writeVTKCells(file, vtk);
71  file << "</Cells>\n"
72  << "</Piece>\n"
73  << "</UnstructuredGrid>\n"
74  << "</VTKFile>\n";
75  file.close();
76 }
77 
78 void WallVTKWriter::write(std::fstream& file, std::string name, std::function<double(BaseWall*)> f) const
79 {
80  file << " <DataArray type=\"Float32\" Name=\""+ name + "\" format=\"ascii\">\n";
81  for (const auto& p: handler_)
82  {
83  file << '\t' << f(p) << '\n';
84  }
85  file << " </DataArray>\n";
86 }
87 
88 void WallVTKWriter::writeVTKPoints(std::fstream& file, VTKContainer& vtk) const
89 {
90  file << " <DataArray type=\"Float32\" Name=\"Position\" NumberOfComponents=\"3\" format=\"ascii\">\n";
91  for (const Vec3D& p : vtk.points)
92  {
93  file << '\t' << p << '\n';
94  }
95  file << " </DataArray>\n";
96 }
97 
98 void WallVTKWriter::writeVTKCells(std::fstream& file, VTKContainer& vtk) const
99 {
100  file << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n";
101  for (const std::vector<double>& c : vtk.triangleStrips)
102  {
103  file << '\t';
104  for (const double& i : c) file << i << ' ';
105  file << '\n';
106  }
107  file << " </DataArray>\n";
108  file << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n";
109  unsigned count = 0;
110  for (const std::vector<double>& c : vtk.triangleStrips)
111  {
112  count += c.size();
113  file << '\t' << count << '\n';
114  }
115  file << " </DataArray>\n";
116  file << " <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n";
117  for (const std::vector<double>& c : vtk.triangleStrips)
118  {
119  if (c.front() == c.back())
120  {
121  //polygon
122  file << "\t7\n";
123  }
124  else
125  {
126  //triangle strips
127  file << "\t6\n";
128  }
129  }
130  file << " </DataArray>\n";
131 }
unsigned int getSize() const
Gets the size of the particleHandler (including mpi and periodic particles)
Definition: BaseHandler.h:655
WallHandler & handler_
particle handler from which the particles should be written
Definition: BaseVTKWriter.h:72
void writeVTKPoints(std::fstream &file, VTKContainer &vtk) const
writes the point data to the vtu file (i.e.
const std::complex< Mdouble > i
Definition: ExtendedMath.h:50
std::fstream makeVTKFileWithHeader() const
void write(std::fstream &file, std::string name, std::function< double(BaseWall *)> f) const
void writeVTKCells(std::fstream &file, VTKContainer &vtk) const
writes the cell data to the vtu file (i.e.
std::vector< std::vector< double > > triangleStrips
Definition: BaseWall.h:39
#define PROCESSOR_ID
Definition: GeneralDefine.h:63
void getVTKData(VTKContainer &vtk) const
extracts vtk data from the wallHandler and stores it in a VTKContainer
Basic class for walls.
Definition: BaseWall.h:47
void writeVTK() const override
writes a vtk file
std::vector< Vec3D > points
Definition: BaseWall.h:38
Definition: Vector.h:49