eulumdat-goniosim 0.7.0

CPU Monte Carlo photon tracer for virtual goniophotometry
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Monte Carlo photon tracer.

use crate::detector::Detector;
use crate::material::Interaction;
use crate::ray::Photon;
use crate::scene::Scene;
use nalgebra::Point3;
use rand::SeedableRng;
use rand_xoshiro::Xoshiro256PlusPlus;
use std::time::{Duration, Instant};

/// Configuration for a trace run.
#[derive(Debug, Clone)]
pub struct TracerConfig {
    /// Total photons to trace.
    pub num_photons: u64,
    /// Maximum interactions per photon before termination.
    pub max_bounces: u32,
    /// Energy threshold below which Russian roulette kicks in.
    pub russian_roulette_threshold: f64,
    /// RNG seed for reproducibility.
    pub seed: u64,
    /// Detector C-angle resolution in degrees.
    pub detector_c_resolution: f64,
    /// Detector gamma resolution in degrees.
    pub detector_g_resolution: f64,
    /// Number of photon trails to record for visualization (0 to disable).
    pub max_trails: usize,
}

impl Default for TracerConfig {
    fn default() -> Self {
        Self {
            num_photons: 1_000_000,
            max_bounces: 50,
            russian_roulette_threshold: 0.01,
            seed: 42,
            detector_c_resolution: 1.0,
            detector_g_resolution: 1.0,
            max_trails: 0,
        }
    }
}

/// Result of a completed trace.
#[derive(Debug, Clone)]
pub struct TracerResult {
    /// Filled detector with accumulated photon data.
    pub detector: Detector,
    /// Statistics about the trace run.
    pub stats: TracerStats,
    /// Recorded photon trails for visualization.
    pub trails: Vec<PhotonTrail>,
}

/// Statistics about a completed trace.
#[derive(Debug, Clone, Default)]
pub struct TracerStats {
    pub photons_traced: u64,
    pub photons_detected: u64,
    pub photons_absorbed: u64,
    pub photons_max_bounces: u64,
    pub photons_russian_roulette: u64,
    pub total_energy_emitted: f64,
    pub total_energy_detected: f64,
    pub elapsed: Duration,
}

/// Progress information for the progress callback.
#[derive(Debug, Clone)]
pub struct ProgressInfo {
    pub photons_done: u64,
    pub photons_total: u64,
    pub photons_per_second: f64,
    pub current_stats: TracerStats,
}

/// A recorded photon path for visualization.
#[derive(Debug, Clone)]
pub struct PhotonTrail {
    pub points: Vec<TrailPoint>,
}

/// A point along a photon's path.
#[derive(Debug, Clone)]
pub struct TrailPoint {
    pub position: Point3<f64>,
    pub event: TrailEvent,
}

/// Type of event at a trail point.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TrailEvent {
    Emitted,
    Reflected,
    Transmitted,
    Scattered,
    Absorbed,
    Detected,
}

/// The Monte Carlo photon tracer.
pub struct Tracer;

impl Tracer {
    /// Trace all photons through the scene.
    pub fn trace(scene: &Scene, config: &TracerConfig) -> TracerResult {
        Self::trace_with_progress(scene, config, |_| {})
    }

    /// Trace with a progress callback (called periodically).
    pub fn trace_with_progress(
        scene: &Scene,
        config: &TracerConfig,
        callback: impl Fn(ProgressInfo) + Send + Sync,
    ) -> TracerResult {
        let start = Instant::now();

        #[cfg(feature = "parallel")]
        let result = trace_parallel(scene, config, &callback, start);

        #[cfg(not(feature = "parallel"))]
        let result = trace_sequential(scene, config, &callback, start);

        result
    }
}

