def randomRadiusPlotRZ(): R = 3 ms = [] rms = [] zms = [] plt.figure(figsize=(5, 5)) for _ in range(1000): rm = R * np.random.rand() xm = simulation.randomPointOnSphere(rm) ms.append(xm) rms.append((xm[0]**2 + xm[1]**2)**.5) zms.append(xm[2]) xp = simulation.randomPointOnSphere(1) propdir = np.cross(xm, xp) propdir /= np.linalg.norm(propdir) points = [ xm + i * propdir * (R**2 - rm**2)**.5 for i in np.arange(-1, 1, .05) ] rs = [(p[0]**2 + p[1]**2)**.5 for p in points] zs = [p[2] for p in points] plt.plot(rs, zs, color='blue', linewidth=.1) # plt.scatter(rms, zms, s=.5) plt.axis('equal') # ax.scatter(*list(zip(*ms)), color='red', s=1) plt.show()
def randomRadiusPlot3D(): """ Pick uniformly random r, phi, and theta in sphere as midpoint Pick an xy vector with random angle Take cross product with midpoint to get chord direction Step from midpoint to boundary of circle by sqrt(r^2-m^2) in chord direction """ R = 3 ms = [] fig = plt.figure(figsize=(5, 5)) ax = fig.gca(projection='3d') for _ in range(1000): rm = R * np.random.rand() xm = simulation.randomPointOnSphere(rm) ms.append(xm) xp = simulation.randomPointOnSphere(1) propdir = np.cross(xm, xp) propdir /= np.linalg.norm(propdir) xstart = xm + -1 * propdir * (R**2 - rm**2)**.5 xend = xm + propdir * (R**2 - rm**2)**.5 ax.plot(*list(zip(xstart, xend)), color='blue', linewidth=.1) # ax.scatter(*list(zip(*ms)), color='red', s=1) _axisEqual3D(ax) plt.axis('off') plt.show()
def testRandomPointOnSphere(): points = np.array( [simulation.randomPointOnSphere(10) for _ in range(1000)]) fig = plt.figure(figsize=(5, 5)) ax = fig.gca(projection='3d') ax.scatter(points[:, 0], points[:, 1], points[:, 2]) plt.show()
def testTrajectory(): dt = 1e-11 print('dt (s):', dt) fieldFile = 'oct30.txt' r, z, BR = simulation.fieldGrid('fields/Brs_' + fieldFile) _, _, BZ = simulation.fieldGrid('fields/Bzs_' + fieldFile) BR = BR * 0 # BUG BZ = BZ * 0 E = np.array([0., 0, 0]) qm = 9.6e7 v0 = 2.6e8 maxSteps = 30000 # Generate random point and direction rLim = 30 x = simulation.randomPointOnSphere(rLim) xNoField = x direction = simulation.randomPointOnSphere(rLim) - x direction /= np.linalg.norm(direction) v = direction * v0 vNoField = v xs = np.zeros((maxSteps, 3)) xNoFields = np.zeros((maxSteps, 3)) for i in range(maxSteps): xNoField += vNoField * dt xNoFields[i] = xNoField # x, v = simulation.RKnext(x, v, qm, BR, BZ, r, z, dt) B = simulation.BxyzInterpolated(x, BR, BZ, r, z) x, v = simulation.BBnext(x, v, qm, B, E, dt) xs[i] = x fig = plt.figure(figsize=(10, 10)) ax = fig.gca(projection='3d') ax.plot(xs[:, 0], xs[:, 1], xs[:, 2]) ax.plot(xNoFields[:, 0], xNoFields[:, 1], xNoFields[:, 2], '--') _axisEqual3D(ax) plt.show()
def testRandomDirectionCos(): fig = plt.figure(figsize=(7, 7)) ax = fig.gca(projection='3d') axis = simulation.randomPointOnSphere(3) ax.plot([0, axis[0]], [0, axis[1]], [0, axis[2]], color='black') xs = [] ys = [] zs = [] for _ in range(1000): d = simulation.randomDirectionCos(axis) xs.append(d[0]) ys.append(d[1]) zs.append(d[2]) ax.scatter(xs, ys, zs, color='blue', alpha=.02) _axisEqual3D(ax) plt.show() #
def randomHomogRZ(): """ Randomly select midpoint R and Z in order to get as uniform an R, Z grid as possible. """ R = 3. zs = [] ms = [] rms = [] zms = [] plt.figure(figsize=(10, 10)) for _ in range(3000): xm = np.array([0., 0., 4.]) while np.linalg.norm(xm) > R: xm[2] = 2 * R * (np.random.rand() - 0.5) rm = R * np.random.rand()**2 thetam = 2 * np.pi * np.random.rand() xm[0] = rm * np.cos(thetam) xm[1] = rm * np.sin(thetam) ms.append(xm) rms.append((xm[0]**2 + xm[1]**2)**.5) zms.append(xm[2]) xp = simulation.randomPointOnSphere(1) propdir = np.cross(xm, xp) propdir /= np.linalg.norm(propdir) points = [ xm + i * propdir * (R**2 - rm**2)**.5 for i in np.arange(-1, 1, .05) if np.linalg.norm(xm + i * propdir * (R**2 - rm**2)**.5) < R ] rs = [(p[0]**2 + p[1]**2)**.5 for p in points] zs = [p[2] for p in points] plt.plot(rs, zs, color='blue', linewidth=.1) # plt.scatter(rms, zms, s=.5) plt.axis('equal') # ax.scatter(*list(zip(*ms)), color='red', s=1) plt.show()