stem_core 0.4.1

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
/*!
This module contains the [`LinCore`] type and its builder struct
[`LinCoreBuilder`]. [`LinCore`] forms the basis for all linear magnetic cores
used in the stem ecosystem. See its docstring for more.
 */

use compare_variables::compare_variables;
use planar_geo::{prelude::BoundingBox, shape::Shape};
use std::{num::NonZeroU16, sync::Arc};
use stem_magnet::prelude::*;

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

#[cfg(feature = "serde")]
use serde_mosaic::{deserialize_arc_link, serialize_arc_link};

use super::CoreExt;
use crate::{
    LinOrRot,
    air_gap::{AirGap, PlainAirGap},
    error::IncompatibleFluxBarrier,
    flux_barrier::FluxBarrier,
};

/**
A magnetic core for a linear electric motor / machine.

Seen from its cross section, a radial flux linear electric motor consists of
effectively two rectangles (stator and rotor) where the rotor slides against the
stator. Therefore, the cross section of the stator or rotor core is effectively
also a rectangle and its dimensions are described by its width, height, and
axial length (which in the cross section view goes into the image plane).
*/
#[doc = ""]
#[cfg_attr(feature = "doc-images", doc = "![Main dimensions][cad_lin_core_dims]")]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("cad_lin_core_dims", "docs/img/cad_lin_core_dims.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."
)]
/**

Furthermore, the core may have geometric features such as a special air gap
contour or cutouts (flux barriers). The following image shows the cross section
of a slotted core with simple "star" flux barriers. The slots may hold a
winding, the flux barriers may contain magnets (not depicted).
*/
#[doc = ""]
#[cfg_attr(feature = "doc-images", doc = "![Linear core][lin_core]")]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("lin_core", "docs/img/lin_core.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."
)]
/**

# Building a `LinCore`

A [`LinCore`] is built from a [`LinCoreBuilder`]. If the field values of the
[`LinCoreBuilder`] do not result in a valid core (e.g., if negative dimensions
are given), the conversion fails, as shown in the example below. The field
docstrings of [`LinCoreBuilder`] state the allowed value range for each
parameter. Besides the [`LinCore::new`] constructor, [`TryFrom`] / [`TryInto`]
implementations are also available.

```
use std::sync::Arc;
use stem_core::prelude::*;

// Valid parameters
let builder = LinCoreBuilder {
    height: Length::new::<millimeter>(20.0),
    width: Length::new::<millimeter>(100.0),
    axial_length: Length::new::<millimeter>(100.0),
    axial_coil_overhang: Length::new::<millimeter>(0.0),
    skew_angle: 0.0,
    iron_fill_factor: 1.0,
    material: Arc::new(Material::default()),
    pole_pairs: 2.try_into().expect("not zero"),
    air_gap: Box::new(PlainAirGap::default()),
    flux_barrier: None,
};

let core = LinCore::new(builder).expect("valid inputs");
assert_eq!(core.width().get::<millimeter>(), 100.0);

// Invalid parameters (negative core width).
let builder = LinCoreBuilder {
    height: Length::new::<millimeter>(20.0),
    width: Length::new::<millimeter>(-100.0), // Negative width!
    axial_length: Length::new::<millimeter>(100.0),
    axial_coil_overhang: Length::new::<millimeter>(0.0),
    skew_angle: 0.0,
    iron_fill_factor: 1.0,
    material: Arc::new(Material::default()),
    pole_pairs: 2.try_into().expect("not zero"),
    air_gap: Box::new(PlainAirGap::default()),
    flux_barrier: None,
};

// try_from is equivalent to new
assert!(LinCore::try_from(builder).is_err());
```

# Serialization and deserialization

The serialized representation of a [`LinCore`] is equivalent to that of
[`LinCoreBuilder`]. When deserializing a [`LinCore`], the serialized
representation is first deserialized into a [`LinCoreBuilder`] which is then
converted via [`TryFrom`].

```
# #[cfg(feature = "serde")]
# {
use approxim;
use stem_core::prelude::*;
use serde_yaml;

let str = indoc::indoc! {"
height: 20 mm
width: 100 mm
axial_length: 100 mm
axial_coil_overhang: 0 mm
skew_angle: 0
iron_fill_factor: 1
material:
    name: lamination
    relative_permeability: 6000
pole_pairs: 2
air_gap:
    PlainAirGap:
        air_gap_winding_height: 0 mm
        winding_coverage: 0
        num_segments: 1
        starts_in_slot_middle: true
        slots: 0
"};

let core: LinCore = serde_yaml::from_str(&str).expect("valid dimensions");
assert_eq!(core.width().get::<millimeter>(), 100.0);
# }
```
 */
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "LinCoreBuilder"))]
#[cfg_attr(feature = "serde", serde(into = "LinCoreBuilder"))]
pub struct LinCore {
    height: Length,
    width: Length,
    axial_length: Length,
    axial_coil_overhang: Length,
    skew_angle: f64,
    iron_fill_factor: f64,
    material: Arc<Material>,
    pole_pairs: NonZeroU16,
    air_gap: Box<dyn AirGap>,
    flux_barrier: Option<Box<dyn FluxBarrier>>,
    shape: Shape,
}

