pub const ATOMIC_MASSES: [f64; 119] = [
0.0, 1.008, 4.002_602, 6.94, 9.012_183, 10.81, 12.011, 14.007, 15.999, 18.998, 20.180, 22.990, 24.305, 26.982, 28.085, 30.974, 32.06, 35.45, 39.948, 39.098, 40.078, 44.956, 47.867, 50.942, 51.996, 54.938, 55.845, 58.933, 58.693, 63.546, 65.38, 69.723, 72.630, 74.922, 78.971, 79.904, 83.798, 85.468, 87.62, 88.906, 91.224, 92.906, 95.95, 98.0, 101.07, 102.91, 106.42, 107.87, 112.41, 114.82, 118.71, 121.76, 127.60, 126.90, 131.29, 132.91, 137.33, 138.91, 140.12, 140.91, 144.24, 145.0, 150.36, 151.96, 157.25, 158.93, 162.50, 164.93, 167.26, 168.93, 173.05, 174.97, 178.49, 180.95, 183.84, 186.21, 190.23, 192.22, 195.08, 196.97, 200.59, 204.38, 207.2, 208.98, 209.0, 210.0, 222.0, 223.0, 226.0, 227.0, 232.04, 231.04, 238.03, 237.0, 244.0, 243.0, 247.0, 247.0, 251.0, 252.0, 257.0, 258.0, 259.0, 262.0, 267.0, 268.0, 271.0, 272.0, 270.0, 277.0, 276.0, 281.0, 282.0, 285.0, 286.0, 289.0, 290.0, 293.0, 294.0, ];
pub const SYMBOLS: [&str; 119] = [
"", "H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S",
"Cl", "Ar", "K", "Ca", "Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge",
"As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr", "Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd",
"In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd", "Pm", "Sm", "Eu", "Gd",
"Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
"Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm",
"Bk", "Cf", "Es", "Fm", "Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn",
"Nh", "Fl", "Mc", "Lv", "Ts", "Og",
];
pub fn atomic_mass(z: usize) -> f64 {
if (1..=118).contains(&z) { ATOMIC_MASSES[z] } else { 0.0 }
}
pub fn symbol(z: usize) -> &'static str {
if (1..=118).contains(&z) { SYMBOLS[z] } else { "" }
}
pub fn molar_mass(formula: &crate::Vec1<(usize, u32)>) -> f64 {
let mut m = 0.0;
for i in 1..=formula.len() {
let (z, count) = formula[i];
m += atomic_mass(z) * count as f64;
}
m
}
#[cfg(test)]
mod tests {
use super::*;
use crate::vec1;
#[test]
fn atomic_mass_h() {
assert!((atomic_mass(1) - 1.008).abs() < 1e-3);
}
#[test]
fn atomic_mass_c() {
assert!((atomic_mass(6) - 12.011).abs() < 1e-3);
}
#[test]
fn atomic_mass_o() {
assert!((atomic_mass(8) - 15.999).abs() < 1e-3);
}
#[test]
fn atomic_mass_out_of_range() {
assert_eq!(atomic_mass(0), 0.0);
assert_eq!(atomic_mass(200), 0.0);
}
#[test]
fn symbols() {
assert_eq!(symbol(1), "H");
assert_eq!(symbol(6), "C");
assert_eq!(symbol(8), "O");
assert_eq!(symbol(79), "Au");
assert_eq!(symbol(0), "");
}
#[test]
fn molar_mass_water() {
let water = vec1![(1usize, 2u32), (8, 1)];
let m = molar_mass(&water);
assert!((m - 18.015).abs() < 0.01);
}
#[test]
fn molar_mass_co2() {
let co2 = vec1![(6usize, 1u32), (8, 2)];
let m = molar_mass(&co2);
assert!((m - 44.009).abs() < 0.01);
}
}