@@ -11,13 +11,41 @@ using Test: @test, @testset
1111module TestUtils
1212 using QuadGK: quadgk
1313 # Exact critical inverse temperature for 2D square lattice Ising model.
14- βc () = 0.5 * log (1 + √ 2 )
14+ βc_2d_ising (elt :: Type{<:Number} = Float64) = elt ( log (1 + √ 2 ) / 2 )
1515 # Exact infinite volume free energy density for 2D square lattice Ising model.
16- function ising_free_energy_density (β:: Real )
17- κ = 2 sinh (2 β) / cosh (2 β)^ 2
18- integrand (θ) = log (0.5 * (1 + sqrt (abs (1 - (κ * sin (θ))^ 2 ))))
16+ function f_2d_ising (β:: Real ; J :: Real = one (β) )
17+ κ = 2 sinh (2 β * J ) / cosh (2 β * J )^ 2
18+ integrand (θ) = log ((1 + √ (abs (1 - (κ * sin (θ))^ 2 ))) / 2 )
1919 integral, _ = quadgk (integrand, 0 , π)
20- return (- log (2 cosh (2 β)) - (1 / (2 π)) * integral) / β
20+ return (- log (2 cosh (2 β * J)) - (1 / (2 π)) * integral) / β
21+ end
22+ function f_1d_ising (β:: Real ; J:: Real = one (β), h:: Real = zero (β))
23+ λ⁺ = exp (β * J) * (cosh (β * h) + √ (sinh (β * h)^ 2 + exp (- 4 β * J)))
24+ return - (log (λ⁺) / β)
25+ end
26+ function f_1d_ising (β:: Real , N:: Integer ; periodic:: Bool = true , kwargs... )
27+ return if periodic
28+ f_1d_ising_periodic (β, N; kwargs... )
29+ else
30+ f_1d_ising_open (β, N; kwargs... )
31+ end
32+ end
33+ function f_1d_ising_periodic (β:: Real , N:: Integer ; J:: Real = one (β), h:: Real = zero (β))
34+ r = √ (sinh (β * h)^ 2 + exp (- 4 β * J))
35+ λ⁺ = exp (β * J) * (cosh (β * h) + r)
36+ λ⁻ = exp (β * J) * (cosh (β * h) - r)
37+ Z = λ⁺^ N + λ⁻^ N
38+ return - (log (Z) / (β * N))
39+ end
40+ function f_1d_ising_open (β:: Real , N:: Integer ; J:: Real = one (β), h:: Real = zero (β))
41+ isone (N) && return 2 cosh (β * h)
42+ T = [
43+ exp (β * (J + h)) exp (- β * J);
44+ exp (- β * J) exp (β * (J - h));
45+ ]
46+ b = [exp (β * h / 2 ), exp (- β * h / 2 )]
47+ Z = (b' * (T^ (N - 1 )) * b)[]
48+ return - (log (Z) / (β * N))
2149 end
2250end
2351
3866 end
3967 end
4068 @testset " Ising Network" begin
41- dims = (4 , 4 )
42- β = TestUtils. βc ()
43- g = named_grid (dims; periodic = true )
44- ldict = Dict (e => Index (2 ) for e in edges (g))
45- l (e) = get (() -> ldict[reverse (e)], ldict, e)
46- tn = ising_network (l, β, g)
47- @test nv (tn) == 16
48- @test ne (tn) == ne (g)
49- @test issetequal (vertices (tn), vertices (g))
50- @test issetequal (arranged_edges (tn), arranged_edges (g))
51- for v in vertices (tn)
52- is = l .(incident_edges (g, v))
53- @test issetequal (is, inds (tn[v]))
54- @test tn[v] ≠ δ (Tuple (is))
69+ @testset " 1D Ising (periodic = $periodic )" for periodic in (false , true )
70+ dims = (4 ,)
71+ β = 0.4
72+ g = named_grid (dims; periodic)
73+ ldict = Dict (e => Index (2 ) for e in edges (g))
74+ l (e) = get (() -> ldict[reverse (e)], ldict, e)
75+ tn = ising_network (l, β, g)
76+ @test nv (tn) == 4
77+ @test ne (tn) == ne (g)
78+ @test issetequal (vertices (tn), vertices (g))
79+ @test issetequal (arranged_edges (tn), arranged_edges (g))
80+ for v in vertices (tn)
81+ is = l .(incident_edges (g, v))
82+ @test issetequal (is, inds (tn[v]))
83+ @test tn[v] ≠ δ (Tuple (is))
84+ end
85+ # TODO : Use eager contraction sequence finding.
86+ z = contract_network (tn; alg = " exact" )[]
87+ f = - log (z) / (β * nv (g))
88+ f_analytic = TestUtils. f_1d_ising (β, 4 ; periodic)
89+ @test f ≈ f_analytic
90+ end
91+ @testset " 2D Ising" begin
92+ dims = (4 , 4 )
93+ β = TestUtils. βc_2d_ising ()
94+ g = named_grid (dims; periodic = true )
95+ ldict = Dict (e => Index (2 ) for e in edges (g))
96+ l (e) = get (() -> ldict[reverse (e)], ldict, e)
97+ tn = ising_network (l, β, g)
98+ @test nv (tn) == 16
99+ @test ne (tn) == ne (g)
100+ @test issetequal (vertices (tn), vertices (g))
101+ @test issetequal (arranged_edges (tn), arranged_edges (g))
102+ for v in vertices (tn)
103+ is = l .(incident_edges (g, v))
104+ @test issetequal (is, inds (tn[v]))
105+ @test tn[v] ≠ δ (Tuple (is))
106+ end
107+ # TODO : Use eager contraction sequence finding.
108+ z = contract_network (tn; alg = " exact" )[]
109+ f = - log (z) / (β * nv (g))
110+ f_inf = TestUtils. f_2d_ising (β)
111+ @test f ≈ f_inf rtol = 1.0e-1
55112 end
56- # TODO : Use eager contraction sequence finding.
57- z = contract_network (tn; alg = " exact" )[]
58- f = - log (z) / (β * nv (g))
59- f_inf = TestUtils. ising_free_energy_density (β)
60- @test f ≈ f_inf rtol = 1.0e-1
61113 end
62114end
0 commit comments