# Permanent Magnet Materials


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

## Executive Summary

**Purpose:** Provide realistic PM properties for motor design

**What it does:** Store and query permanent magnet material data.

### Why It Matters

Motor design requires PM flux linkage and temperature derating curves.

### Quick Start

**Inputs:** Magnet grade (NdFeB, SmCo), operating temperature

**Outputs:** Remanent flux Br, coercivity Hc, temperature derating, J-H
curves

## How It Fits Into the Motor Design Workflow

**Upstream (depends on):** Feeds flux linkage values to 02_pmsm.ipynb
and other analysis

**Downstream (used by):** See notebook connections

## Theory: NdFeB Magnets

**Strongest permanent magnets available.**

**Properties:** - Remanence (Br): ≈1.0–1.5 T at 20°C - Coercivity (Hcb,
Hcj): Depends on grade - Temperature effects: Br and Hcj decline

**Grades:** - N-series (80°C): Room temperature - M-series (100°C):
Moderate temperature - H/SH/UH/EH (120–200°C): High temperature

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

### MagnetGrade

``` python
def MagnetGrade(
    grade:str, br:float, hcb:float, hcj:float, alpha_br:float, alpha_hcj:float
)->None:
```

*Room-temperature (20 °C) properties of a permanent magnet grade.*

Units (as stored in MAGNET_LIBRARY): br : T hcb, hcj : kA/m (MagnetData
expects A/m; use MagnetData.from_grade) alpha_br : %/K temperature
coefficient of Br alpha_hcj : %/K temperature coefficient of Hcj

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

### MagnetData

``` python
def MagnetData(
    grade:str, from_standard:str, temperature_C:float, T_max:float, Br:float, Hcj:float, alpha_br:float,
    alpha_hcj:float, H:np.ndarray=<factory>, J:np.ndarray=<factory>, B:np.ndarray=<factory>,
    J_20C:np.ndarray=<factory>, B_20C:np.ndarray=<factory>
)->None:
```

*Temperature-corrected demagnetization curves.*

Units: Br in T, Hcj in **A/m**, alpha_br and alpha_hcj in %/K,
temperatures in °C. To build one from a MAGNET_LIBRARY grade (stored in
kA/m), use `from_grade`.

## Test

Verify magnet grades and temperature correction.

``` python
# Test MAGNET_LIBRARY
assert "N42" in MAGNET_LIBRARY
assert len(MAGNET_LIBRARY) == 25
n42 = MAGNET_LIBRARY["N42"]
assert n42.br == 1.305
print(f"✓ MAGNET_LIBRARY: {len(MAGNET_LIBRARY)} grades")

# Test MagnetData
magnet = MagnetData(
    grade="N42",
    from_standard="Arnold Magnetics",
    temperature_C=80,
    T_max=80,
    Br=n42.br,
    Hcj=n42.hcj * 1e3,
    alpha_br=n42.alpha_br,
    alpha_hcj=n42.alpha_hcj,
)
magnet.calculate_JH()
assert len(magnet.H) == 100
assert len(magnet.B) == 100
print(f"✓ MagnetData.calculate_JH(): {len(magnet.H)} points")
print("\nAll tests passed!")

# ── Units: library grades are kA/m, MagnetData wants A/m ──────────────
import math, warnings
m1 = MagnetData.from_grade(n42, temperature_C=20, T_max=80)
m1.calculate_JH()
assert m1.Hcj == n42.hcj * 1e3
assert math.isclose(m1.J[-1], n42.br, rel_tol=1e-9), m1.J[-1]       # J(H=0) = Br
assert abs(m1.J[0]) < 1e-9                                          # J(H=-Hcj) = 0
bad = MagnetData(grade="N42", from_standard="lib", temperature_C=20, T_max=80, Br=n42.br,
                 Hcj=n42.hcj, alpha_br=n42.alpha_br, alpha_hcj=n42.alpha_hcj)   # kA/m by mistake
try:
    bad.calculate_JH()
    assert False, "Hcj in kA/m should raise"
except ValueError as e:
    assert "looks like kA/m" in str(e)
print("✓ MagnetData.from_grade and kA/m guard")

# ── T_max warning ─────────────────────────────────────────────────────
with warnings.catch_warnings(record=True) as caught:
    warnings.simplefilter("always")
    MagnetData.from_grade(n42, temperature_C=120, T_max=80).calculate_JH()
    MagnetData.from_grade(n42, temperature_C=60, T_max=80).calculate_JH()
assert len(caught) == 1 and "above the maximum operating temperature" in str(caught[0].message)
print("✓ MagnetData warns above T_max")
```

    ✓ MAGNET_LIBRARY: 25 grades
    ✓ MagnetData.calculate_JH(): 100 points

    All tests passed!
    ✓ MagnetData.from_grade and kA/m guard
    ✓ MagnetData warns above T_max