/// Trace photons sequentially (single-threaded).
#[cfg(not(feature = "parallel"))]
fn trace_sequential(
    scene: &Scene,
    config: &TracerConfig,
    callback: &(impl Fn(ProgressInfo) + Send + Sync),
    start: Instant,
) -> TracerResult {
    let mut rng = Xoshiro256PlusPlus::seed_from_u64(config.seed);
    let mut detector = Detector::new(config.detector_c_resolution, config.detector_g_resolution);
    let mut stats = TracerStats::default();
    let mut trails = Vec::new();

    let batch_size = 10_000u64;
    let num_sources = scene.sources.len();
    assert!(num_sources > 0, "Scene must have at least one source");

    for i in 0..config.num_photons {
        // Round-robin across sources
        let source = &scene.sources[(i as usize) % num_sources];
        let ray = source.sample(&mut rng);
        let record_trail = trails.len() < config.max_trails;

        let result = trace_one_photon(scene, config, ray, &mut rng, record_trail);

        stats.total_energy_emitted += 1.0;
        stats.photons_traced += 1;

        match result.outcome {
            PhotonOutcome::Detected { energy } => {
                detector.record(result.final_direction.as_ref().unwrap(), energy);
                stats.photons_detected += 1;
                stats.total_energy_detected += energy;
            }
            PhotonOutcome::Absorbed => {
                stats.photons_absorbed += 1;
            }
            PhotonOutcome::MaxBounces => {
                stats.photons_max_bounces += 1;
            }
            PhotonOutcome::RussianRoulette => {
                stats.photons_russian_roulette += 1;
            }
        }

        if let Some(trail) = result.trail {
            trails.push(trail);
        }

        // Progress callback
        if (i + 1) % batch_size == 0 || i + 1 == config.num_photons {
            let elapsed = start.elapsed();
            stats.elapsed = elapsed;
            callback(ProgressInfo {
                photons_done: i + 1,
                photons_total: config.num_photons,
                photons_per_second: (i + 1) as f64 / elapsed.as_secs_f64(),
                current_stats: stats.clone(),
            });
        }
    }

    stats.elapsed = start.elapsed();

    TracerResult {
        detector,
        stats,
        trails,
    }
}

/// Trace photons in parallel using Rayon.
#[cfg(feature = "parallel")]
fn trace_parallel(
    scene: &Scene,
    config: &TracerConfig,
    callback: &(impl Fn(ProgressInfo) + Send + Sync),
    start: Instant,
) -> TracerResult {
    use rayon::prelude::*;
    use std::sync::atomic::{AtomicU64, Ordering};

    let num_threads = rayon::current_num_threads();
    let photons_per_thread = config.num_photons / num_threads as u64;
    let progress_counter = AtomicU64::new(0);

    let thread_results: Vec<(Detector, TracerStats, Vec<PhotonTrail>)> = (0..num_threads)
        .into_par_iter()
        .map(|thread_idx| {
            let mut rng =
                Xoshiro256PlusPlus::seed_from_u64(config.seed.wrapping_add(thread_idx as u64));
            let mut detector =
                Detector::new(config.detector_c_resolution, config.detector_g_resolution);
            let mut stats = TracerStats::default();
            let mut trails = Vec::new();
            let num_sources = scene.sources.len();

            // First thread gets any remainder photons
            let n = if thread_idx == 0 {
                photons_per_thread + (config.num_photons % num_threads as u64)
            } else {
                photons_per_thread
            };

            for i in 0..n {
                let source_idx =
                    ((thread_idx as u64 * photons_per_thread + i) as usize) % num_sources;
                let source = &scene.sources[source_idx];
                let ray = source.sample(&mut rng);
                let record_trail = thread_idx == 0 && trails.len() < config.max_trails;

                let result = trace_one_photon(scene, config, ray, &mut rng, record_trail);

                stats.total_energy_emitted += 1.0;
                stats.photons_traced += 1;

                match result.outcome {
                    PhotonOutcome::Detected { energy } => {
                        detector.record(result.final_direction.as_ref().unwrap(), energy);
                        stats.photons_detected += 1;
                        stats.total_energy_detected += energy;
                    }
                    PhotonOutcome::Absorbed => stats.photons_absorbed += 1,
                    PhotonOutcome::MaxBounces => stats.photons_max_bounces += 1,
                    PhotonOutcome::RussianRoulette => stats.photons_russian_roulette += 1,
                }

                if let Some(trail) = result.trail {
                    trails.push(trail);
                }

                // Progress (only thread 0 reports)
                if thread_idx == 0 && (i + 1) % 10_000 == 0 {
                    let total_done = progress_counter.load(Ordering::Relaxed) + i + 1;
                    let elapsed = start.elapsed();
                    callback(ProgressInfo {
                        photons_done: total_done,
                        photons_total: config.num_photons,
                        photons_per_second: total_done as f64 / elapsed.as_secs_f64(),
                        current_stats: stats.clone(),
                    });
                }
            }

            progress_counter.fetch_add(n, Ordering::Relaxed);
            (detector, stats, trails)
        })
        .collect();

    // Merge results
    let mut detector = Detector::new(config.detector_c_resolution, config.detector_g_resolution);
    let mut stats = TracerStats::default();
    let mut trails = Vec::new();

    for (d, s, t) in thread_results {
        detector.merge(&d);
        stats.photons_traced += s.photons_traced;
        stats.photons_detected += s.photons_detected;
        stats.photons_absorbed += s.photons_absorbed;
        stats.photons_max_bounces += s.photons_max_bounces;
        stats.photons_russian_roulette += s.photons_russian_roulette;
        stats.total_energy_emitted += s.total_energy_emitted;
        stats.total_energy_detected += s.total_energy_detected;
        trails.extend(t);
    }

    stats.elapsed = start.elapsed();

    TracerResult {
        detector,
        stats,
        trails,
    }
}

