def test_rotateAB(): d = 1 m = 1 m1 = np.array([[m, d, 0, 0]]) qm1 = pgm.qmoments(10, m1) alpha = -np.pi/4 beta = np.pi/4 # Rotate around z first by alpha m2 = glb.rotate_point_array(m1, alpha, [0, 0, 1]) # Rotate around y second by beta and get new moments qm1b = pgm.qmoments(10, glb.rotate_point_array(m2, beta, [0, 1, 0])) qm1c = rot.rotate_qlm(qm1, 0, beta, alpha) assert (abs(qm1c-qm1b) < 20*np.finfo(float).eps).all()
def test_quadrupole_torque(): """ Compare the point matrix calculation to an analytic formulation of a quadrupole torque. Tests ----- glb.point_matrix_gravity : function """ d = 1 R = rand.rand() * 100 + 1.1 m, M = 1, 1 N = 60 L = 10 m1 = np.array([[m, d, 0, 0], [m, -d, 0, 0]]) m2 = np.array([[M, R, 0, 0], [M, -R, 0, 0]]) qlm = pgm.qmoments(L, m1) tau = np.zeros(N) ts = np.zeros([60, 3]) d2R2 = d**2 + R**2 tc = np.zeros([N, L + 1], dtype='complex') ts = np.zeros([N, L + 1], dtype='complex') for k in range(N): a = 2 * np.pi * k / N ca = np.cos(a) Q = glb.rotate_point_array(m2, a, [0, 0, 1]) Qlmb = pgm.Qmomentsb(L, Q) tau[k] = 2 * mplb.BIG_G * M * m * d * R * np.sin(a) tau[k] *= 1 / (d2R2 - 2 * d * R * ca)**(3 / 2) - 1 / ( d2R2 + 2 * d * R * ca)**(3 / 2) tlm, tc[k], ts[k] = mplb.torque_lm(L, qlm, Qlmb) # XXX should be np.sum(tc, 1)-tau, but existing sign error! assert (abs(np.sum(tc, 1) + tau) < 10 * np.finfo(float).eps).all()
def test_quadrupole_torque(): """ Compare the point matrix calculation to an analytic formulation of a quadrupole torque. Tests ----- glb.point_matrix_gravity : function """ d = 1 R = rand.rand() * 100 + 1.1 m, M = 1, 1 N = 60 m1 = np.array([[m, d, 0, 0], [m, -d, 0, 0]]) m2 = np.array([[M, R, 0, 0], [M, -R, 0, 0]]) tau = np.zeros(N) ts = np.zeros([60, 3]) d2R2 = d**2 + R**2 for k in range(N): a = 2 * np.pi * k / N ca = np.cos(a) Q = glb.rotate_point_array(m2, a, [0, 0, 1]) tau[k] = 2 * glb.BIG_G * M * m * d * R * np.sin(a) tau[k] *= 1 / (d2R2 - 2 * d * R * ca)**(3 / 2) - 1 / ( d2R2 + 2 * d * R * ca)**(3 / 2) f, ts[k] = glb.point_matrix_gravity(m1, Q) assert (abs(tau - ts[:, 2]) < 10 * np.finfo(float).eps).all()
def test_rotateA2(): d = 1 m = 1 m1 = np.array([[m, d, 0, 0], [m, -d, 0, 0]]) qm1 = pgm.qmoments(10, m1) alpha = -np.pi/4 qm1b = pgm.qmoments(10, glb.rotate_point_array(m1, alpha, [0, 0, 1])) qm1c = rot.rotate_qlm(qm1, alpha, 0, 0) assert (abs(qm1c-qm1b) < 20*np.finfo(float).eps).all()
def test_rotateB(): d = 1 m = 1 m1 = np.array([[m, d, 0, 0], [m, -d, 0, 0]]) qm1 = pgm.qmoments(10, m1) beta = np.pi/4 qm1b = pgm.qmoments(10, glb.rotate_point_array(m1, beta, [0, 1, 0])) qm1c = rot.rotate_qlm(qm1, 0, beta, 0) assert (abs(qm1c-qm1b) < 10*np.finfo(float).eps).all()
def test_hexapole_torque(): """ Compare the point matrix calculation to an analytic formulation of a hexapole torque. Tests ----- glb.point_matrix_gravity : function """ d = 1 z = rand.randn() * 10 R = rand.rand() * 100 + 1.1 m, M = 1, 1 N = 60 m0 = np.array([m, d, 0, 0]) m1 = np.copy(m0) m2 = np.array([M, R, 0, z]) m3 = np.copy(m2) zhat = [0, 0, 1] for k in range(1, 3): m1 = np.vstack( [m1, glb.rotate_point_array(m0, 2 * k * np.pi / 3, zhat)]) m3 = np.vstack( [m3, glb.rotate_point_array(m2, 2 * k * np.pi / 3, zhat)]) L = 10 qlm = pgm.qmoments(L, m1) tau = np.zeros(N) tc = np.zeros([N, L + 1], dtype='complex') ts = np.zeros([N, L + 1], dtype='complex') d2R2 = d**2 + R**2 + z**2 for k in range(N): a = 2 * np.pi * k / N Q = glb.rotate_point_array(m3, a, zhat) Qlmb = pgm.Qmomentsb(L, Q) fac = 3 * glb.BIG_G * M * m * d * R tau[k] = np.sin(a) / (d2R2 - 2 * d * R * np.cos(a))**(3 / 2) tau[k] += np.sin(a + 2 * np.pi / 3) / ( d2R2 - 2 * d * R * np.cos(a + 2 * np.pi / 3))**(3 / 2) tau[k] += np.sin(a + 4 * np.pi / 3) / ( d2R2 - 2 * d * R * np.cos(a + 4 * np.pi / 3))**(3 / 2) tau[k] *= fac tlm, tc[k], ts[k] = mplb.torque_lm(L, qlm, Qlmb) # XXX should be np.sum(tc, 1)-tau, but existing sign error! assert (abs(np.sum(tc, 1) + tau) < 10 * np.finfo(float).eps).all()
def __init__(self, N, lmax, inner, mass, H, a, b, phic): Shape.__init__(self, N, lmax, inner) self.mass = mass if self.inner: self.qlm = qlm.rect_prism(lmax, mass, H, a, b, phic) else: print('Outer rectangular prism shape not defined') self.pointmass = gshp.wedge(mass, a, b, H, N, N, N) self.pointmass = glb.rotate_point_array(self.pointmass, phic, [0, 0, 1])
def __init__(self, N, lmax, inner, mass, H, a, phic, Ns): Shape.__init__(self, N, lmax, inner) self.mass = mass if self.inner: self.qlm = qlm.ngon_prism(lmax, mass, H, a, phic, Ns) else: print('Outer Tetrahedron shape not defined.') self.pointmass = gshp.ngon_prism(mass, H, a, Ns, N, N) self.pointmass = glb.rotate_point_array(self.pointmass, phic, [0, 0, 1])
def __init__(self, N, lmax, inner, mass, P, R, phic, phih): Shape.__init__(self, N, lmax, inner) self.mass = mass if self.inner: self.qlm = qlm.cone(lmax, mass, P, R, phic, phih) else: print('Outer cone shape not defined. See OuterCone') self.pointmass = gshp.cone(mass, R, P, phih, N, N) self.pointmass = glb.rotate_point_array(self.pointmass, phic, [0, 0, 1])
def __init__(self, N, lmax, inner, mass, H, Ri, Ro, phic, phih): Shape.__init__(self, N, lmax, inner) self.mass = mass if self.inner: self.qlm = qlm.annulus(lmax, mass, H, Ri, Ro, phic, phih) else: dens = mass/(2*phih*(Ro**2 - Ri**2)*H) self.qlm = bqlm.annulus(lmax, dens, H/2, Ri, Ro, phic, phih) self.qlm += bqlm.annulus(lmax, dens, -H/2, Ri, Ro, phic, phih) self.pointmass = gshp.wedge(mass, Ri, Ro, H, phih, N, N) self.pointmass = glb.rotate_point_array(self.pointmass, phic, [0, 0, 1])
def test_rotate(): """ Check that you can rotate from xHat to yHat Tests ----- glb.rotate_point_array : function """ m = np.array([1, 1, 0, 0]) o = glb.rotate_point_array(m, np.pi / 2., [0, 0, 1]) print(o) assert np.sum(o - np.array([1, 0, 1, 0])) < 4. * np.finfo(float).eps
def test_hexapole_torque(): """ Compare the point matrix calculation to an analytic formulation of a hexapole torque. Tests ----- glb.point_matrix_gravity : function """ d = 1 z = rand.randn() * 10 R = rand.rand() * 100 + 1.1 m, M = 1, 1 N = 60 m0 = np.array([m, d, 0, 0]) m1 = np.copy(m0) m2 = np.array([M, R, 0, z]) m3 = np.copy(m2) zhat = [0, 0, 1] for k in range(1, 3): m1 = np.vstack( [m1, glb.rotate_point_array(m0, 2 * k * np.pi / 3, zhat)]) m3 = np.vstack( [m3, glb.rotate_point_array(m2, 2 * k * np.pi / 3, zhat)]) tau = np.zeros(N) ts = np.zeros([60, 3]) d2R2 = d**2 + R**2 + z**2 for k in range(N): a = 2 * np.pi * k / N Q = glb.rotate_point_array(m3, a, zhat) fac = 3 * glb.BIG_G * M * m * d * R tau[k] = np.sin(a) / (d2R2 - 2 * d * R * np.cos(a))**(3 / 2) tau[k] += np.sin(a + 2 * np.pi / 3) / ( d2R2 - 2 * d * R * np.cos(a + 2 * np.pi / 3))**(3 / 2) tau[k] += np.sin(a + 4 * np.pi / 3) / ( d2R2 - 2 * d * R * np.cos(a + 4 * np.pi / 3))**(3 / 2) tau[k] *= fac f, ts[k] = glb.point_matrix_gravity(m1, Q) assert (abs(tau - ts[:, 2]) < 10 * np.finfo(float).eps).all()
def test_rotate3(): """ Check that rotating by a random angle makes sense Tests ----- glb.rotate_point_array : function """ q = np.array([1, 1, 0, 0]) N = 100 v = np.zeros([N, 2]) for k in range(1, N + 1): a = 2 * np.pi * rand.rand() p = glb.rotate_point_array(q, a, [0, 0, 1]) v[k - 1] = [a, (np.arctan(p[2] / p[1]) % np.pi / 2) - (a % np.pi / 2)] assert (abs(v[:, 1] < 10 * np.finfo(float).eps)).all()
def test_rotate2(): """ Check that rotating by 2 pi in N steps gets you back to start. Tests ----- glb.rotate_point_array : function """ m = np.array([1, 1, 0, 0]) err = np.zeros([100, 4]) for n in range(1, 101): q = np.copy(m) rvec = rand.rand(3) for k in range(1, n + 1): q = glb.rotate_point_array(q, 2 * np.pi / n, rvec) err[n - 1] = m - q assert (abs(err) < 40 * np.finfo(float).eps).all()
def rotate(self, alpha, beta, gamma): self.qlm = rot.rotate_qlm(self.qlm, alpha, beta, gamma) self.pointmass = glb.rotate_point_array(self.pointmass, gamma, [0, 0, 1]) self.pointmass = glb.rotate_point_array(self.pointmass, beta, [0, 1, 0]) self.pointmass = glb.rotate_point_array(self.pointmass, alpha, [0, 0, 1]) # Rotate center of mass using point-gravity tools rotcom = np.concatenate([[self.mass], self.com]) rotcom = glb.rotate_point_array(rotcom, gamma, [0, 0, 1]) rotcom = glb.rotate_point_array(rotcom, beta, [0, 1, 0]) rotcom = glb.rotate_point_array(rotcom, alpha, [0, 0, 1]) self.com = rotcom[1:4]
# -*- coding: utf-8 -*- """ Created on Sat Jun 20 11:09:40 2020 @author: John Greendeer Lee """ import numpy as np import newt.glib as glb import newt.glibShapes as gshp # Create a cylinder cyl = gshp.annulus(1, 0, 1, 1, 10, 10) # Inner cylinders on radius of 1m cyl1 = glb.translate_point_array(cyl, [5, 0, 0]) # Outer cylinders on radius of 5m cyl2 = glb.translate_point_array(cyl, [20, 0, 0]) # Combination of three inner cylinders m1 = np.concatenate([ cyl1, glb.rotate_point_array(cyl1, 2 * np.pi / 3, [0, 0, 1]), glb.rotate_point_array(cyl1, -2 * np.pi / 3, [0, 0, 1]) ]) # Combination of three outer cylinders m2 = np.concatenate([ cyl2, glb.rotate_point_array(cyl2, 2 * np.pi / 3, [0, 0, 1]), glb.rotate_point_array(cyl2, -2 * np.pi / 3, [0, 0, 1]) ]) fig, ax = glb.display_points(m1, m2) ax.set_zlim([-20, 20])