Unit systems#
- class ansys.units.systems.UnitSystem(base_units: Mapping[BaseDimensions, Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str] | None = None, preferred_units: Sequence[Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike] | Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike | None = None, system: Literal['SI', 'CGS', 'BT'] | str = 'SI', copy_from: UnitSystem | None = None)#
Bases:
objectA class representing base units for a unit system.
Predefined unit systems work automatically and are configured when the package is initialized, whereas you can add user-defined systems at any time.
- Parameters:
- base_units: dict, optional
Units mapped to base dimensions types.
- preferred_units: str, list, optional
Preferred units used when converted dimensions match. These units are display/conversion targets for derived quantities, not base-unit slots.
- system: str, Unit, optional
Predefined unit system.
- copy_from: UnitSystem, optional
Make a copy of a unit system.
- Attributes:
MASSMass unit of the unit system.
LENGTHLength unit of the unit system.
TIMETime unit of the unit system.
TEMPERATURETemperature unit of the unit system.
TEMPERATURE_DIFFERENCETemperature unit of the unit system.
ANGLEAngle unit of the unit system.
CHEMICAL_AMOUNTChemical Amount unit of the unit system.
LIGHTLight unit of the unit system.
CURRENTCurrent unit of the unit system.
SOLID_ANGLESolid Angle unit of the unit system.
- property TEMPERATURE_DIFFERENCE: Literal['delta_K', 'delta_C', 'delta_F', 'delta_R'] | str#
Temperature unit of the unit system.
- add_preferred_units(units: Sequence[Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike] | Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike) None#
Add preferred display units for derived dimensions.
When a unit is converted using this unit system, a matching preferred unit is returned instead of the expanded base-unit expression. For example, a millimeter-tonne-second system can prefer
"MPa"for pressure/stress dimensions instead of"tonne mm^-1 s^-2".- Parameters:
- units: list
Preferred units to use for matching dimensions.
- preferred_unit_for(dimensions: Dimensions) str | None#
Return the preferred unit name for dimensions, if one is configured.
- classmethod register_system(name: str, base_units: Mapping[BaseDimensions, Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str], preferred_units: Sequence[Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike] | Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str | UnitLike | None = None) None#
Register a new named unit system for later reuse.
Once registered, the system can be created anywhere via
UnitSystem(system=name), the same way built-in systems such as"SI","CGS", and"BT"are used. Registration is global for the running process (module-scoped), matching the behavior of the predefined unit systems loaded fromcfg.yaml.Note
Only the resolved unit name is persisted, not the
Unitobject itself. Instance-scoped custom units created withregister_unit()are not known globally by name, so while passing one here succeeds (it is valid at registration time), reconstructing the system later withUnitSystem(system=name)fails unless the unit is also resolvable without that specific registry (for example a built-in unit, or a prefixed/derived atomic unit).- Parameters:
- name: str
Name of the new unit system, for example
"MMGS".- base_units: dict
Units mapped to base dimensions types. Every member of
BaseDimensionsmust be provided so the resulting system is fully defined.- preferred_units: str, list, optional
Preferred units used when converted dimensions match. These units are display/conversion targets for derived quantities, not base-unit slots.
- Raises:
UnitSystemAlreadyRegisteredIf
nameis already used by a built-in or previously registered unit system.IncompleteUnitSystemIf
base_unitsis missing one or more base dimensions.NotBaseUnitIf a unit is not dimensionally atomic (for example a derived unit that spans more than one base dimension, such as
"Pa").IncorrectUnitTypeIf a unit does not match its intended base dimension.
UnconfiguredPreferredUnitIf a preferred unit cannot be resolved globally by name.
PreferredUnitAlreadyRegisteredIf two preferred units have the same dimensions.
Examples
Register a custom “mm, g, s” (MMGS) unit system:
>>> from ansys.units import BaseDimensions, UnitSystem >>> dims = BaseDimensions >>> UnitSystem.register_system( ... name="MMGS", ... base_units={ ... dims.MASS: "g", ... dims.LENGTH: "mm", ... dims.TIME: "s", ... dims.TEMPERATURE: "K", ... dims.TEMPERATURE_DIFFERENCE: "delta_K", ... dims.ANGLE: "radian", ... dims.CHEMICAL_AMOUNT: "mol", ... dims.LIGHT: "cd", ... dims.CURRENT: "A", ... dims.SOLID_ANGLE: "sr", ... }, ... ) >>> mmgs = UnitSystem(system="MMGS") >>> mmgs.LENGTH 'mm'
- update(base_units: Mapping[BaseDimensions, Literal['kg', 'g', 'lb', 'lbm', 'slug', 'm', 'cm', 'ft', 'inch', 'in', 's', 'A', 'mol', 'slugmol', 'cd', 'sr', 'radian', 'degree', 'K', 'C', 'F', 'R', 'delta_K', 'delta_C', 'delta_F', 'delta_R', 'N', 'Pa', 'W', 'J', 'V', 'farad', 'H', 'S', 'Wb', 'T', 'dyne', 'erg', 'h', 'pdl', 'psi', 'lbf', 'psf', 'ohm', 'Hz', 'l', 'gal', 'BTU', 'cal', 'coulomb', 'tonne'] | str])#
Change the units of the unit system.
- Parameters:
- base_units: dict
Units mapped to base dimensions types.
- exception ansys.units.systems.IncorrectUnitType(unit, unit_type)#
Bases:
ValueErrorRaised when a unit is provided that does not have a valid type of base unit.
- exception ansys.units.systems.InvalidUnitSystem(sys)#
Bases:
ValueErrorRaised when a unit system is initialized with an unsupported unit system.
- exception ansys.units.systems.NotBaseUnit(unit)#
Bases:
ValueErrorRaised when a unit system unit is not a configured base unit.