Fluid / app.py
Kkjutt000's picture
Update app.py
701cbef verified
import gradio as gr
import time
# Reynolds Number Calculation Logic
def calculate_reynolds_number(velocity, diameter, density, viscosity, velocity_unit, diameter_unit, density_unit, viscosity_unit):
# Unit conversions
if velocity_unit == "cm/s":
velocity_m = velocity / 100
elif velocity_unit == "ft/s":
velocity_m = velocity * 0.3048
else:
velocity_m = velocity
if diameter_unit == "cm":
diameter_m = diameter / 100
elif diameter_unit == "mm":
diameter_m = diameter / 1000
elif diameter_unit == "ft":
diameter_m = diameter * 0.3048
else:
diameter_m = diameter
if density_unit == "g/cm³":
density_kgm3 = density * 1000
else:
density_kgm3 = density
if viscosity_unit == "cP":
viscosity_pas = viscosity / 1000
else:
viscosity_pas = viscosity
try:
reynolds_number = (velocity_m * diameter_m * density_kgm3) / viscosity_pas
if reynolds_number < 2300:
flow_type = "Laminar Flow"
elif 2300 <= reynolds_number <= 4000:
flow_type = "Transitional Flow"
else:
flow_type = "Turbulent Flow"
return f"Reynolds Number: {reynolds_number:.2f} ({flow_type})"
except ZeroDivisionError:
return "Error: Viscosity cannot be zero."
# Create a dynamic name simulation (scrolling effect)
def simulate_name():
name_text = "Designed by Kamran Liaqat - 2k23-chE-05" # Adding the new text
spaces = " " * 10 # Padding before the name
scrolling_name = spaces + name_text + spaces
result = []
for i in range(len(scrolling_name)):
result.append(scrolling_name[i:] + scrolling_name[:i]) # Simulate scrolling effect by rotating the string
time.sleep(0.1) # Adjust speed of scrolling
return result
# Centrifugal Pump Power Calculation Logic
def calculate_pump_power(flow_rate, head, efficiency, density, flow_rate_unit, head_unit, density_unit):
if flow_rate_unit == "L/s":
flow_rate_m3s = flow_rate / 1000
elif flow_rate_unit == "gpm":
flow_rate_m3s = flow_rate * 3.78541 / 60000
else:
flow_rate_m3s = flow_rate / 3600
if head_unit == "ft":
head_m = head * 0.3048
else:
head_m = head
if density_unit == "g/cm³":
density_kgm3 = density * 1000
else:
density_kgm3 = density
try:
g = 9.81
efficiency_decimal = efficiency / 100
hydraulic_power = density_kgm3 * g * flow_rate_m3s * head_m
shaft_power = hydraulic_power / efficiency_decimal
return f"Hydraulic Power: {hydraulic_power / 1000:.2f} kW, Shaft Power: {shaft_power / 1000:.2f} kW"
except ZeroDivisionError:
return "Error: Efficiency cannot be zero."
# Orifice Meter Calculation Logic
def calculate_orifice_meter(diameter_pipe, diameter_orifice, flow_rate, pressure_drop, diameter_unit, flow_rate_unit, pressure_unit):
if diameter_unit == "cm":
diameter_pipe_m = diameter_pipe / 100
diameter_orifice_m = diameter_orifice / 100
elif diameter_unit == "mm":
diameter_pipe_m = diameter_pipe / 1000
diameter_orifice_m = diameter_orifice / 1000
else:
diameter_pipe_m = diameter_pipe
diameter_orifice_m = diameter_orifice
if flow_rate_unit == "L/s":
flow_rate_m3s = flow_rate / 1000
elif flow_rate_unit == "gpm":
flow_rate_m3s = flow_rate * 3.78541 / 60000
else:
flow_rate_m3s = flow_rate
if pressure_unit == "bar":
pressure_pa = pressure_drop * 1e5
elif pressure_unit == "psi":
pressure_pa = pressure_drop * 6894.76
else:
pressure_pa = pressure_drop
try:
beta = diameter_orifice_m / diameter_pipe_m
discharge_coefficient = 0.61
area_orifice = 3.14159 * (diameter_orifice_m / 2) ** 2
flow_rate_calculated = (
discharge_coefficient
* area_orifice
* (2 * pressure_pa / 1000) ** 0.5
)
flow_rate_cgs = flow_rate_calculated * 1000000
flow_rate_imperial = flow_rate_calculated * 2118.88
return (
f"Flow Rate (SI): {flow_rate_calculated:.4f} m³/s, "
f"Flow Rate (CGS): {flow_rate_cgs:.2f} cm³/s, "
f"Flow Rate (Imperial): {flow_rate_imperial:.2f} gpm"
)
except ZeroDivisionError:
return "Error: Diameter or pressure drop cannot be zero."
# Sedimentation Tank Calculation Logic
def calculate_sedimentation_tank(flow_rate, length, width, depth, flow_rate_unit, length_unit, width_unit, depth_unit):
if flow_rate_unit == "L/s":
flow_rate_m3s = flow_rate / 1000
elif flow_rate_unit == "m³/h":
flow_rate_m3s = flow_rate / 3600
else:
flow_rate_m3s = flow_rate
if length_unit == "cm":
length_m = length / 100
elif length_unit == "mm":
length_m = length / 1000
else:
length_m = length
if width_unit == "cm":
width_m = width / 100
elif width_unit == "mm":
width_m = width / 1000
else:
width_m = width
if depth_unit == "cm":
depth_m = depth / 100
elif depth_unit == "mm":
depth_m = depth / 1000
else:
depth_m = depth
try:
tank_volume = length_m * width_m * depth_m
detention_time = tank_volume / flow_rate_m3s
return f"Tank Volume: {tank_volume:.2f} m³, Detention Time: {detention_time:.2f} seconds"
except ZeroDivisionError:
return "Error: Flow rate cannot be zero."
# Bernoulli's Equation Calculation Logic
def calculate_bernoulli(p1, p2, v1, v2, h1, h2, p_unit, v_unit, h_unit):
# Unit conversions
if p_unit == "Pa":
p1_pa = p1
p2_pa = p2
elif p_unit == "bar":
p1_pa = p1 * 1e5
p2_pa = p2 * 1e5
elif p_unit == "psi":
p1_pa = p1 * 6894.76
p2_pa = p2 * 6894.76
else:
p1_pa = p1
p2_pa = p2
if v_unit == "m/s":
v1_mps = v1
v2_mps = v2
elif v_unit == "cm/s":
v1_mps = v1 / 100
v2_mps = v2 / 100
elif v_unit == "ft/s":
v1_mps = v1 * 0.3048
v2_mps = v2 * 0.3048
else:
v1_mps = v1
v2_mps = v2
if h_unit == "m":
h1_m = h1
h2_m = h2
elif h_unit == "cm":
h1_m = h1 / 100
h2_m = h2 / 100
elif h_unit == "ft":
h1_m = h1 * 0.3048
h2_m = h2 * 0.3048
else:
h1_m = h1
h2_m = h2
try:
# Bernoulli's equation: p1 + 0.5 * rho * v1^2 + rho * g * h1 = p2 + 0.5 * rho * v2^2 + rho * g * h2
g = 9.81 # gravity in m/s²
rho = 1000 # density of water in kg/m³
# Calculate the change in pressure based on Bernoulli's equation
delta_p = (0.5 * rho * (v2_mps**2 - v1_mps**2)) + (rho * g * (h2_m - h1_m)) + (p1_pa - p2_pa)
return f"Pressure Difference (ΔP): {delta_p:.2f} Pa"
except ZeroDivisionError:
return "Error: Zero value input encountered."
# Gradio Interface
with gr.Blocks() as demo:
# Custom CSS for background color
gr.HTML("""
<style>
body {
background-color: #76c7c0; /* Reddish-green color */
}
</style>
""")
# Name Template Section with Scrolling Effect
gr.Markdown("# Designed by Kamran Liaqat - 2k23-chE-05")
# Start a simulation for scrolling name
name_simulation = simulate_name()
scrolling_text = gr.Textbox(value="", label="Scrolling Name", interactive=False)
def update_scrolling_text():
for name in name_simulation:
scrolling_text.update(value=name)
time.sleep(0.1)
# Supervised by Hidaytullah Mahar in Small Font beneath scrolling name
gr.Markdown("<div style='font-size: small; color: #333;'>Supervised by Hidaytullah Mahar</div>")
# Start the simulation when the interface is launched
demo.load(update_scrolling_text)
# Tabbed Interface for Calculators
with gr.Tabs():
with gr.Tab("1. Reynolds Number Calculator"):
gr.Markdown("# Reynolds Number Calculator")
with gr.Row():
velocity = gr.Number(label="Fluid Velocity", value=1.0)
velocity_unit = gr.Dropdown(["m/s", "cm/s", "ft/s"], label="Velocity Unit", value="m/s")
with gr.Row():
diameter = gr.Number(label="Hydraulic Diameter", value=0.1)
diameter_unit = gr.Dropdown(["m", "cm", "mm", "ft"], label="Diameter Unit", value="m")
with gr.Row():
density = gr.Number(label="Fluid Density", value=1000.0)
density_unit = gr.Dropdown(["kg/m³", "g/cm³"], label="Density Unit", value="kg/m³")
with gr.Row():
viscosity = gr.Number(label="Dynamic Viscosity", value=0.001)
viscosity_unit = gr.Dropdown(["Pa·s", "cP"], label="Viscosity Unit", value="Pa·s")
reynolds_button = gr.Button("Calculate Reynolds Number")
reynolds_result = gr.Textbox(label="Reynolds Number Result")
reynolds_button.click(
calculate_reynolds_number,
inputs=[velocity, diameter, density, viscosity, velocity_unit, diameter_unit, density_unit, viscosity_unit],
outputs=reynolds_result,
)
with gr.Tab("2. Centrifugal Pump Power Calculator"):
gr.Markdown("# Centrifugal Pump Power Calculator")
with gr.Row():
flow_rate = gr.Number(label="Flow Rate", value=10.0)
flow_rate_unit = gr.Dropdown(["m³/h", "L/s", "gpm"], label="Flow Rate Unit", value="m³/h")
with gr.Row():
head = gr.Number(label="Head", value=20.0)
head_unit = gr.Dropdown(["m", "ft"], label="Head Unit", value="m")
with gr.Row():
efficiency = gr.Number(label="Efficiency (%)", value=75.0)
density = gr.Number(label="Fluid Density", value=1000.0)
density_unit = gr.Dropdown(["kg/m³", "g/cm³"], label="Density Unit", value="kg/m³")
pump_button = gr.Button("Calculate Pump Power")
pump_result = gr.Textbox(label="Pump Power Result")
pump_button.click(
calculate_pump_power,
inputs=[flow_rate, head, efficiency, density, flow_rate_unit, head_unit, density_unit],
outputs=pump_result,
)
with gr.Tab("3. Orifice Meter Flow Calculator"):
gr.Markdown("# Orifice Meter Flow Calculator")
with gr.Row():
diameter_pipe = gr.Number(label="Pipe Diameter", value=0.1)
diameter_orifice = gr.Number(label="Orifice Diameter", value=0.05)
diameter_unit = gr.Dropdown(["m", "cm", "mm"], label="Diameter Unit", value="m")
with gr.Row():
flow_rate = gr.Number(label="Flow Rate", value=10.0)
flow_rate_unit = gr.Dropdown(["m³/s", "L/s", "gpm"], label="Flow Rate Unit", value="m³/s")
with gr.Row():
pressure_drop = gr.Number(label="Pressure Drop", value=10.0)
pressure_unit = gr.Dropdown(["Pa", "bar", "psi"], label="Pressure Unit", value="Pa")
orifice_button = gr.Button("Calculate Orifice Flow Rate")
orifice_result = gr.Textbox(label="Orifice Flow Rate Result")
orifice_button.click(
calculate_orifice_meter,
inputs=[diameter_pipe, diameter_orifice, flow_rate, pressure_drop, diameter_unit, flow_rate_unit, pressure_unit],
outputs=orifice_result,
)
with gr.Tab("4. Sedimentation Tank Calculator"):
gr.Markdown("# Sedimentation Tank Calculator")
with gr.Row():
flow_rate = gr.Number(label="Flow Rate", value=5.0)
flow_rate_unit = gr.Dropdown(["L/s", "m³/h"], label="Flow Rate Unit", value="L/s")
with gr.Row():
length = gr.Number(label="Length", value=5.0)
width = gr.Number(label="Width", value=2.0)
depth = gr.Number(label="Depth", value=3.0)
length_unit = gr.Dropdown(["m", "cm", "mm"], label="Length Unit", value="m")
width_unit = gr.Dropdown(["m", "cm", "mm"], label="Width Unit", value="m")
depth_unit = gr.Dropdown(["m", "cm", "mm"], label="Depth Unit", value="m")
sedimentation_button = gr.Button("Calculate Sedimentation Tank")
sedimentation_result = gr.Textbox(label="Sedimentation Tank Result")
sedimentation_button.click(
calculate_sedimentation_tank,
inputs=[flow_rate, length, width, depth, flow_rate_unit, length_unit, width_unit, depth_unit],
outputs=sedimentation_result,
)
with gr.Tab("5. Bernoulli's Equation Calculation"):
gr.Markdown("# Bernoulli's Equation Calculation")
with gr.Row():
p1 = gr.Number(label="Pressure (P1)", value=101325.0)
p2 = gr.Number(label="Pressure (P2)", value=90000.0)
p_unit = gr.Dropdown(["Pa", "bar", "psi"], label="Pressure Unit", value="Pa")
with gr.Row():
v1 = gr.Number(label="Velocity (V1)", value=5.0)
v2 = gr.Number(label="Velocity (V2)", value=4.0)
v_unit = gr.Dropdown(["m/s", "cm/s", "ft/s"], label="Velocity Unit", value="m/s")
with gr.Row():
h1 = gr.Number(label="Height (H1)", value=10.0)
h2 = gr.Number(label="Height (H2)", value=5.0)
h_unit = gr.Dropdown(["m", "cm", "ft"], label="Height Unit", value="m")
bernoulli_button = gr.Button("Calculate Pressure Difference")
bernoulli_result = gr.Textbox(label="Bernoulli's Result")
bernoulli_button.click(
calculate_bernoulli,
inputs=[p1, p2, v1, v2, h1, h2, p_unit, v_unit, h_unit],
outputs=bernoulli_result,
)
# Launch the interface
demo.launch()