Skip to content

Commit

Permalink
Refactored files into header and cpp files
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaunak Badani committed Sep 22, 2023
1 parent 27a0fa9 commit 9443a32
Show file tree
Hide file tree
Showing 30 changed files with 1,843 additions and 1,509 deletions.
62 changes: 62 additions & 0 deletions complex2d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "complex2d.h"
#include "utilities.h"

Complex2D::Complex2D()
{
this->setupSystem();
this->initializePositions();
this->initializeVelocities();
}

void Complex2D::setupSystem() {
this->numberOfParticles = numParticles;
this->systemDimensionality = 2;
for(int i = 0 ; i < this->numberOfParticles ; i++)
this->masses.push_back(1.0f);
}

void Complex2D::initializePositions() {
this->positions = initializeRandomVector(this->numberOfParticles,
this->systemDimensionality,
-1.0f, 1.0f);
}

float Complex2D::potentialEnergy(std::vector<std::vector<float>>& pos) {
float peValue = 0;
for(int i = 0 ; i < this->numberOfParticles ; i++)
peValue += U(pos[i]);
return peValue;
}

float Complex2D::potentialEnergy() {
return potentialEnergy(this->positions);
}


std::vector<std::vector<float>> Complex2D::force(std::vector<std::vector<float>>& pos) {
std::vector<std::vector<float>> returnValue;
for(int i = 0 ; i < this->numberOfParticles ; i++)
returnValue.push_back(F(pos[i]));
return returnValue;
}

std::vector<std::vector<float>> Complex2D::force() {
return this->force(this->positions);
}

float Complex2D::U(std::vector<float>& pos) {
float x = pos[0];
float y = pos[1];

return 10 * (pow(x, 10) + pow(y, 10)) + 5 * sin(2 * M_PI * x) * cos(2 * M_PI * y);
}

std::vector<float> Complex2D::F(std::vector<float>& pos) {
float x = pos[0];
float y = pos[1];

float fX = -1 * (100 * pow(x, 9) + 10 * M_PI * cos(2 * M_PI * x) * cos(2 * M_PI * y));
float fY = -1 * (100 * pow(y, 9) - 10 * M_PI * sin(2 * M_PI * x) * sin(2 * M_PI * y));

return {fX, fY};
}
60 changes: 10 additions & 50 deletions complex2d.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,64 +8,24 @@ class Complex2D : public System {

public:

Complex2D() {
this->setupSystem();
this->initializePositions();
this->initializeVelocities();
}
Complex2D();

void setupSystem() {
this->numberOfParticles = numParticles;
this->systemDimensionality = 2;
for(int i = 0 ; i < this->numberOfParticles ; i++)
this->masses.push_back(1.0f);
}
void setupSystem();

void initializePositions() {
this->positions = initializeRandomVector(this->numberOfParticles,
this->systemDimensionality,
-1.0f, 1.0f);
}
void initializePositions();

float potentialEnergy(std::vector<std::vector<float>>& pos) {
float peValue = 0;
for(int i = 0 ; i < this->numberOfParticles ; i++)
peValue += U(pos[i]);
return peValue;
}
float potentialEnergy(std::vector<std::vector<float>>& pos);

float potentialEnergy() {
return potentialEnergy(this->positions);
}
float potentialEnergy();

std::vector<std::vector<float>> force(std::vector<std::vector<float>>& pos) {
std::vector<std::vector<float>> returnValue;
for(int i = 0 ; i < this->numberOfParticles ; i++)
returnValue.push_back(F(pos[i]));
return returnValue;
}

std::vector<std::vector<float>> force() {
return this->force(this->positions);
}
std::vector<std::vector<float>> force(std::vector<std::vector<float>>& pos);

std::vector<std::vector<float>> force();

protected:
float U(std::vector<float>& pos) {
float x = pos[0];
float y = pos[1];

return 10 * (pow(x, 10) + pow(y, 10)) + 5 * sin(2 * M_PI * x) * cos(2 * M_PI * y);
}

std::vector<float> F(std::vector<float>& pos) {
float x = pos[0];
float y = pos[1];

float fX = -1 * (100 * pow(x, 9) + 10 * M_PI * cos(2 * M_PI * x) * cos(2 * M_PI * y));
float fY = -1 * (100 * pow(y, 9) - 10 * M_PI * sin(2 * M_PI * x) * sin(2 * M_PI * y));
float U(std::vector<float>& pos);

return {fX, fY};
}
std::vector<float> F(std::vector<float>& pos);

};

Expand Down
112 changes: 112 additions & 0 deletions config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
#include "config.h"
#include <string>
#include <mpi.h>
#include <iostream>

float samplingTemperature = 0.3;
std::vector<float> samplingTemperatures = {0.3f, 2.0f};
int numSteps = 1000;
int numParticles = 1;
int outputPeriod = 100;
std::string runType = "nve";
std::string runName = "currentRun";
std::string systemName = "1D_Smit";
std::string rst;
float tauValue = 0.024f;
bool isRstPresent = false;
bool ada = false;
bool useScalingProtocol = false;

float temperature() {
int noOfReplicas;

MPI_Comm_size( MPI_COMM_WORLD, &noOfReplicas );

if(noOfReplicas > 1) {
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
return samplingTemperatures[rank];
}
return samplingTemperature;
}

