8#ifndef GRIDFORMAT_WRITER_HPP_
9#define GRIDFORMAT_WRITER_HPP_
17#include <gridformat/common/exceptions.hpp>
20#include <gridformat/grid/type_traits.hpp>
21#include <gridformat/parallel/concepts.hpp>
25template<
typename FileFormat>
30namespace WriterDetail {
32 template<
typename FileFormat,
typename Gr
id>
33 static constexpr bool has_sequential_factory
34 = is_complete<WriterFactory<FileFormat>>
35 and Concepts::Grid<Grid>
36 and
requires(
const FileFormat& f,
const Grid& grid) {
37 { WriterFactory<FileFormat>::make(f, grid) } -> std::derived_from<GridWriter<Grid>>;
40 template<
typename FileFormat,
typename Gr
id>
41 static constexpr bool has_sequential_time_series_factory
42 = is_complete<WriterFactory<FileFormat>>
43 and Concepts::Grid<Grid>
44 and
requires(
const FileFormat& f,
const Grid& grid) {
45 { WriterFactory<FileFormat>::make(f, grid, std::string{}) } -> std::derived_from<TimeSeriesGridWriter<Grid>>;
48 template<
typename FileFormat,
typename Gr
id,
typename Comm>
49 static constexpr bool has_parallel_factory
50 = is_complete<WriterFactory<FileFormat>>
51 and Concepts::Grid<Grid>
52 and Concepts::Communicator<Comm>
53 and
requires(
const FileFormat& f,
const Grid& grid,
const Comm& comm) {
54 { WriterFactory<FileFormat>::make(f, grid, comm) } -> std::derived_from<GridWriter<Grid>>;
57 template<
typename FileFormat,
typename Gr
id,
typename Comm>
58 static constexpr bool has_parallel_time_series_factory
59 = is_complete<WriterFactory<FileFormat>>
60 and Concepts::Grid<Grid>
61 and Concepts::Communicator<Comm>
62 and
requires(
const FileFormat& f,
const Grid& grid,
const Comm& comm) {
63 { WriterFactory<FileFormat>::make(f, grid, comm, std::string{}) } -> std::derived_from<TimeSeriesGridWriter<Grid>>;
66 template<
typename _Gr
id,
typename Gr
id>
67 concept Compatible = std::same_as<std::remove_cvref_t<_Grid>, Grid> and std::is_lvalue_reference_v<_Grid>;
89template<Concepts::Gr
id G>
99 template<
typename FileFormat, WriterDetail::Compatible<Gr
id> _Gr
id>
100 requires(WriterDetail::has_sequential_factory<FileFormat, Grid>)
111 template<
typename FileFormat, WriterDetail::Compatible<Gr
id> _Gr
id>
112 requires(WriterDetail::has_sequential_time_series_factory<FileFormat, Grid>)
113 Writer(
const FileFormat& f, _Grid&&
grid,
const std::string& base_filename)
123 template<
typename FileFormat, WriterDetail::Compatible<Gr
id> _Gr
id, Concepts::Communicator Comm>
124 requires(WriterDetail::has_parallel_factory<FileFormat, Grid, Comm>)
136 template<
typename FileFormat, WriterDetail::Compatible<Gr
id> _Gr
id, Concepts::Communicator Comm>
137 requires(WriterDetail::has_parallel_time_series_factory<FileFormat, Grid, Comm>)
138 Writer(
const FileFormat& f, _Grid&&
grid,
const Comm& comm,
const std::string& base_filename)
143 template<std::derived_from<Gr
idWriter<Gr
id>> W>
144 requires(!std::is_lvalue_reference_v<W>)
146 : _writer(std::make_unique<W>(std::move(writer)))
150 template<std::derived_from<TimeSeriesGr
idWriter<Gr
id>> W>
151 requires(!std::is_lvalue_reference_v<W>)
153 : _time_series_writer(std::make_unique<W>(std::move(writer)))
163 std::string
write(
const std::string& filename)
const {
166 "Writer was constructed as a time series writer. Only write(Scalar) can be used."
168 return _writer->write(filename);
178 template<Concepts::Scalar T>
179 std::string
write(
const T& time_value)
const {
180 if (!_time_series_writer)
182 "Writer was not constructed as a time series writer. Only write(std::string) can be used."
184 return _time_series_writer->write(time_value);
193 template<
typename Field>
195 _visit_writer([&] (
auto& writer) {
196 writer.set_meta_data(name, std::forward<Field>(field));
209 template<
typename Field>
211 _visit_writer([&] (
auto& writer) {
212 writer.set_point_field(name, std::forward<Field>(field));
222 template<
typename Field,
typename T>
224 _visit_writer([&] (
auto& writer) {
225 writer.set_point_field(name, std::forward<Field>(field), prec);
238 template<
typename Field>
240 _visit_writer([&] (
auto& writer) {
241 writer.set_cell_field(name, std::forward<Field>(field));
251 template<
typename Field,
typename T>
253 _visit_writer([&] (
auto& writer) {
254 writer.set_cell_field(name, std::forward<Field>(field), prec);
263 return _visit_writer([&] (
auto& writer) {
264 return writer.remove_meta_data(name);
273 return _visit_writer([&] (
auto& writer) {
274 return writer.remove_point_field(name);
283 return _visit_writer([&] (
auto& writer) {
284 return writer.remove_cell_field(name);
290 _visit_writer([&] (
auto& writer) {
297 _visit_writer([&] (
auto& writer) {
298 writer.set_ignore_warnings(value);
306 template<
typename Writer>
308 _visit_writer([&] (
const auto& writer) {
309 writer.copy_fields(out);
318 return _visit_writer([&] (
const auto& writer) ->
const std::optional<WriterOptions>& {
319 return writer.writer_options();
327 return _visit_writer([&] (
const auto& writer) ->
const Grid& {
328 return writer.grid();
342 return w._visit_writer([&] (
const auto& writer) {
357 return w._visit_writer([&] (
const auto& writer) {
372 return w._visit_writer([&] (
const auto& writer) {
378 template<
typename Visitor>
379 decltype(
auto) _visit_writer(
const Visitor& visitor) {
381 return std::invoke(visitor, *_writer);
383 if (!_time_series_writer)
384 throw InvalidState(
"No writer set");
385 return std::invoke(visitor, *_time_series_writer);
389 template<
typename Visitor>
390 decltype(
auto) _visit_writer(
const Visitor& visitor)
const {
392 return std::invoke(visitor, *_writer);
394 if (!_time_series_writer)
395 throw InvalidState(
"No writer set");
396 return std::invoke(visitor, *_time_series_writer);
400 std::unique_ptr<GridWriter<Grid>> _writer{
nullptr};
401 std::unique_ptr<TimeSeriesGridWriter<Grid>> _time_series_writer{
nullptr};
404template<
typename F,
typename G>
405 requires(Concepts::Grid<std::remove_cvref_t<G>>)
406Writer(
const F&, G&&) -> Writer<std::remove_cvref_t<G>>;
408template<
typename F,
typename G, Concepts::Communicator C>
409 requires(Concepts::Grid<std::remove_cvref_t<G>>)
410Writer(
const F&, G&&,
const C&) -> Writer<std::remove_cvref_t<G>>;
412template<
typename F,
typename G>
413 requires(Concepts::Grid<std::remove_cvref_t<G>>)
414Writer(
const F&, G&&,
const std::string&) -> Writer<std::remove_cvref_t<G>>;
416template<
typename F,
typename G, Concepts::Communicator C>
417 requires(Concepts::Grid<std::remove_cvref_t<G>>)
418Writer(
const F&, G&&,
const C&,
const std::string&) -> Writer<std::remove_cvref_t<G>>;
Base classes for grid data writers.
std::shared_ptr< const Field > FieldPtr
Pointer type used by writers/readers for fields.
Definition: field.hpp:186