Skip to content
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
23 changes: 15 additions & 8 deletions aiidalab_widgets_base/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,14 @@ class StructureUploadWidget(ipw.VBox):
)

def __init__(
self, title="", description="Upload Structure", allow_trajectories=False
self,
title="",
description="Upload Structure",
allow_trajectories=False,
add_auxiliary_cell=True,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about the parameter name, suggestions welcome, especially shorter ones :-)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, the name is good. It does the job.

):
self.title = title
self.add_auxiliary_cell = add_auxiliary_cell
self.file_upload = ipw.FileUpload(
description=description, multiple=False, layout={"width": "initial"}
)
Expand All @@ -403,11 +408,11 @@ def _validate_and_fix_ase_cell(self, ase_structure, vacuum_ang=10.0):
if not ase_structure:
return None

if not self.add_auxiliary_cell:
return ase_structure

cell = ase_structure.cell

# TODO: Since AiiDA 2.0, zero cell is possible if PBC=false
# so we should honor that here and do not put artificial cell
# around gas phase molecules.
if (
np.linalg.norm(cell[0]) < 0.1
or np.linalg.norm(cell[1]) < 0.1
Expand Down Expand Up @@ -758,8 +763,9 @@ class SmilesWidget(ipw.VBox):

SPINNER = """<i class="fa fa-spinner fa-pulse" style="color:red;" ></i>"""

def __init__(self, title=""):
def __init__(self, title="", add_auxiliary_cell=True):
self.title = title
self.add_auxiliary_cell = add_auxiliary_cell
try:
from rdkit import Chem # noqa: F401
from rdkit.Chem import AllChem # noqa: F401
Expand Down Expand Up @@ -790,12 +796,10 @@ def __init__(self, title=""):
def _make_ase(self, species, positions, smiles):
"""Create ase Atoms object."""
atoms = ase.Atoms(species, positions=positions, pbc=False)
atoms.cell = np.ptp(atoms.positions, axis=0) + 10
atoms.center()
# We're attaching this info so that it
# can be later stored as an extra on AiiDA Structure node.
atoms.info["smiles"] = smiles

return atoms

def _rdkit_opt(self, smiles, steps):
Expand Down Expand Up @@ -831,7 +835,10 @@ def _rdkit_opt(self, smiles, steps):
positions = mol.GetConformer().GetPositions()
natoms = mol.GetNumAtoms()
species = [mol.GetAtomWithIdx(j).GetSymbol() for j in range(natoms)]
return self._make_ase(species, positions, smiles)
ase_mol = self._make_ase(species, positions, smiles)
if self.add_auxiliary_cell:
ase_mol.cell = np.ptp(ase_mol.positions, axis=0) + 10
return ase_mol

def _mol_from_smiles(self, smiles, steps=1000):
"""Convert SMILES to ASE structure using RDKit"""
Expand Down
38 changes: 36 additions & 2 deletions tests/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,10 +199,11 @@ def raise_not_existent(pk):
)


@pytest.mark.parametrize("add_auxiliary_cell", (False, True))
@pytest.mark.usefixtures("aiida_profile_clean")
def test_structure_upload_widget(file_upload_change):
def test_structure_upload_widget(add_auxiliary_cell, file_upload_change):
"""Test the `StructureUploadWidget`."""
widget = awb.StructureUploadWidget()
widget = awb.StructureUploadWidget(add_auxiliary_cell=add_auxiliary_cell)
assert widget.structure is None

# Simulate the structure upload.
Expand All @@ -220,6 +221,16 @@ def test_structure_upload_widget(file_upload_change):
assert widget.structure.get_chemical_formula() == "Si2"
assert np.all(widget.structure[0].position == [0, 0, 0])

if add_auxiliary_cell:
cell = widget.structure.cell
assert all(np.greater(np.diag(cell), [10, 10, 10]))
assert np.array_equal(cell.angles(), [90.0, 90.0, 90.0])
else:
# There should be no cell, which ASE represents with a zero cell
assert not any(widget.structure.pbc)
assert not np.any(widget.structure.cell)
assert not widget.structure.cell.volume


@pytest.mark.parametrize(
("fname", "fcontent", "errmsg"),
Expand Down Expand Up @@ -275,6 +286,12 @@ def test_smiles_widget():
widget._on_button_pressed()
assert isinstance(widget.structure, ase.Atoms)
assert widget.structure.get_chemical_formula() == "CH4"
# By default, a rectangular cell is added (10 angstroms in each direction)
# but PBC is False
assert not any(widget.structure.pbc)
cell = widget.structure.cell
assert all(np.greater(np.diag(cell), [10, 10, 10]))
assert np.array_equal(cell.angles(), [90.0, 90.0, 90.0])

# Regression test that we can generate 1-atom and 2-atom molecules
widget.smiles.value = "[O]"
Expand All @@ -293,6 +310,23 @@ def test_smiles_widget():
assert widget.structure is None


@pytest.mark.usefixtures("aiida_profile_clean")
def test_smiles_widget_without_cell():
"""Test the `SmilesWidget`."""
widget = awb.SmilesWidget(add_auxiliary_cell=False)
assert widget.structure is None

# Simulate the structure generation.
widget.smiles.value = "C"
widget._on_button_pressed()
assert isinstance(widget.structure, ase.Atoms)
assert widget.structure.get_chemical_formula() == "CH4"
# There should be no cell, which ASE represents with a zero cell
assert not any(widget.structure.pbc)
assert not np.any(widget.structure.cell)
assert not widget.structure.cell.volume


@pytest.mark.usefixtures("aiida_profile_clean")
def test_smiles_canonicalization():
"""Test the SMILES canonicalization via RdKit."""
Expand Down