| 1 |
// Copyright(C) 1999-2025 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 |
#include "Ioss_Field.h" |
| 8 |
#include "Ioss_Transform.h" |
| 9 |
#include "Ioss_Utils.h" |
| 10 |
#include "Ioss_VariableType.h" |
| 11 |
#include <cstddef> |
| 12 |
#include <cstdint> |
| 13 |
#include <fmt/format.h> |
| 14 |
#include <fmt/ostream.h> |
| 15 |
#include <fmt/ranges.h> |
| 16 |
#include <iostream> |
| 17 |
#include <string> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "Ioss_CodeTypes.h" |
| 21 |
|
| 22 |
namespace { |
| 23 |
size_t internal_get_size(Ioss::Field::BasicType type, size_t count, |
| 24 |
const Ioss::VariableType *storage); |
| 25 |
size_t internal_get_basic_size(Ioss::Field::BasicType type); |
| 26 |
|
| 27 |
void error_message(const Ioss::Field &field, Ioss::Field::BasicType requested_type) |
| 28 |
{ |
| 29 |
std::ostringstream errmsg; |
| 30 |
fmt::print(errmsg, |
| 31 |
"ERROR: For field named '{}', code requested value of type '{}', but field type is " |
| 32 |
"'{}'. Types must match\n", |
| 33 |
field.get_name(), Ioss::Field::type_string(requested_type), |
| 34 |
Ioss::Field::type_string(field.get_type())); |
| 35 |
IOSS_ERROR(errmsg); |
| 36 |
} |
| 37 |
|
| 38 |
} // namespace |
| 39 |
|
| 40 |
namespace Ioss { |
| 41 |
std::ostream &operator<<(std::ostream &os, const Field &fld) |
| 42 |
{ |
| 43 |
Ioss::NameList components(fld.get_component_count(Field::InOut::INPUT)); |
| 44 |
for (size_t i = 0; i < components.size(); i++) { |
| 45 |
components[i] = fld.get_component_name(i + 1, Field::InOut::INPUT, 1); |
| 46 |
} |
| 47 |
auto storage = fld.raw_storage()->name(); |
| 48 |
if (storage == "scalar") { |
| 49 |
fmt::print(os, "\tField: {}, Storage: {}\t{}\t{}\n", fld.get_name(), |
| 50 |
fld.raw_storage()->name(), fld.type_string(), fld.role_string()); |
| 51 |
} |
| 52 |
else { |
| 53 |
fmt::print(os, |
| 54 |
"\tField: {}, Storage: {} ({}),\t{},\t{}, Sep1: '{}', Sep2: '{}'\n" |
| 55 |
"\t\t\tComponents ({}): {}\n", |
| 56 |
fld.get_name(), fld.raw_storage()->name(), fld.raw_storage()->type_string(), |
| 57 |
fld.type_string(), fld.role_string(), fld.get_suffix_separator(0), |
| 58 |
fld.get_suffix_separator(1), fld.get_component_count(Field::InOut::INPUT), |
| 59 |
fmt::join(components, ", ")); |
| 60 |
} |
| 61 |
return os; |
| 62 |
} |
| 63 |
} // namespace Ioss |
| 64 |
|
| 65 |
/** \brief Create an empty field. |
| 66 |
*/ |
| 67 |
Ioss::Field::Field() { rawStorage_ = transStorage_ = Ioss::VariableType::factory("invalid"); } |
| 68 |
|
| 69 |
/** \brief Create a field. |
| 70 |
* |
| 71 |
* \param[in] name The name of the field |
| 72 |
* \param[in] type The basic data type of data held in the field. |
| 73 |
* \param[in] storage The storage class of the data (ConstructedVariableType, |
| 74 |
* CompositeVariableType, etc) |
| 75 |
* \param[in] role The category of information held in the field (MESH, ATTRIBUTE, TRANSIENT, |
| 76 |
* REDUCTION, etc) |
| 77 |
* \param[in] value_count The number of items in the field. |
| 78 |
* \param[in] index |
| 79 |
* |
| 80 |
*/ |
| 81 |
Ioss::Field::Field(std::string name, const Ioss::Field::BasicType type, const std::string &storage, |
| 82 |
const Ioss::Field::RoleType role, size_t value_count, size_t index) |
| 83 |
: name_(std::move(name)), rawCount_(value_count), transCount_(value_count), index_(index), |
| 84 |
type_(type), role_(role) |
| 85 |
{ |
| 86 |
rawStorage_ = transStorage_ = Ioss::VariableType::factory(storage); |
| 87 |
size_ = internal_get_size(type_, rawCount_, rawStorage_); |
| 88 |
} |
| 89 |
|
| 90 |
/** \brief Create a field. |
| 91 |
* |
| 92 |
* \param[in] name The name of the field |
| 93 |
* \param[in] type The basic data type of data held in the field. |
| 94 |
* \param[in] storage The storage class of the data (ConstructedVariableType, |
| 95 |
* CompositeVariableType, etc) |
| 96 |
* \param[in] copies The number of variables to be combined in a CompositeVariableType field. |
| 97 |
* \param[in] role The category of information held in the field (MESH, ATTRIBUTE, TRANSIENT, |
| 98 |
* REDUCTION, etc) |
| 99 |
* \param[in] value_count The number of items in the field. |
| 100 |
* \param[in] index |
| 101 |
* |
| 102 |
*/ |
| 103 |
Ioss::Field::Field(std::string name, const Ioss::Field::BasicType type, const std::string &storage, |
| 104 |
int copies, const Ioss::Field::RoleType role, size_t value_count, size_t index) |
| 105 |
: name_(std::move(name)), rawCount_(value_count), transCount_(value_count), index_(index), |
| 106 |
type_(type), role_(role) |
| 107 |
{ |
| 108 |
rawStorage_ = transStorage_ = Ioss::VariableType::factory(storage, copies); |
| 109 |
size_ = internal_get_size(type_, rawCount_, rawStorage_); |
| 110 |
} |
| 111 |
|
| 112 |
/** \brief Create a field. |
| 113 |
* |
| 114 |
* \param[in] name The name of the field |
| 115 |
* \param[in] type The basic data type of data held in the field. |
| 116 |
* \param[in] storage The storage class of the data (ConstructedVariableType, |
| 117 |
* CompositeVariableType, etc) |
| 118 |
* \param[in] role The category of information held in the field (MESH, ATTRIBUTE, TRANSIENT, |
| 119 |
* REDUCTION, etc) |
| 120 |
* \param[in] value_count The number of items in the field. |
| 121 |
* \param[in] index |
| 122 |
* |
| 123 |
*/ |
| 124 |
Ioss::Field::Field(std::string name, const Ioss::Field::BasicType type, |
| 125 |
const Ioss::VariableType *storage, const Ioss::Field::RoleType role, |
| 126 |
size_t value_count, size_t index) |
| 127 |
: name_(std::move(name)), rawCount_(value_count), transCount_(value_count), index_(index), |
| 128 |
type_(type), role_(role), rawStorage_(storage), transStorage_(storage) |
| 129 |
{ |
| 130 |
size_ = internal_get_size(type_, rawCount_, rawStorage_); |
| 131 |
} |
| 132 |
|
| 133 |
/** \brief Create a field. |
| 134 |
* |
| 135 |
* \param[in] name The name of the field |
| 136 |
* \param[in] type The basic data type of data held in the field. |
| 137 |
* \param[in] storage The storage class of the data (ConstructedVariableType, |
| 138 |
* CompositeVariableType, etc) |
| 139 |
* \param[in] secondary The secondary storage class of the data (typically "basis") [For a |
| 140 |
* ComposedVariableType field] \param[in] role The category of information held in the field (MESH, |
| 141 |
* ATTRIBUTE, TRANSIENT, REDUCTION, etc) \param[in] value_count The number of items in the field. |
| 142 |
* \param[in] index |
| 143 |
* |
| 144 |
*/ |
| 145 |
Ioss::Field::Field(std::string name, BasicType type, const std::string &storage, |
| 146 |
const std::string &secondary, RoleType role, size_t value_count, size_t index) |
| 147 |
: name_(std::move(name)), rawCount_(value_count), transCount_(value_count), index_(index), |
| 148 |
type_(type), role_(role) |
| 149 |
{ |
| 150 |
rawStorage_ = transStorage_ = Ioss::VariableType::factory(storage, secondary); |
| 151 |
size_ = internal_get_size(type_, rawCount_, rawStorage_); |
| 152 |
} |
| 153 |
|
| 154 |
int Ioss::Field::get_component_count(Ioss::Field::InOut in_out) const |
| 155 |
{ |
| 156 |
const auto *storage = (in_out == InOut::INPUT) ? raw_storage() : transformed_storage(); |
| 157 |
return storage->component_count(); |
| 158 |
} |
| 159 |
|
| 160 |
std::string Ioss::Field::get_component_name(int component_index, InOut in_out, char suffix) const |
| 161 |
{ |
| 162 |
char suffix_separator0 = get_suffix_separator(0); |
| 163 |
if (suffix_separator0 == 1) { |
| 164 |
suffix_separator0 = suffix != 1 ? suffix : '_'; |
| 165 |
} |
| 166 |
char suffix_separator1 = get_suffix_separator(1); |
| 167 |
if (suffix_separator1 == 1) { |
| 168 |
suffix_separator1 = suffix != 1 ? suffix : '_'; |
| 169 |
} |
| 170 |
const auto *storage = (in_out == InOut::INPUT) ? raw_storage() : transformed_storage(); |
| 171 |
return storage->label_name(get_name(), component_index, suffix_separator0, suffix_separator1, |
| 172 |
get_suffices_uppercase()); |
| 173 |
} |
| 174 |
|
| 175 |
/* \brief Verify that data_size is valid. |
| 176 |
* |
| 177 |
* If return value >= 0, then it is the maximum number of |
| 178 |
* entities to get 'count' |
| 179 |
* Throws runtime error if data_size too small. |
| 180 |
* |
| 181 |
* \param[in] data_size The data size to test |
| 182 |
* \returns The maximum number of entities to get 'count' |
| 183 |
* |
| 184 |
*/ |
| 185 |
size_t Ioss::Field::verify(size_t data_size) const |
| 186 |
{ |
| 187 |
if (data_size > 0) { |
| 188 |
// Check sufficient storage |
| 189 |
size_t required = get_size(); |
| 190 |
if (required > data_size) { |
| 191 |
std::ostringstream errmsg; |
| 192 |
fmt::print(errmsg, |
| 193 |
"Field {} requires {} bytes to store its data. Only {} bytes were provided.\n", |
| 194 |
name_, required, data_size); |
| 195 |
IOSS_ERROR(errmsg); |
| 196 |
} |
| 197 |
} |
| 198 |
return rawCount_; |
| 199 |
} |
| 200 |
|
| 201 |
void Ioss::Field::check_type(BasicType the_type) const |
| 202 |
{ |
| 203 |
if (type_ != the_type) { |
| 204 |
if ((the_type == Ioss::Field::INTEGER && type_ == Ioss::Field::REAL) || |
| 205 |
(the_type == Ioss::Field::INT64 && type_ == Ioss::Field::REAL)) { |
| 206 |
// If Ioss created the field by reading the database, it may |
| 207 |
// think the field is a real but it is really an integer. Make |
| 208 |
// sure that the field type is correct here... |
| 209 |
auto *new_this = const_cast<Ioss::Field *>(this); |
| 210 |
new_this->reset_type(the_type); |
| 211 |
} |
| 212 |
else { |
| 213 |
error_message(*this, the_type); |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
const Ioss::Field &Ioss::Field::set_zero_copy_enabled(bool true_false) const |
| 219 |
{ |
| 220 |
if (has_transform()) { |
| 221 |
std::ostringstream errmsg; |
| 222 |
fmt::print(errmsg, |
| 223 |
"Field {} is being set to `zero_copy_enabled`; however, it contains 1 or more " |
| 224 |
"transforms which is not allowed.\n", |
| 225 |
name_); |
| 226 |
IOSS_ERROR(errmsg); |
| 227 |
} |
| 228 |
zeroCopyable_ = true_false; |
| 229 |
return *this; |
| 230 |
} |
| 231 |
|
| 232 |
void Ioss::Field::reset_count(size_t new_count) |
| 233 |
{ |
| 234 |
if (transCount_ == rawCount_) { |
| 235 |
transCount_ = new_count; |
| 236 |
} |
| 237 |
rawCount_ = new_count; |
| 238 |
size_ = 0; |
| 239 |
} |
| 240 |
|
| 241 |
void Ioss::Field::reset_type(Ioss::Field::BasicType new_type) |
| 242 |
{ |
| 243 |
type_ = new_type; |
| 244 |
size_ = 0; |
| 245 |
} |
| 246 |
|
| 247 |
// Return number of bytes required to store entire field |
| 248 |
size_t Ioss::Field::get_size() const |
| 249 |
{ |
| 250 |
if (size_ == 0) { |
| 251 |
auto *new_this = const_cast<Ioss::Field *>(this); |
| 252 |
new_this->size_ = internal_get_size(type_, rawCount_, rawStorage_); |
| 253 |
|
| 254 |
new_this->transCount_ = rawCount_; |
| 255 |
new_this->transStorage_ = rawStorage_; |
| 256 |
for (const auto &my_transform : transforms_) { |
| 257 |
new_this->transCount_ = my_transform->output_count(transCount_); |
| 258 |
new_this->transStorage_ = my_transform->output_storage(transStorage_); |
| 259 |
size_t size = internal_get_size(type_, transCount_, transStorage_); |
| 260 |
if (size > size_) { |
| 261 |
new_this->size_ = size; |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
return size_; |
| 266 |
} |
| 267 |
|
| 268 |
size_t Ioss::Field::get_basic_size() const |
| 269 |
{ |
| 270 |
// Calculate size of the low-level data type |
| 271 |
return internal_get_basic_size(type_); |
| 272 |
} |
| 273 |
|
| 274 |
bool Ioss::Field::add_transform(Transform *my_transform) |
| 275 |
{ |
| 276 |
if (zero_copy_enabled()) { |
| 277 |
std::ostringstream errmsg; |
| 278 |
fmt::print(errmsg, |
| 279 |
"Field {} is currently set to `zero_copy_enabled` which does not support adding a " |
| 280 |
"transform. The transform has *not* been added to this field.\n", |
| 281 |
name_); |
| 282 |
IOSS_ERROR(errmsg); |
| 283 |
} |
| 284 |
|
| 285 |
const Ioss::VariableType *new_storage = my_transform->output_storage(transStorage_); |
| 286 |
size_t new_count = my_transform->output_count(transCount_); |
| 287 |
|
| 288 |
if (new_storage != nullptr && new_count > 0) { |
| 289 |
transStorage_ = new_storage; |
| 290 |
transCount_ = new_count; |
| 291 |
} |
| 292 |
else { |
| 293 |
return false; |
| 294 |
} |
| 295 |
|
| 296 |
if (transCount_ < rawCount_) { |
| 297 |
role_ = REDUCTION; |
| 298 |
} |
| 299 |
|
| 300 |
size_t size = internal_get_size(type_, transCount_, transStorage_); |
| 301 |
if (size > size_) { |
| 302 |
size_ = size; |
| 303 |
} |
| 304 |
|
| 305 |
transforms_.push_back(my_transform); |
| 306 |
return true; |
| 307 |
} |
| 308 |
|
| 309 |
bool Ioss::Field::transform(void *data) |
| 310 |
{ |
| 311 |
transStorage_ = rawStorage_; |
| 312 |
transCount_ = rawCount_; |
| 313 |
|
| 314 |
for (auto &my_transform : transforms_) { |
| 315 |
my_transform->execute(*this, data); |
| 316 |
|
| 317 |
transStorage_ = my_transform->output_storage(transStorage_); |
| 318 |
transCount_ = my_transform->output_count(transCount_); |
| 319 |
} |
| 320 |
return true; |
| 321 |
} |
| 322 |
|
| 323 |
bool Ioss::Field::equal_(const Ioss::Field &rhs, bool quiet) const |
| 324 |
{ |
| 325 |
bool is_same = true; |
| 326 |
if (!Ioss::Utils::str_equal(this->name_, rhs.name_)) { |
| 327 |
if (!quiet) { |
| 328 |
fmt::print(Ioss::OUTPUT(), "\tFIELD name mismatch ({} v. {})\n", this->name_, rhs.name_); |
| 329 |
} |
| 330 |
is_same = false; |
| 331 |
} |
| 332 |
|
| 333 |
if (this->type_ != rhs.type_) { |
| 334 |
if (!quiet) { |
| 335 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} type mismatch ({} v. {})\n", this->name_, |
| 336 |
this->type_string(), rhs.type_string()); |
| 337 |
} |
| 338 |
is_same = false; |
| 339 |
} |
| 340 |
|
| 341 |
if (this->role_ != rhs.role_) { |
| 342 |
if (!quiet) { |
| 343 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} role mismatch ({} v. {})\n", this->name_, |
| 344 |
this->role_string(), rhs.role_string()); |
| 345 |
} |
| 346 |
is_same = false; |
| 347 |
} |
| 348 |
|
| 349 |
if (this->rawCount_ != rhs.rawCount_) { |
| 350 |
if (!quiet) { |
| 351 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} rawCount mismatch ({} v. {})\n", this->name_, |
| 352 |
this->rawCount_, rhs.rawCount_); |
| 353 |
} |
| 354 |
is_same = false; |
| 355 |
} |
| 356 |
|
| 357 |
if (this->transCount_ != rhs.transCount_) { |
| 358 |
if (!quiet) { |
| 359 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} transCount mismatch ({} v. {})\n", this->name_, |
| 360 |
this->transCount_, rhs.transCount_); |
| 361 |
} |
| 362 |
is_same = false; |
| 363 |
} |
| 364 |
|
| 365 |
if (this->get_size() != rhs.get_size()) { |
| 366 |
if (!quiet) { |
| 367 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} size mismatch ({} v. {})\n", this->name_, |
| 368 |
this->get_size(), rhs.get_size()); |
| 369 |
} |
| 370 |
is_same = false; |
| 371 |
} |
| 372 |
|
| 373 |
if (!quiet) { |
| 374 |
if (this->get_suffices_uppercase() != rhs.get_suffices_uppercase()) { |
| 375 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} suffices_uppercase mismatch ({} v. {})\n", this->name_, |
| 376 |
this->get_suffices_uppercase(), rhs.get_suffices_uppercase()); |
| 377 |
is_same = false; |
| 378 |
} |
| 379 |
} |
| 380 |
|
| 381 |
if (!quiet) { |
| 382 |
if (this->zero_copy_enabled() != rhs.zero_copy_enabled()) { |
| 383 |
fmt::print(Ioss::OUTPUT(), "\tFIELD {} zero_copy_enabled mismatch ({} v. {})\n", this->name_, |
| 384 |
this->zero_copy_enabled(), rhs.zero_copy_enabled()); |
| 385 |
is_same = false; |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
return is_same; |
| 390 |
} |
| 391 |
|
| 392 |
bool Ioss::Field::operator==(const Ioss::Field &rhs) const { return equal_(rhs, true); } |
| 393 |
|
| 394 |
bool Ioss::Field::operator!=(const Ioss::Field &rhs) const { return !(*this == rhs); } |
| 395 |
|
| 396 |
bool Ioss::Field::equal(const Ioss::Field &rhs) const { return equal_(rhs, false); } |
| 397 |
|
| 398 |
std::string Ioss::Field::type_string() const { return type_string(get_type()); } |
| 399 |
|
| 400 |
std::string Ioss::Field::type_string(Ioss::Field::BasicType type) |
| 401 |
{ |
| 402 |
switch (type) { |
| 403 |
case Ioss::Field::REAL: return {"real"}; |
| 404 |
case Ioss::Field::INTEGER: return {"integer"}; |
| 405 |
case Ioss::Field::INT64: return {"64-bit integer"}; |
| 406 |
case Ioss::Field::COMPLEX: return {"complex"}; |
| 407 |
case Ioss::Field::STRING: return {"string"}; |
| 408 |
case Ioss::Field::CHARACTER: return {"char"}; |
| 409 |
case Ioss::Field::INVALID: return {"invalid"}; |
| 410 |
} |
| 411 |
return {"internal error"}; |
| 412 |
} |
| 413 |
|
| 414 |
std::string Ioss::Field::role_string() const { return role_string(get_role()); } |
| 415 |
|
| 416 |
std::string Ioss::Field::role_string(Ioss::Field::RoleType role) |
| 417 |
{ |
| 418 |
switch (role) { |
| 419 |
case Ioss::Field::INTERNAL: return {"Internal"}; |
| 420 |
case Ioss::Field::MAP: return {"Map"}; |
| 421 |
case Ioss::Field::MESH: return {"Mesh"}; |
| 422 |
case Ioss::Field::ATTRIBUTE: return {"Attribute"}; |
| 423 |
case Ioss::Field::COMMUNICATION: return {"Communication"}; |
| 424 |
case Ioss::Field::MESH_REDUCTION: return {"Mesh Reduction"}; |
| 425 |
case Ioss::Field::REDUCTION: return {"Reduction"}; |
| 426 |
case Ioss::Field::TRANSIENT: return {"Transient"}; |
| 427 |
} |
| 428 |
return {"internal error"}; |
| 429 |
} |
| 430 |
|
| 431 |
namespace { |
| 432 |
size_t internal_get_basic_size(Ioss::Field::BasicType type) |
| 433 |
{ |
| 434 |
// Calculate size of the low-level data type |
| 435 |
size_t basic_size = 0; |
| 436 |
switch (type) { |
| 437 |
case Ioss::Field::REAL: basic_size = sizeof(double); break; |
| 438 |
case Ioss::Field::INTEGER: basic_size = sizeof(int); break; |
| 439 |
case Ioss::Field::INT64: basic_size = sizeof(int64_t); break; |
| 440 |
case Ioss::Field::COMPLEX: basic_size = sizeof(Complex); break; |
| 441 |
case Ioss::Field::STRING: basic_size = sizeof(std::string *); break; |
| 442 |
case Ioss::Field::CHARACTER: basic_size = sizeof(char); break; |
| 443 |
case Ioss::Field::INVALID: basic_size = 0; break; |
| 444 |
} |
| 445 |
|
| 446 |
return basic_size; |
| 447 |
} |
| 448 |
|
| 449 |
size_t internal_get_size(Ioss::Field::BasicType type, size_t count, |
| 450 |
const Ioss::VariableType *storage) |
| 451 |
{ |
| 452 |
// Calculate size of the low-level data type |
| 453 |
size_t basic_size = internal_get_basic_size(type); |
| 454 |
|
| 455 |
// Calculate size of the storage type |
| 456 |
size_t storage_size = storage->component_count(); |
| 457 |
|
| 458 |
return basic_size * storage_size * count; |
| 459 |
} |
| 460 |
} // namespace |