Skip to content

Commit 5fadfb5

Browse files
authored
Merge pull request cb-geo#194 from cb-geo/map_remove
🏁 Remove elements from a map - fixes cb-geo#89
2 parents 284dd4f + d0d7f7d commit 5fadfb5

File tree

7 files changed

+100
-69
lines changed

7 files changed

+100
-69
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ SET(test_src
106106
${mpm_SOURCE_DIR}/tests/mpm_explicit_test.cc
107107
${mpm_SOURCE_DIR}/tests/mpm_explicit_test_unitcell.cc
108108
${mpm_SOURCE_DIR}/tests/node_container_test.cc
109-
${mpm_SOURCE_DIR}/tests/node_handler_test.cc
109+
${mpm_SOURCE_DIR}/tests/node_map_test.cc
110110
${mpm_SOURCE_DIR}/tests/node_test.cc
111111
${mpm_SOURCE_DIR}/tests/particle_container_test.cc
112112
${mpm_SOURCE_DIR}/tests/particle_test.cc

include/cell.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#include "Eigen/LU"
1010

1111
#include "affine_transform.h"
12-
#include "handler.h"
1312
#include "logger.h"
13+
#include "map.h"
1414
#include "node_base.h"
1515
#include "shapefn.h"
1616

@@ -235,10 +235,10 @@ class Cell {
235235
std::vector<Index> particles_;
236236

237237
//! Container of node pointers (local id, node pointer)
238-
Handler<NodeBase<Tdim>> nodes_;
238+
Map<NodeBase<Tdim>> nodes_;
239239

240240
//! Container of cell neighbours
241-
Handler<Cell<Tdim>> neighbour_cells_;
241+
Map<Cell<Tdim>> neighbour_cells_;
242242

243243
//! Shape function
244244
std::shared_ptr<const ShapeFn<Tdim>> shapefn_{nullptr};

include/handler.h renamed to include/map.h

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
#ifndef MPM_HANDLER_H_
2-
#define MPM_HANDLER_H_
1+
#ifndef MPM_MAP_H_
2+
#define MPM_MAP_H_
33

44
#include <algorithm>
55
#include <unordered_map>
@@ -9,14 +9,14 @@ namespace mpm {
99
// Global index type for the node
1010
using Index = unsigned long long;
1111

12-
// handler class
12+
// Map class
1313
//! \brief A class that offers a container and iterators
1414
//! \tparam T A class with a template argument Tdim
1515
template <class T>
16-
class Handler {
16+
class Map {
1717
public:
1818
//! Default constructor
19-
Handler<T>() = default;
19+
Map<T>() = default;
2020

2121
//! Insert a pointer
2222
//! \param[in] ptr A shared pointer
@@ -27,6 +27,10 @@ class Handler {
2727
//! \param[in] ptr A shared pointer
2828
bool insert(Index id, const std::shared_ptr<T>& ptr);
2929

30+
//! Remove a pointer at a given id
31+
//! \param[in] id Global/local index of the pointer
32+
bool remove(Index id);
33+
3034
//! Return number of elements in the container
3135
std::size_t size() const { return elements_.size(); }
3236

@@ -53,9 +57,9 @@ class Handler {
5357
private:
5458
// Unordered map of index and pointer
5559
std::unordered_map<Index, std::shared_ptr<T>> elements_;
56-
}; // Handler class
60+
}; // Map class
5761

58-
#include "handler.tcc"
62+
#include "map.tcc"
5963

6064
} // namespace mpm
61-
#endif // MPM_HANDLER_H_
65+
#endif // MPM_MAP_H_
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
//! Insert a pointer
22
template <class T>
3-
bool mpm::Handler<T>::insert(const std::shared_ptr<T>& ptr) {
3+
bool mpm::Map<T>::insert(const std::shared_ptr<T>& ptr) {
44
bool insertion_status =
55
elements_.insert(std::make_pair(ptr->id(), ptr)).second;
66
return insertion_status;
77
}
88

99
//! Insert a pointer at a given id
1010
template <class T>
11-
bool mpm::Handler<T>::insert(mpm::Index id, const std::shared_ptr<T>& ptr) {
11+
bool mpm::Map<T>::insert(mpm::Index id, const std::shared_ptr<T>& ptr) {
1212
bool insertion_status = elements_.insert(std::make_pair(id, ptr)).second;
1313
return insertion_status;
1414
}
1515

16+
//! Remove a pointer at a given id
17+
template <class T>
18+
bool mpm::Map<T>::remove(mpm::Index id) {
19+
return elements_.erase(id);
20+
}
21+
1622
//! Iterate over elements in the container
1723
template <class T>
1824
template <class Tunaryfn>
19-
Tunaryfn mpm::Handler<T>::for_each(Tunaryfn fn) {
25+
Tunaryfn mpm::Map<T>::for_each(Tunaryfn fn) {
2026
for (const auto& element : elements_) {
2127
fn((element).second);
2228
}

include/mesh.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ class Mesh {
196196
unsigned id_{std::numeric_limits<unsigned>::max()};
197197

198198
//! Container of mesh neighbours
199-
Handler<Mesh<Tdim>> neighbour_meshes_;
199+
Map<Mesh<Tdim>> neighbour_meshes_;
200200

201201
//! Container of particles
202202
Container<ParticleBase<Tdim>> particles_;
@@ -205,7 +205,7 @@ class Mesh {
205205
Container<NodeBase<Tdim>> nodes_;
206206

207207
//! Map of nodes for fast retrieval
208-
Handler<NodeBase<Tdim>> map_nodes_;
208+
Map<NodeBase<Tdim>> map_nodes_;
209209

210210
//! Container of cells
211211
Container<Cell<Tdim>> cells_;

include/mesh.tcc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ bool mpm::Mesh<Tdim>::add_node(
5454
template <unsigned Tdim>
5555
bool mpm::Mesh<Tdim>::remove_node(
5656
const std::shared_ptr<mpm::NodeBase<Tdim>>& node) {
57+
const mpm::Index id = node->id();
5758
// Remove a node if found in the container
58-
bool status = nodes_.remove(node);
59-
// TODO: Remove node from map_nodes_
60-
return status;
59+
return (nodes_.remove(node) && map_nodes_.remove(id));
6160
}
6261

6362
//! Iterate over nodes
Lines changed: 71 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
#include "Eigen/Dense"
77
#include "catch.hpp"
88

9-
#include "handler.h"
9+
#include "map.h"
1010
#include "node.h"
1111

12-
//! \brief Check node handler class for 2D case
13-
TEST_CASE("Node handler is checked for 2D case", "[nodehandler][2D]") {
12+
//! \brief Check node map class for 2D case
13+
TEST_CASE("Node map is checked for 2D case", "[nodemap][2D]") {
1414
// Dimension
1515
const unsigned Dim = 2;
1616
// Degrees of freedom
@@ -32,41 +32,52 @@ TEST_CASE("Node handler is checked for 2D case", "[nodehandler][2D]") {
3232
std::shared_ptr<mpm::NodeBase<Dim>> node2 =
3333
std::make_shared<mpm::Node<Dim, Dof, Nphases>>(id2, coords);
3434

35-
// Node handler
36-
auto nodehandler = std::make_shared<mpm::Handler<mpm::NodeBase<Dim>>>();
35+
// Node 3
36+
mpm::Index id3 = 2;
37+
std::shared_ptr<mpm::NodeBase<Dim>> node3 =
38+
std::make_shared<mpm::Node<Dim, Dof, Nphases>>(id3, coords);
3739

38-
// Check insert node
39-
SECTION("Check insert node functionality") {
40+
// Node map
41+
auto nodemap = std::make_shared<mpm::Map<mpm::NodeBase<Dim>>>();
42+
43+
// Check insert and remove node
44+
SECTION("Check insert and remove node functionality") {
4045
// Insert node 1 and check status
41-
bool status1 = nodehandler->insert(node1);
42-
REQUIRE(status1 == true);
46+
REQUIRE(nodemap->insert(node1) == true);
4347
// Insert node 2 and check status
44-
bool status2 = nodehandler->insert(node2->id(), node2);
45-
REQUIRE(status2 == true);
48+
REQUIRE(nodemap->insert(node2->id(), node2) == true);
49+
// Insert node 3 and check status
50+
REQUIRE(nodemap->insert(node3->id(), node3) == true);
4651
// Check size of node hanlder
47-
REQUIRE(nodehandler->size() == 2);
52+
REQUIRE(nodemap->size() == 3);
53+
// Remove node 3 and check status
54+
REQUIRE(nodemap->remove(node3->id()) == true);
55+
// Try to remove node 3 again
56+
REQUIRE(nodemap->remove(node3->id()) == false);
57+
// Check size of node hanlder
58+
REQUIRE(nodemap->size() == 2);
4859
}
4960

5061
SECTION("Check operator []") {
5162
// Insert node 1
52-
nodehandler->insert(id1, node1);
63+
nodemap->insert(id1, node1);
5364
// Insert node 2
54-
nodehandler->insert(id2, node2);
65+
nodemap->insert(id2, node2);
5566

5667
// Check operator []
57-
REQUIRE((*nodehandler)[0]->id() == id1);
58-
REQUIRE((*nodehandler)[1]->id() == id2);
68+
REQUIRE((*nodemap)[0]->id() == id1);
69+
REQUIRE((*nodemap)[1]->id() == id2);
5970
}
6071

6172
// Check iterator
6273
SECTION("Check node range iterator") {
6374
// Insert node 1
64-
nodehandler->insert(node1);
75+
nodemap->insert(node1);
6576
// Insert node 2
66-
nodehandler->insert(node2);
77+
nodemap->insert(node2);
6778
// Check size of node hanlder
6879
std::size_t counter = 0;
69-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
80+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
7081
auto coords = ((*itr).second)->coordinates();
7182
// Check if coordinates for each node is zero
7283
for (unsigned i = 0; i < coords.size(); ++i)
@@ -81,14 +92,14 @@ TEST_CASE("Node handler is checked for 2D case", "[nodehandler][2D]") {
8192
// Check for_each
8293
SECTION("Check node for_each") {
8394
// Insert node 1
84-
nodehandler->insert(node1);
95+
nodemap->insert(node1);
8596
// Insert node 2
86-
nodehandler->insert(node2);
97+
nodemap->insert(node2);
8798
// Check size of node hanlder
88-
REQUIRE(nodehandler->size() == 2);
99+
REQUIRE(nodemap->size() == 2);
89100

90101
// Check coordinates before updating
91-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
102+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
92103
auto coords = ((*itr).second)->coordinates();
93104
// Check if coordinates for each node is zero
94105
for (unsigned i = 0; i < coords.size(); ++i)
@@ -98,13 +109,13 @@ TEST_CASE("Node handler is checked for 2D case", "[nodehandler][2D]") {
98109
// Set coordinates to unity
99110
coords << 1., 1.;
100111

101-
// Iterate through node handler to update coordinaates
102-
nodehandler->for_each( // function structure
112+
// Iterate through node map to update coordinaates
113+
nodemap->for_each( // function structure
103114
std::bind(&mpm::NodeBase<Dim>::assign_coordinates,
104115
std::placeholders::_1, coords));
105116

106117
// Check if update has gone through
107-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
118+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
108119
auto coords = ((*itr).second)->coordinates();
109120
// Check if coordinates for each node is zero
110121
for (unsigned i = 0; i < coords.size(); ++i)
@@ -113,8 +124,8 @@ TEST_CASE("Node handler is checked for 2D case", "[nodehandler][2D]") {
113124
}
114125
}
115126

116-
//! \brief Check node handler class for 3D case
117-
TEST_CASE("Node handler is checked for 3D case", "[nodehandler][3D]") {
127+
//! \brief Check node map class for 3D case
128+
TEST_CASE("Node map is checked for 3D case", "[nodemap][3D]") {
118129
// Dimension
119130
const unsigned Dim = 3;
120131
// Degrees of freedom
@@ -136,41 +147,52 @@ TEST_CASE("Node handler is checked for 3D case", "[nodehandler][3D]") {
136147
std::shared_ptr<mpm::NodeBase<Dim>> node2 =
137148
std::make_shared<mpm::Node<Dim, Dof, Nphases>>(id2, coords);
138149

139-
// Node handler
140-
auto nodehandler = std::make_shared<mpm::Handler<mpm::NodeBase<Dim>>>();
150+
// Node 3
151+
mpm::Index id3 = 2;
152+
std::shared_ptr<mpm::NodeBase<Dim>> node3 =
153+
std::make_shared<mpm::Node<Dim, Dof, Nphases>>(id3, coords);
154+
155+
// Node map
156+
auto nodemap = std::make_shared<mpm::Map<mpm::NodeBase<Dim>>>();
141157

142158
// Check insert node
143159
SECTION("Check insert node functionality") {
144160
// Insert node 1 and check status
145-
bool status1 = nodehandler->insert(node1);
146-
REQUIRE(status1 == true);
161+
REQUIRE(nodemap->insert(node1) == true);
147162
// Insert node 2 and check status
148-
bool status2 = nodehandler->insert(node2->id(), node2);
149-
REQUIRE(status2 == true);
163+
REQUIRE(nodemap->insert(node2->id(), node2) == true);
164+
// Insert node 3 and check status
165+
REQUIRE(nodemap->insert(node3->id(), node3) == true);
166+
// Check size of node hanlder
167+
REQUIRE(nodemap->size() == 3);
168+
// Remove node 3 and check status
169+
REQUIRE(nodemap->remove(node3->id()) == true);
170+
// Try to remove node 3 again
171+
REQUIRE(nodemap->remove(node3->id()) == false);
150172
// Check size of node hanlder
151-
REQUIRE(nodehandler->size() == 2);
173+
REQUIRE(nodemap->size() == 2);
152174
}
153175

154176
SECTION("Check operator []") {
155177
// Insert node 1
156-
nodehandler->insert(id1, node1);
178+
nodemap->insert(id1, node1);
157179
// Insert node 2
158-
nodehandler->insert(id2, node2);
180+
nodemap->insert(id2, node2);
159181

160182
// Check operator []
161-
REQUIRE((*nodehandler)[0]->id() == id1);
162-
REQUIRE((*nodehandler)[1]->id() == id2);
183+
REQUIRE((*nodemap)[0]->id() == id1);
184+
REQUIRE((*nodemap)[1]->id() == id2);
163185
}
164186

165187
// Check iterator
166188
SECTION("Check node range iterator") {
167189
// Insert node 1
168-
nodehandler->insert(node1);
190+
nodemap->insert(node1);
169191
// Insert node 2
170-
nodehandler->insert(node2);
192+
nodemap->insert(node2);
171193
// Check size of node hanlder
172194
std::size_t counter = 0;
173-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
195+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
174196
auto coords = ((*itr).second)->coordinates();
175197
// Check if coordinates for each node is zero
176198
for (unsigned i = 0; i < coords.size(); ++i)
@@ -185,13 +207,13 @@ TEST_CASE("Node handler is checked for 3D case", "[nodehandler][3D]") {
185207
// Check for_each
186208
SECTION("Check node for_each") {
187209
// Insert node 1
188-
nodehandler->insert(node1);
210+
nodemap->insert(node1);
189211
// Insert node 2
190-
nodehandler->insert(node2);
212+
nodemap->insert(node2);
191213
// Check size of node hanlder
192-
REQUIRE(nodehandler->size() == 2);
214+
REQUIRE(nodemap->size() == 2);
193215

194-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
216+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
195217
auto coords = ((*itr).second)->coordinates();
196218
// Check if coordinates for each node is zero
197219
for (unsigned i = 0; i < coords.size(); ++i)
@@ -201,14 +223,14 @@ TEST_CASE("Node handler is checked for 3D case", "[nodehandler][3D]") {
201223
// Set coordinates to unity
202224
coords << 1., 1., 1.;
203225

204-
// Iterate through node handler to update coordinaates
205-
nodehandler->for_each(
226+
// Iterate through node map to update coordinaates
227+
nodemap->for_each(
206228
// function structure
207229
std::bind(&mpm::NodeBase<Dim>::assign_coordinates,
208230
std::placeholders::_1, coords));
209231

210232
// Check if update has gone through
211-
for (auto itr = nodehandler->begin(); itr != nodehandler->end(); ++itr) {
233+
for (auto itr = nodemap->begin(); itr != nodemap->end(); ++itr) {
212234
auto coords = ((*itr).second)->coordinates();
213235
// Check if coordinates for each node is zero
214236
for (unsigned i = 0; i < coords.size(); ++i)

0 commit comments

Comments
 (0)