Units

Unit conversion utilities and registry.

Unit management: registry and conversions.

Provides:
  • UNITS: Full unit registry by category

  • STRESS_UNITS: Flat dict for stress units (backward compatible)

  • convert(): Convert between compatible units

  • to_base(), from_base(): Stress unit conversion helpers

mechlab.units.convert(value, from_unit, to_unit)[source]

Convert a value from one unit to another.

Parameters:
  • value (float) – Numeric value to convert

  • from_unit (str) – Source unit name

  • to_unit (str) – Target unit name

Returns:

Converted value

Raises:

UnitError – If units are unknown or incompatible

Return type:

float

Example

>>> convert(100, 'MPa', 'psi')
14503.773773...
>>> convert(1, 'm', 'ft')
3.28084...
exception mechlab.units.UnitError[source]

Bases: Exception

Exception raised for unit conversion errors.

mechlab.units.to_base(value, unit)[source]

Convert stress value to base unit (Pascal).

Parameters:
  • value (float) – Stress value in specified unit

  • unit (str) – Unit name

Returns:

Stress value in Pascals

Return type:

float

mechlab.units.from_base(value, unit)[source]

Convert stress value from base unit (Pascal) to specified unit.

Parameters:
  • value (float) – Stress value in Pascals

  • unit (str) – Target unit name

Returns:

Stress value in target unit

Return type:

float

Data

mechlab.units.UNITS = {'Unit registry organized by physical quantity.\n    \n    Each category maps unit names to conversion factors (multiply by factor to get SI base unit).\n    \n    Categories:\n        - length: meter (m)\n        - force: newton (N) \n        - pressure: pascal (Pa)\n        - mass: kilogram (kg)\n        - area: square meter (m²)\n        - moment: newton-meter (N⋅m)\n    length': {'cm': 0.01, 'ft': 0.3048, 'inch': 0.0254, 'km': 1000.0, 'm': 1.0, 'mm': 0.001, 'yd': 0.9144}, 'area': {'cm2': 0.0001, 'ft2': 0.092903, 'in2': 0.00064516, 'm2': 1.0, 'mm2': 1e-06}, 'force': {'MN': 1000000.0, 'N': 1.0, 'kN': 1000.0, 'kgf': 9.80665, 'lbf': 4.44822}, 'mass': {'g': 0.001, 'kg': 1.0, 'lb': 0.453592, 'mg': 1e-06, 'oz': 0.0283495, 'ton': 1000.0}, 'moment': {'Nm': 1.0, 'kNm': 1000.0, 'lbf_ft': 1.35582, 'lbf_in': 0.112985}, 'pressure': {'GPa': 1000000000.0, 'MPa': 1000000.0, 'Pa': 1.0, 'bar': 100000.0, 'kPa': 1000.0, 'ksi': 6894760.0, 'psi': 6894.76}}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)

mechlab.units.STRESS_UNITS = {'GPa': 1000000000.0, 'MPa': 1000000.0, 'Pa': 1.0, 'bar': 100000.0, 'kPa': 1000.0, 'ksi': 6894760.0, 'psi': 6894.76}

dict() -> new empty dictionary dict(mapping) -> new dictionary initialized from a mapping object’s

(key, value) pairs

dict(iterable) -> new dictionary initialized as if via:

d = {} for k, v in iterable:

d[k] = v

dict(**kwargs) -> new dictionary initialized with the name=value pairs

in the keyword argument list. For example: dict(one=1, two=2)