Go to file
elfeiin 89fb4f6256
1.0
2024-04-27 22:35:10 -07:00
examples enabled generic const exprs 2024-04-27 22:10:56 -07:00
src disambiguate module pub use 2024-04-27 22:12:22 -07:00
.gitignore v0.4.0 2024-03-01 16:54:05 -08:00
Cargo.toml 1.0 2024-04-27 22:35:10 -07:00
README.md updated README.md 2024-04-27 22:14:48 -07:00
build.rs finishing touches 2024-04-27 22:09:55 -07:00

README.md

units

Type-safe units handling library. Do arithmetic on measurements. Convert between different units.

example

#![feature(generic_const_exprs)]

use std::f64::consts::TAU;
use units::{
    dimensions::{
        Energy,
        Frequency,
        Length,
        Mass,
        MassMoment,
    },
    units::{
        Gram,
        Joule,
        Meter,
        Rpm,
        Watthour,
    },
    Unit,
    prefixes::Kilo,
};

#[derive(Debug)]
pub struct Flywheel {
    mass: Mass,
    radius: Length,
    inertial_constant: f64,
}

impl Flywheel {
    pub fn new_cylinder(mass: Mass, radius: Length) -> Self {
        Self {
            mass,
            radius,
            // Cylinder
            inertial_constant: 0.606,
        }
    }

    pub fn new_good(mass: Mass, radius: Length) -> Self {
        Self {
            mass,
            radius,
            // Mass concentrated around rim
            inertial_constant: 1.0,
        }
    }

    pub fn inertial_moment(&self) -> MassMoment {
        self.mass * self.inertial_constant * (self.radius * self.radius)
    }

    pub fn energy(&self, spin_rate: Frequency) -> Energy {
        let angular_velocity = spin_rate * TAU;
        self.inertial_moment() * (angular_velocity * angular_velocity) / 2.0
    }
}

fn main() {
    let flywheel = Flywheel::new_good(Kilo::<Gram>::new(300.0), Meter::new(0.5));
    let frequency = Rpm::new(60000.0);
    let energy = flywheel.energy(frequency);
    println!["{flywheel:?}"];
    println![
        "Energy at {} rpm: {} joules or {} kilowatthours",
        frequency.to_units::<Rpm>(),
        energy.to_units::<Joule>(),
        energy.to_units::<Watthour>() / 1000.0
    ];
}