| 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 |
#include "iotm_export.h" |
| 8 |
|
| 9 |
// ####################### Start Clang Header Tool Managed Headers ######################## |
| 10 |
// clang-format off |
| 11 |
#include <ctype.h> // for toupper |
| 12 |
#include <stddef.h> // for size_t |
| 13 |
#include <algorithm> // for remove, etc |
| 14 |
#include <iterator> // for insert_iterator |
| 15 |
#include <map> |
| 16 |
#include <set> // for set |
| 17 |
#include <sstream> // for operator<<, etc |
| 18 |
#include <string> // for basic_string, etc |
| 19 |
#include <utility> // for pair |
| 20 |
#include <vector> // for vector |
| 21 |
#include <unordered_map> |
| 22 |
#include <sstream> // for ostringstream |
| 23 |
#include <iostream> |
| 24 |
#include <functional> |
| 25 |
#include <stdexcept> |
| 26 |
#include <numeric> |
| 27 |
#include <strings.h> |
| 28 |
|
| 29 |
#include "Iotm_TextMeshFuncs.h" |
| 30 |
#include "Iotm_TextMeshEntityGroup.h" |
| 31 |
|
| 32 |
// clang-format on |
| 33 |
// ####################### End Clang Header Tool Managed Headers ######################## |
| 34 |
namespace Iotm { |
| 35 |
namespace text_mesh { |
| 36 |
|
| 37 |
using ErrorHandler = std::function<void(const std::ostringstream &)>; |
| 38 |
|
| 39 |
enum AssemblyType { ASSEMBLY, BLOCK, SIDESET, NODESET, INVALID_ASSEMBLY }; |
| 40 |
|
| 41 |
inline std::ostream &operator<<(std::ostream &out, const AssemblyType &t) |
| 42 |
{ |
| 43 |
switch (t) { |
| 44 |
case AssemblyType::ASSEMBLY: return out << "ASSEMBLY"; break; |
| 45 |
case AssemblyType::BLOCK: return out << "ELEMENT_BLOCK"; break; |
| 46 |
case AssemblyType::SIDESET: return out << "SIDESET"; break; |
| 47 |
case AssemblyType::NODESET: return out << "NODESET"; break; |
| 48 |
default: return out << "INVALID"; break; |
| 49 |
} |
| 50 |
return out << "INVALID[" << (unsigned)t << "]"; |
| 51 |
} |
| 52 |
|
| 53 |
using AssemblyDataType = std::string; |
| 54 |
|
| 55 |
struct IOTM_EXPORT AssemblyData : public EntityGroupData<AssemblyDataType> |
| 56 |
{ |
| 57 |
using DataType = AssemblyDataType; |
| 58 |
|
| 59 |
void set_assembly_type(AssemblyType type_) { assemblyType = type_; } |
| 60 |
AssemblyType get_assembly_type() const { return assemblyType; } |
| 61 |
|
| 62 |
private: |
| 63 |
AssemblyType assemblyType = INVALID_ASSEMBLY; |
| 64 |
}; |
| 65 |
|
| 66 |
template <typename EntityId> class Assemblies : public EntityGroup<AssemblyData> |
| 67 |
{ |
| 68 |
public: |
| 69 |
using BaseClass = EntityGroup<AssemblyData>; |
| 70 |
|
| 71 |
Assemblies() |
| 72 |
: EntityGroup<AssemblyData>("ASSEMBLY", "ASSEMBLY_", {"BLOCK_", "SURFACE_", "NODELIST_"}) |
| 73 |
{ |
| 74 |
} |
| 75 |
|
| 76 |
bool is_cyclic(const std::string &assembly) const |
| 77 |
{ |
| 78 |
initialize_graph(); |
| 79 |
return check_for_cycle(assembly); |
| 80 |
} |
| 81 |
|
| 82 |
bool is_cyclic() const |
| 83 |
{ |
| 84 |
for (const std::string &assembly : BaseClass::get_part_names()) |
| 85 |
if (is_cyclic(assembly)) { |
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
std::vector<std::string> &&get_forward_traversal_list(const std::string &assembly) const |
| 93 |
{ |
| 94 |
initialize_graph(); |
| 95 |
fill_traversal(assembly); |
| 96 |
|
| 97 |
return std::move(m_traversalList); |
| 98 |
} |
| 99 |
|
| 100 |
std::vector<std::string> &&get_reverse_traversal_list(const std::string &assembly) const |
| 101 |
{ |
| 102 |
initialize_graph(); |
| 103 |
fill_traversal(assembly); |
| 104 |
std::reverse(m_traversalList.begin(), m_traversalList.end()); |
| 105 |
|
| 106 |
return std::move(m_traversalList); |
| 107 |
} |
| 108 |
|
| 109 |
private: |
| 110 |
// Walk the tree without cyclic dependency |
| 111 |
void fill_traversal(const std::string &assembly) const |
| 112 |
{ |
| 113 |
const AssemblyData *assemblyData = BaseClass::get_group_data(assembly); |
| 114 |
if (nullptr != assemblyData) { |
| 115 |
if (m_visitedNodes[assembly] == false) { |
| 116 |
m_visitedNodes[assembly] = true; |
| 117 |
m_traversalList.push_back(assembly); |
| 118 |
|
| 119 |
if (assemblyData->get_assembly_type() == AssemblyType::ASSEMBLY) { |
| 120 |
for (const std::string &member : assemblyData->data) { |
| 121 |
fill_traversal(member); |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
bool check_for_cycle(const std::string &assembly) const |
| 129 |
{ |
| 130 |
bool isCyclic = false; |
| 131 |
const AssemblyData *assemblyData = BaseClass::get_group_data(assembly); |
| 132 |
if (nullptr != assemblyData) { |
| 133 |
if (m_visitedNodes[assembly] == true) { |
| 134 |
isCyclic = true; |
| 135 |
} |
| 136 |
else { |
| 137 |
m_visitedNodes[assembly] = true; |
| 138 |
|
| 139 |
if (assemblyData->get_assembly_type() == AssemblyType::ASSEMBLY) { |
| 140 |
for (const std::string &member : assemblyData->data) { |
| 141 |
isCyclic |= check_for_cycle(member); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
return isCyclic; |
| 147 |
} |
| 148 |
|
| 149 |
void initialize_graph() const |
| 150 |
{ |
| 151 |
m_traversalList.clear(); |
| 152 |
m_traversalList.reserve(BaseClass::size()); |
| 153 |
|
| 154 |
for (const std::string &name : BaseClass::get_part_names()) { |
| 155 |
m_visitedNodes[name] = false; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
mutable std::unordered_map<std::string, bool> m_visitedNodes; |
| 160 |
mutable std::vector<std::string> m_traversalList; |
| 161 |
}; |
| 162 |
|
| 163 |
class IOTM_EXPORT AssemblyParser |
| 164 |
{ |
| 165 |
public: |
| 166 |
AssemblyParser() : m_assemblyType(INVALID_ASSEMBLY) |
| 167 |
{ |
| 168 |
ErrorHandler errorHandler = [](const std::ostringstream &errmsg) { |
| 169 |
default_error_handler(errmsg); |
| 170 |
}; |
| 171 |
set_error_handler(errorHandler); |
| 172 |
} |
| 173 |
|
| 174 |
void set_error_handler(ErrorHandler errorHandler) { m_errorHandler = errorHandler; } |
| 175 |
|
| 176 |
std::string get_name() { return m_name; } |
| 177 |
|
| 178 |
AssemblyType get_assembly_type() const { return m_assemblyType; } |
| 179 |
|
| 180 |
const std::vector<std::string> &get_assembly_data() { return m_members; } |
| 181 |
|
| 182 |
// Expected format for assembly string data is: |
| 183 |
// "name=<name>; type=<assembly|block|sideset|nodeset>; member=member_1,...,member_n;" |
| 184 |
// Only type keyword is required |
| 185 |
void parse(const std::string &parseData) |
| 186 |
{ |
| 187 |
auto options = get_tokens(parseData, ";"); |
| 188 |
|
| 189 |
for (const auto &option : options) { |
| 190 |
parse_option_group(option); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void verify_parse() const |
| 195 |
{ |
| 196 |
if (m_assemblyType == INVALID_ASSEMBLY) { |
| 197 |
std::ostringstream errmsg; |
| 198 |
errmsg << "Error! Assembly with name: " << m_name << " does not have a defined type."; |
| 199 |
m_errorHandler(errmsg); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
private: |
| 204 |
void parse_option(std::string optionName, const std::string &optionValue) |
| 205 |
{ |
| 206 |
convert_to_lower_case(optionName); |
| 207 |
|
| 208 |
if (optionName == "name") { |
| 209 |
parse_name(optionValue); |
| 210 |
} |
| 211 |
else if (optionName == "type") { |
| 212 |
parse_assembly_type(optionValue); |
| 213 |
} |
| 214 |
else if (optionName == "member") { |
| 215 |
parse_assembly_members(optionValue); |
| 216 |
} |
| 217 |
else { |
| 218 |
std::ostringstream errmsg; |
| 219 |
errmsg << "Unrecognized assembly option: " << optionName; |
| 220 |
m_errorHandler(errmsg); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
void parse_option_group(const std::string &option) |
| 225 |
{ |
| 226 |
if (!option.empty()) { |
| 227 |
auto optionTokens = get_tokens(option, "="); |
| 228 |
|
| 229 |
if (optionTokens.size() != 2) { |
| 230 |
std::ostringstream errmsg; |
| 231 |
errmsg << "Unrecognized assembly option: " << option; |
| 232 |
m_errorHandler(errmsg); |
| 233 |
} |
| 234 |
|
| 235 |
parse_option(optionTokens[0], optionTokens[1]); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
void parse_name(const std::string &data) { m_name = data; } |
| 240 |
|
| 241 |
void parse_assembly_type(std::string type) |
| 242 |
{ |
| 243 |
convert_to_lower_case(type); |
| 244 |
|
| 245 |
if (type == "assembly") { |
| 246 |
m_assemblyType = ASSEMBLY; |
| 247 |
} |
| 248 |
else if (type == "block") { |
| 249 |
m_assemblyType = BLOCK; |
| 250 |
} |
| 251 |
else if (type == "sideset") { |
| 252 |
m_assemblyType = SIDESET; |
| 253 |
} |
| 254 |
else if (type == "nodeset") { |
| 255 |
m_assemblyType = NODESET; |
| 256 |
} |
| 257 |
else { |
| 258 |
std::ostringstream errmsg; |
| 259 |
errmsg << "Unrecognized assembly type: " << type; |
| 260 |
m_errorHandler(errmsg); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
void parse_assembly_members(const std::string &data) |
| 265 |
{ |
| 266 |
std::vector<std::string> assemblyData = get_tokens(data, ","); |
| 267 |
for (std::string &member : assemblyData) { |
| 268 |
convert_to_upper_case(member); |
| 269 |
} |
| 270 |
|
| 271 |
m_members = assemblyData; |
| 272 |
} |
| 273 |
|
| 274 |
std::vector<std::string> m_members{}; |
| 275 |
std::string m_name{}; |
| 276 |
AssemblyType m_assemblyType{INVALID_ASSEMBLY}; |
| 277 |
ErrorHandler m_errorHandler; |
| 278 |
}; |
| 279 |
|
| 280 |
} // namespace text_mesh |
| 281 |
} // namespace Iotm |