Skip to content

Commit

Permalink
V1.3.1 (#21)
Browse files Browse the repository at this point in the history
* Ensures symmetries are enforced at all times. The previous implementation would turn gf[:,:,t1,t2] = a into gf[i,j,t1,t2] = a[i,j] for all (i,j) which did not enforce the SkewHermitian symmetry as the adjoint operation would be taken element-wise

* adjust notebooks to new kbsolve

---------

Co-authored-by: Francisco Meirinhos <[email protected]>
  • Loading branch information
timbode and fmeirinhos authored Mar 17, 2024
1 parent 54eee03 commit 1481ad6
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 68 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "KadanoffBaym"
uuid = "82532805-809c-4ef0-842b-4b00c5e9be5f"
authors = ["Francisco Meirinhos, Tim Bode"]
version = "1.3"
version = "1.3.1"

[deps]
AbstractFFTs = "621f4979-c628-5d54-868e-fcf4e3e8185c"
Expand Down
45 changes: 22 additions & 23 deletions examples/fermi-hubbard-2B.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"using JLD\n",
"\n",
"using PyPlot\n",
"PyPlot.plt.style.use(\"./paper.mplstyle\")\n",
"# PyPlot.plt.style.use(\"./paper.mplstyle\")\n",
"using LaTeXStrings"
]
},
Expand Down Expand Up @@ -360,7 +360,6 @@
"outputs": [],
"source": [
"let\n",
" Gs = loaded_sol[\"solution\"]\n",
" num_points = loaded_sol[\"solution\"].t |> length\n",
" \n",
" xpad = 8\n",
Expand All @@ -370,10 +369,10 @@
" idx_1 = 1\n",
" idx_2 = 8\n",
"\n",
" ax1.plot(loaded_sol[\"solution\"].t, [imag(loaded_sol[\"solution\"].u[1][idx_1, idx_1, k, k] .+ loaded_sol[\"solution\"].u[3][idx_1, idx_1, k, k]) for k = 1:num_points], \n",
" ax1.plot(loaded_sol[\"solution\"].t, [imag(data.GL_u.data[idx_1, idx_1, k, k] .+ data.GL_d.data[idx_1, idx_1, k, k]) for k = 1:num_points], \n",
" label = \"\\$ i=1\\$\", lw=1.5, ls = \"--\", c = \"#438E6A\")\n",
"\n",
" ax1.plot(loaded_sol[\"solution\"].t, [imag(loaded_sol[\"solution\"].u[1][idx_2, idx_2, k, k] .+ loaded_sol[\"solution\"].u[3][idx_2, idx_2, k, k]) for k = 1:num_points], \n",
" ax1.plot(loaded_sol[\"solution\"].t, [imag(data.GL_u.data[idx_2, idx_2, k, k] .+ data.GL_d.data[idx_2, idx_2, k, k]) for k = 1:num_points], \n",
" label = \"\\$ i=8\\$\", lw=1.5, ls = \"-\", c = \"#2D5FAA\")\n",
"\n",
" ax1.set_xlim(0, tmax)\n",
Expand All @@ -387,10 +386,10 @@
"\n",
" # ax2 = subplot(122)\n",
"\n",
" ax2.plot(loaded_sol[\"solution\"].t, [imag(loaded_sol[\"solution\"].u[1][idx_1, idx_1, k, k] .- loaded_sol[\"solution\"].u[3][idx_1, idx_1, k, k]) for k = 1:num_points], \n",
" ax2.plot(loaded_sol[\"solution\"].t, [imag(data.GL_u.data[idx_1, idx_1, k, k] .- data.GL_d.data[idx_1, idx_1, k, k]) for k = 1:num_points], \n",
" label = \"\\$ i=2\\$\", lw=1.5, ls = \"--\", c = \"#438E6A\")\n",
"\n",
" ax2.plot(loaded_sol[\"solution\"].t, [imag(loaded_sol[\"solution\"].u[1][idx_2, idx_2, k, k] .- loaded_sol[\"solution\"].u[3][idx_2, idx_2, k, k]) for k = 1:num_points], \n",
" ax2.plot(loaded_sol[\"solution\"].t, [imag(data.GL_u.data[idx_2, idx_2, k, k] .- data.GL_d.data[idx_2, idx_2, k, k]) for k = 1:num_points], \n",
" label = \"\\$ i=2\\$\", lw=1.5, ls = \"-\", c = \"#2D5FAA\")\n",
"\n",
" ax2.set_xlim(0, tmax)\n",
Expand All @@ -407,7 +406,7 @@
" bbox_to_anchor=(0.96, .0, .0, .0),\n",
" bbox_transform=ax1.transAxes)\n",
"\n",
" axins1.plot(loaded_sol[\"solution\"].t, [(tr(loaded_sol[\"solution\"].u[1][k, k]) |> imag) .+ (tr(loaded_sol[\"solution\"].u[3][k, k]) |> imag) for k = 1:num_points] \n",
" axins1.plot(loaded_sol[\"solution\"].t, [(tr(data.GL_u.data[:, :, k, k]) |> imag) .+ (tr(data.GL_d.data[:, :, k, k]) |> imag) for k = 1:num_points] \n",
" .- sum(N_u .+ N_d) .|> abs, \n",
" label = \"\\$ c \\$\", ls = \"-\", c = \"k\")\n",
" axins1.set_xlim(0, tmax)\n",
Expand All @@ -424,14 +423,14 @@
" bbox_to_anchor=(0.96, .0, .0, .0),\n",
" bbox_transform=ax2.transAxes)\n",
"\n",
" axins2.plot(loaded_sol[\"solution\"].t, [(tr(loaded_sol[\"solution\"].u[1][k, k]) |> imag) .- (tr(loaded_sol[\"solution\"].u[3][k, k]) |> imag) for k = 1:num_points] \n",
" axins2.plot(loaded_sol[\"solution\"].t, [(tr(data.GL_u.data[:, :, k, k]) |> imag) .- (tr(data.GL_d.data[:, :, k, k]) |> imag) for k = 1:num_points] \n",
" .- sum(N_u .- N_d) .|> abs, \n",
" label = \"\\$ c \\$\", ls = \"-\", c = \"k\")\n",
" axins2.set_xlim(0, tmax)\n",
" axins2.set_xticks([0, 8, 16, 24, 32])\n",
" axins2.set_yticks([k for k in 0:0.5:1] .* 3e-15)\n",
"# axins2.set_yticks([k for k in 0:0.5:1] .* 3e-15)\n",
" axins2.set_xticklabels([])\n",
" axins2.set_ylim([0.0, 1] .* 3e-15)\n",
" axins2.set_ylim([0.0, 1] .* 12e-15)\n",
" axins2.set_ylabel(L\"S(t) - S_0\", fontdict = Dict(:fontsize=>10))\n",
" axins2.tick_params(axis=\"y\", labelsize=10)\n",
" axins2.yaxis.get_offset_text().set_fontsize(10)\n",
Expand Down Expand Up @@ -473,12 +472,12 @@
"source": [
"let\n",
" # quantum number to look at\n",
" idx = 5\n",
" idx = 1\n",
" shift = 0\n",
"\n",
" ρτ, (τs, ts) = wigner_transform_itp((loaded_sol[\"solution\"].u[2][idx, idx, :, :] - loaded_sol[\"solution\"].u[1][idx, idx, :, :]), \n",
" ρτ, (τs, ts) = wigner_transform_itp((data.GG_u.data[idx, idx, :, :] - data.GL_u.data[idx, idx, :, :]), \n",
" loaded_sol[\"solution\"].t[1+shift:end-shift], fourier=false);\n",
" ρω, (ωs, ts) = wigner_transform_itp((loaded_sol[\"solution\"].u[2][idx, idx, :, :] - loaded_sol[\"solution\"].u[1][idx, idx, :, :]), \n",
" ρω, (ωs, ts) = wigner_transform_itp((data.GG_u.data[idx, idx, :, :] - data.GL_u.data[idx, idx, :, :]), \n",
" loaded_sol[\"solution\"].t[1+shift:end-shift], fourier=true);\n",
" cmap = \"gist_heat\";\n",
" \n",
Expand All @@ -504,7 +503,7 @@
" ax.set_ylabel(\"\\$ A_{11, \\\\uparrow}(T_{\\\\mathrm{max}}/2, \\\\tau)_W \\$\")\n",
"\n",
" ax = subplot(122)\n",
" heatmap = ax.pcolormesh(X, Y, imag(loaded_sol[\"solution\"].u[1][1, 1, :, :]) .- imag(loaded_sol[\"solution\"].u[2][1, 1, :, :]), cmap=cmap, rasterized=true, vmin=vmin, vmax=vmax)\n",
" heatmap = ax.pcolormesh(X, Y, imag(data.GL_u.data[1, 1, :, :]) .- imag(data.GG_u.data[1, 1, :, :]), cmap=cmap, rasterized=true, vmin=vmin, vmax=vmax)\n",
" heatmap.set_edgecolor(\"face\")\n",
" ax.set_aspect(\"equal\")\n",
" cbar = colorbar(mappable=heatmap)\n",
Expand Down Expand Up @@ -542,7 +541,9 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"tmax = 16;\n",
Expand Down Expand Up @@ -606,9 +607,9 @@
" # quantum number to look at\n",
" idx = 1\n",
"\n",
" ρτ, (τs, ts) = wigner_transform_itp((loaded_sol[\"solution\"].u[2].data[idx, idx, :, :] - loaded_sol[\"solution\"].u[1].data[idx, idx, :, :]), \n",
" ρτ, (τs, ts) = wigner_transform_itp((data.GG_u.data[idx, idx, :, :] - data.GL_u.data[idx, idx, :, :]), \n",
" loaded_sol[\"solution\"].t[1:end], fourier=false);\n",
" ρω, (ωs, ts) = wigner_transform_itp((loaded_sol[\"solution\"].u[2].data[idx, idx, :, :] - loaded_sol[\"solution\"].u[1].data[idx, idx, :, :]), \n",
" ρω, (ωs, ts) = wigner_transform_itp((data.GG_u.data[idx, idx, :, :] - data.GL_u.data[idx, idx, :, :]), \n",
" loaded_sol[\"solution\"].t[1:end], fourier=true);\n",
"\n",
" t_scale = 1\n",
Expand Down Expand Up @@ -638,7 +639,7 @@
" vmax = 1.0\n",
"\n",
" ax = subplot(122)\n",
" heatmap = ax.pcolormesh(X, Y, imag(loaded_sol[\"solution\"].u[1].data[1, 1, :, :]) .- imag(loaded_sol[\"solution\"].u[2].data[1, 1, :, :]), cmap=cmap, rasterized=true, vmin=vmin, vmax=vmax)\n",
" heatmap = ax.pcolormesh(X, Y, imag(data.GL_u.data[1, 1, :, :]) .- imag(data.GG_u.data[1, 1, :, :]), cmap=cmap, rasterized=true, vmin=vmin, vmax=vmax)\n",
" heatmap.set_edgecolor(\"face\")\n",
" ax.set_aspect(\"equal\")\n",
" cbar = colorbar(mappable=heatmap)\n",
Expand All @@ -649,8 +650,6 @@
" ax.set_ylim(0, t_scale * tmax)\n",
" ax.set_xlim(0, t_scale * 8)\n",
" ax.set_ylim(0, t_scale * 8)\n",
" # ax.set_xticks(t_scale .* [0, tmax/2, tmax])\n",
" # ax.set_yticks(t_scale .* [0, tmax/2, tmax])\n",
"\n",
" ax.set_xticks(t_scale .* [0, 2, 4, 6, 8])\n",
" ax.set_yticks(t_scale .* [0, 2, 4, 6, 8])\n",
Expand All @@ -671,15 +670,15 @@
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.2",
"display_name": "Julia 1.9.4",
"language": "julia",
"name": "julia-1.7"
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.2"
"version": "1.9.4"
}
},
"nbformat": 4,
Expand Down
74 changes: 60 additions & 14 deletions examples/fermi-hubbard-TM.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"outputs": [],
"source": [
"using PyPlot\n",
"PyPlot.plt.style.use(\"./paper.mplstyle\")\n",
"# PyPlot.plt.style.use(\"./paper.mplstyle\")\n",
"using LaTeXStrings"
]
},
Expand Down Expand Up @@ -436,7 +436,48 @@
" stop = x -> (println(\"t: $(x[end])\"); flush(stdout); false)\n",
" );\n",
"\n",
" save(\"FH_3D_T_matrix_sol_U_$(U₀)_tmax_$(tmax)_atol_$(atol)_rtol_$(rtol).jld\", \"solution\", sol)\n",
" save(\"FH_3D_T_matrix_sol_U_$(U₀)_tmax_$(tmax)_atol_$(atol)_rtol_$(rtol).jld\",\n",
" \"solution\", sol,\n",
" \"GFs\", data.GL_u.data .+ data.GL_d.data)\n",
"end"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Second Born approx."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Callback function for the self-energies\n",
"function second_Born!(model, data, times, _, _, t, t′)\n",
" # Unpack data and model\n",
" (; GL_u, GG_u, GL_d, GG_d, ΣL_u, ΣG_u, ΣL_d, ΣG_d) = data\n",
" (; U) = model\n",
" \n",
" # Resize self-energies when Green functions are resized \n",
" if (n = size(GL_u, 3)) > size(ΣL_u, 3)\n",
" resize!(ΣL_u, n)\n",
" resize!(ΣG_u, n)\n",
" resize!(ΣL_d, n)\n",
" resize!(ΣG_d, n) \n",
" end\n",
" \n",
" # The interaction varies as a function of the forward time (t+t')/2\n",
" U_t = U((times[t] + times[t′])/2)\n",
" \n",
" # Define the self-energies\n",
" ΣL_u[t, t′] = U_t^2 .* GL_u[t, t′] .* GL_d[t, t′] .* transpose(GG_d[t′, t])\n",
" ΣL_d[t, t′] = U_t^2 .* GL_u[t, t′] .* GL_d[t, t′] .* transpose(GG_u[t′, t])\n",
" \n",
" ΣG_u[t, t′] = U_t^2 .* GG_u[t, t′] .* GG_d[t, t′] .* transpose(GL_d[t′, t])\n",
" ΣG_d[t, t′] = U_t^2 .* GG_u[t, t′] .* GG_d[t, t′] .* transpose(GL_u[t′, t])\n",
"end"
]
},
Expand Down Expand Up @@ -500,15 +541,13 @@
"metadata": {},
"outputs": [],
"source": [
"loaded_sol = []\n",
"GFs = []\n",
"ts = []\n",
"\n",
"for (i, (rtol, atol)) in enumerate(tols)\n",
" sol = load(\"FH_3D_T_matrix_sol_U_$(U₀)_tmax_$(tmax)_atol_$(atol)_rtol_$(rtol).jld\")\n",
" push!(loaded_sol, sol)\n",
" push!(GFs, sol[\"solution\"].u)\n",
" push!(ts, sol[\"solution\"].t)\n",
" loaded_data = load(\"FH_3D_T_matrix_sol_U_$(U₀)_tmax_$(tmax)_atol_$(atol)_rtol_$(rtol).jld\")\n",
" push!(GFs, loaded_data[\"GFs\"])\n",
" push!(ts, loaded_data[\"solution\"].t)\n",
"end;"
]
},
Expand All @@ -521,7 +560,7 @@
"outputs": [],
"source": [
"xpad = 8\n",
"ypad = 5;\n",
"ypad = 5\n",
"\n",
"figure(figsize = (8, 3))\n",
"\n",
Expand All @@ -535,11 +574,11 @@
"\n",
"plot([], [], label=\"\\$\\\\texttt{rtol}\\$\", c=\"w\")\n",
"for (i, (rtol, atol)) in enumerate(tols)\n",
" plot(ts[i], [imag(GFs[i][1][idx_1, idx_1, k, k] + GFs[i][3][idx_1, idx_1, k, k]) for k in eachindex(ts[i])], \n",
" plot(ts[i], [imag(GFs[i][idx_1, idx_1, k, k]) for k in eachindex(ts[i])], \n",
" label = L\"\\texttt{%$(rtol)}\", lw=1.5, styles[i], ms=ms, c = colors[i])\n",
"end\n",
"\n",
"plot(sol_2B.t, [imag(sol_2B.u[1][idx_1, idx_1, k, k] .+ sol_2B.u[3][idx_1, idx_1, k, k]) for k in eachindex(sol_2B.t)], \n",
"plot(sol_2B.t, [imag(data.GL_u.data[idx_1, idx_1, k, k] .+ data.GL_d.data[idx_1, idx_1, k, k]) for k in eachindex(sol_2B.t)], \n",
" lw=3, ls = \"--\", c = \"k\", alpha=0.75)\n",
"\n",
"xlim(0, tmax)\n",
Expand All @@ -554,7 +593,7 @@
"\n",
"ax = subplot(122)\n",
"for (i, (rtol, atol)) in enumerate(reverse(tols))\n",
" semilogy(ts[i], [tr(GFs[i][1][k, k]) + tr(GFs[i][3][k, k]) |> imag for k in eachindex(ts[i])] .- sum(N_u .+ N_d) .|> abs, \n",
" semilogy(ts[i], [tr(GFs[i][:, :, k, k]) |> imag for k in eachindex(ts[i])] .- sum(N_u .+ N_d) .|> abs, \n",
" ls = ls[i], c = colors[i])\n",
"end\n",
"\n",
Expand All @@ -571,19 +610,26 @@
"tight_layout(pad = 0.1, w_pad = 0.5, h_pad = 0)\n",
"# savefig(\"fermi_hubbard_T_matrix_convergence.pdf\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Julia 1.7.2",
"display_name": "Julia 1.9.4",
"language": "julia",
"name": "julia-1.7"
"name": "julia-1.9"
},
"language_info": {
"file_extension": ".jl",
"mimetype": "application/julia",
"name": "julia",
"version": "1.7.2"
"version": "1.9.4"
}
},
"nbformat": 4,
Expand Down
19 changes: 10 additions & 9 deletions src/gf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ julia> gf[i,j] == gf[:,:,...,:,i,j]
julia> gf[i,j,k] == gf[:,:,...,:,i,j,k]
```
In order to ensure a correct behaviour of the symmetries, `setindex!` is only
defined at the level of time coordinates
```julia-repl
julia> gf[i, j] = a # is equivalent to gf[..., i, j] = a[...]
```
Custom symmetries can be implemented via multiple dispatch
```julia-repl
julia> struct MySymmetry <: KadanoffBaym.AbstractSymmetry end
Expand Down Expand Up @@ -97,16 +103,11 @@ Base.eltype(::GreenFunction{T}) where {T} = T
@inline Base.getindex(::GreenFunction{T,N,A,U}, I) where {T,N,A,U} = error("Single indexing not allowed")
Base.@propagate_inbounds Base.getindex(G::GreenFunction{T,N,A,U}, I::Vararg{T1,N1}) where {T,N,A,U,T1,N1} = G.data[ntuple(i -> Colon(), N-N1)..., I...]

