Skip to content
Snippets Groups Projects
Commit 0a9e7282 authored by Antonio Manesco's avatar Antonio Manesco
Browse files

update examples

parent 5ffc9253
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:cb509096-42c6-4a45-8dc4-a8eed3116e67 tags:
``` python
import kwant
import numpy as np
import matplotlib.pyplot as plt
from codes import utils, hf, kwant_examples
from tqdm import tqdm
```
%% Cell type:code id:c22c6258-9ee8-4ccf-bfd8-94d50d27114d tags:
``` python
def hamiltonian(k):
tk = (1 + np.exp(1j * k)) * np.eye(2)
return np.block([
[0 * np.eye(2), tk],
[tk.conj(), 0 * np.eye(2)]
])
```
%% Cell type:code id:d31cbfea-18ea-454e-8a63-d706a85cd3fc tags:
``` python
# Compute non-interacting Hamiltonian on a coarse k-point grid
# Number of k-points along each direction
nk = 50
# k-points must start at zero
ks = np.linspace(0, 2 * np.pi, nk, endpoint=False)
hamiltonians_0 = np.array([hamiltonian(k) for k in ks])
hopping_vecs = np.array([[0,], [1,], [-1,]])
```
%% Cell type:code id:b39a2976-7c35-4670-83ef-12157bd3fc0e tags:
``` python
vals, vecs = np.linalg.eigh(hamiltonians_0)
plt.plot(ks, vals)
plt.show()
```
%% Output
%% Cell type:code id:41bd9f60-8f29-4e7c-a0c4-a0bbf66445b2 tags:
``` python
def compute_gap(
H_int,
ks,
ks_dense,
hamiltonians_0=hamiltonians_0,
filling=2,
tol=1e-5,
mixing=0.01,
order=10,
guess=None
):
# Generate guess on the same grid
if guess is None:
guess = utils.generate_guess(ks, hopping_vecs, ndof=hamiltonians_0.shape[-1], scale=1)
else:
guess += np.max(guess) * utils.generate_guess(ks, hopping_vecs, ndof=hamiltonians_0.shape[-1], scale=0.1)
# Find groundstate Hamiltonian on the same grid
hk = hf.find_groundstate_ham(
H_int=H_int,
filling=filling,
hamiltonians_0=hamiltonians_0,
tol=tol,
guess=guess,
mixing=mixing,
order=order,
)
# Diagonalize groundstate Hamiltonian
vals, vecs = np.linalg.eigh(hk)
# Extract dense-grid Fermi energy
E_F = utils.get_fermi_energy(vals, filling)
gap = utils.calc_gap(vals, E_F)
return gap, hk - hamiltonians_0, vals - E_F
```
%% Cell type:code id:32b9e7c5-db12-44f9-930c-21e5494404b8 tags:
``` python
def compute_phase_diagram(
Us,
ks,
ks_dense,
tol=1e-5,
mixing=0.01,
order=10,
):
# onsite interactions
v_int = np.block(
[[np.ones((2, 2)), np.zeros((2, 2))], [np.zeros((2, 2)), np.ones((2, 2))]]
)
v_int = np.array([v_int for k in ks])
gap = []
vals = []
guess = None
for U in tqdm(Us):
H_int = U * v_int
_gap, guess, _vals = compute_gap(
H_int=H_int,
ks=ks,
ks_dense=ks_dense,
tol=tol,
mixing=mixing,
order=order,
guess=guess,
)
gap.append(_gap)
vals.append(_vals)
return np.asarray(gap, dtype=float), np.asarray(vals)
```
%% Cell type:code id:6a8c08a9-7e31-420b-b6b4-709abfb26793 tags:
``` python
# Generate dense-grid k-points
nk_dense = 100
ks_dense = np.linspace(0, 2 * np.pi, nk_dense, endpoint=True)
# Interaction strengths
Us = np.linspace(0, 3, 10, endpoint=True)
gap, vals = compute_phase_diagram(Us, ks=ks, ks_dense=ks_dense, tol=1e-9)
```
%% Output
100%|██████████| 10/10 [00:07<00:00, 1.34it/s]
%% Cell type:code id:e17fc96c-c463-4e1f-8250-c254d761b92a tags:
``` python
import xarray as xr
ds = xr.Dataset(
data_vars=dict(
vals=(["Us", "ks", "n"], vals),
gap=(["Us"], gap)
),
coords=dict(Us=Us, ks=ks, n=np.arange(vals.shape[-1])),
)
```
%% Cell type:code id:868cf368-45a0-465e-b042-6182ff8b6998 tags:
``` python
ds.vals.plot.scatter(x='ks', hue='Us')
plt.axhline(0)
```
%% Output
<matplotlib.lines.Line2D at 0x7f008b4ef350>
%% Cell type:code id:ac2eb725-f3bd-4d5b-a509-85d0d0071958 tags:
``` python
ds.gap.plot()
```
%% Output
[<matplotlib.lines.Line2D at 0x7f007257b690>]
%% Cell type:code id:0cb395cd-84d1-49b4-89dd-da7a2d09c8d0 tags:
``` python
ds.to_netcdf('./data/1d_hubbard_example.nc')
```
File added
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment