Skip to content

Commit 9518085

Browse files
xjf729janosh
andauthored
fix TypeError when attr force_field not exists (#3495)
* fix TypeError when attr force_field not exists Fix bug "TypeError: argument of type 'NoneType' is not iterable". Not all LammpsData in CombinedData should have attribute force_field, replace this `NoneType` attribute with `[]` to skip unexisting force_field. Signed-off-by: XiongJF <[email protected]> * add test for CombinedData with different ff type * refactor new tests --------- Signed-off-by: XiongJF <[email protected]> Co-authored-by: Janosh Riebesell <[email protected]>
1 parent 2439dea commit 9518085

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

pymatgen/io/lammps/data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1277,7 +1277,7 @@ def __init__(
12771277
self.force_field = {}
12781278
for kw in ff_kws:
12791279
self.force_field[kw] = pd.concat(
1280-
[mol.force_field[kw].copy() for mol in self.mols if kw in mol.force_field],
1280+
[mol.force_field[kw].copy() for mol in self.mols if kw in (mol.force_field or [])],
12811281
ignore_index=True,
12821282
)
12831283
self.force_field[kw].index += 1

tests/io/lammps/test_data.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,9 @@ def setUpClass(cls):
823823
cls.ec_li = CombinedData.from_lammpsdata([cls.ec, cls.li], ["EC", "Li"], [1, 1], cls.small_coord_2)
824824
cls.li_2 = CombinedData.from_lammpsdata([cls.li], ["Li"], [2], cls.small_coord_3)
825825
cls.li_2_minimal = CombinedData.from_lammpsdata([cls.li_minimal], ["Li_minimal"], [2], cls.small_coord_3)
826+
cls.ec_li_minimal = CombinedData.from_lammpsdata(
827+
[cls.ec, cls.li_minimal], ["EC", "Li"], [1, 1], cls.small_coord_2
828+
)
826829

827830
def test_from_files(self):
828831
# general tests
@@ -957,7 +960,7 @@ def test_from_lammpsdata(self):
957960
assert topo["Impropers"].loc[1, "atom3"] == 3
958961
assert topo["Impropers"].loc[1, "atom4"] == 6
959962

960-
# tests for data objects with different number of ff kw
963+
# test data objects with different number of FF keywords
961964
li_ec = self.li_ec
962965
ec_li = self.ec_li
963966
assert li_ec.force_field["Pair Coeffs"].loc[6, "coeff2"] == 2.42
@@ -971,15 +974,25 @@ def test_from_lammpsdata(self):
971974
assert li_ec.force_field["Improper Coeffs"].loc[1, "coeff1"] == 10.5
972975
assert ec_li.force_field["Improper Coeffs"].loc[1, "coeff1"] == 10.5
973976

974-
# tests for combining data with no topo info
977+
# test combining data with no topo info
975978
li_2 = self.li_2
976979
assert li_2.topology is None, "Empty topo info should be none"
977980

978-
# tests for combining data with no topo and ff info
981+
# test combining data with no topo info but FF info
979982
li_2_minimal = self.li_2_minimal
980983
assert li_2_minimal.force_field is None, "Empty ff info should be none"
981984
assert li_2_minimal.topology is None, "Empty topo info should be none"
982985

986+
# test combining data with no FF info and existing FF info
987+
ec_li_minimal = self.ec_li_minimal
988+
for key in ("Bonds", "Angles", "Dihedrals", "Impropers"):
989+
pd.testing.assert_frame_equal(ec_li_minimal.topology[key], ec_li_minimal.topology[key])
990+
pd.testing.assert_frame_equal(
991+
ec_li_minimal.force_field["Pair Coeffs"], ec_li.force_field["Pair Coeffs"].loc[1:5]
992+
)
993+
for key in ("Bond Coeffs", "Angle Coeffs", "Dihedral Coeffs", "Improper Coeffs"):
994+
pd.testing.assert_frame_equal(ec_li_minimal.force_field[key], ec_li.force_field[key])
995+
983996
def test_get_str(self):
984997
# general tests
985998
ec_fec_lines = self.ec_fec1.get_str().splitlines()

0 commit comments

Comments
 (0)