moonbitTolerance

Deterministic mechanical tolerance stack-up analysis for MoonBit.

tolerance
mechanical
dimension-chain
engineering
moon add gckbbrant/moonbitTolerance@0.2.0
Download zip
Author
Version
0.2.0
License
Apache-2.0
Last updated
19 hours ago
Downloads
3
README

#moonbitTolerance

Deterministic mechanical tolerance stack-up analysis for MoonBit. The library is a reusable calculation kernel for CAD-adjacent tools, process planning, inspection software, and manufacturing data pipelines.

#Project positioning

moonbitTolerance keeps geometry, statistical assumptions, constraint evaluation, and reporting separate from any CAD file format. It is suitable when a host application needs reproducible one-dimensional or projected two-dimensional tolerance calculations with explicit assumptions.

#Core capabilities

  • Signed one-dimensional dimension chains with worst-case, RSS, and seeded Monte Carlo analysis.
  • Two-dimensional vector dimensions and projected stack-up intervals.
  • Uniform, triangular, and bounded normal-approximation sampling policies.
  • Process capability indices (Cp, Cpu, Cpl, and Cpk).
  • Gap constraints with satisfied, violated, and inconclusive outcomes.
  • CSV result export for small downstream reporting adapters.
  • Reusable process profiles, calibration points, and deterministic regression vectors.
  • Native CLI demonstration and benchmark suite.

#Quick start

Requirements: MoonBit stable toolchain and a native-capable host for the CLI.

moon test --target wasm-gc --deny-warn moon run cmd/main

let chain = @moonbitTolerance.Chain::new("shaft", [
@moonbitTolerance.Dimension::new("housing", 20.0, 0.05),
@moonbitTolerance.Dimension::new(
"cover", 0.2, 0.02,
direction=@moonbitTolerance.negative_direction(),
),
])
let result = chain.monte_carlo(10000, seed=42U)

#CLI

moon run cmd/main prints a small RSS analysis and runs the deterministic benchmark suite. The CLI is intentionally a host-integration example; applications should import the root package and keep their own I/O boundary.

#Architecture

The root package owns the public engineering types. tolerance.mbt contains the one-dimensional kernel; geometry.mbt contains vector projection; distributions.mbt contains sampling and capability calculations; constraints.mbt contains interval checks and CSV output. The catalog files contain structured, inspectable reference inputs rather than hidden runtime state. cmd/main is an executable consumer of the package.

#Benchmark

The benchmark suite uses the same public API as an application: five chains with 4, 8, 16, 32, and 64 contributors, each sampled with an explicit seed. Run it with:

moon run cmd/main

The output is deterministic for a fixed toolchain, sample count, and seed. The repository does not claim a hardware-independent throughput number; wall-clock measurements belong to the machine and backend that produced them. The checked-in reference vector corpus is a deterministic regression fixture, not a measurement claim.

#Testing

moon fmt --check moon check --deny-warn moon test --target wasm-gc --deny-warn moon test --target native --deny-warn moon info

Tests cover invalid inputs, signed dimensions, deterministic sampling, projection boundaries, process capability edges, constraints, calibration correction, and reference corpus shape.

#CI

GitHub Actions runs formatting, warning-free type checking, generated-interface drift detection, coverage analysis, wasm-gc tests, and native tests on Linux, macOS, and Windows. The workflow installs the current MoonBit stable toolchain using the official installer and keeps the generated interface under review.

#License

Apache-2.0. See LICENSE.

#Development

See CONTRIBUTING.md for the local validation loop and docs/assumptions.md for modeling assumptions.

#
AnalysisResult

pub struct AnalysisResult {
nominal : Double
lower : Double
upper : Double
mean : Double
standard_deviation : Double
yield_rate : Double
sensitivity : Array[(String, Double)]
} derive(
Debug
)

#
BenchmarkCase

pub struct BenchmarkCase {
name : String
dimensions : Int
tolerance : Double
seed : UInt
} derive(
Debug
)

#
BenchmarkCase::chain

fn BenchmarkCase::chain(self : BenchmarkCase) -> Chain

#
CalibrationPoint

pub struct CalibrationPoint {
instrument : String
axis : String
nominal : Double
bias : Double
repeatability : Double
confidence : Double
} derive(
Debug
)

#
Chain

pub struct Chain {
name : String
dimensions : Array[Dimension]
} derive(
Debug
)

#
Chain::monte_carlo

fn Chain::monte_carlo(self : Chain, samples : Int, seed? : UInt) -> AnalysisResult

#
Chain::new

fn Chain::new(name : String, dimensions : Array[Dimension]) -> Chain

#
Chain::nominal

fn Chain::nominal(self : Chain) -> Double

#
Chain::rss

fn Chain::rss(self : Chain) -> AnalysisResult

#
Chain::worst_case

fn Chain::worst_case(self : Chain) -> AnalysisResult

#
ConstraintReport

pub struct ConstraintReport {
passed : Int
failed : Int
inconclusive : Int
statuses : Array[(String, ConstraintStatus)]
} derive(
Debug
)

#
ConstraintStatus

pub enum ConstraintStatus {
Satisfied
Violated
Inconclusive
} derive(Eq,
Debug
)

#
Dimension

pub struct Dimension {
name : String
nominal : Double
tolerance : Double
direction : Direction
} derive(Eq,
Debug
)

#
Dimension::new

