From 1194fdf17c4e9d836022ecfaf88fdd58f3442707 Mon Sep 17 00:00:00 2001
From: Kostas Vilkelis <kostasvilkelis@gmail.com>
Date: Tue, 7 May 2024 13:39:01 +0200
Subject: [PATCH] add kgrid_to_tb fun; bump down the default nk values

---
 pymf/__init__.py      |  3 ++-
 pymf/mf.py            |  5 ++---
 pymf/model.py         |  2 +-
 pymf/solvers.py       |  4 ++--
 pymf/tb/transforms.py | 20 ++++++++++++++++++++
 5 files changed, 27 insertions(+), 7 deletions(-)

diff --git a/pymf/__init__.py b/pymf/__init__.py
index ee5764d..bb870d2 100644
--- a/pymf/__init__.py
+++ b/pymf/__init__.py
@@ -14,7 +14,7 @@ from .solvers import solver
 from .model import Model
 from .observables import expectation_value
 from .tb.tb import add_tb, scale_tb
-from .tb.transforms import tb_to_kgrid
+from .tb.transforms import tb_to_kgrid, kgrid_to_tb
 from .tb.utils import generate_guess, calculate_fermi_energy
 
 
@@ -29,6 +29,7 @@ __all__ = [
     "construct_density_matrix",
     "meanfield",
     "tb_to_kgrid",
+    "kgrid_to_tb",
     "__version__",
     "__version_tuple__",
 ]
diff --git a/pymf/mf.py b/pymf/mf.py
index e6aa9ec..fd2c338 100644
--- a/pymf/mf.py
+++ b/pymf/mf.py
@@ -1,9 +1,8 @@
 import numpy as np
-from scipy.fftpack import ifftn
 from typing import Tuple
 
 from pymf.tb.tb import add_tb, _tb_type
-from pymf.tb.transforms import ifftn_to_tb, tb_to_kgrid
+from pymf.tb.transforms import tb_to_kgrid, kgrid_to_tb
 
 
 def construct_density_matrix_kgrid(
@@ -61,7 +60,7 @@ def construct_density_matrix(
         kham = tb_to_kgrid(h, nk=nk)
         density_matrix_krid, fermi = construct_density_matrix_kgrid(kham, filling)
         return (
-            ifftn_to_tb(ifftn(density_matrix_krid, axes=np.arange(ndim))),
+            kgrid_to_tb(density_matrix_krid),
             fermi,
         )
     else:
diff --git a/pymf/model.py b/pymf/model.py
index 89ea730..00a7fd4 100644
--- a/pymf/model.py
+++ b/pymf/model.py
@@ -76,7 +76,7 @@ class Model:
         _check_hermiticity(h_0)
         _check_hermiticity(h_int)
 
-    def mfield(self, mf: _tb_type, nk: int = 200) -> _tb_type:
+    def mfield(self, mf: _tb_type, nk: int = 20) -> _tb_type:
         """Computes a new mean-field correction from a given one.
 
         Parameters
diff --git a/pymf/solvers.py b/pymf/solvers.py
index dc73dc5..5209412 100644
--- a/pymf/solvers.py
+++ b/pymf/solvers.py
@@ -9,7 +9,7 @@ from pymf.model import Model
 from pymf.tb.utils import calculate_fermi_energy
 
 
-def cost(mf_param: np.ndarray, model: Model, nk: int = 100) -> np.ndarray:
+def cost(mf_param: np.ndarray, model: Model, nk: int = 20) -> np.ndarray:
     """Defines the cost function for root solver.
 
     The cost function is the difference between the computed and inputted mean-field.
@@ -40,7 +40,7 @@ def cost(mf_param: np.ndarray, model: Model, nk: int = 100) -> np.ndarray:
 def solver(
     model: Model,
     mf_guess: np.ndarray,
-    nk: int = 100,
+    nk: int = 20,
     optimizer: Optional[Callable] = scipy.optimize.anderson,
     optimizer_kwargs: Optional[dict[str, str]] = {"M": 0},
 ) -> _tb_type:
diff --git a/pymf/tb/transforms.py b/pymf/tb/transforms.py
index eb7cf09..6d7a871 100644
--- a/pymf/tb/transforms.py
+++ b/pymf/tb/transforms.py
@@ -1,5 +1,6 @@
 import itertools
 import numpy as np
+from scipy.fftpack import ifftn
 
 from pymf.tb.tb import _tb_type
 
@@ -37,6 +38,25 @@ def tb_to_kgrid(tb: _tb_type, nk: int) -> np.ndarray:
     return np.sum(tb_array * k_dependency, axis=0)
 
 
+def kgrid_to_tb(kgrid_array: np.ndarray) -> _tb_type:
+    """
+    Convert a k-space grid array to a tight-binding dictionary.
+
+    Parameters
+    ----------
+    kgrid_array :
+        K-space grid array to convert to a tight-binding dictionary.
+        The array should be of shape (nk, nk, ..., ndof, ndof),
+        where ndof is number of internal degrees of freedom.
+    Returns
+    -------
+    :
+        Tight-binding dictionary.
+    """
+    ndim = len(kgrid_array.shape) - 2
+    return ifftn_to_tb(ifftn(kgrid_array, axes=np.arange(ndim)))
+
+
 def ifftn_to_tb(ifft_array: np.ndarray) -> _tb_type:
     """
     Convert the result of `scipy.fft.ifftn` to a tight-binding dictionary.
-- 
GitLab