Skip to content

Commit

Permalink
Fix everything
Browse files Browse the repository at this point in the history
  • Loading branch information
Adam-Boesky committed Jan 1, 2024
1 parent 431121f commit dc63a76
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 62 deletions.
9 changes: 5 additions & 4 deletions include/environment.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,8 @@ 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);
GravitationalEnvironment(const std::string configFileName, const bool log);
GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix);
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
void loadParticlesFromConfig(std::string configFileName);
Expand All @@ -32,6 +30,9 @@ class GravitationalEnvironment {
int nParticles; // This should be calculated in the constructor.
std::string logFileName;

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

// Helper functions
Expand Down
61 changes: 7 additions & 54 deletions src/environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,61 +120,13 @@ GravitationalEnvironment<T>::GravitationalEnvironment(const std::vector<std::sha
}
}

// Get the largest log file number and create new log file
int lastLogNum = getLargestLabelNumber(lastLogFileNames, "run");
logFileName = dataPath + "/run" + std::to_string(lastLogNum + 1) + ".csv";
}
}
GravitationalEnvironment::GravitationalEnvironment(const std::vector<std::shared_ptr<Particle>>& particlePtrs, const bool log, std::string logFilePrefix = "run")
: particlePtrs(particlePtrs), log(log), time(0) {

// Declare the number of particles
nParticles = particlePtrs.size();

// Create a log file if we want one
if (log == true) {

// Get a vector of the filenames in the data directory
std::vector<std::string> lastLogFileNames;
std::string dataPath = REPOPATH + "/data";
for (const auto& entry : fs::directory_iterator(dataPath)) {
if (fs::is_regular_file(entry.status())) {
lastLogFileNames.push_back(entry.path().filename().string());
}
}

// Get the largest log file number and create new log file
int lastLogNum = getLargestLabelNumber(lastLogFileNames, logFilePrefix);
logFileName = dataPath + "/" + logFilePrefix + std::to_string(lastLogNum + 1) + ".csv";
}
}
GravitationalEnvironment::GravitationalEnvironment(const std::string configFileName, const bool log)
: log(log), time(0) {

// Get particles
loadParticlesFromConfig(configFileName);

// Declare the number of particles
nParticles = particlePtrs.size();

// Create a log file if we want one
if (log == true) {

// Get a vector of the filenames in the data directory
std::vector<std::string> lastLogFileNames;
std::string dataPath = REPOPATH + "/data";
for (const auto& entry : fs::directory_iterator(dataPath)) {
if (fs::is_regular_file(entry.status())) {
lastLogFileNames.push_back(entry.path().filename().string());
}
}

// Get the largest log file number and create new log file
int lastLogNum = getLargestLabelNumber(lastLogFileNames, "run");
logFileName = dataPath + "/run" + std::to_string(lastLogNum + 1) + ".csv";
}
}
GravitationalEnvironment::GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix = "run")
template <typename T>
GravitationalEnvironment<T>::GravitationalEnvironment(const std::string configFileName, const bool log, std::string logFilePrefix)
: log(log), time(0) {

// Get particles
Expand Down Expand Up @@ -203,7 +155,8 @@ GravitationalEnvironment::GravitationalEnvironment(const std::string configFileN


// Load a full environment from the configuration file
void GravitationalEnvironment::loadParticlesFromConfig(const std::string configFileName) {
template <typename T>
void GravitationalEnvironment<T>::loadParticlesFromConfig(const std::string configFileName) {

// Get configuration map
std::map<std::string, std::map<std::string, std::string>> configMap = loadConfig(configFileName);
Expand Down Expand Up @@ -253,7 +206,7 @@ void GravitationalEnvironment::loadParticlesFromConfig(const std::string configF
double mass = envParams.at("mass")[i];

// Create Particle instance with pointers to elements in the positions and velocities vectors
particlePtrs.push_back(std::make_shared<Particle>(&positions[i], &velocities[i], mass));
particlePtrs.push_back(std::make_shared<T>(&positions[i], &velocities[i], mass));
}
}

Expand Down Expand Up @@ -367,7 +320,7 @@ void GravitationalEnvironment<T>::simulate(const double duration, const double t

// If logging, append to log string
if (log == true) {
logStr += std::to_string(i * timestep) + ",";
logStr += std::to_string(i * timestep);
logStr += getStepLog() + "\n";
}
std::cout << i * timestep << ",\t" << getStepLog() << "\n";
Expand All @@ -390,7 +343,7 @@ void GravitationalEnvironment<T>::simulate(const double duration, const double t
std::cerr << "Failed to open the file: " << logFileName << std::endl;
} else {
logFile << logStr;
std::cout << "Successfully logged to " + logFileName;
std::cout << "Successfully logged to " + logFileName + "\n";
}
logFile.close();
}
Expand Down
2 changes: 1 addition & 1 deletion src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include "../include/environment.h"

int main() {
GravitationalEnvironment defaultEnv("default.yaml", true);
GravitationalEnvironment<Body> defaultEnv("default.yaml", true);

// Simulate
defaultEnv.simulate(3, 0.5);
Expand Down
6 changes: 3 additions & 3 deletions test/test_environment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ TEST_CASE("Load Config File") {
CHECK(defaultConfig["global"]["nParticles"] == "1000");
}

void checkDefaultEnv(GravitationalEnvironment& env) {
void checkDefaultEnv(GravitationalEnvironment<Particle>& env) {
// Check that it all looks good... that default vals are:
// mass:
// dist: constant
Expand Down Expand Up @@ -231,8 +231,8 @@ void checkDefaultEnv(GravitationalEnvironment& env) {
TEST_CASE("Load Particles From Config") {

// Get a defualt environment class
GravitationalEnvironment defaultEnv("default.yaml", true);
GravitationalEnvironment defaultEnv2("default.yaml", true, "prefixyprefix");
GravitationalEnvironment<Particle> defaultEnv("default.yaml", true);
GravitationalEnvironment<Particle> defaultEnv2("default.yaml", true, "prefixyprefix");

// Check 'em
checkDefaultEnv(defaultEnv);
Expand Down

0 comments on commit dc63a76

Please sign in to comment.