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
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! General utilities
use crate::math::lerp;
use crate::{Frequency, RIndex, Speed, Wavelength, Wavenumber, TWO_PI};
use dim::ucum::{self, C_, ONE, RAD};
use dim::{ucum::UCUM, Dimensioned};
use na::{Unit, Vector3};
use rayon::iter::IntoParallelIterator;

/// Extension to facilitate getting the x, y, and z components of a dimensioned vector
pub trait DimVector {
  /// The type of the units
  type Units;
  /// Get the x component as a dimensioned value
  fn x(&self) -> Self::Units;
  /// Get the y component as a dimensioned value
  fn y(&self) -> Self::Units;
  /// Get the z component as a dimensioned value
  fn z(&self) -> Self::Units;
}

impl<U> DimVector for UCUM<Vector3<f64>, U> {
  type Units = UCUM<f64, U>;
  fn x(&self) -> Self::Units {
    UCUM::new(self.value_unsafe().x)
  }
  fn y(&self) -> Self::Units {
    UCUM::new(self.value_unsafe().y)
  }
  fn z(&self) -> Self::Units {
    UCUM::new(self.value_unsafe().z)
  }
}

/// Get the dimensioned vector from a scalar value and a direction.
pub fn dim_vector<U>(val: UCUM<f64, U>, dir: Unit<Vector3<f64>>) -> UCUM<Vector3<f64>, U> {
  UCUM::<Vector3<f64>, U>::new(*val.value_unsafe() * *dir)
}

/// convert from celsius to kelvin
pub fn from_celsius_to_kelvin(c: f64) -> ucum::Kelvin<f64> {
  ucum::Kelvin::new(c + 273.15)
}

/// convert from kelvin to celsius
pub fn from_kelvin_to_celsius(k: ucum::Kelvin<f64>) -> f64 {
  *(k / ucum::K) - 273.15
}

/// Get the wavenumber of light at frequency in a medium with refractive index
pub fn frequency_to_wavenumber(omega: Frequency, n: RIndex) -> Wavenumber {
  n * omega / C_
}

/// Get the frequency of light with wavenumber in a medium with refractive index
pub fn wavenumber_to_frequency(k: Wavenumber, n: RIndex) -> Frequency {
  k * C_ / n
}

/// Get the frequency of light at wavelength in a medium with refractive index
pub fn wavelength_to_frequency(lambda: Wavelength, n: RIndex) -> Frequency {
  TWO_PI * RAD * C_ / (lambda * n)
}

/// Get the wavelength of light at frequency in a medium with refractive index
pub fn frequency_to_wavelength(omega: Frequency, n: RIndex) -> Wavelength {
  TWO_PI * RAD * C_ / (omega * n)
}

/// Get the phase velocity of light from frequency and wavenumber
pub fn phase_velocity(omega: Frequency, k: Wavenumber) -> Speed {
  omega / k
}

/// Get the frequency of light from its vacuum wavelength
pub fn vacuum_wavelength_to_frequency(lambda: Wavelength) -> Frequency {
  wavelength_to_frequency(lambda, ONE)
}

/// Get the vacuum wavelength of light from its frequency
pub fn frequency_to_vacuum_wavelength(omega: Frequency) -> Wavelength {
  frequency_to_wavelength(omega, ONE)
}

/// Utility for creating evenly spaced steps between two endpoints over floating point numbers
///
/// ## Example:
/// ```
/// use spdcalc::utils::Steps;
///
/// let arr : Vec<f64> = Steps(0., 1., 100).into_iter().collect();
/// assert_eq!(arr.len(), 100);
/// assert!((arr[0] - 0.).abs() < 1e-12);
/// assert!((arr[99] - 1.).abs() < 1e-12);
/// ```
#[derive(Debug, Copy, Clone, Hash, Serialize, Deserialize)]
pub struct Steps<T>(pub T, pub T, pub usize);

impl<T> From<(T, T, usize)> for Steps<T> {
  fn from(args: (T, T, usize)) -> Self {
    Self(args.0, args.1, args.2)
  }
}

