"""Units for reading: the one table of what each SI unit is shown in.
Everything is computed and stored in SI. A figure, a table or the workbench
shows a value as ``value × scale`` in the display unit:
from openairbearing import display
display.shown(stiffness, "N/m") # values in N/µm
display.label("Stiffness", "N/m") # "Stiffness [N/µm]"
A unit that is not in the table is shown as it is. Flow in m³/s is free gas
at ambient pressure, shown in L/min.
"""
from __future__ import annotations
import math
import numpy as np
__all__ = ["COORDINATE", "DISPLAY", "dof_units", "label", "shown", "unit"]
# SI unit -> the unit people read it in, shown value = SI value × scale
DISPLAY = {
"m": {"display": "µm", "scale": 1e6},
"rad": {"display": "mrad", "scale": 1e3},
"Pa": {"display": "MPa", "scale": 1e-6},
"m³/s": {"display": "L/min", "scale": 6e4},
"kg/s": {"display": "g/s", "scale": 1e3},
"N/m": {"display": "N/µm", "scale": 1e-6},
"N/rad": {"display": "N/mrad", "scale": 1e-3},
"N·m/m": {"display": "N·m/µm", "scale": 1e-6},
"N·m/rad": {"display": "N·m/mrad", "scale": 1e-3},
}
# The same for the film coordinates, which are sizes of the bearing, not of
# the gap: mm and degrees.
COORDINATE = {
"m": {"display": "mm", "scale": 1e3},
"rad": {"display": "°", "scale": 180.0 / math.pi},
}
[docs]
def dof_units(name: str) -> tuple[str, str]:
"""SI units (position, conjugate force) of a position coordinate.
("rad", "N·m") for a rotation (rx, ry, rz), ("m", "N") for the rest:
h, x, y and z.
"""
return ("rad", "N·m") if name in ("rx", "ry", "rz") else ("m", "N")
[docs]
def unit(si: str, *, coordinate: bool = False) -> dict:
"""{"display", "scale"} of an SI unit; coordinate=True reads COORDINATE."""
table = COORDINATE if coordinate else DISPLAY
return table.get(si, {"display": si, "scale": 1.0})
[docs]
def shown(values, si: str, *, coordinate: bool = False) -> np.ndarray:
"""SI values as an array in the display unit of si. None becomes NaN."""
scale = unit(si, coordinate=coordinate)["scale"]
return np.array(values, dtype=float) * scale
[docs]
def label(name: str, si: str, *, coordinate: bool = False) -> str:
"""Axis label: the name with the display unit of si, "Stiffness [N/µm]"."""
return f"{name} [{unit(si, coordinate=coordinate)['display'] or '-'}]"