calcs/steel-beam/test_steel_beam.py
smillmorel ddd5ecff58 Drop unit suffixes from steel-beam variables and results
Rename calc.py locals and result keys, add inline unit comments, and
normalize Lp/Lr/Lb to feet. Update steel-beam.typ references and the
tests (current W10X15 input, --stdout summary behavior).
2026-09-21 20:11:51 -04:00

92 lines
3.2 KiB
Python

from pathlib import Path
import importlib.util
import json
import subprocess
import sys
import pytest
import yaml
HERE = Path(__file__).resolve().parent
spec = importlib.util.spec_from_file_location("steel_beam_calc", HERE / "calc.py")
assert spec is not None and spec.loader is not None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
def result():
with (HERE / "input.yaml").open(encoding="utf-8") as handle:
return module.compute(yaml.safe_load(handle))
def test_typst_load_demands_match_checked_inputs():
subprocess.run([sys.executable, str(HERE / "calc.py")], check=True, cwd=HERE.parents[1])
query = subprocess.run(
[
"typst", "eval", "query(<load-demands>)", "--root", ".",
"--in", "calcs/steel-beam/steel-beam.typ", "--format", "json",
],
check=True,
capture_output=True,
text=True,
cwd=HERE.parents[1],
)
published = json.loads(query.stdout)
assert len(published) == 1
derived = published[0]["value"]
values = result()["values"]
assert derived["Mu"] == pytest.approx(values["moment"], abs=0.001)
assert derived["Vu"] == pytest.approx(values["shear"], abs=0.001)
def test_section_properties():
values = result()["values"]
assert values["section_d"] == pytest.approx(10.0)
assert values["section_A"] == pytest.approx(4.41)
assert values["section_Sx"] == pytest.approx(13.8)
assert values["section_Zx"] == pytest.approx(16.0)
assert values["section_rts"] == pytest.approx(1.01)
def test_demands_and_flexure():
output = result()
values = output["values"]
assert values["moment"] == pytest.approx(8.124)
assert values["shear"] == pytest.approx(3.611)
assert values["Lp"] == pytest.approx(2.861, abs=0.01)
assert values["Lr"] == pytest.approx(8.609, abs=0.01)
assert values["Lb"] == pytest.approx(9.0)
assert values["Fcr"] == pytest.approx(32.556, abs=0.01)
assert values["Mp"] == pytest.approx(66.667, abs=0.01)
assert values["MnLTB"] == pytest.approx(37.439, abs=0.01)
assert values["phiMn"] == pytest.approx(33.695, abs=0.01)
assert output["checks"]["flexure"]["ok"] is True
def test_shear_and_deflection():
output = result()
values = output["values"]
assert values["Aw"] == pytest.approx(2.5)
assert values["Cv1"] == pytest.approx(1)
assert values["phiVn"] == pytest.approx(75.0)
assert values["delta_limit"] == pytest.approx(0.45)
assert values["delta"] == pytest.approx(0.040559, abs=1e-5)
assert all(output["checks"][name]["ok"] for name in ("shear", "deflection"))
def test_invalid_section_is_rejected():
with pytest.raises(ValueError, match="not found"):
module.compute({**yaml.safe_load((HERE / "input.yaml").read_text()), "section": "W0X0"})
def test_cli_stdout_prints_summary():
completed = subprocess.run(
[sys.executable, str(HERE / "calc.py"), "--input", str(HERE / "input.yaml"), "--stdout"],
check=True,
capture_output=True,
text=True,
)
assert completed.stderr == ""
assert "Steel Beam Design Summary" in completed.stdout
assert "Section: W10X15" in completed.stdout
assert "Flexure: OK" in completed.stdout