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
//! # Beam
//!
//! Used for pump, signal, idler beams
use crate::{
  crystal::CrystalSetup,
  math::*,
  utils::{
    frequency_to_vacuum_wavelength, frequency_to_wavenumber, vacuum_wavelength_to_frequency,
  },
  PeriodicPoling, *,
};
use dim::ucum::{self, C_, M, RAD};
use na::*;
use std::{
  f64::{self, consts::FRAC_PI_2},
  ops::{Deref, DerefMut},
};
mod beam_waist;
pub use beam_waist::*;

/// Create a unit direction vector from polar coordinates
pub fn direction_from_polar(phi: Angle, theta: Angle) -> Direction {
  let theta_rad = *(theta / ucum::RAD);
  let phi_rad = *(phi / ucum::RAD);
  Unit::new_normalize(Vector3::new(
    f64::sin(theta_rad) * f64::cos(phi_rad),
    f64::sin(theta_rad) * f64::sin(phi_rad),
    f64::cos(theta_rad),
  ))
}

/// PumpBeam wraps the Beam type.
///
/// Ensures we don't get confused about which beam is which.
#[derive(Debug, Clone, PartialEq)]
pub struct PumpBeam(Beam);
impl PumpBeam {
  /// Create a new PumpBeam
  pub fn new(beam: Beam) -> Self {
    Self(beam)
  }
  /// Get the inner Beam
  pub fn as_beam(self) -> Beam {
    self.0
  }
}
impl From<PumpBeam> for Beam {
  fn from(value: PumpBeam) -> Self {
    value.0
  }
}
impl From<Beam> for PumpBeam {
  fn from(value: Beam) -> Self {
    let mut pump = Self::new(value);
    pump.0.set_angles(0. * RAD, 0. * RAD);
    pump
  }
}
impl Deref for PumpBeam {
  type Target = Beam;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}
impl DerefMut for PumpBeam {
  fn deref_mut(&mut self) -> &mut Beam {
    &mut self.0
  }
}

/// SignalBeam wraps the Beam type.
///
/// Ensures we don't get confused about which beam is which.
#[derive(Debug, Clone, PartialEq)]
pub struct SignalBeam(Beam);
impl SignalBeam {
  /// Create a new SignalBeam
  pub fn new(beam: Beam) -> Self {
    Self(beam)
  }
  /// Get the inner Beam
  pub fn as_beam(self) -> Beam {
    self.0
  }
}
impl From<SignalBeam> for Beam {
  fn from(value: SignalBeam) -> Self {
    value.0
  }
}
impl From<Beam> for SignalBeam {
  fn from(value: Beam) -> Self {
    Self::new(value)
  }
}
impl Deref for SignalBeam {
  type Target = Beam;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}
impl DerefMut for SignalBeam {
  fn deref_mut(&mut self) -> &mut Beam {
    &mut self.0
  }
}

