Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,67 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
|
| 3 |
-
import
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
st.
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
import tempfile
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import pickle
|
| 5 |
+
|
| 6 |
+
st.set_page_config(page_title="TSNet Transient Simulator", layout="wide")
|
| 7 |
+
st.title("🌊 TSNet Transient Simulation App")
|
| 8 |
+
|
| 9 |
+
# Step 1: Upload EPANET .inp File
|
| 10 |
+
st.header("1. Upload EPANET .inp File")
|
| 11 |
+
uploaded_file = st.file_uploader("Upload your .inp file", type=["inp"])
|
| 12 |
+
|
| 13 |
+
if uploaded_file is not None:
|
| 14 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".inp") as temp_inp:
|
| 15 |
+
temp_inp.write(uploaded_file.read())
|
| 16 |
+
inp_path = temp_inp.name
|
| 17 |
+
|
| 18 |
+
st.success("File uploaded and saved.")
|
| 19 |
+
st.header("2. Set Simulation Parameters")
|
| 20 |
+
|
| 21 |
+
wave_speed = st.number_input("Wave Speed (m/s)", value=1000.0)
|
| 22 |
+
dt = st.number_input("Time Step (s)", value=0.01)
|
| 23 |
+
duration = st.number_input("Simulation Duration (s)", value=10.0)
|
| 24 |
+
|
| 25 |
+
if st.button("Run TSNet Simulation"):
|
| 26 |
+
import tsnet
|
| 27 |
+
from tsnet.network.model import TransientModel
|
| 28 |
+
from tsnet.simulation import MOCSimulator
|
| 29 |
+
|
| 30 |
+
net = TransientModel(inp_path)
|
| 31 |
+
net.set_wavespeed(wave_speed)
|
| 32 |
+
net.set_time(duration, dt)
|
| 33 |
+
|
| 34 |
+
for pipe in net.pipes():
|
| 35 |
+
if not hasattr(pipe, 'initial_head'):
|
| 36 |
+
pipe.initial_head = (pipe.start_node.head + pipe.end_node.head) / 2
|
| 37 |
+
|
| 38 |
+
sim = MOCSimulator(net, 'results')
|
| 39 |
+
with open("results.obj", "wb") as f:
|
| 40 |
+
pickle.dump(sim, f)
|
| 41 |
+
|
| 42 |
+
st.session_state["sim_results"] = "results.obj"
|
| 43 |
+
st.success("Simulation complete.")
|
| 44 |
+
|
| 45 |
+
if "sim_results" in st.session_state:
|
| 46 |
+
st.header("3. Plot Results")
|
| 47 |
+
sim = pickle.load(open(st.session_state["sim_results"], "rb"))
|
| 48 |
+
|
| 49 |
+
node_id = st.selectbox("Select Node to Plot Head", sim.node_name_list)
|
| 50 |
+
pipe_id = st.selectbox("Select Pipe to Plot Flow", sim.pipe_name_list)
|
| 51 |
+
|
| 52 |
+
head = sim.get_node_head(node_id)
|
| 53 |
+
flow = sim.get_link_flow(pipe_id)
|
| 54 |
+
|
| 55 |
+
fig1, ax1 = plt.subplots()
|
| 56 |
+
ax1.plot(sim.Time, head)
|
| 57 |
+
ax1.set_title(f"Head at Node {node_id}")
|
| 58 |
+
ax1.set_xlabel("Time (s)")
|
| 59 |
+
ax1.set_ylabel("Head (m)")
|
| 60 |
+
st.pyplot(fig1)
|
| 61 |
+
|
| 62 |
+
fig2, ax2 = plt.subplots()
|
| 63 |
+
ax2.plot(sim.Time, flow)
|
| 64 |
+
ax2.set_title(f"Flow in Pipe {pipe_id}")
|
| 65 |
+
ax2.set_xlabel("Time (s)")
|
| 66 |
+
ax2.set_ylabel("Flow (L/s)")
|
| 67 |
+
st.pyplot(fig2)
|