From 97c6b24b8d0ecde73b68fbc8001c9b920b4518c3 Mon Sep 17 00:00:00 2001 From: Thomas Fischer <thomas.fischer@ufz.de> Date: Fri, 9 Jul 2021 16:25:21 +0200 Subject: [PATCH] [MeL/Mesh] Make setNodesConnectedByElements a free fct. Rename setNodesConnectedByElements to calculateNodesConnectedByElements and make it a free function. This 'out sourcing' makes it possible to compute the connection information only when it is required. --- MeshLib/Mesh.cpp | 73 ++++++++++++++++++++++++------------------------ MeshLib/Mesh.h | 8 +++--- 2 files changed, 41 insertions(+), 40 deletions(-) diff --git a/MeshLib/Mesh.cpp b/MeshLib/Mesh.cpp index 6cf57cec18e..9e81ba0cee5 100644 --- a/MeshLib/Mesh.cpp +++ b/MeshLib/Mesh.cpp @@ -62,7 +62,6 @@ Mesh::Mesh(std::string name, } this->setDimension(); this->setElementsConnectedToNodes(); - this->setNodesConnectedByElements(); this->setElementNeighbors(); this->calcEdgeLengthRange(); @@ -103,7 +102,6 @@ Mesh::Mesh(const Mesh& mesh) this->setDimension(); } this->setElementsConnectedToNodes(); - // this->setNodesConnectedByElements(); this->setElementNeighbors(); } @@ -236,40 +234,6 @@ void Mesh::setElementNeighbors() } } -void Mesh::setNodesConnectedByElements() -{ - // Allocate temporary space for adjacent nodes. - std::vector<Node*> adjacent_nodes; - for (Node* const node : _nodes) - { - adjacent_nodes.clear(); - - // Get all elements, to which this node is connected. - std::vector<Element*> const& conn_elems = node->getElements(); - - // And collect all elements' nodes. - for (Element const* const element : conn_elems) - { - Node* const* const single_elem_nodes = element->getNodes(); - std::size_t const nnodes = element->getNumberOfNodes(); - for (std::size_t n = 0; n < nnodes; n++) - { - adjacent_nodes.push_back(single_elem_nodes[n]); - } - } - - // Make nodes unique and sorted by their ids. - // This relies on the node's id being equivalent to it's address. - std::sort(adjacent_nodes.begin(), adjacent_nodes.end(), - [](Node* a, Node* b) { return a->getID() < b->getID(); }); - auto const last = - std::unique(adjacent_nodes.begin(), adjacent_nodes.end()); - adjacent_nodes.erase(last, adjacent_nodes.end()); - - node->setConnectedNodes(adjacent_nodes); - } -} - void Mesh::checkNonlinearNodeIDs() const { for (MeshLib::Element const* e : _elements) @@ -386,4 +350,41 @@ std::unique_ptr<MeshLib::Mesh> createMeshFromElementSelection( return mesh; } + +std::vector<std::vector<Node*>> calculateNodesConnectedByElements( + Mesh const& mesh) +{ + std::vector<std::vector<Node*>> nodes_connected_by_elements; + auto const& nodes = mesh.getNodes(); + nodes_connected_by_elements.resize(nodes.size()); + for (std::size_t i = 0; i < nodes.size(); ++i) + { + auto& adjacent_nodes = nodes_connected_by_elements[i]; + auto const* node = nodes[i]; + + // Get all elements, to which this node is connected. + auto const& connected_elements = node->getElements(); + + // And collect all elements' nodes. + for (Element const* const element : connected_elements) + { + Node* const* const single_elem_nodes = element->getNodes(); + std::size_t const nnodes = element->getNumberOfNodes(); + for (std::size_t n = 0; n < nnodes; n++) + { + adjacent_nodes.push_back(single_elem_nodes[n]); + } + } + + // Make nodes unique and sorted by their ids. + // This relies on the node's id being equivalent to it's address. + std::sort(adjacent_nodes.begin(), adjacent_nodes.end(), + [](Node* a, Node* b) { return a->getID() < b->getID(); }); + auto const last = + std::unique(adjacent_nodes.begin(), adjacent_nodes.end()); + adjacent_nodes.erase(last, adjacent_nodes.end()); + } + return nodes_connected_by_elements; +} + } // namespace MeshLib diff --git a/MeshLib/Mesh.h b/MeshLib/Mesh.h index 2056afbe4da..0cca934b120 100644 --- a/MeshLib/Mesh.h +++ b/MeshLib/Mesh.h @@ -150,10 +150,6 @@ protected: /// Note: Using this implementation, an element e can only have neighbors that have the same dimensionality as e. void setElementNeighbors(); - /// Computes the element-connectivity of nodes. Two nodes i and j are - /// connected if they are shared by an element. - void setNodesConnectedByElements(); - /// Check if all the nonlinear nodes are stored at the end of the node vector void checkNonlinearNodeIDs() const; @@ -177,6 +173,10 @@ protected: bool _is_axially_symmetric = false; }; /* class */ +/// Computes the element-connectivity of nodes. Two nodes i and j are +/// connected if they are shared by an element. +std::vector<std::vector<Node*>> calculateNodesConnectedByElements( + Mesh const& mesh); /// Meshes are equal if their id's are equal. inline bool operator==(Mesh const& a, Mesh const& b) -- GitLab