diff options
| author | LalleSX | 2026-10-24 04:35:56 +0200 |
|---|---|---|
| committer | LalleSX | 2026-10-24 04:35:56 +0200 |
| commit | c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5 (patch) | |
| tree | 8fcbbfffb6a34af73697a703f77e6b82f191b91f /src/physics/models.test.ts | |
| download | 3D-Electrophysics-c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5.tar.gz 3D-Electrophysics-c4c1c840f8761ea45a3d2dcc912a59d8ced78cb5.zip | |
init
Diffstat (limited to '')
| -rw-r--r-- | src/physics/models.test.ts | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/physics/models.test.ts b/src/physics/models.test.ts new file mode 100644 index 0000000..3ff6487 --- /dev/null +++ b/src/physics/models.test.ts @@ -0,0 +1,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); + }); +}); |