Magnetics add-on: template shapes and hand formulas
Builds ready-made magnet study cross-sections and the textbook value each result is compared with.
How the precision is kept
Each hand value is one closed-form 64-bit expression from the template's sizes in mm (converted with × 1e-3) and the study's currents and permeabilities. μ0 is written as 4π × 10⁻⁷. The formulas are idealizations, and the file stores an acceptance tolerance with each.
Calculations under this policy
7 calculations in 1 source file. The code is read from the released app when this page loads. It is the calculation people run, not a copy kept for this page.
4e-7 × Math.PI in 64-bit. This is the pre-2019 defined value; the measured value differs at about the tenth significant digit, far inside the templates' tolerances.
addons/magnetics/lib/templates.js
lines 2 to 19
PartCraft 0.8.1
/**
* templates.js — ready-made magnet studies (the Magnetics add-on, 0.8 SG.7+; DOM-free). Each one is the cross-section of a long device, in mm,
* as closed polygons with a role per polygon (air, iron, magnet, coil: the shapes of PartCraft's magnet-study.js), so a study can be set up and
* solved without drawing sketches first. Five of them, three with a hand formula the result is compared against:
*
* ccore C-core electromagnet: an iron C with an air gap, a coil round its back leg (go and return) hand: μ0·NI / (g + l/μr) in the gap
* frame a permanent magnet in one leg of a closed iron frame hand: Br·(lm/μrm) / (lm/μrm + L/μr)
* keeper a holding magnet: U yoke with a magnet in its base, a keeper across the poles over a small gap (no hand formula; force through a thin gap is rough)
* wire one round conductor in air hand: B = μ0·I / 2πr at r = 2a
* wires two parallel conductors with opposite currents hand: F = μ0·I² / 2πd per metre
*
* TEMPLATES { id → { params: [{ key, def, min, max }], pad? (the air box margin it wants), build(p) → { loops, specs, names }, hand?(p, specs) → { kind, … } } }
* build(id, params) → { loops: [[[x, y] …] …] (mm), specs: [spec], names: [language key], hand: … | null, params (clamped) }
* circle(cx, cy, r, n) → a polygon with n corners (counter-clockwise)
* A spec is magnet-study.js's: { role, mur, curve, br, angle, murMag, current (total A through the profile), force }.
* Every material number here is an example (iron μr 1000–2000, magnet Br 1.2 T, μr 1.05), not a data sheet.
*/
export const MU0 = 4e-7 * Math.PI;
Round conductor as a polygon
64-bit
The corners of the polygon that stands for a round wire.
pᵢ = (cx + r cos(2πi ÷ n), cy + r sin(2πi ÷ n)), n = 48
Corners lie on the circle, so the polygon's area is slightly less than the circle's. Angles are 2π × i ÷ n with Math.cos and Math.sin.
addons/magnetics/lib/templates.js
line 23
PartCraft 0.8.1
export const circle = (cx, cy, r, n = 48) => Array.from({ length: n }, (_, i) => [cx + r * Math.cos(2 * Math.PI * i / n), cy + r * Math.sin(2 * Math.PI * i / n)]);
C-core gap field
64-bit
The flux density in the air gap of a C-core electromagnet by the magnetic circuit formula.
B = μ0 × NI ÷ ((g + l ÷ μr) × 10⁻³), l = 2(w − tk) + 2(h − tk) − g
Sizes are in mm; the path length g + l ÷ μr is multiplied by 1e-3 once. NI and μr are read with Number(); a missing μr becomes 1.
addons/magnetics/lib/templates.js
lines 40 to 41
PartCraft 0.8.1
const { w, h, tk } = p, g = Math.min(p.gap, h - 2 * tk - 2), l = 2 * (w - tk) + 2 * (h - tk) - g, NI = Math.abs(Number(specs[1]?.current) || 0), mur = Number(specs[0]?.mur) || 1;
return { kind: 'meanB', box: [w - tk, h / 2 - g / 2, w, h / 2 + g / 2], value: MU0 * NI / ((g + l / mur) * 1e-3), what: 'hand.ccore', tol: 0.15 };
Magnet in a closed iron frame
64-bit
The flux density in a permanent magnet set in one leg of a closed frame.
B = Br × (lm ÷ μrm) ÷ (lm ÷ μrm + L ÷ μr), L = 2(w − tk) + 2(h − tk) − lm
Lengths appear in a ratio, so no unit factor is needed. A missing magnet or iron permeability becomes 1.
Clamps each size a person types to the template's allowed range.
Each size is clamped to its range; a value that is not a finite number falls back to the default. The wall thickness is limited to (min(w, h) − 6) ÷ 2.
addons/magnetics/lib/templates.js
lines 100 to 106
PartCraft 0.8.1
export function cleanParams(id, params = {}) {
const T = TEMPLATES[id];
const out = {};
for (const q of T.params) { const v = Number(params[q.key]); out[q.key] = Number.isFinite(v) ? Math.min(q.max, Math.max(q.min, v)) : q.def; }
if (id === 'ccore' || id === 'frame' || id === 'keeper') { out.tk = Math.min(out.tk, (Math.min(out.w, out.h) - 6) / 2); }
return out;
}
In detail
Method
C-core: B = μ0·NI ÷ (g + l ÷ μr). Frame: B = Br·(lm ÷ μrm) ÷ (lm ÷ μrm + L ÷ μr), for which the file cites P. Campbell, Permanent Magnet Materials and their Application (1994). Wire: B = μ0·I ÷ (2πr). Two wires: F = μ0·I₁I₂ ÷ (2πd) per meter.
Number types
64-bit numbers.
Tolerances
Acceptance tolerances stored with the formulas: 15 % (C-core), 2 % (frame), 2 % (wire), 6 % (two wires).
Limits
μ0 = 4π × 10⁻⁷ is the value that was fixed by definition before the 2019 SI revision; the measured value recommended since differs from it at about the tenth significant digit. The C-core comment says the formula has no fringing and no leakage.
Approach: 64-bit, direct
Every value is a 64-bit number from input to result. Formulas are closed form or a direct solve, and every comparison uses a stated tolerance.