Skip to content

Commit

Permalink
implemented unitsystems for rust
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Jan 1, 2021
1 parent 9490c2a commit ff609fe
Show file tree
Hide file tree
Showing 8 changed files with 919 additions and 11 deletions.
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,14 @@ docs/site/
# committed for packages, but should be committed for applications that require a static
# environment.
Manifest.toml

# Generated by Cargo
# will have compiled files and executables
/target/

# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
Cargo.lock

# These are backup files generated by rustfmt
**/*.rs.bk
17 changes: 17 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "unitsystems"
version = "0.1.0"
authors = ["Michael Reed"]
edition = "2018"
license = "MIT"
description = "Physical unit systems (Metric, English, Natural, etc...)"
homepage = "https://geophysics.crucialflow.com/dev/units"
documentation = "https://geophysics.crucialflow.com/dev/units"
repository = "https://github.com/chakravala/UnitSystems.jl"
readme = "README.md"
keywords = ["physics","units","scientific","dimensional-analysis","measurement"]
categories = ["science","mathematics","embedded","data-structures","config"]

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
12 changes: 6 additions & 6 deletions src/UnitSystems.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ module UnitSystems

import Base: @pure, length, time

export slug, ft, KJ1990, KJ2014, RK1990, RK2014, mₑ1990, mₑ2014, temp, units
export slug, ft, ftUS, KJ1990, KJ2014, RK1990, RK2014, mₑ1990, mₑ2014, temp, units
export slugs, kilograms, lbm, meters, feet, rankine, kelvin, moles, molecules
export UnitSystem, US, SI, CGS, CGS2019, CGSm, CGSe, HLU, FFF
export UnitSystem, US, SI, MKS, CGS, CGS2019, CGSm, CGSe, HLU, FFF

const Systems = (:Metric,:SI2019,:CODATA,:Conventional,:MTS,:English,:EnglishUS,:IAU,:SI1976,:Mixed,:ESU2019,:EMU2019,:EMU,:ESU,:Gauss,:LorentzHeaviside,:Thomson,:Kennelly,:Planck,:PlanckGauss,:Stoney,:Hartree,:Rydberg,:Schrodinger,:Electronic,:Natural,:NaturalGauss,:QCD,:QCDGauss,:QCDoriginal)
const Constants = (:hyperfine,:lightspeed,:planck,:planckreduced,:electronmass,:molarmass,:boltzmann,:permeability,:rationalization,:lorentz,:luminousefficacy)
Expand All @@ -31,7 +31,7 @@ listext(x) = join(x,"`, `")
Fundamental constants of physics are: `kB` Boltzmann's constant, `ħ` reduced Planck's constant, `𝘤` speed of light, `μ₀` vacuum permeability, `mₑ` electron rest mass, `λ` Gauss rationalization, and `αL` Lorentz's constant.
Primarily the `Metric` SI unit system is used in addition to the historic `English` engineering unit system.
These constants induce derived values for `avogadro`, `boltzmann`, `universal`, `planck`, `planckreduced`, `lightspeed`, `planckmass`, `atomicmass`, `protonmass`, `electronmass`, `newton`, `einstein`, `permeability`, `permittivity`, `coulomb`, and
additional constants `molarmass`, `hyperfine`, `luminousefficacy`, `stefan`, `radiationintensity`, `ampere`, `lorentz`, `biotsavart`, `rationalization`, `impedance`, `charge`, `magneton`, `conductance`, `faraday`, `magneticflux`, `josephson`, `klitzing`, `hartree`, `rydberg`, `bohr`, and `bohrreduced`.
additional constants `molarmass`, `hyperfine`, `luminousefficacy`, `stefan`, `radiationdensity`, `ampere`, `lorentz`, `biotsavart`, `rationalization`, `impedance`, `charge`, `magneton`, `conductance`, `faraday`, `magneticflux`, `josephson`, `klitzing`, `hartree`, `rydberg`, `bohr`, and `bohrreduced`.
Additional reference `UnitSystem` variants: `EMU`, `ESU`, `Gauss`, `LorentzHeaviside`, `MTS`, `SI2019`, `CODATA`, `Conventional`, `IAU`, `EnglishUS`; and natural atomic units based on gravitational coupling `αG` and the fine structure `1/αinv` constant (`Planck`, `PlanckGauss`, `Stoney`, `Hartree`, `Rydberg`, `Schrodinger`, `Electronic`, `Natural`, `NaturalGauss`, `QCD`, `QCDGauss`, and `QCDoriginal`).
Expand Down Expand Up @@ -98,8 +98,8 @@ end

# common conversion factors

const atm,g₀,lbm = 101325.0,9.80665,32.17404856 # lb-f to pdl
const slug,ft,ftUS,rankine,kelvin = 0.45359237lbm,0.3048,1200/3937,5/9,9/5
const g₀,ft,ftUS = 9.80665,0.3048,1200/3937; const lbm,lbmUS = g₀/ft,g₀/ftUS
const slug,slugUS,rankine,kelvin,atm = 0.45359237lbm,0.45359237lbmUS,5/9,9/5,101325.0
const kcalₜₕ,kcal₄,kcal₁₀,kcal₂₀,kcalₘ,kcalᵢₜ = 4184,4204,4185.5,4182,4190,4186.8
const calₜₕ,cal₄,cal₁₀,cal₂₀,calₘ,calᵢₜ = (kcalₜₕ,kcal₄,kcal₁₀,kcal₂₀,kcalₘ,kcalᵢₜ)./1e3
const kcal = kcalₜₕ; const cal = kcal/1000 # calₜₕ thermal calorie
Expand Down Expand Up @@ -147,7 +147,7 @@ const IAU = UnitSystem{Rᵤ*mₑ/μₑᵤ/0.001/Jₛ,ħ/day/Jₛ,day*𝘤/au,4π

const mf = mass(90/lbm,Metric,English); const Jf = mf*(201.168/14day)^2
const FFF = UnitSystem{1000Rᵤ*mₑ/μₑᵤ*rankine/Jf,ħ/14day/Jf,14day*𝘤/201.168,0,mₑ/mf}()
const units, US, SI, temp = UnitSystem, UnitSystem, SI2019, temperature
const units, US, SI, MKS, temp = UnitSystem, UnitSystem, SI2019, Metric, temperature
const CGS, CGS2019, CGSm, CGSe, HLU = Gauss, EMU2019, EMU, ESU, LorentzHeaviside

# natural units
Expand Down
2 changes: 1 addition & 1 deletion src/electromagnetic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ $(resistance(Metric,SI2019))
@pure resistance(U::UnitSystem,S::UnitSystem) = unit(electricpotential(U,S)/current(U,S))

"""
$(convertext(:conductance,"electricpotential(U,S)/current(U,S)"))
$(convertext(:conductance,"current(U,S)/electricpotential(U,S)"))
Electrical `conductance` or `current` per `electricpotential` (S, Ω⁻¹, A⋅V⁻¹), unit conversion factor.
Expand Down
6 changes: 3 additions & 3 deletions src/kinematic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ $(specificenergy(EnglishUS,English))
@doc """
$(convertext(:mass,"electronmass(S)/electronmass(U)"))
Inertal `mass` or resitance to aceleration or quantity of matter (kg), unit conversion factor.
Inertal `mass` or matter quantity or resistance to aceleration (kg), unit conversion factor.
```Julia
julia> mass(CGS,Metric) # kg⋅g⁻¹
Expand Down Expand Up @@ -654,7 +654,7 @@ $(lineardensity(English,Metric))
"""
$(convertext(:massflow,"mass(U,S)/time(U,S)"))
Rate of `massflow` or `mass` per `time` (kg⋅s¹), unit conversion factor.
Rate of `massflow` or `mass` per `time` (kg⋅s¹), unit conversion factor.
```Julia
julia> massflow(CGS,Metric) # kg⋅g⁻¹
Expand Down Expand Up @@ -780,7 +780,7 @@ julia> soundexposure(English,Metric) # Pa²⋅ft⁴⋅lb⁻²
$(soundexposure(English,Metric))
```
"""
@pure soundexposure(U::UnitSystem,S::UnitSystem) = pressure(U,S)^2/time(U,S)
@pure soundexposure(U::UnitSystem,S::UnitSystem) = unit(time(U,S)*pressure(U,S)^2)

"""
$(convertext(:specificimpedance,"pressure(U,S)/speed(U,S)"))
Expand Down
Loading

0 comments on commit ff609fe

Please sign in to comment.