engkamranansari's picture
Update app.py
7c08ff9 verified
import streamlit as st
import math
import matplotlib.pyplot as plt
import numpy as np
# Function to calculate pipe diameter
def calculate_diameter(flow_rate, velocity):
try:
# Calculate diameter (D = sqrt((4 * Q) / (pi * v)))
diameter = math.sqrt((4 * flow_rate) / (math.pi * velocity))
return diameter
except ZeroDivisionError:
return "Velocity cannot be zero"
except Exception as e:
return f"Error: {e}"
# Streamlit application UI
st.set_page_config(page_title="Pipe Sizing Helper", page_icon="🔧") # Adding an icon and page title
st.title("Pipe Sizing Helper 🔧")
# Adding some description to make the interface more engaging
st.markdown("""
This tool helps you to calculate the **recommended pipe diameter** based on the **flow rate** and **permissible velocity**.
Fill in the required inputs and click **Calculate Pipe Diameter** to get the result.
""")
# Input fields for flow rate and permissible velocity
flow_rate = st.number_input("Enter the Flow Rate (m³/s):", min_value=0.0, step=0.01, format="%.2f")
velocity = st.number_input("Enter the Permissible Velocity (m/s):", min_value=0.1, step=0.1, format="%.2f")
# Button to trigger pipe diameter calculation
if st.button("Calculate Pipe Diameter"):
# Only calculate if both inputs are positive
if flow_rate > 0 and velocity > 0:
diameter = calculate_diameter(flow_rate, velocity)
st.success(f"Recommended Pipe Diameter: {diameter:.2f} meters")
else:
st.error("Please enter valid positive values for flow rate and velocity.")
else:
st.info("Click the button to calculate the pipe diameter.")
# Plot the relationship between flow rate and pipe diameter for a fixed velocity
if velocity > 0:
# Generate a range of flow rates for the graph
flow_rate_range = np.linspace(0.1, 10, 100) # Flow rates from 0.1 to 10 m³/s
diameters = [calculate_diameter(q, velocity) for q in flow_rate_range]
# Create the plot
plt.figure(figsize=(8, 6))
plt.plot(flow_rate_range, diameters, label=f"Velocity = {velocity} m/s", color='b')
plt.title("Flow Rate vs Pipe Diameter")
plt.xlabel("Flow Rate (m³/s)")
plt.ylabel("Pipe Diameter (meters)")
plt.grid(True)
plt.legend()
st.pyplot(plt) # Display the plot in the Streamlit app