-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathequivalent_resistance.py
78 lines (60 loc) · 2.84 KB
/
equivalent_resistance.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
import numpy as np
def parse_circuit_data(input_json):
# List of nodes and connections
nodes, input_node, output_node = circuit_data["nodes"], circuit_data["input_node"], circuit_data["output_node"]
# Map each node to a matrix index with dictionary comprehension
node_index = {node: i for i, node in enumerate(nodes)}
num_nodes = len(nodes)
# Initialize conductance matrix and current vector
G = np.zeros((num_nodes, num_nodes)) # Conductance matrix
I = np.zeros(num_nodes) # Current vector
# Populate the conductance matrix based on connections
for conn in circuit_data["connections"]:
start, end, resistance = conn["start"], conn["end"], conn["resistance"]
i, j = node_index[start], node_index[end]
# Calculate conductance (1 / resistance)
conductance = 1 / resistance
# Diagonal elements would be positive and the off diagonal elements would be negative
G[i, i] += conductance
G[j, j] += conductance
G[i, j] -= conductance
G[j, i] -= conductance
# Set boundary conditions: voltage at input node, and reference current
input_idx = node_index[input_node]
output_idx = node_index[output_node]
# Inject 1A at the input node to calculate equivalent resistance
I[input_idx] = 1
return G, I, input_idx, output_idx
def calculate_equivalent_resistance(G, I, input_idx, output_idx):
# Solve for node voltages
# Remove the row and column for the reference (ground) node (output_idx) for a reduced matrix
G_reduced = np.delete(np.delete(G, output_idx, axis=0), output_idx, axis=1)
I_reduced = np.delete(I, output_idx)
# Solve the system of linear equations
V_reduced = np.linalg.solve(G_reduced, I_reduced)
# Insert the ground voltage (0V) back into the voltage array at the output index
V = np.insert(V_reduced, output_idx, 0)
# Equivalent resistance calculation
R_eq = V[input_idx]
return R_eq
# Sample circuit data
circuit_data = {
"nodes": ["A", "B", "C", "D", "E", "F"],
"connections": [
{"start": "A", "end": "C", "resistance": 20},
{"start": "C", "end": "D", "resistance": 30},
{"start": "D", "end": "E", "resistance": 40},
{"start": "D", "end": "F", "resistance": 10},
{"start": "C", "end": "F", "resistance": 60},
{"start": "F", "end": "E", "resistance": 50},
{"start": "E", "end": "B", "resistance": 80},
],
"input_node": "A",
"output_node": "B"
}
# Construct conductance matrix, current vector, and indices for input/output nodes
G, I, input_idx, output_idx = parse_circuit_data(circuit_data)
# Calculate equivalent resistance
R_eq = calculate_equivalent_resistance(G, I, input_idx, output_idx)
print("Equivalent Resistance between nodes", circuit_data["input_node"], "and", circuit_data["output_node"], "is:",
R_eq, "ohms")