# Star-of-Slots Winding Layout


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Executive Summary

This notebook implements the **star-of-slots (SOS) method** — the
canonical winding layout engine for AC machines.

### What it does

Given a slot/pole/phase combination (Q, P, m), the SOS method: 1.
Projects each slot’s EMF phasor onto a circle divided into 2m sectors.
2. Assigns each slot conductor to a phase and current direction based on
which sector its phasor falls in. 3. Builds the coil matrix used by
winding factor and MMF analysis.

### When to use this module

- **Winding layout**: understand which slots carry which phase in which
  direction.
- **Feasibility check**: verify that a given (Q, P, m) combination
  supports a valid balanced winding.
- **Input to winding factors and MMF**: `build_coil_matrix` is the
  single output consumed by `winding_factors.py` and `mmf.py`.

> **Swappability:** this module is deliberately isolated as the layout
> engine. Future layout techniques (e.g. optimal assignment, genetic
> algorithm) can replace it without changing `winding_factors.py` or
> `mmf.py`.

## How It Fits Into Motor Design

    01_winding_sos.ipynb   ←── YOU ARE HERE
    (winding layout)
          │
          ▼
    01_winding_factors.ipynb   (kp, kd, kw, harmonic spectrum)
          │
          ▼
    01_winding_mmf.ipynb       (MMF waveform, harmonic amplitudes)
          │
          ▼
    02_pmsm.ipynb              (torque, back-EMF)

**Depends on:** nothing (this is the foundation layer).

**Feeds into:** `01_winding_factors.ipynb`, `01_winding_mmf.ipynb`.

## What You Provide (Inputs)

<table>
<thead>
<tr>
<th>Parameter</th>
<th>Symbol</th>
<th>Description</th>
<th>Typical Range</th>
</tr>
</thead>
<tbody>
<tr>
<td>Stator slots</td>
<td>Q</td>
<td>Total slot count</td>
<td>6 – 72</td>
</tr>
<tr>
<td>Poles</td>
<td>P</td>
<td>Number of poles (even)</td>
<td>2 – 20</td>
</tr>
<tr>
<td>Phases</td>
<td>m</td>
<td>Phase count</td>
<td>3</td>
</tr>
<tr>
<td>Layers</td>
<td>layers</td>
<td>1 = single-layer, 2 = double-layer</td>
<td>1 or 2</td>
</tr>
<tr>
<td>Coil span</td>
<td>w</td>
<td>Coil span in slots</td>
<td>1 – Q//p</td>
</tr>
</tbody>
</table>

## What You Get (Outputs)

<table>
<thead>
<tr>
<th>Result</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>build_star_of_slots</code></td>
<td>Phasor angles for each slot</td>
</tr>
<tr>
<td><code>assign_phases</code></td>
<td>Phase/direction assignment per slot</td>
</tr>
<tr>
<td><code>build_coil_matrix</code></td>
<td>Integer occupancy matrix (layers × Q)</td>
</tr>
<tr>
<td><code>winding_factor_sos</code></td>
<td></td>
</tr>
<tr>
<td><code>get_basic_params</code></td>
<td>Derived parameters (p, q, t, …)</td>
</tr>
<tr>
<td><code>check_winding</code></td>
<td>Is a coil matrix buildable and balanced?</td>
</tr>
<tr>
<td><code>check_symmetry</code></td>
<td>Validate symmetry/balance</td>
</tr>
<tr>
<td><code>get_valid_coil_spans</code></td>
<td>All mechanically valid coil spans</td>
</tr>
<tr>
<td><code>is_valid_combination</code></td>
<td>Boolean feasibility check</td>
</tr>
</tbody>
</table>

## Abbreviations and Notation

