Cholla 3.0.1-dev
Cholla - Massively parallel hydro on GPUs
Loading...
Searching...
No Matches
FieldWriter.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <memory>
9#include <string>
10#include <utility> // std::pair
11#include <vector>
12
13#include "../global/global.h"
14#include "../grid/field_info.h"
15#include "../grid/grid3D.h"
16#include "../io/FnameTemplate.h" // define FnameTemplate
17#include "../io/LazyScratchBuf.h"
18#include "../io/ParameterMap.h" // define ParameterMap
19#include "../io/WriterManager.h" // declare WriterFn
20#include "../utils/basic_structs.h" // VectorXYZ
21
22namespace io
23{
24
36inline std::optional<std::string> lookup_legacy_short_name_(std::string_view field_name, bool for_text_file = true)
37{
38 if (field_name == "density") {
39 const char *name = for_text_file ? "rho" : "d";
40 return {std::string(name)};
41 } else if (field_name == "momentum_x") {
42 return {std::string("mx")};
43 } else if (field_name == "momentum_y") {
44 return {std::string("my")};
45 } else if (field_name == "momentum_z") {
46 return {std::string("mz")};
47 } else if (field_name == "Energy") {
48 return {std::string("E")};
49 } else if (field_name == "GasEnergy") {
50 const char *name = for_text_file ? "ge" : "GE";
51 return {std::string(name)};
52 } else {
53 return std::nullopt;
54 }
55}
56
57enum struct FileFormat { TEXT, H5_NATIVE_PRECISION, H5_F32 };
58
60enum struct WriteCond { ALWAYS, REQUIRE_COMPLETE_DATA };
61
74 std::string name;
79
80 // the following constructor is defined in order to make this type work with
81 // std::vector::emplace_back. Delete it, once we require C++20 or newer
82#if __cpp_aggregate_paren_init < 201902L
85 {
86 }
87#endif
88};
89
93 std::vector<DatasetSpecEntry> cc_dataset_entries;
99};
100
107{
108 FileFormat file_format_;
109 DatasetSpec dataset_spec_;
116 std::shared_ptr<LazyScratchBuf> lazy_scratch_buf_;
117
118 // this has been made private to force the usage of the factory method
119 FieldWriter(FileFormat file_format, ParameterMap &pmap, const FieldInfo &field_info);
120
121 public:
122 FieldWriter() = delete;
123
134 static std::pair<WriterFn, std::string> try_create(int ndim, ParameterMap &pmap, const FieldInfo &field_info,
135 bool write_h5_f32)
136 {
137 // construct a payload denoting an error
138 auto prep_err = [](const std::string &msg) -> std::pair<WriterFn, std::string> {
139 return std::pair<WriterFn, std::string>{WriterFn(), msg};
140 };
141
142#ifdef HDF5
143 FileFormat file_format = (write_h5_f32) ? FileFormat::H5_F32 : FileFormat::H5_NATIVE_PRECISION;
144#else
145 if (write_h5_f32) return prep_err("h5_f32 outputs require compilation with hdf5");
146 FileFormat file_format = FileFormat::TEXT;
147#endif
148
149 if (file_format == FileFormat::TEXT and ndim != 1) {
150 return prep_err("can only write Fields to text files for 1D simulations");
151 } else if (file_format == FileFormat::H5_F32 and ndim != 3) {
152 return prep_err("can only write Fields to h5_f32 files for 3D simulations");
153 }
154 std::pair<WriterFn, std::string> out{WriterFn(FieldWriter(file_format, pmap, field_info)), ""};
155 return out;
156 }
157
165 void operator()(Grid3D &G, Parameters P, int nfile, const FnameTemplate &fname_template) const;
166};
167
168} // namespace io
std::optional< std::string > lookup_legacy_short_name_(std::string_view field_name, bool for_text_file=true)
Definition FieldWriter.h:36
WriteCond
Definition FieldWriter.h:60
std::function< void(Grid3D &, Parameters, int, const FnameTemplate &)> WriterFn
Definition WriterManager.h:22
Definition field_info.h:49
Definition FnameTemplate.h:43
Class to create a the gravity object.
Definition grid3D.h:229
A class that provides map-like access to parameter files.
Definition ParameterMap.h:74
A callable type that writes general grid data.
Definition FieldWriter.h:107
void operator()(Grid3D &G, Parameters P, int nfile, const FnameTemplate &fname_template) const
Definition FieldWriter.cpp:529
static std::pair< WriterFn, std::string > try_create(int ndim, ParameterMap &pmap, const FieldInfo &field_info, bool write_h5_f32)
Definition FieldWriter.h:134
IOBuf
Definition field_info.h:21
Definition global.h:217
A data only struct that acts as a simple 3 element vector.
Definition basic_structs.h:32
Definition FieldWriter.h:66
int field_id
the id of the field that will be written
Definition FieldWriter.h:68
WriteCond condition
the condition for writing this dataset
Definition FieldWriter.h:78
std::string name
Definition FieldWriter.h:74
field::IOBuf io_buf
indicates whether we record values from the host or device buffers
Definition FieldWriter.h:76
Definition FieldWriter.h:91
std::vector< DatasetSpecEntry > cc_dataset_entries
describes properties about dataset creation for ordinary cell-centered fields
Definition FieldWriter.h:93
hydro_utilities::VectorXYZ< bool > write_mag
Definition FieldWriter.h:98