rdpe 0.1.0

Reaction Diffusion Particle Engine - GPU particle simulations made easy
Documentation
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
//! # Biological Neural Network Simulation
//!
//! 100,000+ neurons forming a brain-like structure with realistic
//! activation dynamics, wave propagation, and emergent synchronization.
//!
//! ## Architecture
//!
//! - **Cortical structure**: Layered organization like real brain tissue
//! - **Excitatory neurons** (80%): Spread activation to neighbors
//! - **Inhibitory neurons** (20%): Dampen local activity (prevents seizures)
//! - **Pacemaker cells**: Spontaneous firing to seed activity
//!
//! ## Dynamics
//!
//! - Membrane potential accumulates from neighbor activity
//! - Fires when threshold crossed (Rule::Sync)
//! - Refractory period prevents immediate re-firing
//! - Activity waves propagate through the network
//!
//! ## Controls
//!
//! - Adjust excitability, inhibition balance, wave speed
//! - Watch for spontaneous pattern formation
//! - See how parameter changes affect network dynamics
//!
//! Run with: `cargo run --example neural_network --release --features egui`

use rand::Rng;
use rdpe::prelude::*;
use std::sync::{Arc, Mutex};

#[derive(Particle, Clone)]
struct Neuron {
    position: Vec3,
    velocity: Vec3,
    #[color]
    color: Vec3,
    /// Membrane potential (accumulates toward threshold)
    potential: f32,
    /// Refractory timer (can't fire while > 0)
    refractory: f32,
    /// Neuron type: 0 = excitatory, 1 = inhibitory, 2 = pacemaker
    neuron_type: u32,
    /// Intrinsic firing rate (varies per neuron)
    intrinsic_rate: f32,
    /// Current activation level (for visualization)
    activation: f32,
    /// Synaptic fatigue (reduces output when high - prevents seizures)
    fatigue: f32,
    /// Adaptive threshold (increases after firing - stabilizes network)
    adaptation: f32,
}

struct NeuralParams {
    // Network dynamics
    excitability: f32,
    inhibition_strength: f32,
    threshold: f32,
    refractory_period: f32,
    decay_rate: f32,

    // Propagation
    coupling_strength: f32,
    propagation_speed: f32,

    // Visualization
    volume_density: f32,

    // Stimulation
    stimulate: bool,
}

impl Default for NeuralParams {
    fn default() -> Self {
        Self {
            // Tuned for "edge of criticality" - interesting dynamics without seizure
            excitability: 0.8,           // Slightly reduced
            inhibition_strength: 1.0,    // Increased for better balance
            threshold: 0.6,              // Lower threshold = more responsive
            refractory_period: 0.15,     // Slightly longer = more stable
            decay_rate: 3.0,             // Faster leak = harder to accumulate
            coupling_strength: 0.35,     // Slightly reduced coupling
            propagation_speed: 0.8,      // Slower propagation
            volume_density: 6.0,
            stimulate: false,
        }
    }
}

