#parser #formula #chemical #nested #weight #percent #chemisty

chemical-formula

Chemical formula parser that can handle wt% and nested structure

1 unstable release

0.1.0 Feb 22, 2024

#581 in Science

MIT/Apache

61KB
1K SLoC

Rust 1K SLoC // 0.0% comments Pest 128 SLoC

chemical-formula-rs

Lib.rs Crates.io Docs.rs

Rust 1.74

Crates.io - License

GitHub open issues open pull requests good first issues

This crate provides a simple way to parse and manipulate chemical formulas including weight percent and nested formulas.

The initial motivation was to parse a formula such as Pt5wt%/SiO2, which are heavily used annotation in the field of Heterogeneous catalysis. Another motivatation was to parse a formula that is nested such as (Pt5wt%/SiO2)50wt%(CeO2)50wt%, which can be used to describe a composite material. We also provide a way simple API to convert between molecular formula and weight percent.

Installation

Please use cargo-edit to always add the latest version of this library:

cargo add chemical-formula

Example

use chemical_formula::prelude::*;

fn main() {
    let formula = parse_formula("H2O").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {O, H}, stoichiometry: {H: 2.0, O: 1.0}, wt_percent: {} }

    println!("Molecular weight: {:?}", formula.molecular_weight());
    // Molecular weight: Ok(18.015)

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {O, H}, stoichiometry: {}, wt_percent: {H: 11.19067443796836, O: 88.80932556203165} }

    let formula = parse_formula("Pt5wt%/SiO2").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, O: 2.0}, wt_percent: {Pt: 5.0} }

    println!(
        "Molecular Formula: {:?}",
        formula.to_molecular_formula().unwrap()
    );
    // Molecular Formula: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {Si: 1.0, Pt: 0.016209751480873558, O: 2.0}, wt_percent: {} }

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {Si, O, Pt}, stoichiometry: {}, wt_percent: {Pt: 5.0, Si: 44.406487692026026, O: 50.59351230797397} }

    let formula = parse_formula("(Pt5wt%/SiO2)50wt%(CeO2)50wt%").unwrap();

    println!("Orignal formula: {:?}", formula);
    // Orignal formula: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }

    println!("Wt%: {:?}", formula.to_wt_percent().unwrap());
    // Wt%: ChemicalFormula { element: {Si, Ce, Pt, O}, stoichiometry: {}, wt_percent: {Si: 22.203243846013017, O: 34.59233931398559, Pt: 2.5000000000000004, Ce: 40.70441684000139} }
}

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

See CONTRIBUTING for more information.

Code of Conduct

Changelog

Versioning

chemical-formula-rs strictly follows Semantic Versioning 2.0.0 with the following exceptions:

  • The minor version will not reset to 0 on major version changes (except for v1).
    Consider it the global feature level.
  • The patch version will not reset to 0 on major or minor version changes (except for v0.1 and v1).
    Consider it the global patch level.

This includes the Rust version requirement specified above.
Earlier Rust versions may be compatible, but this can change with minor or patch releases.

Which versions are affected by features and patches can be determined from the respective headings in CHANGELOG.md.

Dependencies

~2.2–3MB
~60K SLoC