stem_core 0.2.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
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/*!
This module contains the [`RotCore`] type and its builder struct
[`RotCoreBuilder`]. [`RotCore`] forms the basis for all rotary 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::{f64::consts::TAU, 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, error::IncompatibleFluxBarrier, flux_barrier::FluxBarrier};

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

Seen from its cross section, a radial flux rotary electric motor consists of two
coaxial hollow cylinders (or tubes), where one of them (the rotor) rotates around
the other (the stator). Therefore, the cross section of the stator or rotor core
is effectively also a hollow cylinder described by the inner radius, outer
radius and axial length (which in the cross section view goes into the image
plane).
*/
#[doc = ""]
#[cfg_attr(feature = "doc-images", doc = "![Main dimensions][cad_rot_core_dims]")]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("cad_rot_core_dims", "docs/img/cad_rot_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."
)]
/**

The space between the two cylinders is the air gap. The outer radius of the
inner cylinder / core is called the [`air_gap_radius`](RotCore::air_gap_radius)
and its inner radius is called the [`yoke_radius`](RotCore::yoke_radius). For
the outer cylinder / core, it is the other way around: The outer radius is the
yoke radius, the inner one the air gap radius. Hence, a core is an _inner_ core,
if `core.air_gap_radius() > core.yoke_radius()`, otherwise it is an _outer_ core.

Both inner and outer cores may have geometric features such as a special air gap
contour or cutouts (flux barriers). The following image shows an inner core with
a simple "star" flux barrier and 6 poles and an outer core with slots for a
winding (not depicted).
 */
