Skip to content

Commit

Permalink
Implement configs (#18)
Browse files Browse the repository at this point in the history
* made progress on octree class; working on building the octree class

* finished implementing octree class and members/functions; need to fix segmentation fault in testing

* update testing and previous implementation; debug octree implementation and makefile configuration

* add inclusion of <memory> to octree files

* add tests for body implementation; slightly edit body.cpp implementation

* Implement config file functionality.

* Fix everything

* Add yaml-cpp install to workflows

* Debug

---------

Co-authored-by: jdinovi <[email protected]>
  • Loading branch information
Adam-Boesky and jdinovi authored Jan 1, 2024
1 parent 5ad3d9b commit bc4b8ad
Show file tree
Hide file tree
Showing 15 changed files with 676 additions and 65 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ jobs:
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install requirements
run: |
sudo apt-get update -q
sudo apt-get install libyaml-cpp-dev -y
- name: Evaluate coverage
run: |
Expand Down
5 changes: 5 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install requirements
run: |
sudo apt-get update -q
sudo apt-get install libyaml-cpp-dev -y
- name: Run tests
run: |
result=$(make test)
Expand Down
5 changes: 3 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CXX = g++
CXXFLAGS = -std=c++17 -Wall --coverage
LDFLAGS = -lyaml-cpp
SRC_DIR = src
INC_DIR = include
OBJ_DIR = obj
Expand All @@ -22,15 +23,15 @@ INC_DIRS = -I $(INC_DIR)

# Linking step for src files
$(BIN_DIR)/$(TARGET): $(OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# Compiling step for src files
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp
$(CXX) $(CXXFLAGS) $(INC_DIRS) -c -o $@ $<

# Linking step for test files
$(BIN_DIR)/$(TEST_TARGET): $(filter-out $(OBJ_DIR)/simulation.o, $(OBJS)) $(TEST_OBJS)
$(CXX) $(CXXFLAGS) -o $@ $^
$(CXX) $(CXXFLAGS) -o $@ $^ $(LDFLAGS)

# Compiling step for test files
$(TEST_OBJ_DIR)/%.o: $(TEST_DIR)/%.cpp
Expand Down
29 changes: 29 additions & 0 deletions configs/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
global:
nParticles: 1000
mass:
dist: constant
val: 100000000
x:
dist: uniform
min: 0
max: 4
y:
dist: uniform
min: -2
max: 4
z:
dist: uniform
min: 1
max: 10
vx:
dist: uniform
min: 0
max: 4
vy:
dist: normal
mu: -2
sigma: 4
vz:
dist: uniform
min: -10
max: 10
53 changes: 28 additions & 25 deletions include/environment.h
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
#pragma once
#include <vector>
#include <array>
#include <memory>
#include <map>
#include <memory> // Include for smart pointers
#include "particle.h"

#include "./particle.h"

template <typename T>
class GravitationalEnvironment{

public:
// Constructors
// GravitationalEnvironment(const std::vector<std::shared_ptr<T>>& particlePtrs, const bool log);
GravitationalEnvironment(const std::vector<std::shared_ptr<T>>& particlePtrs, 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");
GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix = "run");

// Define member functions
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();
// 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();

// Instantiation of the physical members
std::vector<std::shared_ptr<T>> particlePtrs; // Changed to std::shared_ptr
bool log;
double time;
int nParticles;
std::string logFileName;
// 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;

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

// Helper functions
int getLargestLabelNumber(const std::vector<std::string>& filenames, const std::string logFilePrefix);


typedef GravitationalEnvironment<Particle> ParticleEnvironment;
std::map<std::string, std::map<std::string, std::string>> loadConfig(const std::string& fileName);
40 changes: 40 additions & 0 deletions include/octree.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#pragma once

#include <vector>
#include <memory>

#include "body.h"

template <typename T>
class Octree {
public:

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

// Member functions
void clear();
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;

// 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;

};
18 changes: 18 additions & 0 deletions include/statistics.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once
#include <vector>
#include <random>

// Assuming you have the random device and generator declared somewhere
extern std::random_device rd;
extern std::mt19937 GENERATOR;

// Distribution template used to sample from random distributions
template <typename Distribution>
std::vector<double> sampleFromDistribution(size_t n, Distribution& distribution) {
// Generate samples
std::vector<double> samples(n);
for (size_t i = 0; i < n; ++i) {
samples[i] = distribution(GENERATOR);
}
return samples;
}
2 changes: 1 addition & 1 deletion src/body.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ 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)
: Particle(initial_position, initial_velocity, mass), radius(radius == 0 ? pow(3 * mass / (4 * M_PI * DENSITY), 1/3) : radius) {};
: Particle(initial_position, initial_velocity, mass), radius((radius == 0) ? pow(3 * mass / (4 * M_PI * DENSITY), 1./3.) : radius) {};
Loading

0 comments on commit bc4b8ad

Please sign in to comment.