File size: 14,181 Bytes
1d6e5bc 2856998 1d6e5bc e18c8c7 2856998 345780e 2856998 e18c8c7 efeb68a e18c8c7 efeb68a e18c8c7 efeb68a e18c8c7 d8637bb e18c8c7 98363fd d8637bb 98363fd e18c8c7 98363fd e18c8c7 98363fd d8637bb e0ecfa3 1d6e5bc 525a617 2856998 345780e 55c02a8 2856998 701cbef 2856998 be39936 701cbef be39936 345780e be39936 345780e be39936 345780e be39936 345780e be39936 345780e be39936 2856998 be39936 2856998 be39936 2856998 be39936 2856998 525a617 be39936 525a617 be39936 769be53 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | 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()
|