void setDefaultConfig() {
samplingTemperature = 0.3;
samplingTemperatures = {0.3f, 2.0f};
numSteps = 1000;
numParticles = 1;
outputPeriod = 100;
runType = "nve";
runName = "currentRun";
systemName = "1D_Smit";
tauValue = 0.024f;
isRstPresent = false;
ada = false;
useScalingProtocol = false;
}

void printConfig(int rank) {
std::cout << "Temperature : " << temperature() << std::endl;

if(rank == 0) {
std::cout << "Number of steps : " << numSteps << std::endl;
std::cout << "Number of particles : " << numParticles << std::endl;
std::cout << "Output Period : " << outputPeriod << std::endl;
std::cout << "Run Type : " << runType << std::endl;
std::cout << "System : " << systemName << std::endl;
std::cout << "Tau Value : " << tauValue << std::endl;
std::cout << "Use Scaling Protocol : " << useScalingProtocol << std::endl;
}

}

void getConfigFromFile(std::string fileName) {

std::ifstream jsonFileObject(fileName);
json data = json::parse(jsonFileObject);

if(data.contains("temperature"))
samplingTemperature = data["temperature"];

if(data.contains("temperatures")) {
for(int i = 0 ; i < data["temperatures"].size() ; i++)
samplingTemperatures[i] = data["temperatures"][i];
}

if(data.contains("num_steps"))
numSteps = data["num_steps"];

if(data.contains("output_period"))
outputPeriod = data["output_period"];

if(data.contains("run_type"))
runType = data["run_type"];

if(data.contains("run_name"))
runName = data["run_name"];

if(data.contains("system"))
systemName = data["system"];

if(data.contains("tau"))
tauValue = data["tau"];

if(data.contains("rst")) {
isRstPresent = true;
rst = data["rst"];
}

if(data.contains("num_particles"))
numParticles = data["num_particles"];

if(data.contains("ada"))
ada = data["ada"];

if(data.contains("use_scaling_protocol"))
useScalingProtocol = data["use_scaling_protocol"];



// if(data.contains("temperatures"))
// samplingTemperatures = data["temperatures"];
}
124 changes: 18 additions & 106 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,118 +3,30 @@

#include <fstream>
#include "json.hpp"
#include <mpi.h>

using json = nlohmann::json;

float samplingTemperature;
std::vector<float> samplingTemperatures;
int numSteps;
int numParticles;
int outputPeriod;
std::string runType;
std::string runName;
std::string systemName;
std::string rst;
bool isRstPresent;
bool ada;
float tauValue;
bool useScalingProtocol;
extern float samplingTemperature;
extern std::vector<float> samplingTemperatures;
extern int numSteps;
extern int numParticles;
extern int outputPeriod;
extern std::string runType;
extern std::string runName;
extern std::string systemName;
extern std::string rst;
extern bool isRstPresent;
extern bool ada;
extern float tauValue;
extern bool useScalingProtocol;

float temperature() {
int noOfReplicas;
float temperature();

MPI_Comm_size( MPI_COMM_WORLD, &noOfReplicas );
void setDefaultConfig();

if(noOfReplicas > 1) {
int rank;
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
return samplingTemperatures[rank];
}
return samplingTemperature;
}
void printConfig(int rank);

void setDefaultConfig() {
samplingTemperature = 0.3;
samplingTemperatures = {0.3f, 2.0f};
numSteps = 1000;
numParticles = 1;
outputPeriod = 100;
runType = "nve";
runName = "currentRun";
systemName = "1D_Smit";
tauValue = 0.024f;
isRstPresent = false;
ada = false;
useScalingProtocol = false;
}

void printConfig(int rank) {
std::cout << "Temperature : " << temperature() << std::endl;

if(rank == 0) {
std::cout << "Number of steps : " << numSteps << std::endl;
std::cout << "Number of particles : " << numParticles << std::endl;
std::cout << "Output Period : " << outputPeriod << std::endl;
std::cout << "Run Type : " << runType << std::endl;
std::cout << "System : " << systemName << std::endl;
std::cout << "Tau Value : " << tauValue << std::endl;
std::cout << "Use Scaling Protocol : " << useScalingProtocol << std::endl;
}

}

void getConfigFromFile(std::string fileName) {

std::ifstream jsonFileObject(fileName);
json data = json::parse(jsonFileObject);

if(data.contains("temperature"))
samplingTemperature = data["temperature"];

if(data.contains("temperatures")) {
for(int i = 0 ; i < data["temperatures"].size() ; i++)
samplingTemperatures[i] = data["temperatures"][i];
}

if(data.contains("num_steps"))
numSteps = data["num_steps"];

if(data.contains("output_period"))
outputPeriod = data["output_period"];

if(data.contains("run_type"))
runType = data["run_type"];

if(data.contains("run_name"))
runName = data["run_name"];

if(data.contains("system"))
systemName = data["system"];

if(data.contains("tau"))
tauValue = data["tau"];

if(data.contains("rst")) {
isRstPresent = true;
rst = data["rst"];
}

if(data.contains("num_particles"))
numParticles = data["num_particles"];

if(data.contains("ada"))
ada = data["ada"];

if(data.contains("use_scaling_protocol"))
useScalingProtocol = data["use_scaling_protocol"];



// if(data.contains("temperatures"))
// samplingTemperatures = data["temperatures"];
}



void getConfigFromFile(std::string fileName);

#endif
Loading

0 comments on commit 9443a32

Please sign in to comment.