fn Dimension::new(name : String, nominal : Double, tolerance : Double, direction? : Direction) -> Dimension

#
Direction

pub enum Direction {
Positive
Negative
} derive(Eq,
Debug
)

#
Distribution

pub enum Distribution {
Uniform
Triangular
NormalApproximation
} derive(Eq,
Debug
)

#
GapConstraint

pub struct GapConstraint {
name : String
minimum : Double
maximum : Double
} derive(
Debug
)

#
GapConstraint::evaluate

#
GapConstraint::new

fn GapConstraint::new(name : String, minimum : Double, maximum : Double) -> GapConstraint

#
ProcessCapability

pub struct ProcessCapability {
lower_spec : Double
upper_spec : Double
mean : Double
standard_deviation : Double
} derive(
Debug
)

#
ProcessCapability::cp

fn ProcessCapability::cp(self : ProcessCapability) -> Double

#
ProcessCapability::cpk

fn ProcessCapability::cpk(self : ProcessCapability) -> Double

#
ProcessCapability::cpl

fn ProcessCapability::cpl(self : ProcessCapability) -> Double

#
ProcessCapability::cpu

fn ProcessCapability::cpu(self : ProcessCapability) -> Double

#
ProcessCapability::new

fn ProcessCapability::new(lower_spec : Double, upper_spec : Double, mean : Double, standard_deviation : Double) -> ProcessCapability

#
ProjectedDimension

pub struct ProjectedDimension {
name : String
vector : Vector2
tolerance : Double
} derive(
Debug
)

#
ProjectedDimension::new

fn ProjectedDimension::new(name : String, vector : Vector2, tolerance : Double) -> ProjectedDimension

#
ProjectedResult

pub struct ProjectedResult {
nominal : Vector2
lower_x : Double
upper_x : Double
lower_y : Double
upper_y : Double
radial_tolerance : Double
sensitivity : Array[(String, Double)]
} derive(
Debug
)

#
ReferenceVector

pub struct ReferenceVector {
dimensions : Int
tolerance : Double
nominal : Double
worst_lower : Double
worst_upper : Double
rss_sigma : Double
} derive(
Debug
)

#
SamplingPolicy

pub struct SamplingPolicy {
distribution : Distribution
sigma_factor : Double
seed : UInt
} derive(
Debug
)

#
SamplingPolicy::new

fn SamplingPolicy::new(distribution? : Distribution, sigma_factor? : Double, seed? : UInt) -> SamplingPolicy

#
StandardProfile

pub struct StandardProfile {
code : String
family : String
nominal_mm : Double
tolerance_mm : Double
cpk_target : Double
sample_count : Int
} derive(
Debug
)

#
Vector2

pub struct Vector2 {
x : Double
y : Double
} derive(Eq,
Debug
)

#
Vector2::add

fn Vector2::add(self : Vector2, other : Vector2) -> Vector2

#
Vector2::dot

fn Vector2::dot(self : Vector2, other : Vector2) -> Double

#
Vector2::length

fn Vector2::length(self : Vector2) -> Double

#
Vector2::length_squared

fn Vector2::length_squared(self : Vector2) -> Double

#
Vector2::new

fn Vector2::new(x : Double, y : Double) -> Vector2

#
Vector2::normalize

fn Vector2::normalize(self : Vector2) -> Vector2

#
Vector2::scale

fn Vector2::scale(self : Vector2, factor : Double) -> Vector2

#
Vector2::sub

fn Vector2::sub(self : Vector2, other : Vector2) -> Vector2

#
Vector2::zero

fn Vector2::zero() -> Vector2

#
benchmark_suite

fn benchmark_suite() -> Array[BenchmarkCase]

#
calibration_point_count

fn calibration_point_count() -> Int

#
calibration_points

fn calibration_points() -> Array[CalibrationPoint]

#
corrected_measurement

fn corrected_measurement(point : CalibrationPoint, observed : Double) -> Double

#
deterministic_uniform

fn deterministic_uniform(seed : UInt) -> (UInt, Double)

#
evaluate_constraints

fn evaluate_constraints(result : AnalysisResult, constraints : Array[GapConstraint]) -> ConstraintReport

#
interval_contains

fn interval_contains(result : AnalysisResult, value : Double) -> Bool

#
interval_width

fn interval_width(result : AnalysisResult) -> Double

#
negative_direction

fn negative_direction() -> Direction

#
normal_approximation_policy

fn normal_approximation_policy(seed? : UInt) -> SamplingPolicy

#
project_chain

fn project_chain(dimensions : Array[ProjectedDimension]) -> ProjectedResult

#
project_direction

fn project_direction(angle_radians : Double) -> Vector2

#
project_signed

fn project_signed(value : Double, angle_radians : Double) -> Vector2

#
reference_vectors

fn reference_vectors() -> Array[ReferenceVector]

#
result_to_csv

fn result_to_csv(result : AnalysisResult) -> String

#
run_benchmark_case

fn run_benchmark_case(case : BenchmarkCase, samples : Int) -> AnalysisResult

#
sample_deviation

fn sample_deviation(policy : SamplingPolicy, unit : Double, tolerance : Double) -> Double

#
standard_profile_count

fn standard_profile_count() -> Int

#
standard_profiles

fn standard_profiles() -> Array[StandardProfile]

#
triangular_policy

fn triangular_policy(seed? : UInt) -> SamplingPolicy

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io