Skip to content

Instantly share code, notes, and snippets.

@FirokOtaku
Last active April 11, 2023 07:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FirokOtaku/34060ed25bd2506e328091f94d5904e5 to your computer and use it in GitHub Desktop.
Save FirokOtaku/34060ed25bd2506e328091f94d5904e5 to your computer and use it in GitHub Desktop.
230411 demo EPANET .inp file and related .py script
import wntr
workdir = 'F:/Projects/epanet-wntr-demo/workdir'
file_input = workdir + '/net-2.inp'
file_output_network = workdir + '/output-network.png'
print('file input: ' + file_input)
HOUR1 = 3600
HOUR_MAX = 24
# Create a water network model
wn = wntr.network.WaterNetworkModel(file_input)
# earthquake sim
# wn = wntr.morph.scale_node_coordinates(wn, 1000)
epicenter = (32000, 15000) # x,y location
magnitude = 2.5 # Richter scale
depth = 10000 # m, shallow depth
earthquake = wntr.scenario.Earthquake(epicenter, magnitude, depth)
distance = earthquake.distance_to_epicenter(wn, element_type=wntr.network.Pipe)
pga = earthquake.pga_attenuation_model(distance)
pgv = earthquake.pgv_attenuation_model(distance)
repair_rate = earthquake.repair_rate_model(pgv)
wntr.graphics.plot_network(
wn,
link_attribute=pga,
node_size=4,
link_width=2,
link_colorbar_label='PGA (g) Peak ground acceleration',
filename=workdir + '/output-network-earthquake.png'
)
# leak sim
leak_junction_1: wntr.network.Junction = wn.get_node('j1')
leak_junction_2: wntr.network.Junction = wn.get_node('j2')
leak_junction_2.add_leak(
wn,
area=0.2,
start_time=HOUR1 * 6,
end_time=HOUR1 * 9
)
leak_junction_1.add_leak(
wn,
area=0.5,
start_time=HOUR1 * 16,
end_time=HOUR1 * 20
)
# outage sim
outage_pump_j1_j4: wntr.network.Pump = wn.get_link('pp-j1-j4')
outage_pump_j1_j4.add_outage(wn, HOUR1 * 7, HOUR1 * 14)
# general config
wn.options.time.duration = HOUR1 * HOUR_MAX
# wn.options.time.quality_timestep = HOUR1
wn.options.time.report_timestep = HOUR1 / 4
wn.options.time.hydraulic_timestep = HOUR1 / 4
wn.options.hydraulic.minimum_pressure = 0
DRAW_PRESSURE = True
DRAW_HEAD = False
# Graph the network
# wntr.graphics.plot_network(wn, title=wn.name, filename=file_output_network)
sim = wntr.sim.EpanetSimulator(wn)
results = sim.run_sim()
print('results.node', results.node)
# Plot results on the network
# pressure_at_5hr = results.node['pressure'].loc[0, :]
for step in range(0, HOUR_MAX):
pt = step * HOUR1
print(''.join([str(step), ' - time - ', str(pt)]))
if DRAW_PRESSURE:
pressure_at_pt = results.node['pressure'].loc[pt, :]
title_pressure = ''.join(['Pressure at HOUR (', str(step), ')'])
wntr.graphics.plot_network(
wn,
node_attribute=pressure_at_pt,
link_attribute=pressure_at_pt,
node_size=30,
title=title_pressure,
filename=workdir + '/' + title_pressure + '.png',
)
if DRAW_HEAD:
head_at_pt = results.node['head'].loc[pt, :]
title_head = ''.join(['Head at HOUR (', str(step), ')'])
wntr.graphics.plot_network(
wn,
node_attribute=head_at_pt,
node_size=30,
title=title_head,
filename=workdir + '/' + title_head + '.png'
)
[TITLE]
[JUNCTIONS]
;ID Elev Demand Pattern
j1 0 0 ;
j2 0 0 ;
j3 0 0 ;
j4 0 0 ;
[RESERVOIRS]
;ID Head Pattern
s1 0 ;
s2 0 ;
s3 0 ;
[TANKS]
;ID Elevation InitLevel MinLevel MaxLevel Diameter MinVol VolCurve Overflow
t1 0 10 0 300 1000 0 ;
[PIPES]
;ID Node1 Node2 Length Diameter Roughness MinorLoss Status
p-s1-j1 s1 j1 1000 12 100 0 Open ;
p-s2-j2 s2 j2 1000 12 150 0 Open ;
p-s3-j3 s3 j3 1500 12 150 0 Open ;
p-j4-t1 j4 t1 1000 12 100 0 Open ;
[PUMPS]
;ID Node1 Node2 Parameters
pp-j1-j4 j1 j4 POWER 100 SPEED 50 ;
pp-j2-j4 j2 j4 POWER 100 SPEED 80 ;
pp-j3-t1 j3 t1 POWER 100 SPEED 40 ;
[VALVES]
;ID Node1 Node2 Diameter Type Setting MinorLoss
[TAGS]
[DEMANDS]
;Junction Demand Pattern Category
[STATUS]
;ID Status/Setting
pp-j1-j4 50
pp-j2-j4 80
pp-j3-t1 40
[PATTERNS]
;ID Multipliers
[CURVES]
;ID X-Value Y-Value
[CONTROLS]
[RULES]
[ENERGY]
Global Efficiency 75
Global Price 0
Demand Charge 0
[EMITTERS]
;Junction Coefficient
[QUALITY]
;Node InitQual
j1 0
j2 0
j3 0
s1 0
s2 100
s3 300
[SOURCES]
;Node Type Quality Pattern
[REACTIONS]
;Type Pipe/Tank Coefficient
[REACTIONS]
Order Bulk 1
Order Tank 1
Order Wall 1
Global Bulk 0
Global Wall 0
Limiting Potential 0
Roughness Correlation 0
[MIXING]
;Tank Model
[TIMES]
Duration 0
Hydraulic Timestep 1:00
Quality Timestep 0:05
Pattern Timestep 1:00
Pattern Start 0:00
Report Timestep 1:00
Report Start 0:00
Start ClockTime 12 am
Statistic None
[REPORT]
Status No
Summary No
Page 0
[OPTIONS]
Units GPM
Headloss H-W
Specific Gravity 1
Viscosity 1
Trials 40
Accuracy 0.001
CHECKFREQ 2
MAXCHECK 10
DAMPLIMIT 0
Unbalanced Continue 10
Pattern 1
Demand Multiplier 1.0
Emitter Exponent 0.5
Quality None mg/L
Diffusivity 1
Tolerance 0.01
[COORDINATES]
;Node X-Coord Y-Coord
j1 5604.167 8229.167
j2 5625.000 6375.000
j3 8958.333 4645.833
j4 8854.167 7166.667
s1 1354.167 8166.667
s2 1333.333 6312.500
s3 1312.500 4791.667
t1 12562.500 7187.500
[VERTICES]
;Link X-Coord Y-Coord
[LABELS]
;X-Coord Y-Coord Label & Anchor Node
500.000 8770.833 "水源s1 水质0"
395.833 6958.333 "水源s2 水质100"
145.833 5375.000 "水源s3 水质300"
5229.167 8812.500 "结点1"
5187.500 6937.500 "结点2"
8604.167 5229.167 "结点3"
2291.667 6125.000 "p-s2-j2 摩擦力150"
2458.333 8041.667 "p-s1-j1 摩擦力100"
2958.333 4625.000 "p-s3-j3 摩擦力 150 长度 1500"
8562.500 7791.667 "结点4"
7395.833 8395.833 "pp-j1-j4 速度 50"
7125.000 6520.833 "pp-j2-j4 速度 80"
11291.667 7875.000 "t1 直径 1000 最高 300"
9583.333 7062.500 "p-j4-t1 长度 1000"
10770.833 5770.833 "pp-j3-t1 速度 40"
[BACKDROP]
DIMENSIONS 0.000 0.000 10000.000 10000.000
UNITS None
FILE
OFFSET 0.00 0.00
[END]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment