diff --git a/dune/vtk/filewriter.hh b/dune/vtk/filewriter.hh index ffe21e06698895a5f018ebc1c2fdea0e5d31e3c1..217c79488900051f03aeda8967bed3792d9b4990 100644 --- a/dune/vtk/filewriter.hh +++ b/dune/vtk/filewriter.hh @@ -11,7 +11,7 @@ namespace Dune virtual ~FileWriter () = default; /// Write to file given by `filename` - virtual void write (std::string const& filename) = 0; + virtual void write (std::string const& filename) const = 0; }; } // end namespace Dune diff --git a/dune/vtk/pvdwriter.hh b/dune/vtk/pvdwriter.hh index 2d0b5d8e46ec487f147bfaab88272a9461281d74..76fe74bd0b84fa70821ee9385acfc3862c35c2d1 100644 --- a/dune/vtk/pvdwriter.hh +++ b/dune/vtk/pvdwriter.hh @@ -6,7 +6,7 @@ #include <tuple> #include <dune/vtk/vtktypes.hh> -#include <dune/vtk/vtkwriterinterface.hh> +// #include <dune/vtk/vtkwriterinterface.hh> namespace Dune { @@ -16,26 +16,20 @@ namespace Dune { using Self = PvdWriter; - static_assert(IsVtkWriter<VtkWriter>::value, "Writer must implement the VtkWriterInterface."); + // static_assert(IsVtkWriter<VtkWriter>::value, "Writer must implement the VtkWriterInterface."); public: /// Constructor, creates a VtkWriter with constructor arguments forwarded template <class... Args, disableCopyMove<Self,Args...> = 0> explicit PvdWriter (Args&&... args) : vtkWriter_{std::forward<Args>(args)...} - {} - - /// Write the attached data to the file - void write (double time, std::string const& fn) { - write(time, fn, Vtk::BINARY); + format_ = vtkWriter_.getFormat(); + datatype_ = vtkWriter_.getDatatype(); } - /// Write the attached data to the file with \ref Vtk::FormatTypes and \ref Vtk::DataTypes - void write (double time, - std::string const& fn, - Vtk::FormatTypes format, - Vtk::DataTypes datatype = Vtk::FLOAT32); + /// Write the attached data to the file + void write (double time, std::string const& fn) const; /// Attach point data to the writer, \see VtkFunction for possible arguments template <class Function, class... Args> @@ -59,9 +53,10 @@ namespace Dune protected: VtkWriter vtkWriter_; - std::vector<std::pair<double, std::string>> timeSeries_; Vtk::FormatTypes format_; Vtk::DataTypes datatype_; + + mutable std::vector<std::pair<double, std::string>> timesteps_; }; } // end namespace Dune diff --git a/dune/vtk/pvdwriter.impl.hh b/dune/vtk/pvdwriter.impl.hh index 5c1a528cdb6a0c01fe64e3d502c000a44eb05496..83a7666e5c62cd49802ccfa03b42709b18ce54c6 100644 --- a/dune/vtk/pvdwriter.impl.hh +++ b/dune/vtk/pvdwriter.impl.hh @@ -9,23 +9,15 @@ namespace Dune { template <class W> void PvdWriter<W> - ::write (double time, std::string const& fn, Vtk::FormatTypes format, Vtk::DataTypes datatype) + ::write (double time, std::string const& fn) const { - format_ = format; - datatype_ = datatype; - -#ifndef HAVE_ZLIB - if (format_ == Vtk::COMPRESSED) - format_ = Vtk::BINARY; -#endif - auto p = filesystem::path(fn); auto name = p.stem(); p.remove_filename(); p /= name.string(); std::string ext = "." + vtkWriter_.getFileExtension(); - std::string filename = p.string() + "_t" + std::to_string(timeSeries_.size()); + std::string filename = p.string() + "_t" + std::to_string(timesteps_.size()); int rank = 0; int num_ranks = 1; @@ -36,8 +28,8 @@ void PvdWriter<W> ext = ".p" + vtkWriter_.getFileExtension(); #endif - timeSeries_.emplace_back(time, filename + ext); - vtkWriter_.write(filename + ext, format_, datatype_); + timesteps_.emplace_back(time, filename + ext); + vtkWriter_.write(filename + ext); if (rank == 0) writeFile(time, p.string() + ".pvd"); @@ -67,7 +59,7 @@ void PvdWriter<W> out << "<Collection>\n"; // Write all timesteps - for (auto const& timestep : timeSeries_) { + for (auto const& timestep : timesteps_) { out << "<DataSet" << " timestep=\"" << timestep.first << "\"" << " part=\"0\"" diff --git a/dune/vtk/vtktimeserieswriter.hh b/dune/vtk/vtktimeserieswriter.hh index bb4d646942fa7dbe1af1908ee6d97970934c3daf..da15804ba374c1126dd32f5be37fada05f3074d4 100644 --- a/dune/vtk/vtktimeserieswriter.hh +++ b/dune/vtk/vtktimeserieswriter.hh @@ -57,7 +57,7 @@ namespace Dune /// Writes all timesteps to single timeseries file. // NOTE: requires an aforging call to \ref writeTimestep - virtual void write (std::string const& fn) override; + virtual void write (std::string const& fn) const override; /// Attach point data to the writer, \see VtkFunction for possible arguments template <class Function, class... Args> @@ -84,8 +84,8 @@ namespace Dune // block size of attached data std::vector<std::uint64_t> blocks_; - std::string filenameMesh_; - std::vector<std::pair<double, std::string>> timesteps_; + mutable std::string filenameMesh_; + mutable std::vector<std::pair<double, std::string>> timesteps_; }; } // end namespace Dune diff --git a/dune/vtk/vtktimeserieswriter.impl.hh b/dune/vtk/vtktimeserieswriter.impl.hh index 37b35527c9b6d9df66ed7171667470925e8b9a18..beac3b66521a21d59e95288aba0aceb9b30bc993 100644 --- a/dune/vtk/vtktimeserieswriter.impl.hh +++ b/dune/vtk/vtktimeserieswriter.impl.hh @@ -30,7 +30,7 @@ void VtkTimeseriesWriter<W> auto tmp = tmpDir_; tmp /= name.string(); - vtkWriter_.dataCollector_.update(); + vtkWriter_.update(); std::string filenameBase = tmp.string(); @@ -60,7 +60,7 @@ void VtkTimeseriesWriter<W> template <class W> void VtkTimeseriesWriter<W> - ::write (std::string const& fn) + ::write (std::string const& fn) const { assert( initialized_ ); assert( timesteps_.size() < 1000 ); // maximal number of allowed timesteps in timeseries file @@ -96,6 +96,7 @@ void VtkTimeseriesWriter<W> ec = std::remove(timestep.second.c_str()); assert(ec == 0); } + timesteps_.clear(); } } // end namespace Dune diff --git a/dune/vtk/vtkwriterinterface.hh b/dune/vtk/vtkwriterinterface.hh index 4bd647589a9fabe8212f498a60fd46d9753e598a..4fa6b7ff7f431c7477a8e53311b20f7432ad5988 100644 --- a/dune/vtk/vtkwriterinterface.hh +++ b/dune/vtk/vtkwriterinterface.hh @@ -5,15 +5,11 @@ #include <string> #include <vector> -<<<<<<< HEAD #ifdef HAVE_ZLIB #include <zlib.h> #endif #include <dune/common/std/optional.hh> - -======= ->>>>>>> feature/pvdwriter #include <dune/vtk/filewriter.hh> #include <dune/vtk/vtkfunction.hh> #include <dune/vtk/vtktypes.hh> @@ -64,7 +60,7 @@ namespace Dune } /// Write the attached data to the file - virtual void write (std::string const& fn) override; + virtual void write (std::string const& fn) const override; /// Attach point data to the writer, \see VtkFunction for possible arguments template <class Function, class... Args> @@ -141,6 +137,21 @@ namespace Dune return fileExtension(); } + Vtk::FormatTypes getFormat () const + { + return format_; + } + + Vtk::DataTypes getDatatype () const + { + return datatype_; + } + + void update () + { + dataCollector_.update(); + } + protected: mutable DataCollector dataCollector_; diff --git a/dune/vtk/vtkwriterinterface.impl.hh b/dune/vtk/vtkwriterinterface.impl.hh index 945dc4bbcd2ce56da815ed354ec4ccfa74ac5dcd..28692e8999211f36f6d902c1b86fee2b30511cc7 100644 --- a/dune/vtk/vtkwriterinterface.impl.hh +++ b/dune/vtk/vtkwriterinterface.impl.hh @@ -19,7 +19,7 @@ namespace Dune { template <class GV, class DC> void VtkWriterInterface<GV,DC> - ::write (std::string const& fn) + ::write (std::string const& fn) const { dataCollector_.update(); diff --git a/src/pvdwriter.cc b/src/pvdwriter.cc index 37207edf5065e1089f41d357e54ce2d881bd5431..fd1192daf9eade6b7402fb8bea7614615705b47f 100644 --- a/src/pvdwriter.cc +++ b/src/pvdwriter.cc @@ -18,8 +18,8 @@ #include <dune/grid/yaspgrid.hh> -#include <dune/vtk/writers/vtkunstructuredgridwriter.hh> #include <dune/vtk/pvdwriter.hh> +#include <dune/vtk/writers/vtkunstructuredgridwriter.hh> using namespace Dune; using namespace Dune::Functions; @@ -39,13 +39,12 @@ void write (std::string prefix, GridView const& gridView) auto p1Analytic = makeAnalyticGridViewFunction([&c](auto const& x) { return c.dot(x); }, gridView); using Writer = VtkUnstructuredGridWriter<GridView>; - PvdWriter<Writer> pvdWriter(gridView); + PvdWriter<Writer> pvdWriter(gridView, Vtk::ASCII, Vtk::FLOAT32); pvdWriter.addPointData(p1Analytic, "p1"); pvdWriter.addCellData(p1Analytic, "p0"); for (double t = 0.0; t < 10.0; t += 1.0) { - pvdWriter.write(t, prefix + "_" + std::to_string(GridView::dimension) + "d_ascii.vtu", - Vtk::ASCII, Vtk::FLOAT32); + pvdWriter.write(t, prefix + "_" + std::to_string(GridView::dimension) + "d_ascii.vtu"); } }