uom icon indicating copy to clipboard operation
uom copied to clipboard

Unit request: LinearEnergyDensity

Open Titaniumtown opened this issue 11 months ago • 2 comments

I mentioned in this issue: https://github.com/iliekturtles/uom/issues/357#issuecomment-2599569955 my need for a LinearEnergyDensity type. My use case would include a wh/mi representation and such. Can anyone guide me through how to create such a unit? I'm a bit lost. Thanks!

I created this as a stop-gap:

pub struct LinearEnergyDensity<T: uom::si::length::Conversion<f32>> {
    energy: Energy,
    _base_unit: T,
}

impl<T: uom::si::length::Conversion<f32>> LinearEnergyDensity<T> {
    pub const fn new(energy: Energy, base_unit: T) -> Self {
        Self {
            energy,
            _base_unit: base_unit,
        }
    }
}

impl<T: uom::si::length::Conversion<f32>> Mul<Length> for LinearEnergyDensity<T> {
    type Output = Energy;

    fn mul(self, rhs: Length) -> Self::Output {
        let conv_length = rhs.get::<T>();
        self.energy * conv_length
    }
}

Titaniumtown avatar Jan 19 '25 01:01 Titaniumtown

I figured this out, using type expansion:

use uom::si::{Dimension, Quantity};
use uom::typenum::{NInt, PInt, UInt, UTerm, B0, B1, Z0};
use uom::Kind;
pub type LinearEnergyDensity = Quantity<
    (dyn Dimension<
        I = Z0,
        J = Z0,
        Kind = (dyn Kind + 'static),
        L = PInt<UInt<UTerm, B1>>,
        M = PInt<UInt<UTerm, B1>>,
        N = Z0,
        T = NInt<UInt<UInt<UTerm, B1>, B0>>,
        Th = Z0,
    > + 'static),
    dyn uom::si::Units<
        f32,
        amount_of_substance = uom::si::amount_of_substance::mole,
        electric_current = uom::si::electric_current::ampere,
        length = uom::si::length::meter,
        luminous_intensity = uom::si::luminous_intensity::candela,
        mass = uom::si::mass::kilogram,
        thermodynamic_temperature = uom::si::thermodynamic_temperature::kelvin,
        time = uom::si::time::second,
    >,
    f32,
>;

There should really be a macro to create this. Like create_unit!(Energy / Length). As I have no idea what the type actually means by just reading it.

Titaniumtown avatar Jan 21 '25 21:01 Titaniumtown

Figured out I can do this:

/// m^1 kg^1 s^-2
pub type LinearEnergyDensity = Quantity<ISQ<P1, P1, N2, Z0, Z0, Z0, Z0>, SI<f32>, f32>;

very nice!

Titaniumtown avatar Jan 22 '25 15:01 Titaniumtown