@@ -746,25 +746,22 @@ def ewald(cell, ew_eta=None, ew_cut=None):
746746 # where
747747 # ZS_I(G) = \sum_a Z_a exp (i G.R_a)
748748
749- Gv , Gvbase , weights = cell .get_Gv_weights (mesh )
750- absG2 = np .einsum ('gi,gi->g' , Gv , Gv )
751- absG2 [absG2 == 0 ] = 1e200
749+ if cell .dimension == 3 or cell .low_dim_ft_type == 'inf_vacuum' :
750+ Gv , Gvbase , weights = cell .get_Gv_weights (mesh )
751+ absG2 = np .einsum ('gi,gi->g' , Gv , Gv )
752+ absG2 [absG2 == 0 ] = 1e200
752753
753- if cell .dimension != 2 or cell .low_dim_ft_type == 'inf_vacuum' :
754- # TODO: truncated Coulomb for 0D. The non-uniform grids for inf-vacuum
755- # have relatively large error
756754 coulG = 4 * np .pi / absG2
757755 coulG *= weights
758756
759757 #:ZSI = np.einsum('i,ij->j', chargs, cell.get_SI(Gv))
760- ngrids = len (Gv )
761- ZSI = np .empty ((ngrids ,), dtype = np .complex128 )
762- mem_avail = cell .max_memory - lib .current_memory ()[0 ]
763- blksize = int ((mem_avail * 1e6 - cell .natm * 24 )/ ((3 + cell .natm * 2 )* 8 ))
764- blksize = min (ngrids , max (mesh [2 ], blksize ))
765- for ig0 , ig1 in lib .prange (0 , ngrids , blksize ):
766- np .einsum ('i,ij->j' , chargs , cell .get_SI (Gv [ig0 :ig1 ]), out = ZSI [ig0 :ig1 ])
767-
758+ basex , basey , basez = cell .get_Gv_weights (mesh )[1 ]
759+ b = cell .reciprocal_vectors ()
760+ rb = np .dot (coords , b .T )
761+ SIx = np .exp (- 1j * np .einsum ('z,g->zg' , rb [:,0 ], basex ))
762+ SIy = np .exp (- 1j * np .einsum ('z,g->zg' , rb [:,1 ], basey ))
763+ SIz = np .exp (- 1j * np .einsum ('z,g->zg' , rb [:,2 ], basez ))
764+ ZSI = np .einsum ('i,ix,iy,iz->xyz' , chargs , SIx , SIy , SIz ).ravel ()
768765 ZexpG2 = ZSI * np .exp (- absG2 / (4 * ew_eta ** 2 ))
769766 ewg = .5 * np .einsum ('i,i,i' , ZSI .conj (), ZexpG2 , coulG ).real
770767
@@ -789,6 +786,8 @@ def gn0(eta,z):
789786 inv_area = np .linalg .norm (np .cross (b [0 ], b [1 ]))/ (2 * np .pi )** 2
790787 # Perform the reciprocal space summation over all reciprocal vectors
791788 # within the x,y plane.
789+ Gv , Gvbase , weights = cell .get_Gv_weights (mesh )
790+ absG2 = np .einsum ('gi,gi->g' , Gv , Gv )
792791 planarG2_idx = np .logical_and (Gv [:,2 ] == 0 , absG2 > 0.0 )
793792 Gv = Gv [planarG2_idx ]
794793 absG2 = absG2 [planarG2_idx ]
0 commit comments