IOSS 2.0
Loading...
Searching...
No Matches
Iocgns_Utils.h
Go to the documentation of this file.
1// Copyright(C) 1999-2024 National Technology & Engineering Solutions
2// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
3// NTESS, the U.S. Government retains certain rights in this software.
4//
5// See packages/seacas/LICENSE for details
6
7#pragma once
8
9#include "Ioss_CodeTypes.h"
10#include "Ioss_DatabaseIO.h"
12#include "Ioss_FaceGenerator.h"
13#include "Ioss_Region.h"
14#include "Ioss_SideBlock.h"
15#include "Ioss_SideSet.h"
17#include "Ioss_Utils.h"
18#include "cgns/Iocgns_Defines.h"
19#include <array>
20#include <cgnslib.h>
21#include <cgnstypes.h>
22#include <map>
23#include <ostream>
24#include <stddef.h>
25#include <string>
26#include <vector>
27
28#include "Ioss_Field.h"
29#include "iocgns_export.h"
30
31namespace Iocgns {
32 class Utils;
33} // namespace Iocgns
34namespace Ioss {
35 class Assembly;
36 class DatabaseIO;
37 class EntityBlock;
38 class GroupingEntity;
39 class Region;
40 class StructuredBlock;
41 enum class MeshType;
42} // namespace Ioss
43
44// Used in Iocgns_DatabaseIO.C and Iocgns_ParallelDatabase.C
45// non-Member function -- can't access m_cgnsFilePtr; make sure cgns_file_ptr is passed in...
46#define CGCHECK(funcall) \
47 do { \
48 if ((funcall) != CG_OK) { \
49 Iocgns::Utils::cgns_error(cgns_file_ptr, __FILE__, __func__, __LINE__, myProcessor); \
50 } \
51 } while (0)
52
53// Member function -- can access m_cgnsFilePtr
54#define CGCHECKM(funcall) \
55 do { \
56 if ((funcall) != CG_OK) { \
57 Iocgns::Utils::cgns_error(m_cgnsFilePtr, __FILE__, __func__, __LINE__, myProcessor); \
58 } \
59 } while (0)
60
61#define CGCHECKNP(funcall) \
62 do { \
63 if ((funcall) != CG_OK) { \
64 Iocgns::Utils::cgns_error(cgns_file_ptr, __FILE__, __func__, __LINE__, -1); \
65 } \
66 } while (0)
67
68// Used in Iocgns_Decomposition.C
69#define CGCHECK2(funcall) \
70 do { \
71 if ((funcall) != CG_OK) { \
72 Iocgns::Utils::cgns_error(filePtr, __FILE__, __func__, __LINE__, \
73 m_decomposition.m_processor); \
74 } \
75 } while (0)
76
77IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(BCType_t) t) { return BCTypeName[t]; }
78IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(DataType_t) t) { return DataTypeName[t]; }
79IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(ElementType_t) t) { return ElementTypeName[t]; }
80IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(GridConnectivityType_t) t)
81{
82 return GridConnectivityTypeName[t];
83}
84IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(GridLocation_t) t) { return GridLocationName[t]; }
85IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(PointSetType_t) t) { return PointSetTypeName[t]; }
86IOSS_NODISCARD inline auto format_as(CGNS_ENUMT(ZoneType_t) t) { return ZoneTypeName[t]; }
87
88namespace Iocgns {
89 class StructuredZoneData;
90
91 struct IOCGNS_EXPORT ZoneBC
92 {
93 ZoneBC(std::string bc_name, std::array<cgsize_t, 2> &point_range)
94 : name(std::move(bc_name)), range_beg(point_range[0]), range_end(point_range[1])
95 {
96 }
97
98 std::string name;
99 cgsize_t range_beg;
100 cgsize_t range_end;
101 };
102
103 class IOCGNS_EXPORT Utils
104 {
105 public:
106 IOSS_NODISCARD static std::pair<std::string, int> decompose_name(const std::string &name,
107 bool is_parallel);
108 IOSS_NODISCARD static std::string decompose_sb_name(const std::string &name);
109
110 IOSS_NODISCARD static size_t index(const Ioss::Field &field);
111
112 static void cgns_error(int cgnsid, const char *file, const char *function, int lineno,
113 int processor);
114
115 static void update_db_zone_property(int cgns_file_ptr, const Ioss::Region *region,
116 int myProcessor, bool is_parallel, bool is_parallel_io);
117 IOSS_NODISCARD static int get_db_zone(const Ioss::GroupingEntity *entity);
118 static void set_field_index(const Ioss::Field &field, size_t index,
119 CGNS_ENUMT(GridLocation_t) location);
120 IOSS_NODISCARD static bool is_cell_field(const Ioss::Field &field);
121
122 template <typename INT>
123 static void map_cgns_connectivity(const Ioss::ElementTopology *topo, size_t element_count,
124 INT *idata)
125 {
126 // Map from CGNS to IOSS/Exodus/Patran order...
127 switch (topo->shape()) {
129 switch (topo->number_nodes()) {
130 case 8:
131 case 20: break;
132 case 27: {
133 // nodes 1 through 20 are the same...
134 //
135 // ioss: 21, 22, 23, 24, 25, 26, 27 [zero-based: 20, 21, 22, 23, 24, 25, 26]
136 // cgns: 27, 21, 26, 25, 23, 22, 24 [zero-based: 26, 20, 25, 24, 22, 21, 23]
137 static std::array<int, 7> hex27_map{26, 20, 25, 24, 22, 21, 23};
138 for (size_t i = 0; i < element_count; i++) {
139 size_t con_beg = 27 * i; // start of connectivity for i'th element.
140 std::array<int, 7> reorder;
141 for (size_t j = 0; j < 7; j++) {
142 reorder[j] = idata[con_beg + hex27_map[j]];
143 }
144
145 for (size_t j = 0; j < 7; j++) {
146 idata[con_beg + 20 + j] = reorder[j];
147 }
148 }
149 }
150 }
151 break;
152 default:
153 // do nothing cgns ordering matches ioss/exodus/patran (or not handled yet... todo: add
154 // error checking)
155 ;
156 }
157 }
158
159 template <typename INT>
160 static void unmap_cgns_connectivity(const Ioss::ElementTopology *topo, size_t element_count,
161 INT *idata)
162 {
163 // Map from IOSS/Exodus/Patran to CGNS order...
164 switch (topo->shape()) {
166 switch (topo->number_nodes()) {
167 case 8:
168 case 20: break;
169 case 27: {
170 // nodes 1 through 20 are the same...
171 //
172 // ioss: 21, 22, 23, 24, 25, 26, 27 [zero-based: 20, 21, 22, 23, 24, 25, 26]
173 // cgns: 27, 21, 26, 25, 23, 22, 24 [zero-based: 26, 20, 25, 24, 22, 21, 23]
174 static std::array<int, 7> hex27_map{26, 20, 25, 24, 22, 21, 23};
175 for (size_t i = 0; i < element_count; i++) {
176 size_t con_beg = 27 * i; // start of connectivity for i'th element.
177 std::array<int, 7> reorder;
178 for (size_t j = 0; j < 7; j++) {
179 reorder[j] = idata[con_beg + 20 + j];
180 }
181
182 for (size_t j = 0; j < 7; j++) {
183 idata[con_beg + hex27_map[j]] = reorder[j];
184 }
185 }
186 }
187 }
188 break;
189 default:
190 // do nothing cgns ordering matches ioss/exodus/patran (or not handled yet... todo: add
191 // error checking)
192 ;
193 }
194 }
195
196 template <typename INT>
197 static void map_cgns_face_to_ioss(const Ioss::ElementTopology *parent_topo, size_t num_to_get,
198 INT *idata)
199 {
200 // The {topo}_map[] arrays map from CGNS face# to IOSS face#.
201 // See http://cgns.github.io/CGNS_docs_current/sids/conv.html#unstructgrid
202 // NOTE: '0' for first entry is to account for 1-based face numbering.
203
204 switch (parent_topo->shape()) {
206 static std::array<int, 7> hex_map = {0, 5, 1, 2, 3, 4, 6};
207 for (size_t i = 0; i < num_to_get; i++) {
208 idata[2 * i + 1] = hex_map[idata[2 * i + 1]];
209 }
210 } break;
211
213 static std::array<int, 5> tet_map = {0, 4, 1, 2, 3};
214 for (size_t i = 0; i < num_to_get; i++) {
215 idata[2 * i + 1] = tet_map[idata[2 * i + 1]];
216 }
217 } break;
218
220 static std::array<int, 6> pyr_map = {0, 5, 1, 2, 3, 4};
221 for (size_t i = 0; i < num_to_get; i++) {
222 idata[2 * i + 1] = pyr_map[idata[2 * i + 1]];
223 }
224 } break;
225
227#if 0
228 static std::array<int, 6> wed_map = {0, 1, 2, 3, 4, 5}; // Same
229 // Not needed -- maps 1 to 1
230 for (size_t i=0; i < num_to_get; i++) {
231 idata[2*i+1] = wed_map[idata[2*i+1]];
232 }
233#endif
234 break;
235 default:;
236 }
237 }
238
239 static void map_ioss_face_to_cgns(const Ioss::ElementTopology *parent_topo, size_t num_to_get,
240 CGNSIntVector &data)
241 {
242 // The {topo}_map[] arrays map from CGNS face# to IOSS face#.
243 // See http://cgns.github.io/CGNS_docs_current/sids/conv.html#unstructgrid
244 // NOTE: '0' for first entry is to account for 1-based face numbering.
245
246 switch (parent_topo->shape()) {
248 static int hex_map[] = {0, 2, 3, 4, 5, 1, 6};
249 for (size_t i = 0; i < num_to_get; i++) {
250 data[num_to_get * 2 + i] = hex_map[data[num_to_get * 2 + i]];
251 }
252 } break;
253
255 static int tet_map[] = {0, 2, 3, 4, 1};
256 for (size_t i = 0; i < num_to_get; i++) {
257 data[num_to_get * 2 + i] = tet_map[data[num_to_get * 2 + i]];
258 }
259 } break;
260
262 static int pyr_map[] = {0, 2, 3, 4, 5, 1};
263 for (size_t i = 0; i < num_to_get; i++) {
264 data[num_to_get * 2 + i] = pyr_map[data[num_to_get * 2 + i]];
265 }
266 } break;
267
269#if 0
270 static int wed_map[] = {0, 1, 2, 3, 4, 5}; // Same
271 // Not needed -- maps 1 to 1
272 for (size_t i=0; i < num_to_get; i++) {
273 data[num_to_get * 2 + i] = wed_map[data[num_to_get * 2 + i]];
274 }
275#endif
276 break;
277 default:;
278 }
279 }
280
281 IOSS_NODISCARD static std::vector<ZoneBC> parse_zonebc_sideblocks(int cgns_file_ptr, int base,
282 int zone, int myProcessor);
283
284 static void
285 generate_boundary_faces(Ioss::Region *region,
286 std::map<std::string, Ioss::FaceUnorderedSet> &boundary_faces,
287 Ioss::Field::BasicType field_type);
288
289 static void write_flow_solution_metadata(int file_ptr, int base_ptr, Ioss::Region *region,
290 int state, const int *vertex_solution_index,
291 const int *cell_center_solution_index,
292 bool is_parallel_io);
293 IOSS_NODISCARD static int find_solution_index(int cgns_file_ptr, int base, int zone, int step,
294 CGNS_ENUMT(GridLocation_t) location);
295 IOSS_NODISCARD static Ioss::MeshType check_mesh_type(int cgns_file_ptr);
296
297 static void output_assembly(int file_ptr, const Ioss::Assembly *assembly, bool is_parallel_io,
298 bool appending = false);
299 static void output_assemblies(int file_ptr, const Ioss::Region &region, bool is_parallel_io);
300
301 static void write_state_meta_data(int file_ptr, const Ioss::Region &region,
302 bool is_parallel_io);
303 static size_t common_write_meta_data(int file_ptr, const Ioss::Region &region,
304 std::vector<size_t> &zone_offset, bool is_parallel);
305 static size_t resolve_nodes(Ioss::Region &region, int my_processor, bool is_parallel);
306 IOSS_NODISCARD static std::vector<std::vector<std::pair<size_t, size_t>>>
307 resolve_processor_shared_nodes(Ioss::Region &region, int my_processor);
308
309 IOSS_NODISCARD static CGNS_ENUMT(ElementType_t) map_topology_to_cgns(const std::string &name);
310 IOSS_NODISCARD static std::string map_cgns_to_topology_type(CGNS_ENUMT(ElementType_t) type);
311 static void add_sidesets(int cgns_file_ptr, Ioss::DatabaseIO *db);
312 static void add_assemblies(int cgns_file_ptr, Ioss::DatabaseIO *db);
313 static void add_to_assembly(int cgns_file_ptr, Ioss::Region *region, Ioss::EntityBlock *block,
314 int base, int zone);
315
316 static void add_structured_boundary_conditions(int cgns_file_ptr, Ioss::StructuredBlock *block,
317 bool is_parallel_io);
318 static void add_structured_boundary_conditions_fpp(int cgns_file_ptr,
319 Ioss::StructuredBlock *block);
320 static void add_structured_boundary_conditions_pio(int cgns_file_ptr,
321 Ioss::StructuredBlock *block);
322
323 static void finalize_database(int cgns_file_ptr, const std::vector<double> &timesteps,
324 Ioss::Region *region, int myProcessor, bool is_parallel_io);
325 static int get_step_times(int cgns_file_ptr, std::vector<double> &timesteps,
326 Ioss::Region *region, double timeScaleFactor, int myProcessor);
327 static void add_transient_variables(int cgns_file_ptr, const std::vector<double> &timesteps,
328 Ioss::Region *region, int myProcessor, bool is_parallel_io);
329
330 static void set_line_decomposition(int cgns_file_ptr, const std::string &line_decomposition,
331 std::vector<Iocgns::StructuredZoneData *> &zones, int rank,
332 bool verbose);
333 static void decompose_model(std::vector<Iocgns::StructuredZoneData *> &zones, int proc_count,
334 int rank, double load_balance_threshold, bool verbose);
335 static int pre_split(std::vector<Iocgns::StructuredZoneData *> &zones, double avg_work,
336 double load_balance, int proc_rank, int proc_count, bool verbose);
337 static void assign_zones_to_procs(std::vector<Iocgns::StructuredZoneData *> &zones,
338 std::vector<size_t> &work_vector, bool verbose);
339 IOSS_NODISCARD static std::string show_config();
340
341 template <typename INT>
342 static void generate_block_faces(Ioss::ElementTopology *topo, size_t num_elem,
343 const std::vector<INT> &connectivity,
344 Ioss::FaceUnorderedSet &boundary,
345 const std::vector<INT> &zone_local_zone_global);
346 };
347} // namespace Iocgns
std::vector< cgsize_t > CGNSIntVector
Definition Iocgns_Defines.h:12
CGNS_ENUMT(ElementType_t)
Definition Iocgns_Utils.C:1470
IOSS_NODISCARD auto format_as(CGNS_ENUMT(BCType_t) t)
Definition Iocgns_Utils.h:77
#define IOSS_NODISCARD
Definition Ioss_CodeTypes.h:54
Definition Iocgns_Utils.h:104
static void unmap_cgns_connectivity(const Ioss::ElementTopology *topo, size_t element_count, INT *idata)
Definition Iocgns_Utils.h:160
static void map_cgns_face_to_ioss(const Ioss::ElementTopology *parent_topo, size_t num_to_get, INT *idata)
Definition Iocgns_Utils.h:197
static void map_ioss_face_to_cgns(const Ioss::ElementTopology *parent_topo, size_t num_to_get, CGNSIntVector &data)
Definition Iocgns_Utils.h:239
static void map_cgns_connectivity(const Ioss::ElementTopology *topo, size_t element_count, INT *idata)
Definition Iocgns_Utils.h:123
A homogeneous collection of other GroupingEntities.
Definition Ioss_Assembly.h:31
An input or output Database.
Definition Ioss_DatabaseIO.h:63
Represents an element topology.
Definition Ioss_ElementTopology.h:69
virtual IOSS_NODISCARD ElementShape shape() const =0
virtual IOSS_NODISCARD int number_nodes() const =0
Base class for all 'block'-type grouping entities, which means all members of the block are similar o...
Definition Ioss_EntityBlock.h:38
Holds metadata for bulk data associated with a GroupingEntity.
Definition Ioss_Field.h:25
BasicType
The basic data type held in the field.
Definition Ioss_Field.h:29
Base class for all 'grouping' entities. The following derived classes are typical:
Definition Ioss_GroupingEntity.h:67
A grouping entity that contains other grouping entities.
Definition Ioss_Region.h:90
A structured zone – i,j,k.
Definition Ioss_StructuredBlock.h:104
Definition robin_set.h:90
A namespace for the CGNS database format.
Definition Iocgns_DatabaseIO.C:555
The main namespace for the Ioss library.
Definition Ioad_DatabaseIO.C:40
MeshType
The mesh type – structured, unstructured, hybrid (future), or unknown.
Definition Ioss_MeshType.h:12
Definition Iocgns_Utils.h:92
cgsize_t range_beg
Definition Iocgns_Utils.h:99
std::string name
Definition Iocgns_Utils.h:98
ZoneBC(std::string bc_name, std::array< cgsize_t, 2 > &point_range)
Definition Iocgns_Utils.h:93
cgsize_t range_end
Definition Iocgns_Utils.h:100