stem_core 0.1.0

Magnetic core definition for stem - a Simulation Toolbox for Electric Motors.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
/*!
This module provides the [`LinCore`] and [`RotCore`] structs, the owning and
borrowing enum wrappers [`Core`] and [`CoreRef`] and the sealed trait
[`CoreExt`] which provides a common interface for both core types.

# Overview

Both stator and rotor of a radial flux machine are typically built around a
magnetic core, which serves as a scaffolding for mounting magnets and windings
and guides the magnetic flux produced by them. Radial flux machines can be
either _linear_ (meaning that the rotor performs a linear movement relative
to the stator) or _rotary_ (meaning that the rotor rotates around its center).
For a linear machine, both the stator and rotor are usually cuboids, whereas for
a rotary machine, they are annular (hollow) cylinders that share a common
rotation axis. In stem_core, a linear core is modeled by the [`LinCore`] type, a
rotary core by the [`RotCore`] type.

The magnetic force that drives a motor or powers a generator is created by
magnetic flux passing through the air gap between stator and rotor. The local
distribution of that flux is heavily influenced by the shape of the stator /
rotor contour at the air gap. That contour is defined by an
[`AirGap`](crate::air_gap::AirGap) trait object used to create the [`LinCore`] /
[`RotCore`] instance. The following image shows an example for both core types
where the air gap contour is defined by [`Slot`](stem_slot::slot::Slot)s:
*/
#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
    feature = "doc-images",
    doc = "![Slotted linear and rotary core][lin_and_rot_core_slotted.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("lin_and_rot_core_slotted.svg", "docs/img/lin_and_rot_core_slotted.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."
)]
/*!
_Image created with `/examples/lin_and_rot_core_plots.rs`._

The core shape can be further customized by optionally inserting a
[`FluxBarrier`](crate::flux_barrier::FluxBarrier) into the core, which can both
be used to direct the magnetic flux and to provide space for interior magnets.
The latter aspect is discussed in depth further below.

Since every core in stem is either a [`LinCore`] or a [`RotCore`], an owning
([`Core`]) and a borrowing ([`CoreRef`]) wrapper is provided as well. The
sealed [`CoreExt`] trait provides a common interface for both [`LinCore`] and
[`RotCore`] and therefore also for the [`Core`] and [`CoreRef`] enums.

# Windable cores

A core is called _windable_ if its [`AirGap`](crate::air_gap::AirGap) contour
allows for a winding to be mounted (see [`CoreExt::is_windable`]). The following
image shows two different examples: One with a
[`PlainAirGap`](crate::air_gap::PlainAirGap), where the winding is mounted
directly on the core / in the air gap, and one with a
[`SlottedAirGap`](crate::air_gap::SlottedAirGap), where the winding is mounted
inside the slots along the air gap contour.

*/
#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
    feature = "doc-images",
    doc = "![Winding in core][lin_core_air_gap_and_slotted_winding.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("lin_core_air_gap_and_slotted_winding.svg", "docs/img/lin_core_air_gap_and_slotted_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."
)]
/*!
_Image created with `/examples/lin_and_rot_core_plots.rs`._

It is evident that the space available for the individual winding zones is
determined by the specific [`AirGap`](crate::air_gap::AirGap) trait object.
[`CoreExt::winding_zones`] returns an iterator over the winding zone contours
together with the respective [`Zone`](crate::winding_zones::Zone) index. This
iterator can be used, for example, to create a visualization of the winding
itself (as shown in the previous image) or to determine the available area for
the winding and hence its resistance and current carrying capacity.

# Surface magnets

Some [`AirGap`](crate::air_gap::AirGap)s allow for mounting magnets directly on
the core surface / in the core air gap. Similar to [`CoreExt::winding_zones`],
the [`CoreExt::surface_magnets`] method returns an iterator over the
[`PositionedMagnetShape`](crate::magnets::PositionedMagnetShape)s for a provided
[`MagnetAssembly`]. The [`CoreExt::assembly_check`] method can be used to
check if the mounting results in collisions and therefore if the provided
[`MagnetAssembly`] is compatible with the core. The image below shows an example
for a linear core with an assembly consisting of [`BreadLoafMagnet`]s and for a
rotary core with an assembly consisting of [`ArcParallelMagnet`]s.

*/
#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
    feature = "doc-images",
    doc = "![Surface magnets on cores][lin_and_rot_core_surface_magnets.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("lin_and_rot_core_surface_magnets.svg", "docs/img/lin_and_rot_core_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."
)]
/*!
_Image created with `/examples/lin_and_rot_core_plots.rs`._

# Interior magnets

Some [`FluxBarrier`](crate::flux_barrier::FluxBarrier)s provide space for
interior magnets. Similar to [`CoreExt::surface_magnets`], the
[`CoreExt::interior_magnets`] method returns an iterator over the
[`PositionedMagnetShape`](crate::magnets::PositionedMagnetShape)s of the
interior magnets. The type of those magnets is determined by the
[`FluxBarrier`](crate::flux_barrier::FluxBarrier), hence it is not necessary to
specify the [`MagnetAssembly`] for the interior magnets. In the image below,
a linear and a rotary core with the
[`Spoke1FluxBarrier`](crate::flux_barrier::Spoke1FluxBarrier) are shown.
*/
#![cfg_attr(feature = "doc-images", doc = "")]
#![cfg_attr(
    feature = "doc-images",
    doc = "![Interior magnets in cores][lin_and_rot_core_interior_magnets.svg]"
)]
#![cfg_attr(feature = "doc-images",
cfg_attr(all(),
doc = ::embed_doc_image::embed_image!("lin_and_rot_core_interior_magnets.svg", "docs/img/lin_and_rot_core_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."
)]
/*!
_Image created with `/examples/lin_and_rot_core_plots.rs`._
 */

