def test_mul1(self): for i in range(100): a = random.random() + i + 1 b = random.random() + i + 1 c = random.random() + i + 1 d = random.random() + i + 1 self.assertTrue(P3(a, b, d) * c, P3(a * c, b * c, d * c))
def test_p3_sub(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i d = random.random() + i e = random.random() + i f = random.random() + i self.assertEqual( P3(a, b, e) - P3(c, d, f), P3(a - c, b - d, e - f))
def test_p3_add(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i d = random.random() + i e = random.random() + i f = random.random() + i self.assertEqual( P3(a, b, e) + P3(c, d, f), P3(a + c, b + d, e + f))
def test_mul2(self): for i in range(100): a = random.random() + i + 1 b = random.random() + i + 1 c = random.random() + i + 1 d = random.random() + i + 1 e = random.random() + i + 1 f = random.random() + i + 1 self.assertTrue( BaseUtils.equal(P3(a, b, e) * P3(c, d, f), a * c + b * d + e * f, msg="error"))
def test__matmul__(self): x = P3.x_direct() y = P3.y_direct() z = P3.z_direct() self.assertEqual(x @ y, z) self.assertEqual(y @ z, x) self.assertEqual(z @ x, y) self.assertEqual(x @ -y, -z) self.assertEqual(y @ -z, -x) self.assertEqual(z @ -x, -y)
def test_p3_isub(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i d = random.random() + i e = random.random() + i f = random.random() + i p1 = P3(a, b, e) p2 = P3(c, d, f) p1_copy = p1.copy() p1 -= p2 self.assertEqual(p1, p1_copy - p2)
def test_normalize(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i self.assertTrue( BaseUtils.equal(P3(a, b, c).normalize().length(), 1.0))
def test_change_length(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i d = random.random() + i self.assertTrue( BaseUtils.equal(P3(a, b, c).change_length(d).length(), d))
def test_length(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i self.assertTrue( BaseUtils.equal( P3(a, b, c).length(), float(np.sqrt(a**2 + b**2 + c**2))))
def test_magnet_at(self): cct = CCT( LocalCoordinateSystem.global_coordinate_system(), 0.95, 83 * MM + 15 * MM * 2, 67.5, [30.0, 80.0, 90.0, 90.0], 128, -9664, P2(0, 0), P2(128 * np.pi * 2, 67.5 / 180.0 * np.pi), ) m = cct.magnetic_field_at(P3.origin()) self.assertEqual( m, P3(0.0031436355039083964, -0.00470478301086915, 0.00888627084434009))
def test_quad_1(self): """ 测试 qs 四极场 Returns ------- """ length = 0.2 * M aper = 30 * MM g = -45.7 L = 0 lc = LocalCoordinateSystem(P3(), -P3.x_direct(), P3.y_direct()) qs = QS(lc, length, g, L, aper) m = qs.magnetic_field_at(P3(10 * MM, 0.1, 0)) self.assertTrue(m == P3(0.0, 0.0, 0.457)) m = qs.magnetic_field_at(P3(15 * MM, 0.1, 0)) self.assertTrue(m == P3(0.0, 0.0, 0.6855)) m = qs.magnetic_field_at(P3(15 * MM, 0.1, 5 * MM)) self.assertTrue(m == P3(0.2285, 1.399158968025851e-17, 0.6855))
def test_quad_0(self): """ 测试 qs 四极场 Returns ------- """ length = 0.2 * M aper = 30 * MM g = 10.0 L = 0 lc = LocalCoordinateSystem(P3(), -P3.x_direct(), P3.y_direct()) qs = QS(lc, length, g, L, aper) m = qs.magnetic_field_at(P3(10 * MM, 0.1, 0.0)) self.assertTrue(m == P3(0.0, 0.0, -0.1)) m = qs.magnetic_field_at(P3(15 * MM, 0.1, 0.0)) self.assertTrue(m == P3(0.0, 0.0, -0.15)) m = qs.magnetic_field_at(P3(15 * MM, 0.1, 5 * MM)) self.assertTrue(m == P3(-0.05, -3.061616997868383e-18, -0.15))
def test_p3_neg(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i self.assertEqual(P3(-a, -b, -c), -P3(a, b, c))
MM, ) cct = CCT( LocalCoordinateSystem.global_coordinate_system(), 0.95, 83 * MM + 15 * MM * 2, 67.5, [30.0, 80.0, 90.0, 90.0], 128, -9664, P2(0, 0), P2(128 * np.pi * 2, 67.5 / 180.0 * np.pi), ) # Plot2.plot_cct(cct) # Plot3.plot_cct(cct) # Plot2.show() # Plot3.show() m = cct.magnetic_field_at(P3()) print(m) import time s = time.time() for p in BaseUtils.linspace(P3(),P3(y=2),500): print(cct.magnetic_field_at(p)) print(time.time()-s)
def test_copy(self): for i in range(100): a = random.random() + i b = random.random() + i c = random.random() + i self.assertTrue(P3(a, b, c) == P3(a, b, c).copy())
def test_to_p3(self): for i in range(100): a = random.random() + i b = random.random() + i self.assertTrue(P2(a, b).to_p3() == P3(a, b, 0.0))
magnet( drv.In(winding), drv.In(p), drv.In(np.array([length]).astype(np.int32)), drv.Out(ret), block=(512, 1, 1), grid=(250, 1), ) print(ret) print(ret * cct.current * 1e-7) ###################### time ############### print("--------") m = cct.magnetic_field_at_cpu(P3()) print(m) m = cct.magnetic_field_at_gpu(P3()) print(m) print("--------------------------------") ###################### time ############### import time times = 2 ################# s = time.time() for x in np.linspace(0, 0.01, times): p = np.array([x, 0.0, 0.0]).astype(np.float32) magnet(
import time times = 2 ################# s = time.time() for x in np.linspace(0, 0.01, times): p = np.array([x, 0.0, 0.0]).astype(np.float32) magnet( drv.In(winding), drv.In(p), drv.In(np.array([length]).astype(np.int32)), drv.Out(ret), block=(512, 1, 1), grid=(250, 1), ) print(P3.from_numpy_ndarry3(ret * cct.current * 1e-7), p) print(f"CUDA_ONE d={time.time()-s}") ################# s = time.time() for x in np.linspace(0, 0.01, times): p = P3(x, 0, 0) m = cct.magnetic_field_at_cpu(p) print(m, p) print(f"CPU-d={time.time()-s}") ################# s = time.time() for x in np.linspace(0, 0.01, times): p = P3(x, 0, 0) m = cct.magnetic_field_at(p)
from cctpy import M, MM, LocalCoordinateSystem, QS, Plot3, P3 length = 0.2 * M aper = 30 * MM g = 10.0 L = 0 lc = LocalCoordinateSystem(P3(), -P3.x_direct(), P3.y_direct()) qs = QS(lc, length, g, L, aper) Plot3.plot_qs(qs) Plot3.show() m = qs.magnetic_field_at_cpu(P3(10 * MM, 0.1, 0.0)) print(m)