goad 1.1.9

Compute the single scattering properties of particles much larger than the wavelength of light with geometric optics and aperture diffraction theory.
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
463
464
465
pub mod cli;
pub mod constants;
pub mod loading;
pub mod validation;

use nalgebra::Complex;
use pyo3::prelude::*;
#[cfg(feature = "stub-gen")]
use pyo3_stub_gen::derive::*;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;

use crate::bins::{self, BinningScheme};
use crate::diff::Mapping;
use crate::orientation::Euler;
use crate::orientation::*;
use crate::zones::ZoneConfig;

/// Provides a default empty zones vec.
fn default_zones() -> Vec<ZoneConfig> {
    vec![]
}

/// Configuration for output file generation
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct OutputConfig {
    /// Enable writing of settings.json configuration file
    pub settings_json: bool,
    /// Enable writing of 2D Mueller matrix files
    pub mueller_2d: bool,
    /// Enable writing of 1D integrated Mueller matrix files
    pub mueller_1d: bool,
    /// Enable writing of specific Mueller components
    pub mueller_components: MuellerComponentConfig,
}

/// Configuration for Mueller matrix component outputs
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct MuellerComponentConfig {
    /// Enable total scattering component output
    pub total: bool,
    /// Enable beam component output
    pub beam: bool,
    /// Enable external diffraction component output
    pub external: bool,
}

// Re-export constants, defaults, and loading functions for backward compatibility
pub use self::constants::*;
pub use self::loading::{load_config, load_config_with_cli, load_default_config};

/// Runtime configuration for the application.
#[cfg_attr(feature = "stub-gen", gen_stub_pyclass)]
#[pyclass(module = "goad._goad")]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct Settings {
    pub wavelength: f32,
    pub beam_power_threshold: f32,
    pub beam_area_threshold_fac: f32,
    pub cutoff: f32,
    pub medium_refr_index: Complex<f32>,
    pub particle_refr_index: Vec<Complex<f32>>,
    pub orientation: Orientation,
    pub geom_name: String,
    pub max_rec: i32,
    pub max_tir: i32,
    /// Zones for binning (new format). Takes precedence over `binning`.
    #[serde(default = "default_zones")]
    pub zones: Vec<ZoneConfig>,
    /// Legacy binning field (deprecated, use `zones` instead).
    /// If present and zones is empty, this will be converted to a single zone.
    #[serde(default, skip_serializing)]
    pub binning: Option<BinningScheme>,
    pub seed: Option<u64>,
    /// Problem scaling factor - scales the entire problem (geometry, wavelength, and beam area thresholds)
    #[serde(default = "constants::default_scale_factor")]
    pub scale: f32,
    pub distortion: Option<f32>,
    /// Per-axis geometry scaling [x, y, z] - scales only the geometry in each dimension
    #[serde(default = "constants::default_geom_scale")]
    pub geom_scale: Option<Vec<f32>>,
    #[serde(default = "constants::default_directory")]
    pub directory: PathBuf,
    #[serde(default = "constants::default_fov_factor")]
    pub fov_factor: Option<f32>,
    pub mapping: Mapping,
    #[serde(default = "constants::default_output_config")]
    pub output: OutputConfig,
    pub coherence: bool,
    /// Suppress progress bars and status messages
    #[serde(default = "constants::default_quiet")]
    pub quiet: bool,
}

