#si-units #physics #units #unit #si #array

quantity

Representation of quantites, i.e. of unit valued scalars and arrays.

18 releases (9 breaking)

new 0.10.0 Dec 5, 2024
0.9.1 Nov 28, 2024
0.9.0 Oct 24, 2024
0.8.0 Apr 11, 2024
0.2.0 Jul 26, 2021

#26 in Science

Download history 8/week @ 2024-08-23 17/week @ 2024-08-30 52/week @ 2024-09-20 25/week @ 2024-09-27 12/week @ 2024-10-04 3/week @ 2024-10-11 156/week @ 2024-10-18 135/week @ 2024-10-25 9/week @ 2024-11-01 10/week @ 2024-11-15 90/week @ 2024-11-22 148/week @ 2024-11-29

250 downloads per month
Used in 7 crates

MIT/Apache

64KB
1K SLoC

quantity

crate documentation

Representation of quantities, i.e. of unit valued scalars and arrays.

The units are checked at compile time and can be arbitrarily complex. Additional to simple scalar quantities, it also provides utilities for vector valued quantities, based on the ndarray crate, where all entries share the same unit.

The crate is published together with the standalone Python package si-units. Together with PyO3, the data types defined in the Rust crate can be directly used in a Python interface. See the example for details.

Installation and Usage

Add this to your Cargo.toml:

[dependencies]
quantity = "0.10"

Examples

Calculate pressure of an ideal gas.

let temperature = 25.0 * CELSIUS;
let volume = 1.5 * METER.powi(3);
let moles = 75.0 * MOL;
let pressure = moles * RGAS * temperature / volume;
println!("{:.5}", pressure);            // 123.94785 kPa

Calculate the gravitational pull of the moon on the earth.

let mass_earth = 5.9724e24 * KILOGRAM;
let mass_moon = 7.346e22 * KILOGRAM;
let distance = 383.398 * KILO * METER;
let force = G * mass_earth * mass_moon / distance.powi(2);
println!("{:.5e}", force);              // 1.99208e26 N

Calculate the pressure distribution in the atmosphere using the barometric formula.

let z = SIArray1::linspace(1.0 * METER, 70.0 * KILO * METER, 10)?;
let g = 9.81 * METER / SECOND.powi(2);
let m = 28.949 * GRAM / MOL;
let t = 10.0 * CELSIUS;
let p0 = BAR;
let pressure = p0 * (-&z * m * g).to_reduced(RGAS * t)?.mapv(f64::exp);
for i in 0..10 {
    println!("z = {:8.5}   p = {:9.5}", z.get(i), pressure.get(i));
}
// z =  1.00000  m   p =  99.98794 kPa
// z =  7.77867 km   p =  39.12796 kPa
// z = 15.55633 km   p =  15.31182 kPa
// z = 23.33400 km   p =   5.99192 kPa
// z = 31.11167 km   p =   2.34480 kPa
// z = 38.88933 km   p = 917.58301  Pa
// z = 46.66700 km   p = 359.07479  Pa
// z = 54.44467 km   p = 140.51557  Pa
// z = 62.22233 km   p =  54.98750  Pa
// z = 70.00000 km   p =  21.51808  Pa

Documentation

For the rust documentation, see here.

For the python documentation, see here.

Development

To build the project including the bindings to python, we use maturin.

When developing, use

maturin develop --release

To build the python wheels, use

maturin build --release

To build the documentation you need sphinx and some additional packages. From the root directory, type

cd si-units/docs
make html

To run the doctests, from the root directory, type

cd si-units/docs
make doctest

Dependencies

~0.3–2MB
~41K SLoC