Skip to content

Simulation does not stop on under-/overvoltage #11

Closed

Description

@DavidMStraub

When choosing incorrect inputs or simulation settings, the simulation keeps running with voltage or temperature growing without bound.

Activity

  1. added theissue type on May 9, 2026
  2. self-assigned this
    on May 11, 2026
  3. DavidMStraub commented on May 11, 2026

    @DavidMStraub
    CollaboratorAuthor

    Hi @milanofthe, I'm unsure what the correct pattern is in PathSim to auto-terminate simulations on specific events. Concretely, I need to stop when the voltage crosses the lower or upper cutoff voltage of the cell, as results become nonsensical outside (in real systems, the battery management system needs to ensure the thresholds are never crossed). I can use ZeroCrossingDown to define the event, but I'm not sure where to put them, as the block doesn't know about the simulation.

  4. milanofthe commented on May 11, 2026

    @milanofthe
    Member

    In the Python script you can do something like this, it sets a flag internally and exits the timestepping loop at next iteration. By the time its called Sim is already in the namespace:

    #events to detect
    def func_evt_1(t):
        i, o, s = VL()
        return s[0] - 4
    
    def func_evt_2(t):
        i, o, s = VL()
        return s[1] - 4
    
    E1 = ZeroCrossingUp(
        func_evt=func_evt_1,
        tolerance=1e-4
        )
    
    E2 = ZeroCrossingUp(
        func_evt=func_evt_2,
        func_act=lambda _: Sim.stop(), # <--- this one 
        tolerance=1e-4
        )
    
    E3 = ZeroCrossingDown(
        func_evt=func_evt_1,
        tolerance=1e-4
        )
    
    E4 = ZeroCrossingDown(
        func_evt=func_evt_2,
        tolerance=1e-4
        )
    
    #initialize simulation with the blocks, connections, timestep and logging enabled
    Sim = Simulation(
        blocks, 
        connections, 
        [E1, E2, E3, E4],
        dt=dt, 
        log=True, 
        Solver=RKBS32, 
        tolerance_lte_rel=1e-4, 
        tolerance_lte_abs=1e-6
        )

    A distinct Stop event might be nice to have though.

  5. DavidMStraub commented on May 11, 2026

    @DavidMStraub
    CollaboratorAuthor

    Somehow I think it would be nice if models/blocks could gracefully tell the running simulation to stop.

    What about the following modification to PathSim:

    class StopSimulation(Exception):
        pass
    
    class Simulation:
        def _run_loop(...):
            # ...
            try:
                success, ... = self.timestep(...)
            except StopSimulation:
                self.stop()
                break

    What do you think? Then certain events could just throw StopSimulation and it would bubble up to the calling simulation, without need for a reference to it.

  6. milanofthe commented on May 11, 2026

    @milanofthe
    Member

    Yes that would work. I have played with the idea of introducing more custom exceptions anyway. This would be another argument for it.

  7. DavidMStraub commented on May 11, 2026

    @DavidMStraub
    CollaboratorAuthor

    I could draft a PR.

  8. DavidMStraub commented on May 20, 2026

    @DavidMStraub
    CollaboratorAuthor

    Fixed in #14.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions