Autocalibration for EPA Stormwater Management Model (SWMM) version 5 using multi- or single objective optimization in R.
You're fully responsible for the any problems related to this software - I'm not maintaining it currently. It was tested with 5.0.022. It looks like if moving to SWMM version 5.1+, the binary file reader in RSWMM.r would have to be changed (probably a minor change). See item 33 under "Build 5.1.001 (3/24/2014)" on this EPA page listing SWMM changes by version.
This R code runs a SWMM input text file repeatedly with modification of the text file based on the last SWMM output and the next trial parameters selected by R optimization code. The prerequisites for using the code are having a SWMM input file, calibration data, willingness to read code (it's not that bad :)), and willingness to check the output closely as the code is experimental. These scripts currently only do autocalibration.
I am working on a similar, separate and larger programming effort (in parallelized Python not R). RSWMM is only a proof of concept. The new effort extends these ideas to a more flexible interface and tiered design optimization. If you are interested in any of that, or have any feedback on RSWMM, please let me know (see below)
- Get a SWMM .inp input text file
- Read below (a cut and paste of runRSWMM.r) and note each comment's warnings
- Choose multi- or single objective
- Get your calibration time series CSV in the right format, or use an argument to accomodate your format
- Run it after making sure you have disk space or you have edited the code to programmatically delete files
runSWMM.r is quoted below. It is just a lot of help comments around R function calls that use SWMM binary file reader as part of autocalibration.
*Read the rest of runRSWMM.r#runRSWMM Developed by Peter Steinberg #Version 1: December 2011 #Revision 1.1: January 1/10/2012, corrected problem in binary file reader
#General Notes #Before editing this script do the following things: #Move your SWMM file to a directory that can hold a lot of files #Test that you can run your SWMM file from this directory and you haven't messed up paths to files or something #Take your SWMM input file and replace the uncertain parameters with codes like
#You can repeat codes if you want the optimization algorithm to repeat a parameter. #For example, if you know 2 subcatchments should have the same infiltration rate, you
#Create a parameter bounds CSV file that looks like this (without the comment sign #):
#Create a calibration time series data CSV that looks like this (without the comment sign #): #Date ,(CFS) #1/1/07 0:01,0.08 #1/1/07 0:02,0.22 #1/1/07 0:03,0.38 #1/1/07 0:04,0.54 #1/1/07 0:05,0.67 #1/1/07 0:06,0.83
#REMEMBER YOU HAVE TO USE DOUBLE BACKSLASHES FOR ALL FILENAMES########## #You have to manually create all directories you provide. RSWMM does not make directories.
#Preliminaries: clear workspace and source the RSWMM code for a function library rm(list=ls()) #edit this source line to reflect where you have saved RSWMM.r. source("O:\departments\Water Quality\Users\Peter\programs\RSWMM\RSWMM.r")
#If you are doing a calibration run, you need to provide the following lines
#Calibration Data should be in a CSV with datetimes in the first column, #and data in the second column #The text file is assumed to have a one line header #Call this function with the correct dateFormat for your datetimes #the dateFormat is passed to strptime, so look for formatting information there #for example, dates like this 1/1/07 12:00, can be read with the default dateFormat #e.g.: #Date ,(CFS) #1/1/07 0:01,0.08 #1/1/07 0:02,0.22 #1/1/07 0:03,0.38 #1/1/07 0:04,0.54 #1/1/07 0:05,0.67 #1/1/07 0:06,0.83
calDataCSV="O:\departments\Water Quality\Users\Peter\programs\RSWMM\testingData\CalData1.csv"