| 1 |
// Copyright(C) 1999-2020, 2023, 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 |
#include "Ioss_Field.h" |
| 8 |
#include "Ioss_FieldManager.h" |
| 9 |
#include "Ioss_GroupingEntity.h" |
| 10 |
#include "Ioss_Property.h" |
| 11 |
#include "Ioss_PropertyManager.h" |
| 12 |
#include "Ioss_Utils.h" |
| 13 |
#include "exodus/Ioex_SuperElement.h" // for SuperElement |
| 14 |
#include <cassert> |
| 15 |
#include <cstddef> |
| 16 |
#include <fmt/ostream.h> |
| 17 |
#include <iosfwd> |
| 18 |
#include <netcdf.h> |
| 19 |
#include <string> |
| 20 |
|
| 21 |
#include "Ioss_CodeTypes.h" |
| 22 |
|
| 23 |
namespace { |
| 24 |
int nc_get_array(int ncid, const char *name, double *data) |
| 25 |
{ |
| 26 |
// NOTE: size of data array is validated in |
| 27 |
// calling code. Just read and store here. |
| 28 |
|
| 29 |
int varid = 0; |
| 30 |
int status = nc_inq_varid(ncid, name, &varid); |
| 31 |
if (status != NC_NOERR) { |
| 32 |
return status; |
| 33 |
} |
| 34 |
|
| 35 |
status = nc_get_var_double(ncid, varid, data); |
| 36 |
return status; |
| 37 |
} |
| 38 |
|
| 39 |
int nc_get_dimension(int ncid, const char *DIMENSION, const char *label, size_t *count) |
| 40 |
{ |
| 41 |
*count = 0; |
| 42 |
int dimid = -1; |
| 43 |
|
| 44 |
int status = nc_inq_dimid(ncid, DIMENSION, &dimid); |
| 45 |
if (status != NC_NOERR) { |
| 46 |
if (status == NC_EBADDIM) { |
| 47 |
// Value is zero if the dimension is not defined. |
| 48 |
*count = 0; |
| 49 |
return 0; |
| 50 |
} |
| 51 |
IOSS_ERROR(fmt::format("ERROR: Failed to locate number of {} in superelement file.", label)); |
| 52 |
} |
| 53 |
|
| 54 |
status = nc_inq_dimlen(ncid, dimid, count); |
| 55 |
if (status != NC_NOERR) { |
| 56 |
IOSS_ERROR(fmt::format("ERROR: Failed to get number of {} in superelement file.", label)); |
| 57 |
} |
| 58 |
return status; |
| 59 |
} |
| 60 |
} // namespace |
| 61 |
|
| 62 |
Ioex::SuperElement::SuperElement(std::string filename, const std::string &my_name) |
| 63 |
: Ioss::GroupingEntity(nullptr, my_name, 1), fileName(std::move(filename)) |
| 64 |
{ |
| 65 |
|
| 66 |
// For now, we will open the raw netcdf file here and parse the |
| 67 |
// dimensions. This is probably not how this should be done long |
| 68 |
// term, but is better than putting netcdf calls in application... |
| 69 |
|
| 70 |
// Check that file specified by filename exists... |
| 71 |
// Add working directory if needed. |
| 72 |
std::string local_filename = fileName; |
| 73 |
|
| 74 |
int status = nc_open(local_filename.c_str(), NC_NOWRITE, &filePtr); |
| 75 |
if (status != NC_NOERR) { |
| 76 |
IOSS_ERROR(fmt::format("ERROR: Failed to open superelement file '{}'.", local_filename)); |
| 77 |
} |
| 78 |
|
| 79 |
// At this point have a valid netcdf file handle. |
| 80 |
// Read some dimensions to determine size of Mass and Stiffness |
| 81 |
// matrix. |
| 82 |
nc_get_dimension(filePtr, "NumDof", "number of degrees of freedom", &numDOF); |
| 83 |
|
| 84 |
nc_get_dimension(filePtr, "num_nodes", "number of nodes", &num_nodes); |
| 85 |
|
| 86 |
nc_get_dimension(filePtr, "NumEig", "number of eigenvalues", &numEIG); |
| 87 |
|
| 88 |
nc_get_dimension(filePtr, "NumRbm", "number of rigid body modes", &numRBM); |
| 89 |
|
| 90 |
nc_get_dimension(filePtr, "num_dim", "number of dimensions", &num_dim); |
| 91 |
|
| 92 |
size_t num_constraints = 0; |
| 93 |
nc_get_dimension(filePtr, "NumConstraints", "number of interface dof", &num_constraints); |
| 94 |
assert(num_constraints == numDOF - numEIG); |
| 95 |
|
| 96 |
// Add the standard properties... |
| 97 |
properties.add(Ioss::Property(this, "numDOF", Ioss::Property::INTEGER)); |
| 98 |
if (num_nodes > 0) { |
| 99 |
properties.add(Ioss::Property(this, "num_nodes", Ioss::Property::INTEGER)); |
| 100 |
} |
| 101 |
properties.add(Ioss::Property(this, "numEIG", Ioss::Property::INTEGER)); |
| 102 |
|
| 103 |
properties.add(Ioss::Property(this, "numRBM", Ioss::Property::INTEGER)); |
| 104 |
|
| 105 |
properties.add(Ioss::Property(this, "numDIM", Ioss::Property::INTEGER)); |
| 106 |
|
| 107 |
properties.add(Ioss::Property(this, "numConstraints", Ioss::Property::INTEGER)); |
| 108 |
|
| 109 |
// Add the standard fields... |
| 110 |
if (num_nodes > 0) { |
| 111 |
fields.add( |
| 112 |
Ioss::Field("coordx", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, num_nodes)); |
| 113 |
fields.add( |
| 114 |
Ioss::Field("coordy", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, num_nodes)); |
| 115 |
fields.add( |
| 116 |
Ioss::Field("coordz", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, num_nodes)); |
| 117 |
fields.add(Ioss::Field("node_num_map", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, |
| 118 |
num_nodes)); |
| 119 |
fields.add(Ioss::Field("cbmap", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, |
| 120 |
2 * num_nodes * num_dim)); |
| 121 |
} |
| 122 |
|
| 123 |
fields.add( |
| 124 |
Ioss::Field("Kr", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, numDOF * numDOF)); |
| 125 |
|
| 126 |
fields.add( |
| 127 |
Ioss::Field("Mr", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, numDOF * numDOF)); |
| 128 |
|
| 129 |
if (numRBM > 0) { |
| 130 |
fields.add(Ioss::Field("InertiaTensor", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, |
| 131 |
numDOF * numRBM)); |
| 132 |
fields.add(Ioss::Field("MassInertia", Ioss::Field::REAL, IOSS_SCALAR(), Ioss::Field::MESH, |
| 133 |
numDOF * numRBM)); |
| 134 |
} |
| 135 |
|
| 136 |
// There are additional properties and fields on the netcdf file, |
| 137 |
// but for now we only need "Kr", "Mr", and "InertiaTensor" |
| 138 |
} |
| 139 |
|
| 140 |
int64_t Ioex::SuperElement::internal_get_field_data(const Ioss::Field &field, void *data, |
| 141 |
size_t data_size) const |
| 142 |
{ |
| 143 |
size_t num_to_get = field.verify(data_size); |
| 144 |
|
| 145 |
if (field.get_name() == "cbmap") { |
| 146 |
assert(num_to_get == 2 * num_nodes * num_dim); |
| 147 |
int status = nc_get_array(filePtr, "cbmap", reinterpret_cast<double *>(data)); |
| 148 |
if (status != 0) { |
| 149 |
IOSS_ERROR(fmt::format("ERROR: Could not load coordinate data field 'cbmap' from file '{}'.", |
| 150 |
fileName)); |
| 151 |
} |
| 152 |
} |
| 153 |
else if (field.get_name() == "node_num_map") { |
| 154 |
assert(num_to_get == num_nodes); |
| 155 |
int status = nc_get_array(filePtr, "node_num_map", reinterpret_cast<double *>(data)); |
| 156 |
|
| 157 |
if (status != 0) { |
| 158 |
IOSS_ERROR(fmt::format( |
| 159 |
"ERROR: Could not load coordinate data field 'node_num_map' from file '{}'.", fileName)); |
| 160 |
} |
| 161 |
} |
| 162 |
else if (field.get_name() == "coordx") { |
| 163 |
assert(num_to_get == num_nodes); |
| 164 |
int status = nc_get_array(filePtr, "coordx", reinterpret_cast<double *>(data)); |
| 165 |
if (status != 0) { |
| 166 |
IOSS_ERROR(fmt::format("ERROR: Could not load coordinate data field 'coordx' from file '{}'.", |
| 167 |
fileName)); |
| 168 |
} |
| 169 |
} |
| 170 |
else if (field.get_name() == "coordy") { |
| 171 |
assert(num_to_get == num_nodes); |
| 172 |
int status = nc_get_array(filePtr, "coordy", reinterpret_cast<double *>(data)); |
| 173 |
if (status != 0) { |
| 174 |
IOSS_ERROR(fmt::format("ERROR: Could not load coordinate data field 'coordy' from file '{}'.", |
| 175 |
fileName)); |
| 176 |
} |
| 177 |
} |
| 178 |
else if (field.get_name() == "coordz") { |
| 179 |
assert(num_to_get == num_nodes); |
| 180 |
int status = nc_get_array(filePtr, "coordz", reinterpret_cast<double *>(data)); |
| 181 |
if (status != 0) { |
| 182 |
IOSS_ERROR(fmt::format("ERROR: Could not load coordinate data field 'coordz' from file '{}'.", |
| 183 |
fileName)); |
| 184 |
} |
| 185 |
} |
| 186 |
else if (field.get_name() == "Kr") { |
| 187 |
assert(num_to_get == numDOF * numDOF); |
| 188 |
int status = nc_get_array(filePtr, "Kr", reinterpret_cast<double *>(data)); |
| 189 |
if (status != 0) { |
| 190 |
IOSS_ERROR(fmt::format("ERROR: Could not load stiffness matrix field 'Kr' from file '{}'.", |
| 191 |
fileName)); |
| 192 |
} |
| 193 |
} |
| 194 |
else if (field.get_name() == "Mr") { |
| 195 |
assert(num_to_get == numDOF * numDOF); |
| 196 |
int status = nc_get_array(filePtr, "Mr", reinterpret_cast<double *>(data)); |
| 197 |
if (status != 0) { |
| 198 |
IOSS_ERROR( |
| 199 |
fmt::format("ERROR: Could not load mass matrix field 'Mr' from file '{}'.", fileName)); |
| 200 |
} |
| 201 |
} |
| 202 |
else if (field.get_name() == "InertiaTensor") { |
| 203 |
assert(num_to_get == numDOF * numRBM); |
| 204 |
int status = nc_get_array(filePtr, "InertiaTensor", reinterpret_cast<double *>(data)); |
| 205 |
if (status != 0) { |
| 206 |
IOSS_ERROR(fmt::format( |
| 207 |
"ERROR: Could not load inertia matrix field 'InertialTensor' from file '{}'.", fileName)); |
| 208 |
} |
| 209 |
} |
| 210 |
else if (field.get_name() == "MassInertia") { |
| 211 |
assert(num_to_get == numDOF * numRBM); |
| 212 |
int status = nc_get_array(filePtr, "MassInertia", reinterpret_cast<double *>(data)); |
| 213 |
if (status != 0) { |
| 214 |
IOSS_ERROR(fmt::format( |
| 215 |
"ERROR: Could not mass inertia matrix field 'MassInertia' from file '{}'.", fileName)); |
| 216 |
} |
| 217 |
} |
| 218 |
else { |
| 219 |
fmt::print(Ioss::WarnOut(), "{} '{}'. Unknown input field '{}'", static_cast<int>(type()), |
| 220 |
name(), field.get_name()); |
| 221 |
return -4; |
| 222 |
} |
| 223 |
return num_to_get; |
| 224 |
} |
| 225 |
|
| 226 |
Ioex::SuperElement::~SuperElement() |
| 227 |
{ |
| 228 |
if (filePtr != 0) { |
| 229 |
nc_close(filePtr); |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
int64_t Ioex::SuperElement::internal_put_field_data(const Ioss::Field & /* field */, |
| 234 |
void * /* data */, size_t /* data_size */) const |
| 235 |
{ |
| 236 |
return -1; |
| 237 |
} |
| 238 |
|
| 239 |
Ioss::Property Ioex::SuperElement::get_implicit_property(const std::string &the_name) const |
| 240 |
{ |
| 241 |
if (Ioss::Utils::str_equal(the_name, "numDOF")) { |
| 242 |
return {the_name, static_cast<int>(numDOF)}; |
| 243 |
} |
| 244 |
if (Ioss::Utils::str_equal(the_name, "num_nodes")) { |
| 245 |
return {the_name, static_cast<int>(num_nodes)}; |
| 246 |
} |
| 247 |
if (Ioss::Utils::str_equal(the_name, "numEIG")) { |
| 248 |
return {the_name, static_cast<int>(numEIG)}; |
| 249 |
} |
| 250 |
if (Ioss::Utils::str_equal(the_name, "num_dim")) { |
| 251 |
return {the_name, static_cast<int>(num_dim)}; |
| 252 |
} |
| 253 |
if (Ioss::Utils::str_equal(the_name, "numConstraints")) { |
| 254 |
return {the_name, static_cast<int>(numDOF) - static_cast<int>(numEIG)}; |
| 255 |
} |
| 256 |
|
| 257 |
return Ioss::GroupingEntity::get_implicit_property(the_name); |
| 258 |
} |