Update app.py
Browse files
app.py
CHANGED
|
@@ -2,19 +2,15 @@
|
|
| 2 |
Tubesheet Thickness Calculator — ASME (with optional TEMA mode)
|
| 3 |
Single-file Streamlit app.
|
| 4 |
|
| 5 |
-
|
| 6 |
-
-
|
| 7 |
-
-
|
| 8 |
-
-
|
| 9 |
-
-
|
| 10 |
-
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
Notes:
|
| 14 |
-
- Bundled material allowable stresses are illustrative. Verify with ASME Section II for production.
|
| 15 |
-
- TEMA uses illustrative modifiers — consult TEMA documentation for strict compliance.
|
| 16 |
Author: ChatGPT
|
| 17 |
-
Updated: 2025-09-07
|
| 18 |
"""
|
| 19 |
|
| 20 |
import streamlit as st
|
|
@@ -29,8 +25,8 @@ st.set_page_config(page_title="Tubesheet Thickness Calculator", layout="wide")
|
|
| 29 |
st.title("Tubesheet Thickness Calculator — ASME VIII-1 (Preliminary)")
|
| 30 |
|
| 31 |
# -----------------------
|
| 32 |
-
#
|
| 33 |
-
#
|
| 34 |
MATERIAL_LOOKUP = {
|
| 35 |
"SA-516 Gr 70": [
|
| 36 |
{"temperature_C": 20, "allowable_MPa": 138},
|
|
@@ -67,93 +63,56 @@ MATERIAL_LOOKUP = {
|
|
| 67 |
# -----------------------
|
| 68 |
# Helper functions
|
| 69 |
# -----------------------
|
| 70 |
-
def mpato_psi(mpa):
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
def
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
def mpatobar(mpa):
|
| 80 |
-
return mpa / 0.1
|
| 81 |
-
|
| 82 |
-
def inchtomm(val_in):
|
| 83 |
-
return val_in * 25.4
|
| 84 |
-
|
| 85 |
-
def mmtoinch(val_mm):
|
| 86 |
-
return val_mm / 25.4
|
| 87 |
-
|
| 88 |
-
def round_up_standard_mm(val_mm):
|
| 89 |
-
# round up to nearest 1 mm
|
| 90 |
-
return math.ceil(val_mm)
|
| 91 |
-
|
| 92 |
def round_up_standard_in(val_in):
|
| 93 |
-
# round up to nearest 1/16 in
|
| 94 |
sixteenth = 1.0 / 16.0
|
| 95 |
-
|
| 96 |
-
return n * sixteenth
|
| 97 |
|
| 98 |
def interpolate_allowable(material, temp_C):
|
| 99 |
-
"""
|
| 100 |
-
Interpolate allowable stress from bundled table (MPa vs temp_C).
|
| 101 |
-
If material not in table -> return None.
|
| 102 |
-
"""
|
| 103 |
if material not in MATERIAL_LOOKUP:
|
| 104 |
return None
|
| 105 |
-
table = MATERIAL_LOOKUP[material]
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
svals = [r["allowable_MPa"] for r in table_sorted]
|
| 109 |
if temp_C <= temps[0]:
|
| 110 |
return svals[0]
|
| 111 |
if temp_C >= temps[-1]:
|
| 112 |
return svals[-1]
|
| 113 |
-
# linear interp
|
| 114 |
for i in range(len(temps)-1):
|
| 115 |
if temps[i] <= temp_C <= temps[i+1]:
|
| 116 |
-
t0, t1 = temps[i], temps[i+1]
|
| 117 |
-
s0
|
| 118 |
-
frac = (temp_C - t0) / (t1 - t0)
|
| 119 |
-
return s0 + frac * (s1 - s0)
|
| 120 |
return None
|
| 121 |
|
| 122 |
# -----------------------
|
| 123 |
-
# BWG table (
|
|
|
|
| 124 |
BWG_TO_INCH = {
|
| 125 |
-
"BWG 7":
|
| 126 |
-
"BWG
|
| 127 |
-
"BWG
|
| 128 |
-
"BWG
|
| 129 |
-
"BWG
|
| 130 |
-
"BWG
|
| 131 |
-
"BWG 13": 0.095,
|
| 132 |
-
"BWG 14": 0.083,
|
| 133 |
-
"BWG 15": 0.072,
|
| 134 |
-
"BWG 16": 0.065,
|
| 135 |
-
"BWG 17": 0.058,
|
| 136 |
-
"BWG 18": 0.049,
|
| 137 |
-
"BWG 19": 0.042,
|
| 138 |
-
"BWG 20": 0.035,
|
| 139 |
-
"BWG 21": 0.032,
|
| 140 |
-
"BWG 22": 0.028,
|
| 141 |
-
"BWG 23": 0.025,
|
| 142 |
-
"BWG 24": 0.023,
|
| 143 |
-
"BWG 25": 0.020,
|
| 144 |
-
"BWG 26": 0.018,
|
| 145 |
-
"BWG 27": 0.016,
|
| 146 |
-
"BWG 28": 0.014,
|
| 147 |
}
|
| 148 |
|
| 149 |
# -----------------------
|
| 150 |
-
# Sidebar
|
|
|
|
| 151 |
with st.sidebar:
|
| 152 |
st.header("Inputs")
|
| 153 |
project_name = st.text_input("Project name", value="Tubesheet Calculation")
|
| 154 |
designer_name = st.text_input("Designer", value="")
|
| 155 |
-
units = st.selectbox("Units system",
|
| 156 |
use_SI = units.startswith("SI")
|
|
|
|
| 157 |
if use_SI:
|
| 158 |
u_len = "mm"
|
| 159 |
u_press = "bar"
|
|
@@ -196,11 +155,13 @@ with st.sidebar:
|
|
| 196 |
tube_od_step = 1.0 / 64.0
|
| 197 |
tube_od_format = "%.6f"
|
| 198 |
|
| 199 |
-
OD_tube = st.number_input(
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
| 204 |
|
| 205 |
# --- Tube wall thickness input mode: BWG or manual ---
|
| 206 |
st.markdown("**Tube wall thickness input**")
|
|
@@ -221,29 +182,42 @@ with st.sidebar:
|
|
| 221 |
if use_SI:
|
| 222 |
t_tube_min = 0.1
|
| 223 |
t_tube_default = 1.0
|
| 224 |
-
t_tube = st.number_input(
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
|
|
|
|
|
|
| 229 |
else:
|
| 230 |
t_tube_min = 1.0 / 128.0
|
| 231 |
t_tube_default = 0.035
|
| 232 |
-
t_tube = st.number_input(
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 239 |
|
| 240 |
# default pitch based on OD_tube; user can edit
|
| 241 |
default_pitch = (OD_tube * 1.25) if (OD_tube is not None) else (25.4 * 1.25 if use_SI else 1.0 * 1.25)
|
| 242 |
-
pitch = st.number_input(
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
|
|
|
|
|
|
| 247 |
|
| 248 |
st.subheader("Pressure & Temperature")
|
| 249 |
if use_SI:
|
|
@@ -284,7 +258,6 @@ with st.sidebar:
|
|
| 284 |
st.write("No auto-lookup available for selected material & temperature in bundled table.")
|
| 285 |
|
| 286 |
S_override = st.checkbox("Manual override allowable stress")
|
| 287 |
-
# prepare both vars so later code can rely on them
|
| 288 |
S_allowable_manual = None
|
| 289 |
S_allowable_manual_psi = None
|
| 290 |
if S_override:
|
|
@@ -316,21 +289,20 @@ with st.sidebar:
|
|
| 316 |
compute = st.button("Compute")
|
| 317 |
|
| 318 |
# -----------------------
|
| 319 |
-
# Helper: generate tube
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
"""
|
| 328 |
centers = []
|
| 329 |
R = radius_mm
|
| 330 |
-
if pitch_mm <= 0:
|
| 331 |
return centers
|
| 332 |
|
| 333 |
-
if layout == "Square":
|
| 334 |
xs = np.arange(-R, R + 1e-8, pitch_mm)
|
| 335 |
ys = np.arange(-R, R + 1e-8, pitch_mm)
|
| 336 |
for x in xs:
|
|
@@ -339,29 +311,27 @@ def generate_tube_centers(radius_mm, pitch_mm, layout="Triangular", tube_OD_mm=0
|
|
| 339 |
centers.append((x, y))
|
| 340 |
if max_count and len(centers) >= max_count:
|
| 341 |
return centers
|
| 342 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 343 |
vert = pitch_mm * math.sqrt(3)/2.0
|
| 344 |
-
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
row_offsets.append(y_center)
|
| 349 |
-
step = vert
|
| 350 |
-
n = 1
|
| 351 |
-
while True:
|
| 352 |
-
y_up = n * step
|
| 353 |
-
y_down = -n * step
|
| 354 |
-
if abs(y_up) <= R or abs(y_down) <= R:
|
| 355 |
-
if abs(y_up) <= R:
|
| 356 |
-
row_offsets.append(y_up)
|
| 357 |
-
if abs(y_down) <= R:
|
| 358 |
-
row_offsets.append(y_down)
|
| 359 |
-
n += 1
|
| 360 |
-
else:
|
| 361 |
-
break
|
| 362 |
-
row_offsets = sorted(set(row_offsets))
|
| 363 |
-
for idx, y in enumerate(row_offsets):
|
| 364 |
-
if idx % 2 == 0:
|
| 365 |
xs = np.arange(-R, R + 1e-8, pitch_mm)
|
| 366 |
else:
|
| 367 |
xs = np.arange(-R + pitch_mm/2.0, R + 1e-8, pitch_mm)
|
|
@@ -370,10 +340,36 @@ def generate_tube_centers(radius_mm, pitch_mm, layout="Triangular", tube_OD_mm=0
|
|
| 370 |
centers.append((x, y))
|
| 371 |
if max_count and len(centers) >= max_count:
|
| 372 |
return centers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 373 |
return centers
|
| 374 |
|
| 375 |
# -----------------------
|
| 376 |
# Calculation logic
|
|
|
|
| 377 |
def compute_all():
|
| 378 |
# Unit conversions into consistent internal units:
|
| 379 |
# lengths in mm, pressure in MPa, stresses in MPa
|
|
@@ -418,14 +414,12 @@ def compute_all():
|
|
| 418 |
if use_SI:
|
| 419 |
S_allow_MPa = float(S_allowable_manual)
|
| 420 |
else:
|
| 421 |
-
# ensure psi->MPa conversion
|
| 422 |
S_allow_MPa = psito_mpa(float(S_allowable_manual_psi))
|
| 423 |
source_S = "Manual override"
|
| 424 |
else:
|
| 425 |
S_allow_MPa = S_allowable_auto if S_allowable_auto is not None else None
|
| 426 |
source_S = "Auto lookup (interpolated)" if S_allowable_auto is not None else "None available"
|
| 427 |
|
| 428 |
-
# fallback if none available
|
| 429 |
if S_allow_MPa is None:
|
| 430 |
S_allow_MPa = 100.0
|
| 431 |
source_S = "Default used (no lookup) - user must verify"
|
|
@@ -437,7 +431,7 @@ def compute_all():
|
|
| 437 |
else:
|
| 438 |
k = 0.375
|
| 439 |
else:
|
| 440 |
-
# TEMA illustrative modifiers
|
| 441 |
if edge_condition.startswith("Clamped"):
|
| 442 |
k = 0.27
|
| 443 |
else:
|
|
@@ -575,7 +569,6 @@ def compute_all():
|
|
| 575 |
csv_data = csv_buffer.getvalue().encode("utf-8")
|
| 576 |
|
| 577 |
# Convert centers into normalized plot coordinates for schematic: scale by a_mm (radius)
|
| 578 |
-
# Plot uses coordinates in range [-1,1]
|
| 579 |
plot_centers = [(x / a_mm, y / a_mm) for (x, y) in centers]
|
| 580 |
|
| 581 |
return results, log_text, summary_df, csv_data, plot_centers, OD_tube_mm, a_mm
|
|
@@ -610,18 +603,15 @@ if compute:
|
|
| 610 |
with right:
|
| 611 |
st.markdown("**Schematic (to scale in layout) — normalized to tubesheet radius**")
|
| 612 |
fig = go.Figure()
|
| 613 |
-
# outer circle (
|
| 614 |
-
theta = np.linspace(0, 2*np.pi,
|
| 615 |
fig.add_trace(go.Scatter(x=np.cos(theta), y=np.sin(theta), mode="lines", name="Tubesheet OD",
|
| 616 |
line=dict(width=2, color="RoyalBlue")))
|
| 617 |
-
# tubes as markers; scale marker size approx proportional to OD_tube_mm / a_mm
|
| 618 |
xs = [c[0] for c in plot_centers]
|
| 619 |
ys = [c[1] for c in plot_centers]
|
| 620 |
-
# compute
|
| 621 |
if a_mm > 0:
|
| 622 |
-
# fraction of normalized radius occupied by tube radius
|
| 623 |
frac = (OD_tube_mm / 2.0) / a_mm
|
| 624 |
-
# convert fraction to marker size (empirical scaling)
|
| 625 |
marker_size = max(4, min(40, frac * 800))
|
| 626 |
else:
|
| 627 |
marker_size = 6
|
|
@@ -631,6 +621,13 @@ if compute:
|
|
| 631 |
title_text="Tubesheet layout (normalized to radius = 1)")
|
| 632 |
st.plotly_chart(fig, use_container_width=True)
|
| 633 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
# Calculation log
|
| 635 |
if show_detail:
|
| 636 |
st.subheader("Detailed calculation log")
|
|
@@ -655,5 +652,4 @@ if st.sidebar.button("Example 1 (SI)"):
|
|
| 655 |
st.info("Preset example: OD 1000 mm, ~100 tubes, tube OD 25.4 mm, P_shell=3 bar, P_tube=1 bar — set values manually or copy/paste into fields.")
|
| 656 |
if st.sidebar.button("Example 2 (Imperial)"):
|
| 657 |
st.info("Preset example: OD 40 in, ~200 tubes, tube OD 0.75 in, P_shell=50 psi, P_tube=14.7 psi — set values manually or copy/paste into fields.")
|
| 658 |
-
|
| 659 |
# End of app
|
|
|
|
| 2 |
Tubesheet Thickness Calculator — ASME (with optional TEMA mode)
|
| 3 |
Single-file Streamlit app.
|
| 4 |
|
| 5 |
+
Improvements:
|
| 6 |
+
- Fixed minor bugs and syntax errors.
|
| 7 |
+
- Added additional layout options and improved schematic export safety.
|
| 8 |
+
- Shows allowable stress in selected units.
|
| 9 |
+
- Uses pitch/layout to place real tube centers (limited to user N_tubes).
|
| 10 |
+
- TEMA mode is illustrative only — verify before use.
|
| 11 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
Author: ChatGPT
|
| 13 |
+
Updated: 2025-09-07 (fixed & cleaned)
|
| 14 |
"""
|
| 15 |
|
| 16 |
import streamlit as st
|
|
|
|
| 25 |
st.title("Tubesheet Thickness Calculator — ASME VIII-1 (Preliminary)")
|
| 26 |
|
| 27 |
# -----------------------
|
| 28 |
+
# Material table (example values only — replace with ASME Section II verified data)
|
| 29 |
+
# -----------------------
|
| 30 |
MATERIAL_LOOKUP = {
|
| 31 |
"SA-516 Gr 70": [
|
| 32 |
{"temperature_C": 20, "allowable_MPa": 138},
|
|
|
|
| 63 |
# -----------------------
|
| 64 |
# Helper functions
|
| 65 |
# -----------------------
|
| 66 |
+
def mpato_psi(mpa): return mpa * 145.0377377
|
| 67 |
+
def psito_mpa(psi): return psi / 145.0377377
|
| 68 |
+
def bartompa(bar): return bar * 0.1
|
| 69 |
+
def mpatobar(mpa): return mpa / 0.1
|
| 70 |
+
def inchtomm(val_in): return val_in * 25.4
|
| 71 |
+
def mmtoinch(val_mm): return val_mm / 25.4
|
| 72 |
+
|
| 73 |
+
def round_up_standard_mm(val_mm): return math.ceil(val_mm)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
def round_up_standard_in(val_in):
|
|
|
|
| 75 |
sixteenth = 1.0 / 16.0
|
| 76 |
+
return math.ceil(val_in / sixteenth) * sixteenth
|
|
|
|
| 77 |
|
| 78 |
def interpolate_allowable(material, temp_C):
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
if material not in MATERIAL_LOOKUP:
|
| 80 |
return None
|
| 81 |
+
table = sorted(MATERIAL_LOOKUP[material], key=lambda x: x["temperature_C"])
|
| 82 |
+
temps = [r["temperature_C"] for r in table]
|
| 83 |
+
svals = [r["allowable_MPa"] for r in table]
|
|
|
|
| 84 |
if temp_C <= temps[0]:
|
| 85 |
return svals[0]
|
| 86 |
if temp_C >= temps[-1]:
|
| 87 |
return svals[-1]
|
|
|
|
| 88 |
for i in range(len(temps)-1):
|
| 89 |
if temps[i] <= temp_C <= temps[i+1]:
|
| 90 |
+
t0, t1, s0, s1 = temps[i], temps[i+1], svals[i], svals[i+1]
|
| 91 |
+
return s0 + (s1 - s0) * (temp_C - t0) / (t1 - t0)
|
|
|
|
|
|
|
| 92 |
return None
|
| 93 |
|
| 94 |
# -----------------------
|
| 95 |
+
# BWG thickness table (inches)
|
| 96 |
+
# -----------------------
|
| 97 |
BWG_TO_INCH = {
|
| 98 |
+
"BWG 7": 0.180, "BWG 8": 0.165, "BWG 9": 0.148, "BWG 10": 0.134,
|
| 99 |
+
"BWG 11": 0.120, "BWG 12": 0.109, "BWG 13": 0.095, "BWG 14": 0.083,
|
| 100 |
+
"BWG 15": 0.072, "BWG 16": 0.065, "BWG 17": 0.058, "BWG 18": 0.049,
|
| 101 |
+
"BWG 19": 0.042, "BWG 20": 0.035, "BWG 21": 0.032, "BWG 22": 0.028,
|
| 102 |
+
"BWG 23": 0.025, "BWG 24": 0.023, "BWG 25": 0.020, "BWG 26": 0.018,
|
| 103 |
+
"BWG 27": 0.016, "BWG 28": 0.014,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
}
|
| 105 |
|
| 106 |
# -----------------------
|
| 107 |
+
# Sidebar Inputs
|
| 108 |
+
# -----------------------
|
| 109 |
with st.sidebar:
|
| 110 |
st.header("Inputs")
|
| 111 |
project_name = st.text_input("Project name", value="Tubesheet Calculation")
|
| 112 |
designer_name = st.text_input("Designer", value="")
|
| 113 |
+
units = st.selectbox("Units system", ["SI (mm, bar, °C)", "Imperial (in, psi, °F)"])
|
| 114 |
use_SI = units.startswith("SI")
|
| 115 |
+
|
| 116 |
if use_SI:
|
| 117 |
u_len = "mm"
|
| 118 |
u_press = "bar"
|
|
|
|
| 155 |
tube_od_step = 1.0 / 64.0
|
| 156 |
tube_od_format = "%.6f"
|
| 157 |
|
| 158 |
+
OD_tube = st.number_input(
|
| 159 |
+
f"Tube outside diameter ({u_len})",
|
| 160 |
+
min_value=tube_od_min,
|
| 161 |
+
value=tube_od_default,
|
| 162 |
+
step=tube_od_step,
|
| 163 |
+
format=tube_od_format
|
| 164 |
+
)
|
| 165 |
|
| 166 |
# --- Tube wall thickness input mode: BWG or manual ---
|
| 167 |
st.markdown("**Tube wall thickness input**")
|
|
|
|
| 182 |
if use_SI:
|
| 183 |
t_tube_min = 0.1
|
| 184 |
t_tube_default = 1.0
|
| 185 |
+
t_tube = st.number_input(
|
| 186 |
+
f"Tube wall thickness ({u_len})",
|
| 187 |
+
min_value=t_tube_min,
|
| 188 |
+
value=t_tube_default,
|
| 189 |
+
step=0.01,
|
| 190 |
+
format="%.3f"
|
| 191 |
+
)
|
| 192 |
else:
|
| 193 |
t_tube_min = 1.0 / 128.0
|
| 194 |
t_tube_default = 0.035
|
| 195 |
+
t_tube = st.number_input(
|
| 196 |
+
f"Tube wall thickness ({u_len})",
|
| 197 |
+
min_value=t_tube_min,
|
| 198 |
+
value=t_tube_default,
|
| 199 |
+
step=1.0/128.0,
|
| 200 |
+
format="%.6f"
|
| 201 |
+
)
|
| 202 |
+
|
| 203 |
+
# New: more layout options
|
| 204 |
+
layout = st.selectbox("Tube pitch layout", options=[
|
| 205 |
+
"Triangular (hex-packed)",
|
| 206 |
+
"Square (inline)",
|
| 207 |
+
"Rotated Square (45°)",
|
| 208 |
+
"Hexagonal grid",
|
| 209 |
+
"Concentric rings"
|
| 210 |
+
])
|
| 211 |
|
| 212 |
# default pitch based on OD_tube; user can edit
|
| 213 |
default_pitch = (OD_tube * 1.25) if (OD_tube is not None) else (25.4 * 1.25 if use_SI else 1.0 * 1.25)
|
| 214 |
+
pitch = st.number_input(
|
| 215 |
+
f"Tube pitch ({u_len})",
|
| 216 |
+
min_value=max(OD_tube * 1.0, tube_od_min),
|
| 217 |
+
value=default_pitch,
|
| 218 |
+
step=tube_od_step,
|
| 219 |
+
format=tube_od_format
|
| 220 |
+
)
|
| 221 |
|
| 222 |
st.subheader("Pressure & Temperature")
|
| 223 |
if use_SI:
|
|
|
|
| 258 |
st.write("No auto-lookup available for selected material & temperature in bundled table.")
|
| 259 |
|
| 260 |
S_override = st.checkbox("Manual override allowable stress")
|
|
|
|
| 261 |
S_allowable_manual = None
|
| 262 |
S_allowable_manual_psi = None
|
| 263 |
if S_override:
|
|
|
|
| 289 |
compute = st.button("Compute")
|
| 290 |
|
| 291 |
# -----------------------
|
| 292 |
+
# Helper: generate tube grids with more layout options
|
| 293 |
+
# -----------------------
|
| 294 |
+
def rotate_points(points, angle_rad):
|
| 295 |
+
c = math.cos(angle_rad)
|
| 296 |
+
s = math.sin(angle_rad)
|
| 297 |
+
return [(c*x - s*y, s*x + c*y) for (x, y) in points]
|
| 298 |
+
|
| 299 |
+
def generate_tube_centers(radius_mm, pitch_mm, layout="Triangular (hex-packed)", tube_OD_mm=0.0, max_count=None):
|
|
|
|
| 300 |
centers = []
|
| 301 |
R = radius_mm
|
| 302 |
+
if pitch_mm <= 0 or R <= 0:
|
| 303 |
return centers
|
| 304 |
|
| 305 |
+
if layout == "Square (inline)":
|
| 306 |
xs = np.arange(-R, R + 1e-8, pitch_mm)
|
| 307 |
ys = np.arange(-R, R + 1e-8, pitch_mm)
|
| 308 |
for x in xs:
|
|
|
|
| 311 |
centers.append((x, y))
|
| 312 |
if max_count and len(centers) >= max_count:
|
| 313 |
return centers
|
| 314 |
+
|
| 315 |
+
elif layout == "Rotated Square (45°)":
|
| 316 |
+
xs = np.arange(-R * math.sqrt(2), R * math.sqrt(2) + 1e-8, pitch_mm)
|
| 317 |
+
ys = np.arange(-R * math.sqrt(2), R * math.sqrt(2) + 1e-8, pitch_mm)
|
| 318 |
+
temp = []
|
| 319 |
+
for x in xs:
|
| 320 |
+
for y in ys:
|
| 321 |
+
temp.append((x, y))
|
| 322 |
+
temp_rot = rotate_points(temp, math.radians(45))
|
| 323 |
+
for (x, y) in temp_rot:
|
| 324 |
+
if x**2 + y**2 <= (R - tube_OD_mm/2.0)**2:
|
| 325 |
+
centers.append((x, y))
|
| 326 |
+
if max_count and len(centers) >= max_count:
|
| 327 |
+
return centers
|
| 328 |
+
|
| 329 |
+
elif layout == "Triangular (hex-packed)" or layout == "Hexagonal grid":
|
| 330 |
vert = pitch_mm * math.sqrt(3)/2.0
|
| 331 |
+
row_idx = 0
|
| 332 |
+
y = -R
|
| 333 |
+
while y <= R:
|
| 334 |
+
if row_idx % 2 == 0:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 335 |
xs = np.arange(-R, R + 1e-8, pitch_mm)
|
| 336 |
else:
|
| 337 |
xs = np.arange(-R + pitch_mm/2.0, R + 1e-8, pitch_mm)
|
|
|
|
| 340 |
centers.append((x, y))
|
| 341 |
if max_count and len(centers) >= max_count:
|
| 342 |
return centers
|
| 343 |
+
row_idx += 1
|
| 344 |
+
y += vert
|
| 345 |
+
|
| 346 |
+
elif layout == "Concentric rings":
|
| 347 |
+
tube_r = tube_OD_mm / 2.0
|
| 348 |
+
if 0 <= (R - tube_r):
|
| 349 |
+
centers.append((0.0, 0.0))
|
| 350 |
+
ring = 1
|
| 351 |
+
while True:
|
| 352 |
+
r_ring = ring * pitch_mm
|
| 353 |
+
if r_ring > (R - tube_r):
|
| 354 |
+
break
|
| 355 |
+
n_on_ring = max(1, int(math.floor(2 * math.pi * r_ring / pitch_mm + 0.5)))
|
| 356 |
+
for i in range(n_on_ring):
|
| 357 |
+
ang = 2 * math.pi * i / n_on_ring
|
| 358 |
+
x = r_ring * math.cos(ang)
|
| 359 |
+
y = r_ring * math.sin(ang)
|
| 360 |
+
if x**2 + y**2 <= (R - tube_r)**2:
|
| 361 |
+
centers.append((x, y))
|
| 362 |
+
if max_count and len(centers) >= max_count:
|
| 363 |
+
return centers
|
| 364 |
+
ring += 1
|
| 365 |
+
else:
|
| 366 |
+
return generate_tube_centers(radius_mm, pitch_mm, layout="Triangular (hex-packed)", tube_OD_mm=tube_OD_mm, max_count=max_count)
|
| 367 |
+
|
| 368 |
return centers
|
| 369 |
|
| 370 |
# -----------------------
|
| 371 |
# Calculation logic
|
| 372 |
+
# -----------------------
|
| 373 |
def compute_all():
|
| 374 |
# Unit conversions into consistent internal units:
|
| 375 |
# lengths in mm, pressure in MPa, stresses in MPa
|
|
|
|
| 414 |
if use_SI:
|
| 415 |
S_allow_MPa = float(S_allowable_manual)
|
| 416 |
else:
|
|
|
|
| 417 |
S_allow_MPa = psito_mpa(float(S_allowable_manual_psi))
|
| 418 |
source_S = "Manual override"
|
| 419 |
else:
|
| 420 |
S_allow_MPa = S_allowable_auto if S_allowable_auto is not None else None
|
| 421 |
source_S = "Auto lookup (interpolated)" if S_allowable_auto is not None else "None available"
|
| 422 |
|
|
|
|
| 423 |
if S_allow_MPa is None:
|
| 424 |
S_allow_MPa = 100.0
|
| 425 |
source_S = "Default used (no lookup) - user must verify"
|
|
|
|
| 431 |
else:
|
| 432 |
k = 0.375
|
| 433 |
else:
|
| 434 |
+
# TEMA illustrative modifiers
|
| 435 |
if edge_condition.startswith("Clamped"):
|
| 436 |
k = 0.27
|
| 437 |
else:
|
|
|
|
| 569 |
csv_data = csv_buffer.getvalue().encode("utf-8")
|
| 570 |
|
| 571 |
# Convert centers into normalized plot coordinates for schematic: scale by a_mm (radius)
|
|
|
|
| 572 |
plot_centers = [(x / a_mm, y / a_mm) for (x, y) in centers]
|
| 573 |
|
| 574 |
return results, log_text, summary_df, csv_data, plot_centers, OD_tube_mm, a_mm
|
|
|
|
| 603 |
with right:
|
| 604 |
st.markdown("**Schematic (to scale in layout) — normalized to tubesheet radius**")
|
| 605 |
fig = go.Figure()
|
| 606 |
+
# outer circle (normalized)
|
| 607 |
+
theta = np.linspace(0, 2*np.pi, 400)
|
| 608 |
fig.add_trace(go.Scatter(x=np.cos(theta), y=np.sin(theta), mode="lines", name="Tubesheet OD",
|
| 609 |
line=dict(width=2, color="RoyalBlue")))
|
|
|
|
| 610 |
xs = [c[0] for c in plot_centers]
|
| 611 |
ys = [c[1] for c in plot_centers]
|
| 612 |
+
# compute marker size from tube OD and radius (empirical)
|
| 613 |
if a_mm > 0:
|
|
|
|
| 614 |
frac = (OD_tube_mm / 2.0) / a_mm
|
|
|
|
| 615 |
marker_size = max(4, min(40, frac * 800))
|
| 616 |
else:
|
| 617 |
marker_size = 6
|
|
|
|
| 621 |
title_text="Tubesheet layout (normalized to radius = 1)")
|
| 622 |
st.plotly_chart(fig, use_container_width=True)
|
| 623 |
|
| 624 |
+
# export PNG option (safe: try/except in case kaleido not installed)
|
| 625 |
+
try:
|
| 626 |
+
img_bytes = fig.to_image(format="png", width=1000, height=1000, scale=1)
|
| 627 |
+
st.download_button("Download schematic PNG", data=img_bytes, file_name="tubesheet_layout.png", mime="image/png")
|
| 628 |
+
except Exception as e:
|
| 629 |
+
st.info("PNG export requires 'kaleido'. If you want PNG export, install 'kaleido' (see requirements.txt).")
|
| 630 |
+
|
| 631 |
# Calculation log
|
| 632 |
if show_detail:
|
| 633 |
st.subheader("Detailed calculation log")
|
|
|
|
| 652 |
st.info("Preset example: OD 1000 mm, ~100 tubes, tube OD 25.4 mm, P_shell=3 bar, P_tube=1 bar — set values manually or copy/paste into fields.")
|
| 653 |
if st.sidebar.button("Example 2 (Imperial)"):
|
| 654 |
st.info("Preset example: OD 40 in, ~200 tubes, tube OD 0.75 in, P_shell=50 psi, P_tube=14.7 psi — set values manually or copy/paste into fields.")
|
|
|
|
| 655 |
# End of app
|