@@ -352,26 +352,40 @@ def test_get_points_in_sphere(self):
352
352
# This is a non-niggli representation of a cubic lattice
353
353
lattice = Lattice ([[1 , 5 , 0 ], [0 , 1 , 0 ], [5 , 0 , 1 ]])
354
354
# evenly spaced points array between 0 and 1
355
- pts = np .array (list (itertools .product (range (5 ), repeat = 3 ))) / 5
356
- pts = lattice .get_fractional_coords (pts )
355
+ points = np .array (list (itertools .product (range (5 ), repeat = 3 ))) / 5
356
+ points = lattice .get_fractional_coords (points )
357
357
358
358
# Test getting neighbors within 1 neighbor distance of the origin
359
- fcoords , dists , inds , images = lattice .get_points_in_sphere (pts , [0 , 0 , 0 ], 0.20001 , zip_results = False )
360
- assert len (fcoords ) == 7 # There are 7 neighbors
359
+ frac_coords , dists , indices , images = lattice .get_points_in_sphere (
360
+ points , [0 , 0 , 0 ], 0.20001 , zip_results = False
361
+ )
362
+ assert len (frac_coords ) == 7 # There are 7 neighbors
361
363
assert np .isclose (dists , 0.2 ).sum () == 6 # 6 are at 0.2
362
364
assert np .isclose (dists , 0 ).sum () == 1 # 1 is at 0
363
- assert len (set (inds )) == 7 # They have unique indices
364
- assert_array_equal ( images [np .isclose (dists , 0 )], [[0 , 0 , 0 ]])
365
+ assert len (set (indices )) == 7 # They have unique indices
366
+ assert images [np .isclose (dists , 0 )]. tolist () == [[0 , 0 , 0 ]]
365
367
366
368
# More complicated case, using the zip output
367
- result = lattice .get_points_in_sphere (pts , [0.5 , 0.5 , 0.5 ], 1.0001 )
369
+ result = lattice .get_points_in_sphere (points , [0.5 , 0.5 , 0.5 ], 1.0001 )
370
+ assert isinstance (result , tuple )
368
371
assert len (result ) == 552
369
- assert len (result [0 ]) == 4 # coords, dists, ind , supercell
372
+ assert len (result [0 ]) == 4 # coords, dists, indices , supercell
370
373
371
374
# test pbc
372
375
latt_pbc = Lattice ([[1 , 5 , 0 ], [0 , 1 , 0 ], [5 , 0 , 1 ]], pbc = (True , True , False ))
373
- fcoords , dists , inds , images = latt_pbc .get_points_in_sphere (pts , [0 , 0 , 0 ], 0.20001 , zip_results = False )
374
- assert len (fcoords ) == 6
376
+ frac_coords , dists , indices , images = latt_pbc .get_points_in_sphere (
377
+ points , [0 , 0 , 0 ], 0.20001 , zip_results = False
378
+ )
379
+ assert len (frac_coords ) == 6
380
+
381
+ # ensure consistent return type if zip_results=False and no points in sphere are found
382
+ # https://github.com/materialsproject/pymatgen/issues/3794
383
+ result = lattice .get_points_in_sphere (points , [0.5 , 0.5 , 0.5 ], 0.0001 , zip_results = False )
384
+ assert isinstance (result , tuple )
385
+ assert len (result ) == 4
386
+ assert all (len (arr ) == 0 for arr in result )
387
+ types = {* map (type , result )}
388
+ assert types == {np .ndarray }, f"Expected only np.ndarray, got { types } "
375
389
376
390
def test_get_all_distances (self ):
377
391
fcoords = np .array (
0 commit comments