@inline Base.setindex!(::GreenFunction{T,N,A,U}, v, I) where {T,N,A,U} = error("Single indexing not allowed")
Base.@propagate_inbounds function Base.setindex!(G::GreenFunction{T,N,A,U}, v, I::Vararg{T1,N1}) where {T,N,A,U,T1,N1}
ts = last2(I...)
jj = front2(I...)
Base.@propagate_inbounds function Base.setindex!(G::GreenFunction{T,N,A,U}, v, i1::Int, i2::Int) where {T,N,A,U}
G.data[ntuple(i -> Colon(), Val(N - 2))..., i1, i2] = v

if ==(ts...)
G.data[ntuple(i -> Colon(), N-N1)..., jj..., ts...] = v
else
G.data[ntuple(i -> Colon(), N-N1)..., jj..., ts...] = v
G.data[ntuple(i -> Colon(), N-N1)..., jj..., reverse(ts)...] = symmetry(U)(v)
if i1 != i2
G.data[ntuple(i -> Colon(), Val(N - 2))..., i2, i1] = symmetry(U)(v)
end
end

Expand Down
37 changes: 16 additions & 21 deletions test/gf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,6 @@ end
end

@testset "setindex!" begin
function setindexA(A::AbstractArray)
for i in 1:N, j in 1:i
A[:, :, i, j] = b
if j != i
A[:, :, j, i] = -adjoint(b)
end
end
end

function setindexG(G)
for i in 1:N, j in 1:i
G[i, j] = b
end
end

data = zeros(ComplexF64, N, N, N, N)
gf = GreenFunction(copy(data), SkewHermitian)

Expand All @@ -73,11 +58,21 @@ end
data[:, :, 2, 1] = -adjoint(b)
@test gf.data == data

setindexA(data)
setindexG(gf)
@test gf.data == data
# # this used to fail
# gf[:, :, 2, 1] = b
# data[:, :, 2, 1] = b
# data[:, :, 1, 2] = -adjoint(b)

for i in 1:N, j in 1:i
data[:, :, i, j] = b
if j != i
data[:, :, j, i] = -adjoint(b)
end
end

for i in 1:N, j in 1:i
gf[i, j] = b
end

# using BenchmarkTools
# @show @btime $setindexA($data)
# @show @btime $setindexG($gf)
@test gf.data == data
end

2 comments on commit 1481ad6

@timbode
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/103046

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v1.3.1 -m "<description of version>" 1481ad6b7209a00fb8841d43758d44f747b76207
git push origin v1.3.1

Please sign in to comment.