lib_jsl_electromagnetics/signal_models/
point_source.rs1use super::Coherency;
6use crate::arrays::SensorArray;
7use crate::conventions::C64;
8use crate::linalg::hermitian_transpose;
9use lib_jsl_core::ErrorsJSL;
10use ndarray::Array2;
11use std::f64::consts::PI;
12
13#[derive(Debug, Clone, Copy, PartialEq)]
16pub struct PointSource {
17 pub theta: f64,
18 pub phi: f64,
19 pub flux_density: f64,
20 pub polarization: Coherency,
21}
22
23impl PointSource {
24 pub fn new(theta: f64, phi: f64, flux_density: f64, polarization: Coherency) -> Result<Self, ErrorsJSL> {
25 if !flux_density.is_finite() || flux_density < 0.0 || !theta.is_finite() || !phi.is_finite() {
26 return Err(ErrorsJSL::InvalidInputRange("Need a finite direction and a non-negative flux density."));
27 }
28 Ok(PointSource { theta, phi, flux_density, polarization })
29 }
30
31 pub fn covariance(&self, array: &SensorArray) -> Result<Array2<C64>, ErrorsJSL> {
34 let a = array.response_matrix(self.theta, self.phi)?;
35 let j = Array2::from_shape_fn((2, 2), |(r, c)| self.polarization.0[r][c]);
36 let scale = self.flux_density * array.wavelength().powi(2) / (4.0 * PI);
37 Ok(a.dot(&j).dot(&hermitian_transpose(&a)).mapv(|v| v * scale))
38 }
39}
40
41#[cfg(test)]
42mod tests {
43 use super::*;
44 use crate::antenna_models::Isotropic;
45 use crate::conventions::TimeConvention;
46 use crate::coordinates::transforms::RigidTransform;
47 use lib_jsl_core::constants::em::C0;
48 use std::sync::Arc;
49
50 fn array(pol: [C64; 2]) -> SensorArray {
51 let f = 1e9;
52 let positions = SensorArray::linear_positions(3, 0.5 * C0 / f, [0.0, 1.0, 0.0]);
53 SensorArray::from_antenna(&positions, Arc::new(Isotropic { polarization: pol }), &RigidTransform::identity(), f, TimeConvention::PlusJ, 1.0).unwrap()
54 }
55
56 #[test]
57 fn received_power_and_rank_one_structure() {
58 let a = array([C64::new(1.0, 0.0), C64::new(0.0, 0.0)]);
59 let s = PointSource::new(1.0, 0.5, 2e-12, Coherency::theta()).unwrap();
60 let r = s.covariance(&a).unwrap();
61 let lambda = a.wavelength();
62 let want = 2e-12 * lambda * lambda / (4.0 * PI); for i in 0..3 {
64 assert!((r[[i, i]].re - want).abs() < 1e-9 * want);
65 for j in 0..3 {
66 assert!((r[[i, j]].norm() - want).abs() < 1e-9 * want); }
68 }
69 assert!(PointSource::new(1.0, 0.5, 2e-12, Coherency::phi()).unwrap().covariance(&a).unwrap().iter().all(|v| v.norm() < 1e-30));
71 assert!((PointSource::new(1.0, 0.5, 2e-12, Coherency::unpolarized()).unwrap().covariance(&a).unwrap()[[0, 0]].re - want / 2.0).abs() < 1e-9 * want);
72 assert!(PointSource::new(1.0, 0.5, -1.0, Coherency::theta()).is_err());
73 }
74
75 #[test]
76 fn circularly_polarized_elements_match_the_same_handedness_only() {
77 let conv = TimeConvention::PlusJ;
78 let rhcp_element = array([C64::new(std::f64::consts::FRAC_1_SQRT_2, 0.0), -conv.unit() * std::f64::consts::FRAC_1_SQRT_2]);
80 let co = PointSource::new(0.7, 0.2, 1.0, Coherency::rhcp_incoming(conv)).unwrap().covariance(&rhcp_element).unwrap();
81 let cross = PointSource::new(0.7, 0.2, 1.0, Coherency::lhcp_incoming(conv)).unwrap().covariance(&rhcp_element).unwrap();
82 assert!(co[[0, 0]].re > 0.0 && cross[[0, 0]].norm() < 1e-12 * co[[0, 0]].re);
83 }
84}