def __init__(self, name, ccs_name, s=0, ecs_origin=[0, 0, 0], ecs_e1=[1, 0, 0], ecs_e2=[0, 1, 0], ccs_out=None, ccs_origin=None, ccs_e1=None, screen_width=0.2, ds=0): #print(name, ccs_name, s, ecs_origin, ecs_e1, ecs_e2, ccs_out, ccs_e1, screen_width) self._type = 'ScreenZXS' self._name = name self._ccs_name = ccs_name self._s = s self._ecs_origin = cvector(ecs_origin) self._ds = ds self._e1 = cvector(ecs_e1) self._e2 = cvector(ecs_e2) self._ccs_out = ccs_out self._ccs_e1 = cvector(ccs_e1) self._ccs_origin = cvector(ccs_origin) self._screen_width = screen_width
def set_screens(self): e1 = self.e1_beg thetas = np.linspace(0, self._theta, self._n_screen) p_screen_a = np.zeros((3, len(thetas))) p_screen_b = np.zeros((3, len(thetas))) arc1_beg = self.p_beg + np.sign( self._theta) * (self._width / 2) * cvector(self.e1_beg) self._p_screen_a = get_arc(self._R - self._width / 2, arc1_beg, self.e1_beg, self._theta, npts=self._n_screen) arc2_beg = self.p_beg - np.sign( self._theta) * (self._width / 2) * cvector(self.e1_beg) self._p_screen_b = get_arc(self._R + self._width / 2, arc2_beg, self.e1_beg, self._theta, npts=self._n_screen) self._p_screen_center = get_arc(self._R, self.p_beg, self.e1_beg, self._theta, npts=self._n_screen) self._theta_screen = thetas self._s_screen = self.s_beg + np.linspace(0, self._length, self._n_screen)
def place(self, previous_element=Beg(), ds=0, ref_origin='end', element_origin='beg'): assert ds>=0, "Bending magnets must be added in beamline order (ds>=0)." assert ref_origin=='end' and element_origin=='beg', "Benging magnets are places wrt end of reference element to beg of bending element." self._ccs_beg = previous_element.ccs_end self._ccs_end = f'{self.name}_ccs_end' s = previous_element.s_end M = previous_element.M_end p = previous_element.p_end if(is_bend(previous_element)): self._ccs_beg_origin = p else: self._ccs_beg_origin = previous_element.ccs_beg_origin e1 = cvector(M[:,0]) e3 = cvector(M[:,2]) dM = rotation_matrix(self._theta) self._s_beg = s + ds self._p_beg = p + ds*e3 self._M_beg = M self._p_end = self.p_beg +np.sign(self._theta)*self._R*(e1-np.matmul(dM,e1)) self._M_end = np.matmul(dM, M) self._s_end = self._s_beg + np.abs(rad(self._theta))*self._R self._ds = np.linalg.norm(self._p_beg - self.ccs_beg_origin) self.set_ref_trajectory() self.set_pole_faces()
def plot_floor(self, axis='equal', ax=None, alpha=1): if(ax == None): ax = plt.gca() e1_beg = cvector(self._M_beg[:,0]) arc1_beg = self.p_beg + np.sign(self._theta)*(self._width/2)*cvector(e1_beg) arc1 = get_arc(self._R-self._width/2, arc1_beg, e1_beg, self._theta ) arc2_beg = self.p_beg - np.sign(self._theta)*(self._width/2)*cvector(e1_beg) arc2 = np.fliplr(get_arc(self._R+self._width/2, arc2_beg, e1_beg, self._theta )) ps = np.concatenate( (arc1_beg, arc1, arc2, arc1_beg), axis=1) ax.plot(ps[2], ps[0], self.color, alpha=alpha) if(self._plot_pole_faces): ax.plot(self._pole_face_beg[2], self._pole_face_beg[0], color='k', alpha=alpha) ax.plot(self._pole_face_end[2], self._pole_face_end[0], color='k', alpha=alpha) ax.set_xlabel('z (m)') ax.set_ylabel('x (m)') if(axis=='equal'): ax.set_aspect('equal') return ax
def plot_floor(self, axis=None, alpha=1.0, ax=None): if(ax == None): ax = plt.gca() p1 = self.p_beg + (self._width/2)*cvector(self._M_beg[:,0]) p2 = self.p_beg - (self._width/2)*cvector(self._M_beg[:,0]) p3 = self.p_end + (self._width/2)*cvector(self._M_end[:,0]) p4 = self.p_end - (self._width/2)*cvector(self._M_end[:,0]) ps = np.concatenate( (p1, p3, p4, p2, p1), axis=1) ax.plot(ps[2], ps[0], self.color, alpha=alpha ) ax.set_xlabel('z (m)') ax.set_ylabel('x (m)') if(axis=='equal'): ax.set_aspect('equal')
def __init__(self, s=0, origin=[0,0,0], angles=[0,0,0]): self._M_beg = rotation_matrix(angles[0], angles[1], angles[2]) super().__init__('beg', angles=[0,0,0]) self._type = 'lattice starting element' self._M_beg = rotation_matrix(angles[0], angles[1], angles[2]) self._M_end = self._M_beg self._s_beg = s self._s_end = s self._p_beg = cvector(origin) self._p_end = cvector(origin) self.set_ref_trajectory() self._ccs_beg = 'wcs' self._ccs_end = 'wcs' self._ccs_beg_origin = cvector(origin)
def set_ref_trajectory(self, npts=100): """This sets the reference trajectory of the bend, user can supply how many points for generating the 3d curve""" self._s_ref = np.linspace(self._s_beg, self._s_end, npts) angles = np.linspace(0, self._theta, npts) self._p_ref = self._p_beg for ii, s in enumerate(self._s_ref[1:]): Mii = np.matmul(rotation_matrix(theta=angles[ii]), self._M_beg) e1ii = cvector(Mii[:,0]) ps = self._p_beg +np.sign(self._theta)*self._R*(self.e1_beg-e1ii) self._p_ref = np.concatenate( (self._p_ref, ps) , axis=1)
def set_ref_trajectory(self, npts=100): """ Get points to visualize the reference (geometric) trajectory """ if(self._s_beg!=self._s_end): self._s_ref = np.linspace(self._s_beg, self._s_end, npts) self._p_ref = self._p_beg for s in self._s_ref[1:]: ps = self._p_beg + (s-self._s_beg)*cvector(self._M_beg[:,2]) self._p_ref = np.concatenate( (self._p_ref, ps) , axis=1) else: self._s_ref = np.array([self._s_beg, self._s_end]) self._p_ref = np.concatenate( (self._p_beg, self._p_end) , axis=1)
def e2_end(self): return cvector(self._M_end[:,1])
def gpt_lines(self, ccs=None, gdf_file=None, e1=[1, 0, 0], e2=[0, 1, 0], scale=None, user_vars=[]): """ Creates ASCII lines defininig field map element in GPT syntax """ element = self._name if (ccs is None): ccs = self.ccs_beg ds = in_ecs(self._p_beg, self._ccs_beg_origin, self.M_beg)[2] ccs_beg_e3 = cvector([0, 0, 1]) r = ds * ccs_beg_e3 map_line = f'{self.type}("{self.ccs_beg}", ' extra_lines = {} for ii, coordinate in enumerate(['x', 'y', 'z']): #if(coordinate in user_vars): if (coordinate == 'z'): val = r[ii][0] - self['z'][0] #print(self['z'][0]) else: val = r[ii][0] extra_lines[coordinate] = f'{element}_{coordinate} = {val};' map_line = map_line + f'{element}_{coordinate}, ' #else: # map_line = map_line + f'{str(r[ii][0])}, ' for ii, m in enumerate(['e11', 'e12', 'e13']): if (m in user_vars): extra_lines[m] = f'{element}_{m} = {e1[ii]};' map_line = map_line + f'{element}_{m}, ' else: map_line = map_line + f'{str(e1[ii])}, ' for ii, m in enumerate(['e21', 'e22', 'e23']): if (m in user_vars): extra_lines[m] = f'{element}_{m} = {e2[ii]};' map_line = map_line + f'{element}_{m}, ' else: map_line = map_line + f'{str(e2[ii])}, ' if (gdf_file is None): gdf_file = self.source_data_file map_line = map_line + f'"{gdf_file}", ' for ii, rc in enumerate(self.required_columns): name = self.gpt_label_to_fieldmap_label(rc) map_line = map_line + f'"{name}", ' if (scale is None): scale = self._scale #if('scale' in user_vars): extra_lines['scale'] = f'{element}_scale = {scale};' map_line = map_line + f'{element}_scale);' #else: # map_line = map_line + f'{scale});' return [extra_line for extra_line in extra_lines.values()] + [map_line]
def e3_end(self): return cvector(self._M_end[:,2])
def gpt_lines(self): lines = [] bname = self.name lines = lines + [f'\n#***********************************************'] lines = lines + [f'# Sectorbend: {self.name} '] lines = lines + [f'#***********************************************'] exit_ccs_line = f'\nccs("{self.ccs_beg}", {self.name}_end_x, {bname}_end_y, {bname}_end_z' if (self.ccs_beg == 'wcs'): M = np.linalg.inv(self.M_end) lines = lines + [f'{bname}_end_x = {self.p_end[0][0]};'] lines = lines + [f'{bname}_end_y = {self.p_end[1][0]};'] lines = lines + [f'{bname}_end_z = {self.p_end[2][0]};'] exit_ccs_line = exit_ccs_line + f', {M[0,0]}, {M[0,1]}, {M[0,2]}, 0, 1, 0, "{self.ccs_end}");' else: ds = np.linalg.norm((self._p_beg - self._ccs_beg_origin)) ccs_beg_e1 = cvector([1, 0, 0]) ccs_beg_e3 = cvector([0, 0, 1]) p_beg_ccs = ds * ccs_beg_e3 dM = rotation_matrix(self._theta) p_end_ccs = p_beg_ccs + np.sign(self._theta) * self.R * ( ccs_beg_e1 - np.matmul(dM, ccs_beg_e1)) lines = lines + [f'{bname}_end_x = {p_end_ccs[0][0]};'] lines = lines + [f'{bname}_end_y = {p_end_ccs[1][0]};'] lines = lines + [f'{bname}_end_z = {p_end_ccs[2][0]};'] dM_inv = np.linalg.inv(dM) exit_ccs_line = exit_ccs_line + f', {dM_inv[0,0]}, {dM_inv[0,1]}, {dM_inv[0,2]}, 0, 1, 0, "{self.ccs_end}");' lines = lines + [exit_ccs_line + '\n'] lines = lines + [f'{bname}_radius = {self._R};'] lines = lines + [f'{bname}_Bfield = {self._B};'] lines = lines + [f'{bname}_phi_in = {self.phi_in};'] lines = lines + [f'{bname}_phi_out = {self.phi_out};'] lines = lines + [f'{bname}_fringe_dl = {self.dl};'] lines = lines + [f'{bname}_fringe_b1 = {self.b1};'] lines = lines + [f'{bname}_fringe_b2 = {self.b2};'] if (self._fix): btype = 'sectormagnet_fix' else: btype = 'sectormagnet' bend_line = f'\n{btype}("{self.ccs_beg}", "{self.ccs_end}"' bend_line = bend_line + f', {bname}_radius, {bname}_Bfield, {bname}_phi_in/deg, {bname}_phi_out/deg' bend_line = bend_line + f', {bname}_fringe_dl, {bname}_fringe_b1, {bname}_fringe_b2);' lines = lines + [bend_line] p_end_ccs_beg = in_ecs(self.p_end, self._ccs_beg_origin, self.M_beg) for ii, theta in enumerate(self._theta_screen): dM = rotation_matrix(theta) pii_ccs_beg = in_ecs(cvector(self.p_screen_center[:, ii]), self._ccs_beg_origin, self.M_beg) if (np.abs(theta) <= np.abs(self._theta) / 2.0): ccs_line = f'ccs("{self.ccs_beg}", {write_ecs(pii_ccs_beg, dM)}"{self.name}_scr_ccs_{ii+1}");' lines.append(ccs_line) scr_line = f'screen("{self.ccs_beg}", {write_ecs(pii_ccs_beg, dM)}0, "{self.name}_scr_ccs_{ii+1}");' lines.append(scr_line) if (np.abs(theta) >= np.abs(self._theta) / 2.0): pii_ccs_end = in_ecs(pii_ccs_beg, p_end_ccs_beg, rotation_matrix(self._theta)) Mii = np.matmul( dM, np.matmul(np.linalg.inv(self.M_end), self.M_beg)) #print(pii_ccs_end.T, Mii[:,0].T, Mii[:,2].T) #print(self.name, Mii[:,0].T, Mii[:,2].T) #Mii = np.matmul(dM, self.M_end) #Mii = np.linalg.inv(np.matmul(dM, self.M_beg)) #Mii = np.linalg.inv(dM) #print(pii.T, cvector(Mii[:,0]).T, cvector(Mii[:,2]).T) ccs_line = f'ccs("{self.ccs_end}", {write_ecs(pii_ccs_end, Mii)}"{self.name}_scr_ccs_{ii+1}");' lines.append(ccs_line) scr_line = f'screen("{self.ccs_end}", {write_ecs(pii_ccs_end, Mii/2.0)}0, "{self.name}_scr_ccs_{ii+1}");' lines.append(scr_line) return lines
def e1_end(self): return cvector(self._M_end[:,0])
def e3_beg(self): return cvector(self._M_beg[:,2])
def e2_beg(self): return cvector(self._M_beg[:,1])
def e1_beg(self): return cvector(self._M_beg[:,0])
def p_in_ccs(p, ccs_origin, ccs_M): return np.matmul( np.linalg.inv(ccs_M),(cvector(p) - cvector(ccs_origin)) )