Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
edan-bainglass committed Oct 25, 2024
1 parent 5f6a75d commit 14a6568
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 203 deletions.
179 changes: 85 additions & 94 deletions tests/configuration/test_advanced.py
Original file line number Diff line number Diff line change
@@ -1,217 +1,208 @@
import pytest

from aiidalab_qe.app.main import App
from aiidalab_qe.app.configuration.advanced.advanced import AdvancedSettings
from aiidalab_qe.app.configuration.advanced.model import AdvancedModel


def test_advanced_default():
"""Test default behavior of advanced setting."""
app = App(qe_auto_setup=False)
model = app.config_model
model = AdvancedModel()
_ = AdvancedSettings(model=model)

# Test override functionality in advanced settings
model.advanced.override = True
model.workchain.protocol = "fast"
model.advanced.smearing.type = "methfessel-paxton"
model.advanced.smearing.degauss = 0.03
model.advanced.kpoints_distance = 0.22
model.override = True
model.protocol = "fast"
model.smearing.type = "methfessel-paxton"
model.smearing.degauss = 0.03
model.kpoints_distance = 0.22

# Reset values to default after removing override
model.advanced.override = False
# Reset values to default w.r.t protocol
model.override = False

assert model.advanced.smearing.type == "cold"
assert model.advanced.smearing.degauss == 0.01
assert model.advanced.kpoints_distance == 0.5
assert model.smearing.type == "cold"
assert model.smearing.degauss == 0.01
assert model.kpoints_distance == 0.5


def test_advanced_smearing_settings():
"""Test Smearing Settings."""
app = App(qe_auto_setup=False)
model = app.config_model
advanced = app.configure_step.advanced_settings
advanced.smearing.render()
from aiidalab_qe.app.configuration.advanced.smearing import SmearingSettings

model = AdvancedModel()
smearing = SmearingSettings(model=model)
smearing.render()

# Test widget disable state on override
assert advanced.smearing.degauss.disabled is True
assert advanced.smearing.smearing.disabled is True
assert smearing.degauss.disabled is True
assert smearing.smearing.disabled is True

model.advanced.override = True
model.override = True

assert advanced.smearing.degauss.disabled is False
assert advanced.smearing.smearing.disabled is False
assert smearing.degauss.disabled is False
assert smearing.smearing.disabled is False

assert model.advanced.smearing.type == "cold"
assert model.advanced.smearing.degauss == 0.01
assert model.smearing.type == "cold"
assert model.smearing.degauss == 0.01

# Test protocol-dependent smearing change
model.workchain.protocol = "fast"
model.protocol = "fast"

assert model.advanced.smearing.type == "cold"
assert model.advanced.smearing.degauss == 0.01
assert model.smearing.type == "cold"
assert model.smearing.degauss == 0.01

# Check reset
model.advanced.smearing.type = "gaussian"
model.advanced.smearing.degauss = 0.05
model.advanced.smearing.reset()
model.smearing.type = "gaussian"
model.smearing.degauss = 0.05
model.smearing.reset()

assert model.workchain.protocol == "fast" # reset does not apply to protocol
assert model.advanced.smearing.type == "cold"
assert model.advanced.smearing.degauss == 0.01
assert model.protocol == "fast" # reset does not apply to protocol
assert model.smearing.type == "cold"
assert model.smearing.degauss == 0.01


def test_advanced_kpoints_settings():
"""Test kpoint setting of advanced setting widget."""
app = App(qe_auto_setup=False)
model = app.config_model
advanced = app.configure_step.advanced_settings
"""Test kpoints setting of advanced setting widget."""
model = AdvancedModel()
advanced = AdvancedSettings(model=model)
advanced.render()

# Check the disable of is bind to override switch
assert advanced.kpoints_distance.disabled is True

model.advanced.override = True
model.override = True
assert advanced.kpoints_distance.disabled is False

assert model.advanced.kpoints_distance == 0.15
assert model.kpoints_distance == 0.15

model.workchain.protocol = "fast"
assert model.advanced.kpoints_distance == 0.5
model.protocol = "fast"
assert model.kpoints_distance == 0.5

# Check reset
model.advanced.kpoints_distance = 0.1
model.advanced.reset()
model.kpoints_distance = 0.1
model.reset()

assert model.workchain.protocol == "fast" # reset does not apply to protocol
assert model.advanced.kpoints_distance == 0.5
assert model.protocol == "fast" # reset does not apply to protocol
assert model.kpoints_distance == 0.5


@pytest.mark.usefixtures("aiida_profile_clean", "sssp")
def test_advanced_molecule_settings(generate_structure_data):
"""Test kpoints setting of advanced setting widget."""
app = App(qe_auto_setup=False)

model = app.config_model
advanced = app.configure_step.advanced_settings
model = AdvancedModel()
advanced = AdvancedSettings(model=model)
advanced.render()

