def build_b_arrays(self): if self.mag_array == None: offset = (self.id_length) * self.steps_per_magnet length = 3 * offset x, y, z = self.get_adapeted_magnet(0) total = mt.calculate_magnetic_components(x, y, z, self.b_arrays[0][:, :, offset : offset + length, :, :]) for i in range(1, self.id_length): if "E" in self.types[i - 1]: if "E" in self.types[i]: offset -= self.steps_per_magnet / 2 else: offset -= self.steps_per_magnet * 3 / 4 else: if "E" in self.types[i]: offset -= self.steps_per_magnet * 3 / 4 else: offset -= self.steps_per_magnet x, y, z = self.get_adapeted_magnet(i) # print "Beam", i, self.types[i], x, y, z, offset total += mt.calculate_magnetic_components( x, y, z, self.b_arrays[i][:, :, offset : offset + length, :, :] ) if self.id_position == BeamAssembly.bottom: total[:, :, :, 0] *= -1.0 total[:, :, :, 1] *= -1.0 # total = np.roll(total, +14, 2)#Test - Ed mass = np.abs(total[total.shape[0] / 2, total.shape[1] / 2, :, 1]) pos = np.arange(total.shape[2]) com = np.average(pos, axis=0, weights=mass) com = int(np.round(com)) self.mag_array = total[:, :, com - (self.trunc_length / 2) : com + (self.trunc_length / 2), :] return self.mag_array
def flip_magnet(self, pos): # get the removed magnets field offset = (self.id_length)*self.steps_per_magnet length = 3*offset offset = (self.id_length-pos)*self.steps_per_magnet x, y, z = self.get_adapeted_magnet(pos) remove = mt.calculate_magnetic_components(x, y, z, self.b_arrays[pos][:,:,offset:offset+length,:,:]) self.flip[pos] *= -1 x, y, z = self.get_adapeted_magnet(pos) add = mt.calculate_magnetic_components(x, y, z, self.b_arrays[pos][:,:,offset:offset+length,:,:]) modify = add - remove if self.id_position == BeamAssembly.bottom: modify[:,:,:,0] *= -1.0 modify[:,:,:,1] *= -1.0 if self.shimming: self.real_barray += modify[int(modify.shape[0]/2),int(modify.shape[1]/2),((modify.shape[2]-self.trunc_length))/2:-(modify.shape[2]-self.trunc_length)/2,:] #self.ppm.real_b_array += modify[int(modify.shape[0]/2),int(modify.shape[1]/2),((modify.shape[2]-self.trunc_length))/2:-(modify.shape[2]-self.trunc_length)/2,:] else : self.mag_array += modify[:,:,((modify.shape[2]-self.trunc_length))/2:-(modify.shape[2]-self.trunc_length)/2,:] #TODO Correct the integral arrays rather than resetting them self.mag_fintx_array = None self.mag_fintz_array = None self.mag_sintx_array = None self.mag_sintz_array = None return (modify, self.trunc_length)
def swap_set_magnet(self, pos, type, key, flip): # get the removed magnets field offset = (self.id_length) * self.steps_per_magnet length = 3 * offset offset = (self.id_length - pos) * self.steps_per_magnet x, y, z = self.get_adapeted_magnet(pos) remove = mt.calculate_magnetic_components(x, y, z, self.b_arrays[pos][:, :, offset : offset + length, :, :]) magnet = self.magnets[pos] # replace this with a new one new_magnet = self.magnet_pool.magnet_sets[type].borrow_magnet(key) if new_magnet == None: print "magnet Not found!!! **************************************************" self.magnets[pos] = new_magnet self.magnet_pool.magnet_sets[type].return_magnet(magnet) # and flip this self.flip[pos] = flip x, y, z = self.get_adapeted_magnet(pos) add = mt.calculate_magnetic_components(x, y, z, self.b_arrays[pos][:, :, offset : offset + length, :, :]) modify = add - remove if self.id_position == BeamAssembly.bottom: modify[:, :, :, 0] *= -1.0 modify[:, :, :, 1] *= -1.0 if self.shimming: self.real_barray += modify[ int(modify.shape[0] / 2), int(modify.shape[1] / 2), ((modify.shape[2] - self.trunc_length)) / 2 : -(modify.shape[2] - self.trunc_length) / 2, :, ] # self.ppm.real_b_array += modify[int(modify.shape[0]/2),int(modify.shape[1]/2),((modify.shape[2]-self.trunc_length))/2:-(modify.shape[2]-self.trunc_length)/2,:] else: self.mag_array += modify[ :, :, ((modify.shape[2] - self.trunc_length)) / 2 : -(modify.shape[2] - self.trunc_length) / 2, : ] # TODO Correct the integral arrays rather than resetting them self.mag_fintx_array = None self.mag_fintz_array = None self.mag_sintx_array = None self.mag_sintz_array = None return (modify, self.trunc_length)
def save_to_h5(self, filename): f = h5py.File(filename, 'w') # create the whole system as a big array offset = (self.top_beam.id_length)*self.steps_per_magnet length = 3*offset xt, yt, zt = self.top_beam.get_adapeted_magnet(0) xb, yb, zb = self.bottom_beam.get_adapeted_magnet(0) totalT = mt.calculate_magnetic_components(xt, yt, zt, self.top_beam.b_arrays[0][:,:,offset:offset+length,:,:]) totalB = mt.calculate_magnetic_components(xb, yb, zb, self.bottom_beam.b_arrays[0][:,:,offset:offset+length,:,:]) totalB[:,:,:,0] *= -1.0 totalB[:,:,:,1] *= -1.0 total = totalB + totalT dataT = f.create_dataset("Top_B_Elements", (self.top_beam.id_length,)+totalT.shape, totalT.dtype) dataB = f.create_dataset("Bottom_B_Elements", (self.top_beam.id_length,)+totalB.shape, totalB.dtype) data = f.create_dataset("B_Elements", (self.top_beam.id_length,)+total.shape, total.dtype) dataT[0,:,:,:,:] = totalT dataB[0,:,:,:,:] = totalB data[0,:,:,:,:] = total for i in range(1, self.top_beam.id_length): if "E" in self.top_beam.types[i-1] : if "E" in self.top_beam.types[i] : offset -= self.steps_per_magnet/2 else : offset -= self.steps_per_magnet*3/4 else : if "E" in self.top_beam.types[i] : offset -= self.steps_per_magnet*3/4 else : offset -= self.steps_per_magnet xt, yt, zt = self.top_beam.get_adapeted_magnet(i) xb, yb, zb = self.bottom_beam.get_adapeted_magnet(i) #print "Beam", i, self.types[i], x, y, z, offset nextT = mt.calculate_magnetic_components(xt, yt, zt, self.top_beam.b_arrays[i][:,:,offset:offset+length,:,:]) nextB = mt.calculate_magnetic_components(xb, yb, zb, self.bottom_beam.b_arrays[i][:,:,offset:offset+length,:,:]) nextB[:,:,:,0] *= -1.0 nextB[:,:,:,1] *= -1.0 next = nextT + nextB dataT[i,:,:,:,:] = nextT dataB[i,:,:,:,:] = nextB data[i,:,:,:,:] = next total += next total = f.create_dataset("B", total.shape, total.dtype, total); f.close()
def calculate_b_contribution(self, magnet_number): x, y, z = self.get_adapeted_magnet(magnet_number) # print "Beam", magnet_number,x,y,z return mt.calculate_magnetic_components(x, y, z, self.b_arrays[magnet_number])