summaryrefslogtreecommitdiff
path: root/src/update_boundary.c
blob: 3d2a086be3982f36fdb43d1ae399f5bd94aaf3b4 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86

#include "fracture.h"

void update_boundary(finst *instance, const double *avg_field) {
	int size = instance->network->num_edges;
	int width = sqrt(size);
	int num_verts = instance->network->num_verts;
	double *boundary = (double *)instance->boundary_cond->x;

	boundary[num_verts] = 0;
	boundary[num_verts + 1] = 0;

	if (instance->voltage_bound) {
		for (int i = 0; i < width / 4; i++) {
			int t_ind = (width + 1) * (width / 8) + width / 4 - 1 + i;
			double t_val = avg_field[t_ind];
			boundary[2 * i] = t_val;
			boundary[2 * i + 1] = t_val;

			int b_ind = num_verts - (width + 1) * (width / 8) - width / 4 - i;
			double b_val = avg_field[b_ind];
			boundary[num_verts - 1 - 2 * i] = b_val;
			boundary[num_verts - 2 - 2 * i] = b_val;

			int l_ind = (width + 1) * (width / 8) + width / 2 + width / 4 - 1 +
									i * (width + 1);
			double l_val = avg_field[l_ind];
			boundary[width / 2 + 2 * i * (width + 1)] = l_val;
			boundary[width / 2 + (2 * i + 1) * (width + 1)] = l_val;

			int r_ind = (width + 1) * (width / 8) + width - 1 + i * (width + 1);
			double r_val = avg_field[r_ind];
			boundary[width + 2 * i * (width + 1)] = r_val;
			boundary[width + (2 * i + 1) * (width + 1)] = r_val;
		}
	} else {
		const double *stress = avg_field;

		for (int i = 0; i < width / 4; i++) {
			boundary[2 * i] = stress[(width / 4 - 1) * width + width / 4 + 2 * i] +
												stress[(width / 4 - 1) * width + width / 4 + 1 + 2 * i];
			boundary[2 * i + 1] =
					stress[(width / 4 - 1) * width + width / 4 + 2 * i] +
					stress[(width / 4 - 1) * width + width / 4 + 1 + 2 * i];

			boundary[num_verts - 2 * i - 1] =
					-stress[size - 1 - ((width / 4 - 1) * width + width / 4 + 2 * i)] -
					stress[size - 1 - ((width / 4 - 1) * width + width / 4 + 1 + 2 * i)];
			boundary[num_verts - 2 * i - 2] =
					-stress[size - 1 - ((width / 4 - 1) * width + width / 4 + 2 * i)] -
					stress[size - 1 - ((width / 4 - 1) * width + width / 4 + 1 + 2 * i)];

			boundary[(width + 1) / 2 + 2 * i * (width + 1)] =
					stress[((width / 4) + 2 * i) * width + width / 4 - 1] -
					stress[((width / 4) + 2 * i + 1) * width + width / 4 - 1];
			boundary[(width + 1) / 2 + (2 * i + 1) * (width + 1)] =
					stress[((width / 4) + 2 * i) * width + width / 4 - 1] -
					stress[((width / 4) + 2 * i + 1) * width + width / 4 - 1];

			boundary[(width + 1) / 2 + 2 * i * (width + 1) + width / 2] =
					stress[((width / 4) + 2 * i) * width + width / 4 + width / 2] -
					stress[((width / 4) + 2 * i + 1) * width + width / 4 + width / 2];
			boundary[(width + 1) / 2 + (2 * i + 1) * (width + 1) + width / 2] =
					stress[((width / 4) + 2 * i) * width + width / 4 + width / 2] -
					stress[((width / 4) + 2 * i + 1) * width + width / 4 + width / 2];
		}

		double s_total = 0;
		double total = 0;
		for (int i = 0; i < num_verts; i++) {
			if (boundary[i] != boundary[i]) {
				boundary[i] = 0;
			} else {
				total += fabs(boundary[i]);
				s_total += boundary[i];
			}
		}

		// assert(fabs(s_total) < inf);
		boundary[num_verts] -= s_total;

		for (int i = 0; i < num_verts + 1; i++) {
			boundary[i] /= total;
		}
	}
}