Compare commits

...

4 Commits

Author SHA1 Message Date
elfeiin 3fcf6bc215
updated README 2024-03-01 15:30:53 -08:00
elfeiin b8d048bba4
updated examples 2024-03-01 15:30:00 -08:00
elfeiin 0e4eebb11d
fixes 2024-03-01 15:29:18 -08:00
elfeiin 73608b7892
made scalars easier 2024-03-01 15:22:56 -08:00
4 changed files with 92 additions and 57 deletions

View File

@ -6,18 +6,17 @@ Type-safe units handling library. Do arithmetic on measurements. Convert between
```rs
use std::f64::consts::TAU;
use units::{
dimensions::{Energy, Frequency, Length, Mass, MassMoment, Scalar},
prefixes::Kilo,
units::{Grams, Joules, Meters, Number, Rpm, Watthours},
units::{Grams, Joules, Meters, Rpm, Watthours},
};
#[derive(Debug)]
pub struct Flywheel {
mass: Mass,
radius: Length,
inertial_constant: Scalar,
inertial_constant: f64,
}
impl Flywheel {
@ -25,22 +24,24 @@ impl Flywheel {
Self {
mass,
radius,
inertial_constant: Scalar::new::<Number>(0.606),
// Cylinder
inertial_constant: 0.606,
}
}
pub fn new_good(mass: Mass, radius: Length) -> Self {
Self {
mass,
radius,
inertial_constant: Scalar::new::<Number>(1.0),
// Mass concentrated around rim
inertial_constant: 1.0,
}
}
pub fn inertial_moment(&self) -> MassMoment {
self.inertial_constant * self.mass * (self.radius * self.radius)
self.mass * self.inertial_constant * (self.radius * self.radius)
}
pub fn energy(&self, spin_rate: Frequency) -> Energy {
let angular_velocity = spin_rate * Scalar::new::<Number>(TAU);
self.inertial_moment() * (angular_velocity * angular_velocity) / Scalar::new::<Number>(2.0)
let angular_velocity = spin_rate * TAU;
self.inertial_moment() * (angular_velocity * angular_velocity) / 2.0
}
}
@ -48,7 +49,6 @@ 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 energy = flywheel.energy(frequency);
println!["Mass is in kg; Length is in meters; The inertial constant was taken from a note about moments of inertia in flywheels. A value of 1.0 means the mass is concentrated around the rim."];
println!["{flywheel:?}"];
println![
"Energy at {} rpm: {} joules or {} kilowatthours",

111
build.rs
View File

@ -35,12 +35,23 @@ impl Sub for Matrix {
fn impl_convert(dimension: &str) -> TokenStream {
let dim_ident = format_ident!["{dimension}"];
quote! {
pub fn new<T: Scale<#dim_ident>>(v: f64) -> Self {
Self(v * T::scale().0 + T::scale().1)
if dimension == "Scalar" {
quote! {
pub fn new(v: f64) -> Self {
Self(v)
}
pub fn val(self) -> f64 {
self.0
}
}
pub fn val<T: Scale<#dim_ident>>(self) -> f64 {
(self.0 - T::scale().1) / T::scale().0
} else {
quote! {
pub fn new<T: Scale<#dim_ident>>(v: f64) -> Self {
Self(v * T::scale().0 + T::scale().1)
}
pub fn val<T: Scale<#dim_ident>>(self) -> f64 {
(self.0 - T::scale().1) / T::scale().0
}
}
}
}
@ -186,6 +197,30 @@ fn main() {
let decl = quote! {
#[derive(Copy, Clone, Debug, Default)]
pub struct #basename_ident(f64);
impl Mul<f64> for #basename_ident {
type Output = Self;
fn mul(self, rhs: f64) -> Self::Output {
Self(self.0 * rhs)
}
}
impl Mul<#basename_ident> for f64 {
type Output = #basename_ident;
fn mul(self, rhs: #basename_ident) -> Self::Output {
#basename_ident(self * rhs.0)
}
}
impl Div<f64> for #basename_ident {
type Output = Self;
fn div(self, rhs: f64) -> Self::Output {
Self(self.0 / rhs)
}
}
impl Div<#basename_ident> for f64 {
type Output = #basename_ident;
fn div(self, rhs: #basename_ident) -> Self::Output {
#basename_ident(self * rhs.0)
}
}
};
dims_mod.extend(decl);
let mut conversion = quote! {};
@ -244,38 +279,38 @@ fn main() {
#[rustfmt::skip]
mod matricies {
use super::Matrix;
pub const Scalar: Matrix = Matrix([ 0, 0, 0, 0, 0, 0, 0]);
pub const Time: Matrix = Matrix([ 1, 0, 0, 0, 0, 0, 0]);
pub const Frequency: Matrix = Matrix([-1, 0, 0, 0, 0, 0, 0]);
pub const Tempoflux: Matrix = Matrix([-2, 0, 0, 0, 0, 0, 0]);
pub const Length: Matrix = Matrix([ 0, 1, 0, 0, 0, 0, 0]);
pub const Area: Matrix = Matrix([ 0, 2, 0, 0, 0, 0, 0]);
pub const Speed: Matrix = Matrix([-1, 1, 0, 0, 0, 0, 0]);
pub const SquareSpeed: Matrix = Matrix([-2, 2, 0, 0, 0, 0, 0]);
pub const Acceleration: Matrix = Matrix([-2, 1, 0, 0, 0, 0, 0]);
pub const Mass: Matrix = Matrix([ 0, 0, 1, 0, 0, 0, 0]);
pub const MassPerSqTime: Matrix = Matrix([-2, 0, 1, 0, 0, 0, 0]);
pub const Dispersion: Matrix = Matrix([ 0,-2, 1, 0, 0, 0, 0]);
pub const Density: Matrix = Matrix([ 0,-3, 1, 0, 0, 0, 0]);
pub const MassMoment: Matrix = Matrix([ 0, 2, 1, 0, 0, 0, 0]);
pub const Force: Matrix = Matrix([-2, 1, 1, 0, 0, 0, 0]);
pub const ForceMoment: Matrix = Matrix([-2, 2, 1, 0, 0, 0, 0]);
pub const Pressure: Matrix = Matrix([-2,-1, 1, 0, 0, 0, 0]);
pub const Energy: Matrix = Matrix([-2, 2, 1, 0, 0, 0, 0]);
pub const Power: Matrix = Matrix([-3, 2, 1, 0, 0, 0, 0]);
pub const Current: Matrix = Matrix([ 0, 0, 0, 1, 0, 0, 0]);
pub const Charge: Matrix = Matrix([ 1, 0, 0, 1, 0, 0, 0]);
pub const Capacitance: Matrix = Matrix([ 4,-2,-1, 1, 0, 0, 0]);
pub const Potential: Matrix = Matrix([-3,-2, 1,-1, 0, 0, 0]);
pub const Resistance: Matrix = Matrix([-3, 2, 1,-2, 0, 0, 0]);
pub const Conductance: Matrix = Matrix([ 3,-2,-1, 2, 0, 0, 0]);
pub const Flux: Matrix = Matrix([-2, 2, 1,-1, 0, 0, 0]);
pub const FluxDensity: Matrix = Matrix([ 2, 0, 1,-1, 0, 0, 0]);
pub const Inductance: Matrix = Matrix([-2, 2, 1,-2, 0, 0, 0]);
pub const Temperature: Matrix = Matrix([ 0, 0, 0, 0, 1, 0, 0]);
pub const Amount: Matrix = Matrix([ 0, 0, 0, 0, 0, 1, 0]);
pub const Catalytic: Matrix = Matrix([-1, 0, 0, 0, 0, 1, 0]);
pub const Intensity: Matrix = Matrix([ 0, 0, 0, 0, 0, 0, 1]);
pub const Scalar: Matrix = Matrix([ 0, 0, 0, 0, 0, 0, 0]);
pub const Time: Matrix = Matrix([ 1, 0, 0, 0, 0, 0, 0]);
pub const Frequency: Matrix = Matrix([-1, 0, 0, 0, 0, 0, 0]);
pub const Tempoflux: Matrix = Matrix([-2, 0, 0, 0, 0, 0, 0]);
pub const Length: Matrix = Matrix([ 0, 1, 0, 0, 0, 0, 0]);
pub const Area: Matrix = Matrix([ 0, 2, 0, 0, 0, 0, 0]);
pub const Speed: Matrix = Matrix([-1, 1, 0, 0, 0, 0, 0]);
pub const SquareSpeed: Matrix = Matrix([-2, 2, 0, 0, 0, 0, 0]);
pub const Acceleration: Matrix = Matrix([-2, 1, 0, 0, 0, 0, 0]);
pub const Mass: Matrix = Matrix([ 0, 0, 1, 0, 0, 0, 0]);
pub const MassPerSqTime: Matrix = Matrix([-2, 0, 1, 0, 0, 0, 0]);
pub const Dispersion: Matrix = Matrix([ 0,-2, 1, 0, 0, 0, 0]);
pub const Density: Matrix = Matrix([ 0,-3, 1, 0, 0, 0, 0]);
pub const MassMoment: Matrix = Matrix([ 0, 2, 1, 0, 0, 0, 0]);
pub const Force: Matrix = Matrix([-2, 1, 1, 0, 0, 0, 0]);
pub const ForceMoment: Matrix = Matrix([-2, 2, 1, 0, 0, 0, 0]);
pub const Pressure: Matrix = Matrix([-2,-1, 1, 0, 0, 0, 0]);
pub const Energy: Matrix = Matrix([-2, 2, 1, 0, 0, 0, 0]);
pub const Power: Matrix = Matrix([-3, 2, 1, 0, 0, 0, 0]);
pub const Current: Matrix = Matrix([ 0, 0, 0, 1, 0, 0, 0]);
pub const Charge: Matrix = Matrix([ 1, 0, 0, 1, 0, 0, 0]);
pub const Capacitance: Matrix = Matrix([ 4,-2,-1, 1, 0, 0, 0]);
pub const Potential: Matrix = Matrix([-3,-2, 1,-1, 0, 0, 0]);
pub const Resistance: Matrix = Matrix([-3, 2, 1,-2, 0, 0, 0]);
pub const Conductance: Matrix = Matrix([ 3,-2,-1, 2, 0, 0, 0]);
pub const Flux: Matrix = Matrix([-2, 2, 1,-1, 0, 0, 0]);
pub const FluxDensity: Matrix = Matrix([ 2, 0, 1,-1, 0, 0, 0]);
pub const Inductance: Matrix = Matrix([-2, 2, 1,-2, 0, 0, 0]);
pub const Temperature: Matrix = Matrix([ 0, 0, 0, 0, 1, 0, 0]);
pub const Amount: Matrix = Matrix([ 0, 0, 0, 0, 0, 1, 0]);
pub const Catalytic: Matrix = Matrix([-1, 0, 0, 0, 0, 1, 0]);
pub const Intensity: Matrix = Matrix([ 0, 0, 0, 0, 0, 0, 1]);
}
use matricies::*;
@ -287,7 +322,7 @@ fn gen_units_list() -> HashMap<Matrix, Dimension> {
Scalar,
Dimension {
basename: "Scalar",
conversions: vec![("Number", (1.0, 0.0))],
conversions: vec![],
},
);
output.insert(

View File

@ -4,5 +4,5 @@ use units::{
};
fn main() {
println!["{}", Length::new::<Meters>(2.0).val::<Feet>()];
println!["{}", Length::new::<Feet>(1601.0).val::<Meters>()];
}

View File

@ -1,16 +1,15 @@
use std::f64::consts::TAU;
use units::{
dimensions::{Energy, Frequency, Length, Mass, MassMoment, Scalar},
prefixes::Kilo,
units::{Grams, Joules, Meters, Number, Rpm, Watthours},
units::{Grams, Joules, Meters, Rpm, Watthours},
};
#[derive(Debug)]
pub struct Flywheel {
mass: Mass,
radius: Length,
inertial_constant: Scalar,
inertial_constant: f64,
}
impl Flywheel {
@ -18,22 +17,24 @@ impl Flywheel {
Self {
mass,
radius,
inertial_constant: Scalar::new::<Number>(0.606),
// Cylinder
inertial_constant: 0.606,
}
}
pub fn new_good(mass: Mass, radius: Length) -> Self {
Self {
mass,
radius,
inertial_constant: Scalar::new::<Number>(1.0),
// Mass concentrated around rim
inertial_constant: 1.0,
}
}
pub fn inertial_moment(&self) -> MassMoment {
self.inertial_constant * self.mass * (self.radius * self.radius)
self.mass * self.inertial_constant * (self.radius * self.radius)
}
pub fn energy(&self, spin_rate: Frequency) -> Energy {
let angular_velocity = spin_rate * Scalar::new::<Number>(TAU);
self.inertial_moment() * (angular_velocity * angular_velocity) / Scalar::new::<Number>(2.0)
let angular_velocity = spin_rate * TAU;
self.inertial_moment() * (angular_velocity * angular_velocity) / 2.0
}
}
@ -41,7 +42,6 @@ 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 energy = flywheel.energy(frequency);
println!["Mass is in kg; Length is in meters; The inertial constant was taken from a note about moments of inertia in flywheels. A value of 1.0 means the mass is concentrated around the rim."];
println!["{flywheel:?}"];
println![
"Energy at {} rpm: {} joules or {} kilowatthours",