6#ifndef DUNE_GRID_IO_FILE_VTK_BASICWRITER_HH
7#define DUNE_GRID_IO_FILE_VTK_BASICWRITER_HH
17#include <dune/common/parallel/mpiguard.hh>
18#include <dune/common/path.hh>
20#include <dune/geometry/referenceelements.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;
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;
62 typename IteratorFactory::ConnectivityWriter connectivity;
68 : factory(factory_), connectivity(factory.makeConnectivity())
76 void addCellData(
const std::shared_ptr<FunctionWriter>& writer) {
77 cellData.push_back(writer);
81 pointData.push_back(writer);
97 unsigned ncells)
const
99 if(functionWriter.
beginWrite(vtuWriter, ncells)) {
100 const CellIterator& cellend = factory.endCells();
101 for(CellIterator cellit = factory.beginCells(); cellit != cellend;
103 functionWriter.
write(*cellit, Refelems::general(cellit->type()).
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());
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());
140 if (d->ncomps() == 1)
147 if (d->ncomps() == 3)
153 if(cellData.empty())
return;
158 for (
auto&& cD : cellData)
164 if(pointData.empty())
return;
169 for (
auto&& pD : pointData)
192 writeGrid(vtuWriter, ncells, npoints, ncorners);
197 std::ofstream stream;
198 stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
199 std::ios_base::eofbit);
200 stream.open(filename.c_str(), std::ios::binary);
202 VTUWriter vtuWriter(stream, outputType, fileType);
204 unsigned ncells = std::distance(factory.beginCells(),
206 unsigned npoints = std::distance(factory.beginPoints(),
207 factory.endPoints());
208 unsigned ncorners = std::distance(factory.beginCorners(),
209 factory.endCorners());
212 writeAll(vtuWriter, ncells, npoints, ncorners);
216 writeAll(vtuWriter, ncells, npoints, ncorners);
239 const std::string& piecename,
240 const std::string& piecepath)
242 std::ofstream stream;
243 stream.exceptions(std::ios_base::badbit | std::ios_base::failbit |
244 std::ios_base::eofbit);
245 stream.open(name.c_str(), std::ios::binary);
254 for(WIterator it=pointData.begin(); it!=pointData.end(); ++it)
255 (*it)->addArray(writer);
261 for(WIterator it=cellData.begin(); it!=cellData.end(); ++it)
262 (*it)->addArray(writer);
271 for(
int i = 0; i < factory.comm().size(); ++i )
294 const std::string& path,
int rank)
const
296 std::ostringstream s;
297 if(path.size() > 0) {
299 if(path[path.size()-1] !=
'/')
302 s <<
's' << std::setw(4) << std::setfill(
'0') << factory.comm().size()
304 s <<
'p' << std::setw(4) << std::setfill(
'0') << rank <<
':';
324 const std::string& path)
const
326 std::ostringstream s;
327 if(path.size() > 0) {
329 if(path[path.size()-1] !=
'/')
332 s <<
's' << std::setw(4) << std::setfill(
'0') << factory.comm().size()
336 case polyData : s <<
".pvtp";
break;
356 const std::string& path)
const
359 case polyData :
return concatPaths(path, name+
".vtp");
362 return concatPaths(path, name);
393 std::string
pwrite(
const std::string& name,
const std::string& path,
394 const std::string& extendpath,
OutputType outputType)
396 MPIGuard guard(factory.comm());
401 file.exceptions(std::ios_base::badbit | std::ios_base::failbit |
402 std::ios_base::eofbit);
403 std::string piecepath = concatPaths(path, extendpath);
404 std::string relpiecepath = relativePath(path, piecepath);
408 factory.comm().rank());
413 if(factory.comm().rank() == 0)
438 if(factory.comm().size() > 1)
439 return pwrite(name,
"",
"", outputType);
Common stuff for the VTKWriter.
Include standard header files.
Definition agrid.hh:60
OutputType
How the bulk data should be stored in the file.
Definition common.hh:43
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
Definition basicwriter.hh:35
void writeCellData(VTUWriter &vtuWriter, unsigned ncells) const
Definition basicwriter.hh:152
BasicWriter(const IteratorFactory &factory_)
Definition basicwriter.hh:67
std::string getSerialPieceName(const std::string &name, const std::string &path) const
return name of a serial piece file
Definition basicwriter.hh:355
void writeCollection(const std::string name, const std::string &piecename, const std::string &piecepath)
write header file in parallel case to stream
Definition basicwriter.hh:238
void writeGrid(VTUWriter &vtuWriter, unsigned ncells, unsigned npoints, unsigned ncorners)
Definition basicwriter.hh:174
void writeAll(VTUWriter &vtuWriter, unsigned ncells, unsigned npoints, unsigned ncorners)
Definition basicwriter.hh:188
std::string pwrite(const std::string &name, const std::string &path, const std::string &extendpath, OutputType outputType)
write output; interface might change later
Definition basicwriter.hh:393
void clear()
Definition basicwriter.hh:84
void writeCellFunction(VTUWriter &vtuWriter, FunctionWriter &functionWriter, unsigned ncells) const
Definition basicwriter.hh:95
void writeCornerFunction(VTUWriter &vtuWriter, FunctionWriter &functionWriter, unsigned ncorners) const
Definition basicwriter.hh:121
std::string getParallelHeaderName(const std::string &name, const std::string &path) const
return name of a parallel header file
Definition basicwriter.hh:323
void writePointData(VTUWriter &vtuWriter, unsigned npoints) const
Definition basicwriter.hh:163
static std::string getFirstVector(const WriterList &data)
Definition basicwriter.hh:145
FunctionWriterBase< Cell > FunctionWriter
Definition basicwriter.hh:43
void writePointFunction(VTUWriter &vtuWriter, FunctionWriter &functionWriter, unsigned npoints) const
Definition basicwriter.hh:109
void writePiece(const std::string &filename, OutputType outputType)
Definition basicwriter.hh:196
void addCellData(const std::shared_ptr< FunctionWriter > &writer)
Definition basicwriter.hh:76
static std::string getFirstScalar(const WriterList &data)
Definition basicwriter.hh:138
std::string getParallelPieceName(const std::string &name, const std::string &path, int rank) const
return name of a parallel piece file
Definition basicwriter.hh:293
std::string write(const std::string &name, OutputType outputType)
write output (interface might change later)
Definition basicwriter.hh:434
void addPointData(const std::shared_ptr< FunctionWriter > &writer)
Definition basicwriter.hh:80
Base class for function writers.
Definition functionwriter.hh:34
virtual void write(const Cell &, const Domain &)
write at the given position
Definition functionwriter.hh:59
virtual void endWrite()=0
signal end of writing
virtual bool beginWrite(VTUWriter &writer, std::size_t nitems)=0
start writing with the given writer
writer for the Coordinates array
Definition functionwriter.hh:147
virtual void addArray(PVTUWriter &writer)
add this field to the given parallel writer
Definition functionwriter.hh:165
writer for the offsets array
Definition functionwriter.hh:300
writer for the types array
Definition functionwriter.hh:340
Dump a .vtu/.vtp files contents to a stream.
Definition pvtuwriter.hh:62
void endMain()
finish the main PolyData/UnstructuredGrid section
Definition pvtuwriter.hh:195
void endCellData()
finish CellData section
Definition pvtuwriter.hh:155
void beginMain(unsigned ghostLevel=0)
start the main PPolyData/PUnstructuredGrid section
Definition pvtuwriter.hh:189
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition pvtuwriter.hh:146
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition pvtuwriter.hh:120
void addPiece(const std::string &filename)
Add a serial piece to the output file.
Definition pvtuwriter.hh:215
void endPointData()
finish PointData section
Definition pvtuwriter.hh:129
void endPoints()
finish section for the point coordinates
Definition pvtuwriter.hh:171
void beginPoints()
start section for the point coordinates
Definition pvtuwriter.hh:166
Dump a .vtu/.vtp files contents to a stream.
Definition vtuwriter.hh:98
bool beginAppended()
start the appended data section
Definition vtuwriter.hh:345
void endAppended()
finish the appended data section
Definition vtuwriter.hh:359
void endCellData()
finish CellData section
Definition vtuwriter.hh:220
void beginMain(unsigned ncells, unsigned npoints)
start the main PolyData/UnstructuredGrid section
Definition vtuwriter.hh:310
void beginCells()
start section for the grid cells/PolyData lines
Definition vtuwriter.hh:274
void endPointData()
finish PointData section
Definition vtuwriter.hh:182
void beginCellData(const std::string &scalars="", const std::string &vectors="")
start CellData section
Definition vtuwriter.hh:205
void beginPointData(const std::string &scalars="", const std::string &vectors="")
start PointData section
Definition vtuwriter.hh:167
void endPoints()
finish section for the point coordinates
Definition vtuwriter.hh:249
void endCells()
start section for the grid cells/PolyData lines
Definition vtuwriter.hh:285
void beginPoints()
start section for the point coordinates
Definition vtuwriter.hh:238
void endMain()
finish the main PolyData/UnstructuredGrid section
Definition vtuwriter.hh:320