def test_coil_integrity(): numbers = np.load('rand.npy') numbers[0:50, 0] = 0 numbers[51:200, 2] = 0 numbers[100:200, 3] = 0 r1x = m1.field_axial(numbers[:, 0], numbers[:, 1], numbers[:, 2], numbers[:, 3]) r1r = m1.field_radial(numbers[:, 0], numbers[:, 1], numbers[:, 2], numbers[:, 3]) r2x = [m2.field_axial(*line) for line in numbers] r2r = [m2.field_radial(*line) for line in numbers] assert np.allclose(r1x, r2x) assert np.allclose(r1r, r2r) assert np.isclose(m1.field_axial(2.2, 5, 3, 1), 1.7293349e-07) assert np.isclose(m1.field_axial(2.2, 5, 3, -1), 1.7293349e-07)
def _run_sp_legacy(self): """ Single-threaded solver without vectorization, slow for larger meshes or magnets. :return: None """ if self.done: return x_mesh, y_mesh = self._mesh.get_matrix() x_field = np.zeros(x_mesh.shape) y_field = np.zeros(x_mesh.shape) for i in range(x_mesh.shape[0]): for j in range(x_mesh.shape[1]): for source in self._sources: loops = source.get_loop_list() for loop in loops: xp = x_mesh[i][j] yp = y_mesh[i][j] a = loop[1] / 1000 x = (xp - loop[0]) / 1000 r = yp / 1000 current = loop[2] bx = loop_calculator.field_axial(current, a, x, r) br = loop_calculator.field_radial(current, a, x, r) x_field[i][j] += bx y_field[i][j] += br self.done = True self._x_field = x_field self._y_field = y_field self._x_field_interpolator, self._y_field_interpolator = self._make_field_interpolator( ) self._center_field = self.calculate_center_field()
def b_field(self, xp, yp): """ Calculate the field at [xp, yp] :param xp: x coords, in mm. :param yp: y coords, in mm. :return: Field in Tesla. """ loops = self.get_loop_list() a = loops[:, 1] / 1000 x = (xp - loops[:, 0]) / 1000 r = yp / 1000 current = loops[:, 2] bx = np.sum(loop_calculator.field_axial(current, a, x, r)) br = np.sum(loop_calculator.field_radial(current, a, x, r)) return bx, br
def _mp_process_run(ij_list, x_mesh, y_mesh, loop_list): x_field = np.zeros(x_mesh.shape) y_field = np.zeros(x_mesh.shape) loops = np.asarray(loop_list) for i, j in ij_list: xp = x_mesh[i][j] yp = y_mesh[i][j] a = loops[:, 1] / 1000 x = (xp - loops[:, 0]) / 1000 r = yp / 1000 current = loops[:, 2] bx = loop_calculator.field_axial(current, a, x, r) br = loop_calculator.field_radial(current, a, x, r) x_field[i][j] += np.sum(bx) y_field[i][j] += np.sum(br) return x_field, y_field
def b_field_vec(self, x_mesh, y_mesh): """ Calculates field on a (M, N) 2-D meshgrid in a vectorized manner. Optional: if not present will fall back to b_field method. :param x_mesh: (M, N) array, usually from np.meshgrid :param y_mesh: (M, N) array, usually from np.meshgrid :return: ((M, N), (M, N)) arrays of x and y fields. """ loops = self.get_loop_list() ones_xy = np.ones(x_mesh.shape) ones_z = np.ones(loops.shape[0]) a = ones_xy[..., None] * loops[:, 1] / 1000 x = (x_mesh[..., None] * ones_z - ones_xy[..., None] * loops[:, 0]) / 1000 r = y_mesh[..., None] * ones_z / 1000 current = ones_xy[..., None] * loops[:, 2] bx_mesh = np.sum(loop_calculator.field_axial(current, a, x, r), axis=2) by_mesh = np.sum(loop_calculator.field_radial(current, a, x, r), axis=2) return bx_mesh, by_mesh