-
Notifications
You must be signed in to change notification settings - Fork 6
/
shellmodel_main.jl
1803 lines (1681 loc) · 63 KB
/
shellmodel_main.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
const element = ["H", "He", "Li", "Be", "B", "C", "N", "O", "F", "Ne", "Na", "Mg", "Al", "Si", "P", "S", "Cl", "Ar", "K", "Ca",
"Sc", "Ti", "V", "Cr", "Mn", "Fe", "Co", "Ni", "Cu", "Zn", "Ga", "Ge", "As", "Se", "Br", "Kr", "Rb", "Sr", "Y", "Zr",
"Nb", "Mo", "Tc", "Ru", "Rh", "Pd", "Ag", "Cd", "In", "Sn", "Sb", "Te", "I", "Xe", "Cs", "Ba", "La", "Ce", "Pr", "Nd",
"Pm", "Sm", "Eu", "Gd", "Tb", "Dy", "Ho", "Er", "Tm", "Yb", "Lu", "Hf", "Ta", "W", "Re", "Os", "Ir", "Pt", "Au", "Hg",
"Tl", "Pb", "Bi", "Po", "At", "Rn", "Fr", "Ra", "Ac", "Th", "Pa", "U", "Np", "Pu", "Am", "Cm", "Bk", "Cf", "Es", "Fm",
"Md", "No", "Lr", "Rf", "Db", "Sg", "Bh", "Hs", "Mt", "Ds", "Rg", "Cn", "Nh", "Fl", "Mc", "Lv", "Ts", "Og"]
const reg = r"[0-9]+"
struct bit2b
a::Int64
b::Int64
c::Int64
d::Int64
end
struct ifph
i::Int64
f::Int64
phase::Bool
end
struct Jpninfo
fac::Float64
pjump::Array{ifph,1}
njump::Array{ifph,1}
end
"""
Struct to store 2b-jumps (T=1).
This includes the following information:
`f::Int64` the final index `f` for a given index `i`/
`coef::Float64` the coefficient of the 2b-jump.
"""
struct T1info
f::Int64
coef::Float64
end
struct MiMf
Mi::Int64
Mf::Int64
fac::Float64
end
"""
main_sm(sntf,target_nuc,num_ev,target_J;
save_wav=false,q=1,is_block=false,is_show=false,num_history=3,lm=300,ls=20,tol=1.e-8,
in_wf="",mdimmode=false,calc_moment=false, visualize_occ=false, gfactors=[1.0,0.0,5.586,-3.826],effcharge=[1.5,0.5])
Digonalize the model-space Hamiltonian
# Arguments:
- `sntf`: path to input interaction file (.snt fmt)
- `target_nuc`: target nucleus
- `num_ev`: number of eigenstates to be evaluated
- `target_J`: target total J (specified by e.g. [0]). Set to [] if you want lowest states with any J.
Note that J should be doubled (J=0=>[0], J=1/2=>[1], J=1=>[2],...)
# Optional arguments:
- `q=1` block size for Block-Lanczos methods
- `is_block=false` whether or not to use Block algorithm
- `save_wav=false` whether or not to save wavefunction file
- `is_show = true` to show elapsed time & allocations
- `lm = 100` number of Lanczos vectors to store
- `ls = 20` number of vectors to be used for Thick-Restart
- `tol= 1.e-8` tolerance for convergence check in the Lanczos method
- `in_wf=""` path to initial w.f. (for preprocessing)
- `mdimmode=false` `true` => calculate only the M-scheme dimension
- `calc_moment=false` `true` => calculate mu&Q moments
- `calc_entropy=false` `true` => calculate entropy, which is not optimized yet.
- `visualize_occ=false` `true` => visualize all configurations to be considered
- `gfactors=[1.0,0.0,5.586,-3.826]` angular momentum and spin g-factors
- `effcgarge=[1.5,0.5]` effective charges
- `truncation_scheme==""` option to specify a truncation scheme, "jocc" and "pn-pair" is supported and you can use multiple schemes by separating them with camma like "jocc,pn-pair".
- `truncated_jocc=Dict{String,Vector{Int64}}()` option to specify the truncation scheme for each orbit, e.g. Dict("p0p1"=>[1],"n0p3"=>[2,3]) means that the occupation number for proton 0p1 is truncated upto 1, and for neutron 0p3 min=2 and max=3"
"""
function main_sm(sntf,target_nuc,num_ev,target_J;save_wav=false,q=1,is_block=false,is_show=false,num_history=3,lm=300,ls=20,tol=1.e-8,
in_wf="",mdimmode=false, print_evec=false, calc_moment = false, calc_entropy = false, visualize_occ = false,
gfactors = [1.0,0.0,5.586,-3.826], effcharge=[1.5,0.5], truncation_scheme="",truncated_jocc=Dict{String,Vector{Int64}}(),
debugmode=false)
to = TimerOutput()
Anum = parse(Int64, match(reg,target_nuc).match)
lp,ln,cp,cn,massop,Aref,pow,p_sps,n_sps,SPEs,olabels,oTBMEs,labels,TBMEs = readsmsnt(sntf,Anum)
massformula = ifelse(16<=Anum<= 40,2,1)
## massformula=2: J. Blomqvist and A. Molinari, Nucl. Phys. A106, 545 (1968).
## we use this for the sd-shell nuclei
hw, bpar = init_ho_by_mass(Anum,massformula)
@assert length(target_J) <= 1 "Invalid `target_J` Multiple J is not supported now."
Mtot = ifelse(Anum%2==0,0,1)
tJ = -1; eval_jj = -1.0
if length(target_J) > 0; Mtot = minimum(target_J);tJ=target_J[1]
eval_jj = 0.5*tJ*(tJ/2+1)
end
if Anum % 2 != Mtot % 2; println("invalid targetJ $tJ");exit();end
target_el = replace(target_nuc, string(Anum)=>"")
Z,N,vp,vn = getZNA(target_el,Anum,cp,cn)
tf_pairwise_truncation = ifelse(occursin("-pair",truncation_scheme),true,false)
if tf_pairwise_truncation
#if Z!=N; @error "Invalid truncation scheme: pn-pair is not supported for Z != N nuclei"; exit();end
if is_block; @error "Block Lanczos method is not supported for pn-pair truncation"; exit();end
end
msps_p,msps_n,mz_p,mz_n = construct_msps(p_sps,n_sps)
pbits,nbits,jocc_p,jocc_n,Mps,Mns,tdims = occ(p_sps,msps_p,mz_p,vp,n_sps,msps_n,mz_n,vn,Mtot;truncation_scheme=truncation_scheme,truncated_jocc=truncated_jocc)
lblock=length(pbits)
mdim = tdims[end]; if mdim==0; println("Aborted due to the mdim=0");return true;end
num_ev = min(num_ev,mdim)
mdim_print(target_nuc,Z,N,cp,cn,vp,vn,mdim,tJ)
if mdimmode; return nothing;end
if visualize_occ; visualize_configurations(msps_p,msps_n,pbits,nbits,mdim); end
if tf_pairwise_truncation && mdim >= 10^4
@warn "For pairwise truncation, constructing H explicitly, is not recommended for large model space."
end
##Making bit representation of Hamiltonian operators, and storing them
@timeit to "prep. 1b&2b-jumps" begin
## storing two-body jumps for pp/nn 2b interaction
pp_2bjump,nn_2bjump = prep_Hamil_T1(p_sps,n_sps,msps_p,msps_n,pbits,nbits,labels,TBMEs)
bVpn,Vpn,delMs = prep_Hamil_pn(p_sps,n_sps,msps_p,msps_n,labels[3],TBMEs[3])
## storing one-body jumps for pn 2b interaction
bis,bfs,p_NiNfs,n_NiNfs,num_task = prep_pn(lblock,pbits,nbits,Mps,delMs,bVpn,Vpn)
bVpn=nothing
## distribute task
block_tasks = make_distribute(num_task)
end
@timeit to "Jcalc." begin
oPP,oNN,oPNu,oPNd = prep_J(tdims,p_sps,n_sps,msps_p,msps_n,pbits,nbits)
Js = [ 0.5*Mtot*(0.5*Mtot+1) for i = 1:num_ev]
Jtasks = zeros(Int64,lblock)
for i = 1:lblock
Jtasks[i] = length(pbits[i])*length(nbits[i])
end
Jidxs = make_distribute_J(Jtasks)
end
@timeit to "Lanczos" begin
en =[ [1.e+4 for j=1:num_ev] for i = 1:num_history]
Rvecs = [ zeros(Float64,mdim) for i=1:num_ev]
Tmat = zeros(Float64,lm,lm)
vks = Vector{Float64}[]; uks=Vector{Float64}[];itmin = 1; elit=1
doubleLanczos = false
if tJ !=-1; doubleLanczos = true;end
if is_block #Thick-Restart double Block Lanczos: TRdBL
ls_sub = div(ls,q)
vks = [ zeros(Float64,q,mdim) for i=1:div(lm,q)]
uks = [ zeros(Float64,q,mdim) for i=1:ls_sub*q]
Beta_H = zeros(Float64,q,q)
V = vks[1]
if in_wf !=""
try
read_appwav(in_wf,mdim,V,q,true,is_block)
bl_QR!(V',Beta_H,mdim,q)
catch
println("error @preprocessing: failed to read appwav")
initialize_bl_wav(mdim,q,vks[1])
bl_QR!(V',Beta_H,mdim,q)
end
else
initialize_bl_wav(mdim,q,vks[1])
bl_QR!(V',Beta_H,mdim,q)
end
elit = TRBL(q,vks,uks,Tmat,Beta_H,pbits,nbits,jocc_p,jocc_n,SPEs,
pp_2bjump,nn_2bjump,bis,bfs,block_tasks,
p_NiNfs,n_NiNfs,Mps,delMs,Vpn,tdims,
eval_jj,oPP,oNN,oPNu,oPNd,Jidxs,
num_ev,num_history,lm,ls_sub,en,tol,to;doubleLanczos=doubleLanczos,debugmode=debugmode)
else #Thick Restart (double) Lanczos: TRL
vks = [ zeros(Float64,mdim) for i=1:lm]
uks = [ zeros(Float64,mdim) for i=1:ls]
if in_wf==""
initialize_wf(vks[1],"rand",tJ,mdim)
else
read_appwav(in_wf,mdim,vks[1],1,true,is_block)
end
elit = TRL(vks,uks,Tmat,itmin,
pbits,nbits,jocc_p,jocc_n,SPEs,
pp_2bjump,nn_2bjump,bis,bfs,block_tasks,
p_NiNfs,n_NiNfs,Mps,delMs,Vpn,
eval_jj,oPP,oNN,oPNu,oPNd,Jidxs,
tdims,num_ev,num_history,lm,ls,en,tol,to;doubleLanczos=doubleLanczos,debugmode=debugmode)
end
end
@timeit to "Rvecs" begin
vals,vecs = eigen(@views Tmat[1:elit*q,1:elit*q])
@inbounds for (nth,Rvec) in enumerate(Rvecs)
if is_block == false
for k=1:length(vals)
Rvec .+= vecs[k,nth] .* vks[k]
end
else
for k=1:length(vals)
it = div(k-1,q)
b = k - q*it
Rvec .+= @views vecs[k,nth] .* vks[it+1][b,:]
end
end
Rvec .*= 1.0/sqrt(dot(Rvec,Rvec))
end
end
vt = zeros(Float64,mdim)
for (nth,Rv) in enumerate(Rvecs)
vt .= 0.0
operate_J!(Rv,vt,pbits,nbits,tdims,Jidxs,oPP,oNN,oPNu,oPNd)
Js[nth] += dot(Rv,vt)
end
if print_evec
print("\n")
if target_nuc == "Li6"
svd_li6(Rvecs)
end
end
totJs = J_from_JJ1.(Js)
#println("totJs $totJs")
tx_mom =""
if calc_moment
tx_mom = eval_moment(Mtot,Rvecs,totJs,p_sps,n_sps,msps_p,msps_n,tdims,
jocc_p,jocc_n,pbits,nbits,bpar,gfactors,effcharge)
end
if save_wav
@timeit to "I/O" begin
csnt = split(split(sntf,"/")[end],".")[1]
oupf="./"*target_nuc*"_"*csnt*".wav"
if tJ != -1;oupf="./"*target_nuc*"_"*csnt*"_j"*string(tJ)*".wav";end
writeRitzvecs(mdim,Mtot,en[1],totJs,Rvecs,oupf)
end
end
#println("sntf: $sntf")
if length(target_J) == 0
println("J $totJs")
print("En. ");map(x -> @printf("%10.4f ",x), en[1])
print("\nEx. ")
map(x -> @printf("%10.4f ",x),[en[1][i]-en[1][1] for i=1:num_ev])
else
tJ = Int(2*totJs[1])
print("2J= $tJ En.")
map(x -> @printf("%12.5f ",x), en[1])
end
print("\n")
if tx_mom != ""
println(tx_mom)
end
if tf_pairwise_truncation
main_pairwise_truncation(truncation_scheme,Rvecs,vals[1:num_ev],tdims,msps_p,msps_n,pbits,nbits,jocc_p,jocc_n,SPEs,pp_2bjump,nn_2bjump,bis,bfs,block_tasks,p_NiNfs,n_NiNfs,Mps,delMs,Vpn,Jidxs,oPP,oNN,oPNu,oPNd)
end
if calc_entropy
entropy(Rvecs,pbits,nbits,tdims,to)
end
show_TimerOutput_results(to;tf=is_show)
return en[1]
end
"""
readsmsnt(sntf,Anum)
To read interaction file in ".snt" format.
- `sntf`: path to the interaction file
- `Anum`: mass number (used for "scaling" of TBMEs)
!!! note
The current version supports only "properly ordered",like ``a \\leq b,c \\leq d,a \\leq c`` for ``V(abcd;J)``, snt file.
"""
function readsmsnt(sntf,Anum)
f = open(sntf,"r");tlines = readlines(f);close(f)
lines = rm_comment(tlines)
line = lines[1]
lp,ln,cp,cn = map(x->parse(Int,x),rm_nan(split(line," ")))
p_sps = [zeros(Int64,4) for i=1:lp]
n_sps = [zeros(Int64,4) for i=1:ln]
for i = 1:lp
ith,n,l,j,tz = map(x->parse(Int64,x),rm_nan(split(lines[1+i]," "))[1:5])
tp = p_sps[ith]; tp[1] = n; tp[2] = l; tp[3] = j; tp[4] = tz
end
for i = 1:ln
ith,n,l,j,tz = map(x->parse(Int64,x),rm_nan(split(lines[1+i+ln]," "))[1:5])
tp = n_sps[ith-lp]
tp[1] = n; tp[2] = l; tp[3] = j; tp[4] = tz
end
nsp,zero = map(x->parse(Int,x),rm_nan(split(lines[1+ln+lp+1]," "))[1:2])
SPEs = [ [0.0 for i=1:lp],[0.0 for i=1:ln]]
for i = 1:nsp
ttxt = rm_nan(split(lines[1+ln+lp+1+i]," "))
j = parse(Int64,ttxt[1])
idx = ifelse(j<=lp,1,2)
if idx ==2; j -= lp; end
SPEs[idx][j] =parse(Float64,ttxt[3])
end
p = 0.0
tmp = rm_nan(split(lines[1+ln+lp+1+nsp+1]," "))
ntbme,massop,Aref = tmp[1:3]
if length(tmp)>3; p = tmp[4]; p = parse(Float64,p); end
ntbme = parse(Int,ntbme); massop=parse(Int,massop)
Aref = parse(Float64,string(Aref))
labels = [ [ [0,0] ] for i=1:3]
olabels = [ [0,0] ]
for i=1:3; deleteat!(labels[i],1);end
deleteat!(olabels,1)
TBMEs=[ Float64[] for i =1:3] #pp/nn/pn
oTBMEs= Float64[]
@inbounds for ith = 1:ntbme
tmp = rm_nan(split(lines[1+ln+lp+1+nsp+1+ith], " "))
i = tmp[1]; j = tmp[2]; k = tmp[3]; l =tmp[4]; totJ = tmp[5]; TBME= tmp[6]
i = parse(Int,i);j = parse(Int,j);k = parse(Int,k);l = parse(Int,l);
nth = 0
if i<=lp && j<=lp
nth = 1
elseif i>lp && j > lp
nth = 2
elseif i<=lp && j>lp
nth = 3
else
println("i $i j $j lp $lp")
println("err");exit()
end
## snt file must be "ordered"; a<=b & c=d & a<=c
TBME = parse(Float64,TBME)
if massop == 1; TBME*= (Anum/Aref)^(p);end
if unique([i,j]) != unique([k,l])
push!(labels[nth],[k,l,i,j,parse(Int,totJ),ith])
push!(TBMEs[nth],TBME)
end
push!(labels[nth],[i,j,k,l,parse(Int,totJ),ith])
push!(TBMEs[nth],TBME)
push!(olabels,[i,j,k,l,parse(Int,totJ),ith])
push!(oTBMEs,TBME)
end
return lp,ln,cp,cn,massop,Aref,p,p_sps,n_sps,SPEs,olabels,oTBMEs,labels,TBMEs
end
"""
construct_msps(p_sps,n_sps)
Function to define the single particle states specified by `[n,l,j,tz,mz,p(n)idx]`.
The last elements `pidx` and `nidx` to represent original index of `p_sps`/`n_sps`, which is specified by `[n,l,j,tz]``.
"""
function construct_msps(p_sps,n_sps)
msps_p = Vector{Int64}[] ; msps_n = Vector{Int64}[]; mz_p = Int64[]; mz_n = Int64[]
for (pidx,tsps) in enumerate(p_sps)
n,l,j,tz = tsps
for mz = -j:2:j;push!(msps_p,[n,l,j,tz,mz,pidx]);push!(mz_p,mz);end
end
for (nidx,tsps) in enumerate(n_sps)
n,l,j,tz = tsps
for mz = -j:2:j;push!(msps_n,[n,l,j,tz,mz,nidx]);push!(mz_n,mz);end
end
return msps_p, msps_n,mz_p,mz_n
end
"""
all_perm!(ln::Int64,num_valence::Int64,occs::Array{Array{Bool,1}})
make all possible permutation of 'bits'
Example:
If 2 protons and 1 neutron are in the 0p-shell space,
valence orbits(0p1/2,0p3/2) => -1/2, 1/2, -3/2, -1/2, 1/2, 3/2
configurations are represented like:
proton: 000011, 000101, ..., 110000
neutron: 000001, 000010, ..., 100000
"""
function all_perm!(ln::Int64, num_valence::Int64, occs::Array{Array{Bool,1}})
for (i,tcomb) in enumerate(collect(combinations(collect(1:ln),num_valence)))
for nth in tcomb
occs[i][nth] = 1
end
end
return nothing
end
function Mcount!(ln::Int64,mzs::Array{Int64,1}, occ::Array{Bool,1}, Mret::Array{Int64,1})
Mret[1] = 0
@inbounds for i = 1:ln
if occ[i]
Mret[1] += mzs[i]
end
end
return nothing
end
"""
Function to check if the specified configuration `tocc_j` is valid within a given truncation scheme.
"""
function check_jocc_truncation(tz,m_sps,tocc_j,truncated_jocc,truncation_scheme="")
if truncation_scheme == ""
return true
end
if occursin("jocc",truncation_scheme)
if typeof(truncated_jocc) == Dict{String,Vector{Int64}}
if length(keys(truncated_jocc)) == 0
@error "You should specify `truncated_jocc::Dict{String,Vector{Int64}}`, see the document of `main_sm` (main API of ShellModel) for more details."
exit()
end
return jocc_truncation_dict(tz,m_sps,tocc_j,truncated_jocc)
elseif typeof(truncated_jocc) == Vector{Vector{Int64}}
if length(truncated_jocc) == 0
@error "Inappropriate specification of `truncated_jocc::Vector{Vector{Int64}`! You must specify it like [ [n,l,j,tz,max_occ],... ] or [ [n,l,j,tz,min_occ,max_occ],... ]."
exit()
end
return jocc_truncation(tz,m_sps,tocc_j,truncated_jocc)
else
@error "Invalid type of truncated_jocc !!! $(typeof(truncated_jocc)) is not supported!!"
exit()
end
elseif occursin("pn-pair",truncation_scheme) # it doesn't care pn-pair for now
return true
elseif occursin("nn-pair",truncation_scheme)
return true
else
@error "truncation_scheme $truncation_scheme is not defined now!!"
exit()
end
return false
end
function jocc_truncation_dict(target_tz,m_sps,tocc_j,truncated_jocc_dict::Dict{String,Vector{Int64}})
regex = r"(\d+)(\D)(\d+)"
for tkey in keys(truncated_jocc_dict)
if length(tkey) < 4
@error "KeyError: length(tkey=$tkey) < 4, key must include proton/neutron, n, l, j such as p0p1, p1s1, n0d5"
exit()
end
if string(tkey[1]) !="p" && string(tkey[1]) != "n"
@error "KeyError: ($tkey) key must include proton/neutron, n, l, j such as p0p1, p1s1, n0d5, etc."
exit()
end
tz = ifelse(string(tkey[1])=="p",-1,1)
if tz != target_tz; continue;end
n = l = j = -1
tmp = match(regex, tkey)
n = parse(Int64,tmp.captures[1])
j = parse(Int64,tmp.captures[3])
lstr = tmp.captures[2]
l = findall(x->x==lstr, chara_l)[1] - 1
if n == -1 || l == -1 || j == -1
println("something wrong! key $tkey => n $n l $l j $j")
exit()
end
min_occ = 0; max_occ = truncated_jocc_dict[tkey][end]
if length(truncated_jocc_dict[tkey]) == 2
min_occ = truncated_jocc_dict[tkey][1]
end
occupation_number = 0
for (idx, occ_num) in enumerate(tocc_j)
tn, tl, tj, ttz, tjz, idx_orb = m_sps[idx]
if tn != n || tl != l || tj != j || ttz != tz
continue
end
if occ_num #bool
occupation_number += 1
end
end
if occupation_number < min_occ || occupation_number > max_occ
return false
end
end
return true
end
function jocc_truncation(target_tz,m_sps,tocc_j,truncated_jocc)
for tkey in truncated_jocc
ln = length(tkey)
if ln < 5 || ln > 6
@error "KeyError: key must be either [n,l,j,tz,occ_max] or [n,l,j,tz,occ_min,occ_max]"
exit()
end
n = l = j = -1; min_occ = max_occ = 0
if ln == 5
n,l,j,tz,max_occ = tkey
elseif ln == 6
n,l,j,tz,min_occ,max_occ = tkey
end
if tz != target_tz; continue;end
occupation_number = 0
for (idx, occ_bool) in enumerate(tocc_j)
tn, tl, tj, ttz, tjz, idx_orb = m_sps[idx]
if tn != n || tl != l || tj != j || ttz != tz
continue
end
if occ_bool
occupation_number += 1
end
end
if occupation_number < min_occ || occupation_number > max_occ
return false
end
end
return true
end
"""
Function to check occupations in proton and neutron are only in time-reversal pairs.
"""
function check_pnpair_truncation(msps_p,msps_n,ovec_p,ovec_n,truncation_scheme="")
if !occursin("pn-pair",truncation_scheme); return true; end
tf = true
for (idx_p,occ_p) in enumerate(ovec_p)
n_p,l_p,j_p,tz_p,mz_p = msps_p[idx_p][1:5]
for (idx_n,occ_n) in enumerate(ovec_n)
n_n,l_n,j_n,tz_n,mz_n = msps_n[idx_n][1:5]
if n_p != n_n || l_p != l_n || j_p != j_n || tz_p != -tz_n || mz_p != -mz_n
continue
end
tf *= ifelse(occ_p == occ_n,true,false)
end
end
return tf
end
function possible_mz(nljtz,mstates)
n,l,j,tz = nljtz
mzs = Int64[]; midxs=Int64[]
for mz = -j:2:j
push!(mzs,mz)
for k = 1:length(mstates)
if @views mstates[k][1:5] == [n,l,j,tz,mz]
push!(midxs,k)
break
end
end
end
return j,mzs, midxs
end
function initialize_tvec!(tvec::Array{Bool,1})
tvec .= false
return nothing
end
"""
function prep_Hamil_T1(p_sps::Array{Array{Int64,1}},n_sps::Array{Array{Int64,1}},
msps_p::Array{Array{Int64,1},1},msps_n::Array{Array{Int64,1},1},
labels::Array{Array{Array{Int64,1},1},1},TBMEs::Array{Array{Float64,1}})
First, this makes bit representations of T=1 (proton-proton&neutron-neutron) interactions for each {m_z}.
"""
function prep_Hamil_T1(p_sps::Array{Array{Int64,1}},n_sps::Array{Array{Int64,1}},
msps_p::Array{Array{Int64,1},1},msps_n::Array{Array{Int64,1},1},
pbits,nbits,
labels::Array{Array{Array{Int64,1},1},1},TBMEs::Array{Array{Float64,1}})
lp = length(msps_p)
ln = length(msps_n)
bV1 = [ bit2b[] for i=1:2 ]
V1 = [ Float64[] for i=1:2]
mstates = [msps_p,msps_n]
sps = [p_sps,n_sps]
loffs = [ 0, length(p_sps)]
for vrank =1:2 #pp:1, nn:2
loff = loffs[vrank]
vecs= [ [ [ false for i = 1:lp] for j=1:2],
[ [ false for i = 1:ln] for j=1:2]]
blist=bit2b[]
Vs=Float64[]
@inbounds for (i,ME) in enumerate(TBMEs[vrank])
a,b,c,d,totJ,dummy = labels[vrank][i]
J2 = 2*totJ
ja,ma_s,ma_idxs = possible_mz(sps[vrank][a-loff],mstates[vrank])
jb,mb_s,mb_idxs = possible_mz(sps[vrank][b-loff],mstates[vrank])
jc,mc_s,mc_idxs = possible_mz(sps[vrank][c-loff],mstates[vrank])
jd,md_s,md_idxs = possible_mz(sps[vrank][d-loff],mstates[vrank])
for (ic,mc) in enumerate(mc_s)
for (id,md) in enumerate(md_s)
if c == d && mc >= md; continue;end
if abs(mc + md) > J2; continue;end
M_ani = mc + md
initialize_tvec!(vecs[vrank][1]);vecs[vrank][1][mc_idxs[ic]] = true
bit_c = bitarr_to_int(vecs[vrank][1])
initialize_tvec!(vecs[vrank][1]); vecs[vrank][1][md_idxs[id]] = true
bit_d = bitarr_to_int(vecs[vrank][1])
for (ia,ma) in enumerate(ma_s)
for (ib,mb) in enumerate(mb_s)
if a == b && ma>=mb; continue;end
if ma + mb != M_ani;continue;end
initialize_tvec!(vecs[vrank][2]);vecs[vrank][2][ma_idxs[ia]] = true
bit_a = bitarr_to_int(vecs[vrank][2])
initialize_tvec!(vecs[vrank][2]);vecs[vrank][2][mb_idxs[ib]] = true
bit_b = bitarr_to_int(vecs[vrank][2])
CG1 = clebschgordan(Float64,ja//2,ma//2,jb//2,mb//2, J2//2, M_ani//2)
CG2 = clebschgordan(Float64,jc//2,mc//2,jd//2,md//2, J2//2, M_ani//2)
tl = bit2b(bit_a,bit_b,bit_c,bit_d)
if ( tl in blist) == false
push!(blist,tl)
push!(Vs, ME * sqrt( (1.0+deltaf(a,b)) *(1.0+deltaf(c,d)) ) * CG1 * CG2)
continue
end
for kk = 1:length(blist)
if blist[kk] == tl
Vs[kk] += ME * sqrt( (1.0+deltaf(a,b)) *(1.0+deltaf(c,d)) ) * CG1 * CG2
break
end
end
end
end
end
end
end
bV1[vrank] = blist
V1[vrank] = Vs
end
pp_2bjump = prep_2bjumps(msps_p,pbits,bV1[1],V1[1])
nn_2bjump = prep_2bjumps(msps_n,nbits,bV1[2],V1[2])
return pp_2bjump,nn_2bjump
end
"""
prep_Hamil_pn(p_sps::Array{Array{Int64,1}},n_sps::Array{Array{Int64,1}},
msps_p::Array{Array{Int64,1},1},msps_n::Array{Array{Int64,1},1},
labels::Array{Array{Int64,1}},TBMEs::Array{Float64,1},zeroME=false)
make bit representation of T=0 (proton-neutron) interactions for each {m_z}
"""
function prep_Hamil_pn(p_sps::Array{Array{Int64,1}},
n_sps::Array{Array{Int64,1}},
msps_p::Array{Array{Int64,1},1},
msps_n::Array{Array{Int64,1},1},
labels::Array{Array{Int64,1}},
TBMEs::Array{Float64,1},
zeroME=false)
lp = length(msps_p); ln = length(msps_n)
loff = length(p_sps)
Mzs = Int64[]
for j = 1:lp
for i = 1:lp
push!(Mzs,msps_p[i][5]-msps_p[j][5])
end
end
unique!(Mzs);sort!(Mzs,rev=true)
lenMz = length(Mzs)
bVpn= [ [[1,1]] for i=1:lenMz ]
for i = 1:lenMz;deleteat!(bVpn[i],1);end
Vpn=[ Float64[ ] for i=1:lenMz]
ret = [-1,-1,-1]
vec_ani_p = [false for i = 1:lp];vec_cre_p = [false for i = 1:lp]
vec_ani_n = [false for i = 1:ln];vec_cre_n = [false for i = 1:ln]
@inbounds for (i,ME) in enumerate(TBMEs)
a,b,c,d,totJ,dummy = labels[i]
J2 = 2*totJ
ja,ma_s,ma_idxs = possible_mz(p_sps[a],msps_p)
jc,mc_s,mc_idxs = possible_mz(p_sps[c],msps_p)
jb,mb_s,mb_idxs = possible_mz(n_sps[b-loff],msps_n)
jd,md_s,md_idxs = possible_mz(n_sps[d-loff],msps_n)
ja = ja//2; jb=jb//2;jc = jc//2; jd=jd//2
for (ic,mc) in enumerate(mc_s)
initialize_tvec!(vec_ani_p); vec_ani_p[mc_idxs[ic]] = true
bit_c = bitarr_to_int(vec_ani_p)
for (ia,ma) in enumerate(ma_s)
initialize_tvec!(vec_cre_p); vec_cre_p[ma_idxs[ia]] = true
bit_a = bitarr_to_int(vec_cre_p)
Mp = ma - mc
bisearch!(Mzs,Mp,ret);idx = ret[1]
tV = Vpn[idx]; bV = bVpn[idx]
for (id,md) in enumerate(md_s)
if abs(mc + md) > J2; continue;end
initialize_tvec!(vec_ani_n); vec_ani_n[md_idxs[id]] = true
bit_d = bitarr_to_int(vec_ani_n)
CG1 = clebschgordan(Float64,jc,mc//2,jd,md//2,J2//2,(mc+md)//2)
tfac = ME .* CG1
for (ib,mb) in enumerate(mb_s)
if mb - md + Mp != 0; continue;end
initialize_tvec!(vec_cre_n); vec_cre_n[mb_idxs[ib]] = true
bit_b = bitarr_to_int(vec_cre_n)
fac = tfac .* clebschgordan(Float64,ja,ma//2,jb,mb//2,J2//2,(ma+mb)//2)
tl = [bit_a,bit_b,bit_c,bit_d]
if (tl in bV) == false
push!(bV,copy(tl))
push!(tV,fac)
continue
end
for kk = 1:length(bV)
if bV[kk] == tl
tV[kk] += fac
break
end
end
end
end
end
end
end
return bVpn,Vpn,Mzs
end
function bitarr_to_int(arr::Array{Bool,1}, val = 0)
v = 2^(length(arr)-1)
for i in eachindex(arr)
val += v*arr[i]
v >>= 1
end
return val
end
function bitarr_to_int!(arr::Array{Bool,1},ret)
ret[1] = 0
ret[2] = 2^(length(arr)-1)
for i in eachindex(arr)
ret[1] += ret[2]*arr[i]
ret[2] >>= 1
end
return nothing
end
function deltaf(i::Int64,j::Int64)
ifelse(i==j,1.0,0.0)
end
"""
occ(p_sps::Array{Array{Int64,1}},msps_p::Array{Array{Int64,1}},mzp::Array{Int64,1},num_vp::Int64,
n_sps::Array{Array{Int64,1}},msps_n::Array{Array{Int64,1}},mzn::Array{Int64,1},num_vn::Int64,Mtot::Int64;pnpair_truncated=false)
prepare bit representations of proton/neutron Slater determinants => pbits/nbits
jocc_p, jocc_n: corresponding occupation numbers for a "j" state,
which is used for one-body operation and OBTDs.
Mps/Mns: total M for proton/neutron "blocks"
For 6Li in the p shell and M=0, Mps = [-3,-1,1,3] & Mns = [3,1,-1,-3]
blocks => [ (Mp,Mn)=(-3,3),(Mp,Mn)=(-1,1),...]
tdims: array of cumulative number of M-scheme dimensions for "blocks"
tdims =[ # of possible configurations of (-3,3),
# of possible configurations of (-1,1),...]
"""
function occ(p_sps::Vector{Vector{Int64}}, msps_p::Vector{Vector{Int64}}, mzp::Array{Int64,1}, num_vp::Int64,
n_sps::Vector{Vector{Int64}}, msps_n::Vector{Vector{Int64}}, mzn::Array{Int64,1}, num_vn::Int64, Mtot::Int64;
truncation_scheme="",truncated_jocc=Dict{String,Vector{Int64}}())
lp = length(mzp); ln = length(mzn)
all_pDim = binomial(lp,num_vp)
all_occs_p =[ [false for j=1:lp] for i = 1:all_pDim]
all_perm!(lp,num_vp,all_occs_p)
all_nDim = binomial(ln,num_vn)
all_occs_n =[ [false for j=1:ln] for i = 1:all_nDim]
all_perm!(ln,num_vn,all_occs_n)
occs_p = Vector{Bool}[ ]
occs_n = Vector{Bool}[ ]
mdim = 0;tdims=Int64[];push!(tdims,0)
Mret = Int64[0]; Mps = Int64[]; Mns = Int64[]
for i=1:all_pDim
tocc = all_occs_p[i]
Mcount!(lp,mzp,tocc,Mret)
if !check_jocc_truncation(-1,msps_p,tocc,truncated_jocc,truncation_scheme); continue; end
push!(occs_p,copy(tocc))
push!(Mps, Mret[1])
end
for i=1:all_nDim
tocc = all_occs_n[i]
Mcount!(ln,mzn,tocc,Mret)
if !check_jocc_truncation(1,msps_n,tocc,truncated_jocc,truncation_scheme); continue;end
push!(occs_n,copy(tocc))
push!(Mns, Mret[1])
end
pDim = length(occs_p); nDim = length(occs_n)
Mps = unique(Mps); Mns = unique(Mns)
sort!(Mps,rev=false); sort!(Mns,rev=true)
possidxs = Vector{Int64}[]
for i = 1:length(Mps)
for j = 1:length(Mns)
if Mps[i] + Mns[j] == Mtot
push!(possidxs,[i,j])
end
end
end
pbits = [ Int64[] for i=1:length(possidxs) ]
nbits = [ Int64[] for i=1:length(possidxs) ]
occ_p_j = [ [[0.0,0.0]] for i=1:length(possidxs) ]
occ_n_j = [ [[0.0,0.0]] for i=1:length(possidxs) ]
for i =1:length(possidxs)
deleteat!(occ_p_j[i],1);deleteat!(occ_n_j[i],1)
end
tocc_p_j = zeros(Float64,length(p_sps))
tocc_n_j = zeros(Float64,length(n_sps))
for ith = 1:length(possidxs)
ni,nj = possidxs[ith]
Mp = Mps[ni]; Mn = Mns[nj]
if Mp + Mn != Mtot;@error "something wrong in occ func" ;end
for i = 1:pDim
Mcount!(lp,mzp,occs_p[i],Mret)
if Mp != Mret[1]; continue;end
push!(pbits[ith], bitarr_to_int(occs_p[i]))
count_jocc!(p_sps,msps_p,occs_p[i],tocc_p_j)
push!(occ_p_j[ith],copy(tocc_p_j))
for j=1:nDim
Mcount!(ln,mzn,occs_n[j],Mret)
if Mp + Mret[1] != Mtot; continue; end
mdim += 1
end
end
for j=1:nDim
Mcount!(ln,mzn,occs_n[j],Mret)
if Mn != Mret[1]; continue; end
nbit = bitarr_to_int(occs_n[j])
push!(nbits[ith], nbit)
count_jocc!(n_sps,msps_n,occs_n[j],tocc_n_j)
push!(occ_n_j[ith],copy(tocc_n_j))
end
push!(tdims,mdim)
end
return pbits,nbits,occ_p_j,occ_n_j,Mps,Mns,tdims
end
function TF_connectable(Phi::Int64,bVs::bit2b,TF::Array{Bool,1})
# bVs = [ bit(Ca^+), bit(Cb^+), bit(Cc), bit(Cd) ]
if bVs.c != (bVs.c & Phi);TF[1]=false;return nothing;end
if bVs.d != (bVs.d & Phi);TF[1]=false;return nothing;end
APhi = (~bVs.d) & ( (~bVs.c) & Phi)
if bVs.a != (bVs.a & (~APhi));TF[1]=false;return nothing;end
if bVs.b != (bVs.b & (~APhi));TF[1]=false;return nothing;end
TF[1] = true
return nothing
end
function TF_connectable_1(Phi::Int64,a::Int64,c::Int64,TF::Array{Bool,1})
# a: creation c: anihilation
if c != c & Phi;TF[1]=false;return nothing;end
if a != (a & (~((~c) & Phi)));TF[1]=false;return nothing;end
TF[1] = true
return nothing
end
function calc_phase!(Phi::Int64,bVs::bit2b,ln::Int64,ret::Array{Int64,1})
ret[1] = 0; ret[2] = -1 # ret=[phase,nPhi]
## anihilation c
ret[1] += count_ones(Phi& (~(bVs.c-1)))
ret[2] = (~bVs.c) & Phi #|phi>:= Cc|v>
## anihilation d
ret[1] += count_ones(ret[2]& (~(bVs.d-1)))
ret[2] = (~bVs.d) & ret[2] #|phi>:=CdCc|v>
## creation b
ret[1] += count_ones(ret[2]& (~(bVs.b-1)))
ret[2] = bVs.b | ret[2] #|phi>:=C^+bCdCc|v>
## creation a
ret[1] += count_ones(ret[2]& (~(bVs.a-1)))
ret[2] = bVs.a | ret[2] #|phi>:=C^+aC^+bCdCc|v>
ret[1] = (-1)^ret[1]
return nothing
end
function calc_phase_1!(Phi::Int64,cre::Int64,ani::Int64,ret::Array{Int64,1})
ret[1] = 1; ret[2] = -1
## anihilation #|phi>:=Cc|v>
ret[1] += count_ones(Phi & (~(ani-1)))
ret[2] = (~ani) & Phi
## creation #|phi>:=C^+aCc|v>
ret[1] += count_ones(ret[2] & (~(cre-1)))
ret[2] = cre | ret[2]
ret[1] = (-1)^ret[1]
return nothing
end
function count_jocc!(sps,mstates,occ_bit,tocc)
tocc .= 0.0
for i = 1:length(occ_bit)
if occ_bit[i]
tocc[mstates[i][6]] += 1.0
end
end
return nothing
end
function getZNA(target_el,Anum,cp,cn)
Z = 0
for i = 1:length(element)
if element[i] == target_el;Z = i; break; end
end
N = Anum - Z
vp = Z - cp; vn = N - cn
return Z,N,vp,vn
end
function ThickRestart_J(vks,uks,Tmat,lm,ls,eval_jj,Jtol)
vals,vecs = eigen(@views Tmat[1:lm-1,1:lm-1])
k = argmin(abs.(vals.-eval_jj))
beta = Tmat[lm-1,lm]
Tmat .= 0.0
Tmat[1,1] = vals[k]
uk=uks[1]
tmp = beta * vecs[lm-1,k]
Tmat[ls+1,k] = tmp; Tmat[k,ls+1] = tmp
uk .= 0.0
@inbounds for j=1:lm-1
axpy!(vecs[j,k],vks[j],uk)
end
vks[1] .= uk .* (1.0/sqrt(dot(uk,uk)))
vks[2] .= vks[lm]
for k = ls+2:length(vks)
vks[k] .= 0.0
end
return nothing
end
function initialize_wf(v1,method,tJ,mdim)
Random.seed!(1234)
if method=="rand" || tJ == -1
v1 .= randn(mdim,)
v1 ./= sqrt(dot(v1,v1))
elseif method == "one"
v1 .= 1.0 / sqrt(mdim)
end
return nothing
end
function initialize_bl_wav(mdim,q,vks)
Random.seed!(1234)
for i=1:q
v = @views vks[i,:]
v .= randn(mdim,)
if i>1
for k=i-1:-1:1
tv = @views vks[k,:]
v .-= dot(v,tv) .* tv
end
end
tmp = 1.0/sqrt(dot(v,v))
v.*= tmp
end
return nothing
end
function read_wav(inpf,mdim,n;all=false,verbose=false)
fid =open(inpf)
neig = read(fid,Int32)
mtot = read(fid,Int32)
Es = [read(fid,Float64) for i = 1:neig]
jj = [read(fid,Int32) for i=1:neig]
V = [ [read(fid,Float64) for i=1:mdim] for nth=1:neig]
close(fid)
if all
return V,jj
else
return V[n],jj[n]
end
end
function read_appwav(inpf,mdim,V,q,verbose=false,is_block=false)
fid =open(inpf)
neig = read(fid,Int32)
if neig < q; println("#(appwav) must be >= q");exit();end
mtot = read(fid,Int32)
Es = [read(fid,Float64) for i = 1:neig]
jj = [read(fid,Int32) for i=1:neig]
if verbose
println("appwav: $inpf")
print_vec("Es(n=$neig)",Es)
end
if is_block
for j=1:q
V[j,:] .= [read(fid,Float64) for i=1:mdim]
end
else
V .= [read(fid,Float64) for i=1:mdim]
end
close(fid)
return nothing
end
function ReORTH(it,vtarget,vks)
for l = it:-1:1
v = vks[l]
alpha = dot(v,vtarget)
axpy!(-alpha,v,vtarget)
end
return nothing
end
function Check_Orthogonality(it::Int,vks,en)
svks = @views vks[1:it+1]
for i = 1:it+1
for j = i:it+1
tdot = dot(svks[i],svks[j])