main.js entry 4.6 KB
Copy main.js
SimKit.define({
title: "Parallel resistors: shared voltage, divided current",
dt: 2e-5, speed: 0.05, height: 400,
params: {
V: { label: "Source voltage", unit: "V", min: 1, max: 24, value: 12, step: 0.5 },
n: { label: "Number of resistors", unit: "", min: 1, max: 6, value: 3, step: 1 },
R1: { label: "R1", unit: "Ω", min: 10, max: 10000, value: 6, log: true },
R2: { label: "R2", unit: "Ω", min: 10, max: 10000, value: 12, log: true },
R3: { label: "R3", unit: "Ω", min: 10, max: 10000, value: 100, log: true },
R4: { label: "R4", unit: "Ω", min: 10, max: 10000, value: 220, log: true },
R5: { label: "R5", unit: "Ω", min: 10, max: 10000, value: 470, log: true },
R6: { label: "R6", unit: "Ω", min: 10, max: 10000, value: 1000, log: true },
pulse: { label: "Pulsating source on (1) / constant DC (0)", unit: "", min: 0, max: 1, value: 1, step: 1 },
f: { label: "Square-wave frequency", unit: "Hz", min: 1, max: 200, value: 10, log: true }
},
_rKeys: ["R1", "R2", "R3", "R4", "R5", "R6"],
init(P, S) {
const c = new SimKit.Circuit();
const a = c.node();
S.c = c; S.a = a;
S.src = c.V(a, 0, 0);
S.resistors = [];
this._syncResistors(P, S);
},
_syncResistors(P, S) {
const n = Math.round(P.n);
while (S.resistors.length < n) {
const k = S.resistors.length;
S.resistors.push(S.c.R(S.a, 0, P[this._rKeys[k]]));
}
while (S.resistors.length > n) {
const r = S.resistors.pop();
if (r && r.remove) r.remove();
}
},
step(dt, P, S, t) {
this._syncResistors(P, S);
S.src.value = (P.pulse < 0.5 || Math.floor(t * P.f * 2) % 2 === 0) ? P.V : 0;
for (let k = 0; k < S.resistors.length; k++) {
S.resistors[k].value = P[this._rKeys[k]];
}
S.c.step(dt);
},
draw(g, W, H, P, S) {
const sch = SimKit.sch, v = S.c.v, vm = Math.max(P.V, 1);
const c = SimKit.voltColor(v[S.a], vm);
const n = S.resistors.length;
const padL = 60, padR = 40;
const x0 = padL;
const xRight = W - padR;
const yt = 62;
const yb = H - 110;
const ym = (yt + yb) / 2;
const span = xRight - x0;
const dx = span / (n + 1);
const xs = [];
for (let k = 0; k < n; k++) xs.push(x0 + (k + 1) * dx);
sch.wire(g, x0, yt, xRight, yt, c);
sch.wire(g, x0, yb, xRight, yb, "#555");
sch.vsource(g, x0, yb, x0, yt, c);
sch.ground(g, x0, yb);
const compact = dx < 105;
const colX = dx * 0.5;
const lx = (x) => x + Math.min(colX, 52);
for (let k = 0; k < n; k++) {
const x = xs[k];
const r = S.resistors[k];
const rv = P[this._rKeys[k]];
sch.resistor(g, x, yt, x, yb, c);
const tx = lx(x);
if (compact) {
sch.label(g, "R" + (k + 1), tx, ym - 22);
sch.label(g, SimKit.fmt(rv, "Ω"), tx, ym - 6);
sch.label(g, SimKit.fmt(r.i, "A"), tx, ym + 14, "#d03030");
} else {
sch.label(g, "R" + (k + 1) + " = " + SimKit.fmt(rv, "Ω"), tx, ym - 8);
sch.label(g, "I" + (k + 1) + " = " + SimKit.fmt(r.i, "A"), tx, ym + 12, "#d03030");
}
}
const it = S.resistors.reduce((s, r) => s + r.i, 0);
let invRt = 0;
for (let k = 0; k < n; k++) invRt += 1 / P[this._rKeys[k]];
const rt = 1 / invRt;
const cx = (x0 + xRight) / 2;
sch.label(g, "V = " + SimKit.fmt(v[S.a], "V") + " on every branch", cx, yt - 28, "#5aa0ff");
const longForm = n > 3 || W < 560;
const sumI = longForm
? "ΣI"
: Array.from({ length: n }, (_, k) => "I" + (k + 1)).join(" + ");
const sumInv = longForm
? "Σ1/R"
: Array.from({ length: n }, (_, k) => "1/R" + (k + 1)).join(" + ");
sch.label(g, "IT = " + sumI + " = " + SimKit.fmt(it, "A"), cx, yb + 38, "#d03030");
sch.label(g, "1/RT = " + sumInv, cx, yb + 62, "#444");
sch.label(g, "RT = " + SimKit.fmt(rt, "Ω"), cx, yb + 84, "#444");
},
scope: {
span: P => 4 / P.f, unit: "mA",
traces: [
{ label: "I1", color: "#5aa0ff", get: S => (S.resistors[0] ? S.resistors[0].i * 1000 : 0) },
{ label: "I2", color: "#ff6060", get: S => (S.resistors[1] ? S.resistors[1].i * 1000 : 0) },
{ label: "I3", color: "#3cb371", get: S => (S.resistors[2] ? S.resistors[2].i * 1000 : 0) },
{ label: "I4", color: "#e8a020", get: S => (S.resistors[3] ? S.resistors[3].i * 1000 : 0) },
{ label: "I5", color: "#a060e0", get: S => (S.resistors[4] ? S.resistors[4].i * 1000 : 0) },
{ label: "I6", color: "#20c0c0", get: S => (S.resistors[5] ? S.resistors[5].i * 1000 : 0) },
{ label: "IT", color: "#888888", get: S => S.resistors.reduce((s, r) => s + r.i, 0) * 1000 }
]
}
});