Skip to content

Commit

Permalink
patched shit ton of floating point errors
Browse files Browse the repository at this point in the history
  • Loading branch information
teknasd authored Feb 6, 2018
1 parent 9055e94 commit b890e0c
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 61 deletions.
2 changes: 2 additions & 0 deletions Headers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 7,8 @@
#include <algorithm>
#include <random>
#include <vector>
#include <fstream>
#include <string>



Expand Down
72 changes: 61 additions & 11 deletions LatticeStuff.cpp
Original file line number Diff line number Diff line change
@@ -1,7 1,7 @@
#include "Headers.h"
#include "Parameters.h" // >> contains all the req parameters

void create_Bmat_bimodal(std::vector <int>& Bmat)
void create_Bmat_bimodal(std::vector <float>& Bmat,float del)
{
long i = 0, var = 0, count1 = 0, count2 = 0;
std::uniform_int_distribution<int> distr(0, 1000);
Expand All @@ -14,12 14,12 @@ void create_Bmat_bimodal(std::vector <int>& Bmat)
var = distr(generator);
if (var < 500)
{
Bmat[i] = w;
Bmat[i] = del;
count1 ;
}
else
{
Bmat[i] = -w;
Bmat[i] = -del;
count2 ;
}
//std::cout << Bmat[i] << d;
Expand All @@ -29,7 29,7 @@ void create_Bmat_bimodal(std::vector <int>& Bmat)
}

