-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add string and chemical formulae utility functions
""" parse_number_name(nname::AbstractString) -> (number, name) Parse a string of form "-1*A" """ """ ChemistryUtils.parse_chemical_formula(formula::AbstractString) -> element_counts::OrderedDict{Symbol, Int64} Parse a chemical formula into a Dict of element counts """ """ ChemistryUtils.calc_molar_mass(element_counts::AbstractDict) -> molar_mass::Float64 Add up element masses to get molar mass (g) """ """ Constants.STANDARD_ATOMIC_WEIGHTS IUPAC recommended values of relative atomic masses of sources in the local environment of the Earth's crust and atmosphere (ie with Earth-specific isotope composition) """
- Loading branch information
Showing
7 changed files
with
137 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
module ChemistryUtils | ||
|
||
import PALEOboxes as PB | ||
import OrderedCollections | ||
|
||
""" | ||
parse_chemical_formula(formula::AbstractString) -> element_counts::OrderedDict{Symbol, Int64} | ||
Parse a chemical formula into a Dict of element counts | ||
# Examples | ||
```jldoctest; setup = :(import PALEOboxes as PB) | ||
julia> PB.ChemistryUtils.parse_chemical_formula("HO2NO2") | ||
OrderedCollections.OrderedDict{Symbol, Float64} with 3 entries: | ||
:H => 1.0 | ||
:N => 1.0 | ||
:O => 4.0 | ||
``` | ||
""" | ||
function parse_chemical_formula(formula::AbstractString) | ||
elementcounts = OrderedCollections.OrderedDict{Symbol, Float64}() | ||
for m in eachmatch(r"([A-Z][a-z]*)(\d*)", formula) | ||
elementstr, countstr = m.captures | ||
element = Symbol(elementstr) | ||
count = isempty(countstr) ? 1 : parse(Int, countstr) | ||
elementcounts[element] = get(elementcounts, element, 0) + count | ||
end | ||
|
||
sort!(elementcounts) | ||
|
||
return elementcounts | ||
end | ||
|
||
|
||
|
||
""" | ||
calc_molar_mass(element_counts::AbstractDict) -> molar_mass::Float64 | ||
calc_molar_mass(element_counts::AbstractVector{<:Pair}) -> molar_mass::Float64 | ||
calc_molar_mass(element_counts::NamedTuple) -> molar_mass::Float64 | ||
Add up element masses to get molar mass (g) | ||
# Examples | ||
```jldoctest; setup = :(import PALEOboxes as PB) | ||
julia> PB.ChemistryUtils.calc_molar_mass([:C=>1, :O=>2]) | ||
44.009 | ||
``` | ||
""" | ||
function calc_molar_mass(element_counts) | ||
|
||
molar_mass = 0.0 | ||
for (element, count) in element_counts | ||
element isa Symbol || error("element $element is not a Symbol eg :O, :C") | ||
haskey(PB.Constants.STANDARD_ATOMIC_WEIGHTS, element) || error("unknown element $element") | ||
count isa Number || error("element count $count is not a Number") | ||
molar_mass += count*PB.Constants.STANDARD_ATOMIC_WEIGHTS[element] | ||
end | ||
|
||
return molar_mass | ||
end | ||
|
||
calc_molar_mass(element_counts::NamedTuple) = calc_molar_mass(pairs(element_counts)) | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
""" | ||
parse_number_name(nname::AbstractString) -> (number, name) | ||
Parse a string of form "-1*A" | ||
""" | ||
function parse_number_name( | ||
nname::AbstractString; | ||
sep=['*', ' '], | ||
number_first=true, | ||
io=IOBuffer(), | ||
errmsg="invalid field in nname, not of form number*name: ", | ||
) | ||
|
||
# parse multiplier | ||
svmn = split(nname, sep, keepempty=false) | ||
mult = nothing | ||
if length(svmn) == 1 | ||
mult, name = 1, svmn[1] | ||
elseif length(svmn) == 2 | ||
if !number_first | ||
tmp = svmn[1] | ||
svmn[1] = svmn[2] | ||
svmn[2] = tmp | ||
end | ||
mult = tryparse(Int64, svmn[1]) | ||
if isnothing(mult) | ||
mult = tryparse(Float64, svmn[1]) | ||
end | ||
name = svmn[2] | ||
end | ||
|
||
!isnothing(mult) || infoerror(io, errmsg*nname) | ||
|
||
return (mult, name) | ||
end | ||
|
||
parse_name_to_power_number(nname::AbstractString) = parse_number_name(nname; sep=['^', ' '], number_first=false) |