uom/si/
energy.rs

1//! Energy (base unit joule, kg · m² · s⁻²).
2
3quantity! {
4    /// Energy (base unit joule, kg · m² · s⁻²).
5    quantity: Energy; "energy";
6    /// Dimension of energy, L²MT⁻² (base unit joule, kg · m² · s⁻²).
7    dimension: ISQ<
8        P2,     // length
9        P1,     // mass
10        N2,     // time
11        Z0,     // electric current
12        Z0,     // thermodynamic temperature
13        Z0,     // amount of substance
14        Z0>;    // luminous intensity
15    units {
16        @yottajoule: prefix!(yotta); "YJ", "yottajoule", "yottajoules";
17        @zettajoule: prefix!(zetta); "ZJ", "zettajoule", "zettajoules";
18        @exajoule: prefix!(exa); "EJ", "exajoule", "exajoules";
19        @petajoule: prefix!(peta); "PJ", "petajoule", "petajoules";
20        @terajoule: prefix!(tera); "TJ", "terajoule", "terajoules";
21        @gigajoule: prefix!(giga); "GJ", "gigajoule", "gigajoules";
22        @megajoule: prefix!(mega); "MJ", "megajoule", "megajoules";
23        @kilojoule: prefix!(kilo); "kJ", "kilojoule", "kilojoules";
24        @hectojoule: prefix!(hecto); "hJ", "hectojoule", "hectojoules";
25        @decajoule: prefix!(deca); "daJ", "decajoule", "decajoules";
26        /// Derived unit of energy.
27        @joule: prefix!(none); "J", "joule", "joules";
28        @decijoule: prefix!(deci); "dJ", "decijoule", "decijoules";
29        @centijoule: prefix!(centi); "cJ", "centijoule", "centijoules";
30        @millijoule: prefix!(milli); "mJ", "millijoule", "millijoules";
31        @microjoule: prefix!(micro); "µJ", "microjoule", "microjoules";
32        @nanojoule: prefix!(nano); "nJ", "nanojoule", "nanojoules";
33        @picojoule: prefix!(pico); "pJ", "picojoule", "picojoules";
34        @femtojoule: prefix!(femto); "fJ", "femtojoule", "femtojoules";
35        @attojoule: prefix!(atto); "aJ", "attojoule", "attojoules";
36        @zeptojoule: prefix!(zepto); "zJ", "zeptojoule", "zeptojoules";
37        @yoctojoule: prefix!(yocto); "yJ", "yoctojoule", "yoctojoules";
38
39        @petawatt_hour: 3.6_E18; "PW · h", "petawatt hour", "petawatt hours";
40        @terawatt_hour: 3.6_E15; "TW · h", "terawatt hour", "terawatt hours";
41        @gigawatt_hour: 3.6_E12; "GW · h", "gigawatt hour", "gigawatt hours";
42        @megawatt_hour: 3.6_E9; "MW · h", "megawatt hour", "megawatt hours";
43        @kilowatt_hour: 3.6_E6; "kW · h", "kilowatt hour", "kilowatt hours";
44        @hectowatt_hour: 3.6_E5; "hW · h", "hectowatt hour", "hectowatt hours";
45        @decawatt_hour: 3.6_E4; "daW · h", "decawatt hour", "decawatt hours";
46        @watt_hour: 3.6_E3; "W · h", "watt hour", "watt hours";
47        @milliwatt_hour: 3.6_E0; "mW · h", "milliwatt hour", "milliwatt hours";
48        @microwatt_hour: 3.6_E-3; "µW · h", "microwatt hour", "microwatt hours";
49
50        @petaelectronvolt: 1.602_176_634_E-4; "PeV", "petaelectronvolt", "petaelectronvolts";
51        @teraelectronvolt: 1.602_176_634_E-7; "TeV", "teraelectronvolt", "teraelectronvolts";
52        @gigaelectronvolt: 1.602_176_634_E-10; "GeV", "gigaelectronvolt", "gigaelectronvolts";
53        @megaelectronvolt: 1.602_176_634_E-13; "MeV", "megaelectronvolt", "megaelectronvolts";
54        @kiloelectronvolt: 1.602_176_634_E-16; "keV", "kiloelectronvolt", "kiloelectronvolts";
55        @hectoelectronvolt: 1.602_176_634_E-17; "heV", "hectoelectronvolt", "hectoelectronvolts";
56        @decaelectronvolt: 1.602_176_634_E-18; "daeV", "decaelectronvolt", "decaelectronvolts";
57        @electronvolt: 1.602_176_634_E-19; "eV", "electronvolt", "electronvolts";
58
59        /// Atomic unit of energy (Hartree energy).
60        @hartree: 4.359_744_722_207_1_E-18; "Eₕ", "hartree", "hartrees";
61
62        @btu_it: 1.055_056_E3; "Btu (IT)", "British thermal unit (IT)",
63            "British thermal units (IT)";
64        @btu: 1.054_350_E3; "Btu", "British thermal unit", "British thermal units";
65        @btu_39: 1.059_67_E3; "Btu₃₉", "British thermal unit (39 °F)",
66            "British thermal units (39 °F)";
67        @btu_59: 1.054_80_E3; "Btu₅₉", "British thermal unit (59 °F)",
68            "British thermal units (59 °F)";
69        @btu_60: 1.054_68_E3; "Btu₆₀", "British thermal unit (60 °F)",
70            "British thermal units (60 °F)";
71        @calorie_it: 4.186_8_E0; "cal (IT)", "calorie (IT)", "calories (IT)";
72        @calorie: 4.184_E0; "cal", "calorie", "calories";
73        @calorie_15: 4.185_80_E0; "cal₁₅", "calorie (15 °C)", "calories (15 °C)";
74        @calorie_20: 4.181_90_E0; "cal₂₀", "calorie (20 °C)", "calories (20 °C)";
75        @calorie_it_nutrition: 4.186_8_E3; "Cal (IT)", "Calorie (IT)", "Calories (IT)";
76        @calorie_nutrition: 4.184_E3; "Cal", "Calorie", "Calories";
77        @erg: 1.0_E-7; "erg", "erg", "ergs";
78        @foot_poundal: 4.214_011_E-2; "ft · pdl", "foot poundal", "foot poundals";
79        @foot_pound: 1.355_818_E0; "ft · lbf", "foot pound-force",
80            "foot pounds-force"; // @foot_pound_force
81        @kilocalorie_it: 4.186_8_E3; "kcal (IT)", "kilocalorie (IT)", "kilocalories (IT)";
82        @kilocalorie: 4.184_E3; "kcal", "kilocalorie", "kilocalories";
83        @quad: 1.055_056_E18; "10¹⁵ Btu (IT)", "quad", "quads";
84        @therm_ec: 1.055_06_E8; "thm (EC)", "therm (EC)", "therms (EC)";
85        @therm_us: 1.054_804_E8; "thm", "therm", "therms";
86        @ton_tnt: 4.184_E9; "t of TNT", "ton of TNT", "tons of TNT";
87        @watt_second: 1.0_E0; "W · s", "watt second", "watt seconds";
88    }
89}
90
91#[cfg(test)]
92mod tests {
93    storage_types! {
94        use crate::num::One;
95        use crate::si::area as a;
96        use crate::si::energy as e;
97        use crate::si::mass as m;
98        use crate::si::quantities::*;
99        use crate::si::time as t;
100        use crate::tests::Test;
101
102        #[test]
103        fn check_dimension() {
104            let _: Energy<V> = Area::new::<a::square_meter>(V::one())
105                * Mass::new::<m::kilogram>(V::one())
106                / (Time::new::<t::second>(V::one()) * Time::new::<t::second>(V::one()));
107        }
108
109        #[test]
110        fn check_units() {
111            test::<m::yottagram, e::zettajoule>();
112            test::<m::zettagram, e::exajoule>();
113            test::<m::exagram, e::petajoule>();
114            test::<m::petagram, e::terajoule>();
115            test::<m::teragram, e::gigajoule>();
116            test::<m::gigagram, e::megajoule>();
117            test::<m::megagram, e::kilojoule>();
118            test::<m::kilogram, e::joule>();
119            test::<m::decagram, e::centijoule>();
120            test::<m::gram, e::millijoule>();
121            test::<m::milligram, e::microjoule>();
122            test::<m::microgram, e::nanojoule>();
123            test::<m::nanogram, e::picojoule>();
124            test::<m::picogram, e::femtojoule>();
125            test::<m::femtogram, e::attojoule>();
126            test::<m::attogram, e::zeptojoule>();
127            test::<m::zeptogram, e::yoctojoule>();
128
129            fn test<M: m::Conversion<V>, E: e::Conversion<V>>() {
130                Test::assert_approx_eq(&Energy::new::<E>(V::one()),
131                    &(Area::new::<a::square_meter>(V::one())
132                        * Mass::new::<M>(V::one())
133                        / (Time::new::<t::second>(V::one()) * Time::new::<t::second>(V::one()))));
134            }
135        }
136    }
137}