【问题标题】:Parallel resistors in graph (node representation of electric circuits)图中的并联电阻(电路的节点表示)
【发布时间】:2021-01-05 22:00:30
【问题描述】:

所以我有这个对象:电阻器、电压源、节点和图表。我的主要目标是从电路中表示一些图,我遇到了这个问题:当我将 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
|             |      |
|---------------------

我想用图表表示的电路:

【问题讨论】:

  • 您的对象来自哪里?您使用的是自定义库还是现成的库?
  • @PaulH 这是一个自定义的,图表是我从 (baeldung.com/java-dijkstra) 获得的,我创建了电压和电阻对象
  • R2没有被R3替代,它们是并行的,R = 1/(1/R2+1/R3)
  • @WalterTross 当然,我没有解释自己。我的意思是在代码中将 R2 之间的边缘替换为 R3 之间的边缘
  • 我认为这些对象的实现对于回答这个问题可能很重要

标签: python data-structures nodes


【解决方案1】:

跟踪两个节点之间所有电导的总和。电导是电阻的倒数。平行电导正常相加。要获得净电阻,只需返回总跟踪电导和的倒数。

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

【讨论】:

    猜你喜欢
    • 2015-07-09
    • 2022-12-14
    • 1970-01-01
    • 2023-03-15
    • 2018-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多