Parsing tools

ChemistryLab.stoich_coef_roundFunction
stoich_coef_round(x::T; tol=1e-4) where {T<:Real} -> Union{Int, Rational, Float64}
stoich_coef_round(x) -> Any

Round stoichiometric coefficients to integer, rational, or float representation.

Arguments

  • x: numeric value to round.
  • tol: tolerance for rounding decisions (default 1e-4).

Returns

  • Integer if close to a whole number, Rational if a simple fraction (denominator < 10), or Float64 rounded to 5 digits otherwise. Non-numeric inputs are returned unchanged.

Examples

julia> stoich_coef_round(2.00001)
2

julia> stoich_coef_round(0.3333)
1//3

julia> stoich_coef_round(3.14159)
3.14159
source
ChemistryLab.phreeqc_to_unicodeFunction
phreeqc_to_unicode(s::AbstractString) -> String

Convert a PHREEQC formula string to Unicode representation with subscripts and superscripts.

Arguments

  • s: PHREEQC-formatted chemical formula string.

Returns

  • Unicode-formatted string with subscript coefficients and superscript charges.

Examples

julia> phreeqc_to_unicode("Ca+2")
"Ca²⁺"

julia> phreeqc_to_unicode("SO4-2")
"SO₄²⁻"
source
ChemistryLab.unicode_to_phreeqcFunction
unicode_to_phreeqc(s::AbstractString) -> String

Convert a Unicode formula string back to PHREEQC format.

Arguments

  • s: Unicode-formatted chemical formula.

Returns

  • PHREEQC-formatted string with plain text charges and coefficients.

Examples

julia> unicode_to_phreeqc("Ca²⁺")
"Ca+2"

julia> unicode_to_phreeqc("SO₄²⁻")
"SO4-2"
source
ChemistryLab.colored_formulaFunction
colored_formula(s::AbstractString; colorcharge=true) -> String

Generate a terminal-colored representation of a chemical formula.

Arguments

  • s: formula string.
  • colorcharge: if true, color the charge portion (default true).

Returns

  • String with ANSI color codes for terminal display.
source
for s in ("HSO₄²⁻", "HSO4-2", "(CaO)₄//₃(SiO₂)₁(H₂O)₁₃//₆")
    println(colored_formula(s))
end
HSO²⁻
HSO4-2
(CaO)₄//₃(SiO)(HO)₁₃//₆
ChemistryLab.parse_formulaFunction
parse_formula(formula::AbstractString) -> OrderedDict{Symbol,Number}

Parse a chemical formula string into an atomic composition dictionary.

Arguments

  • formula: formula string (supports parentheses, brackets, rational/decimal coefficients).

Returns

  • OrderedDict mapping element symbols to stoichiometric coefficients.

Examples

julia> parse_formula("H2O")
OrderedDict{Symbol, Int64} with 2 entries:
  :H => 2
  :O => 1

julia> parse_formula("Ca(OH)2")
OrderedDict{Symbol, Int64} with 3 entries:
  :Ca => 1
  :O  => 2
  :H  => 2
source
ChemistryLab.extract_chargeFunction
extract_charge(formula::AbstractString) -> Int

Extract the formal charge from a chemical formula string.

Arguments

  • formula: formula string with optional charge notation (e.g., "+2", "-", "3+").

Returns

  • Integer charge value (0 if no charge present).

Examples

julia> extract_charge("Ca+2")
2

julia> extract_charge("SO4-2")
-2

julia> extract_charge("H2O")
0
source
ChemistryLab.to_mendeleevFunction
to_mendeleev(oxides::AbstractDict{Symbol,T}) where {T<:Number} -> OrderedDict{Symbol,Number}

Convert cement oxide notation to Mendeleev element composition.

Arguments

  • oxides: dictionary mapping oxide symbols (C, S, A, etc.) to coefficients.

Returns

  • OrderedDict mapping element symbols to stoichiometric coefficients.

Examples

julia> to_mendeleev(OrderedDict(:C => 1, :S => 2))
OrderedDict{Symbol, Int64} with 3 entries:
  :Ca => 1
  :O  => 5
  :Si => 2

See also

source
ChemistryLab.parse_equationFunction
parse_equation(equation::AbstractString) -> Tuple{OrderedDict{String,Real}, OrderedDict{String,Real}, Char}

Parse a chemical equation string into reactants, products, and equality sign.

Arguments

  • equation: equation string with reactants and products separated by an equality operator.

Returns

  • Tuple of (reactants dict, products dict, equal sign char).

Examples

julia> reactants, products, sign = parse_equation("2H2 + O2 = 2H2O")
(OrderedDict("H2" => 2, "O2" => 1), OrderedDict("H2O" => 2), '=')

julia> reactants["H2"]
2

julia> products["H2O"]
2
source
ChemistryLab.colored_equationFunction
colored_equation(equation::AbstractString) -> String

Generate a terminal-colored representation of a chemical equation.

Arguments

  • equation: chemical equation string.

Returns

  • String with ANSI color codes for terminal display.
source
println(colored_equation("Ca₄Al₂(OH)₁₄(H₂O)₆ + 6H⁺ = 2AlO₂⁻ + 16H₂O@ + 4Ca²⁺"))
CaAl(OH)₁₄(HO) + 6H = 2AlO + 16HO@ + 4Ca²⁺
ChemistryLab.format_equationFunction
format_equation(coeffs::AbstractDict; scaling=1, equal_sign='=') -> String

Format a stoichiometric coefficient dictionary into an equation string.

Arguments

  • coeffs: dictionary mapping species to stoichiometric coefficients (negative = reactants, positive = products).
  • scaling: optional scaling factor for all coefficients (default 1).
  • equal_sign: equality operator to use (default '=').

Returns

  • Formatted equation string with automatic electron balancing if needed.

Examples

julia> coeffs = OrderedDict("H2" => -2, "O2" => -1, "H2O" => 2);

julia> format_equation(coeffs)
"2H2 + O2 = 2H2O"
source