Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/tof/chopper.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,10 @@ def __init__(
widths: sc.Variable | None = None,
direction: Direction = Clockwise,
):
if frequency <= (0.0 * frequency.unit):
raise ValueError(f"Chopper frequency must be positive, got {frequency:c}.")
if frequency < (0.0 * frequency.unit):
raise ValueError(
f"Chopper frequency must be non-negative, got {frequency:c}."
)
self.frequency = frequency.to(dtype=float, copy=False)
if direction not in (Clockwise, AntiClockwise):
raise ValueError(
Expand Down Expand Up @@ -208,6 +210,13 @@ def open_close_times(
time_limit = sc.scalar(0.0, unit='us')
if unit is None:
unit = time_limit.unit

if self.frequency.value == 0.0:
return (
sc.array(dims=['cutout'], values=[-np.inf], unit=unit),
sc.array(dims=['cutout'], values=[np.inf], unit=unit),
)

nrot = max(int(sc.ceil((time_limit * self.frequency).to(unit='')).value), 1)
# We make a unique dim name that is different from self.open.dim and
# self.close.dim to make use of automatic broadcasting below.
Expand Down
16 changes: 15 additions & 1 deletion tests/chopper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def test_phase_int():


def test_frequency_must_be_positive():
with pytest.raises(ValueError, match="Chopper frequency must be positive"):
with pytest.raises(ValueError, match="Chopper frequency must be non-negative"):
tof.Chopper(
frequency=-1.0 * Hz,
open=0.0 * deg,
Expand Down Expand Up @@ -688,3 +688,17 @@ def test_from_nexus_clockwise():
nexus_chopper['slit_edges'][1::2] - nexus_chopper['beam_position'],
)
assert chopper.direction == tof.Clockwise


def test_chopper_zero_frequency():
chopper = tof.Chopper(
frequency=0.0 * Hz,
open=10.0 * deg,
close=20.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
topen, tclose = chopper.open_close_times(0.0 * sec)
assert sc.identical(topen[0], -np.inf * sec)
assert sc.identical(tclose[0], np.inf * sec)
25 changes: 25 additions & 0 deletions tests/model_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,28 @@ def test_model_run_without_components_raises(dummy_source):
ValueError, match="Cannot run model: no components have been defined."
):
model.run()


def test_chopper_with_zero_frequency_blocks_nothing(make_source):
chopper = tof.Chopper(
frequency=0.0 * Hz,
open=10.0 * deg,
close=20.0 * deg,
phase=0.0 * deg,
distance=5.0 * meter,
name='chopper',
)
detector = tof.Detector(distance=20 * meter, name='detector')

N = 10_000

source = tof.Source(facility='ess', neutrons=N)
model = tof.Model(source=source, choppers=[chopper], detectors=[detector])
res = model.run()

assert res.choppers['chopper'].toa.data.sum().value == N
assert res.choppers['chopper'].toa.data.masks['blocked_by_me'].sum().value == 0
assert res.detectors['detector'].toa.data.sum().value == N
assert (
res.detectors['detector'].toa.data.masks['blocked_by_others'].sum().value == 0
)
Loading