Skip to content
Snippets Groups Projects
Commit 9c4bf187 authored by Johanna Zijderveld's avatar Johanna Zijderveld
Browse files

add minimal example and function for hopping dict to flat and back

parent 72b05849
No related branches found
No related tags found
1 merge request!4Interface refactoring
This commit is part of merge request !4. Comments created here will be created in the context of that merge request.
# %%
import numpy as np
from codes.kwant_helper import utils
from codes import kwant_examples
# %%
# Example hopping dictionary to use:
graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard()
tb_model = utils.builder2tb_model(graphene_builder)
# %%
def hop_dict_to_flat(hop_dict):
sorted_vals = np.array(list(hop_dict.values()))[
np.lexsort(np.array(list(hop_dict.keys())).T)
]
flat = sorted_vals[..., *np.triu_indices(sorted_vals.shape[-1])].flatten()
return flat
def flat_to_hop_dict(flat, shape, hop_dict_keys):
matrix = np.zeros(shape, dtype=complex)
matrix[..., *np.triu_indices(shape[-1])] = flat.reshape(*shape[:-2], -1)
indices = np.arange(shape[-1])
diagonal = matrix[..., indices, indices]
matrix += np.moveaxis(matrix[-1::-1], -1, -2).conj()
matrix[..., indices, indices] -= diagonal
hop_dict_keys = np.array(list(hop_dict_keys))
sorted_keys = hop_dict_keys[np.lexsort(hop_dict_keys.T)]
hop_dict = dict(zip(map(tuple, sorted_keys), matrix))
return hop_dict
# %%
flat = hop_dict_to_flat(tb_model)
shape = (len(tb_model.keys()), *list(tb_model.values())[0].shape)
hop_dict = flat_to_hop_dict(flat, shape, tb_model.keys())
# %%
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