Skip to content

Commit

Permalink
mimic the behavior of nglview and transforms the "displayed_structure…
Browse files Browse the repository at this point in the history
…" into the standard form (#576)

This PR transforms the "displayed_structure" into the standard form, where the a-axis aligns along the x-axis, prior to rendering in nglview. This ensures that nglview's internal handling does not further modify the structure unexpectedly. This ensures the atom positions and custom bonds calculated are matched.
  • Loading branch information
superstar54 authored Apr 23, 2024
1 parent 01be224 commit 70259cc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
13 changes: 12 additions & 1 deletion aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,18 @@ def _observe_supercell(self, _=None):
self.set_trait(
"displayed_structure", None
) # To make sure the structure is always updated.
self.set_trait("displayed_structure", self.structure.repeat(self.supercell))
# nglview displays structures by first saving them to a temporary "pdb" file, which necessitates
# converting the unit cell and atomic positions into a standard form where the a-axis aligns along the x-axis.
# This transformation can cause discrepancies between the atom positions and custom bonds calculated from the original structure.
# To mitigate this, we transform the "displayed_structure" into the standard form prior to rendering in nglview.
# This ensures that nglview's internal handling does not further modify the structure unexpectedly.
standard_structure = self.structure.copy()
standard_structure.set_cell(
self.structure.cell.standard_form()[0], scale_atoms=True
)
self.set_trait(
"displayed_structure", standard_structure.repeat(self.supercell)
)

@tl.validate("structure")
def _valid_structure(self, change):
Expand Down
4 changes: 4 additions & 0 deletions miscellaneous/structures/Si.xyz
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
2
Lattice="3.81196 0.0 0.0 1.9059800000000005 3.3012541982101284 0.0 1.9059800000000005 1.100418066070043 3.112452306633254" Properties=species:S:1:pos:R:3 spacegroup="P 1" unit_cell=conventional pbc="T T T"
Si 0.00000000 0.00000000 0.00000000
Si 1.90598000 1.10041807 0.77811308
3 changes: 2 additions & 1 deletion notebooks/structures.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
" awb.StructureExamplesWidget(\n",
" title=\"From Examples\",\n",
" examples=[\n",
" (\"Silicon oxide\", \"../miscellaneous/structures/SiO2.xyz\")\n",
" (\"Silicon oxide\", \"../miscellaneous/structures/SiO2.xyz\"),\n",
" (\"Silicon\", \"../miscellaneous/structures/Si.xyz\")\n",
" ]),\n",
" ],\n",
" editors = [\n",
Expand Down

0 comments on commit 70259cc

Please sign in to comment.