From 18290755d13ade3c5be29289f64eac53e32976dd Mon Sep 17 00:00:00 2001 From: jdinovi Date: Fri, 29 Dec 2023 18:40:14 -0500 Subject: [PATCH] update testing and previous implementation; debug octree implementation and makefile configuration --- include/octree.h | 2 +- src/environment.cpp | 3 +-- src/octree.cpp | 22 +++++++++++++--------- test/test_environment.cpp | 10 ++++------ 4 files changed, 19 insertions(+), 18 deletions(-) diff --git a/include/octree.h b/include/octree.h index eb31896..723c911 100644 --- a/include/octree.h +++ b/include/octree.h @@ -16,7 +16,7 @@ class Octree { void build(std::vector>& objPtrs); // Members - std::vector objPtrs; + std::vector> objPtrs; std::array centerOfMass; float* totalMass; diff --git a/src/environment.cpp b/src/environment.cpp index 6243e34..658d749 100644 --- a/src/environment.cpp +++ b/src/environment.cpp @@ -52,7 +52,7 @@ GravitationalEnvironment::GravitationalEnvironment(const std::vector lastLogFileNames; const char* repoPath = std::getenv("HOOTSIM_PATH"); - std::string dataPath = std::string(repoPath) + "/data"; + std::string dataPath = repoPath == nullptr ? "./data" : std::string(repoPath) + "/data"; if (!fs::exists(dataPath)) { fs::create_directory(dataPath); std::cout << "data directory created successfully.\n"; @@ -212,7 +212,6 @@ void GravitationalEnvironment::simulate(const double duration, const double t template // Reset the environment void GravitationalEnvironment::reset() { - time = 0; } diff --git a/src/octree.cpp b/src/octree.cpp index 0013e9b..6a9af6f 100644 --- a/src/octree.cpp +++ b/src/octree.cpp @@ -1,9 +1,10 @@ #include "./../include/octree.h" #include +#include template Octree::Octree(std::array& xCoords, std::array& yCoords, std::array& zCoords) - : xCoords(xCoords), yCoords(yCoords), zCoords(zCoords), totalMass(0) {}; + : totalMass(0), xCoords(xCoords), yCoords(yCoords), zCoords(zCoords) {}; // Recursively set every child to null in the tree, but preserving the tree template @@ -57,17 +58,17 @@ void Octree::insert(std::shared_ptr objPtr) { objPtrs.push_back(objPtr); // Instantiate the center of mass if it doesn't exist; update it otherwise - float newTotalMass = totalMass + objPtr->mass; - if (totalMass == 0) { - centerOfMass = objPtr->position; + float newTotalMass = *totalMass + objPtr->mass; + if (*totalMass == 0) { + std::copy(std::begin(objPtr->position), std::end(objPtr->position), std::begin(centerOfMass)); } else { for (int i = 0; i < 3; i++) { - centerOfMass[i] = (((centerOfMass[i] * totalMass)) + ((objPtr->position[i]) * (objPtr->mass))) / (newTotalMass); + centerOfMass[i] = (((centerOfMass[i] * (*totalMass))) + ((objPtr->position[i]) * (objPtr->mass))) / (newTotalMass); } } // Update the total mass - totalMass = newTotalMass; + *totalMass = newTotalMass; // Midpoints of coordinates float mX = (xCoords[0] + xCoords[1]) / 2.; @@ -116,7 +117,7 @@ void Octree::insert(std::shared_ptr objPtr) { } // Instantiate a new octree with the calculated coordinates - Octree* newOctreePtr = Octree(&xCoordsNew, &yCoordsNew, &zCoordsNew); + Octree* newOctreePtr = new Octree(xCoordsNew, yCoordsNew, zCoordsNew); // Insert the new octree into the correct child with if statements (both location, and if child exists) if (xFlag & yFlag & zFlag) { @@ -166,7 +167,10 @@ void Octree::insert(std::shared_ptr objPtr) { // Build the octree template void Octree::build(std::vector>& objPtrs) { - for (int i = 0; i < length(&objPtrs); i++) { - this->insert((&objPtrs)[i]); + for (int i = 0; i < objPtrs.size(); i++) { + this->insert(objPtrs[i]); } } + +template class Octree; +template class Octree; diff --git a/test/test_environment.cpp b/test/test_environment.cpp index 3b65dc1..777d1a1 100644 --- a/test/test_environment.cpp +++ b/test/test_environment.cpp @@ -14,7 +14,7 @@ double _G = 6.6743e-11; // Data path const char* repoPath = std::getenv("HOOTSIM_PATH"); -std::string dataPath = std::string(repoPath) + "/data"; +std::string dataPath = repoPath == nullptr ? "./data" : std::string(repoPath )+ "/data"; // Initialize particle specs double mass = 1E10; @@ -28,11 +28,9 @@ auto particle1Ptr = std::make_shared(&initial_position1, &initial_velo auto particle2Ptr = std::make_shared(&initial_position2, &initial_velocity2, mass); std::vector> particles = {particle1Ptr, particle2Ptr}; -// Initialize an environment -GravitationalEnvironment env1(particles, true); -GravitationalEnvironment env2(particles, true, "funPrefix"); - - +// Initialize a particle environment +GravitationalEnvironment env1(particles, true); +GravitationalEnvironment env2(particles, true, "funPrefix");