Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Octree and Barnes Hut Implementation #20

Merged
merged 27 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
a867eb4
made progress on octree class; working on building the octree class
jdinovi Dec 26, 2023
6ae21b6
finished implementing octree class and members/functions; need to fix…
jdinovi Dec 29, 2023
1829075
update testing and previous implementation; debug octree implementati…
jdinovi Dec 29, 2023
33ce06b
add inclusion of <memory> to octree files
jdinovi Dec 29, 2023
aad4eaf
add tests for body implementation; slightly edit body.cpp implementation
jdinovi Dec 29, 2023
d92a170
fixed implementation of octree
jdinovi Jan 3, 2024
5a41be6
fixed octree insertion implementation; added testing for octree inser…
jdinovi Jan 3, 2024
681d49f
fixed basic pointers vs shared pointers in octree implementation; add…
jdinovi Jan 3, 2024
dc8b482
fixed issue with types; need to add more coverage and implement barne…
jdinovi Jan 3, 2024
4f47c7d
change every double to a float; implemented barnes-hut force algo; ne…
jdinovi Jan 4, 2024
142391b
implemented barnes-hut force calculation; need to implement testing
jdinovi Jan 4, 2024
c7610be
Add functional header
Adam-Boesky Jan 4, 2024
197ee76
added testing for barnes-hut force calculation algorithm
jdinovi Jan 4, 2024
72d9c10
add an include header of math.h to test_environment
jdinovi Jan 4, 2024
415735e
fix type issue in test of barnes-hut algo
jdinovi Jan 4, 2024
bd1dc27
added more testing for octree class; changed totalMass to a float fro…
jdinovi Jan 5, 2024
590e272
Pulled main into octree branch before making a PR; just have to fix y…
jdinovi Jan 5, 2024
5bc7f80
added line to workaround makefile issue on John's laptop; changed to …
jdinovi Jan 5, 2024
0538478
change include to INC_DIR variable; fix warning in test_statistics.cpp
jdinovi Jan 5, 2024
6537dd1
small change to push and test workflow
jdinovi Jan 5, 2024
488159d
commented out test case that detects for errors in loading file
jdinovi Jan 5, 2024
53a940b
add line to coverage to debug
jdinovi Jan 5, 2024
b281768
added debugging print to coverage workflow
jdinovi Jan 5, 2024
de11e74
added dummy executable for the statistics file; changed += from Makef…
jdinovi Jan 5, 2024
c5fc4cc
adjut Makefile for John's local machine; testing automation on github
jdinovi Jan 9, 2024
84277b2
Fix small typo in Makefile to fix automation (LDLFLAGS --> LDFLAGS)
jdinovi Jan 9, 2024
764c747
change initilization of coordinate looping in environment to at first…
jdinovi Jan 10, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ jobs:
run: |
sudo apt-get update -q
sudo apt-get install libyaml-cpp-dev -y
sudo apt-get install bc -y

- name: Evaluate coverage
run: |
make coverage > coverage_output.txt
cat coverage_output.txt

COVERAGE=$(make coverage | grep 'TOTAL COVERAGE' | grep -Eo '[0-9]+')

