Skip to content

Commit

Permalink
calculate cos^2 - sin^2 in model (#42)
Browse files Browse the repository at this point in the history
* calculate cos^2 - sin^2 in model

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* add test

* math optimization

* comments in test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
KyleQianliMa and pre-commit-ci[bot] authored Feb 17, 2025
1 parent 50eb1d6 commit f1704cf
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/hyspecppt/hppt/experiment_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
alpha + subscript_s,
"cos" + square + alpha + subscript_s,
"(1+cos" + square + alpha + subscript_s + ")/2",
"(cos" + square + alpha + subscript_s + "-sin" + square + alpha + subscript_s + ")",
]

# default parameters
Expand Down
3 changes: 2 additions & 1 deletion src/hyspecppt/hppt/hppt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,5 +305,6 @@ def calculate_graph_data(self) -> dict[str, np.array]:
intensity = cos_ang_PQ**2
elif self.plot_type == PLOT_TYPES[2]: # "(cos^2(a)+1)/2"
intensity = (cos_ang_PQ**2 + 1) / 2

elif self.plot_type == PLOT_TYPES[3]:
intensity = 2 * cos_ang_PQ**2 - 1
return dict(Q_low=Q_low, Q_hi=Q_hi, E=E, Q2d=Q2d, E2d=E2d, intensity=intensity, plot_type=self.plot_type)
35 changes: 35 additions & 0 deletions tests/hppt_model/test_hyspecpptmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,38 @@ def test_zero_alpha():
model.set_experiment_data(Ei=20.0, S2=-40.0, alpha_p=70.0, plot_type=PLOT_TYPES[0])
# This is the point of Q ~ 2.1 \\A-1, E ~ 0 meV, alpha should be close to 0
assert np.isclose(model.calculate_graph_data()["intensity"][94][105], 0.209092)


def test_cos2_sin2():
"""Test calculating different graph data"""
model = HyspecPPTModel()
model.set_experiment_data(Ei=20.0, S2=60.0, alpha_p=30.0, plot_type=PLOT_TYPES[3])
assert np.isclose(min(model.calculate_graph_data()["Q_low"]), 1.55338)
assert np.isclose(max(model.calculate_graph_data()["Q_low"]), 2.30880)

assert np.isclose(min(model.calculate_graph_data()["Q_hi"]), 3.25839)
assert np.isclose(max(model.calculate_graph_data()["Q_hi"]), 5.3811)
assert model.calculate_graph_data()["E"].all() == np.linspace(-20, 18, 200).all()

assert model.calculate_graph_data()["Q2d"][0][0] == 0.0
assert model.calculate_graph_data()["Q2d"][0][1] == 0.0

assert np.isclose(model.calculate_graph_data()["Q2d"][199][0], 5.38106)
assert np.isclose(model.calculate_graph_data()["Q2d"][199][1], 5.38106)

assert np.isclose(model.calculate_graph_data()["E2d"][0][0], -20)
assert np.isclose(model.calculate_graph_data()["E2d"][0][199], 18.0)

assert np.isnan(model.calculate_graph_data()["intensity"][0][0]) # not allowed (Q, E) positions
assert np.isnan(model.calculate_graph_data()["intensity"][84][4]) # not allowed (Q, E) positions

# keeping cos^2(alpha) - sin^2(alpha) in tests for more robustness
assert np.isclose(
model.calculate_graph_data()["intensity"][84][5],
np.cos(np.radians(136.5994336)) ** 2 - np.sin(np.radians(136.5994336)) ** 2,
)
assert np.isclose(
model.calculate_graph_data()["intensity"][199][0],
np.cos(np.radians(84.7356103)) ** 2 - np.sin(np.radians(84.7356103)) ** 2,
)
assert np.isnan(model.calculate_graph_data()["intensity"][199][1]) # not allowed (Q, E) positions

0 comments on commit f1704cf

Please sign in to comment.