/// IdlerBeam wraps the Beam type.
///
/// Ensures we don't get confused about which beam is which.
#[derive(Debug, Clone, PartialEq)]
pub struct IdlerBeam(Beam);
impl IdlerBeam {
  /// Create a new IdlerBeam
  pub fn new(beam: Beam) -> Self {
    Self(beam)
  }
  /// Get the inner Beam
  pub fn as_beam(self) -> Beam {
    self.0
  }
  /// Calculate the optimal idler beam for the given signal and pump beams.
  pub fn try_new_optimum<P: AsRef<PeriodicPoling>>(
    signal: &SignalBeam,
    pump: &PumpBeam,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> Result<Self, SPDCError> {
    let ls = signal.vacuum_wavelength();
    let lp = pump.vacuum_wavelength();

    if ls <= lp {
      return Err(SPDCError(
        "Signal wavelength must be greater than Pump wavelength".into(),
      ));
    }

    let ns = signal.refractive_index(signal.frequency(), crystal_setup);
    let np = pump.refractive_index(pump.frequency(), crystal_setup);
    let k_pp = ls / pp.as_ref().signed_period(); // ls / period
    let theta_s = signal.theta_internal();
    let ns_z = ns * cos(theta_s);
    let ls_over_lp = ls / lp;
    let np_by_ls_over_lp = np * ls_over_lp;

    let arg = (ns * ns)
      + np_by_ls_over_lp.powi(2)
      + 2. * (k_pp * ns_z - np_by_ls_over_lp * ns_z - k_pp * np_by_ls_over_lp)
      + k_pp * k_pp;

    let numerator = ns * sin(theta_s);
    let val = (*numerator) / arg.sqrt();

    // if val > 1.0 || val < 0. {
    //   return Err(SPDCError("Invalid solution for optimal idler theta".into()));
    // }

    let sign = (theta_s / RAD).signum();
    let theta = if (cos(theta_s).signum() < 0.) ^ crystal_setup.counter_propagation {
      PI - f64::asin(val)
    } else {
      f64::asin(val)
    } * sign
      * ucum::RAD;
    let wavelength = ls * lp / (ls - lp);
    let phi = normalize_angle(signal.phi() + PI * RAD);

    Ok(
      Beam::new(
        crystal_setup.pm_type.idler_polarization(),
        phi,
        theta,
        wavelength,
        signal.waist(),
      )
      .into(),
    )
  }
}
impl From<IdlerBeam> for Beam {
  fn from(value: IdlerBeam) -> Self {
    value.0
  }
}
impl From<Beam> for IdlerBeam {
  fn from(value: Beam) -> Self {
    Self::new(value)
  }
}
impl Deref for IdlerBeam {
  type Target = Beam;

  fn deref(&self) -> &Self::Target {
    &self.0
  }
}
impl DerefMut for IdlerBeam {
  fn deref_mut(&mut self) -> &mut Beam {
    &mut self.0
  }
}

/// The Beam type represents pump/signal/idler properties.
///
/// Use with PumpBeam, SignalBeam, IdlerBeam
#[derive(Debug, Clone, PartialEq)]
pub struct Beam {
  waist: BeamWaist,
  frequency: Frequency,
  // the polarization
  polarization: PolarizationType,
  /// (internal) azimuthal angle (-π, π]
  theta: Angle,
  /// polar angle [0, 2Ï€)
  phi: Angle,
  /// direction of propagation
  direction: Direction,
}

impl Beam {
  /// create a beam
  pub fn new<W: Into<BeamWaist>>(
    polarization: PolarizationType,
    phi: Angle,
    theta: Angle,
    vacuum_wavelength: Wavelength,
    waist: W,
  ) -> Self {
    let phi = normalize_angle(phi);
    let theta = normalize_angle_signed(theta);
    Self {
      polarization,
      frequency: vacuum_wavelength_to_frequency(vacuum_wavelength),
      waist: waist.into(),
      phi,
      theta,
      direction: direction_from_polar(phi, theta),
    }
  }

