So I have this objects: Resistor, Voltage Source, Node and Graph. My main goal is to represent some graph from an electric circuit and I have this problem: when I add the weight for the parallel nodes from Node2 to NodeRef clearly R2 resistance is replaced with R3 resistance.Any suggestions to implement this without multi-graphs?
def create_components(self):
NodeRef = Node("0")
Node1 = Node("1")
Node2 = Node("2")
R1 = Resistor("R1", 100) #R1.get_val() returns the value in ohms
R2 = Resistor("R2", 200)
R3 = Resistor("R3", 300)
V1 = Voltage("Source", 5)
NodeRef.add_destination(Node1, 0) # Node.add_destination(destination, weight)
Node1.add_destination(Node2, R1.get_val())
Node2.add_destination(NodeRef, R2.get_val())
Node2.add_destination(NodeRef, R3.get_val())
since I can´t post images yet, the circuit schematic goes like this:
|-------R1------------
| | |
V1 R2 R3
| | |
|---------------------
The circuit I want to represent as a graph:
Track the sum of all conductances between two nodes. Conductance is reciprocal to resistance. Parallel conductances add normally. To get the net resistance, just return the reciprocal of the total tracked conductance sum.
class Node():
def __init__(self,label):
self.label = label
self.connections = {}
def add_destination(self,other,weight):
if not other.label in self.connections:
self.connections[other.label] = 0
self.connections[other.label] += (1.0/weight if weight !=0 else float('inf'))
other.connections[self.label] = self.connections[other.label]
def resistance(self,other):
if other.label in self.connections and self.connections[other.label] != 0:
return 1.0/self.connections[other.label]
return float('inf')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.