SabaAnver commited on
Commit
f6184ad
·
verified ·
1 Parent(s): bf8a92a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +153 -0
app.py ADDED
@@ -0,0 +1,153 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import math
2
+ import streamlit as st
3
+
4
+ st.set_page_config(page_title="Pump Power Calculator", page_icon="💧", layout="centered")
5
+
6
+ # ----------------------------
7
+ # Helpers
8
+ # ----------------------------
9
+ G_SI = 9.80665 # m/s^2
10
+ RHO_WATER_4C = 1000.0 # kg/m^3 (reference for SG)
11
+
12
+ FLOW_UNITS = {
13
+ "m³/s": 1.0,
14
+ "L/s": 1e-3,
15
+ "L/min": 1e-3 / 60.0,
16
+ "m³/h": 1.0 / 3600.0,
17
+ "gpm (US)": 0.003785411784 / 60.0, # 1 US gallon = 3.785411784 L
18
+ "ft³/s": 0.028316846592,
19
+ }
20
+
21
+ HEAD_UNITS = {
22
+ "m": 1.0,
23
+ "ft": 0.3048,
24
+ }
25
+
26
+ EFF_UNITS = {
27
+ "%": 0.01,
28
+ "fraction": 1.0,
29
+ }
30
+
31
+ def to_si_flow(value, unit):
32
+ return value * FLOW_UNITS[unit]
33
+
34
+ def to_si_head(value, unit):
35
+ return value * HEAD_UNITS[unit]
36
+
37
+ def to_fraction(value, unit):
38
+ return max(1e-6, min(0.999999, value * EFF_UNITS[unit]))
39
+
40
+ def sg_to_density(sg):
41
+ return max(1e-6, sg) * RHO_WATER_4C
42
+
43
+ def hydraulic_power_kw(rho, g, Q, H):
44
+ # P_h = rho * g * Q * H [W]
45
+ return (rho * g * Q * H) / 1000.0
46
+
47
+ def shaft_power_kw(P_h_kw, eff_frac):
48
+ return P_h_kw / eff_frac
49
+
50
+ def kw_to_hp(kw):
51
+ return kw * 1.34102209 # metric HP
52
+
53
+ # ----------------------------
54
+ # UI
55
+ # ----------------------------
56
+ st.title("💧 Pump Power Calculator")
57
+ st.caption("Compute hydraulic and shaft power for a centrifugal/rotodynamic pump. Supports SI & US units, any fluid via specific gravity.")
58
+
59
+ with st.sidebar:
60
+ st.header("Inputs")
61
+ st.markdown("**Flow rate**")
62
+ Q_val = st.number_input("Flow value", min_value=0.0, value=0.05, step=0.01)
63
+ Q_unit = st.selectbox("Flow unit", list(FLOW_UNITS.keys()), index=0)
64
+
65
+ st.markdown("**Total dynamic head (TDH)**")
66
+ H_val = st.number_input("Head value", min_value=0.0, value=20.0, step=0.5)
67
+ H_unit = st.selectbox("Head unit", list(HEAD_UNITS.keys()), index=0)
68
+
69
+ st.markdown("**Fluid**")
70
+ sg = st.number_input("Specific Gravity (SG)", min_value=0.0, value=1.0, step=0.05,
71
+ help="SG = ρ_fluid / ρ_water. Water ≈ 1.00, seawater ≈ 1.025, oils < 1, etc.")
72
+ rho_override = st.checkbox("Override with custom density (kg/m³)")
73
+ rho_custom = st.number_input("Custom density (kg/m³)", min_value=0.0, value=1000.0, step=10.0, disabled=not rho_override)
74
+
75
+ st.markdown("**Pump/Drive**")
76
+ eff_val = st.number_input("Pump efficiency", min_value=0.0, value=70.0, step=1.0)
77
+ eff_unit = st.selectbox("Efficiency unit", ["%", "fraction"], index=0)
78
+
79
+ st.markdown("---")
80
+ st.markdown("**Advanced**")
81
+ g_val = st.number_input("Gravity g (m/s²)", min_value=0.0, value=G_SI, step=0.001,
82
+ help="Use 9.81 m/s² for standard Earth gravity.")
83
+ show_steps = st.checkbox("Show calculation steps", value=True)
84
+
85
+ # Convert inputs to SI
86
+ Q_si = to_si_flow(Q_val, Q_unit) # m³/s
87
+ H_si = to_si_head(H_val, H_unit) # m
88
+ eta = to_fraction(eff_val, eff_unit) # fraction
89
+ rho = rho_custom if rho_override else sg_to_density(sg)
90
+
91
+ # Calculations
92
+ P_h_kw = hydraulic_power_kw(rho, g_val, Q_si, H_si)
93
+ P_shaft_kw = shaft_power_kw(P_h_kw, eta)
94
+ P_h_hp = kw_to_hp(P_h_kw)
95
+ P_shaft_hp = kw_to_hp(P_shaft_kw)
96
+
97
+ # Output
98
+ st.subheader("Results")
99
+ c1, c2 = st.columns(2)
100
+ with c1:
101
+ st.metric("Hydraulic Power", f"{P_h_kw:,.3f} kW", help="Power imparted to the fluid.")
102
+ st.metric("Hydraulic Power", f"{P_h_hp:,.3f} HP")
103
+ with c2:
104
+ st.metric("Shaft Power", f"{P_shaft_kw:,.3f} kW", help="Power at the pump shaft (before motor/drive losses).")
105
+ st.metric("Shaft Power", f"{P_shaft_hp:,.3f} HP")
106
+
107
+ st.divider()
108
+
109
+ # Quick notes
110
+ with st.expander("Assumptions & Notes", expanded=False):
111
+ st.markdown(
112
+ """
113
+ - **Formulae**
114
+ - Hydraulic power: \n
115
+ \\(P_{hyd} = \\rho\\, g\\, Q\\, H\\)
116
+ - Shaft power: \n
117
+ \\(P_{shaft} = \\dfrac{P_{hyd}}{\\eta}\\)
118
+ - **Units**: Internally, the app converts flow to m³/s and head to meters.
119
+ - **Specific Gravity (SG)** converts to density by \\(\\rho = SG \\cdot 1000\\,\\text{kg/m³}\\).
120
+ - **Efficiency** is the pump hydraulic efficiency (not motor efficiency).
121
+ - For a **motor** selection, you may want to add motor efficiency and a service factor.
122
+ """
123
+ )
124
+
125
+ if show_steps:
126
+ st.subheader("Calculation Steps")
127
+ st.latex(r"Q_{SI} = Q \times \text{(flow unit factor)} \quad [\text{m}^3/\text{s}]")
128
+ st.latex(r"H_{SI} = H \times \text{(head unit factor)} \quad [\text{m}]")
129
+ st.latex(r"\rho = \begin{cases}"
130
+ r"\rho_{\text{custom}} & \text{if custom density is used} \\"
131
+ r"\text{SG} \times 1000 & \text{otherwise}"
132
+ r"\end{cases}")
133
+ st.latex(r"P_{hyd} = \rho \, g \, Q_{SI} \, H_{SI} \quad [\text{W}]")
134
+ st.latex(r"P_{shaft} = \dfrac{P_{hyd}}{\eta}")
135
+ with st.container(border=True):
136
+ st.code(
137
+ f"""Given:
138
+ Flow = {Q_val} {Q_unit} -> Q_SI = {Q_si:.6f} m³/s
139
+ Head = {H_val} {H_unit} -> H_SI = {H_si:.6f} m
140
+ Specific Gravity = {sg:.4f}
141
+ Density used = {rho:.2f} kg/m³ (custom override = {rho_override})
142
+ g = {g_val} m/s²
143
+ Efficiency = {eff_val} {eff_unit} -> η = {eta:.6f}
144
+
145
+ Computed:
146
+ Hydraulic Power = ρ·g·Q·H = {P_h_kw:.6f} kW = {P_h_hp:.6f} HP
147
+ Shaft Power = P_h / η = {P_shaft_kw:.6f} kW = {P_shaft_hp:.6f} HP
148
+ """,
149
+ language="text",
150
+ )
151
+
152
+ st.divider()
153
+ st.caption("© Pump Power Calculator • Streamlit • No external APIs • Suitable for Hugging Face Spaces")