def _get_measured(self, *names): x = self._instrument_x.field() y = self._instrument_y.field() z = self._instrument_z.field() measured_values = FieldVector(x=x, y=y, z=z).get_components(*names) # Convert angles from radians to degrees d = dict(zip(names, measured_values)) # Do not do "return list(d.values())", because then there is # no guaranty that the order in which the values are returned # is the same as the original intention return_value = [d[name] for name in names] if len(names) == 1: return_value = return_value[0] return return_value
def test_ramp_safely(driver, x, y, z, caplog): """ Test that we get the first-down-then-up order right """ # reset the instrument to default driver.GRPX.ramp_status('HOLD') driver.GRPY.ramp_status('HOLD') driver.GRPZ.ramp_status('HOLD') # the current field values are always zero for the sim. instr. # Use the FieldVector interface here to increase coverage. driver.field_target(FieldVector(x=x, y=y, z=z)) exp_order = \ np.array(['x', 'y', 'z'])[np.argsort(np.abs(np.array([x, y, z])))] with caplog.at_level(logging.DEBUG, logger='qcodes.instrument.visa'): caplog.clear() driver._ramp_safely() ramp_order = get_ramp_order(caplog.records) assert ramp_order == list(exp_order)
def _set_target(self, coordinate: str, target: float) -> None: """ The function to set a target value for a coordinate, i.e. the set_cmd for the XXX_target parameters """ # first validate the new target valid_vec = FieldVector() valid_vec.copy(self._target_vector) valid_vec.set_component(**{coordinate: target}) components = valid_vec.get_components('x', 'y', 'z') if not self._field_limits(*components): raise ValueError(f'Cannot set {coordinate} target to {target}, ' 'that would violate the field_limits. ') # update our internal target cache self._target_vector.set_component(**{coordinate: target}) # actually assign the target on the slaves cartesian_targ = self._target_vector.get_components('x', 'y', 'z') for targ, slave in zip(cartesian_targ, self.submodules.values()): slave.field_target(targ)
def test_measured(current_driver, set_target): """ Simply call the measurement methods and verify that no exceptions are raised. """ current_driver.cartesian(set_target) cartesian = current_driver.cartesian_measured() cartesian_x = current_driver.x_measured() cartesian_y = current_driver.y_measured() cartesian_z = current_driver.z_measured() assert np.allclose(cartesian, [cartesian_x, cartesian_y, cartesian_z]) assert FieldVector(*set_target).is_equal(FieldVector(x=cartesian_x, y=cartesian_y, z=cartesian_z)) spherical = current_driver.spherical_measured() spherical_field = current_driver.field_measured() spherical_theta = current_driver.theta_measured() spherical_phi = current_driver.phi_measured() assert FieldVector(*set_target).is_equal(FieldVector( r=spherical_field, theta=spherical_theta, phi=spherical_phi) ) assert np.allclose(spherical, [spherical_field, spherical_theta, spherical_phi]) cylindrical = current_driver.cylindrical_measured() cylindrical_rho = current_driver.rho_measured() assert FieldVector(*set_target).is_equal(FieldVector(rho=cylindrical_rho, phi=spherical_phi, z=cartesian_z)) assert np.allclose(cylindrical, [cylindrical_rho, spherical_phi, cartesian_z])
def test_vector_setting(driver): assert driver.field_target().distance(FieldVector(0, 0, 0)) <= 1e-8 driver.field_target(FieldVector(r=0.1, theta=0, phi=0)) assert driver.field_target().distance(FieldVector(r=0.1, theta=0, phi=0)) <= 1e-8
def _get_field(self) -> FieldVector: return FieldVector(x=self.x_measured(), y=self.y_measured(), z=self.z_measured())
def _get_ramp_rate(self) -> FieldVector: return FieldVector( x=self.GRPX.field_ramp_rate(), y=self.GRPY.field_ramp_rate(), z=self.GRPZ.field_ramp_rate(), )
def _get_target_field(self) -> FieldVector: return FieldVector( **{coord: self._get_component(coord) for coord in 'xyz'})
def __init__(self, name: str, address: str, visalib=None, field_limits: Optional[Callable[[float, float, float], bool]] = None, **kwargs) -> None: """ Args: name: The name to give this instrument internally in QCoDeS address: The VISA resource of the instrument. Note that a socket connection to port 7020 must be made visalib: The VISA library to use. Leave blank if not in simulation mode. field_limits: A function describing the allowed field range (T). The function shall take (x, y, z) as an input and return a boolean describing whether that field value is acceptable. """ if field_limits is not None and not (callable(field_limits)): raise ValueError('Got wrong type of field_limits. Must be a ' 'function from (x, y, z) -> Bool. Received ' f'{type(field_limits)} instead.') if visalib: visabackend = visalib.split('@')[1] else: visabackend = 'NI' # ensure that a socket is used unless we are in simulation mode if not address.endswith('SOCKET') and visabackend != 'sim': raise ValueError('Incorrect VISA resource name. Must be of type ' 'TCPIP0::XXX.XXX.XXX.XXX::7020::SOCKET.') super().__init__(name, address, terminator='\n', visalib=visalib, **kwargs) # to ensure a correct snapshot, we must wrap the get function self.IDN.get = self.IDN._wrap_get(self._idn_getter) self.firmware = self.IDN()['firmware'] # TODO: Query instrument to ensure which PSUs are actually present for grp in ['GRPX', 'GRPY', 'GRPZ']: psu_name = grp psu = MercurySlavePS(self, psu_name, grp) self.add_submodule(psu_name, psu) self._field_limits = (field_limits if field_limits else lambda x, y, z: True) self._target_vector = FieldVector(x=self.GRPX.field(), y=self.GRPY.field(), z=self.GRPZ.field()) for coord, unit in zip( ['x', 'y', 'z', 'r', 'theta', 'phi', 'rho'], ['T', 'T', 'T', 'T', 'degrees', 'degrees', 'T']): self.add_parameter(name=f'{coord}_target', label=f'{coord.upper()} target field', unit=unit, get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target, coord)) self.add_parameter(name=f'{coord}_measured', label=f'{coord.upper()} measured field', unit=unit, get_cmd=partial(self._get_measured, [coord])) self.add_parameter(name=f'{coord}_ramp', label=f'{coord.upper()} ramp field', unit=unit, docstring='A safe ramp for each coordinate', get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target_and_ramp, coord, 'safe')) if coord in ['r', 'theta', 'phi', 'rho']: self.add_parameter( name=f'{coord}_simulramp', label=f'{coord.upper()} ramp field', unit=unit, docstring='A simultaneous blocking ramp for a ' 'combined coordinate', get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target_and_ramp, coord, 'simul_block')) # FieldVector-valued parameters # self.add_parameter(name="field_target", label="target field", unit="T", get_cmd=self._get_target_field, set_cmd=self._set_target_field) self.add_parameter(name="field_measured", label="measured field", unit="T", get_cmd=self._get_field) self.add_parameter(name="field_ramp_rate", label="ramp rate", unit="T/s", get_cmd=self._get_ramp_rate, set_cmd=self._set_ramp_rate) self.connect_message()
class MercuryiPS(VisaInstrument): """ Driver class for the QCoDeS Oxford Instruments MercuryiPS magnet power supply """ def __init__(self, name: str, address: str, visalib=None, field_limits: Optional[Callable[[float, float, float], bool]] = None, **kwargs) -> None: """ Args: name: The name to give this instrument internally in QCoDeS address: The VISA resource of the instrument. Note that a socket connection to port 7020 must be made visalib: The VISA library to use. Leave blank if not in simulation mode. field_limits: A function describing the allowed field range (T). The function shall take (x, y, z) as an input and return a boolean describing whether that field value is acceptable. """ if field_limits is not None and not (callable(field_limits)): raise ValueError('Got wrong type of field_limits. Must be a ' 'function from (x, y, z) -> Bool. Received ' f'{type(field_limits)} instead.') if visalib: visabackend = visalib.split('@')[1] else: visabackend = 'NI' # ensure that a socket is used unless we are in simulation mode if not address.endswith('SOCKET') and visabackend != 'sim': raise ValueError('Incorrect VISA resource name. Must be of type ' 'TCPIP0::XXX.XXX.XXX.XXX::7020::SOCKET.') super().__init__(name, address, terminator='\n', visalib=visalib, **kwargs) # to ensure a correct snapshot, we must wrap the get function self.IDN.get = self.IDN._wrap_get(self._idn_getter) self.firmware = self.IDN()['firmware'] # TODO: Query instrument to ensure which PSUs are actually present for grp in ['GRPX', 'GRPY', 'GRPZ']: psu_name = grp psu = MercurySlavePS(self, psu_name, grp) self.add_submodule(psu_name, psu) self._field_limits = (field_limits if field_limits else lambda x, y, z: True) self._target_vector = FieldVector(x=self.GRPX.field(), y=self.GRPY.field(), z=self.GRPZ.field()) for coord, unit in zip( ['x', 'y', 'z', 'r', 'theta', 'phi', 'rho'], ['T', 'T', 'T', 'T', 'degrees', 'degrees', 'T']): self.add_parameter(name=f'{coord}_target', label=f'{coord.upper()} target field', unit=unit, get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target, coord)) self.add_parameter(name=f'{coord}_measured', label=f'{coord.upper()} measured field', unit=unit, get_cmd=partial(self._get_measured, [coord])) self.add_parameter(name=f'{coord}_ramp', label=f'{coord.upper()} ramp field', unit=unit, docstring='A safe ramp for each coordinate', get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target_and_ramp, coord, 'safe')) if coord in ['r', 'theta', 'phi', 'rho']: self.add_parameter( name=f'{coord}_simulramp', label=f'{coord.upper()} ramp field', unit=unit, docstring='A simultaneous blocking ramp for a ' 'combined coordinate', get_cmd=partial(self._get_component, coord), set_cmd=partial(self._set_target_and_ramp, coord, 'simul_block')) # FieldVector-valued parameters # self.add_parameter(name="field_target", label="target field", unit="T", get_cmd=self._get_target_field, set_cmd=self._set_target_field) self.add_parameter(name="field_measured", label="measured field", unit="T", get_cmd=self._get_field) self.add_parameter(name="field_ramp_rate", label="ramp rate", unit="T/s", get_cmd=self._get_ramp_rate, set_cmd=self._set_ramp_rate) self.connect_message() def _get_component(self, coordinate: str) -> float: return self._target_vector.get_components(coordinate)[0] def _get_target_field(self) -> FieldVector: return FieldVector( **{coord: self._get_component(coord) for coord in 'xyz'}) def _get_ramp_rate(self) -> FieldVector: return FieldVector( x=self.GRPX.field_ramp_rate(), y=self.GRPY.field_ramp_rate(), z=self.GRPZ.field_ramp_rate(), ) def _set_ramp_rate(self, rate: FieldVector) -> None: self.GRPX.field_ramp_rate(rate.x) self.GRPY.field_ramp_rate(rate.y) self.GRPZ.field_ramp_rate(rate.z) def _get_measured(self, coordinates: List[str]) -> Union[float, List[float]]: """ Get the measured value of a coordinate. Measures all three fields and computes whatever coordinate we asked for. """ meas_field = FieldVector(x=self.GRPX.field(), y=self.GRPY.field(), z=self.GRPZ.field()) if len(coordinates) == 1: return meas_field.get_components(*coordinates)[0] else: return meas_field.get_components(*coordinates) def _get_field(self) -> FieldVector: return FieldVector(x=self.x_measured(), y=self.y_measured(), z=self.z_measured()) def _set_target(self, coordinate: str, target: float) -> None: """ The function to set a target value for a coordinate, i.e. the set_cmd for the XXX_target parameters """ # first validate the new target valid_vec = FieldVector() valid_vec.copy(self._target_vector) valid_vec.set_component(**{coordinate: target}) components = valid_vec.get_components('x', 'y', 'z') if not self._field_limits(*components): raise ValueError(f'Cannot set {coordinate} target to {target}, ' 'that would violate the field_limits. ') # update our internal target cache self._target_vector.set_component(**{coordinate: target}) # actually assign the target on the slaves cartesian_targ = self._target_vector.get_components('x', 'y', 'z') for targ, slave in zip(cartesian_targ, self.submodules.values()): slave.field_target(targ) def _set_target_field(self, field: FieldVector) -> None: for coord in 'xyz': self._set_target(coord, field[coord]) def _idn_getter(self) -> Dict[str, str]: """ Parse the raw non-SCPI compliant IDN string into an IDN dict Returns: The normal IDN dict """ raw_idn_string = self.ask('*IDN?') resps = raw_idn_string.split(':') idn_dict = { 'model': resps[2], 'vendor': resps[1], 'serial': resps[3], 'firmware': resps[4] } return idn_dict def _ramp_simultaneously(self) -> None: """ Ramp all three fields to their target simultaneously at their given ramp rates. NOTE: there is NO guarantee that this does not take you out of your safe region. Use with care. """ for slave in self.submodules.values(): slave.ramp_to_target() def _ramp_simultaneously_blocking(self) -> None: """ Ramp all three fields to their target simultaneously at their given ramp rates. NOTE: there is NO guarantee that this does not take you out of your safe region. Use with care. This function is BLOCKING. """ self._ramp_simultaneously() for slave in self.submodules.values(): # wait for the ramp to finish, we don't care about the order while slave.ramp_status() == 'TO SET': time.sleep(0.1) self.update_field() def _ramp_safely(self) -> None: """ Ramp all three fields to their target using the 'first-down-then-up' sequential ramping procedure. This function is BLOCKING. """ meas_vals = self._get_measured(['x', 'y', 'z']) targ_vals = self._target_vector.get_components('x', 'y', 'z') order = np.argsort(np.abs(np.array(targ_vals) - np.array(meas_vals))) for slave in np.array(list(self.submodules.values()))[order]: slave.ramp_to_target() # now just wait for the ramp to finish # (unless we are testing) if self.visabackend == 'sim': pass else: while slave.ramp_status() == 'TO SET': time.sleep(0.1) self.update_field() def update_field(self) -> None: """ Update all the field components. """ coords = ['x', 'y', 'z', 'r', 'theta', 'phi', 'rho'] meas_field = self._get_field() [getattr(self, f'{i}_measured').get() for i in coords] def is_ramping(self) -> bool: """ Returns True if any axis has a ramp status that is either 'TO SET' or 'TO ZERO' """ ramping_statuus = ['TO SET', 'TO ZERO'] is_x_ramping = self.GRPX.ramp_status() in ramping_statuus is_y_ramping = self.GRPY.ramp_status() in ramping_statuus is_z_ramping = self.GRPZ.ramp_status() in ramping_statuus return is_x_ramping or is_y_ramping or is_z_ramping def set_new_field_limits(self, limit_func: Callable) -> None: """ Assign a new field limit function to the driver Args: limit_func: must be a function mapping (Bx, By, Bz) -> True/False where True means that the field is INSIDE the allowed region """ # first check that the current target is allowed if not limit_func(*self._target_vector.get_components('x', 'y', 'z')): raise ValueError('Can not assign new limit function; present ' 'target is illegal. Please change the target ' 'and try again.') self._field_limits = limit_func def ramp(self, mode: str = "safe") -> None: """ Ramp the fields to their present target value Args: mode: how to ramp, either 'simul', 'simul-block' or 'safe'. In 'simul' and 'simul-block' mode, the fields are ramping simultaneously in a non-blocking mode and blocking mode, respectively. There is no safety check that the safe zone is not exceeded. In 'safe' mode, the fields are ramped one-by-one in a blocking way that ensures that the total field stays within the safe region (provided that this region is convex). """ if mode not in ['simul', 'safe', 'simul_block']: raise ValueError('Invalid ramp mode. Please provide either "simul"' ',"safe" or "simul_block".') meas_vals = self._get_measured(['x', 'y', 'z']) # we asked for three coordinates, so we know that we got a list meas_vals = cast(List[float], meas_vals) for cur, slave in zip(meas_vals, self.submodules.values()): if slave.field_target() != cur: if slave.field_ramp_rate() == 0: raise ValueError(f'Can not ramp {slave}; ramp rate set to' ' zero!') # then the actual ramp { 'simul': self._ramp_simultaneously, 'safe': self._ramp_safely, 'simul_block': self._ramp_simultaneously_blocking }[mode]() def _set_target_and_ramp(self, coordinate: str, mode: str, target: float) -> None: """Convenient method to combine setting target and ramping""" self._set_target(coordinate, target) self.ramp(mode) def ask(self, cmd: str) -> str: """ Since Oxford Instruments implement their own version of a SCPI-like language, we implement our own reader. Note that this command is used for getting and setting (asking and writing) alike. Args: cmd: the command to send to the instrument """ visalog.debug(f"Writing to instrument {self.name}: {cmd}") resp = self.visa_handle.query(cmd) visalog.debug(f"Got instrument response: {resp}") if 'INVALID' in resp: log.error('Invalid command. Got response: {}'.format(resp)) base_resp = resp # if the command was not invalid, it can either be a SET or a READ # SET: elif resp.endswith('VALID'): base_resp = resp.split(':')[-2] # READ: else: # For "normal" commands only (e.g. '*IDN?' is excepted): # the response of a valid command echoes back said command, # thus we remove that part base_cmd = cmd.replace('READ:', '') base_resp = resp.replace('STAT:{}'.format(base_cmd), '') return base_resp
def shift(offset: FieldVector) -> np.ndarray: mtx = np.eye(4) mtx[:, -1] = offset.as_homogeneous() return mtx
def test_vector_ramp_rate(driver): driver.field_ramp_rate(FieldVector(0.1, 0.1, 0.1)) assert driver.field_ramp_rate().distance(FieldVector(0.1, 0.1, 0.1)) <= 1e-8
def __init__(self, name, instrument_x, instrument_y, instrument_z, field_limit: Union[numbers.Real, Iterable[CartesianFieldLimitFunction]], **kwargs): super().__init__(name, **kwargs) if not isinstance(name, str): raise ValueError("Name should be a string") instruments = [instrument_x, instrument_y, instrument_z] if not all( [isinstance(instrument, AMI430) for instrument in instruments]): raise ValueError("Instruments need to be instances " "of the class AMI430") self._instrument_x = instrument_x self._instrument_y = instrument_y self._instrument_z = instrument_z self._field_limit: Union[float, Iterable[CartesianFieldLimitFunction]] if isinstance(field_limit, collections.abc.Iterable): self._field_limit = field_limit elif isinstance(field_limit, numbers.Real): # Convertion to float makes related driver logic simpler self._field_limit = float(field_limit) else: raise ValueError("field limit should either be a number or " "an iterable of callable field limit functions.") self._set_point = FieldVector(x=self._instrument_x.field(), y=self._instrument_y.field(), z=self._instrument_z.field()) # Get-only parameters that return a measured value self.add_parameter('cartesian_measured', get_cmd=partial(self._get_measured, 'x', 'y', 'z'), unit='T') self.add_parameter('x_measured', get_cmd=partial(self._get_measured, 'x'), unit='T') self.add_parameter('y_measured', get_cmd=partial(self._get_measured, 'y'), unit='T') self.add_parameter('z_measured', get_cmd=partial(self._get_measured, 'z'), unit='T') self.add_parameter('spherical_measured', get_cmd=partial(self._get_measured, 'r', 'theta', 'phi'), unit='T') self.add_parameter('phi_measured', get_cmd=partial(self._get_measured, 'phi'), unit='deg') self.add_parameter('theta_measured', get_cmd=partial(self._get_measured, 'theta'), unit='deg') self.add_parameter('field_measured', get_cmd=partial(self._get_measured, 'r'), unit='T') self.add_parameter('cylindrical_measured', get_cmd=partial(self._get_measured, 'rho', 'phi', 'z'), unit='T') self.add_parameter('rho_measured', get_cmd=partial(self._get_measured, 'rho'), unit='T') # Get and set parameters for the set points of the coordinates self.add_parameter('cartesian', get_cmd=partial(self._get_setpoints, ('x', 'y', 'z')), set_cmd=partial(self._set_setpoints, ('x', 'y', 'z')), unit='T', vals=Anything()) self.add_parameter('x', get_cmd=partial(self._get_setpoints, ('x', )), set_cmd=partial(self._set_setpoints, ('x', )), unit='T', vals=Numbers()) self.add_parameter('y', get_cmd=partial(self._get_setpoints, ('y', )), set_cmd=partial(self._set_setpoints, ('y', )), unit='T', vals=Numbers()) self.add_parameter('z', get_cmd=partial(self._get_setpoints, ('z', )), set_cmd=partial(self._set_setpoints, ('z', )), unit='T', vals=Numbers()) self.add_parameter('spherical', get_cmd=partial(self._get_setpoints, ('r', 'theta', 'phi')), set_cmd=partial(self._set_setpoints, ('r', 'theta', 'phi')), unit='tuple?', vals=Anything()) self.add_parameter('phi', get_cmd=partial(self._get_setpoints, ('phi', )), set_cmd=partial(self._set_setpoints, ('phi', )), unit='deg', vals=Numbers()) self.add_parameter('theta', get_cmd=partial(self._get_setpoints, ('theta', )), set_cmd=partial(self._set_setpoints, ('theta', )), unit='deg', vals=Numbers()) self.add_parameter('field', get_cmd=partial(self._get_setpoints, ('r', )), set_cmd=partial(self._set_setpoints, ('r', )), unit='T', vals=Numbers()) self.add_parameter('cylindrical', get_cmd=partial(self._get_setpoints, ('rho', 'phi', 'z')), set_cmd=partial(self._set_setpoints, ('rho', 'phi', 'z')), unit='tuple?', vals=Anything()) self.add_parameter('rho', get_cmd=partial(self._get_setpoints, ('rho', )), set_cmd=partial(self._set_setpoints, ('rho', )), unit='T', vals=Numbers()) self.add_parameter('block_during_ramp', set_cmd=None, initial_value=True, unit='', vals=Bool())
class AMI430_3D(Instrument): def __init__(self, name, instrument_x, instrument_y, instrument_z, field_limit, **kwargs): super().__init__(name, **kwargs) if not isinstance(name, str): raise ValueError("Name should be a string") instruments = [instrument_x, instrument_y, instrument_z] if not all( [isinstance(instrument, AMI430) for instrument in instruments]): raise ValueError("Instruments need to be instances " "of the class AMI430") self._instrument_x = instrument_x self._instrument_y = instrument_y self._instrument_z = instrument_z if repr(field_limit).isnumeric() or isinstance(field_limit, collections.Iterable): self._field_limit = field_limit else: raise ValueError("field limit should either be" " a number or an iterable") self._set_point = FieldVector(x=self._instrument_x.field(), y=self._instrument_y.field(), z=self._instrument_z.field()) # Get-only parameters that return a measured value self.add_parameter('cartesian_measured', get_cmd=partial(self._get_measured, 'x', 'y', 'z'), unit='T') self.add_parameter('x_measured', get_cmd=partial(self._get_measured, 'x'), unit='T') self.add_parameter('y_measured', get_cmd=partial(self._get_measured, 'y'), unit='T') self.add_parameter('z_measured', get_cmd=partial(self._get_measured, 'z'), unit='T') self.add_parameter('spherical_measured', get_cmd=partial(self._get_measured, 'r', 'theta', 'phi'), unit='T') self.add_parameter('phi_measured', get_cmd=partial(self._get_measured, 'phi'), unit='deg') self.add_parameter('theta_measured', get_cmd=partial(self._get_measured, 'theta'), unit='deg') self.add_parameter('field_measured', get_cmd=partial(self._get_measured, 'r'), unit='T') self.add_parameter('cylindrical_measured', get_cmd=partial(self._get_measured, 'rho', 'phi', 'z'), unit='T') self.add_parameter('rho_measured', get_cmd=partial(self._get_measured, 'rho'), unit='T') # Get and set parameters for the set points of the coordinates self.add_parameter('cartesian', get_cmd=partial(self._get_setpoints, 'x', 'y', 'z'), set_cmd=self._set_cartesian, unit='T', vals=Anything()) self.add_parameter('x', get_cmd=partial(self._get_setpoints, 'x'), set_cmd=self._set_x, unit='T', vals=Numbers()) self.add_parameter('y', get_cmd=partial(self._get_setpoints, 'y'), set_cmd=self._set_y, unit='T', vals=Numbers()) self.add_parameter('z', get_cmd=partial(self._get_setpoints, 'z'), set_cmd=self._set_z, unit='T', vals=Numbers()) self.add_parameter('spherical', get_cmd=partial(self._get_setpoints, 'r', 'theta', 'phi'), set_cmd=self._set_spherical, unit='tuple?', vals=Anything()) self.add_parameter('phi', get_cmd=partial(self._get_setpoints, 'phi'), set_cmd=self._set_phi, unit='deg', vals=Numbers()) self.add_parameter('theta', get_cmd=partial(self._get_setpoints, 'theta'), set_cmd=self._set_theta, unit='deg', vals=Numbers()) self.add_parameter('field', get_cmd=partial(self._get_setpoints, 'r'), set_cmd=self._set_r, unit='T', vals=Numbers()) self.add_parameter('cylindrical', get_cmd=partial(self._get_setpoints, 'rho', 'phi', 'z'), set_cmd=self._set_cylindrical, unit='tuple?', vals=Anything()) self.add_parameter('rho', get_cmd=partial(self._get_setpoints, 'rho'), set_cmd=self._set_rho, unit='T', vals=Numbers()) def _verify_safe_setpoint(self, setpoint_values): if repr(self._field_limit).isnumeric(): return np.linalg.norm(setpoint_values) < self._field_limit answer = any([ limit_function(*setpoint_values) for limit_function in self._field_limit ]) return answer def _set_fields(self, values): """ Set the fields of the x/y/z magnets. This function is called whenever the field is changed and performs several safety checks to make sure no limits are exceeded. Args: values (tuple): a tuple of cartesian coordinates (x, y, z). """ log.debug("Checking whether fields can be set") # Check if exceeding the global field limit if not self._verify_safe_setpoint(values): raise ValueError("_set_fields aborted; field would exceed limit") # Check if the individual instruments are ready for name, value in zip(["x", "y", "z"], values): instrument = getattr(self, "_instrument_{}".format(name)) if instrument.ramping_state() == "ramping": msg = '_set_fields aborted; magnet {} is already ramping' raise AMI430Exception(msg.format(instrument)) # Now that we know we can proceed, call the individual instruments log.debug("Field values OK, proceeding") for operator in [np.less, np.greater]: # First ramp the coils that are decreasing in field strength. # This will ensure that we are always in a safe region as # far as the quenching of the magnets is concerned for name, value in zip(["x", "y", "z"], values): instrument = getattr(self, "_instrument_{}".format(name)) current_actual = instrument.field() # If the new set point is practically equal to the # current one then do nothing if np.isclose(value, current_actual, rtol=0, atol=1e-8): continue # evaluate if the new set point is smaller or larger # than the current value if not operator(abs(value), abs(current_actual)): continue instrument.set_field(value, perform_safety_check=False) def _request_field_change(self, instrument, value): """ This method is called by the child x/y/z magnets if they are set individually. It results in additional safety checks being performed by this 3D driver. """ if instrument is self._instrument_x: self._set_x(value) elif instrument is self._instrument_y: self._set_y(value) elif instrument is self._instrument_z: self._set_z(value) else: msg = 'This magnet doesnt belong to its specified parent {}' raise NameError(msg.format(self)) def _get_measured(self, *names): x = self._instrument_x.field() y = self._instrument_y.field() z = self._instrument_z.field() measured_values = FieldVector(x=x, y=y, z=z).get_components(*names) # Convert angles from radians to degrees d = dict(zip(names, measured_values)) # Do not do "return list(d.values())", because then there is # no guaranty that the order in which the values are returned # is the same as the original intention return_value = [d[name] for name in names] if len(names) == 1: return_value = return_value[0] return return_value def _get_setpoints(self, *names): measured_values = self._set_point.get_components(*names) # Convert angles from radians to degrees d = dict(zip(names, measured_values)) return_value = [d[name] for name in names] # Do not do "return list(d.values())", because then there is # no guarantee that the order in which the values are returned # is the same as the original intention if len(names) == 1: return_value = return_value[0] return return_value def _set_cartesian(self, values): x, y, z = values self._set_point.set_vector(x=x, y=y, z=z) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_x(self, x): self._set_point.set_component(x=x) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_y(self, y): self._set_point.set_component(y=y) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_z(self, z): self._set_point.set_component(z=z) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_spherical(self, values): r, theta, phi = values self._set_point.set_vector(r=r, theta=theta, phi=phi) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_r(self, r): self._set_point.set_component(r=r) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_theta(self, theta): self._set_point.set_component(theta=theta) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_phi(self, phi): self._set_point.set_component(phi=phi) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_cylindrical(self, values): rho, phi, z = values self._set_point.set_vector(rho=rho, phi=phi, z=z) self._set_fields(self._set_point.get_components("x", "y", "z")) def _set_rho(self, rho): self._set_point.set_component(rho=rho) self._set_fields(self._set_point.get_components("x", "y", "z"))
def update(field: FieldVector): status.value = field.repr_spherical()
def set_field(self, target_field: FieldVector, n_steps: int = 10, absolute: bool = True, ramp_rate: float = 1e-3, observer_fn: Optional[Callable[[FieldVector], None]] = None, verbose: bool = False, threshold: float = 1e-6) -> None: """ Sets the field controlled by this problem's instrument to a given target field by taking small steps, measuring, and then updating the target accordingly. Args: target_field: The value of the field that should be set, or the difference between the current and target field if `absolute=True`. n_steps: The number of steps that should be taken in order to reach the given target. absolute: Indicates whether `target_field` is the target field, or a difference from the current field to the target. ramp_rate: A rate at which the field can be safely swept between points. observer_fn: A callable which gets called after each small step. threshold: If the norm of the difference between the current and target field is smaller than this value, no further steps will be taken. """ if IPYTHON and ipw is not None: status = ipw.Label() def update(field: FieldVector): status.value = field.repr_spherical() def finish(): status.value = "Move complete." display(status) else: def update(field: FieldVector): print( f'Magnet reached (r, phi, theta) = ({field.r}, {field.phi}, {field.theta})', end='\r') def finish(): print("Move complete.") target = FieldVector() if absolute: target.copy(target_field) else: initial = self.instrument.field_measured() target = target_field + initial with temporary_setting( self.instrument.field_ramp_rate, FieldVector(x=ramp_rate, y=ramp_rate, z=ramp_rate)): for step_amount in np.linspace(0, 1, n_steps + 1)[1:]: current = self.instrument.field_measured() intermediate_target = step_amount * (target - current) + current if (intermediate_target - current).norm() <= threshold: print("Step threshold met, stopping move early.") break if verbose: print( f"Setting field target to {intermediate_target.repr_spherical()}" ) self.instrument.field_target(intermediate_target) self.instrument.ramp() time.sleep(0.1) current = self.instrument.field_measured() if observer_fn is not None: observer_fn(current) update(current) time.sleep(0.1) finish()
def optimize_and_ramp_magnitude( self, initial_r: float, final_r: float, n_r_steps: int, initial_phi: float, phi_window: float, n_phis: int, initial_theta: float, theta_window: float, n_thetas: int, reoptimization_threshold: float = 0.5, n_steps: int = 5, ramp_rate: float = 1e-3, return_extra: bool = False, verbose: bool = False) -> OptionalExtra[FieldVector]: """ Ramps the magnitude of a magnetic field from a given start point, optimizing the objective by exhaustive search at each increment in field magnitude. Returns: The optimal field found by an exhaustive seach. If `return_extra=True`, this method returns a tuple of the optimal field and a dictionary containing diagnostic data. Args: initial_r: Initial magnitude of the magnetic field. final_r: Target magnitude of the magnetic field. n_r_steps: The number of steps to take in field magnitude. initial_phi: Initial value of phi to use in the exhaustive search at `initial_r`. phi_window: The size of the window around each value of phi to be searched. n_phis: The number of discrete values of phi to be searched at each field magnitude. initial_theta: Initial value of theta to use in the exhaustive search at `initial_r`. theta_window: The size of the window around each value of theta to be searched. n_thetas: The number of discrete values of theta to be searched at each field magnitude. reoptimization_threshold: If an increment in field magnitude changes the objective by more than this threshold, as scaled by the uncertainty, then an exhaustive search will be performed after incrementing. This is useful to avoid situations in which an objective value has changed by less than a "line width." return_extra: If `True`, this method will return additional data as a dictionary. Returns: The optimal field found by an exhaustive seach. If `return_extra=True`, this method returns a tuple of the optimal field and a dictionary containing diagnostic data. """ extra = {} print("Moving to initial field vector...") self.set_field(FieldVector(r=initial_r, phi=initial_phi, theta=initial_theta), absolute=True) rs = np.linspace(initial_r, final_r, n_r_steps) # Find the FWHM, so that we can compare line widths. prev_objective = None prev_uncertainty = None objective_history = [] optima_history = [] for r in rs: print(f"Optimizing at |B| = {r}...") current = self.instrument.field_measured() self.set_field(FieldVector(r=r, theta=current.theta, phi=current.phi), absolute=True, n_steps=n_steps, ramp_rate=ramp_rate, verbose=verbose) current_objective = self.objective() current_uncertainty = self.objective_uncertainty() objective_history.append(current_objective) # Check if we can skip this iteration. # We force optimization at the final iteration. if self.objective_uncertainty is not None and r != rs[ -1] and prev_objective is not None: scaled_distance = np.abs( current_objective - prev_objective) / np.mean( [prev_uncertainty, current_uncertainty]) if scaled_distance < reoptimization_threshold: prev_objective = current_objective prev_uncertainty = current_uncertainty print( f"Within {reoptimization_threshold} line widths, not re-optimizing yet." ) continue else: print( f"Current and previous objectives differ by {scaled_distance} line widths, thus re-optimizing." ) current_best = self.optimize_at_fixed_magnitude( r, (current.phi - phi_window / 2, current.phi + phi_window / 2), n_phis, (current.theta - theta_window / 2, current.theta + theta_window / 2), n_thetas, ramp_rate=ramp_rate, n_steps=n_steps, verbose=verbose) optima_history.append(current_best) # Find the FWHM at the point optimized by align_at, and save # to "prev" so that it's ready for the next iteration. if self.objective_uncertainty is not None: prev_objective = current_objective prev_uncertainty = current_uncertainty if return_extra: extra['history'] = { 'rs': rs, 'objectives': objective_history, 'optima': optima_history } return current_best, extra else: return current_best
def observe(current_field: FieldVector): new_loc = FieldVector() new_loc.copy(current_field) locations.append(new_loc) objectives_meas.append(self.objective())
def optimize_at_fixed_magnitude( self, r: float, phi_range: Interval[float], n_phi: int, theta_range: Interval[float], n_theta: int, return_extra: bool = False, n_steps: int = 5, plot: bool = False, verbose: bool = False, ramp_rate: float = 1.5e-3) -> OptionalExtra[FieldVector]: """ Given the magnitude of a magnetic field, maximizes the objective over the spherical coordinates phi and theta by an exhaustive search. Args: r: The magnitude of the magnetic field to be optimized over angles. phi_range: The interval over which phi will be searched. n_phi: The number of distinct values of phi to be evaluated. theta_range: The interval over which theta will be searched. n_theta: The number of distinct values of theta to be evaluated. return_extra: If `True`, this method will return additional data as a dictionary. plot: If `True`, produces a plot of the path that this method took to find the optimal objective value. Returns: The optimal field found by an exhaustive seach. If `return_extra=True`, this method returns a tuple of the optimal field and a dictionary containing diagnostic data. """ locations = [] objectives_meas = [] still_to_visit = [ FieldVector(r=r, phi=phi, theta=theta) for phi in np.linspace(phi_range[0], phi_range[1], n_phi) for theta in np.linspace(theta_range[0], theta_range[1], n_theta) ] def observe(current_field: FieldVector): new_loc = FieldVector() new_loc.copy(current_field) locations.append(new_loc) objectives_meas.append(self.objective()) while still_to_visit: # Find the nearest point. current = self.instrument.field_measured() nearest = min(still_to_visit, key=current.distance) still_to_visit.remove(nearest) print( f"Evaluating at phi = {nearest.phi}, theta = {nearest.theta}") self.set_field(nearest, absolute=True, n_steps=n_steps, ramp_rate=ramp_rate, observer_fn=observe, verbose=verbose) observe(self.instrument.field_measured()) extra = {'objectives': objectives_meas, 'field_vectors': locations} idx_flat_best = np.argmax(objectives_meas) optimum = FieldVector() optimum.copy(locations[idx_flat_best]) # Renormalize to desired B. optimum['r'] = r # Move the field before returning. print( f"Found optimum for |B| = {r} at ({optimum.phi}, {optimum.theta})." ) self.set_field(optimum, absolute=True) if plot: plt_xs = [vec.phi for vec in extra['field_vectors']] plt_ys = [vec.theta for vec in extra['field_vectors']] plt.figure() plt.plot( plt_xs, plt_ys, ) plt.scatter(plt_xs, plt_ys, c=extra['objectives']) plt.colorbar() if return_extra: return optimum, extra else: return optimum
def __init__(self, name, instrument_x, instrument_y, instrument_z, field_limit, **kwargs): super().__init__(name, **kwargs) if not isinstance(name, str): raise ValueError("Name should be a string") instruments = [instrument_x, instrument_y, instrument_z] if not all( [isinstance(instrument, AMI430) for instrument in instruments]): raise ValueError("Instruments need to be instances " "of the class AMI430") self._instrument_x = instrument_x self._instrument_y = instrument_y self._instrument_z = instrument_z if repr(field_limit).isnumeric() or isinstance(field_limit, collections.Iterable): self._field_limit = field_limit else: raise ValueError("field limit should either be" " a number or an iterable") self._set_point = FieldVector(x=self._instrument_x.field(), y=self._instrument_y.field(), z=self._instrument_z.field()) # Get-only parameters that return a measured value self.add_parameter('cartesian_measured', get_cmd=partial(self._get_measured, 'x', 'y', 'z'), unit='T') self.add_parameter('x_measured', get_cmd=partial(self._get_measured, 'x'), unit='T') self.add_parameter('y_measured', get_cmd=partial(self._get_measured, 'y'), unit='T') self.add_parameter('z_measured', get_cmd=partial(self._get_measured, 'z'), unit='T') self.add_parameter('spherical_measured', get_cmd=partial(self._get_measured, 'r', 'theta', 'phi'), unit='T') self.add_parameter('phi_measured', get_cmd=partial(self._get_measured, 'phi'), unit='deg') self.add_parameter('theta_measured', get_cmd=partial(self._get_measured, 'theta'), unit='deg') self.add_parameter('field_measured', get_cmd=partial(self._get_measured, 'r'), unit='T') self.add_parameter('cylindrical_measured', get_cmd=partial(self._get_measured, 'rho', 'phi', 'z'), unit='T') self.add_parameter('rho_measured', get_cmd=partial(self._get_measured, 'rho'), unit='T') # Get and set parameters for the set points of the coordinates self.add_parameter('cartesian', get_cmd=partial(self._get_setpoints, 'x', 'y', 'z'), set_cmd=self._set_cartesian, unit='T', vals=Anything()) self.add_parameter('x', get_cmd=partial(self._get_setpoints, 'x'), set_cmd=self._set_x, unit='T', vals=Numbers()) self.add_parameter('y', get_cmd=partial(self._get_setpoints, 'y'), set_cmd=self._set_y, unit='T', vals=Numbers()) self.add_parameter('z', get_cmd=partial(self._get_setpoints, 'z'), set_cmd=self._set_z, unit='T', vals=Numbers()) self.add_parameter('spherical', get_cmd=partial(self._get_setpoints, 'r', 'theta', 'phi'), set_cmd=self._set_spherical, unit='tuple?', vals=Anything()) self.add_parameter('phi', get_cmd=partial(self._get_setpoints, 'phi'), set_cmd=self._set_phi, unit='deg', vals=Numbers()) self.add_parameter('theta', get_cmd=partial(self._get_setpoints, 'theta'), set_cmd=self._set_theta, unit='deg', vals=Numbers()) self.add_parameter('field', get_cmd=partial(self._get_setpoints, 'r'), set_cmd=self._set_r, unit='T', vals=Numbers()) self.add_parameter('cylindrical', get_cmd=partial(self._get_setpoints, 'rho', 'phi', 'z'), set_cmd=self._set_cylindrical, unit='tuple?', vals=Anything()) self.add_parameter('rho', get_cmd=partial(self._get_setpoints, 'rho'), set_cmd=self._set_rho, unit='T', vals=Numbers())
def _set(field: FieldVector): transformed = inverse @ field.as_homogeneous() underlying_parameter(FieldVector.from_homogeneous(transformed))