| 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 "EJ_mapping.h" |
| 8 |
#include "Ioss_ElementBlock.h" // for ElementBlock |
| 9 |
#include "Ioss_GroupingEntity.h" // for GroupingEntity |
| 10 |
#include "Ioss_NodeBlock.h" // for NodeBlock |
| 11 |
#include "Ioss_Property.h" // for Property |
| 12 |
#include "Ioss_Region.h" // for Region, etc |
| 13 |
#include "Ioss_SmartAssert.h" |
| 14 |
#include "Ioss_Sort.h" |
| 15 |
#include <algorithm> // for sort, unique |
| 16 |
#include <cstddef> // for size_t |
| 17 |
#include <fmt/ostream.h> |
| 18 |
#include <numeric> |
| 19 |
#include <utility> // for make_pair, pair |
| 20 |
|
| 21 |
namespace { |
| 22 |
bool entity_is_omitted(const Ioss::GroupingEntity *block) |
| 23 |
{ |
| 24 |
bool omitted = block->get_optional_property("omitted", 0) == 1; |
| 25 |
return omitted; |
| 26 |
} |
| 27 |
} // namespace |
| 28 |
|
| 29 |
template <typename INT> |
| 30 |
void eliminate_omitted_nodes(RegionVector &part_mesh, std::vector<INT> &global_node_map, |
| 31 |
std::vector<INT> &local_node_map, bool fill_global) |
| 32 |
{ |
| 33 |
size_t offset = 0; |
| 34 |
size_t j = 0; |
| 35 |
size_t part_count = part_mesh.size(); |
| 36 |
for (size_t p = 0; p < part_count; p++) { |
| 37 |
bool has_omissions = part_mesh[p]->get_property("block_omission_count").get_int() > 0; |
| 38 |
const Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0]; |
| 39 |
size_t loc_size = nb->entity_count(); |
| 40 |
if (has_omissions) { |
| 41 |
// If there are any omitted element blocks for this part, don't |
| 42 |
// map the nodes that are only connected to omitted element |
| 43 |
// blocks. |
| 44 |
std::vector<char> node_status; |
| 45 |
nb->get_field_data("node_connectivity_status", node_status); |
| 46 |
for (size_t i = 0; i < node_status.size(); i++) { |
| 47 |
if (node_status[i] != 1) { |
| 48 |
local_node_map[offset + i] = j; |
| 49 |
if (fill_global) { |
| 50 |
global_node_map.push_back(j + 1); |
| 51 |
} |
| 52 |
j++; |
| 53 |
} |
| 54 |
else { |
| 55 |
local_node_map[offset + i] = -1; |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
else { |
| 60 |
for (size_t i = 0; i < loc_size; i++) { |
| 61 |
local_node_map[offset + i] = j; |
| 62 |
if (fill_global) { |
| 63 |
global_node_map.push_back(j + 1); |
| 64 |
} |
| 65 |
j++; |
| 66 |
} |
| 67 |
} |
| 68 |
offset += loc_size; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
template void eliminate_omitted_nodes(RegionVector &part_mesh, std::vector<int> &global_node_map, |
| 73 |
std::vector<int> &local_node_map, bool fill_global); |
| 74 |
template void eliminate_omitted_nodes(RegionVector &part_mesh, |
| 75 |
std::vector<int64_t> &global_node_map, |
| 76 |
std::vector<int64_t> &local_node_map, bool fill_global); |
| 77 |
|
| 78 |
template <typename INT> |
| 79 |
void build_reverse_node_map(Ioss::Region & /*global*/, RegionVector &part_mesh, |
| 80 |
std::vector<INT> &global_node_map, std::vector<INT> &local_node_map) |
| 81 |
{ |
| 82 |
// Instead of using <set> and <map>, consider using a sorted vector... |
| 83 |
// Append all local node maps to the global node map. |
| 84 |
// Sort the global node map |
| 85 |
// Remove duplicates. |
| 86 |
// Position within map is now the map... |
| 87 |
// When building the local-part node to global id, use binary_search... |
| 88 |
|
| 89 |
size_t part_count = part_mesh.size(); |
| 90 |
|
| 91 |
// Global node map and count. |
| 92 |
std::vector<std::vector<int>> global_nodes(part_count); |
| 93 |
|
| 94 |
size_t tot_size = 0; |
| 95 |
for (size_t p = 0; p < part_count; p++) { |
| 96 |
const Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0]; |
| 97 |
size_t loc_size = nb->entity_count(); |
| 98 |
tot_size += loc_size; |
| 99 |
global_nodes[p].resize(loc_size); |
| 100 |
} |
| 101 |
global_node_map.resize(tot_size); |
| 102 |
|
| 103 |
size_t offset = 0; |
| 104 |
bool any_omitted_nodes = false; |
| 105 |
for (size_t p = 0; p < part_count; p++) { |
| 106 |
const Ioss::NodeBlock *nb = part_mesh[p]->get_node_blocks()[0]; |
| 107 |
nb->get_field_data("ids", global_nodes[p]); |
| 108 |
|
| 109 |
// If there are any omitted element blocks for this part, set |
| 110 |
// the global id of any nodes that are only connected to omitted |
| 111 |
// element blocks to 0. |
| 112 |
bool has_omissions = part_mesh[p]->get_property("block_omission_count").get_int() > 0; |
| 113 |
if (has_omissions) { |
| 114 |
std::vector<char> node_status; |
| 115 |
nb->get_field_data("node_connectivity_status", node_status); |
| 116 |
for (size_t i = 0; i < node_status.size(); i++) { |
| 117 |
if (node_status[i] == 1) { |
| 118 |
any_omitted_nodes = true; |
| 119 |
global_nodes[p][i] = 0; |
| 120 |
} |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
std::copy(global_nodes[p].begin(), global_nodes[p].end(), &global_node_map[offset]); |
| 125 |
offset += global_nodes[p].size(); |
| 126 |
} |
| 127 |
|
| 128 |
// Now, sort the global_node_map array and remove duplicates... |
| 129 |
Ioss::Utils::uniquify(global_node_map); |
| 130 |
|
| 131 |
// If any omitted nodes, remove them from the global_node_map. |
| 132 |
// The id will be 0 |
| 133 |
if (any_omitted_nodes) { |
| 134 |
auto pos = std::remove(global_node_map.begin(), global_node_map.end(), 0); |
| 135 |
global_node_map.erase(pos, global_node_map.end()); |
| 136 |
} |
| 137 |
|
| 138 |
size_t output_node_count = global_node_map.size(); |
| 139 |
|
| 140 |
// See whether the node numbers are contiguous. If so, we can map |
| 141 |
// the nodes back to their original location. Since the nodes are |
| 142 |
// sorted and there are no duplicates, we just need to see if the id |
| 143 |
// at global_node_map.size() == global_node_map.size(); |
| 144 |
size_t max_id = global_node_map[output_node_count - 1]; |
| 145 |
|
| 146 |
bool is_contiguous = max_id == output_node_count; |
| 147 |
fmt::print("Node map {} contiguous.\n", (is_contiguous ? "is" : "is not")); |
| 148 |
|
| 149 |
// Create the map that maps from a local part node to the |
| 150 |
// global map. This combines the mapping local part node to |
| 151 |
// 'global id' and then 'global id' to global position. The |
| 152 |
// mapping is now a direct lookup instead of a lookup followed by |
| 153 |
// a reverse map. |
| 154 |
auto cur_pos = global_node_map.begin(); |
| 155 |
for (size_t p = 0; p < part_count; p++) { |
| 156 |
size_t noffset = part_mesh[p]->get_property("node_offset").get_int(); |
| 157 |
size_t node_count = global_nodes[p].size(); |
| 158 |
for (size_t i = 0; i < node_count; i++) { |
| 159 |
INT global_node = global_nodes[p][i]; |
| 160 |
|
| 161 |
if (global_node > 0) { |
| 162 |
if (cur_pos == global_node_map.end() || *cur_pos != global_node) { |
| 163 |
auto iter = std::lower_bound(global_node_map.begin(), global_node_map.end(), global_node); |
| 164 |
if (iter == global_node_map.end()) { |
| 165 |
fmt::print(stderr, "ERROR: Could not find global node {} in global node map.\n", |
| 166 |
fmt::group_digits(global_node)); |
| 167 |
exit(EXIT_FAILURE); |
| 168 |
} |
| 169 |
cur_pos = iter; |
| 170 |
} |
| 171 |
size_t nodal_value = cur_pos - global_node_map.begin(); |
| 172 |
local_node_map[noffset + i] = nodal_value; |
| 173 |
++cur_pos; |
| 174 |
} |
| 175 |
else { |
| 176 |
local_node_map[noffset + i] = -1; |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
// Update the nodal ids to give a unique, non-repeating set. If contiguous, then |
| 182 |
// there is nothing to do. If not contiguous, then need to determine if there are any |
| 183 |
// repeats (id reuse) and if so, generate a new id for the repeated uses. |
| 184 |
if (!is_contiguous) { |
| 185 |
bool repeat_found = false; |
| 186 |
INT id_last = global_node_map[0]; |
| 187 |
for (size_t i = 1; i < output_node_count; i++) { |
| 188 |
if (global_node_map[i] == id_last) { |
| 189 |
global_node_map[i] = ++max_id; |
| 190 |
repeat_found = true; |
| 191 |
} |
| 192 |
else { |
| 193 |
id_last = global_node_map[i]; |
| 194 |
} |
| 195 |
} |
| 196 |
if (repeat_found) { |
| 197 |
fmt::print("Duplicate node ids were found. Their ids have been renumbered to remove " |
| 198 |
"duplicates.\n"); |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
template void build_reverse_node_map(Ioss::Region &global, RegionVector &part_mesh, |
| 204 |
std::vector<int> &global_node_map, |
| 205 |
std::vector<int> &local_node_map); |
| 206 |
template void build_reverse_node_map(Ioss::Region &global, RegionVector &part_mesh, |
| 207 |
std::vector<int64_t> &global_node_map, |
| 208 |
std::vector<int64_t> &local_node_map); |
| 209 |
|
| 210 |
template <typename INT> |
| 211 |
void build_local_element_map(RegionVector &part_mesh, std::vector<INT> &local_element_map) |
| 212 |
{ |
| 213 |
size_t global = 0; |
| 214 |
size_t offset = 0; |
| 215 |
for (auto &p : part_mesh) { |
| 216 |
|
| 217 |
const auto &ebs = p->get_element_blocks(); |
| 218 |
auto i = ebs.begin(); |
| 219 |
|
| 220 |
while (i != ebs.end()) { |
| 221 |
const auto *eb = *i++; |
| 222 |
size_t num_elem = eb->entity_count(); |
| 223 |
if (entity_is_omitted(eb)) { |
| 224 |
// Fill local_element_map with -1 for the omitted elements. |
| 225 |
for (size_t j = 0; j < num_elem; j++) { |
| 226 |
local_element_map[offset + j] = -1; |
| 227 |
} |
| 228 |
} |
| 229 |
else { |
| 230 |
for (size_t j = 0; j < num_elem; j++) { |
| 231 |
local_element_map[offset + j] = global++; |
| 232 |
} |
| 233 |
} |
| 234 |
offset += num_elem; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
template void build_local_element_map(RegionVector &part_mesh, std::vector<int> &local_element_map); |
| 240 |
template void build_local_element_map(RegionVector &part_mesh, |
| 241 |
std::vector<int64_t> &local_element_map); |
| 242 |
|
| 243 |
template <typename INT> |
| 244 |
void generate_element_ids(RegionVector &part_mesh, const std::vector<INT> &local_element_map, |
| 245 |
std::vector<INT> &global_element_map) |
| 246 |
{ |
| 247 |
// Follow same logic as 'build_local_element_map' to ensure elements |
| 248 |
// are processed in same order. |
| 249 |
|
| 250 |
// Many models do not use the element number map at all, so they |
| 251 |
// will have a 1..numel map. If all parts have that, then we don't |
| 252 |
// want to do any fancy duplicate removal and other processing, just |
| 253 |
// output a 1..numel map for the output mesh... We still generate |
| 254 |
// the global_element_map, but check whether any of the part blocks |
| 255 |
// have a non-1..numel map... |
| 256 |
bool has_map = false; |
| 257 |
size_t offset = 0; |
| 258 |
for (auto &p : part_mesh) { |
| 259 |
const auto &ebs = p->get_element_blocks(); |
| 260 |
for (auto *eb : ebs) { |
| 261 |
INT num_elem = eb->entity_count(); |
| 262 |
if (!entity_is_omitted(eb)) { |
| 263 |
std::vector<INT> part_ids; |
| 264 |
eb->get_field_data("ids", part_ids); |
| 265 |
|
| 266 |
if (!has_map) { |
| 267 |
INT eb_offset = eb->get_offset(); |
| 268 |
for (INT j = 0; j < num_elem; j++) { |
| 269 |
if (part_ids[j] != eb_offset + j + 1) { |
| 270 |
has_map = true; |
| 271 |
break; |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
for (INT j = 0; j < num_elem; j++) { |
| 277 |
INT gpos = local_element_map[offset + j]; |
| 278 |
if (gpos >= 0) { |
| 279 |
global_element_map[gpos] = part_ids[j]; |
| 280 |
} |
| 281 |
} |
| 282 |
} |
| 283 |
offset += num_elem; |
| 284 |
} |
| 285 |
} |
| 286 |
// Check for duplicates... |
| 287 |
// NOTE: Used to use an indexed sort here, but if there was a |
| 288 |
// duplicate id, it didn't really care whether part 1 or part N's |
| 289 |
// index came first which causes really screwy element maps. |
| 290 |
// Instead, lets sort a vector containing pairs of <id, index> where |
| 291 |
// the index will always? increase for increasing part numbers... |
| 292 |
if (has_map) { |
| 293 |
std::vector<std::pair<INT, INT>> index(global_element_map.size()); |
| 294 |
for (size_t i = 0; i < index.size(); i++) { |
| 295 |
index[i] = std::make_pair(global_element_map[i], (INT)i); |
| 296 |
} |
| 297 |
|
| 298 |
Ioss::sort(index.begin(), index.end()); |
| 299 |
|
| 300 |
INT max_id = index[index.size() - 1].first + 1; |
| 301 |
|
| 302 |
size_t beg = 0; |
| 303 |
for (size_t i = 1; i < index.size(); i++) { |
| 304 |
if (index[beg].first == index[i].first) { |
| 305 |
// Duplicate found... Assign it a new id greater than any |
| 306 |
// existing id... (What happens if we exceed INT_MAX?) |
| 307 |
global_element_map[index[i].second] = max_id++; |
| 308 |
// Keep 'beg' the same in case multiple duplicate of this value. |
| 309 |
} |
| 310 |
else { |
| 311 |
beg = i; |
| 312 |
} |
| 313 |
} |
| 314 |
} |
| 315 |
else { |
| 316 |
INT one = 1; |
| 317 |
std::iota(global_element_map.begin(), global_element_map.end(), one); |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
template void generate_element_ids(RegionVector &part_mesh, |
| 322 |
const std::vector<int> &local_element_map, |
| 323 |
std::vector<int> &global_element_map); |
| 324 |
template void generate_element_ids(RegionVector &part_mesh, |
| 325 |
const std::vector<int64_t> &local_element_map, |
| 326 |
std::vector<int64_t> &global_element_map); |