impl LinCore {
    /**
    Builds a new [`LinCore`] from a [`LinCoreBuilder`].

    Building a [`LinCore`] can fail if the provided data is invalid (e.g.,
    negative dimensions). See the field documentation of [`LinCoreBuilder`] for
    details. In such a case, the resulting error is returned instead.

    This method forwards to the `TryInto<LinCore>` implementation of
    [`LinCoreBuilder`].
     */
    pub fn new(builder: LinCoreBuilder) -> Result<Self, crate::error::Error> {
        builder.try_into()
    }

    /// Returns the height (vertical extents) of the core.
    ///
    /// This value is equivalent to [`LinCoreBuilder::height`] from the builder
    /// struct used to create `self`.
    pub fn height(&self) -> Length {
        return self.height;
    }

    /// Returns the width (horizontal extents) of the core.
    ///
    /// This value is equivalent to [`LinCoreBuilder::width`] from the builder
    /// struct used to create `self`.
    pub fn width(&self) -> Length {
        return self.width;
    }

    /// 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.
    ///
    /// # Examples
    ///
    /// The following code shows how adding a compatible flux barrier succeeds,
    /// how it can be removed again and how adding an incompatible flux barrier
    /// fails.
    ///
    /// ```
    /// use std::sync::Arc;
    /// use stem_core::prelude::*;
    ///
    /// let air_gap = PlainAirGap::default();
    /// let mut lin_core: LinCore = LinCoreBuilder {
    ///     height: Length::new::<millimeter>(20.0),
    ///     width: Length::new::<millimeter>(100.0),
    ///     axial_length: Length::new::<millimeter>(100.0),
    ///     axial_coil_overhang: Length::new::<millimeter>(0.0),
    ///     skew_angle: 0.0,
    ///     iron_fill_factor: 1.0,
    ///     material: Arc::new(Material::default()),
    ///     pole_pairs: 2.try_into().expect("not zero"),
    ///     air_gap: Box::new(air_gap),
    ///     flux_barrier: None, // No flux barrier at initialization
    /// }.try_into().expect("valid inputs");
    /// assert!(lin_core.flux_barrier().is_none());
    ///
    /// // A compatible flux barrier
    /// let fb_comp = Spoke1FluxBarrier {
    ///     air_gap_leakage_path_width: Length::new::<millimeter>(1.0),
    ///     yoke_leakage_path_width: Length::new::<millimeter>(1.0),
    ///     relief_path_air_gap_width: Length::new::<millimeter>(4.0),
    ///     magnet_space_width: Length::new::<millimeter>(10.0),
    ///    height_split: Spoke1HeightSplit::ReliefPathWidth(Length::new::<millimeter>(2.0)),
    ///     glue_gap: Length::new::<millimeter>(0.0),
    ///     magnet_material: None,
    ///     cache: None,
    /// };
    /// assert!(lin_core.set_flux_barrier(Some(Box::new(fb_comp))).is_ok());
    /// assert!(lin_core.flux_barrier().is_some());
    ///
    /// // Remove the flux barrier
    /// assert!(lin_core.set_flux_barrier(None).is_ok()); // Cannot fail for None input
    /// assert!(lin_core.flux_barrier().is_none());
    ///
    /// // An incompatible flux barrier
    /// let mut fb_incomp = Spoke1FluxBarrier {
    ///     air_gap_leakage_path_width: Length::new::<millimeter>(1.0),
    ///     yoke_leakage_path_width: Length::new::<millimeter>(1.0),
    ///     relief_path_air_gap_width: Length::new::<millimeter>(4.0),
    ///     magnet_space_width: Length::new::<millimeter>(30.0), // Too wide for the core width
    ///     height_split: Spoke1HeightSplit::ReliefPathWidth(Length::new::<
    ///         millimeter,
    ///     >(2.0)),
    ///     glue_gap: Length::new::<millimeter>(0.0),
    ///     magnet_material: None,
    ///     cache: None,
    /// };
    /// assert!(lin_core.set_flux_barrier(Some(Box::new(fb_incomp))).is_err());
    /// assert!(lin_core.flux_barrier().is_none());
    /// ```
    ///
    /// The image below shows a comparison between the flux barrier contours of
    /// `fb_comp` and `fb_incomp`. It is clear to see that the latter is
    /// incompatible to `lin_core` since the flux barriers intersect the shape
    /// contour as well as each other due to the limited width of `lin_core`.
    #[doc = ""]
    #[cfg_attr(
        feature = "doc-images",
        doc = "![Comparison compatible and incompatible flux barrier][lin_core_set_fb]"
    )]
    #[cfg_attr(
        feature = "doc-images",
        embed_doc_image::embed_doc_image("lin_core_set_fb", "docs/img/lin_core_set_fb.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 set_flux_barrier(
        &mut self,
        flux_barrier: Option<Box<dyn FluxBarrier>>,
    ) -> Result<(), IncompatibleFluxBarrier> {
        let mut air_gap: Box<dyn AirGap> = Box::new(PlainAirGap::default());
        std::mem::swap(&mut air_gap, &mut self.air_gap);
        let mut shape = air_gap.combine(self.as_core_ref()).expect(
            "air gap - core combination produced a valid shape during construction of self. This is a bug.",
        );
        std::mem::swap(&mut air_gap, &mut self.air_gap);

        if let Some(mut fb) = flux_barrier {
            let contours = match fb.combine(self.as_core_ref()) {
                Ok(c) => c,
                Err(cause) => {
                    return Err(IncompatibleFluxBarrier {
                        flux_barrier: fb,
                        cause,
                    });
                }
            };

            for hole in contours {
                if let Err(cause) = shape.add_hole(hole) {
                    return Err(IncompatibleFluxBarrier {
                        flux_barrier: fb,
                        cause: cause.into(),
                    });
                }
            }
            self.flux_barrier = Some(fb);
        } else {
            self.flux_barrier = None;
        }

        self.shape = shape;
        return Ok(());
    }
}

