spdcalc 2.0.1

SPDCalc, the Spontaneous Parametric Downconversion Calculator
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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
use super::*;
use crate::{
  beam::Beam,
  beam::IdlerBeam,
  beam::PumpBeam,
  beam::SignalBeam,
  dim::{
    f64prefixes::{MICRO, NANO, PICO},
    ucum::{DEG, M, MILLIW, RAD, V},
  },
  math::sigfigs,
  utils::{self, from_kelvin_to_celsius},
  CrystalSetup, CrystalType, PMType, SPDCError,
};

mod periodic_poling_config;
pub use periodic_poling_config::PeriodicPolingConfig;
mod apodization;
pub use apodization::ApodizationConfig;
use serde_with::{serde_as, DisplayFromStr};

const SIG_FIGS_IN_CONFIG: u8 = 4;

/// A config parameter the could be signaled to be automatically calculated
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(untagged)]
pub enum AutoCalcParam<T>
where
  T: 'static,
{
  /// "auto"
  Auto(String),
  /// A parameter
  Param(T),
}

impl<T> AutoCalcParam<T> {
  /// Returns true if the parameter is set to be automatically calculated
  pub fn is_auto(&self) -> bool {
    matches!(self, Self::Auto(_))
  }
}

impl<T> Default for AutoCalcParam<T> {
  fn default() -> Self {
    Self::Auto("auto".into())
  }
}

impl<T> From<AutoCalcParam<T>> for Option<T> {
  fn from(param: AutoCalcParam<T>) -> Self {
    match param {
      AutoCalcParam::Param(x) => Some(x),
      AutoCalcParam::Auto(_) => None,
    }
  }
}

impl<T> From<Option<T>> for AutoCalcParam<T> {
  fn from(param: Option<T>) -> Self {
    match param {
      Some(x) => AutoCalcParam::Param(x),
      None => AutoCalcParam::Auto("auto".into()),
    }
  }
}

/// Flat configuration of crystal for ease of import/export
#[serde_as]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CrystalConfig {
  /// The type of crystal
  pub kind: CrystalType,
  /// The type of phase matching
  #[serde_as(as = "DisplayFromStr")]
  pub pm_type: PMType,
  /// The polar angle of the crystal in degrees
  #[serde(default)]
  pub phi_deg: f64,
  /// The azimuthal angle of the crystal in degrees
  #[serde(default)]
  pub theta_deg: AutoCalcParam<f64>,
  /// The length of the crystal in micrometers
  pub length_um: f64,
  /// The temperature of the crystal in degrees Celsius
  pub temperature_c: f64,
  /// Whether the downconversion is counter-propagating
  #[serde(default)]
  pub counter_propagation: bool,
}

impl Default for CrystalConfig {
  fn default() -> Self {
    Self {
      kind: CrystalType::KTP,
      pm_type: PMType::Type2_e_eo,
      phi_deg: 0.,
      theta_deg: AutoCalcParam::Auto("auto".into()),
      length_um: 2_000.,
      temperature_c: 20.,
      counter_propagation: false,
    }
  }
}

/// Converts without autocalculating theta
impl From<CrystalConfig> for CrystalSetup {
  fn from(cfg: CrystalConfig) -> Self {
    let theta = if let AutoCalcParam::Param(theta_deg) = cfg.theta_deg {
      theta_deg * DEG
    } else {
      0. * DEG
    };
    CrystalSetup {
      crystal: cfg.kind,
      pm_type: cfg.pm_type,
      phi: cfg.phi_deg * DEG,
      theta,
      length: cfg.length_um * MICRO * M,
      temperature: utils::from_celsius_to_kelvin(cfg.temperature_c),
      counter_propagation: cfg.counter_propagation,
    }
  }
}