<table>
<thead>
<tr>
<th>Symbol</th>
<th>Definition</th>
<th>Units</th>
</tr>
</thead>
<tbody>
<tr>
<td>Q</td>
<td>Number of stator slots</td>
<td>—</td>
</tr>
<tr>
<td>P</td>
<td>Number of poles</td>
<td>—</td>
</tr>
<tr>
<td>p</td>
<td>Pole pairs, p = P/2</td>
<td>—</td>
</tr>
<tr>
<td>m</td>
<td>Number of phases</td>
<td>—</td>
</tr>
<tr>
<td>t</td>
<td>Winding periodicity, t = gcd(Q, p)</td>
<td>—</td>
</tr>
<tr>
<td>q</td>
<td>Slots per pole per phase, q = Q/(2mp)</td>
<td>—</td>
</tr>
<tr>
<td>τp</td>
<td>Pole pitch in slots, τp = Q/P</td>
<td>slots</td>
</tr>
<tr>
<td>w</td>
<td>Coil span in slots</td>
<td>slots</td>
</tr>
<tr>
<td>α_e</td>
<td>Electrical slot pitch angle, α_e = p·(2π/Q)</td>
<td>rad</td>
</tr>
<tr>
<td>ν</td>
<td>Mechanical spatial harmonic order</td>
<td>—</td>
</tr>
</tbody>
</table>

## Imports and Setup

------------------------------------------------------------------------

## Private Helpers

These internal functions support the SOS algorithm and are not part of
the public API.

------------------------------------------------------------------------

## `get_basic_params`: Winding Parameter Summary

### Theory

All downstream calculations start from a handful of derived parameters.
The winding periodicity *t* = gcd(Q, p) is the key symmetry parameter: a
valid winding repeats itself *t* times around the stator.

The slots-per-pole-per-phase *q* = Q/(2mp) classifies the winding: - q ≥
1 (integer): **distributed integer-slot winding** — low harmonic
content. - q \< 1 (fractional): **fractional-slot concentrated winding
(FSCW)** — compact end-turns, higher harmonic content.

**References:** Pyrhönen et al. (2008), §2.1.

------------------------------------------------------------------------

### get_basic_params

``` python
def get_basic_params(
    Q:int, P:int, m:int=3
)->dict: # p       : int   Pole pairs (P // 2)
t       : int   Winding periodicity = gcd(Q, p)
q       : Fraction   Slots per pole per phase = Q / (2mp)
lcm_QP  : int   lcm(Q, P)
alpha_e : float Electrical slot pitch (radians) = p · 2π/Q
Q_phase : int   Slots per phase = Q // m  (integer part)
```

*Compute basic winding parameters from slot/pole/phase count.*

## Parameters

Q : int Number of stator slots P : int Number of poles (even) m : int
Number of phases (default 3)

## Returns

