Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support hydrogen bond #75

Merged
merged 3 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docs/source/_static/images/example-highlight.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 19 additions & 3 deletions docs/source/bond.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,24 @@ Use can control the bond using `avr.bond.settings`. For example, we delete the b
.. code-block:: python

# delete the bond between Ca and Ti
del viewer1.avr.bond.settings['[Ti, Ca]']
del viewer.avr.bond.settings['Ti-Ca']
# change the bond color between Ti and O
viewer1.avr.bond.settings['[Ti, O]'].update({"color": "red"})
viewer.avr.bond.settings['Ti-O'].update({"color": "red"})
# change the maximum bond length between Ti and O
viewer1.avr.bond.settings['[Ti, O]']["max"] = 3.0
viewer.avr.bond.settings['Ti-O']["max"] = 3.0


Hydrogen bond
-------------
The hydrogen bond can be controlled using `avr.bond.show_hydrogen_bonds`:

.. code-block:: python

# set hydrogen bond length parameters for H-O
viewer.avr.bond.settings['H-O']["min"] = 1.5
viewer.avr.bond.settings['H-O']["max"] = 2.5
# show hydrogen bonds
viewer.avr.bond.show_hydrogen_bonds = True

.. image:: _static/images/example-bond-hydrogen-bond.png
:width: 6cm
31 changes: 31 additions & 0 deletions docs/source/highlight.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Highlight atoms
===============

One can highlight the atoms using the following shapes:

- sphere
- box
- cross

One can see the highlight setting:

.. code-block:: python

viewer.avr.highlight.settings

The default settings has `selection` and `fixed` items, which are used to highlight the selected atoms and fixed atoms, respectively.

Add highlight item
-------------------
One can add one highlight item with the following code:

.. code-block:: python

# highlight the first two atoms with a cross, e.g., show the atoms which are fixed
viewer.avr.highlight.settings['my_highlight'] = {"shape": "cross", "indices": [0, 1], color: "black"}
viewer.avr.draw()
viewer


.. image:: ../_static/images/example-highlight.png
:width: 4cm
1 change: 1 addition & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ A widget to visualize and interact with atomic structures in Jupyter Notebook. I
isosurface
vector_field
lattice_plane
highlight
mesh_primitive
search_operator
selection
Expand Down
9 changes: 8 additions & 1 deletion js/widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,12 @@ function render({ model, el }) {
_showCell: model.get("showCell"),
_showBondedAtoms: model.get("showBondedAtoms"),
_hideLongBonds: model.get("hideLongBonds"),
_showHydrogenBonds: model.get("showHydrogenBonds"),
_boundary: model.get("boundary"),
_currentFrame: model.get("currentFrame"),
};
editor = new weas.WEAS({ domElement, atoms, viewerConfig, guiConfig });
// window.editor = editor; // for debugging
window.editor = editor; // for debugging
editor.avr.selectedAtomsIndices = model.get("selectedAtomsIndices");
// editor.avr.atomScales = model.get("atomScales");
// editor.avr.modelSticks = model.get("modelSticks");
Expand All @@ -61,6 +62,9 @@ function render({ model, el }) {
// bond settings
// console.log("bondSettings: ", model.get("bondSettings"));
editor.avr.bondManager.fromSettings(model.get("bondSettings"));
// highlight settings
// console.log("highlightSettings: ", model.get("highlightSettings"));
editor.avr.highlightManager.fromSettings(model.get("highlightSettings"));
// volumetric data
editor.avr.isosurfaceManager.volumetricData = createVolumeData(model.get("volumetricData"), atoms.cell);
console.log("isosettings: ", model.get("isoSettings"));
Expand Down Expand Up @@ -221,14 +225,17 @@ function render({ model, el }) {
model.on("change:cameraZoom", () => {
const cameraZoom = model.get("cameraZoom");
editor.tjs.camera.updateZoom(cameraZoom);
editor.tjs.render();
});
model.on("change:cameraPosition", () => {
const cameraPosition = model.get("cameraPosition");
editor.tjs.camera.updatePosition(cameraPosition[0], cameraPosition[1], cameraPosition[2]);
editor.tjs.render();
});
model.on("change:cameraLookAt", () => {
const cameraLookAt = model.get("cameraLookAt");
editor.tjs.controls.target.set(cameraLookAt[0], cameraLookAt[1], cameraLookAt[2]);
editor.tjs.controls.update();
editor.tjs.render();
});
}
Expand Down
30 changes: 15 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dependencies": {
"dat.gui": "^0.7.9",
"three": "^0.161.0",
"weas": ">=0.1.25"
"weas": ">=0.1.26"
},
"devDependencies": {
"esbuild": "^0.20.0"
Expand Down
16 changes: 15 additions & 1 deletion src/weas_widget/atoms_viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from .plugins.lattice_plane import LatticePlane
from .plugins.bond import BondManager
from .plugins.species import SpeciesManager
from .plugins.highlight import HighlightManager
from copy import deepcopy


Expand All @@ -20,6 +21,7 @@ class AtomsViewer(WidgetWrapper):
"show_cell": "showCell",
"show_bonded_atoms": "showBondedAtoms",
"hide_long_bonds": "hideLongBonds",
"show_hydrogen_bonds": "showHydrogenBonds",
"atom_label_type": "atomLabelType",
"material_type": "materialType",
"model_sticks": "modelSticks",
Expand All @@ -29,7 +31,16 @@ class AtomsViewer(WidgetWrapper):
"phonon_setting": "phonon",
}

