From 6aebd7d757beacf4b9091e56f7b7db824769498d Mon Sep 17 00:00:00 2001
From: "armindamon.riess" <armindamon.riess@uzh.ch>
Date: Wed, 7 Dec 2022 14:25:19 +0100
Subject: [PATCH] minor changes to update function

---
 lib/node.cpp | 31 +++++++++++++++++++++++--------
 lib/node.hpp |  5 +++--
 lib/tree.cpp |  5 ++++-
 lib/tree.hpp |  3 +++
 4 files changed, 33 insertions(+), 11 deletions(-)

diff --git a/lib/node.cpp b/lib/node.cpp
index 7fea2fa..8c3969a 100644
--- a/lib/node.cpp
+++ b/lib/node.cpp
@@ -19,7 +19,7 @@ Node::Node() {
  */
 
 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) {
+        double size, double center[3], std::vector<unsigned>& localParticles, unsigned nLocalParticles) {
     parent_ = parent;
     particles_ = particles;
     nParticles_ = nParticles;
@@ -37,9 +37,10 @@ Node::Node(Node* root, Node* parent, double* masses, double* particles, unsigned
         mass_ += masses_[localParticles_[i]];
     }
 
+    // TODO
     // allocate children
     // check if child contains no particles, in that case set child to nullptr
-    // TODO
+    // check if node is leaf
 }
 
 Node::~Node() {
@@ -63,12 +64,18 @@ Node& Node::operator=(const Node& other) {
     return *this;
 }
 
-void Node::update() {
-    // update this node
+void Node::update(std::vector<unsigned>& localParticles) {
     // TODO
+    // update center of mass
+    // maybe subdivide? 
+    // update this node
     // update children
+    isLeaf_ = true;
     for (unsigned i=0; i<8; ++i) {
-        if (children_[i] != nullptr) children_[i]->update();
+        if (children_[i] != nullptr) {
+            // children_[i]->update();
+            isLeaf_ = false;
+        }
     }
 }
 
@@ -81,10 +88,18 @@ double* Node::calculateForce(unsigned particle) {
                             + (center_[1] - py)*(center_[1] - py)
                             + (center_[2] - pz)*(center_[2] - pz));
     double theta = size_/lambda;
-    // if angle too large, call this function for every child. If a child is a nullptr, calculate force on particle from particles in this node
-    // else calculate force on particle from particles in this node
+
+    // TODO
+    // If angle too large, call this function for every child which is not a nullptr. 
+    //      If the node is a leaf, calculate force on particle from particles in this node. 
+    //      If the node is not a leaf, call this function for every child which is not a nullptr.
+    // If angle small enough, calculate force on particle from this node.
     if (theta > theta0_) {
-        // TODO
+        if (isLeaf_) {
+            // TODO
+        } else {
+            // TODO
+        }
     } else {
         // TODO
     }
diff --git a/lib/node.hpp b/lib/node.hpp
index ebb859a..cd56829 100644
--- a/lib/node.hpp
+++ b/lib/node.hpp
@@ -10,7 +10,7 @@ public:
     // 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], std::vector<unsigned> localParticles, unsigned nLocalParticles);
+        double size, double center[3], std::vector<unsigned>& localParticles, unsigned nLocalParticles);
     // destuctor
     ~Node();
     // copy constructor
@@ -18,7 +18,7 @@ public:
     // 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();
+    void update(std::vector<unsigned>& allParticles);
     // calculate force on a particle recursively
     double* calculateForce(unsigned particle);
 private:
@@ -35,6 +35,7 @@ private:
     unsigned nLocalParticles_;
     double mass_;
     const double theta0_ = 0.4;
+    bool isLeaf_;
 };
 
 #endif /* NODE_HPP */
\ No newline at end of file
diff --git a/lib/tree.cpp b/lib/tree.cpp
index 08a3824..7ff2bce 100644
--- a/lib/tree.cpp
+++ b/lib/tree.cpp
@@ -17,6 +17,9 @@ Tree::Tree(double* masses, double* particles, double* forces, unsigned nParticle
     root_ = new Node();
     *root_ = Node(root_, root_, masses_, particles_, nParticles_, 0, 
                 size_, center_, localParticles_, nParticles_);
+
+    allParticles_ = std::vector<unsigned>(nParticles_);
+    for (unsigned i=0; i < nParticles_; ++i) allParticles_[i] = i;
 }
 
 Tree::~Tree() {
@@ -29,5 +32,5 @@ void Tree::calculateForce(unsigned particle) {
 }
 
 void Tree::update() {
-    root_->update();
+    root_->update(allParticles_);
 }
\ No newline at end of file
diff --git a/lib/tree.hpp b/lib/tree.hpp
index 5b55b41..40b3682 100644
--- a/lib/tree.hpp
+++ b/lib/tree.hpp
@@ -3,6 +3,8 @@
 
 #include "node.hpp"
 
+#include <vector>
+
 class Tree {
 public:
     // constructor
@@ -22,6 +24,7 @@ private:
     unsigned nParticles_;
     double size_;
     double center_[3];
+    std::vector<unsigned> allParticles_;
 };
 
 #endif /* TREE_HPP */
\ No newline at end of file
-- 
GitLab