Skip to content

Commit

Permalink
Working version of python API
Browse files Browse the repository at this point in the history
minimal version to make the driver in the test folder work.
  • Loading branch information
lcrippa committed Sep 4, 2024
1 parent 64b4a81 commit f744234
Show file tree
Hide file tree
Showing 15 changed files with 976 additions and 278 deletions.
10 changes: 8 additions & 2 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ project('edipy2', 'fortran')
python = import('python').find_installation(pure: false)

scifor_dep= dependency('scifor', required:true)
edipack_dep= dependency('edipack', required:true)
edipack_dep= dependency('edipack2', required:true)

fortran_src = ['src/ED_INPUT_VARS.f90','src/edi2py/edi2py.f90']
fortran_src = ['src/ED_INPUT_VARS.f90',
'src/edi2py/edi2py.f90']
python_src = ['python/edi2py.py',
'python/func_read_input.py',
'python/func_aux_funx.py',
'python/func_bath.py',
'python/func_main.py',
'python/func_io.py',
'python/func_bath_fit.py',
'python/__init__.py']

library('edi2py',
Expand Down
32 changes: 30 additions & 2 deletions python/edi2py.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,36 @@ def setter(self, new_value):
# GLOBAL FUNCTIONS
######################################

#read input
#read_input
import func_read_input
global_env.read_input = types.MethodType(func_read_input.read_input, global_env)


#aux_funx
import func_aux_funx
global_env.set_hloc = types.MethodType(func_aux_funx.set_hloc, global_env)
global_env.search_variable = types.MethodType(func_aux_funx.search_variable, global_env)
global_env.check_convergence = types.MethodType(func_aux_funx.check_convergence, global_env)

#bath
import func_bath
global_env.get_bath_dimension = types.MethodType(func_bath.get_bath_dimension, global_env)
global_env.set_Hreplica = types.MethodType(func_bath.set_Hreplica, global_env)
global_env.break_symmetry_bath = types.MethodType(func_bath.break_symmetry_bath, global_env)
global_env.spin_symmetrize_bath = types.MethodType(func_bath.spin_symmetrize_bath, global_env)
global_env.orb_symmetrize_bath = types.MethodType(func_bath.orb_symmetrize_bath, global_env)
global_env.orb_equality_bath = types.MethodType(func_bath.orb_equality_bath, global_env)
global_env.ph_symmetrize_bath = types.MethodType(func_bath.ph_symmetrize_bath, global_env)

#main
import func_main
global_env.init_solver = types.MethodType(func_main.init_solver, global_env)
global_env.solve = types.MethodType(func_main.solve, global_env)

#io
import func_io
global_env.get_sigma = types.MethodType(func_io.get_sigma, global_env)
global_env.get_gimp = types.MethodType(func_io.get_gimp, global_env)

#bath_fit
import func_bath_fit
global_env.chi2_fitgf = types.MethodType(func_bath_fit.chi2_fitgf, global_env)
93 changes: 93 additions & 0 deletions python/func_aux_funx.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from ctypes import *
import numpy as np
import os,sys
import types

#set_hloc

def set_hloc(self,hloc=None,Nlat=None):
ed_set_Hloc_single_N2 = self.library.ed_set_Hloc_single_N2
ed_set_Hloc_single_N2.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=2, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
ed_set_Hloc_single_N2.restype = None

ed_set_Hloc_single_N4 = self.library.ed_set_Hloc_single_N4
ed_set_Hloc_single_N4.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=4, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
ed_set_Hloc_single_N4.restype = None

ed_set_Hloc_lattice_N2 = self.library.ed_set_Hloc_lattice_N2
ed_set_Hloc_lattice_N2.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=2, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
ed_set_Hloc_lattice_N2.restype = None

ed_set_Hloc_lattice_N3 = self.library.ed_set_Hloc_lattice_N3
ed_set_Hloc_lattice_N3.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=3, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
ed_set_Hloc_lattice_N3.restype = None

ed_set_Hloc_lattice_N5 = self.library.ed_set_Hloc_lattice_N5
ed_set_Hloc_lattice_N5.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=5, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
ed_set_Hloc_lattice_N5.restype = None
dim_hloc = np.asarray(np.shape(hloc),dtype=np.int64,order="F")
if(Nlat is not None):
if len(dim_hloc) == 2:
ed_set_Hloc_lattice_N2(hloc,dim_hloc,Nlat)
elif len(dim_hloc) == 3:
ed_set_Hloc_lattice_N3(hloc,dim_hloc,Nlat)
elif len(dim_hloc) == 5:
ed_set_Hloc_lattice_N5(hloc,dim_hloc,Nlat)
else:
raise ValueError ("ed_set_Hloc_lattice: dimension must be 2,3 or 5")
else:
if len(dim_hloc) == 2:
ed_set_Hloc_single_N2(hloc,dim_hloc)
elif len(dim_hloc) == 4:
ed_set_Hloc_single_N4(hloc,dim_hloc)
else:
raise ValueError ("ed_set_Hloc_site: dimension must be 2 or 4")
return ;


#search_variable
def search_variable(self,var,ntmp,converged):
search_variable_wrap = self.library.search_variable
search_variable_wrap.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=int,ndim=1, flags='F_CONTIGUOUS')]
search_variable_wrap.restype = None
var = np.asarray([var])
ntmp = np.asarray([ntmp])
converged = np.asarray([converged])
conv_int=int(converged)
search_variable_wrap(var,ntmp,converged)
if conv_int[0]==0:
converged=False
else:
converged=True
return err[0],conv_bool

