Skip to content

Open/Close pipe in step-wise simulation #512

@ivan-parreno

Description

@ivan-parreno

Summary
When running a step-wise simulation with WNTR, after the initial simulation run, I want to change the status of a pipe. Specifically, if the pipe is closed, I want to open it; if it is open, I want to close it.

Example
I used a similar version to Net1.inp with the first link connecting tank with junction being changed.
Basically this works:
pipe.initial_status = wntr.network.LinkStatus.Closed
but this does not:
pipe.initial_status = wntr.network.LinkStatus.Closed
And only after the first time i've done:

  • sim = wntr.sim.WNTRSimulator(wn)

wn.options.time.hydraulic_timestep = 1
wn.options.time.report_timestep = 1
wn.options.time.pattern_timestep = 1
wn.options.time.rule_timestep = 1
wn.options.time.duration = 1

stat = pd.DataFrame()

for second in range(4):
print(f"\n--- Second {second} ---")

# Get the pipe/link with id "12"
pipe = wn.get_link("12")

# Change the pipe status at second 2
if second == 2:
    print("Initial status before change:", pipe.initial_status)
    # Toggle between Open and Closed status
    if pipe.initial_status == wntr.network.LinkStatus.Closed:
        pipe.initial_status = wntr.network.LinkStatus.Open
    else:
        pipe.initial_status = wntr.network.LinkStatus.Closed
    print("Changed status to:", pipe.initial_status)

# Retrieve statuses for debugging
initial_status = pipe.initial_status     

current_status = pipe.status              # effective status

# Print statuses with clear labels
print(f"Initial status  : {initial_status}")
print(f"Current status  : {current_status}")
print(f"Level of node '2': {wn.get_node('2').level}")

# Run the simulation
sim = wntr.sim.WNTRSimulator(wn)
results = sim.run_sim()

flowrate = results.link['flowrate']
pressure = results.node['pressure']

print("Flowrate:\n", flowrate)
print("Pressure:\n", pressure)
wn.options.time.duration += 1

wntr.network.write_inpfile(wn,"Modifying.inp")

Environment
Provide information on your computing environment.

  • Operating system: Windows 11
  • Python version: 3.11
  • WNTR version: Latest version

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions