diff --git a/aiidalab_widgets_base/viewers.py b/aiidalab_widgets_base/viewers.py index df7d46687..75bee6b5b 100644 --- a/aiidalab_widgets_base/viewers.py +++ b/aiidalab_widgets_base/viewers.py @@ -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): diff --git a/miscellaneous/structures/Si.xyz b/miscellaneous/structures/Si.xyz new file mode 100644 index 000000000..031661728 --- /dev/null +++ b/miscellaneous/structures/Si.xyz @@ -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 diff --git a/notebooks/structures.ipynb b/notebooks/structures.ipynb index 1b172b0c1..f5974aee6 100644 --- a/notebooks/structures.ipynb +++ b/notebooks/structures.ipynb @@ -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",