dune-vtk 2.12
Loading...
Searching...
No Matches
timeserieswriter.impl.hh
Go to the documentation of this file.
1// SPDX-FileCopyrightText: Copyright © DUNE Project contributors, see file AUTHORS.md
2// SPDX-License-Identifier: LicenseRef-GPL-2.0-only-with-DUNE-exception OR LGPL-3.0-or-later
3#pragma once
4
5#include <algorithm>
6#include <cstdio>
7#include <iomanip>
8#include <iostream>
9#include <iterator>
10#include <fstream>
11#include <sstream>
12#include <string>
13
14#if HAVE_VTK_ZLIB
15#include <zlib.h>
16#endif
17
19#include <dune/geometry/type.hh>
20
24
25namespace Dune::Vtk {
26
27template <class W>
29{
30 if (initialized_) {
31 [[maybe_unused]] int ec = std::remove(filenameMesh_.c_str());
32 assert(ec == 0);
33 for (auto const& timestep : timesteps_) {
34 ec = std::remove(timestep.second.c_str());
35 assert(ec == 0);
36 }
37 }
38 std::remove(tmpDir_.string().c_str());
39}
40
41
42template <class W>
44 ::writeTimestep (double time, std::string const& fn, std::optional<std::string> tmpDir, bool writeCollection) const
45{
46 auto name = Vtk::Path(fn).stem();
47 auto tmpBase = tmpDir ? Vtk::Path(*tmpDir) : tmpDir_;
48 auto tmp = tmpBase;
49 tmp /= name.string();
50
51 vtkWriter_.dataCollector_->update();
52
53 std::string filenameBase = tmp.string();
54
55 if (vtkWriter_.comm().size() > 1)
56 filenameBase = tmp.string() + "_p" + std::to_string(vtkWriter_.comm().rank());
57
58 if (!initialized_) {
60
61 // write points and cells only once
62 filenameMesh_ = filenameBase + ".mesh.vtkdata";
63 std::ofstream out(filenameMesh_, std::ios_base::ate | std::ios::binary);
64 vtkWriter_.writeGridAppended(out, blocks_);
65 initialized_ = true;
66 }
67
68 std::string filenameData = filenameBase + "_t" + std::to_string(timesteps_.size()) + ".vtkdata";
69 std::ofstream out(filenameData, std::ios_base::ate | std::ios::binary);
70 vtkWriter_.writeDataAppended(out, blocks_);
71 timesteps_.emplace_back(time, filenameData);
72
73 if (writeCollection)
74 write(fn);
75}
76
77
78template <class W>
81{
82 assert( initialized_ );
83
84 auto p = Vtk::Path(fn);
85 auto name = p.stem();
86 p.removeFilename();
87
88 Vtk::Path fn_dir = p;
91
92 std::string serial_fn = fn_dir.string() + '/' + name.string() + "_ts";
93 std::string parallel_fn = data_dir.string() + '/' + name.string() + "_ts";
94 std::string rel_fn = rel_dir.string() + '/' + name.string() + "_ts";
95
96 int commRank = vtkWriter_.comm().rank();
97 int commSize = vtkWriter_.comm().size();
98 if (commSize > 1)
100
102
103 { // write serial file
104 outputFilename = serial_fn + "." + vtkWriter_.getFileExtension();
105 std::ofstream serial_out(outputFilename, std::ios_base::ate | std::ios::binary);
106 assert(serial_out.is_open());
107
109 serial_out << std::setprecision(vtkWriter_.getDatatype() == Vtk::DataTypes::FLOAT32
112
113 vtkWriter_.writeTimeseriesSerialFile(serial_out, filenameMesh_, timesteps_, blocks_);
114 }
115
116 if (commSize > 1 && commRank == 0) {
117 // write parallel file
118 outputFilename = parallel_fn + ".p" + vtkWriter_.getFileExtension();
119 std::ofstream parallel_out(outputFilename, std::ios_base::ate | std::ios::binary);
120 assert(parallel_out.is_open());
121
123 parallel_out << std::setprecision(vtkWriter_.getDatatype() == Vtk::DataTypes::FLOAT32
126
127 vtkWriter_.writeTimeseriesParallelFile(parallel_out, rel_fn, commSize, timesteps_);
128 }
129
130 return outputFilename;
131}
132
133} // end namespace Dune::Vtk
Definition datacollector.hh:19
Path relative(Path const &a, Path const &b)
Find the path of a relative to directory of b
Definition filesystem.cc:174
bool createDirectories(Path const &p)
Create directory and non existing parent directories.
Definition filesystem.cc:141
const char * name()
void update()
Update the DataCollector on the current GridView.
Definition datacollectorinterface.hh:47
auto comm() const
Definition datacollectorinterface.hh:112
Implementation of DataCollector for cells, with continuous data.
Definition celltypedatacollector.hh:20
File-Writer for Vtk timeseries .vtu files.
Definition timeserieswriter.hh:27
~TimeseriesWriter()
Remove all written intermediate files and remove temporary directory.
Definition timeserieswriter.impl.hh:28
Definition filesystem.hh:15
Path stem() const
Returns the stem path component.
Definition filesystem.cc:67
T classic(T... args)
T setprecision(T... args)
T to_string(T... args)