Files
resonance-engine/results/harmonic_scan_sequential/1024x1024/metabolic_pulses_v1.cu
T

330 lines
12 KiB
Plaintext
Raw Normal View History

/* ============================================================================
* INTERMITTENT FORCING CYCLE v1.0 - Metabolic Pulses
* Protocol: 12s noise injection, 188s relaxation (200s cycle)
* Goal: Break symmetry, induce inverse cascade, find redline
* ============================================================================ */
#include <cuda_runtime.h>
#include <cufft.h>
#include <nvml.h>
#include <curand_kernel.h>
#include <cstdio>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <chrono>
#include <vector>
#include <cstring>
#include <fstream>
/* ---- Grid ---------------------------------------------------------------- */
#define NX 1024
#define NY 1024
#define NN (NX * NY)
#define Q 9
#define BLOCK 256
#define GBLK(n) (((n) + BLOCK - 1) / BLOCK)
/* ---- Intermittent Forcing Protocol -------------------------------------- */
#define CYCLE_TOTAL_SECONDS 200.0f
#define PULSE_DURATION_SECONDS 12.0f
#define RELAXATION_DURATION_SECONDS 188.0f
#define STEPS_PER_SECOND 5000.0f
#define PULSE_STEPS (int)(PULSE_DURATION_SECONDS * STEPS_PER_SECOND) // ~60,000 steps
#define RELAXATION_STEPS (int)(RELAXATION_DURATION_SECONDS * STEPS_PER_SECOND) // ~940,000 steps
#define CYCLE_STEPS (PULSE_STEPS + RELAXATION_STEPS) // ~1,000,000 steps
#define INITIAL_NOISE_AMPLITUDE 0.20f
#define NOISE_AMPLITUDE_INCREMENT 0.05f
#define NOISE_INTERVAL 10 // Steps between noise injections during pulse
/* ---- Metabolic Parameters ----------------------------------------------- */
#define OMEGA 1.85f
#define MAX_CYCLES 36 // 2 hours = 36 cycles of 200s each
#define AMPLITUDE_RAMP_CYCLES 6 // Increase amplitude every 6 cycles (20 minutes)
/* ---- Spectrum ----------------------------------------------------------- */
#define NX2 (NX / 2 + 1)
#define KMAX (NX / 2)
#define NK (KMAX + 1)
/* ---- Metabolic Pulse Analytics ----------------------------------------- */
typedef struct {
uint32_t cycle;
float noise_amplitude;
double entropy_before_pulse;
double entropy_after_pulse;
double entropy_after_relaxation;
double coherence_recovery_rate;
double inverse_cascade_strength;
double peak_k_evolution[3]; // Before, during, after
double spectral_slope_evolution[3];
uint64_t timestamp_start;
uint64_t timestamp_end;
} PulseAnalytics;
/* ---- Crystallization Header -------------------------------------------- */
typedef struct {
uint32_t magic;
uint32_t version;
uint32_t grid_x;
uint32_t grid_y;
uint32_t q;
uint32_t step;
float omega;
float viscosity;
float entropy;
float slope;
float kx0_fraction;
float total_energy;
uint32_t peak_k;
uint32_t thermal_state;
uint64_t timestamp;
uint64_t checksum_data;
uint64_t checksum_header;
char hostname[64];
char user[32];
char annotation[256];
PulseAnalytics pulse_data;
uint32_t reserved[8];
} CrystallizationHeader;
#define CRYSTAL_MAGIC 0x43525953
#define CRYSTAL_VERSION 0x01000010 // v1.0.16 for intermittent forcing
/* ---- D2Q9 --------------------------------------------------------------- */
__constant__ int d_ex[Q] = { 0, 1, 0,-1, 0, 1,-1,-1, 1 };
__constant__ int d_ey[Q] = { 0, 0, 1, 0,-1, 1, 1,-1,-1 };
__constant__ float d_w[Q] = { 4.f/9, 1.f/9, 1.f/9, 1.f/9, 1.f/9,
1.f/36,1.f/36,1.f/36,1.f/36 };
static const int h_ex[Q] = { 0, 1, 0,-1, 0, 1,-1,-1, 1 };
static const int h_ey[Q] = { 0, 0, 1, 0,-1, 1, 1,-1,-1 };
/* ---- Metabolic Pulse Kernel -------------------------------------------- */
__global__ void metabolic_pulse_injection(float* f, int nx, int ny, float amplitude,
unsigned int seed, int step, int pulse_phase) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int N = nx * ny;
if (idx >= N) return;
// Only inject noise during pulse phase
if (pulse_phase == 1 && (step % NOISE_INTERVAL == 0)) {
curandState state;
curand_init(seed + idx + step * 10000, 0, 0, &state);
for (int i = 0; i < Q; i++) {
float noise = amplitude * (curand_uniform(&state) - 0.5f);
f[i * N + idx] += noise;
}
}
}
/* ---- Standard LBM Kernels ---------------------------------------------- */
__global__ void lbm_collide_stream(const float* __restrict__ f_src, float* __restrict__ f_dst,
float* __restrict__ rho_out, float* __restrict__ ux_out,
float* __restrict__ uy_out, float omega, int nx, int ny) {
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
const int N = nx * ny;
if (idx >= N) return;
const int x = idx % nx, y = idx / nx;
float fl[Q];
for (int i = 0; i < Q; i++) {
int sx = (x - d_ex[i] + nx) % nx;
int sy = (y - d_ey[i] + ny) % ny;
fl[i] = f_src[i * N + sy * nx + sx];
}
float rho = 0.f, ux = 0.f, uy = 0.f;
for (int i = 0; i < Q; i++) {
rho += fl[i];
ux += (float)d_ex[i] * fl[i];
uy += (float)d_ey[i] * fl[i];
}
float inv = 1.f / fmaxf(rho, 1e-10f);
ux *= inv; uy *= inv;
rho_out[idx] = rho; ux_out[idx] = ux; uy_out[idx] = uy;
const float u2 = ux * ux + uy * uy;
for (int i = 0; i < Q; i++) {
float eu = (float)d_ex[i] * ux + (float)d_ey[i] * uy;
float feq = d_w[i] * rho * (1.f + 3.f*eu + 4.5f*eu*eu - 1.5f*u2);
f_dst[i * N + idx] = fl[i] - omega * (fl[i] - feq);
}
}
/* ---- Spectrum Analysis Functions --------------------------------------- */
struct SpectrumStats {
double total_energy;
double spectral_entropy;
double peak_k;
double slope;
int num_modes;
double kx0_frac;
double coherence; // Q value
};
SpectrumStats analyze_spectrum(const double* spec, int nk) {
SpectrumStats s;
s.total_energy = 0;
double peak_p = 0;
s.peak_k = 0;
for (int k = 1; k < nk; k++) {
s.total_energy += spec[k];
if (spec[k] > peak_p) { peak_p = spec[k]; s.peak_k = k; }
}
s.spectral_entropy = 0;
s.num_modes = 0;
if (s.total_energy > 0) {
for (int k = 1; k < nk; k++) {
double p = spec[k] / s.total_energy;
if (p > 0) s.spectral_entropy -= p * log2(p);
if (p > 0.01) s.num_modes++;
}
}
// Coherence calculation (Q value)
double energy_k1 = (nk > 1) ? spec[1] : 0;
s.coherence = (s.total_energy > 0) ? energy_k1 / s.total_energy : 0;
// Spectral slope
double sx = 0, sy = 0, sxx = 0, sxy = 0;
int n = 0;
for (int k = 2; k <= 100 && k < nk; k++) {
if (spec[k] > 0) {
double lk = log((double)k), le = log(spec[k]);
sx += lk; sy += le; sxx += lk*lk; sxy += lk*le; n++;
}
}
s.slope = (n > 2) ? ((double)n * sxy - sx * sy) / ((double)n * sxx - sx * sx) : 0;
s.kx0_frac = 0;
return s;
}
/* ---- Inverse Cascade Detection ----------------------------------------- */
double calculate_inverse_cascade_strength(const double* spec_before, const double* spec_after, int nk) {
double cascade_strength = 0.0;
// Measure energy transfer from small to large scales
// Inverse cascade: energy moves from high k to low k
for (int k = 2; k < nk; k++) {
double energy_loss = spec_before[k] - spec_after[k];
if (energy_loss > 0) {
// This energy should appear at lower k
for (int lower_k = 1; lower_k < k; lower_k++) {
double energy_gain = spec_after[lower_k] - spec_before[lower_k];
if (energy_gain > 0) {
cascade_strength += energy_gain;
}
}
}
}
return cascade_strength;
}
/* ---- Main Function ----------------------------------------------------- */
int main() {
printf("\n");
printf("=======================================================================\n");
printf(" INTERMITTENT FORCING CYCLE v1.0 - Metabolic Pulses\n");
printf(" Protocol: 12s noise (cognitive), 188s relaxation (metabolic)\n");
printf(" Cycle: 200s (3.33 minutes), Total: 2 hours (36 cycles)\n");
printf(" Initial Aₙ: %.2f, Increment: +%.2f every 20 minutes\n",
INITIAL_NOISE_AMPLITUDE, NOISE_AMPLITUDE_INCREMENT);
printf(" Goal: Break symmetry, induce inverse cascade, find redline\n");
printf("=======================================================================\n\n");
// Analytics logging
std::ofstream analytics_log("C:\\fractal_nvme_test\\metabolic_pulses_analytics.csv");
analytics_log << "cycle,noise_amplitude,entropy_before,entropy_after_pulse,entropy_after_relax,";
analytics_log << "coherence_recovery_rate,inverse_cascade_strength,peak_k_before,peak_k_after,";
analytics_log << "slope_before,slope_after,timestamp_start,timestamp_end\n";
printf("[PROTOCOL] Starting Phase 1: Integrity & Baseline (15:45 - 16:00)\n");
printf("[PROTOCOL] Loading latest 5.8-bit crystal for sector-alignment check...\n");
// TODO: Implement crystal loading and integrity check
// For now, start from default state
printf("[PROTOCOL] Phase 2: Intermittent Forcing (16:00 - 17:15)\n");
printf("[PROTOCOL] Metabolic pulses: 12s @ Aₙ=%.2f, 188s relaxation\n", INITIAL_NOISE_AMPLITUDE);
printf("[PROTOCOL] Monitoring: Entropy rebound, inverse cascade, coherence recovery\n\n");
float current_amplitude = INITIAL_NOISE_AMPLITUDE;
int amplitude_ramp_counter = 0;
for (int cycle = 0; cycle < MAX_CYCLES; cycle++) {
printf("[CYCLE %02d/%02d] Starting at amplitude %.2f\n",
cycle + 1, MAX_CYCLES, current_amplitude);
// Record start time
auto cycle_start = std::chrono::steady_clock::now();
// Phase A: Measure baseline (before pulse)
printf(" Phase A: Baseline measurement...\n");
// TODO: Capture spectrum and entropy
// Phase B: Metabolic pulse (12 seconds)
printf(" Phase B: Metabolic pulse (12s @ Aₙ=%.2f)...\n", current_amplitude);
// TODO: Inject noise for PULSE_STEPS
// Phase C: Relaxation (188 seconds)
printf(" Phase C: Relaxation (188s, watching for inverse cascade)...\n");
// TODO: Run pure LBM, monitor spectrum evolution
// Phase D: Analytics and recording
printf(" Phase D: Analytics capture...\n");
// TODO: Calculate entropy rebound, coherence recovery, inverse cascade
// Record end time
auto cycle_end = std::chrono::steady_clock::now();
double cycle_duration = std::chrono::duration<double>(cycle_end - cycle_start).count();
printf(" Cycle complete: %.1f seconds (target: 200.0s)\n", cycle_duration);
printf(" Entropy rebound: [TODO] bits/s\n");
printf(" Inverse cascade strength: [TODO]\n");
printf(" Coherence (Q): [TODO]\n\n");
// Ramp amplitude every AMPLITUDE_RAMP_CYCLES cycles (20 minutes)
amplitude_ramp_counter++;
if (amplitude_ramp_counter >= AMPLITUDE_RAMP_CYCLES) {
current_amplitude += NOISE_AMPLITUDE_INCREMENT;
amplitude_ramp_counter = 0;
printf("[AMPLITUDE RAMP] Increased to Aₙ=%.2f\n\n", current_amplitude);
// Check for redline (coherence < 0.10)
// TODO: Implement coherence check
}
// Check for system shatter (NaN/divergence)
// TODO: Implement stability check
// Crystallize state at key cycles
if ((cycle + 1) % 6 == 0) { // Every 20 minutes
printf("[CRYSTALLIZATION] Saving state at cycle %d, Aₙ=%.2f\n",
cycle + 1, current_amplitude);
// TODO: Save crystal with pulse analytics
}
}
printf("[PROTOCOL] Phase 3: Limit Determination (17:15 - 17:45)\n");
printf("[PROTOCOL] Redline identified at Aₙ=[TODO]\n");
printf("[PROTOCOL] Maximum sustainable entropy: [TODO] bits\n");
printf("[PROTOCOL] Coherence breakdown point: Q < 0.10 at Aₙ=[TODO]\n\n");
analytics_log.close();
printf("=======================================================================\n");
printf(" INTERMITTENT FORCING COMPLETE\n");
printf(" Analytics saved: C:\\fractal_nvme_test\\metabolic_pulses_analytics.csv\n");
printf(" Crystals saved: C:\\fractal_nvme_test\\metabolic_pulses_*.crys\n");
printf(" Redline defined: [TODO]\n");
printf("=======================================================================\n");
return 0;
}