summaryrefslogtreecommitdiff
path: root/src/fracture_network.c
blob: 428b0920a21e10e9259b56b378475f5c3dbd519a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

#include "fracture.h"

int inc_break_fuses(net_t *instance, double *thres, double *field,
														 double cutoff) {
	unsigned int size = (instance->graph)->ne;

	int min_pos = -1;
	long double min_val = -1;

	for (unsigned int i = 0; i < size; i++) {
		if (!instance->fuses[i] && fabs(field[i]) > cutoff) {
			double val = fabs(field[i]) / thres[i];
			if (min_val < val) {
				min_val = val; min_pos = i;
			}
		}
	}

	return min_pos;
}

data_t *fracture_network(net_t *instance, double *fuse_thres,
														 cholmod_common *c, double cutoff) {
	unsigned int num_edges = instance->graph->ne;
	unsigned int num_verts = instance->graph->nv;

	data_t *breaking_data = alloc_break_data(num_edges);

	while (true) {
		double *voltages = get_voltage(instance, c);
		double *field = get_current_v(instance, voltages, c);

		double conductivity = get_conductivity(instance, voltages, c);
		if (conductivity < 1e-12 && instance->voltage_bound) {
			free(voltages);
			free(field);
			break;
		}

		int last_broke = inc_break_fuses(instance, fuse_thres, field, cutoff);
		if (last_broke > num_edges || last_broke < -1 || conductivity < 1e-8) {
			printf("whoops %u\n\n", breaking_data->num_broken);
			free(voltages);
			free(field);
			break;
		}

		update_break_data(breaking_data, last_broke, fabs(conductivity * fuse_thres[last_broke] / field[last_broke]), conductivity);

		free(voltages);
		free(field);

		break_edge(instance, last_broke, c);

		if (instance->num_components > 1 && instance->graph->boundary == TORUS_BOUND) {
			break;
		}

		if (instance->marks[num_verts] != instance->marks[num_verts + 1] && instance->graph->boundary != TORUS_BOUND) {
			break;
		}
	}

	return breaking_data;
}