diff --git a/docs/source/bond.rst b/docs/source/bond.rst index 0793926..8c7e4c9 100644 --- a/docs/source/bond.rst +++ b/docs/source/bond.rst @@ -1,6 +1,12 @@ Bond =============== -Use can control the bond using `avr.bond.settings`. For example, we delete the bond between Ca and Ti, and Ti and Ca. +Use can control the bond using `avr.bond.settings`. + + +Delete bond pair +---------------- + +For example, we delete the bond between Ca and Ti, and Ti and Ca. .. code-block:: python @@ -11,6 +17,13 @@ Use can control the bond using `avr.bond.settings`. For example, we delete the b # change the maximum bond length between Ti and O viewer.avr.bond.settings['Ti-O']["max"] = 3.0 +Add bond pair +------------- + +.. code-block:: python + + # add the bond between Pt and Au + viewer.avr.bond.add_bond_pair('Pt', 'Au', max=3.2) Hydrogen bond ------------- diff --git a/src/weas_widget/base_class.py b/src/weas_widget/base_class.py index fc5afda..f49f690 100644 --- a/src/weas_widget/base_class.py +++ b/src/weas_widget/base_class.py @@ -116,7 +116,7 @@ def update(self, *args, **kwargs): def _mark_changed(self): """Set the changed flag to True, notify parent, and update widget if set.""" self._changed = True - if self._parent: + if self._parent is not None: self._parent._mark_changed() else: if not (self._widget and self._key): diff --git a/src/weas_widget/plugins/bond.py b/src/weas_widget/plugins/bond.py index 8b032ec..614c9d8 100644 --- a/src/weas_widget/plugins/bond.py +++ b/src/weas_widget/plugins/bond.py @@ -1,3 +1,4 @@ +from __future__ import annotations from ..base_class import WidgetWrapper, ChangeTrackingDict from weas_widget.data import default_bond_pairs @@ -58,3 +59,41 @@ def get_default_settings(self): "type": bond_line_type, } return settings + + def add_bond_pair( + self, + species1: str, + species2: str | None = None, + min: float = 0.0, + max: float = None, + color1: list | None = None, + color2: list | None = None, + type: int = 0, + ) -> None: + """Add a bond between two species.""" + + species2 = species1 if species2 is None else species2 + if max is None: + max = ( + self._widget.speciesSettings[species1]["radius"] + + self._widget.speciesSettings[species2]["radius"] + ) * 1.1 + color1 = ( + self._widget.speciesSettings[species1]["color"] + if color1 is None + else color1 + ) + color2 = ( + self._widget.speciesSettings[species2]["color"] + if color2 is None + else color2 + ) + self.settings[f"{species1}-{species2}"] = { + "specie1": species1, + "specie2": species2, + "color1": color1, + "color2": color2, + "min": min, + "max": max, + "type": type, + } diff --git a/tests/test_bond.py b/tests/test_bond.py new file mode 100644 index 0000000..2bc720e --- /dev/null +++ b/tests/test_bond.py @@ -0,0 +1,20 @@ +def test_add_bond(): + from weas_widget import WeasWidget + from ase.build import bulk + + atoms = bulk("Al", cubic=True) + atoms[0].symbol = "Cu" + viewer = WeasWidget(from_ase=atoms) + viewer.avr.show_bonded_atoms = True + viewer.avr.model_style = 1 + viewer.avr.boundary = [[-0.1, 1.1], [-0.1, 1.1], [-0.1, 1.1]] + # add bonds + viewer.avr.bond.settings["Al-Cu"] = { + "specie1": "Al", + "specie2": "Cu", + "color1": viewer.avr.species.settings["Al"]["color"], + "color2": viewer.avr.species.settings["Cu"]["color"], + "min": 0, + "max": 3, + } + len(viewer.avr.bond.settings) == 1