Friday, February 10, 2023

SWMM 5.2 App Python Code - Open, Run and Close Files

 This code is a Python interface for a SWMM library (_lib) using the ctypes module. It provides functions to run, open, start, step, stride, end, get mass balance error, report, and close a SWMM simulation. The functions take various arguments, such as filenames, save flags, time steps, and return values, such as elapsed time, mass balance error, and status codes. The ctypes module converts Python data types into C data types so they can be passed as arguments to the SWMM library.


def getVersion(): return _lib.swmm_getVersion() def run(f1, f2, f3 = ''): return _lib.swmm_run(ctypes.c_char_p(f1.encode()), ctypes.c_char_p(f2.encode()), ctypes.c_char_p(f3.encode())) def open(f1, f2, f3 = ''): return _lib.swmm_open(ctypes.c_char_p(f1.encode()), ctypes.c_char_p(f2.encode()), ctypes.c_char_p(f3.encode())) def start(saveFlag): return _lib.swmm_start(ctypes.c_int(saveFlag)) def step(): elapsed_time = ctypes.c_double() _lib.swmm_step(ctypes.byref(elapsed_time)) return elapsed_time.value def stride(strideStep): elapsed_time = ctypes.c_double() _lib.swmm_stride(ctypes.c_int(strideStep), ctypes.byref(elapsed_time)) return elapsed_time.value def end(): _lib.swmm_end() def getMassBalErr(): runoff = ctypes.c_float() flow = ctypes.c_float() qual = ctypes.c_float() _lib.swmm_getMassBalErr( ctypes.byref(runoff), ctypes.byref(flow), ctypes.byref(qual)) return runoff.value, flow.value, qual.value def report(): return _lib.swmm_report() def close(): _lib.swmm_close()



What does the Acronmym RAFTS In InfoWorks ICM Mean?

A method is presented to extend a single event urban rainfall/runoff model into a statistically based runoff frequency model by introducing a loss model which utilizes as input joint probability data to link the stochastic elements of rainfall and catchment antecedent soil moisture with the deterministic parts of the rainfall/runoff process. 

The single event model briefly described has been known for nearly a decade as the Regional Stormwater Drainage Model (RSWM) but due to extensions and additions to its structure, including the loss model, which is known as the Stochastic/Deterministic Loss Model (SDLM), the rainfall/runoff model described in the paper is referred to as the Runoff Analysis Flow Training System (RAFTS).

Thursday, February 9, 2023

SWMM 5.2 App Python Code for the Interface

This code is a Python interface to the SWMM5 simulation engine. It provides functions to control and obtain information from the SWMM5 simulation engine using the SWMM5 API. The functions cover various aspects of the simulation, including opening and running the simulation, reporting the results, and accessing the properties of the simulation objects, such as nodes and links. The code also includes a function to decode the simulation date and time into a Python datetime object.


"""Python SWMM5 interface"""

import ctypes
import platform
import datetime

_plat= platform.system()
if _plat=='Linux':
    _lib = ctypes.CDLL("libswmm5.so")
elif _plat=='Windows':
    _lib = ctypes.WinDLL(".\swmm5.dll")
else:
    Exception('Platform '+ _plat +' unsupported')

def getVersion():
    return _lib.swmm_getVersion()

def run(f1, f2, f3 = ''):
    return _lib.swmm_run(ctypes.c_char_p(f1.encode()), 
                        ctypes.c_char_p(f2.encode()), 
                        ctypes.c_char_p(f3.encode()))

def open(f1, f2, f3 = ''):
    return _lib.swmm_open(ctypes.c_char_p(f1.encode()), 
                        ctypes.c_char_p(f2.encode()), 
                        ctypes.c_char_p(f3.encode()))

def start(saveFlag):
    return _lib.swmm_start(ctypes.c_int(saveFlag))

def step():
    elapsed_time = ctypes.c_double()
    _lib.swmm_step(ctypes.byref(elapsed_time))
    return elapsed_time.value

def stride(strideStep):
    elapsed_time = ctypes.c_double()
    _lib.swmm_stride(ctypes.c_int(strideStep), ctypes.byref(elapsed_time))
    return elapsed_time.value

def end():
    _lib.swmm_end()

def getMassBalErr():     
    runoff = ctypes.c_float()
    flow = ctypes.c_float()
    qual = ctypes.c_float()
    _lib.swmm_getMassBalErr(
        ctypes.byref(runoff), ctypes.byref(flow), ctypes.byref(qual))
    return runoff.value, flow.value, qual.value

def report():
    return _lib.swmm_report()

def close():
  _lib.swmm_close()

def getWarnings():
    return _lib.swmm_getWarnings()

def getError():
    errmsg = ctypes.create_string_buffer(240)
    _lib.swmm_getError(ctypes.byref(errmsg), ctypes.c_int(240))
    return errmsg.value.decode()

def getCount(objtype):
    return _lib.swmm_getCount(ctypes.c_int(objtype))

def getName(objtype, index, size):
    name = ctypes.create_string_buffer(size)
    _lib.swmm_getName(
        ctypes.c_int(objtype), ctypes.c_int(index), ctypes.byref(name), ctypes.c_int(size))
    return name.value.decode()

def getIndex(objtype, name):
    return _lib.swmm_getIndex(ctypes.c_int(objtype), ctypes.c_char_p(name.encode()))

def getValue(property, index):
    _lib.swmm_getValue.restype = ctypes.c_double   
    return _lib.swmm_getValue(ctypes.c_int(property), ctypes.c_int(index))

def getSavedValue(property, index, period):
    _lib.swmm_getSavedValue.restype = ctypes.c_double   
    return _lib.swmm_getSavedValue(
        ctypes.c_int(property), ctypes.c_int(index), ctypes.c_int(period))

def setValue(property, index, value):
    _lib.swmm_setValue(
        ctypes.c_int(property), ctypes.c_int(index), ctypes.c_double(value))

def writeLine(line):
    _lib.swmm_writeLine(ctypes.c_char_p(line.encode()))

def decodeDate(date):

    """  Decodes a SWMM DateTime value to a Python DateTime value. """
    """  Use Python's datetime.weekday()  method to get day of week """
    """  (where Monday = 0 and Sunday = 6) if needed. """

    year= ctypes.c_int()
    month= ctypes.c_int()
    day= ctypes.c_int()
    hour= ctypes.c_int()
    minute= ctypes.c_int()
    second= ctypes.c_int()
    dayofweek= ctypes.c_int()
    _lib.swmm_decodeDate(
        ctypes.c_double(date), ctypes.byref(year), ctypes.byref(month),
        ctypes.byref(day),  ctypes.byref(hour), ctypes.byref(minute),
        ctypes.byref(second), ctypes.byref(dayofweek))
    d = datetime.datetime(
        year.value, month.value, day.value, hour.value, minute.value, second.value)
    return d

SWMM 5.2 App Python Code for the Index

 

This code is a Python interface to the SWMM5 hydrology and hydraulic simulation software. It uses the ctypes library to interface with the SWMM5 library, which is either a shared library file on Linux systems (libswmm5.so) or a dynamic-link library file on Windows systems (swmm5.dll). The code provides several functions to run and control the SWMM5 simulation, such as run, open, step, end, close, and report. It also provides functions to retrieve information from the simulation, such as getMassBalErr, getWarnings, getError, getValue, getSavedValue, and decodeDate. Additionally, the code includes several constants that are used to specify the type of objects and properties in the simulation.


Index NameInteger ValueDescription
GAGE0
SUBCATCH1
NODE2
LINK3
SYSTEM100
JUNCTION0
OUTFALL1
STORAGE2
DIVIDER3
CONDUIT0
PUMP1
ORIFICE2
WEIR3
OUTLET4
GAGE_RAINFALL100
SUBCATCH_AREA200
SUBCATCH_RAINGAGE201
SUBCATCH_RAINFALL202
SUBCATCH_EVAP203
SUBCATCH_INFIL204
SUBCATCH_RUNOFF205
SUBCATCH_RPTFLAG206
NODE_TYPE300
NODE_ELEV301
NODE_MAXDEPTH302
NODE_DEPTH303
NODE_HEAD304
NODE_VOLUME305
NODE_LATFLOW306
NODE_INFLOW307
NODE_OVERFLOW308
NODE_RPTFLAG309
LINK_TYPE400
LINK_NODE1401
LINK_NODE2402
LINK_LENGTH403
LINK_SLOPE404
LINK_FULLDEPTH405
LINK_FULLFLOW406
LINK_SETTING407
LINK_TIMEOPEN408
LINK_TIMECLOSED409
LINK_FLOW410
LINK_DEPTH411
LINK_VELOCITY412
LINK_TOPWIDTH413
LINK_RPTFLAG414
STARTDATE0
CURRENTDATE1
ELAPSEDTIME2
ROUTESTEP3
MAXROUTESTEP4
REPORTSTEP5
TOTALSTEPS6
NOREPORT7
FLOWUNITS8
CFS0
GPM1
MGD2
CMS3
LPS4
MLD5

Introduction to Scenarios in ICM

### Introduction to Scenarios in ICM In network modeling software like InfoWorks ICM, scenarios are a powerful feature that allows users to ...