| 1 |
// Copyright(C) 1999-2020, 2022 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 |
#pragma once |
| 6 |
|
| 7 |
// ####################### Start Clang Header Tool Managed Headers ######################## |
| 8 |
// clang-format off |
| 9 |
#include <ctype.h> // for toupper |
| 10 |
#include <stddef.h> // for size_t |
| 11 |
#include <algorithm> // for remove, etc |
| 12 |
#include <iterator> // for insert_iterator |
| 13 |
#include <map> |
| 14 |
#include <set> // for set |
| 15 |
#include <sstream> // for operator<<, etc |
| 16 |
#include <string> // for basic_string, etc |
| 17 |
#include <utility> // for pair |
| 18 |
#include <vector> // for vector |
| 19 |
#include <unordered_map> |
| 20 |
#include <sstream> // for ostringstream |
| 21 |
#include <iostream> |
| 22 |
#include <functional> |
| 23 |
#include <stdexcept> |
| 24 |
#include <numeric> |
| 25 |
#include <strings.h> |
| 26 |
|
| 27 |
#include "Iotm_TextMeshFuncs.h" |
| 28 |
|
| 29 |
// clang-format on |
| 30 |
// ####################### End Clang Header Tool Managed Headers ######################## |
| 31 |
namespace Iotm { |
| 32 |
namespace text_mesh { |
| 33 |
|
| 34 |
using ErrorHandler = std::function<void(const std::ostringstream &)>; |
| 35 |
|
| 36 |
template <typename T> struct EntityGroupData |
| 37 |
{ |
| 38 |
using DataType = T; |
| 39 |
static constexpr unsigned INVALID_ID = std::numeric_limits<unsigned>::max(); |
| 40 |
|
| 41 |
bool hasInputName = false; |
| 42 |
unsigned id = INVALID_ID; |
| 43 |
std::string name = ""; |
| 44 |
std::string type = ""; |
| 45 |
std::vector<DataType> data{}; |
| 46 |
|
| 47 |
bool has_valid_id() const { return id != 0 && id != INVALID_ID; } |
| 48 |
bool has_name() const { return !name.empty(); } |
| 49 |
}; |
| 50 |
|
| 51 |
// Base grouping class for sidesets, nodesets, assemblies, etc |
| 52 |
template <typename GroupData> class EntityGroup |
| 53 |
{ |
| 54 |
private: |
| 55 |
using DataType = typename GroupData::DataType; |
| 56 |
|
| 57 |
public: |
| 58 |
EntityGroup(const std::string &type, const std::string &namePrefix, |
| 59 |
const std::vector<std::string> &invalidNamePrefixes) |
| 60 |
: m_idsAssigned(false), m_type(type), m_exodusPrefix(namePrefix), |
| 61 |
m_invalidPrefixes(invalidNamePrefixes) |
| 62 |
{ |
| 63 |
set_error_handler([](const std::ostringstream &errmsg) { default_error_handler(errmsg); }); |
| 64 |
} |
| 65 |
|
| 66 |
virtual ~EntityGroup() {} |
| 67 |
|
| 68 |
virtual void set_error_handler(ErrorHandler errorHandler) { m_errorHandler = errorHandler; } |
| 69 |
|
| 70 |
GroupData *add_group_data(const std::string &name, const std::vector<DataType> &data) |
| 71 |
{ |
| 72 |
GroupData groupData; |
| 73 |
groupData.data = data; |
| 74 |
groupData.type = m_type; |
| 75 |
|
| 76 |
if (!name.empty()) { |
| 77 |
verify_name(name); |
| 78 |
groupData.name = name; |
| 79 |
groupData.hasInputName = true; |
| 80 |
} |
| 81 |
|
| 82 |
m_groupDataVec.push_back(groupData); |
| 83 |
|
| 84 |
return &m_groupDataVec.back(); |
| 85 |
} |
| 86 |
|
| 87 |
void finalize_parse() |
| 88 |
{ |
| 89 |
assign_id_from_default_exodus_name(); |
| 90 |
assign_id_and_name_for_empty_name(); |
| 91 |
assign_id_for_non_default_exodus_name(); |
| 92 |
|
| 93 |
if (m_groupDataVec.size() != m_groupDataMap.size()) { |
| 94 |
std::ostringstream errmsg; |
| 95 |
errmsg << "Error populating " << m_type << " map"; |
| 96 |
m_errorHandler(errmsg); |
| 97 |
} |
| 98 |
m_idsAssigned = true; |
| 99 |
} |
| 100 |
|
| 101 |
size_t size() const { return m_groupDataVec.size(); } |
| 102 |
|
| 103 |
const std::vector<GroupData> &get_group_data() const { return m_groupDataVec; } |
| 104 |
|
| 105 |
const std::vector<std::string> &get_part_names() const { return m_partNames; } |
| 106 |
|
| 107 |
const std::string &get_group_type() const { return m_type; } |
| 108 |
|
| 109 |
const GroupData *get_group_data(unsigned id) const |
| 110 |
{ |
| 111 |
if (is_assigned(id)) { |
| 112 |
auto iter = m_parts.find(id); |
| 113 |
return &m_groupDataVec[m_groupDataMap[iter->second]]; |
| 114 |
} |
| 115 |
|
| 116 |
return nullptr; |
| 117 |
} |
| 118 |
|
| 119 |
const GroupData *get_group_data(std::string name) const |
| 120 |
{ |
| 121 |
convert_to_upper_case(name); |
| 122 |
if (is_registered(name)) { |
| 123 |
return &m_groupDataVec[m_groupDataMap[name]]; |
| 124 |
} |
| 125 |
|
| 126 |
return nullptr; |
| 127 |
} |
| 128 |
|
| 129 |
bool is_registered(const std::string &name) const { return m_ids.count(name) > 0; } |
| 130 |
|
| 131 |
protected: |
| 132 |
EntityGroup(); |
| 133 |
|
| 134 |
unsigned get_unassigned_id() const |
| 135 |
{ |
| 136 |
unsigned nextPartId = 1; |
| 137 |
while (is_assigned(nextPartId)) |
| 138 |
nextPartId++; |
| 139 |
return nextPartId; |
| 140 |
} |
| 141 |
|
| 142 |
void validate_group_meta_data(const GroupData &groupData) |
| 143 |
{ |
| 144 |
if (!groupData.has_name()) { |
| 145 |
std::ostringstream errmsg; |
| 146 |
errmsg << m_type << " has no name"; |
| 147 |
m_errorHandler(errmsg); |
| 148 |
} |
| 149 |
|
| 150 |
if (!groupData.has_valid_id()) { |
| 151 |
std::ostringstream errmsg; |
| 152 |
errmsg << m_type << " named " << groupData.name << " has invalid id"; |
| 153 |
m_errorHandler(errmsg); |
| 154 |
} |
| 155 |
|
| 156 |
if (is_registered(groupData.name)) { |
| 157 |
std::ostringstream errmsg; |
| 158 |
errmsg << "Multiple declarations of " << m_type << ": " << groupData.name; |
| 159 |
m_errorHandler(errmsg); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
void assign(size_t index) |
| 164 |
{ |
| 165 |
GroupData &groupData = m_groupDataVec[index]; |
| 166 |
|
| 167 |
convert_to_upper_case(groupData.name); |
| 168 |
validate_group_meta_data(groupData); |
| 169 |
|
| 170 |
m_partNames.push_back(groupData.name); |
| 171 |
m_ids[groupData.name] = groupData.id; |
| 172 |
m_parts[groupData.id] = groupData.name; |
| 173 |
m_groupDataMap[groupData.name] = index; |
| 174 |
} |
| 175 |
|
| 176 |
void assign_id_from_default_exodus_name() |
| 177 |
{ |
| 178 |
for (size_t i = 0; i < m_groupDataVec.size(); i++) { |
| 179 |
GroupData &groupData = m_groupDataVec[i]; |
| 180 |
if (groupData.has_name()) { |
| 181 |
std::pair<unsigned, bool> result = |
| 182 |
get_id_from_part_name(groupData.name, m_exodusPrefix); |
| 183 |
|
| 184 |
if (result.second) { |
| 185 |
groupData.id = result.first; |
| 186 |
assign(i); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
void assign_id_and_name_for_empty_name() |
| 193 |
{ |
| 194 |
for (size_t i = 0; i < m_groupDataVec.size(); i++) { |
| 195 |
GroupData &groupData = m_groupDataVec[i]; |
| 196 |
if (!groupData.has_name()) { |
| 197 |
unsigned id = get_unassigned_id(); |
| 198 |
|
| 199 |
std::ostringstream oss; |
| 200 |
oss << m_exodusPrefix; |
| 201 |
oss << id; |
| 202 |
std::string name = oss.str(); |
| 203 |
|
| 204 |
groupData.id = id; |
| 205 |
groupData.name = name; |
| 206 |
assign(i); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void assign_id_for_non_default_exodus_name() |
| 212 |
{ |
| 213 |
for (size_t i = 0; i < m_groupDataVec.size(); i++) { |
| 214 |
GroupData &groupData = m_groupDataVec[i]; |
| 215 |
if (groupData.has_name()) { |
| 216 |
std::pair<unsigned, bool> result = |
| 217 |
get_id_from_part_name(groupData.name, m_exodusPrefix); |
| 218 |
|
| 219 |
if (!result.second) { |
| 220 |
groupData.id = get_unassigned_id(); |
| 221 |
assign(i); |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
bool is_assigned(unsigned id) const { return m_parts.count(id) > 0; } |
| 228 |
|
| 229 |
void verify_name(const std::string &name) |
| 230 |
{ |
| 231 |
for (const std::string &invalidPrefix : m_invalidPrefixes) { |
| 232 |
const unsigned prefixLength = invalidPrefix.length(); |
| 233 |
const std::string namePrefix = name.substr(0, prefixLength); |
| 234 |
|
| 235 |
if (strcasecmp(namePrefix.c_str(), invalidPrefix.c_str()) == 0) { |
| 236 |
std::ostringstream errmsg; |
| 237 |
errmsg << "Invalid name '" << name << "' for a " << m_type << " part"; |
| 238 |
m_errorHandler(errmsg); |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
std::vector<std::string> m_partNames{}; |
| 244 |
mutable std::unordered_map<std::string, unsigned> m_ids; |
| 245 |
mutable std::unordered_map<unsigned, std::string> m_parts; |
| 246 |
mutable bool m_idsAssigned{false}; |
| 247 |
mutable std::unordered_map<std::string, size_t> m_groupDataMap; |
| 248 |
|
| 249 |
std::string m_type{}; |
| 250 |
std::string m_exodusPrefix{}; |
| 251 |
std::vector<std::string> m_invalidPrefixes{}; |
| 252 |
std::vector<GroupData> m_groupDataVec{}; |
| 253 |
|
| 254 |
ErrorHandler m_errorHandler; |
| 255 |
}; |
| 256 |
|
| 257 |
} // namespace text_mesh |
| 258 |
} // namespace Iotm |