1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
| import numpy as np import matplotlib.pyplot as plt from scipy.integrate import solve_ivp from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm
def torus(u, v, R=2, r=1): """ Parametrize a torus with major radius R and minor radius r u, v are the parameters in [0, 2π) """ x = (R + r * np.cos(v)) * np.cos(u) y = (R + r * np.cos(v)) * np.sin(u) z = r * np.sin(v) return x, y, z
def torus_metric(u, v, R=2, r=1): """ Compute the metric coefficients E, F, G for the torus at point (u, v) """ E = (R + r * np.cos(v))**2 F = 0 G = r**2 return E, F, G
def christoffel_symbols(u, v, R=2, r=1): """ Compute the Christoffel symbols for the torus at point (u, v) Returns a 2x2x2 array where Gamma[i,j,k] = Gamma^i_{jk} """ E, F, G = torus_metric(u, v, R, r) dE_du = 0 dE_dv = -2 * (R + r * np.cos(v)) * r * np.sin(v) dF_du = dF_dv = 0 dG_du = 0 dG_dv = 0 Gamma = np.zeros((2, 2, 2)) g_inv_11 = 1/E g_inv_22 = 1/G Gamma[0, 0, 0] = 0.5 * g_inv_11 * dE_du Gamma[0, 0, 1] = Gamma[0, 1, 0] = 0.5 * g_inv_11 * dE_dv Gamma[0, 1, 1] = -0.5 * g_inv_11 * dG_du Gamma[1, 0, 0] = -0.5 * g_inv_22 * dE_dv Gamma[1, 0, 1] = Gamma[1, 1, 0] = 0.5 * g_inv_22 * dF_du Gamma[1, 1, 1] = 0.5 * g_inv_22 * dG_dv return Gamma
def geodesic_equation(t, y, R=2, r=1): """ The geodesic equation as a first-order system y = [u, v, u_dot, v_dot] Returns [u_dot, v_dot, u_ddot, v_ddot] """ u, v, u_dot, v_dot = y Gamma = christoffel_symbols(u, v, R, r) u_ddot = -Gamma[0, 0, 0] * u_dot**2 - 2 * Gamma[0, 0, 1] * u_dot * v_dot - Gamma[0, 1, 1] * v_dot**2 v_ddot = -Gamma[1, 0, 0] * u_dot**2 - 2 * Gamma[1, 0, 1] * u_dot * v_dot - Gamma[1, 1, 1] * v_dot**2 return [u_dot, v_dot, u_ddot, v_ddot]
def solve_geodesic(u0, v0, u_dot0, v_dot0, t_span, R=2, r=1): """ Solve the geodesic equation with initial conditions u0, v0: initial position u_dot0, v_dot0: initial velocity t_span: time span [t_start, t_end] """ y0 = [u0, v0, u_dot0, v_dot0] sol = solve_ivp( lambda t, y: geodesic_equation(t, y, R, r), t_span, y0, method='RK45', rtol=1e-8, atol=1e-8, dense_output=True ) return sol
def plot_torus_and_geodesic(sol, R=2, r=1, num_points=1000): """ Plot the torus surface and the geodesic curve """ fig = plt.figure(figsize=(12, 10)) ax = fig.add_subplot(111, projection='3d') u = np.linspace(0, 2*np.pi, 100) v = np.linspace(0, 2*np.pi, 100) u_grid, v_grid = np.meshgrid(u, v) x, y, z = torus(u_grid, v_grid, R, r) ax.plot_surface(x, y, z, cmap=cm.viridis, alpha=0.6, edgecolor='none') t = np.linspace(sol.t[0], sol.t[-1], num_points) y_interp = sol.sol(t) u_sol, v_sol = y_interp[0], y_interp[1] x_geo, y_geo, z_geo = torus(u_sol, v_sol, R, r) ax.plot(x_geo, y_geo, z_geo, 'r-', linewidth=2, label='Geodesic') ax.plot([x_geo[0]], [y_geo[0]], [z_geo[0]], 'go', markersize=8, label='Start') ax.plot([x_geo[-1]], [y_geo[-1]], [z_geo[-1]], 'bo', markersize=8, label='End') ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.set_title('Geodesic on a Torus (R={}, r={})'.format(R, r)) ax.legend() plt.tight_layout() plt.show() return fig, ax
def analyze_geodesic(sol, R=2, r=1, num_points=1000): """ Analyze properties of the geodesic such as length and curvature """ t = np.linspace(sol.t[0], sol.t[-1], num_points) y_interp = sol.sol(t) u_sol, v_sol = y_interp[0], y_interp[1] u_dot_sol, v_dot_sol = y_interp[2], y_interp[3] E, F, G = [], [], [] for i in range(len(t)): E_i, F_i, G_i = torus_metric(u_sol[i], v_sol[i], R, r) E.append(E_i) F.append(F_i) G.append(G_i) E = np.array(E) F = np.array(F) G = np.array(G) speed = np.sqrt(E * u_dot_sol**2 + 2*F * u_dot_sol * v_dot_sol + G * v_dot_sol**2) dt = t[1] - t[0] length = np.sum(speed * dt) plt.figure(figsize=(10, 6)) plt.plot(t, speed) plt.title('Speed along the Geodesic') plt.xlabel('t') plt.ylabel('Speed') plt.grid(True) plt.show() print(f"Geodesic Length: {length:.6f}") return length
if __name__ == "__main__": R = 2.0 r = 1.0 u0 = 0.0 v0 = 0.0 u_dot0 = 1.0 v_dot0 = 0.5 t_span = [0, 10] sol = solve_geodesic(u0, v0, u_dot0, v_dot0, t_span, R, r) plot_torus_and_geodesic(sol, R, r) geodesic_length = analyze_geodesic(sol, R, r) plt.figure(figsize=(12, 10)) initial_conditions = [ (0.0, 0.0, 1.0, 0.0, 'r', 'Horizontal'), (0.0, 0.0, 0.0, 1.0, 'g', 'Vertical'), (0.0, 0.0, 1.0, 0.5, 'b', 'Diagonal'), (0.0, 0.0, 1.0, 2.0, 'purple', 'Steep Diagonal'), ] ax = plt.axes(projection='3d') u = np.linspace(0, 2*np.pi, 30) v = np.linspace(0, 2*np.pi, 30) u_grid, v_grid = np.meshgrid(u, v) x, y, z = torus(u_grid, v_grid, R, r) ax.plot_surface(x, y, z, color='lightgray', alpha=0.2) for u0, v0, u_dot0, v_dot0, color, label in initial_conditions: sol = solve_geodesic(u0, v0, u_dot0, v_dot0, t_span, R, r) t = np.linspace(sol.t[0], sol.t[-1], 1000) y_interp = sol.sol(t) u_sol, v_sol = y_interp[0], y_interp[1] x_geo, y_geo, z_geo = torus(u_sol, v_sol, R, r) ax.plot(x_geo, y_geo, z_geo, color=color, linewidth=2, label=label) ax.set_xlabel('X') ax.set_ylabel('Y') ax.set_zlabel('Z') ax.set_title('Different Geodesics on a Torus (R={}, r={})'.format(R, r)) ax.legend() plt.tight_layout() plt.show()
|