Add inelastic sample component that changes neutron wavelengths#124
Add inelastic sample component that changes neutron wavelengths#124
Conversation
|
@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 |
There was a problem hiding this comment.
This is where we need to also update the dashboard to support samples. This will be done in a follow up PR.
|
Comments from in-person discussion:
|
Fix cbar zoom on results plot
| "rng = np.random.default_rng(seed=83)\n", | ||
| "\n", | ||
| "\n", | ||
| "def uniform_deltae(e_i):\n", |
There was a problem hiding this comment.
I"m wondering if it would make more sense to have a function take in the initial energy and return the final energy instead of the energy transfer?
It could look something like:
def apply_energy_transfer(e_i):
de = sc.array(dims=e_i.dims, values=rng.uniform(-0.2, 0.2, size=e_i.shape), unit='meV')
return e_i.to(unit='meV') - de?
I initially went with returning DeltaE so that the user writing the function would not have to worry about handling unit conversions and unphysical final energies. But the unit conversions are not too much code to add, and we can still fix final energies inside the package code after the final energy is returned by the function.
This mechanism here would also allow the user to fix the negative energies in the way they want: either throw them away or give them a zero energy.
@bingli621 any opinions?
There was a problem hiding this comment.
In the end, I went with the function that returns final energy, I think it makes more sense.
There was a problem hiding this comment.
from the simulation's perspective, having Ef will make the calculation much easier. but the user should be able to change the energy transfer as well.
uniform_sampler = lambda size: rng.uniform(-0.2, 0.2, size=size)
def apply_energy_transfer(e_i, energy_transfer_sampler= uniform_sampler):
de = sc.array(dims=e_i.dims, values=sampler(size=e_i.shape), unit='meV')
return e_i.to(unit='meV') - de
There was a problem hiding this comment.
Not sure I understand.
The user supplied the whole function, so they can put in there what they want.
The question was: should the user supply a function that outputs energy transfer, or should it output final_energy?
In the first case, computing the final_energy from the initial_energy and the energy_transfer would be done internally in the package.
I think the second case make more sense; a function that would output the energy_transfer feels unusual/strange.
There was a problem hiding this comment.
At T-REX and CSPEC, one sample can be measured with different Ei. In this sense, I would imagine user should supply a function describing the energy transfer, which is the property of the sample, regardless of what Ei is used to measure it. And the Sample component should stay the same when we change the condition how it is measured. I hope this what you are asking...
|
@bingli621 if you have time to take another look, it would be great :-) |
|
I will try it out, I have a great use case in mind, but it needs a bit more work on the code I'm working on, will update asap. |
This PR adds a new type of component:
InelasticSample.It modifies the neutron energies according to a user-defined function for energy shift.
It is used as follows:
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 therunfunction).Instead of taking in
choppersanddetectors, 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