图中的并联电阻电路的节点表示

问题描述

所以我有这个对象:电阻器、电压源、节点和图形。我的主要目标是表示电路中的一些图形,但我遇到了这个问题:当我将并行节点的权重从 Node2 添加到 NodeRef 时,显然 R2 电阻被 R3 电阻替换。有没有建议可以在没有多图的情况下实现这一点?

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())

由于我还不能发布图片,电路原理图是这样的:

|-------R1------------
|             |      |
V1            R2     R3
|             |      |
|---------------------

我想用图形表示的电路:

The circuit I want to represent as a graph

解决方法

跟踪两个节点之间所有电导的总和。电导与电阻成反比。平行电导通常相加。要获得净电阻,只需返回总跟踪电导和的倒数即可。

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')