main.js entry 5.0 KB
Copy main.js
SimKit.define({
title: "Series resistors: shared current, divided voltage",
dt: 2e-5, speed: 0.05, height: 380,
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: 100, log: true },
R2: { label: "R2", unit: "Ω", min: 10, max: 10000, value: 220, log: true },
R3: { label: "R3", unit: "Ω", min: 10, max: 10000, value: 470, log: true },
R4: { label: "R4", unit: "Ω", min: 10, max: 10000, value: 1000, log: true },
R5: { label: "R5", unit: "Ω", min: 10, max: 10000, value: 2200, log: true },
R6: { label: "R6", unit: "Ω", min: 10, max: 10000, value: 4700, 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) {
this._build(P, S);
},
// Перебудова кола: вузол k — це точка ПЕРЕД резистором k, вузол n — земля (0)
_build(P, S) {
const n = Math.round(P.n);
const c = new SimKit.Circuit();
const nodes = [];
for (let k = 0; k < n; k++) nodes.push(c.node());
nodes.push(0);
S.c = c;
S.nodes = nodes;
S.nBuilt = n;
S.src = c.V(nodes[0], 0, 0);
S.resistors = [];
for (let k = 0; k < n; k++) {
S.resistors.push(c.R(nodes[k], nodes[k + 1], P[this._rKeys[k]]));
}
},
step(dt, P, S, t) {
if (Math.round(P.n) !== S.nBuilt) this._build(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 n = S.resistors.length;
const nodeV = (k) => (k >= n ? 0 : v[S.nodes[k]]);
const col = (k) => SimKit.voltColor(nodeV(k), vm);
// ── Геометрія ─────────────────────────────────────────────
const padL = 60, padR = 40;
const x0 = padL;
const xRight = W - padR;
const yt = 80;
const yb = H - 110;
const ym = (yt + yb) / 2;
const seg = (xRight - x0) / n;
const rw = Math.min(70, seg * 0.6);
const compact = seg < 105;
sch.wire(g, x0, yb, xRight, yb, "#555");
sch.vsource(g, x0, yb, x0, yt, col(0));
sch.ground(g, x0, yb);
let xPrev = x0;
for (let k = 0; k < n; k++) {
const xc = x0 + (k + 0.5) * seg;
const xa = xc - rw / 2;
const xb = xc + rw / 2;
const r = S.resistors[k];
const rv = P[this._rKeys[k]];
const vk = nodeV(k) - nodeV(k + 1);
sch.wire(g, xPrev, yt, xa, yt, col(k));
sch.resistor(g, xa, yt, xb, yt, col(k));
xPrev = xb;
if (compact) {
sch.label(g, "R" + (k + 1), xc, yt - 36);
sch.label(g, SimKit.fmt(rv, "Ω"), xc, yt - 20);
} else {
sch.label(g, "R" + (k + 1) + " = " + SimKit.fmt(rv, "Ω"), xc, yt - 24);
}
sch.label(g, (compact ? "" : "V" + (k + 1) + " = ") + SimKit.fmt(vk, "V"), xc, yt + 26, "#5aa0ff");
}
sch.wire(g, xPrev, yt, xRight, yt, col(n));
sch.wire(g, xRight, yt, xRight, yb, col(n));
const i = S.resistors[0] ? S.resistors[0].i : 0;
let rt = 0;
for (let k = 0; k < n; k++) rt += P[this._rKeys[k]];
const cx = (x0 + xRight) / 2;
sch.label(g, "I = " + SimKit.fmt(i, "A") + " through every resistor", cx, ym, "#d03030");
const longForm = n > 3 || W < 560;
const sumV = longForm
? "ΣV"
: Array.from({ length: n }, (_, k) => "V" + (k + 1)).join(" + ");
const sumR = longForm
? "ΣR"
: Array.from({ length: n }, (_, k) => "R" + (k + 1)).join(" + ");
sch.label(g, "V = " + sumV + " = " + SimKit.fmt(nodeV(0), "V"), cx, yb + 38, "#5aa0ff");
sch.label(g, "RT = " + sumR + " = " + SimKit.fmt(rt, "Ω"), cx, yb + 62, "#444");
sch.label(g, "I = V / RT = " + SimKit.fmt(i, "A"), cx, yb + 84, "#d03030");
},
scope: {
span: P => 4 / P.f, unit: "V",
traces: [
{ label: "V1", color: "#5aa0ff", get: S => (S.resistors[0] ? S.c.v[S.nodes[0]] - S.c.v[S.nodes[1]] : 0) },
{ label: "V2", color: "#ff6060", get: S => (S.resistors[1] ? S.c.v[S.nodes[1]] - S.c.v[S.nodes[2]] : 0) },
{ label: "V3", color: "#3cb371", get: S => (S.resistors[2] ? S.c.v[S.nodes[2]] - S.c.v[S.nodes[3]] : 0) },
{ label: "V4", color: "#e8a020", get: S => (S.resistors[3] ? S.c.v[S.nodes[3]] - S.c.v[S.nodes[4]] : 0) },
{ label: "V5", color: "#a060e0", get: S => (S.resistors[4] ? S.c.v[S.nodes[4]] - S.c.v[S.nodes[5]] : 0) },
{ label: "V6", color: "#20c0c0", get: S => (S.resistors[5] ? S.c.v[S.nodes[5]] - S.c.v[S.nodes[6]] : 0) },
{ label: "V", color: "#888888", get: S => S.c.v[S.nodes[0]] }
]
}
});