// http://www.cplusplus.com/reference/random/normal_distribution/
void create_Bmat_gaussian(std::vector <int>& Bmat,int del)
void create_Bmat_gaussian(std::vector <float>& Bmat,float del)
{
double var = 0;
long i = 0, count1 = 0, count2 = 0;
Expand Down Expand Up @@ -77,7 77,7 @@ void create_Exmat(std::vector < std::vector <int> >& Exmat, std::vector <int> &
}


void create_CapacityMat(std::vector < std::vector <int> >& CapacityMat, std::vector < std::vector <int> >& Exmat)
void create_CapacityMat(std::vector < std::vector <float> >& CapacityMat, std::vector < std::vector <int> >& Exmat)
{
for (int i = 1; i <= N; i )
{
Expand All @@ -86,7 86,7 @@ void create_CapacityMat(std::vector < std::vector <int> >& CapacityMat, std::vec
{
if (i < j)
{
CapacityMat[i][j] = 4 * J * Exmat[i - 1][j - 1] * 10;
CapacityMat[i][j] = 4 * J * Exmat[i - 1][j - 1] ;

}
else
Expand All @@ -97,7 97,7 @@ void create_CapacityMat(std::vector < std::vector <int> >& CapacityMat, std::vec
}


void create_Wmat(std::vector < int >& Wmat,std::vector < std::vector <int> >& CapacityMat, std::vector < int >& Bmat, int del )
void create_Wmat(std::vector < float >& Wmat,std::vector < std::vector <float> >& CapacityMat, std::vector < float >& Bmat, float del )
{
int i = 0, j = 0, cap = 0;
for (i = 1; i <= N; i )
Expand All @@ -109,12 109,12 @@ void create_Wmat(std::vector < int >& Wmat,std::vector < std::vector <int> >& Ca
}
Wmat[i - 1] = -2 * Bmat[i - 1] * del - cap / 2;
cap = 0;
//cout <<Wmat[i-1]<< endl;
//std::cout <<Wmat[i-1]<< std::endl;
}
}

// making reisdual graph by overwriting the flow mat,flow[i][j] = CapacityMat[i][j] - flow[i][j];
void create_Augumented_CapacityMat(std::vector < int >& Wmat, std::vector < std::vector <int> >& CapacityMat)
void create_Augumented_CapacityMat(std::vector < float >& Wmat, std::vector < std::vector <float> >& CapacityMat)
{
int i = 0;
for (i = 1; i <= N; i )
Expand All @@ -134,7 134,7 @@ void create_Augumented_CapacityMat(std::vector < int >& Wmat, std::vector < std:
}
}

void create_Residual_graph(std::vector < std::vector <int> >& CapacityMat, std::vector < std::vector <int> >& flow)
void create_Residual_graph(std::vector < std::vector <float> >& CapacityMat, std::vector < std::vector <float> >& flow)
{
int i = 0, j = 0;
for (i = 0; i < V; i )
Expand All @@ -152,7 152,7 @@ void printMatrix(std::vector <std::vector <int> > & M, int len) {
int i, j;
for (i = 0; i < len; i ) {
for (j = 0; j < len; j )
std::cout << "\t" << M[i][j];
std::cout << "\t" << M[i][j];
std::cout << "\n";
}
}
Expand All @@ -164,3 164,53 @@ void printMatrix(std::vector<int> & M, int len) {

}std::cout << "\n";
}


void savedata(std::vector< int > & Mat,int l,float del,std::string s)
{
std::ofstream fobj(s "-" std::to_string(l) "-" std::to_string(del) ".csv");
//fobj << "iteration : " << l << "\ndelta : " << del << std::endl;
int j;
Mat.erase(Mat.begin() 1);
for (int i = 0; i < Mat.size(); i )
{
std::cout << Mat[i];
}

for (int i = 0; i < N; i )
{
if (i%VER == 0) fobj << std::endl;
fobj << Mat[i]<<",";

}



fobj.clear();
}





void savedata(std::vector< float > & Mat, int l, float del, std::string s)
{
std::ofstream fobj(s "-" std::to_string(l) "-" std::to_string(del) ".csv");
//fobj << "iteration : " << l << "\ndelta : " << del << std::endl;



for (int i = 0; i < N; i )
{

if (i%VER == 0) fobj << std::endl;
fobj << Mat[i] << ",";

}


fobj.clear();
}



15 changes: 9 additions & 6 deletions LatticeStuff.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 2,24 @@

#include "Headers.h"

void create_Bmat_bimodal(std::vector <int>& Bmat);
void create_Bmat_bimodal(std::vector <float>& Bmat,float del);

void create_Bmat_gaussian(std::vector <int>& Bmat,int del);
void create_Bmat_gaussian(std::vector <float>& Bmat, float del);

void create_Exmat(std::vector < std::vector <int> >& Exmat, std::vector <int> & latt);

void create_CapacityMat(std::vector < std::vector <int> >& CapacityMat, std::vector < std::vector <int> >& Exmat);
void create_CapacityMat(std::vector < std::vector <float> >& CapacityMat, std::vector < std::vector <int> >& Exmat);

void create_Wmat(std::vector < int >& Wmat, std::vector < std::vector <int> >& CapacityMat, std::vector <int >& BMat, int del);
void create_Wmat(std::vector < float >& Wmat, std::vector < std::vector <float> >& CapacityMat, std::vector <float >& BMat, float del);

void create_Augumented_CapacityMat(std::vector < int >& Wmat, std::vector < std::vector <int> >& CapacityMat);
void create_Augumented_CapacityMat(std::vector < float >& Wmat, std::vector < std::vector <float> >& CapacityMat);

void create_Residual_graph(std::vector < std::vector <int> >& CapacityMat, std::vector < std::vector <int> >& flow);
void create_Residual_graph(std::vector < std::vector <float> >& CapacityMat, std::vector < std::vector <float> >& flow);

void printMatrix(std::vector <std::vector <int> > const & M, int len);

void printMatrix(std::vector<int> const & M, int len);

void savedata(std::vector< int > & Mat, int l, float del, std::string s);

void savedata(std::vector< float > & Mat, int l, float del, std::string s);
14 changes: 6 additions & 8 deletions Parameters.h
Original file line number Diff line number Diff line change
@@ -1,8 1,8 @@
#pragma once

//* DEFINE ALL THE PARAMETERS HERE */
#define iter 10
#define VER 10 // width of lattice matrix. for ex 6*6 lattice
#define iter 1
#define VER 32 // width of lattice matrix. for ex 6*6 lattice
#define DIM 2 // dimension of lattice // here 2; square lattice
#define latt_pc 1 // percentage of lattice points where atom exist
#define upspin_pc 0.5 // percentage of upspin in lattice
Expand All @@ -14,13 14,11 @@
#define HIGH INFINITE

//delta is multipling factor with Bmat
#define del_beg 0
#define del_end 40
#define del_inc 2



#define del_beg .7
#define del_end 1.2
#define del_inc .1


// text substitute
#define tab "\t"
#define d " : "
Expand Down
17 changes: 9 additions & 8 deletions PushRelabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 2,28 @@
#include "Parameters.h" // >> contains all the req parameters


void push(std::vector <std::vector <int> >const& C1, std::vector <std::vector <int> >& F1, std::vector <int>& excess1, int u, int v) {
void push(std::vector <std::vector <float> >const& C1, std::vector <std::vector <float> >& F1, std::vector <float>& excess1, int u, int v) {
//std::cout << "\nin push\n";
int send = MIN(excess1[u], C1[u][v] - F1[u][v]);
float send = std::min(excess1[u], C1[u][v] - F1[u][v]);
F1[u][v] = send;
F1[v][u] -= send;
excess1[u] -= send;
excess1[v] = send;
}

void relabel(std::vector <std::vector <int> >const & C, std::vector <std::vector <int> >& F, std::vector <int>& height, int u) {
void relabel(std::vector <std::vector <float> >const & C, std::vector <std::vector <float> >& F, std::vector <int>& height, int u) {
int v;
int min_height = INFINITE;
for (v = 0; v < V; v ) {
if (C[u][v] - F[u][v] > 0) {
min_height = MIN(min_height, height[v]);
min_height = std::min(min_height, height[v]);
height[u] = min_height 1;
}
}
}

//void discharge(const int * const * C, int ** F, int *excess, int *height, int *seen, int u) {
void discharge(std::vector <std::vector <int> >const & C, std::vector <std::vector <int> >& F, std::vector <int> & excess, std::vector <int> & height, std::vector <int>& seen, int u) {
void discharge(std::vector <std::vector <float> >const & C, std::vector <std::vector <float> >& F, std::vector <float> & excess, std::vector <int> & height, std::vector <int>& seen, int u) {

//std::cout << "\nflag 2\n";
while (excess[u] > 0) {
Expand Down Expand Up @@ -53,9 53,10 @@ void moveToFront(int i, std::vector <int>& A) {
}

//int pushRelabel(const int * const * C, int ** F, int source, int sink) {
void pushRelabel(std::vector <std::vector <int> >const& C, std::vector <std::vector <int> >& F, int source, int sink) {
void pushRelabel(std::vector <std::vector <float> >const& C, std::vector <std::vector <float> >& F, int source, int sink) {
//int *excess, *height, *list, *seen, i, p;
std::vector <int> excess(V, 0), height(V, 0), list(V - 2, 0), seen(V, 0);
std::vector <int> height(V, 0), list(V - 2, 0), seen(V, 0);
std::vector <float> excess(V, 0);
int i, p;
//std::cout << "add in fn " << &C[100][70];
/*excess = new int[V];
Expand Down Expand Up @@ -97,7 98,7 @@ void pushRelabel(std::vector <std::vector <int> >const& C, std::vector <std::vec
else
p = 1;
}
int maxflow = 0;
float maxflow = 0;
for (i = 0; i < V; i )
maxflow = F[source][i];
std::cout << "maxflow: " << maxflow << tab;
Expand Down
8 changes: 4 additions & 4 deletions PushRelabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 2,12 @@

#include "Headers.h"

void push(std::vector <std::vector <int> >const& C1, std::vector <std::vector <int> >& F1, std::vector <int>& excess1, int u, int v);
void push(std::vector <std::vector <float> >const& C1, std::vector <std::vector <float> >& F1, std::vector <float>& excess1, int u, int v);

void relabel(std::vector <std::vector <int> >const & C, std::vector <std::vector <int> >& F, std::vector <int>& height, int u);
void relabel(std::vector <std::vector <float> >const & C, std::vector <std::vector <float> >& F, std::vector <int>& height, int u);

void discharge(std::vector <std::vector <int> >const & C, std::vector <std::vector <int> >& F, std::vector <int> & excess, std::vector <int> & height, std::vector <int>& seen, int u);
void discharge(std::vector <std::vector <float> >const & C, std::vector <std::vector <float> >& F, std::vector <float> & excess, std::vector <int> & height, std::vector <int>& seen, int u);

void moveToFront(int i, std::vector <int>& A);

void pushRelabel(std::vector <std::vector <int> >const& C, std::vector <std::vector <int> >& F, int source, int sink);
void pushRelabel(std::vector <std::vector <float> >const& C, std::vector <std::vector <float> >& F, int source, int sink);
38 changes: 19 additions & 19 deletions RFIM_2D_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 20,7 @@ int main(void) {
// storing data into csv file
ofstream file("log_rfim_2D.csv");
file << "\niter,del,m=(mag/N)^2" << endl;
time_t time_begin, time_end, time_1, time_2;
time_t time_begin=0, time_end=0, time_1=0, time_2=0;

long t1, t2, tdiff[iter] = { 0 }, l = 0, sum = 0, i, j, cap = 0, clusters = 0;

Expand All @@ -30,15 30,14 @@ int main(void) {


/* ==================== INITIALISING 2D VECTORS ================*/
vector < vector <int> > flow(V, vector<int>(V, 0))
, CapacityMat(V, vector<int>(V, 0))
, Exmat(N, vector<int>(N, 0))
vector < vector <int> > Exmat(N, vector<int>(N, 0))
, sqlat0(VER 1, vector<int>(VER 1, 0))
, sqlat1(VER 1, vector<int>(VER 1, 0));

vector < vector <float> > flow(V, vector<float>(V, 0))
, CapacityMat(V, vector<float>(V, 0));
/* ==================== INITIALISING 1D VECTORS ================*/
vector <int> visited(V, 0), Wmat(N, 0), latt(N, 1), Bmat(N, 0), clusstats0(V / 2, 0), clusstats1(V / 2, 0);

vector <int> visited(V, 0), latt(N, 1), clusstats0(V / 2, 0), clusstats1(V / 2, 0);
vector<float> Bmat(N, 0), Wmat(N, 0);

/* ====================== CREATING EXISTANCE MATRIX (Exmat)============= */

Expand All @@ -52,35 51,35 @@ int main(void) {

/* =================== LOOP TO ITERATE OVER RANGE OF DELTA ============= */

for (int del = del_beg; del <= del_end; del = del_inc)
for (float del = del_beg; del <= del_end; del = del_inc)
{
//open
cout << "\ndEL: " << float(del) / 10 << tab;


cout << "\ndEL: " << del << tab;

/* =========================== BETA MATRIX (Bmat)======================= */

//create_Bmat_bimodal(Bmat); // UNCOMMENT FOR BIMODAL DISTRIBUTION
create_Bmat_gaussian(Bmat,del);
//cout << "Bmat created" << endl;

create_Bmat_bimodal(Bmat,del); // UNCOMMENT FOR BIMODAL DISTRIBUTION
//create_Bmat_gaussian(Bmat,del); // UNCOMMENT FOR GAUSSIAN DISTRIBUTION
cout << "Bmat created" << endl;
//printMatrix(Bmat, N);
savedata(Bmat,l,del,"Phi");

create_Wmat(Wmat, CapacityMat, Bmat, del);

/* =========== AGUMENTING CAPACITY MATRIX WITH WMAT ============= */

create_Augumented_CapacityMat(Wmat, CapacityMat);

t1 = time(&time_1); /* get current time;*/

t1 = long(time(&time_1)); /* get current time;*/
cout << "\ntic toc";
/*================================================================================================*/
/* ladies and gentlemen its honour to present you the most important stuff in this awesome code */
/* ===================== CALLING PUSH RELABEL(CapacityMat)======================================= */

pushRelabel(CapacityMat, flow, 0, V - 1);

/*====================================================================================*/
t2 = time(&time_2); /* get current time;*/
t2 = long(time(&time_2)); /* get current time;*/

/* ====================== CREATE RESIDUAL GRAPH ================== */
create_Residual_graph(CapacityMat, flow);
Expand All @@ -91,7 90,8 @@ int main(void) {

/* ================= DEPTH FIRST SEARCH ON FLOW ================== */
dfs(flow, 0, visited);

savedata(visited, l, del, "Si");
//printMatrix(visited, V);
/* ============ CREATE AGUMENTED MATRIX AROUND LATTICE =========== */
createAgumentedMatrix(sqlat0, sqlat1, visited);

Expand Down
Loading

0 comments on commit b890e0c

Please sign in to comment.