Skip to content

Commit

Permalink
Numpy 2 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
bonfus committed Sep 5, 2024
1 parent 689398d commit 4fa94af
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
12 changes: 6 additions & 6 deletions python/LFC.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def __init__(self, BCont, BDip, BLor, ACont=0.):
raise ValueError("Must have the same shape!")


self._BLor = np.asarray(BLor,np.float)
self._BDip = np.asarray(BDip,np.float)
self._BCont = np.asarray(BCont,np.float)
self._BLor = np.asarray(BLor,np.float64)
self._BDip = np.asarray(BDip,np.float64)
self._BCont = np.asarray(BCont,np.float64)

try:
self._ACont = np.float(ACont)
self._ACont = np.float64(ACont)
except:
raise TypeError( "Cannot set value for ACont. Must be float." )

Expand Down Expand Up @@ -154,7 +154,7 @@ def ACont(self):
@ACont.setter
def ACont(self,value):
try:
self._ACont = np.float(value)
self._ACont = np.float64(value)
except:
raise TypeError( "Cannot set value for ACont" )

Expand Down Expand Up @@ -328,7 +328,7 @@ def locfield(lattice_params, atomic_positions, fourier_components, propagation_v
# Remove non magnetic atoms from list
magnetic_atoms=[]
for i, e in enumerate(fourier_components):
if not np.allclose(e,np.zeros(3,dtype=np.complex)):
if not np.allclose(e,np.zeros(3,dtype=np.complex128)):
magnetic_atoms.append(i)

p = positions[magnetic_atoms,:]
Expand Down
13 changes: 7 additions & 6 deletions python/lfclib.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "Python.h"
#include <numpy/arrayobject.h>
#include <numpy/npy_math.h>
#include "dipolartensor.h"
#include "fastincommsum.h"
#include "rotatesum.h"
Expand Down Expand Up @@ -387,14 +388,14 @@ static PyObject * py_lfclib_fields(PyObject *self, PyObject *args) {

for (i=0; i< num_atoms; i++){
v = *(npy_cdouble *)PyArray_GETPTR2(FC, i,0);
in_fc[6*i+0] = v.real;
in_fc[6*i+1] = v.imag;
in_fc[6*i+0] = npy_creal(v);
in_fc[6*i+1] = npy_cimag(v);
v = *(npy_cdouble *)PyArray_GETPTR2(FC, i,1);
in_fc[6*i+2] = v.real;
in_fc[6*i+3] = v.imag;
in_fc[6*i+2] = npy_creal(v);
in_fc[6*i+3] = npy_cimag(v);
v = *(npy_cdouble *)PyArray_GETPTR2(FC, i,2);
in_fc[6*i+4] = v.real;
in_fc[6*i+5] = v.imag;
in_fc[6*i+4] = npy_creal(v);
in_fc[6*i+5] = npy_cimag(v);
}


Expand Down
20 changes: 10 additions & 10 deletions python/tests/test_lfclib.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def rotation_matrix(axis, theta):
class TestLFCExtension(unittest.TestCase):
def test_one_over_r_cube(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.])

phi= np.array([0.,])
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_one_over_r_cube(self):

def test_rotation_of_cart_coord(self):
p = np.array([[0.1,0.2,0.3]])
fc = np.array([[0.2,0.4,1.]],dtype=np.complex)
fc = np.array([[0.2,0.4,1.]],dtype=np.complex128)
k = np.array([0.2,0.3,0.4])

phi= np.array([0.,])
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_rotation_of_cart_coord(self):

def test_rotate1(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.])

phi= np.array([0.,])
Expand Down Expand Up @@ -142,7 +142,7 @@ def test_rotate1(self):

def test_icommensurate(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,1.j,1.]],dtype=np.complex)
fc = np.array([[0.,1.j,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.0])

phi= np.array([0.,])
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_icommensurate(self):

def test_phase(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.0])

phi= np.array([0.,])
Expand Down Expand Up @@ -230,7 +230,7 @@ def test_phase(self):
## + 0 - 0 or + + - -

p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.j]],dtype=np.complex)
fc = np.array([[0.,0.,1.j]],dtype=np.complex128)
k = np.array([0.,0.,0.0])

phi= np.array([0.125,])
Expand All @@ -249,7 +249,7 @@ def test_phase(self):
# no use the equivalent order without phase
phi= np.array([0.,])
#pi/4 is sqrt(2)/2
fc = np.array([[0.,0.,np.sqrt(2.)/2.]],dtype=np.complex)
fc = np.array([[0.,0.,np.sqrt(2.)/2.]],dtype=np.complex128)

c,d,l = lfclib.Fields('s', p,fc,k,phi,mu,sc,latpar,r,nnn,rc)

Expand All @@ -259,7 +259,7 @@ def test_phase(self):

#### now test incommensurate function
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,1.j,1.]],dtype=np.complex)
fc = np.array([[0.,1.j,1.]],dtype=np.complex128)
k = np.array([0.1,0.,0.0])

phi= np.array([0.125,])
Expand Down Expand Up @@ -288,7 +288,7 @@ def test_phase(self):

def test_null_by_symmetry(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.0])

phi= np.array([0.,])
Expand Down Expand Up @@ -344,7 +344,7 @@ def test_dipolar_tensor(self):

def test_dipolar_tensor_traceless(self):
p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.0])

phi= np.array([0.,])
Expand Down
2 changes: 1 addition & 1 deletion python/tests/test_lfcwrappers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class TestLFCWrappers(unittest.TestCase):
def test_one_over_r_cube(self):

p = np.array([[0.,0.,0.]])
fc = np.array([[0.,0.,1.]],dtype=np.complex)
fc = np.array([[0.,0.,1.]],dtype=np.complex128)
k = np.array([0.,0.,0.])

phi= np.array([0.,])
Expand Down

0 comments on commit 4fa94af

Please sign in to comment.