Skip to content

Add inelastic sample component that changes neutron wavelengths#124

Open
nvaytet wants to merge 22 commits intomainfrom
inelastic-sample
Open

Add inelastic sample component that changes neutron wavelengths#124
nvaytet wants to merge 22 commits intomainfrom
inelastic-sample

Conversation

@nvaytet
Copy link
Member

@nvaytet nvaytet commented Feb 25, 2026

This PR adds a new type of component: InelasticSample.
It modifies the neutron energies according to a probability function for energy shift.

It is used as follows:

# Sample 1: Uniform probability distribution
sample1 = tof.InelasticSample(
    distance=28.0 * meter,
    name="sample",
    delta_e=sc.DataArray(
        data=sc.ones(sizes={'e': 100}),
        coords={'e': sc.linspace('e', -0.2, 0.2, 100, unit='meV')},
    ),
)

# Sample 2: normal distribution
x = sc.linspace('e', -0.2, 0.2, 100, unit='meV')
sig = sc.scalar(0.03, unit='meV')
y = 1.0 / (np.sqrt(2.0 * np.pi) * sig) * sc.exp(-((x / sig) ** 2) / 2)
y.unit = ""

sample2 = tof.InelasticSample(
    distance=28.0 * meter,
    name="sample",
    delta_e=sc.DataArray(data=y, coords={'e': x}),
)

tof.Model(source=source, components=choppers + detectors + [sample1])
Figure 4 Figure 1 (32) Figure 1 (33)

Internal changes

Quite a bit of the internals had to be refactored, because of the way the chopper and detector logic was hard-coded inside the Model (especially the run function).
Instead of taking in choppers and detectors, the Model now accepts a flat list of components.
The effects of each component are applied onto the neutrons in succession, and the logic for those effects has been moved inside the components themselves.

I think it makes for a much cleaner structure in the model. Logic for plotting was also moved into the respective components.

Fixes #117

@nvaytet
Copy link
Member Author

nvaytet commented Feb 25, 2026

@bingli621 please take a look (and also try it out!)

self.add_detector(None)
det = self.detectors_container.children[-1]
det.distance_widget.value = comp.distance.to(unit='m').value
det.name_widget.value = comp.name
Copy link
Member Author

Choose a reason for hiding this comment

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

This is where we need to also update the dashboard to support samples. This will be done in a follow up PR.

@nvaytet
Copy link
Member Author

nvaytet commented Feb 26, 2026

Comments from in-person discussion:

  • Use a callable instead of a data array with probability distribution to apply the sample effects on the neutrons (more flexible)
  • Prevent neutrons from becoming unphysical (large DeltaE for large wavelengths could mean the final energy is less than zero)

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add "sample" component that changes neutron energy

1 participant