Skip to content

Commit

Permalink
updated sim and network attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaklise committed Oct 16, 2023
1 parent f320df0 commit fc85408
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 64 deletions.
103 changes: 45 additions & 58 deletions wntr/stormwater/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,46 @@ class StormWaterNetworkModel(object):

def __init__(self, inp_file_name=None):

"""
swmmio.Model.links.geodataframe returns a geodataframe containing
PUMPS, CONDUITS, WEIRS, and ORIFICES joined with XSECTIONS and
COORDINATES
"""

if inp_file_name:
self._swmmio_model = swmmio.Model(inp_file_name)

# These dataframes can be used to modify the network model
# prior to running pyswmm, the updates are saved to a new INP file
# before running the simulation
self.nodes = self._swmmio_model.nodes.geodataframe.copy()
self.links = self._swmmio_model.links.geodataframe.copy()
self.subcatchments = self._swmmio_model.subcatchments.geodataframe.copy()
self.raingages = self._swmmio_model.inp.raingages.copy()
self.options = self._swmmio_model.inp.options.copy()
# Attributes of StormWaterNetworkModel link to attributes
# in swmmio.Model.inp, which contains dataframes from an INP file.
# The swmmio.Model.inp object also includes a .save method to
# write a new INP file.

# Nodes = Junctions, outfall, and storage nodes
self.junctions = self._swmmio_model.inp.junctions
self.outfalls = self._swmmio_model.inp.outfalls
self.storage = self._swmmio_model.inp.junctions

# Links = Conduits, weirs, orifices, and pumps
self.conduits = self._swmmio_model.inp.conduits
self.weirs = self._swmmio_model.inp.weirs
self.orifices = self._swmmio_model.inp.orifices
self.pumps = self._swmmio_model.inp.pumps

self.subcatchments = self._swmmio_model.inp.subcatchments
self.subareas = self._swmmio_model.inp.subareas

self.raingages = self._swmmio_model.inp.raingages
self.infiltration = self._swmmio_model.inp.infiltration
self.inflows = self._swmmio_model.inp.inflows
#self.dwf = self._swmmio_model.inp.dwf

self.curves = self._swmmio_model.inp.curves
self.timeseries = self._swmmio_model.inp.timeseries

self.options = self._swmmio_model.inp.options
self.files = self._swmmio_model.inp.files

self.coordinates = self._swmmio_model.inp.coordinates
self.vertices = self._swmmio_model.inp.vertices
self.polygons = self._swmmio_model.inp.polygons
self.xsections = self._swmmio_model.inp.xsections

else:
self._swmmio_model = None

self.nodes = None
self.links = None
self.subcatchments = None
self.raingages = None
self.options = None


@property
def node_name_list(self):
Expand All @@ -60,7 +73,8 @@ def node_name_list(self):
list of strings
"""
return self.junction_name_list + self.outfall_name_list + self.storage_name_list
return self.junction_name_list + self.outfall_name_list + \
self.storage_name_list

@property
def junction_name_list(self):
Expand All @@ -71,7 +85,7 @@ def junction_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.junctions.index)
return list(self.junctions.index)


@property
Expand All @@ -83,7 +97,7 @@ def outfall_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.outfalls.index)
return list(self.outfalls.index)


@property
Expand All @@ -95,7 +109,7 @@ def storage_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.storage.index)
return list(self.storage.index)

@property
def link_name_list(self):
Expand All @@ -118,7 +132,7 @@ def conduit_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.conduits.index)
return list(self.conduits.index)

@property
def weir_name_list(self):
Expand All @@ -129,7 +143,7 @@ def weir_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.weirs.index)
return list(self.weirs.index)

@property
def orifice_name_list(self):
Expand All @@ -140,7 +154,7 @@ def orifice_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.orifices.index)
return list(self.orifices.index)

@property
def pump_name_list(self):
Expand All @@ -151,7 +165,7 @@ def pump_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.pumps.index)
return list(self.pumps.index)

@property
def subcatchment_name_list(self):
Expand All @@ -162,7 +176,7 @@ def subcatchment_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.subcatchments.index)
return list(self.subcatchments.index)

@property
def raingage_name_list(self):
Expand All @@ -173,7 +187,7 @@ def raingage_name_list(self):
list of strings
"""
return list(self._swmmio_model.inp.raingages.index)
return list(self.raingages.index)

@property
def num_nodes(self):
Expand Down Expand Up @@ -258,30 +272,3 @@ def to_graph(self, node_weight=None, link_weight=None,
networkx MultiDiGraph
"""
return to_graph(self)


def udpate_model_inp(self, filename=None):
"""
Update self._swmmio_model.inp based udpates to self.nodes, self.links,
self.subcatchments, and self.raingages
"""
model_inp = self._swmmio_model.inp

# nodes
for df in [model_inp.junctions, model_inp.outfalls, model_inp.storage]:
df = self.nodes.loc[df.index, df.columns]
# links
for df in [model_inp.conduits, model_inp.weirs, model_inp.orifices, model_inp.pumps]:
df = self.links.loc[df.index, df.columns]
# subcatchments
for df in [model_inp.subcatchments]:
df = self.subcatchments.loc[df.index, df.columns]
# raingages
for df in [model_inp.raingages]:
df = self.raingages.loc[df.index, df.columns]
# options
for df in [model_inp.options]:
df = self.options.loc[df.index, df.columns]

if filename:
write_inpfile(self, filename)
17 changes: 11 additions & 6 deletions wntr/stormwater/sim.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import pyswmm

from wntr.stormwater.io import read_outfile
from wntr.stormwater.io import write_inpfile, read_outfile

os.environ["CONDA_DLL_SEARCH_MODIFICATION_ENABLE"] = "1"
# See https://github.com/OpenWaterAnalytics/pyswmm/issues/298
Expand All @@ -16,12 +16,17 @@ def run_sim(self, file_prefix='temp'):

inpfile = file_prefix + '.inp'
outfile = file_prefix + '.out'

# Update swmmio model inp based on swn data
self._swn.udpate_model_inp(inpfile)

sim = pyswmm.Simulation(inpfile)
sim.execute()
write_inpfile(self._swn, inpfile)

# The use of swmmio run command seems slower and would not report errors
# import subprocess
# subprocess.run("python -m swmmio --run " + inpfile)

with pyswmm.Simulation(inpfile) as sim:
for step in sim:
pass
sim.report()

results = read_outfile(outfile)

Expand Down

0 comments on commit fc85408

Please sign in to comment.