| 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 "ED_SystemInterface.h" // for SystemInterface, etc |
| 8 |
#include "exodusII.h" // for ex_set, etc |
| 9 |
#include "iqsort.h" // for index_qsort |
| 10 |
#include "side_set.h" |
| 11 |
#include "smart_assert.h" // for SMART_ASSERT |
| 12 |
#include <cstdlib> // for exit |
| 13 |
#include <vector> // for vector |
| 14 |
|
| 15 |
template <typename INT> Side_Set<INT>::Side_Set() : Exo_Entity() {} |
| 16 |
|
| 17 |
template <typename INT> Side_Set<INT>::Side_Set(int file_id, size_t id) : Exo_Entity(file_id, id) |
| 18 |
{ |
| 19 |
SMART_ASSERT((int)id != EX_INVALID_ID); |
| 20 |
} |
| 21 |
|
| 22 |
template <typename INT> |
| 23 |
Side_Set<INT>::Side_Set(int file_id, size_t id, size_t ns, size_t ndf) |
| 24 |
: Exo_Entity(file_id, id, ns), num_dist_factors(ndf) |
| 25 |
{ |
| 26 |
SMART_ASSERT(id > 0); |
| 27 |
} |
| 28 |
|
| 29 |
template <typename INT> Side_Set<INT>::~Side_Set() |
| 30 |
{ |
| 31 |
SMART_ASSERT(Check_State()); |
| 32 |
|
| 33 |
delete[] elmts; |
| 34 |
delete[] sides; |
| 35 |
delete[] sideIndex; |
| 36 |
delete[] dfIndex; |
| 37 |
delete[] dist_factors; |
| 38 |
} |
| 39 |
|
| 40 |
template <typename INT> EXOTYPE Side_Set<INT>::exodus_type() const { return EX_SIDE_SET; } |
| 41 |
|
| 42 |
template <typename INT> void Side_Set<INT>::entity_load_params() |
| 43 |
{ |
| 44 |
std::vector<ex_set> sets(1); |
| 45 |
sets[0].id = id_; |
| 46 |
sets[0].type = EX_SIDE_SET; |
| 47 |
sets[0].entry_list = nullptr; |
| 48 |
sets[0].extra_list = nullptr; |
| 49 |
sets[0].distribution_factor_list = nullptr; |
| 50 |
|
| 51 |
int err = ex_get_sets(fileId, 1, Data(sets)); |
| 52 |
|
| 53 |
if (err < 0) { |
| 54 |
Error(fmt::format("{}: Failed to get sideset parameters for sideset {}. ! Aborting...\n", |
| 55 |
__func__, id_)); |
| 56 |
} |
| 57 |
|
| 58 |
numEntity = sets[0].num_entry; |
| 59 |
num_dist_factors = sets[0].num_distribution_factor; |
| 60 |
} |
| 61 |
|
| 62 |
template <typename INT> void Side_Set<INT>::apply_map(const std::vector<INT> &elmt_map) |
| 63 |
{ |
| 64 |
SMART_ASSERT(!elmt_map.empty()); |
| 65 |
if (elmts != nullptr) { |
| 66 |
delete[] elmts; |
| 67 |
elmts = nullptr; |
| 68 |
delete[] sides; |
| 69 |
sides = nullptr; |
| 70 |
delete[] sideIndex; |
| 71 |
sideIndex = nullptr; |
| 72 |
} |
| 73 |
load_sides(elmt_map); |
| 74 |
} |
| 75 |
|
| 76 |
template <typename INT> void Side_Set<INT>::load_sides(const std::vector<INT> &elmt_map) const |
| 77 |
{ |
| 78 |
if ((elmts == nullptr || sides == nullptr) && numEntity > 0) { |
| 79 |
elmts = new INT[numEntity]; |
| 80 |
sides = new INT[numEntity]; |
| 81 |
sideIndex = new INT[numEntity]; |
| 82 |
|
| 83 |
int err = ex_get_set(fileId, EX_SIDE_SET, id_, elmts, sides); |
| 84 |
|
| 85 |
if (err < 0) { |
| 86 |
Error(fmt::format("{}: Failed to read side set {}! Aborting...\n", __func__, id_)); |
| 87 |
} |
| 88 |
|
| 89 |
if (!elmt_map.empty()) { |
| 90 |
for (size_t i = 0; i < numEntity; i++) { |
| 91 |
elmts[i] = 1 + elmt_map[elmts[i] - 1]; |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
if (interFace.ssmap_flag) { |
| 96 |
for (size_t i = 0; i < numEntity; i++) { |
| 97 |
sideIndex[i] = i; |
| 98 |
elmts[i] = elmts[i] * 8 + sides[i]; |
| 99 |
} |
| 100 |
|
| 101 |
index_qsort(elmts, sideIndex, numEntity); |
| 102 |
|
| 103 |
// Recover elmts... |
| 104 |
for (size_t i = 0; i < numEntity; i++) { |
| 105 |
elmts[i] = elmts[i] / 8; |
| 106 |
} |
| 107 |
} |
| 108 |
else { |
| 109 |
for (size_t i = 0; i < numEntity; i++) { |
| 110 |
sideIndex[i] = i; |
| 111 |
} |
| 112 |
} |
| 113 |
SMART_ASSERT(Check_State()); |
| 114 |
} |
| 115 |
} |
| 116 |
|
| 117 |
template <typename INT> void Side_Set<INT>::load_df() const |
| 118 |
{ |
| 119 |
if (elmts == nullptr) { |
| 120 |
std::vector<INT> tmp; |
| 121 |
load_sides(tmp); |
| 122 |
} |
| 123 |
|
| 124 |
if (dist_factors != nullptr) { |
| 125 |
return; // Already loaded. |
| 126 |
} |
| 127 |
|
| 128 |
dfIndex = new INT[numEntity + 1]; |
| 129 |
SMART_ASSERT(dfIndex != nullptr); |
| 130 |
std::vector<int> count(numEntity); |
| 131 |
|
| 132 |
// Handle the sierra "universal side set" which only has a single df per face... |
| 133 |
if (num_dist_factors == numEntity) { |
| 134 |
for (size_t i = 0; i < numEntity; i++) { |
| 135 |
count[i] = 1; |
| 136 |
} |
| 137 |
} |
| 138 |
else { |
| 139 |
int err = ex_get_side_set_node_count(fileId, id_, Data(count)); |
| 140 |
if (err < 0) { |
| 141 |
Error(fmt::format("{}: Failed to read side set node count for sideset {}! Aborting...\n", |
| 142 |
__func__, id_)); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
// Convert raw counts to index... |
| 147 |
size_t index = 0; |
| 148 |
for (size_t i = 0; i < numEntity; i++) { |
| 149 |
dfIndex[i] = index; |
| 150 |
index += count[i]; |
| 151 |
} |
| 152 |
dfIndex[numEntity] = index; |
| 153 |
|
| 154 |
// index value should now equal df count for this sideset... |
| 155 |
if (index != num_dist_factors) { |
| 156 |
Error(fmt::format("{}: Mismatch in distribution factor count for sideset {}, " |
| 157 |
"file says there should be {},\n\t\tbut ex_get_side_set_node_count says " |
| 158 |
"there should be {}! Aborting...\n", |
| 159 |
__func__, id_, num_dist_factors, index)); |
| 160 |
} |
| 161 |
SMART_ASSERT(index == num_dist_factors); |
| 162 |
dist_factors = new double[index]; |
| 163 |
int err = ex_get_set_dist_fact(fileId, EX_SIDE_SET, id_, dist_factors); |
| 164 |
if (err < 0) { |
| 165 |
Error(fmt::format( |
| 166 |
"{}: Failed to read side set distribution factors for sideset {}! Aborting...\n", __func__, |
| 167 |
id_)); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
template <typename INT> const INT *Side_Set<INT>::Elements() const |
| 172 |
{ |
| 173 |
std::vector<INT> tmp; |
| 174 |
load_sides(tmp); |
| 175 |
return elmts; |
| 176 |
} |
| 177 |
|
| 178 |
template <typename INT> const INT *Side_Set<INT>::Sides() const |
| 179 |
{ |
| 180 |
std::vector<INT> tmp; |
| 181 |
load_sides(tmp); |
| 182 |
return sides; |
| 183 |
} |
| 184 |
|
| 185 |
template <typename INT> std::pair<INT, INT> Side_Set<INT>::Side_Id(size_t position) const |
| 186 |
{ |
| 187 |
std::vector<INT> tmp; |
| 188 |
load_sides(tmp); |
| 189 |
SMART_ASSERT(position < numEntity); |
| 190 |
return std::make_pair(elmts[sideIndex[position]], sides[sideIndex[position]]); |
| 191 |
} |
| 192 |
|
| 193 |
template <typename INT> size_t Side_Set<INT>::Side_Index(size_t position) const |
| 194 |
{ |
| 195 |
std::vector<INT> tmp; |
| 196 |
load_sides(tmp); |
| 197 |
SMART_ASSERT(position < numEntity); |
| 198 |
return sideIndex[position]; |
| 199 |
} |
| 200 |
|
| 201 |
template <typename INT> const double *Side_Set<INT>::Distribution_Factors() const |
| 202 |
{ |
| 203 |
if (dist_factors == nullptr) { |
| 204 |
load_df(); |
| 205 |
} |
| 206 |
return dist_factors; |
| 207 |
} |
| 208 |
|
| 209 |
template <typename INT> void Side_Set<INT>::Free_Distribution_Factors() const |
| 210 |
{ |
| 211 |
if (dist_factors) { |
| 212 |
delete[] dist_factors; |
| 213 |
dist_factors = nullptr; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
template <typename INT> |
| 218 |
std::pair<INT, INT> Side_Set<INT>::Distribution_Factor_Range(size_t side) const |
| 219 |
{ |
| 220 |
if (dfIndex == nullptr) { |
| 221 |
load_df(); |
| 222 |
} |
| 223 |
if (dfIndex == nullptr) { |
| 224 |
Error(fmt::format("{}: Failed to get distribution factors for sideset {}! Aborting...\n", |
| 225 |
__func__, id_)); |
| 226 |
} |
| 227 |
size_t side_index = sideIndex[side]; |
| 228 |
return std::make_pair(dfIndex[side_index], dfIndex[side_index + 1]); |
| 229 |
} |
| 230 |
|
| 231 |
template <typename INT> int Side_Set<INT>::Check_State() const |
| 232 |
{ |
| 233 |
SMART_ASSERT(id_ >= EX_INVALID_ID); |
| 234 |
SMART_ASSERT(!(id_ == EX_INVALID_ID && numEntity > 0)); |
| 235 |
SMART_ASSERT(!(id_ == EX_INVALID_ID && num_dist_factors > 0)); |
| 236 |
SMART_ASSERT(!(id_ == EX_INVALID_ID && elmts)); |
| 237 |
SMART_ASSERT(!(id_ == EX_INVALID_ID && sides)); |
| 238 |
SMART_ASSERT(!(id_ == EX_INVALID_ID && dist_factors)); |
| 239 |
|
| 240 |
SMART_ASSERT(!(elmts && !sides)); |
| 241 |
SMART_ASSERT(!(!elmts && sides)); |
| 242 |
|
| 243 |
return 1; |
| 244 |
} |
| 245 |
|
| 246 |
template class Side_Set<int>; |
| 247 |
template class Side_Set<int64_t>; |