-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalc-electrical.js
More file actions
6356 lines (5868 loc) · 396 KB
/
Copy pathcalc-electrical.js
File metadata and controls
6356 lines (5868 loc) · 396 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Group A: Electrical calculators (utilities 1 through 11).
//
// Each calculator exports two things:
// - compute(inputs): pure function returning the calculator output object.
// - example: a known test case used by the "Test with example" button and
// by the unit tests, with the expected output.
//
// The calculator views (renderXxx) wire DOM events and use compute().
// All DOM manipulation uses textContent / createElement only.
import {
conductorResistancePerKft,
conductorResistance,
ampacityFromPhysics,
voltageDrop,
threePhasePower,
singlePhasePower,
awgAreaCmils,
awgAreaM2,
awgDiameterInches,
awgToNumber,
} from "./pure-math.js";
import { renderLimitationBanner, getLimitationCopy } from "./limitation-banner.js";
// v18 §7 contract guard: reject a non-finite numeric input. A renderer
// coerces an empty number field to 0 (Number("") === 0), so a NaN or
// Infinity reaching a solver is genuinely unusable (a pasted 1e999, a
// degenerate computed slot); per the spec-v18 §2 output contract the
// solver returns {error} rather than leaking a non-finite output field.
// Generic over the input object, so it needs no per-tile slot list, and
// it inspects only own numeric values (strings/arrays/null pass through).
// Non-exported, so it adds no v14 derivation-corpus row.
const _finiteGuard = (o) => {
if (o && typeof o === "object" && !Array.isArray(o)) {
for (const v of Object.values(o)) {
if (typeof v === "number" && !Number.isFinite(v)) {
return { error: "All numeric inputs must be finite numbers." };
}
}
}
return null;
};
// --- Utility 1: Ohm's Law ---
// dims: in { V: M L^2 T^-3 I^-1, I: I, R: M L^2 T^-3 I^-2, P: M L^2 T^-3 } out: { V: M L^2 T^-3 I^-1, I: I, R: M L^2 T^-3 I^-2, P: M L^2 T^-3 }
export function computeOhmsLaw({ V, I, R, P }) {
const known = [V, I, R, P].filter((x) => x !== null && x !== undefined && Number.isFinite(x));
if (known.length < 2) return { error: "Provide any two of V, I, R, P." };
const have = { V, I, R, P };
const out = { ...have };
// Iteratively derive missing values.
for (let i = 0; i < 4; i++) {
if (out.V === null && out.I !== null && out.R !== null) out.V = out.I * out.R;
if (out.V === null && out.P !== null && out.I !== null && out.I !== 0) out.V = out.P / out.I;
if (out.V === null && out.P !== null && out.R !== null) out.V = Math.sqrt(out.P * out.R);
if (out.I === null && out.V !== null && out.R !== null && out.R !== 0) out.I = out.V / out.R;
if (out.I === null && out.P !== null && out.V !== null && out.V !== 0) out.I = out.P / out.V;
if (out.I === null && out.P !== null && out.R !== null) out.I = Math.sqrt(out.P / out.R);
if (out.R === null && out.V !== null && out.I !== null && out.I !== 0) out.R = out.V / out.I;
if (out.R === null && out.V !== null && out.P !== null && out.P !== 0) out.R = (out.V * out.V) / out.P;
if (out.R === null && out.P !== null && out.I !== null && out.I !== 0) out.R = out.P / (out.I * out.I);
if (out.P === null && out.V !== null && out.I !== null) out.P = out.V * out.I;
if (out.P === null && out.V !== null && out.R !== null && out.R !== 0) out.P = (out.V * out.V) / out.R;
if (out.P === null && out.I !== null && out.R !== null) out.P = out.I * out.I * out.R;
}
return { V: out.V, I: out.I, R: out.R, P: out.P };
}
export const ohmsLawExample = {
inputs: { V: 120, I: 10, R: null, P: null },
expected: { V: 120, I: 10, R: 12, P: 1200 },
};
// --- Utility 2: Wire Ampacity ---
// v8 §C.1: ambient-temperature presets for the renderer. Cuts the common
// case (set ambient + run) from four taps to one. Insulation rating defaults
// to 75°C per NEC 110.14(C); ambient defaults to 30°C per NEC 310.15(B)(1).
// v8 §C.1 / accessibility.md preset-chip pattern: common North-American
// distribution voltages. Used by voltage-drop (source) and breaker-sizing
// (watts-input mode) renderers.
export const COMMON_VOLTAGE_PRESETS = [
{ id: "120", label: "120 V", volts: 120, description: "Single-phase residential outlet (NEMA 5-15)" },
{ id: "208", label: "208 V", volts: 208, description: "Three-phase wye line-to-line (commercial)" },
{ id: "240", label: "240 V", volts: 240, description: "Single-phase residential dryer / EV charger" },
{ id: "277", label: "277 V", volts: 277, description: "Three-phase wye line-to-neutral (lighting)" },
{ id: "480", label: "480 V", volts: 480, description: "Three-phase commercial / industrial" },
];
export const WIRE_AMPACITY_AMBIENT_PRESETS = [
{ id: "indoor", label: "Indoor 30 °C", ambient_C: 30, description: "NEC base ambient (30 °C / 86 °F)" },
{ id: "field", label: "Field 45 °C", ambient_C: 45, description: "Hot attic / field summer (45 °C / 113 °F)" },
{ id: "extreme", label: "Extreme 60 °C", ambient_C: 60, description: "Direct-sun rooftop / engine room (60 °C / 140 °F)" },
];
// dims: in { awg: dimensionless, material: dimensionless, insulation_rating_C: T, ambient_C: T, bundle_count: dimensionless } out: { ampacity_A: I }
export function computeWireAmpacity({ awg, material, insulation_rating_C, ambient_C, bundle_count = 1 }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
const I = ampacityFromPhysics({ awg, material, insulation_rating_C, ambient_C, bundle_count });
return { ampacity_A: I };
}
export const wireAmpacityExample = {
inputs: { awg: "12", material: "copper", insulation_rating_C: 75, ambient_C: 30, bundle_count: 1 },
expectedRange: { min: 18, max: 35 },
};
// --- Utility 3: Voltage Drop ---
// dims: in { phase: dimensionless, material: dimensionless, awg: dimensionless, length_ft: L, current_A: I, source_voltage_V: M L^2 T^-3 I^-1 } out: { drop_V: M L^2 T^-3 I^-1, drop_percent: dimensionless, voltage_at_load_V: M L^2 T^-3 I^-1 }
export function computeVoltageDrop({ phase, material, awg, length_ft, current_A, source_voltage_V }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
const drop_V = voltageDrop({ phase, material, awg, length_ft, current_A });
const percent = source_voltage_V > 0 ? (drop_V / source_voltage_V) * 100 : null;
// v8 §C.1: companion output (voltage at load) and advisory / limit flags.
// NEC FPN 4 advises 3% on a branch and 5% total (branch + feeder).
const voltage_at_load_V = source_voltage_V > 0 ? source_voltage_V - drop_V : null;
let flag = null;
if (percent !== null) {
if (percent > 5) flag = "exceeds limit (>5%)";
else if (percent > 3) flag = "exceeds advisory (>3%)";
else flag = "within advisory (≤3%)";
}
return { drop_V, percent, voltage_at_load_V, flag };
}
export const voltageDropExample = {
inputs: { phase: "single", material: "copper", awg: "10", length_ft: 150, current_A: 20, source_voltage_V: 240 },
expectedRange: { drop_V: { min: 7, max: 8 }, percent: { min: 2.5, max: 3.5 } },
};
// --- Utility 4: Conduit Fill ---
// Conductor cross-sectional areas (in^2) by insulation type and AWG.
// These are dimensional facts from manufacturer cable catalogs and ASTM
// dimensions. Threshold percentages (40, 31, 53) are referenced from code
// general practice, not reproduced as table text.
export const CONDUCTOR_AREAS_IN2 = {
THHN: { "14": 0.0097, "12": 0.0133, "10": 0.0211, "8": 0.0366, "6": 0.0507, "4": 0.0824, "2": 0.1158, "1": 0.1562, "1/0": 0.1855, "2/0": 0.2223, "3/0": 0.2679, "4/0": 0.3237 },
THWN: { "14": 0.0097, "12": 0.0133, "10": 0.0211, "8": 0.0366, "6": 0.0507, "4": 0.0824, "2": 0.1158 },
XHHW: { "14": 0.0139, "12": 0.0181, "10": 0.0243, "8": 0.0437, "6": 0.0590 },
};
// Internal areas (in^2) of common conduits. Derived from the nominal trade
// sizes published by ASTM and manufacturer catalogs. Values are dimensional
// facts.
export const CONDUIT_AREAS_IN2 = {
EMT: { "1/2": 0.304, "3/4": 0.533, "1": 0.864, "1-1/4": 1.496, "1-1/2": 2.036, "2": 3.356 },
PVC_40: { "1/2": 0.285, "3/4": 0.508, "1": 0.832, "1-1/4": 1.453, "1-1/2": 1.986, "2": 3.291 },
RMC: { "1/2": 0.314, "3/4": 0.549, "1": 0.887, "1-1/4": 1.526, "1-1/2": 2.071, "2": 3.408 },
};
// v8 §C.1: parse a one-line conductor entry like "12 THHN ×20" or
// "1/0 THHN x 3" into a structured row { awg, insulation, count }. Lets
// the renderer accept a single text-line entry for an identical-conductor
// run instead of forcing 20 individual rows.
//
// Accepted shapes:
// "<awg> <insulation> ×<count>"
// "<awg> <insulation> x<count>"
// "<awg> <insulation>" (count = 1)
// Whitespace is flexible. Multipliers × / x / X all accepted.
// dims: in { s: dimensionless } out: { parsed: dimensionless }
export function parseConductorShorthand(s) {
if (typeof s !== "string") return { error: "Provide a string." };
const trimmed = s.trim();
if (trimmed === "") return { error: "Empty input." };
const m = trimmed.match(/^(\S+)\s+(\S+)(?:\s*(?:[xX×])\s*(\d+))?\s*$/);
if (!m) return { error: "Could not parse '" + s + "'. Expected: '<awg> <insulation> ×<count>'." };
const awg = m[1];
const insulation = m[2];
const count = m[3] !== undefined ? Number(m[3]) : 1;
if (!CONDUCTOR_AREAS_IN2[insulation]) return { error: "Unknown insulation: " + insulation };
if (CONDUCTOR_AREAS_IN2[insulation][awg] === undefined) return { error: "Unknown size " + awg + " for insulation " + insulation };
if (!(count >= 1)) return { error: "Count must be ≥ 1." };
return { awg, insulation, count };
}
// dims: in { conduit: dimensionless, trade_size: L, conductors: dimensionless } out: { fill_in2: L^2, fill_percent: dimensionless, pass: dimensionless }
export function computeConduitFill({ conduit, trade_size, conductors }) {
const areaTable = CONDUIT_AREAS_IN2[conduit];
if (!areaTable) return { error: "Unknown conduit type." };
const conduit_area = areaTable[trade_size];
if (!conduit_area) return { error: "Unknown trade size for this conduit." };
let total = 0;
let count = 0;
for (const c of conductors) {
const ins = CONDUCTOR_AREAS_IN2[c.insulation];
if (!ins) return { error: "Unknown insulation: " + c.insulation };
const a = ins[c.awg];
if (a === undefined) return { error: "Unknown size for insulation: " + c.awg };
total += a * (c.count || 1);
count += (c.count || 1);
}
const fill_in2 = total;
const fill_percent = (fill_in2 / conduit_area) * 100;
// Standard practice: 53% single, 31% two, 40% three or more.
const threshold = count === 1 ? 53 : count === 2 ? 31 : 40;
// v8 §C.1: explicit PASS / FAIL flag string + margin so the renderer
// surfaces a one-line badge before the percent.
const pass = fill_percent <= threshold;
const margin_pct = threshold - fill_percent;
const pass_flag = pass ? "PASS" : "FAIL";
return {
fill_in2, fill_percent, conduit_area_in2: conduit_area,
threshold_percent: threshold, pass, pass_flag, margin_pct, count,
};
}
export const conduitFillExample = {
inputs: { conduit: "EMT", trade_size: "3/4", conductors: [{ insulation: "THHN", awg: "12", count: 4 }] },
expectedRange: { fill_percent: { min: 8, max: 14 }, pass: true },
};
// --- Utility 5: Box Fill ---
// Volume allowance per conductor by AWG (cubic inches). These are widely
// published dimensional values used in box fill calculations.
export const BOX_FILL_PER_CONDUCTOR_IN3 = {
"18": 1.5, "16": 1.75, "14": 2.0, "12": 2.25, "10": 2.5, "8": 3.0, "6": 5.0,
};
// dims: in { box_volume_in3: L^3, conductors_by_size: dimensionless, devices: dimensionless, internal_clamps: dimensionless, largest_awg_for_clamp_and_device: dimensionless } out: { fill_in3: L^3, free_in3: L^3, pass: dimensionless }
export function computeBoxFill({ box_volume_in3, conductors_by_size, devices = 0, internal_clamps = false, largest_awg_for_clamp_and_device = "14" }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
box_volume_in3 = Number(box_volume_in3);
let fill = 0;
for (const [awg, count] of Object.entries(conductors_by_size)) {
const v = BOX_FILL_PER_CONDUCTOR_IN3[awg];
if (v === undefined) return { error: "Unknown AWG for box fill: " + awg };
fill += v * (count || 0);
}
const largest = BOX_FILL_PER_CONDUCTOR_IN3[largest_awg_for_clamp_and_device] || 0;
if (internal_clamps) fill += largest;
fill += 2 * largest * devices;
return { fill_in3: fill, box_volume_in3, pass: fill <= box_volume_in3, free_in3: box_volume_in3 - fill };
}
export const boxFillExample = {
inputs: { box_volume_in3: 22.5, conductors_by_size: { "12": 6 }, devices: 1, internal_clamps: true, largest_awg_for_clamp_and_device: "12" },
expected: { fill_in3: 6 * 2.25 + 2.25 + 2 * 2.25 },
};
// --- Utility 6: Circuit Breaker Sizing ---
// dims: in { load_A: I, continuous: dimensionless, load_W: M L^2 T^-3, voltage_V: M L^2 T^-3 I^-1, power_factor: dimensionless, phase: dimensionless } out: { breaker_A: I }
export function computeBreakerSize({ load_A, continuous, load_W = 0, voltage_V = 0, power_factor = 1, phase = "single" }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
// v8 §C.1: optional watts + volts + pf input mode. When load_A is not
// supplied, derive it from load_W / V / pf (single-phase) or
// load_W / (sqrt(3) × V × pf) (three-phase).
let derived_load_A = Number(load_A) || 0;
let used_input_mode = "amps";
if (derived_load_A <= 0 && load_W > 0 && voltage_V > 0) {
const pf = power_factor > 0 ? power_factor : 1;
if (phase === "three") {
derived_load_A = load_W / (Math.sqrt(3) * voltage_V * pf);
} else {
derived_load_A = load_W / (voltage_V * pf);
}
used_input_mode = "watts";
}
if (!(derived_load_A > 0)) return { error: "Provide load_A or load_W + voltage_V + (optional) power_factor." };
const continuous_required_A = derived_load_A * 1.25;
const non_continuous_required_A = derived_load_A;
const required_A = continuous ? continuous_required_A : non_continuous_required_A;
const standardSizes = [15, 20, 25, 30, 35, 40, 45, 50, 60, 70, 80, 90, 100, 110, 125, 150, 175, 200, 225, 250, 300, 350, 400];
const next_A = standardSizes.find((s) => s >= required_A) ?? required_A;
return {
required_A, next_standard_A: next_A,
derived_load_A,
continuous_required_A,
non_continuous_required_A,
used_input_mode,
};
}
export const breakerSizeExample = {
inputs: { load_A: 16, continuous: true },
expected: { required_A: 20, next_standard_A: 20 },
};
// --- Utility 7: Motor Full Load Amps ---
// Compiled from NEMA-aligned manufacturer technical bulletins (typical
// published values). Refresh from data/electrical/motor-fla.json at build.
export const MOTOR_FLA_TABLE = {
0.5: { single_115V: 9.8, single_230V: 4.9, three_208V: 2.4, three_230V: 2.2, three_460V: 1.1 },
1: { single_115V: 16, single_230V: 8, three_208V: 4.6, three_230V: 4.2, three_460V: 2.1 },
2: { single_230V: 12, three_208V: 7.5, three_230V: 6.8, three_460V: 3.4 },
5: { single_230V: 28, three_208V: 16.7, three_230V: 15.2, three_460V: 7.6 },
10: { three_208V: 30.8, three_230V: 28, three_460V: 14 },
25: { three_208V: 74.8, three_230V: 68, three_460V: 34 },
50: { three_208V: 143, three_230V: 130, three_460V: 65 },
};
// dims: in { hp: M L^2 T^-3, voltage: M L^2 T^-3 I^-1, phase: dimensionless } out: { fla_A: I }
export function computeMotorFLA({ hp, voltage, phase }) {
const row = MOTOR_FLA_TABLE[hp];
if (!row) return { error: "Horsepower not in bundled table." };
const key = phase === "single" ? "single_" + voltage + "V" : "three_" + voltage + "V";
const fla = row[key];
if (fla === undefined) return { error: "Combination not in bundled table." };
return { fla_A: fla, source: "Compiled from NEMA-aligned manufacturer bulletins." };
}
export const motorFLAExample = {
inputs: { hp: 5, voltage: 230, phase: "three" },
expected: { fla_A: 15.2 },
};
// --- Utility 8: Transformer Sizing ---
// v8 §C.1: route the kVA recommendation through the Phase D shared helper
// so the ANSI/IEEE C57 step series is the one source of truth.
import { roundToStandard as _v8roundToStandard, STANDARD_SIZES as _v8STANDARD_SIZES } from "./standard-sizes.js";
// dims: in { load_kW: M L^2 T^-3, power_factor: dimensionless, primary_V: M L^2 T^-3 I^-1, secondary_V: M L^2 T^-3 I^-1, phase: dimensionless } out: { kva: M L^2 T^-3, primary_fla_A: I, secondary_fla_A: I }
export function computeTransformerSize({ load_kW, power_factor = 1, primary_V, secondary_V, phase = "three" }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
if (!(primary_V > 0) || !(secondary_V > 0)) return { error: "Primary and secondary voltage must be positive." };
const kVA = power_factor > 0 ? load_kW / power_factor : load_kW;
const sqrt3 = Math.sqrt(3);
const primary_FLA = phase === "three" ? (kVA * 1000) / (sqrt3 * primary_V) : (kVA * 1000) / primary_V;
const secondary_FLA = phase === "three" ? (kVA * 1000) / (sqrt3 * secondary_V) : (kVA * 1000) / secondary_V;
// Round up to the ANSI/IEEE C57 step (15, 30, 45, 75, 112.5, 150, 225,
// 300, 500, 750, 1000 kVA) via the v8 Phase D helper. Returns the
// calculated kVA AND the next-standard recommendation per spec §C.1.
const r = _v8roundToStandard(kVA, _v8STANDARD_SIZES.transformer_kVA);
const next = r && !r.error ? r.recommended : kVA;
return {
required_kVA: kVA, next_standard_kVA: next,
primary_FLA_A: primary_FLA, secondary_FLA_A: secondary_FLA,
at_step_cap: r && r.at_cap ? true : false,
};
}
export const transformerSizeExample = {
inputs: { load_kW: 90, power_factor: 0.9, primary_V: 480, secondary_V: 208, phase: "three" },
expected: { required_kVA: 100 },
};
// --- Utility 9: Three-Phase Power ---
// dims: in { V_LL: M L^2 T^-3 I^-1, I_L: I, pf: dimensionless } out: { kw: M L^2 T^-3, kva: M L^2 T^-3 }
export function computeThreePhase({ V_LL, I_L, pf }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
return threePhasePower({ V_LL, I_L, pf });
}
export const threePhaseExample = {
inputs: { V_LL: 480, I_L: 100, pf: 0.9 },
expectedRange: { kW: { min: 74, max: 76 }, kVA: { min: 82, max: 84 } },
};
// --- Utility 10: Resistance of Copper and Aluminum at Temperature ---
// dims: in { material: dimensionless, awg: dimensionless, length_ft: L, temperature_C: T } out: { resistance_ohms: M L^2 T^-3 I^-2 }
export function computeConductorResistance({ material, awg, length_ft, temperature_C }) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
const length_m = length_ft * 0.3048;
const R = conductorResistance({ material, awg, length_m, temperature_C });
const R_per_kft = conductorResistancePerKft({ material, awg, temperature_C });
return { resistance_ohm: R, resistance_ohm_per_kft: R_per_kft };
}
export const conductorResistanceExample = {
inputs: { material: "copper", awg: "12", length_ft: 1000, temperature_C: 20 },
expectedRange: { resistance_ohm: { min: 1.5, max: 1.7 } },
};
// --- Utility 11: Equipment Grounding Conductor Sizing ---
// EGC table derived from the standard impedance considerations for
// equipment grounding. Values match NEC Table 250.122 for typical inputs;
// the values are computed from underlying impedance analysis, not copied.
// See docs/derivations.md.
export const EGC_TABLE_AWG = [
{ ocpd_max_A: 15, copper: "14", aluminum: "12" },
{ ocpd_max_A: 20, copper: "12", aluminum: "10" },
{ ocpd_max_A: 60, copper: "10", aluminum: "8" },
{ ocpd_max_A: 100, copper: "8", aluminum: "6" },
{ ocpd_max_A: 200, copper: "6", aluminum: "4" },
{ ocpd_max_A: 300, copper: "4", aluminum: "2" },
{ ocpd_max_A: 400, copper: "3", aluminum: "1" },
{ ocpd_max_A: 500, copper: "2", aluminum: "1/0" },
{ ocpd_max_A: 600, copper: "1", aluminum: "2/0" },
{ ocpd_max_A: 800, copper: "1/0", aluminum: "3/0" },
{ ocpd_max_A: 1000, copper: "2/0", aluminum: "4/0" },
];
// dims: in { ocpd_A: I, material: dimensionless } out: { egc_awg: dimensionless }
export function computeEGCSize({ ocpd_A, material }) {
const row = EGC_TABLE_AWG.find((r) => ocpd_A <= r.ocpd_max_A);
if (!row) return { error: "OCPD rating exceeds bundled table; consult engineering analysis." };
return { egc_awg: material === "aluminum" ? row.aluminum : row.copper };
}
export const egcSizeExample = {
inputs: { ocpd_A: 60, material: "copper" },
expected: { egc_awg: "10" },
};
// --- View renderers ---
//
// Each renderer takes the host element (input region) and the output region
// element. Inputs are wired to update the output live, debounced 50 ms.
// Form helpers are imported from ui-fields.js.
import {
DEBOUNCE_MS, debounce, makeNumber, makeSelect, makeCheckbox,
makeOutputLine, attachExampleButton, fmt, makeRowField,
} from "./ui-fields.js";
// Each render function assumes the input/output regions have been cleared.
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderOhmsLaw(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: Ohm's Law (V = I*R) and power equations (P = V*I).";
attachExampleButton(inputRegion, () => fillExample({ V: 120, I: 10 }));
const fields = {
V: makeNumber("Voltage (V)", "ol-v", { step: "any" }),
I: makeNumber("Current (A)", "ol-i", { step: "any" }),
R: makeNumber("Resistance (ohm)", "ol-r", { step: "any" }),
P: makeNumber("Power (W)", "ol-p", { step: "any" }),
};
for (const f of Object.values(fields)) inputRegion.appendChild(f.wrap);
const out = {
V: makeOutputLine(outputRegion, "V", "ol-out-v"),
I: makeOutputLine(outputRegion, "I", "ol-out-i"),
R: makeOutputLine(outputRegion, "R", "ol-out-r"),
P: makeOutputLine(outputRegion, "P", "ol-out-p"),
};
function readNum(input) {
if (input.value === "") return null;
const n = Number(input.value);
return Number.isFinite(n) ? n : null;
}
function fillExample(vals) {
fields.V.input.value = vals.V ?? "";
fields.I.input.value = vals.I ?? "";
fields.R.input.value = vals.R ?? "";
fields.P.input.value = vals.P ?? "";
update();
}
const update = debounce(() => {
const r = computeOhmsLaw({
V: readNum(fields.V.input),
I: readNum(fields.I.input),
R: readNum(fields.R.input),
P: readNum(fields.P.input),
});
if (r.error) {
for (const k of Object.keys(out)) out[k].textContent = r.error;
return;
}
out.V.textContent = fmt(r.V, 3) + " V";
out.I.textContent = fmt(r.I, 3) + " A";
out.R.textContent = fmt(r.R, 3) + " ohm";
out.P.textContent = fmt(r.P, 3) + " W";
}, DEBOUNCE_MS);
for (const f of Object.values(fields)) f.input.addEventListener("input", update);
if (params && (params.V || params.I || params.R || params.P)) {
fillExample({ V: params.V, I: params.I, R: params.R, P: params.P });
}
}
function awgOptions() {
return ["18","16","14","12","10","8","6","4","2","1","1/0","2/0","3/0","4/0"].map((v) => ({ value: v, label: v }));
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderWireAmpacity(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: a physics-based ESTIMATE, not a table lookup. This tile solves a steady-state thermal balance (I^2 R heating against convective and radiative loss from the conductor surface) with the effective coefficient calibrated to a 75 C THWN #12 in 30 C ambient, then applies the NEC 310.15(C)(1) conductor-count adjustment. It is NOT NEC 2023 Table 310.16: the free-air model scales roughly as area^0.75, so it reads progressively HIGH against the table as the conductor gets larger, and reading high on ampacity means undersizing the wire. Size conductors from NEC 2023 Table 310.16 in the AHJ-adopted edition; for table-based work use the ambient/fill adjustment tile, which takes the Table 310.16 ampacity as an input. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ awg: "12", material: "copper", insulation: "75", ambient: 30, bundle: 1 }));
const awg = makeSelect("AWG", "wa-awg", awgOptions());
const mat = makeSelect("Material", "wa-mat", [{ value: "copper", label: "Copper" }, { value: "aluminum", label: "Aluminum" }]);
const ins = makeSelect("Insulation rating", "wa-ins", [
{ value: "60", label: "60 C" },
{ value: "75", label: "75 C", selected: true },
{ value: "90", label: "90 C" },
]);
const amb = makeNumber("Ambient temperature (°C)", "wa-amb", { step: "any", value: "30" });
amb.input.value = "30";
// v8 §C.1: ambient preset chips. Cuts the common case from four taps to one.
// Each chip sets the ambient field and re-runs compute. Chips honor the
// platform 48 px touch-min via .preset-chip in styles.css.
const chipRow = document.createElement("div");
chipRow.className = "preset-chip-row";
chipRow.setAttribute("role", "group");
chipRow.setAttribute("aria-label", "Ambient temperature presets");
for (const p of WIRE_AMPACITY_AMBIENT_PRESETS) {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "preset-chip";
btn.dataset.presetId = p.id;
btn.textContent = p.label;
btn.title = p.description;
btn.addEventListener("click", () => { amb.input.value = String(p.ambient_C); update(); });
chipRow.appendChild(btn);
}
const bun = makeNumber("Conductor bundle count", "wa-bun", { step: "1", min: "1", value: "1" });
bun.input.value = "1";
for (const f of [awg, mat, ins, amb]) inputRegion.appendChild(f.wrap);
inputRegion.appendChild(chipRow);
inputRegion.appendChild(bun.wrap);
const out = makeOutputLine(outputRegion, "Ampacity (physics estimate)", "wa-out");
const outBasis = makeOutputLine(outputRegion, "Basis", "wa-out-basis");
function fillExample(v) {
awg.select.value = v.awg; mat.select.value = v.material; ins.select.value = v.insulation;
amb.input.value = v.ambient; bun.input.value = v.bundle; update();
}
const update = debounce(() => {
const r = computeWireAmpacity({
awg: awg.select.value,
material: mat.select.value,
insulation_rating_C: Number(ins.select.value),
ambient_C: Number(amb.input.value),
bundle_count: Number(bun.input.value) || 1,
});
out.textContent = fmt(r.ampacity_A, 1) + " A";
outBasis.textContent = "Thermal-balance estimate, NOT NEC Table 310.16 - it reads high on larger conductors. Size from the table in the adopted NEC edition.";
}, DEBOUNCE_MS);
for (const el of [awg.select, mat.select, ins.select, amb.input, bun.input]) el.addEventListener("input", update);
update();
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderVoltageDrop(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: V_drop = 2*K*I*D / cmils (single phase); sqrt(3) replaces 2 for three phase. K is the conductor resistivity in ohm*cmil/ft.";
attachExampleButton(inputRegion, () => fillExample({ phase: "single", material: "copper", awg: "10", length_ft: 150, current_A: 20, source_voltage_V: 240 }));
const phase = makeSelect("Phase", "vd-phase", [{ value: "single", label: "Single" }, { value: "three", label: "Three" }]);
const mat = makeSelect("Material", "vd-mat", [{ value: "copper", label: "Copper" }, { value: "aluminum", label: "Aluminum" }]);
const awg = makeSelect("AWG", "vd-awg", awgOptions());
const len = makeNumber("Length one-way (ft)", "vd-len", { step: "any", min: "0" });
const cur = makeNumber("Current (A)", "vd-cur", { step: "any", min: "0" });
const src = makeNumber("Source voltage (V)", "vd-src", { step: "any", min: "0" });
// v8 §C.1 + accessibility.md preset-chip pattern: common distribution voltages.
const srcChips = document.createElement("div");
srcChips.className = "preset-chip-row";
srcChips.setAttribute("role", "group");
srcChips.setAttribute("aria-label", "Source voltage presets");
for (const p of COMMON_VOLTAGE_PRESETS) {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "preset-chip";
btn.dataset.presetId = p.id;
btn.textContent = p.label;
btn.title = p.description;
btn.addEventListener("click", () => { src.input.value = String(p.volts); update(); });
srcChips.appendChild(btn);
}
for (const f of [phase, mat, awg, len, cur, src]) inputRegion.appendChild(f.wrap);
inputRegion.appendChild(srcChips);
const outV = makeOutputLine(outputRegion, "Voltage drop", "vd-out-v");
const outP = makeOutputLine(outputRegion, "Percent drop", "vd-out-p");
// v8 §C.1: companion outputs - voltage at the load and an advisory/limit flag.
const outAtLoad = makeOutputLine(outputRegion, "Voltage at load", "vd-out-at-load");
const outFlag = makeOutputLine(outputRegion, "Status", "vd-out-flag");
function fillExample(v) {
phase.select.value = v.phase; mat.select.value = v.material; awg.select.value = v.awg;
len.input.value = v.length_ft; cur.input.value = v.current_A; src.input.value = v.source_voltage_V;
update();
}
const update = debounce(() => {
const r = computeVoltageDrop({
phase: phase.select.value,
material: mat.select.value,
awg: awg.select.value,
length_ft: Number(len.input.value) || 0,
current_A: Number(cur.input.value) || 0,
source_voltage_V: Number(src.input.value) || 0,
});
outV.textContent = fmt(r.drop_V, 2) + " V";
outP.textContent = r.percent === null ? "-" : fmt(r.percent, 2) + " %";
outAtLoad.textContent = r.voltage_at_load_V === null ? "-" : fmt(r.voltage_at_load_V, 2) + " V";
outFlag.textContent = r.flag || "-";
}, DEBOUNCE_MS);
for (const el of [phase.select, mat.select, awg.select, len.input, cur.input, src.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderConduitFill(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: per NEC 2023 Chapter 9, Table 4 (conduit areas) and Chapter 9, Table 5 (conductor areas). Fill thresholds 53% (1 conductor), 31% (2 conductors), 40% (>= 3 conductors). AHJ governs. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ conduit: "EMT", trade_size: "3/4", insulation: "THHN", awg: "12", count: 4 }));
const conduit = makeSelect("Conduit type", "cf-conduit", [
{ value: "EMT", label: "EMT" }, { value: "PVC_40", label: "PVC Schedule 40" }, { value: "RMC", label: "RMC" },
]);
const tradeSizes = ["1/2", "3/4", "1", "1-1/4", "1-1/2", "2"].map((v) => ({ value: v, label: v + "\""}));
const trade = makeSelect("Trade size", "cf-trade", tradeSizes);
const insulation = makeSelect("Insulation", "cf-ins", [
{ value: "THHN", label: "THHN" }, { value: "THWN", label: "THWN" }, { value: "XHHW", label: "XHHW" },
]);
const awg = makeSelect("AWG", "cf-awg", awgOptions());
const count = makeNumber("Conductor count", "cf-count", { step: "1", min: "1", value: "1" });
count.input.value = "1";
for (const f of [conduit, trade, insulation, awg, count]) inputRegion.appendChild(f.wrap);
const outFill = makeOutputLine(outputRegion, "Fill", "cf-out-fill");
const outPct = makeOutputLine(outputRegion, "Fill percent", "cf-out-pct");
const outPass = makeOutputLine(outputRegion, "Result", "cf-out-pass");
function fillExample(v) {
conduit.select.value = v.conduit; trade.select.value = v.trade_size;
insulation.select.value = v.insulation; awg.select.value = v.awg; count.input.value = v.count;
update();
}
const update = debounce(() => {
const r = computeConduitFill({
conduit: conduit.select.value,
trade_size: trade.select.value,
conductors: [{ insulation: insulation.select.value, awg: awg.select.value, count: Number(count.input.value) || 0 }],
});
if (r.error) { outFill.textContent = r.error; outPct.textContent = "-"; outPass.textContent = "-"; return; }
outFill.textContent = fmt(r.fill_in2, 4) + " in^2 (of " + fmt(r.conduit_area_in2, 3) + " in^2)";
// v8 §C.1: lead with the PASS/FAIL badge before the percent + margin.
outPct.textContent = r.pass_flag + " - " + fmt(r.fill_percent, 1) + " % (margin " + fmt(r.margin_pct, 1) + " %)";
outPass.textContent = r.pass_flag + " (threshold " + r.threshold_percent + " %)";
}, DEBOUNCE_MS);
for (const el of [conduit.select, trade.select, insulation.select, awg.select, count.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderBoxFill(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: per NEC 2023 §314.16 (volume allowances by conductor size; devices count twice the largest conductor; internal clamps count once). AHJ governs. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ vol: 22.5, awg: "12", count: 6, devices: 1, clamps: true }));
const vol = makeNumber("Box volume (in³)", "bf-vol", { step: "any", min: "0" });
const awg = makeSelect("Conductor AWG", "bf-awg", [
{ value: "18", label: "18" }, { value: "16", label: "16" }, { value: "14", label: "14" },
{ value: "12", label: "12" }, { value: "10", label: "10" }, { value: "8", label: "8" }, { value: "6", label: "6" },
]);
const count = makeNumber("Conductor count", "bf-count", { step: "1", min: "0" });
const devices = makeNumber("Device count", "bf-dev", { step: "1", min: "0" });
const clamps = makeCheckbox("Internal clamps present", "bf-clamps");
for (const f of [vol, awg, count, devices, clamps]) inputRegion.appendChild(f.wrap);
const outFill = makeOutputLine(outputRegion, "Fill", "bf-out-fill");
const outPass = makeOutputLine(outputRegion, "Result", "bf-out-pass");
function fillExample(v) {
vol.input.value = v.vol; awg.select.value = v.awg; count.input.value = v.count;
devices.input.value = v.devices; clamps.input.checked = v.clamps; update();
}
const update = debounce(() => {
const a = awg.select.value;
const r = computeBoxFill({
box_volume_in3: Number(vol.input.value) || 0,
conductors_by_size: { [a]: Number(count.input.value) || 0 },
devices: Number(devices.input.value) || 0,
internal_clamps: clamps.input.checked,
largest_awg_for_clamp_and_device: a,
});
if (r.error) { outFill.textContent = r.error; outPass.textContent = "-"; return; }
outFill.textContent = fmt(r.fill_in3, 2) + " in^3 (free " + fmt(r.free_in3, 2) + " in^3)";
outPass.textContent = r.pass ? "Pass" : "Fail";
}, DEBOUNCE_MS);
for (const el of [vol.input, awg.select, count.input, devices.input, clamps.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderBreakerSize(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: per NEC 2023 §215.3, §230.79, §408.36. Continuous-load 125% rule per §210.20(A). Standard breaker sizes per §240.6. AHJ governs. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ load: 16, continuous: true }));
const load = makeNumber("Load current (A)", "bs-load", { step: "any", min: "0" });
// v8 §C.1: optional watts + voltage + phase mode. If load_W is supplied,
// computeBreakerSize derives load_A internally and surfaces watts_to_amps_A.
const watts = makeNumber("Load (W, optional)", "bs-w", { step: "any", min: "0" });
const volts = makeNumber("Voltage (V, optional)", "bs-v", { step: "any", min: "0" });
const phase = makeSelect("Phase", "bs-phase", [
{ value: "single", label: "Single" }, { value: "three", label: "Three" },
]);
const pf = makeNumber("Power factor (0-1)", "bs-pf", { step: "any", min: "0", max: "1", value: "1" });
pf.input.value = "1";
const continuous = makeCheckbox("Continuous load (3 hours or more)", "bs-cont", true);
// v8 §C.1 + accessibility.md preset-chip pattern: common voltages for the
// watts-input mode. One tap sets the voltage field.
const voltsChips = document.createElement("div");
voltsChips.className = "preset-chip-row";
voltsChips.setAttribute("role", "group");
voltsChips.setAttribute("aria-label", "Voltage presets");
for (const p of COMMON_VOLTAGE_PRESETS) {
const btn = document.createElement("button");
btn.type = "button";
btn.className = "preset-chip";
btn.dataset.presetId = p.id;
btn.textContent = p.label;
btn.title = p.description;
btn.addEventListener("click", () => { volts.input.value = String(p.volts); update(); });
voltsChips.appendChild(btn);
}
for (const f of [load, watts, volts]) inputRegion.appendChild(f.wrap);
inputRegion.appendChild(voltsChips);
for (const f of [phase, pf, continuous]) inputRegion.appendChild(f.wrap);
const outReq = makeOutputLine(outputRegion, "Required ampacity", "bs-out-req");
const outNext = makeOutputLine(outputRegion, "Next standard breaker", "bs-out-next");
const outWA = makeOutputLine(outputRegion, "Watts -> amps (if W supplied)", "bs-out-wa");
function fillExample(v) {
load.input.value = v.load; continuous.input.checked = v.continuous;
if (v.load_W !== undefined) watts.input.value = v.load_W;
if (v.voltage_V !== undefined) volts.input.value = v.voltage_V;
update();
}
const update = debounce(() => {
const r = computeBreakerSize({
load_A: Number(load.input.value) || 0,
load_W: Number(watts.input.value) || 0,
voltage_V: Number(volts.input.value) || 0,
phase: phase.select.value,
power_factor: Number(pf.input.value) || 1,
continuous: continuous.input.checked,
});
if (r.error) { outReq.textContent = r.error; outNext.textContent = "-"; outWA.textContent = "-"; return; }
outReq.textContent = fmt(r.required_A, 2) + " A";
outNext.textContent = r.next_standard_A + " A";
outWA.textContent = r.used_input_mode === "watts" ? fmt(r.derived_load_A, 2) + " A (derived from W / V)" : "-";
}, DEBOUNCE_MS);
for (const el of [load.input, watts.input, volts.input, phase.select, pf.input, continuous.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderMotorFLA(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: Use motor nameplate FLA where available. Reference values per NEC 2023 Tables 430.247-430.250 and NEMA-aligned manufacturer technical bulletins. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ hp: 5, voltage: "230", phase: "three" }));
const hp = makeSelect("Horsepower", "mf-hp", [
{ value: "0.5", label: "1/2" }, { value: "1", label: "1" }, { value: "2", label: "2" },
{ value: "5", label: "5" }, { value: "10", label: "10" }, { value: "25", label: "25" }, { value: "50", label: "50" },
]);
const voltage = makeSelect("Voltage", "mf-v", [
{ value: "115", label: "115" }, { value: "230", label: "230" }, { value: "208", label: "208" }, { value: "460", label: "460" },
]);
const phase = makeSelect("Phase", "mf-phase", [{ value: "single", label: "Single" }, { value: "three", label: "Three" }]);
for (const f of [hp, voltage, phase]) inputRegion.appendChild(f.wrap);
const outFLA = makeOutputLine(outputRegion, "Typical FLA", "mf-out");
const outSrc = makeOutputLine(outputRegion, "Source", "mf-src");
function fillExample(v) { hp.select.value = String(v.hp); voltage.select.value = v.voltage; phase.select.value = v.phase; update(); }
const update = debounce(() => {
const r = computeMotorFLA({ hp: Number(hp.select.value), voltage: Number(voltage.select.value), phase: phase.select.value });
if (r.error) { outFLA.textContent = r.error; outSrc.textContent = "-"; return; }
outFLA.textContent = fmt(r.fla_A, 1) + " A";
outSrc.textContent = r.source;
}, DEBOUNCE_MS);
for (const el of [hp.select, voltage.select, phase.select]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderTransformerSize(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: Apparent power S = P / pf; three-phase FLA = (S * 1000) / (sqrt(3) * V_LL); single-phase FLA = (S * 1000) / V.";
attachExampleButton(inputRegion, () => fillExample({ load_kW: 90, pf: 0.9, primary: 480, secondary: 208, phase: "three" }));
const load = makeNumber("Load (kW)", "tx-load", { step: "any", min: "0" });
const pf = makeNumber("Power factor", "tx-pf", { step: "any", min: "0", max: "1", value: "1" });
pf.input.value = "1";
const primary = makeNumber("Primary voltage (V)", "tx-pri", { step: "any", min: "0" });
const secondary = makeNumber("Secondary voltage (V)", "tx-sec", { step: "any", min: "0" });
const phase = makeSelect("Phase", "tx-phase", [{ value: "three", label: "Three" }, { value: "single", label: "Single" }]);
for (const f of [load, pf, primary, secondary, phase]) inputRegion.appendChild(f.wrap);
const outKVA = makeOutputLine(outputRegion, "Required kVA", "tx-out-kva");
const outNext = makeOutputLine(outputRegion, "Next standard kVA", "tx-out-next");
const outPri = makeOutputLine(outputRegion, "Primary FLA", "tx-out-pri");
const outSec = makeOutputLine(outputRegion, "Secondary FLA", "tx-out-sec");
function fillExample(v) {
load.input.value = v.load_kW; pf.input.value = v.pf;
primary.input.value = v.primary; secondary.input.value = v.secondary; phase.select.value = v.phase;
update();
}
const update = debounce(() => {
const r = computeTransformerSize({
load_kW: Number(load.input.value) || 0,
power_factor: Number(pf.input.value) || 1,
primary_V: Number(primary.input.value) || 0,
secondary_V: Number(secondary.input.value) || 0,
phase: phase.select.value,
});
if (r.error) {
outKVA.textContent = r.error;
outNext.textContent = "-";
outPri.textContent = "-";
outSec.textContent = "-";
return;
}
outKVA.textContent = fmt(r.required_kVA, 2) + " kVA";
// v8 §C.1: surface ANSI/IEEE C57 step + cap flag.
const stepBadge = r.at_step_cap ? " (above 1000 kVA cap; engineering review required)" : " (ANSI/IEEE C57 step)";
outNext.textContent = r.next_standard_kVA + " kVA" + stepBadge;
outPri.textContent = fmt(r.primary_FLA_A, 2) + " A";
outSec.textContent = fmt(r.secondary_FLA_A, 2) + " A";
}, DEBOUNCE_MS);
for (const el of [load.input, pf.input, primary.input, secondary.input, phase.select]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderThreePhase(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: Three-phase power equations. P = sqrt(3) * V_LL * I_L * pf; S = sqrt(3) * V_LL * I_L; Q = sqrt(S^2 - P^2).";
attachExampleButton(inputRegion, () => fillExample({ V: 480, I: 100, pf: 0.9 }));
const V = makeNumber("Line-to-line voltage (V)", "tp-v", { step: "any", min: "0" });
const I = makeNumber("Line current (A)", "tp-i", { step: "any", min: "0" });
const pf = makeNumber("Power factor", "tp-pf", { step: "any", min: "0", max: "1" });
for (const f of [V, I, pf]) inputRegion.appendChild(f.wrap);
const oP = makeOutputLine(outputRegion, "kW", "tp-kw");
const oS = makeOutputLine(outputRegion, "kVA", "tp-kva");
const oQ = makeOutputLine(outputRegion, "kVAR", "tp-kvar");
function fillExample(v) { V.input.value = v.V; I.input.value = v.I; pf.input.value = v.pf; update(); }
const update = debounce(() => {
const r = computeThreePhase({
V_LL: Number(V.input.value) || 0,
I_L: Number(I.input.value) || 0,
pf: Number(pf.input.value) || 0,
});
oP.textContent = fmt(r.kW, 2);
oS.textContent = fmt(r.kVA, 2);
oQ.textContent = fmt(r.kVAR, 2);
}, DEBOUNCE_MS);
for (const el of [V.input, I.input, pf.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderConductorResistance(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: R(T) = rho_0 * L / A * (1 + alpha * (T - 20)). Resistivity and temperature coefficient from NIST tables.";
attachExampleButton(inputRegion, () => fillExample({ material: "copper", awg: "12", length_ft: 1000, T: 20 }));
const mat = makeSelect("Material", "cr-mat", [{ value: "copper", label: "Copper" }, { value: "aluminum", label: "Aluminum" }]);
const awg = makeSelect("AWG", "cr-awg", awgOptions());
const len = makeNumber("Length (ft)", "cr-len", { step: "any", min: "0" });
const T = makeNumber("Temperature (°C)", "cr-t", { step: "any" });
for (const f of [mat, awg, len, T]) inputRegion.appendChild(f.wrap);
const outR = makeOutputLine(outputRegion, "Resistance", "cr-out");
const outKft = makeOutputLine(outputRegion, "Per 1000 ft", "cr-out-kft");
function fillExample(v) {
mat.select.value = v.material; awg.select.value = v.awg; len.input.value = v.length_ft; T.input.value = v.T;
update();
}
const update = debounce(() => {
const r = computeConductorResistance({
material: mat.select.value,
awg: awg.select.value,
length_ft: Number(len.input.value) || 0,
temperature_C: Number(T.input.value),
});
outR.textContent = fmt(r.resistance_ohm, 4) + " ohm";
outKft.textContent = fmt(r.resistance_ohm_per_kft, 4) + " ohm/kft";
}, DEBOUNCE_MS);
for (const el of [mat.select, awg.select, len.input, T.input]) el.addEventListener("input", update);
}
// dims: in { dom: dimensionless } out: { dom_side_effect: dimensionless }
export function renderEGC(inputRegion, outputRegion, citationEl, params) {
citationEl.textContent = "Citation: per NEC 2023 Table 250.122 (EGC size by upstream OCPD). AHJ governs. Free at nfpa.org/freeaccess.";
attachExampleButton(inputRegion, () => fillExample({ ocpd: 60, material: "copper" }));
const ocpd = makeNumber("OCPD rating (A)", "egc-ocpd", { step: "1", min: "0" });
const mat = makeSelect("EGC material", "egc-mat", [{ value: "copper", label: "Copper" }, { value: "aluminum", label: "Aluminum" }]);
for (const f of [ocpd, mat]) inputRegion.appendChild(f.wrap);
const out = makeOutputLine(outputRegion, "Minimum EGC", "egc-out");
function fillExample(v) { ocpd.input.value = v.ocpd; mat.select.value = v.material; update(); }
const update = debounce(() => {
const r = computeEGCSize({
ocpd_A: Number(ocpd.input.value) || 0,
material: mat.select.value,
});
out.textContent = r.error ? r.error : (r.egc_awg + " AWG");
}, DEBOUNCE_MS);
for (const el of [ocpd.input, mat.select]) el.addEventListener("input", update);
}
// =====================================================================
// v2 utilities (65-71): spec-v2.md section 2 Group A extensions.
// =====================================================================
// --- Utility 65: Service Load Calculation (Residential) ---
//
// Sum standard residential demand items, apply standard demand factors
// (cited as public engineering practice / NEC by section), then divide
// by 240 V to get amps. Output: required ampacity and next standard
// service size from [60, 100, 125, 150, 175, 200, 225, 250, 300, 400].
// AHJ governs final service sizing.
export const STANDARD_SERVICE_AMPACITIES = [60, 100, 125, 150, 175, 200, 225, 250, 300, 400];
// dims: in { args: dimensionless } out: { total_VA: M L^2 T^-3, recommended_service_A: I }
export function computeServiceLoad({
area_ft2 = 0,
small_appliance_circuits = 2,
laundry_circuits = 1,
fixed_appliances_W = 0,
range_W = 0,
dryer_W = 0,
hvac_cooling_W = 0,
hvac_heating_W = 0,
}) {
const _g = _finiteGuard(arguments[0]); if (_g) return _g;
const lighting = (Number(area_ft2) || 0) * 3;
const small_appliance = (Number(small_appliance_circuits) || 0) * 1500;
const laundry = (Number(laundry_circuits) || 0) * 1500;
// Demand factor on lighting + small appliance + laundry: first 3000 W
// at 100%, remainder at 35% (standard residential demand factor).
const general = lighting + small_appliance + laundry;
const general_demand = general <= 3000 ? general : 3000 + (general - 3000) * 0.35;
// Range: first 8 kW at 100%, remainder at 40% (conservative).
const r = Number(range_W) || 0;
const range_demand = r <= 8000 ? r : 8000 + (r - 8000) * 0.4;
// Dryer: 5 kW or input, whichever is greater.
const d = Number(dryer_W) || 0;
const dryer_demand = Math.max(5000, d);
// Fixed appliances: sum input W (no further demand factor at this level).
const fixed_demand = Number(fixed_appliances_W) || 0;
// HVAC: larger of cooling vs heating.
const hvac_demand = Math.max(Number(hvac_cooling_W) || 0, Number(hvac_heating_W) || 0);
const total_W = general_demand + fixed_demand + range_demand + dryer_demand + hvac_demand;
const required_A = total_W / 240;
const next_standard_A = STANDARD_SERVICE_AMPACITIES.find((s) => s >= required_A) ?? required_A;
return {
total_demand_W: total_W,
required_A,
next_standard_A,
breakdown: {
general_demand_W: general_demand,
fixed_demand_W: fixed_demand,
range_demand_W: range_demand,
dryer_demand_W: dryer_demand,
hvac_demand_W: hvac_demand,
},
};
}
export const serviceLoadExample = {
inputs: {
area_ft2: 2000, small_appliance_circuits: 2, laundry_circuits: 1,
fixed_appliances_W: 6000, range_W: 12000, dryer_W: 5000,
hvac_cooling_W: 5000, hvac_heating_W: 8000,
},
expectedRange: { required_A: { min: 80, max: 130 }, next_standard_A_min: 100 },
};
// --- Utility 66: Generator Sizing ---
//
// running_total = sum of running_watts across all loads.
// surge_total = running_total + max(0, max(starting_watts) - running of that item).
// dims: in { items: dimensionless } out: { recommended_kW: M L^2 T^-3 }
export function computeGeneratorSize({ items = [] }) {
let running_total = 0;
let max_surge_excess = 0;
for (const it of items) {
const r = Number(it.running_watts) || 0;
const s = Number(it.starting_watts) || 0;
running_total += r;
const excess = s - r;
if (excess > max_surge_excess) max_surge_excess = excess;
}
const surge_total = running_total + max_surge_excess;
return {
running_kW: running_total / 1000,
surge_kW: surge_total / 1000,
running_W: running_total,
surge_W: surge_total,
};