if [ "$COVERAGE" -gt 89 ]; then
Expand Down
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CXX = g++
CXXFLAGS = -std=c++17 -Wall --coverage
CXXFLAGS = -g -std=c++17 -Wall --coverage
LDFLAGS = -lyaml-cpp
SRC_DIR = src
INC_DIR = include
Expand All @@ -19,7 +19,7 @@ TEST_SRCS = $(wildcard $(TEST_DIR)/*.cpp)
TEST_OBJS = $(patsubst $(TEST_DIR)/%.cpp,$(TEST_OBJ_DIR)/%.o,$(TEST_SRCS))

# Include directories
INC_DIRS = -I $(INC_DIR)
INC_DIRS = -I $(INC_DIR) #-I /opt/homebrew/Cellar/yaml-cpp/0.8.0/include

# Linking step for src files
$(BIN_DIR)/$(TARGET): $(OBJS)
Expand Down Expand Up @@ -62,7 +62,7 @@ coverage:
for filename in $(filter-out $(SRC_DIR)/simulation.cpp, $(SRCS)); do \
obj_file=$(OBJ_DIR)/$${filename%.cpp}.o; \
result=$$(gcov --object-directory=$(OBJ_DIR) $${filename} -n | grep -v ".*simulation.*" | grep -v ".*\.h" | grep -A 1 "src"); \
results+="\n\n$$result"; \
results="$$results\n\n$$result"; \
lines=$$(echo $$result | grep -Eo "[0-9]+$$"); \
line_pct=$$(echo $$result | grep -Eo '([0-9]+\.[0-9]+)\%' | sed 's/\%//'); \
total_lines=$$((total_lines + lines)); \
Expand Down
2 changes: 1 addition & 1 deletion include/body.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Body : public Particle {
public:

// Child class constructor
explicit Body(const std::array<double, 3>* initial_position, const std::array<double, 3>* initial_velocity, double mass, float radius=0);
explicit Body(const std::array<float, 3>* initial_position, const std::array<float, 3>* initial_velocity, float mass, float radius=0);

// New physical member
float radius;
Expand Down
61 changes: 36 additions & 25 deletions include/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,50 @@
#include <vector>
#include <array>
#include <map>
#include <memory> // Include for smart pointers
#include "particle.h"
#include <memory>
#include <functional>

#include "./particle.h"
#include "./octree.h"

template <typename T>
class GravitationalEnvironment {
public:
// Constructors
GravitationalEnvironment(const std::vector<std::shared_ptr<T>>& particlePtrs, const bool log, std::string logFilePrefix = "run");
GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix = "run");
class GravitationalEnvironment{

public:
// Constructors
GravitationalEnvironment(const std::vector<std::shared_ptr<T>>& particlePtrs, const bool log, std::string logFilePrefix="run", std::string forceAlgorithm="pair-wise");
GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix = "run", std::string forceAlgorithm="pair-wise");

// Define member functions
void loadParticlesFromConfig(std::string configFileName);
std::vector<std::array<double, 3>> getForces(const double timestep);
void updateAll(const std::vector<std::array<double, 3>>& forces, const double timestep);
void step(const double timestep);
void simulate(const double duration, const double timestep);
std::string getStepLog() const;
std::string getLogHeader() const;
void reset();
// Callable member that we will set to pair-wise or Barnes-Hut force algorithm
std::function<std::vector<std::array<float, 3>>(float)> getForces;

// Instantiation of the physical members
std::vector<std::shared_ptr<T>> particlePtrs; // Changed to std::shared_ptr
bool log;
double time;
int nParticles; // This should be calculated in the constructor.
std::string logFileName;
// Define member functions for force algorithms
std::vector<std::array<float, 3>> getForcesPairWise(const float timestep);
std::vector<std::array<float, 3>> getForcesBarnesHut(const float timestep);

void loadParticlesFromConfig(std::string configFileName);
std::array<float, 3> calculateForceBarnesHut(std::shared_ptr<T> objPtr, std::shared_ptr<Octree<T>> currPtr, std::array<float, 3> netForce, float theta);
void updateAll(const std::vector<std::array<float, 3>>& forces, const float timestep);
void step(const float timestep);
void simulate(const float duration, const float timestep);
std::string getStepLog() const;
std::string getLogHeader() const;
void reset();

// Instantiation of the physical members
std::vector<std::shared_ptr<T>> particlePtrs;
bool log;
float time;
int nParticles;
std::string logFileName;
Octree<T> envOctree;

private:
// Instantiation of the physical members
std::string logFilePrefix;
private:
// Instantiation of the physical members
std::string logFilePrefix;
};

// Helper functions
int getLargestLabelNumber(const std::vector<std::string>& filenames, const std::string logFilePrefix);
float getEuclidianDistance(std::array<float, 3> coords1, std::array<float, 3> coords2);
std::map<std::string, std::map<std::string, std::string>> loadConfig(const std::string& fileName);
25 changes: 14 additions & 11 deletions include/octree.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,34 @@ class Octree {
public:

// Octree constructor
Octree(std::array<float, 2>& xCoords, std::array<float, 2>& yCoords, std::array<float, 2>& zCoords);
Octree(std::array<float, 2>& xCoords, std::array<float, 2>& yCoords, std::array<float, 2>& zCoords, bool internal);

// Member functions
void clear();
void clearOctree();
void updateCoords(std::array<float, 2>& newXCoords, std::array<float, 2>& newYCoords, std::array<float, 2>& newZCoords);
void insert(std::shared_ptr<T> objPtr);
void build(std::vector<std::shared_ptr<T>>& objPtrs);

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

// Dimensions of the current octant
std::array<float, 2> xCoords;
std::array<float, 2> yCoords;
std::array<float, 2> zCoords;

// Octree children --> 0-7 based on 2D convention in postive z, and then 2D convention in negative z, observing from above
Octree<T>* child0;
Octree<T>* child1;
Octree<T>* child2;
Octree<T>* child3;
Octree<T>* child4;
Octree<T>* child5;
Octree<T>* child6;
Octree<T>* child7;
std::shared_ptr<Octree<T>> child0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do u think u could add like comments for each of the children that just say which octants they refer to? Might be helpful for understanding the logic in octree.cpp

std::shared_ptr<Octree<T>> child1;
std::shared_ptr<Octree<T>> child2;
std::shared_ptr<Octree<T>> child3;
std::shared_ptr<Octree<T>> child4;
std::shared_ptr<Octree<T>> child5;
std::shared_ptr<Octree<T>> child6;
std::shared_ptr<Octree<T>> child7;


};
10 changes: 5 additions & 5 deletions include/particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ class Particle {

public:
// Constructors
Particle(const std::array<double, 3>* initial_position, const std::array<double, 3>* initial_velocity, double mass);
Particle(const std::array<float, 3>* initial_position, const std::array<float, 3>* initial_velocity, float mass);

// Define member functions
void update (const std::array<double, 3>* force, const double timestep);
void update (const std::array<float, 3>* force, const float timestep);

// Instantiation of the physical members
std::array<double, 3> position;
std::array<double, 3> velocity;
double mass;
std::array<float, 3> position;
std::array<float, 3> velocity;
float mass;
};
7 changes: 5 additions & 2 deletions include/statistics.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ extern std::mt19937 GENERATOR;

// Distribution template used to sample from random distributions
template <typename Distribution>
std::vector<double> sampleFromDistribution(size_t n, Distribution& distribution) {
std::vector<float> sampleFromDistribution(size_t n, Distribution& distribution) {
// Generate samples
std::vector<double> samples(n);
std::vector<float> samples(n);
for (size_t i = 0; i < n; ++i) {
samples[i] = distribution(GENERATOR);
}
return samples;
}

// Dummy executable to work with testing framework
int dummyExecutable();
2 changes: 1 addition & 1 deletion src/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@
float DENSITY = 4E3;

// Constructor definition
Body::Body(const std::array<double, 3>* initial_position, const std::array<double, 3>* initial_velocity, double mass, float radius)
Body::Body(const std::array<float, 3>* initial_position, const std::array<float, 3>* initial_velocity, float mass, float radius)
: Particle(initial_position, initial_velocity, mass), radius((radius == 0) ? pow(3 * mass / (4 * M_PI * DENSITY), 1./3.) : radius) {};
Loading
Loading