  /// Effective index of refraction induced by periodic poling
  // TODO: double check this...
  pub fn effective_index_of_refraction<P: AsRef<PeriodicPoling>>(
    &self,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> RIndex {
    let lambda_o = self.vacuum_wavelength();
    let n = self.refractive_index(self.frequency, crystal_setup);
    n + *(lambda_o / pp.as_ref().signed_period())
  }

  /// Get the phase velocity of the beam through specified crystal setup
  pub fn phase_velocity<P: AsRef<PeriodicPoling>>(
    &self,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> Speed {
    C_ / self.effective_index_of_refraction(crystal_setup, pp)
  }

  /// Get the group velocity of the beam through specified crystal setup
  pub fn group_velocity<P: AsRef<PeriodicPoling>>(
    &self,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> Speed {
    let lambda_o = self.vacuum_wavelength();
    let n_eff = self.effective_index_of_refraction(crystal_setup, pp);
    let vp = C_ / n_eff;
    let n_of_lambda = move |lambda: f64| {
      *crystal_setup.index_along(lambda * M, self.direction(), self.polarization())
    };
    let dn_by_dlambda = derivative_at(n_of_lambda, *(lambda_o / M));
    vp * (1. + (lambda_o / n_eff) * dn_by_dlambda / M)
  }

  /// Get the group index of the beam through specified crystal setup
  pub fn group_index<P: AsRef<PeriodicPoling>>(
    &self,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> RIndex {
    let vg = self.group_velocity(crystal_setup, pp);
    C_ / vg
  }

  /// Use snell's law to calculate the internal theta from external
  pub fn calc_internal_theta_from_external(
    beam: &Self,
    external: Angle,
    crystal_setup: &CrystalSetup,
  ) -> Angle {
    let snell_external = sin(external);
    let guess = *(external / ucum::RAD);
    let phi = beam.phi();

    let curve = |internal| {
      let direction = direction_from_polar(phi, internal * ucum::RAD);
      let n = crystal_setup.index_along(beam.vacuum_wavelength(), direction, beam.polarization());

      num::abs(snell_external - (*n) * f64::sin(internal))
    };

    let sign = guess.signum();
    // TODO: THIS IS BROKEN FOR BACKWARD PROPAGATION
    // THINK ABOUT LIMITS
    let theta = math::nelder_mead_1d(curve, (guess, guess + 1.), 100, 0., FRAC_PI_2, 1e-12);

    sign * theta * ucum::RAD
  }

  /// Use snell's law to calculate the external theta from internal
  pub fn calc_external_theta_from_internal(
    beam: &Self,
    internal: Angle,
    crystal_setup: &CrystalSetup,
  ) -> Angle {
    let direction = direction_from_polar(beam.phi(), internal);
    let n = crystal_setup.index_along(beam.vacuum_wavelength(), direction, beam.polarization());
    // snells law
    f64::asin(*n * f64::sin(*(internal / ucum::RAD))) * ucum::RAD
  }

  /// make a copy of this photon with a new type
  pub fn with_polarization(self, polarization: PolarizationType) -> Self {
    Self {
      polarization,
      ..self
    }
  }

  /// Get the polarization type (ordinary or extraordinary)
  pub fn polarization(&self) -> PolarizationType {
    self.polarization
  }

  /// Set the polarization type (ordinary or extraordinary)
  pub fn set_polarization(&mut self, polarization: PolarizationType) -> &mut Self {
    self.polarization = polarization;
    self
  }

  /// Get index of refraction along direction of propagation at specified freuency
  pub fn refractive_index(&self, omega: Frequency, crystal_setup: &CrystalSetup) -> RIndex {
    let lambda_o = frequency_to_vacuum_wavelength(omega);
    crystal_setup.index_along(lambda_o, self.direction(), self.polarization())
  }

  /// Get the direction of propagation with respect to the pump
  pub fn direction(&self) -> Direction {
    self.direction
  }

  /// Get the beam waist
  pub fn waist(&self) -> BeamWaist {
    self.waist
  }

  /// Set the beam waist
  pub fn set_waist<W: Into<BeamWaist>>(&mut self, waist: W) -> &mut Self {
    self.waist = waist.into();
    self
  }

  /// Get the polar angle relative to the x-axis
  pub fn phi(&self) -> Angle {
    self.phi
  }

  /// Get the azimuthal angle relative to the z-axis (pump direction) internal to the crystal
  pub fn theta_internal(&self) -> Angle {
    self.theta
  }

  /// Get the azimuthal angle relative to the z-axis (pump direction) external to the crystal
  pub fn theta_external(&self, crystal_setup: &CrystalSetup) -> Angle {
    // snells law
    Self::calc_external_theta_from_internal(self, self.theta, crystal_setup)
  }

  /// Get the wavevector of the beam
  pub fn wavevector(&self, omega: Frequency, crystal_setup: &CrystalSetup) -> Wavevector {
    let n = self.refractive_index(omega, crystal_setup);
    let k = frequency_to_wavenumber(omega, n);
    Wavevector::new(self.direction.into_inner() * *(k * M / RAD))
  }

  /// The center frequency of the beam
  pub fn frequency(&self) -> Frequency {
    self.frequency
  }
  /// Set the center frequency of the beam
  pub fn set_frequency(&mut self, omega: Frequency) -> &mut Self {
    self.frequency = omega;
    self
  }

  /// Vacuum wavelength at the center
  pub fn vacuum_wavelength(&self) -> Wavelength {
    frequency_to_vacuum_wavelength(self.frequency)
  }

  /// Set the vacuum wavelength at the center
  pub fn set_vacuum_wavelength(&mut self, lambda_o: Wavelength) -> &mut Self {
    self.frequency = vacuum_wavelength_to_frequency(lambda_o);
    self
  }

  /// Set phi
  pub fn set_phi(&mut self, phi: Angle) -> &mut Self {
    self.phi = normalize_angle(phi);
    self.update_direction();
    self
  }

  /// Set theta internal
  pub fn set_theta_internal(&mut self, theta: Angle) -> &mut Self {
    self.theta = normalize_angle_signed(theta);
    self.update_direction();
    self
  }

  /// Set the external azimuthal angle
  pub fn set_theta_external(&mut self, external: Angle, crystal_setup: &CrystalSetup) -> &mut Self {
    use dim::Abs;
    let theta = Self::calc_internal_theta_from_external(self, external.abs(), crystal_setup);
    self.set_angles(self.phi, theta);
    self
  }

  /// Set both internal angles
  pub fn set_angles(&mut self, phi: Angle, theta: Angle) -> &mut Self {
    self.phi = normalize_angle(phi);
    self.theta = normalize_angle_signed(theta);
    self.update_direction();
    self
  }

  /// Calculate the spatial walk-off
  /// [See equation (37) of Couteau, Christophe. "Spontaneous parametric down-conversion"](https://arxiv.org/pdf/1809.00127.pdf)
  pub fn walkoff_angle(&self, crystal_setup: &CrystalSetup) -> Angle {
    // ***
    // NOTE: in the original version of the program this was TOTALLY bugged
    // and gave the wrong values completely
    // ***

    // n_{e}(\theta)
    let ne_of_theta = |theta| {
      let mut setup = crystal_setup.clone();
      setup.theta = theta * ucum::RAD;
      *self.refractive_index(self.frequency, &setup)
    };

    // derrivative at theta
    let theta = *(crystal_setup.theta / ucum::RAD);
    let np_prime = derivative_at(ne_of_theta, theta);
    let np = *self.refractive_index(self.frequency, crystal_setup);

    // walkoff \tan(\rho) = -\frac{1}{n_e} \frac{dn_e}{d\theta}
    (-np_prime / np).atan() * ucum::RAD
  }

  /// Get the average transit time through the crystal
  pub fn average_transit_time<P: AsRef<PeriodicPoling>>(
    &self,
    crystal_setup: &CrystalSetup,
    pp: P,
  ) -> Time {
    let crystal_length = crystal_setup.length;
    let delta_z = 0.5 * crystal_length;
    // beam direction in lab frame
    // so crystal length is along z axis
    let direction = self.direction().into_inner();
    // take the direction vector and turn it into a displacement
    // by scaling the vector so that z component becomes delta_z
    let disp = (*(delta_z / M) / direction.z) * direction;
    let distance = disp.norm() * M;

    let vg = self.group_velocity(crystal_setup, pp);
    distance / vg
  }

  fn update_direction(&mut self) {
    self.direction = direction_from_polar(self.phi, self.theta);
  }
}

#[cfg(test)]
mod tests {
  extern crate float_cmp;
  use super::*;
  use crate::utils::*;
  use dim::f64prefixes::*;
  use float_cmp::*;
  use ucum::*;

  fn init() -> (CrystalSetup, Beam) {
    let theta = 3.0 * DEG;
    let phi = 2.0 * DEG;
    let wavelength = 1550. * NANO * M;
    let waist = BeamWaist::new(100.0 * MICRO * M);
    let crystal_setup = CrystalSetup {
      crystal: CrystalType::BBO_1,
      pm_type: PMType::Type2_e_eo,
      theta: -3.0 * DEG,
      phi: 1.0 * DEG,
      length: 2_000.0 * MICRO * M,
      temperature: from_celsius_to_kelvin(20.0),
      counter_propagation: false,
    };

    let signal = Beam::new(
      PolarizationType::Extraordinary,
      phi,
      theta,
      wavelength,
      waist,
    );

    (crystal_setup, signal)
  }

  #[test]
  fn beam_direction_test() {
    let (crystal_setup, signal) = init();
    let crystal_rotation = Rotation3::from_euler_angles(
      0.,
      *(crystal_setup.theta / ucum::RAD),
      *(crystal_setup.phi / ucum::RAD),
    );
    let s = signal.direction();
    let dir = crystal_rotation * s;
    let expected = Vector3::new(
      -0.00006370990344706924,
      0.0018256646987702438,
      0.9999983314433358,
    );
    assert!(
      approx_eq!(f64, dir.x, expected.x, ulps = 2),
      "actual: {}, expected: {}",
      dir.x,
      expected.x
    );
    assert!(
      approx_eq!(f64, dir.y, expected.y, ulps = 2),
      "actual: {}, expected: {}",
      dir.y,
      expected.y
    );
    assert!(
      approx_eq!(f64, dir.z, expected.z, ulps = 2),
      "actual: {}, expected: {}",
      dir.z,
      expected.z
    );
  }

  #[test]
  fn beam_refractive_index_test() {
    let (crystal_setup, signal) = init();
    let n = signal.refractive_index(signal.frequency(), &crystal_setup);
    let expected = 1.6465859604517012;
    assert!(
      approx_eq!(f64, *n, expected, ulps = 2),
      "actual: {}, expected: {}",
      *n,
      expected
    )
  }

  #[test]
  fn beam_external_angle_test_for_zero() {
    let (crystal_setup, signal) = init();
    let theta = Beam::calc_internal_theta_from_external(&signal, 0. * ucum::DEG, &crystal_setup);
    assert_eq!(*(theta / ucum::RAD), 0.);
  }

  #[test]
  fn beam_external_angle_test() {
    let (crystal_setup, signal) = init();
    let theta = Beam::calc_internal_theta_from_external(&signal, 13. * ucum::DEG, &crystal_setup);
    let theta_external = Beam::calc_external_theta_from_internal(&signal, theta, &crystal_setup);
    let actual = *(theta_external / ucum::DEG);
    let expected = 13.;
    assert!(
      approx_eq!(f64, actual, expected, ulps = 2, epsilon = 1e-9),
      "actual: {}, expected: {}",
      actual,
      expected
    );
  }

  #[test]
  fn beam_external_angle_test_from_internal() {
    let (crystal_setup, signal) = init();
    let theta_external = signal.theta_external(&crystal_setup);
    let theta = Beam::calc_internal_theta_from_external(&signal, theta_external, &crystal_setup);
    let actual = *(theta / ucum::DEG);
    let expected = *(signal.theta_internal() / ucum::DEG);
    assert!(
      approx_eq!(f64, actual, expected, ulps = 2, epsilon = 1e-6),
      "actual: {}, expected: {}",
      actual,
      expected
    );
  }

  #[test]
  fn optimum_idler_test() {
    let spdc = crate::utils::testing::testing_props(false);
    let idler = spdc.idler;

    let li = *(idler.vacuum_wavelength() / ucum::M);
    let li_expected = 1550. * NANO;
    assert!(
      approx_eq!(f64, li, li_expected, ulps = 2),
      "actual: {}, expected: {}",
      li,
      li_expected
    );

    let phi_i = *(idler.phi() / ucum::DEG);
    let phi_i_expected = 195.0;
    assert!(
      approx_eq!(f64, phi_i, phi_i_expected, ulps = 2),
      "actual: {}, expected: {}",
      phi_i,
      phi_i_expected
    );

    let theta_i = *(idler.theta_internal() / ucum::DEG);
    let theta_i_expected = 0.4912283553443823;
    assert!(
      approx_eq!(f64, theta_i, theta_i_expected, ulps = 2, epsilon = 1e-3),
      "actual: {}, expected: {}",
      theta_i,
      theta_i_expected
    );
  }

  #[test]
  fn walkoff_test() {
    let mut crystal_setup = SPDC::default().crystal_setup;
    crystal_setup.crystal = CrystalType::BBO_1;
    crystal_setup.theta = 31.603728550521122 * ucum::DEG;

    let pump = Beam::new(
      PolarizationType::Extraordinary,
      0. * DEG,
      0. * DEG,
      775. * NANO * M,
      100. * MICRO * M,
    );

    let expected = 0.06674608819804856;
    let actual = *(pump.walkoff_angle(&crystal_setup) / ucum::RAD);

    assert!(
      approx_eq!(f64, actual, expected, ulps = 2, epsilon = 1e-8),
      "actual: {}, expected: {}",
      actual,
      expected
    );
  }
}