#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
feature = "doc-images",
doc = "![Surface and interior magnets][surface_and_interior_magnets.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("surface_and_interior_magnets.svg", "docs/img/surface_and_interior_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."
)]
use crate::planar_geo;
use num::Integer;
use planar_geo::prelude::*;
use std::f64::consts::{FRAC_PI_2, PI, TAU};
use stem_magnet::prelude::*;
#[derive(Debug, Clone)]
pub struct PositionedMagnetShape {
pub shape: Shape,
pub is_north: bool,
pub magnet_type: usize,
pub pole: usize,
}
impl PositionedMagnetShape {
#[cfg(feature = "cairo")]
pub fn into_drawable(self) -> Drawable {
let style = if self.is_north {
stem_magnet::NORTH_POLE_STYLE
} else {
stem_magnet::SOUTH_POLE_STYLE
};
return Drawable::new(self.shape, style);
}
#[cfg(feature = "cairo")]
pub fn into_drawable_with_style(self, style: planar_geo::draw::Style) -> Drawable {
Drawable::new(self.shape, style)
}
#[cfg(feature = "cairo")]
pub fn as_drawable<'a>(&'a self) -> DrawableRef<'a> {
let style = if self.is_north {
stem_magnet::NORTH_POLE_STYLE
} else {
stem_magnet::SOUTH_POLE_STYLE
};
return DrawableRef::new(&self.shape, style);
}
#[cfg(feature = "cairo")]
pub fn as_drawable_with_style<'a>(&'a self, style: planar_geo::draw::Style) -> DrawableRef<'a> {
return DrawableRef::new(&self.shape, style);
}
}
#[cfg(feature = "cairo")]
impl From<PositionedMagnetShape> for Drawable {
fn from(value: PositionedMagnetShape) -> Self {
value.into_drawable()
}
}
impl Transformation for PositionedMagnetShape {
fn translate(&mut self, shift: [f64; 2]) {
self.shape.translate(shift);
}
fn rotate(&mut self, center: [f64; 2], angle: f64) {
self.shape.rotate(center, angle);
}
fn scale(&mut self, factor: f64) {
self.shape.scale(factor);
}
fn line_reflection(&mut self, start: [f64; 2], stop: [f64; 2]) -> () {
self.shape.line_reflection(start, stop);
}
}
impl ToBoundingBox for PositionedMagnetShape {
fn bounding_box(&self) -> planar_geo::prelude::BoundingBox {
self.shape.bounding_box()
}
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![How MagnetsPeriodic distributes the winding zones][cad_magnets_periodic]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image("cad_magnets_periodic", "docs/img/cad_magnets_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 MagnetsPeriodic<const LIN: bool> {
air_gap_length: Length,
magnets: Vec<PositionedMagnetShape>,
poles: usize,
d_axis_offset: f64,
index: usize,
}
impl<const LIN: bool> MagnetsPeriodic<LIN> {
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![How MagnetsPeriodic distributes the winding zones][cad_magnets_periodic]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_magnets_periodic",
"docs/img/cad_magnets_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,
magnets: Vec<PositionedMagnetShape>,
poles: usize,
d_axis_offset: f64,
) -> Self {
Self {
poles,
air_gap_length,
magnets,
index: 0,
d_axis_offset,
}
}
pub fn reset(&mut self) {
self.index = 0;
}
}
impl<const LIN: bool> Iterator for MagnetsPeriodic<LIN> {
type Item = PositionedMagnetShape;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.poles * self.magnets.len() {
return None;
}
let current_pole = self.index / (self.magnets.len());
let pole_idx = self.index.rem_euclid(self.magnets.len());
let shape_idx = pole_idx.rem_euclid(self.magnets.len());
self.index = self.index + 1;
let mut shape = self.magnets[shape_idx].clone();
if LIN {
let width_per_pole = self.air_gap_length.get::<meter>() / self.poles as f64;
let offset = (self.d_axis_offset / PI + current_pole as f64) * width_per_pole;
shape.translate([offset, 0.0]);
} else {
shape.translate([0.0, self.air_gap_length.get::<meter>() / TAU]);
let angle_per_pole = TAU / self.poles as f64;
let angle = angle_per_pole * (0.5 + current_pole as f64);
shape.rotate([0.0, 0.0], -angle + self.d_axis_offset);
}
if current_pole.is_odd() {
shape.is_north = !shape.is_north;
}
shape.pole = current_pole;
return Some(shape);
}
}
pub struct Magnets(MagnetsInner);
impl Magnets {
pub fn from_iter<I>(iter: I) -> Self
where
I: Iterator<Item = PositionedMagnetShape> + 'static,
{
Self(MagnetsInner::Other(Box::new(iter)))
}
}
enum MagnetsInner {
MagnetsPeriodicLin(MagnetsPeriodic<true>),
MagnetsPeriodicRot(MagnetsPeriodic<false>),
Other(Box<dyn Iterator<Item = PositionedMagnetShape>>),
}
impl From<MagnetsInner> for Magnets {
fn from(value: MagnetsInner) -> Self {
Self(value)
}
}
impl From<MagnetsPeriodic<true>> for Magnets {
fn from(value: MagnetsPeriodic<true>) -> Self {
MagnetsInner::MagnetsPeriodicLin(value).into()
}
}
impl From<MagnetsPeriodic<false>> for Magnets {
fn from(value: MagnetsPeriodic<false>) -> Self {
MagnetsInner::MagnetsPeriodicRot(value).into()
}
}
impl From<Box<dyn Iterator<Item = PositionedMagnetShape>>> for Magnets {
fn from(value: Box<dyn Iterator<Item = PositionedMagnetShape>>) -> Self {
MagnetsInner::Other(value).into()
}
}
impl Iterator for Magnets {
type Item = PositionedMagnetShape;
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
MagnetsInner::MagnetsPeriodicLin(i) => i.next(),
MagnetsInner::MagnetsPeriodicRot(i) => i.next(),
MagnetsInner::Other(i) => i.next(),
}
}
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Creation of the shapes of a surface magnet assembly][cad_surface_magnet_assembly_shapes]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_surface_magnet_assembly_shapes",
"docs/img/cad_surface_magnet_assembly_shapes.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."
)]
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Influence of the coverage_single_magnet parameter][cad_surface_magnet_assembly_shapes_lin]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_surface_magnet_assembly_shapes_lin",
"docs/img/cad_surface_magnet_assembly_shapes_lin.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 surface_magnet_assembly_shapes_lin(
magnet_assembly: &MagnetAssembly,
split: bool,
coverage_single_magnet: Option<Length>,
) -> Vec<PositionedMagnetShape> {
let mut proto_shapes: Vec<PositionedMagnetShape> = if split {
magnet_assembly
.magnet()
.north_south_shapes()
.into_iter()
.enumerate()
.map(|(i, m)| PositionedMagnetShape {
shape: m.into_owned(),
is_north: i.is_even(),
magnet_type: 0,
pole: 0,
})
.collect()
} else {
vec![PositionedMagnetShape {
shape: magnet_assembly.magnet().shape().into_owned(),
is_north: true,
magnet_type: 0,
pole: 0,
}]
};
for s in proto_shapes.iter_mut() {
s.line_reflection([0.0, 0.0], [1.0, 0.0]);
}
let magnet_coverage = coverage_single_magnet
.map(|l| l.get::<meter>())
.unwrap_or_else(|| {
BoundingBox::from_bounded_entities(proto_shapes.iter().map(|p| &p.shape))
.map(|m| m.width())
.unwrap_or(0.0)
});
let mut shapes =
Vec::with_capacity(magnet_assembly.num_tangential().get() * proto_shapes.len());
for tan_idx in 0..magnet_assembly.num_tangential().get() {
for mut shape in proto_shapes.iter().cloned() {
let offset = (tan_idx as f64
- 0.5 * (magnet_assembly.num_tangential().get() as f64 - 1.0))
* magnet_coverage;
shape.translate([offset, 0.0]);
shapes.push(shape);
}
}
return shapes;
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Creation of the shapes of a surface magnet assembly][cad_surface_magnet_assembly_shapes]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_surface_magnet_assembly_shapes",
"docs/img/cad_surface_magnet_assembly_shapes.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."
)]
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Influence of the coverage_single_magnet parameter][cad_surface_magnet_assembly_shapes_rot]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image(
"cad_surface_magnet_assembly_shapes_rot",
"docs/img/cad_surface_magnet_assembly_shapes_rot.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 surface_magnet_assembly_shapes_rot(
magnet_assembly: &MagnetAssembly,
split: bool,
radius: Length,
is_outer: bool,
coverage_single_magnet: Option<f64>,
) -> Vec<PositionedMagnetShape> {
let mut proto_shapes: Vec<PositionedMagnetShape> = if split {
magnet_assembly
.magnet()
.north_south_shapes()
.into_iter()
.enumerate()
.map(|(i, m)| PositionedMagnetShape {
shape: m.into_owned(),
is_north: i.is_even(),
magnet_type: 0,
pole: 0,
})
.collect()
} else {
vec![PositionedMagnetShape {
shape: magnet_assembly.magnet().shape().into_owned(),
is_north: true,
magnet_type: 0,
pole: 0,
}]
};
if is_outer {
for s in proto_shapes.iter_mut() {
s.line_reflection([0.0, 0.0], [1.0, 0.0]);
}
}
let magnet_coverage = coverage_single_magnet.unwrap_or_else(|| {
let radius = radius.get::<meter>();
covered_angle(proto_shapes.iter().map(|p| &p.shape), radius)
});
let mut shapes =
Vec::with_capacity(magnet_assembly.num_tangential().get() * proto_shapes.len());
for tan_idx in 0..magnet_assembly.num_tangential().get() {
for mut shape in proto_shapes.iter().cloned() {
let angle = (tan_idx as f64
- 0.5 * (magnet_assembly.num_tangential().get() as f64 - 1.0))
* magnet_coverage;
shape.rotate([0.0, -radius.get::<meter>()], -angle);
shapes.push(shape);
}
}
return shapes;
}
#[doc = ""]
#[cfg_attr(
feature = "doc-images",
doc = "![Coverage of magnet shapes][cad_covered_angle]"
)]
#[cfg_attr(
feature = "doc-images",
embed_doc_image::embed_doc_image("cad_covered_angle", "docs/img/cad_covered_angle.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 covered_angle<'a, I: Iterator<Item = &'a Shape> + Clone>(shapes: I, radius: f64) -> f64 {
let mut angle = shapes
.clone()
.map(|shape| shape.contour().points())
.flatten()
.map(|p| (p[1] + radius).atan2(p[0]))
.reduce(f64::max)
.unwrap_or(FRAC_PI_2 * radius.signum());
for s in shapes.map(|shape| shape.contour().segments()).flatten() {
'angle_increment: while angle < PI {
let line = Line::from_point_angle([0.0, -radius], angle);
if let Segment::ArcSegment(a) = s {
match a.intersections_primitive(&line) {
PrimitiveIntersections::Zero => break 'angle_increment,
PrimitiveIntersections::One(_) => break 'angle_increment,
PrimitiveIntersections::Two(_) => {
angle += 0.1;
}
}
} else {
break 'angle_increment;
}
}
}
return 2.0 * (angle - FRAC_PI_2 * radius.signum());
}