use std::sync::Arc;

use planar_geo::prelude::Shape;
use stem_magnet::prelude::*;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

pub mod ext;
pub mod lin;
pub mod rot;

pub use ext::*;
pub use lin::*;
pub use rot::*;

use crate::LinOrRot;
use crate::error::IncompatibleFluxBarrier;
use crate::flux_barrier::FluxBarrier;

/**
An owning enum wrapping a [`LinCore`] or [`RotCore`].

This enum is meant for use cases where a magnetic core is needed but the type
(linear or rotary) is unknown until runtime. This enum provides a type-erased
wrapper which "behaves like" a magnetic core by implementing [`CoreExt`]. Every
method implementation looks like this:

```ignore
fn pole_pairs(&self) -> u16 {
    match self {
        Self::Lin(c) => c.pole_pairs(),
        Self::Rot(c) => c.pole_pairs(),
    }
}
```
 */
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Core {
    /// A wrapped [`LinCore`].
    Lin(LinCore),
    /// A wrapped [`RotCore`].
    Rot(RotCore),
}

impl Core {
    /**
    Returns a reference to the underlying [`LinCore`], if `self` wraps one.
    If `self` wraps a [`RotCore`], this method returns `None` instead.
     */
    pub fn lin<'core>(&'core self) -> Option<&'core LinCore> {
        match self {
            Self::Lin(c) => Some(c),
            Self::Rot(_) => None,
        }
    }

    /**
    Returns a reference to the underlying [`RotCore`], if `self` wraps one.
    If `self` wraps a [`LinCore`], this method returns `None` instead.
     */
    pub fn rot<'core>(&'core self) -> Option<&'core RotCore> {
        match self {
            Self::Lin(_) => None,
            Self::Rot(c) => Some(c),
        }
    }

    /// Fallibly inserts a new [`FluxBarrier`] into `self` or removes an
    /// existing one.
    ///
    /// If `flux_barrier` is `Some`, it is checked whether the wrapped
    /// [`FluxBarrier`] is compatible to `self` by creating the flux barrier
    /// contours via the [`FluxBarrier::combine`] method and checking if those
    /// fit into the shape of `self`. If [`FluxBarrier::combine`] fails or if
    /// the contours don't fit, the resulting error is wrapped into
    /// [`IncompatibleFluxBarrier`] and returned together with the given
    /// [`FluxBarrier`]. Otherwise, the old flux barrier of `self` will be
    /// replaced with the new one. If `flux_barrier` is `None` and `self` has a
    /// flux barrier, it will be removed. Otherwise, this is a no-op.
    ///
    /// This method forwards to either [`LinCore::set_flux_barrier`] or
    /// [`RotCore::set_flux_barrier`], see their docstrings for examples.
    pub fn set_flux_barrier(
        &mut self,
        flux_barrier: Option<Box<dyn FluxBarrier>>,
    ) -> Result<(), IncompatibleFluxBarrier> {
        match self {
            Self::Lin(c) => c.set_flux_barrier(flux_barrier),
            Self::Rot(c) => c.set_flux_barrier(flux_barrier),
        }
    }
}

impl ext::private::Sealed for Core {}

impl CoreExt for Core {
    fn air_gap(&self) -> &dyn crate::air_gap::AirGap {
        match self {
            Self::Lin(c) => c.air_gap(),
            Self::Rot(c) => c.air_gap(),
        }
    }

