#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
feature = "doc-images",
doc = "![Winding zone positioning (plain air gap and slotted air gap)][winding_zones_with_zone_pos.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("winding_zones_with_zone_pos.svg", "docs/img/winding_zones_with_zone_pos.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."
)]
use crate::planar_geo;
use planar_geo::DEFAULT_EPSILON;
use planar_geo::prelude::{Polysegment, ToBoundingBox};
use planar_geo::segment::ArcSegment;
use planar_geo::{Transformation, contour::Contour};
use std::f64::consts::{FRAC_1_SQRT_2, FRAC_PI_2, TAU};
use std::num::NonZeroU16;
use stem_slot::prelude::*;
use stem_slot::{slot::Slot, stem_types::CoilLayout};
#[cfg(feature = "cairo")]
use stem_slot::planar_geo::draw::{Drawable, DrawableRef};
#[derive(Debug, Clone)]
pub struct PositionedZoneContour {
pub contour: Contour,
pub zone: Zone,
}
impl PositionedZoneContour {
#[cfg(feature = "cairo")]
pub fn into_drawable(self) -> Drawable {
Drawable::new(self.contour, stem_slot::SLOT_STYLE)
}
#[cfg(feature = "cairo")]
pub fn into_drawable_with_style(self, style: planar_geo::draw::Style) -> Drawable {
Drawable::new(self.contour, style)
}
#[cfg(feature = "cairo")]
pub fn as_drawable<'a>(&'a self) -> DrawableRef<'a> {
return DrawableRef::new(&self.contour, stem_slot::SLOT_STYLE);
}
#[cfg(feature = "cairo")]
pub fn as_drawable_with_style<'a>(&'a self, style: planar_geo::draw::Style) -> DrawableRef<'a> {
return DrawableRef::new(&self.contour, style);
}
}
impl From<PositionedZoneContour> for Contour {
fn from(value: PositionedZoneContour) -> Self {
value.contour
}
}
impl From<(Contour, Zone)> for PositionedZoneContour {
fn from(value: (Contour, Zone)) -> Self {
Self {
contour: value.0,
zone: value.1,
}
}
}
#[cfg(feature = "cairo")]
impl From<PositionedZoneContour> for Drawable {
fn from(value: PositionedZoneContour) -> Self {
value.into_drawable()
}
}
impl Transformation for PositionedZoneContour {
fn translate(&mut self, shift: [f64; 2]) {
self.contour.translate(shift);
}
fn rotate(&mut self, center: [f64; 2], angle: f64) {
self.contour.rotate(center, angle);
}
fn scale(&mut self, factor: f64) {
self.contour.scale(factor);
}
fn line_reflection(&mut self, start: [f64; 2], stop: [f64; 2]) -> () {
self.contour.line_reflection(start, stop);
}
}
impl ToBoundingBox for PositionedZoneContour {
fn bounding_box(&self) -> planar_geo::prelude::BoundingBox {
self.contour.bounding_box()
}
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![How WindingZonesPeriodic distributes the winding zones][cad_winding_zones_periodic]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_winding_zones_periodic",
"docs/img/cad_winding_zones_periodic.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."
)]
#[derive(Debug, Clone)]
pub struct WindingZonesPeriodic<T: Transformation + Clone, const LIN: bool> {
slots: u16,
air_gap_length: Length,
zones: Vec<T>,
starts_in_slot_middle: bool,
index: usize,
}
impl<T: Transformation + Clone, const LIN: bool> WindingZonesPeriodic<T, LIN> {
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![How WindingZonesPeriodic distributes the winding zones][cad_winding_zones_periodic]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_winding_zones_periodic",
"docs/img/cad_winding_zones_periodic.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."
)]
pub fn new(
air_gap_length: Length,
zones: Vec<T>,
slots: u16,
starts_in_slot_middle: bool,
) -> Self {
return Self {
slots,
air_gap_length,
zones,
starts_in_slot_middle,
index: 0,
};
}
pub fn no_slots() -> Self {
return Self {
slots: 0,
air_gap_length: Length::new::<meter>(0.0),
zones: Vec::new(),
starts_in_slot_middle: true,
index: 0,
};
}
pub fn layers(&self) -> usize {
return self.zones.len();
}
pub fn slots(&self) -> usize {
return self.slots.into();
}
pub fn reset(&mut self) {
self.index = 0;
}
}
impl<T: Transformation + Clone + ToBoundingBox, const LIN: bool> WindingZonesPeriodic<T, LIN> {
fn next_priv(&mut self) -> Option<(T, Zone)> {
let result = self.nth_priv(self.index);
self.index = self.index + 1;
result
}
fn nth_priv(&mut self, n: usize) -> Option<(T, Zone)> {
let layers = self.layers();
if n >= (self.slots as usize) * layers {
return None;
}
let layer = n.rem_euclid(layers);
let slot = n / layers;
let mut geom = self.zones[layer].clone();
if LIN {
let factor = if slot == 0
&& self.starts_in_slot_middle
&& geom.bounding_box().xmax() <= DEFAULT_EPSILON
{
1.0
} else {
1.0 / f64::from(self.slots)
* (slot as f64 + 0.5 * (!self.starts_in_slot_middle) as u32 as f64)
};
geom.translate([self.air_gap_length.get::<meter>() * factor, 0.0]);
} else {
geom.translate([0.0, self.air_gap_length.get::<meter>() / TAU]);
let angle = -TAU
* (slot as f64 + 0.5 * (!self.starts_in_slot_middle) as u32 as f64) as f64
/ self.slots as f64
+ FRAC_PI_2;
geom.rotate([0.0, 0.0], angle);
}
return Some((
geom,
Zone {
slot: slot as u16,
layer: layer as u16,
},
));
}
}
impl<const LIN: bool> WindingZonesPeriodic<Polysegment, LIN> {
pub fn from_slot<S: Slot + ?Sized>(
air_gap_length: Length,
slots: NonZeroU16,
slot: &S,
starts_in_slot_middle: bool,
outer_core: bool,
) -> Self {
let air_gap_length = if LIN {
air_gap_length
} else {
use stem_material::uom::typenum::P2;
let air_gap_radius = air_gap_length / TAU;
let mod_radius = 0.5
* (4.0 * air_gap_radius.powi(P2::new()) - slot.opening_width().powi(P2::new()))
.sqrt();
mod_radius * TAU
};
let mut slot_outline: Polysegment = slot.outline().into_owned();
if !LIN && !outer_core {
slot_outline.line_reflection([0.0, 0.0], [1.0, 0.0])
};
return Self {
slots: slots.get(),
air_gap_length,
zones: vec![slot_outline],
starts_in_slot_middle,
index: 0,
};
}
}
impl<const LIN: bool> WindingZonesPeriodic<Contour, LIN> {
pub fn from_slot<S: Slot + ?Sized>(
air_gap_length: Length,
slots: u16,
slot: &S,
coil_layout: &CoilLayout,
starts_in_slot_middle: bool,
outer_core: bool,
) -> Self {
let mut zones = slot.layer_contours(coil_layout);
let air_gap_length = if LIN {
air_gap_length
} else {
use stem_material::uom::typenum::P2;
let air_gap_radius = air_gap_length / TAU;
if slot.is_open() && coil_layout == &CoilLayout::SingleFilled {
let mut contour = Contour::new(Polysegment::new());
std::mem::swap(&mut contour, &mut zones[0]);
let mut slot_seg = Polysegment::from(contour);
if let Some(opening_seg) = slot_seg.pop_back() {
if let Ok(a) = ArcSegment::from_start_stop_radius(
opening_seg.start(),
opening_seg.stop(),
air_gap_radius.get::<meter>(),
outer_core,
false,
) {
slot_seg.push_back(a.into());
} else {
slot_seg.push_back(opening_seg.into());
}
}
let mut contour = Contour::new(slot_seg);
std::mem::swap(&mut contour, &mut zones[0]);
}
let mod_radius = 0.5
* (4.0 * air_gap_radius.powi(P2::new()) - slot.opening_width().powi(P2::new()))
.sqrt();
mod_radius * TAU
};
if !LIN && !outer_core {
zones
.iter_mut()
.for_each(|c| c.line_reflection([0.0, 0.0], [1.0, 0.0]));
};
return Self {
slots,
air_gap_length,
zones,
starts_in_slot_middle,
index: 0,
};
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Air gap winding dimensions][cad_plain_air_gap_winding]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_plain_air_gap_winding",
"docs/img/cad_plain_air_gap_winding.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."
)]
pub fn from_air_gap_winding(
air_gap_length: Length,
slots: u16,
air_gap_winding_height: Length,
winding_coverage: f64,
coil_layout: &CoilLayout,
starts_in_slot_middle: bool,
outer_core: bool,
) -> Self {
let mut zones = Vec::with_capacity(coil_layout.layers().get() as usize);
let ag_height = if outer_core {
-air_gap_winding_height.get::<meter>()
} else {
air_gap_winding_height.get::<meter>()
};
if LIN {
let half_slot_width =
winding_coverage.clamp(0.0, 1.0) * 0.5 * air_gap_length.get::<meter>()
/ slots as f64;
match coil_layout {
CoilLayout::Single | CoilLayout::SingleFilled => zones.push(Contour::rectangle(
[-half_slot_width, 0.0],
[half_slot_width, ag_height],
)),
CoilLayout::DoubleVertical => {
zones.push(Contour::rectangle(
[-half_slot_width, 0.0],
[half_slot_width, 0.5 * ag_height],
));
zones.push(Contour::rectangle(
[-half_slot_width, 0.5 * ag_height],
[half_slot_width, ag_height],
));
}
CoilLayout::DoubleHorizontal => {
zones.push(Contour::rectangle(
[-half_slot_width, 0.0],
[0.0, ag_height],
));
zones.push(Contour::rectangle([0.0, 0.0], [half_slot_width, ag_height]));
}
CoilLayout::Quadruple => {
zones.push(Contour::rectangle(
[-half_slot_width, 0.0],
[0.0, 0.5 * ag_height],
));
zones.push(Contour::rectangle(
[-half_slot_width, 0.5 * ag_height],
[0.0, ag_height],
));
zones.push(Contour::rectangle(
[0.0, 0.5 * ag_height],
[half_slot_width, ag_height],
));
zones.push(Contour::rectangle(
[0.0, 0.0],
[half_slot_width, 0.5 * ag_height],
));
}
CoilLayout::MultiVertical(layers) => {
let layers = layers.get();
let layer_height = ag_height / (layers) as f64;
for layer in 0..(layers) {
zones.push(Contour::rectangle(
[-half_slot_width, layer as f64 * layer_height],
[half_slot_width, (layer + 1) as f64 * layer_height],
));
}
}
}
} else {
let angle = winding_coverage.clamp(0.0, 1.0) * TAU / slots as f64;
let agr = air_gap_length.get::<meter>() / TAU;
match coil_layout {
CoilLayout::Single | CoilLayout::SingleFilled => {
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
-0.5 * angle + FRAC_PI_2,
angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
0.5 * angle + FRAC_PI_2,
-angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
}
CoilLayout::DoubleVertical => {
let radicand = (agr + ag_height).powi(2) + agr.powi(2);
let mean_radius = radicand.signum() * FRAC_1_SQRT_2 * radicand.sqrt();
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
-0.5 * angle + FRAC_PI_2,
angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
0.5 * angle + FRAC_PI_2,
-angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
-0.5 * angle + FRAC_PI_2,
angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
0.5 * angle + FRAC_PI_2,
-angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
}
CoilLayout::DoubleHorizontal => {
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
FRAC_PI_2 + 0.5 * angle,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
-0.5 * angle + FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
FRAC_PI_2,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
}
CoilLayout::Quadruple => {
let radicand = (agr + ag_height).powi(2) + agr.powi(2);
let mean_radius = radicand.signum() * FRAC_1_SQRT_2 * radicand.sqrt();
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
0.5 * angle + FRAC_PI_2,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
0.5 * angle + FRAC_PI_2,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
-0.5 * angle + FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr + ag_height,
FRAC_PI_2,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
agr,
-0.5 * angle + FRAC_PI_2,
0.5 * angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
mean_radius,
FRAC_PI_2,
-0.5 * angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
}
CoilLayout::MultiVertical(layers) => {
let layers = layers.get();
let layer_height = ag_height / (layers) as f64;
for layer in 0..(layers) {
let first_radius = agr + layer as f64 * layer_height;
let second_radius = agr + (layer + 1) as f64 * layer_height;
let mut ps = Polysegment::with_capacity(4);
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
first_radius,
-0.5 * angle + FRAC_PI_2,
angle,
) {
ps.push_back(arc.into());
}
if let Ok(arc) = ArcSegment::from_center_radius_start_sweep_angle(
[0.0, 0.0],
second_radius,
0.5 * angle + FRAC_PI_2,
-angle,
) {
ps.push_back(arc.into());
}
zones.push(ps.into());
}
}
}
zones.iter_mut().for_each(|c| c.translate([0.0, -agr]));
}
return Self {
slots,
air_gap_length,
zones,
starts_in_slot_middle,
index: 0,
};
}
}
impl<const LIN: bool> Iterator for WindingZonesPeriodic<Polysegment, LIN> {
type Item = Polysegment;
fn next(&mut self) -> Option<Self::Item> {
self.next_priv().map(|t| t.0)
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.nth_priv(n).map(|t| t.0)
}
}
impl<const LIN: bool> Iterator for WindingZonesPeriodic<Contour, LIN> {
type Item = PositionedZoneContour;
fn next(&mut self) -> Option<Self::Item> {
self.next_priv().map(From::from)
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
self.nth_priv(n).map(From::from)
}
}
pub struct WindingZones(WindingZonesInner);
impl WindingZones {
pub fn from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = PositionedZoneContour> + 'static,
{
Self(WindingZonesInner::Other(Box::new(iter)))
}
}
enum WindingZonesInner {
WindingZonesPeriodicLin(WindingZonesPeriodic<Contour, true>),
WindingZonesPeriodicRot(WindingZonesPeriodic<Contour, false>),
Other(Box<dyn Iterator<Item = PositionedZoneContour>>),
}
impl From<WindingZonesInner> for WindingZones {
fn from(value: WindingZonesInner) -> Self {
Self(value)
}
}
impl Iterator for WindingZones {
type Item = PositionedZoneContour;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
WindingZonesInner::WindingZonesPeriodicLin(i) => i.next(),
WindingZonesInner::WindingZonesPeriodicRot(i) => i.next(),
WindingZonesInner::Other(i) => i.next(),
}
}
fn nth(&mut self, n: usize) -> Option<Self::Item> {
match &mut self.0 {
WindingZonesInner::WindingZonesPeriodicLin(i) => i.nth(n),
WindingZonesInner::WindingZonesPeriodicRot(i) => i.nth(n),
WindingZonesInner::Other(i) => i.nth(n),
}
}
}
impl From<WindingZonesPeriodic<Contour, true>> for WindingZones {
fn from(value: WindingZonesPeriodic<Contour, true>) -> Self {
WindingZonesInner::WindingZonesPeriodicLin(value).into()
}
}
impl From<WindingZonesPeriodic<Contour, false>> for WindingZones {
fn from(value: WindingZonesPeriodic<Contour, false>) -> Self {
WindingZonesInner::WindingZonesPeriodicRot(value).into()
}
}
impl From<Box<dyn Iterator<Item = PositionedZoneContour>>> for WindingZones {
fn from(value: Box<dyn Iterator<Item = PositionedZoneContour>>) -> Self {
WindingZonesInner::Other(value).into()
}
}