From 4bf7409f77b84beab75302bd7ec0042a363ec7c2 Mon Sep 17 00:00:00 2001 From: Thomas Steinreiter Date: Thu, 17 Nov 2016 15:21:13 +0100 Subject: [PATCH] refactor typedef struct to OOP-Structs --- .../src/datastructures/BarnesHutTree.cpp | 10 +- bhtree_mpi/src/datastructures/Body.cpp | 72 ++++----- bhtree_mpi/src/datastructures/Body.hpp | 21 +-- bhtree_mpi/src/datastructures/Box.cpp | 150 +++++++++--------- bhtree_mpi/src/datastructures/Box.hpp | 41 ++--- bhtree_mpi/src/datastructures/Node.cpp | 18 +-- bhtree_mpi/src/datastructures/Tree.cpp | 20 +-- bhtree_mpi/src/simulation/MpiSimulation.cpp | 16 +- bhtree_mpi/src/simulation/MpiSimulation.hpp | 4 +- 9 files changed, 177 insertions(+), 175 deletions(-) diff --git a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp index 6bdd1cb..7ef6939 100644 --- a/bhtree_mpi/src/datastructures/BarnesHutTree.cpp +++ b/bhtree_mpi/src/datastructures/BarnesHutTree.cpp @@ -11,7 +11,7 @@ namespace nbody { //determine octree subboxes std::vector BarnesHutTree::splitBB(const Node* node) { - return octreeSplit(node->getBB()); + return node->getBB().octreeSplit(); } int BarnesHutTree::numberOfChildren() const { @@ -68,7 +68,7 @@ namespace nbody { child->parent = current; child->bb = *it; - child->bodies = copyBodies(*it, current->bodies); + child->bodies = it->copyBodies(current->bodies); child->nextSibling = nullptr; child->prevSibling = nullptr; after->insertBefore(child); @@ -122,8 +122,8 @@ namespace nbody { void BarnesHutTree::build(const std::vector& bodies) { Box bb; - initBox(bb); - extendForBodies(bb, bodies); + bb.initBox(); + bb.extendForBodies(bodies); this->build(bodies, bb); } @@ -138,7 +138,7 @@ namespace nbody { while (!current->leaf) { Node* child = current->next; - while (child != nullptr && !contained(child->getBB(), it->position)) { + while (child != nullptr && !child->getBB().contained(it->position)) { child = child->nextSibling; } //TODO(pheinzlr): check for child == nullptr? diff --git a/bhtree_mpi/src/datastructures/Body.cpp b/bhtree_mpi/src/datastructures/Body.cpp index a924c99..f980518 100644 --- a/bhtree_mpi/src/datastructures/Body.cpp +++ b/bhtree_mpi/src/datastructures/Body.cpp @@ -7,59 +7,59 @@ #include namespace nbody { - void resetAcceleration(Body& body) { - std::fill(std::begin(body.acceleration), std::end(body.acceleration), 0.0); + void Body::resetAcceleration() { + std::fill(std::begin(acceleration), std::end(acceleration), 0.0); } //helper method for intergration - Derivative evaluate(const Body& body, double dt, const Derivative& d) { - double velocity[3]; + Derivative Body::evaluate(double dt, const Derivative& d) const { + std::array tmp; Derivative output; for (int i = 0; i < 3; i++) { - velocity[i] = body.velocity[i] + d.dv[i] * dt; - output.dx[i] = velocity[i]; - output.dv[i] = body.acceleration[i]; + tmp[i] = velocity[i] + d.dv[i] * dt; + output.dx[i] = tmp[i]; + output.dv[i] = acceleration[i]; } return output; } //integrating acceleration -> velocity -> position - void integrate(Body& body) { + void Body::integrate() { Derivative start; Derivative a, b, c, d; double dxdt[3]; double dvdt[3]; - if (body.refinement) { + if (refinement) { return; } - a = evaluate(body, 0.0, start); - b = evaluate(body, timestep * 0.5, a); - c = evaluate(body, timestep * 0.5, b); - d = evaluate(body, timestep, c); + a = evaluate(0.0, start); + b = evaluate(timestep * 0.5, a); + c = evaluate(timestep * 0.5, b); + d = evaluate(timestep, c); for (int i = 0; i < 3; i++) { dxdt[i] = 1.0 / 6.0 * (a.dx[i] + 2.0f * (b.dx[i] + c.dx[i]) + d.dx[i]); dvdt[i] = 1.0 / 6.0 * (a.dv[i] + 2.0f * (b.dv[i] + c.dv[i]) + d.dv[i]); - body.position[i] = body.position[i] + dxdt[i] * timestep; - body.velocity[i] = body.velocity[i] + dvdt[i] * timestep; + position[i] = position[i] + dxdt[i] * timestep; + velocity[i] = velocity[i] + dvdt[i] * timestep; } } //gravitational force computation - void accumulateForceOntoBody(Body& to, const Body& from) { + void Body::accumulateForceOntoBody(const Body& from) { double distance2 = 0.0; - 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]); + distance2 += (from.position[0] - position[0]) * (from.position[0] - position[0]); + distance2 += (from.position[1] - position[1]) * (from.position[1] - position[1]); + distance2 += (from.position[2] - position[2]) * (from.position[2] - position[2]); distance2 = std::max(distance2, std::numeric_limits::epsilon()); double distance = sqrt(distance2); - double mdist = -1.0 * ((from.mass * to.mass) / distance2); + double mdist = -1.0 * ((from.mass * mass) / distance2); for (int i = 0; i < 3; i++) { - double vec = (from.position[i] - to.position[i]) / distance; + double vec = (from.position[i] - position[i]) / distance; - to.acceleration[i] += vec * mdist; + acceleration[i] += vec * mdist; } } @@ -67,20 +67,20 @@ namespace nbody { return !std::isnan(val) && std::isfinite(val); } - bool validBody(const Body& body) { - if (!std::all_of(std::begin(body.position), std::end(body.position), validDouble)) return false; - if (!std::all_of(std::begin(body.velocity), std::end(body.velocity), validDouble)) return false; - if (!std::all_of(std::begin(body.acceleration), std::end(body.acceleration), validDouble)) return false; - if (!validDouble(body.mass)) return false; - return body.mass >= 0.0; + bool Body::validBody() const { + if (!std::all_of(std::begin(position), std::end(position), validDouble)) return false; + if (!std::all_of(std::begin(velocity), std::end(velocity), validDouble)) return false; + if (!std::all_of(std::begin(acceleration), std::end(acceleration), validDouble)) return false; + if (!validDouble(mass)) return false; + return mass >= 0.0; } - void printBody(int parallelId, const Body& body) { - std::cout << parallelId << " " << body.id << " Position: " << body.position[0] << " " << body.position[1] << " " << body.position[2] << '\n'; - std::cout << parallelId << " " << body.id << " Velocity: " << body.velocity[0] << " " << body.velocity[1] << " " << body.velocity[2] << '\n'; - std::cout << parallelId << " " << body.id << " Acceleration: " << body.acceleration[0] << " " << body.acceleration[1] << " " << body.acceleration[2] << '\n'; - std::cout << parallelId << " " << body.id << " Mass: " << body.mass << '\n'; - std::cout << parallelId << " " << body.id << " Refinement: " << body.refinement << '\n' << '\n'; + void Body::printBody(int parallelId) const { //TODO(steinret): do printing via put + std::cout << parallelId << " " << id << " Position: " << position[0] << " " << position[1] << " " << position[2] << '\n'; + std::cout << parallelId << " " << id << " Velocity: " << velocity[0] << " " << velocity[1] << " " << velocity[2] << '\n'; + std::cout << parallelId << " " << id << " Acceleration: " << acceleration[0] << " " << acceleration[1] << " " << acceleration[2] << '\n'; + std::cout << parallelId << " " << id << " Mass: " << mass << '\n'; + std::cout << parallelId << " " << id << " Refinement: " << refinement << '\n' << '\n'; } //parse input files @@ -118,11 +118,11 @@ namespace nbody { void printBodies(int parallelId, const std::vector& bodies) { for (auto& body : bodies) { - printBody(parallelId, body); + body.printBody(parallelId); } } bool valid(const std::vector& bodies) { - return std::all_of(std::begin(bodies), std::end(bodies), validBody); + return std::all_of(std::begin(bodies), std::end(bodies), [](const Body& b) { return b.validBody(); }); } } diff --git a/bhtree_mpi/src/datastructures/Body.hpp b/bhtree_mpi/src/datastructures/Body.hpp index 6c96bb1..5494d59 100644 --- a/bhtree_mpi/src/datastructures/Body.hpp +++ b/bhtree_mpi/src/datastructures/Body.hpp @@ -8,26 +8,27 @@ namespace nbody { static const double timestep = 1.0; - typedef struct DerivativeStruct { + struct Derivative { std::array dx; std::array dv; - } Derivative; + }; - typedef struct BodyStruct { //TODO(steinret): c++ structs + struct Body { unsigned long id; std::array position; std::array velocity; std::array acceleration; double mass; int refinement; - } Body; - void resetAcceleration(Body& body); - Derivative evaluate(const Body& body, double dt, const Derivative& d); - void integrate(Body& body); - void accumulateForceOntoBody(Body& to, const Body& from); - bool validBody(const Body& body); - void printBody(int parallelId, const Body& body); + void resetAcceleration(); + Derivative evaluate(double dt, const Derivative& d) const; + void integrate(); + void accumulateForceOntoBody(const Body& from); + bool validBody() const; //TODO(steinret): rename + void printBody(int parallelId) const; + }; + void printBodies(int parallelId, const std::vector& bodies); bool valid(const std::vector& bodies); std::vector dubinskiParse(const std::string& filename); diff --git a/bhtree_mpi/src/datastructures/Box.cpp b/bhtree_mpi/src/datastructures/Box.cpp index 62628d3..be8c80c 100644 --- a/bhtree_mpi/src/datastructures/Box.cpp +++ b/bhtree_mpi/src/datastructures/Box.cpp @@ -5,20 +5,20 @@ #include "Box.hpp" namespace nbody { - void initBox(Box& box) { - std::fill(std::begin(box.min), std::end(box.min), std::numeric_limits::max()); - std::fill(std::begin(box.max), std::end(box.max), std::numeric_limits::min()); + void Box::initBox() { + std::fill(std::begin(min), std::end(min), std::numeric_limits::max()); + std::fill(std::begin(max), std::end(max), std::numeric_limits::min()); } //extend box to form cube - void extendToCube(Box& box) { + void Box::extendToCube() { int longestSide = -1; double sidelength = 0.0; for (int i = 0; i < 3; i++) { - if (box.max[i] - box.min[i] >= sidelength) { + if (max[i] - min[i] >= sidelength) { longestSide = i; - sidelength = box.max[i] - box.min[i]; + sidelength = max[i] - min[i]; } } if (longestSide == -1) { @@ -26,33 +26,33 @@ namespace nbody { } for (int i = 0; i < 3; i++) { if (i != longestSide) { - double extend = (sidelength - (box.max[i] - box.min[i])) / 2.0; + double extend = (sidelength - (max[i] - min[i])) / 2.0; - box.min[i] -= extend; - box.max[i] += extend; + min[i] -= extend; + max[i] += extend; } } } //extend for bodies - void extendForBodies(Box& box, const std::vector& bodies) { + void Box::extendForBodies(const std::vector& bodies) { for (const auto& body : bodies) { for (int i = 0; i < 3; i++) { - box.min[i] = std::min(body.position[i], box.min[i]); - box.max[i] = std::max(body.position[i], box.max[i]); + min[i] = std::min(body.position[i], min[i]); + max[i] = std::max(body.position[i], max[i]); } } } //extract bodies within box - std::vector extractBodies(const Box& box, std::vector& bodies) { + std::vector Box::extractBodies(std::vector& bodies) const { std::vector result; auto it = std::begin(bodies); while (it != std::end(bodies)) { - if (it->position[0] >= box.min[0] && it->position[0] <= box.max[0] && - it->position[1] >= box.min[1] && it->position[1] <= box.max[1] && - it->position[2] >= box.min[2] && it->position[2] <= box.max[2]) { + if (it->position[0] >= min[0] && it->position[0] <= max[0] && + it->position[1] >= min[1] && it->position[1] <= max[1] && + it->position[2] >= min[2] && it->position[2] <= max[2]) { result.push_back(*it); it = bodies.erase(it); } else { @@ -63,12 +63,12 @@ namespace nbody { } //copy bodies within box - std::vector copyBodies(const Box& box, const std::vector& bodies) { + std::vector Box::copyBodies(const std::vector& bodies) const { std::vector result; 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 body.position[0] >= min[0] && body.position[0] <= max[0] && + body.position[1] >=min[1] && body.position[1] <= max[1] && + body.position[2] >=min[2] && body.position[2] <= max[2]; }); return result; } @@ -94,33 +94,33 @@ namespace nbody { } //box volume - double volume(const Box& box) { - if (!isValid(box)) { + double Box::volume() const { + if (!isValid()) { return -1.0; } double result = 1.0; for (int i = 0; i < 3; i++) { - result *= box.max[i] - box.min[i]; + result *= max[i] - min[i]; } return result; } - double maxSidelength(const Box& box) { + double Box::maxSidelength() const { double maxVal = 0.0; - if (!isValid(box)) { + if (!isValid()) { return -1.0; } for (int i = 0; i < 3; i++) { - maxVal = std::max(box.max[i] - box.min[i], maxVal); + maxVal = std::max(max[i] - min[i], maxVal); } return maxVal; } - bool isCorrectBox(const Box& box) { + bool Box::isCorrectBox() const { //TODO(steinret): dupe isValid for (int i = 0; i < 3; i++) { - if (box.max[i] < box.min[i]) { + if (max[i] < min[i]) { std::cout << "inverted bb\n"; return false; } @@ -128,41 +128,41 @@ namespace nbody { return true; } - bool isValid(const Box& box) { + bool Box::isValid() const { for (int i = 0; i < 3; i++) { - if (box.max[i] < box.min[i]) { + if (max[i] < min[i]) { return false; } } return true; } - void printBB(int parallelId, const Box& box) { + void Box::printBB(int parallelId) const { std::cout << parallelId << ": min "; for (int i = 0; i < 3; i++) { - std::cout << ": " << box.min[i] << " "; + std::cout << ": " << min[i] << " "; } std::cout << parallelId << ": max "; for (int i = 0; i < 3; i++) { - std::cout << box.max[i] << " "; + std::cout << max[i] << " "; } std::cout << '\n'; } //check for box/sphere overlap - bool overlapsSphere(const Box& box, const double* sphereCenter, double sphereRadius) { + bool Box::overlapsSphere(const double* sphereCenter, double sphereRadius) const { double dmin = 0.0; - if (!isValid(box)) { + if (!isValid()) { return false; } for (int i = 0; i < 3; i++) { - if (sphereCenter[i] < box.min[i]) { - double dist = sphereCenter[i] - box.min[i]; + if (sphereCenter[i] < min[i]) { + double dist = sphereCenter[i] - min[i]; dmin += dist * dist; - } else if (sphereCenter[i] > box.max[i]) { - double dist = sphereCenter[i] - box.max[i]; + } else if (sphereCenter[i] > max[i]) { + double dist = sphereCenter[i] - max[i]; dmin += dist * dist; } @@ -171,18 +171,18 @@ namespace nbody { } //distance from nearest box order to position - double distanceToPosition(const Box& box, const double* position) { + double Box::distanceToPosition(const double* position) const { int inside = 0; double nextPosition[3] = {position[0], position[1], position[2]}; - if (!isValid(box)) { + if (!isValid()) { return std::numeric_limits::max(); } for (int i = 0; i < 3; i++) { - if (nextPosition[i] < box.min[i]) { - nextPosition[i] = box.min[i]; - } else if (nextPosition[i] > box.max[i]) { - nextPosition[i] = box.max[i]; + if (nextPosition[i] < min[i]) { + nextPosition[i] = min[i]; + } else if (nextPosition[i] > max[i]) { + nextPosition[i] = max[i]; } else { inside++; } @@ -200,19 +200,19 @@ namespace nbody { } //box - box distance - double distanceToBox(const Box& box1, const Box& box2) { + double Box::distanceToBox(const Box& box2) const { double length = 0.0; - if (!isValid(box1) || !isValid(box2)) { + if (!isValid() || !box2.isValid()) { return std::numeric_limits::max(); } for (int i = 0; i < 3; i++) { double elem; - if (box2.min[i] < box1.min[i] && box2.max[i] < box1.min[i]) { - elem = box1.min[i] - box2.max[i]; - } else if (box2.min[i] > box1.max[i] && box2.max[i] > box1.max[i]) { - elem = box2.min[i] - box1.max[i]; + if (box2.min[i] < min[i] && box2.max[i] < min[i]) { + elem = min[i] - box2.max[i]; + } else if (box2.min[i] > max[i] && box2.max[i] > max[i]) { + elem = box2.min[i] - max[i]; } else { elem = 0.0; } @@ -222,14 +222,14 @@ namespace nbody { } //determine octree subboxes - std::vector octreeSplit(const Box& box) { + std::vector Box::octreeSplit() const { std::vector result; - if (!isValid(box)) { + if (!isValid()) { return result; } for (unsigned int i = 0; i < 8; i++) { - Box current = box; + Box current = *this; for (unsigned int j = 0; j < 3; j++) { double middle = current.min[j] + (current.max[j] - current.min[j]) / 2.0; @@ -246,35 +246,35 @@ namespace nbody { } //split box into two across longest side - std::vector splitLongestSide(const Box& box) { + std::vector Box::splitLongestSide() const { std::vector result; int longestIndex = -1; double longestSide = -1.0; - if (!isValid(box)) { + if (!isValid()) { return result; } for (int i = 0; i < 3; i++) { - if (box.max[i] - box.min[i] > longestSide) { - longestSide = box.max[i] - box.min[i]; + if (max[i] - min[i] > longestSide) { + longestSide = max[i] - min[i]; longestIndex = i; } } - double middle = box.min[longestIndex] + (box.max[longestIndex] - box.min[longestIndex]) / 2.0; - result.push_back(box); + double middle = min[longestIndex] + (max[longestIndex] - min[longestIndex]) / 2.0; + result.push_back(*this); result.back().max[longestIndex] = middle; - result.push_back(box); + result.push_back(*this); result.back().min[longestIndex] = middle; return result; } //check for position in box - bool contained(const Box& box, const std::array& position) { - if (!isValid(box)) { + bool Box::contained(const std::array& position) const { + if (!isValid()) { return false; } for (int i = 0; i < 3; i++) { - if (position[i] < box.min[i] || position[i] > box.max[i]) { + if (position[i] < min[i] || position[i] > max[i]) { return false; } } @@ -282,28 +282,28 @@ namespace nbody { } //extend box by box - void extend(Box& box, const Box& extender) { - if (!isValid(extender)) { + void Box::extend(const Box& extender) { + if (!extender.isValid()) { return; } for (int i = 0; i < 3; i++) { - if (box.min[i] > extender.min[i]) { - box.min[i] = extender.min[i]; + if (min[i] > extender.min[i]) { //TODO(steinret): min max + min[i] = extender.min[i]; } - if (box.max[i] < extender.max[i]) { - box.max[i] = extender.max[i]; + if (max[i] < extender.max[i]) { + max[i] = extender.max[i]; } } } //extend box by body - void extend(Box& box, const Body& extender) { + void Box::extend(const Body& extender) { for (int i = 0; i < 3; i++) { - if (box.min[i] > extender.position[i]) { - box.min[i] = extender.position[i]; + if (min[i] > extender.position[i]) { //TODO(steinret): min max + min[i] = extender.position[i]; } - if (box.max[i] < extender.position[i]) { - box.max[i] = extender.position[i]; + if (max[i] < extender.position[i]) { + max[i] = extender.position[i]; } } } diff --git a/bhtree_mpi/src/datastructures/Box.hpp b/bhtree_mpi/src/datastructures/Box.hpp index 0b316c7..33eeda6 100644 --- a/bhtree_mpi/src/datastructures/Box.hpp +++ b/bhtree_mpi/src/datastructures/Box.hpp @@ -6,31 +6,32 @@ #include namespace nbody { - typedef struct _Box { + struct Box { std::array min; std::array max; - } Box; - void initBox(Box& box); - void extendToCube(Box& box); - void extendForBodies(Box& box, const std::vector& bodies); - std::vector extractBodies(const Box& box, std::vector& bodies); - std::vector copyBodies(const Box& box, const std::vector& bodies); + void initBox(); + void extendToCube(); + void extendForBodies(const std::vector& bodies); + std::vector extractBodies(std::vector& bodies) const; + std::vector copyBodies(const std::vector& bodies) const; + double volume() const; + double maxSidelength() const; + bool isCorrectBox() const; + bool isValid() const; + void printBB(int parallelId) const; + bool overlapsSphere(const double* sphereCenter, double sphereRadius) const; //TODO(steinret): double* srsly? + double distanceToPosition(const double* position) const; + double distanceToBox(const Box& box2) const; + std::vector octreeSplit() const; + std::vector splitLongestSide() const; + bool contained(const std::array& position) const; + void extend(const Box& extender); + void extend(const Body& extender); + }; bool isContained(const Body& body, const Box& box); bool isContained(const Box& inner, const Box& outer); - double volume(const Box& box); - double maxSidelength(const Box& box); - bool isCorrectBox(const Box& box); - bool isValid(const Box& box); - void printBB(int parallelId, const Box& box); - bool overlapsSphere(const Box& box, const double* sphereCenter, double sphereRadius); //TODO(steinret): double* srsly? - double distanceToPosition(const Box& box, const double* position); - double distanceToBox(const Box& box1, const Box& box2); - std::vector octreeSplit(const Box& box); - std::vector splitLongestSide(const Box& box); - bool contained(const Box& box, const std::array& position); - void extend(Box& box, const Box& extender); - void extend(Box& box, const Body& extender); + } #endif diff --git a/bhtree_mpi/src/datastructures/Node.cpp b/bhtree_mpi/src/datastructures/Node.cpp index 8ce3a48..e1f3e2a 100644 --- a/bhtree_mpi/src/datastructures/Node.cpp +++ b/bhtree_mpi/src/datastructures/Node.cpp @@ -6,7 +6,7 @@ namespace nbody { Node::Node(Tree* tree) { - initBox(this->bb); + this->bb.initBox(); this->afterSubtree = nullptr; this->prev = this; this->next = this; @@ -33,18 +33,18 @@ namespace nbody { result = false; } //this is to prevent errors with collocated particles - if (volume(this->bb) <= std::numeric_limits::epsilon()) { + if (this->bb.volume() <= std::numeric_limits::epsilon()) { result = false; } return result; } void Node::extendBBforBodies() { - extendForBodies(this->bb, this->bodies); + this->bb.extendForBodies(this->bodies); } void Node::extendBBtoCube() { - extendToCube(this->bb); + this->bb.extendToCube(); } std::vector Node::getBodies() const { @@ -74,7 +74,7 @@ namespace nbody { std::cerr << "after subtree null\n"; return false; } - if (!isCorrectBox(this->bb)) { + if (!this->bb.isCorrectBox()) { std::cerr << "bb wrong\n"; return false; } @@ -149,14 +149,14 @@ namespace nbody { //get criterion to check if node is sufficient for force evaluation double Node::getL() const { - return maxSidelength(this->bb); + return this->bb.maxSidelength(); } void Node::print(int parallelId) const { - printBB(parallelId, this->bb); + this->bb.printBB(parallelId); for (auto& body : bodies) { std::cout << " "; - printBody(parallelId, body); + body.printBody(parallelId); } } @@ -172,7 +172,7 @@ namespace nbody { //check if node is sufficient for force evaluation for all bodies in box bool Node::sufficientForBox(const Box& box) const { - return distanceToBox(this->bb, box) > this->getL(); + return this->bb.distanceToBox(box) > this->getL(); } void Node::setBodies(const std::vector& bodies) { diff --git a/bhtree_mpi/src/datastructures/Tree.cpp b/bhtree_mpi/src/datastructures/Tree.cpp index 5f5c061..d0b5d53 100644 --- a/bhtree_mpi/src/datastructures/Tree.cpp +++ b/bhtree_mpi/src/datastructures/Tree.cpp @@ -62,14 +62,14 @@ namespace nbody { void Tree::accumulateForceOnto(Body& body) { Node* n = this->nodes->next; - resetAcceleration(body); + body.resetAcceleration(); while (n != this->nodes) { if (n->sufficientForBody(body)) { - accumulateForceOntoBody(body, n->representative); + body.accumulateForceOntoBody(n->representative); n = n->afterSubtree; } else if (n->leaf) { for (auto b : n->bodies) { - accumulateForceOntoBody(body, b); + body.accumulateForceOntoBody(b); } n = n->afterSubtree; } else { @@ -112,7 +112,7 @@ namespace nbody { std::vector result; Node* current = this->nodes->next; - if (!isValid(current->bb)) { + if (!current->bb.isValid()) { //empty tree means no refinements return result; } @@ -162,8 +162,8 @@ namespace nbody { if (n->leaf) { for (auto& b : n->bodies) { if (!b.refinement) { - integrate(b); - extend(bb, b); + b.integrate(); + bb.extend(b); } } } @@ -175,12 +175,12 @@ namespace nbody { Box Tree::getLocalBB() const { Box result; - initBox(result); + result.initBox(); for (Node* n = this->nodes->next; n != this->nodes; n = n->next) { if (n->leaf) { for (auto& b : n->bodies) { if (!b.refinement) { - extend(result, b); + result.extend(b); } } } @@ -190,11 +190,11 @@ namespace nbody { void Tree::print(int parallelId) const { for (Node* n = this->nodes->next; n != this->nodes; n = n->next) { - printBB(parallelId, n->bb); + n->bb.printBB(parallelId); if (n->leaf) { for (auto& b : n->bodies) { if (!b.refinement) { - printBody(this->parallelId, b); + b.printBody(this->parallelId); } } } diff --git a/bhtree_mpi/src/simulation/MpiSimulation.cpp b/bhtree_mpi/src/simulation/MpiSimulation.cpp index b7de8e5..058398a 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.cpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.cpp @@ -125,10 +125,10 @@ namespace nbody { std::vector nodes; Box bb; - initBox(bb); + bb.initBox(); nodes.push_back(Node(nullptr)); nodes.front().setBodies(this->bodies); - extendForBodies(bb, this->bodies); + bb.extendForBodies(this->bodies); nodes.front().setBB(bb); //determine how to distribute bodies to processes //split box with most particles by halfing its longest side @@ -141,15 +141,15 @@ namespace nbody { mostBodiesIndex = i; } } - std::vector subdomains = splitLongestSide(nodes[mostBodiesIndex].getBB()); + std::vector subdomains = nodes[mostBodiesIndex].getBB().splitLongestSide(); std::vector buf = nodes[mostBodiesIndex].getBodies(); Node n(nullptr); - n.setBodies(extractBodies(subdomains[0], buf)); + n.setBodies(subdomains[0].extractBodies(buf)); n.setBB(subdomains[0]); nodes.insert(std::begin(nodes) + mostBodiesIndex, n); n = Node(nullptr); - n.setBodies(extractBodies(subdomains[1], buf)); + n.setBodies(subdomains[1].extractBodies(buf)); n.setBB(subdomains[1]); nodes.insert(std::begin(nodes) + mostBodiesIndex, n); nodes.erase(std::begin(nodes) + mostBodiesIndex + 2); @@ -167,8 +167,8 @@ namespace nbody { Box localDomain; //determine local domain size - initBox(localDomain); - extendForBodies(localDomain, localBodies); + localDomain.initBox(); + localDomain.extendForBodies(localBodies); this->distributeDomains(localDomain); } @@ -185,7 +185,7 @@ namespace nbody { this->overallDomain = localDomain; //determine overall domain size for (int i = 0; i < this->parallelSize; i++) { - extend(this->overallDomain, this->domains[i]); + this->overallDomain.extend(this->domains[i]); } } diff --git a/bhtree_mpi/src/simulation/MpiSimulation.hpp b/bhtree_mpi/src/simulation/MpiSimulation.hpp index 8eec35d..9fbaa12 100644 --- a/bhtree_mpi/src/simulation/MpiSimulation.hpp +++ b/bhtree_mpi/src/simulation/MpiSimulation.hpp @@ -9,11 +9,11 @@ #include namespace nbody { - typedef struct SendStoreStruct { + struct SendStore { Body* bodies; MPI_Request request; int size; - } SendStore; + }; //MPI simulation class MpiSimulation : public Simulation { -- GitLab