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

Add volume information in the cell tab #653

Merged
merged 3 commits into from
Dec 23, 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
18 changes: 18 additions & 0 deletions aiidalab_widgets_base/viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import traitlets as tl
import vapory
from aiida import cmdline, orm, tools
from aiida.orm.nodes.data.structure import _get_dimensionality
from ase.data import colors
from IPython.display import clear_output, display
from matplotlib.colors import to_rgb
Expand Down Expand Up @@ -282,6 +283,11 @@ class _StructureDataBaseViewer(ipw.VBox):
DEFAULT_SELECTION_COLOR = "green"
REPRESENTATION_PREFIX = "_aiidalab_viewer_representation_"
DEFAULT_REPRESENTATION = "_aiidalab_viewer_representation_default"
_CELL_LABELS = {
1: ["length", "Å"],
2: ["area", "Ų"],
3: ["volume", "ų"],
}

def __init__(
self,
Expand Down Expand Up @@ -710,6 +716,15 @@ def _observe_cell(self, _=None):
self.periodicity.value = (
f"Periodicity: {periodicity_map[tuple(self.structure.pbc)]}"
)
# Calculate the volume of the cell using the function from orm.StructureData
dimension_data = _get_dimensionality(self.structure.pbc, self.cell)
# Determine the label and unit based on dimensionality
cell_label = self._CELL_LABELS.get(dimension_data["dim"])
if cell_label:
self.cell_volume.value = f"Cell {cell_label[0]}: {dimension_data['value']:.4f} ({cell_label[1]})"
else:
self.cell_volume.value = "Cell volume: -"

else:
self.cell_a.value = "<i><b>a</b></i>:"
self.cell_b.value = "<i><b>b</b></i>:"
Expand Down Expand Up @@ -744,6 +759,8 @@ def _cell_tab(self):
self.cell_hall = ipw.HTML()
self.periodicity = ipw.HTML()

self.cell_volume = ipw.HTML()

self._observe_cell()

return ipw.VBox(
Expand Down Expand Up @@ -791,6 +808,7 @@ def _cell_tab(self):
),
]
),
self.cell_volume,
]
)

Expand Down
1 change: 1 addition & 0 deletions tests/test_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def test_pbc_structure_data_viewer(structure_data_object):
viewer = viewers.StructureDataViewer()
viewer.structure = ase_input
assert viewer.periodicity.value == "Periodicity: xy"
assert viewer.cell_volume.value == "Cell area: 12.2500 (Ų)"


@pytest.mark.usefixtures("aiida_profile_clean")
Expand Down
Loading