version 3.9
gridwriter.hh
Go to the documentation of this file.
1// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2// vi: set et ts=4 sw=2 sts=2:
3//
4// SPDX-FileCopyrightInfo: Copyright © DuMux Project contributors, see AUTHORS.md in root folder
5// SPDX-License-Identifier: GPL-3.0-or-later
6//
12#ifndef DUMUX_IO_GRID_WRITER_HH
13#define DUMUX_IO_GRID_WRITER_HH
14
15#include <config.h>
16
17#if DUMUX_HAVE_GRIDFORMAT
18
19#include <type_traits>
20
21#include <gridformat/gridformat.hpp>
22#include <gridformat/traits/dune.hpp>
23
24namespace Dumux::IO {
25
26
27#ifndef DOXYGEN
28namespace Detail {
29
30template<typename Grid, typename Format, typename Comm, typename... Args>
31auto makeParallelWriter(const Grid& grid, const Format& fmt, const Dune::Communication<Comm>& comm, Args&&... args)
32{
33 return comm.size() > 1
34 ? GridFormat::Writer<Grid>{fmt, grid, static_cast<Comm>(comm), std::forward<Args>(args)...}
35 : GridFormat::Writer<Grid>{fmt, grid, std::forward<Args>(args)...};
36}
37
38template<typename Grid, typename Format, typename... Args>
39auto makeParallelWriter(const Grid& grid, const Format& fmt, const Dune::Communication<Dune::No_Comm>&, Args&&... args)
40{ return GridFormat::Writer<Grid>{fmt, grid, std::forward<Args>(args)...}; }
41
42template<typename Grid, typename Format, typename... Args>
43auto makeWriter(const Grid& grid, const Format& fmt, Args&&... args)
44{
45 const auto& comm = GridFormat::Dune::Traits::GridView<Grid>::get(grid).comm();
46 return makeParallelWriter(grid, fmt, comm, std::forward<Args>(args)...);
47}
48
49} // namespace Detail
50#endif // DOXYGEN
51
52
53namespace VTK { using namespace GridFormat::VTK; }
54namespace Format { using namespace GridFormat::Formats; }
55namespace Encoding { using namespace GridFormat::Encoding; }
56namespace Compression { using namespace GridFormat::Compression; using GridFormat::none; }
57namespace Precision {
58 using GridFormat::float32;
59 using GridFormat::float64;
60
61 using GridFormat::uint64;
62 using GridFormat::uint32;
63 using GridFormat::uint16;
64 using GridFormat::uint8;
65
66 using GridFormat::int64;
67 using GridFormat::int32;
68 using GridFormat::int16;
69 using GridFormat::int8;
70} // namespace Precision
71
72
77template<int order>
78struct Order { static_assert(order > 0, "order must be > 0"); };
79
80template<int o>
81inline constexpr auto order = Order<o>{};
82
92template<GridFormat::Concepts::Grid GridView, int order = 1>
93class GridWriter
94{
95 using Grid = std::conditional_t<
96 (order > 1),
97 GridFormat::Dune::LagrangePolynomialGrid<GridView>,
98 GridView
99 >;
100 using Cell = GridFormat::Cell<Grid>;
101 using Vertex = typename GridView::template Codim<GridView::dimension>::Entity;
102 using Element = typename GridView::template Codim<0>::Entity;
103 using Coordinate = typename Element::Geometry::GlobalCoordinate;
104 using Writer = GridFormat::Writer<Grid>;
105
106 public:
111 template<typename Format>
112 explicit GridWriter(const Format& fmt,
113 const GridView& gridView,
114 const Order<order>& = {})
115 : gridView_{gridView}
116 , grid_{makeGrid_(gridView)}
117 , writer_{Detail::makeWriter(grid_, fmt)}
118 { writer_.set_meta_data("rank", gridView_.comm().rank()); }
119
124 template<typename Format>
125 explicit GridWriter(const Format& fmt,
126 const GridView& gridView,
127 const std::string& filename,
128 const Order<order>& = {})
129 : gridView_{gridView}
130 , grid_{makeGrid_(gridView)}
131 , writer_{Detail::makeWriter(grid_, fmt, filename)}
132 { writer_.set_meta_data("rank", gridView_.comm().rank()); }
133
139 std::string write(const std::string& name) const
140 { return writer_.write(name); }
141
147 template<std::floating_point T>
148 std::string write(T time) const
149 { return writer_.write(time); }
150
152 template<GridFormat::Concepts::CellFunction<GridView> F,
153 GridFormat::Concepts::Scalar T = GridFormat::FieldScalar<std::invoke_result_t<F, Element>>>
154 void setCellField(const std::string& name, F&& f, const GridFormat::Precision<T>& prec = {})
155 { writer_.set_cell_field(name, std::move(f), prec); }
156
158 template<GridFormat::Dune::Concepts::Function<GridView> F>
159 void setCellField(const std::string& name, F&& f)
160 { GridFormat::Dune::set_cell_function(std::forward<F>(f), writer_, name); }
161
163 template<typename F, GridFormat::Concepts::Scalar T>
164 void setCellField(const std::string& name, F&& f, const GridFormat::Precision<T>& prec)
165 { GridFormat::Dune::set_cell_function(std::forward<F>(f), writer_, name, prec); }
166
168 template<GridFormat::Concepts::PointFunction<GridView> F,
169 GridFormat::Concepts::Scalar T = GridFormat::FieldScalar<std::invoke_result_t<F, Vertex>>>
170 void setPointField(const std::string& name, F&& f, const GridFormat::Precision<T>& prec = {})
171 {
172 static_assert(order == 1, "Point lambdas can only be used for order == 1. Use Dune::Functions instead.");
173 writer_.set_point_field(name, std::move(f), prec);
174 }
175
177 template<GridFormat::Dune::Concepts::Function<GridView> F>
178 void setPointField(const std::string& name, F&& f)
179 { GridFormat::Dune::set_point_function(std::forward<F>(f), writer_, name); }
180
182 template<GridFormat::Dune::Concepts::Function<GridView> F, GridFormat::Concepts::Scalar T>
183 void setPointField(const std::string& name, F&& f, const GridFormat::Precision<T>& prec)
184 { GridFormat::Dune::set_point_function(std::forward<F>(f), writer_, name, prec); }
185
187 void clear()
188 { writer_.clear(); }
189
191 void update()
192 {
193 if constexpr (order > 1)
194 grid_.update(gridView_);
195 }
196
197 private:
198 Grid makeGrid_(const GridView& gv) const
199 {
200 if constexpr (order > 1)
201 return Grid{gv, order};
202 else
203 return gv;
204 }
205
206 GridView gridView_;
207 Grid grid_;
208 Writer writer_;
209};
210
211} // namespace Dumux::IO
212
213#else // DUMUX_HAVE_GRIDFORMAT
214
215namespace Dumux::IO {
216
217template<class... Args>
219{
220public:
221 template<class... _Args>
222 GridWriter(_Args&&...)
223 {
224 static_assert(
225 false,
226 "GridWriter only available when the GridFormat library is available. "
227 "Use `git submodule init && git submodule update` to pull it."
228 );
229 }
230};
231
232} // namespace Dumux::IO
233
234#endif // DUMUX_HAVE_GRIDFORMAT
235#endif // DUMUX_IO_GRID_HH
Definition: gridwriter.hh:219
GridWriter(_Args &&...)
Definition: gridwriter.hh:222
constexpr None none
Definition: method.hh:153
Definition: gridwriter.hh:215