diff --git a/lib/node.cpp b/lib/node.cpp index b7ce061636b8daef52de34c0987fd88c527f7554..7fea2fa0fa96869dc482f447b8c30761f4feb55e 100644 --- a/lib/node.cpp +++ b/lib/node.cpp @@ -1,5 +1,6 @@ #include "node.hpp" +#include <vector> #include <cmath> /* @@ -17,7 +18,8 @@ Node::Node() { } */ -Node::Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, double size, double center[3], unsigned* localParticles, unsigned nLocalParticles) { +Node::Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, + double size, double center[3], std::vector<unsigned> localParticles, unsigned nLocalParticles) { parent_ = parent; particles_ = particles; nParticles_ = nParticles; @@ -44,7 +46,6 @@ Node::~Node() { for (unsigned i=0; i<8; ++i) { delete children_[i]; } - delete[] localParticles_; } Node& Node::operator=(const Node& other) { @@ -62,6 +63,15 @@ Node& Node::operator=(const Node& other) { return *this; } +void Node::update() { + // update this node + // TODO + // update children + for (unsigned i=0; i<8; ++i) { + if (children_[i] != nullptr) children_[i]->update(); + } +} + double* Node::calculateForce(unsigned particle) { // calculate theta double px = particles_[3*particle + 0]; diff --git a/lib/node.hpp b/lib/node.hpp index 0905c7458ea1e015aa0b87ef3bcf8034f8ccf5b8..ebb859a7e05b804eaf004327cad6f35ea986f86b 100644 --- a/lib/node.hpp +++ b/lib/node.hpp @@ -1,19 +1,24 @@ #ifndef NODE_HPP #define NODE_HPP +#include <vector> + class Node { public: // default constructor creates a leaf node Node() = default; // whoever creates the node is responsible for deciding which particles are in which octant and for allocating the localParticles array // node will write to localParticles array - Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, double size, double center[3], unsigned* localParticles, unsigned nLocalParticles); + Node(Node* root, Node* parent, double* masses, double* particles, unsigned nParticles, unsigned depth, + double size, double center[3], std::vector<unsigned> localParticles, unsigned nLocalParticles); // destuctor ~Node(); // copy constructor Node(const Node&) = default; // copy assignment Node& operator=(const Node&); + // update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region) + void update(); // calculate force on a particle recursively double* calculateForce(unsigned particle); private: @@ -26,7 +31,7 @@ private: unsigned depth_; double size_; double center_[3]; - unsigned* localParticles_; + std::vector<unsigned> localParticles_; unsigned nLocalParticles_; double mass_; const double theta0_ = 0.4; diff --git a/lib/tree.cpp b/lib/tree.cpp index 15a4f9b7d408bedd705bfe02cfb09887390d50ab..08a382405957f107198f5cf6387879c6edb44193 100644 --- a/lib/tree.cpp +++ b/lib/tree.cpp @@ -1,6 +1,8 @@ #include "tree.hpp" #include "node.hpp" +#include <vector> + Tree::Tree(double* masses, double* particles, double* forces, unsigned nParticles, double size, double* center) { particles_ = particles; forces_ = forces; @@ -9,11 +11,12 @@ Tree::Tree(double* masses, double* particles, double* forces, unsigned nParticle for (unsigned i=0; i < 3; ++i) center_[i] = center[i]; - unsigned* localParticles_ = new unsigned[nParticles_]; + std::vector<unsigned> localParticles_(nParticles_); for (unsigned i=0; i < nParticles_; ++i) localParticles_[i] = i; root_ = new Node(); - *root_ = Node(root_, root_, masses_, particles_, nParticles_, 0, size_, center_, localParticles_, nParticles_); + *root_ = Node(root_, root_, masses_, particles_, nParticles_, 0, + size_, center_, localParticles_, nParticles_); } Tree::~Tree() { @@ -26,5 +29,5 @@ void Tree::calculateForce(unsigned particle) { } void Tree::update() { - // TODO + root_->update(); } \ No newline at end of file diff --git a/lib/tree.hpp b/lib/tree.hpp index deca6ebecae263acc8faba48682b01e962844fe7..5b55b41e8823d7fdbd5b40efd593ebe609471d61 100644 --- a/lib/tree.hpp +++ b/lib/tree.hpp @@ -12,7 +12,7 @@ public: ~Tree(); // calculate force on a particle void calculateForce(unsigned particle); - // update tree: visit each node, check if it needs to be split or merged (for example if a particle has left the region) + // update tree void update(); private: Node* root_;