# Check the disable of is bind to override switch
assert advanced.kpoints_distance.disabled is True

model.advanced.override = True
model.override = True
assert advanced.kpoints_distance.disabled is False

# Create molecule
structure = generate_structure_data(name="H2O", pbc=(False, False, False))
# Assign the molecule
model.input_structure = structure

# Check override can not modify the kpoints_distance
assert advanced.kpoints_distance.disabled is True
model.advanced.override = True
model.override = True
assert advanced.kpoints_distance.disabled is True

# Confirm the value of kpoints_distance is fixed
assert model.advanced.kpoints_distance == 100.0
assert model.kpoints_distance == 100.0

model.workchain.protocol = "fast"
assert model.advanced.kpoints_distance == 100.0
model.protocol = "fast"
assert model.kpoints_distance == 100.0

# Check that reset is done w.r.t the molecule structure
model.advanced.reset()
assert model.workchain.protocol == "fast" # reset does not apply to protocol
assert model.advanced.kpoints_distance == 100
model.reset()
assert model.protocol == "fast" # reset does not apply to protocol
assert model.kpoints_distance == 100


def test_advanced_tot_charge_settings():
"""Test TotCharge widget."""
app = App(qe_auto_setup=False)
model = app.config_model
advanced = app.configure_step.advanced_settings
model = AdvancedModel()
advanced = AdvancedSettings(model=model)
advanced.render()

# Check the disable of is bind to override switch
assert advanced.total_charge.disabled is True

model.advanced.override = True
model.override = True
assert advanced.total_charge.disabled is False

assert model.advanced.total_charge == 0.0
assert model.total_charge == 0.0

# Check reset
model.advanced.total_charge = 1.0
model.advanced.reset()
model.total_charge = 1.0
model.reset()

assert model.advanced.total_charge == 0.0
assert model.total_charge == 0.0


@pytest.mark.usefixtures("aiida_profile_clean", "sssp")
def test_advanced_kpoints_mesh(generate_structure_data):
"""Test Mesh Grid HTML widget."""
app = App(qe_auto_setup=False)
model = app.config_model
model = AdvancedModel()
_ = AdvancedSettings(model=model)

structure = generate_structure_data(name="silicon")
model.input_structure = structure

model.advanced.override = True
assert model.advanced.mesh_grid == "Mesh [14, 14, 14]"
model.override = True
assert model.mesh_grid == "Mesh [14, 14, 14]"

# change protocol
model.workchain.protocol = "fast"
assert model.advanced.mesh_grid == "Mesh [5, 5, 5]"
model.protocol = "fast"
assert model.mesh_grid == "Mesh [5, 5, 5]"


@pytest.mark.usefixtures("aiida_profile_clean", "sssp")
def test_advanced_hubbard_settings(generate_structure_data):
"""Test Hubbard widget."""
app = App(qe_auto_setup=False)
from aiidalab_qe.app.configuration.advanced.hubbard import HubbardSettings

model = app.config_model
hubbard_model = model.advanced.hubbard
model = AdvancedModel()
hubbard = HubbardSettings(model=model)
hubbard.render()

structure = generate_structure_data(name="LiCoO2")
model.input_structure = structure

# Activate Hubbard U widget
hubbard_model.is_active = True
assert hubbard_model.orbital_labels == ["Co - 3d", "O - 2p", "Li - 2s"]

# Render the settings widget to allow for widget testing
hubbard = app.configure_step.advanced_settings.hubbard
hubbard.render()
model.hubbard.is_active = True
assert model.hubbard.orbital_labels == ["Co - 3d", "O - 2p", "Li - 2s"]

# Change the Hubbard U parameters for Co, O, and Li
hubbard_parameters = hubbard.hubbard_widget.children[1:] # type: ignore
hubbard_parameters[0].value = 1 # Co - 3d
hubbard_parameters[1].value = 2 # O - 2p
hubbard_parameters[2].value = 3 # Li - 2s

assert hubbard_model.parameters == {
assert model.hubbard.parameters == {
"Co - 3d": 1.0,
"O - 2p": 2.0,
"Li - 2s": 3.0,
}

# The widget hierarchy for eigenvalues:
# - w.hubbard_widget.eigen_values_widget.children[0]: List of eigenvalues for Co
# - w.hubbard_widget.eigen_values_widget.children[0].children[1]: Widgets for up and down spin
# - w.hubbard_widget.eigen_values_widget.children[0].children[1].children[0]: Widget for up spin
# - w.hubbard_widget.eigen_values_widget.children[0].children[1].children[0].children[1]: Widget for eigenvalue 1 (3d range: 1 to 5)
# - hubbard.eigenvalues_widget.children[0]: List of eigenvalues for Co
# - hubbard.eigenvalues_widget.children[0].children[1]: Widgets for up and down spin
# - hubbard.eigenvalues_widget.children[0].children[1].children[0]: Widget for up spin
# - hubbard.eigenvalues_widget.children[0].children[1].children[0].children[1]: Widget for eigenvalue 1 (3d range: 1 to 5)

# Check eigenvalues are empty
# assert hubbard_model.eigenvalues == [] # TODO should they be?
# assert model.hubbard.eigenvalues == [] # TODO should they be?

# Check there is only eigenvalues for Co (Transition metal)
hubbard_model.has_eigenvalues = True
assert len(hubbard_model.applicable_elements) == 1
assert len(hubbard_model.eigenvalues) == 1
model.hubbard.has_eigenvalues = True
assert len(model.hubbard.applicable_elements) == 1
assert len(model.hubbard.eigenvalues) == 1

Co_eigenvalues = hubbard.eigenvalues_widget.children[0].children[1] # type: ignore
Co_spin_down_row = Co_eigenvalues.children[1]
Co_spin_down_row.children[1].value = "1"
Co_spin_down_row.children[3].value = "1"
Co_spin_down_row.children[5].value = "1"

assert hubbard_model.get_active_eigenvalues() == [
assert model.hubbard.get_active_eigenvalues() == [
[1, 1, "Co", 1],
[3, 1, "Co", 1],
[5, 1, "Co", 1],
Expand Down
41 changes: 23 additions & 18 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,9 +362,7 @@ def _workchain_settings_generator(**kwargs):
@pytest.fixture()
def smearing_settings_generator():
"""Return a function that generates a smearing settings dictionary."""
from aiidalab_qe.app.configuration.advanced.smearing.smearing import (
SmearingSettings,
)
from aiidalab_qe.app.configuration.advanced.smearing import SmearingSettings

def _smearing_settings_generator(**kwargs):
model = ConfigurationModel()
Expand All @@ -389,6 +387,7 @@ def app(pw_code, dos_code, projwfc_code, projwfc_bands_code):
# installation, we need to mock set the installation status to `True` to
# avoid the blocker message pop up in the submission step.
app.submit_step.sssp_installation.installed = True
app.submit_step.qe_setup.installed = True

# set up codes
app.submit_model.get_code("pdos", "dos").activate()
Expand Down Expand Up @@ -451,25 +450,28 @@ def _submit_app_generator(
}
}
app.config_model.set_model_state(parameters)

# Advanced settings
app.config_model.advanced.override = True
app.config_model.advanced.total_charge = tot_charge
app.config_model.advanced.van_der_waals = vdw_corr
app.config_model.advanced.kpoints_distance = kpoints_distance
app.config_model.advanced.electron_maxstep = electron_maxstep
advanced_model = app.config_model.get_model("advanced")

advanced_model.override = True
advanced_model.total_charge = tot_charge
advanced_model.van_der_waals = vdw_corr
advanced_model.kpoints_distance = kpoints_distance
advanced_model.electron_maxstep = electron_maxstep
if isinstance(initial_magnetic_moments, (int, float)):
initial_magnetic_moments = [initial_magnetic_moments]
app.config_model.advanced.magnetization.moments = dict(
advanced_model.magnetization.moments = dict(
zip(
app.config_model.input_structure.get_kind_names(),
initial_magnetic_moments,
)
)
# mimic the behavior of the smearing widget set up
app.config_model.advanced.smearing.type = smearing
app.config_model.advanced.smearing.degauss = degauss
advanced_model.smearing.type = smearing
advanced_model.smearing.degauss = degauss
app.configure_step.confirm()
#

app.submit_model.input_structure = generate_structure_data()
app.submit_model.code_widgets["pw"].num_cpus.value = 2

Expand Down Expand Up @@ -746,20 +748,23 @@ def _generate_qeapp_workchain(
structure = app.struct_model.structure # type: ignore

# step 2 configure
app.config_model.workchain.relax_type = relax_type
workchain_model = app.config_model.get_model("workchain")
advanced_model = app.config_model.get_model("advanced")

app.config_model.relax_type = relax_type

# In order to prepare complete inputs, I set all the properties to true
# this can be overridden later
app.config_model.get_model("bands").include = run_bands
app.config_model.get_model("pdos").include = run_pdos

app.config_model.workchain.protocol = "fast"
app.config_model.workchain.spin_type = spin_type
app.config_model.workchain.electronic_type = electronic_type
workchain_model.protocol = "fast"
workchain_model.spin_type = spin_type
workchain_model.electronic_type = electronic_type

if spin_type == "collinear":
app.config_model.advanced.override = True
magnetization = app.config_model.advanced.magnetization
advanced_model.override = True
magnetization = advanced_model.magnetization
if electronic_type == "insulator":
magnetization.total = tot_magnetization
elif magnetization_type == "starting_magnetization":
Expand Down
Loading

0 comments on commit 14a6568

Please sign in to comment.