lib_jsl_electromagnetics/algorithms/
overlap_integral.rs1use crate::conventions::C64;
6use crate::fields::field_container::HarmonicField;
7use crate::fields::interpolation::{Extrapolation, InterpolationMethod};
8use lib_jsl_core::ErrorsJSL;
9use std::sync::Arc;
10
11#[derive(Debug, Clone, Copy, PartialEq)]
13pub struct Overlap {
14 pub coefficient: C64,
16 pub efficiency: f64,
18}
19
20pub fn overlap_integral(a: &HarmonicField, b: &HarmonicField, method: InterpolationMethod) -> Result<Overlap, ErrorsJSL> {
29 if a.is_scalar() != b.is_scalar() {
30 return Err(ErrorsJSL::InvalidInputRange("Both fields must be scalar or both vector."));
31 }
32 if (a.frequency() - b.frequency()).abs() > 1e-9 * a.frequency() {
33 return Err(ErrorsJSL::InvalidInputRange("The fields must be at the same frequency."));
34 }
35 let b = b.with_convention(a.convention());
36 let b = if Arc::ptr_eq(a.domain(), b.domain()) || a.domain() == b.domain() { b } else { b.resample_to(a.domain().clone(), method, Extrapolation::Zero)? };
37 let (na, nb) = (a.integrate_norm_squared(), b.integrate_norm_squared());
38 if !(na > 0.0 && nb > 0.0) {
39 return Err(ErrorsJSL::InvalidInputRange("Both fields must be non-zero on the integration domain."));
40 }
41 let coefficient = a.inner_product(&b)? / (na * nb).sqrt();
42 Ok(Overlap { coefficient, efficiency: coefficient.norm_sqr() })
43}
44
45#[cfg(test)]
46mod tests {
47 use super::*;
48 use crate::conventions::TimeConvention;
49 use crate::coordinates::polarization::PolarizationBasis;
50 use crate::coordinates::systems::CoordinateSystem;
51 use crate::coordinates::transforms::RigidTransform;
52 use crate::fields::domain::{Axis, Domain};
53 use crate::fields::field_container::{FieldQuantity, Rank};
54 use std::f64::consts::{FRAC_1_SQRT_2, PI, TAU};
55
56 fn gaussian(d: Arc<Domain>, waist: f64, offset: f64, pol: [f64; 3]) -> HarmonicField {
57 HarmonicField::vector_from_fn(d, FieldQuantity::ElectricField, PolarizationBasis::CARTESIAN, 1e9, TimeConvention::PlusJ, |p| {
58 let [x, y, _] = p.to_cartesian();
59 let g = (-((x - offset).powi(2) + y * y) / (waist * waist)).exp();
60 pol.map(|v| C64::new(v * g, 0.0))
61 })
62 .unwrap()
63 }
64
65 fn plane(n: usize, half: f64) -> Arc<Domain> {
66 Arc::new(Domain::plane(Axis::gauss_legendre(-half, half, n).unwrap(), Axis::gauss_legendre(-half, half, n).unwrap(), 0.0).unwrap())
67 }
68
69 #[test]
70 fn gaussian_beam_coupling_matches_the_closed_form() {
71 let d = plane(60, 6.0);
72 let x = [1.0, 0.0, 0.0];
73 let a = gaussian(d.clone(), 1.0, 0.0, x);
74 assert!((overlap_integral(&a, &a, InterpolationMethod::Linear).unwrap().efficiency - 1.0).abs() < 1e-12);
75 let b = gaussian(d.clone(), 1.5, 0.0, x);
77 let want = (2.0 * 1.0 * 1.5 / (1.0 + 2.25f64)).powi(2);
78 assert!((overlap_integral(&a, &b, InterpolationMethod::Linear).unwrap().efficiency - want).abs() < 1e-10);
79 let c = gaussian(d.clone(), 1.0, 0.8, x);
81 assert!((overlap_integral(&a, &c, InterpolationMethod::Linear).unwrap().efficiency - (-0.64f64).exp()).abs() < 1e-10);
82 let ortho = gaussian(d.clone(), 1.0, 0.0, [0.0, 1.0, 0.0]);
84 assert!(overlap_integral(&a, &ortho, InterpolationMethod::Linear).unwrap().efficiency < 1e-20);
85 let tilted = gaussian(d.clone(), 1.0, 0.0, [FRAC_1_SQRT_2, FRAC_1_SQRT_2, 0.0]);
86 assert!((overlap_integral(&a, &tilted, InterpolationMethod::Linear).unwrap().efficiency - 0.5).abs() < 1e-12);
87 let shifted = a.scaled(C64::from_polar(1.0, 0.7));
89 let o = overlap_integral(&a, &shifted, InterpolationMethod::Linear).unwrap();
90 assert!((o.coefficient - C64::from_polar(1.0, -0.7)).norm() < 1e-12 && (o.efficiency - 1.0).abs() < 1e-12);
91 }
92
93 #[test]
94 fn different_grids_bases_and_coordinate_systems() {
95 let coarse = plane(40, 6.0);
98 let fine = plane(90, 7.0);
99 let a = gaussian(coarse, 1.2, 0.0, [1.0, 0.0, 0.0]);
100 let b = gaussian(fine, 1.2, 0.0, [1.0, 0.0, 0.0]).to_basis(PolarizationBasis::CYLINDRICAL).unwrap();
101 let o = overlap_integral(&a, &b, InterpolationMethod::Cubic).unwrap();
102 assert!((o.efficiency - 1.0).abs() < 1e-6, "{}", o.efficiency);
103 let s = Arc::new(Domain::far_field_gauss(24, 48).unwrap());
106 let dip = HarmonicField::vector_from_fn(s.clone(), FieldQuantity::ElectricField, PolarizationBasis::SPHERICAL, 1e9, TimeConvention::PlusJ, |p| [C64::new(0.0, 0.0), C64::new(p.coords[1].sin(), 0.0), C64::new(0.0, 0.0)]).unwrap();
107 let same = dip.to_basis(PolarizationBasis::Circular(CoordinateSystem::Spherical)).unwrap();
108 assert!((overlap_integral(&dip, &same, InterpolationMethod::Linear).unwrap().efficiency - 1.0).abs() < 1e-12);
109 let x_dip = dip.transformed(&RigidTransform::rotation_about([0.0, 1.0, 0.0], PI / 2.0), InterpolationMethod::Cubic, Extrapolation::Error).unwrap();
110 assert!(overlap_integral(&dip, &x_dip, InterpolationMethod::Linear).unwrap().efficiency < 1e-4);
112 let spun = dip.transformed(&RigidTransform::rotation_about([0.0, 0.0, 1.0], 1.0), InterpolationMethod::Cubic, Extrapolation::Error).unwrap();
114 assert!((overlap_integral(&dip, &spun, InterpolationMethod::Linear).unwrap().efficiency - 1.0).abs() < 1e-6);
115 let _ = TAU;
116 }
117
118 #[test]
119 fn validation_and_conventions() {
120 let d = plane(10, 3.0);
121 let a = gaussian(d.clone(), 1.0, 0.0, [1.0, 0.0, 0.0]);
122 assert!((overlap_integral(&a, &a.with_convention(TimeConvention::MinusI), InterpolationMethod::Linear).unwrap().efficiency - 1.0).abs() < 1e-12);
124 let zero = a.scaled(C64::new(0.0, 0.0));
125 assert!(overlap_integral(&a, &zero, InterpolationMethod::Linear).is_err());
126 let s = HarmonicField::zeros(d.clone(), FieldQuantity::Generic, Rank::Scalar, 1e9, TimeConvention::PlusJ).unwrap();
127 assert!(overlap_integral(&a, &s, InterpolationMethod::Linear).is_err());
128 let other_freq = HarmonicField::vector_from_fn(d, FieldQuantity::ElectricField, PolarizationBasis::CARTESIAN, 2e9, TimeConvention::PlusJ, |_| [C64::new(1.0, 0.0), C64::new(0.0, 0.0), C64::new(0.0, 0.0)]).unwrap();
129 assert!(overlap_integral(&a, &other_freq, InterpolationMethod::Linear).is_err());
130 }
131}