From 4748d28f3d422a0d61118ca8c9367928bf6ae4f0 Mon Sep 17 00:00:00 2001
From: "armindamon.riess" <armindamon.riess@uzh.ch>
Date: Wed, 7 Dec 2022 10:34:36 +0100
Subject: [PATCH] prepare implementation of time step

---
 lib/nBodySim.cpp |  1 -
 lib/tree.hpp     |  4 ++--
 main.cpp         | 36 +++++++++++++++++++++---------------
 3 files changed, 23 insertions(+), 18 deletions(-)

diff --git a/lib/nBodySim.cpp b/lib/nBodySim.cpp
index a92c3f1..7ed7323 100644
--- a/lib/nBodySim.cpp
+++ b/lib/nBodySim.cpp
@@ -62,7 +62,6 @@ nBodySim::~nBodySim() {
 
 void nBodySim::runSimulation(double dt, unsigned nSteps) {
     for (unsigned i=0; i < nSteps; ++i) {
-        calculateForces();
         doTimeStep(dt);
     }
 }
diff --git a/lib/tree.hpp b/lib/tree.hpp
index 9578237..38cca7a 100644
--- a/lib/tree.hpp
+++ b/lib/tree.hpp
@@ -12,9 +12,9 @@ public:
     ~Tree();
     // 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();
-    // drift tree: visit each node, update center of mass and center of mass velocity
+    // drift tree: visit each node, update positions
     void drift(double dt);
-    // kick tree: visit each node, update center of mass velocity
+    // kick tree: visit each node, update velocities
     void kick(double dt);
 private:
     Node* root_;
diff --git a/main.cpp b/main.cpp
index d723a1f..5224b29 100644
--- a/main.cpp
+++ b/main.cpp
@@ -26,36 +26,42 @@ int main(int argc, char** argv) {
     std::cout << "N = " << N << std::endl;
 
     /*
+    // print positions
     for (unsigned i=0; i<N; ++i) {
         for (unsigned j=0; j<3; ++j) {
             std::cout << sim.getPositions()[3*i+j] << " ";
         }
         std::cout << std::endl;
     }
-    */
 
-    try {
-        // calculate mean interparticle distance
-        double meanInterparticleDistance = sim.calculateMeanInterparticleDistance();
-        std::cout << "Mean interparticle distance: " << meanInterparticleDistance << std::endl;
+    // calculate mean interparticle distance
+    double meanInterparticleDistance = sim.calculateMeanInterparticleDistance();
+    std::cout << "Mean interparticle distance: " << meanInterparticleDistance << std::endl;
+
+    // calculate forces and measure computation time
+    auto start = std::chrono::high_resolution_clock::now();
+    sim.calculateForces();
+    auto stop = std::chrono::high_resolution_clock::now();
 
-        // calculate forces and measure computation time
-        auto start = std::chrono::high_resolution_clock::now();
-        sim.calculateForces();
-        auto stop = std::chrono::high_resolution_clock::now();
+    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
+    std::cout << "Computation Time: " << duration.count()/1000000.0 << " seconds" << std::endl;
 
-        auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
-        std::cout << "Computation Time: " << duration.count()/1000000.0 << " seconds" << std::endl;
+    // calculate mean force magnitude
+    double meanForceMagnitude = sim.calculateMeanForceMagnitude();
+    std::cout << "Mean force magnitude: " << meanForceMagnitude << std::endl;
 
-        // calculate mean force magnitude
-        double meanForceMagnitude = sim.calculateMeanForceMagnitude();
-        std::cout << "Mean force magnitude: " << meanForceMagnitude << std::endl;
+    sim.write2file(sim.getForces(), "../out/forces.txt", 3);
+    */
 
-        sim.write2file(sim.getForces(), "../out/forces.txt", 3);
+    try {
+        sim.runSimulation(dt, nSteps);
     } catch (std::exception& e) {
         std::cerr << e.what() << std::endl;
         return 1;
     }
 
+    // save state of simulation
+    sim.saveState2file(nSteps, "../out/state.txt");
+
     return 0;
 }
\ No newline at end of file
-- 
GitLab