diff --git a/src/hyspecppt/hppt/experiment_settings.py b/src/hyspecppt/hppt/experiment_settings.py index 508e500..a942107 100644 --- a/src/hyspecppt/hppt/experiment_settings.py +++ b/src/hyspecppt/hppt/experiment_settings.py @@ -20,6 +20,9 @@ DEFAULT_CROSSHAIR = dict(DeltaE=0, modQ=0) DEFAULT_MODE = dict(current_experiment_type="single_crystal") +# maximum momentum transfer +MaxQ = 15 + # invalid style INVALID_QLINEEDIT = """ QLineEdit { diff --git a/src/hyspecppt/hppt/hppt_model.py b/src/hyspecppt/hppt/hppt_model.py index 338e5c0..788eef1 100644 --- a/src/hyspecppt/hppt/hppt_model.py +++ b/src/hyspecppt/hppt/hppt_model.py @@ -5,7 +5,7 @@ import numpy as np from scipy.constants import e, hbar, m_n -from .experiment_settings import PLOT_TYPES +from .experiment_settings import DEFAULT_CROSSHAIR, DEFAULT_EXPERIMENT, DEFAULT_LATTICE, DEFAULT_MODE, PLOT_TYPES, MaxQ logger = logging.getLogger("hyspecppt") @@ -13,15 +13,15 @@ class SingleCrystalParameters: """Model for single crystal calculations""" - a: float - b: float - c: float - alpha: float - beta: float - gamma: float - h: float - k: float - l: float + a: float = DEFAULT_LATTICE["a"] + b: float = DEFAULT_LATTICE["b"] + c: float = DEFAULT_LATTICE["c"] + alpha: float = DEFAULT_LATTICE["alpha"] + beta: float = DEFAULT_LATTICE["beta"] + gamma: float = DEFAULT_LATTICE["gamma"] + h: float = DEFAULT_LATTICE["h"] + k: float = DEFAULT_LATTICE["k"] + l: float = DEFAULT_LATTICE["l"] def __init__(self) -> None: """Constructor""" @@ -97,9 +97,9 @@ def calculate_modQ(self) -> float: class CrosshairParameters: """Model for the crosshair parameters""" - modQ: float - DeltaE: float - current_experiment_type: str + modQ: float = DEFAULT_CROSSHAIR["modQ"] + DeltaE: float = DEFAULT_CROSSHAIR["DeltaE"] + current_experiment_type: str = DEFAULT_MODE["current_experiment_type"] sc_parameters: SingleCrystalParameters experiment_types = ["single_crystal", "powder"] @@ -118,17 +118,21 @@ def set_crosshair(self, current_experiment_type: str, DeltaE: float = None, modQ def get_crosshair(self) -> dict[str, float]: """Get the crosshair""" if self.current_experiment_type == "single_crystal": - self.modQ = self.sc_parameters.calculate_modQ() - return dict(DeltaE=self.DeltaE, modQ=self.modQ) + modQ = self.sc_parameters.calculate_modQ() + if modQ < MaxQ: + self.modQ = modQ + return dict(DeltaE=self.DeltaE, modQ=modQ) + else: + return dict(DeltaE=self.DeltaE, modQ=self.modQ) class HyspecPPTModel: """Main model""" - Ei: float = 0 - S2: float = 0 - alpha_p: float = 0 - plot_type: str = "" + Ei: float = DEFAULT_EXPERIMENT["Ei"] + S2: float = DEFAULT_EXPERIMENT["S2"] + alpha_p: float = DEFAULT_EXPERIMENT["alpha_p"] + plot_type: str = DEFAULT_EXPERIMENT["plot_type"] cp: CrosshairParameters def __init__(self): diff --git a/tests/hppt_model/test_hyspecpptmodel.py b/tests/hppt_model/test_hyspecpptmodel.py new file mode 100644 index 0000000..c10e9b9 --- /dev/null +++ b/tests/hppt_model/test_hyspecpptmodel.py @@ -0,0 +1,8 @@ +# import numpy as np # noqa: F401 + +from hyspecppt.hppt.hppt_model import HyspecPPTModel # noqa: F401 + + +def test_single_crystal_parameter_set_parameters(): + """Test SingleCrystalParameters set_parameters function""" + # saving for next story diff --git a/tests/hppt_model/test_single_crystal_model.py b/tests/hppt_model/test_single_crystal_model.py new file mode 100644 index 0000000..3164883 --- /dev/null +++ b/tests/hppt_model/test_single_crystal_model.py @@ -0,0 +1,99 @@ +import numpy as np + +from hyspecppt.hppt.hppt_model import CrosshairParameters, SingleCrystalParameters + + +def test_single_crystal_parameter_set_parameters(): + """Test SingleCrystalParameters set_parameters function""" + scp = SingleCrystalParameters() + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} + scp.set_parameters(sc_data) + + assert scp.a == 1.0 + assert scp.b == 1.0 + assert scp.c == 1.0 + assert scp.alpha == 90.0 + assert scp.beta == 90.0 + assert scp.gamma == 90.0 + assert scp.h == 0 + assert scp.k == 0 + assert scp.l == 0 + + +def test_single_crystal_parameter_get_parameters(): + """Test SingleCrystalParameters get_parameters function""" + scp = SingleCrystalParameters() + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} + scp.set_parameters(sc_data) + + assert scp.get_parameters()["a"] == 1.0 + assert scp.get_parameters()["b"] == 1.0 + assert scp.get_parameters()["c"] == 1.0 + assert scp.get_parameters()["alpha"] == 90.0 + assert scp.get_parameters()["beta"] == 90.0 + assert scp.get_parameters()["gamma"] == 90.0 + assert scp.get_parameters()["h"] == 0 + assert scp.get_parameters()["k"] == 0 + assert scp.get_parameters()["l"] == 0 + + +def test_single_crystal_parameter_calculate_modQ(): + """Test SingleCrystalParameters calculate_modQ function""" + scp = SingleCrystalParameters() + sc_data = {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} + scp.set_parameters(sc_data) + assert scp.calculate_modQ() == 0.0 + + sc_data["h"] = 1 + sc_data["k"] = 2 + sc_data["l"] = 3 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 23.5095267) + + sc_data["alpha"] = 60 + sc_data["beta"] = 60 + sc_data["gamma"] = 90 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 19.3660777) + + sc_data["a"] = 10 + sc_data["b"] = 10 + sc_data["c"] = 15.12312 + scp.set_parameters(sc_data) + assert np.isclose(scp.calculate_modQ(), 1.469240) + + +def test_cross_hair_parameters_set_crosshair(): + """Test Crosshair set_crosshair function""" + cp = CrosshairParameters() + current_experiment_type = "single_crystal" + DeltaE = 10.0 + modQ = 1.23 + cp.set_crosshair(current_experiment_type) + assert cp.current_experiment_type == "single_crystal" + + cp.set_crosshair(current_experiment_type, DeltaE=DeltaE) + assert cp.DeltaE == 10.0 + + cp.set_crosshair(current_experiment_type, modQ=modQ) + assert cp.DeltaE == 10.0 + assert cp.modQ == 1.23 + + +def test_get_cross_hair_parameters_set_crosshair(): + """Test Crosshair get_crosshair function""" + cp = CrosshairParameters() + current_experiment_type = "single_crystal" + DeltaE = 10.0 + modQ = 1.23 + + cp.set_crosshair(current_experiment_type, DeltaE=DeltaE, modQ=modQ) + cp.sc_parameters.set_parameters( + {"a": 1.0, "b": 1.0, "c": 1.0, "alpha": 90.0, "beta": 90, "gamma": 90, "h": 0, "k": 0, "l": 0} + ) + assert cp.get_crosshair()["DeltaE"] == 10.0 + assert cp.get_crosshair()["modQ"] == 0.0 + + cp.set_crosshair("powder", DeltaE=DeltaE, modQ=modQ) + assert cp.get_crosshair()["DeltaE"] == 10.0 + assert cp.get_crosshair()["modQ"] == 1.23