Skip to content

Commit 655ed3a

Browse files
committed
Reset block inputs in _assemble_graph instead of remove_connection
1 parent 0e2f038 commit 655ed3a

3 files changed

Lines changed: 15 additions & 14 deletions

File tree

src/pathsim/simulation.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -446,11 +446,6 @@ def remove_connection(self, connection):
446446
self.logger.error(_msg)
447447
raise ValueError(_msg)
448448

449-
#zero out target input ports to avoid stale values
450-
for target in connection.targets:
451-
for port in target.ports:
452-
target.block.inputs[port] = 0.0
453-
454449
#remove from global connection list
455450
self.connections.discard(connection)
456451

@@ -504,10 +499,14 @@ def remove_event(self, event):
504499
# system assembly -------------------------------------------------------------
505500

506501
def _assemble_graph(self):
507-
"""Build the internal graph representation for fast system function
502+
"""Build the internal graph representation for fast system function
508503
evaluation and algebraic loop resolution.
509504
"""
510505

506+
#reset all block inputs to clear stale values from removed connections
507+
for block in self.blocks:
508+
block.inputs.reset()
509+
511510
#time the graph construction
512511
with Timer(verbose=False) as T:
513512
self.graph = Graph(self.blocks, self.connections)

src/pathsim/subsystem.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,11 +337,6 @@ def remove_connection(self, connection):
337337
if connection not in self.connections:
338338
raise ValueError(f"{connection} not part of subsystem")
339339

340-
#zero out target input ports to avoid stale values
341-
for target in connection.targets:
342-
for port in target.ports:
343-
target.block.inputs[port] = 0.0
344-
345340
self.connections.discard(connection)
346341

347342
if self.graph:
@@ -386,6 +381,11 @@ def _assemble_graph(self):
386381
"""Assemble internal graph of subsystem for fast
387382
algebraic evaluation during simulation.
388383
"""
384+
385+
#reset all block inputs to clear stale values from removed connections
386+
for block in self.blocks:
387+
block.inputs.reset()
388+
389389
self.graph = Graph({*self.blocks, self.interface}, self.connections)
390390
self._graph_dirty = False
391391

tests/pathsim/test_simulation.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -885,18 +885,20 @@ def test_remove_connection_error(self):
885885
self.Sim.remove_connection(C)
886886

887887
def test_remove_connection_zeroes_inputs(self):
888-
"""Removing a connection zeroes the target block's input ports"""
888+
"""Removing a connection zeroes target inputs on next graph rebuild"""
889889
# Run a step so the connection pushes data
890890
self.Src.function = lambda t: 5.0
891891
self.Sim.step(0.01)
892892

893893
# Int should have received input from Src
894894
self.assertNotEqual(self.Int.inputs[0], 0.0)
895895

896-
# Remove the connection
896+
# Remove the connection — graph is dirty but not rebuilt yet
897897
self.Sim.remove_connection(self.C1)
898+
self.assertTrue(self.Sim._graph_dirty)
898899

899-
# Target input should now be zero
900+
# Next step triggers graph rebuild which resets inputs
901+
self.Sim.step(0.01)
900902
self.assertEqual(self.Int.inputs[0], 0.0)
901903

902904
def test_remove_event(self):

0 commit comments

Comments
 (0)