| 1 |
// Copyright(C) 1999-2021, 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 <visualization/cgns/Iovs_cgns_DatabaseIO.h> |
| 8 |
#include <visualization/utils/Iovs_Utils.h> |
| 9 |
|
| 10 |
#include "Ioss_Property.h" |
| 11 |
#include "Ioss_Region.h" |
| 12 |
#include "Ioss_State.h" |
| 13 |
#include "Ioss_StructuredBlock.h" |
| 14 |
#include "Ioss_Utils.h" |
| 15 |
|
| 16 |
namespace Iovs_cgns { |
| 17 |
|
| 18 |
DatabaseIO::DatabaseIO(Ioss::Region *region, const std::string &filename, |
| 19 |
Ioss::DatabaseUsage db_usage, Ioss_MPI_Comm communicator, |
| 20 |
const Ioss::PropertyManager &props) |
| 21 |
: Ioss::DatabaseIO(region, |
| 22 |
Iovs::Utils::getInstance().getDatabaseOutputFilePath(filename, props), |
| 23 |
db_usage, communicator, props) |
| 24 |
{ |
| 25 |
|
| 26 |
Iovs::Utils::DatabaseInfo dbinfo; |
| 27 |
dbinfo.databaseFilename = this->DBFilename; |
| 28 |
dbinfo.separatorCharacter = std::string(1, this->get_field_separator()); |
| 29 |
dbinfo.parallelUtils = &this->util(); |
| 30 |
isIdOutputCreated = false; |
| 31 |
|
| 32 |
Iovs::Utils::getInstance().checkDbUsage(db_usage); |
| 33 |
Iovs::Utils::getInstance().createDatabaseOutputFile(dbinfo); |
| 34 |
dbState = Ioss::STATE_UNKNOWN; |
| 35 |
Iovs::Utils::getInstance().writeToCatalystLogFile(dbinfo, props); |
| 36 |
this->catCGNSMesh = Iovs::Utils::getInstance().createCatalystCGNSMesh(dbinfo, props); |
| 37 |
} |
| 38 |
|
| 39 |
DatabaseIO::~DatabaseIO() { this->catCGNSMesh->Delete(); } |
| 40 |
|
| 41 |
bool DatabaseIO::begin_nl(Ioss::State /*state*/) { return true; } |
| 42 |
|
| 43 |
bool DatabaseIO::end_nl(Ioss::State state) |
| 44 |
{ |
| 45 |
switch (state) { |
| 46 |
case Ioss::STATE_DEFINE_MODEL: { |
| 47 |
write_meta_data(); |
| 48 |
break; |
| 49 |
} |
| 50 |
default: break; |
| 51 |
} |
| 52 |
return true; |
| 53 |
} |
| 54 |
|
| 55 |
bool DatabaseIO::begin_state_nl(int state, double time) |
| 56 |
{ |
| 57 |
this->catCGNSMesh->ReleaseMemory(); |
| 58 |
if (!isIdOutputCreated) { |
| 59 |
createIdOutput(); |
| 60 |
isIdOutputCreated = true; |
| 61 |
} |
| 62 |
this->catCGNSMesh->SetTimeData(time, state - 1); |
| 63 |
return true; |
| 64 |
} |
| 65 |
|
| 66 |
bool DatabaseIO::end_state_nl(int /*state*/, double /*time*/) |
| 67 |
{ |
| 68 |
std::vector<int> error_codes; |
| 69 |
std::vector<std::string> error_messages; |
| 70 |
|
| 71 |
this->catCGNSMesh->logMemoryUsageAndTakeTimerReading(); |
| 72 |
this->catCGNSMesh->PerformCoProcessing(error_codes, error_messages); |
| 73 |
this->catCGNSMesh->logMemoryUsageAndTakeTimerReading(); |
| 74 |
Iovs::Utils::getInstance().reportCatalystErrorMessages(error_codes, error_messages, |
| 75 |
this->parallel_rank()); |
| 76 |
return true; |
| 77 |
} |
| 78 |
|
| 79 |
void DatabaseIO::read_meta_data_nl() {} |
| 80 |
|
| 81 |
void DatabaseIO::write_meta_data() |
| 82 |
{ |
| 83 |
const auto &structured_blocks = get_region()->get_structured_blocks(); |
| 84 |
int zone = 1; |
| 85 |
for (const auto &sb : structured_blocks) { |
| 86 |
sb->property_update("zone", zone); |
| 87 |
zone++; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
void DatabaseIO::createIdOutput() |
| 92 |
{ |
| 93 |
const auto &structured_blocks = get_region()->get_structured_blocks(); |
| 94 |
for (const auto &sb : structured_blocks) { |
| 95 |
size_t node_count = sb->get_property("node_count").get_int(); |
| 96 |
std::vector<int64_t> ids; |
| 97 |
ids.resize(node_count); |
| 98 |
sb->get_cell_node_ids(Data(ids), true); |
| 99 |
bool isCellField = false; |
| 100 |
outputId("cell_node_ids", ids, isCellField, sb); |
| 101 |
|
| 102 |
size_t cell_count = sb->get_property("cell_count").get_int(); |
| 103 |
ids.resize(cell_count); |
| 104 |
sb->get_cell_ids(Data(ids), true); |
| 105 |
isCellField = true; |
| 106 |
outputId("cell_ids", ids, isCellField, sb); |
| 107 |
|
| 108 |
auto zone_id = sb->get_property("zone").get_int(); |
| 109 |
std::vector<int64_t> object_id(cell_count, zone_id); |
| 110 |
outputId("object_id", object_id, isCellField, sb); |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void DatabaseIO::outputId(const std::string idName, std::vector<int64_t> &ids, bool isCellField, |
| 115 |
const Ioss::StructuredBlock *sb) |
| 116 |
{ |
| 117 |
CatalystCGNSMeshBase::ZoneData zoneData; |
| 118 |
initZoneDataFromStructuredBlock(zoneData, sb); |
| 119 |
zoneData.data_name = idName; |
| 120 |
zoneData.comp_count = 1; |
| 121 |
zoneData.is_cell_field = isCellField; |
| 122 |
zoneData.size = ids.size(); |
| 123 |
zoneData.data_type = zoneData.T_INT64; |
| 124 |
zoneData.data.p_int64 = Data(ids); |
| 125 |
|
| 126 |
this->catCGNSMesh->AddStructuredZoneData(zoneData); |
| 127 |
} |
| 128 |
|
| 129 |
void DatabaseIO::initZoneDataFromStructuredBlock(CatalystCGNSMeshBase::ZoneData &zoneData, |
| 130 |
const Ioss::StructuredBlock *sb) const |
| 131 |
{ |
| 132 |
zoneData.zone_id = sb->get_property("zone").get_int(); |
| 133 |
zoneData.zone_name = sb->name(); |
| 134 |
zoneData.ni = sb->get_property("ni").get_int(); |
| 135 |
zoneData.nj = sb->get_property("nj").get_int(); |
| 136 |
zoneData.nk = sb->get_property("nk").get_int(); |
| 137 |
} |
| 138 |
|
| 139 |
int64_t DatabaseIO::put_field_internal(const Ioss::StructuredBlock *sb, const Ioss::Field &field, |
| 140 |
void *data, size_t data_size) const |
| 141 |
{ |
| 142 |
size_t node_count = sb->get_property("node_count").get_int(); |
| 143 |
size_t num_to_get = field.verify(data_size); |
| 144 |
auto var_type = field.transformed_storage(); |
| 145 |
auto ioss_type = field.get_type(); |
| 146 |
int comp_count = var_type->component_count(); |
| 147 |
void *rdata = num_to_get > 0 ? data : nullptr; |
| 148 |
|
| 149 |
bool is_cell_field = true; |
| 150 |
if (node_count == num_to_get) { |
| 151 |
is_cell_field = false; |
| 152 |
} |
| 153 |
|
| 154 |
CatalystCGNSMeshBase::ZoneData zoneData; |
| 155 |
initZoneDataFromStructuredBlock(zoneData, sb); |
| 156 |
zoneData.data_name = field.get_name(); |
| 157 |
zoneData.comp_count = comp_count; |
| 158 |
zoneData.is_cell_field = is_cell_field; |
| 159 |
zoneData.size = num_to_get; |
| 160 |
|
| 161 |
if (ioss_type == Ioss::Field::INTEGER) { |
| 162 |
zoneData.data_type = zoneData.T_INT; |
| 163 |
zoneData.data.p_int = static_cast<int *>(data); |
| 164 |
} |
| 165 |
else if (ioss_type == Ioss::Field::INT64) { |
| 166 |
zoneData.data_type = zoneData.T_INT64; |
| 167 |
zoneData.data.p_int64 = static_cast<int64_t *>(data); |
| 168 |
} |
| 169 |
else { |
| 170 |
zoneData.data_type = zoneData.T_DOUBLE; |
| 171 |
zoneData.data.p_double = static_cast<double *>(data); |
| 172 |
} |
| 173 |
|
| 174 |
if (field.get_name() == "mesh_model_coordinates") { |
| 175 |
int phys_dimension = get_region()->get_property("spatial_dimension").get_int(); |
| 176 |
|
| 177 |
std::vector<double> coord(num_to_get); |
| 178 |
|
| 179 |
// ======================================================================== |
| 180 |
// Repetitive code for each coordinate direction; use a lambda to consolidate... |
| 181 |
auto coord_lambda = [&zoneData, &coord, &rdata, num_to_get, phys_dimension, |
| 182 |
this](const char *ordinate, int ordinal) { |
| 183 |
// Data required by upper classes store x0, y0, z0, ... xn, |
| 184 |
// yn, zn. Data stored in cgns file is x0, ..., xn, y0, |
| 185 |
// ..., yn, z0, ..., zn so we have to allocate some scratch |
| 186 |
// memory to read in the data and then map into supplied |
| 187 |
// 'data' |
| 188 |
// Map to global coordinate position... |
| 189 |
for (size_t i = 0; i < num_to_get; i++) { |
| 190 |
coord[i] = static_cast<double *>(rdata)[phys_dimension * i + ordinal]; |
| 191 |
} |
| 192 |
|
| 193 |
zoneData.data_name = ordinate; |
| 194 |
zoneData.data.p_double = Data(coord); |
| 195 |
this->catCGNSMesh->AddStructuredZoneData(zoneData); |
| 196 |
}; |
| 197 |
// ======================================================================== |
| 198 |
|
| 199 |
coord_lambda("mesh_model_coordinates_x", 0); |
| 200 |
|
| 201 |
if (phys_dimension >= 2) { |
| 202 |
coord_lambda("mesh_model_coordinates_y", 1); |
| 203 |
} |
| 204 |
|
| 205 |
if (phys_dimension == 3) { |
| 206 |
coord_lambda("mesh_model_coordinates_z", 2); |
| 207 |
} |
| 208 |
} |
| 209 |
else { |
| 210 |
this->catCGNSMesh->AddStructuredZoneData(zoneData); |
| 211 |
} |
| 212 |
return num_to_get; |
| 213 |
} |
| 214 |
|
| 215 |
} // namespace Iovs_cgns |