impl super::ext::private::Sealed for LinCore {}

impl CoreExt for LinCore {
    fn air_gap(&self) -> &dyn AirGap {
        return &*self.air_gap;
    }

    fn flux_barrier(&self) -> Option<&dyn FluxBarrier> {
        return self.flux_barrier.as_ref().map(|v| &**v);
    }
    fn axial_length(&self) -> Length {
        return self.axial_length;
    }

    fn air_gap_length(&self) -> Length {
        return self.width;
    }

    fn iron_fill_factor(&self) -> f64 {
        return self.iron_fill_factor;
    }

    fn pole_pairs(&self) -> NonZeroU16 {
        return self.pole_pairs;
    }

    fn material(&self) -> &Arc<Material> {
        return &self.material;
    }

    fn yoke_height(&self) -> Length {
        return self.height - self.air_gap.tooth_height(self.into());
    }

    fn skew_angle(&self) -> f64 {
        return self.skew_angle;
    }

    fn lin_or_rot(&self) -> LinOrRot {
        return LinOrRot::Lin;
    }

    fn axial_coil_overhang(&self) -> Length {
        return self.axial_coil_overhang;
    }

    fn shape<'a>(&'a self) -> &'a Shape {
        return &self.shape;
    }

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

    fn pole_coverage(&self, surface_magnet_assembly: Option<&MagnetAssembly>) -> f64 {
        if let Some(assembly) = surface_magnet_assembly {
            let single_magnet_coverage = BoundingBox::from(&*assembly.magnet().shape()).width();
            return self.poles().get() as f64
                * assembly.num_tangential().get() as f64
                * single_magnet_coverage
                / self.width().get::<meter>();
        } else {
            if let Some(flux_barrier) = &self.flux_barrier {
                return flux_barrier
                    .pole_coverage(self.as_core_ref())
                    .clamp(0.0, 1.0);
            } else {
                return 0.5;
            }
        }
    }

    fn tooth_mass(&self) -> VarQuantity<Mass> {
        match self.slot() {
            Some(slot) => {
                let total_slot_area = self.width() * (self.height() - self.yoke_height());
                return VarQuantity::from(
                    (total_slot_area / f64::from(self.slots()) - slot.area()) * self.iron_length(),
                ) * self.material().mass_density();
            }
            None => return Mass::new::<kilogram>(0.0).into(),
        }
    }

    fn air_gap_curvature_factor(
        &self,
        _effective_air_gap: Length,
        _spatial_order: stem_slot::stem_types::SpatialOrder,
    ) -> f64 {
        1.0
    }
}

