use std::{f64::consts::PI, num::NonZeroU16};
use dyn_clone::DynClone;
use planar_geo::{polysegment::Polysegment, shape::Shape};
use stem_magnet::assembly::MagnetAssembly;
use stem_slot::prelude::*;
use crate::{
core::{CoreExt, CoreRef},
error::Error,
magnets::Magnets,
winding_zones::{WindingZones, WindingZonesPeriodic},
};
pub mod plain;
pub mod slotted;
pub mod straight_indents;
pub use plain::PlainAirGap;
pub use slotted::{CarterFactorModel, SlottedAirGap};
pub use straight_indents::{PolygonAirGapBuilder, StraightIndentsAirGap};
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Different air gaps, same core otherwise][rot_air_gap_comparison]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"rot_air_gap_comparison",
"docs/img/rot_air_gap_comparison.svg"
)
)]
#[cfg_attr(
not(feature = "doc-images"),
doc = "**Doc images not enabled**. Compile docs with
`cargo doc --features 'doc-images'` and Rust version >= 1.54."
)]
#[cfg_attr(feature = "serde", typetag::serde)]
pub trait AirGap: DynClone + Sync + Send + std::fmt::Debug + std::any::Any {
fn combine(&mut self, core: CoreRef<'_>) -> Result<Shape, Error>;
fn num_segments(&self, core: CoreRef<'_>) -> usize;
fn slots(&self, core: CoreRef<'_>) -> u16;
fn slot_opening_factor(&self, core: CoreRef<'_>, spatial_order: SpatialOrder) -> f64;
fn carter_factor(&self, core: CoreRef<'_>, geometrical_air_gap: Length) -> f64;
fn slot(&self, _core: CoreRef<'_>) -> Option<&dyn Slot>;
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Surface magnets for a plain and an indent air gap][surface_magnets]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image("surface_magnets", "docs/img/surface_magnets.svg")
)]
#[cfg_attr(
not(feature = "doc-images"),
doc = "**Doc images not enabled**. Compile docs with
`cargo doc --features 'doc-images'` and Rust version >= 1.54."
)]
fn surface_magnets(
&self,
core: CoreRef<'_>,
_magnet_assembly: &MagnetAssembly,
_split: bool,
) -> Magnets {
return crate::magnets::MagnetsPeriodic::<true>::new(
Length::new::<millimeter>(0.0),
Vec::new(),
0,
core.d_axis_offset(),
)
.into();
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Winding zones for a slotted and a plain air gap][winding_zones]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image("winding_zones", "docs/img/winding_zones.svg")
)]
#[cfg_attr(
not(feature = "doc-images"),
doc = "**Doc images not enabled**. Compile docs with
`cargo doc --features 'doc-images'` and Rust version >= 1.54."
)]
fn winding_zones(&self, _core: CoreRef<'_>, _coil_layout: &CoilLayout) -> WindingZones {
WindingZonesPeriodic::<planar_geo::prelude::Contour, true>::no_slots().into()
}
fn tooth_height(&self, core: CoreRef<'_>) -> Length {
self.slot(core)
.map_or(Length::new::<meter>(0.0), |s| s.height())
}
fn tooth_width_at(&self, core: CoreRef<'_>, height: Length) -> Length {
let slot = match self.slot(core) {
Some(s) => s,
None => return Length::new::<meter>(0.0),
};
if height < Length::new::<meter>(0.0) {
return Length::new::<meter>(0.0);
}
match core {
CoreRef::Lin(lin_core) => {
return lin_core.width() / self.slots(core) as f64 - slot.width_at(height);
}
CoreRef::Rot(rot_core) => {
let width = slot.width_at(height).get::<meter>();
let origin_height = if rot_core.is_outer() {
(rot_core.origin_offset_core_to_slot() + height).get::<meter>()
} else {
(rot_core.origin_offset_core_to_slot() - height).get::<meter>()
};
let radius = (origin_height.powi(2) + (0.5 * width).powi(2)).sqrt();
return Length::new::<meter>(
stem_slot::slot::semi_regular_polygon_side_length(
width,
radius,
2 * usize::from(self.slots(core)),
)
.unwrap(),
);
}
}
}
fn current_displacement_coefficients(
&self,
_core: CoreRef<'_>,
) -> CurrentDisplacementCalculator {
return CurrentDisplacementCalculator::from_slice_dims([].into_iter());
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Slot middle offset comparison][d_axis_to_first_slot_middle_offset]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"d_axis_to_first_slot_middle_offset",
"docs/img/d_axis_to_first_slot_middle_offset.svg"
)
)]
#[cfg_attr(
not(feature = "doc-images"),
doc = "**Doc images not enabled**. Compile docs with
`cargo doc --features 'doc-images'` and Rust version >= 1.54."
)]
fn slot_middle_offset(&self, core: CoreRef<'_>) -> f64 {
let _ = core;
return 0.0;
}
}
dyn_clone::clone_trait_object!(AirGap);
pub fn slot_opening_factor(
slot_pitch: Length,
slot_opening_width: Length,
slots: u16,
pole_pairs: NonZeroU16,
spatial_order: SpatialOrder,
) -> f64 {
let mech_order = f64::from(spatial_order.to_mechanical(pole_pairs));
let k = mech_order * (slot_opening_width / slot_pitch).get::<ratio>() * PI / f64::from(slots);
if k == 0.0 {
return 1.0;
} else {
return k.sin() / k;
}
}
fn combine_air_gap_and_yoke_to_shape(
air_gap: Polysegment,
yoke: Polysegment,
is_outer: bool,
) -> Result<Shape, Error> {
let (outer, inner) = if is_outer {
(yoke, air_gap)
} else {
(air_gap, yoke)
};
return Shape::new(vec![outer.into(), inner.into()]).map_err(From::from);
}
fn zero_length() -> Length {
Length::new::<meter>(0.0)
}
#[cfg(feature = "serde")]
fn deserialize_nonnegative_length<'de, D>(deserializer: D) -> Result<Length, D::Error>
where
D: serde::Deserializer<'de>,
{
let value = deserialize_quantity(deserializer)?;
let zero_length = zero_length();
if let Err(err) = compare_variables::compare_variables!(value >= zero_length) {
return Err(serde::de::Error::custom(err));
}
Ok(value)
}