Skip to content

Commit 3a20efb

Browse files
committed
Update examples
1 parent 81e3679 commit 3a20efb

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

examples/pbc/31-pbc_0D_as_mol.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,20 @@
4545
print('E(HF) of molecular RHF with cell %s' % mf.e_tot)
4646

4747
#
48-
# Convert mol back to cell.
48+
# Convert mol back to cell using the to_cell function.
49+
# Lattice vectors "a" are not available in the mole object. Specify a "box" to
50+
# place the mole in the cell.
4951
#
50-
# The mol ojbect above contains all information of the pbc system which was
51-
# initialized at the beginning. Using the "view" method to convert mol back to
52-
# the cell object, all information can be transfer to the resultant cell
53-
# object. Lattice vectors "a" are not available in the mole object. It needs
54-
# to be specified in the cell.
55-
#
56-
cell_0D = mol.view(pbcgto.Cell)
57-
cell_0D.a = numpy.eye(3)
58-
cell_0D.dimension = 0
52+
cell_0D = mol.to_cell(box=a, dimension=0)
5953
mf = pbcscf.RHF(cell).density_fit().run()
6054
print('E(HF) with 0D PBC RHF calculation %s' % mf.e_tot)
55+
56+
#
57+
# By transforming Mole to Cell instance, we can apply the MultiGrid integral
58+
# algorithm for DFT calculations to fastly evaluate the Coulomb and XC functional.
59+
#
60+
cell_0D.verbose = 5
61+
mf = cell_0D.RKS(xc='pbe')
62+
mf.run() # This calls the standard numint module
63+
mf = mf.multigrid_numint()
64+
mf.run() # This calls the MultiGridNumInt algorithm

pyscf/gto/mole.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3849,14 +3849,13 @@ def to_cell(self, box=None, dimension=None, margin=None):
38493849
dimension : integer
38503850
PBC dimensions
38513851
margin: float
3852-
The distance from the edge of the molecule to the edge of the box.
3852+
The distance from the edge of the molecule to the edge of the box.
38533853
If not provided, a default margin will be estimated, to ensure
38543854
that the electron density decays to approximately 1e-7 outside
38553855
of the box.
38563856
'''
38573857
from pyscf.pbc.gto import Cell, rcut_by_shells
3858-
cell = Cell()
3859-
cell.__dict__.update(self.__dict__)
3858+
cell = mol.view(Cell)
38603859
if box is None:
38613860
# Place molecule in a big box for dimension=0
38623861
dimension = 0

pyscf/pbc/gto/cell.py

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)