def __init__(self, inputfile, reportfile=None, outputfile=None): self._model = PySWMM(inputfile, reportfile, outputfile) self._model.swmm_open() self._isOpen = True self._advance_seconds = None self._isStarted = False self._initial_conditions = None
def test_nodes_4(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) swmmobject.swmm_open() print(swmmobject.getNodeParam('J2', tka.NodeParams.invertElev)) swmmobject.setNodeParam('J2', tka.NodeParams.invertElev, 19) print(swmmobject.getNodeParam('J2', tka.NodeParams.invertElev)) print(swmmobject.getLinkParam('C1:C2', tka.LinkParams.offset1)) swmmobject.setLinkParam('C1:C2', tka.LinkParams.offset1, 19) print(swmmobject.getLinkParam('C1:C2', tka.LinkParams.offset1)) print(swmmobject.getSubcatchParam('S2', tka.SubcParams.area)) swmmobject.setSubcatchParam('S2', tka.SubcParams.area, 19) print(swmmobject.getSubcatchParam('S2', tka.SubcParams.area)) swmmobject.swmm_start() i = 0 while (True): time = swmmobject.swmm_stride(600) i += 1 if (time <= 0.0): break if i % 144 == 0: print(i) swmmobject.setLinkParam('C1:C2', tka.LinkParams.qLimit, 0.25) swmmobject.swmm_end() swmmobject.swmm_close() print("swmm_step() Check Passed")
def test_pyswmm_exception(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) with pytest.raises(PYSWMMException): swmmobject.swmm_open() print(swmmobject.fileLoaded) swmmobject.swmm_open() swmmobject.swmm_close()
def test_set_current_time(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) swmmobject.swmm_open() print(swmmobject.getSimulationDateTime(0)) print(swmmobject.getSimulationDateTime(0) - timedelta(days=10)) swmmobject.setSimulationDateTime( 0, swmmobject.getSimulationDateTime(0) - timedelta(days=10), ) print(swmmobject.getSimulationDateTime(0)) print(swmmobject.getSimulationDateTime(1)) print(swmmobject.getSimulationDateTime(1) - timedelta(days=10)) swmmobject.setSimulationDateTime( 1, swmmobject.getSimulationDateTime(1) - timedelta(days=10), ) print(swmmobject.getSimulationDateTime(1)) print(swmmobject.getSimulationDateTime(2)) print(swmmobject.getSimulationDateTime(2) - timedelta(days=10)) swmmobject.setSimulationDateTime( 2, swmmobject.getSimulationDateTime(2) - timedelta(days=10), ) print(swmmobject.getSimulationDateTime(2)) swmmobject.swmm_start() i = 0 while (True): time = swmmobject.swmm_stride(600) i += 1 print(swmmobject.getCurrentSimulationTime()) print(type(swmmobject.getCurrentSimulationTime())) if (time <= 0.0): break if i % 144 == 0: print(i) swmmobject.swmm_end() swmmobject.swmm_close()
def __init__(self, inputfile, reportfile=None, outputfile=None, swmm_lib_path=None): self._model = PySWMM(inputfile, reportfile, outputfile, swmm_lib_path) self._model.swmm_open() self._isOpen = True self._advance_seconds = None self._isStarted = False self._callbacks = { "before_start": None, "before_step": None, "after_step": None, "before_end": None, "after_end": None, "after_close": None }
def test_current_time(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) swmmobject.swmm_open() date = swmmobject.getSimulationDateTime(0) print(date) swmmobject.swmm_start() i = 0 while (True): time = swmmobject.swmm_stride(600) print(time) i += 1 current_time = swmmobject.getCurrentSimualationTime() print(current_time) print(type(swmmobject.getCurrentSimualationTime())) if (time <= 0.0): break if i % 144 == 0: print(i) swmmobject.swmm_end() swmmobject.swmm_report() swmmobject.swmm_close()
def test_nodes_1(): swmmobject = PySWMM(MODEL_WEIR_SETTING_PATH) swmmobject.swmm_open() node = Node(swmmobject, "J1") print(node.invert_elevation)
episode_timer = 0 while episode_timer < episode_count: """ i=random.randint(1,3) """ i = 1 sim_num = i #run SWMM rain = rain_generation(i) swmm_model = PySWMM(r'../model/test' + str(i) + '.inp', r'../model/test' + str(i) + '.rpt', r'../model/test' + str(i) + '.out') swmm_model.swmm_open() swmm_model.swmm_start() n1 = "Tank_LaoDongLu" n2 = "Tank_ChengXi" p1 = "Pump_LaoDongLu1" p2 = "Pump_LaoDongLu2" p3 = "Pump_LaoDongLu3" p4 = "Pump_ChengXi1" p5 = "Pump_ChengXi2" p6 = "Pump_ChengXi3" p7 = "Pump_ChengXi4" node = [n1, n2] pump = [p1, p2, p3, p4, p5, p6, p7]
if i == 1: rain = rain_case_1 elif i == 2: rain = rain_case_2 else: rain = rain_case_3 #run SWMM #rain=rain_generation(i) #sim_num=1 #rain = rain_generation(1) #swmm_model = PySWMM('test_rain_case_0.inp','test_rain_case_0.rpt','test_rain_case_0.out') swmm_model = PySWMM('test_rain_case_' + str(i) + '.inp', 'test_rain_case_' + str(i) + '.rpt', 'test_rain_case_' + str(i) + '.out') #swmm_model = PySWMM('test'+str(i)+'.inp','test'+str(i)+'.rpt','test'+str(i)+'.out') swmm_model.swmm_open() swmm_model.swmm_start() n1 = "Tank_LaoDongLu" n2 = "Tank_ChengXi" p1 = "Pump_LaoDongLu1" p2 = "Pump_LaoDongLu2" p3 = "Pump_LaoDongLu3" p4 = "Pump_ChengXi1" p5 = "Pump_ChengXi2" p6 = "Pump_ChengXi3" p7 = "Pump_ChengXi4" node = [n1, n2] pump = [p1, p2, p3, p4, p5, p6, p7]
def test_nodes_1(): ''' pytest pyswmm/tests/test_nodes.py -k `test_nodes_1` ''' swmmobject = PySWMM(MODEL_WEIR_SETTING_PATH) swmmobject.swmm_open() node = Node(swmmobject, "J1") assert(node.invert_elevation == 20.728)
def test_error_rpt_out(): swmmobject = PySWMM(MODEL_WEIR_SETTING_PATH) swmmobject.swmm_open()
def test_plot_weir_setting(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) swmmobject.swmm_open() swmmobject.swmm_start(True) fig = plt.figure() ax = fig.add_subplot(221) ax.set_ylabel('Flow Rate') line, = ax.plot([], [], label='C3') ax.grid() ax.legend() ax2 = fig.add_subplot(223, sharex=ax) ax2.set_ylabel('Setting') line2, = ax2.plot([], [], label='C3') ax2.grid() xdata, ydata = [], [] ydata2 = [] ax3 = fig.add_subplot(2, 2, (2, 4)) arr_lena = read_png(IMAGE_WEIR_SETTING_PATH) imagebox = OffsetImage(arr_lena, zoom=0.45) ab = AnnotationBbox( imagebox, (0.5, 0.5), xybox=(0.5, 0.5), xycoords='data', boxcoords="offset points", pad=0.0, ) ax3.add_artist(ab) ax3.axis('off') def data_gen(t=0): i = 0 while (True): time = swmmobject.swmm_stride(300) i += 1 if i == 80: swmmobject.setLinkSetting('C3', 0.9) elif i == 90: swmmobject.setLinkSetting('C3', 0.8) elif i == 100: swmmobject.setLinkSetting('C3', 0.7) elif i == 110: swmmobject.setLinkSetting('C3', 0.6) elif i == 120: swmmobject.setLinkSetting('C3', 0.5) elif i == 130: swmmobject.setLinkSetting('C3', 0.4) elif i == 140: swmmobject.setLinkSetting('C3', 0.3) elif i == 150: swmmobject.setLinkSetting('C3', 0.2) elif i == 160: swmmobject.setLinkSetting('C3', 0.1) elif i == 170: swmmobject.setLinkSetting('C3', 0.0) elif i == 220: swmmobject.setLinkSetting('C3', 1.0) if i > 0 and time == 0.0: break if i > 0 and time > 0: yield time def run(t): xdata.append(t) new_y = swmmobject.getLinkResult('C3', tka.LinkResults.newFlow) ydata.append(new_y) new_y2 = swmmobject.getLinkResult('C3', tka.LinkResults.setting) ydata2.append(new_y2) xmin, xmax = ax.get_xlim() ymin, ymax = ax.get_ylim() ymin2, ymax2 = ax2.get_ylim() # ax if new_y > ymax: ax.set_ylim(-0.1, 1.5 * ymax) if t >= xmax: ax.set_xlim(xmin, 1.5 * xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) # ax1 if new_y2 > ymax2: ax2.set_ylim(-0.1, 1.2 * ymax2) line2.set_data(xdata, ydata2) ani = animation.FuncAnimation(fig, run, data_gen, blit=False, repeat=False, save_count=800, interval=10) show_fig = False if show_fig: plt.show() else: movie_path = os.path.join(DATA_PATH, "weir_setting.mp4") print(movie_path, ani) # ani.save(movie_path, fps=20, dpi=170, bitrate=50000) plt.close() swmmobject.swmm_end() swmmobject.swmm_close() print("Check Passed")
def test_plot_node_inflows(): swmmobject = PySWMM(*get_model_files(MODEL_NODE_INFLOWS_PATH)) swmmobject.swmm_open() swmmobject.swmm_start() fig = plt.figure() ax = fig.add_subplot(2, 3, (1, 2)) ax.set_ylabel('Flow Rate') line, = ax.plot([], [], label='C3') ax.grid() ax.legend() ax2 = fig.add_subplot(2, 3, (4, 5), sharex=ax) ax2.set_ylabel('Toolkit API\nInflow') line2, = ax2.plot([], [], label='J1') ax2.grid() xdata, ydata = [], [] ydata2 = [] ax3 = fig.add_subplot(2, 3, (3, 6)) arr_lena = read_png(IMAGE_NODE_INFLOWS_PATH) imagebox = OffsetImage(arr_lena, zoom=0.67) ab = AnnotationBbox( imagebox, (0.5, 0.5), xybox=(0.5, 0.5), xycoords='data', boxcoords="offset points", pad=0.0, ) ax3.add_artist(ab) ax3.axis('off') def data_gen(t=0): i = 0 while (True): if i >= 80: swmmobject.setNodeInflow('J1', random.randint(1, 5)) time = swmmobject.swmm_stride(1500) i += 1 if i > 0 and time == 0.0: break if i > 0 and time > 0: yield time def run(t): xdata.append(t) new_y = swmmobject.getLinkResult('C2', tka.LinkResults.newFlow) ydata.append(new_y) new_y2 = swmmobject.getNodeResult('J1', tka.NodeResults.newLatFlow) ydata2.append(new_y2) xmin, xmax = ax.get_xlim() ymin, ymax = ax.get_ylim() ymin2, ymax2 = ax2.get_ylim() # ax if new_y > ymax: ax.set_ylim(-0.1, 1.5 * ymax) if t >= xmax: ax.set_xlim(xmin, 1.5 * xmax) ax.figure.canvas.draw() line.set_data(xdata, ydata) # ax1 if new_y2 > ymax2: ax2.set_ylim(-0.1, 1.2 * ymax2) line2.set_data(xdata, ydata2) ani = animation.FuncAnimation( fig, run, data_gen, blit=False, repeat=False, save_count=800, interval=10, ) show_figure = False if show_figure: plt.show() else: movie_path = os.path.join(DATA_PATH, "node_inflows.mp4") print(movie_path, ani) # ani.save(movie_path, fps=20, dpi=170, bitrate=50000) plt.close() swmmobject.swmm_end() swmmobject.swmm_report() swmmobject.swmm_close() print("Check Passed")
def test_warning(): swmmobject = PySWMM(*get_model_files(MODEL_WEIR_SETTING_PATH)) swmmobject.swmm_open() swmmobject.getLinkResult('C2', 0) swmmobject.swmm_close()
# Simulation Time Steps episode_count = 198 timesteps = episode_count * 576 epi_start = 0 epi_end = 1 epsilon_value = np.linspace(epi_start, epi_end, timesteps + 10) episode_timer = 0 while episode_timer < episode_count: #run SWMM swmm_model = PySWMM('test.inp', 'test.rpt', 'test.out') swmm_model.swmm_open() swmm_model.swmm_start() n1 = "Tank_LaoDongLu" n2 = "Tank_ChengXi" p1 = "Pump_LaoDongLu1" p2 = "Pump_LaoDongLu2" p3 = "Pump_LaoDongLu3" p4 = "Pump_ChengXi1" p5 = "Pump_ChengXi2" p6 = "Pump_ChengXi3" p7 = "Pump_ChengXi4" node = [n1, n2] pump = [p1, p2, p3, p4, p5, p6, p7] for i in node_controlled: