MercuryDPM  Trunk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseVTKWriter.h
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 
26 
27 #ifndef VTKWRITER_H
28 #define VTKWRITER_H
29 
30 #include <string>
31 #include <fstream>
32 #include "Logger.h"
33 
38 template<typename H>
40 {
41 
42 public:
43 
44  BaseVTKWriter(H& handler) : handler_(handler)
45  {
46  fileCounter = 0;
47  }
48 
50  {
51  fileCounter = other.fileCounter;
52  }
53 
54  virtual void writeVTK() const = 0;
55 
56  unsigned getFileCounter() const
57  {
58  return fileCounter;
59  }
60 
61  void setFileCounter(unsigned fileCounter)
62  {
63  this->fileCounter = fileCounter;
64  }
65 
66 protected:
67  std::fstream makeVTKFileWithHeader() const;
68 
69  void writeVTKFooterAndClose(std::fstream& file) const;
70 
73 
74  mutable unsigned int fileCounter;
75 
76 };
77 
79 template<typename T>
81 {
82  //extract the word "Wall" or "Particle" from the VTK writer name
83  std::string name = handler_.getName();
84  name = name.substr(0, name.length() - 7);
85 
86  //determine name of output file
87  std::string fileName;
88 #ifdef MERCURY_USE_MPI
89  if (NUMBER_OF_PROCESSORS > 1 && name != "Wall")
90  {
91  fileName = handler_.getDPMBase()->getName() + "Processor_" + std::to_string(PROCESSOR_ID) +
92  '_' + name + '_' + std::to_string(fileCounter++) + ".vtu";
93  }
94  else
95  {
96  fileName = handler_.getDPMBase()->getName() +
97  name + '_' +
98  std::to_string(fileCounter++) + ".vtu";
99  }
100 #else
101  fileName = handler_.getDPMBase()->getName() +
102  name + '_' +
103  std::to_string(fileCounter++) + ".vtu";
104 #endif
105 
106  //open output file
107  std::fstream file;
108  file.open(fileName.c_str(), std::ios_base::out);
109  if (file.fail())
110  {
111  logger(WARN, "File % could not be opened", fileName);
112  }
113 
114  // write output file header
115  file << "<?xml version=\"1.0\"?>\n";
116  file << "<!-- time " << handler_.getDPMBase()->getTime() << "-->\n";
117  file << "<VTKFile type=\"UnstructuredGrid\" version=\"0.1\" byte_order=\"LittleEndian\">\n";
118  file << "<UnstructuredGrid>\n";
119  return file;
120 }
121 
122 template<typename T>
123 void BaseVTKWriter<T>::writeVTKFooterAndClose(std::fstream& file) const
124 {
125  // write output file footer
126  file << "<Cells>\n";
127  file << " <DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">\n";
128  file << " </DataArray>\n";
129  file << " <DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">\n";
130  file << " </DataArray>\n";
131  file << " <DataArray type=\"UInt8\" Name=\"types\" format=\"ascii\">\n";
132  file << " </DataArray>\n";
133  file << "</Cells>\n";
134  file << "</Piece>\n";
135  file << "</UnstructuredGrid>\n";
136  file << "</VTKFile>\n";
137  // close output file
138  file.close();
139 }
140 
141 #endif
void setFileCounter(unsigned fileCounter)
Definition: BaseVTKWriter.h:61
unsigned getFileCounter() const
Definition: BaseVTKWriter.h:56
BaseVTKWriter(const BaseVTKWriter &other)
Definition: BaseVTKWriter.h:49
virtual void writeVTK() const =0
H & handler_
particle handler from which the particles should be written
Definition: BaseVTKWriter.h:72
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
std::fstream makeVTKFileWithHeader() const
Definition: BaseVTKWriter.h:80
void writeVTKFooterAndClose(std::fstream &file) const
unsigned int fileCounter
Definition: BaseVTKWriter.h:74
LL< Log::WARN > WARN
Warning log level.
Definition: Logger.cc:54
BaseVTKWriter(H &handler)
Definition: BaseVTKWriter.h:44
#define PROCESSOR_ID
Definition: GeneralDefine.h:63
#define NUMBER_OF_PROCESSORS
For the MPI communication routines this quantity is often required. defining this macro makes the cod...
Definition: GeneralDefine.h:62
std::string to_string(const T &n)
Definition: Helpers.h:227