impl<T> Steps<T>
where
  T: std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  /// Starting value
  #[inline(always)]
  pub fn start(&self) -> T {
    self.0
  }
  /// Ending value
  #[inline(always)]
  pub fn end(&self) -> T {
    self.1
  }
  /// Number of steps
  #[inline(always)]
  pub fn steps(&self) -> usize {
    self.2
  }
  /// Number of steps
  #[inline(always)]
  pub fn len(&self) -> usize {
    self.steps()
  }
  /// Is this empty?
  #[inline(always)]
  pub fn is_empty(&self) -> bool {
    self.steps() == 0
  }
  /// Get the range as a tuple
  #[inline(always)]
  pub fn range(&self) -> (T, T) {
    (self.start(), self.end())
  }
  /// Get the number of divisions (steps - 1)
  #[inline(always)]
  pub fn divisions(&self) -> usize {
    self.steps() - 1
  }

  /// Get the value at a given index
  pub fn value(&self, index: usize) -> T {
    let (start, end) = self.range();
    // if we have only one step... then just set progress to be zero
    if self.steps() > 1 {
      let index = index as f64;
      let d = self.divisions() as f64;
      (start * (d - index) + end * index) / d
    } else {
      start
    }
  }

  /// Get the width of the gap between each step.
  ///
  /// ## Example:
  /// ```
  /// use spdcalc::utils::Steps;
  ///
  /// let steps = Steps(0., 4., 3); // 3 steps: |0| --- |2| --- |4|
  /// let dx = 2.;
  /// assert!((steps.division_width() - dx).abs() < 1e-12);
  /// ```
  pub fn division_width(&self) -> T {
    (self.end() - self.start()) / (self.divisions() as f64)
  }
}

impl<T> IntoIterator for Steps<T>
where
  T: std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = T;
  type IntoIter = Iterator1D<T>;

  fn into_iter(self) -> Self::IntoIter {
    Iterator1D {
      steps: self,
      index: 0,
      index_back: self.steps(),
    }
  }
}

impl<T> IntoParallelIterator for Steps<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = T;
  type Iter = ParIterator1D<T>;

  fn into_par_iter(self) -> Self::Iter {
    ParIterator1D { steps: self }
  }
}

/// Iterator for 1D steps
pub struct Iterator1D<T> {
  steps: Steps<T>,
  index: usize,
  index_back: usize,
}

impl<T> Iterator for Iterator1D<T>
where
  T: std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = T;

  fn next(&mut self) -> Option<Self::Item> {
    if self.index >= self.index_back {
      return None;
    }

    let value = self.steps.value(self.index);
    self.index += 1;

    Some(value)
  }
}

impl<T> ExactSizeIterator for Iterator1D<T>
where
  T: std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  fn len(&self) -> usize {
    self.steps.len()
  }
}

impl<T> DoubleEndedIterator for Iterator1D<T>
where
  T: std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  fn next_back(&mut self) -> Option<Self::Item> {
    if self.index_back <= self.index {
      return None;
    }

    self.index_back -= 1;
    Some(self.steps.value(self.index_back))
  }
}

/// Parallel iterator for 1D steps
pub struct ParIterator1D<T> {
  steps: Steps<T>,
}

impl<T> rayon::iter::plumbing::Producer for ParIterator1D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = T;
  type IntoIter = Iterator1D<T>;

  fn into_iter(self) -> Self::IntoIter {
    self.steps.into_iter()
  }

  fn split_at(self, index: usize) -> (Self, Self) {
    let (start, end) = self.steps.range();
    let s1 = self.steps.value(index - 1);
    let s2 = self.steps.value(index);
    (
      ParIterator1D {
        steps: Steps(start, s1, index),
      },
      ParIterator1D {
        steps: Steps(s2, end, self.steps.steps() - index),
      },
    )
  }
}

impl<T> rayon::iter::ParallelIterator for ParIterator1D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = T;

  fn drive_unindexed<C>(self, consumer: C) -> C::Result
  where
    C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
  {
    rayon::iter::plumbing::bridge(self, consumer)
  }

  fn opt_len(&self) -> Option<usize> {
    Some(self.steps.steps())
  }
}

impl<T> rayon::iter::IndexedParallelIterator for ParIterator1D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  fn len(&self) -> usize {
    self.steps.steps()
  }

  fn drive<C>(self, consumer: C) -> C::Result
  where
    C: rayon::iter::plumbing::Consumer<Self::Item>,
  {
    rayon::iter::plumbing::bridge(self, consumer)
  }

  fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(
    self,
    callback: CB,
  ) -> CB::Output {
    callback.callback(self)
  }
}

