From 2bb0740b68fdb62d45adc00204b3990ca42ade77 Mon Sep 17 00:00:00 2001 From: Jaron Kent-Dobias Date: Mon, 22 Aug 2016 10:11:14 -0400 Subject: started repo again without all the data files gunking the works --- src/instance.c | 118 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 src/instance.c (limited to 'src/instance.c') diff --git a/src/instance.c b/src/instance.c new file mode 100644 index 0000000..5b3d8fb --- /dev/null +++ b/src/instance.c @@ -0,0 +1,118 @@ + +#include "fracture.h" + +finst *create_instance(fnet *network, double inf, bool voltage_bound, + bool startnow, cholmod_common *c) { + finst *instance = (finst *)calloc(1, sizeof(finst)); + assert(instance != NULL); + + instance->network = network; + instance->num_remaining_edges = network->num_edges; + instance->fuses = (bool *)calloc(network->num_edges, sizeof(bool)); + assert(instance->fuses != NULL); + instance->inf = inf; + instance->voltage_bound = voltage_bound; + instance->boundary_cond = CHOL_F(zeros)( + network->break_dim, 1, CHOLMOD_REAL, c); + if (network->boundary != TORUS_BOUND) { + if (voltage_bound) { + for (unsigned int i = 0; i < network->bound_inds[1]; i++) { + ((double *)instance->boundary_cond->x)[network->bound_verts[i]] =1; + } + } else { + ((double *)instance->boundary_cond->x)[0] = 1; + ((double *)instance->boundary_cond->x)[network->num_verts] = 1; + ((double *)instance->boundary_cond->x)[network->num_verts + 1] = -1; + } + } else { + for (unsigned int i = 0; i < network->bound_inds[1]; i++) { + ((double *)instance->boundary_cond->x)[network->bound_verts[i]] = 1; + ((double *)instance->boundary_cond->x)[network->num_verts + i] = -1; + } + ((double *)instance->boundary_cond->x)[network->bound_verts[0]] = 1; + } + + instance->adjacency = gen_adjacency(instance, false, false, 0, c); + + if (startnow) { + cholmod_sparse *laplacian = gen_laplacian(instance, c, true); + instance->factor = CHOL_F(analyze)(laplacian, c); + CHOL_F(factorize)(laplacian, instance->factor, c); + CHOL_F(free_sparse)(&laplacian, c); + } + + instance->marks = (unsigned int *)malloc( + (instance->network->break_dim) * + sizeof(unsigned int)); + instance->dual_marks = (unsigned int *)malloc( + (instance->network->num_dual_verts) * + sizeof(unsigned int)); + assert(instance->marks != NULL); + + for (unsigned int i = 0; + i < (instance->network->break_dim); + i++) { + instance->marks[i] = 1; + } + for (unsigned int i = 0; + i < (instance->network->num_dual_verts); + i++) { + instance->dual_marks[i] = i+1; + } + instance->num_components = 1; + + return instance; +} + +void finish_instance(finst *instance, cholmod_common *c) { + cholmod_sparse *laplacian = gen_laplacian(instance, c, true); + instance->factor = CHOL_F(analyze)(laplacian, c); + CHOL_F(factorize)(laplacian, instance->factor, c); + CHOL_F(free_sparse)(&laplacian, c); +} + +finst *copy_instance(const finst *instance, cholmod_common *c) { + finst *instance_copy = (finst *)calloc(1, sizeof(finst)); + memcpy(instance_copy, instance, sizeof(finst)); + + size_t fuses_size = (instance->network)->num_edges * sizeof(bool); + instance_copy->fuses = (bool *)malloc(fuses_size); + memcpy(instance_copy->fuses, instance->fuses, fuses_size); + + size_t marks_size = + (instance->network->break_dim) * + sizeof(unsigned int); + instance_copy->marks = (unsigned int *)malloc(marks_size); + memcpy(instance_copy->marks, instance->marks, marks_size); + + size_t dual_marks_size = + (instance->network->num_dual_verts) * + sizeof(unsigned int); + instance_copy->dual_marks = (unsigned int *)malloc(dual_marks_size); + memcpy(instance_copy->dual_marks, instance->dual_marks, dual_marks_size); + + instance_copy->adjacency = CHOL_F(copy_sparse)(instance->adjacency, c); + instance_copy->boundary_cond = CHOL_F(copy_dense)(instance->boundary_cond, c); + instance_copy->factor = CHOL_F(copy_factor)(instance->factor, c); + + return instance_copy; +} + +void free_instance(finst *instance, cholmod_common *c) { + free(instance->fuses); + CHOL_F(free_dense)(&(instance->boundary_cond), c); + CHOL_F(free_sparse)(&(instance->adjacency), c); + CHOL_F(free_factor)(&(instance->factor), c); + free(instance->marks); + free(instance->dual_marks); + free(instance); +} + +bool check_instance(const finst *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; +} -- cgit v1.2.3-70-g09d2