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