fn main() {
    let mut rng = rand::thread_rng();

    let state = Arc::new(Mutex::new(NeuralParams::default()));
    let ui_state = state.clone();
    let update_state = state.clone();

    // Build a brain-like structure
    // Multiple cortical "columns" arranged in a curved sheet
    let num_neurons = 120_000;

    let particles: Vec<Neuron> = (0..num_neurons)
        .map(|i| {
            // Create a folded cortical sheet (like brain gyri)
            let t = i as f32 / num_neurons as f32;

            // Base position on a curved surface
            let theta = t * std::f32::consts::TAU * 3.0; // Wind around
            let phi = (t * 7.0).sin() * 0.5 + 0.5; // Undulating

            // Cortical sheet coordinates
            let sheet_x = theta.cos() * (0.4 + phi * 0.3);
            let sheet_z = theta.sin() * (0.4 + phi * 0.3);
            let sheet_y = (theta * 2.0).sin() * 0.15 + (theta * 5.0).cos() * 0.08;

            // Add cortical depth (6 layers, roughly)
            let layer = rng.gen_range(0.0..1.0_f32);
            let depth = layer * 0.12; // Cortical thickness

            // Radial direction for depth
            let radial = Vec3::new(sheet_x, 0.0, sheet_z).normalize();

            // Final position with noise
            let pos = Vec3::new(
                sheet_x + radial.x * depth + rng.gen_range(-0.02..0.02),
                sheet_y + rng.gen_range(-0.03..0.03) + layer * 0.05,
                sheet_z + radial.z * depth + rng.gen_range(-0.02..0.02),
            );

            // Neuron type distribution (80% excitatory, 15% inhibitory, 5% pacemaker)
            let type_roll: f32 = rng.gen();
            let neuron_type = if type_roll < 0.05 {
                2 // Pacemaker
            } else if type_roll < 0.20 {
                1 // Inhibitory
            } else {
                0 // Excitatory
            };

            // Color by type
            let color = match neuron_type {
                0 => Vec3::new(0.2, 0.3, 0.5), // Excitatory: blue-ish (dim)
                1 => Vec3::new(0.5, 0.2, 0.3), // Inhibitory: red-ish (dim)
                _ => Vec3::new(0.4, 0.5, 0.2), // Pacemaker: yellow-green (dim)
            };

            // Intrinsic rate (pacemakers have higher rates)
            let intrinsic_rate = match neuron_type {
                2 => rng.gen_range(0.3..0.8),  // Pacemakers fire spontaneously
                _ => rng.gen_range(0.0..0.1),  // Others mostly respond to input
            };

            // Random initial potential (some neurons start closer to threshold)
            let potential = rng.gen_range(0.0..0.5);

            Neuron {
                position: pos,
                velocity: Vec3::ZERO,
                color,
                potential,
                refractory: 0.0,
                neuron_type,
                intrinsic_rate,
                activation: 0.0,
                fatigue: 0.0,
                adaptation: 0.0,
            }
        })
        .collect();

    Simulation::<Neuron>::new()
        .with_particle_count(num_neurons)
        .with_particle_size(0.004)
        .with_bounds(1.0)
        .with_spawner(move |ctx| particles[ctx.index as usize].clone())
        .with_spatial_config(0.1, 64)
        // Neural activity field (for local signal propagation)
        // Excitation decays quickly and spreads moderately
        .with_field(
            "activity",
            FieldConfig::new(80)
                .with_extent(1.0)
                .with_decay(0.85)  // Faster decay - signals don't linger
                .with_blur(0.2)
                .with_blur_iterations(2),
        )
        // Inhibitory field - spreads FASTER and WIDER than excitation
        // This is key for stability (inhibition must "catch up" to excitation)
        .with_field(
            "inhibition",
            FieldConfig::new(64)
                .with_extent(1.0)
                .with_decay(0.80)  // Faster decay too
                .with_blur(0.5)    // Much wider spread!
                .with_blur_iterations(3),
        )
        // Volume render the activity
        .with_volume_render(
            VolumeConfig::new()
                .with_field(0)
                .with_steps(64)
                .with_density_scale(6.0)
                .with_palette(Palette::Inferno)
                .with_threshold(0.01)
                .with_additive(true),
        )
        // Uniforms - matched to default params
        .with_uniform::<f32>("excitability", 0.8)
        .with_uniform::<f32>("inhibition_strength", 1.0)
        .with_uniform::<f32>("threshold", 0.6)
        .with_uniform::<f32>("refractory_period", 0.15)
        .with_uniform::<f32>("decay_rate", 3.0)
        .with_uniform::<f32>("coupling_strength", 0.35)
        .with_uniform::<f32>("propagation_speed", 0.8)
        .with_uniform::<f32>("stimulate", 0.0)
        .with_uniform::<f32>("stim_x", 0.0)
        .with_uniform::<f32>("stim_y", 0.0)
        // UI
        .with_ui(move |ctx| {
            let mut s = ui_state.lock().unwrap();

            egui::Window::new("Neural Network")
                .default_pos([10.0, 10.0])
                .default_width(300.0)
                .show(ctx, |ui| {
                    ui.heading("Network Dynamics");

                    ui.add(
                        egui::Slider::new(&mut s.excitability, 0.3..=1.5)
                            .text("Excitability")
                    );
                    ui.add(
                        egui::Slider::new(&mut s.inhibition_strength, 0.3..=2.0)
                            .text("Inhibition")
                    );
                    ui.label("Homeostatic: network self-stabilizes!");

                    ui.separator();
                    ui.add(
                        egui::Slider::new(&mut s.threshold, 0.3..=1.0)
                            .text("Fire Threshold")
                    );
                    ui.add(
                        egui::Slider::new(&mut s.refractory_period, 0.05..=0.3)
                            .text("Refractory Period")
                    );
                    ui.add(
                        egui::Slider::new(&mut s.decay_rate, 1.0..=5.0)
                            .text("Potential Decay")
                    );

                    ui.separator();
                    ui.heading("Propagation");
                    ui.add(
                        egui::Slider::new(&mut s.coupling_strength, 0.1..=0.8)
                            .text("Coupling")
                    );
                    ui.add(
                        egui::Slider::new(&mut s.propagation_speed, 0.3..=2.0)
                            .text("Wave Speed")
                    );

                    ui.separator();
                    ui.heading("Visualization");
                    ui.add(
                        egui::Slider::new(&mut s.volume_density, 2.0..=15.0)
                            .text("Glow Intensity")
                    );

                    ui.separator();
                    ui.label("Left-click to stimulate a region!");

                    ui.separator();
                    ui.heading("Neuron Counts");
                    ui.label("Total: 120,000");
                    ui.label("Excitatory: ~96,000 (80%)");
                    ui.label("Inhibitory: ~18,000 (15%)");
                    ui.label("Pacemakers: ~6,000 (5%)");

                    if ui.button("Reset to Defaults").clicked() {
                        *s = NeuralParams::default();
                    }

                    if ui.button("Trigger Wave").clicked() {
                        s.stimulate = true;
                    }
                });
        })
        .with_update(move |ctx| {
            let mut s = update_state.lock().unwrap();
            ctx.set("excitability", s.excitability);
            ctx.set("inhibition_strength", s.inhibition_strength);
            ctx.set("threshold", s.threshold);
            ctx.set("refractory_period", s.refractory_period);
            ctx.set("decay_rate", s.decay_rate);
            ctx.set("coupling_strength", s.coupling_strength);
            ctx.set("propagation_speed", s.propagation_speed);

            // Handle mouse stimulation
            if ctx.input.mouse_held(MouseButton::Left) {
                let mouse = ctx.input.mouse_ndc();
                ctx.set("stimulate", 1.0);
                ctx.set("stim_x", mouse.x);
                ctx.set("stim_y", mouse.y);
            } else if s.stimulate {
                ctx.set("stimulate", 1.0);
                ctx.set("stim_x", 0.0);
                ctx.set("stim_y", 0.0);
                s.stimulate = false;
            } else {
                ctx.set("stimulate", 0.0);
            }
        })
        // Neural dynamics with homeostatic mechanisms
        .with_rule(Rule::Custom(r#"
            // === HOMEOSTATIC DECAY ===
            // Fatigue recovers slowly (synaptic vesicle replenishment)
            p.fatigue = max(0.0, p.fatigue - uniforms.delta_time * 0.5);

            // Adaptation decays back to baseline (ion channel recovery)
            p.adaptation = max(0.0, p.adaptation - uniforms.delta_time * 0.8);

            // Decrease refractory timer
            p.refractory = max(0.0, p.refractory - uniforms.delta_time);

            // Skip if in refractory period
            if p.refractory > 0.0 {
                p.activation = max(0.0, p.activation - uniforms.delta_time * 3.0);
                return;
            }

            // === READ FIELD INPUTS ===
            let excitation = field_read(0u, p.position);
            let inhibition = field_read(1u, p.position);

            // Net input with E/I balance
            // Inhibition is stronger and acts as a "brake"
            let net_input = excitation * uniforms.excitability
                          - inhibition * uniforms.inhibition_strength * 1.5;

            // Intrinsic activity (pacemakers generate their own input)
            // Reduced when fatigued
            let fatigue_factor = 1.0 - p.fatigue * 0.8;
            let intrinsic = p.intrinsic_rate * uniforms.delta_time * fatigue_factor;

            // External stimulation
            var stim = 0.0;
            if uniforms.stimulate > 0.5 {
                let stim_pos = vec3<f32>(uniforms.stim_x * 0.8, uniforms.stim_y * 0.5, 0.0);
                let stim_dist = length(p.position - stim_pos);
                if stim_dist < 0.25 {
                    stim = (0.25 - stim_dist) * 3.0;
                }
            }

            // === ACCUMULATE POTENTIAL ===
            // Only accumulate positive input (soft rectification)
            let input = max(0.0, net_input) + intrinsic + stim;
            p.potential += input * uniforms.coupling_strength * uniforms.propagation_speed;

            // Soft saturation - potential can't exceed 2x threshold (prevents runaway)
            let max_potential = uniforms.threshold * 2.0;
            p.potential = min(p.potential, max_potential);

            // Leak (decay toward resting potential)
            p.potential = p.potential * (1.0 - uniforms.decay_rate * uniforms.delta_time);

            // === ADAPTIVE THRESHOLD ===
            // Effective threshold increases with recent activity
            let effective_threshold = uniforms.threshold * (1.0 + p.adaptation * 0.5);

            // === CHECK FOR FIRING ===
            if p.potential >= effective_threshold {
                // FIRE!
                p.potential = 0.0;
                p.refractory = uniforms.refractory_period;
                p.activation = 1.0;

                // Increase fatigue and adaptation (homeostatic response)
                p.fatigue = min(1.0, p.fatigue + 0.3);
                p.adaptation = min(1.0, p.adaptation + 0.2);

                // Emit to field - OUTPUT REDUCED BY FATIGUE
                let output_strength = 1.0 - p.fatigue * 0.6;

                if p.neuron_type == 1u {
                    // Inhibitory neuron - emit to inhibition field
                    // Inhibitory neurons are slightly LESS affected by fatigue
                    field_write(1u, p.position, 0.9 * (1.0 - p.fatigue * 0.4));
                } else {
                    // Excitatory or pacemaker - emit to activity field
                    field_write(0u, p.position, output_strength * 0.8);
                }
            }

            // === VISUALIZATION ===
            p.activation = max(0.0, p.activation - uniforms.delta_time * 4.0);

            // Base colors (slightly brighter for better visibility)
            var base_color = vec3<f32>(0.12, 0.18, 0.35); // Excitatory: blue
            if p.neuron_type == 1u {
                base_color = vec3<f32>(0.3, 0.12, 0.18); // Inhibitory: red
            } else if p.neuron_type == 2u {
                base_color = vec3<f32>(0.18, 0.28, 0.12); // Pacemaker: green
            }

            // Firing colors
            var fire_color = vec3<f32>(1.0, 0.95, 0.6); // Yellow-white flash
            if p.neuron_type == 1u {
                fire_color = vec3<f32>(1.0, 0.5, 0.6); // Inhibitory: pink flash
            }

            // Show potential buildup AND fatigue state
            let potential_glow = p.potential / effective_threshold * 0.25;
            let fatigue_dim = 1.0 - p.fatigue * 0.3; // Fatigued neurons look dimmer

            p.color = mix(base_color * fatigue_dim, fire_color, p.activation + potential_glow);
        "#.into()))
        // Gentle drift to prevent static structure
        .with_rule(Rule::Wander {
            strength: 0.02,
            frequency: 0.2,
        })
        .with_rule(Rule::Drag(5.0))
        .with_rule(Rule::SpeedLimit { min: 0.0, max: 0.05 })
        .with_rule(Rule::BounceWalls { restitution: 1.0 })
        .with_visuals(|v| {
            v.blend_mode(BlendMode::Additive);
            v.background(Vec3::new(0.02, 0.02, 0.03));
        })
        .run().expect("Simulation failed");
}