PySWMM Documentation
repository·main·Indexed 18 days ago
https://github.com/pyswmm/pyswmmPySWMM provides Python wrappers for the USEPA Stormwater Management Model (SWMM5), allowing users to create, manipulate, and study complex stormwater networks. It enables the implementation of Python-based control algorithms and real-time monitoring of hydraulic properties for Nodes, Links, and Subcatchments. The library includes the Simulation class for model execution and the Output class for extracting timeseries data from SWMM binary (.out) files. It supports SWMM engine versions 5.1.14 to 5.2.4 and requires a 64-bit Python installation.
What's inside PySWMM
- PySWMM is a Python package designed to load, manipulate, and study USEPA Stormwater Management Models (SWMM5). It provides a Python-based interface for complex network dynamics, allowing developers to implement control algorithms exclusively in Python. This enables the use of Python functions and objects to manage hydraulic trends and control actions, moving beyond the native EPA-SWMM control capabilities.
Use the Raingages module to manage rain gages
mainThe
pyswmm.raingagesmodule provides classes to interact with and manage rain gages within a SWMM5 simulation. It includes theRainGagescontainer class for managing multiple gages and theRainGageclass for interacting with individual rain gage objects.To use these, you typically access them through a simulation instance. Detailed API signatures for
RainGagesandRainGagecan be found in the specific API documentation.Use the PySWMM Output module to access simulation results
mainThe
pyswmm.outputmodule provides classes to extract time-series data and results from a completed SWMM simulation. You can access data for specific nodes, links, subcatchments, or the entire system using the following primary classes:Output: The base class for accessing simulation results.NodeSeries: For retrieving time-series data for nodes (e.g., junctions, outfalls).LinkSeries: For retrieving time-series data for links (e.g., conduits, orifices).SubcatchSeries: For retrieving time-series data for subcatchments.SystemSeries: For retrieving system-wide summary statistics and results.
What is PySWMM and how does it differ from standard SWMM?
mainPySWMM is a Python interface for the EPA's Stormwater Management Model (SWMM5). Unlike standard SWMM distributions that primarily rely on reading and writing
.inpfiles (which creates data redundancy), PySWMM provides direct access to the SWMM data model.This architecture allows for:
- Mid-simulation interaction: You can interact with the model during simulation time, rather than just before or after.
- Direct Data Access: Access node, link, subcatchment, LID statistics, and results both during and after simulation without duplicating the data model in external files.
- Parameter Manipulation: Use low-level getters and setters to edit hydraulic network settings, load externally-generated inflows, and manipulate Low Impact Development (LID) parameters on the fly.
PySWMM is designed to augment SWMM's capabilities while maintaining backward compatibility.
Access LID control layers using pyswmm.lidlayers
mainLID controls are composed of multiple layers. PySWMM exposes these layers through the
pyswmm.lidlayersmodule. Each class in this module represents a specific type of layer within an LID Control object:SurfaceSoilStoragePavementDrainDrainMat
You can use these classes to access or modify the properties of the specific layers that make up a defined LID control.
Access SWMM nodes using the Nodes module
mainThepyswmm.nodesmodule provides interfaces to interact with various types of nodes within a Stormwater Management Model (SWMM5) simulation. You can access and manipulate nodes through theNodescollection, which contains individualNodeobjects. Specialized node types includeOutfallandStoragenodes, which inherit from the baseNodeclass but provide specific functionality relevant to their roles in the hydraulic model.Implement PySWMM Controls
mainPySWMM allows you to move control rules from the native SWMM5 engine into Python. This enables the use of complex Python logic and external libraries for model control.
Priority and Execution:
- PySWMM control actions have the highest priority.
- They are evaluated at the end of each simulation step, after native EPA-SWMM5 controls have been evaluated.
- Any updates made via PySWMM are reported in the
*.rptfile.
from pyswmm import Simulation, Links, Nodes def TestDepth(node, node2): return node > node2 with Simulation('./testmodel.inp') as sim: link_object = Links(sim) c1c2 = link_object["C1:C2"] node_object = Nodes(sim) J1 = node_object["J1"] J2 = node_object["J2"] for step in sim: if TestDepth(J1.depth, J2.depth): c1c2.target_setting = 0.5Use the Simulation module to control SWMM5 runs
mainThepyswmm.simulationmodule provides the primary interface for controlling the execution of a Stormwater Management Model (SWMM5) simulation. It contains theSimulationclass for managing active runs and theSimulationPreConfigclass for configuring simulation parameters before the engine starts.Access Low-Impact Development (LID) controls in PySWMM
mainPySWMM provides access to objects defined in the
[LID_CONTROLS]section of a SWMM input file through thepyswmm.lidcontrolsmodule.To interact with LID controls, you use the following classes:
LidControls: Provides access to the collection of LID controls.LidControl: Represents an individual LID control object.
For detailed implementation patterns, refer to the
LidControlsAPI documentation.Install PySWMM
mainYou can install the latest version of PySWMM from PyPI using
pip.Starting from version 1.3.1, you can also specify a particular version of the SWMM engine (ranging from 5.1.14 to 5.2.4) during installation using pip extras.
# Standard installation pip install pyswmm # Install with a specific SWMM engine version pip install pyswmm[swmm5.2.4]Load and run a SWMM simulation
mainYou can load a model using the
Simulationclass. There are two primary ways to run it:- Simple Execution: Use
sim.execute()if you only want to run the model without interacting with parameters during the simulation. - Manual Stepping: Use a
withstatement to iterate through the simulation steps. This is the recommended approach for interacting with the model (getting/setting parameters) while it runs. Thewithstatement ensures automatic cleanup when the simulation completes.
from pyswmm import Simulation # Option 1: Simple execution sim = Simulation('./testmodel.inp') sim.execute() # Option 2: Manual stepping with interaction with Simulation('./testmodel.inp') as sim: for step in sim: pass- Simple Execution: Use
Import PySWMM and initialize a Simulation
mainTo use PySWMM, import the
Simulationclass from thepyswmmmodule. You can then initialize a simulation object by passing the path to a SWMM input file (*.inp) to theSimulationconstructor. Once initialized, you can run or edit the simulation using the available methods on the simulation object.from pyswmm import Simulation # Initialize a SWMM model with an input file sim = Simulation(r"./example.inp")