#[doc = ""]
#[cfg_attr(feature = "doc-images", doc = "![Rotary core][rot_core]")]
#[cfg_attr(
    feature = "doc-images",
    embed_doc_image::embed_doc_image("rot_core", "docs/img/rot_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 `RotCore`

A [`RotCore`] is built from a [`RotCoreBuilder`]. If the field values of the
[`RotCoreBuilder`] 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 [`RotCoreBuilder`] state the allowed value range for each
parameter. Besides the [`RotCore::new`] constructor, [`TryFrom`] / [`TryInto`]
implementations are also available.

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

// Valid parameters (resulting in an outer core, since air_gap_radius < yoke_radius)
let builder = RotCoreBuilder {
    air_gap_radius: Length::new::<millimeter>(55.0),
    yoke_radius: Length::new::<millimeter>(90.0),
    axial_length: Length::new::<millimeter>(165.0),
    axial_coil_overhang: Length::new::<millimeter>(0.0),
    iron_fill_factor: 1.0,
    material: Arc::new(Material::default()),
    pole_pairs: 2.try_into().expect("not zero"),
    skew_angle: 0.0,
    air_gap: Box::new(PlainAirGap::default()),
    flux_barrier: None,
};

let core = RotCore::new(builder).expect("valid inputs");
assert_eq!(core.air_gap_radius().get::<millimeter>(), 55.0);

// Invalid parameters (negative air_gap_radius).
let builder = RotCoreBuilder {
    air_gap_radius: Length::new::<millimeter>(-55.0),
    yoke_radius: Length::new::<millimeter>(90.0),
    axial_length: Length::new::<millimeter>(165.0),
    axial_coil_overhang: Length::new::<millimeter>(0.0),
    iron_fill_factor: 1.0,
    material: Arc::new(Material::default()),
    pole_pairs: 2.try_into().expect("not zero"),
    skew_angle: 0.0,
    air_gap: Box::new(PlainAirGap::default()),
    flux_barrier: None,
};

// try_from is equivalent to new
assert!(RotCore::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`].

```
use approxim;
use stem_core::prelude::*;
use serde_yaml;

let str = indoc::indoc! {"
air_gap_radius: 55 mm
yoke_radius: 90 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: RotCore = serde_yaml::from_str(&str).expect("valid dimensions");
assert_eq!(core.air_gap_radius().get::<millimeter>(), 55.0);
```
 */
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "RotCoreBuilder"))]
#[cfg_attr(feature = "serde", serde(into = "RotCoreBuilder"))]
pub struct RotCore {
    air_gap_radius: Length,
    yoke_radius: Length,
    axial_length: Length,
    axial_coil_overhang: Length,
    iron_fill_factor: f64,
    material: Arc<Material>,
    pole_pairs: NonZeroU16,
    skew_angle: f64,
    air_gap: Box<dyn AirGap>,
    flux_barrier: Option<Box<dyn FluxBarrier>>,
    shape: Shape,
}

impl RotCore {
    /**
    Builds a new [`RotCore`] from a [`RotCoreBuilder`].

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

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

    /// Returns the air gap radius of the core.
    ///
    /// If this value is larger than [`RotCore::yoke_radius`], the core is an
    /// inner core, otherwise it is an outer core. This value is equivalent to
    /// [`RotCoreBuilder::air_gap_radius`] from the builder struct used to
    /// create `self`.
    pub fn air_gap_radius(&self) -> Length {
        return self.air_gap_radius;
    }

    /// Returns the yoke radius of the core.
    ///
    /// If this value is larger than [`RotCore::air_gap_radius`], the core is an
    /// outer core, otherwise it is an inner core. This value is equivalent to
    /// [`RotCoreBuilder::yoke_radius`] from the builder struct used to create
    /// `self`.
    pub fn yoke_radius(&self) -> Length {
        return self.yoke_radius;
    }

    /**
    Returns whether `self` is an inner or outer core.

    This method is implemented as `self.air_gap_radius() < self.yoke_radius()`.
    See the docstring of [`RotCore`] for an explanation of the concept.
     */
    pub fn is_outer(&self) -> bool {
        self.air_gap_radius() < self.yoke_radius()
    }

    /// Returns the radius at the yoke middle.
    ///
    /// If [`RotCore::is_outer`] is true, this is [`RotCore::yoke_radius`] minus
    /// half the [`CoreExt::yoke_height`]. Otherwise, it is the yoke radius plus
    /// half the yoke height.
    #[doc = ""]
    #[cfg_attr(
        feature = "doc-images",
        doc = "![Yoke middle radius][cad_yoke_tooth_height]"
    )]
    #[cfg_attr(
        feature = "doc-images",
        embed_doc_image::embed_doc_image(
            "cad_yoke_tooth_height",
            "docs/img/cad_yoke_tooth_height.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."
    )]
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use stem_core::prelude::*;
    ///
    /// let air_gap = PlainAirGap::default();
    /// let plain_core: RotCore = RotCoreBuilder {
    ///     air_gap_radius: Length::new::<millimeter>(40.0),
    ///     yoke_radius: Length::new::<millimeter>(20.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: 3.try_into().expect("not zero"),
    ///     air_gap: Box::new(air_gap),
    ///     flux_barrier: None,
    /// }.try_into().expect("valid inputs");
    ///
    /// assert_eq!(plain_core.yoke_middle_radius().get::<millimeter>(), 30.0);
    ///
    /// let slot: SemiTrapezoidSlot = SemiTrapezoidWithoutSlopesBuilder {
    ///     bottom_width: Length::new::<millimeter>(4.0),
    ///     opening_width: Length::new::<millimeter>(2.0),
    ///     height: Length::new::<millimeter>(10.0),
    ///     opening_height: Length::new::<millimeter>(1.0),
    ///     slot_angle: 0.0,
    ///     bottom_radius: Length::new::<millimeter>(0.0),
    ///     top_radius: Length::new::<millimeter>(0.0),
    ///     opening_radius: Length::new::<millimeter>(0.0),
    ///     consider_tooth_tip_leakage: true,
    /// }
    /// .try_into()
    /// .unwrap();
    /// let air_gap = SlottedAirGap::new(
    ///     12.try_into().expect("not zero"),
    ///     true,
    ///     CarterFactorModel::Bin12,
    ///     Arc::new(slot),
    /// );
    /// let slotted_core: RotCore = RotCoreBuilder {
    ///     air_gap_radius: Length::new::<millimeter>(40.0),
    ///     yoke_radius: Length::new::<millimeter>(20.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: 3.try_into().expect("not zero"),
    ///     air_gap: Box::new(air_gap),
    ///     flux_barrier: None,
    /// }.try_into().expect("valid inputs");
    ///
    /// assert_eq!(slotted_core.yoke_middle_radius().get::<millimeter>(), 25.0);
    /// ```
    pub fn yoke_middle_radius(&self) -> Length {
        let outer = self.is_outer();
        let sign = outer as i32 as f64 - (!outer) as i32 as f64;
        return self.yoke_radius() - 0.5 * sign * self.yoke_height();
    }

    /// Returns the offset between the coordinate system origin of the core and
    /// that of its slots, if it has any.
    ///
    /// This method returns the `offset` between the coordinate system of a
    /// [`Slot`] and that of `self`:
    ///
    /// `r_core = y_slot + offset`.
    ///
    /// If the slot is closed, `offset` is simply the air gap radius. If the
    /// slot is open, the slot coordinate system is shifted inwards by `delta`
    /// as shown in the image below. This is done because the actual slot
    /// opening height would be larger than [`Slot::opening_height`] because a
    /// rotary core "bends away" from the slot baseline, necessitating the
    /// shift by `delta` to compensate. For an open slot, `offset` is therefore:
    ///
    /// `offset = sqrt(RotCore::air_gap_radius² - (Slot::opening_width/2)²)`
    ///
    /// If the core has no slots, this method returns `0 m`.
    #[doc = ""]
    #[cfg_attr(
        feature = "doc-images",
        doc = "![Offset between the core and the slot coordinate system origin][cad_rot_core_slot_cs_offset]"
    )]
    #[cfg_attr(
        feature = "doc-images",
        embed_doc_image::embed_doc_image(
            "cad_rot_core_slot_cs_offset",
            "docs/img/cad_rot_core_slot_cs_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."
    )]
    ///
    /// # Examples
    ///
    /// ```
    /// use std::sync::Arc;
    /// use approxim::assert_abs_diff_eq;
    /// use stem_core::prelude::*;
    ///
    /// let air_gap = PlainAirGap::default();
    /// let plain_core: RotCore = RotCoreBuilder {
    ///     air_gap_radius: Length::new::<millimeter>(40.0),
    ///     yoke_radius: Length::new::<millimeter>(20.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: 3.try_into().expect("not zero"),
    ///     air_gap: Box::new(air_gap),
    ///     flux_barrier: None,
    /// }.try_into().expect("valid inputs");
    /// assert_eq!(plain_core.origin_offset_core_to_slot().get::<millimeter>(), 0.0);
    ///
    /// let slot: SemiTrapezoidSlot = SemiTrapezoidWithoutSlopesBuilder {
    ///     bottom_width: Length::new::<millimeter>(4.0),
    ///     opening_width: Length::new::<millimeter>(2.0),
    ///     height: Length::new::<millimeter>(8.0),
    ///     opening_height: Length::new::<millimeter>(1.0),
    ///     slot_angle: 0.0,
    ///     bottom_radius: Length::new::<millimeter>(0.0),
    ///     top_radius: Length::new::<millimeter>(0.0),
    ///     opening_radius: Length::new::<millimeter>(0.0),
    ///     consider_tooth_tip_leakage: true,
    /// }
    /// .try_into()
    /// .unwrap();
    /// let air_gap = SlottedAirGap::new(
    ///     12.try_into().expect("not zero"),
    ///     true,
    ///     CarterFactorModel::Bin12,
    ///     Arc::new(slot),
    /// );
    /// let slotted_core: RotCore = RotCoreBuilder {
    ///     air_gap_radius: Length::new::<millimeter>(40.0),
    ///     yoke_radius: Length::new::<millimeter>(20.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: 3.try_into().expect("not zero"),
    ///     air_gap: Box::new(air_gap),
    ///     flux_barrier: None,
    /// }.try_into().expect("valid inputs");
    ///
    /// assert_abs_diff_eq!(slotted_core.origin_offset_core_to_slot().get::<millimeter>(), 39.987, epsilon = 1e-3);
    /// ```
    pub fn origin_offset_core_to_slot(&self) -> Length {
        use uom::typenum::P2;
        if let Some(slot) = self.slot() {
            let r = self.air_gap_radius();
            let s = slot.opening_width();
            return (r.powi(P2::new()) - (0.5 * s).powi(P2::new())).sqrt();
        } else {
            return Length::new::<meter>(0.0);
        }
    }

    /// 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 mut rot_core: RotCore = RotCoreBuilder {
    ///     air_gap_radius: Length::new::<millimeter>(40.0),
    ///     yoke_radius: Length::new::<millimeter>(15.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: 3.try_into().expect("not zero"),
    ///     air_gap: Box::new(PlainAirGap::default()),
    ///     flux_barrier: None, // No flux barrier at initialization
    /// }.try_into().expect("valid inputs");
    /// assert!(rot_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!(rot_core.set_flux_barrier(Some(Box::new(fb_comp))).is_ok());
    /// assert!(rot_core.flux_barrier().is_some());
    ///
    /// // Remove the flux barrier
    /// assert!(rot_core.set_flux_barrier(None).is_ok()); // Cannot fail for None input
    /// assert!(rot_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!(rot_core.set_flux_barrier(Some(Box::new(fb_incomp))).is_err());
    /// assert!(rot_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 `rot_core` since the flux barriers intersect the shape
    /// contour as well as each other due to the limited width of `rot_core`.
    #[doc = ""]
    #[cfg_attr(
        feature = "doc-images",
        doc = "![Comparison compatible and incompatible flux barrier][rot_core_set_fb]"
    )]
    #[cfg_attr(
        feature = "doc-images",
        embed_doc_image::embed_doc_image("rot_core_set_fb", "docs/img/rot_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 crate::prelude::FluxBarrier>>,
    ) -> Result<(), IncompatibleFluxBarrier> {
        let mut air_gap: Box<dyn AirGap> = Box::new(crate::air_gap::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 RotCore {}

impl CoreExt for RotCore {
    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 air_gap_length(&self) -> Length {
        return self.air_gap_radius() * TAU;
    }

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

    fn yoke_height(&self) -> Length {
        return (self.air_gap_radius() - self.yoke_radius()).abs() - self.tooth_height();
    }

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

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

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

    fn num_segments(&self) -> usize {
        return self.air_gap.num_segments(self.into());
    }

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

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

    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 = crate::magnets::covered_angle(
                std::iter::once(&*assembly.magnet().shape()),
                self.air_gap_radius.get::<meter>(),
            );
            return self.poles().get() as f64
                * assembly.num_tangential().get() as f64
                * single_magnet_coverage
                / TAU;
        } 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) => {
                use uom::typenum::P2;

                let m = if self.is_outer() { 1.0 } else { -1.0 };
                let total_slot_area = std::f64::consts::PI
                    * ((self.air_gap_radius() + m * self.tooth_height()).powi(P2::new())
                        - self.air_gap_radius().powi(P2::new()))
                    .abs();
                return VarQuantity::from(
                    (total_slot_area / f64::from(self.slots()) - slot.area()) * self.iron_length(),
                ) * self.material().mass_density();
            }
            None => return Default::default(),
        }
    }

    fn air_gap_curvature_factor(
        &self,
        effective_air_gap: Length,
        spatial_order: stem_slot::stem_types::SpatialOrder,
    ) -> f64 {
        {
            let opposite_air_gap_surface_radius = if self.is_outer() {
                self.air_gap_radius() - effective_air_gap
            } else {
                self.air_gap_radius() + effective_air_gap
            };
            air_gap_curvature_factor(
                self.pole_pairs(),
                self.air_gap_radius(),
                opposite_air_gap_surface_radius,
                spatial_order,
            )
            .abs()
        }
    }
}

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

