TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Ecrire_YAML.h
1/****************************************************************************
2* Copyright (c) 2026, CEA
3* All rights reserved.
4*
5* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
6* 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7* 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8* 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
9*
10* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
11* IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
12* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
13*
14*****************************************************************************/
15
16#ifndef Ecrire_YAML_included
17#define Ecrire_YAML_included
18
19#include <TRUST_Ref.h>
20#include <YAML_data.h>
21#include <Nom.h>
22#include <string>
23#include <vector>
24#include <map>
25
26class Probleme_base;
27
28/*! @brief classe Ecrire_YAML Use this to generate a yaml file that will then be read by the PDI library (for checkpoint/restart or for domain partitioning)
29 *
30 * The data that will be written are the ones that have been set in Equation_base::data_a_sauvegarder() (+overloads)
31 * and Postraitement_base::data_a_sauvegarder() (+ overloads)
32 *
33 * These data must then be shared with PDI (during the simulation) in order to trigger the wanted IO operations, see also the class TRUST_2_PDI
34 *
35 * The structure of the file is as follows (indentation matters!):
36 *
37 * Metadata:
38 * mdata1 : type # for scalar
39 * mdata2 : { type: array, subtype: ..., size: ... } # for arrays
40 * ...
41 * Data:
42 * data1 : type
43 * ...
44 * Plugins:
45 * plugin1:
46 * plugin2:
47 * ...
48 *
49 * We're using the HDF5 plugin. Here is an example of how to declare a HDF5 dataset (structure gathering data of the same type) and write our TRUST data into it:
50 * - file: filename
51 * datasets:
52 * dataset1: { type: array, subtype: ..., size: ... } #declaration of first dataset
53 * dataset2: { type: array, subtype: ..., size: ... } #declaration of second dataset
54 * ...
55 * write:
56 * data1: # TRUST data (it has to be declared in the data section first!)
57 * dataset: dataset1 # the dataset to which the data belongs
58 * dataset_selection:
59 * size: [...,...] # What is the size of the section in the dataset (for every dimension) we will work with ?
60 * start:[...,...] # Where do we want to start to write/read the data in the dataset (for every dimension)?
61 *
62 */
64{
65public:
66 Ecrire_YAML() : indent_(2) { }
67
68 void write_checkpoint_file(const std::string& yaml_fname);
69 void write_restart_file(const std::string& yaml_fname);
70 void write_champ_fonc_restart_file(const std::string& filename);
71
72 // Adds a problem to write into the YAML file
73 // (with the name of the corresponding checkpoint filename)
74 // (we can have independent checkpoint files for each problem or a common one)
75 // This has to be called before generating the YAML file for checkpoint/restart!
76 void add_pb_base(const Probleme_base& pb_base, const Nom& full_file_name)
77 {
78 Pb2Save new_pb;
79 new_pb.pb = pb_base;
80
81 const std::string& full_fname = full_file_name.getString();
82 std::size_t relative_path = full_fname.find_last_of("/");
83 std::string path = relative_path != std::string::npos ? full_fname.substr(0, relative_path + 1) : "";
84 std::string fname = relative_path != std::string::npos ? full_fname.substr(relative_path + 1) : full_fname;
85 std::size_t with_ext = fname.find(".");
86 std::string basename = with_ext != std::string::npos ? fname.substr(0, with_ext) : fname;
87 std::string extension = with_ext != std::string::npos ? fname.substr(with_ext) : "";
88 std::string nodeIndex = Process::is_parallel() ? "_${nodeId}" : "";
89
90 new_pb.filename = path + basename + nodeIndex + extension;
91 new_pb.configFilename_ = path + basename + "_config" + extension;
92 pbs_.push_back(new_pb);
93
94 }
95
96 void add_field(Nom pb, Nom nom, int nb_dim);
97 void add_scalar(Nom pb, Nom nom, Nom type, bool is_local);
98
99private:
100 // Begins a new bloc in the YAML file
101 void begin_bloc(const std::string& line, std::string& text)
102 {
103 add_line(line, text);
104 indent_ += 2;
105 }
106 // End the current bloc in the YAML file
107 void end_bloc() { indent_ -= 2; }
108 // Adds a line with the correct indentation in the YAML file
109 void add_line(const std::string& line, std::string& text)
110 {
111 text = text + "\n";
112 for(int i=0; i<indent_; i++)
113 text = text + " ";
114 text = text + line;
115 }
116
117 void set_data();
118
119 // ==============================================================
120 // Private methods to generate the YAML file that are common
121 // for checkpoint and restart
122 // ==============================================================
123 void write_checkpoint_restart_file(bool save, const std::string& yaml_fname);
124 void declare_metadata(bool save, std::string& text);
125 void declare_data(bool save, std::string& text);
126 void write_time_scheme(bool save, const std::string& fname, std::string& text);
127 void write_config_file(bool save, int pb_i, std::string& text);
128 void write_partition(bool save, int pb_i, std::string& text);
129
130 // ==============================================================
131 // Private methods to generate the YAML file for checkpoint only
132 // ==============================================================
133 void write_data_for_checkpoint(int pb_i, bool is_parallel, std::string& text);
134 void write_file_initialization(int pb_i, std::string& text);
135 void write_fields_types_for_checkpoint(int pb_i, std::string& text);
136 void write_config_file_initialization(int pb_i, std::string& text);
137 void write_format_for_checkpoint(const std::string& fname, std::string& text);
138
139 // ==============================================================
140 // Private methods to generate the YAML file for restart only
141 // ==============================================================
142 void write_data_for_restart(int pb_i, std::string& text);
143 void write_fields_types_for_restart(int pb_i, std::string& text);
144
145 // ==============================================================
146 // Private methods useful to write generic structures
147 // ==============================================================
148 void declare_scalar(const std::string& name, const std::string& type, std::string& text);
149 void declare_array(const std::string& name, const std::string& type, const std::string& size, std::string& text);
150 void declare_dtab(const std::string& dname, const std::string& name, const std::string& type, int nb_dim, std::string& text);
151 void declare_TRUST_dataset(const std::string& dname, const std::string& name, const std::string& type, int nb_dim, bool is_parallel, std::string& text);
152 void write_impl_dataset(const std::string& dname, const std::string& fname, std::string& text);
153 void write_impl_dataset(const std::string& dname, const std::string& fname, const std::vector<std::string>& attribute, std::string& text);
154 void write_TRUST_dataset(const std::string& dname, const std::string& name, int nb_dim, const std::string& cond, bool is_parallel, const std::vector<std::string>& attribute, std::string& text);
155 void write_TRUST_dataset_selection(const std::string& name, int nb_dim, bool is_parallel, std::string& text);
156 void write_dataset_selection(const std::vector<std::string>& sizes, const std::vector<std::string>& offsets,std::string& text);
157 void write_attributes(const std::vector<std::string>& attributes, std::string& text);
158
159 // indicates the number of spaces to add at the beginning of the next line to write in the file (so that the YAML file correctly indented)
160 int indent_;
161
162 typedef struct
163 {
164 OBS_PTR(Probleme_base) pb;
165 std::vector<YAML_data> data;
166 std::string filename;
167 std::string configFilename_;
168 } Pb2Save;
169 // List of all the problems we want to save/restore
170 // Each of the problems contain:
171 // - a reference to the problem
172 // - the list of the fields and the scalars to be saved/restored, with all the corresponding information
173 // - the name of the corresponding checkpoint file
174 // - the name of the corresponding configuration checkpoint file
175 std::vector<Pb2Save> pbs_;
176
177 // the name of the corresponding checkpoint configuration file
178
179};
180
181#endif
void add_field(Nom pb, Nom nom, int nb_dim)
Adds a field (name+dimension) to the block of the problem pb_name in the YAML file.
void write_champ_fonc_restart_file(const std::string &filename)
Generate the YAML file that will be read with champ_fonc_reprise.
void write_checkpoint_file(const std::string &yaml_fname)
Generate the YAML file that will be read for checkpoint.
void add_pb_base(const Probleme_base &pb_base, const Nom &full_file_name)
Definition Ecrire_YAML.h:76
void add_scalar(Nom pb, Nom nom, Nom type, bool is_local)
Adds a scalar (name+type) to the block of the problem pb_name in the YAML file.
void write_restart_file(const std::string &yaml_fname)
Generate the YAML file that will be read for restart.
class Nom Une chaine de caractere pour nommer les objets de TRUST
Definition Nom.h:31
const std::string & getString() const
Definition Nom.h:92
classe Probleme_base C'est un Probleme_U qui n'est pas un couplage.
static bool is_parallel()
Definition Process.cpp:110