| 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_ElementTopology.h" |
| 11 |
#include "Ioss_EntityType.h" |
| 12 |
#include "Ioss_Field.h" |
| 13 |
#include "Ioss_Property.h" |
| 14 |
#include "Ioss_Sort.h" |
| 15 |
#include <algorithm> // for sort, lower_bound, copy, etc |
| 16 |
#include <cassert> |
| 17 |
#include <cmath> |
| 18 |
#include <cstddef> // for size_t |
| 19 |
#include <cstdint> // for int64_t |
| 20 |
#include <cstdlib> // for nullptrr |
| 21 |
#include <iostream> // for ostringstream, etcstream, etc |
| 22 |
#include <stdexcept> // for runtime_error |
| 23 |
#include <string> // for string |
| 24 |
#include <vector> // for vector |
| 25 |
|
| 26 |
#include "ioss_export.h" |
| 27 |
|
| 28 |
namespace Ioss { |
| 29 |
class DatabaseIO; |
| 30 |
class Field; |
| 31 |
class GroupingEntity; |
| 32 |
class Region; |
| 33 |
class SideBlock; |
| 34 |
class PropertyManager; |
| 35 |
enum class ElementShape : unsigned int; |
| 36 |
} // namespace Ioss |
| 37 |
|
| 38 |
[[noreturn]] inline void IOSS_ERROR(const std::ostringstream &errmsg) |
| 39 |
{ |
| 40 |
throw std::runtime_error((errmsg).str()); |
| 41 |
} |
| 42 |
|
| 43 |
[[noreturn]] inline void IOSS_ERROR(const std::string &errmsg) { throw std::runtime_error(errmsg); } |
| 44 |
|
| 45 |
#ifdef NDEBUG |
| 46 |
#define IOSS_ASSERT_USED(x) (void)x |
| 47 |
#else |
| 48 |
#define IOSS_ASSERT_USED(x) |
| 49 |
#endif |
| 50 |
|
| 51 |
// We have been relying on the assumption that calling `.data()` on an empty vector |
| 52 |
// will return `nullptr`. However, according to cppreference (based on the standard): |
| 53 |
// |
| 54 |
// `If size() is 0, data() may or may not return a null pointer.` |
| 55 |
// |
| 56 |
// We don't have any systems on which we have found that (yet?), but this is proactive |
| 57 |
// in removing our use of `.data()` on potentially empty vectors... |
| 58 |
template <typename T> IOSS_NODISCARD constexpr T *Data(std::vector<T> &vec) |
| 59 |
{ |
| 60 |
if (vec.empty()) { |
| 61 |
return nullptr; |
| 62 |
} |
| 63 |
return vec.data(); |
| 64 |
} |
| 65 |
|
| 66 |
template <typename T> IOSS_NODISCARD constexpr const T *Data(const std::vector<T> &vec) |
| 67 |
{ |
| 68 |
if (vec.empty()) { |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
return vec.data(); |
| 72 |
} |
| 73 |
|
| 74 |
template <typename T, size_t N> IOSS_NODISCARD constexpr T *Data(std::array<T, N> &arr) |
| 75 |
{ |
| 76 |
return N == 0 ? nullptr : arr.data(); |
| 77 |
} |
| 78 |
|
| 79 |
template <typename T, size_t N> IOSS_NODISCARD constexpr const T *Data(const std::array<T, N> &arr) |
| 80 |
{ |
| 81 |
return N == 0 ? nullptr : arr.data(); |
| 82 |
} |
| 83 |
|
| 84 |
namespace Ioss { |
| 85 |
/* \brief Utility methods. |
| 86 |
*/ |
| 87 |
class IOSS_EXPORT Utils |
| 88 |
{ |
| 89 |
static std::ostream |
| 90 |
*m_outputStream; ///< general informational output (very rare). Default std::cerr |
| 91 |
static std::ostream *m_debugStream; ///< debug output when requested. Default std::cerr |
| 92 |
static std::ostream *m_warningStream; ///< IOSS warning output. Default std::cerr |
| 93 |
static std::string m_preWarningText; ///< is a string that prepends all warning message output. |
| 94 |
///< Default is "\nIOSS WARNING: " |
| 95 |
public: |
| 96 |
/** |
| 97 |
* \defgroup IossStreams Streams used for IOSS output |
| 98 |
*@{ |
| 99 |
*/ |
| 100 |
/** \brief set the stream for all streams (output, debug, and warning) to the specified |
| 101 |
* `out_stream` |
| 102 |
*/ |
| 103 |
static void set_all_streams(std::ostream &out_stream); |
| 104 |
|
| 105 |
/** \brief get the debug stream. |
| 106 |
*/ |
| 107 |
IOSS_NODISCARD static std::ostream &get_debug_stream(); |
| 108 |
|
| 109 |
/** \brief get the warning stream. |
| 110 |
*/ |
| 111 |
IOSS_NODISCARD static std::ostream &get_warning_stream(); |
| 112 |
|
| 113 |
/** \brief get the output stream. |
| 114 |
*/ |
| 115 |
IOSS_NODISCARD static std::ostream &get_output_stream(); |
| 116 |
|
| 117 |
IOSS_NODISCARD static std::string &get_warning_text() { return m_preWarningText; } |
| 118 |
|
| 119 |
/** \brief set the output stream to the specified `output_stream` |
| 120 |
*/ |
| 121 |
static void set_output_stream(std::ostream &output_stream); |
| 122 |
|
| 123 |
/** \brief set the debug stream to the specified `debug_stream` |
| 124 |
*/ |
| 125 |
static void set_debug_stream(std::ostream &debug_stream); |
| 126 |
|
| 127 |
/** \brief set the warning stream to the specified `warning_stream` |
| 128 |
*/ |
| 129 |
static void set_warning_stream(std::ostream &warning_stream); |
| 130 |
|
| 131 |
/** \brief set the pre-warning text |
| 132 |
* Sets the text output prior to a warning to the specified text. |
| 133 |
* Pass an empty string to disable this. Default is `"\nIOSS WARNING: "` |
| 134 |
*/ |
| 135 |
static void set_pre_warning_text(const std::string &text) { m_preWarningText = text; } |
| 136 |
/** @}*/ |
| 137 |
|
| 138 |
static void copyright(std::ostream &out, const std::string &year_range); |
| 139 |
|
| 140 |
IOSS_NODISCARD static bool check_valid_change_set_name(const std::string &cs_name, |
| 141 |
const Ioss::Region ®ion, |
| 142 |
int rank = 0); |
| 143 |
|
| 144 |
static void check_dynamic_cast(const void *ptr) |
| 145 |
{ |
| 146 |
if (ptr == nullptr) { |
| 147 |
std::ostringstream errmsg; |
| 148 |
errmsg << "INTERNAL ERROR: Invalid dynamic cast returned nullptr\n"; |
| 149 |
IOSS_ERROR(errmsg); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
// NOTE: This code previously checked for existence of filesystem include, but |
| 154 |
// gcc-8.X has the include but needs a library, also intel and clang |
| 155 |
// pretend to be gcc, so macro to test for usability of filesystem |
| 156 |
// was complicated and we can easily get by with the following code. |
| 157 |
static bool is_path_absolute(const std::string &path) |
| 158 |
{ |
| 159 |
if (!path.empty()) { |
| 160 |
#ifdef __IOSS_WINDOWS__ |
| 161 |
return path[0] == '\\' && path[1] == ':'; |
| 162 |
#else |
| 163 |
return path[0] == '/'; |
| 164 |
#endif |
| 165 |
} |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
/** \brief guess file type from extension */ |
| 170 |
IOSS_NODISCARD static std::string get_type_from_file(const std::string &filename); |
| 171 |
|
| 172 |
template <typename T> static void uniquify(std::vector<T> &vec, bool skip_first = false) |
| 173 |
{ |
| 174 |
auto it = vec.begin(); |
| 175 |
if (skip_first) { |
| 176 |
it++; |
| 177 |
} |
| 178 |
Ioss::sort(it, vec.end()); |
| 179 |
vec.resize(unique(vec, skip_first)); |
| 180 |
vec.shrink_to_fit(); |
| 181 |
} |
| 182 |
|
| 183 |
template <typename T> static void generate_index(std::vector<T> &index) |
| 184 |
{ |
| 185 |
T sum = 0; |
| 186 |
for (size_t i = 0; i < index.size() - 1; i++) { |
| 187 |
T cnt = index[i]; |
| 188 |
index[i] = sum; |
| 189 |
sum += cnt; |
| 190 |
} |
| 191 |
index.back() = sum; |
| 192 |
} |
| 193 |
|
| 194 |
template <typename T> |
| 195 |
IOSS_NODISCARD static T find_index_location(T node, const std::vector<T> &index) |
| 196 |
{ |
| 197 |
// 0-based node numbering |
| 198 |
// index[p] = first node (0-based) on processor p |
| 199 |
|
| 200 |
#if 1 |
| 201 |
// Assume data coherence. I.e., a new search will be close to the |
| 202 |
// previous search. |
| 203 |
static size_t prev = 1; |
| 204 |
|
| 205 |
size_t nproc = index.size(); |
| 206 |
if (prev < nproc && index[prev - 1] <= node && index[prev] > node) { |
| 207 |
return prev - 1; |
| 208 |
} |
| 209 |
|
| 210 |
for (size_t p = 1; p < nproc; p++) { |
| 211 |
if (index[p] > node) { |
| 212 |
prev = p; |
| 213 |
return p - 1; |
| 214 |
} |
| 215 |
} |
| 216 |
std::ostringstream errmsg; |
| 217 |
errmsg << "FATAL ERROR: find_index_location. Searching for " << node << " in:\n"; |
| 218 |
for (auto idx : index) { |
| 219 |
errmsg << idx << ", "; |
| 220 |
} |
| 221 |
errmsg << "\n"; |
| 222 |
IOSS_ERROR(errmsg); |
| 223 |
#else |
| 224 |
return std::distance(index.begin(), std::upper_bound(index.begin(), index.end(), node)) - 1; |
| 225 |
#endif |
| 226 |
} |
| 227 |
|
| 228 |
static void copy_string(char *dest, char const *source, size_t elements); |
| 229 |
|
| 230 |
static void copy_string(char *dest, const std::string &source, size_t elements) |
| 231 |
{ |
| 232 |
copy_string(dest, source.c_str(), elements); |
| 233 |
} |
| 234 |
|
| 235 |
template <size_t size> static void copy_string(char (&output)[size], const std::string &source) |
| 236 |
{ |
| 237 |
copy_string(output, source.c_str(), size); |
| 238 |
} |
| 239 |
|
| 240 |
template <size_t size> static void copy_string(char (&output)[size], const char *source) |
| 241 |
{ |
| 242 |
// Copy the string don't copy too many bytes. |
| 243 |
copy_string(output, source, size); |
| 244 |
} |
| 245 |
|
| 246 |
template <typename T> static void clear(std::vector<T> &vec) |
| 247 |
{ |
| 248 |
vec.clear(); |
| 249 |
vec.shrink_to_fit(); |
| 250 |
assert(vec.capacity() == 0); |
| 251 |
} |
| 252 |
|
| 253 |
/** |
| 254 |
* Returns the number of digits required to print the number. |
| 255 |
* If `use_commas` is specified, then the width will be adjusted |
| 256 |
* to account for the comma used every 3 digits. |
| 257 |
* (1,234,567,890 would return 13) |
| 258 |
* Typically used with the `fmt::print()` functions as: |
| 259 |
* ``` |
| 260 |
* fmt::print("{:{}}", number, number_width(number,true)) |
| 261 |
* fmt::print("{:{}d}", number, number_width(number,false)) |
| 262 |
* ``` |
| 263 |
*/ |
| 264 |
IOSS_NODISCARD inline static int number_width(const size_t number, bool use_commas = false) |
| 265 |
{ |
| 266 |
if (number == 0) { |
| 267 |
return 1; |
| 268 |
} |
| 269 |
int width = static_cast<int>(std::floor(std::log10(number))) + 1; |
| 270 |
if (use_commas) { |
| 271 |
width += ((width - 1) / 3); |
| 272 |
} |
| 273 |
return width; |
| 274 |
} |
| 275 |
|
| 276 |
IOSS_NODISCARD inline static int power_2(int count) |
| 277 |
{ |
| 278 |
// Return the power of two which is equal to or greater than `count` |
| 279 |
// count = 15 -> returns 16 |
| 280 |
// count = 16 -> returns 16 |
| 281 |
// count = 17 -> returns 32 |
| 282 |
|
| 283 |
// Use brute force... |
| 284 |
int pow2 = 1; |
| 285 |
while (pow2 < count) { |
| 286 |
pow2 *= 2; |
| 287 |
} |
| 288 |
return pow2; |
| 289 |
} |
| 290 |
|
| 291 |
template <typename T> |
| 292 |
IOSS_NODISCARD static bool check_block_order(IOSS_MAYBE_UNUSED const std::vector<T *> &blocks) |
| 293 |
{ |
| 294 |
#ifndef NDEBUG |
| 295 |
// Verify that element blocks are defined in sorted offset order... |
| 296 |
typename std::vector<T *>::const_iterator I; |
| 297 |
|
| 298 |
int64_t eb_offset = -1; |
| 299 |
for (I = blocks.begin(); I != blocks.end(); ++I) { |
| 300 |
int64_t this_off = (*I)->get_offset(); |
| 301 |
if (this_off < eb_offset) { |
| 302 |
{ |
| 303 |
{ |
| 304 |
return false; |
| 305 |
} |
| 306 |
} |
| 307 |
} |
| 308 |
eb_offset = this_off; |
| 309 |
} |
| 310 |
#endif |
| 311 |
return true; |
| 312 |
} |
| 313 |
|
| 314 |
IOSS_NODISCARD static int term_width(); |
| 315 |
|
| 316 |
IOSS_NODISCARD static int log_power_2(uint64_t value); |
| 317 |
|
| 318 |
/** \brief Get formatted time and date strings. |
| 319 |
* |
| 320 |
* Fill time_string and date_string with current time and date |
| 321 |
* formatted as "HH:MM:SS" for time and "yy/mm/dd" or "yyyy/mm/dd" |
| 322 |
* for date. |
| 323 |
* |
| 324 |
* \param[out] time_string The formatted time string. |
| 325 |
* \param[out] date_string The formatted date string. |
| 326 |
* \param[in] length Use 8 for short-year date format, or 10 for long-year date format. |
| 327 |
*/ |
| 328 |
static void time_and_date(char *time_string, char *date_string, size_t length); |
| 329 |
|
| 330 |
IOSS_NODISCARD static std::string decode_filename(const std::string &filename, int processor, |
| 331 |
int num_processors); |
| 332 |
IOSS_NODISCARD static int get_number(const std::string &suffix); |
| 333 |
IOSS_NODISCARD static int extract_id(const std::string &name_id); |
| 334 |
IOSS_NODISCARD static std::string encode_entity_name(const std::string &entity_type, |
| 335 |
int64_t id); |
| 336 |
|
| 337 |
/** Return the trailing digits (if any) from `name` |
| 338 |
* `hex20` would return the string `20` |
| 339 |
* `tetra` would return an empty string. |
| 340 |
*/ |
| 341 |
IOSS_NODISCARD static std::string get_trailing_digits(const std::string &name); |
| 342 |
|
| 343 |
/** \brief create a string that describes the list of input `ids` collapsing ranges if possible. |
| 344 |
* |
| 345 |
* Traverse the sorted input vector `ids` and return a string that has all sequential ranges |
| 346 |
* collapsed and separated by `rng_sep` and all individual ids or ranges separated by `seq_sep`. |
| 347 |
* Will throw an exception if `ids` is not sorted. An empty list returns an empty string. |
| 348 |
* The sequence of ids `1, 2, 3, 5, 6, 7` with `rng_sep=".."` will return the default |
| 349 |
* string `1..3, 5..8` |
| 350 |
*/ |
| 351 |
IOSS_NODISCARD static std::string format_id_list(const std::vector<size_t> &ids, |
| 352 |
const std::string &rng_sep = " to ", |
| 353 |
const std::string &seq_sep = ", "); |
| 354 |
|
| 355 |
/** \brief Convert a string to lower case, and convert spaces to `_`. |
| 356 |
* |
| 357 |
* The conversion is performed in place. |
| 358 |
* |
| 359 |
* \param[in,out] name On input, the string to convert. On output, the converted string. |
| 360 |
* |
| 361 |
*/ |
| 362 |
static void fixup_name(char *name); |
| 363 |
|
| 364 |
/** \brief Convert a string to lower case, and convert spaces to `_`. |
| 365 |
* |
| 366 |
* The conversion is performed in place. |
| 367 |
* |
| 368 |
* \param[in,out] name On input, the string to convert. On output, the converted string. |
| 369 |
* |
| 370 |
*/ |
| 371 |
static void fixup_name(std::string &name); |
| 372 |
|
| 373 |
/** \brief Check whether property `prop_name` exists and if so, set `prop_value` |
| 374 |
* |
| 375 |
* based on the property value. Either "TRUE", "YES", "ON", or nonzero for true; |
| 376 |
* or "FALSE", "NO", "OFF", or 0 for false. |
| 377 |
* \param[in] properties the Ioss::PropertyManager containing the properties to be checked. |
| 378 |
* \param[in] prop_name the name of the property to check whether it exists and if so, set its |
| 379 |
* value. |
| 380 |
* \param[out] prop_value if `prop_name` exists and has a valid value, set prop_value |
| 381 |
* accordingly. Does not modify if `prop_name` does not exist. \returns true/false depending on |
| 382 |
* whether property found and value set. |
| 383 |
*/ |
| 384 |
|
| 385 |
static bool check_set_bool_property(const Ioss::PropertyManager &properties, |
| 386 |
const std::string &prop_name, bool &prop_value); |
| 387 |
|
| 388 |
/** \brief Determine whether an entity has the property `omitted`. |
| 389 |
* |
| 390 |
* \param[in] block The entity. |
| 391 |
* \returns True if the entity has the property `omitted`. |
| 392 |
*/ |
| 393 |
IOSS_NODISCARD static bool block_is_omitted(Ioss::GroupingEntity *block); |
| 394 |
|
| 395 |
/** \brief Process the base element type `base` which has |
| 396 |
* `nodes_per_element` nodes and a spatial dimension of `spatial` |
| 397 |
* into a form that the IO system can (hopefully) recognize. |
| 398 |
* |
| 399 |
* Lowercases the name; converts spaces to `_`, adds |
| 400 |
* nodes_per_element at end of name (if not already there), and |
| 401 |
* does some other transformations to remove some exodusII ambiguity. |
| 402 |
* |
| 403 |
* \param[in] base The element base name. |
| 404 |
* \param[in] nodes_per_element The number of nodes per element. |
| 405 |
* \param[in] spatial The spatial dimension of the element. |
| 406 |
* \returns The Ioss-formatted element name. |
| 407 |
*/ |
| 408 |
IOSS_NODISCARD static std::string fixup_type(const std::string &base, int nodes_per_element, |
| 409 |
int spatial); |
| 410 |
|
| 411 |
/** \brief Uppercase the first letter of the string |
| 412 |
* |
| 413 |
* \param[in] name The string to convert. |
| 414 |
* \returns The converted string. |
| 415 |
*/ |
| 416 |
IOSS_NODISCARD static std::string capitalize(std::string name); |
| 417 |
|
| 418 |
/** \brief Convert a string to upper case. |
| 419 |
* |
| 420 |
* \param[in] name The string to convert. |
| 421 |
* \returns The converted string. |
| 422 |
*/ |
| 423 |
IOSS_NODISCARD static std::string uppercase(std::string name); |
| 424 |
|
| 425 |
/** \brief Convert a string to lower case. |
| 426 |
* |
| 427 |
* \param[in] name The string to convert. |
| 428 |
* \returns The converted string. |
| 429 |
*/ |
| 430 |
IOSS_NODISCARD static std::string lowercase(std::string name); |
| 431 |
|
| 432 |
static void check_non_null(void *ptr, const char *type, const std::string &name, |
| 433 |
const std::string &func); |
| 434 |
|
| 435 |
/** \brief Case-insensitive string comparison. |
| 436 |
* |
| 437 |
* \param[in] s1 First string |
| 438 |
* \param[in] s2 Second string |
| 439 |
* \returns `true` if strings are equal |
| 440 |
*/ |
| 441 |
IOSS_NODISCARD static bool str_equal(const std::string &s1, const std::string &s2); |
| 442 |
|
| 443 |
/** \brief Case-insensitive substring comparison. |
| 444 |
* |
| 445 |
* \param[in] prefix The prefix that should start the string |
| 446 |
* \param[in] str The string which should begin with prefix |
| 447 |
* \returns `true` if `str` begins with `prefix` or `prefix` is empty |
| 448 |
*/ |
| 449 |
IOSS_NODISCARD static bool substr_equal(const std::string &prefix, const std::string &str); |
| 450 |
|
| 451 |
/** Check all values in `data` to make sure that if they are converted to a double and |
| 452 |
* back again, there will be no data loss. This requires that the value be less than 2^53. |
| 453 |
* This is done in the exodus database since it stores all transient data as doubles... |
| 454 |
*/ |
| 455 |
static bool check_int_to_real_overflow(const Ioss::Field &field, int64_t *data, |
| 456 |
size_t num_entity); |
| 457 |
|
| 458 |
/** \brief Get a string containing `uname` output. |
| 459 |
* |
| 460 |
* This output contains information about the current computing platform. |
| 461 |
* This is used as information data in the created results file to help |
| 462 |
* in tracking when/where/... the file was created. |
| 463 |
* |
| 464 |
* \returns The platform information string. |
| 465 |
*/ |
| 466 |
IOSS_NODISCARD static std::string platform_information(); |
| 467 |
|
| 468 |
/** \brief Get a filename relative to the specified working directory (if any) |
| 469 |
* of the current execution. |
| 470 |
* |
| 471 |
* Working_directory must end with `/` or be empty. |
| 472 |
* |
| 473 |
* \param[in] relative_filename The file path to be appended to the working directory path. |
| 474 |
* \param[in] type The file type. "generated" file types are treated differently. |
| 475 |
* \param[in] working_directory the path to which the relative_filename path is appended. |
| 476 |
* \returns The full path (working_directory + relative_filename) |
| 477 |
*/ |
| 478 |
IOSS_NODISCARD static std::string local_filename(const std::string &relative_filename, |
| 479 |
const std::string &type, |
| 480 |
const std::string &working_directory); |
| 481 |
|
| 482 |
static void get_fields(int64_t entity_count, Ioss::NameList &names, |
| 483 |
Ioss::Field::RoleType fld_role, const DatabaseIO *db, int *local_truth, |
| 484 |
std::vector<Ioss::Field> &fields); |
| 485 |
|
| 486 |
static int field_warning(const Ioss::GroupingEntity *ge, const Ioss::Field &field, |
| 487 |
std::string_view inout); |
| 488 |
|
| 489 |
static void calculate_sideblock_membership(IntVector &face_is_member, const SideBlock *sb, |
| 490 |
size_t int_byte_size, const void *element, |
| 491 |
const void *sides, int64_t number_sides, |
| 492 |
const Region *region); |
| 493 |
|
| 494 |
/** \brief Get the appropriate index offset for the sides of elements in a SideBlock. |
| 495 |
* |
| 496 |
* And yet another idiosyncrasy of sidesets... |
| 497 |
* The side of an element (especially shells) can be |
| 498 |
* either a face or an edge in the same sideset. The |
| 499 |
* ordinal of an edge is (local_edge_number+numfaces) on the |
| 500 |
* database, but needs to be (local_edge_number) for Sierra... |
| 501 |
* |
| 502 |
* If the sideblock has a "parent_element_topology" and a |
| 503 |
* "topology", then we can determine whether to offset the |
| 504 |
* side ordinals... |
| 505 |
* |
| 506 |
* \param[in] sb Compute the offset for element sides in this SideBlock |
| 507 |
* \returns The offset. |
| 508 |
*/ |
| 509 |
IOSS_NODISCARD static int64_t get_side_offset(const Ioss::ElementTopology *parent_topo, |
| 510 |
const Ioss::ElementTopology *side_topo); |
| 511 |
|
| 512 |
IOSS_NODISCARD static int64_t get_side_offset(const Ioss::SideBlock *sb); |
| 513 |
|
| 514 |
IOSS_NODISCARD static unsigned int hash(const std::string &name); |
| 515 |
|
| 516 |
IOSS_NODISCARD static double timer(); |
| 517 |
|
| 518 |
/** \brief Convert an input file to a vector of strings containing one string for each line of |
| 519 |
* the file. |
| 520 |
* |
| 521 |
* Should only be called by a single processor or each processor will be accessing the file |
| 522 |
* at the same time... |
| 523 |
* |
| 524 |
* \param[in] file_name The name of the file. |
| 525 |
* \param[out] lines The vector of strings containing the lines of the file |
| 526 |
* \param[in] max_line_length The maximum number of characters in any line of the file. |
| 527 |
*/ |
| 528 |
static void input_file(const std::string &file_name, Ioss::NameList *lines, |
| 529 |
size_t max_line_length = 0); |
| 530 |
|
| 531 |
template <class T> IOSS_NODISCARD static std::string to_string(const T &t) |
| 532 |
{ |
| 533 |
return std::to_string(t); |
| 534 |
} |
| 535 |
|
| 536 |
//! \brief Tries to shorten long variable names to an acceptable |
| 537 |
//! length, and converts to lowercase and spaces to `_` |
| 538 |
//! |
| 539 |
//! Many databases have a maximum length for variable names which can |
| 540 |
//! cause a problem with variable name length. |
| 541 |
//! |
| 542 |
|
| 543 |
//! This routine tries to shorten long variable names to an |
| 544 |
//! acceptable length (`max_var_len` characters max). If the name |
| 545 |
//! is already less than this length, it is returned unchanged... |
| 546 |
//! |
| 547 |
//! Since there is a (good) chance that two shortened names will match, |
| 548 |
//! a 2-letter `hash` code is appended to the end of the variable name. |
| 549 |
//! |
| 550 |
//! So, we shorten the name to a maximum of `max_var_len`-3 |
| 551 |
//! characters and append a 2 character hash+separator. |
| 552 |
//! |
| 553 |
//! It also converts name to lowercase and converts spaces to `_` |
| 554 |
IOSS_NODISCARD static std::string variable_name_kluge(const std::string &name, |
| 555 |
size_t component_count, size_t copies, |
| 556 |
size_t max_var_len); |
| 557 |
|
| 558 |
IOSS_NODISCARD static std::string shape_to_string(const Ioss::ElementShape &shape); |
| 559 |
|
| 560 |
IOSS_NODISCARD static std::string entity_type_to_string(const Ioss::EntityType &type); |
| 561 |
|
| 562 |
/** \brief Create a nominal mesh for use in history databases. |
| 563 |
* |
| 564 |
* The model for a history file is a single sphere element (1 node, 1 element). |
| 565 |
* This is needed for some applications that read this file that require a |
| 566 |
* "mesh" even though a history file is just a collection of global variables |
| 567 |
* with no real mesh. This routine will add the mesh portion to a history file. |
| 568 |
* |
| 569 |
* \param[in,out] region The region on which the nominal mesh is to be defined. |
| 570 |
*/ |
| 571 |
static void generate_history_mesh(Ioss::Region *region); |
| 572 |
|
| 573 |
static void info_fields(const Ioss::GroupingEntity *ige, Ioss::Field::RoleType role, |
| 574 |
const std::string &header, const std::string &suffix = "\n\t", |
| 575 |
bool detail = false); |
| 576 |
|
| 577 |
static void info_property(const Ioss::GroupingEntity *ige, Ioss::Property::Origin origin, |
| 578 |
const std::string &header, const std::string &suffix = "\n\t", |
| 579 |
bool print_empty = false); |
| 580 |
|
| 581 |
static void insert_sort_and_unique(const Ioss::NameList &src, Ioss::NameList &dest) |
| 582 |
{ |
| 583 |
dest.insert(dest.end(), src.begin(), src.end()); |
| 584 |
std::sort(dest.begin(), dest.end(), std::less<>()); |
| 585 |
auto endIter = std::unique(dest.begin(), dest.end()); |
| 586 |
dest.resize(endIter - dest.begin()); |
| 587 |
} |
| 588 |
|
| 589 |
private: |
| 590 |
// SEE: http://lemire.me/blog/2017/04/10/removing-duplicates-from-lists-quickly |
| 591 |
template <typename T> static size_t unique(std::vector<T> &out, bool skip_first) |
| 592 |
{ |
| 593 |
if (out.empty()) { |
| 594 |
return 0; |
| 595 |
} |
| 596 |
size_t i = 1; |
| 597 |
size_t pos = 1; |
| 598 |
T oldv = out[0]; |
| 599 |
if (skip_first) { |
| 600 |
i = 2; |
| 601 |
pos = 2; |
| 602 |
oldv = out[1]; |
| 603 |
} |
| 604 |
for (; i < out.size(); ++i) { |
| 605 |
T newv = out[i]; |
| 606 |
out[pos] = newv; |
| 607 |
pos += (newv != oldv); |
| 608 |
oldv = newv; |
| 609 |
} |
| 610 |
return pos; |
| 611 |
} |
| 612 |
}; |
| 613 |
|
| 614 |
inline std::ostream &OUTPUT() { return Utils::get_output_stream(); } |
| 615 |
|
| 616 |
inline std::ostream &DebugOut() { return Utils::get_debug_stream(); } |
| 617 |
|
| 618 |
inline std::ostream &WarnOut(bool output_prewarning = true) |
| 619 |
{ |
| 620 |
if (output_prewarning) { |
| 621 |
Utils::get_warning_stream() << Utils::get_warning_text(); |
| 622 |
} |
| 623 |
return Utils::get_warning_stream(); |
| 624 |
} |
| 625 |
|
| 626 |
} // namespace Ioss |