QGAN_Project / plasma_sim_v2.py
1bnjmn3's picture
Add files using upload-large-folder tool
c5f84b2 verified
import numpy as np
import pandas as pd
from scipy.integrate import odeint
def lorenz_system(current_state, t, sigma=10, rho=28, beta=8/3):
"""
The Lorenz Attractor equations.
x = Proxy for Plasma Radial Position
y = Proxy for Poloidal Magnetic Field
z = Proxy for Toroidal Magnetic Field
"""
x, y, z = current_state
dx_dt = sigma * (y - x)
dy_dt = x * (rho - z) - y
dz_dt = x * y - beta * z
return [dx_dt, dy_dt, dz_dt]
def generate_plasma_data(n_samples=5000):
"""
Generates synthetic fusion data based on chaotic turbulence.
"""
print(f"Generating {n_samples} physics-informed plasma samples...")
# --- 1. Generate HEALTHY Data (The Attractor) ---
# We simulate a stable plasma orbit on the 'strange attractor'
dt = 0.01
t_span = np.arange(0, n_samples * dt, dt)
initial_state = [1.0, 1.0, 1.0]
# Solve the differential equations
healthy_data = odeint(lorenz_system, initial_state, t_span)
# Add 47 dummy 'noise' sensors to mimic a 50-sensor array
# Real sensors are just correlated versions of the main physics variables
noise_sensors = np.random.normal(0, 0.5, (n_samples, 47))
X_healthy = np.hstack([healthy_data, noise_sensors])
# --- 2. Generate ANOMALOUS Data (The Disruption) ---
# We simulate a 'Locked Mode' by changing the physics parameter 'rho'
# This causes the plasma to drift off the stable manifold
t_span_anom = np.arange(0, (n_samples // 10) * dt, dt)
# rho=15 changes the topology of the attractor (Disruption Precursor)
anomalous_data = odeint(lorenz_system, initial_state, t_span_anom, args=(10, 15, 8/3))
noise_sensors_anom = np.random.normal(0, 0.5, (len(anomalous_data), 47))
X_anomalous = np.hstack([anomalous_data, noise_sensors_anom])
# --- 3. Save to CSV ---
df_healthy = pd.DataFrame(X_healthy, columns=[f'sensor_{i}' for i in range(50)])
df_healthy['label'] = 0 # 0 = Healthy
df_anomalous = pd.DataFrame(X_anomalous, columns=[f'sensor_{i}' for i in range(50)])
df_anomalous['label'] = 1 # 1 = Disruption
df_final = pd.concat([df_healthy, df_anomalous], ignore_index=True)
output_file = "physics_informed_data.csv"
df_final.to_csv(output_file, index=False)
print(f"Success! Saved physics-informed data to {output_file}")
print("Shape:", df_final.shape)
if __name__ == "__main__":
generate_plasma_data()