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

No comments:

Today is day 356 or 97.5 percent of the year 2024

English: Today is day 356 or 97.5 percent of the year 2024 Mandarin Chinese: 今天是2024年的第356天,即97.5% Hindi: आज 2024 का 356वां दिन या 97.5 प्रत...