From 915925d1d0c328b2e8e43516b0d62a0758f061e2 Mon Sep 17 00:00:00 2001 From: meng5113 Date: Sat, 5 Oct 2024 16:57:08 +0800 Subject: [PATCH] moved long function definitions out of Gmsh class declaration and header file --- cpp/modmesh/inout/gmsh.cpp | 153 ++++++++++++++++++++++++++++++++++++ cpp/modmesh/inout/gmsh.hpp | 157 ++----------------------------------- 2 files changed, 158 insertions(+), 152 deletions(-) diff --git a/cpp/modmesh/inout/gmsh.cpp b/cpp/modmesh/inout/gmsh.cpp index 34838f4f..fc24ffeb 100644 --- a/cpp/modmesh/inout/gmsh.cpp +++ b/cpp/modmesh/inout/gmsh.cpp @@ -94,6 +94,159 @@ void Gmsh::build_interior(const std::shared_ptr & blk) blk->build_boundary(); blk->build_ghost(); } + +// Check the finite state machine state transition is valid or not to check msh file format is correct +bool Gmsh::is_valid_transition(const std::string s) +{ + if (last_fmt_state == FormatState::BEGIN) + { + return s == "$MeshFormat"; + } + else if (last_fmt_state == FormatState::META_END || last_fmt_state == FormatState::PYHSICAL_NAME_END) + { + return s == "$PhysicalNames" || s == "$Nodes"; + } + else if (last_fmt_state == FormatState::NODE_END) + { + return s == "$Elements"; + } + + return false; +} + +void Gmsh::load_meta(void) +{ + std::string line; + + while (std::getline(stream, line) && line.find('$') == std::string::npos) + { + auto tokens = tokenize(line, ' '); + msh_ver = std::stod(tokens[0]); + + // The parse only support ver 2.2 msh file. + if (msh_ver != 2.2) + { + throw std::invalid_argument(Formatter() << "modmesh does not support msh file ver " << msh_ver << "."); + } + + msh_file_type = std::stoi(tokens[1]); + msh_data_size = std::stoi(tokens[2]); + } + + if (!line.compare("$EndMeshFormat")) + { + last_fmt_state = FormatState::META_END; + } +} + +// TODO: PhysicalNames section parsing logic not complete yet, but without PhysicalNames section +// modmesh mesh viewer still working, therefore we can finish this later. +void Gmsh::load_physical(void) +{ + std::string line; + + while (std::getline(stream, line) && line.find('$') == std::string::npos) {} + + if (!line.compare("$EndPhysicalNames")) + { + last_fmt_state = FormatState::PYHSICAL_NAME_END; + } +} + +void Gmsh::load_nodes(void) +{ + std::string line; + std::getline(stream, line); + auto nnode = std::stoul(line); + + m_nds.remake(small_vector{nnode, 3}, 0); + + while (std::getline(stream, line) && line.find('$') == std::string::npos) + { + auto tokens = tokenize(line, ' '); + // gmsh node index is 1-based index + m_nds(std::stoul(tokens[0]) - 1, 0) = std::stod(tokens[1]); + m_nds(std::stoul(tokens[0]) - 1, 1) = std::stod(tokens[2]); + m_nds(std::stoul(tokens[0]) - 1, 2) = std::stod(tokens[3]); + } + + if (!line.compare("$EndNodes")) + { + last_fmt_state = FormatState::NODE_END; + } +} + +void Gmsh::load_elements(void) +{ + std::string line; + std::getline(stream, line); + auto nelement = std::stoul(line); + std::vector usnds; + + m_cltpn.remake(small_vector{nelement}, 0); + m_elgrp.remake(small_vector{nelement}, 0); + m_elgeo.remake(small_vector{nelement}, 0); + m_eldim.remake(small_vector{nelement}, 0); + + uint_type idx = 0; + + while (std::getline(stream, line) && line.find('$') == std::string::npos && idx < nelement) + { + auto tokens = tokenize(line, ' '); + + // parse element type + auto tpn = std::stoul(tokens[1]); + auto eldef = GmshElementDef::by_id(tpn); + // parse element tag + std::vector tag; + for (size_t i = 0; i < std::stoul(tokens[2]); ++i) + { + tag.push_back(std::stoul(tokens[3 + i])); + } + + // parse node number list + std::vector nds; + for (size_t i = 3 + std::stoul(tokens[2]); i < tokens.size(); ++i) + { + nds.push_back(std::stoul(tokens[i])); + } + + m_cltpn[idx] = eldef.mmtpn(); + m_elgrp[idx] = tag[0]; + m_elgeo[idx] = tag[1]; + m_eldim[idx] = eldef.ndim(); + + small_vector nds_temp(nds.size() + 1, 0); + auto mmcl = eldef.mmcl(); + + for (size_t i = 0; i < mmcl.size(); ++i) + { + nds_temp[mmcl[i] + 1] = nds[i] - 1; + } + usnds.insert(usnds.end(), nds_temp.begin() + 1, nds_temp.end()); + nds_temp[0] = mmcl.size(); + m_elems.insert(std::pair{idx, nds_temp}); + idx++; + } + + if (!line.compare("$EndElements")) + { + last_fmt_state = FormatState::ELEMENT_END; + } + + // sorting used node and remove duplicate node id + std::sort(usnds.begin(), usnds.end()); + auto remove = std::unique(usnds.begin(), usnds.end()); + usnds.erase(remove, usnds.end()); + + // put used node id to m_ndmap + m_ndmap.remake(small_vector{usnds.size()}, -1); + for (size_t i = 0; i < usnds.size(); ++i) + { + m_ndmap(usnds[i]) = i; + } +} + } /* end namespace inout */ } /* end namespace modmesh */ diff --git a/cpp/modmesh/inout/gmsh.hpp b/cpp/modmesh/inout/gmsh.hpp index 3aac2ae2..c2e24620 100644 --- a/cpp/modmesh/inout/gmsh.hpp +++ b/cpp/modmesh/inout/gmsh.hpp @@ -78,158 +78,11 @@ class Gmsh ELEMENT_END }; - // Check the finite state machine state transition is valid or not to check msh file format is correct - bool is_valid_transition(const std::string s) - { - if (last_fmt_state == FormatState::BEGIN) - { - return s == "$MeshFormat"; - } - else if (last_fmt_state == FormatState::META_END || last_fmt_state == FormatState::PYHSICAL_NAME_END) - { - return s == "$PhysicalNames" || s == "$Nodes"; - } - else if (last_fmt_state == FormatState::NODE_END) - { - return s == "$Elements"; - } - - return false; - } - - void load_meta(void) - { - std::string line; - - while (std::getline(stream, line) && line.find('$') == std::string::npos) - { - auto tokens = tokenize(line, ' '); - msh_ver = std::stod(tokens[0]); - - // The parse only support ver 2.2 msh file. - if (msh_ver != 2.2) - { - throw std::invalid_argument(Formatter() << "modmesh does not support msh file ver " << msh_ver << "."); - } - - msh_file_type = std::stoi(tokens[1]); - msh_data_size = std::stoi(tokens[2]); - } - - if (!line.compare("$EndMeshFormat")) - { - last_fmt_state = FormatState::META_END; - } - } - - // TODO: PhysicalNames section parsing logic not complete yet, but without PhysicalNames section - // modmesh mesh viewer still working, therefore we can finish this later. - void load_physical(void) - { - std::string line; - - while (std::getline(stream, line) && line.find('$') == std::string::npos) {} - - if (!line.compare("$EndPhysicalNames")) - { - last_fmt_state = FormatState::PYHSICAL_NAME_END; - } - } - - void load_nodes(void) - { - std::string line; - std::getline(stream, line); - auto nnode = std::stoul(line); - - m_nds.remake(small_vector{nnode, 3}, 0); - - while (std::getline(stream, line) && line.find('$') == std::string::npos) - { - auto tokens = tokenize(line, ' '); - // gmsh node index is 1-based index - m_nds(std::stoul(tokens[0]) - 1, 0) = std::stod(tokens[1]); - m_nds(std::stoul(tokens[0]) - 1, 1) = std::stod(tokens[2]); - m_nds(std::stoul(tokens[0]) - 1, 2) = std::stod(tokens[3]); - } - - if (!line.compare("$EndNodes")) - { - last_fmt_state = FormatState::NODE_END; - } - } - - void load_elements(void) - { - std::string line; - std::getline(stream, line); - auto nelement = std::stoul(line); - std::vector usnds; - - m_cltpn.remake(small_vector{nelement}, 0); - m_elgrp.remake(small_vector{nelement}, 0); - m_elgeo.remake(small_vector{nelement}, 0); - m_eldim.remake(small_vector{nelement}, 0); - - uint_type idx = 0; - - while (std::getline(stream, line) && line.find('$') == std::string::npos && idx < nelement) - { - auto tokens = tokenize(line, ' '); - - // parse element type - auto tpn = std::stoul(tokens[1]); - auto eldef = GmshElementDef::by_id(tpn); - // parse element tag - std::vector tag; - for (size_t i = 0; i < std::stoul(tokens[2]); ++i) - { - tag.push_back(std::stoul(tokens[3 + i])); - } - - // parse node number list - std::vector nds; - for (size_t i = 3 + std::stoul(tokens[2]); i < tokens.size(); ++i) - { - nds.push_back(std::stoul(tokens[i])); - } - - m_cltpn[idx] = eldef.mmtpn(); - m_elgrp[idx] = tag[0]; - m_elgeo[idx] = tag[1]; - m_eldim[idx] = eldef.ndim(); - - small_vector nds_temp(nds.size() + 1, 0); - auto mmcl = eldef.mmcl(); - - for (size_t i = 0; i < mmcl.size(); ++i) - { - nds_temp[mmcl[i] + 1] = nds[i] - 1; - } - usnds.insert(usnds.end(), nds_temp.begin() + 1, nds_temp.end()); - nds_temp[0] = mmcl.size(); - m_elems.insert(std::pair{idx, nds_temp}); - idx++; - } - - if (!line.compare("$EndElements")) - { - last_fmt_state = FormatState::ELEMENT_END; - } - - // sorting used node and remove duplicate node id - std::sort(usnds.begin(), usnds.end()); - auto remove = std::unique(usnds.begin(), usnds.end()); - usnds.erase(remove, usnds.end()); - - // put used node id to m_ndmap - m_ndmap.remake(small_vector{usnds.size()}, -1); - for (size_t i = 0; i < usnds.size(); ++i) - { - m_ndmap(usnds[i]) = i; - } - } - + bool is_valid_transition(const std::string s); + void load_meta(void); + void load_physical(void); + void load_nodes(void); + void load_elements(void); void build_interior(const std::shared_ptr & blk); std::stringstream stream;