11using Test
2- using SymmetricTightBinding, Crystalline
2+ using SymmetricTightBinding
3+ using Crystalline
4+ using Crystalline: free
35
4- @testset " Symmetry analysis" begin
6+ @testset " Symmetry analysis (documentation example)" begin
7+ # Example 1
8+ sgnum = 17 # plane group p6mm
9+ brs = calc_bandreps (sgnum, Val (2 )) # band representations
10+ cbr = @composite brs[5 ] # (2b|A₁) EBR
11+ tbm = tb_hamiltonian (cbr) # tight-binding model (nearest neigbors)
12+ ptbm = tbm ([0 , 1 ]) # zero self-energy, nonzero nearest-neighbor hopping
13+ ns = collect_compatible (ptbm)
14+ @test only (ns) == SymmetryVector (cbr) == SymmetryVector (CompositeBandRep (ptbm))
15+
16+ # Example 2
17+ cbr′ = @composite brs[3 ] + brs[5 ] # (2a|A₁) + (3c|B₂)
18+ tbm′ = tb_hamiltonian (cbr′)
19+ ptbm′ = tbm′ ([2.5 , 0 , 0.2 , 0 , - 1 , 0 ])
20+ ns′ = collect_compatible (ptbm′)
21+ @test length (ns′) == 1 # bands are overlapping and not separable
22+ @test only (ns′) == SymmetryVector (cbr′)
23+
24+ ptbm′′ = tbm′ ([2.5 , 0 , 0.2 , 0 , - 1 , .5 ]) # turn on hybridization and split bands
25+ ns′′ = collect_compatible (ptbm′′)
26+ @test length (ns′′) == 2 # bands are overlapping and not separable
27+ @test ns′′[1 ] ∉ brs[[3 , 5 ]]
28+ @test ns′′[2 ] ∉ brs[[3 , 5 ]]
29+ @test sum (ns′′) == SymmetryVector (cbr′)
30+ end
31+
32+ @testset " Symmetry analysis (full scan over EBRs)" begin
33+ # construct a tb-model for every EBR and verify that `collect_compatible` returns a set
34+ # of symmetry vectors whose sum is equal to the underlying EBR's: note that we cannot
35+ # generally require that `collect_compatible` returns a _single_ symmetry vector
36+ # equaling the EBR's symmetry vector, since the EBR might be decomposable (either into
37+ # fragile or genuinely topological EBRs): i.e., we don't know if the model is a single
38+ # connected band or not, only what the "sum" of symmetry vectors will be
539 for D in 1 : 3
6- for sgnum in MAX_SGNUM[D]
7- @testset " Space group $sgnum in dimension $D " begin
40+ αβγ = D == 1 ? [.1 ] : D == 2 ? [.1 , .2 ] : [.1 , .2 , .3 ] # for `pin_free!`
41+ println (" --- D = $D ---" )
42+ for sgnum in 1 : MAX_SGNUM[D]
43+ showed_sgnum = false
44+ # @testset "Space group $sgnum in dimension $D" begin
845 brs = calc_bandreps (sgnum, Val (D))
946 for i in eachindex (brs)
1047 coefs = zeros (length (brs))
1148 coefs[i] = 1
1249 cbr = CompositeBandRep (coefs, brs)
13- ptbm = tb_hamiltonian (cbr)(randn (length (cbr)))
1450
15- v = SymmetryVector (cbr)
16- v_model = collect_compatible (ptbm)
51+ # if the EBR had any free parameters associated with its Wyckoff
52+ # position, we must pin them, using `pin_free!`
53+ iszero (free (position (brs[i]))) || pin_free! (brs, i=> αβγ)
54+
55+ # build a random tight-binding model for the `i`th EBR
56+ tbm = try
57+ tb_hamiltonian (cbr)
58+ catch e
59+ showed_sgnum || (showed_sgnum = true ; println (" #$sgnum " ))
60+ println (" brs[$i ] = $cbr :\t `tb_hamiltonian` error" )
61+ continue
62+ end
63+ ptbm = tbm (randn (length (tbm)))
1764
18- @test v == v_model[1 ] # check that the symmetry vector is compatible with the model
65+ # check that the symmetry vector returned by `collect_compatible` is
66+ # what we started with (i.e. equal to `cbr`)
67+ try
68+ ns = collect_compatible (ptbm)
69+ if length (ns) == 0
70+ showed_sgnum || (showed_sgnum = true ; println (" #$sgnum :" ))
71+ println (" brs[$i ] = $cbr :\t failed to collect any compatible symmetry vectors" )
72+ else
73+ cond = sum (ns) == SymmetryVector (cbr)
74+ if ! cond
75+ showed_sgnum || (showed_sgnum = true ; println (" #$sgnum :" ))
76+ println (" brs[$i ] = $cbr :\t symmetry vector discrepancy" )
77+ println (" n = $(sum (ns)) \n cbr = $(SymmetryVector (cbr)) " )
78+ end
79+ end
80+ catch e
81+ showed_sgnum || (showed_sgnum = true ; println (" #$sgnum :" ))
82+ println (" brs[$i ] = $cbr :\t `collect_compatible` error" )
83+ println (" " , e)
84+ continue
85+ end
86+ # @test cbr == only(collect_compatible(ptbm))
1987 end # for br in brs
20- end # @testset "Space group $sgnum in dimension $D"
88+ # end # @testset "Space group $sgnum in dimension $D"
2189 end # for sgnum in MAX_SGNUM[D]
90+ println ()
2291 end # for D in 1:3
2392end # @testset "Symmetry analysis"
0 commit comments