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
# Create a water network model
#inp_file = 'networks/Net6_mod_scipy.inp'
#inp_file = 'networks/Net3_timing.inp
inp_file = 'networks/Net3_easy_copy.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate using Epanet
print "-----------------------------------"
print "EPANET SIMULATOR: "
t0 = time.time()
epa_sim = wntr.sim.EpanetSimulator(wn)
t1 = time.time()
results = epa_sim.run_sim()
t2 = time.time()
total_epanet_time = t2 - t0
epanet_obj_creation_time = t1-t0
epanet_run_sim_time = t2-t1
print "-----------------------------------"
# Simulate using Scipy
print "-----------------------------------"
print "SCIPY SIMULATOR: "
t0 = time.time()
sci_sim = wntr.sim.ScipySimulator(wn)
t1 = time.time()
results = sci_sim.run_sim()
t2 = time.time()
from __future__ import print_function
import wntr
import pandas as pd
import pickle
import matplotlib.pylab as plt
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate using EPANET
epanet_sim = wntr.sim.EpanetSimulator(wn)
epanet_sim_results = epanet_sim.run_sim()
# Simulate using WNTR
wntr_sim = wntr.sim.WNTRSimulator(wn)
wntr_sim_results = wntr_sim.run_sim()
# Compare link flowrate results
plt.figure(figsize=(6,10))
plt.subplot(2,1,1)
plt.plot(epanet_sim_results.link['flowrate'])
plt.title('EPANET, Link Flowrate')
plt.subplot(2,1,2)
plt.plot(epanet_sim_results.link['flowrate'] - wntr_sim_results.link['flowrate'])
plt.title('EPANET - WNTR, Link Flowrate')
# Reset the water network and run again
def water_security_metrics(wn):
# Define WQ scenario
wn.options.quality.mode = 'CHEMICAL'
# Source pattern already exists
# source_pattern = wntr.network.elements.Pattern.binary_pattern('SourcePattern', step_size=wn.options.time.pattern_timestep, start_time=2*3600, end_time=15*3600, duration=wn.options.time.duration)
# wn.add_pattern('SourcePattern', source_pattern)
wn.add_source('Source1', '121', 'SETPOINT', 1000, 'SourcePattern')
# Simulate hydraulics and water quality for each scenario
sim = wntr.sim.EpanetSimulator(wn)
results_CHEM = sim.run_sim()
demand = results_CHEM.node['demand'].loc[:,wn.junction_name_list]
quality = results_CHEM.node['quality'].loc[:,wn.junction_name_list]
flowrate = results_CHEM.link['flowrate'].loc[:,wn.pipe_name_list]
MC = wntr.metrics.mass_contaminant_consumed(demand, quality)
VC = wntr.metrics.volume_contaminant_consumed(demand, quality, 0.001)
EC = wntr.metrics.extent_contaminant(quality, flowrate, wn, 0.001)
wntr.graphics.plot_network(wn, node_attribute=MC.sum(axis=0), node_range = [0,1e5], node_size=40,
title='Total mass consumed')
plt.figure()
EC.plot(title='Extent of contamination')
from __future__ import print_function
import wntr
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate hydraulics
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
# Access node and link panels
print(results.node)
print(results.link)
# Access the pressure and demand at node '123' at 1 hour
print(results.node.loc[['pressure', 'demand'], 3600, '123'])
# Access the pressure for all nodes and times
print(results.node.loc['pressure', :, :])
# Plot time-series
pressure_at_node123 = results.node.loc['pressure', :, '123']
pressure_at_node123.plot()
import wntr
import plotly
import matplotlib.pylab as plt
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate hydraulics
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
# Extract simulation results to plot
pressure_at_5hr = results.node['pressure'].loc[5*3600,:]
# Create static network graphics
wntr.graphics.plot_network(wn, node_attribute='elevation', title='Elevation')
wntr.graphics.plot_network(wn, node_attribute=['123', '199'], title='Node 123 and 199')
wntr.graphics.plot_network(wn, node_attribute=pressure_at_5hr, title='Pressure at 5 hours')
## Create interactive scalable network graphics
wntr.graphics.plot_interactive_network(wn, node_attribute='elevation',
title='Elevation', filename='elevation.html', auto_open=False)
wntr.graphics.plot_interactive_network(wn, node_attribute=['123', '199'],
title='Node 123 and 199', filename='node123_199.html', auto_open=False)
wntr.graphics.plot_interactive_network(wn, node_attribute=pressure_at_5hr,
import wntr
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
plt.close('all')
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Simulate hydraulics
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
# Create list of node names
junctions = [name for name, node in wn.junctions()]
# Define pressure lower bound
P_lower = 21.09 # m (30 psi)
# Query pressure
pressure = results.node.loc['pressure', :, junctions]
mask = wntr.metrics.query(pressure, np.greater, P_lower)
pressure_regulation = mask.all(axis=0).sum() # True over all time
print "Fraction of nodes > 30 psi: " + str(pressure_regulation)
print "Average node pressure: " +str(pressure.mean().mean()) + " m"
wntr.network.draw_graph(wn, node_attribute=pressure.min(axis=0), node_size=40,
title= 'Min pressure')
import wntr
import matplotlib.pyplot as plt
# Create a water network model
inp_file = 'networks/Net3.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Define WQ scenarios
WQscenario = wntr.scenario.Waterquality('CHEM', '121', 'SETPOINT', 1000, 2*3600, 15*3600)
# Simulate hydraulics and water quality for each scenario
sim = wntr.sim.EpanetSimulator(wn)
results_CHEM = sim.run_sim(WQscenario)
MC = wntr.metrics.mass_contaminant_consumed(results_CHEM.node)
VC = wntr.metrics.volume_contaminant_consumed(results_CHEM.node, 0.001)
EC = wntr.metrics.extent_contaminant(results_CHEM.node, results_CHEM.link, wn, 0.001)
wntr.network.draw_graph(wn, node_attribute=MC.sum(axis=0), node_range = [0,400], node_size=40,
title='Total mass consumed')
plt.figure()
EC.sum(axis=1).plot(title='Extent of contamination')
def population_impacted_metrics(wn):
# Compute population per node
pop = wntr.metrics.population(wn)
total_population = pop.sum()
print("Total population: " + str(total_population))
wntr.graphics.plot_network(wn, node_attribute=pop, node_range = [0,400], node_size=40,
title='Population, Total = ' + str(total_population))
# Find population and nodes impacted by pressure less than 40 m
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
pressure = results.node['pressure'].loc[:,wn.junction_name_list]
pop_impacted = wntr.metrics.population_impacted(pop, pressure, np.less, 40)
plt.figure()
pop_impacted.sum(axis=1).plot(title='Total population with pressure < 40 m')
nodes_impacted = wntr.metrics.query(pressure, np.less, 40)
wntr.graphics.plot_network(wn, node_attribute=nodes_impacted.any(axis=0), node_size=40,
title='Nodes impacted')
"""
import numpy as np
import pandas as pd
import matplotlib.pylab as plt
import chama
import wntr
# Create a water network model
inp_file = 'networks/Net1.inp'
wn = wntr.network.WaterNetworkModel(inp_file)
# Run trace simulations (one from each junction) and extract data needed for
# sensor placement optimization. You can run this step once, save the data to a
# file, and reload the file for sensor placement
scenario_names = wn.junction_name_list
sim = wntr.sim.EpanetSimulator(wn)
sim.run_sim(save_hyd = True)
wn.options.quality.mode = 'TRACE'
signal = pd.DataFrame()
for inj_node in scenario_names:
print(inj_node)
wn.options.quality.trace_node = inj_node
sim_results = sim.run_sim(use_hyd = True)
trace = sim_results.node['quality']
trace = trace.stack()
trace = trace.reset_index()
trace.columns = ['T', 'Node', inj_node]
signal = signal.combine_first(trace)
signal.to_csv('signal.csv')
# Define feasible sensors using location, sample times, and detection threshold
sensor_names = wn.junction_name_list