Skip to main content

lib_jsl_electromagnetics/fields/
field_container.rs

1//! Field containers: scalar or vector samples on a [`Domain`], harmonic or time-varying, tagged
2//! with the physical quantity they hold.
3//!
4//! - [`HarmonicField`]: complex peak phasors at one frequency, with a [`TimeConvention`]. Values
5//!   are stored as a `(samples, components)` array, one component for scalar fields and three
6//!   (in the field's [`PolarizationBasis`]) for vector fields.
7//! - [`TimeField`]: real samples on a uniform [`TimeAxis`], stored as `(steps, samples, components)`.
8//!   Vector time fields use linear bases only (circular components are complex by nature).
9//!
10//! Every sample's position comes from the domain, so a field never stores positions itself.
11//! Domains are shared through `Arc`, which makes fields cheap to derive from one another.
12//!
13//! See the book entry [Fields on grids](https://jwkunz.github.io/lib_jsl/book/em-arrays/fields.html).
14
15use crate::conventions::{C64, TimeConvention};
16use crate::coordinates::polarization::PolarizationBasis;
17use crate::coordinates::systems::Position;
18use crate::fields::domain::Domain;
19use lib_jsl_core::ErrorsJSL;
20use ndarray::{Array2, Array3};
21use std::f64::consts::TAU;
22use std::sync::Arc;
23
24/// The physical quantity a field holds, with its SI units.
25#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
26pub enum FieldQuantity {
27    /// E, V/m
28    ElectricField,
29    /// H, A/m
30    MagneticField,
31    /// D, C/m²
32    ElectricFluxDensity,
33    /// B, T
34    MagneticFluxDensity,
35    /// J, A/m² (volume) or A/m (surface)
36    ElectricCurrentDensity,
37    /// M, V/m² (volume) or V/m (surface)
38    MagneticCurrentDensity,
39    /// A, Wb/m
40    VectorPotential,
41    /// Φ, V
42    ScalarPotential,
43    /// S (Poynting vector), W/m²
44    PowerDensity,
45    /// Brightness or noise temperature, K
46    Temperature,
47    /// Anything else (no unit or rank checks)
48    Generic,
49}
50
51impl FieldQuantity {
52    pub fn units(self) -> &'static str {
53        match self {
54            FieldQuantity::ElectricField => "V/m",
55            FieldQuantity::MagneticField => "A/m",
56            FieldQuantity::ElectricFluxDensity => "C/m²",
57            FieldQuantity::MagneticFluxDensity => "T",
58            FieldQuantity::ElectricCurrentDensity => "A/m²",
59            FieldQuantity::MagneticCurrentDensity => "V/m²",
60            FieldQuantity::VectorPotential => "Wb/m",
61            FieldQuantity::ScalarPotential => "V",
62            FieldQuantity::PowerDensity => "W/m²",
63            FieldQuantity::Temperature => "K",
64            FieldQuantity::Generic => "",
65        }
66    }
67
68    /// Whether the quantity is inherently scalar (`Some(true)`), vector (`Some(false)`), or either.
69    pub fn is_scalar(self) -> Option<bool> {
70        match self {
71            FieldQuantity::ScalarPotential | FieldQuantity::Temperature => Some(true),
72            FieldQuantity::Generic => None,
73            _ => Some(false),
74        }
75    }
76}
77
78/// Scalar, or vector with components in a polarization basis.
79#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
80pub enum Rank {
81    Scalar,
82    Vector(PolarizationBasis),
83}
84
85impl Rank {
86    pub fn components(self) -> usize {
87        match self {
88            Rank::Scalar => 1,
89            Rank::Vector(_) => 3,
90        }
91    }
92
93    pub fn basis(self) -> Option<PolarizationBasis> {
94        match self {
95            Rank::Scalar => None,
96            Rank::Vector(b) => Some(b),
97        }
98    }
99}
100
101fn check_rank(quantity: FieldQuantity, rank: Rank) -> Result<(), ErrorsJSL> {
102    match (quantity.is_scalar(), rank) {
103        (Some(true), Rank::Vector(_)) => Err(ErrorsJSL::InvalidInputRange("This quantity is a scalar field.")),
104        (Some(false), Rank::Scalar) => Err(ErrorsJSL::InvalidInputRange("This quantity is a vector field.")),
105        _ => Ok(()),
106    }
107}
108
109/// Complex peak phasors at one frequency on a domain.
110#[derive(Debug, Clone, PartialEq)]
111pub struct HarmonicField {
112    domain: Arc<Domain>,
113    quantity: FieldQuantity,
114    rank: Rank,
115    frequency: f64,
116    convention: TimeConvention,
117    values: Array2<C64>,
118}
119
120impl HarmonicField {
121    /// A field from a `(samples, components)` array.
122    pub fn new(domain: Arc<Domain>, quantity: FieldQuantity, rank: Rank, frequency: f64, convention: TimeConvention, values: Array2<C64>) -> Result<Self, ErrorsJSL> {
123        check_rank(quantity, rank)?;
124        if !frequency.is_finite() || frequency <= 0.0 {
125            return Err(ErrorsJSL::InvalidInputRange("The frequency must be positive and finite."));
126        }
127        if values.dim() != (domain.len(), rank.components()) {
128            return Err(ErrorsJSL::IncompatibleArraySizes((values.nrows(), domain.len())));
129        }
130        Ok(HarmonicField { domain, quantity, rank, frequency, convention, values })
131    }
132
133    /// A zero field.
134    pub fn zeros(domain: Arc<Domain>, quantity: FieldQuantity, rank: Rank, frequency: f64, convention: TimeConvention) -> Result<Self, ErrorsJSL> {
135        let values = Array2::zeros((domain.len(), rank.components()));
136        Self::new(domain, quantity, rank, frequency, convention, values)
137    }
138
139    /// A scalar field sampled from a function of position.
140    pub fn scalar_from_fn(domain: Arc<Domain>, quantity: FieldQuantity, frequency: f64, convention: TimeConvention, f: impl Fn(&Position) -> C64) -> Result<Self, ErrorsJSL> {
141        let values = Array2::from_shape_fn((domain.len(), 1), |(i, _)| f(&domain.position_flat(i)));
142        Self::new(domain, quantity, Rank::Scalar, frequency, convention, values)
143    }
144
145    /// A vector field sampled from a function returning components in `basis`.
146    pub fn vector_from_fn(domain: Arc<Domain>, quantity: FieldQuantity, basis: PolarizationBasis, frequency: f64, convention: TimeConvention, f: impl Fn(&Position) -> [C64; 3]) -> Result<Self, ErrorsJSL> {
147        let mut values = Array2::zeros((domain.len(), 3));
148        for i in 0..domain.len() {
149            let v = f(&domain.position_flat(i));
150            for k in 0..3 {
151                values[[i, k]] = v[k];
152            }
153        }
154        Self::new(domain, quantity, Rank::Vector(basis), frequency, convention, values)
155    }
156
157    pub fn domain(&self) -> &Arc<Domain> {
158        &self.domain
159    }
160
161    pub fn quantity(&self) -> FieldQuantity {
162        self.quantity
163    }
164
165    pub fn rank(&self) -> Rank {
166        self.rank
167    }
168
169    pub fn basis(&self) -> Option<PolarizationBasis> {
170        self.rank.basis()
171    }
172
173    pub fn is_scalar(&self) -> bool {
174        self.rank == Rank::Scalar
175    }
176
177    pub fn frequency(&self) -> f64 {
178        self.frequency
179    }
180
181    /// The angular frequency `2πf`.
182    pub fn omega(&self) -> f64 {
183        TAU * self.frequency
184    }
185
186    pub fn convention(&self) -> TimeConvention {
187        self.convention
188    }
189
190    pub fn values(&self) -> &Array2<C64> {
191        &self.values
192    }
193
194    pub fn values_mut(&mut self) -> &mut Array2<C64> {
195        &mut self.values
196    }
197
198    pub fn len(&self) -> usize {
199        self.values.nrows()
200    }
201
202    pub fn is_empty(&self) -> bool {
203        self.values.nrows() == 0
204    }
205
206    /// The value of a scalar field at a sample.
207    pub fn scalar(&self, sample: usize) -> C64 {
208        self.values[[sample, 0]]
209    }
210
211    /// The components of a vector field at a sample, in the field's basis.
212    pub fn vector(&self, sample: usize) -> [C64; 3] {
213        [self.values[[sample, 0]], self.values[[sample, 1]], self.values[[sample, 2]]]
214    }
215
216    /// The Cartesian components of a vector field at a sample.
217    pub fn cartesian(&self, sample: usize) -> [C64; 3] {
218        match self.rank {
219            Rank::Vector(b) => b.to_cartesian(self.vector(sample), &self.domain.position_flat(sample), self.convention),
220            Rank::Scalar => [self.scalar(sample), C64::new(0.0, 0.0), C64::new(0.0, 0.0)],
221        }
222    }
223
224    /// The same field with a different quantity tag (rank rules still apply).
225    pub fn with_quantity(mut self, quantity: FieldQuantity) -> Result<Self, ErrorsJSL> {
226        check_rank(quantity, self.rank)?;
227        self.quantity = quantity;
228        Ok(self)
229    }
230
231    /// The same physical field expressed in another time convention (components are conjugated;
232    /// circular components stay consistent because the circular basis vectors conjugate too).
233    pub fn with_convention(&self, convention: TimeConvention) -> Self {
234        let mut out = self.clone();
235        if convention != self.convention {
236            out.values.mapv_inplace(|v| v.conj());
237            out.convention = convention;
238        }
239        out
240    }
241
242    /// The field multiplied by a complex factor.
243    pub fn scaled(&self, factor: C64) -> Self {
244        let mut out = self.clone();
245        out.values.mapv_inplace(|v| v * factor);
246        out
247    }
248
249    fn check_compatible(&self, other: &HarmonicField) -> Result<(), ErrorsJSL> {
250        if !(Arc::ptr_eq(&self.domain, &other.domain) || self.domain == other.domain) {
251            return Err(ErrorsJSL::InvalidInputRange("Fields live on different domains (resample first)."));
252        }
253        if self.rank != other.rank || self.convention != other.convention || (self.frequency - other.frequency).abs() > 1e-12 * self.frequency {
254            return Err(ErrorsJSL::InvalidInputRange("Fields differ in rank, basis, convention or frequency."));
255        }
256        Ok(())
257    }
258
259    /// The sample-wise sum of two compatible fields of the same quantity (superposition).
260    pub fn add(&self, other: &HarmonicField) -> Result<Self, ErrorsJSL> {
261        self.check_compatible(other)?;
262        if self.quantity != other.quantity {
263            return Err(ErrorsJSL::InvalidInputRange("Cannot add different physical quantities."));
264        }
265        let mut out = self.clone();
266        out.values = &self.values + &other.values;
267        Ok(out)
268    }
269
270    /// `|F|²` at every sample (`Σ|cᵢ|²`, which no unitary basis change alters).
271    pub fn norm_squared(&self) -> Vec<f64> {
272        self.values.rows().into_iter().map(|r| r.iter().map(|v| v.norm_sqr()).sum()).collect()
273    }
274
275    /// `∫ |F|² dA` (or dV, dΩ) with the domain's quadrature weights.
276    pub fn integrate_norm_squared(&self) -> f64 {
277        self.norm_squared().iter().zip(self.domain.weights()).map(|(v, w)| v * w).sum()
278    }
279
280    /// `∫ F dA` for a scalar field.
281    pub fn integrate(&self) -> Result<C64, ErrorsJSL> {
282        if !self.is_scalar() {
283            return Err(ErrorsJSL::InvalidInputRange("Only scalar fields integrate to a number."));
284        }
285        Ok(self.values.column(0).iter().zip(self.domain.weights()).map(|(v, w)| v * *w).sum())
286    }
287
288    /// The inner product `∫ F · G* dA` of two fields on the same domain, computed in Cartesian
289    /// components (so the two fields may use different bases).
290    pub fn inner_product(&self, other: &HarmonicField) -> Result<C64, ErrorsJSL> {
291        if !(Arc::ptr_eq(&self.domain, &other.domain) || self.domain == other.domain) {
292            return Err(ErrorsJSL::InvalidInputRange("Fields live on different domains (resample first)."));
293        }
294        if self.is_scalar() != other.is_scalar() || self.convention != other.convention {
295            return Err(ErrorsJSL::InvalidInputRange("Inner products need matching rank and time convention."));
296        }
297        Ok((0..self.len())
298            .map(|i| {
299                let (a, b) = (self.cartesian(i), other.cartesian(i));
300                let dot: C64 = if self.is_scalar() { a[0] * b[0].conj() } else { (0..3).map(|k| a[k] * b[k].conj()).sum() };
301                dot * self.domain.weights()[i]
302            })
303            .sum())
304    }
305
306    /// The real time signal `Re{X e^{±jωt}}` on a time axis (linear bases only).
307    pub fn to_time_series(&self, time: TimeAxis) -> Result<TimeField, ErrorsJSL> {
308        if self.basis().is_some_and(|b| b.is_circular()) {
309            return Err(ErrorsJSL::InvalidInputRange("Convert circular components to a linear basis first."));
310        }
311        let (n, c) = self.values.dim();
312        let omega = self.omega();
313        let values = Array3::from_shape_fn((time.count, n, c), |(t, i, k)| self.convention.instantaneous(self.values[[i, k]], omega, time.time(t)));
314        TimeField::new(self.domain.clone(), self.quantity, self.rank, time, values)
315    }
316}
317
318/// A uniform time axis `start + k·step`, `k = 0..count`.
319#[derive(Debug, Clone, Copy, PartialEq)]
320pub struct TimeAxis {
321    pub start: f64,
322    pub step: f64,
323    pub count: usize,
324}
325
326impl TimeAxis {
327    pub fn new(start: f64, step: f64, count: usize) -> Result<Self, ErrorsJSL> {
328        if !start.is_finite() || !step.is_finite() || step <= 0.0 || count == 0 {
329            return Err(ErrorsJSL::InvalidInputRange("A time axis needs a finite start, a positive step and at least one sample."));
330        }
331        Ok(TimeAxis { start, step, count })
332    }
333
334    pub fn time(&self, k: usize) -> f64 {
335        self.start + self.step * k as f64
336    }
337
338    pub fn times(&self) -> Vec<f64> {
339        (0..self.count).map(|k| self.time(k)).collect()
340    }
341}
342
343/// Real field samples on a domain over a uniform time axis.
344#[derive(Debug, Clone, PartialEq)]
345pub struct TimeField {
346    domain: Arc<Domain>,
347    quantity: FieldQuantity,
348    rank: Rank,
349    time: TimeAxis,
350    values: Array3<f64>,
351}
352
353impl TimeField {
354    /// A field from a `(steps, samples, components)` array; vector fields need a linear basis.
355    pub fn new(domain: Arc<Domain>, quantity: FieldQuantity, rank: Rank, time: TimeAxis, values: Array3<f64>) -> Result<Self, ErrorsJSL> {
356        check_rank(quantity, rank)?;
357        if rank.basis().is_some_and(|b| b.is_circular()) {
358            return Err(ErrorsJSL::InvalidInputRange("Time-varying vector fields need a linear basis."));
359        }
360        if values.dim() != (time.count, domain.len(), rank.components()) {
361            return Err(ErrorsJSL::IncompatibleArraySizes((values.dim().1, domain.len())));
362        }
363        Ok(TimeField { domain, quantity, rank, time, values })
364    }
365
366    /// A field sampled from a function of position and time returning `rank.components()` values.
367    pub fn from_fn(domain: Arc<Domain>, quantity: FieldQuantity, rank: Rank, time: TimeAxis, f: impl Fn(&Position, f64) -> Vec<f64>) -> Result<Self, ErrorsJSL> {
368        let (n, c) = (domain.len(), rank.components());
369        let mut values = Array3::zeros((time.count, n, c));
370        for i in 0..n {
371            let p = domain.position_flat(i);
372            for t in 0..time.count {
373                let v = f(&p, time.time(t));
374                if v.len() != c {
375                    return Err(ErrorsJSL::IncompatibleArraySizes((v.len(), c)));
376                }
377                for k in 0..c {
378                    values[[t, i, k]] = v[k];
379                }
380            }
381        }
382        Self::new(domain, quantity, rank, time, values)
383    }
384
385    pub fn domain(&self) -> &Arc<Domain> {
386        &self.domain
387    }
388
389    pub fn quantity(&self) -> FieldQuantity {
390        self.quantity
391    }
392
393    pub fn rank(&self) -> Rank {
394        self.rank
395    }
396
397    pub fn basis(&self) -> Option<PolarizationBasis> {
398        self.rank.basis()
399    }
400
401    pub fn time_axis(&self) -> TimeAxis {
402        self.time
403    }
404
405    pub fn values(&self) -> &Array3<f64> {
406        &self.values
407    }
408
409    pub fn values_mut(&mut self) -> &mut Array3<f64> {
410        &mut self.values
411    }
412
413    /// The components at one time step and sample.
414    pub fn at(&self, step: usize, sample: usize) -> Vec<f64> {
415        (0..self.rank.components()).map(|k| self.values[[step, sample, k]]).collect()
416    }
417
418    /// The harmonic component at `frequency` by projection over the time axis:
419    /// `X = (2/N) Σ x(t) e^{∓jωt}`, exact when the axis spans a whole number of periods.
420    /// `frequency` must be positive.
421    pub fn harmonic_at(&self, frequency: f64, convention: TimeConvention) -> Result<HarmonicField, ErrorsJSL> {
422        let (steps, n, c) = self.values.dim();
423        let omega = TAU * frequency;
424        let mut out = Array2::<C64>::zeros((n, c));
425        for t in 0..steps {
426            let phase = C64::from_polar(2.0 / steps as f64, -convention.sign() * omega * self.time.time(t));
427            for i in 0..n {
428                for k in 0..c {
429                    out[[i, k]] += phase * self.values[[t, i, k]];
430                }
431            }
432        }
433        HarmonicField::new(self.domain.clone(), self.quantity, self.rank, frequency, convention, out)
434    }
435}
436
437#[cfg(test)]
438mod tests {
439    use super::*;
440    use crate::coordinates::systems::CoordinateSystem;
441    use crate::fields::domain::Axis;
442    use std::f64::consts::PI;
443
444    fn sphere() -> Arc<Domain> {
445        Arc::new(Domain::far_field_gauss(8, 16).unwrap())
446    }
447
448    fn c(re: f64, im: f64) -> C64 {
449        C64::new(re, im)
450    }
451
452    #[test]
453    fn construction_rank_rules_and_accessors() {
454        let d = sphere();
455        let e = HarmonicField::vector_from_fn(d.clone(), FieldQuantity::ElectricField, PolarizationBasis::SPHERICAL, 1e9, TimeConvention::PlusJ, |p| [c(0.0, 0.0), c(p.coords[1].sin(), 0.0), c(0.0, 1.0)]).unwrap();
456        assert_eq!((e.len(), e.basis(), e.quantity().units()), (d.len(), Some(PolarizationBasis::SPHERICAL), "V/m"));
457        assert!((e.omega() - TAU * 1e9).abs() < 1e-3);
458        // A θ̂ vector at the equator is -ẑ in Cartesian components.
459        let eq = (0..d.len()).find(|&i| (d.position_flat(i).coords[1] - PI / 2.0).abs() < 0.2).unwrap();
460        let cart = e.cartesian(eq);
461        assert!(cart[2].re < -0.9);
462        assert!(HarmonicField::zeros(d.clone(), FieldQuantity::Temperature, Rank::Vector(PolarizationBasis::CARTESIAN), 1.0, TimeConvention::PlusJ).is_err());
463        assert!(HarmonicField::zeros(d.clone(), FieldQuantity::ElectricField, Rank::Scalar, 1.0, TimeConvention::PlusJ).is_err());
464        assert!(HarmonicField::zeros(d.clone(), FieldQuantity::Generic, Rank::Scalar, 0.0, TimeConvention::PlusJ).is_err());
465        assert!(HarmonicField::new(d.clone(), FieldQuantity::Generic, Rank::Scalar, 1.0, TimeConvention::PlusJ, Array2::zeros((3, 1))).is_err());
466        assert!(e.clone().with_quantity(FieldQuantity::MagneticField).is_ok() && e.clone().with_quantity(FieldQuantity::ScalarPotential).is_err());
467    }
468
469    #[test]
470    fn integrals_inner_products_and_superposition() {
471        let d = sphere();
472        let one = HarmonicField::scalar_from_fn(d.clone(), FieldQuantity::Generic, 1.0, TimeConvention::PlusJ, |_| c(1.0, 0.0)).unwrap();
473        assert!((one.integrate().unwrap().re - 4.0 * PI).abs() < 1e-12);
474        // ∫|ẑ|² dΩ = 4π regardless of the basis the field is stored in.
475        let z_cart = HarmonicField::vector_from_fn(d.clone(), FieldQuantity::ElectricField, PolarizationBasis::CARTESIAN, 1.0, TimeConvention::PlusJ, |_| [c(0.0, 0.0), c(0.0, 0.0), c(1.0, 0.0)]).unwrap();
476        let z_sph = HarmonicField::vector_from_fn(d.clone(), FieldQuantity::ElectricField, PolarizationBasis::SPHERICAL, 1.0, TimeConvention::PlusJ, |p| {
477            let (s, cth) = p.coords[1].sin_cos();
478            [c(cth, 0.0), c(-s, 0.0), c(0.0, 0.0)]
479        })
480        .unwrap();
481        assert!((z_cart.integrate_norm_squared() - 4.0 * PI).abs() < 1e-12);
482        assert!((z_cart.inner_product(&z_sph).unwrap() - c(4.0 * PI, 0.0)).norm() < 1e-12);
483        let double = z_cart.add(&z_cart).unwrap();
484        assert!((double.integrate_norm_squared() - 16.0 * PI).abs() < 1e-11);
485        assert!((z_cart.scaled(c(0.0, 2.0)).integrate_norm_squared() - 16.0 * PI).abs() < 1e-11);
486        assert!(z_cart.add(&z_sph).is_err()); // different bases: convert first
487        assert!(z_cart.integrate().is_err());
488        let other = Arc::new(Domain::far_field_gauss(4, 8).unwrap());
489        let elsewhere = HarmonicField::zeros(other, FieldQuantity::ElectricField, Rank::Vector(PolarizationBasis::CARTESIAN), 1.0, TimeConvention::PlusJ).unwrap();
490        assert!(z_cart.inner_product(&elsewhere).is_err());
491    }
492
493    #[test]
494    fn time_conventions_and_time_series_describe_the_same_signal() {
495        let d = Arc::new(Domain::plane(Axis::uniform(0.0, 1.0, 3).unwrap(), Axis::uniform(0.0, 1.0, 2).unwrap(), 0.0).unwrap());
496        let f = 50.0;
497        let h = HarmonicField::scalar_from_fn(d.clone(), FieldQuantity::ScalarPotential, f, TimeConvention::PlusJ, |p| C64::from_polar(1.0 + p.coords[0], 0.4)).unwrap();
498        let t = TimeAxis::new(0.0, 1.0 / (f * 40.0), 40).unwrap(); // exactly one period
499        let series = h.to_time_series(t).unwrap();
500        let other = h.with_convention(TimeConvention::MinusI);
501        assert_ne!(other.values(), h.values());
502        let series2 = other.to_time_series(t).unwrap();
503        assert!(series.values().iter().zip(series2.values().iter()).all(|(a, b)| (a - b).abs() < 1e-12));
504        assert!((series.at(0, 2)[0] - 1.5 * 0.4f64.cos()).abs() < 1e-12); // sample 2 is x = 0.5, y = 0
505        // Projecting the time series back recovers the phasors, in either convention.
506        for conv in [TimeConvention::PlusJ, TimeConvention::MinusI] {
507            let back = series.harmonic_at(f, conv).unwrap();
508            let want = h.with_convention(conv);
509            assert!(back.values().iter().zip(want.values().iter()).all(|(a, b)| (a - b).norm() < 1e-12), "{conv:?}");
510        }
511    }
512
513    #[test]
514    fn time_field_rules() {
515        let d = Arc::new(Domain::cartesian_box(Axis::uniform(0.0, 1.0, 2).unwrap(), Axis::uniform(0.0, 1.0, 2).unwrap(), Axis::uniform(0.0, 1.0, 2).unwrap()).unwrap());
516        let t = TimeAxis::new(0.0, 0.1, 5).unwrap();
517        let tf = TimeField::from_fn(d.clone(), FieldQuantity::ElectricField, Rank::Vector(PolarizationBasis::CARTESIAN), t, |p, time| vec![p.coords[0] * time, 0.0, 1.0]).unwrap();
518        assert_eq!(tf.at(4, 7), vec![0.4, 0.0, 1.0]);
519        assert_eq!(tf.time_axis().times().len(), 5);
520        assert!(TimeField::from_fn(d.clone(), FieldQuantity::Generic, Rank::Vector(PolarizationBasis::Circular(CoordinateSystem::Cartesian)), t, |_, _| vec![0.0; 3]).is_err());
521        assert!(TimeField::from_fn(d.clone(), FieldQuantity::Generic, Rank::Scalar, t, |_, _| vec![0.0; 2]).is_err());
522        assert!(TimeAxis::new(0.0, 0.0, 3).is_err() && TimeAxis::new(0.0, 1.0, 0).is_err());
523        let circ = HarmonicField::zeros(d, FieldQuantity::ElectricField, Rank::Vector(PolarizationBasis::Circular(CoordinateSystem::Cartesian)), 1.0, TimeConvention::PlusJ).unwrap();
524        assert!(circ.to_time_series(t).is_err());
525    }
526}