-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
89 lines (65 loc) · 2.46 KB
/
main.py
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
87
88
89
from file_io import *
from graph_generation import *
from circuit_solver import *
from visualization import *
from tests import *
def load_compute_save_test(filename, draw=True):
G, s, t, U = load_graph(filename)
print(f"{filename.removesuffix('.txt')}")
G, total_current = find_currents_in_circuit(G, s, t, U)
print(f'Total current: {total_current:.2f} A')
print('Satisfies the Kirchhoff\'s current law: ' +\
f'{test_kirchhoffs_current_law(G, s, t, total_current)}')
save_results(G, total_current, filename.removesuffix('.txt') + '_res.txt')
print()
if not draw:
return
H = transform_into_digraph(G)
n = len(G)
m = len(G.edges)
title = r'$\|V\| =$' + str(n) + ' ' +\
r'$\|E\| =$' + str(m) + ' ' +\
r'$U_{s t} =$' + str(U) + 'V' + ' ' +\
r'$I_{c} =$' + f'{total_current:.2f}' + 'A'
img_filename = filename.removesuffix('.txt')
draw_circuit(H, title=title, node_size=20,
edge_width=2, filename=img_filename+'_1.png',
show=True)
draw_circuit(H, node_labels=True, s=s, t=t, font_size=6,
title=title, node_size=50, edge_width=2,
filename=img_filename+'_2.png',
show=False)
draw_circuit(H, node_labels=True, s=s, t=t, font_size=6,
title=title, node_size=50, edge_width=2,
edge_labels={'R', 'I'}, filename=img_filename+'_3.png',
show=False)
if __name__ == '__main__':
filenames = []
grid_n_m = [(2, 3),
(2, 5),
(4, 4),
(6, 6),
(10, 10),
(10, 20),
(20, 20)]
U = 50
for n, m in grid_n_m:
G = generate_grid(n, m)
filename = f'grid_{n}_{m}.txt'
save_circuit(G, 0, n*m-1, U, filename)
filenames.append(filename)
U = 50
for n in range(3, 20, 3):
G = generate_small_world(n)
filename = f'small_world_{n}.txt'
save_circuit(G, 0, n*n-1, U, filename)
filenames.append(filename)
erdos_renyi_n_p = [(n, p) for n in range(15, 26, 10) for p in [0.3, 0.4, 0.5]]
U = 50
for n, p in erdos_renyi_n_p:
G = generate_erdos_renyi(n, p, 1, 10)
filename = f'erdos_renyi_{n}_{p}.txt'
save_circuit(G, 0, n-1, U, filename)
filenames.append(filename)
for filename in filenames:
load_compute_save_test(filename)