aboutsummaryrefslogtreecommitdiff
path: root/src/physics/electricField.test.ts
blob: 2e01309c14f24ef1b583b466a7e2ddaea70805fb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { describe, expect, it } from 'vitest';
import { electricField, COULOMB_CONSTANT } from './electricField';

describe('Coulomb electric field', () => {
  it('points away from a positive charge with inverse-square magnitude', () => {
    const field = electricField({ x: 2, y: 0, z: 0 }, [{ x: 0, y: 0, z: 0, coulombs: 1e-9 }]);
    expect(field.x).toBeCloseTo(COULOMB_CONSTANT * 1e-9 / 4);
    expect(field.y).toBe(0);
    expect(field.z).toBe(0);
  });
  it('superposes opposite charges in the same direction at the midpoint', () => {
    const field = electricField({ x: 0, y: 0, z: 0 }, [
      { x: -1, y: 0, z: 0, coulombs: 1e-9 },
      { x: 1, y: 0, z: 0, coulombs: -1e-9 },
    ]);
    expect(field.x).toBeCloseTo(2 * COULOMB_CONSTANT * 1e-9);
  });
  it('omits singular samples within the visualization exclusion radius', () => {
    expect(electricField({ x: 0, y: 0, z: 0 }, [{ x: 0, y: 0, z: 0, coulombs: 1e-9 }])).toEqual({ x: 0, y: 0, z: 0 });
  });
});