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_beam_line_contains_one_passive_component_WHEN_beam_set_THEN_component_has_beam_out_same_as_beam_in( self): beam_start = PositionAndAngle(y=0, z=0, angle=0) jaws = Component("jaws", movement_strategy=LinearMovement(0, 2, 90)) beamline = Beamline([jaws], [], [], []) beamline.set_incoming_beam(beam_start) result = beamline[0].get_outgoing_beam() assert_that(result, is_(position_and_angle(beam_start)))
def create_beamline(): beam_angle_natural = -45 beam_start = PositionAndAngle(0.0, 0.0, beam_angle_natural) perp_to_floor = 90.0 # COMPONENTS # s1 = Component("s1", LinearMovement(0.0, 7.3025, perp_to_beam_angle)) # s2 = Component("s2", LinearMovement(0.0, 9.6885, perp_to_beam_angle)) # s3 = Component("s3", LinearMovement(0.0, 10.651, perp_to_beam_angle)) # s4 = Component("s4", LinearMovement(0.0, 11.983, perp_to_beam_angle)) # super_mirror = ReflectingComponent("sm", LinearMovement(0.0, 7.7685, perp_to_beam_angle)) # sample = ReflectingComponent("sample", LinearMovement(0.0, 10.25, perp_to_beam_angle)) # point_det = TiltingJaws("pdet", LinearMovement(0.0, 12.113, perp_to_beam_angle)) s1 = Component("s1", LinearMovement(0.0, 1, perp_to_floor)) super_mirror = ReflectingComponent("sm", LinearMovement(0.0, 5, perp_to_floor)) s2 = Component("s2", LinearMovement(0.0, 9, perp_to_floor)) sample = ReflectingComponent("sample", LinearMovement(0.0, 10, perp_to_floor)) s3 = Component("s3", LinearMovement(0.0, 15, perp_to_floor)) s4 = Component("s4", LinearMovement(0.0, 19, perp_to_floor)) point_det = TiltingJaws("det", LinearMovement(0.0, 20, perp_to_floor)) comps = [s1, super_mirror, s2, sample, s3, s4, point_det] # BEAMLINE PARAMETERS sm_enabled = ComponentEnabled("smenabled", super_mirror, True) sm_angle = ReflectionAngle("smangle", super_mirror, True) slit2_pos = TrackingPosition("slit2pos", s2, True) sample_pos = TrackingPosition("samplepos", sample, True) theta = Theta("theta", sample, True) slit3_pos = TrackingPosition("slit3pos", s3, True) slit4_pos = TrackingPosition("slit4pos", s4, True) det = TrackingPosition("detpos", point_det, True) params = [ sm_enabled, sm_angle, slit2_pos, sample_pos, theta, slit3_pos, slit4_pos, det ] nr_inits = {"smenabled": False, "smangle": 0.0} pnr_inits = {"smenabled": True, "smangle": 0.5} nr_mode = BeamlineMode( "nr", [param for param in PARAMS_FIELDS.keys() if param is not "smangle"], nr_inits) pnr_mode = BeamlineMode("pnr", PARAMS_FIELDS.keys(), pnr_inits) disabled_mode = BeamlineMode("disabled", []) modes = [nr_mode, pnr_mode, disabled_mode] # init beamline bl = Beamline(comps, params, [], modes) bl.set_incoming_beam(beam_start) bl.active_mode = nr_mode return bl, modes
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_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 beamline_with_3_empty_parameters(): """ Returns: a beamline with three empty parameters, all in a mode """ one = EmptyBeamlineParameter("one") two = EmptyBeamlineParameter("two") three = EmptyBeamlineParameter("three") beamline_parameters = [one, two, three] mode = BeamlineMode("all", [ beamline_parameter.name for beamline_parameter in beamline_parameters ]) beamline = Beamline([], beamline_parameters, [], [mode]) beamline.active_mode = mode return beamline_parameters, beamline
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_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 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_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_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))
class TestComponentBeamline(unittest.TestCase): 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_beam_line_contains_multiple_component_WHEN_set_theta_THEN_angle_between_incoming_and_outgoing_beam_is_correct( self): self.beamline.active_mode = self.nr_mode theta_set = 10.0 self.beamline.parameter("theta").sp = theta_set reflection_angle = self.ideal_sample_point.get_outgoing_beam( ).angle - self.ideal_sample_point.incoming_beam.angle assert_that(reflection_angle, is_(theta_set * 2.0)) def test_GIVEN_beam_line_contains_active_super_mirror_WHEN_set_theta_THEN_angle_between_incoming_and_outgoing_beam_is_correct( self): self.beamline.active_mode = self.polarised_mode theta_set = 10.0 self.polarising_mirror.enabled = True self.beamline.parameter("smangle").sp = 10 self.beamline.parameter("theta").sp = theta_set reflection_angle = self.ideal_sample_point.get_outgoing_beam( ).angle - self.ideal_sample_point.incoming_beam.angle assert_that(reflection_angle, is_(theta_set * 2.0)) def test_GIVEN_beam_line_contains_active_super_mirror_WHEN_angle_set_THEN_angle_between_incoming_and_outgoing_beam_is_correct( self): self.beamline.active_mode = self.polarised_mode theta_set = 10.0 self.beamline.parameter("theta").sp = theta_set self.polarising_mirror.enabled = True self.beamline.parameter("smangle").sp = 10 reflection_angle = self.ideal_sample_point.get_outgoing_beam( ).angle - self.ideal_sample_point.incoming_beam.angle assert_that(reflection_angle, is_(theta_set * 2.0))
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)