Chemical Formula Manipulation
ChemistryLab allows you to create and manipulate chemical formulas. It is based on Formula which is a structure (struct) which contains an expression, a writing of the formula close to those found in the Phreeqc databases, a unicode expression as well as a composition in the form of dictionaries and a charge.
struct Formula{T<:Number}
expr::String
phreeqc::String
unicode::String
colored::String
composition::OrderedDict{Symbol,T}
charge::Int8
endFormula construction
Formulas can be constructed:
- by parsing a string containing eventually fractional or decimal coefficients
fgen = Formula("C3AFS5//8H4.32")Formula{Real}
formula: C3AFS5//8H4.32 ◆ C₃AFS⅝H₄.₃₂ ◆ C₃AFS⅝H₄.₃₂
composition: C => 3, A => 1, F => 1, S => 5//8, H => 4.32
charge: 0
- from symbols representing atoms
fCO2 = :C + 2 * :OFormula{Int64}
formula: CO2 ◆ CO₂ ◆ CO₂
composition: O => 2, C => 1
charge: 0
Charges can also be included during the creation in two different ways:
fHSO₄⁻ = :H+:S+4*:O+:eFormula{Int64}
formula: HSO4- ◆ HSO₄- ◆ HSO₄⁻
composition: H => 1, S => 1, O => 4
charge: -1
Or:
fNa⁺ = :Na+:ZzFormula{Int64}
formula: Na+ ◆ Na⁺
composition: Na => 1
charge: 1
Type of Formula
The type of the Formula struct being associated with the most complex type of the set of coefficients.
typeof(Formula("H2O"))Formula{Int64}typeof(Formula("C3AFS5//8H4.32"))Formula{Real}Change of type
Coefficient types can be converted a posteriori.
convert(Float64, Formula("H2O"))Formula{Float64}
formula: H2O ◆ H₂O ◆ H₂O
composition: H => 2.0, O => 1.0
charge: 0