def test_pollutants_1(): with Simulation(MODEL_POLLUTANTS_PATH) as sim: S1 = Subcatchments(sim)["S1"] S2 = Subcatchments(sim)["S2"] S3 = Subcatchments(sim)["S3"] for step in sim: print(S1.statistics) print(S2.statistics) print(S3.statistics) assert S1.statistics['pollutant_buildup']['test-pollutant'] == 25.000 assert S2.statistics['pollutant_buildup']['test-pollutant'] == 25.000 assert S3.statistics['pollutant_buildup']['test-pollutant'] == 25.000
def test_subcatchments_2(): with Simulation(MODEL_WEIR_SETTING_PATH) as sim: for subcatchment in Subcatchments(sim): print(subcatchment) print(subcatchment.subcatchmentid) print(subcatchment.curb_length) subcatchment.curb_length = 10 print(subcatchment.curb_length)
def create_network(inputfilename=FileSettings.settingsdict['inputfilename']): """ This function creates the system-wide NetworkX DiGraph that contains every node and connection. :param inputfilename: :return: """ global sim linklist = [] with Simulation(inputfilename) as sim: for sub in Subcatchments(sim): network.add_node(sub.subcatchmentid) for nodes in Nodes(sim): network.add_node(nodes.nodeid) for link in Links(sim): linklist.append(link.connections) network.add_edges_from(linklist) for sub in Subcatchments(sim): network.add_edge(sub.subcatchmentid, sub.connection[1]) return
def swmm_run(k=k, pctImperv_c=pctImperv_c, pctImperv_l=pctImperv_l, slope=slope, nPerv=nPerv, nImperv=nImperv, stoPerv=stoPerv, rain=rain, stoImperv=stoImperv, infi=infi,imd=imd): #def swmm_run(k=k, slope=slope): #sublist = [] lawn = ['s31', 's32', 's33', 's34', 's41', 's42', 's43', 's59', 's60', 's61', 's62', 's63', 's64'] simflow = [] i = 0 flow_temp = 0 with Simulation('tianheful_backup625.inp') as sim: #with Simulation('tianheful_backup1.inp') as sim: subcatchments = Subcatchments(sim) #for sub in subcatchments: # sublist.append(sub.subcatchmentid) for sub in subcatchments: sub.width = k / 100 * np.sqrt(sub.area * 10000) sub.percent_impervious = pctImperv_c / 100 sub.slope = slope / 100 sub.perv_n = nPerv / 100 sub.imperv_n = nImperv / 100 sub.perv_sto = stoPerv / 100 sub.imperv_sto = stoImperv / 100 sub.Ks = infi / 100 sub.IMD = imd / 100 sub.rainMulti = rain / 100 #for j in range(len(sublist)): # subcatchments[sublist[j]].width = k / 100 * np.sqrt(subcatchments[sublist[j]].area * 10000) # subcatchments[sublist[j]].slope = slope[j] / 100 # subcatchments[sublist[j]].Ks = infi / 100 # subcatchments[sublist[j]].IMD = imd / 100 for sublawn in lawn: subcatchments[sublawn].percent_impervious = pctImperv_l / 100 #print(sublawn) #print(subcatchments[sublawn].percent_impervious) links = Links(sim) #for link in links: # link.con_roughness = roughness / 100 l29 = links['l29'] #print(l29.con_roughness) for step in sim: flow_temp += l29.flow # integrate 30s data to 1min i += 1 if i > 1: #print(i) simflow.append(flow_temp / 2) i = 0 flow_temp = 0 #simflow.append(l29.flow) #print(sim.current_time) #print(sim.flow_routing_error) return simflow
def test_subcatchments_3(): with Simulation(MODEL_SUBCATCH_STATS_PATH) as sim: S1 = Subcatchments(sim)['S1'] S2 = Subcatchments(sim)['S2'] for ind, step in enumerate(sim): pass print(S1.statistics) print(S2.statistics) assert (S1.statistics['evaporation'] == approx(0.00, rel=UT_PRECISION) ) # ft3 assert (S1.statistics['runoff'] == approx(1244.58, rel=UT_PRECISION)) # ft3 assert (S1.statistics['runon'] == approx(0.00, rel=UT_PRECISION)) # ft3 assert (S1.statistics['peak_runoff_rate'] == approx(0.12, rel=UT_PRECISION) ) # cfs assert (S1.statistics['infiltration'] == approx(0.00, rel=UT_PRECISION) ) # ft3 assert (S1.statistics['precipitation'] == approx(0.12, rel=UT_PRECISION) ) # in assert (S2.statistics['evaporation'] == approx(0.00, rel=UT_PRECISION) ) # ft3 assert (S2.statistics['runoff'] == approx(0.00, rel=UT_PRECISION)) # ft3 assert (S2.statistics['runon'] == approx(1207.74, rel=UT_PRECISION)) # ft3 assert (S2.statistics['peak_runoff_rate'] == approx(0.00, rel=UT_PRECISION) ) #cfs assert (S2.statistics['infiltration'] == approx(3476.57, rel=UT_PRECISION) ) # ft3 assert (S2.statistics['precipitation'] == approx(0.12, rel=UT_PRECISION) ) # in
def test_pollutants_1(): with Simulation(MODEL_POLLUTANTS_PATH) as sim: S1 = Subcatchments(sim)["S1"] S2 = Subcatchments(sim)["S2"] S3 = Subcatchments(sim)["S3"] for step in sim: print(S1.buildup) print(S2.buildup) print(S3.buildup) print(S1.concPonded) print(S2.concPonded) print(S3.concPonded) assert S1.buildup['test-pollutant'] == 25.000 assert S2.buildup['test-pollutant'] == 25.000 assert S3.buildup['test-pollutant'] == 25.000 assert 9.99999 <= S1.concPonded['test-pollutant'] <= 10.00001 assert 9.99999 <= S2.concPonded['test-pollutant'] <= 10.00001 assert 9.99999 <= S3.concPonded['test-pollutant'] <= 10.00001
def test_subcatchments_1(): with Simulation(MODEL_WEIR_SETTING_PATH) as sim: print("\n\n\nSubcatchments\n") S3 = Subcatchments(sim)["S3"] print(S3.area) print(S3.width) print(S3.connection) print(S3.percent_impervious) print(S3.slope) sim.step_advance(300) for ind, step in enumerate(sim): print(S3.runoff)
def subnetwork_subcatchments( inputfilename=FileSettings.settingsdict['inputfilename']): """ This function searches the clipped DiGraph from subnetwork_delineation() and compares it to the "subcatchments" that are read by PySWMM. This removes "junctions" and only consideres "subcatchment" names that are upstream of the "root" node. This function is the only one that is actually called, as it itself calls the other create_network() subnetwork_delineateion() functions. :param inputfilename: :return list_of_subcatchments: List of strings containing the names of the subcatchments that fall into the scope of the calibration problem. This variable is used by many other functions to identify the parts of the input file to write to. """ global list_of_subcatchments list_of_subcatchments = [] create_network(inputfilename) subnetwork_delineation() with Simulation(inputfilename) as sim: for subcatchment in Subcatchments(sim): subcatchmentname = subcatchment.subcatchmentid for subnode in subnetwork: if subnode == subcatchmentname: list_of_subcatchments.append(subcatchmentname) return list_of_subcatchments
def iterate_subcatchments(input_file, name_list): subcatchment_objects = Subcatchments( Simulation(input_file) ) #retrieves model data and stores in the subcatchment_objects list(idk if it is even a list) all_subcatchments = [ ] # initialize an empty list to contain all subcatchments delta_subcatchments = [ ] # initialize an empty list to contain the new subcatchments area = 0 #initialize an empty function variable to add and store total areas for i in subcatchment_objects: area += i.area if i.subcatchmentid not in name_list: #if subcatchment name is not in the given name list, then store it in the empty list delta_subcatchments.append( highlight_Subcatchment(i.subcatchmentid, i.area, i.connection[1], i.percent_impervious, i.slope, i.width) ) #only the first arguemtn of i.connection is relevant for the report else: all_subcatchments.append( highlight_Subcatchment(i.subcatchmentid, i.area, i.connection[1], i.percent_impervious, i.slope, i.width)) all_subcatchments.sort(key=attrgetter('name')) return all_subcatchments, delta_subcatchments, area
def test_nodes_3(): with Simulation(MODEL_FULL_FEATURES_PATH) as sim: S2 = Subcatchments(sim)["S2"] # Subcatchment for step in sim: print(S2.statistics)
def test_pollutants_1(): with Simulation(MODEL_POLLUTANTS_PATH) as sim: S1 = Subcatchments(sim)["S1"] S2 = Subcatchments(sim)["S2"] S3 = Subcatchments(sim)["S3"] C1 = Links(sim)["C1"] C2 = Links(sim)["C2"] C3 = Links(sim)["C3"] C4 = Links(sim)["C4"] J1 = Nodes(sim)["J1"] J2 = Nodes(sim)["J2"] J3 = Nodes(sim)["J3"] J4 = Nodes(sim)["J4"] for step in sim: pass # print(S1.buildup) # print(S2.buildup) # print(S3.buildup) # print(S1.conc_ponded) # print(S2.conc_ponded) # print(S3.conc_ponded) # print(S1.pollut_quality) # print(S2.pollut_quality) # print(S3.pollut_quality) # print(S1.runoff_total_loading) # print(S2.runoff_total_loading) # print(S3.runoff_total_loading) # print(C1.pollut_quality) # print(C2.pollut_quality) # print(C3.pollut_quality) # print(C4.pollut_quality) # print(C1.total_loading) # print(C2.total_loading) # print(C3.total_loading) # print(C4.total_loading) # print(J1.pollut_quality) # print(J2.pollut_quality) # print(J3.pollut_quality) # print(J4.pollut_quality) assert S1.buildup['test-pollutant'] == 25.000 assert S2.buildup['test-pollutant'] == 25.000 assert S3.buildup['test-pollutant'] == 25.000 assert 9.99999 <= S1.conc_ponded['test-pollutant'] <= 10.00001 assert 9.99999 <= S2.conc_ponded['test-pollutant'] <= 10.00001 assert 9.99999 <= S3.conc_ponded['test-pollutant'] <= 10.00001 assert S1.pollut_quality['test-pollutant'] == 0.0 assert S2.pollut_quality['test-pollutant'] == 0.0 assert S3.pollut_quality['test-pollutant'] == 0.0 assert 0.0016769 <= S1.runoff_total_loading[ 'test-pollutant'] <= 0.0016770 assert 0.0012341 <= S2.runoff_total_loading[ 'test-pollutant'] <= 0.0012342 assert 0.00077987 <= S3.runoff_total_loading[ 'test-pollutant'] <= 0.00077988 assert C1.pollut_quality['test-pollutant'] == 0.0 assert C2.pollut_quality['test-pollutant'] == 0.0 assert C3.pollut_quality['test-pollutant'] == 0.0 assert C4.pollut_quality['test-pollutant'] == 0.0 assert 26.87 <= C1.total_loading['test-pollutant'] <= 26.88 assert 46.64 <= C2.total_loading['test-pollutant'] <= 46.66 assert 12.48 <= C3.total_loading['test-pollutant'] <= 12.50 assert 59.12 <= C4.total_loading['test-pollutant'] <= 59.15 assert J1.pollut_quality['test-pollutant'] == 0.0 assert J2.pollut_quality['test-pollutant'] == 0.0 assert J3.pollut_quality['test-pollutant'] == 0.0 assert J4.pollut_quality['test-pollutant'] == 0.0
with Simulation(swmm_inp) as sim: # set up simulation sim.step_advance(control_time_step) node_object = Nodes(sim) # init node object St1 = node_object["St1"] St2 = node_object["St2"] J1 = node_object["J1"] St1.full_depth = 4.61 St2.full_depth = 4.61 link_object = Links(sim) # init link object R1 = link_object["R1"] R2 = link_object["R2"] subcatchment_object = Subcatchments(sim) S1 = subcatchment_object["S1"] S2 = subcatchment_object["S2"] for step in sim: # loop through all steps in the simulation if sim.current_time == sim.start_time: R1.target_setting = 1 R2.target_setting = 1 St1_depth.append(St1.depth) St2_depth.append(St2.depth) J1_depth.append(J1.depth) St1_flooding.append(St1.flooding) St2_flooding.append(St2.flooding) J1_flooding.append(J1.flooding) St1_full.append(St1.full_depth)
def SEI(self, catchment, tree_pit_storage_depth,inital_loss): filename = str(uuid.uuid4()) fo = open("/tmp/" + filename + ".inp", 'w') intervall = (datetime.datetime(2000, 1, 1) + datetime.timedelta(seconds=self.time_delta)).strftime('%H:%M:%S') dt = datetime.timedelta(seconds=self.time_delta) swmm_rain_filename = self.swmm_rain_file if self.rain_vector_from_city != "": self.city.reset_reading() for c in self.city: import ogr self.write_rain_file(filename, dm_get_double_list(c, self.rain_vector_from_city), 60 * 6) swmm_rain_filename = "/tmp/" + filename + ".dat" self.init_swmm_model(fo, rainfile=swmm_rain_filename, start='01/01/2000', stop='12/30/2000', intervall=intervall, sub_satchment=catchment, tree_pit_storage_depth = tree_pit_storage_depth, inital_loss=inital_loss) fo.close() results = {} with Simulation("/tmp/" + filename + ".inp") as sim: s = 360. sim.step_advance(s) # catchment catchment_data = { "rainfall": s / (60 * 60), "runoff": s } catchments, catchment_r = result_dict(Subcatchments(sim), catchment_data, "subcatchmentid") results['catchment'] = [catchments, catchment_r, catchment_data, "subcatchmentid"] # nodes node_data = { "total_inflow": s } catchments, catchment_r = result_dict(Nodes(sim), node_data, "nodeid") results['nodes'] = [catchments, catchment_r, node_data, "nodeid"] catch = Subcatchments(sim)["1"] peak_flows = [] max_runoff = 0 for idx, step in enumerate(sim): if (idx+1) * s % (60*60*40) == 0: if idx > 0: peak_flows.append(max_runoff) max_runoff = 0 if catch.runoff > max_runoff: max_runoff = catch.runoff for k in list(results.keys()): r = results[k] for c in r[0]: id = getattr(c, r[3]) for c_r in list(r[1][id].keys()): r[1][id][c_r] += getattr(c, c_r) * r[2][c_r] # sim.report() peak_flows.append(max_runoff) # sim.report() results["peak_flows"] = peak_flows tree_pit_inflow = self.getTreePitInflow("/tmp/" + filename + ".rpt") results["tree_pit_inflow"] = tree_pit_inflow os.remove("/tmp/" + filename + ".inp") os.remove("/tmp/" + filename + ".rpt") os.remove("/tmp/" + filename + ".out") if self.rain_vector_from_city != "": os.remove("/tmp/" + filename + ".dat") return results
# -*- coding: utf-8 -*- from pyswmm import Simulation, Subcatchments with Simulation('swmm_example.inp') as sim: S1 = Subcatchments(sim)["S1"] for step in sim: print(sim.current_time) print(S1.runoff)
) for i in pre_list: func_report.write("{}, {}, {}, {}, {}, {}\n".format( i.name, i.area, i.connection, i.percent_impervious * 100, i.slope, i.width)) func_report.write('\nPOST DEVELOPED\n') for i in post_list: func_report.write("{}, {}, {}, {}, {}, {}\n".format( i.name, i.area, i.connection, i.percent_impervious * 100, i.slope, i.width)) if __name__ == "__main__": # runs through a simulation to develop the pre-developed name list for the iterate_subcatchments function pre_subcatchments = Subcatchments(Simulation('pre_100.inp')) pre_subcatnames = [i.subcatchmentid for i in pre_subcatchments] # runs through a simulation to develop the post-developed name list for the iterate_subcatchments function post_subcatchments = Subcatchments(Simulation('post_100.inp')) post_subcatnames = [i.subcatchmentid for i in post_subcatchments] #creates the object lists containing the highlighted subcatchments and the total areas of the pre & post model. #currently assumes two files are in the cd named 'pre_100.inp' and 'post_100.inp' all_pre_Subcatchments, all_new_Subcatchments, pre_totarea = iterate_subcatchments( 'pre_100.inp', post_subcatnames) all_post_Subcatchments, missing_old_Subcatchments, post_totarea = iterate_subcatchments( 'post_100.inp', pre_subcatnames) modified_pre_subcatchments, modified_post_subcatchments = compare_subcatchments( all_pre_Subcatchments, all_post_Subcatchments)
(11 / ts) * sph, (12 / ts) * sph, ] # times to record buildup i = 1 rec_step = 1 if not os.path.exists("buildup"): os.mkdir("buildup") with Simulation('swmm_example.inp') as sim: for step in sim: if rec_step in times: with open('buildup/pollut_buildup' + str("%i" % i) + '.csv', 'w', newline='') as csvfile: load = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) load.writerow(['time:', sim.current_time]) load.writerow(['Subcatchment', 'Loading']) for subcatchment in Subcatchments(sim): load.writerow([ subcatchment.subcatchmentid, subcatchment.buildup['test-pollutant'] ]) i += 1 rec_step += 1