Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pyswmm dependency #139

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file removed lib/linux/swmm5
Binary file not shown.
Binary file removed lib/windows/swmm5_22.exe
Binary file not shown.
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,5 @@ m2r
mistune==0.8.4
# Run dependencies
pyproj>=3.0.0
geopandas
pyswmm==1.2
9 changes: 4 additions & 5 deletions swmmio/defs/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
# This is the swmmio project root
ROOT_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))

# path to the SWMM5 executable used within the run_models module
if os.name == 'posix':
SWMM_ENGINE_PATH = os.path.join(ROOT_DIR, 'lib', 'linux', 'swmm5')
else:
SWMM_ENGINE_PATH = os.path.join(ROOT_DIR, 'lib', 'windows', 'swmm5_22.exe')
# path to the Python executable used to run your version of Python
PYTHON_EXE_PATH = "python"#os.path.join(os.__file__.split("lib/")[0],"bin","python")

# feature class name of parcels in geodatabase
PARCEL_FEATURES = r'PWD_PARCELS_SHEDS_PPORT'
Expand All @@ -25,3 +22,5 @@
BASEMAP_PATH = os.path.join(ROOT_DIR, 'swmmio', 'reporting', 'basemaps', 'index.html')
BETTER_BASEMAP_PATH = os.path.join(ROOT_DIR, 'swmmio', 'reporting', 'basemaps', 'mapbox_base.html')

# PySWMM Wrapper Path
PYSWMM_WRAPPER_PATH = os.path.join(ROOT_DIR, 'swmmio', 'wrapper', 'pyswmm_wrapper.py')
23 changes: 15 additions & 8 deletions swmmio/run_models/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,27 @@
from swmmio.utils.modify_model import replace_inp_section
from swmmio.run_models import defs
from swmmio import Model
from swmmio.defs.config import SWMM_ENGINE_PATH
from swmmio.defs.config import PYTHON_EXE_PATH, PYSWMM_WRAPPER_PATH


def run_simple(inp_path, swmm_eng=SWMM_ENGINE_PATH):
def run_simple(inp_path, py_path=PYTHON_EXE_PATH, pyswmm_wrapper=PYSWMM_WRAPPER_PATH):
"""
run a model once as is.
"""
print('running {} with {}'.format(inp_path, swmm_eng))
print('running {}'.format(inp_path))
#inp_path = model.inp.path
rpt_path = os.path.splitext(inp_path)[0] + '.rpt'
out_path = os.path.splitext(inp_path)[0] + '.out'

subprocess.call([swmm_eng, inp_path, rpt_path])
# Pass Environment Info to Run
env_definition = os.environ.copy()
env_definition["PATH"] = "/usr/sbin:/sbin:" + env_definition["PATH"]

def run_hot_start_sequence(inp_path, swmm_eng=SWMM_ENGINE_PATH):
subprocess.call([py_path, pyswmm_wrapper, inp_path, rpt_path, out_path],
env=env_definition)
return 0

def run_hot_start_sequence(inp_path, py_path=PYTHON_EXE_PATH):

# inp_path = model.inp.path
model = Model(inp_path)
Expand All @@ -33,15 +40,15 @@ def run_hot_start_sequence(inp_path, swmm_eng=SWMM_ENGINE_PATH):
model = replace_inp_section(model.inp.path, '[FILES]', hot1_df)
model = replace_inp_section(model.inp.path, '[REPORT]', defs.REPORT_none)
model = replace_inp_section(model.inp.path, '[OPTIONS]', defs.OPTIONS_no_rain)
subprocess.call([swmm_eng, model.inp.path, rpt_path])
run_simple(model.inp.path)

# if os.path.exists(hotstart1) and not os.path.exists(hotstart2):
#create new model inp with params to use hotstart1 and save hotstart2
print('with params to use hotstart1 and save hotstart2')
s = pd.Series(['USE HOTSTART "{}"'.format(hotstart1), 'SAVE HOTSTART "{}"'.format(hotstart2)])
hot2_df = pd.DataFrame(s, columns=['[FILES]'])
model = replace_inp_section(model.inp.path, '[FILES]', hot2_df)
subprocess.call([swmm_eng, model.inp.path, rpt_path])
run_simple(model.inp.path)

# if os.path.exists(hotstart2):
#create new model inp with params to use hotstart2 and not save anything
Expand All @@ -53,4 +60,4 @@ def run_hot_start_sequence(inp_path, swmm_eng=SWMM_ENGINE_PATH):
model = replace_inp_section(model.inp.path, '[REPORT]', defs.REPORT_none)# defs.REPORT_nodes_links)
model = replace_inp_section(model.inp.path, '[OPTIONS]', defs.OPTIONS_normal)

subprocess.call([swmm_eng, model.inp.path, rpt_path])
run_simple(model.inp.path)
66 changes: 66 additions & 0 deletions swmmio/wrapper/pyswmm_wrapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------
# Copyright (c) 2022 Bryant E. McDonnell
#
# Licensed under the terms of the BSD2 License
# See LICENSE.txt for details
# -----------------------------------------------------------------------------
"""
Function developed to execute a PySWMM Simulation on the command line. To run,
execute the following on the command line:

python --help # Produces a list of options

python <path>/pyswmm_wrapper.py <path>/*.inp <path>/*.rpt <path>/*.out # optional args
"""

import argparse
import pathlib
import pyswmm


def run_model():
"""Run Model."""
# Argument Resolution
parser = argparse.ArgumentParser()
parser.add_argument('inp_file', type=pathlib.Path,
help='Input File Path')
parser.add_argument('rpt_file', type=pathlib.Path, nargs='?',
help='Report File Path (Optional).')
parser.add_argument('out_file', type=pathlib.Path, nargs='?',
help='Output File Path (Optional).')

report_prog_help = "--report-progress can be useful for longer model runs. The drawback "\
+"is that it slows the simulation down. Use an integer to specify how "\
+"frequent to interrup the simulation. This depends of the number of time "\
+"steps"
parser.add_argument('--report_progress', default=False, type=int,
help=report_prog_help)
args = parser.parse_args()

# File Naming -> Str Paths
inp_file = str(args.inp_file)
if args.rpt_file:
rpt_file = str(args.rpt_file)
else:
rpt_file = args.rpt_file
out_file = str(args.out_file)
if args.out_file:
out_file = str(args.out_file)
else:
out_file = args.out_file

# Running the simulation without and with progress reporting.
if args.report_progress == False:
sim = pyswmm.Simulation(inp_file, rpt_file, out_file)
sim.execute()
else:
with pyswmm.Simulation(inp_file, rpt_file, out_file) as sim:
for ind, step in enumerate(sim):
if ind % args.report_progress == 0:
print(round(sim.percent_complete*1000)/10.0)

return 0

if __name__ in "__main__":
run_model()