Skip to content

Commit

Permalink
Add layer of abstraction in Panel classes
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Nov 27, 2024
1 parent 2fd8c91 commit 555701f
Show file tree
Hide file tree
Showing 66 changed files with 355 additions and 290 deletions.
23 changes: 12 additions & 11 deletions src/aiidalab_qe/app/configuration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@

from aiidalab_qe.app.parameters import DEFAULT_PARAMETERS
from aiidalab_qe.app.utils import get_entry_items
from aiidalab_qe.common.panel import SettingsModel, SettingsPanel
from aiidalab_qe.common.panel import CalculationSettingsModel, CalculationSettingsPanel
from aiidalab_widgets_base import WizardAppWidgetStep

from .advanced import AdvancedModel, AdvancedSettings
from .basic import WorkChainModel, WorkChainSettings
from .advanced import AdvancedCalculationSettingsModel, AdvancedCalculationSettingsPanel
from .basic import BasicCalculationSettingsModel, BasicCalculationSettingsPanel
from .model import ConfigurationStepModel

DEFAULT: dict = DEFAULT_PARAMETERS # type: ignore
Expand Down Expand Up @@ -56,12 +56,12 @@ def __init__(self, model: ConfigurationStepModel, **kwargs):
""",
)

workchain_model = WorkChainModel()
self.workchain_settings = WorkChainSettings(model=workchain_model)
workchain_model = BasicCalculationSettingsModel()
self.workchain_settings = BasicCalculationSettingsPanel(model=workchain_model)
self._model.add_model("workchain", workchain_model)

advanced_model = AdvancedModel()
self.advanced_settings = AdvancedSettings(model=advanced_model)
advanced_model = AdvancedCalculationSettingsModel()
self.advanced_settings = AdvancedCalculationSettingsPanel(model=advanced_model)
self._model.add_model("advanced", advanced_model)

self.settings = {
Expand All @@ -71,7 +71,7 @@ def __init__(self, model: ConfigurationStepModel, **kwargs):

self.property_children = []

self._fetch_plugin_settings()
self._fetch_plugin_calculation_settings()

def render(self):
if self.rendered:
Expand Down Expand Up @@ -178,7 +178,7 @@ def _on_previous_step_state_change(self, _):
def _on_tab_change(self, change):
if (tab_index := change["new"]) is None:
return
tab: SettingsPanel = self.tabs.children[tab_index] # type: ignore
tab: CalculationSettingsPanel = self.tabs.children[tab_index] # type: ignore
tab.render()
tab.update()

Expand Down Expand Up @@ -214,15 +214,16 @@ def _update_state(self, _=None):
else:
self.state = self.State.INIT

def _fetch_plugin_settings(self):
def _fetch_plugin_calculation_settings(self):
outlines = get_entry_items("aiidalab_qe.properties", "outline")
entries = get_entry_items("aiidalab_qe.properties", "setting")
for identifier, entry in entries.items():
for key in ("panel", "model"):
if key not in entry:
raise ValueError(f"Entry {identifier} is missing the '{key}' key")

panel = entry["panel"]
model: SettingsModel = entry["model"]()
model: CalculationSettingsModel = entry["model"]()
self._model.add_model(identifier, model)

outline = outlines[identifier]()
Expand Down
8 changes: 4 additions & 4 deletions src/aiidalab_qe/app/configuration/advanced/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .advanced import AdvancedSettings
from .model import AdvancedModel
from .advanced import AdvancedCalculationSettingsPanel
from .model import AdvancedCalculationSettingsModel

__all__ = [
"AdvancedModel",
"AdvancedSettings",
"AdvancedCalculationSettingsModel",
"AdvancedCalculationSettingsPanel",
]
39 changes: 23 additions & 16 deletions src/aiidalab_qe/app/configuration/advanced/advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@

import ipywidgets as ipw

from aiidalab_qe.common.panel import SettingsPanel
from aiidalab_qe.common.panel import CalculationSettingsPanel

from .hubbard import HubbardModel, HubbardSettings
from .magnetization import MagnetizationModel, MagnetizationSettings
from .model import AdvancedModel
from .pseudos import PseudoSettings, PseudosModel
from .smearing import SmearingModel, SmearingSettings
from .hubbard import HubbardCalculationSettingsModel, HubbardCalculationSettingsPanel
from .magnetization import (
MagnetizationCalculationSettingsModel,
MagnetizationCalculationSettingsPanel,
)
from .model import AdvancedCalculationSettingsModel
from .pseudos import PseudoCalculationSettingsPanel, PseudosCalculationSettingsModel
from .smearing import SmearingCalculationSettingsModel, SmearingCalculationSettingsPanel


class AdvancedSettings(SettingsPanel[AdvancedModel]):
class AdvancedCalculationSettingsPanel(
CalculationSettingsPanel[AdvancedCalculationSettingsModel],
):
title = "Advanced Settings"
identifier = "advanced"

def __init__(self, model: AdvancedModel, **kwargs):
def __init__(self, model: AdvancedCalculationSettingsModel, **kwargs):
super().__init__(
model=model,
layout={"justify_content": "space-between", **kwargs.get("layout", {})},
Expand All @@ -42,20 +47,22 @@ def __init__(self, model: AdvancedModel, **kwargs):
"kpoints_distance",
)

smearing_model = SmearingModel()
self.smearing = SmearingSettings(model=smearing_model)
smearing_model = SmearingCalculationSettingsModel()
self.smearing = SmearingCalculationSettingsPanel(model=smearing_model)
model.add_model("smearing", smearing_model)

magnetization_model = MagnetizationModel()
self.magnetization = MagnetizationSettings(model=magnetization_model)
magnetization_model = MagnetizationCalculationSettingsModel()
self.magnetization = MagnetizationCalculationSettingsPanel(
model=magnetization_model,
)
model.add_model("magnetization", magnetization_model)

hubbard_model = HubbardModel()
self.hubbard = HubbardSettings(model=hubbard_model)
hubbard_model = HubbardCalculationSettingsModel()
self.hubbard = HubbardCalculationSettingsPanel(model=hubbard_model)
model.add_model("hubbard", hubbard_model)

pseudos_model = PseudosModel()
self.pseudos = PseudoSettings(model=pseudos_model)
pseudos_model = PseudosCalculationSettingsModel()
self.pseudos = PseudoCalculationSettingsPanel(model=pseudos_model)
model.add_model("pseudos", pseudos_model)

def render(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .hubbard import HubbardSettings
from .model import HubbardModel
from .hubbard import HubbardCalculationSettingsPanel
from .model import HubbardCalculationSettingsModel

__all__ = [
"HubbardSettings",
"HubbardModel",
"HubbardCalculationSettingsModel",
"HubbardCalculationSettingsPanel",
]
12 changes: 7 additions & 5 deletions src/aiidalab_qe/app/configuration/advanced/hubbard/hubbard.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import ipywidgets as ipw

from ..subsettings import AdvancedSubSettings
from .model import HubbardModel
from ..subsettings import AdvancedCalculationSubSettings
from .model import HubbardCalculationSettingsModel


class HubbardSettings(AdvancedSubSettings[HubbardModel]):
class HubbardCalculationSettingsPanel(
AdvancedCalculationSubSettings[HubbardCalculationSettingsModel],
):
identifier = "hubbard"

def __init__(self, model: HubbardModel, **kwargs):
def __init__(self, model: HubbardCalculationSettingsModel, **kwargs):
super().__init__(model, **kwargs)

self._model.observe(
Expand Down Expand Up @@ -100,7 +102,7 @@ def _update(self, specific=""):
if self.updated:
return
self._show_loading()
if not self._model.loaded_from_process or specific and specific != "widgets":
if not self._model.loaded_from_process or (specific and specific != "widgets"):
self._model.update(specific)
self._build_hubbard_widget()
self._toggle_hubbard_widget()
Expand Down
7 changes: 5 additions & 2 deletions src/aiidalab_qe/app/configuration/advanced/hubbard/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
from aiida_quantumespresso.data.hubbard_structure import HubbardStructureData
from aiidalab_qe.common.mixins import HasInputStructure

from ..subsettings import AdvancedSubModel
from ..subsettings import AdvancedCalculationSubSettingsModel


class HubbardModel(AdvancedSubModel, HasInputStructure):
class HubbardCalculationSettingsModel(
AdvancedCalculationSubSettingsModel,
HasInputStructure,
):
dependencies = [
"input_structure",
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from .magnetization import MagnetizationSettings
from .model import MagnetizationModel
from .magnetization import MagnetizationCalculationSettingsPanel
from .model import MagnetizationCalculationSettingsModel

__all__ = [
"MagnetizationModel",
"MagnetizationSettings",
"MagnetizationCalculationSettingsModel",
"MagnetizationCalculationSettingsPanel",
]
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import ipywidgets as ipw

from ..subsettings import AdvancedSubSettings
from .model import MagnetizationModel
from ..subsettings import AdvancedCalculationSubSettings
from .model import MagnetizationCalculationSettingsModel


class MagnetizationSettings(AdvancedSubSettings[MagnetizationModel]):
class MagnetizationCalculationSettingsPanel(
AdvancedCalculationSubSettings[MagnetizationCalculationSettingsModel],
):
"""Widget to set the type of magnetization used in the calculation:
1) Tot_magnetization: Total majority spin charge - minority spin charge.
2) Starting magnetization: Starting spin polarization on atomic type 'i' in a spin polarized (LSDA or noncollinear/spin-orbit) calculation.
Expand All @@ -20,7 +22,7 @@ class MagnetizationSettings(AdvancedSubSettings[MagnetizationModel]):