    fn axial_length(&self) -> Length {
        match self {
            Self::Lin(c) => c.axial_length(),
            Self::Rot(c) => c.axial_length(),
        }
    }

    fn iron_fill_factor(&self) -> f64 {
        match self {
            Self::Lin(c) => c.iron_fill_factor(),
            Self::Rot(c) => c.iron_fill_factor(),
        }
    }

    fn pole_pairs(&self) -> u16 {
        match self {
            Self::Lin(c) => c.pole_pairs(),
            Self::Rot(c) => c.pole_pairs(),
        }
    }

    fn material(&self) -> &Arc<Material> {
        match self {
            Self::Lin(c) => c.material(),
            Self::Rot(c) => c.material(),
        }
    }

    fn yoke_height(&self) -> Length {
        match self {
            Self::Lin(c) => c.yoke_height(),
            Self::Rot(c) => c.yoke_height(),
        }
    }

    fn skew_angle(&self) -> f64 {
        match self {
            Self::Lin(c) => c.skew_angle(),
            Self::Rot(c) => c.skew_angle(),
        }
    }

    fn air_gap_length(&self) -> Length {
        match self {
            Self::Lin(c) => c.air_gap_length(),
            Self::Rot(c) => c.air_gap_length(),
        }
    }

    fn lin_or_rot(&self) -> LinOrRot {
        match self {
            Self::Lin(c) => c.lin_or_rot(),
            Self::Rot(c) => c.lin_or_rot(),
        }
    }

    fn axial_coil_overhang(&self) -> Length {
        match self {
            Self::Lin(c) => c.axial_coil_overhang(),
            Self::Rot(c) => c.axial_coil_overhang(),
        }
    }

    fn flux_barrier(&self) -> Option<&dyn crate::flux_barrier::FluxBarrier> {
        match self {
            Self::Lin(c) => c.flux_barrier(),
            Self::Rot(c) => c.flux_barrier(),
        }
    }

    fn shape<'a>(&'a self) -> &'a Shape {
        match self {
            Self::Lin(c) => c.shape(),
            Self::Rot(c) => c.shape(),
        }
    }

    fn as_core_ref(&self) -> CoreRef<'_> {
        match self {
            Self::Lin(c) => c.as_core_ref(),
            Self::Rot(c) => c.as_core_ref(),
        }
    }

    fn pole_coverage(&self, surface_magnet_assembly: Option<&MagnetAssembly>) -> f64 {
        match self {
            Self::Lin(c) => c.pole_coverage(surface_magnet_assembly),
            Self::Rot(c) => c.pole_coverage(surface_magnet_assembly),
        }
    }

    fn tooth_mass(&self) -> Mass {
        match self {
            Self::Lin(c) => c.tooth_mass(),
            Self::Rot(c) => c.tooth_mass(),
        }
    }
}

impl From<LinCore> for Core {
    fn from(value: LinCore) -> Self {
        Self::Lin(value)
    }
}

impl From<RotCore> for Core {
    fn from(value: RotCore) -> Self {
        Self::Rot(value)
    }
}

impl TryFrom<Core> for LinCore {
    type Error = Core;

    fn try_from(value: Core) -> Result<Self, Self::Error> {
        match value {
            Core::Lin(c) => Ok(c),
            Core::Rot(_) => Err(value),
        }
    }
}

impl TryFrom<Core> for RotCore {
    type Error = Core;

    fn try_from(value: Core) -> Result<Self, Self::Error> {
        match value {
            Core::Lin(_) => Err(value),
            Core::Rot(c) => Ok(c),
        }
    }
}

// =============================================================================

/**
A borrowing enum wrapping a [`LinCore`] or [`RotCore`] reference.

This enum is meant for use cases where a reference to a magnetic core is needed
but the type (linear or rotary) is unknown until runtime. This enum provides an
type-erased wrapper which "behaves like" a magnetic core by implementing
[`CoreExt`]. Every method implementation looks like this:

```ignore
fn pole_pairs(&self) -> u16 {
    match self {
        Self::Lin(c) => c.pole_pairs(),
        Self::Rot(c) => c.pole_pairs(),
    }
}
```

This wrapper is particularly useful for the [`AirGap`](crate::air_gap::AirGap)
and [`FluxBarrier`] traits, since these are meant to be used for creating trait
objects and therefore don't have generic methods. Therefore, the trait methods
require passing a [`CoreRef`] object, which can be created via the
[`CoreExt::as_core_ref`] method from a [`LinCore`], [`RotCore`] or [`Core`].
 */