/**
Builder struct for [`LinCore`].

This struct can be (fallibly) converted into a[`LinCore`] via its [`TryFrom`] /
[`TryInto`] implementation or via [`LinCore::new`]. The conversion fails if one
of the field values is not inside the value range given on the individual field
docstrings.

The serialized representation of a [`LinCore`] is equivalent to that of this
struct. When deserializing a [`LinCore`], the serialized representation is first
deserialized into a [`LinCoreBuilder`] which is then converted via [`TryFrom`].

See the docstring of [`LinCore`] for examples.
 */
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct LinCoreBuilder {
    /// Height (vertical extents) of the core. Must be positive (`height >= 0
    /// m`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    pub height: Length,
    /// Width (horizontal extents) of the core. Must be positive (`width >= 0
    /// m`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    pub width: Length,
    /// Axial length of the core. This dimension is invisible when using the
    /// typical cross-section view of a core because it goes into the image
    /// plane. Must be positive (`axial_length >= 0 m`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    pub axial_length: Length,
    /// If the core holds a winding, this specifies the axial overhang of both
    /// sides. See [`CoreExt::axial_coil_overhang`] for details. Must be
    /// positive (`axial_coil_overhang >= 0 m`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(default))]
    pub axial_coil_overhang: Length,
    /// Skew angle of the core. See [`CoreExt::skew_angle`] for details.
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_angle"))]
    pub skew_angle: f64,
    /// Magnetic cores are often build from stacked sheets of ferromagnetic
    /// lamination, which are connected by glue. The gap between the sheets
    /// reduces the effective magnetic conductivity, see
    /// [`CoreExt::iron_length`]. This effect can be modeled by setting this
    /// factor somewhere between 0 and 1 (`0 <= iron_fill_factor <= 1`). Typical
    /// values are usually between 0.9 and 1.
    pub iron_fill_factor: f64,
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arc_link"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_arc_link"))]
    /// Material used for the core.
    pub material: Arc<Material>,
    /// Number of pole pairs of the core.
    pub pole_pairs: NonZeroU16,
    /// Definition of the air gap contour. See the docstring of [`AirGap`] for
    /// details.
    pub air_gap: Box<dyn AirGap>,
    /// Definition of the flux barrier geometry, if the core has any. See the
    /// docstring of [`FluxBarrier`] for more. Setting this field to `None`
    /// means that the core has no flux barriers. This field can also be set
    /// after the creation of a [`LinCore`] with [`LinCore::set_flux_barrier`].
    /// This field can be omitted when deserializing, in which case it is set to
    /// `None`.
    #[cfg_attr(feature = "serde", serde(default))]
    pub flux_barrier: Option<Box<dyn FluxBarrier>>,
}

impl From<LinCore> for LinCoreBuilder {
    fn from(value: LinCore) -> Self {
        LinCoreBuilder {
            height: value.height,
            width: value.width,
            axial_length: value.axial_length,
            axial_coil_overhang: value.axial_coil_overhang,
            skew_angle: value.skew_angle,
            iron_fill_factor: value.iron_fill_factor,
            material: value.material,
            pole_pairs: value.pole_pairs,
            air_gap: value.air_gap,
            flux_barrier: value.flux_barrier,
        }
    }
}

impl TryFrom<LinCoreBuilder> for LinCore {
    type Error = crate::error::Error;

    fn try_from(builder: LinCoreBuilder) -> Result<Self, Self::Error> {
        let zero = Length::new::<meter>(0.0);
        compare_variables!(val zero <= builder.height)?;
        compare_variables!(val zero <= builder.width)?;
        compare_variables!(val zero <= builder.axial_length)?;
        compare_variables!(val zero <= builder.axial_coil_overhang)?;
        compare_variables!(0.0 <= builder.iron_fill_factor <= 1.0)?;

        let mut this = LinCore {
            height: builder.height,
            width: builder.width,
            axial_length: builder.axial_length,
            iron_fill_factor: builder.iron_fill_factor,
            material: builder.material,
            pole_pairs: builder.pole_pairs,
            skew_angle: builder.skew_angle,
            axial_coil_overhang: builder.axial_coil_overhang,
            air_gap: builder.air_gap.clone(),
            flux_barrier: builder.flux_barrier, // Inserted so AirGap::combine has access to it
            // Placeholder
            shape: Shape::from_outer(BoundingBox::new(0.0, 1.0, 0.0, 1.0).into())?,
        };

        let mut ag = builder.air_gap;
        this.shape = AirGap::combine(&mut *ag, this.as_core_ref())?;
        this.air_gap = ag;

        // Move the flux barrier temporarily out again so FluxBarrier::combine can have
        // a mutable reference to it
        let mut flux_barrier_opt = None;
        std::mem::swap(&mut flux_barrier_opt, &mut this.flux_barrier);

        // Check if core and flux barrier are compatible
        if let Some(mut fb) = flux_barrier_opt {
            let contours = FluxBarrier::combine(&mut *fb, this.as_core_ref())?;
            for contour in contours {
                this.shape.add_hole(contour)?;
            }
            this.flux_barrier = Some(fb);
        }

        return Ok(this);
    }
}