Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix TypeError when attr force_field not exists #3495

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pymatgen/io/lammps/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -1277,7 +1277,7 @@ def __init__(
self.force_field = {}
for kw in ff_kws:
self.force_field[kw] = pd.concat(
[mol.force_field[kw].copy() for mol in self.mols if kw in mol.force_field],
[mol.force_field[kw].copy() for mol in self.mols if kw in (mol.force_field or [])],
ignore_index=True,
)
self.force_field[kw].index += 1
Expand Down
19 changes: 16 additions & 3 deletions tests/io/lammps/test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,9 @@ def setUpClass(cls):
cls.ec_li = CombinedData.from_lammpsdata([cls.ec, cls.li], ["EC", "Li"], [1, 1], cls.small_coord_2)
cls.li_2 = CombinedData.from_lammpsdata([cls.li], ["Li"], [2], cls.small_coord_3)
cls.li_2_minimal = CombinedData.from_lammpsdata([cls.li_minimal], ["Li_minimal"], [2], cls.small_coord_3)
cls.ec_li_minimal = CombinedData.from_lammpsdata(
[cls.ec, cls.li_minimal], ["EC", "Li"], [1, 1], cls.small_coord_2
)

def test_from_files(self):
# general tests
Expand Down Expand Up @@ -957,7 +960,7 @@ def test_from_lammpsdata(self):
assert topo["Impropers"].loc[1, "atom3"] == 3
assert topo["Impropers"].loc[1, "atom4"] == 6

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

# tests for combining data with no topo info
# test combining data with no topo info
li_2 = self.li_2
assert li_2.topology is None, "Empty topo info should be none"

# tests for combining data with no topo and ff info
# test combining data with no topo info but FF info
li_2_minimal = self.li_2_minimal
assert li_2_minimal.force_field is None, "Empty ff info should be none"
assert li_2_minimal.topology is None, "Empty topo info should be none"

# test combining data with no FF info and existing FF info
ec_li_minimal = self.ec_li_minimal
for key in ("Bonds", "Angles", "Dihedrals", "Impropers"):
pd.testing.assert_frame_equal(ec_li_minimal.topology[key], ec_li_minimal.topology[key])
pd.testing.assert_frame_equal(
ec_li_minimal.force_field["Pair Coeffs"], ec_li.force_field["Pair Coeffs"].loc[1:5]
)
for key in ("Bond Coeffs", "Angle Coeffs", "Dihedral Coeffs", "Improper Coeffs"):
pd.testing.assert_frame_equal(ec_li_minimal.force_field[key], ec_li.force_field[key])

def test_get_str(self):
# general tests
ec_fec_lines = self.ec_fec1.get_str().splitlines()
Expand Down