Demo#

This notebook is a short example on how to use the tof package for making time-of-light diagrams of neutrons passing through a chopper cascade.

[1]:
import scipp as sc
import tof

Hz = sc.Unit('Hz')
deg = sc.Unit('deg')
meter = sc.Unit('m')

Create a source pulse#

We first create a source with one pulse containing 1 million neutrons whose distribution follows the ESS time and wavelength profiles (both thermal and cold neutrons are included).

[2]:
source = tof.Source(facility='ess', neutrons=1_000_000)
source
[2]:
Source:
  pulses=1, neutrons per pulse=1000000
  frequency=14.0 Hz
  facility='ess'
[3]:
source.plot()
[3]:
_images/demo_4_0.svg

Chopper set-up#

We create a list of choppers that will be included in our beamline. In our case, we make two WFM choppers, and two frame-overlap choppers. All choppers have 6 openings.

[4]:
choppers = [
    tof.Chopper(
        frequency=70.0 * Hz,
        open=sc.array(
            dims=['cutout'],
            values=[98.71, 155.49, 208.26, 257.32, 302.91, 345.3],
            unit='deg',
        ),
        close=sc.array(
            dims=['cutout'],
            values=[109.7, 170.79, 227.56, 280.33, 329.37, 375.0],
            unit='deg',
        ),
        phase=47.10 * deg,
        distance=6.6 * meter,
        name="WFM1",
    ),
    tof.Chopper(
        frequency=70 * Hz,
        open=sc.array(
            dims=['cutout'],
            values=[80.04, 141.1, 197.88, 250.67, 299.73, 345.0],
            unit='deg',
        ),
        close=sc.array(
            dims=['cutout'],
            values=[91.03, 156.4, 217.18, 269.97, 322.74, 375.0],
            unit='deg',
        ),
        phase=76.76 * deg,
        distance=7.1 * meter,
        name="WFM2",
    ),
    tof.Chopper(
        frequency=56 * Hz,
        open=sc.array(
            dims=['cutout'],
            values=[74.6, 139.6, 194.3, 245.3, 294.8, 347.2],
            unit='deg',
        ),
        close=sc.array(
            dims=['cutout'],
            values=[95.2, 162.8, 216.1, 263.1, 310.5, 371.6],
            unit='deg',
        ),
        phase=62.40 * deg,
        distance=8.8 * meter,
        name="Frame-overlap 1",
    ),
    tof.Chopper(
        frequency=28 * Hz,
        open=sc.array(
            dims=['cutout'],
            values=[98.0, 154.0, 206.8, 254.0, 299.0, 344.65],
            unit='deg',
        ),
        close=sc.array(
            dims=['cutout'],
            values=[134.6, 190.06, 237.01, 280.88, 323.56, 373.76],
            unit='deg',
        ),
        phase=12.27 * deg,
        distance=15.9 * meter,
        name="Frame-overlap 2",
    ),
    tof.Chopper(
        frequency=7 * Hz,
        open=sc.array(
            dims=['cutout'],
            values=[30.0],
            unit='deg',
        ),
        close=sc.array(
            dims=['cutout'],
            values=[140.0],
            unit='deg',
        ),
        phase=0 * deg,
        distance=22 * meter,
        name="Pulse-overlap",
    ),
]

Detector set-up#

We add a monitor 26 meters from the source, and a main detector 32 meters from the source.

[5]:
detectors = [
    tof.Detector(distance=26.0 * meter, name='monitor'),
    tof.Detector(distance=32.0 * meter, name='detector'),
]

Run the model#

We combine the source, choppers, and detectors into our model, and then use the .run() method to execute the ray-tracing simulation.

[6]:
model = tof.Model(source=source, choppers=choppers, detectors=detectors)
model
[6]:
Model:
  Source: Source:
  pulses=1, neutrons per pulse=1000000
  frequency=14.0 Hz
  facility='ess'
  Choppers:
    WFM1: Chopper(name=WFM1, distance=6.6 m, frequency=70.0 Hz, phase=47.1 deg, direction=CLOCKWISE, cutouts=6)
    WFM2: Chopper(name=WFM2, distance=7.1 m, frequency=70.0 Hz, phase=76.76 deg, direction=CLOCKWISE, cutouts=6)
    Frame-overlap 1: Chopper(name=Frame-overlap 1, distance=8.8 m, frequency=56.0 Hz, phase=62.4 deg, direction=CLOCKWISE, cutouts=6)
    Frame-overlap 2: Chopper(name=Frame-overlap 2, distance=15.9 m, frequency=28.0 Hz, phase=12.27 deg, direction=CLOCKWISE, cutouts=6)
    Pulse-overlap: Chopper(name=Pulse-overlap, distance=22.0 m, frequency=7.0 Hz, phase=0.0 deg, direction=CLOCKWISE, cutouts=1)
  Detectors:
    monitor: Detector(name=monitor, distance=26.0 m)
    detector: Detector(name=detector, distance=32.0 m)
