diff --git a/bhtree_mpi/src/datastructures/Body.cpp b/bhtree_mpi/src/datastructures/Body.cpp index 9aa9edecee6373152bbdbbbb800df356676b22f6..20b09e95177758e78fbf7ba85d29c6a0d4121119 100644 --- a/bhtree_mpi/src/datastructures/Body.cpp +++ b/bhtree_mpi/src/datastructures/Body.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -56,9 +57,7 @@ namespace nbody { distance2 += (from.position[0] - to.position[0]) * (from.position[0] - to.position[0]); distance2 += (from.position[1] - to.position[1]) * (from.position[1] - to.position[1]); distance2 += (from.position[2] - to.position[2]) * (from.position[2] - to.position[2]); - if (fabs(distance2) < std::numeric_limits::epsilon()) { //TODO(steinret) max? - distance2 = std::numeric_limits::epsilon(); - } + distance2 = std::max(distance2, std::numeric_limits::epsilon()); double distance = sqrt(distance2); double mdist = -1.0 * ((from.mass * to.mass) / distance2); for (int i = 0; i < 3; i++) { diff --git a/bhtree_mpi/src/datastructures/Box.cpp b/bhtree_mpi/src/datastructures/Box.cpp index 474a3a0624152b53bf85d39a08857f355d2b148d..4f99ce1646a974e74bfbcfed68411c4313437ac1 100644 --- a/bhtree_mpi/src/datastructures/Box.cpp +++ b/bhtree_mpi/src/datastructures/Box.cpp @@ -63,16 +63,13 @@ namespace nbody { } //copy bodies within box - std::vector copyBodies(Box box, std::vector bodies) { //TODO(steinret) std::copy_if ?? + std::vector copyBodies(Box box, std::vector bodies) { std::vector result; - - for (auto& body : bodies) { - if (body.position[0] >= box.min[0] && body.position[0] <= box.max[0] && - body.position[1] >= box.min[1] && body.position[1] <= box.max[1] && - body.position[2] >= box.min[2] && body.position[2] <= box.max[2]) { - result.push_back(body); - } - } + std::copy_if(std::begin(bodies), std::end(bodies), std::back_inserter(result), [&](const Body& body) { + return body.position[0] >= box.min[0] && body.position[0] <= box.max[0] && + body.position[1] >= box.min[1] && body.position[1] <= box.max[1] && + body.position[2] >= box.min[2] && body.position[2] <= box.max[2]; + }); return result; } diff --git a/bhtree_mpi/src/datastructures/Node.cpp b/bhtree_mpi/src/datastructures/Node.cpp index ccde9965588cf7bde2029a52f132b72949ce174a..ce147c984f04e792fbbd3efcd81bbb1edb8c048d 100644 --- a/bhtree_mpi/src/datastructures/Node.cpp +++ b/bhtree_mpi/src/datastructures/Node.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Node.hpp" namespace nbody { @@ -88,11 +89,9 @@ namespace nbody { return false; } } - for (auto& body : bodies) { //TODO(steinret) std::all_of ?? - if (!isContained(body, this->bb)) { - std::cerr << "bb out of bounds\n"; - return false; - } + if (!std::all_of(std::begin(bodies), std::end(bodies), [&](const Body& b) {return isContained(b, bb); })) { + std::cerr << "bb out of bounds\n"; + return false; } if (!this->leaf) { Node* current = this->next; @@ -186,15 +185,8 @@ namespace nbody { } //get local bodies - void Node::extractLocalBodiesTo(std::vector& bodies) { //TODO(steinret): std::copy_if - std::vector result; - auto it= std::begin(bodies); - - while (it != std::end(bodies)) { - if (!it->refinement) { - bodies.push_back(*it); - } - it = this->bodies.erase(it); - } + void Node::extractLocalBodiesTo(std::vector& result) { + std::copy_if(std::begin(this->bodies), std::end(this->bodies), std::back_inserter(result), [](const Body& b) {return !b.refinement; }); + this->bodies.clear(); } } diff --git a/bhtree_mpi/src/datastructures/Tree.cpp b/bhtree_mpi/src/datastructures/Tree.cpp index 2e1d5c1d5bbd5fa26675dca3f99f86fd99b781ac..e4bf5051a106411a5d5faccba8dd3f03421f0cfb 100644 --- a/bhtree_mpi/src/datastructures/Tree.cpp +++ b/bhtree_mpi/src/datastructures/Tree.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "Tree.hpp" #include "Node.hpp" #include "Simulation.hpp" @@ -96,11 +97,7 @@ namespace nbody { while (this->nodes->next != this->nodes) { if (this->nodes->next->leaf) { - for (auto& b : nodes->next->bodies) { //TODO(steinret) copy_if - if (!b.refinement) { - result.push_back(b); - } - } + std::copy_if(std::begin(nodes->next->bodies), std::end(nodes->next->bodies), std::back_inserter(result), [](const Body& b) {return !b.refinement; }); } Node* h = this->nodes->next; this->nodes->next->unlink();