From 2d1ab8c60e43f3e10eb8bd52d62d83734a242e6d Mon Sep 17 00:00:00 2001
From: "armindamon.riess" <armindamon.riess@uzh.ch>
Date: Wed, 7 Dec 2022 12:44:10 +0100
Subject: [PATCH] leapfrog integration done

---
 lib/nBodySim.cpp | 14 +++++++++-----
 lib/nBodySim.hpp |  4 +++-
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/nBodySim.cpp b/lib/nBodySim.cpp
index 181ca15..c47a32f 100644
--- a/lib/nBodySim.cpp
+++ b/lib/nBodySim.cpp
@@ -62,7 +62,9 @@ nBodySim::~nBodySim() {
 
 void nBodySim::runSimulation(double dt, unsigned nSteps) {
     for (unsigned i=0; i < nSteps; ++i) {
+        treeCalculateForces();
         doTimeStep(dt);
+        updateTree();
     }
 }
 
@@ -157,18 +159,20 @@ double nBodySim::calculateMeanForceMagnitude() {
 }
 
 void nBodySim::doTimeStep(double dt) {
+    // update velocities, then positions
     #pragma omp parallel for
     for (unsigned i=0; i < nParticles_; ++i) {
         for (unsigned j=0; j < 3; ++j) {
-            // TODO
-            tree_->kick(dt/2);
-            tree_->drift(dt);
-            tree_->update();
-            tree_->kick(dt/2);
+            velocities_[3*i+j] += 0.5 * forces_[3*i+j] * dt / masses_[i];
+            positions_[3*i+j] += velocities_[3*i+j] * dt;
         }
     }
 }
 
+void nBodySim::updateTree() {
+    tree_->update();
+}
+
 void nBodySim::write2file(const double* array, std::string filename, unsigned dim) const {
     // writes array to file, with N rows and dim columns
     std::ofstream file(filename);
diff --git a/lib/nBodySim.hpp b/lib/nBodySim.hpp
index cd9fbc8..380bce4 100644
--- a/lib/nBodySim.hpp
+++ b/lib/nBodySim.hpp
@@ -20,8 +20,10 @@ public:
     double calculateMeanInterparticleDistance();
     // loop over all force vectors and calculate mean force magnitude
     double calculateMeanForceMagnitude();
-    // updates positions and velocities using the calculated forces
+    // integrate using leapfrog algorithm. doesn't update forces, doesn't update tree
     void doTimeStep(double dt);
+    // update tree
+    void updateTree();
     // writes data to file, every row is a particle
     void write2file(const double* array, std::string filename, unsigned dim) const;
     // write current state of simulation to file
-- 
GitLab