// ---------------------------------------------------------------------------
// Single photon tracing
// ---------------------------------------------------------------------------

enum PhotonOutcome {
    Detected { energy: f64 },
    Absorbed,
    MaxBounces,
    RussianRoulette,
}

struct SinglePhotonResult {
    outcome: PhotonOutcome,
    final_direction: Option<nalgebra::Vector3<f64>>,
    trail: Option<PhotonTrail>,
}

fn trace_one_photon(
    scene: &Scene,
    config: &TracerConfig,
    initial_ray: crate::ray::Ray,
    rng: &mut Xoshiro256PlusPlus,
    record_trail: bool,
) -> SinglePhotonResult {
    use rand::Rng;

    let mut photon = Photon::new(initial_ray);
    let mut trail_points = if record_trail {
        vec![TrailPoint {
            position: photon.ray.origin,
            event: TrailEvent::Emitted,
        }]
    } else {
        Vec::new()
    };

    loop {
        // Find nearest intersection
        let hit = scene.intersect(&photon.ray, 1e-6, 1e10);

        match hit {
            None => {
                // Photon escaped — record on detector
                if record_trail {
                    let far_point = photon.ray.at(1.0);
                    trail_points.push(TrailPoint {
                        position: far_point,
                        event: TrailEvent::Detected,
                    });
                }
                return SinglePhotonResult {
                    outcome: PhotonOutcome::Detected {
                        energy: photon.energy,
                    },
                    final_direction: Some(*photon.ray.direction.as_ref()),
                    trail: if record_trail {
                        Some(PhotonTrail {
                            points: trail_points,
                        })
                    } else {
                        None
                    },
                };
            }

            Some(hit) => {
                let material = scene.material(hit.material);
                let interaction = material.interact(&photon, &hit, rng);

                match interaction {
                    Interaction::Absorbed => {
                        if record_trail {
                            trail_points.push(TrailPoint {
                                position: hit.point,
                                event: TrailEvent::Absorbed,
                            });
                        }
                        return SinglePhotonResult {
                            outcome: PhotonOutcome::Absorbed,
                            final_direction: None,
                            trail: if record_trail {
                                Some(PhotonTrail {
                                    points: trail_points,
                                })
                            } else {
                                None
                            },
                        };
                    }

                    Interaction::Reflected {
                        new_ray,
                        attenuation,
                    } => {
                        if record_trail {
                            trail_points.push(TrailPoint {
                                position: hit.point,
                                event: TrailEvent::Reflected,
                            });
                        }
                        photon.ray = new_ray;
                        photon.energy *= attenuation;
                    }

                    Interaction::Transmitted {
                        new_ray,
                        attenuation,
                    } => {
                        if record_trail {
                            trail_points.push(TrailPoint {
                                position: hit.point,
                                event: TrailEvent::Transmitted,
                            });
                        }
                        photon.ray = new_ray;
                        photon.energy *= attenuation;
                    }
                }

                photon.bounces += 1;

                // Max bounces check
                if photon.bounces >= config.max_bounces {
                    return SinglePhotonResult {
                        outcome: PhotonOutcome::MaxBounces,
                        final_direction: None,
                        trail: if record_trail {
                            Some(PhotonTrail {
                                points: trail_points,
                            })
                        } else {
                            None
                        },
                    };
                }

                // Russian roulette
                if photon.energy < config.russian_roulette_threshold {
                    let survive_prob = photon.energy / config.russian_roulette_threshold;
                    if rng.random::<f64>() > survive_prob {
                        return SinglePhotonResult {
                            outcome: PhotonOutcome::RussianRoulette,
                            final_direction: None,
                            trail: if record_trail {
                                Some(PhotonTrail {
                                    points: trail_points,
                                })
                            } else {
                                None
                            },
                        };
                    }
                    // Survived — boost energy to compensate
                    photon.energy = config.russian_roulette_threshold;
                }
            }
        }
    }
}