feat: 开源母线电容计算器并接入独立自动部署

整理 Buck 与 PMSM 完整源码、离线构建、校核测试和使用说明,采用 MIT 许可。
增加页首开源链接,支持推送 main 后自动测试、构建并发布到原计算器网址。
This commit is contained in:
2026-11-27 04:38:28 +08:00
commit 73ab36f7ff
75 changed files with 8300 additions and 0 deletions
+221
View File
@@ -0,0 +1,221 @@
'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const vm = require('node:vm');
const core = require('../src/buck-calculator-engine.js');
const supply = require('../src/motor-bus-supply.js');
const TAU = 2 * Math.PI;
function near(actual, expected, tolerance = 1e-10) {
assert.ok(Math.abs(actual - expected) <= tolerance * Math.max(1, Math.abs(expected)), `${actual} != ${expected}`);
}
function current(re = [2], im = re.map(() => 0), extra = {}) {
const f0 = extra.f0 === undefined ? 100 : extra.f0;
const k = extra.k === undefined ? Uint32Array.from(re, (_, i) => i + 1) : Uint32Array.from(extra.k);
return { dc: 0, f0, k, freq: Float64Array.from(k, n => n * f0),
re: Float64Array.from(re), im: Float64Array.from(im), ...extra };
}
function densePp(input, branch, samples = 65536) {
const lines = Array.from(input.re, (ir, i) => {
const z = supply.impedance(branch, input.k[i] * input.f0);
return { n: input.k[i], re: -(z.re * ir - z.im * input.im[i]), im: -(z.re * input.im[i] + z.im * ir) };
});
let min = Infinity, max = -Infinity;
for (let j = 0; j < samples; j++) {
let voltage = 0;
for (const line of lines) {
const angle = TAU * line.n * j / samples;
voltage += line.re * Math.cos(angle) - line.im * Math.sin(angle);
}
min = Math.min(min, voltage); max = Math.max(max, voltage);
}
return max - min;
}
test('端口阻抗与旧内核一致;C=0 和 DC 是有效边界', () => {
for (const branch of [{ R: 0.05, Ls: 1e-6, C: 470e-6 }, { R: 0.2, Ls: 0, C: 1e-3 }]) {
for (const f of [0, 10, 1000, 20000, 200000]) {
const actual = supply.impedance(branch, f), expected = core.impedance(branch.R, branch.Ls, branch.C, TAU * f);
near(actual.re, expected.re); near(actual.im, expected.im); near(actual.amp, expected.amp);
}
}
const zeroC = supply.impedance({ R: 0.2, Ls: 3e-6, C: 0 }, 20000);
near(zeroC.re, 0.2); near(zeroC.im, TAU * 20000 * 3e-6);
const dc = supply.transfer({ R: 0.2, Ls: 3e-6, C: 1e-3 }, 0);
assert.equal(dc.sourceRe, 1); assert.equal(dc.capRe, 0);
});
test('支路传递等于解析 H;电容吸收方向 H−1;阻抗负号保持', () => {
const branch = { R: 0.05, Ls: 1e-6, C: 470e-6 };
for (const f of [1, 100, 5000, 200000]) {
const w = TAU * f, a = 1 - w * w * branch.Ls * branch.C, b = w * branch.R * branch.C;
const H = supply.transfer(branch, f), den = a * a + b * b;
near(H.sourceRe, a / den); near(H.sourceIm, -b / den);
near(H.capRe, H.sourceRe - 1); near(H.capIm, H.sourceIm);
}
const result = supply.pointMetrics(current([2]), { R: 0.5, Ls: 0, C: 0 }, { waveform: true });
near(result.wave.voltage[0], -1); // I=+2cos,纯R电压−1cos。
});
test('单正弦 pp=2|Z I|,峰值相量仅分半,不再双倍', () => {
const input = current([3], [4]);
const branch = { R: 0.2, Ls: 2e-6, C: 1000e-6 };
const result = supply.pointMetrics(input, branch, { waveform: true });
const exact = 10 * supply.impedance(branch, 100).amp;
assert.ok(result.ppLower <= exact + 1e-12);
assert.ok(result.ppUpper >= exact - 1e-12);
near(result.ppSample, densePp(input, branch, result.M), 1e-12);
assert.ok(result.fftImagMax < 1e-12);
near(result.wave.time.at(-1), 0.01); near(result.wave.voltage[0], result.wave.voltage.at(-1));
assert.equal(result.scope, 'finite-band'); assert.equal(result.tailKnown, false);
assert.equal(result.totalPpUpper, null); assert.equal(result.rmsTruncated, true);
});
test('有符号再生/静止 DC:source含dc²,cap不含,纹波不加DC', () => {
const result = supply.pointMetrics(current([], [], { dc: -7, rpm: 0, angle: 0 }), { R: 0.1, Ls: 0, C: 0 });
near(result.sourceRms, 7); near(result.demandRms, 7); assert.equal(result.capRms, 0);
near(result.dcDrop, -0.7); near(result.ppSample, 0); near(result.ppUpper, 0);
assert.equal(result.lineCount, 0);
});
test('有限频带 RMS 与分支解析相量及 Parseval 一致', () => {
const input = current([3, -0.4, 1.2], [4, 1, -0.7], { dc: -2.3 });
const branch = { R: 0.1, Ls: 4e-6, C: 330e-6 };
let srcSq = input.dc ** 2, capSq = 0, demandSq = input.dc ** 2;
for (let i = 0; i < input.re.length; i++) {
const H = supply.transfer(branch, input.freq[i]);
const ampSq = input.re[i] ** 2 + input.im[i] ** 2;
srcSq += (H.sourceRe ** 2 + H.sourceIm ** 2) * ampSq / 2;
capSq += (H.capRe ** 2 + H.capIm ** 2) * ampSq / 2;
demandSq += ampSq / 2;
}
const result = supply.pointMetrics(input, branch);
near(result.sourceRms, Math.sqrt(srcSq)); near(result.capRms, Math.sqrt(capSq)); near(result.demandRms, Math.sqrt(demandSq));
assert.equal('wave' in result, false);
});
test('保峰谷的显示采样与细网格峰峰值一致', () => {
const input = current([1.5, -0.3, 0.2], [0.7, 0.8, 0.4], { k: [1, 159, 211], f0: 10 });
const result = supply.pointMetrics(input, { R: 0.1, Ls: 1e-6, C: 2e-6 }, { waveform: 'extrema', wavePoints: 26 });
const {time, voltage, period, extremaPreserved} = result.wave;
assert.ok(extremaPreserved && time.length <= 26);
assert.equal(time[0], 0); assert.equal(time.at(-1), period);
for (let i = 1; i < time.length; i++) assert.ok(time[i] >= time[i-1]);
near(Math.max(...voltage) - Math.min(...voltage), result.ppSample);
near(voltage[0], voltage.at(-1));
});
test('同工况相位完整保留:不能把幅值三角和或跨工况包络当波形', () => {
const branch = { R: 1, Ls: 0, C: 0 };
const a = current([1, 0.5], [0, 0]);
const b = current([1, 0], [0, 0.5]);
const ra = supply.pointMetrics(a, branch), rb = supply.pointMetrics(b, branch);
near(ra.ppSample, 2.25, 0.01);
assert.ok(Math.abs(ra.ppSample - rb.ppSample) > 0.2);
near(ra.demandRms, rb.demandRms);
assert.ok(ra.ppSample < 2 * (1 + 0.5));
const scan = supply.evaluate([a, b], branch);
near(scan.maxPpSample, Math.max(ra.ppSample, rb.ppSample));
});
test('稀疏高次 k 按原频率放入 IFFT,有限网格误差包含密网格极值', () => {
const input = current([1, -0.8, 0.3], [0.7, 0.4, -0.1], { k: Uint32Array.from([1, 7, 19]), f0: 125 });
const branch = { R: 0.13, Ls: 6e-6, C: 47e-6 };
const coarse = supply.pointMetrics(input, branch), fine = supply.pointMetrics(input, branch, { gridFactor: 256 });
assert.equal(coarse.maxK, 19); assert.equal(coarse.maxHz, 2375);
assert.ok(coarse.ppLower <= fine.ppSample + 1e-12);
assert.ok(coarse.ppUpper >= fine.ppSample - 1e-12);
near(fine.ppSample, densePp(input, branch, fine.M), 1e-11);
});
test('C0 感性源只给有限频带结果;供电RMS=需求RMS,电容RMS=0', () => {
const input = current([3, 2], [1, -0.5], { dc: 4 });
const result = supply.pointMetrics(input, { R: 0.05, Ls: 30e-6, C: 0 }, { target: 1e9 });
near(result.sourceRms, result.demandRms); assert.equal(result.capRms, 0);
assert.equal(result.c0Inductive, true); assert.match(result.notice, /尖峰/);
assert.equal(result.finiteBandStatus, 'within-target');
assert.equal(result.allBandwidthStatus, 'unknown'); assert.equal(result.totalPpUpper, null);
assert.equal('pass' in result, false);
});
test('evaluate 极值分别跨工况汇总、最坏工况轻量且不相位拼接', () => {
const cases = [current([1], [0], { rpm: 10, iqPeak: 2, dc: 8, angle: 0 }),
current([4], [0], { rpm: 20, iqPeak: -4, dc: -2, angle: 0.2 })];
const branch = { R: 0.1, Ls: 0, C: 0 };
const result = supply.evalCandidate(cases, branch, 1, { waveform: true });
assert.equal(result.worstIndex, 1); assert.equal(result.worstCase.rpm, 20);
assert.equal(result.worstSourceIndex, 0); near(result.maxAbsDcDropV, 0.8);
near(result.maxPpSample, Math.max(...result.results.map(r => r.ppSample)));
near(result.maxPpLower, Math.max(...result.results.map(r => r.ppLower)));
near(result.maxPpUpper, Math.max(...result.results.map(r => r.ppUpper)));
near(result.maxSourceRms, Math.max(...result.results.map(r => r.sourceRms)));
near(result.maxCapRms, 0);
assert.equal(result.finiteBandStatus, 'within-target');
assert.equal('re' in result.worstCase, false); assert.equal('im' in result.worstCase, false);
assert.equal('wave' in result.results[0], false); assert.ok(result.results[1].wave);
assert.equal(result.worstPoint, result.results[1]);
});
test('每次 evaluate 读取最新工况数组,不复用旧谱/旧 DC', () => {
const input = current([1], [0]), branch = { R: 1, Ls: 0, C: 0 };
const old = supply.evaluate([input], branch);
input.re[0] = 4; input.dc = -3;
const fresh = supply.evaluate([input], branch);
near(old.maxPpSample, 2); near(fresh.maxPpSample, 8);
near(fresh.maxSourceRms, Math.sqrt(17)); near(fresh.maxAbsDcDropV, 3);
});
test('目标单位V,有限频带比较保留 uncertain;空扫描不是通过', () => {
const input = current(), branch = { R: 1, Ls: 0, C: 0 };
assert.equal(supply.evalCandidate(input, branch, 5).finiteBandStatus, 'within-target');
assert.equal(supply.evalCandidate(input, branch, 1).finiteBandStatus, 'exceeds-target');
assert.equal(supply.evalCandidate(input, branch, 4).finiteBandStatus, 'uncertain');
const empty = supply.evaluate([], branch, { target: 1 });
assert.equal(empty.finiteBandStatus, 'not-evaluated'); assert.equal(empty.maxPpSample, null);
assert.equal(empty.maxPpUpper, null); assert.equal(empty.worstCase, null);
});
test('严格拒绝非有限值、不共周期谱、重复k及超大网格', () => {
const input = current(), branch = { R: 0.1, Ls: 0, C: 0 };
for (const candidate of [{ ...branch, R: 0 }, { ...branch, Ls: -1 }, { ...branch, C: NaN }]) {
assert.throws(() => supply.pointMetrics(input, candidate), /必须|不能/);
}
assert.throws(() => supply.impedance(branch, -1), /不能/);
assert.throws(() => supply.pointMetrics({ ...input, re: [Infinity] }, branch), /有限/);
assert.throws(() => supply.pointMetrics({ ...input, freq: [101] }, branch), /k\*f0/);
assert.throws(() => supply.pointMetrics(current([1, 1], [0, 0], { k: [1, 1] }), branch), /严格递增/);
assert.throws(() => supply.pointMetrics({ ...input, duration: 1 }, branch), /不一致/);
assert.throws(() => supply.pointMetrics(input, branch, { gridFactor: 4 }), /小于 8/);
assert.throws(() => supply.pointMetrics(input, branch, { gridFactor: 1e9 }), /网格过大/);
});
test('真实电机 rotating/locked/q0 工况接口一致,C0 Parseval 与内核 bandRms 对齐', () => {
const motor = require('../src/motor-bus-engine.js');
const points = [motor.caseSpectrum({}, { fe: 312.5, iqPeak: Math.SQRT2 * 13.3 }),
motor.caseSpectrum({}, { fe: 312.5, iqPeak: -Math.SQRT2 * 13.3 }),
motor.caseSpectrum({}, { fe: 312.5, iqPeak: 0 }),
motor.caseSpectrum({}, { fe: 0, iqPeak: 49.04, angle: 0 })];
const result = supply.evaluate(points, { R: 0.05, Ls: 1e-6, C: 0 }, { waveform: true });
assert.equal(result.caseCount, 4);
assert.ok(points[1].dc < 0); assert.ok(points[2].bandRms > 0);
for (let i = 0; i < points.length; i++) {
const expected = Math.hypot(points[i].dc, points[i].bandRms);
near(result.results[i].sourceRms, expected, 1e-11);
near(result.results[i].demandRms, expected, 1e-11);
assert.equal(result.results[i].capRms, 0);
near(result.results[i].period, points[i].duration);
}
const withCap = supply.evaluate(points, { R: 0.05, Ls: 1e-6, C: 1000e-6 }, { target: 0.12 });
assert.ok(withCap.maxPpSample > 0); assert.ok(withCap.maxCapRms > 0);
assert.equal(withCap.allBandwidthStatus, 'unknown');
});
test('UMD 浏览器全局及别名可用', () => {
const context = { BuckRLC: core };
vm.runInNewContext(fs.readFileSync(require.resolve('../src/motor-bus-supply.js'), 'utf8'), context);
assert.equal(typeof context.MotorBusSupply.evaluate, 'function');
assert.equal(context.MotorBusSupply.z, context.MotorBusSupply.impedance);
assert.equal(context.MotorBusSupply.H, context.MotorBusSupply.transfer);
const result = context.MotorBusSupply.pointMetrics(current(), { R: 1, Ls: 0, C: 0 });
near(result.ppSample, 4);
});