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 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
| import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import time from scipy.sparse import diags from scipy.sparse.linalg import spsolve
class ExoplanetLifeSimulator: def __init__(self, grid_size=50, domain_size=100.0): """ Initialize the exoplanet life evolution simulator Parameters: - grid_size: Number of spatial grid points - domain_size: Physical domain size in km """ self.grid_size = grid_size self.domain_size = domain_size self.dx = domain_size / grid_size self.dt_base = 0.01 self.D = 0.5 self.r0 = 1.2 self.K = 100.0 self.mu = 0.1 self.T_opt = 298.0 self.sigma_T = 15.0 self.I0 = 50.0 self.x = np.linspace(0, domain_size, grid_size) self.y = np.linspace(0, domain_size, grid_size) self.X, self.Y = np.meshgrid(self.x, self.y) self.N = self._initialize_population() self.T = self._initialize_temperature() self.I = self._initialize_radiation() def _initialize_population(self): """Initialize population with localized colonies""" N = np.zeros((self.grid_size, self.grid_size)) centers = [(25, 25), (40, 15), (15, 40)] for cx, cy in centers: for i in range(self.grid_size): for j in range(self.grid_size): dist = np.sqrt((i - cx)**2 + (j - cy)**2) N[i, j] += 30 * np.exp(-dist**2 / 50) return N def _initialize_temperature(self): """Temperature gradient across the planet surface""" T = 298 + 20 * np.sin(2 * np.pi * self.X / self.domain_size) * \ np.cos(2 * np.pi * self.Y / self.domain_size) return T def _initialize_radiation(self): """Radiation intensity with latitude variation""" I = 100 * (1 - 0.3 * (self.Y / self.domain_size - 0.5)**2) return I def growth_rate(self, T, I): """Calculate growth rate based on temperature and radiation""" temp_factor = np.exp(-((T - self.T_opt)**2) / (2 * self.sigma_T**2)) radiation_factor = I / (self.I0 + I) return self.r0 * temp_factor * radiation_factor def compute_laplacian_sparse(self, N): """Compute Laplacian using sparse matrices for efficiency""" n = self.grid_size main_diag = -4 * np.ones(n * n) off_diag = np.ones(n * n - 1) off_diag_n = np.ones(n * n - n) for i in range(n): off_diag[i * n - 1] = 0 diagonals = [main_diag, off_diag, off_diag, off_diag_n, off_diag_n] offsets = [0, -1, 1, -n, n] L = diags(diagonals, offsets, shape=(n*n, n*n), format='csr') N_flat = N.flatten() laplacian_flat = L.dot(N_flat) / (self.dx**2) return laplacian_flat.reshape((n, n)) def adaptive_timestep(self, N): """Calculate adaptive time step based on local gradients""" max_gradient = np.max(np.abs(np.gradient(N)[0])) + np.max(np.abs(np.gradient(N)[1])) if max_gradient > 1e-10: dt = min(self.dt_base, 0.5 * self.dx**2 / (2 * self.D)) dt = min(dt, 0.1 / max_gradient) else: dt = self.dt_base return dt def simulate_optimized(self, total_time=10.0, accuracy_threshold=0.95): """ Optimized simulation using adaptive time-stepping and sparse matrices Parameters: - total_time: Total simulation time - accuracy_threshold: Minimum required accuracy Returns: - results: Dictionary containing simulation results """ start_time = time.time() t = 0 step = 0 time_points = [0] populations = [self.N.copy()] total_population = [np.sum(self.N)] while t < total_time: dt = self.adaptive_timestep(self.N) dt = min(dt, total_time - t) r = self.growth_rate(self.T, self.I) laplacian = self.compute_laplacian_sparse(self.N) reaction = r * self.N * (1 - self.N / self.K) - self.mu * self.N diffusion = self.D * laplacian N_new = self.N + dt * (diffusion + reaction) N_new = np.maximum(N_new, 0) N_new[0, :] = N_new[1, :] N_new[-1, :] = N_new[-2, :] N_new[:, 0] = N_new[:, 1] N_new[:, -1] = N_new[:, -2] self.N = N_new t += dt step += 1 if step % 10 == 0: time_points.append(t) populations.append(self.N.copy()) total_population.append(np.sum(self.N)) computation_time = time.time() - start_time accuracy = self._calculate_accuracy(populations) results = { 'time_points': np.array(time_points), 'populations': populations, 'total_population': np.array(total_population), 'computation_time': computation_time, 'accuracy': accuracy, 'final_state': self.N.copy(), 'steps': step } return results def _calculate_accuracy(self, populations): """Calculate simulation accuracy based on conservation and smoothness""" total_masses = [np.sum(p) for p in populations] mass_variation = np.std(total_masses) / (np.mean(total_masses) + 1e-10) smoothness = 1.0 / (1.0 + mass_variation * 10) stability = 1.0 if not np.any(np.isnan(populations[-1])) else 0.0 accuracy = 0.7 * smoothness + 0.3 * stability return accuracy def simulate_baseline(self, total_time=10.0): """Baseline simulation with fixed time step for comparison""" start_time = time.time() dt = 0.001 steps = int(total_time / dt) t = 0 time_points = [0] populations = [self.N.copy()] total_population = [np.sum(self.N)] for step in range(steps): r = self.growth_rate(self.T, self.I) laplacian = self.compute_laplacian_sparse(self.N) reaction = r * self.N * (1 - self.N / self.K) - self.mu * self.N diffusion = self.D * laplacian N_new = self.N + dt * (diffusion + reaction) N_new = np.maximum(N_new, 0) N_new[0, :] = N_new[1, :] N_new[-1, :] = N_new[-2, :] N_new[:, 0] = N_new[:, 1] N_new[:, -1] = N_new[:, -2] self.N = N_new t += dt if step % 100 == 0: time_points.append(t) populations.append(self.N.copy()) total_population.append(np.sum(self.N)) computation_time = time.time() - start_time accuracy = self._calculate_accuracy(populations) results = { 'time_points': np.array(time_points), 'populations': populations, 'total_population': np.array(total_population), 'computation_time': computation_time, 'accuracy': accuracy, 'final_state': self.N.copy(), 'steps': steps } return results
print("Starting Exoplanet Life Evolution Simulations...") print("=" * 60)
sim_opt = ExoplanetLifeSimulator(grid_size=50, domain_size=100.0) print("\nRunning OPTIMIZED simulation...") results_opt = sim_opt.simulate_optimized(total_time=10.0)
sim_base = ExoplanetLifeSimulator(grid_size=50, domain_size=100.0) print("Running BASELINE simulation...") results_base = sim_base.simulate_baseline(total_time=10.0)
print("\n" + "=" * 60) print("SIMULATION RESULTS COMPARISON") print("=" * 60) print(f"\nOptimized Simulation:") print(f" Computation Time: {results_opt['computation_time']:.4f} seconds") print(f" Accuracy: {results_opt['accuracy']:.4f} ({results_opt['accuracy']*100:.2f}%)") print(f" Total Steps: {results_opt['steps']}")
print(f"\nBaseline Simulation:") print(f" Computation Time: {results_base['computation_time']:.4f} seconds") print(f" Accuracy: {results_base['accuracy']:.4f} ({results_base['accuracy']*100:.2f}%)") print(f" Total Steps: {results_base['steps']}")
speedup = results_base['computation_time'] / results_opt['computation_time'] print(f"\nSpeedup Factor: {speedup:.2f}x") print(f"Time Saved: {results_base['computation_time'] - results_opt['computation_time']:.4f} seconds") print("=" * 60)
fig = plt.figure(figsize=(18, 12))
ax1 = fig.add_subplot(3, 3, 1) im1 = ax1.imshow(results_opt['populations'][0], cmap='viridis', origin='lower', extent=[0, 100, 0, 100]) ax1.set_title('Initial Population Distribution', fontsize=12, fontweight='bold') ax1.set_xlabel('X (km)') ax1.set_ylabel('Y (km)') plt.colorbar(im1, ax=ax1, label='Population Density')
ax2 = fig.add_subplot(3, 3, 2) im2 = ax2.imshow(results_opt['final_state'], cmap='viridis', origin='lower', extent=[0, 100, 0, 100]) ax2.set_title('Final Population Distribution (Optimized)', fontsize=12, fontweight='bold') ax2.set_xlabel('X (km)') ax2.set_ylabel('Y (km)') plt.colorbar(im2, ax=ax2, label='Population Density')
ax3 = fig.add_subplot(3, 3, 3) im3 = ax3.imshow(sim_opt.T, cmap='RdYlBu_r', origin='lower', extent=[0, 100, 0, 100]) ax3.set_title('Temperature Distribution', fontsize=12, fontweight='bold') ax3.set_xlabel('X (km)') ax3.set_ylabel('Y (km)') plt.colorbar(im3, ax=ax3, label='Temperature (K)')
ax4 = fig.add_subplot(3, 3, 4, projection='3d') surf1 = ax4.plot_surface(sim_opt.X, sim_opt.Y, results_opt['populations'][0], cmap='viridis', alpha=0.9, edgecolor='none') ax4.set_title('Initial Population (3D)', fontsize=12, fontweight='bold') ax4.set_xlabel('X (km)') ax4.set_ylabel('Y (km)') ax4.set_zlabel('Population') ax4.view_init(elev=30, azim=45)
ax5 = fig.add_subplot(3, 3, 5, projection='3d') surf2 = ax5.plot_surface(sim_opt.X, sim_opt.Y, results_opt['final_state'], cmap='plasma', alpha=0.9, edgecolor='none') ax5.set_title('Final Population (3D)', fontsize=12, fontweight='bold') ax5.set_xlabel('X (km)') ax5.set_ylabel('Y (km)') ax5.set_zlabel('Population') ax5.view_init(elev=30, azim=45)
ax6 = fig.add_subplot(3, 3, 6, projection='3d') surf3 = ax6.plot_surface(sim_opt.X, sim_opt.Y, sim_opt.T, cmap='RdYlBu_r', alpha=0.9, edgecolor='none') ax6.set_title('Temperature Field (3D)', fontsize=12, fontweight='bold') ax6.set_xlabel('X (km)') ax6.set_ylabel('Y (km)') ax6.set_zlabel('Temperature (K)') ax6.view_init(elev=25, azim=60)
ax7 = fig.add_subplot(3, 3, 7) ax7.plot(results_opt['time_points'], results_opt['total_population'], 'b-', linewidth=2, label='Optimized') ax7.plot(results_base['time_points'], results_base['total_population'], 'r--', linewidth=2, label='Baseline') ax7.set_title('Total Population Evolution', fontsize=12, fontweight='bold') ax7.set_xlabel('Time') ax7.set_ylabel('Total Population') ax7.legend() ax7.grid(True, alpha=0.3)
ax8 = fig.add_subplot(3, 3, 8) methods = ['Optimized', 'Baseline'] times = [results_opt['computation_time'], results_base['computation_time']] colors = ['green', 'red'] bars = ax8.bar(methods, times, color=colors, alpha=0.7, edgecolor='black') ax8.set_title('Computation Time Comparison', fontsize=12, fontweight='bold') ax8.set_ylabel('Time (seconds)') ax8.grid(True, alpha=0.3, axis='y') for bar, time_val in zip(bars, times): height = bar.get_height() ax8.text(bar.get_x() + bar.get_width()/2., height, f'{time_val:.3f}s', ha='center', va='bottom', fontweight='bold')
ax9 = fig.add_subplot(3, 3, 9) accuracies = [results_opt['accuracy'] * 100, results_base['accuracy'] * 100] bars2 = ax9.bar(methods, accuracies, color=['blue', 'orange'], alpha=0.7, edgecolor='black') ax9.set_title('Accuracy Comparison', fontsize=12, fontweight='bold') ax9.set_ylabel('Accuracy (%)') ax9.set_ylim([90, 100]) ax9.axhline(y=95, color='red', linestyle='--', linewidth=2, label='Threshold (95%)') ax9.legend() ax9.grid(True, alpha=0.3, axis='y') for bar, acc_val in zip(bars2, accuracies): height = bar.get_height() ax9.text(bar.get_x() + bar.get_width()/2., height, f'{acc_val:.2f}%', ha='center', va='bottom', fontweight='bold')
plt.tight_layout() plt.savefig('exoplanet_simulation_results.png', dpi=300, bbox_inches='tight') plt.show()
print("\nVisualization complete! Graph saved as 'exoplanet_simulation_results.png'")
|