diff --git a/src/tof/model.py b/src/tof/model.py index be81c90..3e7bee3 100644 --- a/src/tof/model.py +++ b/src/tof/model.py @@ -144,6 +144,7 @@ def run(self): container[c.name]['data'] = self.source.data.copy(deep=False) t = birth_time + (c.distance / speed).to(unit=birth_time.unit, copy=False) container[c.name]['data'].coords['toa'] = t + container[c.name]['data'].coords['distance'] = c.distance # TODO: remove 'tof' coordinate once deprecation period is over container[c.name]['data'].coords['tof'] = t if isinstance(c, Detector): diff --git a/tests/model_test.py b/tests/model_test.py index 043f1fc..c62042c 100644 --- a/tests/model_test.py +++ b/tests/model_test.py @@ -355,3 +355,35 @@ def test_model_repr_does_not_raise(): source=source, choppers=[chopper1, chopper2], detectors=[detector] ) assert repr(model) is not None + + +def test_component_distance(): + # Make a chopper open from 10-20 ms. Assume zero phase. + topen = 10.0 * ms + tclose = 20.0 * ms + chopper = make_chopper( + topen=[topen], + tclose=[tclose], + f=10.0 * Hz, + phase=0.0 * deg, + distance=10 * meter, + name='chopper', + ) + monitor = tof.Detector(distance=17 * meter, name='monitor') + detector = tof.Detector(distance=20 * meter, name='detector') + + # Make a pulse with 3 neutrons with one neutron going through the chopper opening + # and the other two neutrons on either side of the opening. + source = make_source( + arrival_times=sc.concat( + [0.9 * topen, 0.5 * (topen + tclose), 1.1 * tclose], dim='event' + ), + distance=chopper.distance, + ) + + model = tof.Model(source=source, choppers=[chopper], detectors=[monitor, detector]) + res = model.run() + + assert sc.identical(res['monitor'].data.coords['distance'], monitor.distance) + assert sc.identical(res['detector'].data.coords['distance'], detector.distance) + assert sc.identical(res['chopper'].data.coords['distance'], chopper.distance)