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
|
import { describe, expect, it } from 'vitest';
import { electricField } from './electricField';
import { potential, forceOn, pairEnergy, gaussianFlux, capacitor, conductor, semiconductor } from './models';
// Fixtures use hand-calculated SI results, independent of model implementations.
describe('charge, potential and energy', () => {
const source = [{ x: 0, y: 0, z: 0, coulombs: 2e-9 }];
it('gives scalar potential tending to zero at infinity', () => {
expect(potential({ x: 2, y: 0, z: 0 }, source)).toBeCloseTo(8.98755, 4);
expect(potential({ x: 1e9, y: 0, z: 0 }, source)).toBeCloseTo(0, 6);
});
it('makes the field the negative spatial gradient of potential', () => {
const h = 1e-4;
const numerical = -(potential({ x: 2 + h, y: 0, z: 0 }, source) - potential({ x: 2 - h, y: 0, z: 0 }, source)) / (2 * h);
expect(electricField({ x: 2, y: 0, z: 0 }, source).x).toBeCloseTo(numerical, 5);
});
it('reverses force for a negative test charge', () => {
expect(forceOn({ x: 2, y: 0, z: 0, coulombs: -3e-9 }, source).x).toBeCloseTo(-13.48132768845e-9, 16);
});
it('gives negative interaction energy for opposite charges', () => {
expect(pairEnergy(2e-9, -3e-9, 2)).toBeCloseTo(-26.9626553769e-9, 16);
});
it('marks potential at a charge as undefined instead of inventing a finite value', () => {
expect(potential({ x: 0, y: 0, z: 0 }, source)).toBeNaN();
expect(() => pairEnergy(1, 1, 0)).toThrow(RangeError);
});
});
describe('Gauss law', () => {
it('counts only enclosed charge, including its sign', () => {
const charges = [{ x: 0, y: 0, z: 0, coulombs: -2e-9 }, { x: 3, y: 0, z: 0, coulombs: 5e-9 }];
expect(gaussianFlux(charges, 2)).toBeCloseTo(-225.8818, 3);
expect(gaussianFlux(charges, 4)).toBeCloseTo(338.8227, 3);
});
it('does not depend on surface size when enclosed charge is unchanged', () => {
const charges = [{ x: 0, y: 0, z: 0, coulombs: 1e-9 }];
expect(gaussianFlux(charges, 1)).toBe(gaussianFlux(charges, 2));
});
it('marks charge on the Gaussian surface as undefined', () => {
expect(gaussianFlux([{ x: 2, y: 0, z: 0, coulombs: 1e-9 }], 2)).toBeNaN();
});
});
describe('parallel plate capacitor', () => {
it('converts physical geometry into C, Q, E and stored energy', () => {
const c = capacitor({ area: 0.01, gap: 0.001, relativePermittivity: 1, voltage: 10 });
expect(c.capacitance).toBeCloseTo(8.8541878128e-11, 18);
expect(c.charge).toBeCloseTo(8.8541878128e-10, 18);
expect(c.field).toBe(10000);
expect(c.energy).toBeCloseTo(4.4270939064e-9, 17);
});
it('increases free charge at fixed voltage when dielectric is inserted', () => {
const c = capacitor({ area: 0.01, gap: 0.001, relativePermittivity: 4, voltage: 10 });
expect(c.field).toBe(10000);
expect(c.charge).toBeCloseTo(3.54167512512e-9, 17);
expect(c.polarization).toBeCloseTo(2.65625634384e-7, 15);
});
it('reduces voltage and energy at fixed free charge', () => {
const c = capacitor({ area: 0.01, gap: 0.001, relativePermittivity: 4, voltage: 10, isolated: true });
expect(c.charge).toBeCloseTo(8.8541878128e-10, 18);
expect(c.voltage).toBeCloseTo(2.5);
expect(c.field).toBeCloseTo(2500);
expect(c.energy).toBeCloseTo(1.1067734766e-9, 17);
});
it('rejects impossible geometry', () => {
expect(() => capacitor({ area: 0.01, gap: 0, relativePermittivity: 1, voltage: 10 })).toThrow(RangeError);
});
});
describe('current and drift', () => {
it('follows Ohm law and sends electrons opposite conventional current', () => {
const wire = conductor({ voltage: 2, resistivity: 1.68e-8, length: 1, area: 1e-6, carrierDensity: 8.5e28 });
expect(wire.resistance).toBeCloseTo(0.0168, 6);
expect(wire.current).toBeCloseTo(119.047619, 5);
expect(wire.electronDrift).toBeCloseTo(-0.00874160935, 10);
expect(wire.power).toBeCloseTo(238.095238, 5);
});
it('reverses current but keeps dissipated power positive with reversed voltage', () => {
const wire = conductor({ voltage: -2, resistivity: 1.68e-8, length: 1, area: 1e-6, carrierDensity: 8.5e28 });
expect(wire.current).toBeLessThan(0);
expect(wire.electronDrift).toBeGreaterThan(0);
expect(wire.power).toBeGreaterThan(0);
});
});
describe('semiconductor equilibrium at 300 K', () => {
it('has equal intrinsic electrons and holes', () => {
const s = semiconductor({ intrinsicDensity: 1e16, netDoping: 0, electronMobility: 0.135, holeMobility: 0.048 });
expect(s.electrons).toBe(1e16);
expect(s.holes).toBe(1e16);
expect(s.fermiShift).toBe(0);
expect(s.conductivity).toBeCloseTo(0.0002931983, 9);
});
it('conserves mass action and charge neutrality even at heavy donor doping', () => {
const s = semiconductor({ intrinsicDensity: 1e16, netDoping: 1e22, electronMobility: 0.135, holeMobility: 0.048 });
expect(s.electrons / 1e22).toBeCloseTo(1, 10);
expect(s.holes).toBeCloseTo(1e10, 0);
expect(s.electrons * s.holes / 1e32).toBeCloseTo(1, 12);
expect((s.electrons - s.holes) / 1e22).toBeCloseTo(1, 12);
expect(s.fermiShift).toBeCloseTo(0.3571586, 5);
});
it('makes holes majority carriers for acceptor doping', () => {
const s = semiconductor({ intrinsicDensity: 1e16, netDoping: -1e22, electronMobility: 0.135, holeMobility: 0.048 });
expect(s.holes / 1e22).toBeCloseTo(1, 10);
expect(s.electrons).toBeCloseTo(1e10, 0);
expect(s.fermiShift).toBeLessThan(0);
});
});
|