Chaste Commit::8b5d759ac2eb95e67ae57699734101efccb0a0a9
EdgeHelper.cpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "EdgeHelper.hpp"
37
38template<unsigned SPACE_DIM>
42
43template<unsigned SPACE_DIM>
45 Node<SPACE_DIM>* pNodeB)
46{
47 auto edge_map_indices = Edge<SPACE_DIM>::GenerateMapIndex(pNodeA->GetIndex(), pNodeB->GetIndex());
48
49 // Check that an edge hasn't been created already
50 Edge<SPACE_DIM>* p_edge = nullptr;
51
52 auto edge_iter = mEdgesMap.find(edge_map_indices);
53 if (edge_iter == mEdgesMap.end() || edge_iter->second->IsDeleted())
54 {
55 mEdges.push_back(std::make_unique<Edge<SPACE_DIM>> (mEdges.size(), pNodeA, pNodeB));
56 p_edge = mEdges.back().get();
57 mEdgesMap[edge_map_indices] = p_edge;
58 }
59 else
60 {
61 p_edge = edge_iter->second;
62 p_edge->SetNodes(pNodeA, pNodeB);
63 }
64
65 return p_edge;
66}
67
68template<unsigned SPACE_DIM>
71 Node<SPACE_DIM>* pNodeA,
72 Node<SPACE_DIM>* pNodeB)
73{
74 auto p_edge = GetEdgeFromNodes(pNodeA, pNodeB);
75 p_edge->AddElement(elementIndex);
76 return p_edge;
77}
78
79template<unsigned SPACE_DIM>
81{
82 assert(index < mEdges.size());
83 return mEdges[index].get();
84}
85
86template<unsigned SPACE_DIM>
88{
89 assert(index < mEdges.size());
90 return mEdges[index].get();
91}
92
93template<unsigned SPACE_DIM>
95{
96 /*
97 * Remove any edges that have been marked for deletion and store all other
98 * nodes in a temporary structure.
99 */
100 std::vector<std::unique_ptr<Edge<SPACE_DIM> > > live_edges;
101
102 for (auto& p_edge : mEdges)
103 {
104 // An edge can be deleted if it is not contained in any elements
105 if (p_edge->GetNumElements() != 0)
106 {
107 live_edges.push_back(std::move(p_edge));
108 }
109 /*
110 * Note: there's no need to manually delete the edge, because the
111 * unique_ptr automatically takes care of that when it goes out of scope.
112 */
113 }
114
115 /*
116 * Repopulate the edges vector, using std::move to efficiently transfer
117 * ownership of the pointers.
118 */
119 mEdges = std::move(live_edges);
120
121 // Reset the edge indices
122 for (unsigned i = 0; i < this->mEdges.size(); ++i)
123 {
124 mEdges[i]->SetIndex(i);
125 }
126
127 UpdateEdgesMapKey();
128}
129
130template<unsigned SPACE_DIM>
132{
133 mEdgesMap.clear();
134
135 for (auto& p_edge : mEdges)
136 {
137 mEdgesMap[p_edge->GetMapIndex()] = p_edge.get();
138 }
139}
140
141template<unsigned SPACE_DIM>
143{
144 return mEdges.size();
145}
146
147// Explicit instantiation
148template class EdgeHelper<1>;
149template class EdgeHelper<2>;
150template class EdgeHelper<3>;
unsigned GetNumEdges() const
Edge< SPACE_DIM > * operator[](unsigned index) const
void UpdateEdgesMapKey()
Edge< SPACE_DIM > * GetEdge(unsigned index) const
Edge< SPACE_DIM > * GetEdgeFromNodes(Node< SPACE_DIM > *pNodeA, Node< SPACE_DIM > *pNodeB)
void RemoveDeletedEdges()
Definition Edge.hpp:52
void SetNodes(Node< SPACE_DIM > *pNodeA, Node< SPACE_DIM > *pNodeB)
Definition Edge.cpp:102
static std::pair< unsigned, unsigned > GenerateMapIndex(unsigned index1, unsigned index2)
Definition Edge.cpp:56
Definition Node.hpp:59
unsigned GetIndex() const
Definition Node.cpp:158