iliekturtles / uom

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Better precision and abbreviation change for `astronomical_unit`

pos-ki opened this issue · comments

Hi, I am planning to use uom for my astronomy project and I have been looking into its use to make sure it's gonna work well for me. Since I am aiming for the best calculation precision possible, I am planning to define a custom a custom set of Quantity type aliases that would use AU as the underlying base unit for distance (and perhaps also change some other base units) - since that would make the distances closer to 1 compared to what they would be if I used (kilo)meters, resulting in better floating point accuracy in the calculations.

However, I noticed some issues with the current definition of the astronomical_unit in uom:

  • The abbreviation is "ua" instead of "au" for some reason - is this just a typo or is there a reason for this?
  • The actual numerical definition is rather imprecise. I would understand this with something like a parsec, which is defined using AU and pi, making it infinitely precise, requiring some sort of precision cutoff to be chosen. However, the astronomical unit has an exact value - 1 AU = 149 597 870 700 m (as mentioned on Wikipedia, referencing the relevant article). Would it be possible to reflect this in the code? I can temporarily remedy this by creating a new custom unit for it, but it would be nice if this was available for everyone.

My proposal would therefore be to change the relevant line to this:

@astronomical_unit: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";

I would be happy to make a PR with the tweak if you give me a green light. Thanks!

I am trying to make the workaround solution in the meantime by basically combining two of the example files - creating a new unit and using it to define a different set of base units.

I currently have the following draft test code:

#[macro_use]
extern crate uom;

use uom::fmt::DisplayStyle::*;
use uom::si::f64::*;
use uom::si::length::meter;

unit! {
    system: uom::si;
    quantity: uom::si::length;

    @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
}

mod solar_system_units {
    use crate::au;

    ISQ!(
        uom::si,
        f64,
        (au, kilogram, day, ampere, kelvin, mole, candela)
    );
}

fn main() {
    let l1 = Length::new::<meter>(1000.0);
    let l2 = Length::new::<au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(meter, Abbreviation),
        l1.into_format_args(au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(au, Abbreviation),
        l2.into_format_args(meter, Abbreviation)
    );
}

which produces a rather confusing compiler error, telling me to import crate::au which is already imported on that very line, while simultaneously telling me that this import is unused.

error[E0412]: cannot find type `au` in module `__system::length`
  --> src/main.rs:21:10
   |
21 |         (au, kilogram, day, ampere, kelvin, mole, candela)
   |          ^^ not found in `__system::length`
   |
help: consider importing this struct
   |
16 +     use crate::au;
   |

warning: unused import: `crate::au`
  --> src/main.rs:16:9
   |
16 |     use crate::au;
   |         ^^^^^^^^^
   |
   = note: `#[warn(unused_imports)]` on by default

For more information about this error, try `rustc --explain E0412`.

I am unfortunately not too familiar with macros yet, so I am not sure whether what I am trying to do is even remotely supposed to work.

If all your doing is extending an existing quantity with units, then you don't need to create a new set of quantities. Deleting the solar_system_units mod should make your example code compile.

For ease of use you could create a custom si module to access your custom units with the same import as base units:

mod si {
  pub use uom::si::*;
  
  pub mod length {
      pub use uom::si::length::*;
      
      unit! {
         system: uom::si;
         quantity: uom::si::length;

        @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
       }
   }
}
      

My impression was that I had to create a new set of quantities to adjust the units in which each quantity is stored under the hood. My goal is to store distances as AU, mass as zettagrams (did not give that in the example, but have added it since), and time as days, in order to maintain best precision with large values.

ISQ!(uom::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela)

While also redefining the AU as in the example. To achieve all of this, I believe I have to create the new set of quantities, right?

Try this:

#[macro_use]
extern crate uom;

use uom::fmt::DisplayStyle::*;

mod si {
    pub use uom::si::*;
    
    pub mod length {
        pub use uom::si::length::*;
        
        unit! {
           system: uom::si;
           quantity: uom::si::length;
  
          @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
         }
     }
  }


mod custom_base {
    ISQ!(super::si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));
}



fn main() {
    let l1 = custom_base::Length::new::<si::length::meter>(1000.0);
    let l2 = custom_base::Length::new::<si::length::au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(si::length::meter, Abbreviation),
        l1.into_format_args(si::length::au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(si::length::au, Abbreviation),
        l2.into_format_args(si::length::meter, Abbreviation)
    );
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn base_unit_of_length_should_be_au() {
        let l = custom_base::Length::new::<si::length::au>(1.0);

        assert_eq!(l.value, 1.0);
    }

    #[test]
    fn base_unit_of_time_should_be_day() {
        let t_base = custom_base::Time::new::<si::time::day>(1.0);
        let t_year = custom_base::Time::new::<si::time::year>(1.0);

        assert_eq!(t_base.value, 1.0);

        assert_eq!(t_year.value, 365.0);
    }
}

Just for more context, you don't need to create a new set of qunatities, i.e like the mks example, but create an alias to the ISQ system using the ISQ! macro and changing the base units, i.e like the base example.

If you want to pack all this into one module, then you can do the following:

// Custom system of units
mod csi {

    ISQ!(si, f64, (au, zettagram, day, ampere, kelvin, mole, candela));

    mod si {
        pub use uom::si::*;

        pub mod length {
            pub use uom::si::length::*;

            unit! {
              system: uom::si;
              quantity: uom::si::length;

             @au: 1.495_978_707_E11; "au", "astronomical unit", "astronomical units";
            }
        }
    }

    pub use si::*;
}

fn main() {
    let l1 = csi::Length::new::<csi::length::meter>(1000.0);
    let l2 = csi::Length::new::<csi::length::au>(1.0);

    println!(
        "{} = {}",
        l1.into_format_args(csi::length::meter, Abbreviation),
        l1.into_format_args(csi::length::au, Abbreviation)
    );
    println!(
        "{} = {}",
        l2.into_format_args(csi::length::au, Abbreviation),
        l2.into_format_args(csi::length::meter, Abbreviation)
    );
}

Okay, thanks, the workaround works like that! Still hope for the AU abbreviation and the exact value to be adjusted, though.