FERRETGame engine over Babylon.js
Ferret

@ferret/physics-electric

circuits of sources, wires, lamps, motors, inductors and capacitors solved each step.

48 exports · source in packages/physics-electric/src

Classes

Base class

abstract class Base implements Element
extra: numberextra unknowns this element needs (ideal voltage sources and transformers)
k: numberfirst extra index, set by the circuit
nonlinear: boolean
kind: Element['kind']
power: numberpower the element takes from the circuit now, W (negative = delivers)
v: numberlast voltage across it and last current through it
i: number
constructor(readonly name: string, readonly a: string, readonly b: string)
abstract stamp(s: Stamper, h: number, t: number): voidput the element into the matrix for a step of h ending at time t
commit(va: number, vb: number, _h: number, _t: number, _c: Circuit): voidafter the step is accepted: voltages across a→b and the current a→b through the element
abstract current(va: number, vb: number, x: number[]): numbercurrent a → b through the element for the solution (to read it)

packages/physics-electric/src/circuit.ts:65

Battery class

class Battery extends Base

A battery: an open-circuit voltage that falls with the state of charge behind an internal resistance; Ah of capacity.

kind: "source"
soc: number
heat: number
empty: boolean
drawn: number
constructor(name: string, a: string, b: string, public capacityAh: number, public rInt = 0.05, public vFull = 12.6, public vEmpty = 10.5, public soc0 = 1)
get open()
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element

packages/physics-electric/src/circuit.ts:198

Breaker class

class Breaker extends Base

Thermal–magnetic breaker: θ' = (I²/In² − θ)/τ trips at θ = 1, and a current above magnetic·In trips at once.

theta: number
tripped: boolean
constructor(name: string, a: string, b: string, public rated: number, public tau = 10, public magnetic = 8, public R = 1e-3)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
reset()
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:180

Capacitor class