/// Utility for creating evenly spaced steps between two endpoints in 2 dimensions
///
/// ## Example:
/// ```
/// use spdcalc::utils::Steps2D;
///
/// let arr : Vec<(f64, f64)> = Steps2D((0., 1., 100), (0., 100., 100)).into_iter().collect();
/// assert_eq!(arr.len(), 100 * 100);
/// assert!((arr[0].0 - 0.).abs() < 1e-12);
/// assert!((arr[99].0 - 1.).abs() < 1e-12);
/// assert!((arr[0].1 - 0.).abs() < 1e-12);
/// assert!((arr[99].1 - 0.).abs() < 1e-12);
/// assert!((arr[9999].0 - 1.).abs() < 1e-12);
/// assert!((arr[9999].1 - 100.).abs() < 1e-12);
/// ```
#[derive(Debug, Copy, Clone, Hash, Serialize, Deserialize)]
pub struct Steps2D<T>(pub (T, T, usize), pub (T, T, usize));

impl<T> Steps2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  /// Create a new instance
  pub fn new(x: (T, T, usize), y: (T, T, usize)) -> Self {
    Self(x, y)
  }

  /// Number of divisions for each axis
  pub fn divisions(&self) -> (usize, usize) {
    (
      Steps::from(self.0).divisions(),
      Steps::from(self.1).divisions(),
    )
  }

  /// Range of each axis
  pub fn ranges(&self) -> ((T, T), (T, T)) {
    ((self.0 .0, self.0 .1), (self.1 .0, self.1 .1))
  }

  /// Total number of steps
  pub fn len(&self) -> usize {
    (self.0).2 * (self.1).2
  }

  /// Is it empty?
  pub fn is_empty(&self) -> bool {
    self.len() == 0
  }

  /// Same number of steps for each axis?
  pub fn is_square(&self) -> bool {
    self.0 .2 == self.1 .2
  }

  /// Get the value at the given index
  pub fn value(&self, index: usize) -> (T, T) {
    let cols = self.0 .2;
    let rows = self.1 .2;
    let (nx, ny) = get_2d_indices(index, cols);
    let xt = if cols > 1 {
      (nx as f64) / ((cols - 1) as f64)
    } else {
      0.
    };
    let yt = if rows > 1 {
      (ny as f64) / ((rows - 1) as f64)
    } else {
      0.
    };
    let x = lerp(self.0 .0, self.0 .1, xt);
    let y = lerp(self.1 .0, self.1 .1, yt);
    (x, y)
  }

  /// Swap x and y
  pub fn swapped(self) -> Steps2D<T> {
    Steps2D(self.1, self.0)
  }

  /// Get the width of the gap between each step.
  ///
  /// ## Example:
  /// ```
  /// use spdcalc::utils::Steps2D;
  ///
  /// let steps = Steps2D((0., 4., 3), (0., 8., 3)); // 3 steps: |0| --- |2| --- |4| and |0| --- |4| --- |8|
  /// let dx = 2.;
  /// let dy = 4.;
  /// assert!((steps.division_widths().0 - dx).abs() < 1e-12);
  /// assert!((steps.division_widths().1 - dy).abs() < 1e-12);
  /// ```
  pub fn division_widths(&self) -> (T, T) {
    (
      Steps::from(self.0).division_width(),
      Steps::from(self.1).division_width(),
    )
  }
}

impl<T> IntoIterator for Steps2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  type Item = (T, T);
  type IntoIter = Iterator2D<T>;

  fn into_iter(self) -> Self::IntoIter {
    Iterator2D::new(self)
  }
}

/// get the 2d indices (column, row) from the linear index
pub fn get_2d_indices(index: usize, cols: usize) -> (usize, usize) {
  ((index % cols), (index / cols))
}

/// get the 1d index corresponding to a (col, row) of a 2d lattice
pub fn get_1d_index(col: usize, row: usize, cols: usize) -> usize {
  assert!(col < cols);
  row * cols + col
}

/// An iterator that will iterate through rows and columns, giving you the
/// coordinates at every iteration. Like a 2d linspace.
///
/// ## Example
///
/// ```
/// use spdcalc::utils::{Steps2D, Iterator2D};
///
/// let x_steps = (0., 100., 11); // 0-100 in 10 steps = [0, 10, 20, ..., 100]
/// let y_steps = (0., 10., 6); // 0-10 in 5 steps = [0, 2, 4, 6, 8, 10]
/// let steps = Steps2D::new(x_steps, y_steps);
/// let grid : Vec<f64> = Iterator2D::new(steps).map(|(x, y)| {
///    x * y
/// }).collect();
/// assert_eq!(grid[12], 20.);
/// ```
#[derive(Debug, Copy, Clone)]
pub struct Iterator2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  steps: Steps2D<T>,
  partition: (usize, usize),
  index: usize,
  index_back: usize,
}

