Commit 10dc7b1c authored by Praetorius, Simon's avatar Praetorius, Simon
Browse files

pass ostream to writeSerialFile and writeParallelFile and set locale to "C"

parent 5ada89c6
......@@ -49,7 +49,7 @@ namespace Dune
protected:
/// Write a series of vtk files in a .pvd ParaView Data file
void writeFile (double time, std::string const& filename) const;
void writeFile (double time, std::ofstream& out) const;
protected:
VtkWriter vtkWriter_;
......
......@@ -31,23 +31,24 @@ void PvdWriter<W>
timesteps_.emplace_back(time, filename + ext);
vtkWriter_.write(filename + ext);
if (rank == 0)
writeFile(time, p.string() + ".pvd");
if (rank == 0) {
std::ofstream out(p.string() + ".pvd", std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out.imbue(std::locale::classic());
out << std::setprecision(datatype_ == Vtk::FLOAT32
? std::numeric_limits<float>::digits10+2
: std::numeric_limits<double>::digits10+2);
writeFile(time, out);
}
}
template <class W>
void PvdWriter<W>
::writeFile (double time, std::string const& filename) const
::writeFile (double time, std::ofstream& out) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
out << "<?xml version=\"1.0\"?>\n";
out << "<VTKFile"
<< " type=\"Collection\""
......
......@@ -70,7 +70,7 @@ void VtkTimeseriesWriter<W>
p.remove_filename();
p /= name.string();
std::string filenameBase = p.string() + "_ts";
std::string filename = p.string() + "_ts";
int rank = 0;
int num_ranks = 1;
......@@ -78,15 +78,36 @@ void VtkTimeseriesWriter<W>
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
if (num_ranks > 1)
filenameBase = p.string() + "_ts_p" + std::to_string(rank);
filename = p.string() + "_ts_p" + std::to_string(rank);
#endif
std::string filename = filenameBase + "." + vtkWriter_.getFileExtension();
vtkWriter_.writeTimeseriesSerialFile(filename, filenameMesh_, timesteps_, blocks_);
{ // write serial file
std::ofstream serial_out(filename + "." + vtkWriter_.getFileExtension(),
std::ios_base::ate | std::ios::binary);
assert(serial_out.is_open());
serial_out.imbue(std::locale::classic());
serial_out << std::setprecision(vtkWriter_.getDatatype() == Vtk::FLOAT32
? std::numeric_limits<float>::digits10+2
: std::numeric_limits<double>::digits10+2);
vtkWriter_.writeTimeseriesSerialFile(serial_out, filenameMesh_, timesteps_, blocks_);
}
#ifdef HAVE_MPI
if (num_ranks > 1 && rank == 0)
vtkWriter_.writeTimeseriesParallelFile(p.string() + "_ts", num_ranks, timesteps_);
if (num_ranks > 1 && rank == 0) {
// write parallel file
std::ofstream parallel_out(p.string() + "_ts.p" + vtkWriter_.getFileExtension(),
std::ios_base::ate | std::ios::binary);
assert(parallel_out.is_open());
parallel_out.imbue(std::locale::classic());
parallel_out << std::setprecision(vtkWriter_.getDatatype() == Vtk::FLOAT32
? std::numeric_limits<float>::digits10+2
: std::numeric_limits<double>::digits10+2);
vtkWriter_.writeTimeseriesParallelFile(parallel_out, p.string() + "_ts", num_ranks, timesteps_);
}
#endif
// remove all temporary data files
......
......@@ -80,12 +80,12 @@ namespace Dune
private:
/// Write a serial VTK file in Unstructured format
virtual void writeSerialFile (std::string const& filename) const = 0;
virtual void writeSerialFile (std::ofstream& out) const = 0;
/// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
/// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
/// for [i] in [0,...,size).
virtual void writeParallelFile (std::string const& pfilename, int size) const = 0;
virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const = 0;
/// Return the file extension of the serial file (not including the dot)
virtual std::string fileExtension () const = 0;
......
......@@ -28,7 +28,7 @@ void VtkWriterInterface<GV,DC>
p.remove_filename();
p /= name.string();
std::string filename = p.string() + "." + fileExtension();
std::string filename = p.string();
int rank = 0;
int num_ranks = 1;
......@@ -37,14 +37,34 @@ void VtkWriterInterface<GV,DC>
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &num_ranks);
if (num_ranks > 1)
filename = p.string() + "_p" + std::to_string(rank) + "." + fileExtension();
filename = p.string() + "_p" + std::to_string(rank);
#endif
writeSerialFile(filename);
{ // write serial file
std::ofstream serial_out(filename + "." + fileExtension(), std::ios_base::ate | std::ios::binary);
assert(serial_out.is_open());
serial_out.imbue(std::locale::classic());
serial_out << std::setprecision(datatype_ == Vtk::FLOAT32
? std::numeric_limits<float>::digits10+2
: std::numeric_limits<double>::digits10+2);
writeSerialFile(serial_out);
}
#ifdef HAVE_MPI
if (num_ranks > 1 && rank == 0)
writeParallelFile(p.string(), num_ranks);
if (num_ranks > 1 && rank == 0) {
// write parallel file
std::ofstream parallel_out(p.string() + ".p" + fileExtension(), std::ios_base::ate | std::ios::binary);
assert(parallel_out.is_open());
parallel_out.imbue(std::locale::classic());
parallel_out << std::setprecision(datatype_ == Vtk::FLOAT32
? std::numeric_limits<float>::digits10+2
: std::numeric_limits<double>::digits10+2);
writeParallelFile(parallel_out, p.string(), num_ranks);
}
#endif
}
......
......@@ -37,12 +37,12 @@ namespace Dune
private:
/// Write a serial VTK file in Unstructured format
virtual void writeSerialFile (std::string const& filename) const override;
virtual void writeSerialFile (std::ofstream& out) const override;
/// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
/// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
/// for [i] in [0,...,size).
virtual void writeParallelFile (std::string const& pfilename, int size) const override;
virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
virtual std::string fileExtension () const override
{
......
......@@ -18,18 +18,8 @@ namespace Dune {
template <class GV, class DC>
void VtkImageDataWriter<GV,DC>
::writeSerialFile (std::string const& filename) const
::writeSerialFile (std::ofstream& out) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
if (format_ == Vtk::ASCII) {
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
}
std::vector<pos_type> offsets; // pos => offset
out << "<VTKFile"
<< " type=\"ImageData\""
......@@ -74,12 +64,8 @@ void VtkImageDataWriter<GV,DC>
template <class GV, class DC>
void VtkImageDataWriter<GV,DC>
::writeParallelFile (std::string const& pfilename, int /*size*/) const
::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
{
std::string filename = pfilename + ".p" + this->fileExtension();
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out << "<VTKFile"
<< " type=\"PImageData\""
<< " version=\"1.0\""
......
......@@ -37,12 +37,12 @@ namespace Dune
private:
/// Write a serial VTK file in Unstructured format
virtual void writeSerialFile (std::string const& filename) const override;
virtual void writeSerialFile (std::ofstream& out) const override;
/// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
/// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
/// for [i] in [0,...,size).
virtual void writeParallelFile (std::string const& pfilename, int size) const override;
virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
void writeCoordinates (std::ofstream& out, std::vector<pos_type>& offsets,
Std::optional<std::size_t> timestep = {}) const;
......
......@@ -18,17 +18,8 @@ namespace Dune {
template <class GV, class DC>
void VtkRectilinearGridWriter<GV,DC>
::writeSerialFile (std::string const& filename) const
::writeSerialFile (std::ofstream& out) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
if (format_ == Vtk::ASCII) {
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
}
std::vector<pos_type> offsets; // pos => offset
out << "<VTKFile"
<< " type=\"RectilinearGrid\""
......@@ -74,12 +65,8 @@ void VtkRectilinearGridWriter<GV,DC>
template <class GV, class DC>
void VtkRectilinearGridWriter<GV,DC>
::writeParallelFile (std::string const& pfilename, int /*size*/) const
::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
{
std::string filename = pfilename + ".p" + this->fileExtension();
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out << "<VTKFile"
<< " type=\"PRectilinearGrid\""
<< " version=\"1.0\""
......
......@@ -37,12 +37,12 @@ namespace Dune
private:
/// Write a serial VTK file in Unstructured format
virtual void writeSerialFile (std::string const& filename) const override;
virtual void writeSerialFile (std::ofstream& out) const override;
/// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
/// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
/// for [i] in [0,...,size).
virtual void writeParallelFile (std::string const& pfilename, int size) const override;
virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
virtual std::string fileExtension () const override
{
......
......@@ -18,17 +18,8 @@ namespace Dune {
template <class GV, class DC>
void VtkStructuredGridWriter<GV,DC>
::writeSerialFile (std::string const& filename) const
::writeSerialFile (std::ofstream& out) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
if (format_ == Vtk::ASCII) {
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
}
std::vector<pos_type> offsets; // pos => offset
out << "<VTKFile"
<< " type=\"StructuredGrid\""
......@@ -72,12 +63,8 @@ void VtkStructuredGridWriter<GV,DC>
template <class GV, class DC>
void VtkStructuredGridWriter<GV,DC>
::writeParallelFile (std::string const& pfilename, int /*size*/) const
::writeParallelFile (std::ofstream& out, std::string const& pfilename, int /*size*/) const
{
std::string filename = pfilename + ".p" + this->fileExtension();
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out << "<VTKFile"
<< " type=\"PStructuredGrid\""
<< " version=\"1.0\""
......
......@@ -39,12 +39,12 @@ namespace Dune
private:
/// Write a serial VTK file in Unstructured format
virtual void writeSerialFile (std::string const& filename) const override;
virtual void writeSerialFile (std::ofstream& out) const override;
/// Write a parallel VTK file `pfilename.pvtu` in Unstructured format,
/// with `size` the number of pieces and serial files given by `pfilename_p[i].vtu`
/// for [i] in [0,...,size).
virtual void writeParallelFile (std::string const& pfilename, int size) const override;
virtual void writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const override;
/// Write a series of timesteps in one file
/**
......@@ -55,13 +55,14 @@ namespace Dune
* \param blocks A list of block sizes of the binary data stored in the files.
* Order: (points, cells, pointdata[0], celldata[0], pointdata[1], celldata[1],...)
**/
void writeTimeseriesSerialFile (std::string const& filename,
void writeTimeseriesSerialFile (std::ofstream& out,
std::string const& filenameMesh,
std::vector<std::pair<double, std::string>> const& timesteps,
std::vector<std::uint64_t> const& blocks) const;
/// Write parallel VTK file for series of timesteps
void writeTimeseriesParallelFile (std::string const& pfilename, int size,
void writeTimeseriesParallelFile (std::ofstream& out,
std::string const& pfilename, int size,
std::vector<std::pair<double, std::string>> const& timesteps) const;
virtual std::string fileExtension () const override
......
......@@ -18,18 +18,8 @@ namespace Dune {
template <class GV, class DC>
void VtkUnstructuredGridWriter<GV,DC>
::writeSerialFile (std::string const& filename) const
::writeSerialFile (std::ofstream& out) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
if (format_ == Vtk::ASCII) {
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
}
std::vector<pos_type> offsets; // pos => offset
out << "<VTKFile"
<< " type=\"UnstructuredGrid\""
......@@ -77,12 +67,8 @@ void VtkUnstructuredGridWriter<GV,DC>
template <class GV, class DC>
void VtkUnstructuredGridWriter<GV,DC>
::writeParallelFile (std::string const& pfilename, int size) const
::writeParallelFile (std::ofstream& out, std::string const& pfilename, int size) const
{
std::string filename = pfilename + ".pvtu";
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out << "<VTKFile"
<< " type=\"PUnstructuredGrid\""
<< " version=\"1.0\""
......@@ -136,19 +122,12 @@ void VtkUnstructuredGridWriter<GV,DC>
template <class GV, class DC>
void VtkUnstructuredGridWriter<GV,DC>
::writeTimeseriesSerialFile (std::string const& filename,
::writeTimeseriesSerialFile (std::ofstream& out,
std::string const& filenameMesh,
std::vector<std::pair<double, std::string>> const& timesteps,
std::vector<std::uint64_t> const& blocks) const
{
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
assert(is_a(format_, Vtk::APPENDED));
if (datatype_ == Vtk::FLOAT32)
out << std::setprecision(std::numeric_limits<float>::digits10+2);
else
out << std::setprecision(std::numeric_limits<double>::digits10+2);
std::vector<std::vector<pos_type>> offsets(timesteps.size()); // pos => offset
out << "<VTKFile"
......@@ -253,12 +232,11 @@ void VtkUnstructuredGridWriter<GV,DC>
template <class GV, class DC>
void VtkUnstructuredGridWriter<GV,DC>
::writeTimeseriesParallelFile (std::string const& pfilename, int size, std::vector<std::pair<double, std::string>> const& timesteps) const
::writeTimeseriesParallelFile (std::ofstream& out,
std::string const& pfilename,
int size,
std::vector<std::pair<double, std::string>> const& timesteps) const
{
std::string filename = pfilename + ".pvtu";
std::ofstream out(filename, std::ios_base::ate | std::ios::binary);
assert(out.is_open());
out << "<VTKFile"
<< " type=\"PUnstructuredGrid\""
<< " version=\"1.0\""
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment