# QMD calculation reports

> Write engineering calculations as Quarto .qmd reports where Python computes every number. Use for technical calculations.

Write the calculation as a [Quarto](https://quarto.org) `.qmd` report: prose, equations and clause references around Python code that computes every number. Quarto runs the code when it renders, so the same inputs always give the same result, and a reviewer can check every input, formula and result in the report.

## When to use it

- For any technical calculations

## Rules

- **Python computes every number.** Never type a calculated value into the prose. Print it from the code.
- **Every input has a value, a unit and a source**, e.g. a drawing, line list, survey or table in the standard.
- **Every formula cites its offical source**.
- **The author is responsible** The responsible person reviews it and signs it off and takes ownership over the content and outcome.

## Setup

1. Check what's already installed:

```bash
quarto --version
quarto check jupyter
python3 -c "import handcalcs"
```

2. Install anything missing:

- **Quarto**: download it from [quarto.org](https://quarto.org/docs/get-started/).
- **Jupyter and handcalcs**: `python3 -m pip install jupyter handcalcs`
- **LaTeX**, for PDF output only: `quarto install tinytex`

## Workflow

1. **Write the report**: as instructed by the user.
2. **Render it**: `quarto render <calc-name>.qmd --to docx` (or `--to pdf`). Confirm the format with the user.
3. **Check it**: check for render errors and review the entire contents of the report for correctness. Fix any issues identified.

## Golden Reference

An ASME B31.3 minimum wall thickness check.

````markdown
---
title: "ASME B31.3 minimum wall thickness"
subtitle: "NPS 6 carbon steel process line, UT survey 2026"
execute:
  echo: false
  output: asis
---

```{python}
#| include: false
from handcalcs.decorator import handcalc

# Every input with its unit and source, so the reviewer can check it without asking.
inputs = {
    "P": (8.0, "MPa", "Line list, design pressure"),
    "T": (150, "°C", "Line list, design temperature"),
    "D": (168.3, "mm", "NPS 6 outside diameter, ASME B36.10M"),
    "S": (138.0, "MPa", "B31.3 Table A-1M, A106 Gr B at 150 °C"),
    "E": (1.0, "", "B31.3 Table A-1B, seamless"),
    "W": (1.0, "", "B31.3 para. 302.3.5, below 427 °C"),
    "Y": (0.4, "", "B31.3 Table 304.1.1, ferritic below 482 °C"),
    "c": (1.5, "mm", "Piping class corrosion allowance"),
    "t_meas": (5.8, "mm", "UT survey 2026, minimum reading"),
}
globals().update({name: value for name, (value, unit, source) in inputs.items()})
```

## Inputs

```{python}
print("")
print("| Input | Value | Unit | Source |")
print("|:---|---:|:---|:---|")
for name, (value, unit, source) in inputs.items():
    print(f"| `{name}` | {value} | {unit} | {source} |")
print("")
```

## Pressure design thickness (B31.3 para. 304.1.2)

Valid for straight pipe where $t < D/6$.

```{python}
@handcalc(override="long", jupyter_display=False, precision=2)
def _thickness(P=P, D=D, S=S, E=E, W=W, Y=Y, c=c):
    t = P * D / (2 * (S * E * W + P * Y))  # pressure design thickness, mm
    t_m = t + c  # minimum required thickness, mm
    return locals()

_latex, _vars = _thickness()
print(_latex)
globals().update(_vars)
assert t < D / 6, "para. 304.1.2 only applies where t < D/6"
```

## Result

```{python}
checks = [
    ("Pressure design thickness, $t$", "304.1.2", f"{t:.2f} mm", f"{t_meas:.2f} mm", t / t_meas),
    ("Minimum required thickness, $t_m = t + c$", "304.1.2", f"{t_m:.2f} mm", f"{t_meas:.2f} mm", t_m / t_meas),
]

print("")
print("| Check | Clause | Required | Actual | Utilisation | Result |")
print("|:---|:---|---:|---:|---:|:---|")
for check, clause, required, actual, utilisation in checks:
    result = "PASS" if utilisation <= 1 else "**FAIL**"
    print(f"| {check} | {clause} | {required} | {actual} | {utilisation:.2f} | {result} |")
print("")
```
````
