| 1 |
// Copyright(C) 1999-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_DatabaseIO.h" |
| 8 |
#include "Ioss_ElementTopology.h" |
| 9 |
#include "Ioss_EntityBlock.h" |
| 10 |
#include "Ioss_Field.h" |
| 11 |
#include "Ioss_Property.h" |
| 12 |
#include "Ioss_SideBlock.h" |
| 13 |
#include <cassert> |
| 14 |
#include <cstddef> |
| 15 |
#include <fmt/ostream.h> |
| 16 |
#include <iosfwd> |
| 17 |
#include <string> |
| 18 |
#include <tokenize.h> |
| 19 |
#include <vector> |
| 20 |
|
| 21 |
#include "Ioss_FieldManager.h" |
| 22 |
#include "Ioss_ParallelUtils.h" |
| 23 |
#include "Ioss_PropertyManager.h" |
| 24 |
#include "Ioss_Utils.h" |
| 25 |
|
| 26 |
/** \brief Create a side block. |
| 27 |
* |
| 28 |
* \param[in] io_database The database associated with the region containing the side block. |
| 29 |
* \param[in] my_name The side block's name. |
| 30 |
* \param[in] side_type The name of the side topology type for the side block. |
| 31 |
* \param[in] element_type The name of the element topology of the parent element type for the side |
| 32 |
* block. |
| 33 |
* \param[in] side_count The number of sides in the side block. |
| 34 |
*/ |
| 35 |
Ioss::SideBlock::SideBlock(Ioss::DatabaseIO *io_database, const std::string &my_name, |
| 36 |
const std::string &side_type, const std::string &element_type, |
| 37 |
size_t side_count) |
| 38 |
: Ioss::EntityBlock(io_database, my_name, side_type, side_count) |
| 39 |
{ |
| 40 |
parentTopology_ = ElementTopology::factory(element_type); |
| 41 |
assert(parentTopology_ != nullptr); |
| 42 |
|
| 43 |
properties.add(Ioss::Property(this, "parent_topology_type", Ioss::Property::STRING)); |
| 44 |
|
| 45 |
properties.add(Ioss::Property(this, "distribution_factor_count", Ioss::Property::INTEGER)); |
| 46 |
|
| 47 |
fields.add(Ioss::Field("element_side", field_int_type(), "pair", Ioss::Field::MESH, side_count)); |
| 48 |
|
| 49 |
// Same as element_side except that the element id are the local |
| 50 |
// element position (1-based) and not the global element id. |
| 51 |
fields.add( |
| 52 |
Ioss::Field("element_side_raw", field_int_type(), "pair", Ioss::Field::MESH, side_count)); |
| 53 |
|
| 54 |
// Distribution factors are optional... |
| 55 |
} |
| 56 |
|
| 57 |
Ioss::SideBlock::SideBlock(const Ioss::SideBlock &other) |
| 58 |
: EntityBlock(other), parentTopology_(other.parentTopology_), |
| 59 |
consistentSideNumber(other.consistentSideNumber) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
std::string Ioss::SideBlock::generate_sideblock_name(const std::string &sideset_name, |
| 64 |
const std::string &block_or_element, |
| 65 |
const std::string &face_topology_name) |
| 66 |
{ |
| 67 |
// The naming of sideblocks is: |
| 68 |
// * If name is of form surface_{id}, |
| 69 |
// * then {surface} + _ + block-or-element-topology + _ + side_topology + _ + {id} |
| 70 |
// * Eg – surface_1 would have sideblocks surface_block_1_quad_1 |
| 71 |
// |
| 72 |
// * If name is not of that form (e.g. surface_1_foam or “gregs_liner”) then: |
| 73 |
// * Name + _ + block-or-element-topology + _ + side_topology |
| 74 |
// * Eg surface_1_foam_block_1_edge2, surface_1_foam_quad4_edge2 |
| 75 |
// * Eg gregs_liner_block_1_edge2, gregs_liner_quad4_edge2 |
| 76 |
|
| 77 |
// Check whether the `block_or_element` portion of the name names a valid element topology... |
| 78 |
std::string tmp_block_or_element = block_or_element; |
| 79 |
auto *element_topology = ElementTopology::factory(block_or_element); |
| 80 |
if (element_topology != nullptr) { |
| 81 |
tmp_block_or_element = element_topology->name(); |
| 82 |
} |
| 83 |
|
| 84 |
// Verify that `face_topology_name` is a valid topology and Get its "non-aliased" name. |
| 85 |
std::string tmp_face_topology = face_topology_name; |
| 86 |
auto *face_topology = ElementTopology::factory(face_topology_name); |
| 87 |
if (face_topology != nullptr) { |
| 88 |
tmp_face_topology = face_topology->name(); |
| 89 |
} |
| 90 |
else { |
| 91 |
std::ostringstream errmsg; |
| 92 |
fmt::print(errmsg, "ERROR: Invalid face topology '{}' in function {}.\n", face_topology_name, |
| 93 |
__func__); |
| 94 |
IOSS_ERROR(errmsg); |
| 95 |
} |
| 96 |
|
| 97 |
auto tokens = Ioss::tokenize(sideset_name, "_"); |
| 98 |
if (tokens.size() == 2) { |
| 99 |
bool all_dig = tokens[1].find_first_not_of("0123456789") == std::string::npos; |
| 100 |
if (all_dig && Ioss::Utils::str_equal(tokens[0], "surface")) { |
| 101 |
std::string sideblock_name = |
| 102 |
tokens[0] + "_" + tmp_block_or_element + "_" + tmp_face_topology + "_" + tokens[1]; |
| 103 |
return sideblock_name; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
std::string sideblock_name = sideset_name + "_" + tmp_block_or_element + "_" + tmp_face_topology; |
| 108 |
return sideblock_name; |
| 109 |
} |
| 110 |
|
| 111 |
int64_t Ioss::SideBlock::internal_get_field_data(const Ioss::Field &field, void *data, |
| 112 |
size_t data_size) const |
| 113 |
{ |
| 114 |
return get_database()->get_field(this, field, data, data_size); |
| 115 |
} |
| 116 |
|
| 117 |
int64_t Ioss::SideBlock::internal_put_field_data(const Ioss::Field &field, void *data, |
| 118 |
size_t data_size) const |
| 119 |
{ |
| 120 |
return get_database()->put_field(this, field, data, data_size); |
| 121 |
} |
| 122 |
|
| 123 |
int64_t Ioss::SideBlock::internal_get_zc_field_data(const Field &field, void **data, |
| 124 |
size_t *data_size) const |
| 125 |
{ |
| 126 |
return get_database()->get_zc_field(this, field, data, data_size); |
| 127 |
} |
| 128 |
|
| 129 |
Ioss::Property Ioss::SideBlock::get_implicit_property(const std::string &my_name) const |
| 130 |
{ |
| 131 |
if (my_name == "distribution_factor_count") { |
| 132 |
if (field_exists("distribution_factors")) { |
| 133 |
int64_t nnodes = topology()->number_nodes(); |
| 134 |
int64_t nside = entity_count(); |
| 135 |
return {my_name, nnodes * nside}; |
| 136 |
} |
| 137 |
return {my_name, 0}; |
| 138 |
} |
| 139 |
if (my_name == "parent_topology_type") { |
| 140 |
return {my_name, parent_element_topology()->name()}; |
| 141 |
} |
| 142 |
|
| 143 |
return Ioss::EntityBlock::get_implicit_property(my_name); |
| 144 |
} |
| 145 |
|
| 146 |
void Ioss::SideBlock::block_membership(Ioss::NameList &block_members) |
| 147 |
{ |
| 148 |
// Simplest case. If the surfaces are split by element block, then this will |
| 149 |
// return non-null |
| 150 |
// and we are done. |
| 151 |
const Ioss::EntityBlock *eb = parent_block(); |
| 152 |
if (eb != nullptr) { |
| 153 |
block_members.push_back(eb->name()); |
| 154 |
return; |
| 155 |
} |
| 156 |
|
| 157 |
if (blockMembership.empty()) { |
| 158 |
get_database()->compute_block_membership(this, blockMembership); |
| 159 |
} |
| 160 |
block_members = blockMembership; |
| 161 |
} |
| 162 |
|
| 163 |
namespace { |
| 164 |
template <typename INT> int internal_consistent_side_number(std::vector<INT> &element_side) |
| 165 |
{ |
| 166 |
size_t ecount = element_side.size(); |
| 167 |
int side = ecount > 0 ? static_cast<INT>(element_side[1]) : 0; |
| 168 |
for (size_t i = 3; i < ecount; i += 2) { |
| 169 |
int this_side = static_cast<INT>(element_side[i]); |
| 170 |
if (this_side != side) { |
| 171 |
side = 999; // Indicates the sides are not consistent ; |
| 172 |
break; |
| 173 |
} |
| 174 |
} |
| 175 |
return side; |
| 176 |
} |
| 177 |
} // namespace |
| 178 |
|
| 179 |
int Ioss::SideBlock::get_consistent_side_number() const |
| 180 |
{ |
| 181 |
if (consistentSideNumber == -1) { |
| 182 |
// It wasn't calculated during the metadata reading of the surfaces. |
| 183 |
// Determine it now... |
| 184 |
if (field_exists("element_side")) { |
| 185 |
int side = 0; |
| 186 |
if (get_database()->int_byte_size_api() == 8) { |
| 187 |
std::vector<int64_t> element_side; |
| 188 |
get_field_data("element_side", element_side); |
| 189 |
side = internal_consistent_side_number(element_side); |
| 190 |
} |
| 191 |
else { |
| 192 |
std::vector<int> element_side; |
| 193 |
get_field_data("element_side", element_side); |
| 194 |
side = internal_consistent_side_number(element_side); |
| 195 |
} |
| 196 |
|
| 197 |
int side_max = get_database()->util().global_minmax(side, Ioss::ParallelUtils::DO_MAX); |
| 198 |
if (side_max != 999) { |
| 199 |
consistentSideNumber = side_max; |
| 200 |
} |
| 201 |
else { |
| 202 |
consistentSideNumber = 0; |
| 203 |
} |
| 204 |
} |
| 205 |
else { |
| 206 |
consistentSideNumber = 0; |
| 207 |
} |
| 208 |
} |
| 209 |
return consistentSideNumber; |
| 210 |
} |
| 211 |
|
| 212 |
bool Ioss::SideBlock::equal_(const Ioss::SideBlock &rhs, bool quiet) const |
| 213 |
{ |
| 214 |
if (this->parentTopology_ != rhs.parentTopology_) { |
| 215 |
if (!quiet) { |
| 216 |
fmt::print(Ioss::OUTPUT(), "SideBlock: parentTopology_ mismatch\n"); |
| 217 |
} |
| 218 |
return false; |
| 219 |
} |
| 220 |
|
| 221 |
if (this->blockMembership != rhs.blockMembership) { |
| 222 |
if (!quiet) { |
| 223 |
fmt::print(Ioss::OUTPUT(), "SideBlock: blockMembership mismatch\n"); |
| 224 |
} |
| 225 |
return false; |
| 226 |
} |
| 227 |
|
| 228 |
if (this->consistentSideNumber != rhs.consistentSideNumber) { |
| 229 |
if (!quiet) { |
| 230 |
fmt::print(Ioss::OUTPUT(), "SideBlock: consistentSideNumber mismatch ({} vs. {})\n", |
| 231 |
this->consistentSideNumber, rhs.consistentSideNumber); |
| 232 |
} |
| 233 |
return false; |
| 234 |
} |
| 235 |
|
| 236 |
if (!quiet) { |
| 237 |
return Ioss::EntityBlock::equal(rhs); |
| 238 |
} |
| 239 |
return Ioss::EntityBlock::operator==(rhs); |
| 240 |
} |
| 241 |
|
| 242 |
bool Ioss::SideBlock::operator==(const Ioss::SideBlock &rhs) const { return equal_(rhs, true); } |
| 243 |
|
| 244 |
bool Ioss::SideBlock::operator!=(const Ioss::SideBlock &rhs) const { return !(*this == rhs); } |
| 245 |
|
| 246 |
bool Ioss::SideBlock::equal(const Ioss::SideBlock &rhs) const { return equal_(rhs, false); } |