This struct can be (fallibly) converted into a[`RotCore`] via its [`TryFrom`] /
[`TryInto`] implementation or via [`RotCore::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 [`RotCore`] is equivalent to that of this
struct. When deserializing a [`RotCore`], the serialized representation is first
deserialized into a [`RotCoreBuilder`] which is then converted via [`TryFrom`].

See the docstring of [`RotCore`] for examples.
 */
#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
pub struct RotCoreBuilder {
    /// Air gap radius of the core. Must be positive and not equal to
    /// [`RotCoreBuilder::yoke_radius`] (`0 m <= air_gap_radius !=
    /// yoke_radius`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    pub air_gap_radius: Length,
    /// Yoke radius of the core. Must be positive and not equal to
    /// [`RotCoreBuilder::air_gap_radius`] (`0 m <= yoke_radius !=
    /// air_gap_radius`).
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_quantity"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_quantity"))]
    pub yoke_radius: 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,
    /// Material used for the core.
    #[cfg_attr(feature = "serde", serde(serialize_with = "serialize_arc_link"))]
    #[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_arc_link"))]
    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 [`RotCore`] with [`RotCore::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<RotCore> for RotCoreBuilder {
    fn from(value: RotCore) -> Self {
        RotCoreBuilder {
            air_gap_radius: value.air_gap_radius,
            yoke_radius: value.yoke_radius,
            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<RotCoreBuilder> for RotCore {
    type Error = crate::error::Error;

    fn try_from(builder: RotCoreBuilder) -> Result<Self, Self::Error> {
        let zero = Length::new::<meter>(0.0);
        compare_variables!(val zero <= builder.air_gap_radius)?;
        compare_variables!(val zero <= builder.yoke_radius)?;
        compare_variables!(builder.air_gap_radius != builder.yoke_radius)?;
        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 = RotCore {
            air_gap_radius: builder.air_gap_radius,
            yoke_radius: builder.yoke_radius,
            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);
    }
}

/// Returns the curvature factor of a cylindrical air gap for the spatial order
/// of a field harmonic.
///
/// The curvature factor accounts for the difference between the cylindrical
/// air-gap field and the corresponding field in a flat air gap. It is evaluated
/// at `air_gap_surface_radius`; `opposite_air_gap_surface_radius` is the radius
/// of the opposite air-gap surface.
///
/// The factor is obtained from the analytical solution of Laplace's equation
/// in cylindrical coordinates [\(1\)](#air_gap_curvature_factor_1). For a
/// harmonic of spatial order `v`, the radial dependence of the magnetic scalar
/// potential has the form
///
/// `Phi(r) = A * r^v + B * r^(-v)`.
///
/// Applying the boundary conditions at the two cylindrical air-gap surfaces
/// results in the curvature factor
///
/// `k = g * v / r1 * ((r1 / r2)^(2 * v) + 1) / ((r1 / r2)^(2 * v) - 1)`,
///
/// where `g` is the air-gap width, `r1` is the `air_gap_surface_radius` and
/// `r2` is the `opposite_air_gap_surface_radius`.
///
/// In the limit of a small air gap relative to the radius, the cylindrical
/// air gap approaches a flat air gap and the magnitude of the curvature
/// factor approaches `1`.
///
/// The sign of the returned factor follows the radial direction implied by
/// `r1` and `r2`.
///
/// # Literature
///
/// <a id="air_gap_curvature_factor_1">\(1\)</a>
/// Andresen, J., Ponick, B. and Mertens, A.: Direct Radial and Circumferential
/// Analytical Air-Gap Field Calculation for Electrical Machines,
/// 2019 International Aegean Conference on Electrical Machines and Power
/// Electronics, pp. 191–198, 2019.
pub fn air_gap_curvature_factor(
    pole_pairs: NonZeroU16,
    air_gap_surface_radius: Length,
    opposite_air_gap_surface_radius: Length,
    spatial_order: stem_slot::stem_types::SpatialOrder,
) -> f64 {
    let air_gap_width = (air_gap_surface_radius - opposite_air_gap_surface_radius).abs();
    let v = spatial_order.to_mechanical(pole_pairs);
    let a = f64::from(air_gap_surface_radius / opposite_air_gap_surface_radius).powi(2 * v as i32);
    return f64::from(
        air_gap_width * f64::from(v) / air_gap_surface_radius * (a + 1.0) / (a - 1.0),
    );
}