PySWMM Documentation

repository·main·Indexed 18 days ago

https://github.com/pyswmm/pyswmm

PySWMM 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.

Tokens
6.8K
Snippets
21
Records
37
Agent score
59%

What's inside PySWMM

  1. Overview of PySWMM capabilities

    main
    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.
  2. Use the Raingages module to manage rain gages

    main

    The pyswmm.raingages module provides classes to interact with and manage rain gages within a SWMM5 simulation. It includes the RainGages container class for managing multiple gages and the RainGage class for interacting with individual rain gage objects.

    To use these, you typically access them through a simulation instance. Detailed API signatures for RainGages and RainGage can be found in the specific API documentation.

  3. Use the PySWMM Output module to access simulation results

    main

    The pyswmm.output module 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.
  4. What is PySWMM and how does it differ from standard SWMM?

    main

    PySWMM is a Python interface for the EPA's Stormwater Management Model (SWMM5). Unlike standard SWMM distributions that primarily rely on reading and writing .inp files (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.

  5. Access LID control layers using pyswmm.lidlayers

    main

    LID controls are composed of multiple layers. PySWMM exposes these layers through the pyswmm.lidlayers module. Each class in this module represents a specific type of layer within an LID Control object:

    • Surface
    • Soil
    • Storage
    • Pavement
    • Drain
    • DrainMat

    You can use these classes to access or modify the properties of the specific layers that make up a defined LID control.

  6. Access SWMM nodes using the Nodes module

    main
    The pyswmm.nodes module provides interfaces to interact with various types of nodes within a Stormwater Management Model (SWMM5) simulation. You can access and manipulate nodes through the Nodes collection, which contains individual Node objects. Specialized node types include Outfall and Storage nodes, which inherit from the base Node class but provide specific functionality relevant to their roles in the hydraulic model.
  7. Implement PySWMM Controls

    main

    PySWMM 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 *.rpt file.
    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.5
  8. Use the Simulation module to control SWMM5 runs

    main
    The pyswmm.simulation module provides the primary interface for controlling the execution of a Stormwater Management Model (SWMM5) simulation. It contains the Simulation class for managing active runs and the SimulationPreConfig class for configuring simulation parameters before the engine starts.
  9. Access Low-Impact Development (LID) controls in PySWMM

    main

    PySWMM provides access to objects defined in the [LID_CONTROLS] section of a SWMM input file through the pyswmm.lidcontrols module.

    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 LidControls API documentation.

  10. Install PySWMM

    main

    You 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]
  11. Load and run a SWMM simulation

    main

    You can load a model using the Simulation class. There are two primary ways to run it:

    1. Simple Execution: Use sim.execute() if you only want to run the model without interacting with parameters during the simulation.
    2. Manual Stepping: Use a with statement to iterate through the simulation steps. This is the recommended approach for interacting with the model (getting/setting parameters) while it runs. The with statement 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
  12. Import PySWMM and initialize a Simulation

    main

    To use PySWMM, import the Simulation class from the pyswmm module. You can then initialize a simulation object by passing the path to a SWMM input file (*.inp) to the Simulation constructor. 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")