dict with keys: p : int Pole pairs (P // 2) t : int Winding periodicity
= gcd(Q, p) q : Fraction Slots per pole per phase = Q / (2mp) lcm_QP :
int lcm(Q, P) alpha_e : float Electrical slot pitch (radians) = p · 2π/Q
Q_phase : int Slots per phase = Q // m (integer part)

``` python
import math
from fractions import Fraction
import numpy as np
from emachines.winding.sos import (get_basic_params, build_star_of_slots, assign_phases, check_winding,
    build_coil_matrix, winding_factor_sos, check_symmetry, get_valid_coil_spans, is_valid_combination)

# Examples
p = get_basic_params(12, 10)
assert (p['p'], p['t']) == (5, 1)
assert get_basic_params(24, 4)['q'] == Fraction(2, 1)
```

``` python
from emachines.winding.sos import get_basic_params

params = get_basic_params(12, 10)
print("12s/10p (FSCW):")
for k, v in params.items():
    print(f"  {k:10s} = {v}")

print()
params = get_basic_params(24, 4)
print("24s/4p (integer-slot):")
for k, v in params.items():
    print(f"  {k:10s} = {v}")
```

    12s/10p (FSCW):
      p          = 5
      t          = 1
      q          = 2/5
      lcm_QP     = 60
      alpha_e    = 2.6179938779914944
      Q_phase    = 4

    24s/4p (integer-slot):
      p          = 2
      t          = 2
      q          = 2
      lcm_QP     = 24
      alpha_e    = 0.5235987755982988
      Q_phase    = 8

------------------------------------------------------------------------

## `build_star_of_slots`: EMF Phasor Angles

### Theory

The **star of slots** (also called the voltage star or EMF star) maps
each stator slot to a phasor angle in the electrical domain.

For slot *i*, the electrical phasor angle is:

$$
\alpha_i = i \cdot \alpha_e \mod 2\pi, \qquad \alpha_e = p \cdot \frac{2\pi}{Q}
$$

where *p* = P/2 is the number of pole pairs and *α_e* is the electrical
slot pitch.

Plotting all Q phasors reveals the “star” pattern that names the method.
Slots whose phasors point in similar directions naturally carry the same
phase current.

**Reference:** Bianchi & Bolognani (2002), IEEE Trans. Ind. Appl.,
38(5).

------------------------------------------------------------------------

### build_star_of_slots

``` python
def build_star_of_slots(
    Q:int, P:int
)->np.ndarray: # Phasor angles α_i = i · p · 2π/Q mod 2π
```

*Compute EMF phasor angles for all Q slots (electrical radians, mod
2π).*

## Parameters

Q : int Number of stator slots P : int Number of poles

## Returns

np.ndarray, shape (Q,), dtype float64 Phasor angles α_i = i · p · 2π/Q
mod 2π

``` python
# Examples
angles = build_star_of_slots(6, 2)
assert np.allclose(angles, np.arange(6) * np.pi / 3)    # 0°, 60°, … 300°
```

``` python
from emachines.winding.sos import build_star_of_slots
import numpy as np

angles = build_star_of_slots(12, 10)
print("12s/10p phasor angles (degrees):")
for i, a in enumerate(angles):
    print(f"  Slot {i:2d}: {np.degrees(a):6.1f}°")
```

    12s/10p phasor angles (degrees):
      Slot  0:    0.0°
      Slot  1:  150.0°
      Slot  2:  300.0°
      Slot  3:   90.0°
      Slot  4:  240.0°
      Slot  5:   30.0°
      Slot  6:  180.0°
      Slot  7:  330.0°
      Slot  8:  120.0°
      Slot  9:  270.0°
      Slot 10:   60.0°
      Slot 11:  210.0°

------------------------------------------------------------------------

## `assign_phases`: Phase Assignment via Sector Map

### Theory

The 2π electrical circle is divided into **2m equal sectors** (width =
π/m each). Phase j (0-based) has its go belt at j·2π/m and its return
belt π further on, so the sectors interleave go (+) and return (−) belts
of different phases. For three phases the six 60° sectors read:

<table>
<thead>
<tr>
<th>Sector</th>
<th>0–60°</th>
<th>60–120°</th>
<th>120–180°</th>
<th>180–240°</th>
<th>240–300°</th>
<th>300–360°</th>
</tr>
</thead>
<tbody>
<tr>
<td>Phase</td>
<td>+A</td>
<td>−C</td>
<td>+B</td>
<td>−A</td>
<td>+C</td>
<td>−B</td>
</tr>
</tbody>
</table>

This places the go belts of A, B and C 120° apart, as a balanced
three-phase winding requires. The rule needs an odd phase count; even m
raises `ValueError`.

Each slot’s phasor (from `build_star_of_slots`) is projected onto this
sector map: - The slot is assigned to the phase whose sector contains
the phasor. - Positive sector → forward conductor (+phase index). -
Negative sector → return conductor (−phase index).

For FSCW, multiple slots in the same phase produce phasors close
together, forming tight “clusters” in the star diagram.

**Reference:** Pyrhönen et al. (2008), §2.3; Bianchi & Bolognani (2002).

------------------------------------------------------------------------

### assign_phases

``` python
def assign_phases(
    Q:int, P:int, m:int=3
)->np.ndarray: # Signed phase codes: +k = phase k forward, −k = phase k return.
Phase index k ∈ {1, …, m}.
```

*Assign each slot to a phase and current direction using the
star-of-slots.*

## Parameters

Q : int Number of stator slots P : int Number of poles m : int Number of
phases (default 3)

## Returns

np.ndarray, shape (Q,), dtype int Signed phase codes: +k = phase k
forward, −k = phase k return. Phase index k ∈ {1, …, m}.

## Raises

ValueError If m is even.

``` python
# Examples
assert assign_phases(6, 2, m=3).tolist() == [1, -3, 2, -1, 3, -2]    # A, -C, B, -A, C, -B
```

``` python
from emachines.winding.sos import assign_phases

codes = assign_phases(12, 10)
print("12s/10p phase assignment:")
names = {1: 'A+', 2: 'B+', 3: 'C+', -1: 'A-', -2: 'B-', -3: 'C-'}
for i, c in enumerate(codes):
    print(f"  Slot {i:2d}: {names[c]}")
```

    12s/10p phase assignment:
      Slot  0: A+
      Slot  1: B+
      Slot  2: B-
      Slot  3: C-
      Slot  4: C+
      Slot  5: A+
      Slot  6: A-
      Slot  7: B-
      Slot  8: B+
      Slot  9: C+
      Slot 10: C-
      Slot 11: A-

------------------------------------------------------------------------

## `check_winding`: Can It Be Wound, and Is It Balanced?

A coil matrix describes a usable m-phase winding only if two conditions
hold:

1.  **Buildable.** Every coil has a go side and a return side, so each
    phase needs as many go (+) as return (−) conductors. Otherwise its
    conductors cannot be paired into coils. Odd Q, for example, can
    never be wound single-layer.
2.  **Balanced.** At the working harmonic ν = p, the phase EMF phasors
    *E*<sub>*k*</sub> = ∑<sub>*i*</sub>*s*<sub>*i*</sub> *e*<sup>*j**p**θ*<sub>*i*</sub></sup>
    (θᵢ = 2πi/Q) have equal magnitude and step by exactly 360°/m.

Counting conductors per phase is not enough: the old 60°-apart layouts
had equal counts but were not balanced.

The fundamental winding factor is
*k*<sub>*w*1</sub> = |*E*<sub>*A*</sub>|/*N*<sub>*A*</sub>, with
*N*<sub>*A*</sub> the conductors in phase A.

------------------------------------------------------------------------

### check_winding

``` python
def check_winding(
    matrix:np.ndarray, # Signed phase codes, as returned by ``build_coil_matrix``.
    P:int, m:int=3
)->dict: # buildable    : bool   Each phase has as many go as return conductors
balanced     : bool   Phase EMFs at ν = p equal in magnitude, 360°/m apart
kw1          : float  Fundamental winding factor |E_A| / N_A
go, ret      : list   Go and return conductor counts per phase
phase_angles : list | None
                      Electrical angle of each phase EMF relative to
                      phase A, in degrees (None if the EMF is zero)
reason       : str    Why the winding fails, or '' if it passes
```

*Check that a coil matrix can be wound into coils and is balanced.*

## Parameters

matrix : np.ndarray, shape (layers, Q) Signed phase codes, as returned
by `build_coil_matrix`. P : int Number of poles m : int Number of phases
(default 3)

## Returns

dict with keys: buildable : bool Each phase has as many go as return
conductors balanced : bool Phase EMFs at ν = p equal in magnitude,
360°/m apart kw1 : float Fundamental winding factor |E_A| / N_A go, ret
: list Go and return conductor counts per phase phase_angles : list |
None Electrical angle of each phase EMF relative to phase A, in degrees
(None if the EMF is zero) reason : str Why the winding fails, or ’’ if
it passes

``` python
# Examples
assert check_winding(build_coil_matrix(12, 10, layers=2, w=1), 10)['balanced']
assert check_winding(build_coil_matrix(9, 8, layers=1), 8)['reason'] == (
    "phase A has 2 go and 1 return conductors, so it can't be wound into coils")
```

``` python
from emachines.winding.sos import check_winding, build_coil_matrix

for Q, P, layers, w in [(12, 10, 2, 1), (24, 4, 1, None), (9, 8, 1, None), (9, 8, 2, 1), (24, 4, 2, 12)]:
    r = check_winding(build_coil_matrix(Q, P, layers=layers, w=w), P)
    label = f"{Q}s/{P}p {'single layer' if layers == 1 else f'double layer, w={w}'}"
    status = "✓" if r['buildable'] and r['balanced'] else "✗"
    print(f"  {status} {label:<28} kw1={r['kw1']:.4f}  phases at {r['phase_angles']}  {r['reason']}")
```

      ✓ 12s/10p double layer, w=1    kw1=0.9330  phases at [0.0, 120.0, 240.0]  
      ✓ 24s/4p single layer          kw1=0.9659  phases at [0.0, 120.0, 240.0]  
      ✗ 9s/8p single layer           kw1=0.9598  phases at [0.0, 120.0, 240.0]  phase A has 2 go and 1 return conductors, so it can't be wound into coils
      ✓ 9s/8p double layer, w=1      kw1=0.9452  phases at [0.0, 120.0, 240.0]  
      ✗ 24s/4p double layer, w=12    kw1=0.0000  phases at None  phase EMF at the working harmonic is zero

------------------------------------------------------------------------

## Winding candidates and the default layout (internal)

For a slot/pole/phase combination the star of slots can produce a
single-layer winding and a double-layer winding for each coil span.
`_winding_options` builds each one, keeps those that pass
`check_winding`, and sorts them by fundamental winding factor kw₁,
highest first. Ties go to double layer, then to the shorter span (less
end-winding copper).

When `w` is not given, spans from 1 up to the pole pitch Q/P, rounded
up, are tried; longer spans only repeat shorter ones mirrored.

`_resolve_layout` is how every function in the winding module fills in a
missing `layers` or `w`: it takes the first candidate. Calls that give
both `layers` and `w`, or `layers=1`, are used exactly as given.
`design_windings` (in the design notebook) returns the full candidate
list.

------------------------------------------------------------------------

## `build_coil_matrix`: Conductor Occupancy Matrix

### Theory

A coil connects a **go** conductor in one slot to a **return** conductor
`w` slots away (coil span *w*). For a single-layer winding, only go
conductors are placed; the return side falls in a dedicated slot. For a
double-layer winding, every slot has two conductor layers — a go side
from one coil and a return from another.

The coil matrix shape is `(layers, Q)`: - Value `+k` : phase k, forward
current direction. - Value `−k` : phase k, return current direction. -
Value `0` : empty slot (only possible in single-layer windings).

This matrix is the single interface consumed by `winding_factors.py` and
`mmf.py`.

**References:** Hanselman (2003), Ch. 4; Pyrhönen et al. (2008), §2.3.

------------------------------------------------------------------------

### build_coil_matrix

``` python
def build_coil_matrix(
    Q:int, P:int, m:int=3, layers:int | None=None, # buildable, balanced candidate (see ``design_windings``).
    w:int | None=None, # Ignored for ``layers=1``.
)->np.ndarray: # +k = phase k forward, −k = phase k return, 0 = empty.
```

*Build the (layers × Q) conductor occupancy matrix.*

## Parameters

Q : int Number of stator slots P : int Number of poles m : int Number of
phases (default 3) layers : int | None 1 (single-layer) or 2
(double-layer). None → the best buildable, balanced candidate (see
`design_windings`). w : int | None Double-layer coil span in slots. None
→ the best span. Ignored for `layers=1`.

## Returns

np.ndarray, shape (layers, Q), dtype int +k = phase k forward, −k =
phase k return, 0 = empty.

``` python
# Examples
assert build_coil_matrix(6, 2, m=3, layers=1).tolist() == [[1, -3, 2, -1, 3, -2]]
assert build_coil_matrix(6, 2, m=3, layers=2, w=3).shape == (2, 6)
```

``` python
from emachines.winding.sos import build_coil_matrix
import numpy as np

print("6s/2p single-layer:")
m1 = build_coil_matrix(6, 2, m=3, layers=1)
print(m1)

print()
print("12s/10p double-layer:")
m2 = build_coil_matrix(12, 10, m=3, layers=2)
print("Top layer:   ", m2[0])
print("Bottom layer:", m2[1])

print()
print("Interpretation: +1=A forward, -1=A return, +2=B forward, -2=B return, etc.")
```

    6s/2p single-layer:
    [[ 1 -3  2 -1  3 -2]]

    12s/10p double-layer:
    Top layer:    [ 1  2 -2 -3  3  1 -1 -2  2  3 -3 -1]
    Bottom layer: [ 1 -1 -2  2  3 -3 -1  1  2 -2 -3  3]

    Interpretation: +1=A forward, -1=A return, +2=B forward, -2=B return, etc.

------------------------------------------------------------------------

## `winding_factor_sos`: Winding Factor via Phasor Sum

### Theory

The winding factor for mechanical harmonic ν is the magnitude of the
normalised phasor sum of all conductors of one phase:

$$
k\_{w\nu} = \frac{1}{N_k} \left| \sum\_{i,\text{lyr}} s\_{i,\text{lyr}} \cdot e^{j\nu \frac{2\pi i}{Q}} \right|
$$

where *s*<sub>*i*, lyr</sub> ∈ {+1, −1} is the conductor direction, and
*N*<sub>*k*</sub> is the total conductor count per phase. The result is
averaged over all m phases.

This phasor method works for **all winding types** — integer-slot,
fractional-slot, and FSCW — without needing separate kp and kd formulas.

**References:** Müller, Vogt & Ponick (2008), eq. (3.65); Bianchi &
Bolognani (2002).

------------------------------------------------------------------------

### winding_factor_sos

``` python
def winding_factor_sos(
    nu:int, Q:int, P:int, m:int=3, layers:int | None=None, w:int | None=None
)->float:
```

*Winding factor |kw(ν)| via the star-of-slots phasor method.*

Works for integer-slot, fractional-slot, and FSCW windings.

## Parameters

nu : int Mechanical harmonic order Q : int Number of stator slots P :
int Number of poles m : int Number of phases (default 3) layers : int 1
or 2 (default 1) w : int | None Coil span. None → Q // (P // 2).

## Returns

float |kw(ν)| ∈ \[0, 1\]

``` python
# Examples
assert math.isclose(winding_factor_sos(5, 12, 10, layers=2), 0.9330, abs_tol=1e-4)
assert math.isclose(winding_factor_sos(4, 12, 8, layers=2), 0.8660, abs_tol=1e-4)
```

``` python
from emachines.winding.sos import winding_factor_sos

configs = [
    (12, 10, 2, "12s/10p FSCW  — ν=5 working"),
    (12,  8, 2, "12s/8p  FSCW  — ν=4 working"),
    ( 9,  8, 2, "9s/8p   FSCW  — ν=4 working"),
    (24,  4, 2, "24s/4p  ISW   — ν=2 working"),
]

print(f"{'Configuration':<35}  kw at working ν")
print("-" * 55)
for Q, P, layers, label in configs:
    p = P // 2
    kw = winding_factor_sos(p, Q, P, layers=layers)
    print(f"  {label:<33}  {kw:.4f}")
```

    Configuration                        kw at working ν
    -------------------------------------------------------
      12s/10p FSCW  — ν=5 working        0.9330
      12s/8p  FSCW  — ν=4 working        0.8660
      9s/8p   FSCW  — ν=4 working        0.9452
      24s/4p  ISW   — ν=2 working        0.9659

------------------------------------------------------------------------

## `check_symmetry`: Validate Winding Balance

### Theory

A valid three-phase winding must be **balanced**: each phase must carry
the same number of conductors, and the total slot count must divide
evenly.

The check verifies: 1. Q is divisible by m (equal slots per phase). 2.
The total period t = gcd(Q, p) \> 0. 3. The number of conductors per
phase is equal across all m phases.

**Reference:** Pyrhönen et al. (2008), §2.1.

------------------------------------------------------------------------

### check_symmetry

``` python
def check_symmetry(
    Q:int, P:int, m:int=3, layers:int | None=None, # if no candidate exists.
    w:int | None=None
)->dict: # balanced     : bool   Phase EMFs equal in magnitude and 360°/m apart
buildable    : bool   Each phase has as many go as return conductors
conductors   : list   Conductor count per phase [n_A, n_B, n_C, …]
periodicity  : int    Winding periodicity t = gcd(Q, p)
q            : Fraction   Slots per pole per phase
valid        : bool   balanced and buildable and Q % m == 0
layers, w    : int, int | None   The layout that was checked
kw1          : float  Fundamental winding factor
reason       : str    Why it fails, or ''
```

*Check whether the star-of-slots winding for (Q, P, m) is usable.*

## Parameters

Q : int Number of stator slots P : int Number of poles m : int Number of
phases (default 3) layers : int | None 1 or 2. None → the best
candidate, or single layer if no candidate exists. w : int | None
Double-layer coil span. None → the best span.

## Returns

dict with keys: balanced : bool Phase EMFs equal in magnitude and 360°/m
apart buildable : bool Each phase has as many go as return conductors
conductors : list Conductor count per phase \[n_A, n_B, n_C, …\]
periodicity : int Winding periodicity t = gcd(Q, p) q : Fraction Slots
per pole per phase valid : bool balanced and buildable and Q % m == 0
layers, w : int, int | None The layout that was checked kw1 : float
Fundamental winding factor reason : str Why it fails, or ’’

``` python
# Examples
r = check_symmetry(12, 10, layers=2)
assert (r['balanced'], r['valid']) == (True, True)
r = check_symmetry(9, 8, layers=1)
assert (r['buildable'], r['valid']) == (False, False)
```

``` python
from emachines.winding.sos import check_symmetry

for Q, P, layers in [(12, 10, 2), (12, 8, 2), (9, 8, 2), (9, 8, 1), (24, 4, 1), (6, 2, 1)]:
    r = check_symmetry(Q, P, layers=layers)
    status = "✓" if r['valid'] else "✗"
    print(f"  {status} {Q}s/{P}p layers={layers}  balanced={r['balanced']}  buildable={r['buildable']}  "
          f"conductors={r['conductors']}  q={r['q']}  t={r['periodicity']}  {r['reason']}")
```

      ✓ 12s/10p layers=2  balanced=True  buildable=True  conductors=[8, 8, 8]  q=2/5  t=1  
      ✓ 12s/8p layers=2  balanced=True  buildable=True  conductors=[8, 8, 8]  q=1/2  t=4  
      ✓ 9s/8p layers=2  balanced=True  buildable=True  conductors=[6, 6, 6]  q=3/8  t=1  
      ✗ 9s/8p layers=1  balanced=True  buildable=False  conductors=[3, 3, 3]  q=3/8  t=1  phase A has 2 go and 1 return conductors, so it can't be wound into coils
      ✓ 24s/4p layers=1  balanced=True  buildable=True  conductors=[8, 8, 8]  q=2  t=2  
      ✓ 6s/2p layers=1  balanced=True  buildable=True  conductors=[2, 2, 2]  q=1  t=1  

------------------------------------------------------------------------

## `get_valid_coil_spans`: Valid Coil Span Enumeration

### Theory

Not all coil spans produce a valid balanced winding. The valid spans are
those that maintain winding symmetry — specifically those where the span
does not coincide with a phase boundary, causing phase short-circuiting.

For FSCW (q \< 1), the only valid span is typically w = 1 (tooth coil).
For integer-slot windings, spans from 1 to `full_pitch` (= Q//p) are
candidates; the full-pitch span maximises kw.

The function returns all spans that produce a balanced winding (equal
conductors per phase).

------------------------------------------------------------------------

### get_valid_coil_spans

``` python
def get_valid_coil_spans(
    Q:int, P:int, m:int=3
)->list[int]:
```

*Return all coil spans (in slots) that yield a balanced winding.*

Evaluates double-layer spans 1 … max(Q//P, Q//p) and keeps those that
pass `check_winding` (phase EMFs equal and 360°/m apart).

## Parameters

Q : int Number of stator slots P : int Number of poles m : int Number of
phases (default 3)

## Returns

list\[int\] Valid coil spans in ascending order (empty if none).

``` python
# Examples
assert get_valid_coil_spans(12, 10) == [1, 2]
assert get_valid_coil_spans(24, 4) == list(range(1, 12))
```

``` python
from emachines.winding.sos import get_valid_coil_spans

for Q, P in [(12, 10), (12, 8), (9, 8), (24, 4), (36, 6)]:
    spans = get_valid_coil_spans(Q, P)
    print(f"  {Q}s/{P}p  valid spans: {spans}  (pole pitch = {Q / P:.2f} slots)")
```

      12s/10p  valid spans: [1, 2]  (pole pitch = 1.20 slots)
      12s/8p  valid spans: [1, 2]  (pole pitch = 1.50 slots)
      9s/8p  valid spans: [1, 2]  (pole pitch = 1.12 slots)
      24s/4p  valid spans: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]  (pole pitch = 6.00 slots)
      36s/6p  valid spans: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]  (pole pitch = 6.00 slots)

------------------------------------------------------------------------

## `is_valid_combination`: Feasibility Check

Returns `True` if a (Q, P, m) combination can produce a balanced m-phase
winding. This is the entry-point for design sweeps and UI filtering.

------------------------------------------------------------------------

### is_valid_combination

``` python
def is_valid_combination(
    Q:int, P:int, m:int=3
)->bool:
```

*Return True if (Q, P, m) supports a balanced m-phase winding.*

A combination is valid when at least one star-of-slots winding (single
layer, or double layer with some coil span) can be wound into coils and
is balanced. P must be even and ≥ 2, and m odd.

## Parameters

Q : int Number of stator slots P : int Number of poles m : int Number of
phases (default 3)

``` python
# Examples
assert is_valid_combination(12, 10)
assert not is_valid_combination(12, 10, m=5)
assert not is_valid_combination(7, 4)
```

``` python
from emachines.winding.sos import is_valid_combination

print("Feasibility sweep (Q=6…18, P=2…12, m=3):")
for Q in range(6, 19, 3):
    row = []
    for P in range(2, 13, 2):
        v = is_valid_combination(Q, P)
        row.append(f"P={P}:{'✓' if v else '✗'}")
    print(f"  Q={Q:2d}: {' '.join(row)}")
```

    Feasibility sweep (Q=6…18, P=2…12, m=3):
      Q= 6: P=2:✓ P=4:✓ P=6:✗ P=8:✓ P=10:✓ P=12:✗
      Q= 9: P=2:✓ P=4:✓ P=6:✓ P=8:✓ P=10:✓ P=12:✓
      Q=12: P=2:✓ P=4:✓ P=6:✗ P=8:✓ P=10:✓ P=12:✗
      Q=15: P=2:✓ P=4:✓ P=6:✗ P=8:✓ P=10:✓ P=12:✗
      Q=18: P=2:✓ P=4:✓ P=6:✓ P=8:✓ P=10:✓ P=12:✓

------------------------------------------------------------------------

## References

1.  Pyrhönen, J., Jokinen, T., & Hrabovcová, V. (2008). *Design of
    Rotating Electrical Machines*. Wiley. §2.1–2.4.
2.  Hanselman, D.C. (2003). *Brushless Permanent Magnet Motor Design*,
    2nd ed. Writer’s Collective. Ch. 4.
3.  Bianchi, N., & Bolognani, S. (2002). Design techniques for reducing
    cogging torque in surface-mounted PM motors. *IEEE Trans. Ind.
    Appl.*, 38(5), 1259–1265.
4.  Müller, G., Vogt, K., & Ponick, B. (2008). *Berechnung elektrischer
    Maschinen*. Wiley-VCH. §3.4.
5.  SWAT-EM: <https://github.com/bayonet222/swat-em>

------------------------------------------------------------------------

## Tests
