import {createHash} from "node:crypto"; import {readFile} from "node:fs/promises"; import assert from "node:assert/strict"; import { BREWSTER_ANGLE_DEGREES, GLASS_INTERFACE, glassInterface } from "./fresnel-interface.mjs"; const radiansPerDegree = Math.PI / 180; const tolerance = 2e-15; const polarizations = ["s", "p", "unpolarized"]; function near(actual, expected, label, limit = tolerance) { assert.ok(Math.abs(actual - expected) <= limit, label + ": " + actual + " vs " + expected); } assert.deepEqual(GLASS_INTERFACE, { vacuumWavelengthNm: 550, incidentIndex: 1, transmittedIndex: 1.5 }); for (const invalidAngle of [null, undefined, NaN, Infinity, -Infinity, "45", -1, 91]) { assert.throws(() => glassInterface(invalidAngle), RangeError); } const normal = glassInterface(0); assert.equal(normal.refractionDegrees, 0); for (const polarization of polarizations) { near(normal[polarization].reflectance, 1 / 25, "normal R " + polarization); near(normal[polarization].transmittance, 24 / 25, "normal T " + polarization); } const brewster = glassInterface(BREWSTER_ANGLE_DEGREES); near(BREWSTER_ANGLE_DEGREES, 56.309932474020215, "Brewster angle", 1e-12); near(BREWSTER_ANGLE_DEGREES + brewster.refractionDegrees, 90, "orthogonal rays", 1e-12); near(brewster.s.reflectance, 25 / 169, "Brewster s power"); near(brewster.s.transmittance, 144 / 169, "Brewster s transmission"); near(brewster.p.reflectance, 0, "Brewster p power"); near(brewster.p.transmittance, 1, "Brewster p transmission"); near(brewster.unpolarized.reflectance, 25 / 338, "Brewster unpolarized power"); assert.ok(glassInterface(BREWSTER_ANGLE_DEGREES - 1).p.reflectance > brewster.p.reflectance); assert.ok(glassInterface(BREWSTER_ANGLE_DEGREES + 1).p.reflectance > brewster.p.reflectance); const grazing = glassInterface(90); near(grazing.refractionDegrees, 41.810314895778596, "grazing refraction", 1e-12); for (const polarization of polarizations) { assert.deepEqual(grazing[polarization], { reflectance: 1, transmittance: 0 }); } const obliqueChecks = [15, 30, 45, 60, 75]; for (const degrees of obliqueChecks) { const incidence = degrees * radiansPerDegree; const refraction = Math.asin(Math.sin(incidence) * 2 / 3); const result = glassInterface(degrees); near(result.refractionDegrees, refraction / radiansPerDegree, "Snell refraction", 1e-12); // Angle-only identities provide an oblique check without the production // implementation's index-times-cosine amplitude factors. const expectedS = (Math.sin(incidence - refraction) / Math.sin(incidence + refraction)) ** 2; const expectedP = (Math.tan(incidence - refraction) / Math.tan(incidence + refraction)) ** 2; near(result.s.reflectance, expectedS, "oblique Rs"); near(result.p.reflectance, expectedP, "oblique Rp"); near(result.s.transmittance, 1 - expectedS, "oblique Ts"); near(result.p.transmittance, 1 - expectedP, "oblique Tp"); } let maxConservationError = 0; for (let step = 0; step <= 900; step++) { const point = glassInterface(step / 10); for (const polarization of polarizations) { const { reflectance, transmittance } = point[polarization]; assert.ok(reflectance >= 0 && reflectance <= 1 + tolerance); assert.ok(transmittance >= 0 && transmittance <= 1 + tolerance); near(reflectance + transmittance, 1, "energy conservation for " + polarization); maxConservationError = Math.max(maxConservationError, Math.abs(reflectance + transmittance - 1)); } } const moduleSha256=createHash("sha256").update(await readFile(new URL("./fresnel-interface.mjs",import.meta.url))).digest("hex"); console.log(JSON.stringify({ moduleSha256, settings: GLASS_INTERFACE, brewsterAngleDegrees: BREWSTER_ANGLE_DEGREES, checkedGrid: { totalAngles: 901, stepDegrees: 0.1, polarizations, maxConservationError }, independentObliqueAngles: obliqueChecks, samplePoints: [0, 30, 45, BREWSTER_ANGLE_DEGREES, 60, 89.9, 90].map(glassInterface), }, null, 2));