#[derive(Debug, Clone, Copy)]
pub enum CoreRef<'a> {
    /// A reference to a [`LinCore`].
    Lin(&'a LinCore),
    /// A reference to a [`RotCore`].
    Rot(&'a RotCore),
}

impl<'a> CoreRef<'a> {
    /**
    Returns the underlying [`LinCore`] reference, if `self` wraps one.
    If `self` wraps a [`RotCore`] reference, this method returns `None` instead.
     */
    pub fn lin<'core>(&'core self) -> Option<&'core LinCore> {
        match self {
            CoreRef::Lin(c) => Some(*c),
            CoreRef::Rot(_) => None,
        }
    }

    /**
    Returns the underlying [`RotCore`] reference, if `self` wraps one.
    If `self` wraps a [`LinCore`] reference, this method returns `None` instead.
     */
    pub fn rot<'core>(&'core self) -> Option<&'core RotCore> {
        match self {
            CoreRef::Lin(_) => None,
            CoreRef::Rot(c) => Some(*c),
        }
    }
}

impl<'a> ext::private::Sealed for CoreRef<'a> {}

impl<'a> CoreExt for CoreRef<'a> {
    fn air_gap(&self) -> &dyn crate::air_gap::AirGap {
        match self {
            Self::Lin(c) => c.air_gap(),
            Self::Rot(c) => c.air_gap(),
        }
    }

    fn axial_length(&self) -> Length {
        match self {
            Self::Lin(c) => c.axial_length(),
            Self::Rot(c) => c.axial_length(),
        }
    }

    fn iron_fill_factor(&self) -> f64 {
        match self {
            Self::Lin(c) => c.iron_fill_factor(),
            Self::Rot(c) => c.iron_fill_factor(),
        }
    }

    fn pole_pairs(&self) -> u16 {
        match self {
            Self::Lin(c) => c.pole_pairs(),
            Self::Rot(c) => c.pole_pairs(),
        }
    }

    fn material(&self) -> &Arc<Material> {
        match self {
            Self::Lin(c) => c.material(),
            Self::Rot(c) => c.material(),
        }
    }

    fn yoke_height(&self) -> Length {
        match self {
            Self::Lin(c) => c.yoke_height(),
            Self::Rot(c) => c.yoke_height(),
        }
    }

    fn skew_angle(&self) -> f64 {
        match self {
            Self::Lin(c) => c.skew_angle(),
            Self::Rot(c) => c.skew_angle(),
        }
    }

    fn air_gap_length(&self) -> Length {
        match self {
            Self::Lin(c) => c.air_gap_length(),
            Self::Rot(c) => c.air_gap_length(),
        }
    }

    fn lin_or_rot(&self) -> LinOrRot {
        match self {
            Self::Lin(c) => c.lin_or_rot(),
            Self::Rot(c) => c.lin_or_rot(),
        }
    }

    fn axial_coil_overhang(&self) -> Length {
        match self {
            Self::Lin(c) => c.axial_coil_overhang(),
            Self::Rot(c) => c.axial_coil_overhang(),
        }
    }

    fn flux_barrier(&self) -> Option<&dyn crate::flux_barrier::FluxBarrier> {
        match self {
            Self::Lin(c) => c.flux_barrier(),
            Self::Rot(c) => c.flux_barrier(),
        }
    }

    fn shape(&self) -> &Shape {
        match self {
            Self::Lin(c) => c.shape(),
            Self::Rot(c) => c.shape(),
        }
    }

    fn as_core_ref(&self) -> CoreRef<'_> {
        return self.clone();
    }

    fn pole_coverage(&self, surface_magnet_assembly: Option<&MagnetAssembly>) -> f64 {
        match self {
            Self::Lin(c) => c.pole_coverage(surface_magnet_assembly),
            Self::Rot(c) => c.pole_coverage(surface_magnet_assembly),
        }
    }

    fn tooth_mass(&self) -> Mass {
        match self {
            Self::Lin(c) => c.tooth_mass(),
            Self::Rot(c) => c.tooth_mass(),
        }
    }
}

impl<'a> From<&'a Core> for CoreRef<'a> {
    fn from(value: &'a Core) -> Self {
        match value {
            Core::Lin(c) => Self::Lin(c),
            Core::Rot(c) => Self::Rot(c),
        }
    }
}

impl<'a> From<&'a LinCore> for CoreRef<'a> {
    fn from(value: &'a LinCore) -> Self {
        Self::Lin(value)
    }
}

impl<'a> From<&'a RotCore> for CoreRef<'a> {
    fn from(value: &'a RotCore) -> Self {
        Self::Rot(value)
    }
}

impl<'a, 'b> From<&'b CoreRef<'a>> for CoreRef<'a> {
    fn from(value: &'b CoreRef<'a>) -> Self {
        value.clone()
    }
}