impl<T> Iterator2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  /// Create a new 2d iterator
  pub fn new(steps: Steps2D<T>) -> Self {
    Self::new_partition(steps, 0, steps.len())
  }

  /// Create a new 2d iterator over a portion of the grid
  pub fn new_partition(steps: Steps2D<T>, start: usize, end: usize) -> Self {
    assert!(
      start <= end,
      "start is greater than end, start {}, end {}",
      start,
      end
    );
    Self {
      steps,
      partition: (start, end),
      index: start,
      index_back: end,
    }
  }

  /// get the x and y coordinates corresponding to the specified index
  pub fn get_xy(&self, index: usize) -> (T, T) {
    self.steps.value(index)
  }

  /// Get the x step size
  pub fn get_dx(&self) -> T {
    self.steps.division_widths().0
  }

  /// Get the y step size
  pub fn get_dy(&self) -> T {
    self.steps.division_widths().1
  }
}

impl<T> rayon::iter::IntoParallelIterator for Steps2D<T>
where
  T: Send
    + Sync
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  type Item = (T, T);
  type Iter = ParIterator2D<T>;
  fn into_par_iter(self) -> Self::Iter {
    ParIterator2D {
      it: self.into_iter(),
    }
  }
}

impl<T> Iterator for Iterator2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  type Item = (T, T); // x, y

  fn next(&mut self) -> Option<Self::Item> {
    if self.index >= self.index_back {
      return None;
    }

    let item = self.get_xy(self.index);
    self.index += 1;
    Some(item)
  }
}

impl<T> DoubleEndedIterator for Iterator2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  fn next_back(&mut self) -> Option<Self::Item> {
    if self.index_back <= self.index {
      return None;
    }

    self.index_back -= 1;
    let item = self.get_xy(self.index_back);
    Some(item)
  }
}

impl<T> ExactSizeIterator for Iterator2D<T>
where
  T: std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + Copy,
{
  fn len(&self) -> usize {
    self.partition.1 - self.partition.0
  }
}

/// Parallel iterator for 2D grid
pub struct ParIterator2D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  it: Iterator2D<T>,
}

impl<T> rayon::iter::plumbing::Producer for ParIterator2D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = (T, T);
  type IntoIter = Iterator2D<T>;

  fn into_iter(self) -> Self::IntoIter {
    self.it
  }

  fn split_at(self, index: usize) -> (Self, Self) {
    let left = Iterator2D::new_partition(
      self.it.steps,
      self.it.partition.0,
      self.it.partition.0 + index,
    );
    let right = Iterator2D::new_partition(
      self.it.steps,
      self.it.partition.0 + index,
      self.it.partition.1,
    );
    (ParIterator2D { it: left }, ParIterator2D { it: right })
  }
}

impl<T> rayon::iter::ParallelIterator for ParIterator2D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  type Item = (T, T);

  fn drive_unindexed<C>(self, consumer: C) -> C::Result
  where
    C: rayon::iter::plumbing::UnindexedConsumer<Self::Item>,
  {
    rayon::iter::plumbing::bridge(self, consumer)
  }

  fn opt_len(&self) -> Option<usize> {
    Some(self.it.len())
  }
}

impl<T> rayon::iter::IndexedParallelIterator for ParIterator2D<T>
where
  T: Send
    + Sync
    + std::ops::Mul<f64, Output = T>
    + std::ops::Add<T, Output = T>
    + std::ops::Div<f64, Output = T>
    + std::ops::Sub<T, Output = T>
    + Copy,
{
  fn len(&self) -> usize {
    self.it.len()
  }

  fn drive<C>(self, consumer: C) -> C::Result
  where
    C: rayon::iter::plumbing::Consumer<Self::Item>,
  {
    rayon::iter::plumbing::bridge(self, consumer)
  }

  fn with_producer<CB: rayon::iter::plumbing::ProducerCallback<Self::Item>>(
    self,
    callback: CB,
  ) -> CB::Output {
    callback.callback(self)
  }
}

/// Transpose a 2D matrix represented as a 1D vector.
pub fn transpose_vec<T: Clone>(vec: Vec<T>, num_cols: usize) -> Vec<T> {
  // use swap to transpose in place
  let mut vec = vec;
  let len = vec.len();
  let num_rows = len.div_ceil(num_cols);
  for row in 0..num_rows {
    for col in (row + 1)..num_cols {
      let index1 = get_1d_index(row, col, num_cols);
      let index2 = get_1d_index(col, row, num_cols);
      vec.swap(index1, index2);
    }
  }
  vec
}

