Skip to content

Commit

Permalink
Add tests for cell.dumps
Browse files Browse the repository at this point in the history
  • Loading branch information
sunqm committed Jan 31, 2024
1 parent 0187133 commit 9fb90f2
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pyscf/gto/mole.py
Original file line number Diff line number Diff line change
Expand Up @@ -2577,11 +2577,16 @@ def build(self, dump_input=True, parse_arg=ARGPARSE,
# number of electrons are consistent.
self.nelec

if self.magmom is None or len(self.magmom) != self.natm:
if self.magmom is None:
self.magmom = [0,] * self.natm
elif len(self.magmom) != self.natm:
logger.warn(self, 'len(magmom) != natm. Set magmom to zero')
self.magmom = [0,] * self.natm
if self.spin == 0 and abs(numpy.sum(self.magmom) - self.spin) > 1e-6:
#don't check for unrestricted calcs.
raise ValueError("mol.magmom is set incorrectly.")
if isinstance(self.magmom, np.ndarray):
self.magmom = self.magmom.tolist()

if self.symmetry:
self._build_symmetry()
Expand Down
3 changes: 2 additions & 1 deletion pyscf/pbc/gto/cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ def dumps(cell):
celldic['pseudo'] = repr(cell.pseudo)
celldic['ecp'] = repr(cell.ecp)
# Explicitly convert mesh because it is often created as numpy array
celldic['mesh'] = list(cell.mesh)
if isinstance(cell.mesh, np.ndarray):
celldic['mesh'] = cell.mesh.tolist()

try:
return json.dumps(celldic)
Expand Down
4 changes: 4 additions & 0 deletions pyscf/pbc/gto/test/test_cell.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ def test_mixed_basis(self):

def test_dumps_loads(self):
cl1.loads(cl1.dumps())
# see issue 2026
from pyscf.pbc.tools.pbc import super_cell
sc = super_cell(cl1, [1,1,1])
sc.dumps()

def test_get_lattice_Ls(self):
#self.assertEqual(cl1.get_lattice_Ls([0,0,0]).shape, (1 , 3))
Expand Down
5 changes: 4 additions & 1 deletion pyscf/pbc/tools/pbc.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,10 @@ def super_cell(cell, ncopy, wrap_around=False):
supcell.a = np.einsum('i,ij->ij', ncopy, a)
mesh = np.asarray(ncopy) * np.asarray(cell.mesh)
supcell.mesh = (mesh // 2) * 2 + 1
supcell.magmom = list(cell.magmom) * np.prod(ncopy)
if isinstance(cell.magmom, np.ndarray):
supcell.magmom = cell.magmom.tolist() * np.prod(ncopy)
else:
supcell.magmom = cell.magmom * np.prod(ncopy)
return _build_supcell_(supcell, cell, Ls)


Expand Down

0 comments on commit 9fb90f2

Please sign in to comment.