iliekturtles / uom

Units of measurement -- type-safe zero-cost dimensional analysis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Conversion to Watts

jchidley opened this issue · comments

I have this function:

    fn ventilation_heat_loss() {
        let β = 50.0; //efficiency as percentage
        let cp_air = SpecificHeatCapacity::new::<kilojoule_per_kilogram_kelvin>(0.7171);
        let ρ_air = MassDensity::new::<kilogram_per_cubic_meter>(1.276);
        let t_i = TemperatureInterval::new::<temperature_interval::degree_celsius>(20.0);
        let t_o = TemperatureInterval::new::<temperature_interval::degree_celsius>(10.0);
        let qv = VolumeRate::new::<cubic_meter_per_second>(15.0 / 1000.0);
        let hv = (1.0 - β / 100.0) * cp_air * ρ_air * qv * (t_i - t_o); // watts

        println!("{hv:#?}");

This prints an answer in these units: m^2 kg^1 s^-3 or, as I normally think about it, watts.

I can't figure out, for the life of me, how to get the answer into actual watts in uom (or even what the correct watts is). I can see that uom a really comprehensive and useful library but, as a newbie to rust and the library, it is daunting with a few examples and no tutorial.

This doesn't work:

62 |         let watts = Power::new::<watt>(hv);
   |                     ------------------ ^^ expected `f32`, found `Quantity<dyn Dimension<I = ..., J = ..., Kind = ..., L = ..., M = ..., N = ..., T = ..., Th = ...>, ..., ...>`

Which tells me that I am doing the conversion wrongly.

So, of course, I found the answer in the unit.rs example a few minutes after I posted my question.

Once I had added this two lines at the top:

use uom::fmt::DisplayStyle::Description;
use uom::si::power::watt;

I could print using this statement:

println!("{}", hv.into_format_args(watt, Description));