Skip to content

Commit

Permalink
Easy way to add bond pair (#88)
Browse files Browse the repository at this point in the history
Add `add_bond_pair` method
  • Loading branch information
superstar54 authored Jan 10, 2025
1 parent 72b3561 commit 26faaf6
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
15 changes: 14 additions & 1 deletion docs/source/bond.rst
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
-------------
Expand Down
2 changes: 1 addition & 1 deletion src/weas_widget/base_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
39 changes: 39 additions & 0 deletions src/weas_widget/plugins/bond.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from __future__ import annotations
from ..base_class import WidgetWrapper, ChangeTrackingDict
from weas_widget.data import default_bond_pairs

Expand Down Expand Up @@ -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,
}
20 changes: 20 additions & 0 deletions tests/test_bond.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 26faaf6

Please sign in to comment.