class Capacitor extends Base
kind: "storage"
constructor(name: string, a: string, b: string, public C: number, v0 = 0, public leak = 0)
stamp(s: Stamper, h: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
stored()
phasor(s: PhasorStamper, w: number)

packages/physics-electric/src/circuit.ts:80

Circuit class

class Circuit
readonly elements: Element[]
readonly byName: Map<string, Element>
time: number
euler: booleanthe step in hand is a backward-Euler step
x: number[]extras and node voltages of the last solution
voltages: Map<string, number>
events: CircuitEvent[]
onEvent: ((e: CircuitEvent) => void) | null
delivered: numbercumulative energy given by the sources and taken by the elements, J
dissipated: number
add<T extends Element>(e: T): T
get<T extends Element = Element>(name: string): T
transformer(name: string, p1: string, p2: string, s1: string, s2: string, spec: TransformerSpec)A real transformer: winding resistances, magnetising inductance and core loss around an ideal one. Returns its parts.
restart()A discontinuity (a switch thrown) is coming: take the next step with backward Euler so the trapezoid does not ring.
emit(type: CircuitEventType, element: string)
get size()
step(h: number)Advance h seconds.
balance()Energy bookkeeping: delivered by sources against dissipated plus stored, J.
voltage(node: string)
phasor(w: number): Map<string, [number, number]>AC steady state at angular frequency w: complex node voltages (re, im) by node name.

packages/physics-electric/src/circuit.ts:383

Coil class

class Coil extends Base

An electromagnet as a circuit element: a coil of N turns on a core with a pole area A and an air gap, so its inductance follows the gap, L = L_leak + μ0 N² A / (gaps · g), and falls off as the iron saturates (B = B_sat tanh(B_lin / B_sat)). The state is the flux linkage λ, integrated by the trapezoid rule, so moving the armature (changing gap) makes a motional EMF by itself: v = R i + dλ/dt. force is the pull on the armature, gaps · B² A / (2 μ0), and work the mechanical work done on it while the gap closed.

kind: "storage"
gap: numberair gap per pole, m; set by whoever moves the armature
lambda: numberflux linkage, Wb
work: number
constructor( name: string, a: string, b: string, public turns: number, public area: number, gap: number, public Rw = 0, public gaps = 2, public Bsat = 1.6, public leakage = 0, )
L(i = this.i): numberthe secant inductance at the present current (the unsaturated one at zero), H
get B()flux density in the gap, T
get force()pull on the armature toward the core, N
stamp(s: Stamper, h: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
stored()

packages/physics-electric/src/elements.ts:11

DCMachine class

class DCMachine extends Base

A DC machine: V = E + R·i + L·di/dt with E = k·ω from the shaft, and the torque on the shaft T = k·i. Run as a motor (current into the a terminal) it drives the shaft; turned by the shaft it makes E, delivers current and loads the shaft with k·i < 0.

kind: "machine"
omega: numbershaft speed, rad/s, set by whoever owns the shaft (the capability, or a test)
field: numberfield excitation 0..1 for a wound-field machine (k scales with it)
electrical: numberelectrical work taken, mechanical work given (motor) or taken (generator), J
mechanical: number
copper: number
constructor(name: string, a: string, b: string, public k_: number, public Ra: number, public La = 0)
get kEff()
get torque()torque on the shaft, N·m (positive drives it)
stamp(s: Stamper, h: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
stored()

packages/physics-electric/src/circuit.ts:336

Diode class

class Diode extends Base

A diode with a forward drop and an on-resistance (piecewise linear) or the exponential law; an LED is a diode with a larger drop.

nonlinear: boolean
constructor(name: string, a: string, b: string, public Vf = 0.7, public Ron = 0.05, public exponential = false, public Is = 1e-12, public n = 1.8)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)

packages/physics-electric/src/circuit.ts:147

Fuse class

class Fuse extends Base

The element melts when ∫I²dt passes its limit (A²·s).

integral: number
blown: boolean
constructor(name: string, a: string, b: string, public i2t: number, public R = 1e-3)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:167

Heater class

class Heater extends Resistor

A heater: a resistor whose heat is handed to a part; its resistance follows the temperature coefficient.

joules: number
constructor(name: string, a: string, b: string, R: number, public alpha = 0, public temperature = 293.15, public T0 = 293.15)
get cold()
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element

packages/physics-electric/src/circuit.ts:269

IdealTransformer class

class IdealTransformer extends Base

An ideal transformer between two ports through one extra unknown (the secondary current). V1 = n·V2 and I1 = I2/n, n = N1/N2.

extra: numberextra unknowns this element needs (ideal voltage sources and transformers)
kind: "other"
constructor(name: string, public p1: string, public p2: string, public s1: string, public s2: string, public n: number)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number, x: number[])current a → b through the element for the solution (to read it)
commit(va: number, vb: number, _h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:305

InductionMotor class

class InductionMotor extends Base

A three-phase induction motor, one phase of its equivalent circuit (wye, so the phase voltage is across a→b): the stator R1 + L1 in series with the magnetising Lm across the rest, then the rotor L2 and R2/s. The slip s comes from the shaft speed and the supply frequency, so the load resistor R2/s follows the shaft; the air-gap power of the phases over the synchronous speed is the torque, negative above that speed (a generator). omega is set by whoever owns the shaft. install adds the parts and the motor to a circuit.

kind: "machine"
omega: number
torque: numbertorque on the shaft, N·m (averaged over a few supply cycles)
mechanical: number
readonly parts: { r1: Resistor; l1: Inductor; lm: Inductor; l2: Inductor; rr: Resistor }
constructor( name: string, a: string, b: string, public spec: { r1: number; x1: number; xm: number; r2: number; x2: number; hz: number; poles: number; phases?: number }, )
get ws()synchronous speed, rad/s
get slip()
stamp(_s: Stamper)put the element into the matrix for a step of h ending at time t
current()current a → b through the element for the solution (to read it)
commit(_va: number, _vb: number, h: number, _t: number, _c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
install(c: Circuit)

packages/physics-electric/src/elements.ts:96

Inductor class

class Inductor extends Base
kind: "storage"
constructor(name: string, a: string, b: string, public L: number, public Rw = 0, i0 = 0)
stamp(s: Stamper, h: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
stored()
phasor(s: PhasorStamper, w: number)

packages/physics-electric/src/circuit.ts:95

ISource class

class ISource extends Base
kind: "source"
constructor(name: string, a: string, b: string, public I: number | ((t: number) => number))
stamp(s: Stamper, _h: number, t: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, _h: number, t: number)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:130

Lamp class

class Lamp extends Base

Incandescent lamp: the filament's resistance follows its temperature, which follows the power in and the radiation out.

nonlinear: boolean
T: number
burnt: boolean
constructor( name: string, a: string, b: string, /** cold resistance at T0, ohms */ public R0: number, /** heat capacity of the filament, J/K and its radiating area, m² (emissivity included) */ public C = 2e-3, public area = 6e-5, /** temperature coefficient of resistance, 1/K (tungsten ~4.5e-3 near room temperature, falling hot: R ∝ T^1.2 above) */ public alpha = 4.5e-3, public T0 = 293.15, public Tmelt = 3695, public ambient = 293.15, )
resistance(T: number)
stamp(s: Stamper, h: number)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
get radiated()radiated power now, W

packages/physics-electric/src/circuit.ts:218

Potentiometer class

class Potentiometer

A potentiometer: a track of total resistance R from a to b with the wiper at w, position 0..1 from a. Add with addPotentiometer.

constructor(readonly top: Resistor, readonly bottom: Resistor, readonly total: number, private circuit: Circuit, position: number)
get position()
set position(p: number)

packages/physics-electric/src/elements.ts:175

Rcd class

class Rcd extends Switch

A residual-current device (earth-leakage trip): a switch in the line that opens when the current out and the current back differ by more than threshold for delay seconds. sense gives that difference, A (the circuit's own element currents: live − neutral).

tripped: boolean
constructor(name: string, a: string, b: string, public sense: () => number, public threshold = 0.03, public delay = 0.03)
reset()
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element

packages/physics-electric/src/elements.ts:213

Relay class

class Relay extends Coil

A relay: a coil that pulls a contact closed when its current passes pullIn and lets it go below dropOut, after the armature's travel time. The contact is a Switch the circuit holds (addRelay adds both).

closed: boolean
readonly contact: Switch
constructor(name: string, a: string, b: string, ca: string, cb: string, public pullIn: number, public dropOut: number, public travel = 5e-3, coil = { turns: 2000, area: 1e-4, gap: 2e-3, Rw: 100 })
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element

packages/physics-electric/src/elements.ts:63

Resistor class

class Resistor extends Base
constructor(name: string, a: string, b: string, public R: number)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:73

SolarCell class

class SolarCell extends Base

A solar cell: the photocurrent Isc · irradiance / 1000 against the diode I0 (exp(V/(n Vt)) − 1), nonlinear. Wire its series resistance as a Resistor. The cell delivers when the current through it a→b is negative.

nonlinear: boolean
irradiance: numberW/m²
constructor(name: string, a: string, b: string, public Isc = 3, public I0 = 1e-10, public n = 1.3, public cells = 1)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
get Voc()open-circuit voltage, V

packages/physics-electric/src/elements.ts:193

Switch class

class Switch extends Base
constructor(name: string, a: string, b: string, public closed = false, public Ron = 1e-3, public Roff = 1e9)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:139

Thermistor class

class Thermistor extends Base

A thermistor: NTC R = R0 exp(B (1/T − 1/T0)) or PTC R = R0 (1 + α (T − T0)) (ptc = α), with its own temperature from self-heating, C dT/dt = P − (T − T_amb)/Rth. It is stamped at the temperature of the start of the step.

T: number
R: number
constructor(name: string, a: string, b: string, public R0: number, public B = 3950, public T0 = 298.15, public ambient = 298.15, public Rth = 50, public heatCapacity = 0.05, public ptc = 0)
resistance(T: number)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
phasor(s: PhasorStamper)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element

packages/physics-electric/src/elements.ts:157

VSource class

class VSource extends Base
kind: "source"
extra: numberextra unknowns this element needs (ideal voltage sources and transformers)
constructor(name: string, a: string, b: string, public V: number | ((t: number) => number), public phasorValue: [number, number] = [0, 0])
value(t: number)
stamp(s: Stamper, _h: number, t: number)put the element into the matrix for a step of h ending at time t
current(_va: number, _vb: number, x: number[])current a → b through the element for the solution (to read it)
commit(va: number, vb: number, _h: number, _t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:115

Wire class

class Wire extends Base

A wire: resistance from its length, a heat capacity, and a thermal resistance to the air chosen so that the ampacity gives the rated rise.

T: number
over: boolean
joules: number
readonly R: number
readonly Rth: number
constructor( name: string, a: string, b: string, resistancePerM: number, public length: number, public ampacity: number, public heatCapacity: number, public insulation = 343.15, public ambient = 293.15, public ratedRise = 40, )
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)
commit(va: number, vb: number, h: number, t: number, c: Circuit)after the step is accepted: voltages across a→b and the current a→b through the element
phasor(s: PhasorStamper)

packages/physics-electric/src/circuit.ts:285

Zener class

class Zener extends Base

A Zener diode: forward like a diode, and in reverse it holds the voltage at Vz with a slope resistance Rz.

nonlinear: boolean
constructor(name: string, a: string, b: string, public Vz = 5.1, public Rz = 2, public Vf = 0.7, public Ron = 0.05)
stamp(s: Stamper)put the element into the matrix for a step of h ending at time t
current(va: number, vb: number)current a → b through the element for the solution (to read it)

packages/physics-electric/src/elements.ts:141

Functions

addPotentiometer function

function addPotentiometer(c: Circuit, name: string, a: string, w: string, b: string, R: number, position = 0.5)

packages/physics-electric/src/elements.ts:185

addRelay function

function addRelay(c: Circuit, name: string, a: string, b: string, ca: string, cb: string, pullIn: number, dropOut: number, travel?: number)

Add a relay and its contact to a circuit.

packages/physics-electric/src/elements.ts:84

electric function

function electric(): ElectricCapability

packages/physics-electric/src/index.ts:42

gauss function

function gauss(A: number[][], b: number[]): number[] | null

Gaussian elimination with partial pivoting; null when singular.

packages/physics-electric/src/circuit.ts:537

inductionTorque function

function inductionTorque(o: { tMax: number; slipMax: number; hz: number; poles: number; omega: number; voltage?: number }): number

Reduced induction motor: Kloss torque against slip at the line frequency and voltage.

packages/physics-electric/src/circuit.ts:367

magnetForce function

function magnetForce(N: number, I: number, area: number, gap: number, gaps = 1): number

Force of a magnet with a coil of N turns, current I, pole area A over an air gap g (two gaps for a C-core): B²A/(2μ0) per gap.

packages/physics-electric/src/circuit.ts:375

mains function

function mains: (name: string, a: string, b: string, peak: number, hz: number, phase?: number) => VSource

Mains: a sinusoidal source.

packages/physics-electric/src/circuit.ts:127

phasePeak function

function phasePeak: (lineRms: number) => number

The phase peak of a balanced three-phase supply of a given line-to-line RMS voltage, V.

packages/physics-electric/src/elements.ts:236

railForce function

function railForce: (gradient: number, I: number) => number

Force of a rail gun carrying I, with inductance gradient L' (H/m): ½·L'·I².

packages/physics-electric/src/circuit.ts:380

synchronousSpeed function

function synchronousSpeed: (hz: number, poles: number) => number

packages/physics-electric/src/circuit.ts:372

threePhase function

function threePhase(c: Circuit, name: string, p: [string, string, string], peak: number, hz: number, n = '0')

Three-phase mains: three sources of the same peak 120° apart from the nodes p to a neutral n (default ground). The phasor form works as for a single phase. Returns the sources.

packages/physics-electric/src/elements.ts:231

Interfaces

CircuitEntry interface

interface CircuitEntry
circuit: Circuit
machines: MachineBinding[]
induction: InductionBinding[]
heaters: HeaterBinding[]
shorted: boolean

packages/physics-electric/src/index.ts:29

CircuitEvent interface

interface CircuitEvent
type: CircuitEventType
element: string
time: number

packages/physics-electric/src/circuit.ts:11

ElectricCapability interface

interface ElectricCapability extends Capability
readonly circuits: Map<string, CircuitEntry>
addCircuit(name: string): Circuit
bindMachine(circuit: string, machine: string, drivetrain: Drivetrain, shaft: Shaft): voidThe machine turns the shaft with its torque and takes its speed from it.
bindInduction(circuit: string, motor: string, drivetrain: Drivetrain, shaft: Shaft): voidAn induction motor turns the shaft with its torque (negative above synchronous speed: a generator) and takes its speed from it.
bindHeater(circuit: string, heater: string, entity: Entity): voidThe heater's power goes into the part of the entity, and the part's temperature sets its resistance.

packages/physics-electric/src/index.ts:31

Element interface

interface Element
readonly name: string
readonly a: string
readonly b: string
extra: numberextra unknowns this element needs (ideal voltage sources and transformers)
k: numberfirst extra index, set by the circuit
nonlinear: boolean
kind: 'resistive' | 'storage' | 'source' | 'machine' | 'other'
stamp(s: Stamper, h: number, t: number): voidput the element into the matrix for a step of h ending at time t
commit(va: number, vb: number, h: number, t: number, c: Circuit): voidafter the step is accepted: voltages across a→b and the current a→b through the element
current(va: number, vb: number, x: number[]): numbercurrent a → b through the element for the solution (to read it)
stored?(): numberenergy stored, J
phasor?(s: PhasorStamper, w: number): voidthe phasor stamp at angular frequency w (complex admittances)
power: numberpower the element takes from the circuit now, W (negative = delivers)
v: numberlast voltage across it and last current through it
i: number

packages/physics-electric/src/circuit.ts:31

PhasorStamper interface

interface PhasorStamper
y(a: string, b: string, re: number, im: number): void
i(a: string, b: string, re: number, im: number): void
v(a: string, b: string, k: number, re: number, im: number): void
m(row: string, col: string, re: number, im: number): void

packages/physics-electric/src/circuit.ts:58

Stamper interface

interface Stamper
g(a: string, b: string, G: number): voidconductance between two nodes
i(a: string, b: string, I: number): voida current I leaving node a through the source into node b
v(a: string, b: string, k: number, V: number): voida voltage source V = v(a) − v(b) with its current (a → b through the source) as extra unknown number k
at(n: string): numberthe node voltage of the last Newton guess
x(k: number): numberthe extra current k of the last guess
m(row: string, col: string, c: number): voidadd a coefficient to the matrix (row and column are node names or '#k' for the extra unknowns)
rhs(row: string, c: number): void
readonly be: booleanthe step is a backward-Euler one (the first, or after a discontinuity)

packages/physics-electric/src/circuit.ts:13

TransformerSpec interface

interface TransformerSpec
ratio: numberturns ratio N1/N2
r1?: numberwinding resistances, ohms
r2?: number
lm?: numbermagnetising inductance seen from the primary, H, and the core-loss resistance across it, ohms (Infinity = none)
rcore?: number

packages/physics-electric/src/circuit.ts:323

Types

CircuitEventType type

type CircuitEventType = 'blown' | 'trip' | 'battery:empty' | 'lamp:burnt' | 'overheat' | 'short' | 'motor:stall'

packages/physics-electric/src/circuit.ts:10

Constants

ELECTRIC_DEFAULTS const

const ELECTRIC_DEFAULTS: { maxStep: number; shortCurrent: number; }

electricity. Circuits are stepped with the world; machines turn shafts of M and are turned by them, heaters put their power into the parts of H, and every protection element reports as an event.

values (each at world / level / zone / entity scope):

  maxStep       longest circuit sub-step, s                       (0.001)
  shortCurrent  a source current above this is a short circuit, A  (1000)

events: circuit:blown, circuit:trip, circuit:short, circuit:overheat, battery:empty, lamp:burnt, motor:stall, each { circuit, element }.

packages/physics-electric/src/index.ts:24

GND const

const GND: "0"

A circuit network solved by modified nodal analysis. Capacitors and inductors are integrated by the trapezoid rule (second order, no numerical damping), every element that is not linear is solved by Newton iterations on the whole network, and a replay of the same circuit with the same step is identical. Node '0' is ground.

packages/physics-electric/src/circuit.ts:6

MU0 const

const MU0: number

packages/physics-electric/src/circuit.ts:8

SIGMA const

const SIGMA: 5.670374419e-8

packages/physics-electric/src/circuit.ts:7