impl From<CrystalSetup> for CrystalConfig {
  fn from(setup: CrystalSetup) -> Self {
    Self {
      kind: setup.crystal,
      pm_type: setup.pm_type,
      theta_deg: AutoCalcParam::Param(sigfigs(*(setup.theta / DEG), SIG_FIGS_IN_CONFIG)),
      phi_deg: sigfigs(*(setup.phi / DEG), SIG_FIGS_IN_CONFIG),
      length_um: sigfigs(*(setup.length / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      temperature_c: sigfigs(
        from_kelvin_to_celsius(setup.temperature),
        SIG_FIGS_IN_CONFIG,
      ),
      counter_propagation: setup.counter_propagation,
    }
  }
}

/// Flat configuration of pump for ease of import/export
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PumpConfig {
  /// The wavelength of the pump in nanometers
  pub wavelength_nm: f64,
  /// The waist of the pump in micrometers
  pub waist_um: f64,
  /// The bandwidth of the pump in nanometers
  pub bandwidth_nm: f64,
  /// The average power of the pump in milliwatts
  pub average_power_mw: f64,
  /// If unset, defaults to 1e-2
  pub spectrum_threshold: Option<f64>,
}

impl Default for PumpConfig {
  fn default() -> Self {
    Self {
      wavelength_nm: 775.,
      waist_um: 100.,
      bandwidth_nm: 5.53,
      average_power_mw: 1.,
      spectrum_threshold: Some(1e-2),
    }
  }
}

impl PumpConfig {
  /// Converts to a [`PumpBeam`] with the given [`CrystalSetup`]
  pub fn as_beam(self, crystal_setup: &CrystalSetup) -> PumpBeam {
    Beam::new(
      crystal_setup.pm_type.pump_polarization(),
      0. * RAD,
      0. * RAD,
      self.wavelength_nm * NANO * M,
      self.waist_um * MICRO * M,
    )
    .into()
  }
}

/// Flat configuration of signal for ease of import/export
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SignalConfig {
  /// The wavelength of the signal in nanometers
  pub wavelength_nm: f64,
  /// The polar angle of the signal in degrees
  #[serde(default)]
  pub phi_deg: f64,
  // one of...
  /// The azimuthal angle of the signal in degrees (conflicts with `theta_external_deg`)
  pub theta_deg: Option<f64>,
  /// The azimuthal angle of the signal in degrees outside the crystal (conflicts with `theta_deg`)
  pub theta_external_deg: Option<f64>,
  /// The waist of the signal in micrometers
  pub waist_um: f64,
  /// The position of the waist of the signal in micrometers
  #[serde(default)]
  pub waist_position_um: AutoCalcParam<f64>,
}

impl Default for SignalConfig {
  fn default() -> Self {
    Self {
      wavelength_nm: 1550.,
      phi_deg: 0.,
      theta_deg: Some(0.),
      theta_external_deg: None,
      waist_um: 100.,
      waist_position_um: AutoCalcParam::default(),
    }
  }
}

impl SignalConfig {
  /// Converts to a [`SignalBeam`] with the given [`CrystalSetup`]
  pub fn try_as_beam(self, crystal_setup: &CrystalSetup) -> Result<SignalBeam, SPDCError> {
    let phi = self.phi_deg * DEG;
    let mut beam = Beam::new(
      crystal_setup.pm_type.signal_polarization(),
      phi,
      0. * RAD,
      self.wavelength_nm * NANO * M,
      self.waist_um * MICRO * M,
    );
    match (self.theta_deg, self.theta_external_deg) {
      (Some(theta), None) => beam.set_angles(phi, theta * DEG),
      (None, Some(theta_e)) => beam.set_theta_external(theta_e * DEG, crystal_setup),
      _ => {
        return Err(SPDCError(
          "Must specify one of theta_deg or theta_external_deg".into(),
        ))
      }
    };

    Ok(beam.into())
  }
}

/// Flat configuration of idler for ease of import/export
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct IdlerConfig {
  /// The wavelength of the idler in nanometers
  pub wavelength_nm: f64,
  /// The polar angle of the idler in degrees
  #[serde(default)]
  pub phi_deg: f64,
  // one of...
  /// The azimuthal angle of the idler in degrees (conflicts with `theta_external_deg`)
  pub theta_deg: Option<f64>,
  /// The azimuthal angle of the idler in degrees outside the crystal (conflicts with `theta_deg`)
  pub theta_external_deg: Option<f64>,
  /// The waist of the idler in micrometers
  pub waist_um: f64,
  /// The position of the waist of the idler in micrometers
  #[serde(default)]
  pub waist_position_um: AutoCalcParam<f64>,
}

impl IdlerConfig {
  /// Converts to a [`IdlerBeam`] with the given [`CrystalSetup`]
  pub fn try_as_beam(self, crystal_setup: &CrystalSetup) -> Result<IdlerBeam, SPDCError> {
    let phi = self.phi_deg * DEG;
    let mut beam = Beam::new(
      crystal_setup.pm_type.idler_polarization(),
      phi,
      0. * RAD,
      self.wavelength_nm * NANO * M,
      self.waist_um * MICRO * M,
    );
    match (self.theta_deg, self.theta_external_deg) {
      (Some(theta), None) => beam.set_angles(phi, theta * DEG),
      (None, Some(theta_e)) => beam.set_theta_external(theta_e * DEG, crystal_setup),
      _ => {
        return Err(SPDCError(
          "Must specify one of theta_deg or theta_external_deg".into(),
        ))
      }
    };

    Ok(beam.into())
  }
}

/// Flat configuration of SPDC for ease of import/export
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SPDCConfig {
  /// The crystal configuration
  pub crystal: CrystalConfig,
  /// The pump configuration
  pub pump: PumpConfig,
  /// The signal configuration
  pub signal: SignalConfig,
  /// The idler configuration
  #[serde(default)]
  pub idler: AutoCalcParam<IdlerConfig>,
  /// The configuration for the periodic poling
  #[serde(default)]
  pub periodic_poling: PeriodicPolingConfig,
  /// The deff of the crystal in pm/V
  pub deff_pm_per_volt: f64,
}

impl TryFrom<SPDCConfig> for SPDC {
  type Error = SPDCError;

  fn try_from(config: SPDCConfig) -> Result<Self, Self::Error> {
    config.try_as_spdc()
  }
}

impl SPDCConfig {
  /// Converts to a [`SPDC`] with the given [`CrystalSetup`]
  pub fn try_as_spdc(self) -> Result<SPDC, SPDCError> {
    let deff = self.deff_pm_per_volt * PICO * M / V;
    let pump_spectrum_threshold = self.pump.spectrum_threshold.unwrap_or(1e-2);
    let crystal_theta_autocalc = self.crystal.theta_deg.is_auto();
    let signal_waist_position_um = self.signal.waist_position_um.clone();
    let pump_bandwidth = self.pump.bandwidth_nm * NANO * M;
    let pump_average_power = self.pump.average_power_mw * MILLIW;
    let mut crystal_setup: CrystalSetup = self.crystal.into();
    let pump = self.pump.as_beam(&crystal_setup);
    let signal = self.signal.try_as_beam(&crystal_setup)?;
    let periodic_poling =
      self
        .periodic_poling
        .try_as_periodic_poling(&signal, &pump, &crystal_setup)?;

    if crystal_theta_autocalc {
      if periodic_poling == PeriodicPoling::Off {
        crystal_setup.assign_optimum_theta(&signal, &pump);
      } else {
        return Err(SPDCError("Can not autocalc theta when periodic poling is enabled. Provide an explicit value for crystal theta.".into()));
      }
    }
    let idler = match &self.idler {
      AutoCalcParam::Param(idler_cfg) => idler_cfg.clone().try_as_beam(&crystal_setup)?,
      AutoCalcParam::Auto(_) => {
        IdlerBeam::try_new_optimum(&signal, &pump, &crystal_setup, &periodic_poling)?
      }
    };
    let idler_waist_position = {
      let auto = AutoCalcParam::default();
      let autocalc_idler_waist = match self.idler {
        AutoCalcParam::Param(cfg) => cfg.waist_position_um,
        AutoCalcParam::Auto(_) => auto,
      };
      match autocalc_idler_waist {
        AutoCalcParam::Param(focus_um) => -focus_um.abs() * MICRO * M,
        AutoCalcParam::Auto(_) => {
          crystal_setup.optimal_waist_position(idler.vacuum_wavelength(), idler.polarization())
        }
      }
    };
    let signal_waist_position = match signal_waist_position_um {
      AutoCalcParam::Param(focus_um) => -focus_um.abs() * MICRO * M,
      AutoCalcParam::Auto(_) => {
        crystal_setup.optimal_waist_position(signal.vacuum_wavelength(), signal.polarization())
      }
    };

    Ok(SPDC::new(
      crystal_setup,
      signal,
      idler,
      pump,
      pump_bandwidth,
      pump_average_power,
      pump_spectrum_threshold,
      periodic_poling,
      signal_waist_position,
      idler_waist_position,
      deff,
    ))
  }
}

impl Default for SPDCConfig {
  fn default() -> Self {
    Self {
      crystal: CrystalConfig::default(),
      pump: PumpConfig::default(),
      signal: SignalConfig::default(),
      idler: AutoCalcParam::default(),
      periodic_poling: PeriodicPolingConfig::default(),
      deff_pm_per_volt: 1.,
    }
  }
}

impl From<SPDC> for PumpConfig {
  fn from(spdc: SPDC) -> Self {
    Self {
      wavelength_nm: sigfigs(
        *(spdc.pump.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      bandwidth_nm: sigfigs(*(spdc.pump_bandwidth / (NANO * M)), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.pump.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      average_power_mw: sigfigs(*(spdc.pump_average_power / MILLIW), SIG_FIGS_IN_CONFIG),
      spectrum_threshold: Some(spdc.pump_spectrum_threshold),
    }
  }
}

impl From<SPDC> for SignalConfig {
  fn from(spdc: SPDC) -> Self {
    Self {
      wavelength_nm: sigfigs(
        *(spdc.signal.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      theta_deg: Some(sigfigs(
        *(spdc.signal.theta_internal() / DEG),
        SIG_FIGS_IN_CONFIG,
      )),
      theta_external_deg: None,
      phi_deg: sigfigs(*(spdc.signal.phi() / DEG), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.signal.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      waist_position_um: AutoCalcParam::Param(sigfigs(
        *(spdc.signal_waist_position / (MICRO * M)),
        SIG_FIGS_IN_CONFIG,
      )),
    }
  }
}

impl From<SPDC> for IdlerConfig {
  fn from(spdc: SPDC) -> Self {
    Self {
      wavelength_nm: sigfigs(
        *(spdc.idler.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      theta_deg: Some(sigfigs(
        *(spdc.idler.theta_internal() / DEG),
        SIG_FIGS_IN_CONFIG,
      )),
      theta_external_deg: None,
      phi_deg: sigfigs(*(spdc.idler.phi() / DEG), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.idler.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      waist_position_um: AutoCalcParam::Param(*(spdc.idler_waist_position / (MICRO * M))),
    }
  }
}

impl From<SPDC> for SPDCConfig {
  fn from(spdc: SPDC) -> Self {
    let crystal = spdc.crystal_setup.into();
    let pump = PumpConfig {
      wavelength_nm: sigfigs(
        *(spdc.pump.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      bandwidth_nm: sigfigs(*(spdc.pump_bandwidth / (NANO * M)), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.pump.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      average_power_mw: sigfigs(*(spdc.pump_average_power / MILLIW), SIG_FIGS_IN_CONFIG),
      spectrum_threshold: Some(spdc.pump_spectrum_threshold),
    };
    let signal = SignalConfig {
      wavelength_nm: sigfigs(
        *(spdc.signal.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      theta_deg: Some(sigfigs(
        *(spdc.signal.theta_internal() / DEG),
        SIG_FIGS_IN_CONFIG,
      )),
      theta_external_deg: None,
      phi_deg: sigfigs(*(spdc.signal.phi() / DEG), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.signal.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      waist_position_um: AutoCalcParam::Param(sigfigs(
        *(spdc.signal_waist_position / (MICRO * M)),
        SIG_FIGS_IN_CONFIG,
      )),
    };

    let idler = AutoCalcParam::Param(IdlerConfig {
      wavelength_nm: sigfigs(
        *(spdc.idler.vacuum_wavelength() / (NANO * M)),
        SIG_FIGS_IN_CONFIG,
      ),
      theta_deg: Some(sigfigs(
        *(spdc.idler.theta_internal() / DEG),
        SIG_FIGS_IN_CONFIG,
      )),
      theta_external_deg: None,
      phi_deg: sigfigs(*(spdc.idler.phi() / DEG), SIG_FIGS_IN_CONFIG),
      waist_um: sigfigs(*(spdc.idler.waist().x / (MICRO * M)), SIG_FIGS_IN_CONFIG),
      waist_position_um: AutoCalcParam::Param(*(spdc.idler_waist_position / (MICRO * M))),
    });

    let periodic_poling = spdc.pp.into();
    let deff_pm_per_volt = sigfigs(*(spdc.deff / (PICO * M / V)), SIG_FIGS_IN_CONFIG);

    Self {
      crystal,
      pump,
      signal,
      idler,
      periodic_poling,
      deff_pm_per_volt,
    }
  }
}

#[cfg(test)]
mod test {
  use super::*;
  use crate::utils::testing::*;
  use crate::PolarizationType;
  use dim::ucum;
  use float_cmp::approx_eq;
  use serde_json::json;

  #[test]
  fn auto_idler_auto_focus_test() {
    let json = json!({
      "crystal": {
        "kind": "BBO_1",
        "pm_type": "e->eo",
        "phi_deg": 0,
        "theta_deg": 0,
        "length_um": 2000,
        "temperature_c": 20
      },
      "pump": {
        "wavelength_nm": 775,
        "waist_um": 100,
        "bandwidth_nm": 5.35,
        "average_power_mw": 1
      },
      "signal": {
        "wavelength_nm": 1550,
        "phi_deg": 0,
        "theta_external_deg": 0,
        "waist_um": 100,
        "waist_position_um": "auto"
      },
      "idler": "auto",
      "deff_pm_per_volt": 1
    });

    let config: SPDCConfig = serde_json::from_value(json).expect("Could not unwrap json");
    let actual = config
      .try_as_spdc()
      .expect("Could not convert to SPDC instance");

    let expected = {
      let crystal_setup = CrystalSetup {
        crystal: CrystalType::BBO_1,
        pm_type: PMType::Type2_e_eo,
        phi: 0. * RAD,
        theta: 0. * RAD,
        length: 2000. * MICRO * M,
        temperature: 293.15 * ucum::K,
        counter_propagation: false,
      };
      let signal = Beam::new(
        PolarizationType::Extraordinary,
        0. * DEG,
        0. * RAD,
        1550. * NANO * M,
        100. * MICRO * M,
      )
      .into();
      let idler = Beam::new(
        PolarizationType::Ordinary,
        180. * DEG,
        0. * DEG,
        1550. * NANO * M,
        100. * MICRO * M,
      )
      .into();
      let pump = Beam::new(
        PolarizationType::Extraordinary,
        0. * DEG,
        0. * DEG,
        775. * NANO * M,
        100. * MICRO * M,
      )
      .into();
      let pump_average_power = 1. * MILLIW;
      let pump_bandwidth = 5.35 * NANO * M;
      let periodic_poling = PeriodicPoling::Off;
      let signal_waist_position = -0.0006073170564963904 * M;
      let idler_waist_position = -0.0006073170564963904 * M;
      let deff = 1. * PICO * M / V;
      let pump_spectrum_threshold = 1e-2;
      SPDC::new(
        crystal_setup,
        signal,
        idler,
        pump,
        pump_bandwidth,
        pump_average_power,
        pump_spectrum_threshold,
        periodic_poling,
        signal_waist_position,
        idler_waist_position,
        deff,
      )
    };
    dbg!(&actual);
    assert_eq!(actual, expected);
  }

  #[test]
  fn autocalc_crystal_theta_test() {
    let json = json!({
      "crystal": {
        "kind": "KTP",
        "pm_type": "e->eo",
        "phi_deg": 0,
        "theta_deg": "auto",
        "length_um": 2000,
        "temperature_c": 20
      },
      "pump": {
        "wavelength_nm": 775,
        "bandwidth_nm": 5.35,
        "waist_um": 100,
        "average_power_mw": 1
      },
      "signal": {
        "wavelength_nm": 1550,
        "phi_deg": 0,
        "theta_external_deg": 0,
        "waist_um": 100,
        "waist_position_um": "auto"
      },
      "idler": "auto",
      "deff_pm_per_volt": 7.6,
    });

    let config: SPDCConfig = serde_json::from_value(json).expect("Could not unwrap json");
    let spdc = config
      .try_as_spdc()
      .expect("Could not convert to SPDC instance");

    let actual = spdc.crystal_setup.theta;
    let expected = spdc.crystal_setup.optimum_theta(&spdc.signal, &spdc.pump);

    assert_nearly_equal!("crystal theta", actual.value_unsafe, expected.value_unsafe);
  }

  #[test]
  fn auto_pp_test() {
    let json = json!({
      "crystal": {
        "kind": "KTP",
        "pm_type": "e->eo",
        "phi_deg": 0,
        "theta_deg": 90,
        "length_um": 2000,
        "temperature_c": 20
      },
      "pump": {
        "wavelength_nm": 775,
        "bandwidth_nm": 5.35,
        "waist_um": 100,
        "average_power_mw": 1
      },
      "signal": {
        "wavelength_nm": 1550,
        "phi_deg": 0,
        "theta_external_deg": 0,
        "waist_um": 100,
        "waist_position_um": "auto"
      },
      "idler": "auto",
      "periodic_poling": {
        "poling_period_um": "auto",
      },
      "deff_pm_per_volt": 7.6,
    });

    let config: SPDCConfig = serde_json::from_value(json).expect("Could not unwrap json");
    let spdc = config
      .try_as_spdc()
      .expect("Could not convert to SPDC instance");

    let actual = match spdc.pp {
      PeriodicPoling::Off => panic!("Periodic poling should be on"),
      PeriodicPoling::On { period, .. } => period,
    };
    let expected = 46.52987937008885 * MICRO * M;

    assert!(approx_eq!(
      f64,
      actual.value_unsafe,
      expected.value_unsafe,
      epsilon = 1e-6
    ));
  }
}