version 3.10-dev
vtksequencewriter.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
14#ifndef DUMUX_VTKSEQUENCEWRITER_HH
15#define DUMUX_VTKSEQUENCEWRITER_HH
16
17#include <vector>
18#include <iostream>
19#include <sstream>
20#include <fstream>
21#include <iomanip>
22#include <memory>
23
24#include <dune/grid/io/file/vtk/common.hh>
25#include <dune/common/path.hh>
26
27
28namespace Dumux {
29
42 template<class VTKWriter>
44 {
45 std::shared_ptr<VTKWriter > vtkWriter_;
46 std::vector<double> timesteps_;
47 std::string name_,path_,extendpath_;
48 int rank_;
49 int size_;
50 public:
67 explicit VTKSequenceWriter( std::shared_ptr<VTKWriter > vtkWriter,
68 const std::string& name,
69 const std::string& path,
70 const std::string& extendpath,
71 int rank,
72 int size)
73 : vtkWriter_(vtkWriter),
74 name_(name), path_(path),
75 extendpath_(extendpath),
76 rank_(rank),
77 size_(size)
78 {}
79
81
82
88 void write (double time, Dune::VTK::OutputType type = Dune::VTK::ascii)
89 {
90 /* remember current time step */
91 unsigned int count = timesteps_.size();
92 timesteps_.push_back(time);
93
94 /* write VTK file */
95 if(size_==1)
96 vtkWriter_->write(Dune::concatPaths(path_,seqName(count)),type);
97 else
98 vtkWriter_->pwrite(seqName(count), path_,extendpath_,type);
99
100 /* write pvd file ... only on rank 0 */
101 if (rank_==0) {
102 std::ofstream pvdFile;
103 pvdFile.exceptions(std::ios_base::badbit | std::ios_base::failbit |
104 std::ios_base::eofbit);
105 std::string pvdname = name_ + ".pvd";
106 pvdFile.open(pvdname.c_str());
107 pvdFile << "<?xml version=\"1.0\"?> \n"
108 << "<VTKFile type=\"Collection\" version=\"0.1\" byte_order=\"" << Dune::VTK::getEndiannessString() << "\"> \n"
109 << "<Collection> \n";
110 for (unsigned int i=0; i<=count; i++)
111 {
112 // filename
113 std::string piecepath;
114 std::string fullname;
115 if(size_==1) {
116 piecepath = path_;
117 fullname = vtkWriter_->getSerialPieceName(seqName(i), piecepath);
118 }
119 else {
120 piecepath = Dune::concatPaths(path_, extendpath_);
121 fullname = vtkWriter_->getParallelHeaderName(seqName(i), piecepath, size_);
122 }
123 pvdFile << "<DataSet timestep=\"" << timesteps_[i]
124 << "\" group=\"\" part=\"0\" name=\"\" file=\""
125 << fullname << "\"/> \n";
126 }
127 pvdFile << "</Collection> \n"
128 << "</VTKFile> \n" << std::flush;
129 pvdFile.close();
130 }
131 }
132 private:
133
134 // create sequence name
135 std::string seqName(unsigned int count) const
136 {
137 std::stringstream n;
138 n.fill('0');
139 n << name_ << "-" << std::setw(5) << count;
140 return n.str();
141 }
142 };
143
144} // end namespace Dumux
145
146#endif
Base class to write pvd-files which contains a list of all collected vtk-files. This is a modified ve...
Definition: vtksequencewriter.hh:44
~VTKSequenceWriter()
Definition: vtksequencewriter.hh:80
VTKSequenceWriter(std::shared_ptr< VTKWriter > vtkWriter, const std::string &name, const std::string &path, const std::string &extendpath, int rank, int size)
Set up the VTKSequenceWriter class.
Definition: vtksequencewriter.hh:67
void write(double time, Dune::VTK::OutputType type=Dune::VTK::ascii)
Writes VTK data for the given time,.
Definition: vtksequencewriter.hh:88
Definition: adapt.hh:17