Dune Core Modules (unstable)

vtksequencewriterbase.hh
1 // SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file LICENSE.md in module root
2 // SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception
3 // -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 // vi: set et ts=4 sw=2 sts=2:
5 
6 #ifndef DUNE_GRID_IO_FILE_VTK_VTKSEQUENCEWRITERBASE_HH
7 #define DUNE_GRID_IO_FILE_VTK_VTKSEQUENCEWRITERBASE_HH
8 
9 #include <vector>
10 #include <iostream>
11 #include <sstream>
12 #include <fstream>
13 #include <iomanip>
14 #include <memory>
15 
17 #include <dune/common/path.hh>
18 
20 
21 namespace Dune {
22 
32  template<class GridView>
34  {
35  std::shared_ptr<VTKWriter<GridView> > vtkWriter_;
36  std::vector<double> timesteps_;
37  std::string name_,path_,extendpath_;
38  int rank_;
39  int size_;
40  public:
48  const std::string& name,
49  const std::string& path,
50  const std::string& extendpath,
51  int rank,
52  int size)
53  : vtkWriter_(vtkWriter),
54  name_(name), path_(path),
55  extendpath_(extendpath),
56  rank_(rank),
57  size_(size)
58  {}
59 
63  const std::shared_ptr< VTKWriter<GridView> >& vtkWriter() const
64  {
65  return vtkWriter_;
66  }
67 
69  void addCellData (const std::shared_ptr<const typename VTKWriter<GridView>::VTKFunction> &p)
70  {
71  vtkWriter_->addCellData(p);
72  }
73 
79  template<class V >
80  void addCellData (const V &v, const std::string &name, int ncomps=1)
81  {
82  vtkWriter_->addCellData(v, name, ncomps);
83  }
84 
86  void addVertexData (const std::shared_ptr<const typename VTKWriter<GridView>::VTKFunction> &p)
87  {
88  vtkWriter_->addVertexData(p);
89  }
90 
96  template<class V >
97  void addVertexData (const V &v, const std::string &name, int ncomps=1)
98  {
99  vtkWriter_->addVertexData(v, name, ncomps);
100  }
101 
102 
108  void write (double time, VTK::OutputType type = VTK::ascii)
109  {
110  /* remember current time step */
111  unsigned int count = timesteps_.size();
112  timesteps_.push_back(time);
113 
114  /* write VTK file */
115  if(size_==1)
116  vtkWriter_->write(concatPaths(path_,seqName(count)),type);
117  else
118  vtkWriter_->pwrite(seqName(count), path_,extendpath_,type);
119 
120  /* write pvd file ... only on rank 0 */
121  if (rank_==0) {
122  std::ofstream pvdFile;
123  pvdFile.exceptions(std::ios_base::badbit | std::ios_base::failbit |
124  std::ios_base::eofbit);
125  std::string pvdname = name_ + ".pvd";
126  pvdFile.open(pvdname.c_str());
127  pvdFile << "<?xml version=\"1.0\"?> \n"
128  << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << VTK::getEndiannessString() << "\"> \n"
129  << "<Collection> \n";
130  for (unsigned int i=0; i<=count; i++)
131  {
132  // filename
133  std::string piecepath;
134  std::string fullname;
135  if(size_==1) {
136  piecepath = path_;
137  fullname = vtkWriter_->getSerialPieceName(seqName(i), piecepath);
138  }
139  else {
140  piecepath = concatPaths(path_, extendpath_);
141  fullname = vtkWriter_->getParallelHeaderName(seqName(i), piecepath, size_);
142  }
143  pvdFile << "<DataSet timestep=\"" << timesteps_[i]
144  << "\" group=\"\" part=\"0\" name=\"\" file=\""
145  << fullname << "\"/> \n";
146  }
147  pvdFile << "</Collection> \n"
148  << "</VTKFile> \n" << std::flush;
149  pvdFile.close();
150  }
151  }
152 
156  void clear()
157  {
158  vtkWriter_->clear();
159  }
160 
164  const std::vector<double>& getTimeSteps() const
165  {
166  return timesteps_;
167  }
168 
174  void setTimeSteps(const std::vector<double>& timesteps)
175  {
176  timesteps_ = timesteps;
177  }
178 
179  private:
180 
181  // create sequence name
182  std::string seqName(unsigned int count) const
183  {
184  std::stringstream n;
185  n.fill('0');
186  n << name_ << "-" << std::setw(5) << count;
187  return n.str();
188  }
189  };
190 
191 } // end namespace Dune
192 
193 #endif
A base class for grid functions with any return type and dimension.
Definition: function.hh:42
Base class to write pvd-files which contains a list of all collected vtk-files.
Definition: vtksequencewriterbase.hh:34
void addVertexData(const V &v, const std::string &name, int ncomps=1)
Adds a field of vertex data to the VTK file.
Definition: vtksequencewriterbase.hh:97
void write(double time, VTK::OutputType type=VTK::ascii)
Writes VTK data for the given time,.
Definition: vtksequencewriterbase.hh:108
void addVertexData(const std::shared_ptr< const typename VTKWriter< GridView >::VTKFunction > &p)
Adds a field of vertex data to the VTK file.
Definition: vtksequencewriterbase.hh:86
void addCellData(const V &v, const std::string &name, int ncomps=1)
Adds a field of cell data to the VTK file.
Definition: vtksequencewriterbase.hh:80
VTKSequenceWriterBase(std::shared_ptr< VTKWriter< GridView > > vtkWriter, const std::string &name, const std::string &path, const std::string &extendpath, int rank, int size)
Set up the VTKSequenceWriterBase class.
Definition: vtksequencewriterbase.hh:47
const std::shared_ptr< VTKWriter< GridView > > & vtkWriter() const
Definition: vtksequencewriterbase.hh:63
void addCellData(const std::shared_ptr< const typename VTKWriter< GridView >::VTKFunction > &p)
Adds a field of cell data to the VTK file.
Definition: vtksequencewriterbase.hh:69
const std::vector< double > & getTimeSteps() const
Retrieve the current list of time steps.
Definition: vtksequencewriterbase.hh:164
void setTimeSteps(const std::vector< double > &timesteps)
Set the current list of time steps.
Definition: vtksequencewriterbase.hh:174
void clear()
Clears all VTK data added to the VTK writer.
Definition: vtksequencewriterbase.hh:156
Writer for the output of grid functions in the vtk format.
Definition: vtkwriter.hh:95
Common stuff for the VTKWriter.
OutputType
How the bulk data should be stored in the file.
Definition: common.hh:43
std::string concatPaths(const std::string &base, const std::string &p)
concatenate two paths
Definition: path.cc:32
Dune namespace.
Definition: alignedallocator.hh:13
constexpr std::integral_constant< std::size_t, sizeof...(II)> size(std::integer_sequence< T, II... >)
Return the size of the sequence.
Definition: integersequence.hh:75
Utilities for handling filesystem paths.
Provides file i/o for the visualization toolkit.
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (Apr 29, 22:29, 2024)