Skip to content

Quantities and Units

The Quantity module provides two classes to work with quantities and units.

Attributes

quantities module-attribute

quantities = {
    "pressure": (
        "Pressure",
        "pascal",
        True,
        "p",
        "$p$",
        REFERENCE_PRESSURE,
    )
}

Dictionary with quantities. Each quantity is stored as a tuple.

units module-attribute

units = {
    "meter": ("meter", "m", "$m$"),
    "pascal": ("pascal", "Pa", "$Pa$"),
}

Dictionary with units. Each unit is stored as a tuple.

Classes

Unit

Unit(name: str, symbol: str, symbol_latex: str)

Unit of quantity.

Note

Perhaps inherit from tuple or collections.namedtuple?

PARAMETER DESCRIPTION
name

Name of the unit.

TYPE: str

symbol

Symbol of the unit.

TYPE: str

symbol_latex

Symbol of the unit in LaTeX.

TYPE: str

Source code in acoustic_toolbox/quantity.py
def __init__(self, name: str, symbol: str, symbol_latex: str):
    """Initialize the unit.

    Args:
      name: Name of the unit.
      symbol: Symbol of the unit.
      symbol_latex: Symbol of the unit in LaTeX.
    """
    self.name = name
    """Name of the unit."""

    self.symbol = symbol
    """Symbol of the unit."""

    self.symbol_latex = symbol_latex
    """Symbol of the unit in LaTeX."""

Attributes

name instance-attribute
name = name

Name of the unit.

symbol instance-attribute
symbol = symbol

Symbol of the unit.

symbol_latex instance-attribute
symbol_latex = symbol_latex

Symbol of the unit in LaTeX.

Functions

Quantity

Quantity(
    name: str,
    unit: Unit,
    dynamic: bool,
    symbol: str | None = None,
    symbol_latex: str | None = None,
    reference: float = 1.0,
)

Quantity.

PARAMETER DESCRIPTION
name

Name of the quantity.

TYPE: str

unit

Unit of the quantity.

TYPE: Unit

dynamic

Dynamic quantity (True) or energetic (False).

TYPE: bool

symbol

Symbol of the quantity.

TYPE: str | None DEFAULT: None

symbol_latex

Symbol of the quantity in LaTeX.

TYPE: str | None DEFAULT: None

reference

Reference value of the quantity.

TYPE: float DEFAULT: 1.0

Source code in acoustic_toolbox/quantity.py
def __init__(
    self,
    name: str,
    unit: Unit,
    dynamic: bool,
    symbol: str | None = None,
    symbol_latex: str | None = None,
    reference: float = 1.0,
):
    """Initialize the quantity.

    Args:
      name: Name of the quantity.
      unit: Unit of the quantity.
      dynamic: Dynamic quantity (`True`) or energetic (`False`).
      symbol: Symbol of the quantity.
      symbol_latex: Symbol of the quantity in LaTeX.
      reference: Reference value of the quantity.
    """
    self.name = name
    """Name of the quantity."""

    self.symbol = symbol
    """Symbol of the quantity."""

    self.symbol_latex = symbol_latex
    """Symbol of the unit in LaTeX."""

    self.unit = unit
    """Unit. See [`Unit`][acoustic_toolbox.quantity.Unit]."""

    self.dynamic = dynamic
    """Dynamic quantity (`True`) or energetic (`False`)."""

    self.reference = reference
    """Reference value of the quantity."""

Attributes

name instance-attribute
name = name

Name of the quantity.

symbol instance-attribute
symbol = symbol

Symbol of the quantity.

symbol_latex instance-attribute
symbol_latex = symbol_latex

Symbol of the unit in LaTeX.

unit instance-attribute
unit = unit

Unit. See Unit.

dynamic instance-attribute
dynamic = dynamic

Dynamic quantity (True) or energetic (False).

reference instance-attribute
reference = reference

Reference value of the quantity.

energetic property
energetic: bool

Energetic quantity (True) or dynamic (False).

Functions

Functions

get_quantity

get_quantity(name: str) -> Quantity

Get quantity by name. Returns instance of Quantity.

PARAMETER DESCRIPTION
name

Name of the quantity.

TYPE: str

RETURNS DESCRIPTION
Quantity

Instance of Quantity.

Source code in acoustic_toolbox/quantity.py
def get_quantity(name: str) -> Quantity:
    """Get quantity by name. Returns instance of [`Quantity`][acoustic_toolbox.quantity.Quantity].

    Args:
      name: Name of the quantity.

    Returns:
      Instance of [`Quantity`][acoustic_toolbox.quantity.Quantity].
    """
    try:
        q = list(quantities[name])
    except KeyError:
        raise ValueError("Unknown quantity. Quantity is not yet specified.")
    try:
        q[1] = Unit(*units[name])
    except KeyError:
        raise RuntimeError(
            "Unknown unit. Quantity has been specified but unit has not."
        )

    return Quantity(*q)

:::