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
+44
View File
@@ -0,0 +1,44 @@
'use strict';
const test=require('node:test'),assert=require('node:assert/strict');
const chart=require('../src/motor-bus-chart.js'),core=require('../src/buck-calculator-engine.js');
const op=core.operating({vin:24,drop:12,iavg:5,fsw:20000,L:75e-6,target:.12});
const points=Array.from({length:1024},(_,i)=>{const h=core.harmonic(op,i+1);return {f:(i+1)*op.fsw,ipk:h.amp,zRef:op.target/(2*h.amp),mode:'drive'};});
test('all four display densities evaluate three curves on the same exact grid and retain all 1024 real lines',()=>{
for(const sampleCount of [32768,65536,131072,262144]){
let evaluations=0;const peakHz=4321.123456;
const curves=chart.sampleCurves({minHz:1000,maxHz:2.4e7,sampleCount,points,peakHz,targetVpp:.12,referenceFromCurrent:true,
network:f=>{evaluations++;return 1/Math.hypot(1,f/2000);}});
for(const values of [curves.frequency,curves.impedance,curves.reference,curves.currents[0].values])assert.equal(values.length,sampleCount);
assert.equal(evaluations,sampleCount);
for(let i=1;i<sampleCount;i++)assert.ok(curves.frequency[i]>curves.frequency[i-1]);
const indices=new Map(Array.from(curves.frequency,(f,i)=>[f,i]));assert.ok(indices.has(peakHz));
for(const p of points){const index=indices.get(p.f);assert.notEqual(index,undefined);assert.equal(curves.currents[0].values[index],p.ipk);assert.ok(Math.abs(curves.reference[index]/p.zRef-1)<1e-12);}
assert.ok(Number.isNaN(curves.currents[0].values[0]));
assert.equal(points.length,1024);
}
});
test('shape-preserving interpolation keeps real zeros, local extrema, modes and unsupported frequency gaps',()=>{
const f=chart.frequencyGrid(1,100,1000,[2,4,8,16]);
const values=chart.interpolate([{f:2,value:0},{f:4,value:10},{f:8,value:0},{f:16,value:5}],f);
for(let i=0;i<f.length;i++)if(f[i]<2||f[i]>16)assert.ok(Number.isNaN(values[i]));else assert.ok(values[i]>=0&&values[i]<=10);
for(const [frequency,value]of [[2,0],[4,10],[8,0],[16,5]])assert.equal(values[f.indexOf(frequency)],value);
const curves=chart.sampleCurves({minHz:1,maxHz:100,sampleCount:1024,targetVpp:.2,referenceFromCurrent:true,network:()=>1,
points:[{f:2,ipk:0,mode:'drive'},{f:4,ipk:10,mode:'drive'},{f:8,ipk:0,mode:'drive'},{f:4,ipk:3,mode:'regen'},{f:8,ipk:2,mode:'regen'}]});
assert.equal(curves.currents.length,2);assert.equal(curves.reference[curves.frequency.indexOf(2)],Infinity);
});
test('canvas path reduction preserves narrow extrema and non-finite breaks',()=>{
const f=chart.frequencyGrid(1,100,10000),v=new Float64Array(f.length);v.fill(1);v[1234]=100;v[1235]=.001;v[5000]=Infinity;
const indices=chart.pathIndices(f,v,100);
assert.ok(indices.includes(1234));assert.ok(indices.includes(1235));assert.ok(indices.includes(-1));assert.ok(!indices.includes(5000));assert.ok(indices.length<1000);
});
test('the actual renderer uses dense curves and reuses them for hover-only redraws',()=>{
const noop=()=>{},ctx=new Proxy({measureText:s=>({width:s.length*6})},{get:(t,k)=>k in t?t[k]:noop});
const canvas={clientWidth:900,clientHeight:360,style:{},dataset:{},getContext:()=>ctx,getBoundingClientRect:()=>({width:900,height:360})};
let calls=0;const options={minHz:1000,maxHz:2.4e7,sampleCount:32768,points,dataKey:'same-case',branch:{R:.05,Ls:1e-6,C:.0015},referenceFromCurrent:true,targetVpp:.12,network:f=>{calls++;return 1/(1+f/1000);}};
const first=chart.draw(canvas,options);assert.equal(calls,32768);assert.deepEqual(first.sampleCounts,{frequency:32768,impedance:32768,reference:32768,current:32768});
assert.equal(canvas.dataset.referenceSamples,'32768');chart.draw(canvas,{...options,hoverHz:40000});assert.equal(calls,32768);
});