6#ifndef DUNE_GRID_IO_FILE_VTK_BASICWRITER_HH
7#define DUNE_GRID_IO_FILE_VTK_BASICWRITER_HH
20#include <dune/geometry/referenceelements.hh>
23#include <dune/grid/io/file/vtk/functionwriter.hh>
24#include <dune/grid/io/file/vtk/pvtuwriter.hh>
25#include <dune/grid/io/file/vtk/vtuwriter.hh>
34 template<
typename IteratorFactory>
36 typedef typename IteratorFactory::CellIterator CellIterator;
37 typedef typename IteratorFactory::CornerIterator CornerIterator;
38 typedef typename IteratorFactory::PointIterator PointIterator;
40 typedef typename IteratorFactory::Cell Cell;
43 typedef FunctionWriterBase<Cell> FunctionWriter;
46 typedef std::list<std::shared_ptr<FunctionWriter> > WriterList;
47 typedef typename WriterList::const_iterator WIterator;
49 typedef typename Cell::Geometry::ctype ctype;
50 static const unsigned celldim = Cell::mydimension;
51 typedef ReferenceElements<ctype, celldim> Refelems;
53 static const FileType fileType = celldim == 1
56 const IteratorFactory& factory;
61 CoordinatesWriter<Cell> coords;
62 typename IteratorFactory::ConnectivityWriter connectivity;
63 OffsetsWriter<Cell> offsets;
64 TypesWriter<Cell> types;
67 BasicWriter(
const IteratorFactory& factory_)
68 : factory(factory_), connectivity(factory.makeConnectivity())
76 void addCellData(
const std::shared_ptr<FunctionWriter>& writer) {
77 cellData.push_back(writer);
80 void addPointData(
const std::shared_ptr<FunctionWriter>& writer) {
81 pointData.push_back(writer);
95 void writeCellFunction(VTUWriter& vtuWriter,
96 FunctionWriter& functionWriter,
97 unsigned ncells)
const
99 if(functionWriter.beginWrite(vtuWriter, ncells)) {
100 const CellIterator& cellend = factory.endCells();
101 for(CellIterator cellit = factory.beginCells(); cellit != cellend;
106 functionWriter.endWrite();
109 void writePointFunction(VTUWriter& vtuWriter,
110 FunctionWriter& functionWriter,
111 unsigned npoints)
const
113 if(functionWriter.beginWrite(vtuWriter, npoints)) {
114 const PointIterator& pend = factory.endPoints();
115 for(PointIterator pit = factory.beginPoints(); pit != pend; ++pit)
116 functionWriter.write(pit->cell(), pit->duneIndex());
118 functionWriter.endWrite();
121 void writeCornerFunction(VTUWriter& vtuWriter,
122 FunctionWriter& functionWriter,
123 unsigned ncorners)
const
125 if(functionWriter.beginWrite(vtuWriter, ncorners)) {
126 const CornerIterator& cend = factory.endCorners();
127 for(CornerIterator cit = factory.beginCorners(); cit != cend; ++cit)
128 functionWriter.write(cit->cell(), cit->duneIndex());
130 functionWriter.endWrite();
138 static std::string getFirstScalar(
const WriterList& data) {
139 const WIterator& wend = data.end();
140 for(WIterator wit = data.begin(); wit != wend; ++wit)
141 if((*wit)->ncomps() == 1)
142 return (*wit)->name();
146 static std::string getFirstVector(
const WriterList& data) {
147 const WIterator& wend = data.end();
148 for(WIterator wit = data.begin(); wit != wend; ++wit)
149 if((*wit)->ncomps() == 3)
150 return (*wit)->name();
154 void writeCellData(VTUWriter& vtuWriter,
unsigned ncells)
const {
155 if(cellData.empty())
return;
157 vtuWriter.beginCellData(getFirstScalar(cellData),
158 getFirstVector(cellData));
159 const WIterator& wend = cellData.end();
160 for(WIterator wit = cellData.begin(); wit != wend; ++wit)
161 writeCellFunction(vtuWriter, **wit, ncells);
162 vtuWriter.endCellData();
165 void writePointData(VTUWriter& vtuWriter,
unsigned npoints)
const {
166 if(pointData.empty())
return;
168 vtuWriter.beginPointData(getFirstScalar(pointData),
169 getFirstVector(pointData));
170 const WIterator& wend = pointData.end();
171 for(WIterator wit = pointData.begin(); wit != wend; ++wit)
172 writePointFunction(vtuWriter, **wit, npoints);
173 vtuWriter.endPointData();
176 void writeGrid(VTUWriter& vtuWriter,
unsigned ncells,
unsigned npoints,
178 vtuWriter.beginPoints();
179 writePointFunction(vtuWriter, coords, npoints);
180 vtuWriter.endPoints();
182 vtuWriter.beginCells();
183 writeCornerFunction(vtuWriter, connectivity, ncorners);
184 writeCellFunction(vtuWriter, offsets, ncells);
185 if(fileType != polyData)
186 writeCellFunction(vtuWriter, types, ncells);
187 vtuWriter.endCells();
190 void writeAll(VTUWriter& vtuWriter,
unsigned ncells,
unsigned npoints,
192 writeCellData(vtuWriter, ncells);
193 writePointData(vtuWriter, npoints);
194 writeGrid(vtuWriter, ncells, npoints, ncorners);
198 void writePiece(
const std::string& filename, OutputType outputType) {
199 std::ofstream stream;
200 stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
201 std::ios_base::eofbit);
202 stream.open(filename.c_str(), std::ios::binary);
204 VTUWriter vtuWriter(stream, outputType, fileType);
206 unsigned ncells = std::distance(factory.beginCells(),
208 unsigned npoints = std::distance(factory.beginPoints(),
209 factory.endPoints());
210 unsigned ncorners = std::distance(factory.beginCorners(),
211 factory.endCorners());
213 vtuWriter.beginMain(ncells, npoints);
214 writeAll(vtuWriter, ncells, npoints, ncorners);
217 if(vtuWriter.beginAppended())
218 writeAll(vtuWriter, ncells, npoints, ncorners);
219 vtuWriter.endAppended();
240 void writeCollection(
const std::string name,
241 const std::string& piecename,
242 const std::string& piecepath)
244 std::ofstream stream;
245 stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
246 std::ios_base::eofbit);
247 stream.open(name.c_str(), std::ios::binary);
249 PVTUWriter writer(stream, fileType);
254 writer.beginPointData(getFirstScalar(pointData),
255 getFirstVector(pointData));
256 for(WIterator it=pointData.begin(); it!=pointData.end(); ++it)
257 (*it)->addArray(writer);
258 writer.endPointData();
261 writer.beginCellData(getFirstScalar(cellData),
262 getFirstVector(cellData));
263 for(WIterator it=cellData.begin(); it!=cellData.end(); ++it)
264 (*it)->addArray(writer);
265 writer.endCellData();
268 writer.beginPoints();
269 coords.addArray(writer);
273 for(
int i = 0; i < factory.comm().size(); ++i )
274 writer.addPiece(getParallelPieceName(piecename, piecepath, i));
295 std::string getParallelPieceName(
const std::string& name,
296 const std::string& path,
int rank)
const
298 std::ostringstream s;
299 if(path.size() > 0) {
301 if(path[path.size()-1] !=
'/')
304 s <<
's' << std::setw(4) << std::setfill(
'0') << factory.comm().size()
306 s <<
'p' << std::setw(4) << std::setfill(
'0') << rank <<
':';
325 std::string getParallelHeaderName(
const std::string& name,
326 const std::string& path)
const
328 std::ostringstream s;
329 if(path.size() > 0) {
331 if(path[path.size()-1] !=
'/')
334 s <<
's' << std::setw(4) << std::setfill(
'0') << factory.comm().size()
338 case polyData : s <<
".pvtp";
break;
357 std::string getSerialPieceName(
const std::string& name,
358 const std::string& path)
const
395 std::string pwrite(
const std::string& name,
const std::string& path,
396 const std::string& extendpath, OutputType outputType)
398 MPIGuard guard(factory.comm());
403 file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
404 std::ios_base::eofbit);
405 std::string piecepath =
concatPaths(path, extendpath);
406 std::string relpiecepath =
relativePath(path, piecepath);
409 std::string fullname = getParallelPieceName(name, piecepath,
410 factory.comm().rank());
411 writePiece(fullname, outputType);
414 fullname = getParallelHeaderName(name, path);
415 if(factory.comm().rank() == 0)
416 writeCollection(fullname, name, relpiecepath);
436 std::string write(
const std::string &name, OutputType outputType)
440 if(factory.comm().size() > 1)
441 return pwrite(name,
"",
"", outputType);
444 std::string pieceName = getSerialPieceName(name,
"");
446 writePiece(pieceName, outputType);
Common stuff for the VTKWriter.
FileType
which type of VTK file to write
Definition: common.hh:252
@ polyData
for .vtp files (PolyData)
Definition: common.hh:254
@ unstructuredGrid
for .vtu files (UnstructuredGrid)
Definition: common.hh:256
std::string relativePath(const std::string &newbase, const std::string &p)
compute a relative path between two paths
Definition: path.cc:153
std::string concatPaths(const std::string &base, const std::string &p)
concatenate two paths
Definition: path.cc:32
Implements a MPIGuard which detects an error on a remote process.
Dune namespace.
Definition: alignedallocator.hh:13
Utilities for handling filesystem paths.
static const ReferenceElement & general(const GeometryType &type)
get general reference elements
Definition: referenceelements.hh:198