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

Patches for element reading in lattice builder #1066

Merged
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
21 changes: 20 additions & 1 deletion mbuild/lattice.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
"""mBuild lattice module for working with crystalline systems."""
import itertools as it
import pathlib
import warnings
from collections import defaultdict

import numpy as np
from ele.element import element_from_name, element_from_symbol
from ele.exceptions import ElementError

import mbuild as mb
from mbuild.utils.io import import_
Expand Down Expand Up @@ -631,9 +634,19 @@ def populate(self, compound_dict=None, x=1, y=1, z=1):
ret_lattice = mb.Compound()

# Create (clone) a mb.Compound for the newly generate positions
elementsSet = set()
if compound_dict is None:
for key_id, all_pos in cell.items():
particle = mb.Compound(name=key_id, pos=[0, 0, 0])
for idElement in [element_from_symbol, element_from_name]:
try: # populate element info if it's there
element = idElement(key_id)
elementsSet.add(element)
break
except ElementError:
element = None
particle = mb.Compound(
name=key_id, pos=[0, 0, 0], element=element
)
for pos in all_pos:
particle_to_add = mb.clone(particle)
particle_to_add.translate_to(list(pos))
Expand All @@ -654,6 +667,12 @@ def populate(self, compound_dict=None, x=1, y=1, z=1):
key_id, err_type
)
)
# Raise warnings about assumed elements
for element in elementsSet:
warnings.warn(
f"Element assumed from cif file to be {element}.", UserWarning
)

# Create mbuild.box
ret_lattice.box = mb.Box(
lengths=[a * x, b * y, c * z], angles=self.angles
Expand Down
11 changes: 11 additions & 0 deletions mbuild/tests/test_cif.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,14 @@ def test_cif_triclinic_box_properties(self):
assert np.all(np.isclose(manual_lengths, list(periodic_box.lengths)))
assert np.all(np.isclose(manual_angles, list(periodic_box.angles)))
assert len(periodic_boxed_molecule.children) == manual_num_atoms
assert None not in list(
map(lambda x: x.element, periodic_boxed_molecule.particles())
)

def test_cif_raise_warnings(self):
with pytest.warns(
UserWarning,
match=r"Element assumed from cif file to be Element: silicon, symbol: Si, atomic number: 14, mass: 28.085.",
):
lattice_cif = load_cif(file_or_path=get_fn("ETV_triclinic.cif"))
periodic_boxed_molecule = lattice_cif.populate(x=1, y=1, z=1)

Check notice

Code scanning / CodeQL

Unused local variable

Variable periodic_boxed_molecule is not used.
30 changes: 30 additions & 0 deletions mbuild/tests/test_lattice.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,33 @@ def test_get_box_non_rectangular(self):
assert isinstance(mylat.box, mb.Box)
np.testing.assert_allclose([90, 90, 120], mylat.box.angles)
np.testing.assert_allclose(expected_lengths, mylat.box.lengths)

def test_populate_with_element_symbol(self):
lattice = mb.Lattice(
lattice_spacing=[0.5, 0.5, 1],
angles=[90, 90, 120],
lattice_points={"O": [[0, 0, 0]]},
)
cpd_lat = lattice.populate(x=1, y=1, z=1)
for part in cpd_lat:
assert part.element.name == "oxygen"

def test_populate_with_element_name(self):
lattice = mb.Lattice(
lattice_spacing=[0.5, 0.5, 1],
angles=[90, 90, 120],
lattice_points={"Oxygen": [[0, 0, 0]]},
)
cpd_lat = lattice.populate(x=1, y=1, z=1)
for part in cpd_lat:
assert part.element.name == "oxygen"

def test_populate_no_element(self):
lattice = mb.Lattice(
lattice_spacing=[0.5, 0.5, 1],
angles=[90, 90, 120],
lattice_points={"A": [[0, 0, 0]]},
)
cpd_lat = lattice.populate(x=1, y=1, z=1)
for part in cpd_lat:
assert part.element is None