-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScript.js
More file actions
1899 lines (1636 loc) · 67.2 KB
/
Script.js
File metadata and controls
1899 lines (1636 loc) · 67.2 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
/* ///////////// TABLE OF CONTENTS /////////////
Section 1 — File Header & Overview
1.A Feature summary banner
Section 2 — Debug Harness
2.A __setDebugWindow
2.B logDebug
Section 3 — Utilities
3.A clamp / lerp / normAngleRad / deg2rad / rad2deg
Section 4 — Switch Drag/Rotate Wiring (hit zones)
4.A Config & globals (switches, sensitivity, knobStates)
4.B Per-switch wiring & event handlers (setAngle/onMove/onUp)
4.C angleOf helper
Section 5 — Simulator Core (IIFE)
5.A Rated constants (RATED)
5.B State object (state) + exposure
5.C Gate setpoint variable (Gate_Setpoint)
5.D Master start/stop ramps (gateRamp/stopRamp) & kV ramp (kvRamp)
5.E Voltage slew params (KV_* constants)
5.F Angle watch thresholds & maps (THRESH/WATCH/prevAngles)
5.G setFlag86
5.H handleAction (all operator actions)
5.I watchAngles
5.J updateGateSet (Knob_65)
5.K updateVoltageSet (Knob_90)
5.L updateSyncCheck (sync permissive)
5.M updatePhysics (governor/AVR/power)
5.N slewGenKV (kV tracker)
5.O Syncroscope & Lamps
5.O.1 SYNC struct
5.O.2 parseRotateCenter
5.O.3 initSyncUI
5.O.4 updateSyncScopeAndLamps
5.P Glow helpers (setGlow / setGlowWhite)
5.Q updateGlows
5.R updateGateGauge
5.S Hz needle binding (IIFE)
5.T updateKVgauge
5.U fmtNoLeadingZeros
5.V updateDigitals
5.W Main tick loop (requestAnimationFrame)
5.X Oscilloscope (Bus & Gen waveforms)
Section 6 — RPM Text Binding (IIFE)
/////////////////////////////////////////// */
/* ///////////// Section 1 — File Header & Overview ///////////// */
/* ///////////// Section 1.A Feature summary banner ///////////// */
/* Sim_8.js
Complete build with:
- Switch drag wiring
- Start/Stop ramps + 86G trip/reset w/ flag color
- Gate nudge (Knob_65)
- Speed permissive
- Sync-check permissive (±3% V, ±0.15 Hz, <10°)
- Gen kV model + gauge (GenVolts_Rotation: 36°=0, 180°=13, 324°=15)
- Manual voltage (Knob_90); AVR only acts after 52G closes
- Digital readouts: Value_MW / Value_AMPS / Value_MVAR / Value_PowerFactor
- Hz needle + RPM text
- Status/permissive glows
- Syncroscope needle (SyncScope_Rotation) and TWO sync lamps
Lamps brightness ∝ |Vg∠θg − Vb∠θb|; both lamps identical behavior
*/
/* ///////////// Section 2 — Debug Harness ///////////// */
/* ///////////// Section 2.A __setDebugWindow ///////////// */
let __DEBUG_WIN = null;
function __setDebugWindow(win){ __DEBUG_WIN = win; }
/* ///////////// Section 2.B logDebug ///////////// */
function logDebug(message){
try {
const msg = String(message);
try { console.log(msg); } catch(_) {}
const el = document.getElementById('Debug_Log');
if (el){
const line = document.createElement('div');
line.textContent = msg;
const lower = msg.toLowerCase();
let color;
if (lower.includes('trip')){
if (lower.includes('cleared') || lower.includes('reset') || lower.includes('false') || lower.includes('inactive') || lower.includes('normal')){
color = '#006400';
} else {
color = 'red';
}
} else if (lower.includes('overspeed')) {
color = 'red';
} else if (lower.includes('alarm') || lower.includes('active') || lower.includes('inactive') || lower.includes('abnormal') || lower.includes('normal')){
if (lower.includes('inactive') || (lower.includes('normal') && !lower.includes('abnormal')) || lower.includes('false') || lower.includes('cleared')){
color = '#006400';
} else {
color = '#b8860b';
}
}
if (color) line.style.color = color;
el.appendChild(line);
// Always scroll to the bottom so newest messages are visible
// even when the log overflows the viewable area
try { el.scrollTop = el.scrollHeight; } catch (_) {}
}
if(__DEBUG_WIN && !__DEBUG_WIN.closed){
__DEBUG_WIN.postMessage(msg, '*');
}
} catch(_) {}
}
/* ///////////// Section 3 — Utilities ///////////// */
/* ///////////// Section 3.A clamp / lerp / normAngleRad / deg2rad / rad2deg ///////////// */
function clamp(v, lo, hi){ return Math.max(lo, Math.min(hi, v)); }
function lerp(a,b,t){ return a + (b - a) * t; }
function normAngleRad(a){
// normalize to (-π, +π]
a = ((a + Math.PI) % (2*Math.PI) + 2*Math.PI) % (2*Math.PI) - Math.PI;
return a;
}
function deg2rad(d){ return d * Math.PI / 180; }
function rad2deg(r){ return r * 180 / Math.PI; }
/* ///////////// Section 4 — Switch Drag/Rotate Wiring (hit zones) ///////////// */
/* ///////////// Section 4.A Config & globals (switches, sensitivity, knobStates) ///////////// */
const switches = [
{ parentId:'52G_Switch', knobId:'Knob_52G', upperHitId:'Hit_52G_Upper', lowerHitId:'Hit_52G_Lower', type:'momentary', minAngle:-45, maxAngle:45 },
{ parentId:'41_Switch', knobId:'Knob_41', upperHitId:'Hit_41_Upper', lowerHitId:'Hit_41_Lower', type:'momentary', minAngle:-45, maxAngle:45 },
{ parentId:'86G_Switch', knobId:'Knob_86G', upperHitId:'Hit_86G_Upper', lowerHitId:'Hit_86G_Lower', type:'latching', minAngle:-45, maxAngle:0 },
{ parentId:'AVR_Switch', knobId:'Knob_AVR', upperHitId:'Hit_AVR_Upper', lowerHitId:'Hit_AVR_Lower', type:'latching', minAngle:-45, maxAngle:45 },
{ parentId:'Sync_Switch', knobId:'Knob_Sync', upperHitId:'Hit_Sync_Upper', lowerHitId:'Hit_Sync_Lower', type:'latching', minAngle:-45, maxAngle:45 },
{ parentId:'Master_Switch',knobId:'Knob_Master',upperHitId:'Hit_Master_Upper',lowerHitId:'Hit_Master_Lower',type:'momentary', minAngle:-45, maxAngle:45 },
{ parentId:'65_Switch', knobId:'Knob_65', upperHitId:'Hit_65_Upper', lowerHitId:'Hit_65_Lower', type:'momentary', minAngle:-45, maxAngle:45 },
{ parentId:'90_Switch', knobId:'Knob_90', upperHitId:'Hit_90_Upper', lowerHitId:'Hit_90_Lower', type:'momentary', minAngle:-45, maxAngle:45 },
];
const sensitivity = 0.5; // deg per px
const knobStates = {};
/* ///////////// Section 4.B Per-switch wiring & event handlers (setAngle/onMove/onUp) ///////////// */
switches.forEach(cfg => {
const svg = document.getElementById(cfg.parentId);
const knob = document.getElementById(cfg.knobId);
const hitU = document.getElementById(cfg.upperHitId);
const hitL = document.getElementById(cfg.lowerHitId);
if (!svg || !knob || !hitU || !hitL) return;
const bb = svg.getBBox();
const cx = bb.x + bb.width/2;
const cy = bb.y + bb.height/2;
// AVR knob starts at -45° (OFF); Sync knob at -45°; others at 0°
const initAngle =
(cfg.knobId === 'Knob_AVR') ? -45 :
(cfg.knobId === 'Knob_Sync') ? -45 : 0;
knobStates[cfg.knobId] = {
isDragging:false, startX:0, currentAngle:initAngle,
centerX:cx, centerY:cy, minAngle:cfg.minAngle, maxAngle:cfg.maxAngle, type:cfg.type
};
// Apply initial visual angle
knob.setAttribute('transform', `rotate(${initAngle} ${cx} ${cy})`);
function setAngle(knobId, ang){
knobStates[knobId].currentAngle = ang;
knob.setAttribute('transform', `rotate(${ang} ${cx} ${cy})`);
}
hitU.addEventListener('mousedown', (e)=>{
e.preventDefault();
knobStates[cfg.knobId].isDragging = true;
knobStates[cfg.knobId].startX = e.clientX;
document.addEventListener('mousemove', onMoveU);
document.addEventListener('mouseup', onUp);
});
function onMoveU(e){
if(!knobStates[cfg.knobId].isDragging) return;
const dx = e.clientX - knobStates[cfg.knobId].startX;
const ang = clamp(dx * sensitivity, cfg.minAngle, cfg.maxAngle);
setAngle(cfg.knobId, ang);
}
hitL.addEventListener('mousedown', (e)=>{
e.preventDefault();
knobStates[cfg.knobId].isDragging = true;
knobStates[cfg.knobId].startX = e.clientX;
document.addEventListener('mousemove', onMoveL);
document.addEventListener('mouseup', onUp);
});
function onMoveL(e){
if(!knobStates[cfg.knobId].isDragging) return;
const dx = e.clientX - knobStates[cfg.knobId].startX;
const ang = clamp(-dx * sensitivity, cfg.minAngle, cfg.maxAngle);
setAngle(cfg.knobId, ang);
}
function onUp(){
if(!knobStates[cfg.knobId].isDragging) return;
knobStates[cfg.knobId].isDragging = false;
// Momentary returns to defined angle (default 0);
// Latching snaps to nearest extreme
if (cfg.type === 'momentary') {
const returnAngle = (knobStates[cfg.knobId].momentaryReturnAngle != null)
? knobStates[cfg.knobId].momentaryReturnAngle
: 0;
setAngle(cfg.knobId, returnAngle);
} else {
const curr = knobStates[cfg.knobId].currentAngle;
const mid = (cfg.minAngle + cfg.maxAngle) / 2;
setAngle(cfg.knobId, curr >= mid ? cfg.maxAngle : cfg.minAngle);
}
document.removeEventListener('mousemove', onMoveU);
document.removeEventListener('mousemove', onMoveL);
document.removeEventListener('mouseup', onUp);
}
});
/* ///////////// Section 4.C angleOf helper ///////////// */
function angleOf(id){
try{
if (knobStates[id] && typeof knobStates[id].currentAngle === 'number') return knobStates[id].currentAngle;
}catch(_){}
const el = document.getElementById(id);
if(!el) return 0;
const t = el.getAttribute('transform') || '';
const m = t.match(/rotate\(([-\d.]+)/);
return m ? parseFloat(m[1]) : 0;
}
/* ///////////// Section 5 — Simulator Core (IIFE) ///////////// */
(function(){
/* ///////////// Section 5.A Rated constants (RATED) ///////////// */
const RATED = {
KV_LL: 13.8, // kV line-line nominal
MVA: 25, // MVA rating
MW: 23.5, // continuous MW
MVAR_LAG_MAX: 15.5, // +Q
MVAR_LEAD_MAX: 19.4, // -Q
AMPS: 1046.9 // at 13.8kV
};
/* ///////////// Section 5.B State object (state) + exposure ///////////// */
const state = {
Master_Started:false,
AVR_On:false,
Sync_On:false,
// Latches TRUE when 52G closes; does NOT auto-unlatch on open
GeneratorOnline:false,
'41_Brk_Var':false, // field breaker
'52G_Brk_Var':false, // gen breaker (FALSE=open, TRUE=closed)
'86G_Trip_Var':false,
Gate_Pos_Var:0, // %
Gen_Freq_Var:0, // Hz
Gen_RPM_Var:0, // calc
Speed_Perm_Var:false,
SyncCheck_Perm_Var:false,
// Bus references
Bus_Freq_Hz:60,
Bus_Voltage_kV:13.8,
// Voltage model
Gen_kV_Var:0, // actual terminal kV “inside the 52G”
Gen_kV_SP:13.5, // operator/AVR setpoint (kV)
// Power model
MW:0,
MVAR:0,
AMPS:0,
PF:0
};
try{ window.SimState = state; }catch(_){}
/* ///////////// Section 5.C Gate setpoint variable (Gate_Setpoint) ///////////// */
let Gate_Setpoint = 0;
/* ///////////// Section 5.D Master start/stop ramps (gateRamp/stopRamp) ///////////// */
const gateRamp = { active:false, from:0, to:19.67, dur:3000, t0:0 };
const stopRamp = { active:false, from:0, to:0, dur:0, t0:0 }; // ramp-to-zero on STOP
// Shutdown ramp durations (ms)
// Normal: Master STOP; Trip: protective trip or 86G lockout
const STOP_RAMP_NORMAL_MS = 2000;
const STOP_RAMP_TRIP_MS = 500;
const KV_RAMP_MS = 3000;
const kvRamp = { active:false, from:0, to:0, dur:KV_RAMP_MS, t0:0 }; // kV ramp on field breaker
/* ///////////// Section 5.E Voltage slew params (KV_* constants) ///////////// */
const KV_SLEW_MANUAL = 2; // kV/s tracking rate to SP (manual)
const KV_SLEW_AUTO = 1.2; // kV/s when AVR is acting
const KV_MIN = 0.0, KV_MAX = 16.0;
/* Frequency tuning (single-mode) */
const FREQ_GATE_THRESH_PCT = 20; // gate % breakpoint for frequency
const FREQ_GATE_LOW_HZ_PER_PCT = 3; // Hz per % gate below threshold
const FREQ_GATE_HIGH_HZ_PER_PCT = 0.375; // Hz per % gate above threshold
const FREQ_GATE_HIGH_INTERCEPT_HZ = 52.5; // offset for high range
const FREQ_DECEL_HZ_S = 3; // fixed fall rate (Hz/s) when raw < current
const FREQ_DECEL_SLOW_THRESH_HZ = 20; // Hz threshold to slow decel
const FREQ_DECEL_SLOW_HZ_S = FREQ_DECEL_HZ_S / .25; // half-rate below threshold
// AVR line-drop compensation (disabled if 0)
const AVR_LDC_PU = 0.00;
// Power mapping: physical gate needed for ~0 MW when paralleled
const NO_LOAD_GATE_PCT = 20; // set near your sync gate (e.g., 18–20)
const REV_PWR_LIMIT_MW = -5; // cap reverse power (negative)
// Manual-close PF/MW targets (used when AVR is OFF, at 52G close)
const CLOSE_REV_PWR_TARGET_MW = -0.01;
const CLOSE_PF_TARGET = -0.95;
// Small manual kV bias at close (optional)
const MANUAL_CLOSE_V_BIAS_KV = 12.5;
// kV droop in manual mode at full load (approx)
const MANUAL_KV_DROOP = 0.4;
// Reactive gain shaping (keeps vars small near zero load)
const Q_GAIN_MIN = 2.0; // MVAR per kV near zero-load
const Q_GAIN_MAX = 30.0; // MVAR per kV at high load
const Q_GAIN_SHAPE_N = 1.0; // 1=linear
// ---------- Loss Of Excitation (41 OPEN while 52G CLOSED) tunables ----------
const LOE_REV_PWR_MW = -0.4; // small reverse MW on field loss
const LOE_Q_IMPORT_MVAR = 15.0; // inductive vars imported on field loss (+ = lagging)
const LOE_SETTLE_MS = 250; // time constant to settle MW/Q to targets
/* ///////////// Section 5.F Angle watch thresholds & maps (THRESH/WATCH/prevAngles) ///////////// */
const THRESH = { up:20, down:-20 };
const WATCH = [
{ knobIds:['Knob_Master'], upper:'MASTER_START', lower:'MASTER_STOP' },
{ knobIds:['Knob_AVR'], upper:'AVR_ON', lower:'AVR_OFF' },
{ knobIds:['Knob_Sync'], upper:'SYNC_ON', lower:'SYNC_OFF' },
{ knobIds:['Knob_41'], upper:'41_CLOSE', lower:'41_OPEN' },
{ knobIds:['Knob_52G'], upper:'52G_CLOSE', lower:'52G_OPEN' },
{ knobIds:['Knob_86G','Knob_86'], special:'86G' },
];
const prevAngles = Object.create(null);
/* ///////////// Section 5.G setFlag86 ///////////// */
function setFlag86(){
try{
const el = document.getElementById('Flag_86G');
if (!el) return;
const k = (typeof knobStates !== 'undefined') ? knobStates['Knob_86G'] : null;
const angle = (k && typeof k.currentAngle === 'number') ? k.currentAngle : 0;
// < -44° = dark orange; > -1° = default color
el.style.fill = (angle < -44) ? '#CD5A00' : '';
}catch(_){}
}
/* ///////////// Section 5.H handleAction (all operator actions) ///////////// */
function handleAction(tag){
switch(tag){
/* ---- Syncroscope switch ---- */
case 'SYNC_ON':
if (!state.Sync_On){ state.Sync_On = true; try { logDebug('SYNCROSCOPE: ON'); } catch(_){} }
break;
case 'SYNC_OFF':
if (state.Sync_On){ state.Sync_On = false; try { logDebug('SYNCROSCOPE: OFF'); } catch(_){} }
break;
/* ---- AVR switch ---- */
case 'AVR_ON':
if (!state.AVR_On){
state.AVR_On = true;
try {
const sp = (+state.Gen_kV_SP || 0).toFixed(2);
const kv = (+state.Gen_kV_Var || 0).toFixed(2);
logDebug(`AVR: AUTO (SP: ${sp} kV, V: ${kv} kV)`);
} catch(_){}
}
break;
case 'AVR_OFF':
if (state.AVR_On){
state.AVR_On = false;
try {
const sp = (+state.Gen_kV_SP || 0).toFixed(2);
const kv = (+state.Gen_kV_Var || 0).toFixed(2);
logDebug(`AVR: MANUAL (SP: ${sp} kV, V: ${kv} kV)`);
} catch(_){}
}
break;
/* ---- Master ---- */
case 'MASTER_START': {
// 86G knob permissive + not tripped
const k86 = (typeof knobStates !== 'undefined') ? knobStates['Knob_86G'] : null;
const ang86 = (k86 && typeof k86.currentAngle === 'number') ? k86.currentAngle : 0;
if (ang86 <= -1){ try{ logDebug('Master: BLOCKED (86G Permissive)'); }catch(_){ } break; }
if (state['86G_Trip_Var']){ try{ logDebug('Master: BLOCKED (86G: Trip)'); }catch(_){ } break; }
// prevent double staging
if (state.__prestartBusy) break;
state.__prestartBusy = true;
if (!Array.isArray(state.__prestartTimers)) state.__prestartTimers = [];
const T = state.__prestartTimers;
const at = (ms, fn) => T.push(setTimeout(fn, ms));
try{ logDebug('Master: START'); }catch(_){}
// +1.0s — permissives
at(1000, () => {
const perm52 = !state['52G_Brk_Var'];
if (perm52) try{ logDebug('52G Permissive OK'); }catch(_){}
const k86s = knobStates?.['Knob_86G'];
const ang86s = (k86s && typeof k86s.currentAngle === 'number') ? k86s.currentAngle : 0;
if (ang86s > -1) try{ logDebug('86G Permissive OK'); }catch(_){}
});
// +2.0s — lift pump
at(2000, () => { try{ logDebug('Lift Pump On'); }catch(_){}; });
// +4.0s — pressure ok
at(4000, () => { try{ logDebug('Lift Pump Pressure OK'); }catch(_){}; });
// +5.0s — brakes release
at(5000, () => { try{ logDebug('Brakes Released'); }catch(_){}; });
// +5.1s — handoff to normal start
at(5100, () => {
const k86f = knobStates?.['Knob_86G'];
const ang86f = (k86f && typeof k86f.currentAngle === 'number') ? k86f.currentAngle : 0;
const ok86 = (ang86f > -1) && !state['86G_Trip_Var'];
if (!ok86){ state.__prestartBusy = false; return; }
if (!state.Master_Started){
state.Master_Started = true;
stopRamp.active = false;
gateRamp.active = true;
gateRamp.from = (typeof Gate_Setpoint === 'number') ? Gate_Setpoint : 0;
gateRamp.to = 19.67;
gateRamp.dur = 3000;
gateRamp.t0 = performance.now();
}
state.__prestartBusy = false;
while (T.length) clearTimeout(T.pop());
});
break;
}
case 'MASTER_STOP': {
try{ logDebug('Master: STOP'); }catch(_){}
// cancel any staged start sequence
state.__prestartBusy = false;
if (Array.isArray(state.__prestartTimers)) {
while (state.__prestartTimers.length) clearTimeout(state.__prestartTimers.pop());
}
// start STOP ramp
gateRamp.active = false;
stopRamp.active = true;
stopRamp.from = (typeof Gate_Setpoint === 'number') ? Gate_Setpoint : 0;
stopRamp.to = 0;
stopRamp.dur = STOP_RAMP_NORMAL_MS;
stopRamp.t0 = performance.now();
break;
}
/* ---- 41 Field breaker ---- */
case '41_CLOSE':
if(!state['41_Brk_Var']){
if (state.Speed_Perm_Var){
state['41_Brk_Var'] = true;
try{ logDebug('Field Breaker: CLOSED'); }catch(_){}
if (state['52G_Brk_Var'] && state.AVR_On){
state.Gen_kV_SP = 13.8;
} else {
state.Gen_kV_SP = 13.34;
}
kvRamp.active = true;
kvRamp.from = state.Gen_kV_Var;
kvRamp.to = state.Gen_kV_SP;
kvRamp.dur = KV_RAMP_MS;
kvRamp.t0 = performance.now();
} else {
try{ logDebug('41: BLOCKED'); }catch(_){}
}
}
break;
case '41_OPEN':
if(state['41_Brk_Var']){
state['41_Brk_Var'] = false;
try{ logDebug('Field Breaker: OPEN'); }catch(_){}
kvRamp.active = true;
kvRamp.from = state.Gen_kV_Var;
kvRamp.to = 0;
kvRamp.dur = KV_RAMP_MS;
kvRamp.t0 = performance.now();
}
break;
/* ---- 52G Generator breaker ---- */
case '52G_CLOSE':
if(!state['52G_Brk_Var']){
if (state.SyncCheck_Perm_Var){
state['52G_Brk_Var'] = true;
try{ logDebug('52G: CLOSED'); }catch(_){}
// Latch "online" (no auto-unlatch)
if (!state.GeneratorOnline){
state.GeneratorOnline = true;
try { logDebug('Unit Online'); } catch(_){}
}
// No-load calibration at close
(function(){
const slope = 100 / Math.max(1e-3, (100 - NO_LOAD_GATE_PCT));
const MWpu = (CLOSE_REV_PWR_TARGET_MW) / (RATED.MW || 1);
const effNeededPct = (MWpu * 100) / slope; // negative for reverse
state.NoLoadGateCal = clamp(state.Gate_Pos_Var - effNeededPct, 0, 100);
})();
if (state.AVR_On){
state.Gen_kV_SP = 13.8;
state.Gen_kV_Var = state.Gen_kV_SP;
} else {
// manual bias at close
const S = (RATED.MVA || 1);
const pAbs = Math.abs(CLOSE_REV_PWR_TARGET_MW);
const qAbs = Math.sqrt(Math.max(0, S*S - pAbs*pAbs));
const qTarget = (CLOSE_PF_TARGET < 0 ? -qAbs : qAbs);
const qGainClose = Q_GAIN_MIN;
let dvBias = (qGainClose > 1e-6) ? (qTarget / qGainClose) : 0;
dvBias = clamp(dvBias + MANUAL_CLOSE_V_BIAS_KV, -0.5, 0.5);
state.Gen_kV_SP = Math.max(0, (+state.Gen_kV_SP || 0) + dvBias);
state.Gen_kV_Var = state.Gen_kV_SP;
}
}
}
break;
case '52G_OPEN':
if(state['52G_Brk_Var']){
state['52G_Brk_Var'] = false;
delete state.NoLoadGateCal;
try{ logDebug('52G: OPEN'); }catch(_){}
}
break;
/* ---- 86G Lockout ---- */
case '86G_TRIP':
if(!state['86G_Trip_Var']){
state['86G_Trip_Var'] = true;
setFlag86(true);
if(state['41_Brk_Var']){ state['41_Brk_Var'] = false; try{ logDebug('Field Breaker: Trip'); }catch(_){} }
if(state['52G_Brk_Var']){ state['52G_Brk_Var'] = false; try{ logDebug('Generator Breaker: Trip'); }catch(_){} }
gateRamp.active = false;
stopRamp.active = true;
stopRamp.from = (typeof Gate_Setpoint === 'number') ? Gate_Setpoint : 0;
stopRamp.to = 0;
stopRamp.dur = STOP_RAMP_TRIP_MS;
stopRamp.t0 = performance.now();
try{ logDebug('86G: TRIP'); }catch(_){}
}
break;
case '86G_RESET':
if(state['86G_Trip_Var']){
state['86G_Trip_Var'] = false;
setFlag86(false);
try{ logDebug('86G: RESET'); }catch(_){}
}
break;
}
}
/* ///////////// Section 5.H.1 Enforce 86G Permissive on MASTER_START (add-on) ///////////// */
(function Enforce86GPermOnStart(){
const S = window.SimState || window.state || (window.state = {});
const _old = window.handleAction;
function readKnob86Angle(){
// preferred: knobStates cache
const ks = (window.knobStates && window.knobStates['Knob_86G']) || null;
if (ks && typeof ks.currentAngle === 'number') return ks.currentAngle;
// fallback: parse DOM transform
const el = document.getElementById('Knob_86G');
if (el){
const tr = el.getAttribute('transform') || '';
const m = tr.match(/rotate\((-?\d+(?:\.\d+)?)/i);
if (m) return parseFloat(m[1]);
}
return 0; // default safe
}
function get86GPermissive(){
const ang = readKnob86Angle();
const perm = (ang > -1); // same threshold used for Glow_Perm_86G
S['86G_Perm_Var'] = perm; // keep a state bit for reuse
return perm;
}
window.handleAction = function(tag){
if (tag === 'MASTER_START'){
// Block if 86G knob not in NORMAL or lockout is tripped
const perm86 = get86GPermissive();
if (!perm86){
try{ logDebug('Master: BLOCKED (86G Permissive)'); }catch(_){}
return;
}
if (S['86G_Trip_Var'] === true){
try{ logDebug('Master: BLOCKED (86G Tripped)'); }catch(_){}
return;
}
}
return _old ? _old.apply(this, arguments) : undefined;
};
})();
/* ///////////// Section 5.H.2 AVR takeover harmonizer (non-invasive) ///////////// */
(function AVRTakeoverHarmonizer(){
if (typeof window === 'undefined') return;
const origHandle = (typeof window.handleAction === 'function') ? window.handleAction : null;
if (!origHandle) return;
window.handleAction = function(tag){
// Pre-transition context
const wasAVR = !!(state && state.AVR_On);
// On enabling AVR, restore previously stored SP or freeze to present kV
if (tag === 'AVR_ON' && !wasAVR && state) {
if (typeof state.__avrStoredSP === 'number') {
state.Gen_kV_SP = clamp(state.__avrStoredSP, KV_MIN, KV_MAX);
} else {
const kv = +state.Gen_kV_Var || 0;
if (typeof clamp === 'function') {
state.Gen_kV_SP = clamp(kv, KV_MIN, KV_MAX);
} else {
state.Gen_kV_SP = Math.min(KV_MAX, Math.max(KV_MIN, kv));
}
}
}
// On disabling AVR, store SP and freeze setpoint at present kV
if (tag === 'AVR_OFF' && wasAVR && state) {
state.__avrStoredSP = state.Gen_kV_SP;
const kv = Math.max(0, +state.Gen_kV_Var || 0);
state.Gen_kV_SP = kv; // freeze setpoint to present kV
state.Gen_kV_Var = kv;
try {
const kvStr = kv.toFixed(2);
logDebug(`AVR transition to MANUAL: V ${kvStr} kV`);
} catch(_){}
}
// Delegate to original action handler
return origHandle.call(this, tag);
};
})();
/* ///////////// Section 5.I watchAngles ///////////// */
function watchAngles(){
for(const w of WATCH){
let ang = null;
for(const id of w.knobIds){
const a = angleOf(id);
if(a !== 0 || document.getElementById(id)){ ang = a; break; }
}
if(ang === null) continue;
const key = w.knobIds[0];
const prev = prevAngles[key] ?? ang;
if (w.special === '86G'){
const T = -1; // degrees
const hadPrev = Object.prototype.hasOwnProperty.call(prevAngles, key);
if (!hadPrev){
// First run: align state to current knob position
if (ang <= T) { handleAction('86G_TRIP'); }
else { handleAction('86G_RESET'); }
} else {
// Subsequent frames: edge-detect crossings
if (prev > T && ang <= T) handleAction('86G_TRIP');
if (prev <= T && ang > T) handleAction('86G_RESET');
}
} else {
const S = window.SimState || window.state || (window.state = {});
if (prev < THRESH.up && ang >= THRESH.up) {
if (w.upper === 'MASTER_START' && S.MasterStopMask) {
S.MasterStopMask = false;
}
handleAction(w.upper);
}
if (prev > THRESH.down && ang <= THRESH.down) {
if (w.lower === 'MASTER_STOP' && !S.MasterStopMask) {
S.MasterStopMask = true;
}
handleAction(w.lower);
}
}
prevAngles[key] = ang;
}
}
/* ///////////// Section 5.J updateGateSet (Knob_65) ///////////// */
function updateGateSet(){
const NUDGE_THRESH = 20; // degrees
const NUDGE_RATE_OPEN = 0.125; // %/s when 52G OPEN
const NUDGE_RATE_CLOSED = 10; // %/s when 52G CLOSED
const NUDGE_RATE = state['52G_Brk_Var'] ? NUDGE_RATE_CLOSED : NUDGE_RATE_OPEN;
const now = performance.now();
if (typeof updateGateSet._tPrev !== 'number') updateGateSet._tPrev = now;
const dt = Math.min(150, now - updateGateSet._tPrev) / 1000;
updateGateSet._tPrev = now;
const a65 = angleOf('Knob_65') || 0;
if (a65 >= NUDGE_THRESH){
Gate_Setpoint = Math.min(100, Gate_Setpoint + NUDGE_RATE * dt);
if (updateGateSet._lastLog == null || Math.abs(Gate_Setpoint - updateGateSet._lastLog) >= 0.5){
updateGateSet._lastLog = Gate_Setpoint;
}
} else if (a65 <= -NUDGE_THRESH){
Gate_Setpoint = Math.max(0, Gate_Setpoint - NUDGE_RATE * dt);
if (updateGateSet._lastLog == null || Math.abs(Gate_Setpoint - updateGateSet._lastLog) >= 0.5){
updateGateSet._lastLog = Gate_Setpoint;
}
}
}
/* ///////////// Section 5.K updateVoltageSet (Knob_90) ///////////// */
function updateVoltageSet(){
const TH = 20; // deg threshold
const now = performance.now();
if (typeof updateVoltageSet._tPrev !== 'number') updateVoltageSet._tPrev = now;
const dt = Math.min(150, now - updateVoltageSet._tPrev) / 1000;
updateVoltageSet._tPrev = now;
const a90 = angleOf('Knob_90') || 0;
const NU = 0.01; // sensitivity
const RATE = NU * (KV_MAX - KV_MIN); // kV/s equivalent
if (a90 >= TH){
state.Gen_kV_SP = state.AVR_On
? clamp(state.Gen_kV_SP + RATE*dt, KV_MIN, KV_MAX)
: Math.max(0, state.Gen_kV_SP + RATE*dt); // no upper limit when AVR OFF
} else if (a90 <= -TH){
state.Gen_kV_SP = state.AVR_On
? clamp(state.Gen_kV_SP - RATE*dt, KV_MIN, KV_MAX)
: Math.max(0, state.Gen_kV_SP - RATE*dt); // allow down to 0 only
}
}
/* ///////////// Section 5.L updateSyncCheck (sync permissive) ///////////// */
function updateSyncCheck(){
const V_TOL_FRAC = 0.03; // ±3%
const F_TOL_HZ = 0.15; // ±0.15 Hz
const PHASE_DEG = 10.0; // ±10°
const snap = PhaseTracker.snap;
const vb = snap ? snap.vb : (+state.Bus_Voltage_kV || 13.8);
const vg = snap ? snap.vg : (+state.Gen_kV_Var || 0);
const fb = snap ? snap.fb : (+state.Bus_Freq_Hz || 60);
const fg = snap ? snap.fg : (+state.Gen_Freq_Var || 0);
const ddeg = snap ? snap.dphiDeg : PhaseTracker.deltaDeg();
const vOK = Math.abs(vg - vb) <= V_TOL_FRAC * vb;
const fOK = Math.abs(fg - fb) <= F_TOL_HZ;
const pOK = Math.abs(ddeg) <= PHASE_DEG;
const ok = !!(vOK && fOK && pOK);
if (ok !== state.SyncCheck_Perm_Var){
state.SyncCheck_Perm_Var = ok;
}
}
/* ///////////// Section 5.M updatePhysics (governor/AVR/power) ///////////// */
function updatePhysics(){
if (!state.Master_Started) {
gateRamp.active = false;
stopRamp.active = false;
state.Gen_Freq_Var = 0;
state.Gen_RPM_Var = 0;
const targetKV = state['41_Brk_Var'] ? state.Gen_kV_SP : 0;
slewGenKV(targetKV, KV_SLEW_MANUAL);
if (state.Speed_Perm_Var !== false) {
state.Speed_Perm_Var = false;
}
// reset run latch
updatePhysics._wasRunning = false;
state.MW = 0; state.MVAR = 0; state.AMPS = 0; state.PF = 0;
return;
}
// Gate setpoint ramps
if (gateRamp.active){
const p = clamp((performance.now() - gateRamp.t0) / gateRamp.dur, 0, 1);
Gate_Setpoint = gateRamp.from + (gateRamp.to - gateRamp.from) * p;
if (p >= 1){
gateRamp.active = false;
}
} else if (stopRamp.active){
const p = clamp((performance.now() - stopRamp.t0) / stopRamp.dur, 0, 1);
Gate_Setpoint = stopRamp.from + (stopRamp.to - stopRamp.from) * p;
if (p >= 1){
stopRamp.active = false;
Gate_Setpoint = 0;
// do NOT snap gates/freq or clear Master_Started here
}
}
// Governor: actual gate follows setpoint. Rates differ for normal vs. trip shutdowns.
const GATE_SLEW = {
NORMAL: 6 / 1000, // %/ms (≈20 %/s) — normal shutdown
TRIP: 8 / 1000 // %/ms (≈80 %/s) — trip
};
const isTripSlew = !!(
state['86G_Trip_Var'] ||
state.Trip_32 || state.Trip_40 || state.Trip_27_59 || state.Trip_81
);
const rate = isTripSlew ? GATE_SLEW.TRIP : GATE_SLEW.NORMAL;
const now = performance.now();
if (typeof updatePhysics._tPrev !== 'number') updatePhysics._tPrev = now;
const dt = Math.min(100, now - updatePhysics._tPrev);
updatePhysics._tPrev = now;
const maxStep = rate * dt;
const err = Gate_Setpoint - state.Gate_Pos_Var;
if (Math.abs(err) > maxStep){
state.Gate_Pos_Var += Math.sign(err) * maxStep;
} else {
state.Gate_Pos_Var = Gate_Setpoint;
}
// During a Master Stop, automatically open breakers once gates fall low
if (state.MasterStopMask && state.Gate_Pos_Var <= FREQ_GATE_THRESH_PCT) {
if (state['52G_Brk_Var']) handleAction('52G_OPEN');
if (state['41_Brk_Var']) handleAction('41_OPEN');
}
/// Frequency (single-owner slew): on-grid=60; off-grid rises follow gate; falls decay at fixed rate
{
const onGrid = !!state['52G_Brk_Var'];
let raw;
if (onGrid) {
raw = 60;
} else {
const gate = state.Gate_Pos_Var;
raw = (gate <= FREQ_GATE_THRESH_PCT)
? FREQ_GATE_LOW_HZ_PER_PCT * gate
: (FREQ_GATE_HIGH_HZ_PER_PCT * gate + FREQ_GATE_HIGH_INTERCEPT_HZ);
}
const curr = +state.Gen_Freq_Var || 0;
const dt_s = Math.max(0, dt) / 1000;
const decelRate = (curr > FREQ_DECEL_SLOW_THRESH_HZ)
? FREQ_DECEL_HZ_S
: FREQ_DECEL_SLOW_HZ_S;
const next = (raw >= curr) ? raw : Math.max(raw, curr - decelRate * dt_s);
state.Gen_Freq_Var = clamp(next, 0, 94);
state.Gen_RPM_Var = state.Gen_Freq_Var * 1.667;
// Log major stopping events based on frequency thresholds
if (!state['52G_Brk_Var'] && state.Master_Started && curr > state.Gen_Freq_Var) {
if (!updatePhysics._liftPumpLogged && curr >= 40 && state.Gen_Freq_Var < 40) {
try { logDebug('Lift Pump On'); } catch (_) {}
updatePhysics._liftPumpLogged = true;
}
if (!updatePhysics._brakesLogged && curr >= 20 && state.Gen_Freq_Var < 20) {
try { logDebug('Brakes Applied'); } catch (_) {}
updatePhysics._brakesLogged = true;
}
} else {
updatePhysics._liftPumpLogged = false;
updatePhysics._brakesLogged = false;
}
}
// Mark as having run (prevents immediate "Unit Stopped" right after Master Start)
{
if (state.Master_Started && (state.Gate_Pos_Var > 0.5 || state.Gen_Freq_Var > 0.2)) {
updatePhysics._wasRunning = true;
}
}
// Latch "Unit Stopped" only when we are actually stopping AND gates ~0 AND frequency ~0 (off-grid)
{
const CLOSE_LATCH_EPS = 0.5; // %
const FREQ_LATCH_EPS = 0.2; // Hz
const stoppingIntent = stopRamp.active || state['86G_Trip_Var'] || !!updatePhysics._wasRunning;
if (!state['52G_Brk_Var'] &&
state.Master_Started &&
stoppingIntent &&
state.Gate_Pos_Var <= CLOSE_LATCH_EPS &&
state.Gen_Freq_Var <= FREQ_LATCH_EPS) {
state.Gate_Pos_Var = 0;
state.Master_Started = false;
stopRamp.active = false;
updatePhysics._wasRunning = false;
// Mark generator offline so protections don't evaluate after a normal stop
state.GeneratorOnline = false;
logDebug('Unit Stopped');
}
}
// Speed Permissive toggle
const spNext = !!(state.Master_Started && (state.Gate_Pos_Var > 0) && (state.Gen_RPM_Var > 50));
if (state.Speed_Perm_Var !== spNext) {
state.Speed_Perm_Var = spNext;
}
// AVR & kV tracking
const Vbus = state.Bus_Voltage_kV || 13.8;
if (state['52G_Brk_Var']) {
if (state.AVR_On){
let kvTargetSP = state.Gen_kV_SP;
slewGenKV(kvTargetSP, KV_SLEW_AUTO);
} else {
// MANUAL: allow small voltage droop with load
const noLoad = (typeof state.NoLoadGateCal === 'number') ? state.NoLoadGateCal : NO_LOAD_GATE_PCT;
const effGatePU = clamp(
(state.Gate_Pos_Var - noLoad) / Math.max(1e-3, (100 - NO_LOAD_GATE_PCT)),
0, 1
);
const kvTarget = state.Gen_kV_SP - MANUAL_KV_DROOP * effGatePU;
slewGenKV(kvTarget, KV_SLEW_MANUAL);
}
} else {
// Not paralleled: track SP if field on, else decay to 0
const tgt = state['41_Brk_Var'] ? state.Gen_kV_SP : 0;
slewGenKV(tgt, KV_SLEW_MANUAL);
}
// Power model (with no-load gate offset; does NOT move the gates)
let MW = 0;
if (state['52G_Brk_Var']){
const noLoad = (typeof state.NoLoadGateCal === 'number') ? state.NoLoadGateCal : NO_LOAD_GATE_PCT;
const effGate = state.Gate_Pos_Var - noLoad; // can be negative
const slope = 100 / Math.max(1e-3, (100 - NO_LOAD_GATE_PCT)); // keep 100% gate => rated MW
let MW_pu = (effGate * slope) / 100; // per-unit MW
const min_pu = (REV_PWR_LIMIT_MW) / (RATED.MW || 1);
MW_pu = clamp(MW_pu, min_pu, 1);
MW = MW_pu * RATED.MW;
}
// Reactive power: scale gain with effective gate so MVAR is small at close
let Q = 0;
if (state['52G_Brk_Var']){
const dv = (state.Gen_kV_Var - Vbus); // kV
const noLoad = (typeof state.NoLoadGateCal === 'number') ? state.NoLoadGateCal : NO_LOAD_GATE_PCT;
const effGatePU = clamp(
(state.Gate_Pos_Var - noLoad) / Math.max(1e-3, (100 - NO_LOAD_GATE_PCT)),
0, 1
);
const qGain = Q_GAIN_MIN + (Q_GAIN_MAX - Q_GAIN_MIN) * Math.pow(effGatePU, Q_GAIN_SHAPE_N);
Q = clamp(dv * qGain, -RATED.MVAR_LEAD_MAX, RATED.MVAR_LAG_MAX);
}
// -------- Loss Of Excitation override (52G CLOSED & 41 OPEN) ----------
if (state['52G_Brk_Var'] && !state['41_Brk_Var']){
const t = performance.now();
if (typeof updatePhysics._loeT0 !== 'number') updatePhysics._loeT0 = t;
const α = Math.min(1, (t - updatePhysics._loeT0) / LOE_SETTLE_MS);
// Blend current values toward LOE targets
MW = MW + (LOE_REV_PWR_MW - MW) * α; // small reverse
Q = Q + (LOE_Q_IMPORT_MVAR - Q) * α; // inductive import (positive)
} else {
delete updatePhysics._loeT0;
}