Pipe-Dia / app.py
engrtm's picture
Update app.py
8f10281 verified
Raw History Blame Contribute Delete
4.54 kB
import streamlit as st
import math
# Custom CSS for green button (should be at top)
st.markdown("""
<style>
div.stButton > button:first-child {
background-color: #4CAF50;
color: white;
border: none;
padding: 0.5rem 1rem;
}
div.stButton > button:hover {
background-color: #45a049;
color: white;
}
</style>
""", unsafe_allow_html=True)
# Unit conversion dictionaries
flow_units = {
'mΒ³/hr': 1/3600,
'L/s': 0.001,
'US gal/min (GPM)': 0.00006309,
'ftΒ³/s': 0.0283168
}
velocity_units = {
'm/s': 1,
'ft/s': 0.3048,
'cm/s': 0.01,
'km/h': 0.277778
}
st.title("Pipe Size Helper πŸ”§")
st.write("This app calculates required pipe diameter based on flow rate and velocity")
# Formula expander
with st.expander("Formula", expanded=False):
st.subheader("Calculation Formula")
st.latex(r"D = \sqrt{\frac{4Q}{\pi v}}")
st.write("Where:")
st.write("- D = Pipe diameter (meters)")
st.write("- Q = Flow rate in mΒ³/s (converted from input units)")
st.write("- v = Velocity in m/s (converted from input units)")
# Guidelines expander
with st.expander("General Design Guidelines by Fluid Type", expanded=False):
st.markdown("""
**Fluid Velocity Recommendations**
**Water:**
- Cold Water: 0.6–1.8 m/s (2–6 ft/s)
- Hot Water: 1.2–2.4 m/s (4–8 ft/s)
- High-pressure systems: Up to 3 m/s (10 ft/s)
**Steam:**
- Saturated Steam: 25–40 m/s (80–130 ft/s)
- Superheated Steam: 30–60 m/s (100–200 ft/s)
**Compressed Air/Gases:**
- Low-pressure: 10–20 m/s (30–65 ft/s)
- High-pressure: 15–30 m/s (50–100 ft/s)
**Oil/High-Viscosity Fluids:**
- Light oils: 1–2 m/s (3–6 ft/s)
- Heavy oils: 0.5–1.5 m/s (1.5–5 ft/s)
**Slurries/Sewage:**
- 0.5–1.5 m/s (1.5–5 ft/s)
**HVAC Systems:**
- Chilled/Hot Water: 1–2 m/s (3–6 ft/s)
**Key Design Considerations**
- Erosion: <1.5 m/s for abrasive fluids
- Noise: <2 m/s for residential areas
- Material: PVC has lower max velocities than steel
- Flow Regime: Higher velocities for turbulent flow
*Always consult relevant engineering standards (ASME, ISO) for specific applications*
""")
# Input section
st.subheader("Input Parameters")
col1, col2 = st.columns(2)
with col1:
# Flow rate input
flow_col1, flow_col2 = st.columns([2, 1])
with flow_col1:
flow_value = st.number_input(
"Flow Rate",
min_value=0.1,
max_value=1e6,
value=3600.0,
step=1.0,
format="%.2f"
)
with flow_col2:
flow_unit = st.selectbox(
"Flow Unit",
options=list(flow_units.keys()),
index=0
)
with col2:
# Velocity input
vel_col1, vel_col2 = st.columns([2, 1])
with vel_col1:
velocity_value = st.number_input(
"Velocity",
min_value=0.1,
max_value=100.0,
value=1.0,
step=0.1,
format="%.2f"
)
with vel_col2:
velocity_unit = st.selectbox(
"Velocity Unit",
options=list(velocity_units.keys()),
index=0
)
# Calculate button
if st.button("Calculate Pipe Diameter", type="primary"):
try:
# Convert inputs to SI units
Q = flow_value * flow_units[flow_unit]
v = velocity_value * velocity_units[velocity_unit]
# Validate inputs
if v <= 0:
raise ValueError("Velocity must be greater than zero")
# Calculate diameter
diameter_m = math.sqrt((4 * Q) / (math.pi * v))
diameter_in = diameter_m * 39.3701 # Convert to inches
# Display results
st.subheader("Results")
st.metric("Pipe Diameter",
f"{diameter_in:.2f} inches",
f"{diameter_m:.3f} meters")
st.success("Calculation completed successfully! βœ…")
# Conversion details
with st.expander("Show unit conversions"):
st.write(f"Converted flow rate: {Q:.6f} mΒ³/s")
st.write(f"Converted velocity: {v:.3f} m/s")
except ZeroDivisionError:
st.error("Error: Velocity cannot be zero. Please enter a positive value.")
except ValueError as ve:
st.error(f"Input Error: {str(ve)}")
except Exception as e:
st.error(f"Calculation Error: {str(e)}")