Skip to content

Commit

Permalink
Angle correction (#40)
Browse files Browse the repository at this point in the history
* fix Px, Pz

* fixed old tests and added "test_zero_alpha"

* pre-commit fix

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

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

* accurate description

---------

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 4, 2025
1 parent 3a3a02c commit c219107
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions src/hyspecppt/hppt/hppt_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,11 @@ def calculate_graph_data(self) -> dict[str, np.array]:
Qx = kf2d * np.sqrt((1 - cos_theta**2))

# Transform polarization angle in the lab frame to vector
Px = np.cos(np.radians(self.alpha_p))
Pz = np.sin(np.radians(self.alpha_p))
Px = np.sin(np.radians(self.alpha_p))
Pz = np.cos(np.radians(self.alpha_p))

# Calculate angle between polarization vector and momentum transfer
cos_ang_PQ = (Qx * Px + Qz * Pz) / Q2d / np.sqrt(Px**2 + Pz**2)
cos_ang_PQ = (Qx * Px + Qz * Pz) / Q2d

# Select return value for intensity
if self.plot_type == PLOT_TYPES[0]: # alpha
Expand Down
24 changes: 16 additions & 8 deletions tests/hppt_model/test_hyspecpptmodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ def test_calculate_graph_data_alpha():
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

assert np.isclose(model.calculate_graph_data()["intensity"][84][5], 166.59943)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], 114.73561)
assert np.isclose(model.calculate_graph_data()["intensity"][84][5], 136.5994336)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], 84.7356103)
assert np.isnan(model.calculate_graph_data()["intensity"][199][1]) # not allowed (Q, E) positions


Expand All @@ -101,8 +101,8 @@ def test_calculate_graph_data_cos2_alpha():
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

assert np.isclose(model.calculate_graph_data()["intensity"][84][5], np.cos(np.radians(166.59943)) ** 2)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], np.cos(np.radians(114.73561)) ** 2)
assert np.isclose(model.calculate_graph_data()["intensity"][84][5], np.cos(np.radians(136.5994336)) ** 2)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], np.cos(np.radians(84.7356103)) ** 2)
assert np.isnan(model.calculate_graph_data()["intensity"][199][1]) # not allowed (Q, E) positions


Expand All @@ -129,8 +129,8 @@ def test_calculate_graph_data_intensity():
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

assert np.isclose(model.calculate_graph_data()["intensity"][84][5], (np.cos(np.radians(166.59943)) ** 2 + 1) / 2)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], (np.cos(np.radians(114.73561)) ** 2 + 1) / 2)
assert np.isclose(model.calculate_graph_data()["intensity"][84][5], (np.cos(np.radians(136.5994336)) ** 2 + 1) / 2)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], (np.cos(np.radians(84.7356103)) ** 2 + 1) / 2)
assert np.isnan(model.calculate_graph_data()["intensity"][199][1]) # not allowed (Q, E) positions


Expand Down Expand Up @@ -179,8 +179,8 @@ def test_calculate_graph_data_intensity_negative_S2():
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

assert np.isclose(model.calculate_graph_data()["intensity"][84][5], 0.736049)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], 0.995790)
assert np.isclose(model.calculate_graph_data()["intensity"][84][5], 0.5268558)
assert np.isclose(model.calculate_graph_data()["intensity"][199][0], 0.912457)
assert np.isnan(model.calculate_graph_data()["intensity"][199][1]) # not allowed (Q, E) positions


Expand Down Expand Up @@ -211,3 +211,11 @@ def test_calculate_graph_data_consistency():
inds = np.isfinite(d_45_45["intensity"])
assert np.allclose((d_45_45["intensity"] + d_m45_45["intensity"])[inds], 1)
assert np.allclose((d_45_m45["intensity"] + d_m45_m45["intensity"])[inds], 1)


def test_zero_alpha():
"""Test alpha = 0 at S2=-40, Q=2.1, Ei = 20, P_angle = 70"""
model = HyspecPPTModel()
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)

0 comments on commit c219107

Please sign in to comment.