#[cfg(test)]
pub(crate) mod testing {
  use crate::dim::f64prefixes::*;
  use crate::dim::ucum::*;
  use crate::{Apodization, PeriodicPoling, SPDC};

  pub fn percent_diff(actual: f64, expected: f64) -> f64 {
    if expected == 0. && actual == 0. {
      0.
    } else {
      200. * ((expected - actual).abs() / (expected + actual))
    }
  }

  macro_rules! assert_nearly_equal {
    ($name:expr, $actual:expr, $expected:expr, $accept_percent_diff:expr) => {
      let diff = crate::utils::testing::percent_diff($actual, $expected);
      assert!(
        diff.abs() < $accept_percent_diff,
        "{} percent difference: {}%\nactual: {}\nexpected: {}",
        $name,
        diff,
        $actual,
        $expected
      );
    };
    ($name:expr, $actual:expr, $expected:expr) => {
      assert_nearly_equal!($name, $actual, $expected, 1e-6);
    };
  }

  pub(crate) use assert_nearly_equal;

  pub fn testing_props(pp: bool) -> SPDC {
    let mut spdc = SPDC::from_json(serde_json::json!({
      "crystal": {
        "kind": "BBO_1",
        "pm_type": "Type2_e_eo",
        "theta_deg": -3.0,
        "phi_deg": 1.0,
        "length_um": 2_000.0,
        "temperature_c": 20.0,
        "counter_propagation": false
      },
      "signal": {
        "phi_deg": 15.0,
        "theta_deg": 0.5,
        "wavelength_nm": 1550.0,
        "waist_um": 100.0
      },
      "pump": {
        "wavelength_nm": 775.0,
        "waist_um": 100.0,
        "bandwidth_nm": 5.35,
        "average_power_mw": 1,
      },
      "deff_pm_per_volt": 1
    }))
    .unwrap();
    if pp {
      spdc.pp = PeriodicPoling::new(28.5 * MICRO * M, Apodization::Off)
    }
    spdc
  }
}

#[cfg(test)]
mod tests {
  use super::*;
  #[test]
  fn single_step_test() {
    let actual: Vec<f64> = Steps(3.3, 4., 1).into_iter().collect();
    let expected = vec![3.3];
    assert_eq!(actual, expected);
  }

  #[test]
  fn transpose_test() {
    let actual: Vec<f64> = transpose_vec(Steps(1., 4., 4).into_iter().collect(), 2);
    let expected = vec![1., 3., 2., 4.];
    assert_eq!(actual, expected);

    let actual = transpose_vec(
      vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16],
      4,
    );
    let expected = vec![1, 5, 9, 13, 2, 6, 10, 14, 3, 7, 11, 15, 4, 8, 12, 16];

    assert_eq!(actual, expected);
  }

  #[test]
  fn iterator_2d_test() {
    let it = Iterator2D::new(Steps2D::new((0., 1., 2), (0., 1., 2)));

    let actual: Vec<(f64, f64)> = it.collect();
    let expected = vec![(0., 0.), (1., 0.), (0., 1.), (1., 1.)];

    assert!(
      actual.iter().zip(expected.iter()).all(|(a, b)| a == b),
      "assertion failed. actual: {:?}, expected: {:?}",
      actual,
      expected
    );
  }

  #[test]
  fn iterator_2d_rev_test() {
    let it = Iterator2D::new(Steps2D::new((0., 1., 2), (0., 1., 2)));

    let actual: Vec<(f64, f64)> = it.rev().collect();
    let expected = vec![(1., 1.), (0., 1.), (1., 0.), (0., 0.)];

    assert!(
      actual.iter().zip(expected.iter()).all(|(a, b)| a == b),
      "assertion failed. actual: {:?}, expected: {:?}",
      actual,
      expected
    );
  }

  #[test]
  fn test_split_at() {
    let s = Steps(0., 0.9, 10);
    use rayon::iter::plumbing::Producer;
    let (a, b) = ParIterator1D { steps: s }.split_at(5);
    let a: Vec<f64> = a.into_iter().collect();
    let b: Vec<f64> = b.into_iter().collect();
    assert_eq!(a, vec![0., 0.1, 0.2, 0.30000000000000004, 0.4]);
    assert_eq!(b, vec![0.5, 0.6, 0.7, 0.8, 0.9]);
  }

  #[test]
  fn test_par_iter_2d() {
    use rayon::prelude::*;
    let s = Steps2D::new((2., 3., 2), (2., 3., 2));
    let actual: f64 = s.into_par_iter().map(|(x, y)| x + y).sum();
    assert_eq!(actual, 20.);
  }
}