Skip to content

About

Worst-case, RSS and Monte Carlo tolerance stack-up analysis for 1D assemblies. Pure Python, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tolerance-stackup

Worst-case, RSS, and Monte Carlo tolerance analysis for 1D assemblies — from the command line, with no dependencies.

A tolerance stack-up answers one question: given a chain of toleranced dimensions that closes a gap, will the assembly still function? Every CAD package can do this. What they usually can't tell you is the difference between the arithmetic envelope and the statistical one, which is often the difference between scrapping a design and shipping it.

$ python3 scripts/stackup.py --demo tight
Stack-up: Nine-plate stack
nominal gap:            +0.2500
worst case (arithmetic): -0.2500  to  +0.7500   (envelope 1.0000)
RSS (k=3.0, shift=1.00):  +0.0919  to  +0.4081   (sigma 0.0527)
Monte Carlo (200000 draws):  mean +0.2501  sigma 0.0527
  0.135% +0.0893 | 2.5% +0.1470 | 50% +0.2500 | 97.5% +0.3535 | 99.865% +0.4087
limits: min 0.0  max inf
worst case passes: NO
RSS passes:        yes
predicted yield:   100.0000%  (0.0 parts per million out of spec)

Nine plates at ±0.05 mm and a spacer: the arithmetic worst case says the gap can close to −0.25 mm and the design is scrap. Because variances add in quadrature, the real stack has σ = 0.053 mm, so a 0.25 mm nominal gap is more than four sigma of margin. This is the classic argument for statistical tolerancing, and the tool exists mostly to make it quickly.

Usage

python3 scripts/stackup.py assembly.json
python3 scripts/stackup.py assembly.json --samples 500000 --json
python3 scripts/stackup.py --demo bearing           # a passing design
python3 scripts/stackup.py --demo tight             # worst case fails, RSS passes

Options worth knowing:

flag why
--shift 1.5 keeps a mean-shift guard for a process whose mean wanders off nominal
--k 3 how many sigma one tolerance represents
--seed reproduces a Monte Carlo run exactly, for a report
--json machine-readable, for a design spreadsheet or CI

Exit code is 0 when the RSS band meets the drawing limits, 1 otherwise, so a stack-up can gate a build.

Input

{
  "name": "Bearing shoulder clearance",
  "unit": "mm",
  "limit": { "min": 0.00, "max": 0.30 },
  "dimensions": [
    { "name": "housing bore depth", "nominal": 12.00, "tol_plus": 0.05, "tol_minus": 0.05, "sign":  1 },
    { "name": "bearing width",      "nominal":  7.00, "tol_plus": 0.02, "tol_minus": 0.02, "sign": -1 },
    { "name": "shim thickness",     "nominal":  4.85, "tol_plus": 0.03, "tol_minus": 0.03, "sign": -1 }
  ]
}

tol_plus and tol_minus are independent, so asymmetric tolerances work. distribution per dimension is normal (default) or uniform — use uniform for a contributor with a hard manufacturing limit, where a normal tail would put mass on physically impossible values. Both are validated; a negative tolerance or a rating outside 1–5 is an error rather than a silent wrong answer.

Use it as a Claude Code skill

The repo root is the skill (SKILL.md), so one clone installs it:

git clone https://github.com/aditshank/tolerance-stackup.git ~/.claude/skills/tolerance-stackup

Claude then loads it whenever a tolerance, clearance, or stack-up question comes up, and follows the rules in SKILL.md — compare worst case with RSS before changing anything, tighten the widest contributor first, and never report a pass/fail without the yield.

Tests

python3 -m unittest discover -s tests -v

17 tests: the analytic worst case and RSS are checked against hand calculations, Monte Carlo is verified to recover the predicted σ, the seeded RNG is confirmed reproducible, and both the "worst case fails / RSS passes" boundary and the exact-limit floating-point case are pinned down.

Notes

  • Standard library only. No numpy, no install step — it runs on any machine with Python 3.
  • RSS assumes independent, centered processes. Two contributors from the same machine or the same lot are correlated, and RSS will understate the spread.

About

Worst-case, RSS and Monte Carlo tolerance stack-up analysis for 1D assemblies. Pure Python, no dependencies.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages