From 9c6505dace488032dc4f5e3bbb8c5a4cb154b429 Mon Sep 17 00:00:00 2001 From: pants Date: Thu, 8 Sep 2016 12:46:49 -0400 Subject: more refactoring --- src/break_data.c | 34 --------------- src/data.c | 35 +++++++++++++++ src/fracture.c | 2 +- src/fracture.h | 12 ++---- src/free_network.c | 22 ---------- src/graph_free.c | 22 ++++++++++ src/graph_genfunc.c | 78 +++++++++++++++++++++++++++++++++ src/net.c | 121 ---------------------------------------------------- src/net_copy.c | 35 +++++++++++++++ src/net_create.c | 69 ++++++++++++++++++++++++++++++ src/net_fracture.c | 5 +-- src/net_free.c | 14 ++++++ src/randfuncs.c | 93 ---------------------------------------- 13 files changed, 259 insertions(+), 283 deletions(-) delete mode 100644 src/break_data.c create mode 100644 src/data.c delete mode 100644 src/free_network.c create mode 100644 src/graph_free.c create mode 100644 src/graph_genfunc.c delete mode 100644 src/net.c create mode 100644 src/net_copy.c create mode 100644 src/net_create.c create mode 100644 src/net_free.c delete mode 100644 src/randfuncs.c (limited to 'src') diff --git a/src/break_data.c b/src/break_data.c deleted file mode 100644 index 5c922f3..0000000 --- a/src/break_data.c +++ /dev/null @@ -1,34 +0,0 @@ - -#include "fracture.h" - -data_t *alloc_break_data(unsigned int num_edges) { - data_t *data = malloc(1 * sizeof(data_t)); assert(data != NULL); - - data->num_broken = 0; - - data->break_list = (unsigned int *)malloc(num_edges * sizeof(unsigned int)); - assert(data->break_list != NULL); - - data->extern_field = (double *)malloc(num_edges * sizeof(double)); - assert(data->extern_field != NULL); - - data->conductivity = (double *)malloc(num_edges * sizeof(double)); - assert(data->conductivity != NULL); - - return data; -} - -void free_break_data(data_t *data) { - free(data->break_list); - free(data->extern_field); - free(data->conductivity); - free(data); -} - -void update_break_data(data_t *data, unsigned int last_broke, double strength, double conductivity) { - data->break_list[data->num_broken] = last_broke; - data->extern_field[data->num_broken] = strength; - data->conductivity[data->num_broken] = conductivity; - data->num_broken++; -} - diff --git a/src/data.c b/src/data.c new file mode 100644 index 0000000..b54236d --- /dev/null +++ b/src/data.c @@ -0,0 +1,35 @@ + +#include "fracture.h" + +data_t *data_create(uint_t ne) { + data_t *data = malloc(1 * sizeof(data_t)); + assert(data != NULL); + + data->num_broken = 0; + + data->break_list = (uint_t *)malloc(ne * sizeof(uint_t)); + assert(data->break_list != NULL); + + data->extern_field = (double *)malloc(ne * sizeof(double)); + assert(data->extern_field != NULL); + + data->conductivity = (double *)malloc(ne * sizeof(double)); + assert(data->conductivity != NULL); + + return data; +} + +void data_free(data_t *data) { + free(data->break_list); + free(data->extern_field); + free(data->conductivity); + free(data); +} + +void data_update(data_t *data, uint_t last_broke, double strength, double conductivity) { + data->break_list[data->num_broken] = last_broke; + data->extern_field[data->num_broken] = strength; + data->conductivity[data->num_broken] = conductivity; + data->num_broken++; +} + diff --git a/src/fracture.c b/src/fracture.c index e7abaec..f36cfdb 100644 --- a/src/fracture.c +++ b/src/fracture.c @@ -412,7 +412,7 @@ int main(int argc, char *argv[]) { fprintf(data_out, "\n"); } - free_break_data(data); + data_free(data); } printf("\033[F\033[JFRACTURE: COMPLETE\n"); diff --git a/src/fracture.h b/src/fracture.h index 19b1e71..2401d25 100644 --- a/src/fracture.h +++ b/src/fracture.h @@ -148,8 +148,6 @@ graph_t *ini_voro_graph(unsigned int L, bound_t boundary, bool use_dual, double *(*genfunc)(unsigned int, bound_t, gsl_rng *, unsigned int *), cholmod_common *c); -bool check_instance(const net_t *instance, cholmod_common *c); - bool break_edge(net_t *instance, unsigned int edge, cholmod_common *c); void finish_instance(net_t *instance, cholmod_common *c); @@ -164,11 +162,7 @@ double *genfunc_uniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned i double *genfunc_hyperuniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned int *num); void randfunc_flat(gsl_rng *r, double *x, double *y); void randfunc_gaus(gsl_rng *r, double *x, double *y); -double beta_scaling_flat(double beta, double x, double y); -double beta_scaling_gaus(double beta, double x, double y); -double beta_mag(double beta); -unsigned int *dijkstra(const graph_t *network, unsigned int source); unsigned int **get_dists(const graph_t *network); double *get_corr(net_t *instance, unsigned int **dists, cholmod_common *c); @@ -176,9 +170,9 @@ double *bin_values(graph_t *network, unsigned int width, double *values); cholmod_dense *bound_set(const graph_t *g, bool vb, double notch_len, cholmod_common *c); -data_t *alloc_break_data(unsigned int num_edges); -void free_break_data(data_t *data); -void update_break_data(data_t *data, unsigned int last_broke, double strength, double conductivity); +data_t *data_create(uint_t num_edges); +void data_free(data_t *data); +void data_update(data_t *data, uint_t last_broke, double strength, double conductivity); double get_conductivity(net_t *inst, double *current, cholmod_common *c); diff --git a/src/free_network.c b/src/free_network.c deleted file mode 100644 index 2001479..0000000 --- a/src/free_network.c +++ /dev/null @@ -1,22 +0,0 @@ - -#include "fracture.h" - -void graph_free(graph_t *network, cholmod_common *c) { - free(network->ev); - if (network->ev_break != network->ev) { - free(network->ev_break); - } - free(network->vei); - free(network->ve); - free(network->bound_inds); - free(network->bound_verts); - free(network->vx); - free(network->ex); - free(network->dev); - free(network->dvx); - free(network->dvei); - free(network->dve); - free(network->spanning_edges); - CHOL_F(free_sparse)(&(network->voltcurmat), c); - free(network); -} diff --git a/src/graph_free.c b/src/graph_free.c new file mode 100644 index 0000000..e9c55d7 --- /dev/null +++ b/src/graph_free.c @@ -0,0 +1,22 @@ + +#include "fracture.h" + +void graph_free(graph_t *g, cholmod_common *c) { + free(g->ev); + if (g->ev_break != g->ev) { + free(g->ev_break); + } + free(g->vei); + free(g->ve); + free(g->bound_inds); + free(g->bound_verts); + free(g->vx); + free(g->ex); + free(g->dev); + free(g->dvx); + free(g->dvei); + free(g->dve); + free(g->spanning_edges); + CHOL_F(free_sparse)(&(g->voltcurmat), c); + free(g); +} diff --git a/src/graph_genfunc.c b/src/graph_genfunc.c new file mode 100644 index 0000000..991e127 --- /dev/null +++ b/src/graph_genfunc.c @@ -0,0 +1,78 @@ + +#include "fracture.h" + +double *genfunc_uniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned int *num) { + *num = pow(L / 2 + 1, 2) + pow((L + 1) / 2, 2); + + double *lattice = (double *)malloc(2 * (*num) * sizeof(double)); + for (unsigned int i = 0; i < (*num); i++) { + lattice[2*i] = gsl_ran_flat(r, 0, 1); + lattice[2*i+1] = gsl_ran_flat(r, 0, 1); + } + + return lattice; +} + +double *genfunc_hyperuniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned int *num) { + *num = pow(L / 2 + 1, 2) + pow((L + 1) / 2, 2); + + // necessary to prevent crashing when underflow occurs + gsl_set_error_handler_off(); + + double *lattice = (double *)malloc(2 * (*num) * sizeof(double)); + double rho = *num; + unsigned int to_gen = *num; + unsigned int start = 0; + + if (boundary == EMBEDDED_BOUND) { + for (unsigned int i = 0; i < L / 2; i++) { + lattice[2 * i + 1] = 0; + lattice[2 * i] = (2. * i + 1.) / L; + + lattice[L + 2 * i + 1] = 1; + lattice[L + 2 * i] = (2. * i + 1.) / L; + + lattice[2 * L + 2 * i + 1] = (2. * i + 1.) / L; + lattice[2 * L + 2 * i] = 0; + + lattice[3 * L + 2 * i + 1] = (2. * i + 1.) / L; + lattice[3 * L + 2 * i] = 1; + } + + to_gen -= 2 * L; + start = 2 * L; + } + + for (unsigned int i = 0; i < to_gen; i++) { + bool reject = true; + double x, y; + while(reject) { + x = gsl_ran_flat(r, 0, 1); + y = gsl_ran_flat(r, 0, 1); + reject = false; + for (unsigned int j = 0; j < i; j++) { + double *ds = (double *)malloc(5 * sizeof(double)); + ds[0] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1],2); + ds[1] = pow(x-lattice[2*j] + 1,2)+pow(y-lattice[2*j+1],2); + ds[2] = pow(x-lattice[2*j] - 1,2)+pow(y-lattice[2*j+1],2); + ds[3] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1] + 1,2); + ds[4] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1] - 1,2); + double min_val = 100; + for (unsigned int k = 0; k < 5; k++) { + if (min_val > ds[k]) { + min_val = ds[k]; + } + } + if (1-gsl_sf_exp(-M_PI * rho * min_val) < gsl_ran_flat(r, 0, 1)) { + reject = true; + break; + } + } + } + lattice[2*start + 2 * i] = x; + lattice[2*start + 2 * i + 1] = y; + } + + return lattice; +} + diff --git a/src/net.c b/src/net.c deleted file mode 100644 index d090a9a..0000000 --- a/src/net.c +++ /dev/null @@ -1,121 +0,0 @@ - -#include "fracture.h" - -double *get_thres(uint_t ne, double beta) { - assert(beta > 0); - - double *thres = (double *)malloc(ne * sizeof(double)); - assert(thres != NULL); - - gsl_set_error_handler_off(); - - gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); - { - FILE *rf = fopen("/dev/urandom", "r"); - unsigned long int seed; - fread(&seed, sizeof(unsigned long int), 1, rf); - fclose(rf); - gsl_rng_set(r, seed); - } - - for (uint_t i = 0; i < ne; i++) { - while ((thres[i] = exp(log(gsl_ran_flat(r, 0, 1)) / beta)) == 0.0); - } - - gsl_rng_free(r); - - return thres; -} - - -net_t *net_create(const graph_t *g, double inf, double beta, double notch_len, bool vb, cholmod_common *c) { - net_t *net = (net_t *)calloc(1, sizeof(net_t)); - assert(net != NULL); - - net->graph = g; - net->fuses = (bool *)calloc(g->ne, sizeof(bool)); - assert(net->fuses != NULL); - net->thres = get_thres(g->ne, beta); - net->inf = inf; - - net->voltage_bound = vb; - net->boundary_cond = bound_set(g, vb, notch_len, c); - - if (g->boundary != TORUS_BOUND) net->adjacency = gen_adjacency(net, false, false, 0, c); - else net->adjacency = gen_adjacency(net, true, false, 0, c); - - net->marks = (uint_t *)malloc((net->graph->break_dim) * sizeof(uint_t)); - net->dual_marks = (uint_t *)malloc((net->graph->dnv) * sizeof(uint_t)); - assert(net->marks != NULL); - - for (uint_t i = 0; i < (net->graph->break_dim); i++) { - net->marks[i] = 1; - } - for (uint_t i = 0; i < (net->graph->dnv); i++) { - net->dual_marks[i] = i+1; - } - net->num_components = 1; - - net_notch(net, notch_len, c); - - { - cholmod_sparse *laplacian = gen_laplacian(net, c, true); - net->factor = CHOL_F(analyze)(laplacian, c); - CHOL_F(factorize)(laplacian, net->factor, c); - CHOL_F(free_sparse)(&laplacian, c); - } - - return net; -} - -net_t *net_copy(const net_t *net, cholmod_common *c) { - net_t *net_copy = (net_t *)calloc(1, sizeof(net_t)); - assert(net_copy != NULL); - memcpy(net_copy, net, sizeof(net_t)); - - size_t fuses_size = (net->graph)->ne * sizeof(bool); - net_copy->fuses = (bool *)malloc(fuses_size); - assert(net_copy->fuses != NULL); - memcpy(net_copy->fuses, net->fuses, fuses_size); - - size_t thres_size = (net->graph)->ne * sizeof(double); - net_copy->thres = (double *)malloc(thres_size); - assert(net_copy->thres != NULL); - memcpy(net_copy->thres, net->thres, thres_size); - - size_t marks_size = (net->graph->break_dim) * sizeof(uint_t); - net_copy->marks = (uint_t *)malloc(marks_size); - assert(net_copy->marks != NULL); - memcpy(net_copy->marks, net->marks, marks_size); - - size_t dual_marks_size = (net->graph->dnv) * sizeof(uint_t); - net_copy->dual_marks = (uint_t *)malloc(dual_marks_size); - assert(net_copy->dual_marks != NULL); - memcpy(net_copy->dual_marks, net->dual_marks, dual_marks_size); - - net_copy->adjacency = CHOL_F(copy_sparse)(net->adjacency, c); - net_copy->boundary_cond = CHOL_F(copy_dense)(net->boundary_cond, c); - net_copy->factor = CHOL_F(copy_factor)(net->factor, c); - - return net_copy; -} - -void net_free(net_t *net, cholmod_common *c) { - free(net->fuses); - free(net->thres); - CHOL_F(free_dense)(&(net->boundary_cond), c); - CHOL_F(free_sparse)(&(net->adjacency), c); - CHOL_F(free_factor)(&(net->factor), c); - free(net->marks); - free(net->dual_marks); - free(net); -} - -bool check_instance(const net_t *instance, cholmod_common *c) { - assert(instance != NULL); - assert(instance->fuses != NULL); - assert(CHOL_F(check_dense)(instance->boundary_cond, c)); - assert(CHOL_F(check_factor)(instance->factor, c)); - - return true; -} diff --git a/src/net_copy.c b/src/net_copy.c new file mode 100644 index 0000000..dcb4080 --- /dev/null +++ b/src/net_copy.c @@ -0,0 +1,35 @@ + +#include "fracture.h" + +net_t *net_copy(const net_t *net, cholmod_common *c) { + net_t *net_copy = (net_t *)calloc(1, sizeof(net_t)); + assert(net_copy != NULL); + memcpy(net_copy, net, sizeof(net_t)); + + size_t fuses_size = (net->graph)->ne * sizeof(bool); + net_copy->fuses = (bool *)malloc(fuses_size); + assert(net_copy->fuses != NULL); + memcpy(net_copy->fuses, net->fuses, fuses_size); + + size_t thres_size = (net->graph)->ne * sizeof(double); + net_copy->thres = (double *)malloc(thres_size); + assert(net_copy->thres != NULL); + memcpy(net_copy->thres, net->thres, thres_size); + + size_t marks_size = (net->graph->break_dim) * sizeof(uint_t); + net_copy->marks = (uint_t *)malloc(marks_size); + assert(net_copy->marks != NULL); + memcpy(net_copy->marks, net->marks, marks_size); + + size_t dual_marks_size = (net->graph->dnv) * sizeof(uint_t); + net_copy->dual_marks = (uint_t *)malloc(dual_marks_size); + assert(net_copy->dual_marks != NULL); + memcpy(net_copy->dual_marks, net->dual_marks, dual_marks_size); + + net_copy->adjacency = CHOL_F(copy_sparse)(net->adjacency, c); + net_copy->boundary_cond = CHOL_F(copy_dense)(net->boundary_cond, c); + net_copy->factor = CHOL_F(copy_factor)(net->factor, c); + + return net_copy; +} + diff --git a/src/net_create.c b/src/net_create.c new file mode 100644 index 0000000..fedcb3a --- /dev/null +++ b/src/net_create.c @@ -0,0 +1,69 @@ + +#include "fracture.h" + +double *get_thres(uint_t ne, double beta) { + assert(beta > 0); + + double *thres = (double *)malloc(ne * sizeof(double)); + assert(thres != NULL); + + gsl_set_error_handler_off(); + + gsl_rng *r = gsl_rng_alloc(gsl_rng_mt19937); + { + FILE *rf = fopen("/dev/urandom", "r"); + unsigned long int seed; + fread(&seed, sizeof(unsigned long int), 1, rf); + fclose(rf); + gsl_rng_set(r, seed); + } + + for (uint_t i = 0; i < ne; i++) { + while ((thres[i] = exp(log(gsl_ran_flat(r, 0, 1)) / beta)) == 0.0); + } + + gsl_rng_free(r); + + return thres; +} + +net_t *net_create(const graph_t *g, double inf, double beta, double notch_len, bool vb, cholmod_common *c) { + net_t *net = (net_t *)calloc(1, sizeof(net_t)); + assert(net != NULL); + + net->graph = g; + net->fuses = (bool *)calloc(g->ne, sizeof(bool)); + assert(net->fuses != NULL); + net->thres = get_thres(g->ne, beta); + net->inf = inf; + + net->voltage_bound = vb; + net->boundary_cond = bound_set(g, vb, notch_len, c); + + if (g->boundary != TORUS_BOUND) net->adjacency = gen_adjacency(net, false, false, 0, c); + else net->adjacency = gen_adjacency(net, true, false, 0, c); + + net->marks = (uint_t *)malloc((net->graph->break_dim) * sizeof(uint_t)); + net->dual_marks = (uint_t *)malloc((net->graph->dnv) * sizeof(uint_t)); + assert(net->marks != NULL); + + for (uint_t i = 0; i < (net->graph->break_dim); i++) { + net->marks[i] = 1; + } + for (uint_t i = 0; i < (net->graph->dnv); i++) { + net->dual_marks[i] = i+1; + } + net->num_components = 1; + + net_notch(net, notch_len, c); + + { + cholmod_sparse *laplacian = gen_laplacian(net, c, true); + net->factor = CHOL_F(analyze)(laplacian, c); + CHOL_F(factorize)(laplacian, net->factor, c); + CHOL_F(free_sparse)(&laplacian, c); + } + + return net; +} + diff --git a/src/net_fracture.c b/src/net_fracture.c index b7fa61d..48e81f9 100644 --- a/src/net_fracture.c +++ b/src/net_fracture.c @@ -27,9 +27,8 @@ uint_t get_next_broken(net_t *net, double *currents, double cutoff) { return max_pos; } - data_t *net_fracture(net_t *net, cholmod_common *c, double cutoff) { - data_t *data = alloc_break_data(net->graph->ne); + data_t *data = data_create(net->graph->ne); while (true) { double *voltages = get_voltage(net, c); @@ -45,7 +44,7 @@ data_t *net_fracture(net_t *net, cholmod_common *c, double cutoff) { uint_t last_broke = get_next_broken(net, currents, cutoff); - update_break_data(data, last_broke, fabs(conductivity * (net->thres)[last_broke] / currents[last_broke]), conductivity); + data_update(data, last_broke, fabs(conductivity * (net->thres)[last_broke] / currents[last_broke]), conductivity); free(voltages); free(currents); diff --git a/src/net_free.c b/src/net_free.c new file mode 100644 index 0000000..8b5af50 --- /dev/null +++ b/src/net_free.c @@ -0,0 +1,14 @@ + +#include "fracture.h" + +void net_free(net_t *net, cholmod_common *c) { + free(net->fuses); + free(net->thres); + CHOL_F(free_dense)(&(net->boundary_cond), c); + CHOL_F(free_sparse)(&(net->adjacency), c); + CHOL_F(free_factor)(&(net->factor), c); + free(net->marks); + free(net->dual_marks); + free(net); +} + diff --git a/src/randfuncs.c b/src/randfuncs.c deleted file mode 100644 index 137c7ff..0000000 --- a/src/randfuncs.c +++ /dev/null @@ -1,93 +0,0 @@ - -#include "fracture.h" - -double *genfunc_uniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned int *num) { - *num = pow(L / 2 + 1, 2) + pow((L + 1) / 2, 2); - - double *lattice = (double *)malloc(2 * (*num) * sizeof(double)); - for (unsigned int i = 0; i < (*num); i++) { - lattice[2*i] = gsl_ran_flat(r, 0, 1); - lattice[2*i+1] = gsl_ran_flat(r, 0, 1); - } - - return lattice; -} - -double *genfunc_hyperuniform(unsigned int L, bound_t boundary, gsl_rng *r, unsigned int *num) { - *num = pow(L / 2 + 1, 2) + pow((L + 1) / 2, 2); - - // necessary to prevent crashing when underflow occurs - gsl_set_error_handler_off(); - - double *lattice = (double *)malloc(2 * (*num) * sizeof(double)); - double rho = *num; - unsigned int to_gen = *num; - unsigned int start = 0; - - if (boundary == EMBEDDED_BOUND) { - for (unsigned int i = 0; i < L / 2; i++) { - lattice[2 * i + 1] = 0; - lattice[2 * i] = (2. * i + 1.) / L; - - lattice[L + 2 * i + 1] = 1; - lattice[L + 2 * i] = (2. * i + 1.) / L; - - lattice[2 * L + 2 * i + 1] = (2. * i + 1.) / L; - lattice[2 * L + 2 * i] = 0; - - lattice[3 * L + 2 * i + 1] = (2. * i + 1.) / L; - lattice[3 * L + 2 * i] = 1; - } - - to_gen -= 2 * L; - start = 2 * L; - } - - for (unsigned int i = 0; i < to_gen; i++) { - bool reject = true; - double x, y; - while(reject) { - x = gsl_ran_flat(r, 0, 1); - y = gsl_ran_flat(r, 0, 1); - reject = false; - for (unsigned int j = 0; j < i; j++) { - double *ds = (double *)malloc(5 * sizeof(double)); - ds[0] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1],2); - ds[1] = pow(x-lattice[2*j] + 1,2)+pow(y-lattice[2*j+1],2); - ds[2] = pow(x-lattice[2*j] - 1,2)+pow(y-lattice[2*j+1],2); - ds[3] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1] + 1,2); - ds[4] = pow(x-lattice[2*j],2)+pow(y-lattice[2*j+1] - 1,2); - double min_val = 100; - for (unsigned int k = 0; k < 5; k++) { - if (min_val > ds[k]) { - min_val = ds[k]; - } - } - if (1-gsl_sf_exp(-M_PI * rho * min_val) < gsl_ran_flat(r, 0, 1)) { - reject = true; - break; - } - } - } - lattice[2*start + 2 * i] = x; - lattice[2*start + 2 * i + 1] = y; - } - - return lattice; -} - -void randfunc_flat(gsl_rng *r, double *x, double *y) { - *x = gsl_ran_flat(r, 0, 1); - *y = gsl_ran_flat(r, 0, 1); -} - -void randfunc_gaus(gsl_rng *r, double *x, double *y) { - *x = 100; - *y = 100; - double sigma = 0.25; - while (fabs(*x) > 0.5 || fabs(*y) > 0.5) { - gsl_ran_bivariate_gaussian(r, sigma, sigma, 0, x, y); - } - *x += 0.5; - *y += 0.5; -} -- cgit v1.2.3-70-g09d2