AMDiS  0.1
The Adaptive Multi-Dimensional Simulation Toolbox
VTKSequenceWriter.hpp
1 #pragma once
2 
3 #include <vector>
4 #include <iostream>
5 #include <sstream>
6 #include <fstream>
7 #include <iomanip>
8 #include <memory>
9 
10 #include <dune/grid/io/file/vtk/common.hh>
11 #include <dune/common/path.hh>
12 
13 #include <dune/grid/io/file/vtk/vtkwriter.hh>
14 
15 #include <amdis/Environment.hpp>
16 
17 namespace AMDiS
18 {
20 
26  template <class GridView>
28  {
29  using VTKWriter = Dune::VTKWriter<GridView>;
30 
31  public:
33 
36  VTKSequenceWriter(std::shared_ptr<VTKWriter> vtkWriter)
37  : vtkWriter_(std::move(vtkWriter))
38  {}
39 
41 
45  template <class... Args>
46  VTKSequenceWriter(GridView const& gridView, Args&&... args)
47  : VTKSequenceWriter<GridView>(std::make_shared<VTKWriter>(gridView, FWD(args)...))
48  {}
49 
50  virtual ~VTKSequenceWriter() = default;
51 
53  std::shared_ptr<VTKWriter> const& vtkWriter() const
54  {
55  return vtkWriter_;
56  }
57 
59  template <class... Args>
60  void addCellData(Args&&... args)
61  {
62  vtkWriter_->addCellData(FWD(args)...);
63  }
64 
66  template <class... Args>
67  void addVertexData (Args&&... args)
68  {
69  vtkWriter_->addVertexData(FWD(args)...);
70  }
71 
73 
78  virtual void write(double time, std::string const& name, Dune::VTK::OutputType type = Dune::VTK::ascii)
79  {
80  pwrite(time, name, ".", "", type);
81  }
82 
84 
91  virtual void pwrite(double time, std::string const& name, std::string const& path, std::string const& extendpath,
92  Dune::VTK::OutputType type = Dune::VTK::ascii)
93  {
94  // remember current time step
95  unsigned int count = timesteps_.size();
96  timesteps_.push_back(time);
97 
98  // write VTK file
99  vtkWriter_->pwrite(seqName(name, count), Dune::concatPaths(path, extendpath), "", type);
100 
101  // write pvd file ... only on rank 0
102  if (Environment::mpiRank() == 0) {
103  std::ofstream pvdFile;
104  pvdFile.exceptions(std::ios_base::badbit | std::ios_base::failbit |
105  std::ios_base::eofbit);
106  std::string pvdname = path + "/" + name + ".pvd";
107  pvdFile.open(pvdname.c_str());
108  pvdFile << "<?xml version=\"1.0\"?>\n"
109  << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << Dune::VTK::getEndiannessString() << "\">\n"
110  << "<Collection>\n";
111  for (unsigned int i = 0; i <= count; ++i) {
112  std::string piecepath = extendpath;
113  std::string fullname = getParallelHeaderName(seqName(name,i), piecepath, Environment::mpiSize());
114  pvdFile << "<DataSet timestep=\"" << timesteps_[i]
115  << "\" part=\"0\" file=\""
116  << fullname << "\"/>\n";
117  }
118  pvdFile << "</Collection>\n"
119  << "</VTKFile>\n" << std::flush;
120  pvdFile.close();
121  }
122  }
123 
124  protected:
125  // create sequence name
126  static std::string seqName(std::string const& name, unsigned int count)
127  {
128  std::stringstream n;
129  n.fill('0');
130  n << name << "-" << std::setw(5) << count;
131  return n.str();
132  }
133 
134  static std::string getParallelHeaderName(std::string const& name, std::string const& path, int commSize)
135  {
136  std::ostringstream s;
137  if (path.size() > 0) {
138  s << path;
139  if (path[path.size()-1] != '/')
140  s << '/';
141  }
142  s << 's' << std::setw(4) << std::setfill('0') << commSize << '-';
143  s << name;
144  if (GridView::dimension > 1)
145  s << ".pvtu";
146  else
147  s << ".pvtp";
148  return s.str();
149  }
150 
151  private:
152  std::shared_ptr<VTKWriter> vtkWriter_;
153  std::vector<double> timesteps_;
154  };
155 
156 } // end namespace AMDiS
157 
void addVertexData(Args &&... args)
Adds a field of vertex data to the VTK file.
Definition: VTKSequenceWriter.hpp:67
std::shared_ptr< VTKWriter > const & vtkWriter() const
accessor for the underlying VTKWriter instance
Definition: VTKSequenceWriter.hpp:53
void addCellData(Args &&... args)
Adds a field of cell data to the VTK file.
Definition: VTKSequenceWriter.hpp:60
Definition: FieldMatVec.hpp:12
Contains all classes needed for solving linear and non linear equation systems.
Definition: AdaptBase.hpp:6
VTKSequenceWriter(GridView const &gridView, Args &&... args)
Set up the VTKSequenceWriter class by creating a timestep writer of type VTKWriter.
Definition: VTKSequenceWriter.hpp:46
virtual void pwrite(double time, std::string const &name, std::string const &path, std::string const &extendpath, Dune::VTK::OutputType type=Dune::VTK::ascii)
Writes VTK data for the given time,.
Definition: VTKSequenceWriter.hpp:91
static int mpiSize()
Return the MPI_Size of the group created by Dune::MPIHelper.
Definition: Environment.hpp:74
class to write pvd-files which contains a list of all collected vtk-files
Definition: VTKSequenceWriter.hpp:27
virtual void write(double time, std::string const &name, Dune::VTK::OutputType type=Dune::VTK::ascii)
Writes VTK data for the given time,.
Definition: VTKSequenceWriter.hpp:78
VTKSequenceWriter(std::shared_ptr< VTKWriter > vtkWriter)
Set up the VTKSequenceWriter class.
Definition: VTKSequenceWriter.hpp:36
static int mpiRank()
Return the MPI_Rank of the current processor.
Definition: Environment.hpp:68