Repository: pyscf/pyscf — pull request #1821 (python)
Title: Fix pbc empty cell error

fix issue #1819.

Resolved issues:
title: example-pbc: 40-customize_kpts_hamiltonian.py
body: Hi all,
I tried to run 40-customize_kpts_hamiltonian.py, but got the error:

'''
Traceback (most recent call last):
  File "/home/jtolle/Software/pyscf_master/pyscf/examples/pbc/40-customize_kpts_hamiltonian.py", line 33, in <module>
    cell = pbc.gto.M(
  File "/home/jtolle/Software/pyscf_master/pyscf/pyscf/pbc/gto/cell.py", line 73, in M
    cell.build(**kwargs)
  File "/home/jtolle/Software/pyscf_master/pyscf/pyscf/pbc/gto/cell.py", line 1522, in build
    atom_boundary_max = scaled_atom_coords[:,:self.dimension].max(axis=0)
  File "/home/jtolle/anaconda3/lib/python3.9/site-packages/numpy/core/_methods.py", line 41, in _amax
    return umr_maximum(a, axis, None, out, keepdims, initial, where)
ValueError: zero-size array to reduction operation maximum which has no identity
'''
(using the current master)

Using this commit: 71391d862c3db83db28553bbb1518acee466662c it works.
Would be great if someone can help to resolve this issue.
Best regards,
Johannes



Hints:

Fix patch:
diff --git a/pyscf/pbc/gto/cell.py b/pyscf/pbc/gto/cell.py
index 8ae280772a..8fc38d5e61 100644
--- a/pyscf/pbc/gto/cell.py
+++ b/pyscf/pbc/gto/cell.py
@@ -1517,7 +1517,7 @@ def build(self, dump_input=True, parse_arg=mole.ARGPARSE,
             logger.info(self, 'low_dim_ft_type = %s', self.low_dim_ft_type)
             logger.info(self, 'Cell volume = %g', self.vol)
             # Check atoms coordinates
-            if self.dimension > 0:
+            if self.dimension > 0 and self.natm > 0:
                 scaled_atom_coords = np.linalg.solve(_a.T, self.atom_coords().T).T
                 atom_boundary_max = scaled_atom_coords[:,:self.dimension].max(axis=0)
                 atom_boundary_min = scaled_atom_coords[:,:self.dimension].min(axis=0)
diff --git a/pyscf/pbc/tools/pbc.py b/pyscf/pbc/tools/pbc.py
index 985c0f2550..447f752590 100644
--- a/pyscf/pbc/tools/pbc.py
+++ b/pyscf/pbc/tools/pbc.py
@@ -503,7 +503,7 @@ def get_lattice_Ls(cell, nimgs=None, rcut=None, dimension=None, discard=True):
     if rcut is None:
         rcut = cell.rcut
 
-    if dimension == 0 or rcut <= 0:
+    if dimension == 0 or rcut <= 0 or cell.natm == 0:
         return np.zeros((1, 3))
 
     a = cell.lattice_vectors()


Test patch:
diff --git a/pyscf/pbc/gto/test/test_cell.py b/pyscf/pbc/gto/test/test_cell.py
index e7aeafe577..61dc9f6e72 100644
--- a/pyscf/pbc/gto/test/test_cell.py
+++ b/pyscf/pbc/gto/test/test_cell.py
@@ -478,6 +478,11 @@ def test_eval_gto(self):
         ao_value = cell.pbc_eval_gto("GTOval_ip_cart", coords, kpts=cell.make_kpts([3]*3))
         self.assertAlmostEqual(lib.fp(ao_value), (0.38051517609460028+0.062526488684770759j), 9)
 
+    def test_empty_cell(self):
+        cell = pgto.M(a=np.eye(3)*4)
+        Ls = pbctools.get_lattice_Ls(cell)
+        self.assertEqual(abs(Ls-np.zeros([1,3])).max(), 0)
+
 if __name__ == '__main__':
     print("Full Tests for pbc.gto.cell")
     unittest.main()