Parsing tools
ChemistryLab.colored_equationChemistryLab.colored_formulaChemistryLab.extract_chargeChemistryLab.format_equationChemistryLab.parse_equationChemistryLab.parse_formulaChemistryLab.phreeqc_to_unicodeChemistryLab.stoich_coef_roundChemistryLab.to_mendeleevChemistryLab.unicode_to_phreeqc
ChemistryLab.stoich_coef_round — Function
stoich_coef_round(x::T; tol=1e-4) where {T<:Real} -> Union{Int, Rational, Float64}
stoich_coef_round(x) -> AnyRound 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.14159ChemistryLab.phreeqc_to_unicode — Function
phreeqc_to_unicode(s::AbstractString) -> StringConvert 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₄²⁻"ChemistryLab.unicode_to_phreeqc — Function
unicode_to_phreeqc(s::AbstractString) -> StringConvert 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"ChemistryLab.colored_formula — Function
colored_formula(s::AbstractString; colorcharge=true) -> StringGenerate 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.
for s in ("HSO₄²⁻", "HSO4-2", "(CaO)₄//₃(SiO₂)₁(H₂O)₁₃//₆")
println(colored_formula(s))
endHSO₄²⁻
HSO4-2
(CaO)₄//₃(SiO₂)₁(H₂O)₁₃//₆ChemistryLab.parse_formula — Function
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 => 2ChemistryLab.extract_charge — Function
extract_charge(formula::AbstractString) -> IntExtract 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")
0ChemistryLab.to_mendeleev — Function
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 => 2See also
ChemistryLab.parse_equation — Function
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"]
2ChemistryLab.colored_equation — Function
colored_equation(equation::AbstractString) -> StringGenerate a terminal-colored representation of a chemical equation.
Arguments
equation: chemical equation string.
Returns
- String with ANSI color codes for terminal display.
println(colored_equation("Ca₄Al₂(OH)₁₄(H₂O)₆ + 6H⁺ = 2AlO₂⁻ + 16H₂O@ + 4Ca²⁺"))Ca₄Al₂(OH)₁₄(H₂O)₆ + 6H⁺ = 2AlO₂⁻ + 16H₂O@ + 4Ca²⁺ChemistryLab.format_equation — Function
format_equation(coeffs::AbstractDict; scaling=1, equal_sign='=') -> StringFormat 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"