[7]:
res = model.run()
res
[7]:
Result:
  Source: 1 pulses, 1000000 neutrons per pulse.
  Choppers:
    WFM1: visible=210407, blocked=789593
    WFM2: visible=126143, blocked=84264
    Frame-overlap 1: visible=102260, blocked=23883
    Frame-overlap 2: visible=73232, blocked=29028
    Pulse-overlap: visible=73176, blocked=56
  Detectors:
    monitor: visible=73176
    detector: visible=73176

Results#

Plotting#

We can plot the models as a whole (which will show the ray paths through the system), and the individual components (which will show the counts each component is seeing).

[8]:
res.plot(visible_rays=5000)
[8]:
Plot(ax=<Axes: xlabel='Time [μs]', ylabel='Distance [m]'>, fig=<Figure size 640x480 with 2 Axes>)
_images/demo_13_1.png
[9]:
res.detectors['monitor'].toa.plot()
[9]:
_images/demo_14_0.svg
[10]:
res.choppers["Frame-overlap 2"].toa.plot()
[10]:
_images/demo_15_0.svg

Data inspection#

Each component entry in the results objects holds all the information about the neutrons that reached that component. The .data property of the object returns a data array, which has one pulse of neutrons.

[11]:
res.choppers['WFM1'].data
[11]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (55.32 MB)
    • pulse: 1
    • event: 1000000
    • birth_time
      (pulse, event)
      float64
      µs
      1810.663, 1284.588, ..., 2418.716, 1802.127
      Values:
      array([[1810.6630474 , 1284.5880417 , 1069.32458724, ..., 1800.2511843 , 2418.71639274, 1802.12672408]], shape=(1, 1000000))
    • distance
      ()
      float64
      m
      6.6
      Values:
      array(6.6)
    • id
      (pulse, event)
      int64
      0, 1, ..., 999998, 999999
      Values:
      array([[ 0, 1, 2, ..., 999997, 999998, 999999]], shape=(1, 1000000))
    • speed
      (pulse, event)
      float64
      m/s
      1869.611, 650.743, ..., 646.518, 2010.019
      Values:
      array([[1869.61111865, 650.74308263, 5324.28062595, ..., 640.769566 , 646.51843817, 2010.0186217 ]], shape=(1, 1000000))
    • toa
      (pulse, event)
      float64
      µs
      5340.809, 1.143e+04, ..., 1.263e+04, 5085.678
      Values:
      array([[ 5340.80893397, 11426.83953254, 2308.92866968, ..., 12100.36584354, 12627.24195108, 5085.67839308]], shape=(1, 1000000))
    • tof
      (pulse, event)
      float64
      µs
      5340.809, 1.143e+04, ..., 1.263e+04, 5085.678
      Values:
      array([[ 5340.80893397, 11426.83953254, 2308.92866968, ..., 12100.36584354, 12627.24195108, 5085.67839308]], shape=(1, 1000000))
    • wavelength
      (pulse, event)
      float64
      Å
      2.116, 6.079, ..., 6.119, 1.968
      Values:
      array([[2.11596624, 6.07925633, 0.74301756, ..., 6.17387937, 6.11898095, 1.96815789]], shape=(1, 1000000))
    • (pulse, event)
      float64
      counts
      1.0, 1.0, ..., 1.0, 1.0
      Values:
      array([[1., 1., 1., ..., 1., 1., 1.]], shape=(1, 1000000))
    • blocked_by_me
      (pulse, event)
      bool
      True, True, ..., False, True
      Values:
      array([[ True, True, False, ..., False, False, True]], shape=(1, 1000000))
    • blocked_by_others
      (pulse, event)
      bool
      False, False, ..., False, False
      Values:
      array([[False, False, False, ..., False, False, False]], shape=(1, 1000000))

The .toa, .wavelength, .birth_time, and .speed properties of the beamline components return a proxy object, which gives access to the data they hold.

[12]:
res.choppers['WFM1'].toa.data
[12]:
Show/Hide data repr Show/Hide attributes
scipp.DataArray (17.17 MB)
    • pulse: 1
    • event: 1000000
    • toa
      (pulse, event)
      float64
      µs
      5340.809, 1.143e+04, ..., 1.263e+04, 5085.678
      Values:
      array([[ 5340.80893397, 11426.83953254, 2308.92866968, ..., 12100.36584354, 12627.24195108, 5085.67839308]], shape=(1, 1000000))
    • (pulse, event)
      float64
      counts
      1.0, 1.0, ..., 1.0, 1.0
      Values:
      array([[1., 1., 1., ..., 1., 1., 1.]], shape=(1, 1000000))
    • blocked_by_me
      (pulse, event)
      bool
      True, True, ..., False, True
      Values:
      array([[ True, True, False, ..., False, False, True]], shape=(1, 1000000))
    • blocked_by_others
      (pulse, event)
      bool
      False, False, ..., False, False
      Values:
      array([[False, False, False, ..., False, False, False]], shape=(1, 1000000))

As these are Scipp data structures, they can be manipulated (e.g. histogrammed) and plotted directly.

[13]:
res.choppers['WFM1'].wavelength.data.hist(wavelength=500).plot()
[13]:
_images/demo_21_0.svg