Coder Social home page Coder Social logo

Comments (4)

joe-saronic avatar joe-saronic commented on July 17, 2024 1

A link to examples in the top level folder would be very helpful, as would additional explanation of what the macros actually do.

from uom.

Anaphory avatar Anaphory commented on July 17, 2024

If anyone else is confused about how to use it, or if you base as an example on this, this is what I did for now. (I noticed I need a smaller length resolution, not a smaller time resolution, to get a better speed resolution, duh, so it's decimillimeters, not decimilliseconds as in my previous post.)

#[macro_use]
extern crate uom;
// With a target location resolution in centimeters, the underlying model
// should use millimeters to avoid too many rounding artefacts. For speeds
// up to 300 km/h at that space resolution, we would need time resolutions
// of 10^-3 seconds.
//
// Now, working in millimeters and i32 already gives us a maximum length of
// 2147 km, which should be sufficient for any track within Germany. Any
// bigger data type will also be sufficient.
//
// For 10^-3 seconds, i32 can represent about 25 days, which is just about
// sufficient most use cases of our simulation. Larger time frames will have to
// use offsets external to the simulation, and have to be persisted, adapted to
// the new offset, and re-started.
//
// However, with millimeters and milliseconds, the base unit of velocity is
// mm/ms = m/s, which is less than resolution of km/h = 0.278 m/s we would like
// to be able to talk about. So we reduce the space resolution once again, to
// 10^-4 meters.
mod trainphysics_units {
    #[macro_use]
    mod length {
        quantity! {
            /// Length (base unit meter, m).
            quantity: Length; "length";
            /// Length dimension, m
            dimension: LMT<P1, Z0, Z0>;
            units {
                @decimillimeter: 0.0001; "dmm", "decimillimeter", "decimillimeters";
                @millimeter: 0.001; "mm", "millimeter", "millimeters";
                @centimeter: 0.01; "cm", "centimeter", "centimeters";
                @meter: 1.; "m", "meter", "meters";
                @kilometer: 1000.; "km", "kilometer", "kilometers";
            }
        }
    }
    #[macro_use]
    mod mass {
        quantity! {
            /// Mass (base unit kilogram, kg).
            quantity: Mass; "mass";
            /// Mass dimension, kg
            dimension: LMT<Z0, P1, Z0>;
            units {
                @kilogram: 1.; "kg", "kilogram", "kilograms";
                @ton: 1000.; "T", "ton", "tons";
            }
        }
    }
    #[macro_use]
    mod time {
        quantity! {
            /// Time (base unit second, s).
            quantity: Time; "time";
            /// Time dimension, s
            dimension: LMT<Z0, Z0, P1>;
            units {
                @millisecond: 0.001; "ms", "millisecond", "milliseconds";
                @second: 1.; "s", "second", "seconds";
                @minute: 60.; "min", "minute", "minutes";
                @hour: 3600.; "h", "hour", "hours";
                @day: 86400.; "d", "day", "days";
            }
        }
    }
    #[macro_use]
    mod velocity {
        quantity! {
            /// Velocity (base unit meters per second, m/s).
            quantity: Velocity; "velocity";
            /// Velocity dimension, m/s
            dimension: LMT<P1, Z0, N1>;
            units {
                @meter_per_second: 1.; "m/s", "meter per second", "meters per second";
                @kilometer_per_hour: 2.777_777_777_777_778_E-1; "km/h", "kilometer per hour", "kilometers per hour";
            }
        }
    }
    #[macro_use]
    mod acceleration {
        quantity! {
            /// Acceleration (base unit meters per second squared, m/s²).
            quantity: Acceleration; "acceleration";
            /// Acceleration dimension, m/s²
            dimension: LMT<P1, Z0, N2>;
            units {
                @meter_per_second_squared: 1.; "m/s²", "meter per second squared", "meters per second squared";
            }
        }
    }
    #[macro_use]
    mod power {
        quantity! {
            /// Power (base unit Watt).
            quantity: Power; "power";
            /// Power dimension, m²kg/s³
            dimension: LMT<P2, P1, N3>;
            units {
                @watt: 1.; "W", "watt", "watts";
            }
        }
    }
    system! {
        /// System of quantities, Q.
        quantities: LMT {
            length: meter, L;
            mass: kilogram, M;
            time: second, T;
        }
        /// System of units, U.
        units: U {
            mod length::Length,
            mod mass::Mass,
            mod time::Time,
            mod velocity::Velocity,
            mod acceleration::Acceleration,
            mod power::Power,
        }
    }
    pub mod i32 {
        LMT!(
            crate::trainphysics_units,
            i32,
            (decimillimeter, kilogram, millisecond)
        );
    }
}

use trainphysics_units::i32::{Acceleration, Length, Mass, Time, Velocity, Power};

from uom.

iliekturtles avatar iliekturtles commented on July 17, 2024

@Anaphory Did you end up resolving all your issues or did you still have some questions?

Setting up new type aliases with different base units is definitely not intuitive and probably not easy to discover. Any concrete suggestions, especially about documentation, based on your experience?

For anyone coming to this issue in the future, examples/base.rs, gives a minimal example of setting up type aliases with different base units. The ISQ! macro is also documented.

from uom.

sww1235 avatar sww1235 commented on July 17, 2024

Bumping this thread. I for one would like a clearer example of defining custom unit systems with nonstandard base units such as mm or ms to avoid the use of floating point numbers.

Thanks in advance

from uom.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.