TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Ecrire_CGNS.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_CGNS_included
17#define Ecrire_CGNS_included
18
19#include <Domaine_forward.h>
20#include <TRUST_2_CGNS.h>
21#include <unordered_set>
22#include <map>
23
24class Nom;
25
26/**
27 * @brief Writer TRUST -> CGNS for post-processing outputs.
28 *
29 * This class centralizes all CGNS writing strategies used by TRUST postprocessing.
30 * The implementation supports several execution contexts and file layouts:
31 * - sequential single file,
32 * - parallel single file,
33 * - parallel over-zone output,
34 * - linked files (`grid + one solution file per post time + one final link file`),
35 * - communicator-group outputs.
36 *
37 * The current design intentionally keeps some filename construction and mode
38 * dispatch explicit in the implementation. For this code, readability of the
39 * different CGNS workflows is preferred over a stronger abstraction layer.
40 *
41 * High-level behavior:
42 * - In single-file modes, the main CGNS file stays open as long as possible and
43 * is flushed and/or closed according to `Option_CGNS::FLUSH_EVERY_N` and
44 * `Option_CGNS::CLOSE_EVERY_N`.
45 * - In linked-file modes, the mesh file is separated from solution files. A
46 * final link file is rebuilt at each post time so the result remains directly
47 * visualizable during the simulation.
48 * - For deformable or lagrangian cases, additional constraints apply because
49 * coordinates and/or topology evolve with time.
50 *
51 * Important design note about the final link file:
52 * the file is currently rebuilt at each post time instead of being incrementally
53 * updated. This is a deliberate trade-off. In practice, CGNS/HDF5 append-style
54 * updates on iterative nodes (`BaseIterativeData_t`, `ZoneIterativeData_t`) were
55 * found to be more fragile and not clearly faster than rebuilding this small
56 * `index` file.
57 */
59{
60#ifdef HAS_CGNS
61public:
62 void cgns_associer_domaine_dis(const Domaine_dis_base& );
63 void cgns_init_MPI();
64 void cgns_set_postraiter_domain() { postraiter_domaine_ = true; }
65 void cgns_set_is_dual_domain() { is_dual_ = true; }
66 void cgns_set_is_deformable_domain() { is_deformable_ = true; }
67 void cgns_set_lagrangian_domain() { is_lagrangian_ = true, is_deformable_ = true; } /* pour FTD on active les 2 flags */
68 void cgns_set_loc_vector(const std::vector<std::string>& vec) { loc_vect_ = vec; }
69 void cgns_set_base_name(const Nom& );
70 void cgns_set_discr_type(const Nom& type) { discr_type_ = type.getString(); }
71 void cgns_open_file();
72 void cgns_finir();
73 void cgns_add_time(const double );
74 void cgns_write_domaine_dual(const Nom& , const int , const Nom& nom_dom = "??");
75 void cgns_write_domaine(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
76 void cgns_write_field(const Domaine&, const Noms&, double, const Nom&, const Nom&, const Nom&, const DoubleTab&);
77 void cgns_resetTime(const double t, const std::string& dirname, const Nom& basefile);
78 void finir_ecriture(double);
79
80private:
81
82 void fill_infos_loc();
83 void update_grid_name();
84 inline bool is_comm_group_mode() const
85 {
87 && Process::is_parallel() && PE_Groups::has_user_defined_group() && !postraiter_domaine_;
88 }
89
90 inline bool is_linked_files_comm_group_mode() const
91 {
93 && PE_Groups::has_user_defined_group() && !postraiter_domaine_;
94 }
95
96 inline bool is_single_file_comm_group_mode() const
97 {
99 && PE_Groups::has_user_defined_group() && !postraiter_domaine_;
100 }
101
102 Ecrire_CGNS_helper cgns_helper_;
103 OBS_PTR(Domaine_dis_base) domaine_dis_;
104 OBS_PTR(std::vector<std::string>) loc_vect_;
105 std::vector<TRUST_2_CGNS> T2CGNS_;
106
107 std::map<std::string, Nom> fld_loc_map_; /* { Loc , Nom_dom } */
108 std::vector<Nom> doms_written_;
109 std::vector<std::string> baseFile_name_vect_;
110 std::unordered_set<std::string> fieldName_dumped_; /* manage doubled fields / synonyms / compos .. ! */
111 std::string solname_elem_ = "", solname_som_ = "", solname_faces_ = "", baseFile_name_ = "";
112 std::string grid_name_ = "", grid_name_loc_ = "";
113 std::string discr_type_ = "";
114 std::vector<double> time_post_;
115 std::vector<int> baseId_, zoneId_;
116
117 bool need_post_field_ = true;
118 bool has_elem_field_ = false, has_faces_field_ = false, has_som_field_ = false;
119 bool solname_elem_written_ = false, solname_som_written_ = false, solname_faces_written_ = false, grid_name_written_ = false;
120 bool postraiter_domaine_ = false;
121 bool first_time_post_ = true;
122
123 int fileId_ = -123;
124 int flowId_elem_ = 0, fieldId_elem_ = 0;
125 int flowId_som_ = 0, fieldId_som_ = 0;
126 int flowId_faces_ = 0, fieldId_faces_ = 0;
127
128 // specifique pour link
129 bool grid_file_opened_ = false, solution_file_opened_ = false; /* Management of link files */
130 std::vector<std::string> baseZone_name_;
131 std::vector<std::vector<std::string>> connectname_;
132 std::vector<std::vector<cgsize_t>> sizeId_;
133 std::vector<int> cellDim_;
134
135 // specifique par in zone
136 std::vector<std::vector<int>> zoneId_par_; /* par ordre d'ecriture du domaine */
137
138 // specifique LINKED_FILES_PER_COMM_GROUP / SINGLE_FILE_PER_COMM_GROUP
139 void gather_local_sizeId_for_comm_group();
140 int proc_maitre_local_comm_ = -123;
141 std::vector<int> vec_proc_maitre_local_comm_, unique_vec_proc_maitre_local_comm_;
142 std::vector<std::vector<cgsize_t>> sizeId_som_local_comm_, sizeId_elem_local_comm_; // meme dim que sizeId_
143
144 // specifique maillage dual pour faces
145 IntTab fs_dual_, ef_dual_;
146 bool is_dual_ = false;
147 inline IntTab& get_fs_dual() { return fs_dual_;}
148 inline const IntTab& get_fs_dual() const { return fs_dual_;}
149 inline IntTab& get_ef_dual() { return ef_dual_;}
150 inline const IntTab& get_ef_dual() const { return ef_dual_;}
151
152 // gestion elem/som/faces
153 void cgns_fill_field_loc_map(const Nom&);
154
155 // Methodes pour Domaine Lagrangien; ie: FTD
156 bool is_lagrangian_ = false;
157 void cgns_write_final_link_file_lagrangian();
158 void link_multi_loc_support_lagrangian();
159
160 // Methodes pour Domaine Deformable
161 void link_multi_loc_support_pb_deformable();
162 void init_proc_maitre_local_comm();
163 bool is_deformable_ = false, multi_loc_deformable_support_linked_ = false;
164
165 // Methodes pour LINK
166 void cgns_fill_info_grid_link_file(const char*, const CGNS_TYPE&, const int, const int, const int, const bool);
167 void cgns_open_grid_base_link_file();
168 void cgns_init_solution_link_file(const std::string& , const Nom&);
169 void cgns_open_solution_link_file(const double);
170 void cgns_write_final_link_file();
171 void cgns_write_final_link_file_comm_group();
172 void cgns_write_final_link_file_for_single_file_comm_group();
173 void cgns_close_grid_or_solution_link_file(const double, const TYPE_LINK_CGNS, bool is_cerr = true);
174 void add_new_linked_base(const std::string&, const Nom&);
175
176 // Version sequentielle
177 void cgns_write_domaine_seq(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
178 void cgns_write_domaine_deformable_seq(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
179 void cgns_write_field_seq(const int, const double, const Nom&, const Nom&, const Nom&, const Nom&, const DoubleTab&);
180
181 // Version parallele over zone
182 void cgns_write_domaine_par_over_zone(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
183 void cgns_write_field_par_over_zone(const int, const double, const Nom&, const Nom&, const Nom&, const Nom&, const DoubleTab&);
184
185 // Version parallele in zone
186 void cgns_write_domaine_par_in_zone(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
187 void cgns_write_domaine_deformable_par_in_zone(const Domaine * ,const Nom& , const DoubleTab& , const IntTab& , const Motcle& );
188 void cgns_write_field_par_in_zone(const int, const double, const Nom&, const Nom&, const Nom&, const Nom&, const DoubleTab&);
189 void cgns_build_connectivity_sections_par_in_zone(const CGNS_TYPE cgns_type_elem, const bool is_polyedre,
190 const TRUST_2_CGNS& TRUST2CGNS, const int ind_base_zone,
191 const int ne_tot, int& sectionId, int& sectionId2 ) const;
192 void cgns_write_connectivity_par_in_zone(const CGNS_TYPE cgns_type_elem, const bool is_polyedre,
193 const TRUST_2_CGNS& TRUST2CGNS, const int ind_base_zone,
194 const int sectionId, const int sectionId2) const;
195
196 // Version fichier CGNS unique (safe !)
197 void cgns_write_iters();
198 void ensure_modify_open_singlefile();
199 void cgns_flush_to_disk() const;
200 bool ensure_modify_done_ = false;
201 bool singlefile_open_ = false;
202 size_t step_single_file_counter_ = 0;
203
204#endif /* HAS_CGNS */
205};
206
207inline void verify_if_cgns(const char * nom_funct)
208{
209#ifdef HAS_CGNS
210#ifdef MPI_
211 return;
212#else /* NOT MPI_ */
214 Process::exit("Parallel CGNS files need MPI installed ... ");
215#endif /* MPI_ */
216#else /* NOT HAS_CGNS */
217 Cerr << "Format_Post_CGNS::" << nom_funct << " should not be called since TRUST is not compiled with the CGNS library !!! " << finl;
219#endif /* HAS_CGNS */
220}
221
222#endif /* Ecrire_CGNS_included */
classe Domaine_dis_base Cette classe est la base de la hierarchie des domaines discretisees.
Writer TRUST -> CGNS for post-processing outputs.
Definition Ecrire_CGNS.h:59
Une chaine de caractere (Nom) en majuscules.
Definition Motcle.h:26
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
Un tableau de chaine de caracteres (VECT(Nom)).
Definition Noms.h:26
static bool LINKED_FILES_PER_COMM_GROUP
Definition Option_CGNS.h:52
static bool SINGLE_FILE_PER_COMM_GROUP
Definition Option_CGNS.h:48
static bool has_user_defined_group()
static bool is_parallel()
Definition Process.cpp:110
static void exit(int exit_code=-1)
Routine de sortie de TRUST dans une region Kokkos.
Definition Process.cpp:455