#check_convergence
def check_convergence(self,func,threshold,N1,N2):
check_convergence_wrap = self.library.check_convergence
check_convergence_wrap.argtypes = [np.ctypeslib.ndpointer(dtype=complex,ndim=1, flags='F_CONTIGUOUS'),
c_int,
c_double,
c_int,
c_int,
np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=int,ndim=1, flags='F_CONTIGUOUS')]
check_convergence_wrap.restype = None
err=np.asarray([1.0])
converged=np.asarray([0])
dim_func=np.shape(func)
check_convergence_wrap(func,dim_func[0],threshold,N1,N2,err,converged)
if converged[0]==0:
conv_bool=False
else:
conv_bool=True
return err[0],conv_bool
204 changes: 204 additions & 0 deletions python/func_bath.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
from ctypes import *
import numpy as np
import os,sys
import types

#get_bath_dimension
def get_bath_dimension(self):
get_bath_dimension_wrap = self.library.get_bath_dimension
get_bath_dimension_wrap.argtypes = None
get_bath_dimension_wrap.restype = c_int
return get_bath_dimension_wrap()

#init_hreplica

def set_Hreplica(self,hvec,lambdavec):
init_hreplica_symmetries_d5 = self.library.init_Hreplica_symmetries_d5
init_hreplica_symmetries_d5.argtypes =[np.ctypeslib.ndpointer(dtype=complex,ndim=5, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=float,ndim=2, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
init_hreplica_symmetries_d5.restype = None

init_hreplica_symmetries_d3 = self.library.init_Hreplica_symmetries_d3
init_hreplica_symmetries_d3.argtypes =[np.ctypeslib.ndpointer(dtype=complex,ndim=3, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=float,ndim=2, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
init_hreplica_symmetries_d3.restype = None

init_hreplica_symmetries_lattice_d5 = self.library.init_Hreplica_symmetries_lattice_d5
init_hreplica_symmetries_lattice_d5.argtypes =[np.ctypeslib.ndpointer(dtype=complex,ndim=5, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=float,ndim=3, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
init_hreplica_symmetries_lattice_d5.restype = None

init_hreplica_symmetries_lattice_d3 = self.library.init_Hreplica_symmetries_lattice_d3
init_hreplica_symmetries_lattice_d3.argtypes =[np.ctypeslib.ndpointer(dtype=complex,ndim=3, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=float,ndim=3, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS')]
init_hreplica_symmetries_lattice_d3.restype = None


aux_norb=c_int.in_dll(self.library, "Norb").value
aux_nspin=c_int.in_dll(self.library, "Nspin").value
dim_hvec = np.asarray(np.shape(hvec),dtype=np.int64,order="F")
dim_lambdavec = np.asarray(np.shape(lambdavec),dtype=np.int64,order="F")


if(len(dim_hvec) == 3):
if(len(dim_lambdavec)==2):
init_hreplica_symmetries_d3(hvec,dim_hvec,lambdavec,dim_lambdavec)
elif(len(Ddim_lambdavec)==3):
init_hreplica_symmetries_lattice_d3(hvec,dim_hvec,lambdavec,dim_lambdavec)
else:
raise ValueError('Shape(lambdavec) != 2 or 3 in set_Hreplica')
elif(len(dim_hvec) == 5):
if(len(dim_lambdavec)==2):
init_hreplica_symmetries_d5(hvec,dim_hvec,lambdavec,dim_lambdavec)
elif(len(Ddim_lambdavec)==3):
init_hreplica_symmetries_lattice_d5(hvec,dim_hvec,lambdavec,dim_lambdavec)
else:
raise ValueError('Shape(lambdavec) != 2 or 3 in set_Hreplica')
else:
raise ValueError('Shape(Hvec) != 3 or 5 in set_Hreplica')
return ;

#break_symmetry_bath
def break_symmetry_bath(self, bath, field, sign, save=True):
break_symmetry_bath_site = self.library.break_symmetry_bath_site
break_symmetry_bath_site.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_double,
c_double,
c_int]
break_symmetry_bath_site.restype = None

break_symmetry_bath_ineq = self.library.break_symmetry_bath_ineq
break_symmetry_bath_ineq.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_double,
c_double,
c_int]
break_symmetry_bath_ineq.restype = None

if save:
save_int=1
else:
save_int=0
bath_shape = np.asarray(np.shape(bath),dtype=np.int64,order="F")
if (len(bath_shape)) == 1:
break_symmetry_bath_site(bath,bath_shape,field,float(sign),save_int)
else:
break_symmetry_bath_ineq(bath,bath_shape,field,float(sign),save_int)
return bath

#spin_symmetrize_bath

def spin_symmetrize_bath(self, bath, save=True):
spin_symmetrize_bath_site = self.library.spin_symmetrize_bath_site
spin_symmetrize_bath_site.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
spin_symmetrize_bath_site.restypes = None

spin_symmetrize_bath_ineq = self.library.spin_symmetrize_bath_ineq
spin_symmetrize_bath_ineq.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
spin_symmetrize_bath_ineq.restypes = None
if save:
save_int=1
else:
save_int=0
bath_shape = np.asarray(np.shape(bath),dtype=np.int64,order="F")
if (len(bath_shape)) == 1:
spin_symmetrize_bath_site(bath,bath_shape,save_int)
else:
spin_symmetrize_bath_ineq(bath,bath_shape,save_int)
return bath

#orb_symmetrize_bath
def orb_symmetrize_bath(self, bath, save=True):
orb_symmetrize_bath_site = self.library.orb_symmetrize_bath_site
orb_symmetrize_bath_site.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
orb_symmetrize_bath_site.restypes = None

orb_symmetrize_bath_ineq = self.library.orb_symmetrize_bath_ineq
orb_symmetrize_bath_ineq.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
orb_symmetrize_bath_ineq.restypes = None

if save:
save_int=1
else:
save_int=0
bath_shape = np.asarray(np.shape(bath),dtype=np.int64,order="F")
if (len(bath_shape)) == 1:
orb_symmetrize_bath_site(bath,bath_shape,save_int)
else:
orb_symmetrize_bath_ineq(bath,bath_shape,save_int)
return bath

#orb_equality_bath

def orb_equality_bath(self, bath, indx, save=True):
orb_equality_bath_site = self.library.orb_equality_bath_site
orb_equality_bath_site.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int,
c_int]
orb_equality_bath_site.restypes = None

orb_equality_bath_ineq = self.library.orb_equality_bath_ineq
orb_equality_bath_ineq.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int,
c_int]
orb_equality_bath_ineq.restypes = None
aux_norb=c_int.in_dll(self.library, "Norb").value
if save:
save_int=1
else:
save_int=0
bath_shape = np.asarray(np.shape(bath),dtype=np.int64,order="F")
if (indx < 0) or (indx >= aux_norb):
raise ValueError("orb_equality_bath: orbital index should be in [0,Norb]")
else:
indx = indx + 1 #python to fortran convention
if (len(bath_shape)) == 1:
orb_equality_bath_site(bath,bath_shape,indx,save_int)
else:
orb_equality_bath_ineq(bath,bath_shape,indx,save_int)
return bath


#ph_symmetrize_bath
def ph_symmetrize_bath(self, bath, save):
ph_symmetrize_bath_site = self.library.ph_symmetrize_bath_site
ph_symmetrize_bath_site.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
ph_symmetrize_bath_site.restypes = None

ph_symmetrize_bath_ineq = self.library.ph_symmetrize_bath_ineq
ph_symmetrize_bath_ineq.argtypes = [np.ctypeslib.ndpointer(dtype=float,ndim=1, flags='F_CONTIGUOUS'),
np.ctypeslib.ndpointer(dtype=np.int64,ndim=1, flags='F_CONTIGUOUS'),
c_int]
ph_symmetrize_bath_ineq.restypes = None
if save:
save_int=1
else:
save_int=0
bath_shape = np.asarray(np.shape(bath),dtype=np.int64,order="F")
if (len(bath_shape)) == 1:
ph_symmetrize_bath_site(bath,bath_shape,save_int)
else:
ph_symmetrize_bath_ineq(bath,bath_shape,save_int)
return bath

Loading

0 comments on commit f744234

Please sign in to comment.