Skip to content

Commit

Permalink
update testing and previous implementation; debug octree implementati…
Browse files Browse the repository at this point in the history
…on and makefile configuration
  • Loading branch information
jdinovi committed Dec 29, 2023
1 parent 6ae21b6 commit 1829075
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
2 changes: 1 addition & 1 deletion include/octree.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Octree {
void build(std::vector<std::shared_ptr<T>>& objPtrs);

// Members
std::vector<Body*> objPtrs;
std::vector<std::shared_ptr<T>> objPtrs;
std::array<float, 3> centerOfMass;
float* totalMass;

Expand Down
3 changes: 1 addition & 2 deletions src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ GravitationalEnvironment<T>::GravitationalEnvironment(const std::vector<std::sha
// Get a vector of the filenames in the data directory
std::vector<std::string> 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";
Expand Down Expand Up @@ -212,7 +212,6 @@ void GravitationalEnvironment<T>::simulate(const double duration, const double t
template <typename T>
// Reset the environment
void GravitationalEnvironment<T>::reset() {

time = 0;
}

Expand Down
22 changes: 13 additions & 9 deletions src/octree.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include "./../include/octree.h"
#include <array>
#include <iostream>

template <typename T>
Octree<T>::Octree(std::array<float, 2>& xCoords, std::array<float, 2>& yCoords, std::array<float, 2>& 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 <typename T>
Expand Down Expand Up @@ -57,17 +58,17 @@ void Octree<T>::insert(std::shared_ptr<T> 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.;
Expand Down Expand Up @@ -116,7 +117,7 @@ void Octree<T>::insert(std::shared_ptr<T> objPtr) {
}

// Instantiate a new octree with the calculated coordinates
Octree<T>* newOctreePtr = Octree(&xCoordsNew, &yCoordsNew, &zCoordsNew);
Octree<T>* newOctreePtr = new Octree<T>(xCoordsNew, yCoordsNew, zCoordsNew);

// Insert the new octree into the correct child with if statements (both location, and if child exists)
if (xFlag & yFlag & zFlag) {
Expand Down Expand Up @@ -166,7 +167,10 @@ void Octree<T>::insert(std::shared_ptr<T> objPtr) {
// Build the octree
template <typename T>
void Octree<T>::build(std::vector<std::shared_ptr<T>>& 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<Particle>;
template class Octree<Body>;
10 changes: 4 additions & 6 deletions test/test_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -28,11 +28,9 @@ auto particle1Ptr = std::make_shared<Particle>(&initial_position1, &initial_velo
auto particle2Ptr = std::make_shared<Particle>(&initial_position2, &initial_velocity2, mass);
std::vector<std::shared_ptr<Particle>> particles = {particle1Ptr, particle2Ptr};

// Initialize an environment
GravitationalEnvironment env1(particles, true);
GravitationalEnvironment env2(particles, true, "funPrefix");


// Initialize a particle environment
GravitationalEnvironment<Particle> env1(particles, true);
GravitationalEnvironment<Particle> env2(particles, true, "funPrefix");



Expand Down

0 comments on commit 1829075

Please sign in to comment.