TRUST 1.9.8
HPC thermohydraulic platform
Loading...
Searching...
No Matches
Ecrire_CGNS_helper.tpp
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_helper_tpp_included
17#define Ecrire_CGNS_helper_tpp_included
18
19#include <TRUSTTrav.h>
20
21template<TYPE_RUN_CGNS _TYPE_, TYPE_MODE_CGNS _MODE_>
22inline void Ecrire_CGNS_helper::cgns_open_file(const std::string& fn, int& fileId, const bool is_print)
23{
24 constexpr bool is_SEQ = (_TYPE_ == TYPE_RUN_CGNS::SEQ), is_WRITE = (_MODE_ == TYPE_MODE_CGNS::WRITE), is_MODIFY = (_MODE_ == TYPE_MODE_CGNS::MODIFY);
25 if (is_SEQ)
26 {
27 if (cg_open(fn.c_str(), is_WRITE ? CG_MODE_WRITE : ( is_MODIFY ? CG_MODE_MODIFY : CG_MODE_READ), &fileId) != CG_OK)
28 {
29 Cerr << "Error Ecrire_CGNS_helper::cgns_open_file : cg_open !" << finl;
30 TRUST_CGNS_ERROR();
31 }
32
33 if (is_print)
34 Cerr << "**** CGNS file " << fn << " opened !" << finl;
35 }
36 else
37 {
38#ifdef MPI_
39 if (cgp_open(fn.c_str(), is_WRITE ? CG_MODE_WRITE : ( is_MODIFY ? CG_MODE_MODIFY : CG_MODE_READ), &fileId) != CG_OK)
40 {
41 Cerr << "Error Ecrire_CGNS_helper::cgns_open_file : cgp_open !" << finl;
42 TRUST_CGNS_ERROR();
43 }
44
45 if (is_print)
46 Cerr << "**** Parallel CGNS file " << fn << " opened !" << finl;
47#else
48 Cerr << "Parallel CGNS files need MPI installed ... " << finl;
49 TRUST_CGNS_ERROR();
50#endif
51 }
52}
53
54template<TYPE_RUN_CGNS _TYPE_>
55inline void Ecrire_CGNS_helper::cgns_close_file(const std::string& fn, const int fileId, const bool is_print)
56{
57 constexpr bool is_SEQ = (_TYPE_ == TYPE_RUN_CGNS::SEQ);
58 if (is_SEQ)
59 {
60 if (cg_close(fileId) != CG_OK)
61 {
62 Cerr << "Error Ecrire_CGNS_helper::cgns_close_file : cg_close !" << finl;
63 TRUST_CGNS_ERROR();
64 }
65
66 if (is_print)
67 Cerr << "**** CGNS file " << fn << " closed !" << finl;
68 }
69 else
70 {
71#ifdef MPI_
72 if (cgp_close(fileId) != CG_OK)
73 {
74 Cerr << "Error Ecrire_CGNS_helper::cgns_close_file : cgp_close !" << finl;
75 TRUST_CGNS_ERROR();
76 }
77
78 if (is_print)
79 Cerr << "**** Parallel CGNS file " << fn << " closed !" << finl;
80#else
81 Cerr << "Parallel CGNS files need MPI installed ... " << finl;
82 TRUST_CGNS_ERROR();
83#endif
84 }
85}
86
87template<TYPE_ECRITURE_CGNS _TYPE_>
88inline void Ecrire_CGNS_helper::cgns_write_zone_grid_coord(const int icelldim, const int fileId, const int baseId, const char *zonename, const cgsize_t *isize, int& zoneId,
89 const std::vector<double>& xCoords, const std::vector<double>& yCoords, const std::vector<double>& zCoords,
90 int& coordsIdx, int& coordsIdy, int& coordsIdz)
91{
92 constexpr bool is_SEQ = (_TYPE_ == TYPE_ECRITURE_CGNS::SEQ);
93
94 if (cg_zone_write(fileId, baseId, zonename, isize, CGNS_ENUMV(Unstructured), &zoneId) != CG_OK)
95 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cg_zone_write !" << finl, TRUST_CGNS_ERROR();
96
97 if (is_SEQ)
98 {
99 if (cg_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateX", xCoords.data(), &coordsIdx) != CG_OK)
100 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cg_coord_write - X !" << finl, TRUST_CGNS_ERROR();
101
102 if (cg_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateY", yCoords.data(), &coordsIdy) != CG_OK)
103 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cg_coord_write - Y !" << finl, TRUST_CGNS_ERROR();
104
105 if (Objet_U::dimension > 2)
106 if (cg_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateZ", zCoords.data(), &coordsIdz) != CG_OK)
107 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cg_coord_write - Z !" << finl, TRUST_CGNS_ERROR();
108 }
109 else
110 {
111#ifdef MPI_
112 int gridId = -123;
113 if (cg_grid_write(fileId, baseId, zoneId, "GridCoordinates", &gridId) != CG_OK)
114 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cg_grid_write !" << finl, TRUST_CGNS_ERROR();
115
116 if (cgp_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateX", &coordsIdx) != CG_OK)
117 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cgp_coord_write - X !" << finl, TRUST_CGNS_ERROR();
118
119 if (cgp_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateY", &coordsIdy) != CG_OK)
120 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cgp_coord_write - Y !" << finl, TRUST_CGNS_ERROR();
121
122 if (Objet_U::dimension > 2)
123 if (cgp_coord_write(fileId, baseId, zoneId, CGNS_DOUBLE_TYPE, "CoordinateZ", &coordsIdz) != CG_OK)
124 Cerr << "Error Ecrire_CGNS_helper::cgns_write_zone_grid_coord : cgp_coord_write - Z !" << finl, TRUST_CGNS_ERROR();
125#endif
126 }
127}
128
129template<TYPE_ECRITURE_CGNS _TYPE_>
130inline std::enable_if_t<_TYPE_ != TYPE_ECRITURE_CGNS::SEQ, void>
131Ecrire_CGNS_helper::cgns_write_grid_coord_data(const int icelldim, const int fileId, const int baseId, const int zoneId,
132 const int coordsIdx, const int coordsIdy, const int coordsIdz, const cgsize_t min, const cgsize_t max,
133 const std::vector<double>& xCoords, const std::vector<double>& yCoords, const std::vector<double>& zCoords)
134{
135#ifdef MPI_
136 if (cgp_coord_write_data(fileId, baseId, zoneId, coordsIdx, &min, &max, xCoords.data()) != CG_OK)
137 Cerr << "Error Ecrire_CGNS_helper::cgns_write_grid_coord_data : cgp_coord_write_data - X !" << finl, TRUST_CGNS_ERROR();
138
139 if (cgp_coord_write_data(fileId, baseId, zoneId, coordsIdy, &min, &max, yCoords.data()) != CG_OK)
140 Cerr << "Error Ecrire_CGNS_helper::cgns_write_grid_coord_data : cgp_coord_write_data - Y !" << finl, TRUST_CGNS_ERROR();
141
142// if (icelldim > 2)
143 if (Objet_U::dimension > 2)
144 if (cgp_coord_write_data(fileId, baseId, zoneId, coordsIdz, &min, &max, zCoords.data()) != CG_OK)
145 Cerr << "Error Ecrire_CGNS_helper::cgns_write_grid_coord_data : cgp_coord_write_data - Z !" << finl, TRUST_CGNS_ERROR();
146#endif
147}
148
149template<TYPE_ECRITURE_CGNS _TYPE_>
150inline void Ecrire_CGNS_helper::cgns_sol_write(const int nb_zones_to_write, const int fileId, const int baseId, const int ind,
151 const int iteration, const std::vector<int>& zoneId, const std::string& LOC,
152 std::string& solname_som, std::string& solname_elem, std::string& solname_faces,
153 bool& solname_som_written, bool& solname_elem_written, bool& solname_faces_written,
154 int& flowId_som, int& flowId_elem, int& flowId_faces)
155{
156 // une fois par dt !!
157 constexpr bool is_SEQ = (_TYPE_ == TYPE_ECRITURE_CGNS::SEQ), is_PAR_OVER = (_TYPE_ == TYPE_ECRITURE_CGNS::PAR_OVER);
158 std::string solname = "FlowSolution_itr_" + std::to_string(iteration);
159 solname.resize(CGNS_STR_SIZE, ' ');
160
161 if (!solname_som_written && LOC == "SOM")
162 {
163 solname_som += solname;
164
165 // on boucle seulement sur les procs qui n'ont pas des nb_elem 0
166 for (int ii = 0; ii != nb_zones_to_write; ii++)
167 {
168 if (cg_sol_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], solname.c_str(), CGNS_ENUMV(Vertex), &flowId_som) != CG_OK)
169 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_sol_write -- SOM !" << finl, TRUST_CGNS_ERROR();
170
171 if (!is_SEQ)
172 if (cg_goto(fileId, baseId, "Zone_t", zoneId[is_PAR_OVER ? ii : ind], "FlowSolution_t", flowId_som, "end") != CG_OK)
173 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_goto -- SOM !" << finl, TRUST_CGNS_ERROR();
174
175 }
176
177 solname_som_written = true;
178 }
179
180 if (!solname_elem_written && LOC == "ELEM")
181 {
182 solname_elem += solname;
183
184 // on boucle seulement sur les procs qui n'ont pas des nb_elem 0
185 for (int ii = 0; ii != nb_zones_to_write; ii++)
186 {
187 if (cg_sol_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], solname.c_str(), CGNS_ENUMV(CellCenter), &flowId_elem) != CG_OK)
188 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_sol_write -- ELEM !" << finl, TRUST_CGNS_ERROR();
189
190 if (!is_SEQ)
191 if (cg_goto(fileId, baseId, "Zone_t", zoneId[is_PAR_OVER ? ii : ind], "FlowSolution_t", flowId_elem, "end") != CG_OK)
192 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_goto -- ELEM !" << finl, TRUST_CGNS_ERROR();
193
194 }
195
196 solname_elem_written = true;
197 }
198
199 if (!solname_faces_written && LOC == "FACES")
200 {
201 solname_faces += solname;
202
203 // on boucle seulement sur les procs qui n'ont pas des nb_elem 0
204 for (int ii = 0; ii != nb_zones_to_write; ii++)
205 {
206 if (cg_sol_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], solname.c_str(), CGNS_ENUMV(CellCenter), &flowId_faces) != CG_OK)
207 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_sol_write -- FACES !" << finl, TRUST_CGNS_ERROR();
208
209 if (!is_SEQ)
210 if (cg_goto(fileId, baseId, "Zone_t", zoneId[is_PAR_OVER ? ii : ind], "FlowSolution_t", flowId_faces, "end") != CG_OK)
211 Cerr << "Error Ecrire_CGNS_helper::cgns_sol_write : cg_goto -- FACES !" << finl, TRUST_CGNS_ERROR();
212
213 }
214
215 solname_faces_written = true;
216 }
217}
218
219template<TYPE_ECRITURE_CGNS _TYPE_>
220inline std::enable_if_t<_TYPE_ != TYPE_ECRITURE_CGNS::SEQ, void>
221Ecrire_CGNS_helper::cgns_field_write(const int nb_zones_to_write, const int fileId, const int baseId, const int ind, const std::vector<int>& zoneId, const std::string& LOC,
222 const int flowId_som, const int flowId_elem, const int flowId_faces,
223 const char * id_champ, int& fieldId_som, int& fieldId_elem, int& fieldId_faces)
224{
225#ifdef MPI_
226 constexpr bool is_PAR_OVER = (_TYPE_ == TYPE_ECRITURE_CGNS::PAR_OVER);
227 for (int ii = 0; ii != nb_zones_to_write; ii++)
228 {
229 if (LOC == "SOM")
230 {
231 if (cgp_field_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], flowId_som, CGNS_DOUBLE_TYPE, id_champ, &fieldId_som) != CG_OK)
232 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write : cgp_field_write -- SOM !" << finl, TRUST_CGNS_ERROR();
233 }
234 else if (LOC == "ELEM")
235 {
236 if (cgp_field_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], flowId_elem, CGNS_DOUBLE_TYPE, id_champ, &fieldId_elem) != CG_OK)
237 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write : cgp_field_write -- ELEM !" << finl, TRUST_CGNS_ERROR();
238 }
239 else if (LOC == "FACES")
240 {
241 if (cgp_field_write(fileId, baseId, zoneId[is_PAR_OVER ? ii : ind], flowId_faces, CGNS_DOUBLE_TYPE, id_champ, &fieldId_faces) != CG_OK)
242 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write : cgp_field_write -- FACES !" << finl, TRUST_CGNS_ERROR();
243 }
244 else
245 throw std::runtime_error("Ecrire_CGNS_helper::cgns_field_write => Unsupported LOC : " + LOC);
246 }
247#endif
248}
249
250template<TYPE_ECRITURE_CGNS _TYPE_>
251inline std::enable_if_t<_TYPE_ == TYPE_ECRITURE_CGNS::SEQ, void>
252Ecrire_CGNS_helper::cgns_field_write_data(const int fileId, const int baseId, const int ind, const std::vector<int>& zoneId,
253 const std::string& LOC, const int flowId_som, const int flowId_elem, const int flowId_faces, const int comp,
254 const char * id_champ, const DoubleTab& valeurs, int& fieldId_som, int& fieldId_elem, int& fieldId_faces)
255{
256 if (valeurs.dimension(1) == 1) /* No stride ! */
257 {
258 if (LOC == "SOM")
259 {
260 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_som, CGNS_DOUBLE_TYPE, id_champ, valeurs.addr(), &fieldId_som) != CG_OK)
261 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- SOM !" << finl, TRUST_CGNS_ERROR();
262 }
263 else if (LOC == "ELEM")
264 {
265 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_elem, CGNS_DOUBLE_TYPE, id_champ, valeurs.addr(), &fieldId_elem) != CG_OK)
266 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- ELEM !" << finl, TRUST_CGNS_ERROR();
267 }
268 else if (LOC == "FACES")
269 {
270 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_faces, CGNS_DOUBLE_TYPE, id_champ, valeurs.addr(), &fieldId_faces) != CG_OK)
271 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- FACES !" << finl, TRUST_CGNS_ERROR();
272 }
273 else
274 throw std::runtime_error("Ecrire_CGNS_helper::cgns_field_write_data => Unsupported LOC : " + LOC);
275 }
276 else
277 {
278 const int nb = valeurs.dimension(0);
279 DoubleTrav field_cgns(nb);
280 for (int i = 0; i < nb; i++)
281 field_cgns(i) = valeurs(i, comp);
282
283 if (LOC == "SOM")
284 {
285 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_som, CGNS_DOUBLE_TYPE, id_champ, field_cgns.addr(), &fieldId_som) != CG_OK)
286 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- SOM !" << finl, TRUST_CGNS_ERROR();
287 }
288 else if (LOC == "ELEM")
289 {
290 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_elem, CGNS_DOUBLE_TYPE, id_champ, field_cgns.addr(), &fieldId_elem) != CG_OK)
291 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- ELEM !" << finl, TRUST_CGNS_ERROR();
292 }
293 else if (LOC == "FACES")
294 {
295 if (cg_field_write(fileId, baseId, zoneId[ind], flowId_faces, CGNS_DOUBLE_TYPE, id_champ, field_cgns.addr(), &fieldId_faces) != CG_OK)
296 Cerr << "Error Ecrire_CGNS::cgns_write_field_seq : cg_field_write -- FACES !" << finl, TRUST_CGNS_ERROR();
297 }
298 else
299 throw std::runtime_error("Ecrire_CGNS_helper::cgns_field_write_data => Unsupported LOC : " + LOC);
300 }
301}
302
303template<TYPE_ECRITURE_CGNS _TYPE_>
304inline std::enable_if_t<_TYPE_ != TYPE_ECRITURE_CGNS::SEQ, void>
305Ecrire_CGNS_helper::cgns_field_write_data(const int fileId, const int baseId, const int ind, const std::vector<int>& zoneId, const std::string& LOC,
306 const int flowId_som, const int flowId_elem, const int flowId_faces,
307 const int fieldId_som, const int fieldId_elem, const int fieldId_faces,
308 const int comp, const cgsize_t min, const cgsize_t max, const DoubleTab& valeurs)
309{
310#ifdef MPI_
311 if (valeurs.dimension(1) == 1) /* No stride ! */
312 {
313 if (LOC == "SOM")
314 {
315 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_som, fieldId_som, &min, &max, valeurs.addr()) != CG_OK)
316 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- SOM !" << finl, TRUST_CGNS_ERROR();
317 }
318 else if (LOC == "ELEM")
319 {
320 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_elem, fieldId_elem, &min, &max, valeurs.addr()) != CG_OK)
321 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- ELEM !" << finl, TRUST_CGNS_ERROR();
322 }
323 else if (LOC == "FACES")
324 {
325 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_faces, fieldId_faces, &min, &max, valeurs.addr()) != CG_OK)
326 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- FACES !" << finl, TRUST_CGNS_ERROR();
327 }
328 else
329 throw std::runtime_error("Ecrire_CGNS_helper::cgns_field_write_data => Unsupported LOC : " + LOC);
330 }
331 else
332 {
333 const int nb = valeurs.dimension(0);
334 DoubleTrav field_cgns(nb);
335 for (int i = 0; i < nb; i++)
336 field_cgns(i) = valeurs(i, comp);
337
338 if (LOC == "SOM")
339 {
340 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_som, fieldId_som, &min, &max, field_cgns.addr()) != CG_OK)
341 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- SOM !" << finl, TRUST_CGNS_ERROR();
342 }
343 else if (LOC == "ELEM")
344 {
345 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_elem, fieldId_elem, &min, &max, field_cgns.addr()) != CG_OK)
346 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- ELEM !" << finl, TRUST_CGNS_ERROR();
347 }
348 else if (LOC == "FACES")
349 {
350 if (cgp_field_write_data(fileId, baseId, zoneId[ind], flowId_faces, fieldId_faces, &min, &max, field_cgns.addr()) != CG_OK)
351 Cerr << "Error Ecrire_CGNS_helper::cgns_field_write_data : cgp_field_write_data -- FACES !" << finl, TRUST_CGNS_ERROR();
352 }
353 else
354 throw std::runtime_error("Ecrire_CGNS_helper::cgns_field_write_data => Unsupported LOC : " + LOC);
355 }
356#endif
357}
358
359template<TYPE_ECRITURE_CGNS _TYPE_>
360inline void Ecrire_CGNS_helper::cgns_write_iters(const bool has_field, const int nb_zones_to_write, const int fileId, const int baseId, const int ind, const std::vector<int>& zoneId,
361 const std::string& LOC, const std::string& solname_som, const std::string& solname_elem, const std::string& solname_faces,const std::vector<double>& time_post)
362{
363 cgns_write_iters_deformable<_TYPE_>(false /* not deformable */, has_field, nb_zones_to_write, fileId, baseId, ind, zoneId,
364 LOC, solname_som, solname_elem, solname_faces, solname_elem /* inutile */, time_post);
365}
366
367template<TYPE_ECRITURE_CGNS _TYPE_>
368inline void Ecrire_CGNS_helper::cgns_write_iters_deformable(const bool is_deformable, const bool has_field, const int nb_zones_to_write, const int fileId, const int baseId, const int ind, const std::vector<int>& zoneId,
369 const std::string& LOC, const std::string& solname_som, const std::string& solname_elem, const std::string& solname_faces, const std::string& grid_name,
370 const std::vector<double>& time_post)
371{
372 const int nsteps = static_cast<int>(time_post.size());
373 const cgsize_t nuse = static_cast<cgsize_t>(nsteps);
374 assert (nsteps > 0);
375
376 // helper local : supprimer TOUTES les occurrences d'un DataArray_t <name> sous le noeud courant
377 auto delete_all_arrays_named = [](const char *arrname)
378 {
379 for (;;)
380 {
381 if (cg_delete_node(const_cast<char*>(arrname)) == CG_OK) continue; // recommence tant qu'il en trouve
382
383 break; // plus de noeud de ce nom
384 }
385 };
386
387 /* create BaseIterativeData */
388 if (cg_biter_write(fileId, baseId, "TimeIterValues", nsteps) != CG_OK)
389 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_biter_write !" << finl, TRUST_CGNS_ERROR();
390
391 /* Se positionner sur BaseIterativeData_t */
392 if (cg_goto(fileId, baseId, "BaseIterativeData_t", 1, "end") != CG_OK)
393 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_goto !" << finl, TRUST_CGNS_ERROR();
394
395 /* Supprimer + Re-ecrire TimeValues */
396 delete_all_arrays_named("TimeValues");
397 if (cg_array_write("TimeValues", CGNS_DOUBLE_TYPE, 1, &nuse, time_post.data()) != CG_OK)
398 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_array_write !" << finl, TRUST_CGNS_ERROR();
399
400 if (cg_simulation_type_write(fileId, baseId, CGNS_ENUMV(TimeAccurate)) != CG_OK)
401 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_simulation_type_write !" << finl, TRUST_CGNS_ERROR();
402
403 const char* solname = (LOC == "SOM") ? solname_som.c_str() : (LOC == "FACES") ? solname_faces.c_str() : solname_elem.c_str();
404
405 const cgsize_t idata[2] = { CGNS_STR_SIZE , nuse};
406
407 constexpr bool is_PAR_OVER = (_TYPE_ == TYPE_ECRITURE_CGNS::PAR_OVER);
409
410 for (int ii = 0; ii != nb_zones_to_write; ii++)
411 if (zoneId[is_PAR_OVER || is_comm_group ? ii : ind] != -123)
412 {
413 /* create ZoneIterativeData */
414 if (cg_ziter_write(fileId, baseId, zoneId[is_PAR_OVER || is_comm_group ? ii : ind], "ZoneIterativeData") != CG_OK)
415 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_ziter_write !" << finl, cg_error_exit();
416
417 if (cg_goto(fileId, baseId, "Zone_t", zoneId[is_PAR_OVER || is_comm_group ? ii : ind], "ZoneIterativeData_t", 1, "end") != CG_OK)
418 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_goto !" << finl, TRUST_CGNS_ERROR();
419
420 if (is_deformable)
421 {
422 delete_all_arrays_named("GridCoordinatesPointers");
423 assert(grid_name.size() == static_cast<size_t>(CGNS_STR_SIZE) * nuse);
424 if (cg_array_write("GridCoordinatesPointers", CGNS_ENUMV(Character), 2, idata, grid_name.c_str()) != CG_OK)
425 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_array_write GridCoordinatesPointers !" << finl, TRUST_CGNS_ERROR();
426 }
427
428 if (has_field)
429 {
430 delete_all_arrays_named("FlowSolutionPointers");
431 if (cg_array_write("FlowSolutionPointers", CGNS_ENUMV(Character), 2, idata, solname) != CG_OK)
432 Cerr << "Error Ecrire_CGNS_helper::cgns_write_iters : cg_array_write FlowSolutionPointers !" << finl, TRUST_CGNS_ERROR();
433 }
434 }
435}
436
437inline void Ecrire_CGNS_helper::cgns_write_zone_and_classic_links(const bool write_zone, const int fileId, const int baseId, const std::string& zone_name_to_write,
438 const cgsize_t *isize, int& zoneId, const int zone_goto_id,
439 const std::string& linkfile, const std::string& target_base_name, const std::string& target_zone_name,
440 const std::vector<std::string>& connect_names, const char *where, const bool write_connectivity)
441{
442 if (write_zone)
443 if (cg_zone_write(fileId, baseId, zone_name_to_write.c_str(), isize, CGNS_ENUMV(Unstructured), &zoneId) != CG_OK)
444 Cerr << "Error " << where << " : cg_zone_write !" << finl, TRUST_CGNS_ERROR();
445
446 if (cg_goto(fileId, baseId, "Zone_t", zone_goto_id, "end") != CG_OK)
447 Cerr << "Error " << where << " : cg_goto Zone_t !" << finl, TRUST_CGNS_ERROR();
448
449 std::string linkpath = "/" + target_base_name + "/" + target_zone_name + "/GridCoordinates/";
450
451 if (cg_link_write("GridCoordinates", linkfile.c_str(), linkpath.c_str()) != CG_OK)
452 Cerr << "Error " << where << " : cg_link_write GridCoordinates !" << finl, TRUST_CGNS_ERROR();
453
454 if (write_connectivity)
455 for (const auto &itr_conn : connect_names)
456 {
457 linkpath = "/" + target_base_name + "/" + target_zone_name + "/" + itr_conn + "/";
458
459 if (cg_link_write(itr_conn.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
460 Cerr << "Error " << where << " : cg_link_write connectivity " << itr_conn << " !" << finl, TRUST_CGNS_ERROR();
461 }
462}
463
464inline void Ecrire_CGNS_helper::cgns_write_solution_classic_links(const std::string& base_linkfile, const std::string& target_base_name, const std::string& target_zone_name,
465 const std::string& LOC, const std::vector<double>& time_post, const char *where)
466{
467 int idx = 0;
468 for (auto& itr_t : time_post)
469 {
470 const std::string solname = "FlowSolution_itr_" + std::to_string(idx);
471 const std::string linkfile = Option_CGNS::SINGLE_FILE_PER_COMM_GROUP ? base_linkfile + ".cgns" :
472 base_linkfile + ".solution." + convert_double_to_string(itr_t) + ".cgns"; // file name
473 const std::string linkpath = "/" + target_base_name + "/" + target_zone_name + "/" + solname + "/";
474
475 if (cg_link_write(solname.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
476 Cerr << "Error " << where << " : cg_link_write solution !" << finl, TRUST_CGNS_ERROR();
477 idx++;
478 }
479}
480
481inline void Ecrire_CGNS_helper::cgns_write_zone_and_deformable_links(const bool write_zone, const bool has_field, const int fileId, const int baseId, const std::string& zone_name_to_write,
482 const cgsize_t *isize, int& zoneId, const int zone_goto_id,
483 const std::string& file_prefix, const std::string& target_base_name, const std::string& target_zone_name,
484 const std::vector<std::string>& connect_names, const Nom& nom_dom, const std::string& LOC,
485 const std::vector<double>& time_post, const char *where, const bool write_connectivity)
486{
487 if (write_zone)
488 if (cg_zone_write(fileId, baseId, zone_name_to_write.c_str(), isize, CGNS_ENUMV(Unstructured), &zoneId) != CG_OK)
489 Cerr << "Error " << where << " : cg_zone_write !" << finl, TRUST_CGNS_ERROR();
490
491 if (cg_goto(fileId, baseId, "Zone_t", zone_goto_id, "end") != CG_OK)
492 Cerr << "Error " << where << " : cg_goto Zone_t !" << finl, TRUST_CGNS_ERROR();
493
494 std::string linkfile, linkpath, grid_name_loc;
495 bool conn_written = false;
496
497 int idx = 0;
498 for (const auto &itr_t : time_post)
499 {
500 linkfile = Option_CGNS::SINGLE_FILE_PER_COMM_GROUP ? file_prefix + ".cgns" :
501 file_prefix + ".solution." + convert_double_to_string(itr_t) + ".cgns";
502
503 linkpath = "/" + target_base_name + "/" + target_zone_name + "/GridCoordinates/";
504
505 grid_name_loc = "GridCoordinates";
506 if (conn_written)
507 {
508 grid_name_loc += "_itr_";
509 grid_name_loc += std::to_string(idx);
510 }
511
512 if (cg_link_write(grid_name_loc.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
513 Cerr << "Error " << where << " : cg_link_write GridCoordinates !" << finl, TRUST_CGNS_ERROR();
514
515 if (!conn_written)
516 {
517 if (write_connectivity)
518 for (const auto &itr_conn : connect_names)
519 {
520 linkpath = "/" + target_base_name + "/" + target_zone_name + "/" + itr_conn + "/";
521
522 if (cg_link_write(itr_conn.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
523 Cerr << "Error " << where << " : cg_link_write connectivity !" << finl, TRUST_CGNS_ERROR();
524 }
525 conn_written = true;
526 }
527
528 if (has_field)
529 {
530 const std::string solname = "FlowSolution_itr_" + std::to_string(idx);
531 linkpath = "/" + nom_dom.getString() + "/" + nom_dom.getString() + "/" + solname + "/";
532
533 if (cg_link_write(solname.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
534 Cerr << "Error " << where << " : cg_link_write FlowSolution " << solname << " !" << finl, TRUST_CGNS_ERROR();
535 }
536 idx++;
537 }
538}
539
540inline void Ecrire_CGNS_helper::cgns_write_connectivity_deformable_links(const int fileId, const int baseId, const int zone_goto_id, const std::string& linkfile,
541 const std::string& target_base_name, const std::string& target_zone_name,
542 const std::vector<std::string>& connect_names, const char *where)
543{
544 if (cg_goto(fileId, baseId, "Zone_t", zone_goto_id, "end") != CG_OK)
545 Cerr << "Error " << where << " : cg_goto Zone_t !" << finl, TRUST_CGNS_ERROR();
546
547 for (auto &itr_conn : connect_names)
548 {
549 const std::string linkpath = "/" + target_base_name + "/" + target_zone_name + "/" + itr_conn + "/";
550
551 if (cg_link_write(itr_conn.c_str(), linkfile.c_str(), linkpath.c_str()) != CG_OK)
552 Cerr << "Error " << where << " : cg_link_write connectivity !" << finl, TRUST_CGNS_ERROR();
553 }
554}
555
556#endif /* Ecrire_CGNS_helper_tpp_included */
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
static int dimension
Definition Objet_U.h:99
static bool LINKED_FILES_PER_COMM_GROUP
Definition Option_CGNS.h:52
static bool SINGLE_FILE_PER_COMM_GROUP
Definition Option_CGNS.h:48
_TYPE_ * addr()
_SIZE_ dimension(int d) const
Definition TRUSTTab.tpp:133