def test_NthOrderReaction_conc(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'NthOrderReaction', 'parameters': { 'k': 0.01, 'n': 2.0 } } } conc = [] con = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: NOR = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] for step in sim: NOR.updateWQState() c = Tank.pollut_quality conc.append(c['P1']) with Simulation( "./tests/inps/tank_variableinflow_nthorderreaction.inp") as sim: Tank = Nodes(sim)["Tank"] for step in sim: co = Tank.pollut_quality con.append(co['P1']) error = mse(con, conc[1:]) print(error) assert error <= 0.03
def test_EventMeanConc_load(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'EventMeanConc', 'parameters': { 'C': 5.0 } } } conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: EMC = waterQuality(sim, dict1) Outfall = Nodes(sim)["Outfall"] for step in sim: EMC.updateWQState() c = Outfall.pollut_quality conc.append(c['P1']) flow.append(sim._model.getNodeResult("Outfall", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) with Simulation("./tests/inps/tank_variableinflow_emc.inp") as sim: Outfall = Nodes(sim)["Outfall"] for step in sim: c = Outfall.pollut_quality conc1.append(c['P1']) flow1.append(sim._model.getNodeResult("Outfall", 0)) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_CoRemoval_load(): dict1 = {'Tank': {'pollutant': 1, 'method': 'ConstantRemoval', 'parameters': {'R': 0.15}},\ 'Tank': {'pollutant': 0, 'method': 'CoRemoval', 'parameters': {'R1': 0.75, 'R2': 0.15}}} conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: CO = waterQuality(sim, dict1) Outfall = Nodes(sim)["Outfall"] for step in sim: CO.updateWQState() c = Outfall.pollut_quality conc.append(c['P1']) flow.append(sim._model.getNodeResult("Outfall", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) with Simulation("./tests/inps/tank_variableinflow_coremoval.inp") as sim: Outfall = Nodes(sim)["Outfall"] for step in sim: c = Outfall.pollut_quality conc1.append(c['P1']) flow1.append(sim._model.getNodeResult("Outfall", 0)) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_ConstantRemoval_conc(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'ConstantRemoval', 'parameters': { 'R': 0.5 } } } conc = [] con = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: CR = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] for step in sim: CR.updateWQState() c = Tank.pollut_quality conc.append(c['P1']) with Simulation( "./tests/inps/tank_variableinflow_constantremoval.inp") as sim: Tank = Nodes(sim)["Tank"] for step in sim: co = Tank.pollut_quality con.append(co['P1']) error = mse(con, conc[1:]) print(error) assert error <= 0.03
def test_CSTR_steadystate(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'CSTR', 'parameters': { 'k': -0.2, 'n': 1.0, 'c0': 10.0 } } } conc2 = [] vol = [] flow = [] with Simulation("./tests/inps/tank_constantinflow_notreatment.inp") as sim: Tank = Nodes(sim)["Tank"] for index, step in enumerate(sim): v = Tank.volume vol.append(v) q = Tank.total_inflow flow.append(q) with Simulation("./tests/inps/tank_constantinflow_notreatment.inp") as sim: CS = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] for index, step in enumerate(sim): CS.updateWQState_CSTR(index) c = Tank.pollut_quality conc2.append(c['P1']) C_steadystate = dict1['Tank']['parameters']['c0'] / ( (1 - (dict1['Tank']['parameters']['k'] * (np.mean(vol) / np.mean(flow))))**dict1['Tank']['parameters']['n']) error = (C_steadystate - conc2[-1]) / C_steadystate assert error <= 0.03
def test_EventMeanConc_conc(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'EventMeanConc', 'parameters': { 'C': 5.0 } } } conc = [] con = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: EMC = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] for step in sim: EMC.updateWQState() c = Tank.pollut_quality conc.append(c['P1']) with Simulation("./tests/inps/tank_variableinflow_emc.inp") as sim: Tank = Nodes(sim)["Tank"] for step in sim: co = Tank.pollut_quality con.append(co['P1']) error = mse(con, conc) print(error) assert error <= 0.03
def test_ConstantRemoval_load(): dict1 = {'Tank': {0: 0.5}} conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./inps/tank_variableinflow_notreatment.inp") as sim: CR = Node_Quality(sim, dict1) Outfall = Nodes(sim)["Outfall"] for step in sim: CR.ConstantRemoval() c = Outfall.pollut_quality conc.append(c['P1']) flow.append(sim._model.getNodeResult("Outfall", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) with Simulation("./inps/tank_variableinflow_constantremoval.inp") as sim: Outfall = Nodes(sim)["Outfall"] for step in sim: c = Outfall.pollut_quality conc1.append(c['P1']) flow1.append(sim._model.getNodeResult("Outfall", 0)) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_outfalls_8_mgd(): ''' pytest pyswmm/tests/test_nodes.py -k `test_outfalls_8_mgd` ''' sim = Simulation(MODEL_STORAGE_PUMP_MGD) print("\n\n\nOUTFALL\n") outfall = Nodes(sim)["J3"] storage = Nodes(sim)["SU1"] junction = Nodes(sim)["J2"] for ind, step in enumerate(sim): pass stats = outfall.outfall_statistics outfall_cuinflow = outfall.cumulative_inflow sim.close() assert (stats['total_periods'] == approx(208796, rel=UT_PRECISION)) assert (stats['pollutant_loading']['test'] == approx(1305.25, rel=UT_PRECISION)) assert (stats['pollutant_loading']['test'] == approx(1305.75, rel=UT_PRECISION)) assert (stats['average_flowrate'] == approx(4.3, rel=UT_PRECISION)) assert (stats['average_flowrate'] == approx(4.32, rel=UT_PRECISION)) assert (stats['peak_flowrate'] == approx(4.33, rel=UT_PRECISION)) assert (stats['peak_flowrate'] == approx(4.34, rel=UT_PRECISION)) assert (outfall_cuinflow == approx(1395293, rel=UT_PRECISION)) assert (outfall_cuinflow == approx(1395299, rel=UT_PRECISION) and outfall_cuinflow == approx(1395350, rel=UT_PRECISION))
def test_GravitySettling_conc(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'GravitySettling', 'parameters': { 'k': 0.01, 'C_s': 10.0 } } } conc = [] con = [] with Simulation("./tests/inps/tank_variableinflow_notreatment.inp") as sim: GS = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] for step in sim: GS.updateWQState() c = Tank.pollut_quality conc.append(c['P1']) with Simulation( "./tests/inps/tank_variableinflow_gravsettling.inp") as sim: Tank = Nodes(sim)["Tank"] for step in sim: co = Tank.pollut_quality con.append(co['P1']) error = mse(con, conc[1:]) print(error) assert error <= 0.03
def test_nodes_5(): sim = Simulation(MODEL_WEIR_SETTING_PATH) print("\n\n\nNODES\n") J5 = Nodes(sim)["J5"] for ind, step in enumerate(sim): if ind == 7: J5.generated_inflow(544.0) if ind > 8: assert (J5.lateral_inflow >= 543.9) print(J5.lateral_inflow) sim.close()
def test_nodes_5(): sim = Simulation(MODEL_WEIR_SETTING_PATH) print("\n\n\nNODES\n") J5 = Nodes(sim)["J5"] for ind, step in enumerate(sim): if ind == 7: J5.generated_inflow(544.0) if ind > 8: assert (J5.lateral_inflow == approx(543.9, rel=UT_PRECISION)) if ind % 1000 == 0: print(J5.lateral_inflow) sim.close()
def __init__(self,depth=5): # initialize simulation self.sim = Simulation(swmm_inp) # read input file self.control_time_step = 900 # control time step in seconds self.sim.step_advance(self.control_time_step) # set control time step node_object = Nodes(self.sim) # init node object self.St1 = node_object["St1"] self.St2 = node_object["St2"] self.J3 = node_object["J3"] self.depth = depth self.St1.full_depth = self.depth self.St2.full_depth = self.depth link_object = Links(self.sim) # init link object self.R1 = link_object["R1"] self.R2 = link_object["R2"] self.sim.start() if self.sim.current_time == self.sim.start_time: self.R1.target_setting = 0.5 self.R2.target_setting = 0.5 sim_len = self.sim.end_time - self.sim.start_time self.T = int(sim_len.total_seconds()/self.control_time_step) self.t = 1 #print('T=', self.T) self.state = np.asarray([self.St1.depth, self.St2.depth, self.J3.depth, self.St1.flooding, self.St2.flooding, self.J3.flooding]) self.action_space = spaces.Box(low=np.array([0,0]),high=np.array([1,1]), dtype=np.float32) self.observation_space = spaces.Box(low=0, high = 1000, shape=(len(self.state),),dtype=np.float32)
def test_CSTR_load(): dict1 = { 'Tank': { 'pollutant': 0, 'method': 'CSTR', 'parameters': { 'k': -0.2, 'n': 1.0, 'c0': 10.0 } } } conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./tests/inps/tank_constantinflow_notreatment.inp") as sim: CS = waterQuality(sim, dict1) Tank = Nodes(sim)["Tank"] Valve = Links(sim)["Valve"] for index, step in enumerate(sim): CS.updateWQState_CSTR(index) c = Tank.pollut_quality conc.append(c['P1']) c1 = Valve.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getNodeResult("Tank", 0)) flow1.append(sim._model.getLinkResult("Valve", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_CoRemoval_load(): dict1 = {'Culvert': {0: [0.75, 0.15]}} dict2 = {'Culvert': {1: 0.15}} conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./inps/LinkTest_variableinflow2.inp") as sim: CR = Link_Quality(sim, dict1) culvert = Links(sim)["Culvert"] outlet = Nodes(sim)["Outlet"] for step in sim: CR.CoRemoval() c = culvert.pollut_quality conc.append(c['P1']) c1 = outlet.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getLinkResult("Culvert", 0)) flow1.append(sim._model.getNodeResult("Outlet", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def Par_objectivefunctions(trialfile, root=FileSettings.settingsdict['root']): """ This function defines the PySWMM operations that occur for each "trialfile". "Hydrograph" list of floats is defined by calling the SWMM module and assigning the output at each time step to the list. Then each objective function is called and the values are put into a list "objFunc" This function is called in parallel by SWMMCALPY, each "trialfile" can be evaluated independently. :param trialfile: :param root: necessary to tell PySWMM where to read the total inflow output from :return objFunc: list of floats that gets mapped by the multiprocessing.Pool() """ global hydrograph, simulation_timestep, sim_time hydrograph = [] sim_time = [] with Simulation(trialfile) as sim: node_object = Nodes(sim) root_location = node_object[root] simulation_timestep = time_difference.total_seconds() sim.step_advance(simulation_timestep) for step in sim: sim_time.append(sim.current_time) hydrograph.append(root_location.total_inflow) objFunc = [ normalizedpeakerror(), normalizedvolumeerror(), nashsutcliffe(), NED(trialfile) ] # P_prime.append(objFunc) return objFunc
def reset(self): self.sim.close() self.sim = Simulation(swmm_inp) self.sim.step_advance(self.control_time_step) # set control time step node_object = Nodes(self.sim) # init node object self.St1 = node_object["St1"] self.St2 = node_object["St2"] self.J1 = node_object["J1"] link_object = Links(self.sim) # init link object self.R1 = link_object["R1"] self.R2 = link_object["R2"] self.St1.full_depth = self.depth self.St2.full_depth = self.depth self.sim.start() self.t = 1 if self.sim.current_time == self.sim.start_time: self.R1.target_setting = 0.5 self.R2.target_setting = 0.5 self.state = np.asarray([ self.St1.depth, self.St2.depth, self.J1.depth, self.St1.flooding, self.St2.flooding, self.J1.flooding ]) return self.state
def test_CSTR_load(): dict1 = {'Tank': {0: [-0.2, 1.0, 0.0]}} conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./inps/tank_constantinflow_notreatment.inp") as sim: CS = Node_Quality(sim, dict1) Tank = Nodes(sim)["Tank"] Valve = Links(sim)["Valve"] for index, step in enumerate(sim): CS.CSTR_solver(index) c = Tank.pollut_quality conc.append(c['P1']) c1 = Valve.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getNodeResult("Tank", 0)) flow1.append(sim._model.getLinkResult("Valve", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def updateWQState(self): """ Runs the selected water quality method (except CSTR) and updates the pollutant concentration during a SWMM simulation. """ nodes = Nodes(self.sim) links = Links(self.sim) for asset_ID, asset_info in self.config.items(): for node in nodes: attribute = self.config[asset_ID]['method'] if node.nodeid == asset_ID: self.flag = 0 self.method[attribute](asset_ID, \ self.config[asset_ID]['pollutant'], \ self.config[asset_ID]['parameters'], self.flag) for link in links: attribute = self.config[asset_ID]['method'] if link.linkid == asset_ID: self.flag = 1 self.method[attribute](asset_ID, \ self.config[asset_ID]['pollutant'], \ self.config[asset_ID]['parameters'], self.flag)
def objectivefunctions(filelist, observationdatafile, distancefilename, root): """ This function does the same thing as Par_objectivefunctions() with the exception being that it accepts a list of strings as an argument and not a string. This means it is not parallelizable, but still used in the cross-over operation to determine which of the guesses is better. :param filelist: :param observationdatafile: :param distancefilename: :param root: :return: """ global hydrograph, simulation_timestep, sim_time, P_prime P_prime = [] for trialfile in filelist: hydrograph = [] sim_time = [] with Simulation(trialfile) as sim: node_object = Nodes(sim) root_location = node_object[root] simulation_timestep = time_difference.total_seconds() sim.step_advance(simulation_timestep) for step in sim: sim_time.append(sim.current_time) hydrograph.append(root_location.total_inflow) objFunc = [ normalizedpeakerror(), normalizedvolumeerror(), nashsutcliffe(), NED(trialfile) ] P_prime.append(objFunc) return objFunc
def __init__(self, inp_file, fcst_file, depth=4.61): # initialize simulation self.input_file = inp_file self.sim = Simulation(self.input_file) # read input file self.fcst_file = fcst_file self.fcst = np.genfromtxt(self.fcst_file, delimiter=',') # read forecast file as array self.control_time_step = 900 # control time step in seconds self.sim.step_advance(self.control_time_step) # set control time step node_object = Nodes(self.sim) # init node object self.St1 = node_object["St1"] self.St2 = node_object["St2"] self.J1 = node_object["J1"] self.depth = depth self.St1.full_depth = self.depth self.St2.full_depth = self.depth link_object = Links(self.sim) # init link object self.R1 = link_object["R1"] self.R2 = link_object["R2"] self.sim.start() if self.sim.current_time == self.sim.start_time: self.R1.target_setting = 0.5 self.R2.target_setting = 0.5 sim_len = self.sim.end_time - self.sim.start_time self.T = int(sim_len.total_seconds()/self.control_time_step) self.t = 1 self.state = np.concatenate([np.asarray([self.St1.depth, self.St2.depth, self.J1.depth, self.St1.flooding, self.St2.flooding, self.J1.flooding, self.R1.current_setting, self.R2.current_setting]), self.fcst[self.t]]) self.action_space = spaces.Box(low=np.array([0, 0]), high=np.array([1, 1]), dtype=np.float32) self.observation_space = spaces.Box(low=0, high=1000, shape=(len(self.state),), dtype=np.float32)
def reset(self): """Reset state. """ # state = np.random.random(size=(1,)) self.sim.close() self.sim = Simulation(self.modelfile) self.sim.step_advance(self.controlstep) node_object = Nodes(self.sim) self.P1 = node_object["P1"] self.P2 = node_object["P2"] self.P1.depth = self.depth self.P2.depth = self.depth link_object = Links(self.sim) self.L8 = link_object["8"] self.L8.flow = self.flow self.sim.start() if self.sim.current_time == self.sim.start_time: self.L8.target_setting = 0 self.state = np.concatenate([ np.asarray([ self.P1.depth, self.P2.depth, self.L8.flow, self.P1.flooding, self.P2.flooding, self.O1.current_setting, self.O2.current_setting ]) ]) return self.state
def test_Erosion_load(): dict1 = {'Channel': {0: [10.0, 0.001, 2.68, 0.7]}} conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./inps/LinkTest_variableinflow.inp") as sim: ER = Link_Quality(sim, dict1) channel = Links(sim)["Channel"] tailwater = Nodes(sim)["TailWater"] for step in sim: ER.Erosion() c = channel.pollut_quality conc.append(c['P1']) c1 = tailwater.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getLinkResult("Channel", 0)) flow1.append(sim._model.getNodeResult("Tailwater", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_CoRemoval_load(): dict1 = {'Culvert': {'pollutant': 1, 'method': 'ConstantRemoval', 'parameters': {'R': 0.15}},\ 'Culvert': {'pollutant': 0, 'method': 'CoRemoval', 'parameters': {'R1': 0.75, 'R2': 0.15}}, } conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./tests/inps/LinkTest_variableinflow2.inp") as sim: CR = waterQuality(sim, dict1) culvert = Links(sim)["Culvert"] outlet = Nodes(sim)["Outlet"] for step in sim: CR.updateWQState() c = culvert.pollut_quality conc.append(c['P1']) c1 = outlet.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getLinkResult("Culvert", 0)) flow1.append(sim._model.getNodeResult("Outlet", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def test_GravitySettling_load(): dict1 = { 'Culvert': { 'pollutant': 0, 'method': 'GravitySettling', 'parameters': { 'k': 0.01, 'C_s': 10.0 } } } conc = [] conc1 = [] flow = [] flow1 = [] with Simulation("./tests/inps/LinkTest_variableinflow.inp") as sim: GS = waterQuality(sim, dict1) culvert = Links(sim)["Culvert"] outlet = Nodes(sim)["Outlet"] for step in sim: GS.updateWQState() c = culvert.pollut_quality conc.append(c['P1']) c1 = outlet.pollut_quality conc1.append(c1['P1']) flow.append(sim._model.getLinkResult("Culvert", 0)) flow1.append(sim._model.getNodeResult("Outlet", 0)) load = [a * b for a, b in zip(conc, flow)] cum_load = np.cumsum(load) load1 = [a * b for a, b in zip(conc1, flow1)] cum_load1 = np.cumsum(load1) error = (cum_load1[-1] / cum_load[-1]) / cum_load1[-1] print(error) assert error <= 0.03
def reset(self): self.sim.close() self.sim = Simulation(self.input_file) # self.fcst = np.genfromtxt(self.fcst_file, delimiter=',') # read forecast file as array self.fcst = pd.read_csv(self.fcst_file, index_col="datetime", infer_datetime_format=True, parse_dates=True) self.sim.step_advance(self.control_time_step) # set control time step node_object = Nodes(self.sim) # init node object self.St1 = node_object["st1"] self.St2 = node_object["st2"] self.St3 = node_object["F134101"] link_object = Links(self.sim) # init link object self.R1 = link_object["R1"] self.R2 = link_object["R2"] self.R3 = link_object["R3"] self.sim.start() self.t = 1 if self.sim.current_time == self.sim.start_time: self.R1.target_setting = 0.5 self.R2.target_setting = 1 self.R3.target_setting = 0.5 # self.state = np.concatenate([np.asarray([self.St1.depth, self.St3.depth, self.St1.flooding, self.St3.flooding, # self.R1.current_setting, self.R3.current_setting]), self.fcst[self.t]]) current_fcst = self.fcst.iloc[self.fcst.index.get_loc(self.sim.current_time, method='nearest')] rain_fcst = sum(current_fcst[:int(len(current_fcst) / 2)]) # total rainfall in forecast tide_fcst = np.mean(current_fcst[int(len(current_fcst) / 2):]) # mean tide in forecast # self.state = np.asarray([self.St1.depth, self.St3.depth, self.St1.flooding, self.St3.flooding, # self.R1.current_setting, self.R3.current_setting, # rain_fcst, tide_fcst]) self.state = np.asarray([self.St1.depth, self.St3.depth, self.St1.flooding, self.St3.flooding, self.R1.current_setting, self.R3.current_setting, self.R1.pollut_quality['TSS'], self.R3.pollut_quality['TSS'], rain_fcst, tide_fcst]) return self.state
def test_nodes_2(): with Simulation(MODEL_WEIR_SETTING_PATH) as sim: print("\n\n\nNODES\n") for node in Nodes(sim): assert ('J' in node.nodeid) node.invert_elevation = 10 assert (node.invert_elevation == 10)
def reset(self): self.sim.close() self.sim = Simulation(self.input_file) self.fcst = np.genfromtxt(self.fcst_file, delimiter=',') # read forecast file as array self.sim.step_advance(self.control_time_step) # set control time step node_object = Nodes(self.sim) # init node object self.St1 = node_object["St1"] self.St2 = node_object["St2"] self.J1 = node_object["J1"] link_object = Links(self.sim) # init link object self.R1 = link_object["R1"] self.R2 = link_object["R2"] self.St1.full_depth = self.depth self.St2.full_depth = self.depth self.sim.start() self.t = 1 if self.sim.current_time == self.sim.start_time: self.R1.target_setting = 0.5 self.R2.target_setting = 0.5 self.state = np.concatenate([ np.asarray([ self.St1.depth, self.St2.depth, self.J1.depth, self.St1.flooding, self.St2.flooding, self.J1.flooding, self.R1.current_setting, self.R2.current_setting ]), self.fcst[self.t] ]) return self.state
def test_nodes_6(): ''' pytest pyswmm/tests/test_nodes.py -k `test_nodes_6` ''' sim = Simulation(MODEL_WEIR_SETTING_PATH) print("\n\n\nNODES\n") J5 = Nodes(sim)["J5"] for ind, step in enumerate(sim): pass assert(J5.statistics['peak_total_inflow'] >= 0.478) assert(J5.statistics['average_depth'] >= 0.00018) assert(J5.statistics['surcharge_duration'] >= 0.0) assert(J5.statistics['max_ponded_volume'] >= 0.0) assert(J5.statistics['courant_crit_duration'] >= 0.0) assert(J5.statistics['peak_lateral_inflowrate'] >= 0.0) assert(J5.statistics['flooding_duration'] >= 0.0) assert(J5.statistics['peak_flooding_rate'] >= 0.0) assert(J5.statistics['lateral_infow_vol'] >= 0.0) assert(J5.statistics['max_flooding_date'] >= 42309) assert(J5.statistics['max_depth_date'] >= 42309) assert(J5.statistics['max_inflow_date'] >= 42309) assert(J5.statistics['max_depth'] >= 0.0292) assert(J5.statistics['flooding_volume'] >= 0.0) assert(J5.statistics['max_report_depth'] >= 0.0286) sim.close()
def test_nodes_2(): ''' pytest pyswmm/tests/test_nodes.py -k `test_nodes_2` ''' with Simulation(MODEL_WEIR_SETTING_PATH) as sim: print("\n\n\nNODES\n") for node in Nodes(sim): assert ('J' in node.nodeid) node.invert_elevation = 10 assert (node.invert_elevation == 10)
def fit_func(nest, My_Current_step, CONTROL_RULES, Current_States): sim = Simulation(r"./test.inp") Linkname = ["C1", "C2", "O1", "O2"] Nodename = ["St1", "St2", "J1", "J2"] links = Links(sim) nodes = Nodes(sim) t = 0 for link in Linkname: a2 = links[link] a2.initial_flow = Current_States[t][0] t = t + 1 for node in Nodename: a1 = nodes[node] a1.initial_depth = Current_States[t][0] t = t + 1 flood1 = np.array([]) flood2 = np.array([]) flood3 = np.array([]) flood4 = np.array([]) o1 = Links(sim)["O1"] o2 = Links(sim)["O2"] j1 = Nodes(sim)["J1"] j2 = Nodes(sim)["J2"] j3 = Nodes(sim)["St1"] j4 = Nodes(sim)["St2"] sim.start_time = my_Start_time + datetime.timedelta( minutes=My_timestep * (My_Current_step)) sim.end_time = my_Start_time + datetime.timedelta( minutes=My_timestep * (My_Current_step + HorizonLength + 1)) sim.step_advance(My_timestep * 60) CONTROL_RULES[My_Current_step:My_Current_step + HorizonLength] = nest.reshape([4, -1]) i = 0 for step in sim: o1.target_setting = CONTROL_RULES[i + My_Current_step][0] o2.target_setting = CONTROL_RULES[i + My_Current_step][1] i = i + 1 flood1 = np.append(flood1, [j1.flooding]) flood2 = np.append(flood2, [j2.flooding]) flood3 = np.append(flood3, [j3.flooding]) flood4 = np.append(flood4, [j4.flooding]) sim.close() return mytrape(flood1, 60 * My_timestep) + mytrape( flood2, 60 * My_timestep) + mytrape( flood3, 60 * My_timestep) + mytrape(flood4, 60 * My_timestep)