/
configurations.jl
1327 lines (1099 loc) · 39.1 KB
/
configurations.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
struct Configuration{<:AbstractOrbital}
Represents a configuration -- a set of orbitals and their associated
occupation number. Furthermore, each orbital can be in one of the
following _states_: `:open`, `:closed` or `:inactive`.
# Constructors
Configuration(orbitals :: Vector{<:AbstractOrbital},
occupancy :: Vector{Int},
states :: Vector{Symbol}
[; sorted=false])
Configuration(orbitals :: Vector{Tuple{<:AbstractOrbital, Int, Symbol}}
[; sorted=false])
In the first case, the parameters of each orbital have to be passed as
separate vectors, and the orbitals and occupancy have to be of the
same length. The `states` vector can be shorter and then the latter
orbitals that were not explicitly specified by `states` are assumed to
be `:open`.
The second constructor allows you to pass a vector of tuples instead,
where each tuple is a triplet `(orbital :: AbstractOrbital, occupancy
:: Int, state :: Symbol)` corresponding to each orbital.
In all cases, all the orbitals have to be distinct. The orbitals in
the configuration will be sorted (if `sorted`) according to the
ordering defined for the particular [`AbstractOrbital`](@ref).
"""
struct Configuration{O<:AbstractOrbital}
orbitals::Vector{O}
occupancy::Vector{Int}
states::Vector{Symbol}
sorted::Bool
function Configuration(
orbitals::Vector{O},
occupancy::Vector{Int},
states::Vector{Symbol}=[:open for o in orbitals];
sorted=false) where {O<:AbstractOrbital}
length(orbitals) == length(occupancy) ||
throw(ArgumentError("Need to specify occupation numbers for all orbitals"))
length(states) ≤ length(orbitals) ||
throw(ArgumentError("Cannot provide more states than orbitals"))
length(orbitals) == length(unique(orbitals)) ||
throw(ArgumentError("Not all orbitals are unique"))
if length(states) < length(orbitals)
append!(states, repeat([:open], length(orbitals) - length(states)))
end
sd = setdiff(states, [:open, :closed, :inactive])
isempty(sd) || throw(ArgumentError("Unknown orbital states $(sd)"))
for i in eachindex(orbitals)
occ = occupancy[i]
occ < 1 && throw(ArgumentError("Invalid occupancy $(occ)"))
orb = orbitals[i]
degen_orb = degeneracy(orb)
if occ > degen_orb
if O <: Orbital || O <: SpinOrbital
throw(ArgumentError("Higher occupancy than possible for $(orb) with degeneracy $(degen_orb)"))
elseif O <: RelativisticOrbital
orb_conj = flip_j(orb)
degen_orb_conj = degeneracy(orb_conj)
if occ == degen_orb + degen_orb_conj && orb_conj ∉ orbitals
occupancy[i] = degen_orb
push!(orbitals, orb_conj)
push!(occupancy, degen_orb_conj)
push!(states, states[i])
elseif orb_conj ∈ orbitals
throw(ArgumentError("Higher occupancy than possible for $(orb) with degeneracy $(degen_orb)"))
else
throw(ArgumentError("Can only specify higher occupancy for $(orb) if completely filling the $(orb),$(orb_conj) subshell (for total degeneracy of $(degen_orb+degen_orb_conj))"))
end
end
end
end
for i in eachindex(orbitals)
states[i] == :closed && occupancy[i] < degeneracy(orbitals[i]) &&
throw(ArgumentError("Can only close filled orbitals"))
end
p = sorted ? sortperm(orbitals) : eachindex(orbitals)
new{O}(orbitals[p], occupancy[p], states[p], sorted)
end
end
function Configuration(orbs::Vector{Tuple{O,Int,Symbol}}; sorted=false) where {O<:AbstractOrbital}
orbitals = Vector{O}()
occupancy = Vector{Int}()
states = Vector{Symbol}()
for (orb,occ,state) in orbs
push!(orbitals, orb)
push!(occupancy, occ)
push!(states, state)
end
Configuration(orbitals, occupancy, states, sorted=sorted)
end
Configuration(orbital::O, occupancy::Int, state::Symbol=:open; sorted=false) where {O<:AbstractOrbital} =
Configuration([orbital], [occupancy], [state], sorted=sorted)
Configuration{O}(;sorted=false) where {O<:AbstractOrbital} =
Configuration(O[], Int[], Symbol[], sorted=sorted)
Base.copy(cfg::Configuration) =
Configuration(copy(cfg.orbitals), copy(cfg.occupancy), copy(cfg.states),
sorted=cfg.sorted)
const RelativisticConfiguration = Configuration{<:RelativisticOrbital}
"""
issorted(cfg::Configuration)
Tests if the orbitals of `cfg` is sorted.
"""
Base.issorted(cfg::Configuration) = cfg.sorted || issorted(cfg.orbitals)
"""
sort(cfg::Configuration)
Returns a sorted copy of `cfg`.
"""
Base.sort(cfg::Configuration) =
Configuration(cfg.orbitals, cfg.occupancy, cfg.states, sorted=true)
"""
sorted(cfg::Configuration)
Returns `cfg` if it is already sorted or a sorted copy otherwise.
"""
sorted(cfg::Configuration) =
issorted(cfg) ? cfg : sort(cfg)
"""
issimilar(a::Configuration, b::Configuration)
Compares the electronic configurations `a` and `b`, only considering
the constituent orbitals and their occupancy, but disregarding their
ordering and states (`:open`, `:closed`, &c).
# Examples
```jldoctest
julia> a = c"1s 2s"
1s 2s
julia> b = c"2si 1s"
2sⁱ 1s
julia> issimilar(a, b)
true
julia> a==b
false
```
"""
function issimilar(a::Configuration{<:O}, b::Configuration{<:O}) where {O<:AbstractOrbital}
ca = sorted(a)
cb = sorted(b)
ca.orbitals == cb.orbitals && ca.occupancy == cb.occupancy
end
"""
==(a::Configuration, b::Configuration)
Tests if configurations `a` and `b` are the same, considering orbital occupancy,
ordering, and states.
# Examples
```jldoctest
julia> c"1s 2s" == c"1s 2s"
true
julia> c"1s 2s" == c"1s 2si"
false
julia> c"1s 2s" == c"2s 1s"
false
```
"""
Base.:(==)(a::Configuration{<:O}, b::Configuration{<:O}) where {O<:AbstractOrbital} =
a.orbitals == b.orbitals &&
a.occupancy == b.occupancy &&
a.states == b.states
Base.hash(c::Configuration, h::UInt) = hash(c.orbitals, hash(c.occupancy, hash(c.states, h)))
"""
fill(c::Configuration)
Returns a corresponding configuration where the orbitals are completely filled (as
determined by [`degeneracy`](@ref)).
See also: [`fill!`](@ref)
"""
Base.fill(config::Configuration) = fill!(deepcopy(config))
"""
fill!(c::Configuration)
Sets all the occupancies in configuration `c` to maximum, as determined by the
[`degeneracy`](@ref) function.
See also: [`fill`](@ref)
"""
function Base.fill!(config::Configuration)
config.occupancy .= (degeneracy(o) for o in config.orbitals)
return config
end
"""
close(c::Configuration)
Return a corresponding configuration where where all the orbitals are marked `:closed`.
See also: [`close!`](@ref)
"""
Base.close(config::Configuration) = close!(deepcopy(config))
"""
close!(c::Configuration)
Marks all the orbitals in configuration `c` as closed.
See also: [`close`](@ref)
"""
function close!(config::Configuration)
if any(occ != degeneracy(o) for (o, occ, _) in config)
throw(ArgumentError("Can't close $config due to unfilled orbitals."))
end
config.states .= :closed
return config
end
function write_orbitals(io::IO, config::Configuration)
ascii = get(io, :ascii, false)
for (i,(orb,occ,state)) in enumerate(config)
i > 1 && write(io, " ")
show(io, orb)
occ > 1 && write(io, ascii ? "$occ" : to_superscript(occ))
state == :closed && write(io, ascii ? "c" : "ᶜ")
state == :inactive && write(io, ascii ? "i" : "ⁱ")
end
end
function Base.show(io::IO, config::Configuration{O}) where O
ascii = get(io, :ascii, false)
nc = length(config)
if nc == 0
write(io, ascii ? "empty" : "∅")
return
end
noble_core_name = get_noble_core_name(config)
core_config = core(config)
ncc = length(core_config)
if !isnothing(noble_core_name)
write(io, "[$(noble_core_name)]")
write(io, ascii ? "c" : "ᶜ")
ngc = length(get_noble_gas(O, noble_core_name))
core_config = core(config)
if ncc > ngc
write(io, ' ')
write_orbitals(io, core_config[ngc+1:end])
end
nc > ncc && write(io, ' ')
elseif ncc > 0
write_orbitals(io, core_config)
end
write_orbitals(io, peel(config))
end
function Base.ascii(config::Configuration)
io = IOBuffer()
ctx = IOContext(io, :ascii=>true)
show(ctx, config)
String(take!(io))
end
"""
get_noble_core_name(config::Configuration)
Returns the name of the noble gas with the most electrons whose configuration still forms
the first part of the closed part of `config`, or `nothing` if no such element is found.
```jldoctest
julia> AtomicLevels.get_noble_core_name(c"[He] 2s2")
"He"
julia> AtomicLevels.get_noble_core_name(c"1s2c 2s2c 2p6c 3s2c")
"Ne"
julia> AtomicLevels.get_noble_core_name(c"1s2") === nothing
true
```
"""
function get_noble_core_name(config::Configuration{O}) where O
nc = length(config)
nc == 0 && return nothing
core_config = core(config)
ncc = length(core_config)
if length(core_config) > 0
for gas in Iterators.reverse(noble_gases)
gas_cfg = get_noble_gas(O, gas)
ngc = length(gas_cfg)
if ncc ≥ ngc && issimilar(core_config[1:length(gas_cfg)], gas_cfg)
return gas
end
end
end
return nothing
end
function state_sym(state::AbstractString)
if state == "c" || state == "ᶜ"
:closed
elseif state == "i" || state == "ⁱ"
:inactive
else
:open
end
end
function core_configuration(::Type{O}, element::AbstractString, state::AbstractString, sorted) where {O<:AbstractOrbital}
element ∉ keys(noble_gases_configurations[O]) && throw(ArgumentError("Unknown noble gas $(element)"))
state = state_sym(state == "" ? "c" : state) # By default, we'd like cores to be frozen
core_config = noble_gases_configurations[O][element]
Configuration(core_config.orbitals, core_config.occupancy,
[state for o in core_config.orbitals],
sorted=sorted)
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)
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))
if !isnothing(core_m)
core_config = core_configuration(O, core_m[1], core_m[2], sorted)
if length(orbs) > 1
peel_config = Configuration(parse_orbital_occupation.(Ref(O), orbs[2:end]))
Configuration(vcat(core_config.orbitals, peel_config.orbitals),
vcat(core_config.occupancy, peel_config.occupancy),
vcat(core_config.states, peel_config.states),
sorted=sorted)
else
core_config
end
else
Configuration(parse_orbital_occupation.(Ref(O), orbs), sorted=sorted)
end
end
function parse_conf_str(::Type{O}, conf_str, suffix) where O
suffix ∈ ["", "s"] || throw(ArgumentError("Unknown configuration suffix $suffix"))
parse(Configuration{O}, conf_str, sorted=suffix=="s")
end
"""
@c_str -> Configuration{Orbital}
Construct a [`Configuration`](@ref), representing a non-relativistic
configuration, out of a string. With the added string macro suffix
`s`, the configuration is sorted.
# Examples
```jldoctest
julia> c"1s2 2s"
1s² 2s
julia> c"1s² 2s"
1s² 2s
julia> c"1s2.2s"
1s² 2s
julia> c"[Kr] 4d10 5s2 4f2"
[Kr]ᶜ 4d¹⁰ 5s² 4f²
julia> c"[Kr] 4d10 5s2 4f2"s
[Kr]ᶜ 4d¹⁰ 4f² 5s²
```
"""
macro c_str(conf_str, suffix="")
parse_conf_str(Orbital, conf_str, suffix)
end
"""
@rc_str -> Configuration{RelativisticOrbital}
Construct a [`Configuration`](@ref) representing a relativistic
configuration out of a string. With the added string macro suffix `s`,
the configuration is sorted.
# Examples
```jldoctest
julia> rc"[Ne] 3s 3p- 3p"
[Ne]ᶜ 3s 3p- 3p
julia> rc"[Ne] 3s 3p-2 3p4"
[Ne]ᶜ 3s 3p-² 3p⁴
julia> rc"[Ne] 3s 3p-² 3p⁴"
[Ne]ᶜ 3s 3p-² 3p⁴
julia> rc"2p- 1s"s
1s 2p-
```
"""
macro rc_str(conf_str, suffix="")
parse_conf_str(RelativisticOrbital, conf_str, suffix)
end
"""
@scs_str -> Vector{<:SpinConfiguration{<:Orbital}}
Generate all possible spin-configurations out of a string. With the
added string macro suffix `s`, the configuration is sorted.
# Examples
```jldoctest
julia> scs"1s2 2p2"
15-element Vector{SpinConfiguration{SpinOrbital{Orbital{Int64}, Tuple{Int64, HalfIntegers.Half{Int64}}}}}:
1s₀α 1s₀β 2p₋₁α 2p₋₁β
1s₀α 1s₀β 2p₋₁α 2p₀α
1s₀α 1s₀β 2p₋₁α 2p₀β
1s₀α 1s₀β 2p₋₁α 2p₁α
1s₀α 1s₀β 2p₋₁α 2p₁β
1s₀α 1s₀β 2p₋₁β 2p₀α
1s₀α 1s₀β 2p₋₁β 2p₀β
1s₀α 1s₀β 2p₋₁β 2p₁α
1s₀α 1s₀β 2p₋₁β 2p₁β
1s₀α 1s₀β 2p₀α 2p₀β
1s₀α 1s₀β 2p₀α 2p₁α
1s₀α 1s₀β 2p₀α 2p₁β
1s₀α 1s₀β 2p₀β 2p₁α
1s₀α 1s₀β 2p₀β 2p₁β
1s₀α 1s₀β 2p₁α 2p₁β
```
"""
macro scs_str(conf_str, suffix="")
spin_configurations(parse_conf_str(Orbital, conf_str, suffix))
end
"""
@rscs_str -> Vector{<:SpinConfiguration{<:RelativisticOrbital}}
Generate all possible relativistic spin-configurations out of a
string. With the added string macro suffix `s`, the configuration is
sorted.
# Examples
```jldoctest
julia> rscs"1s2 2p2"
6-element Vector{SpinConfiguration{SpinOrbital{RelativisticOrbital{Int64}, Tuple{HalfIntegers.Half{Int64}}}}}:
1s(-1/2) 1s(1/2) 2p(-3/2) 2p(-1/2)
1s(-1/2) 1s(1/2) 2p(-3/2) 2p(1/2)
1s(-1/2) 1s(1/2) 2p(-3/2) 2p(3/2)
1s(-1/2) 1s(1/2) 2p(-1/2) 2p(1/2)
1s(-1/2) 1s(1/2) 2p(-1/2) 2p(3/2)
1s(-1/2) 1s(1/2) 2p(1/2) 2p(3/2)
```
"""
macro rscs_str(conf_str, suffix="")
spin_configurations(parse_conf_str(RelativisticOrbital, conf_str, suffix))
end
Base.getindex(conf::Configuration{O}, i::Integer) where O =
(conf.orbitals[i], conf.occupancy[i], conf.states[i])
Base.getindex(conf::Configuration{O}, i::Union{<:UnitRange{<:Integer},<:AbstractVector{<:Integer}}) where O =
Configuration(Tuple{O,Int,Symbol}[conf[ii] for ii in i], sorted=conf.sorted)
Base.iterate(conf::Configuration{O}, (el, i)=(length(conf)>0 ? conf[1] : nothing,1)) where O =
i > length(conf) ? nothing : (el, (conf[i==length(conf) ? i : i+1],i+1))
Base.length(conf::Configuration) = length(conf.orbitals)
Base.lastindex(conf::Configuration) = length(conf)
Base.eltype(conf::Configuration{O}) where O = (O,Int,Symbol)
function Base.write(io::IO, conf::Configuration{O}) where O
write(io, 'c')
if O <: Orbital
write(io, 'n')
elseif O <: RelativisticOrbital
write(io, 'r')
elseif O <: SpinOrbital
write(io, 's')
end
write(io, length(conf))
for (orb,occ,state) in conf
write(io, orb, occ, first(string(state)))
end
write(io, conf.sorted)
end
function Base.read(io::IO, ::Type{Configuration})
read(io, Char) == 'c' || throw(ArgumentError("Failed to read a Configuration from stream"))
kind = read(io, Char)
O = if kind == 'n'
Orbital
elseif kind == 'r'
RelativisticOrbital
elseif kind == 's'
SpinOrbital
else
throw(ArgumentError("Unknown orbital type $(kind)"))
end
n = read(io, Int)
occupations = Vector{Int}(undef, n)
states = Vector{Symbol}(undef, n)
orbitals = map(1:n) do i
orb = read(io, O)
occupations[i] = read(io, Int)
s = read(io, Char)
states[i] = if s == 'o'
:open
elseif s == 'c'
:closed
elseif s == 'i'
:inactive
else
throw(ArgumentError("Unknown orbital state $(s)"))
end
orb
end
sorted = read(io, Bool)
Configuration(orbitals, occupations, states, sorted=sorted)
end
function Base.isless(a::Configuration{<:O}, b::Configuration{<:O}) where {O<:AbstractOrbital}
a = sorted(a)
b = sorted(b)
l = min(length(a),length(b))
# If they are equal up to orbital l, designate the shorter config
# as the smaller one.
a[1:l] == b[1:l] && return length(a) == l
norm_occ = (orb,w) -> 2w ≥ degeneracy(orb) ? degeneracy(orb) - w : w
for ((orba,occa,statea),(orbb,occb,stateb)) in zip(a[1:l],b[1:l])
if orba < orbb
return true
elseif orba == orbb
# This is slightly arbitrary, but we designate the orbital
# with occupation closest to a filled shell as the smaller
# one.
norm_occ(orba,occa) < norm_occ(orbb,occb) && return true
else
return false
end
end
false
end
"""
num_electrons(c::Configuration) -> Int
Return the number of electrons in the configuration.
```jldoctest
julia> num_electrons(c"1s2")
2
julia> num_electrons(rc"[Kr] 5s2 5p-2 5p2")
42
```
"""
num_electrons(c::Configuration) = sum(c.occupancy)
"""
num_electrons(c::Configuration, o::AbstractOrbital) -> Int
Returns the number of electrons on orbital `o` in configuration `c`. If `o` is not part of
the configuration, returns `0`.
```jldoctest
julia> num_electrons(c"1s 2s2", o"2s")
2
julia> num_electrons(rc"[Rn] Qf-5 Pf3", ro"Qf-")
5
julia> num_electrons(c"[Ne]", o"3s")
0
```
"""
function num_electrons(c::Configuration, o::AbstractOrbital)
idx = findfirst(isequal(o), c.orbitals)
isnothing(idx) && return 0
c.occupancy[idx]
end
"""
in(o::AbstractOrbital, c::Configuration) -> Bool
Checks if orbital `o` is part of configuration `c`.
```jldoctest
julia> in(o"2s", c"1s2 2s2")
true
julia> o"2p" ∈ c"1s2 2s2"
false
```
"""
Base.in(orb::O, conf::Configuration{O}) where {O<:AbstractOrbital} =
orb ∈ conf.orbitals
"""
orbitals(c::Configuration{O}) -> Vector{O}
Access the underlying list of orbitals
```jldoctest
julia> orbitals(c"1s2 2s2")
2-element Vector{Orbital{Int64}}:
1s
2s
julia> orbitals(rc"1s2 2p-2")
2-element Vector{RelativisticOrbital{Int64}}:
1s
2p-
```
"""
orbitals(conf::Configuration) = conf.orbitals
"""
filter(f, c::Configuration) -> Configuration
Filter out the orbitals from configuration `c` for which the predicate `f` returns `false`.
The predicate `f` needs to take three arguments: `orbital`, `occupancy` and `state`.
```julia
julia> filter((o,occ,s) -> o.ℓ == 1, c"[Kr]")
2p⁶ᶜ 3p⁶ᶜ 4p⁶ᶜ
```
"""
Base.filter(f::Function, conf::Configuration) =
conf[filter(j -> f(conf[j]...), eachindex(conf.orbitals))]
"""
core(::Configuration) -> Configuration
Return the core configuration (i.e. the sub-configuration of all the orbitals that are
marked `:closed`).
```jldoctest
julia> core(c"1s2c 2s2c 2p6c 3s2")
[Ne]ᶜ
julia> core(c"1s2 2s2")
∅
julia> core(c"1s2 2s2c 2p6c")
2s²ᶜ 2p⁶ᶜ
```
"""
core(conf::Configuration) = filter((orb,occ,state) -> state == :closed, conf)
"""
peel(::Configuration) -> Configuration
Return the non-core part of the configuration (i.e. orbitals not marked `:closed`).
```jldoctest
julia> peel(c"1s2c 2s2c 2p3")
2p³
julia> peel(c"[Ne] 3s 3p3")
3s 3p³
```
"""
peel(conf::Configuration) = filter((orb,occ,state) -> state != :closed, conf)
"""
inactive(::Configuration) -> Configuration
Return the part of the configuration marked `:inactive`.
```jldoctest
julia> inactive(c"1s2c 2s2i 2p3i 3s2")
2s²ⁱ 2p³ⁱ
```
"""
inactive(conf::Configuration) = filter((orb,occ,state) -> state == :inactive, conf)
"""
active(::Configuration) -> Configuration
Return the part of the configuration marked `:open`.
```jldoctest
julia> active(c"1s2c 2s2i 2p3i 3s2")
3s²
```
"""
active(conf::Configuration) = filter((orb,occ,state) -> state != :inactive, peel(conf))
"""
bound(::Configuration) -> Configuration
Return the bound part of the configuration (see also [`isbound`](@ref)).
```jldoctest
julia> bound(c"1s2 2s2 2p4 Ks2 Kp1")
1s² 2s² 2p⁴
```
"""
bound(conf::Configuration) = filter((orb,occ,state) -> isbound(orb), conf)
"""
continuum(::Configuration) -> Configuration
Return the non-bound (continuum) part of the configuration (see also [`isbound`](@ref)).
```jldoctest
julia> continuum(c"1s2 2s2 2p4 Ks2 Kp1")
Ks² Kp
```
"""
continuum(conf::Configuration) = filter((orb,occ,state) -> !isbound(orb), peel(conf))
Base.empty(conf::Configuration{O}) where {O<:AbstractOrbital} =
Configuration(empty(conf.orbitals), empty(conf.occupancy), empty(conf.states), sorted=conf.sorted)
"""
parity(::Configuration) -> Parity
Return the parity of the configuration.
```jldoctest
julia> parity(c"1s 2p")
odd
julia> parity(c"1s 2p2")
even
```
See also: [`Parity`](@ref)
"""
parity(conf::Configuration) = mapreduce(o -> parity(o[1])^o[2], *, conf)
"""
replace(conf, a => b[; append=false])
Substitute one electron in orbital `a` of `conf` by one electron in
orbital `b`. If `conf` is unsorted the substitution is performed
in-place, unless `append`, in which case the new orbital is appended
instead.
# Examples
```jldoctest
julia> replace(c"1s2 2s", o"1s" => o"2p")
1s 2p 2s
julia> replace(c"1s2 2s", o"1s" => o"2p", append=true)
1s 2s 2p
julia> replace(c"1s2 2s"s, o"1s" => o"2p")
1s 2s 2p
```
"""
function Base.replace(conf::Configuration{O₁}, orbs::Pair{O₂,O₃};
append=false) where {O<:AbstractOrbital,O₁<:O,O₂<:O,O₃<:O}
src,dest = orbs
orbitals = promote_type(O₁,O₂,O₃)[]
append!(orbitals, conf.orbitals)
occupancy = copy(conf.occupancy)
states = copy(conf.states)
i = findfirst(isequal(src), orbitals)
isnothing(i) && throw(ArgumentError("$(src) not present in $(conf)"))
j = findfirst(isequal(dest), orbitals)
if isnothing(j)
j = (append ? length(orbitals) : i) + 1
insert!(orbitals, j, dest)
insert!(occupancy, j, 1)
insert!(states, j, :open)
else
occupancy[j] == degeneracy(dest) &&
throw(ArgumentError("$(dest) already maximally occupied in $(conf)"))
occupancy[j] += 1
end
occupancy[i] -= 1
if occupancy[i] == 0
deleteat!(orbitals, i)
deleteat!(occupancy, i)
deleteat!(states, i)
end
Configuration(orbitals, occupancy, states, sorted=conf.sorted)
end
"""
-(configuration::Configuration, orbital::AbstractOrbital[, n=1])
Remove `n` electrons in the orbital `orbital` from the configuration
`configuration`. If the orbital had previously been `:closed` or
`:inactive`, it will now be `:open`.
"""
function Base.:(-)(configuration::Configuration{O₁}, orbital::O₂, n::Int=1) where {O<:AbstractOrbital,O₁<:O,O₂<:O}
orbitals = promote_type(O₁,O₂)[]
append!(orbitals, configuration.orbitals)
occupancy = copy(configuration.occupancy)
states = copy(configuration.states)
i = findfirst(isequal(orbital), orbitals)
isnothing(i) && throw(ArgumentError("$(orbital) not present in $(configuration)"))
occupancy[i] ≥ n ||
throw(ArgumentError("Trying to remove $(n) electrons from orbital $(orbital) with occupancy $(occupancy[i])"))
occupancy[i] -= n
states[i] = :open
if occupancy[i] == 0
deleteat!(orbitals, i)
deleteat!(occupancy, i)
deleteat!(states, i)
end
Configuration(orbitals, occupancy, states, sorted=configuration.sorted)
end
"""
+(::Configuration, ::Configuration)
Add two configurations together. If both configuration have an orbital, the number of
electrons gets added together, but in this case the status of the orbitals must match.
```jldoctest
julia> c"1s" + c"2s"
1s 2s
julia> c"1s" + c"1s"
1s²
```
"""
function Base.:(+)(a::Configuration{O₁}, b::Configuration{O₂}) where {O<:AbstractOrbital,O₁<:O,O₂<:O}
orbitals = promote_type(O₁,O₂)[]
append!(orbitals, a.orbitals)
occupancy = copy(a.occupancy)
states = copy(a.states)
for (orb,occ,state) in b
i = findfirst(isequal(orb), orbitals)
if isnothing(i)
push!(orbitals, orb)
push!(occupancy, occ)
push!(states, state)
else
occupancy[i] += occ
states[i] == state || throw(ArgumentError("Incompatible states for $(orb): $(states[i]) and $state"))
end
end
Configuration(orbitals, occupancy, states, sorted=a.sorted && b.sorted)
end
"""
delete!(c::Configuration, o::AbstractOrbital)
Remove the entire subshell corresponding to orbital `o` from configuration `c`.
```jldoctest
julia> delete!(c"[Ar] 4s2 3d10 4p2", o"4s")
[Ar]ᶜ 3d¹⁰ 4p²
```
"""
function Base.delete!(c::Configuration{O}, o::O) where O <: AbstractOrbital
idx = findfirst(isequal(o), [co[1] for co in c])
idx === nothing && return c
deleteat!(c.orbitals, idx)
deleteat!(c.occupancy, idx)
deleteat!(c.states, idx)
return c
end
"""
⊗(::Union{Configuration, Vector{Configuration}}, ::Union{Configuration, Vector{Configuration}})
Given two collections of `Configuration`s, it creates an array of `Configuration`s with all
possible juxtapositions of configurations from each collection.
# Examples
```jldoctest
julia> c"1s" ⊗ [c"2s2", c"2s 2p"]
2-element Vector{Configuration{Orbital{Int64}}}:
1s 2s²
1s 2s 2p
julia> [rc"1s", rc"2s"] ⊗ [rc"2p-", rc"2p"]
4-element Vector{Configuration{RelativisticOrbital{Int64}}}:
1s 2p-
1s 2p
2s 2p-
2s 2p
```
"""
⊗(a::Vector{<:Configuration}, b::Vector{<:Configuration}) =
[x+y for x in a for y in b]
⊗(a::Union{<:Configuration,Vector{<:Configuration}}, b::Configuration) =
a ⊗ [b]
⊗(a::Configuration, b::Vector{<:Configuration}) =
[a] ⊗ b
"""
rconfigurations_from_orbital(n, ℓ, occupancy)
Generate all `Configuration`s with relativistic orbitals corresponding to the
non-relativistic orbital with `n` and `ℓ` quantum numbers, with given occupancy.
# Examples
```jldoctest; filter = r"#s[0-9]+"
julia> AtomicLevels.rconfigurations_from_orbital(3, 1, 2)
3-element Vector{Configuration{var"#s00"} where var"#s00"<:RelativisticOrbital}:
3p-²
3p- 3p
3p²
```
"""
function rconfigurations_from_orbital(n::N, ℓ::Int, occupancy::Int) where {N<:MQ}
n isa Integer && ℓ + 1 > n && throw(ArgumentError("ℓ=$ℓ too high for given n=$n"))
occupancy > 2*(2ℓ + 1) && throw(ArgumentError("occupancy=$occupancy too high for given ℓ=$ℓ"))
degeneracy_ℓm, degeneracy_ℓp = 2ℓ, 2ℓ + 2 # degeneracies of nℓ- and nℓ orbitals
nlow_min = max(occupancy - degeneracy_ℓp, 0)
nlow_max = min(degeneracy_ℓm, occupancy)
confs = RelativisticConfiguration[]
for nlow = nlow_max:-1:nlow_min
nhigh = occupancy - nlow
conf = if nlow == 0
Configuration([RelativisticOrbital(n, ℓ, ℓ + 1//2)], [nhigh])
elseif nhigh == 0
Configuration([RelativisticOrbital(n, ℓ, ℓ - 1//2)], [nlow])
else
Configuration(
[RelativisticOrbital(n, ℓ, ℓ - 1//2),
RelativisticOrbital(n, ℓ, ℓ + 1//2)],
[nlow, nhigh]
)
end
push!(confs, conf)
end
return confs
end
"""
rconfigurations_from_orbital(orbital::Orbital, occupancy)
Generate all `Configuration`s with relativistic orbitals corresponding to the
non-relativistic version of the `orbital` with a given occupancy.
# Examples
```jldoctest; filter = r"#s[0-9]+"
julia> AtomicLevels.rconfigurations_from_orbital(o"3p", 2)
3-element Vector{Configuration{var"#s00"} where var"#s00"<:RelativisticOrbital}:
3p-²
3p- 3p
3p²
```
"""
function rconfigurations_from_orbital(orbital::Orbital, occupation::Integer)
rconfigurations_from_orbital(orbital.n, orbital.ℓ, occupation)
end
function rconfigurations_from_nrstring(orb_str::AbstractString)
m = match(r"^([0-9]+|.)([a-z]+)([0-9]+)?$", orb_str)
isnothing(m) && throw(ArgumentError("Invalid orbital string: $(orb_str)"))
n = parse_orbital_n(m)
ℓ = parse_orbital_ℓ(m)
occupancy = isnothing(m[3]) ? 1 : parse(Int, m[3])
return rconfigurations_from_orbital(n, ℓ, occupancy)
end