def test_tripoints_input_rescale(self): # Test at single points x = np.array([(0, 0), (-5, -5), (-5, 5), (5, 5), (2.5, 3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) y = y - 3j * y tri = qhull.Delaunay(x) yi = interpnd.LinearNDInterpolator(tri.points, y)(x) yi_rescale = interpnd.LinearNDInterpolator(tri.points, y, rescale=True)(x) assert_almost_equal(yi, yi_rescale)
def test_smoketest_rescale(self): # Test at single points x = np.array([(0, 0), (-5, -5), (-5, 5), (5, 5), (2.5, 3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) yi = interpnd.LinearNDInterpolator(x, y, rescale=True)(x) assert_almost_equal(y, yi)
def test_smoketest_alternate(self): # Test at single points, alternate calling convention x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) yi = interpnd.LinearNDInterpolator((x[:,0], x[:,1]), y)(x[:,0], x[:,1]) assert_almost_equal(y, yi)
def test_smoketest(self): # Test at single points x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) yi = interpnd.LinearNDInterpolator(x, y)(x) assert_almost_equal(y, yi)
def test_square_rescale(self): # Test barycentric interpolation on a rectangle with rescaling # agaings the same implementation without rescaling points = np.array([(0,0), (0,100), (10,100), (10,0)], dtype=np.double) values = np.array([1., 2., -3., 5.], dtype=np.double) xx, yy = np.broadcast_arrays(np.linspace(0, 10, 14)[:,None], np.linspace(0, 100, 14)[None,:]) xx = xx.ravel() yy = yy.ravel() xi = np.array([xx, yy]).T.copy() zi = interpnd.LinearNDInterpolator(points, values)(xi) zi_rescaled = interpnd.LinearNDInterpolator(points, values, rescale=True)(xi) assert_almost_equal(zi, zi_rescaled)
def test_pickle(self): # Test at single points np.random.seed(1234) x = np.random.rand(30, 2) y = np.random.rand(30) + 1j * np.random.rand(30) ip = interpnd.LinearNDInterpolator(x, y) ip2 = pickle.loads(pickle.dumps(ip)) assert_almost_equal(ip(0.5, 0.5), ip2(0.5, 0.5))
def test_tri_input(self): # Test at single points x = np.array([(0,0), (-0.5,-0.5), (-0.5,0.5), (0.5, 0.5), (0.25, 0.3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) y = y - 3j*y tri = qhull.Delaunay(x) yi = interpnd.LinearNDInterpolator(tri, y)(x) assert_almost_equal(y, yi)
def test_tri_input_rescale(self): # Test at single points x = np.array([(0, 0), (-5, -5), (-5, 5), (5, 5), (2.5, 3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) y = y - 3j * y tri = qhull.Delaunay(x) match = ("Rescaling is not supported when passing a " "Delaunay triangulation as ``points``.") with pytest.raises(ValueError, match=match): interpnd.LinearNDInterpolator(tri, y, rescale=True)(x)
def test_tri_input_rescale(self): # Test at single points x = np.array([(0, 0), (-5, -5), (-5, 5), (5, 5), (2.5, 3)], dtype=np.double) y = np.arange(x.shape[0], dtype=np.double) y = y - 3j * y tri = qhull.Delaunay(x) try: interpnd.LinearNDInterpolator(tri, y, rescale=True)(x) except ValueError as e: if str(e) != ("Rescaling is not supported when passing a " "Delaunay triangulation as ``points``."): raise except: raise
def test_square(self): # Test barycentric interpolation on a square against a manual # implementation points = np.array([(0,0), (0,1), (1,1), (1,0)], dtype=np.double) values = np.array([1., 2., -3., 5.], dtype=np.double) # NB: assume triangles (0, 1, 3) and (1, 2, 3) # # 1----2 # | \ | # | \ | # 0----3 def ip(x, y): t1 = (x + y <= 1) t2 = ~t1 x1 = x[t1] y1 = y[t1] x2 = x[t2] y2 = y[t2] z = 0*x z[t1] = (values[0]*(1 - x1 - y1) + values[1]*y1 + values[3]*x1) z[t2] = (values[2]*(x2 + y2 - 1) + values[1]*(1 - x2) + values[3]*(1 - y2)) return z xx, yy = np.broadcast_arrays(np.linspace(0, 1, 14)[:,None], np.linspace(0, 1, 14)[None,:]) xx = xx.ravel() yy = yy.ravel() xi = np.array([xx, yy]).T.copy() zi = interpnd.LinearNDInterpolator(points, values)(xi) assert_almost_equal(zi, ip(xx, yy))
def __init__(self,astruct,mesh,evals,fermi_energy): import spglib,numpy cell=astruct_to_cell(astruct) self.lattice=cell[0] #celltmp=[ a if a!=float('inf') else 1.0 for a in astruct['cell']] #self.lattice=numpy.diag(celltmp) #print 'lattice',self.lattice #pos=[ [ a/b if b!=float('inf') else 0.0 for a,b in zip(at.values()[0], celltmp)]for at in astruct['positions']] #atoms=[ at.keys()[0] for at in astruct['positions']] #ianames,iatype=numpy.unique(atoms,return_inverse=True) #[1,]*4+[2,]*4 #we should write a function for the iatype #print 'iatype', iatype #cell=(self.lattice,pos,iatype) safe_print('spacegroup',spglib.get_spacegroup(cell, symprec=1e-5)) #then define the pathes and special points import ase.dft.kpoints as ase #we should adapt the 'cubic' cell_tmp=astruct['cell'] #print 'cell',cell_tmp,numpy.allclose(cell_tmp,[cell_tmp[0],]*len(cell_tmp)) if numpy.allclose(cell_tmp,[cell_tmp[0],]*len(cell_tmp)): lattice_string='cubic' else: lattice_string='orthorhombic' safe_print('Lattice found:',lattice_string) self.special_points=ase.get_special_points(lattice_string, self.lattice, eps=0.0001) self.special_paths=ase.parse_path_string(ase.special_paths[lattice_string]) self.fermi_energy=fermi_energy #dataset = spglib.get_symmetry_dataset(cell, symprec=1e-3) #print dataset #the shift has also to be put if present mapping, grid = spglib.get_ir_reciprocal_mesh(mesh, cell, is_shift=[0, 0, 0]) lookup=[] for ikpt in numpy.unique(mapping): ltmp=[] for ind, (m, g) in enumerate(zip(mapping, grid)): if m==ikpt: ltmp.append((g,ind)) lookup.append(ltmp) safe_print('irreductible k-points',len(lookup)) #print 'mapping',mapping #print 'grid',len(grid),numpy.max(grid) coords=numpy.array(grid, dtype = numpy.float)/mesh #print 'coords',coords #print 'shape',coords.shape #print grid #[ x+mesh[0]*y+mesh[0]*mesh[1]*z for x,y,z in grid] #brillouin zone kp=numpy.array([ k.kpt for k in evals]) ourkpt=numpy.rint(kp*(numpy.array(mesh))).astype(int) #print ourkpt bz=numpy.ndarray((coords.shape[0],evals[0].size),dtype=float) #print bz shift=(numpy.array(mesh)-1)/2 #print 'shift',shift for ik in lookup: irrk=None for orbs,bzk in zip(evals,ourkpt): for (kt,ind) in ik: if (bzk==kt).all(): irrk=orbs #print 'hello',orbs.kpt,kt break if irrk is not None: break if irrk is None: safe_print( 'error in ik',ik) safe_print( 'our',ourkpt) safe_print( 'spglib',grid) safe_print( 'mapping',mapping) for (kt,ind) in ik: #r=kt+shift #ind=numpy.argwhere([(g==kt).all() for g in grid]) #print 'ik',kt,r,ind #print irrk.shape, bz.shape #bz[r[0],r[1],r[2],:]=irrk.reshape(irrk.size) bz[ind,:]=irrk.reshape(irrk.size) #duplicate coordinates for the interpolation bztmp=bz#.reshape((mesh[0]*mesh[1]*mesh[2], -1)) #print bztmp ndup=7 duplicates=[[-1,0,0],[1,0,0],[0,-1,0],[0,1,0],[0,0,-1],[0,0,1]] bztot=numpy.ndarray((ndup,bztmp.shape[0],bztmp.shape[1])) bztot[0,:,:]=bztmp ctot=numpy.ndarray((ndup,coords.shape[0],coords.shape[1])) ctot[0,:,:]=coords for i,sh in enumerate(duplicates): bztot[i+1,:,:]=bztmp ctot[i+1,:,:]=coords+sh #print 'coors',coords,coords+[1.0,0,0] bztot=bztot.reshape((ndup*bztmp.shape[0], -1)) ctot=ctot.reshape((ndup*coords.shape[0], -1)) import scipy.interpolate.interpnd as interpnd self.interpolator=interpnd.LinearNDInterpolator(ctot,bztot) #sanity check of the interpolation sanity=0.0 for kpt in evals: diff=numpy.ravel(numpy.ravel(kpt)-numpy.ravel(self.interpolator([ kpt.kpt]))) sanity=max(sanity,numpy.dot(diff,diff)) print('Interpolation bias',sanity)