MercuryDPM  Alpha
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
WallHandler.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, 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 
26 
27 #include <Math/Helpers.h>
28 #include "WallHandler.h"
29 #include "Walls/BaseWall.h"
30 #include "Walls/CylindricalWall.h"
33 #include "Walls/InfiniteWall.h"
35 #include "Walls/Screw.h"
36 #include "Walls/Coil.h"
37 #include "DPMBase.h"
38 
43 {
45  clear();
46  logger(DEBUG, "WallHandler::WallHandler() finished");
47 }
48 
56 {
58  clear();
59  setDPMBase(WH.getDPMBase());
61  logger(DEBUG, "WallHandler::WallHandler(const WallHandler &WH) finished");
62 }
63 
71 {
72  if(this != &rhs)
73  {
74  clear();
75  setDPMBase(rhs.getDPMBase());
77  }
78  logger(DEBUG, "WallHandler::operator = (const WallHandler& rhs) finished");
79  return *this;
80 }
81 
83 {
84  logger(DEBUG, "WallHandler::~WallHandler() finished");
85 }
86 
93 {
94  if (W->getSpecies() == nullptr)
95  {
96  logger(WARN, "WARNING: The wall with ID % that is added in WallHandler::addObject "
97  "does not have a species yet. Please make sure that you have "
98  "set the species somewhere in the driver code.", W->getId());
99  }
100  //Puts the wall in the Wall list
102  //set the particleHandler pointer
103  W->setHandler(this);
104 }
105 
109 void WallHandler::readObject(std::istream& is)
110 {
111  std::string type;
112  is >> type;
113 
114  logger.log(Log::DEBUG, "WallHandler::readObject(is): reading type %.", type);
116 
117  if (type == "CylindricalWall")
118  {
119  CylindricalWall wall;
120  is >> wall;
121  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
122  copyAndAddObject(wall);
123  }
124  else if (type == "AxisymmetricIntersectionOfWalls")
125  {
127  is >> wall;
128  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
129  copyAndAddObject(wall);
130  }
131  else if (type == "IntersectionOfWalls")
132  {
133  IntersectionOfWalls wall;
134  is >> wall;
135  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
136  copyAndAddObject(wall);
137  }
138  else if (type == "InfiniteWall")
139  {
140  InfiniteWall wall;
141  is >> wall;
142  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
143  copyAndAddObject(wall);
144  }
145  else if (type == "InfiniteWallWithHole")
146  {
148  is >> wall;
149  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
150  copyAndAddObject(wall);
151  }
152  else if (type == "Screw")
153  {
154  Screw wall;
155  is >> wall;
156  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
157  copyAndAddObject(wall);
158  }
159  else if (type == "Coil")
160  {
161  Coil wall;
162  is >> wall;
163  wall.setSpecies(sH.getObject(wall.getIndSpecies()));
164  copyAndAddObject(wall);
165  }
166  //for backward compatibility (before svnversion ~2360)
167  else if (type == "numFiniteWalls")
168  {
169  readOldObject(is);
170  }
171  else if (type == "TriolietScrew"||type == "TriolietBaseScrew"||type=="RestrictedWall") {
172  logger(WARN, "Wall type: % cannot be restarted and is ignored", type);
173  InfiniteWall wall;
174  wall.setSpecies(sH.getObject(0));
175  copyAndAddObject(wall);
176  }
177  else if (!getDPMBase()->readUserDefinedWall(type,is))
178  {
179  logger(ERROR, "Wall type: % not understood in restart file", type);
180  }
181 }
182 
190 void WallHandler::readOldObject(std::istream& is)
191 {
192  //read in next line
193  std::stringstream line(std::stringstream::in | std::stringstream::out);
195  logger(VERBOSE, line.str());
196 
197  std::string dummy;
198  unsigned int numWalls;
199  Mdouble position;
200  Vec3D normal;
201  line >> numWalls;
202 
203  if (numWalls == 0)
204  {
205  InfiniteWall wall;
206  wall.setSpecies(getDPMBase()->speciesHandler.getObject(0));
207  line >> dummy >> normal >> dummy >> position;
208  wall.set(normal, position*normal);
209  copyAndAddObject(wall);
210  }
211  else
212  {
213  IntersectionOfWalls wall;
214  wall.setSpecies(getDPMBase()->speciesHandler.getObject(0));
215  for (unsigned int i = 0; i < numWalls; ++i)
216  {
217  line >> dummy >> normal >> dummy >> position;
218  wall.addObject(normal, position*normal);
219  }
220  copyAndAddObject(wall);
221  }
222 }
223 
227 std::string WallHandler::getName() const
228 {
229  return "WallHandler";
230 }
231 
240 {
241  VTKContainer vtk;
242 
243  //set capacity of points and cells based on the previous time step
244  static unsigned int capacityPoints = 0;
245  static unsigned int capacityTriangleStrips = 0;
246 
247  vtk.triangleStrips.reserve(capacityPoints);
248  vtk.points.reserve(capacityTriangleStrips);
249 
250  //add all wall data to the point and cell arrays
251  for (const auto& w: *this)
252  {
253  w->writeVTK(vtk);
254  logger(DEBUG, "points: %, cells: ", vtk.points.size(), vtk.triangleStrips.size());
255  }
256 
257  //write
258  writeVTKFile(vtk);
259 }
260 
268 {
269 // logger(INFO, "%, writing vtk file, points: %, cells: %",
270 // getDPMBase()->getTime(), vtk.points.size(), vtk.triangleStrips.size());
271  static unsigned fileCounter = 0;
272  const std::string fileName = getDPMBase()->getName() + "Wall_" +
273  std::to_string(fileCounter++) + ".vtu";
274  logger(INFO, "writing % (t=%, N=%)", fileName, getDPMBase()->getTime(), this->getNumberOfObjects());
275 
276  std::fstream file;
277  file.open(fileName.c_str(), std::ios::out);
278  if (file.fail())
279  {
281  logger(WARN, "Error in writeToFile: file % could not be opened", fileName);
282  }
283  file << "<?xml version=\"1.0\"?>\n\n";
284  file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
285  file << "<UnstructuredGrid>\n";
286  file << "<Piece NumberOfPoints=\"" << vtk.points.size() << "\" NumberOfCells=\"" << vtk.triangleStrips.size() << "\">\n";
287  file << "<Points>\n";
288  file << " <DataArray type=\"Float32\" Name=\"Position\" NumberOfComponents=\"3\" format=\"ascii\">\n";
289  for (const Vec3D& p : vtk.points)
290  {
291  file << '\t' << p << '\n';
292  }
293  file << " </DataArray>\n";
294  file << "</Points>\n";
295  file << "<Cells>\n";
296  file << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n";
297  for (const std::vector<double>& c : vtk.triangleStrips)
298  {
299  file << '\t';
300  for (const double& i : c)
301  {
302  file << i << ' ';
303  }
304  file << '\n';
305  }
306  file << " </DataArray>\n";
307  file << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n";
308  unsigned count = 0;
309  for (const std::vector<double>& c : vtk.triangleStrips)
310  {
311  count += c.size();
312  file << '\t' << count << '\n';
313  }
314  file << " </DataArray>\n";
315  file << " <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n";
316  for (const std::vector<double>& c : vtk.triangleStrips)
317  {
318  if (c.front()==c.back())
319  {
320  //polygon
321  file << "\t7\n";
322  } else
323  {
324  //triangle strips
325  file << "\t6\n";
326  }
327  }
328  file << " </DataArray>\n";
329  file << "</Cells>\n";
330  file << "</Piece>\n";
331  file << "</UnstructuredGrid>\n";
332  file << "</VTKFile>\n";
333  file.close();
334 }
335 
342 {
343  const std::string fileName = getDPMBase()->getName() + "BoundingBox.vtu";
344  logger(INFO, "% writing vtk file for bounding box: %",
345  getDPMBase()->getTime(), fileName);
346  std::fstream file;
347  file.open(fileName.c_str(), std::ios::out);
348  if (file.fail())
349  {
351  logger(WARN, "Error in writeToFile: file could not be opened");
352  }
353  file << "<?xml version=\"1.0\"?>\n\n";
354  file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
355  file << "<UnstructuredGrid>\n";
356  file << "<Piece NumberOfPoints=\"8\" NumberOfCells=\"1\">\n";
357  file << "<Points>\n";
358  file << " <DataArray type=\"Float32\" Name=\"Position\" NumberOfComponents=\"3\" format=\"ascii\">\n";
359  Vec3D P[2] = {getDPMBase()->getMax(), getDPMBase()->getMin()};
360  for (unsigned i = 0; i < 2; i++)
361  {
362  for (unsigned j = 0; j < 2; j++)
363  {
364  for (unsigned k = 0; k < 2; k++)
365  {
366  Vec3D p = Vec3D(P[i].X, P[j].Y, P[k].Z);
367  file << '\t' << p << '\n';
368  }
369  }
370  }
372  file << " </DataArray>\n";
373  file << "</Points>\n";
374  file << "<Cells>\n";
375  file <<
376  " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n";
377  file << "\t0 1 3 2 0 4 5 1 5 7 3 7 6 2 6 4\n";
378  file << " </DataArray>\n";
379  file << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n";
380  file <<
381  "\t16\n"; //offset into the connectivity array for the end of each cell.
382  file << " </DataArray>\n";
383  file << " <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n";
384  file << "\t4\n";
385  file << " </DataArray>\n";
386  file << "</Cells>\n";
387  file << "</Piece>\n";
388  file << "</UnstructuredGrid>\n";
389  file << "</VTKFile>\n";
390  file.close();
391 }
Container to store all ParticleSpecies.
unsigned int getId() const
Returns the unique identifier of any particular object.
Definition: BaseObject.cc:116
A IntersectionOfWalls is convex polygon defined as an intersection of InfiniteWall's.
This function defines an Archimedes' screw in the z-direction from a (constant) starting point...
Definition: Screw.h:38
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
void setDPMBase(DPMBase *DPMBase)
Sets the problem that is solved using this handler.
void readOldObject(std::istream &is)
Reads a BaseWall into the WallHandler from old-style restart data.
Definition: WallHandler.cc:190
const ParticleSpecies * getSpecies() const
Returns a pointer to the species of this BaseInteractable.
Vec3D getMin() const
Return the "bottom left" corner of the domain, a vector with xMin_, yMin_ and zMin_.
Definition: DPMBase.h:330
virtual void setHandler(WallHandler *handler)
A function which sets the WallHandler for this BaseWall.
Definition: BaseWall.cc:76
double Mdouble
void addObject(Vec3D normal, Vec3D point)
Adds a wall to the set of infinite walls, given an outward normal vector s.t. normal*x=normal*point.
void setSpecies(const ParticleSpecies *species)
sets species of subwalls as well
void readObject(std::istream &is)
Reads BaseWall into the WallHandler from restart data.
Definition: WallHandler.cc:109
void writeVTKFile(const VTKContainer vtk) const
Writes the data collected in writeVTK into vtk files, one per timestep.
Definition: WallHandler.cc:267
void getLineFromStringStream(std::istream &in, std::stringstream &out)
Reads a line from one stringstream into another, and prepares the latter for reading in...
Definition: Helpers.cc:396
std::vector< std::vector< double > > triangleStrips
Definition: BaseWall.h:36
T * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:451
void writeVTKBoundingBox() const
Writes a bounding box around the domain into a vtk file.
Definition: WallHandler.cc:341
WallHandler()
Default constructor, it creates an empty WallHandler.
Definition: WallHandler.cc:42
std::enable_if<!std::is_pointer< U >::value, U * >::type copyAndAddObject(const U &object)
Creates a copy of a Object and adds it to the BaseHandler.
Basic class for walls.
Definition: BaseWall.h:44
unsigned int getNumberOfObjects() const
Gets the number of Object in this BaseHandler.
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. elastic, linear visco-elastic... et cetera...
Definition: DPMBase.h:991
Vec3D getMax() const
Return the "upper right" corner of the domain, a vector with xMin_, yMin_ and zMin_.
Definition: DPMBase.h:335
A AxisymmetricIntersectionOfWalls is an axisymmetric wall, defined by rotating a twodimensional Inter...
Container to store all BaseWall.
Definition: WallHandler.h:42
std::string to_string(const T &n)
Definition: Helpers.h:203
std::vector< Vec3D > points
Definition: BaseWall.h:35
void set(Vec3D normal, Vec3D point)
Defines a standard wall, given an outward normal vector s.t. normal*x=normal*point for all x of the w...
Definition: InfiniteWall.cc:79
virtual void addObject(T *object)
Adds a new Object to the BaseHandler.
Definition: BaseHandler.h:313
This is a class defining walls.
Definition: InfiniteWall.h:47
void addObject(BaseWall *W)
Adds a BaseWall to the WallHandler.
Definition: WallHandler.cc:92
WallHandler operator=(const WallHandler &rhs)
Assignment operator that copies the pointer to the DPMBase and all BaseWall in the given WallHandler...
Definition: WallHandler.cc:70
~WallHandler()
Destructor, it destructs the WallHandler and all BaseWall it contains.
Definition: WallHandler.cc:82
void copyContentsFromOtherHandler(const BaseHandler< BaseWall > &BH)
Function that copies the contents (vector of pointers, maxObject_, nextId_, DPMBase_) from one handle...
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
DPMBase * getDPMBase()
Gets the problem that is solved using this handler.
Definition: BaseHandler.h:543
This class defines a coil in the z-direction from a (constant) starting point, a (constant) length L...
Definition: Coil.h:40
const std::string & getName() const
Returns the name of the file. Does not allow to change it though.
Definition: Files.cc:132
void writeVTK() const
Writes all walls into a vtk format, consisting of points (edges) and cells (faces).
Definition: WallHandler.cc:239
void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0...
void setSpecies(const ParticleSpecies *species)
Define the species of this wall.
Definition: BaseWall.cc:113
std::string getName() const
Returns the name of the handler, namely the string "WallHandler".
Definition: WallHandler.cc:227