diff --git a/codes/kwant_helper/utils.py b/codes/kwant_helper/utils.py index 41612e8ddabe4c151c0ec3dc134498c632b993fe..e2f35af95e86499af3ae6678846153a1ac377ade 100644 --- a/codes/kwant_helper/utils.py +++ b/codes/kwant_helper/utils.py @@ -6,7 +6,7 @@ import inspect from copy import copy -def builder2tb(builder, params={}, return_data=False): +def builder_to_tb(builder, params={}, return_data=False): """ Constructs a tight-binding model dictionary from a `kwant.Builder`. @@ -151,52 +151,3 @@ def build_interacting_syst(builder, lattice, func_onsite, func_hop, max_neighbor for neighbors in range(max_neighbor): int_builder[lattice.neighbors(neighbors + 1)] = func_hop return int_builder - - -def generate_guess(vectors, ndof, scale=1): - """ - vectors : list - List of hopping vectors. - ndof : int - Number internal degrees of freedom (orbitals), - scale : float - The scale of the guess. Maximum absolute value of each element of the guess. - - Returns: - -------- - guess : tb dictionary - Guess in the form of a tight-binding model. - """ - guess = {} - for vector in vectors: - if vector not in guess.keys(): - amplitude = scale * np.random.rand(ndof, ndof) - phase = 2 * np.pi * np.random.rand(ndof, ndof) - rand_hermitian = amplitude * np.exp(1j * phase) - if np.linalg.norm(np.array(vector)) == 0: - rand_hermitian += rand_hermitian.T.conj() - rand_hermitian /= 2 - guess[vector] = rand_hermitian - else: - guess[vector] = rand_hermitian - guess[tuple(-np.array(vector))] = rand_hermitian.T.conj() - - return guess - - -def generate_vectors(cutoff, dim): - """ - Generates hopping vectors up to a cutoff. - - Parameters: - ----------- - cutoff : int - Maximum distance along each direction. - dim : int - Dimension of the vectors. - - Returns: - -------- - List of hopping vectors. - """ - return [*product(*([[*range(-cutoff, cutoff + 1)]] * dim))] diff --git a/codes/tb/utils.py b/codes/tb/utils.py index 1d00831c97c7cfcaba577e6088144037f97a3b9d..041d372d25f3f6877742029b7322892c7d9a78a4 100644 --- a/codes/tb/utils.py +++ b/codes/tb/utils.py @@ -1,6 +1,56 @@ import numpy as np from codes.tb.transforms import tb_to_kfunc import itertools as it +from itertools import product + + +def generate_guess(vectors, ndof, scale=1): + """ + vectors : list + List of hopping vectors. + ndof : int + Number internal degrees of freedom (orbitals), + scale : float + The scale of the guess. Maximum absolute value of each element of the guess. + + Returns: + -------- + guess : tb dictionary + Guess in the form of a tight-binding model. + """ + guess = {} + for vector in vectors: + if vector not in guess.keys(): + amplitude = scale * np.random.rand(ndof, ndof) + phase = 2 * np.pi * np.random.rand(ndof, ndof) + rand_hermitian = amplitude * np.exp(1j * phase) + if np.linalg.norm(np.array(vector)) == 0: + rand_hermitian += rand_hermitian.T.conj() + rand_hermitian /= 2 + guess[vector] = rand_hermitian + else: + guess[vector] = rand_hermitian + guess[tuple(-np.array(vector))] = rand_hermitian.T.conj() + + return guess + + +def generate_vectors(cutoff, dim): + """ + Generates hopping vectors up to a cutoff. + + Parameters: + ----------- + cutoff : int + Maximum distance along each direction. + dim : int + Dimension of the vectors. + + Returns: + -------- + List of hopping vectors. + """ + return [*product(*([[*range(-cutoff, cutoff + 1)]] * dim))] def compute_gap(h, fermi_energy=0, n=100): diff --git a/codes/test_graphene.py b/codes/test_graphene.py index 1ebcd41ae1e313e7b120713756ef69e91380cb35..921428ef58b304e21423c1a7fef4361a9b277d3a 100644 --- a/codes/test_graphene.py +++ b/codes/test_graphene.py @@ -11,7 +11,7 @@ import pytest repeat_number = 10 # %% graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard() -h_0 = utils.builder2tb(graphene_builder) +h_0 = utils.builder_to_tb(graphene_builder) # %% @@ -45,7 +45,7 @@ def gap_prediction(U, V): filling = 2 nk = 20 - h_int = utils.builder2tb(int_builder, params) + h_int = utils.builder_to_tb(int_builder, params) guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0])) model = Model(h_0, h_int, filling) diff --git a/examples/graphene_extended_hubbard.ipynb b/examples/graphene_extended_hubbard.ipynb index 20b9770e677681f124134c24d7f0ba744d4a19bf..12c3bba59b09fd694f62f3af80a9180829fef4a6 100644 --- a/examples/graphene_extended_hubbard.ipynb +++ b/examples/graphene_extended_hubbard.ipynb @@ -65,7 +65,7 @@ "source": [ "# Create translationally-invariant `kwant.Builder`\n", "graphene_builder, int_builder = kwant_examples.graphene_extended_hubbard()\n", - "h_0 = utils.builder2tb(graphene_builder)" + "h_0 = utils.builder_to_tb(graphene_builder)" ] }, { @@ -84,7 +84,7 @@ " guess=None\n", " for V in Vs: \n", " params = dict(U=U, V=V)\n", - " h_int = utils.builder2tb(int_builder, params)\n", + " h_int = utils.builder_to_tb(int_builder, params)\n", " if guess==None:\n", " guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", " model = Model(h_0, h_int, filling=2)\n", @@ -225,7 +225,7 @@ "params = dict(U=0, V=1)\n", "filling = 2 \n", "\n", - "h_int = utils.builder2tb(int_builder, params)\n", + "h_int = utils.builder_to_tb(int_builder, params)\n", "model = Model(h_0, h_int, filling)\n", "mf_guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", "mf_sol = solver(model, mf_guess, nK=30)\n", @@ -295,8 +295,8 @@ "filling = 2\n", "nK = 300\n", "\n", - "h_int = utils.builder2tb(int_builder, params)\n", - "h_0 = utils.builder2tb(graphene_builder)\n", + "h_int = utils.builder_to_tb(int_builder, params)\n", + "h_0 = utils.builder_to_tb(graphene_builder)\n", "guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0]))\n", "\n", "model = Model(h_0, h_int, filling)\n", diff --git a/profiling/graphene.py b/profiling/graphene.py index 9731d90fb0e984ab34250c976b3971819cbc77f8..5b653bd343a2c1e64be0f2cfaf9b3aec01ebaba1 100644 --- a/profiling/graphene.py +++ b/profiling/graphene.py @@ -14,8 +14,8 @@ params = {"U": 0.5, "V": 1.1} filling = 2 nk = 600 -h_int = utils.builder2tb(int_builder, params) -h_0 = utils.builder2tb(graphene_builder) +h_int = utils.builder_to_tb(int_builder, params) +h_0 = utils.builder_to_tb(graphene_builder) guess = utils.generate_guess(frozenset(h_int), len(list(h_0.values())[0])) model = Model(h_0, h_int, filling)