updated README.md

master
elfeiin 2024-04-27 22:14:48 -07:00
parent d7a54eeef6
commit 1e5103ae65
Signed by: elfein
GPG Key ID: A53FDD4FD091A276
1 changed files with 25 additions and 7 deletions

View File

@ -5,11 +5,26 @@ Type-safe units handling library. Do arithmetic on measurements. Convert between
## example
```rs
#![feature(generic_const_exprs)]
use std::f64::consts::TAU;
use units::{
dimensions::{Energy, Frequency, Length, Mass, MassMoment, Scalar},
dimensions::{
Energy,
Frequency,
Length,
Mass,
MassMoment,
},
units::{
Gram,
Joule,
Meter,
Rpm,
Watthour,
},
Unit,
prefixes::Kilo,
units::{Grams, Joules, Meters, Rpm, Watthours},
};
#[derive(Debug)]
@ -28,6 +43,7 @@ impl Flywheel {
inertial_constant: 0.606,
}
}
pub fn new_good(mass: Mass, radius: Length) -> Self {
Self {
mass,
@ -36,9 +52,11 @@ impl Flywheel {
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
@ -46,15 +64,15 @@ impl Flywheel {
}
fn main() {
let flywheel = Flywheel::new_good(Mass::new::<Kilo<Grams>>(300.0), Length::new::<Meters>(0.5));
let frequency = Frequency::new::<Rpm>(60000.0);
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.val::<Rpm>(),
energy.val::<Joules>(),
energy.val::<Watthours>() / 1000.0
frequency.to_units::<Rpm>(),
energy.to_units::<Joule>(),
energy.to_units::<Watthour>() / 1000.0
];
}
```