#[cfg_attr(feature = "stub-gen", gen_stub_pymethods)]
#[pymethods]
impl Settings {
    #[new]
    #[pyo3(signature = (
        geom_path,
        wavelength = DEFAULT_WAVELENGTH,
        particle_refr_index_re = DEFAULT_PARTICLE_REFR_INDEX_RE,
        particle_refr_index_im = DEFAULT_PARTICLE_REFR_INDEX_IM,
        medium_refr_index_re = DEFAULT_MEDIUM_REFR_INDEX_RE,
        medium_refr_index_im = DEFAULT_MEDIUM_REFR_INDEX_IM,
        orientation = None,
        zones = None,
        beam_power_threshold = DEFAULT_BEAM_POWER_THRESHOLD,
        beam_area_threshold_fac = DEFAULT_BEAM_AREA_THRESHOLD_FAC,
        cutoff = DEFAULT_CUTOFF,
        max_rec = DEFAULT_MAX_REC,
        max_tir = DEFAULT_MAX_TIR,
        scale = 1.0,
        distortion = None,
        directory = "goad_run",
        mapping = DEFAULT_MAPPING,
        coherence = DEFAULT_COHERENCE,
        quiet = DEFAULT_QUIET,
        seed = None,
    ))]
    fn py_new(
        geom_path: String,
        wavelength: f32,
        particle_refr_index_re: f32,
        particle_refr_index_im: f32,
        medium_refr_index_re: f32,
        medium_refr_index_im: f32,
        orientation: Option<Orientation>,
        zones: Option<Vec<ZoneConfig>>,
        beam_power_threshold: f32,
        beam_area_threshold_fac: f32,
        cutoff: f32,
        max_rec: i32,
        max_tir: i32,
        scale: f32,
        distortion: Option<f32>,
        directory: &str,
        mapping: Mapping,
        coherence: bool,
        quiet: bool,
        seed: Option<u64>,
    ) -> PyResult<Self> {
        // Input validation
        if wavelength <= 0.0 {
            return Err(pyo3::exceptions::PyValueError::new_err(format!(
                "Wavelength must be positive, got: {}",
                wavelength
            )));
        }

        if !std::path::Path::new(&geom_path).exists() {
            return Err(pyo3::exceptions::PyFileNotFoundError::new_err(format!(
                "Geometry file not found: {}",
                geom_path
            )));
        }

        if cutoff < 0.0 || cutoff > 1.0 {
            return Err(pyo3::exceptions::PyValueError::new_err(format!(
                "Cutoff must be between 0 and 1, got: {}",
                cutoff
            )));
        }

        if max_rec < 0 {
            return Err(pyo3::exceptions::PyValueError::new_err(format!(
                "max_rec must be non-negative, got: {}",
                max_rec
            )));
        }

        if max_tir < 0 {
            return Err(pyo3::exceptions::PyValueError::new_err(format!(
                "max_tir must be non-negative, got: {}",
                max_tir
            )));
        }
        // Create default orientation if none provided (single Sobol orientation)
        let orientation = orientation.unwrap_or_else(|| Orientation {
            scheme: Scheme::Sobol { num_orients: 1 },
            euler_convention: DEFAULT_EULER_ORDER,
        });

        // Use provided zones or create default (single full zone with interval binning)
        let zones = zones.unwrap_or_else(|| {
            vec![ZoneConfig::new(bins::Scheme::Interval {
                thetas: vec![0.0, 5.0, 175.0, 179.0, 180.0],
                theta_spacings: vec![0.1, 2.0, 0.5, 0.1],
                phis: vec![0.0, 360.0],
                phi_spacings: vec![7.5],
            })]
        });

        let mut settings = Settings {
            wavelength,
            beam_power_threshold,
            beam_area_threshold_fac,
            cutoff,
            medium_refr_index: Complex::new(medium_refr_index_re, medium_refr_index_im),
            particle_refr_index: vec![Complex::new(particle_refr_index_re, particle_refr_index_im)],
            orientation,
            geom_name: geom_path,
            max_rec,
            max_tir,
            zones,
            binning: None,
            seed,
            scale,
            distortion,
            geom_scale: None,
            directory: PathBuf::from(directory),
            fov_factor: None,
            mapping,
            output: constants::default_output_config(),
            coherence,
            quiet,
        };

        validation::validate_config(&mut settings);

        Ok(settings)
    }

    /// Set the euler angles
    #[setter]
    fn set_eulers(&mut self, euler: Vec<f32>) {
        self.orientation = Orientation {
            scheme: Scheme::Discrete {
                eulers: vec![Euler::new(euler[0], euler[1], euler[2])],
            },
            euler_convention: EulerConvention::XYZ,
        };
    }

    /// Get the euler angle, assuming the orientation scheme is discrete
    #[getter]
    fn get_eulers(&self) -> Vec<f32> {
        match &self.orientation.scheme {
            Scheme::Discrete { eulers } => vec![eulers[0].alpha, eulers[0].beta, eulers[0].gamma],
            _ => vec![0.0, 0.0, 0.0],
        }
    }

    /// Set the full orientation object
    #[setter]
    fn set_orientation(&mut self, orientation: Orientation) {
        self.orientation = orientation;
    }

    /// Get the full orientation object
    #[getter]
    fn get_orientation(&self) -> Orientation {
        self.orientation.clone()
    }

    /// Set the geometry file path
    #[setter]
    fn set_geom_path(&mut self, geom_path: String) {
        self.geom_name = geom_path;
    }

    /// Get the geometry file path
    #[getter]
    fn get_geom_path(&self) -> String {
        self.geom_name.clone()
    }

    /// Set the wavelength
    #[setter]
    fn set_wavelength(&mut self, wavelength: f32) {
        self.wavelength = wavelength;
    }

    /// Get the wavelength
    #[getter]
    fn get_wavelength(&self) -> f32 {
        self.wavelength
    }

    /// Set the particle refractive index (real part)
    #[setter]
    fn set_particle_refr_index_re(&mut self, re: f32) {
        if !self.particle_refr_index.is_empty() {
            self.particle_refr_index[0].re = re;
        }
    }

    /// Get the particle refractive index (real part)
    #[getter]
    fn get_particle_refr_index_re(&self) -> f32 {
        if !self.particle_refr_index.is_empty() {
            self.particle_refr_index[0].re
        } else {
            0.0
        }
    }

    /// Set the particle refractive index (imaginary part)
    #[setter]
    fn set_particle_refr_index_im(&mut self, im: f32) {
        if !self.particle_refr_index.is_empty() {
            self.particle_refr_index[0].im = im;
        }
    }

    /// Get the particle refractive index (imaginary part)
    #[getter]
    fn get_particle_refr_index_im(&self) -> f32 {
        if !self.particle_refr_index.is_empty() {
            self.particle_refr_index[0].im
        } else {
            0.0
        }
    }

    /// Set the medium refractive index (real part)
    #[setter]
    fn set_medium_refr_index_re(&mut self, re: f32) {
        self.medium_refr_index.re = re;
    }

    /// Get the medium refractive index (real part)
    #[getter]
    fn get_medium_refr_index_re(&self) -> f32 {
        self.medium_refr_index.re
    }

    /// Set the medium refractive index (imaginary part)
    #[setter]
    fn set_medium_refr_index_im(&mut self, im: f32) {
        self.medium_refr_index.im = im;
    }

    /// Get the medium refractive index (imaginary part)
    #[getter]
    fn get_medium_refr_index_im(&self) -> f32 {
        self.medium_refr_index.im
    }

    /// Set the beam power threshold
    #[setter]
    fn set_beam_power_threshold(&mut self, threshold: f32) {
        self.beam_power_threshold = threshold;
    }

    /// Get the beam power threshold
    #[getter]
    fn get_beam_power_threshold(&self) -> f32 {
        self.beam_power_threshold
    }

    /// Set the cutoff
    #[setter]
    fn set_cutoff(&mut self, cutoff: f32) {
        self.cutoff = cutoff;
    }

    /// Get the cutoff
    #[getter]
    fn get_cutoff(&self) -> f32 {
        self.cutoff
    }

    /// Set the max recursion depth
    #[setter]
    fn set_max_rec(&mut self, max_rec: i32) {
        self.max_rec = max_rec;
    }

    /// Get the max recursion depth
    #[getter]
    fn get_max_rec(&self) -> i32 {
        self.max_rec
    }

    /// Set the max TIR bounces
    #[setter]
    fn set_max_tir(&mut self, max_tir: i32) {
        self.max_tir = max_tir;
    }

    /// Get the max TIR bounces
    #[getter]
    fn get_max_tir(&self) -> i32 {
        self.max_tir
    }

    /// Set the zones configuration
    #[setter]
    fn set_zones(&mut self, zones: Vec<ZoneConfig>) {
        self.zones = zones;
    }

    /// Get the zones configuration
    #[getter]
    fn get_zones(&self) -> Vec<ZoneConfig> {
        self.zones.clone()
    }

    /// Set the per-axis geometry scaling [x, y, z]
    #[setter]
    fn set_geom_scale(&mut self, geom_scale: Option<Vec<f32>>) {
        self.geom_scale = geom_scale;
    }

    /// Get the per-axis geometry scaling [x, y, z]
    #[getter]
    fn get_geom_scale(&self) -> Option<Vec<f32>> {
        self.geom_scale.clone()
    }

    /// Set the seed for random number generation
    #[setter]
    fn set_seed(&mut self, seed: Option<u64>) {
        self.seed = seed;
    }

    /// Get the seed for random number generation
    #[getter]
    fn get_seed(&self) -> Option<u64> {
        self.seed
    }

    /// Set the distortion factor
    #[setter]
    fn set_distortion(&mut self, distortion: Option<f32>) {
        self.distortion = distortion;
    }

    /// Get the distortion factor
    #[getter]
    fn get_distortion(&self) -> Option<f32> {
        self.distortion
    }

    /// Set the field of view factor
    #[setter]
    fn set_fov_factor(&mut self, fov_factor: Option<f32>) {
        self.fov_factor = fov_factor;
    }

    /// Get the field of view factor
    #[getter]
    fn get_fov_factor(&self) -> Option<f32> {
        self.fov_factor
    }

    /// Set quiet mode (suppress progress bars)
    #[setter]
    fn set_quiet(&mut self, quiet: bool) {
        self.quiet = quiet;
    }

    /// Get quiet mode
    #[getter]
    fn get_quiet(&self) -> bool {
        self.quiet
    }
}

impl Settings {
    pub fn beam_area_threshold(&self) -> f32 {
        self.wavelength * self.wavelength * self.beam_area_threshold_fac * self.scale.powi(2)
    }
}