Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import wntr
import pandas as pd
import time
import cProfile, pstats, StringIO
# Create a water network model
#inp_file = 'networks/Net6_mod_scipy.inp'
inp_file = 'networks/Net3_timing.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate using Epanet
print "-----------------------------------"
print "EPANET SIMULATOR: "
pr = cProfile.Profile()
pr.enable()
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
pr.disable()
print "-----------------------------------"
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(20)
print s.getvalue()
s = StringIO.StringIO()
sortby = 'tottime'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(20)
print s.getvalue()
import wntr
import pandas as pd
import time
import cProfile, pstats, StringIO
# Create a water network model
#inp_file = 'networks/Net6_mod_scipy.inp'
inp_file = 'networks/Net3_easy.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate using Epanet
print "-----------------------------------"
print "SCIPY SIMULATOR V2: "
pr = cProfile.Profile()
pr.enable()
sim = wntr.sim.ScipySimulatorV2(wn)
results = sim.run_sim()
pr.disable()
print "-----------------------------------"
s = StringIO.StringIO()
sortby = 'cumulative'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(20)
print s.getvalue()
s = StringIO.StringIO()
sortby = 'tottime'
ps = pstats.Stats(pr, stream=s).sort_stats(sortby)
ps.print_stats(20)
print s.getvalue()
print "SCIPY SIMULATOR: "
t0 = time.time()
sci_sim = wntr.sim.ScipySimulator(wn)
t1 = time.time()
results = sci_sim.run_sim()
t2 = time.time()
total_scipy_time = t2 - t0
scipy_obj_creation_time = t1-t0
scipy_run_sim_time = t2-t1
print "-----------------------------------"
# Simulate using Pyomo
print "-----------------------------------"
print "PYOMO SIMULATOR: "
t0 = time.time()
pyo_sim = wntr.sim.PyomoSimulator(wn)
t1 = time.time()
results = pyo_sim.run_sim()
t2 = time.time()
total_pyomo_time = t2 - t0
pyomo_obj_creation_time = t1-t0
pyomo_run_sim_time = t2-t1
print "-----------------------------------"
print('{0:<30s}{1:<12s}{2:<12s}{3:<12s}'.format('Category','Epanet','Scipy','Pyomo'))
print('{0:<30s}{1:<12.4f}{2:<12.4f}{3:<12.4f}'.format('Total Sim Time',total_epanet_time,total_scipy_time, total_pyomo_time))
print('{0:<30s}{1:<12.4f}{2:<12.4f}{3:<12.4f}'.format('Sim obj creation time',epanet_obj_creation_time,scipy_obj_creation_time, pyomo_obj_creation_time))
print('{0:<30s}{1:<12.4f}{2:<12.4f}{3:<12.4f}'.format('run_sim time',epanet_run_sim_time,scipy_run_sim_time, pyomo_run_sim_time))
print('{0:<30s}{1:<12.4f}{2:<12.4f}{3:<12.4f}'.format('Prep time before main loop',epa_sim.prep_time_before_main_loop, sci_sim.prep_time_before_main_loop, pyo_sim.prep_time_before_main_loop))
print('{0:<30s}{1:<12.4f}{2:<12.4f}{3:<12.4f}'.format('Total solve time',sum(epa_sim.solve_step[i] for i in epa_sim.solve_step.keys()),sum(sci_sim.solve_step[i] for i in sci_sim.solve_step.keys()), sum(pyo_sim.solve_step[i] for i in pyo_sim.solve_step.keys())))
print('{0:<30s}{1:<12.4s}{2:<12.4s}{3:<12.4f}'.format('Build pyomo model time','n/a','n/a', sum(pyo_sim.build_model_time[i] for i in pyo_sim.build_model_time.keys())))
import wntr
import matplotlib.pyplot as plt
from matplotlib import animation
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate trace contaminant
wn.options.quality.mode = 'TRACE'
wn.options.quality.trace_node = '111'
# Simulate hydraulics
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
# Animate trace percent
fig = plt.figure(figsize=(12,10), facecolor='w')
ax = plt.gca()
values = results.node['quality']
initial_values = values.loc[0, :]
nodes, edges = wntr.graphics.plot_network(wn, node_attribute=initial_values,
ax=ax, node_range = [0,100], node_size=40, title='Trace at 0 hours')
def update(n):
node_values = values.loc[n*3600, :]
fig.clf()
nodes, edges = wntr.graphics.plot_network(wn, node_attribute=node_values,
ax=plt.gca(), node_range = [0,100], node_size=40, title='Trace at ' + str(n) +' hours')
self.pres_units = None
self.mass_units = None
self.quality_type = None
self.num_nodes = None
self.num_tanks = None
self.num_links = None
self.num_pumps = None
self.num_valves = None
self.report_start = None
self.report_step = None
self.duration = None
self.chemical = None
self.chem_units = None
self.inp_file = None
self.rpt_file = None
self.results = wntr.sim.NetResults()
if result_types is None:
self.items = [ member for name, member in ResultType.__members__.items() ]
else:
self.items = result_types
self.create_network = network
self.keep_energy = energy
self.keep_statistics = statistics
# Select duration of failure, uniform dist, between 12 and 24 hours
duration_of_failure = np.round(np.random.uniform(12,24,1)[0], 2)
# Add leaks to the model
for pipe_to_fail in pipes_to_fail:
pipe = wn.get_link(pipe_to_fail)
leak_diameter = pipe.diameter*0.3
leak_area=3.14159*(leak_diameter/2)**2
wn = wntr.morph.split_pipe(wn, pipe_to_fail, pipe_to_fail + '_B', pipe_to_fail+'leak_node')
leak_node = wn.get_node(pipe_to_fail+'leak_node')
leak_node.add_leak(wn, area=leak_area,
start_time=time_of_failure*3600,
end_time=(time_of_failure + duration_of_failure)*3600)
# Simulate hydraulics and store results
sim = wntr.sim.WNTRSimulator(wn, mode='PDD')
print('Pipe Breaks: ' + str(pipes_to_fail) + ', Start Time: ' + \
str(time_of_failure) + ', End Time: ' + \
str(time_of_failure+duration_of_failure))
results[i] = sim.run_sim()
# Reload the water network model
f=open('wn.pickle','rb')
wn = pickle.load(f)
f.close()
# Plot water service availability and tank water level for each realization
for i in results.keys():
# Water service availability at each junction and time
expected_demand = wntr.metrics.expected_demand(wn)
demand = results[i].node['demand'].loc[:,wn.junction_name_list]