Skip to content

Commit

Permalink
add scale_atoms, and reset transformatioin matrix
Browse files Browse the repository at this point in the history
  • Loading branch information
superstar54 committed Oct 18, 2023
1 parent 4f95704 commit a1c83f7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
36 changes: 29 additions & 7 deletions aiidalab_widgets_base/structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -929,10 +929,17 @@ def __init__(self, title="Cell transform"):
for i in range(3)
]
)
apply_cell_parameters = ipw.Button(description="Apply cell parameters")
apply_cell_parameters.on_click(self.apply_cell_parameters)
apply_cell_parameters_button = ipw.Button(description="Apply cell parameters")
apply_cell_parameters_button.on_click(self.apply_cell_parameters)
self.scale_atoms_position = ipw.Checkbox(description="Scale atoms position",
value=False,
indent=False,
)
apply_cell_transformation = ipw.Button(description="Apply transformation")
apply_cell_transformation.on_click(self.apply_cell_transformation)
reset_transformatioin_button = ipw.Button(description="Reset matrix",
)
reset_transformatioin_button.on_click(self.reset_cell_transformation_matrix)
super().__init__(
children=[
ipw.HBox(
Expand All @@ -949,7 +956,9 @@ def __init__(self, title="Cell transform"):
layout={"margin": "20px 0px 10px 0px"},
),
self.cell_parameters,
apply_cell_parameters,
ipw.HBox([apply_cell_parameters_button,
self.scale_atoms_position,
])
],
layout={"margin": "0px 0px 0px 20px"},
),
Expand All @@ -960,7 +969,8 @@ def __init__(self, title="Cell transform"):
layout={"margin": "20px 0px 10px 0px"},
),
self.cell_transformation,
apply_cell_transformation,
ipw.HBox([apply_cell_transformation,
reset_transformatioin_button])
],
layout={"margin": "0px 0px 0px 20px"},
),
Expand Down Expand Up @@ -1002,33 +1012,37 @@ def _to_standard_cell(
pbc=[True, True, True],
)

@observe("structure")
@tl.observe("structure")
def _observe_structure(self, change):
"""Update cell after the structure has been modified."""
if change["new"] is not None:
cell_parameters = change["new"].get_cell_lengths_and_angles()
cell_parameters = change["new"].cell.cellpar()
for i in range(6):
self.cell_parameters.children[i].children[1].value = round(
cell_parameters[i], 4
)
else:
for i in range(6):
self.cell_parameters.children[i].children[1].value = 0
# reset transformation matrix
self.reset_cell_transformation_matrix()

@_register_structure
def apply_cell_parameters(self, _=None, atoms=None):
"""Apply the cell parameters to the structure."""
from ase.cell import Cell

# only update structure when atoms is not None.
cell_parameters = [
self.cell_parameters.children[i].children[1].value for i in range(6)
]
if atoms is not None:
atoms.cell = Cell.fromcellpar(cell_parameters)
atoms.set_cell(Cell.fromcellpar(cell_parameters), scale_atoms=self.scale_atoms_position.value)
self.structure = atoms

@_register_structure
def apply_cell_transformation(self, _=None, atoms=None):
"""Apply the transformation matrix to the structure."""
from ase.build import make_supercell

# only update structure when atoms is not None.
Expand Down Expand Up @@ -1056,6 +1070,14 @@ def apply_cell_transformation(self, _=None, atoms=None):
atoms.translate(-atoms.cell.array.dot(translate))
self.structure = atoms

@_register_structure
def reset_cell_transformation_matrix(self, _=None, atoms=None):
"""Reset the transformation matrix to identity matrix."""
for i in range(3):
for j in range(4):
self.cell_transformation.children[i].children[j].value = 0
self.cell_transformation.children[i].children[i].value = 1


class BasicStructureEditor(ipw.VBox):
"""
Expand Down
13 changes: 13 additions & 0 deletions tests/test_structures.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,19 @@ def test_basic_cell_editor_widget(structure_data_object):
# Convert to primitive cell.
widget._to_primitive_cell()
assert widget.structure.get_chemical_formula() == "Si2"

# Change the cell parameters.
widget.cell_parameters.children[2].children[1].value = 10.0
widget.apply_cell_parameters()
assert widget.structure.cell.cellpar()[2] == 10.0

# make supercell using cell transformation
widget.cell_transformation.children[0].children[0].value = 2
widget.apply_cell_transformation()
assert widget.structure.get_chemical_formula() == "Si4"
# reset the cell transformation matrix
widget.reset_cell_transformation_matrix()
assert widget.cell_transformation.children[0].children[0].value == 1


@pytest.mark.usefixtures("aiida_profile_clean")
Expand Down

0 comments on commit a1c83f7

Please sign in to comment.