Working with PySWMM#

If we really want to supercharge our modeling workflow, we can tap into the excellent functionality provided by pyswmm.

Note

pyswmm and swmmio | what’s the difference?

Generally speaking, we can use pyswmm to interact with the running simulation, implement control logic in Python, and edit of network and hydrologic parameters, all of which can be done without manipulating the *.inp file.

In contrast, swmmio provides functionality to read the *.inp and *.rpt files and manipulate the *.inp files, which is useful for programmatically generating models, post-processing results, and extracting model data for use in other applications.

We won’t get into the details of pyswmm here (see the official pyswmm docs for that). Here we’ll walk through a simple example that runs a model with pyswmm and post-processes the results with swmmio.

Instantiate a swmmio.Model#

We’ll start by instantiating a swmmio.Model with a model hosted in the swmm-nrtestsuite repo.

from IPython.display import HTML
import swmmio
import pyswmm 

# path to a SWMM model from swmm-nrtestsuite
model_path = 'https://raw.githubusercontent.com/USEPA/swmm-nrtestsuite/refs/heads/dev/public/examples/Example1.inp'
model = swmmio.Model(model_path)
model.summary
{'num_subcatchments': 8,
 'num_conduits': 13,
 'num_junctions': 13,
 'num_outfalls': 1,
 'num_raingages': 1,
 'catchment_area': np.int64(71),
 'mean_subcatchment_slope': np.float64(0.010000000000000002),
 'total_conduit_length': np.int64(4300),
 'invert_range': np.int64(35)}