import streamlit as st import math import pandas as pd def calculate_pipe_diameter(flow_rate, velocity): """ Calculate the required pipe diameter based on flow rate and permissible velocity. Args: flow_rate (float): Flow rate in cubic meters per hour (m³/h) velocity (float): Permissible velocity in meters per second (m/s) Returns: float: Required pipe diameter in millimeters (mm) """ # Convert flow rate from m³/h to m³/s flow_rate_m3s = flow_rate / 3600 # Calculate pipe area using A = Q/V where Q is flow rate and V is velocity pipe_area = flow_rate_m3s / velocity # m² # Calculate pipe diameter using A = πr² → D = 2r = 2√(A/π) pipe_diameter_m = 2 * math.sqrt(pipe_area / math.pi) # Convert from meters to millimeters pipe_diameter_mm = pipe_diameter_m * 1000 return pipe_diameter_mm def get_standard_pipe_sizes(diameter_mm): """ Returns a table of standard pipe sizes near the calculated diameter. Args: diameter_mm (float): Calculated diameter in mm Returns: pandas.DataFrame: Table of standard pipe sizes """ # Common standard pipe sizes in mm (nominal diameter) standard_sizes = [15, 20, 25, 32, 40, 50, 65, 80, 100, 125, 150, 200, 250, 300, 350, 400, 450, 500] # Find nearest standard sizes (one below and two above) nearest_sizes = [] for size in standard_sizes: if len(nearest_sizes) < 3 and size >= diameter_mm: nearest_sizes.append(size) # Add one size below if possible below_sizes = [size for size in standard_sizes if size < diameter_mm] if below_sizes: nearest_sizes.insert(0, max(below_sizes)) # Ensure we have at most 3 sizes nearest_sizes = nearest_sizes[:3] # Create data for each size data = [] for size in nearest_sizes: # Calculate actual area and velocity for this standard size area_m2 = math.pi * ((size/1000)/2)**2 velocity = (flow_rate / 3600) / area_m2 data.append({ "Nominal Size (mm)": size, "Inner Diameter (mm)": size - 2, # Approximate internal diameter "Calculated Velocity (m/s)": round(velocity, 2) }) return pd.DataFrame(data) # Set up the Streamlit app st.set_page_config( page_title="Pipe Size Helper", page_icon="🔧", layout="centered" ) st.title("🔧 Pipe Size Helper") st.markdown(""" This application helps you determine the appropriate pipe size based on your flow rate requirements and permissible velocity constraints. """) # Create two columns for inputs col1, col2 = st.columns(2) with col1: flow_rate = st.number_input( "Flow Rate (m³/h)", min_value=0.1, max_value=10000.0, value=10.0, step=1.0, help="Enter the required flow rate in cubic meters per hour" ) with col2: velocity = st.number_input( "Permissible Velocity (m/s)", min_value=0.1, max_value=10.0, value=1.5, step=0.1, help="Enter the maximum permissible velocity in meters per second" ) # Calculate button if st.button("Calculate Pipe Size", type="primary"): if flow_rate > 0 and velocity > 0: # Calculate the required pipe diameter pipe_diameter = calculate_pipe_diameter(flow_rate, velocity) # Display the result st.markdown("### Results") st.metric( label="Theoretical Pipe Diameter", value=f"{pipe_diameter:.2f} mm" ) # Show standard pipe size recommendations st.markdown("### Recommended Standard Pipe Sizes") st.markdown("Choose a standard pipe size that has a velocity close to your permissible value:") # Get and display standard pipe sizes standard_sizes_df = get_standard_pipe_sizes(pipe_diameter) st.dataframe(standard_sizes_df, use_container_width=True) # Interpretation st.markdown("### Interpretation") if standard_sizes_df["Calculated Velocity (m/s)"].iloc[0] > velocity: st.warning(f"⚠️ The smallest standard pipe size results in velocity above your permissible value of {velocity} m/s.") st.markdown("Consider selecting a larger pipe size or increasing your permissible velocity if appropriate for your application.") else: ideal_size = standard_sizes_df.iloc[0]["Nominal Size (mm)"] st.success(f"✅ Recommended pipe size: **{ideal_size} mm** nominal diameter") else: st.error("Please enter valid values for flow rate and velocity (both must be greater than 0)") # Add explanation section with st.expander("How it works"): st.markdown(""" ### Calculation Method This app uses the following hydraulic principles to calculate the required pipe diameter: 1. **Flow Rate Equation**: Q = V × A - Q = Flow Rate (m³/s) - V = Velocity (m/s) - A = Cross-sectional Area (m²) 2. **Pipe Area**: A = π × r² - r = Pipe radius (m) 3. **Pipe Diameter**: D = 2 × r = 2 × √(A/π) = 2 × √(Q/(V×π)) The app then suggests standard pipe sizes that are commercially available to match your requirements. """) # Footer st.markdown("---") st.markdown("Built with Streamlit | Pipe Size Helper © 2025")