Skip to content

Commit

Permalink
Merge pull request #4 from MikeHeiber/development
Browse files Browse the repository at this point in the history
v2.0-alpha.3 Update - Improvements to the Utils and fixed a rare bug in the Simulation class when there are no valid events for an object.
  • Loading branch information
MikeHeiber authored May 20, 2017
2 parents 25dbddd + bf5e8f0 commit 7298349
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 118 deletions.
8 changes: 2 additions & 6 deletions Object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,8 @@ Object::~Object(){
Object::Object(){
time_created = 0;
tag = 0;
coords.x = 0;
coords.y = 0;
coords.z = 0;
coords_initial.x = 0;
coords_initial.y = 0;
coords_initial.z = 0;
coords.setXYZ(0,0,0);
coords_initial.setXYZ(0,0,0);
dx = 0;
dy = 0;
dz = 0;
Expand Down
4 changes: 2 additions & 2 deletions Simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ list<Event*>::iterator Simulation::chooseNextEvent(){
auto event_target_it = events.begin();
if(events.size()>1){
for(auto it=++events.begin();it!=events.end();++it){
if((*it)->getExecutionTime() < (*event_target_it)->getExecutionTime()){
if((*it)!=nullptr && ((*event_target_it)==nullptr || ((*it)->getExecutionTime() < (*event_target_it)->getExecutionTime()))){
event_target_it = it;
}
}
Expand Down Expand Up @@ -210,7 +210,7 @@ int Simulation::getNumSites(){
return Length*Width*Height;
}

int Simulation::getN_events_executed(){
long int Simulation::getN_events_executed(){
return N_events_executed;
}

Expand Down
4 changes: 2 additions & 2 deletions Simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Simulation{
virtual ~Simulation();
virtual bool checkFinished()=0;
virtual bool executeNextEvent()=0;
int getN_events_executed();
long int getN_events_executed();
int getId();
double getTime();
void initializeSimulation(const Parameters_Simulation& params,const int id);
Expand Down Expand Up @@ -113,7 +113,7 @@ class Simulation{
double Time;
int N_objects;
int N_objects_created;
int N_events_executed;
long int N_events_executed;
// Functions
};

Expand Down
129 changes: 35 additions & 94 deletions Utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@

#include "Utils.h"

double array_avg(const double data[],const int array_size){
double sum = 0;
for(int i=0;i<array_size;i++){
sum += data[i];
void createExponentialDOSVector(vector<double>& data,const double mode,const double urbach_energy,mt19937& gen){
exponential_distribution<double> dist_exp(1/urbach_energy);
auto rand_exp = bind(dist_exp,ref(gen));
normal_distribution<double> dist_gaus(0,2*urbach_energy/sqrt(2*Pi));
auto rand_gaus = bind(dist_gaus,ref(gen));
double energy;
for(int i=0;i<(int)data.size();i++){
energy = rand_gaus();
if(energy>0){
data[i] = mode+energy;
}
else{
data[i] = mode-rand_exp();
}
}
}

void createGaussianDOSVector(vector<double>& data,const double mean,const double stdev,mt19937& gen){
normal_distribution<double> dist(mean,stdev);
auto rand_gaus = bind(dist,ref(gen));
for(int i=0;i<(int)data.size();i++){
data[i] = rand_gaus();
}
return sum/array_size;
}

double array_stdev(const double data[],const int array_size){
double sum = 0;
double avg = array_avg(data,array_size);
for(int i=0;i<array_size;i++){
sum += (data[i]-avg)*(data[i]-avg);
bool importBooleanParam(const string& input,bool& error_status){
if(input.compare("true")==0){
return true;
}
else if(input.compare("false")==0){
return false;
}
else{
cout << "Error importing boolean parameter." << endl;
error_status = true;
return false;
}
return sqrt(sum/(array_size-1));
}

vector<double> MPI_calculateVectorAvg(const vector<double>& input_vector,const int procid,const int nproc){
Expand Down Expand Up @@ -77,9 +99,8 @@ vector<double> MPI_calculateVectorAvg(const vector<double>& input_vector,const i
return output_vector;
}

vector<double> MPI_calculateVectorSum(const vector<double>& input_vector,const int procid,const int nproc){
vector<double> MPI_calculateVectorSum(const vector<double>& input_vector,const int procid){
int data_size = 0;
int data_count = 0;
double *data = NULL;
double *sum = NULL;
vector<double> output_vector;
Expand All @@ -100,9 +121,8 @@ vector<double> MPI_calculateVectorSum(const vector<double>& input_vector,const i
return output_vector;
}

vector<int> MPI_calculateVectorSum(const vector<int>& input_vector,const int procid,const int nproc){
vector<int> MPI_calculateVectorSum(const vector<int>& input_vector,const int procid){
int data_size = 0;
int data_count = 0;
int *data = NULL;
int *sum = NULL;
vector<int> output_vector;
Expand Down Expand Up @@ -130,8 +150,6 @@ vector<double> MPI_gatherVectors(const vector<double>& input_vector,const int pr
double *data_all = NULL;
int *data_sizes = NULL;
int *data_displacement = NULL;
int max_data_size = 0;
double average = 0;
vector<double> output_vector;
if(procid==0){
data_sizes = (int *)malloc(sizeof(int)*nproc);
Expand Down Expand Up @@ -166,80 +184,3 @@ vector<double> MPI_gatherVectors(const vector<double>& input_vector,const int pr
return output_vector;
}

void createExponentialDOSVector(vector<double>& data,const double mode,const double urbach_energy,mt19937& gen){
exponential_distribution<double> dist_exp(1/urbach_energy);
auto rand_exp = bind(dist_exp,ref(gen));
normal_distribution<double> dist_gaus(0,2*urbach_energy/sqrt(2*Pi));
auto rand_gaus = bind(dist_gaus,ref(gen));
double energy;
for(int i=0;i<(int)data.size();i++){
energy = rand_gaus();
if(energy>0){
data[i] = mode+energy;
}
else{
data[i] = mode-rand_exp();
}
}
}

void createGaussianDOSVector(vector<double>& data,const double mean,const double stdev,mt19937& gen){
normal_distribution<double> dist(mean,stdev);
auto rand_gaus = bind(dist,ref(gen));
for(int i=0;i<(int)data.size();i++){
data[i] = rand_gaus();
}
}

double intpow(const double base,const int exponent){
double result = base;
for(int i=1;i<exponent;i++){
result *= base;
}
return result;
}

double vector_avg(const vector<int>& dataset){
double sum = 0;
double avg;
vector<int>::const_iterator it;
for(it=dataset.begin();it!=dataset.end();++it){
sum += *it;
}
avg = sum/dataset.size();
return avg;
}

double vector_avg(const vector<double>& dataset){
double sum = 0;
double avg;
vector<double>::const_iterator it;
for(it=dataset.begin();it!=dataset.end();++it){
sum += *it;
}
avg = sum/dataset.size();
return avg;
}

double vector_stdev(const vector<int>& dataset){
double sum = 0;
double avg, dev;
avg = vector_avg(dataset);
for(auto it=dataset.begin();it!=dataset.end();++it){
sum += (*it-avg)*(*it-avg);
}
dev = sqrt(sum/(dataset.size()-1));
return dev;
}

double vector_stdev(const vector<double>& dataset){
double sum = 0;
double avg, dev;
avg = vector_avg(dataset);
for(auto it=dataset.begin();it!=dataset.end();++it){
sum += (*it-avg)*(*it-avg);
}
dev = sqrt(sum/(dataset.size()-1));
return dev;
}

70 changes: 56 additions & 14 deletions Utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ struct Coords{
int x;
int y;
int z;
void setXYZ(const int& xval,const int& yval,const int& zval){
x = xval;
y = yval;
z = zval;
}
};

// Useful Constants
Expand All @@ -30,24 +35,61 @@ static constexpr double Light_speed = 2.99792458e8;
static constexpr double Pi = 3.14159265359;
static constexpr double Coulomb_constant = 8.987551787e9; // N m^2 C^-2

double array_avg(const double data[],const int size);
double array_stdev(const double data[],const int size);
void createExponentialDOSVector(vector<double>& data,const double mode,const double urbach_energy,mt19937& gen);
void createGaussianDOSVector(vector<double>& data,const double mean,const double stdev,mt19937& gen);
double intpow(const double base,const int exponent);
bool importBooleanParam(const string& input,bool& error_status);
vector<double> MPI_calculateVectorAvg(const vector<double>& input_vector,const int procid,const int nproc);
vector<double> MPI_calculateVectorSum(const vector<double>& input_vector,const int procid,const int nproc);
vector<int> MPI_calculateVectorSum(const vector<int>& input_vector,const int procid,const int nproc);
vector<double> MPI_calculateVectorSum(const vector<double>& input_vector,const int procid);
vector<int> MPI_calculateVectorSum(const vector<int>& input_vector,const int procid);
vector<double> MPI_gatherVectors(const vector<double>& input_vector,const int procid,const int nproc);
double vector_avg(const vector<int>& dataset);
double vector_avg(const vector<double>& dataset);
double vector_stdev(const vector<int>& dataset);
double vector_stdev(const vector<double>& dataset);

template<typename Derived,typename Base,typename Del>
unique_ptr<Derived,Del> static_unique_ptr_cast(unique_ptr<Base,Del>&& p){
auto d = static_cast<Derived*>(p.release());
return unique_ptr<Derived,Del>(d,std::move(p.get_deleter()));


// Template functions
template<typename T>
double array_avg(const T data[],const int array_size){
double sum = 0;
for(int i=0;i<array_size;i++){
sum += data[i];
}
return sum/array_size;
}

template<typename T>
double array_stdev(const T data[],const int array_size){
double sum = 0;
double avg = array_avg(data,array_size);
for(int i=0;i<array_size;i++){
sum += (data[i]-avg)*(data[i]-avg);
}
return sqrt(sum/(array_size-1));
}

template<typename base_type>
base_type intpow(const base_type base,const int exponent){
base_type result = base;
for(int i=1;i<exponent;i++){
result *= base;
}
return result;
}

template<typename T,typename A>
double vector_avg(const vector<T,A>& dataset){
double sum = 0;
for(auto it=dataset.begin();it!=dataset.end();++it){
sum += *it;
}
return sum/dataset.size();
}

template<typename T,typename A>
double vector_stdev(const vector<T,A>& dataset){
double sum = 0;
double avg = vector_avg(dataset);
for(auto it=dataset.begin();it!=dataset.end();++it){
sum += (*it-avg)*(*it-avg);
}
return sqrt(sum/(dataset.size()-1));
}

#endif // UTILS_H

0 comments on commit 7298349

Please sign in to comment.