_extra_allowed_attrs = ["species", "vf", "iso", "lp", "atoms", "bond", "color_type"]
_extra_allowed_attrs = [
"species",
"vf",
"iso",
"lp",
"atoms",
"bond",
"highlight",
"color_type",
]

def __init__(self, _widget):

Expand All @@ -40,6 +51,7 @@ def __init__(self, _widget):
setattr(self, "lp", LatticePlane(_widget))
setattr(self, "bond", BondManager(_widget))
setattr(self, "species", SpeciesManager(_widget))
setattr(self, "highlight", HighlightManager(_widget))

@property
def atoms(self):
Expand All @@ -61,6 +73,8 @@ def atoms(self, atoms):
self.bond.update_atoms()
# vector field
self.vf.update_atoms()
# highlight
self.highlight.update_atoms()

def draw(self):
"""Redraw the widget."""
Expand Down
3 changes: 3 additions & 0 deletions src/weas_widget/base_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class BaseWidget(anywidget.AnyWidget):
atomLabelType = tl.Unicode("None").tag(sync=True)
showCell = tl.Bool(True).tag(sync=True)
showBondedAtoms = tl.Bool(False).tag(sync=True)
showHydrogenBonds = tl.Bool(False).tag(sync=True)
hideLongBonds = tl.Bool(True).tag(sync=True)
atomScales = tl.List([]).tag(sync=True)
modelSticks = tl.List([]).tag(sync=True)
Expand Down Expand Up @@ -66,6 +67,8 @@ class BaseWidget(anywidget.AnyWidget):
bondSettings = tl.Dict({}).tag(sync=True)
# phonon
phonon = tl.Dict({}).tag(sync=True)
# highlight
highlightSettings = tl.Dict({}).tag(sync=True)

def __init__(self, **kwargs):
super().__init__(**kwargs)
Expand Down
25 changes: 20 additions & 5 deletions src/weas_widget/plugins/bond.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,27 @@ def get_default_settings(self):
for species2, data2 in species_dict.items():
if (data1["element"], data2["element"]) not in default_bond_pairs:
continue
settings[f"[{species1}, {species2}]"] = {
bond_type = default_bond_pairs[(data1["element"], data2["element"])][2]
min = 0
max = (data1["radius"] + data2["radius"]) * 1.1
color1 = data1["color"]
color2 = data2["color"]
if bond_type == 1:
min = max + 0.4
max = min + 1
color1 = "#808080"
color2 = "#808080"
bond_line_type = 1
else:
bond_line_type = 0

settings[f"{species1}-{species2}"] = {
"species1": species1,
"species2": species2,
"color1": data1["color"],
"color2": data2["color"],
"min": 0,
"max": (data1["radius"] + data2["radius"]) * 1.1,
"color1": color1,
"color2": color2,
"min": min,
"max": max,
"type": bond_line_type,
}
return settings
41 changes: 41 additions & 0 deletions src/weas_widget/plugins/highlight.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from ..base_class import WidgetWrapper, ChangeTrackingDict


class HighlightManager(WidgetWrapper):

catalog = "highlight"

_attribute_map = {}

_extra_allowed_attrs = ["_settings", "settings"]

def __init__(self, _widget):
super().__init__(_widget)
self._settings = ChangeTrackingDict(
widget=self._widget, key="highlightSettings"
)

@property
def settings(self):
return self._settings

@settings.setter
def settings(self, value):
self._settings = ChangeTrackingDict(
value, widget=self._widget, key="highlightSettings"
)

def update_atoms(self):
self.settings = self.get_default_settings()

def get_default_settings(self):
settings = {
"selection": {
"indices": [],
"scale": 1.1,
"color": "yellow",
"type": "sphere",
},
"fixed": {"indices": [], "scale": 1.1, "color": "black", "type": "cross"},
}
return settings
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading