NaqchoAli's picture
Update app.py
ab9ae01 verified
import streamlit as st
import math
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
# Function to calculate pipe diameter
def calculate_diameter(flow_rate, velocity):
# Q = A * V, where A = pi * (D/2)^2, so D = sqrt(4 * Q / (pi * V))
diameter = math.sqrt((4 * flow_rate) / (math.pi * velocity))
return diameter
# Function to draw pipe cross-section with animation
def draw_pipe(diameter):
fig, ax = plt.subplots()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
# Draw the initial circle (pipe)
circle = plt.Circle((0, 0), diameter / 2, edgecolor='b', facecolor='lightblue', lw=2)
ax.add_patch(circle)
# Animation function to highlight the diameter
def animate(i):
ax.clear()
ax.set_xlim(-1.5, 1.5)
ax.set_ylim(-1.5, 1.5)
ax.set_aspect('equal')
# Draw the pipe circle
ax.add_patch(circle)
# Draw animated diameter line
ax.plot([-diameter/2, diameter/2], [0, 0], 'r-', lw=2)
ax.text(0, 0.1, f'Diameter: {diameter:.2f} m', fontsize=12, ha='center')
# Create animation
ani = FuncAnimation(fig, animate, frames=np.arange(0, 10), interval=500)
# Display the plot
st.pyplot(fig)
# Streamlit UI
st.title("Pipe Diameter Recommendation App")
st.write("""
This application calculates the recommended diameter for a pipe based on the flow rate, liquid name, and velocity.
""")
# Get user inputs
flow_rate = st.number_input("Enter the flow rate of the liquid (in m³/s):", min_value=0.0)
velocity = st.number_input("Enter the velocity of the liquid (in m/s):", min_value=0.0)
liquid_name = st.text_input("Enter the name of the liquid:")
if st.button("Calculate Diameter"):
# Calculate the diameter
if flow_rate > 0 and velocity > 0:
diameter = calculate_diameter(flow_rate, velocity)
st.write(f"The recommended pipe diameter is {diameter:.2f} meters for {liquid_name}.")
# Draw the cross-section of the pipe with animation
draw_pipe(diameter)
else:
st.error("Please enter valid positive values for flow rate and velocity.")