Skip to content

[FREIA] Add inversion mode - #853

Open
jokasimr wants to merge 14 commits into
mainfrom
inversion-mode
Open

jokasimr wants to merge 14 commits into
mainfrom
inversion-mode

Conversation

@jokasimr

@jokasimr jokasimr commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #852

@github-actions github-actions Bot added the essreflectometry Issues for essreflectometry. label Nov 3, 2026
@jokasimr
jokasimr changed the base branch from main to freia-time-dependent November 3, 2026 13:24
@jokasimr
jokasimr marked this pull request as ready for review November 4, 2026 14:13
Base automatically changed from freia-time-dependent to main November 9, 2026 08:34
@jokasimr
jokasimr marked this pull request as draft November 13, 2026 15:14
@jokasimr

jokasimr commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

I noticed that I've misunderstood this a bit: There are three relevant(?) slits in the inverted case, see figure on https://confluence.ess.eu/spaces/ECDC/pages/910460471/FREIA+Geometry+Operation+Modes, not only the two I thought there was, and the mirror is not upstream from the second incident beam slit.

This is fine. The mirror is before the last slit before the sample.

@jokasimr
jokasimr marked this pull request as ready for review November 17, 2026 12:17
@jokasimr
jokasimr requested a review from jl-wynen November 20, 2026 09:52
@jokasimr

Copy link
Copy Markdown
Contributor Author

This is ready for review now 👍

@jokasimr

jokasimr commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

This PR adds two changes:

  1. It adds a workflow mode where we assume there is a mirror in the beam. This changes the flight path and the geometry, the angle the beam hits the sample, etc. The implementation handles this by reading the mirror position and orientation to figure out the mirror plane, and then it mirrors the position of relevant components upstream of the mirror in that plane. From there the incident angle and flight path can be computed as if there was no mirror (except for gravity correction!).
  2. It adds gravity correction to the incident beam. Previously we applied gravity correction only to the outgoing beam, but gravity also affects the incident beam and this should be taken into account. The gravity correction is in principle different for the three incident beams, because it depends on the geometry. But the difference is negligible when there is no mirror in the beam. However, when there is a mirror in the beam, the three beams will hit the mirror in different spots and gravity will act in the opposite direction (relative to the beam direction) for longer or shorter depending on the incident beam.

The effect of gravity is summarized in a coefficient called D, the "effective length" of the gravity correction.
When the beam does not change direction then D is just the distance traveled, for regular gravity correction applied to the outgoing beam D=L2. But when the beam changes direction, as it does in inversion mode, D becomes the combined effect of gravity before the change of direction and after the change of direction.

Before this change there was a mechanism to "look up" the nominal incident angle based on what beam path defines the incident beam. Now the same mechanism is used to look up both the nominal incident angle and the D coefficient. Then the incident angle on the sample is computed using the nominal incident angle, D and wavelength.

filename: Filename[RunType],
) -> SampleSurfaceNormal[RunType]:
"""Load the normal pointing out of the sample's underside in inversion mode."""
return SampleSurfaceNormal[RunType](-mcstas_sample_surface_normal(filename))

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to have a separate type for the inverted normal.

  1. It allows you to derive it from the normal
  2. It prevents accidentally using the wrong vector. Changing the normal like this may break code in hard to detect ways

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conceptually there is no such thing as an "inverted normal" in the workflow. The helper is just there to be used for testing. I'll move it to the test module to make it less confusing.

length = sc.norm(normal)
if not sc.isfinite(length).value or length.value == 0:
raise ValueError('MirrorNormal must have a finite, nonzero norm.')
return MirrorGeometry(position=position, normal=normal / length)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is risky because you end up with two normal vectors in the workflow, MirrorNormal and MirrorGeometry.normal and they are different. Can we do away with the two separate inputs and require loaders and users to specify MirrorGeometry? It's __post_init__ can then check and normalise its inputs.

interval: TimeInterval[RunType],
time_filter: TransformationTimeFilter[
snx.NXmirror, RunType
] = reject_time_dependent_transform,

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of reimplementing the transform chain logic, can you use ess.reduce.nexus's component loader? You should be able to request Component[snx.NXmirror] and its chain if you provide the right nexus class.


def mirror_position(
transformation: NeXusTransformation[snx.NXmirror, RunType],
) -> MirrorPosition[RunType]:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similarly to above, you should be able to request Position[snx.NXmirror]. And if you keep it, MirrorPosition could be an alias to that.

"""Rotate local -z, the NXmirror reference-plane normal, to the lab frame."""
origin = mirror_position(transformation)
tip = transformation.value * sc.vector([0.0, 0.0, -1.0], unit='m')
normal = tip - origin

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How does this work? It looks to me like top - origin will point in -z direction because both vectors are translated the same way. Or does the transform encode a rotation as well? If so, I think you are sensitive to whether to rotates first or translates first.

@jokasimr jokasimr Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The transform encodes a combined translation and rotation. Only the rotation is relevant to determine the mirror normal in the lab coordinate system. The translation term is a nuisance in this case, and it is removed by subtracting the origin. The result is $R n_l$, the transformation of the mirror orientation to the lab coordinate system.

There is no assumption about what order the operations occur in, the only assumption is that the origin is the transformation of the origin in the mirror local coordinate system to the lab system.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. Is there a convention in nexus that without rotation, mirrors are in the x-y plane? Or how come you use [0,0,-z]? Also, why negative? It says elsewhere that the direction of the mirror normal does not matter.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes the Nexus standard specifies the orientation of the mirror in the component local coordinate system: https://manual.nexusformat.org/classes/base_classes/NXmirror.html "The mirror faces negative z values."


def no_mirror() -> MirrorGeometry[RunType] | None:
"""Use the ordinary geometry when inversion is disabled."""
return None

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need this? If the user sets inversion = False, can't you just omit the mirror entirely from the pipeline?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes that is better 👍

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is too much here for me to review on a Friday afternoon. Do you have notes on this with graphs?

@jokasimr jokasimr Nov 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have these AI generated notes:

freia-inversion-gravity.pdf

They are actually not terrible, but should be taken with a grain of salt.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not gonna look at them because I don't have a reference. If you don't have something you or a scientist checked, I'm better off not risking looking at something potentially wrong.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have checked them extensively and they seem correct to me. There might be details wrong or confusing, of course. But they are certainly useful for understanding the ideas behind the implementation.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

essreflectometry Issues for essreflectometry.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FREIA] Add inversion mode

2 participants