Skip to content

Commit

Permalink
Support Unicode occupations/states when parsing configurations (#36, #73
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jagot committed Mar 24, 2021
1 parent 72e13fe commit 5c5a287
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/configurations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,9 @@ function get_noble_core_name(config::Configuration{O}) where O
end

function state_sym(state::AbstractString)
if state == "c"
if state == "c" || state == ""
:closed
elseif state == "i"
elseif state == "i" || state == ""
:inactive
else
:open
Expand All @@ -339,13 +339,21 @@ end

function parse_orbital_occupation(::Type{O}, orb_str) where {O<:AbstractOrbital}
m = match(r"^(([0-9]+|.)([a-z]|\[[0-9]+\])[-]{0,1})([0-9]*)([*ci]{0,1})$", orb_str)
parse(O, m[1]) , (m[4] == "") ? 1 : parse(Int, m[4]), state_sym(m[5])
m2 = match(r"^(([0-9]+|.)([a-z]|\[[0-9]+\])[-]{0,1})([¹²³⁴⁵⁶⁷⁸⁹⁰]*)([ᶜⁱ]{0,1})$", orb_str)
orb,occ,state = if !isnothing(m)
m[1], m[4], m[5]
elseif !isnothing(m2)
m2[1], from_superscript(m2[4]), m2[5]
else
throw(ArgumentError("Unknown subshell specification $(orb_str)"))
end
parse(O, orb) , (occ == "") ? 1 : parse(Int, occ), state_sym(state)
end

function Base.parse(::Type{Configuration{O}}, conf_str; sorted=false) where {O<:AbstractOrbital}
isempty(conf_str) && return Configuration{O}(sorted=sorted)
orbs = split(conf_str, r"[\. ]")
core_m = match(r"\[([a-zA-Z]+)\]([*ci]{0,1})", first(orbs))
core_m = match(r"\[([a-zA-Z]+)\]([*ciᶜⁱ]{0,1})", first(orbs))
if !isnothing(core_m)
core_config = core_configuration(O, core_m[1], core_m[2], sorted)
if length(orbs) > 1
Expand Down
10 changes: 10 additions & 0 deletions test/configurations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
@test_throws ArgumentError parse(Configuration{RelativisticOrbital}, "1s3")
@test_throws ArgumentError parse(Configuration{Orbital}, "1s2 2p-2")

@testset "Unicode occupations/states" begin
for c in [c"1s2 2p6", c"[He]c 2p6", c"[He]i 2p6", c"1s 3d4", c"[Xe]*"]
@test parse(Configuration{Orbital}, string(c)) == c
end

for c in [rc"1s2 2p6", rc"1s 2p- 2p3", rc"[He]c 2p6", rc"[He]i 2p6", rc"1s 3d4", rc"[Xe]*"]
@test parse(Configuration{RelativisticOrbital}, string(c)) == c
end
end

@testset "Spin-configurations" begin
a = sc""
@test a isa SpinConfiguration{<:SpinOrbital{<:Orbital}}
Expand Down

0 comments on commit 5c5a287

Please sign in to comment.