def create_beamline(): perp_to_floor = 90.0 beam_start = PositionAndAngle(y=0, z=0, angle=-2.5) s0 = Component("s0", movement_strategy=LinearMovement(0, 0, perp_to_floor)) s1 = Component("s1", movement_strategy=LinearMovement(0, 1, perp_to_floor)) frame_overlap_mirror = ReflectingComponent( "FOM", movement_strategy=LinearMovement(0, 2, perp_to_floor)) frame_overlap_mirror.enabled = False polarising_mirror = ReflectingComponent("Polarising mirror", movement_strategy=LinearMovement( 0, 3, perp_to_floor)) polarising_mirror.enabled = False s2 = Component("s2", movement_strategy=LinearMovement(0, 4, perp_to_floor)) ideal_sample_point = ReflectingComponent("Ideal Sample Point", movement_strategy=LinearMovement( 0, 5, perp_to_floor)) s3 = Component("s3", movement_strategy=LinearMovement(0, 6, perp_to_floor)) analyser = ReflectingComponent("analyser", movement_strategy=LinearMovement( 0, 7, perp_to_floor)) analyser.enabled = False s4 = Component("s4", movement_strategy=LinearMovement(0, 8, perp_to_floor)) detector = Component("detector", movement_strategy=LinearMovement( 0, 10, perp_to_floor)) theta = Theta("theta", ideal_sample_point) beamline = Beamline([ s0, s1, frame_overlap_mirror, polarising_mirror, s2, ideal_sample_point, s3, analyser, s4, detector ], [theta], []) beamline.set_incoming_beam(beam_start) beamline.active_mode = BeamlineMode("NR", ["theta"]) return beamline
def test_GIVEN_unpolarised_mode_and_beamline_parameters_are_set_WHEN_move_THEN_components_move_onto_beam_line(self): slit2 = Component("s2", LinearMovement(0, z_position=10, angle=90)) ideal_sample_point = ReflectingComponent("ideal_sample_point", LinearMovement(0, z_position=20, angle=90)) detector = Component("detector", LinearMovement(0, z_position=30, angle=90)) components = [slit2, ideal_sample_point, detector] parameters = [ TrackingPosition("slit2height", slit2), TrackingPosition("height", ideal_sample_point), Theta("theta", ideal_sample_point), TrackingPosition("detectorheight", detector)] #parameters["detectorAngle": TrackingAngle(detector) beam = PositionAndAngle(0, 0, -45) beamline = Beamline(components, parameters, [], [DataMother.BEAMLINE_MODE_NEUTRON_REFLECTION]) beamline.set_incoming_beam(beam) beamline.active_mode = DataMother.BEAMLINE_MODE_NEUTRON_REFLECTION beamline.parameter("theta").sp_no_move = 45 beamline.parameter("height").sp_no_move = 0 beamline.parameter("slit2height").sp_no_move = 0 beamline.parameter("detectorheight").sp_no_move = 0 beamline.move = 1 assert_that(slit2.sp_position(), is_(position(Position(-10, 10)))) assert_that(ideal_sample_point.sp_position(), is_(position(Position(-20, 20)))) assert_that(detector.sp_position(), is_(position(Position(-10, 30))))
def test_GIVEN_theta_and_a_set_but_no_move_WHEN_get_changed_THEN_changed_is_true(self): theta_set = 10.0 sample = ReflectingComponent("sample", movement_strategy=LinearMovement(0, 0, 90)) theta = Theta("theta",sample) theta.sp_no_move = theta_set result = theta.sp_changed assert_that(result, is_(True))
def test_GIVEN_component_parameter_disabled_in_mode_WHEN_parameter_moved_to_THEN_component_is_disabled(self): super_mirror = ReflectingComponent("super mirror", LinearMovement(z_position=10, y_position=0, angle=90)) super_mirror.enabled = True sm_enabled = ComponentEnabled("smenabled", super_mirror) enabled_sp = False sm_enabled.sp_no_move = enabled_sp sm_enabled.move = 1 assert_that(sm_enabled.sp_rbv, is_(enabled_sp)) assert_that(super_mirror.enabled, is_(enabled_sp))
def test_GIVEN_a_mode_with_a_two_beamline_parameter_in_WHEN_move_first_THEN_second_beamline_parameter_is_calculated_and_moved_to(self): angle_to_set = 45.0 ideal_sample_point = ReflectingComponent("ideal_sample_point", LinearMovement(y_position=0, z_position=20, angle=90)) theta = Theta("theta", ideal_sample_point) super_mirror = ReflectingComponent("super mirror", LinearMovement(y_position=0, z_position=10, angle=90)) smangle = ReflectionAngle("smangle", super_mirror) beamline_mode = BeamlineMode("mode name", [theta.name, smangle.name]) beamline = Beamline([super_mirror, ideal_sample_point], [smangle, theta], [], [beamline_mode]) beam = PositionAndAngle(0, 0, 0) theta.sp_no_move = angle_to_set smangle.sp_no_move = 0 beamline.set_incoming_beam(beam) beamline.active_mode = beamline_mode beamline.move = 1 smangle_to_set = -10 smangle.sp = smangle_to_set assert_that(ideal_sample_point.angle, is_(smangle_to_set*2 + angle_to_set))
def test_GIVEN_theta_WHEN_set_set_point_THEN_sample_hasnt_moved(self): theta_set = 10.0 sample = ReflectingComponent("sample", movement_strategy=LinearMovement(0, 0, 90)) mirror_pos = -100 sample.angle = mirror_pos theta = Theta("theta", sample) theta.sp_no_move = theta_set assert_that(theta.sp, is_(theta_set)) assert_that(sample.angle, is_(mirror_pos))
def setup_beamline(self, initial_mirror_angle, mirror_position, beam_start): jaws = Component("jaws", movement_strategy=LinearMovement(0, 0, 90)) mirror = ReflectingComponent("mirror", movement_strategy=LinearMovement( 0, mirror_position, 90)) mirror.angle = initial_mirror_angle jaws3 = Component("jaws3", movement_strategy=LinearMovement(0, 20, 90)) beamline = Beamline([jaws, mirror, jaws3], [], [], []) beamline.set_incoming_beam(beam_start) return beamline, mirror
def test_GIVEN_mode_has_initial_value_for_param_not_in_beamline_WHEN_setting_mode_THEN_keyerror_raised(self): sm_angle = 0.0 super_mirror = ReflectingComponent("super mirror", LinearMovement(z_position=10, y_position=0, angle=90)) super_mirror.angle = sm_angle smangle = ReflectionAngle("smangle", super_mirror) smangle.sp_no_move = sm_angle sp_inits = {"nonsense name": sm_angle} beamline_mode = BeamlineMode("mode name", [smangle.name], sp_inits) beamline = Beamline([super_mirror], [smangle], [], [beamline_mode]) with self.assertRaises(KeyError): beamline.active_mode = beamline_mode
def test_GIVEN_theta_and_a_set_and_move_WHEN_get_changed_THEN_changed_is_false(self): theta_set = 10.0 sample = ReflectingComponent("sample", movement_strategy=LinearMovement(0, 0, 90)) sample.set_incoming_beam(PositionAndAngle(0, 0, 0)) theta = Theta("theta",sample) theta.sp_no_move = theta_set theta.move = 1 result = theta.sp_changed assert_that(result, is_(False))
def test_GIVEN_a_mode_with_a_single_beamline_parameter_in_WHEN_move_THEN_beamline_parameter_is_calculated_on_move(self): angle_to_set = 45.0 ideal_sample_point = ReflectingComponent("ideal_sample_point", LinearMovement(y_position=0, z_position=20, angle=90)) theta = Theta("theta", ideal_sample_point) beamline_mode = BeamlineMode("mode name", [theta.name]) beamline = Beamline([ideal_sample_point], [theta], [], [beamline_mode]) beam = PositionAndAngle(0, 0, 0) theta.sp_no_move = angle_to_set beamline.set_incoming_beam(beam) beamline.active_mode = beamline_mode beamline.move = 1 assert_that(ideal_sample_point.angle, is_(angle_to_set))
def setUp(self): beam_start = PositionAndAngle(y=0, z=0, angle=2.5) s0 = Component("s0", movement_strategy=LinearMovement(0, 0, 90)) s1 = Component("s1", movement_strategy=LinearMovement(0, 1, 90)) frame_overlap_mirror = ReflectingComponent( "FOM", movement_strategy=LinearMovement(0, 2, 90)) frame_overlap_mirror.enabled = False self.polarising_mirror = ReflectingComponent( "Polariser", movement_strategy=LinearMovement(0, 3, 90)) self.polarising_mirror.enabled = False self.polarising_mirror.angle = 0 s2 = Component("s2", movement_strategy=LinearMovement(0, 4, 90)) self.ideal_sample_point = ReflectingComponent( "ideal sample point", movement_strategy=LinearMovement(0, 5, 90)) s3 = Component("s3", movement_strategy=LinearMovement(0, 6, 90)) analyser = ReflectingComponent("analyser", movement_strategy=LinearMovement( 0, 7, 90)) analyser.enabled = False s4 = Component("s4", movement_strategy=LinearMovement(0, 8, 90)) detector = Component("detector", movement_strategy=LinearMovement(0, 10, 90)) theta = Theta("theta", self.ideal_sample_point) theta.sp_no_move = 0 smangle = ReflectionAngle("smangle", self.polarising_mirror) smangle.sp_no_move = 0 self.nr_mode = BeamlineMode("NR Mode", [theta.name]) self.polarised_mode = BeamlineMode("NR Mode", [smangle.name, theta.name]) self.beamline = Beamline([ s0, s1, frame_overlap_mirror, self.polarising_mirror, s2, self.ideal_sample_point, s3, analyser, s4, detector ], [smangle, theta], [], [self.nr_mode, self.polarised_mode]) self.beamline.set_incoming_beam(beam_start)
def test_GIVEN_reflection_angle_WHEN_set_set_point_and_move_THEN_readback_is_as_set_and_sample_is_at_setpoint_postion(self): angle_set = 10.0 expected_sample_angle = 10.0 sample = ReflectingComponent("sample", movement_strategy=LinearMovement(0, 0, 90)) sample.set_incoming_beam(PositionAndAngle(0, 0, 0)) mirror_pos = -100 sample.angle = mirror_pos reflection_angle = ReflectionAngle("theta", sample) reflection_angle.sp_no_move = angle_set reflection_angle.move = 1 result = reflection_angle.sp_rbv assert_that(result, is_(angle_set)) assert_that(sample.angle, is_(expected_sample_angle))
def test_GIVEN_theta_set_WHEN_set_point_set_and_move_THEN_readback_is_as_original_value_but_setpoint_is_new_value(self): original_theta = 1.0 theta_set = 10.0 sample = ReflectingComponent("sample", movement_strategy=LinearMovement(0, 0, 90)) sample.set_incoming_beam(PositionAndAngle(0, 0, 0)) mirror_pos = -100 sample.angle = mirror_pos theta = Theta("theta", sample) theta.sp = original_theta theta.sp_no_move = theta_set result = theta.sp_rbv assert_that(result, is_(original_theta)) assert_that(theta.sp, is_(theta_set))
def test_GIVEN_parameter_in_mode_and_not_changed_and_no_previous_parameter_changed_WHEN_moving_beamline_THEN_parameter_unchanged(self): initial_s2_height = 0.0 super_mirror = ReflectingComponent("sm", LinearMovement(0.0, 10, 90.0)) s2 = Component("s2", LinearMovement(initial_s2_height, 20, 90.0)) sm_angle = ReflectionAngle("smangle", super_mirror, True) slit2_pos = TrackingPosition("slit2pos", s2, True) mode = BeamlineMode("both_params", [sm_angle.name, slit2_pos.name]) beamline = Beamline([super_mirror, s2], [sm_angle, slit2_pos], [], [mode]) beamline.set_incoming_beam(PositionAndAngle(0, 0, 0)) beamline.active_mode = mode beamline.move = 1 assert_that(s2.sp_position().y, is_(initial_s2_height))
def test_GIVEN_mirror_with_input_beam_at_WHEN_get_beam_out_THEN_beam_output_correct( self, beam_angle, mirror_angle, outgoing_angle): beam_start = PositionAndAngle(y=0, z=0, angle=beam_angle) expected = PositionAndAngle(y=0, z=0, angle=outgoing_angle) mirror = ReflectingComponent("component", movement_strategy=LinearMovement( 0, 0, 90)) mirror.angle = mirror_angle mirror.set_incoming_beam(beam_start) result = mirror.get_outgoing_beam() assert_that( result, is_(position_and_angle(expected)), "beam_angle: {}, mirror_angle: {}".format(beam_angle, mirror_angle))
def test_GIVEN_mode_has_initial_parameter_value_WHEN_setting_mode_THEN_component_sp_updated_but_rbv_unchanged(self): sm_angle = 0.0 sm_angle_to_set = 45.0 super_mirror = ReflectingComponent("super mirror", LinearMovement(z_position=10, y_position=0, angle=90)) super_mirror.angle = sm_angle smangle = ReflectionAngle("smangle", super_mirror) smangle.sp_no_move = sm_angle sp_inits = {smangle.name: sm_angle_to_set} beamline_mode = BeamlineMode("mode name", [smangle.name], sp_inits) beamline = Beamline([super_mirror], [smangle], [], [beamline_mode]) beamline.set_incoming_beam(PositionAndAngle(0, 0, 0)) beamline.active_mode = beamline_mode assert_that(smangle.sp, is_(sm_angle_to_set)) assert_that(smangle.sp_changed, is_(True)) assert_that(super_mirror.angle, is_(sm_angle))
def test_GIVEN_angled_mirror_is_disabled_WHEN_get_beam_out_THEN_outgoing_beam_is_incoming_beam( self): mirror_z_position = 10 mirror_angle = 15 beam_start = PositionAndAngle(y=0, z=0, angle=0) expected = beam_start mirror = ReflectingComponent("component", movement_strategy=LinearMovement( 0, mirror_z_position, 90)) mirror.angle = mirror_angle mirror.set_incoming_beam(beam_start) mirror.enabled = False result = mirror.get_outgoing_beam() assert_that(result, is_(position_and_angle(expected)))
def test_GIVEN_mirror_with_input_beam_at_0_deg_and_z0_y0_WHEN_get_beam_out_THEN_beam_output_z_is_zmirror_y_is_ymirror_angle_is_input_angle_plus_device_angle( self): mirror_z_position = 10 mirror_angle = 15 beam_start = PositionAndAngle(y=0, z=0, angle=0) expected = PositionAndAngle(y=0, z=mirror_z_position, angle=2 * mirror_angle) mirror = ReflectingComponent("component", movement_strategy=LinearMovement( 0, mirror_z_position, 90)) mirror.angle = mirror_angle mirror.set_incoming_beam(beam_start) result = mirror.get_outgoing_beam() assert_that(result, is_(position_and_angle(expected)))
def test_GIVEN_parameter_changed_and_not_in_mode_and_no_previous_parameter_changed_WHEN_moving_beamline_THEN_parameter_moved_to_sp(self): initial_s2_height = 0.0 target_s2_height = 1.0 super_mirror = ReflectingComponent("sm", LinearMovement(0.0, 10, 90.0)) s2 = Component("s2", LinearMovement(initial_s2_height, 20, 90.0)) sm_angle = ReflectionAngle("smangle", super_mirror) slit2_pos = TrackingPosition("slit2pos", s2) empty_mode = BeamlineMode("empty", []) beamline = Beamline([super_mirror, s2], [sm_angle, slit2_pos], [], [empty_mode]) beamline.set_incoming_beam(PositionAndAngle(0, 0, 0)) beamline.active_mode = empty_mode slit2_pos.sp_no_move = target_s2_height beamline.move = 1 assert_that(s2.sp_position().y, is_(target_s2_height))
def test_GIVEN_multiple_components_in_beamline_WHEN_triggering_move_THEN_components_move_at_speed_of_slowest_axis( self): sm_angle = 0.0 sm_angle_to_set = 22.5 supermirror = ReflectingComponent("supermirror", movement_strategy=LinearMovement( y_position=0.0, z_position=10.0, angle=90.0)) sm_height_axis = create_mock_axis("SM:HEIGHT", 0.0, 10.0) sm_angle_axis = create_mock_axis("SM:ANGLE", sm_angle, 10.0) supermirror.angle = sm_angle supermirror_driver = HeightAndAngleDriver(supermirror, sm_height_axis, sm_angle_axis) slit_2 = Component("slit_2", movement_strategy=LinearMovement(y_position=0.0, z_position=20.0, angle=90.0)) slit_2_height_axis = create_mock_axis("SLIT2:HEIGHT", 0.0, 10.0) slit_2_driver = HeightDriver(slit_2, slit_2_height_axis) slit_3 = Component("slit_3", movement_strategy=LinearMovement(y_position=0.0, z_position=30.0, angle=90.0)) slit_3_height_axis = create_mock_axis("SLIT3:HEIGHT", 0.0, 10.0) slit_3_driver = HeightDriver(slit_3, slit_3_height_axis) detector = TiltingJaws( "jaws", movement_strategy=LinearMovement(y_position=0.0, z_position=40.0, angle=90.0)) detector_height_axis = create_mock_axis("DETECTOR:HEIGHT", 0.0, 10.0) detector_tilt_axis = create_mock_axis("DETECTOR:TILT", 0.0, 10.0) detector_driver = HeightAndTiltDriver(detector, detector_height_axis, detector_tilt_axis) smangle = ReflectionAngle("smangle", supermirror) slit_2_pos = TrackingPosition("s2_pos", slit_2) slit_3_pos = TrackingPosition("s3_pos", slit_3) det_pos = TrackingPosition("det_pos", detector) components = [supermirror, slit_2, slit_3, detector] beamline_parameters = [smangle, slit_2_pos, slit_3_pos, det_pos] drivers = [ supermirror_driver, slit_2_driver, slit_3_driver, detector_driver ] mode = BeamlineMode( "mode name", [smangle.name, slit_2_pos.name, slit_3_pos.name, det_pos.name]) beamline = Beamline(components, beamline_parameters, drivers, [mode]) beamline.active_mode = mode beam_start = PositionAndAngle(0.0, 0.0, 0.0) beamline.set_incoming_beam(beam_start) slit_2_pos.sp_no_move = 0.0 slit_3_pos.sp_no_move = 0.0 det_pos.sp_no_move = 0.0 # detector angle axis takes longest expected_max_duration = 4.5 smangle.sp_no_move = sm_angle_to_set with patch.object(beamline, '_move_drivers') as mock: beamline.move = 1 mock.assert_called_with(expected_max_duration)