identifier = "magnetization"

def __init__(self, model: MagnetizationModel, **kwargs):
def __init__(self, model: MagnetizationCalculationSettingsModel, **kwargs):
super().__init__(model, **kwargs)

self._model.observe(
Expand Down Expand Up @@ -111,7 +113,7 @@ def _update(self, specific=""):
if self.updated:
return
self._show_loading()
if not self._model.loaded_from_process or specific and specific != "widgets":
if not self._model.loaded_from_process or (specific and specific != "widgets"):
self._model.update(specific)
self._build_kinds_widget()
self._switch_widgets()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

from aiidalab_qe.common.mixins import HasInputStructure

from ..subsettings import AdvancedSubModel
from ..subsettings import AdvancedCalculationSubSettingsModel


class MagnetizationModel(AdvancedSubModel, HasInputStructure):
class MagnetizationCalculationSettingsModel(
AdvancedCalculationSubSettingsModel,
HasInputStructure,
):
dependencies = [
"input_structure",
"electronic_type",
Expand Down
44 changes: 25 additions & 19 deletions src/aiidalab_qe/app/configuration/advanced/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
from aiida_quantumespresso.workflows.pw.base import PwBaseWorkChain
from aiidalab_qe.app.parameters import DEFAULT_PARAMETERS
from aiidalab_qe.common.mixins import HasInputStructure, HasModels
from aiidalab_qe.common.panel import SettingsModel
from aiidalab_qe.common.panel import CalculationSettingsModel
from aiidalab_qe.setup.pseudos import PseudoFamily

from .subsettings import AdvancedSubModel
from .subsettings import AdvancedCalculationSubSettingsModel

if t.TYPE_CHECKING:
from .hubbard.hubbard import HubbardModel
from .magnetization import MagnetizationModel
from .pseudos.pseudos import PseudosModel
from .smearing import SmearingModel
from .hubbard.hubbard import HubbardCalculationSettingsModel
from .magnetization import MagnetizationCalculationSettingsModel
from .pseudos.pseudos import PseudosCalculationSettingsModel
from .smearing import SmearingCalculationSettingsModel

DEFAULT: dict = DEFAULT_PARAMETERS # type: ignore


class AdvancedModel(
SettingsModel,
HasModels[AdvancedSubModel],
class AdvancedCalculationSettingsModel(
CalculationSettingsModel,
HasModels[AdvancedCalculationSubSettingsModel],
HasInputStructure,
):
dependencies = [
Expand Down Expand Up @@ -115,15 +115,15 @@ def get_model_state(self):
"kpoints_distance": self.kpoints_distance,
}

hubbard: HubbardModel = self.get_model("hubbard") # type: ignore
hubbard: HubbardCalculationSettingsModel = self.get_model("hubbard") # type: ignore
if hubbard.is_active:
parameters["hubbard_parameters"] = {"hubbard_u": hubbard.parameters}
if hubbard.has_eigenvalues:
parameters["pw"]["parameters"]["SYSTEM"] |= {
"starting_ns_eigenvalue": hubbard.get_active_eigenvalues()
}

pseudos: PseudosModel = self.get_model("pseudos") # type: ignore
pseudos: PseudosCalculationSettingsModel = self.get_model("pseudos") # type: ignore
parameters["pseudo_family"] = pseudos.family
if pseudos.dictionary:
parameters["pw"]["pseudos"] = pseudos.dictionary
Expand All @@ -138,14 +138,16 @@ def get_model_state(self):
self.dftd3_version[self.van_der_waals]
)

smearing: SmearingModel = self.get_model("smearing") # type: ignore
smearing: SmearingCalculationSettingsModel = self.get_model("smearing") # type: ignore
if self.electronic_type == "metal":
# smearing type setting
parameters["pw"]["parameters"]["SYSTEM"]["smearing"] = smearing.type
# smearing degauss setting
parameters["pw"]["parameters"]["SYSTEM"]["degauss"] = smearing.degauss

magnetization: MagnetizationModel = self.get_model("magnetization") # type: ignore
magnetization: MagnetizationCalculationSettingsModel = self.get_model(
"magnetization"
) # type: ignore
if self.spin_type == "collinear":
parameters["initial_magnetic_moments"] = magnetization.moments

Expand Down Expand Up @@ -175,7 +177,7 @@ def get_model_state(self):
return parameters

def set_model_state(self, parameters):
pseudos: PseudosModel = self.get_model("pseudos") # type: ignore
pseudos: PseudosCalculationSettingsModel = self.get_model("pseudos") # type: ignore
if "pseudo_family" in parameters:
pseudo_family = PseudoFamily.from_string(parameters["pseudo_family"])
library = pseudo_family.library
Expand All @@ -194,7 +196,9 @@ def set_model_state(self, parameters):
if (pw_parameters := parameters.get("pw", {}).get("parameters")) is not None:
self._set_pw_parameters(pw_parameters)

magnetization: MagnetizationModel = self.get_model("magnetization") # type: ignore
magnetization: MagnetizationCalculationSettingsModel = self.get_model(
"magnetization"
) # type: ignore
if magnetic_moments := parameters.get("initial_magnetic_moments"):
if isinstance(magnetic_moments, (int, float)):
magnetic_moments = [magnetic_moments]
Expand All @@ -207,7 +211,7 @@ def set_model_state(self, parameters):
)
magnetization.moments = magnetic_moments

hubbard: HubbardModel = self.get_model("hubbard") # type: ignore
hubbard: HubbardCalculationSettingsModel = self.get_model("hubbard") # type: ignore
if parameters.get("hubbard_parameters"):
hubbard.is_active = True
hubbard.parameters = parameters["hubbard_parameters"]["hubbard_u"]
Expand Down Expand Up @@ -238,7 +242,7 @@ def reset(self):
def _get_default(self, trait):
return self._defaults.get(trait, self.traits()[trait].default_value)

def _link_model(self, model: AdvancedSubModel):
def _link_model(self, model: AdvancedCalculationSubSettingsModel):
ipw.dlink(
(self, "loaded_from_process"),
(model, "loaded_from_process"),
Expand Down Expand Up @@ -322,13 +326,15 @@ def _set_pw_parameters(self, pw_parameters):
system_params.get("vdw_corr", "none"),
)

smearing: SmearingModel = self.get_model("smearing") # type: ignore
smearing: SmearingCalculationSettingsModel = self.get_model("smearing") # type: ignore
if "degauss" in system_params:
smearing.degauss = system_params["degauss"]

if "smearing" in system_params:
smearing.type = system_params["smearing"]

magnetization: MagnetizationModel = self.get_model("magnetization") # type: ignore
magnetization: MagnetizationCalculationSettingsModel = self.get_model(
"magnetization"
) # type: ignore
if "tot_magnetization" in system_params:
magnetization.type = "tot_magnetization"
Loading

0 comments on commit 555701f

Please sign in to comment.