Skip to content
forked from pyswmm/swmmio

Python tools for interacting with, editing, and visualizing EPA SWMM5 models

License

Notifications You must be signed in to change notification settings

mehmetbercan/swmmio

 
 

Repository files navigation

SWMMIO

v0.3.6.dev0

Build status Build Status

Kool Picture SWMMIO is a set of python tools aiming to provide a means for version control and visualizing results from the EPA Stormwater Management Model (SWMM). Command line tools are also provided for running models individually and in parallel via Python's multiprocessing module. These tools are being developed specifically for the application of flood risk management, though most functionality is applicable to SWMM modeling in general.

Prerequisites

SWMMIO functions primarily by interfacing with .inp and .rpt (input and report) files produced by SWMM. Functions within the run_models module rely on a SWMM5 engine which can be downloaded here.

Dependencies

Installation:

Before installation, it's recommended to first activate a virtualenv to not crowd your system's package library. If you don't use any of the dependencies listed above, this step is less important. SWMMIO can be installed via pip in your command line:

pip install swmmio

Basic Usage

The swmmio.Model() class provides the basic endpoint for interfacing with SWMM models. To get started, save a SWMM5 model (.inp) in a directory with its report file (.rpt). A few examples:

import swmmio

#instantiate a swmmio model object
mymodel = swmmio.Model('/path/to/directory with swmm files')

#Pandas dataframe with most useful data related to model nodes, conduits, and subcatchments
nodes = mymodel.nodes()
conduits = mymodel.conduits()
subs = mymodel.subcatchments()

#enjoy all the Pandas functions
nodes.head()
InvertElevMaxDepthSurchargeDepthPondedAreaTypeAvgDepthMaxNodeDepthMaxHGLMaxDay_depthMaxHr_depthHoursFloodedMaxQMaxDay_floodMaxHr_floodTotalFloodVolMaximumPondDepthXYcoords
Name
S42A_10.N_413.5066736.3269775.0110.0JUNCTION0.696.3319.83012:010.010.200.011:520.0006.332689107.0227816.000[(2689107.0, 227816.0)]
D70_ShunkStreet_Trunk_438.5084132.4936475.0744.0JUNCTION0.040.238.74012:14NaNNaNNaNNaNNaNNaN2691329.5223675.813[(2691329.5, 223675.813)]
TD61_1_2_905.15000015.3980080.00.0JUNCTION0.6815.4020.55011:550.0119.170.011:560.00015.402698463.5230905.720[(2698463.5, 230905.72)]
D66_36.D.7.C.1_1919.3200003.3357605.06028.0JUNCTION0.573.3822.70012:000.496.450.011:510.0083.382691999.0230309.563[(2691999.0, 230309.563)]
#write to a csv
nodes.to_csv('/path/mynodes.csv')

#calculate average and weighted average impervious
avg_imperviousness = subs.PercImperv.mean()
weighted_avg_imp = (subs.Area * subs.PercImperv).sum() / len(subs)

Generating Graphics

Create an image (.png) visualization of the model. By default, pipe stress and node flood duration is visualized if your model includes output data (a .rpt file should accompany the .inp).

from swmmio.graphics import swmm_graphics as sg
sg.draw_model(mymodel)

Default Draw Output

Use pandas to calculate some interesting stats, and generate a image to highlight what's interesting or important for your project:

#isolate nodes that have flooded for more than 30 minutes
flooded_series = nodes.loc[nodes.HoursFlooded>0.5, 'TotalFloodVol']
flood_vol = sum(flooded_series) #total flood volume (million gallons)
flooded_count = len(flooded_series) #count of flooded nodes

#highlight these nodes in a graphic
nodes['draw_color'] = '#787882' #grey, default node color
nodes.loc[nodes.HoursFlooded>0.5, 'draw_color'] = '#751167' #purple, flooded nodes

#set the radius of flooded nodes as a function of HoursFlooded
nodes.loc[nodes.HoursFlooded>1, 'draw_size'] = nodes.loc[nodes.HoursFlooded>1, 'HoursFlooded'] * 12

#make the conduits grey, sized as function of their geometry
conds['draw_color'] = '#787882'
conds['draw_size'] = conds.Geom1

#add an informative annotation, and draw:
annotation = 'Flooded Volume: {}MG\nFlooded Nodes:{}'.format(round(flood_vol), flooded_count)
sg.draw_model(mymodel, annotation=annotation, file_path='flooded_anno_example.png')

Flooded highlight

Building Variations of Models

Starting with a base SWMM model, other models can be created by inserting altered data into a new inp file. Useful for sensitivity analysis or varying boundary conditions, models can be created using a fairly simple loop, leveraging the modify_model package.

For example, climate change impacts can be investigated by creating a set of models with varying outfall Fixed Stage elevations:

import os, shutil
import swmmio
from swmmio.utils.modify_model import replace_inp_section
from swmmio import create_dataframeINP

#initialize a baseline model object
baseline = swmmio.Model(r'path\to\baseline.inp')
rise = 0.0 #set the starting sea level rise condition

#create models up to 5ft of sea level rise.
while rise <= 5:

    #create a dataframe of the model's outfalls
    outfalls = create_dataframeINP(baseline.inp.path, '[OUTFALLS]')

    #create the Pandas logic to access the StageOrTimeseries column of  FIXED outfalls
    slice_condition = outfalls.OutfallType == 'FIXED', 'StageOrTimeseries'

    #add the current rise to the outfalls' stage elevation
    outfalls.loc[slice_condition] = pd.to_numeric(outfalls.loc[slice_condition]) + rise

    #copy the base model into a new directory    
    newdir = os.path.join(baseline.inp.dir, str(rise))
    os.mkdir(newdir)
    newfilepath = os.path.join(newdir, baseline.inp.name + "_" + str(rise) + '_SLR.inp')
    shutil.copyfile(baseline.inp.path, newfilepath)

    #Overwrite the OUTFALLS section of the new model with the adjusted data
    replace_inp_section(newfilepath, '[OUTFALLS]', outfalls)

    #increase sea level rise for the next loop
    rise += 0.25

Access Model Network

The swmmio.Model class returns a Networkx MultiDiGraph representation of the model via that network parameter:

#access the model as a Networkx MutliDiGraph
G = model.network

#iterate through links
for u, v, key, data in model.network.edges(data=True, keys=True):

        print (key, data['Geom1'])
        # do stuff with the network

Running Models

Using the command line tool, individual SWMM5 models can be run by invoking the swmmio module in your shell as such:

$ python -m swmmio --run path/to/mymodel.inp

If you have many models to run and would like to take advantage of your machine's cores, you can start a pool of simulations with the --start_pool (or -sp) command. After pointing -sp to one or more directories, swmmio will search for SWMM .inp files and add all them to a multiprocessing pool. By default, -sp leaves 4 of your machine's cores unused. This can be changed via the -cores_left argument.

$ #run all models in models in directories Model_Dir1 Model_Dir2
$ python -m swmmio -sp Model_Dir1 Model_Dir2  

$ #leave 1 core unused
$ python -m swmmio -sp Model_Dir1 Model_Dir2  -cores_left=1

Warning

Using all cores for simultaneous model runs can put your machine's CPU usage at 100% for extended periods of time. This probably puts stress on your hardware. Use at your own risk.

Flood Model Options Generation

swmmio can take a set of independent storm flood relief (SFR) alternatives and combine them into every combination of potential infrastructure changes. This lays the ground work for identifying the most-efficient implementation sequence and investment level.

Consider the simplified situaiton where a city is interested in solving a flooding issue by installing new relief sewers along Street A and/or Street B. Further, the city wants to decide whether they should be 1 or 2 blocks long. Engineers then decide to build SWMM models for 4 potential relief sewer options:

  • A1 -> One block of relief sewer on Street A
  • A2 -> Two blocks of relief sewer on Street A
  • B1 -> One block of relief sewer on Street B
  • B2 -> Two blocks of relief sewer on Street B

To be comprehensive, implementation scenarios should be modeled for combinations of these options; it may be more cost-effective, for example, to build relief sewers on one block of Street A and Street B in combination, rather than two blocks on either street independently.

swmmio achieves this within the version_control module. The create_combinations() function builds models for every logical combinations of the segmented flood mitigation models. In the example above, models for the following scenarios will be created:

  • A1 with B1
  • A1 with B2
  • A2 with B1
  • A2 with B2

For the create_combinations() function to work, the model directory needs to be set up as follows:

├───Baseline
        baseline.inp
├───Combinations
└───Segments
    ├───A
    │   ├───A1
    │   │   A1.inp
    │   └───A2
    │       A2.inp
    └───B
        ├───B1
        │   B1.inp
        └───B2
            B2.inp

The new models will be built and saved within the Combinations directory. create_combinations() needs to know where these directories are and optionally takes version_id and comments data:

#load the version_control module
from swmmio.version_control import version_control as vc

#organize the folder structure
baseline_dir = r'path/to/Baseline/'
segments_dir = r'path/to/Segments/'
target_dir = r'path/to/Combinations/'

#generate flood mitigation options
vc.create_combinations(
    baseline_dir,
    segments_dir,
    target_dir,
    version_id='initial',
    comments='example flood model generation comments')

The new models will be saved in subdirectories within the target_dir. New models (and their containing directory) will be named based on a concatenation of their parent models' names. It is recommended to keep parent model names as concise as possible such that child model names are manageable. After running create_combinations(), your project directory will look like this:

├───Baseline
├───Combinations
│   ├───A1_B1
│   ├───A1_B2
│   ├───A2_B1
│   └───A2_B2
└───Segments
    ├───A
    │   ├───A1
    │   └───A2
    └───B
        ├───B1
        └───B2

SWMM Model Version Control

To add more segments to the model space, create a new segment directory and rerun the create_combinations() function. Optionally include a comment summarizing how the model space is changing:

vc.create_combinations(
    baseline_dir,
    alternatives_dir,
    target_dir,
    version_id='addA3',
    comments='added model A3 to the scope')

The create_combinations() function can also be used to in the same way to propogate a change in an existing segment (parent) model to all of the children. Version information for each model is stored within a subdirectory called vc within each model directory. Each time a model is modified from the create_combinations() function, a new "BuildInstructions" file is generated summarizing the changes. BuildInstructions files outline how to recreate the model with respect to the baseline model.

TO BE CONTINUED...

About

Python tools for interacting with, editing, and visualizing EPA SWMM5 models

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 91.1%
  • HTML 8.9%