-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8f158c4
commit 4991e68
Showing
3 changed files
with
155 additions
and
53 deletions.
There are no files selected for viewing
77 changes: 71 additions & 6 deletions
77
src/aiidalab_qe/app/result/components/viewer/structure/model.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,92 @@ | ||
from __future__ import annotations | ||
|
||
import traitlets as tl | ||
|
||
from aiida import orm | ||
from aiidalab_qe.common.panel import ResultsModel | ||
from aiidalab_qe.common.time import format_time, relative_time | ||
|
||
|
||
class StructureResultsModel(ResultsModel): | ||
title = "Structure" | ||
identifier = "structure" | ||
|
||
structure = tl.Instance(orm.StructureData, allow_none=True) | ||
selected_view = tl.Unicode("initial") | ||
header = tl.Unicode() | ||
source = tl.Instance(orm.utils.managers.NodeLinksManager, allow_none=True) | ||
info = tl.Unicode() | ||
table_data = tl.List(tl.List()) | ||
|
||
_this_process_label = "PwRelaxWorkChain" | ||
|
||
source = None | ||
header_template = "<h1 style='margin: 0;'>{title}</h1>" | ||
|
||
@property | ||
def include(self): | ||
return True | ||
|
||
@property | ||
def is_relaxed(self): | ||
return "relax" in self.properties | ||
|
||
def update(self): | ||
is_relaxed = "relax" in self.properties | ||
self.title = "Relaxed structure" if is_relaxed else "Initial structure" | ||
self.source = self.outputs if is_relaxed else self.inputs | ||
self.auto_render = not is_relaxed or self.has_results | ||
with self.hold_trait_notifications(): | ||
if not self.is_relaxed or self.selected_view == "initial": | ||
self.header = self.header_template.format(title="Initial") | ||
self.source = self.inputs | ||
else: | ||
self.header = self.header_template.format(title="Relaxed") | ||
self.source = self.outputs | ||
self.structure = self._get_structure() | ||
if self.structure: | ||
self.info = self._get_structure_info() | ||
self.table_data = self._get_atom_table_data() | ||
|
||
def get_structure(self): | ||
def toggle_selected_view(self): | ||
self.selected_view = "relaxed" if self.selected_view == "initial" else "initial" | ||
|
||
def _get_structure(self) -> orm.StructureData | None: | ||
try: | ||
return self.source.structure if self.source else None | ||
except AttributeError: | ||
# If source is outputs but job failed, there may not be a structure | ||
return None | ||
|
||
def _get_structure_info(self): | ||
structure = self.structure | ||
formatted = format_time(structure.ctime) | ||
relative = relative_time(structure.ctime) | ||
return f""" | ||
<div style='line-height: 1.4;'> | ||
<strong>PK:</strong> {structure.pk}<br> | ||
<strong>Label:</strong> {structure.label}<br> | ||
<strong>Description:</strong> {structure.description}<br> | ||
<strong>Number of atoms:</strong> {len(structure.sites)}<br> | ||
<strong>Creation time:</strong> {formatted} ({relative})<br> | ||
</div> | ||
""" | ||
|
||
def _get_atom_table_data(self): | ||
structure = self.structure.get_ase() | ||
data = [ | ||
[ | ||
"Atom index", | ||
"Chemical symbol", | ||
"Tag", | ||
"x (Å)", | ||
"y (Å)", | ||
"z (Å)", | ||
] | ||
] | ||
positions = structure.positions | ||
chemical_symbols = structure.get_chemical_symbols() | ||
tags = structure.get_tags() | ||
|
||
for index, (symbol, tag, position) in enumerate( | ||
zip(chemical_symbols, tags, positions), start=1 | ||
): | ||
formatted_position = [f"{coord:.2f}" for coord in position] | ||
data.append([index, symbol, tag, *formatted_position]) | ||
|
||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters