Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import math
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from matplotlib.animation import FuncAnimation
|
| 6 |
+
|
| 7 |
+
# Function to calculate pipe diameter
|
| 8 |
+
def calculate_diameter(flow_rate, velocity):
|
| 9 |
+
# Q = A * V, where A = pi * (D/2)^2, so D = sqrt(4 * Q / (pi * V))
|
| 10 |
+
diameter = math.sqrt((4 * flow_rate) / (math.pi * velocity))
|
| 11 |
+
return diameter
|
| 12 |
+
|
| 13 |
+
# Function to draw pipe cross-section with animation
|
| 14 |
+
def draw_pipe(diameter):
|
| 15 |
+
fig, ax = plt.subplots()
|
| 16 |
+
ax.set_xlim(-1.5, 1.5)
|
| 17 |
+
ax.set_ylim(-1.5, 1.5)
|
| 18 |
+
ax.set_aspect('equal')
|
| 19 |
+
|
| 20 |
+
# Draw the initial circle (pipe)
|
| 21 |
+
circle = plt.Circle((0, 0), diameter / 2, edgecolor='b', facecolor='lightblue', lw=2)
|
| 22 |
+
ax.add_patch(circle)
|
| 23 |
+
|
| 24 |
+
# Animation function to highlight the diameter
|
| 25 |
+
def animate(i):
|
| 26 |
+
ax.clear()
|
| 27 |
+
ax.set_xlim(-1.5, 1.5)
|
| 28 |
+
ax.set_ylim(-1.5, 1.5)
|
| 29 |
+
ax.set_aspect('equal')
|
| 30 |
+
|
| 31 |
+
# Draw the pipe circle
|
| 32 |
+
ax.add_patch(circle)
|
| 33 |
+
|
| 34 |
+
# Draw animated diameter line
|
| 35 |
+
ax.plot([-diameter/2, diameter/2], [0, 0], 'r-', lw=2)
|
| 36 |
+
ax.text(0, 0.1, f'Diameter: {diameter:.2f} m', fontsize=12, ha='center')
|
| 37 |
+
|
| 38 |
+
# Create animation
|
| 39 |
+
ani = FuncAnimation(fig, animate, frames=np.arange(0, 10), interval=500)
|
| 40 |
+
|
| 41 |
+
# Display the plot
|
| 42 |
+
st.pyplot(fig)
|
| 43 |
+
|
| 44 |
+
# Streamlit UI
|
| 45 |
+
st.title("Pipe Diameter Recommendation App")
|
| 46 |
+
st.write("""
|
| 47 |
+
This application calculates the recommended diameter for a pipe based on the flow rate, liquid name, and velocity.
|
| 48 |
+
""")
|
| 49 |
+
|
| 50 |
+
# Get user inputs
|
| 51 |
+
flow_rate = st.number_input("Enter the flow rate of the liquid (in m³/s):", min_value=0.0)
|
| 52 |
+
velocity = st.number_input("Enter the velocity of the liquid (in m/s):", min_value=0.0)
|
| 53 |
+
liquid_name = st.text_input("Enter the name of the liquid:")
|
| 54 |
+
|
| 55 |
+
if st.button("Calculate Diameter"):
|
| 56 |
+
# Calculate the diameter
|
| 57 |
+
if flow_rate > 0 and velocity > 0:
|
| 58 |
+
diameter = calculate_diameter(flow_rate, velocity)
|
| 59 |
+
st.write(f"The recommended pipe diameter is {diameter:.2f} meters for {liquid_name}.")
|
| 60 |
+
|
| 61 |
+
# Draw the cross-section of the pipe with animation
|
| 62 |
+
draw_pipe(diameter)
|
| 63 |
+
else:
|
| 64 |
+
st.error("Please enter valid positive values for flow rate and velocity.")
|