-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathCAPI_Bus.pas
1293 lines (1185 loc) · 43.3 KB
/
CAPI_Bus.pas
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
unit CAPI_Bus;
interface
uses
CAPI_Utils,
CAPI_Types;
function Bus_Get_Name(): PAnsiChar; CDECL;
function Bus_Get_NumNodes(): Integer; CDECL;
procedure Bus_Get_SeqVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_SeqVoltages_GR(); CDECL;
procedure Bus_Get_Voltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Voltages_GR(); CDECL;
procedure Bus_Get_Nodes(var ResultPtr: PInteger; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Nodes_GR(); CDECL;
procedure Bus_Get_Isc(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Isc_GR(); CDECL;
procedure Bus_Get_Voc(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Voc_GR(); CDECL;
function Bus_Get_kVBase(): Double; CDECL;
procedure Bus_Get_puVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_puVoltages_GR(); CDECL;
procedure Bus_Get_Zsc0(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Zsc0_GR(); CDECL;
procedure Bus_Get_Zsc1(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_Zsc1_GR(); CDECL;
procedure Bus_Get_ZscMatrix(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_ZscMatrix_GR(); CDECL;
function Bus_ZscRefresh(): TAPIBoolean; CDECL;
procedure Bus_Get_YscMatrix(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_YscMatrix_GR(); CDECL;
function Bus_Get_Coorddefined(): TAPIBoolean; CDECL;
function Bus_Get_x(): Double; CDECL;
procedure Bus_Set_x(Value: Double); CDECL;
function Bus_Get_y(): Double; CDECL;
procedure Bus_Set_y(Value: Double); CDECL;
function Bus_Get_Distance(): Double; CDECL;
function Bus_GetUniqueNodeNumber(StartNumber: Integer): Integer; CDECL;
procedure Bus_Get_CplxSeqVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_CplxSeqVoltages_GR(); CDECL;
function Bus_Get_Int_Duration(): Double; CDECL;
function Bus_Get_Lambda(): Double; CDECL;
function Bus_Get_Cust_Duration(): Double; CDECL;
function Bus_Get_Cust_Interrupts(): Double; CDECL;
function Bus_Get_N_Customers(): Integer; CDECL;
function Bus_Get_N_interrupts(): Double; CDECL;
procedure Bus_Get_puVLL(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_puVLL_GR(); CDECL;
procedure Bus_Get_VLL(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_VLL_GR(); CDECL;
procedure Bus_Get_puVmagAngle(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_puVmagAngle_GR(); CDECL;
procedure Bus_Get_VMagAngle(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_VMagAngle_GR(); CDECL;
function Bus_Get_TotalMiles(): Double; CDECL;
function Bus_Get_SectionID(): Integer; CDECL;
function Bus_Get_Next(): Integer; CDECL; // API Extension
procedure Bus_Get_LineList(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
procedure Bus_Get_LineList_GR(); CDECL;
procedure Bus_Get_LoadList(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
procedure Bus_Get_LoadList_GR(); CDECL;
procedure Bus_Get_ZSC012Matrix(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
procedure Bus_Get_ZSC012Matrix_GR(); CDECL;
procedure Bus_Get_AllPCEatBus(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
procedure Bus_Get_AllPDEatBus(var ResultPtr: PPAnsiChar; ResultCount: PAPISize); CDECL;
implementation
uses
CAPI_Constants,
DSSGlobals,
Circuit,
UComplex, DSSUcomplex,
MathUtil,
sysutils,
ExecHelper,
SolutionAlgs,
Utilities,
Bus,
CktElement,
Ucmatrix,
DSSClass,
DSSHelper;
//------------------------------------------------------------------------------
function _hasActiveBus(DSS: TDSSContext): Boolean; inline;
begin
Result := False;
if InvalidCircuit(DSS) then
Exit;
if (not ((DSS.ActiveCircuit.ActiveBusIndex > 0) and (DSS.ActiveCircuit.ActiveBusIndex <= DSS.ActiveCircuit.NumBuses))) or
(DSS.ActiveCircuit.Buses = NIL) then
begin
if DSS_CAPI_EXT_ERRORS then
begin
DoSimpleMsg(DSS, _('No active bus found! Activate one and retry.'), 8989);
end;
Exit;
end;
Result := True;
end;
//------------------------------------------------------------------------------
function _activeObj(DSS: TDSSContext; out obj: TDSSBus): Boolean; inline;
begin
Result := False;
obj := NIL;
if not _hasActiveBus(DSS) then
Exit;
obj := DSS.ActiveCircuit.Buses[DSS.ActiveCircuit.ActiveBusIndex];
Result := True;
end;
//------------------------------------------------------------------------------
function Bus_Get_Name(): PAnsiChar; CDECL;
begin
Result := NIL;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
Result := DSS_GetAsPAnsiChar(DSSPrime, BusList.NameOfIndex(ActiveBusIndex));
end;
//------------------------------------------------------------------------------
function Bus_Get_NumNodes(): Integer; CDECL;
begin
Result := 0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
Result := DSSPrime.ActiveCircuit.Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].NumNodesThisBus;
end;
//------------------------------------------------------------------------------
procedure Bus_Get_SeqVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Compute sequence voltages for Active Bus
// magnitude only
// returns a set of seq voltages (3)
var
Result: PDoubleArray0;
Nvalues, i, iV: Integer;
VPh, V012: Complex3;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := Buses^[ActiveBusIndex].NumNodesThisBus;
if Nvalues > 3 then
Nvalues := 3;
// Assume nodes 1, 2, and 3 are the 3 phases
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 3);
if Nvalues <> 3 then
for i := 1 to 3 do
Result[i - 1] := -1.0 // Signify seq voltages n/A for less then 3 phases
else
begin
iV := 0;
for i := 1 to 3 do
begin
Vph[i] := Solution.NodeV^[Buses^[ActiveBusIndex].Find(i)];
end;
Phase2SymComp(@Vph, @V012); // Compute Symmetrical components
for i := 1 to 3 do // Stuff it in the result
begin
Result[iV] := Cabs(V012[i]);
Inc(iV);
end;
end;
end
end;
procedure Bus_Get_SeqVoltages_GR(); CDECL;
// Same as Bus_Get_SeqVoltages but uses global result (GR) pointers
begin
Bus_Get_SeqVoltages(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Voltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return Complex for all nodes of voltages for Active Bus
var
Result: PDoubleArray0;
Nvalues, i, iV, NodeIdx, jj: Integer;
Volts: Complex;
pBus: TDSSBus;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := pBus.NumNodesThisBus;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
jj := 1;
with pBus do
for i := 1 to NValues do
begin
// this code so nodes come out in order from smallest to larges
repeat
NodeIdx := FindIdx(jj); // Get the index of the Node that matches jj
inc(jj)
until NodeIdx > 0;
Volts := Solution.NodeV^[GetRef(NodeIdx)]; // referenced to pBus
Result[iV] := Volts.re;
Inc(iV);
Result[iV] := Volts.im;
Inc(iV);
end;
end
end;
procedure Bus_Get_Voltages_GR(); CDECL;
// Same as Bus_Get_Voltages but uses global result (GR) pointers
begin
Bus_Get_Voltages(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Nodes(var ResultPtr: PInteger; ResultCount: PAPISize); CDECL;
// return array of node numbers corresponding to voltages
var
Result: PIntegerArray0;
Nvalues, i, iV, NodeIdx, jj: Integer;
pBus: TDSSBus;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit, pBus do
begin
Nvalues := NumNodesThisBus;
Result := DSS_RecreateArray_PInteger(ResultPtr, ResultCount, NValues);
iV := 0;
jj := 1;
for i := 1 to NValues do
begin
// this code so nodes come out in order from smallest to larges
repeat
NodeIdx := FindIdx(jj); // Get the index of the Node that matches jj
inc(jj)
until NodeIdx > 0;
Result[iV] := Buses^[ActiveBusIndex].GetNum(NodeIdx);
Inc(iV);
end;
end;
end;
procedure Bus_Get_Nodes_GR(); CDECL;
// Same as Bus_Get_Nodes but uses global result (GR) pointers
begin
Bus_Get_Nodes(DSSPrime.GR_DataPtr_PInteger, @DSSPrime.GR_Counts_PInteger[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Isc(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return the short circuit current
var
Result: PDoubleArray0;
Isc: Complex;
i, iV, NValues: Integer;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
if Buses^[ActiveBusIndex].BusCurrent <> NIL then
begin
NValues := Buses^[ActiveBusIndex].NumNodesThisBus;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
for i := 1 to NValues do
begin
Isc := Buses^[ActiveBusIndex].BusCurrent^[i];
Result[iV] := Isc.Re;
Inc(iV);
Result[iV] := Isc.Im;
Inc(iV);
end;
end
else
DefaultResult(ResultPtr, ResultCount);
end
end;
procedure Bus_Get_Isc_GR(); CDECL;
// Same as Bus_Get_Isc but uses global result (GR) pointers
begin
Bus_Get_Isc(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Voc(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return the Open circuit Voltage for this bus
var
Result: PDoubleArray0;
Voc: Complex;
i, iV, NValues: Integer;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
if Buses^[ActiveBusIndex].VBus <> NIL then
begin
NValues := Buses^[ActiveBusIndex].NumNodesThisBus;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
for i := 1 to NValues do
begin
Voc := Buses^[ActiveBusIndex].VBus^[i];
Result[iV] := Voc.Re;
Inc(iV);
Result[iV] := Voc.Im;
Inc(iV);
end;
end
else
DefaultResult(ResultPtr, ResultCount);
end
end;
procedure Bus_Get_Voc_GR(); CDECL;
// Same as Bus_Get_Voc but uses global result (GR) pointers
begin
Bus_Get_Voc(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
function Bus_Get_kVBase(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
Result := DSSPrime.ActiveCircuit.Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].kVBase;
end;
//------------------------------------------------------------------------------
procedure Bus_Get_puVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Returns voltages at bus in per unit. However, if kVBase=0, returns actual volts
var
Result: PDoubleArray0;
Nvalues, i, iV, NodeIdx, jj: Integer;
Volts: Complex;
BaseFactor: Double;
pBus: TDSSBus;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit, pBus do
begin
Nvalues := NumNodesThisBus;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
jj := 1;
if kVBase > 0.0 then
BaseFactor := 1000.0 * kVBase
else
BaseFactor := 1.0;
for i := 1 to NValues do
begin
// this code so nodes come out in order from smallest to larges
repeat
NodeIdx := FindIdx(jj); // Get the index of the Node that matches jj
inc(jj)
until NodeIdx > 0;
Volts := Solution.NodeV^[GetRef(NodeIdx)];
Result[iV] := Volts.re / BaseFactor;
Inc(iV);
Result[iV] := Volts.im / BaseFactor;
Inc(iV);
end;
end;
end;
procedure Bus_Get_puVoltages_GR(); CDECL;
// Same as Bus_Get_puVoltages but uses global result (GR) pointers
begin
Bus_Get_puVoltages(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Zsc0(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Z: Complex;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Z := Buses^[ActiveBusIndex].Zsc0;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2);
Result[0] := Z.Re;
Result[1] := Z.Im;
end
end;
procedure Bus_Get_Zsc0_GR(); CDECL;
// Same as Bus_Get_Zsc0 but uses global result (GR) pointers
begin
Bus_Get_Zsc0(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_Zsc1(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Z: Complex;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Z := Buses^[ActiveBusIndex].Zsc1;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2);
Result[0] := Z.Re;
Result[1] := Z.Im;
end
end;
procedure Bus_Get_Zsc1_GR(); CDECL;
// Same as Bus_Get_Zsc1 but uses global result (GR) pointers
begin
Bus_Get_Zsc1(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_ZscMatrix(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Nelements, iV, i, j: Integer;
Z: Complex;
begin
DefaultResult(ResultPtr, ResultCount);
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
Exit;
try
with DSSPrime.ActiveCircuit do
begin
if Assigned(Buses^[ActiveBusIndex].Zsc) then
begin
Nelements := Buses^[ActiveBusIndex].Zsc.Order;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * Nelements * Nelements);
iV := 0;
with Buses^[ActiveBusIndex] do
for i := 1 to Nelements do
for j := 1 to Nelements do
begin
Z := Zsc.GetElement(i, j);
Result[iV] := Z.Re;
Inc(iV);
Result[iV] := Z.Im;
Inc(iV);
end;
end
end
except
On E: Exception do
DoSimpleMsg(DSSPrime, 'ZscMatrix Error: %s', [E.message], 5016);
end;
end;
procedure Bus_Get_ZscMatrix_GR(); CDECL;
// Same as Bus_Get_ZscMatrix but uses global result (GR) pointers
begin
Bus_Get_ZscMatrix(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
function Bus_ZscRefresh(): TAPIBoolean; CDECL;
begin
Result := FALSE; // Init in case of failure
if DSSPrime.DSSExecutive.DoZscRefresh = 0 then
Result := TRUE;
end;
//------------------------------------------------------------------------------
procedure Bus_Get_YscMatrix(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Nelements, iV, i, j: Integer;
Y1: Complex;
begin
DefaultResult(ResultPtr, ResultCount);
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
Exit;
try
with DSSPrime.ActiveCircuit do
if Assigned(Buses^[ActiveBusIndex].Ysc) then
begin
Nelements := Buses^[ActiveBusIndex].Ysc.Order;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * Nelements * Nelements);
iV := 0;
with Buses^[ActiveBusIndex] do
for i := 1 to Nelements do
for j := 1 to Nelements do
begin
Y1 := Ysc.GetElement(i, j);
Result[iV] := Y1.Re;
Inc(iV);
Result[iV] := Y1.Im;
Inc(iV);
end;
end
except
On E: Exception do
DoSimpleMsg(DSSPrime, 'ZscMatrix Error: %s', [E.message], 5017);
end;
end;
procedure Bus_Get_YscMatrix_GR(); CDECL;
// Same as Bus_Get_YscMatrix but uses global result (GR) pointers
begin
Bus_Get_YscMatrix(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
function Bus_Get_Coorddefined(): TAPIBoolean; CDECL;
begin
Result := FALSE;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
if (Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].CoordDefined) then
Result := TRUE;
end;
//------------------------------------------------------------------------------
function Bus_Get_x(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
if (Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].CoordDefined) then
Result := Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].x;
end;
//------------------------------------------------------------------------------
procedure Bus_Set_x(Value: Double); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
begin
Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].CoordDefined := TRUE;
Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].x := Value;
end;
end;
//------------------------------------------------------------------------------
function Bus_Get_y(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
if (Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].CoordDefined) then
Result := Buses^[DSSPrime.ActiveCircuit.ActiveBusIndex].y;
end;
//------------------------------------------------------------------------------
procedure Bus_Set_y(Value: Double); CDECL;
begin
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if (ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses) then
begin
Buses^[ActiveBusIndex].CoordDefined := TRUE;
Buses^[ActiveBusIndex].y := Value;
end;
end;
//------------------------------------------------------------------------------
function Bus_Get_Distance(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ((ActiveBusIndex > 0) and (ActiveBusIndex <= NumBuses)) then
Result := Buses^[ActiveBusIndex].DistFromMeter;
end;
//------------------------------------------------------------------------------
function Bus_GetUniqueNodeNumber(StartNumber: Integer): Integer; CDECL;
begin
Result := 0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Utilities.GetUniqueNodeNumber(DSSPrime, BusList.NameOfIndex(ActiveBusIndex), StartNumber);
end;
//------------------------------------------------------------------------------
procedure Bus_Get_CplxSeqVoltages(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Compute sequence voltages for Active Bus
// Complex values
// returns a set of seq voltages (3) in 0, 1, 2 order
var
Result: PDoubleArray0;
Nvalues, i, iV: Integer;
VPh, V012: Complex3;
begin
if (InvalidCircuit(DSSPrime)) or
(not ((DSSPrime.ActiveCircuit.ActiveBusIndex > 0) and (DSSPrime.ActiveCircuit.ActiveBusIndex <= DSSPrime.ActiveCircuit.NumBuses))) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := Buses^[ActiveBusIndex].NumNodesThisBus;
if Nvalues > 3 then
Nvalues := 3;
// Assume nodes labelled 1, 2, and 3 are the 3 phases
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 6);
if Nvalues <> 3 then
for i := 1 to 6 do
Result[i - 1] := -1.0 // Signify seq voltages n/A for less then 3 phases
else
begin
iV := 0;
for i := 1 to 3 do
Vph[i] := Solution.NodeV^[Buses^[ActiveBusIndex].Find(i)];
Phase2SymComp(@Vph, @V012); // Compute Symmetrical components
for i := 1 to 3 do // Stuff it in the result
begin
Result[iV] := V012[i].re;
Inc(iV);
Result[iV] := V012[i].im;
Inc(iV);
end;
end;
end
end;
procedure Bus_Get_CplxSeqVoltages_GR(); CDECL;
// Same as Bus_Get_CplxSeqVoltages but uses global result (GR) pointers
begin
Bus_Get_CplxSeqVoltages(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
function Bus_Get_Int_Duration(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].Bus_Int_Duration;
end;
//------------------------------------------------------------------------------
function Bus_Get_Lambda(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].BusFltRate;
end;
//------------------------------------------------------------------------------
function Bus_Get_Cust_Duration(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].BusCustDurations;
end;
//------------------------------------------------------------------------------
function Bus_Get_Cust_Interrupts(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].BusCustDurations;
end;
//------------------------------------------------------------------------------
function Bus_Get_N_Customers(): Integer; CDECL;
begin
Result := 0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].BusTotalNumCustomers;
end;
//------------------------------------------------------------------------------
function Bus_Get_N_interrupts(): Double; CDECL;
begin
Result := 0.0;
if InvalidCircuit(DSSPrime) then
Exit;
with DSSPrime.ActiveCircuit do
if ActiveBusIndex > 0 then
Result := Buses^[ActiveBusIndex].Bus_Num_Interrupt;
end;
//------------------------------------------------------------------------------
procedure Bus_Get_puVLL(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Nvalues, i, iV, NodeIdxi, NodeIdxj, jj, k: Integer;
Volts: Complex;
pBus: TDSSBus;
BaseFactor: Double;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := pBus.NumNodesThisBus;
if Nvalues > 3 then
Nvalues := 3;
if Nvalues > 1 then
begin
if Nvalues = 2 then
Nvalues := 1; // only one L-L voltage if 2 phase
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
with pBus do
begin
if kVBase > 0.0 then
BaseFactor := 1000.0 * kVBase * sqrt3
else
BaseFactor := 1.0;
for i := 1 to NValues do // for 2- or 3-phases
begin
// this code assumes the nodes are ordered 1, 2, 3
//------------------------------------------------------------------------------------------------
// This section was added to prevent measuring using disconnected nodes, for example, if the
// bus has 2 nodes but those are 1 and 3, that will bring a problem.
jj := i;
repeat
NodeIdxi := FindIdx(jj); // Get the index of the Node that matches i
inc(jj);
until NodeIdxi > 0;
// (2020-03-01) Changed in DSS C-API to avoid some corner
// cases that resulted in infinite loops
for k := 1 to 3 do
begin
NodeIdxj := FindIdx(jj); // Get the index of the Node that matches i
if jj > 3 then
jj := 1
else
inc(jj);
if NodeIdxj > 0 then
break;
end;
if NodeIdxj = 0 then
begin
// Could not find appropriate node
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
//------------------------------------------------------------------------------------------------
with Solution do
Volts := NodeV^[GetRef(NodeIdxi)] - NodeV^[GetRef(NodeIdxj)];
Result[iV] := Volts.re / BaseFactor;
Inc(iV);
Result[iV] := Volts.im / BaseFactor;
Inc(iV);
end;
end; // With pBus
end
else
begin // for 1-phase buses, do not attempt to compute.
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2); // just return -1's in array
Result[0] := -99999.0;
Result[1] := 0.0;
end;
end
end;
procedure Bus_Get_puVLL_GR(); CDECL;
// Same as Bus_Get_puVLL but uses global result (GR) pointers
begin
Bus_Get_puVLL(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_VLL(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
var
Result: PDoubleArray0;
Nvalues, i, iV, NodeIdxi, NodeIdxj, jj, k: Integer;
Volts: Complex;
pBus: TDSSBus;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := pBus.NumNodesThisBus;
if Nvalues > 3 then
Nvalues := 3;
if Nvalues > 1 then
begin
if Nvalues = 2 then
Nvalues := 1; // only one L-L voltage if 2 phase
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
with pBus do
for i := 1 to NValues do // for 2- or 3-phases
begin
// this code assumes the nodes are ordered 1, 2, 3
//------------------------------------------------------------------------------------------------
// This section was added to prevent measuring using disconnected nodes, for example, if the
// bus has 2 nodes but those are 1 and 3, that will bring a problem.
jj := i;
repeat
NodeIdxi := FindIdx(jj); // Get the index of the Node that matches i
inc(jj);
until NodeIdxi > 0;
// (2020-03-01) Changed in DSS C-API to avoid some corner
// cases that resulted in infinite loops
for k := 1 to 3 do
begin
NodeIdxj := FindIdx(jj); // Get the index of the Node that matches i
if jj > 3 then
jj := 1
else
inc(jj);
if NodeIdxj > 0 then
break;
end;
if NodeIdxj = 0 then
begin
// Could not find appropriate node
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
//------------------------------------------------------------------------------------------------
with Solution do
Volts := NodeV^[GetRef(NodeIdxi)] - NodeV^[GetRef(NodeIdxj)];
Result[iV] := Volts.re;
Inc(iV);
Result[iV] := Volts.im;
Inc(iV);
end;
end
else
begin // for 1-phase buses, do not attempt to compute.
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2); // just return -1's in array
Result[0] := -99999.0;
Result[1] := 0.0;
end;
end
end;
procedure Bus_Get_VLL_GR(); CDECL;
// Same as Bus_Get_VLL but uses global result (GR) pointers
begin
Bus_Get_VLL(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_puVmagAngle(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;
// Return mag/angle for all nodes of voltages for Active Bus
var
Result: PDoubleArray0;
Nvalues, i, iV, NodeIdx, jj: Integer;
Volts: polar;
pBus: TDSSBus;
Basefactor: Double;
begin
if not _activeObj(DSSPrime, pBus) then
begin
DefaultResult(ResultPtr, ResultCount);
Exit;
end;
with DSSPrime.ActiveCircuit do
begin
Nvalues := pBus.NumNodesThisBus;
Result := DSS_RecreateArray_PDouble(ResultPtr, ResultCount, 2 * NValues);
iV := 0;
jj := 1;
with pBus do
begin
if kVBase > 0.0 then
BaseFactor := 1000.0 * kVBase
else
BaseFactor := 1.0;
for i := 1 to NValues do
begin
// this code so nodes come out in order from smallest to larges
repeat
NodeIdx := FindIdx(jj); // Get the index of the Node that matches jj
inc(jj)
until NodeIdx > 0;
Volts := ctopolardeg(Solution.NodeV^[GetRef(NodeIdx)]); // referenced to pBus
Result[iV] := Volts.mag / BaseFactor;
Inc(iV);
Result[iV] := Volts.ang;
Inc(iV);
end;
end;
end
end;
procedure Bus_Get_puVmagAngle_GR(); CDECL;
// Same as Bus_Get_puVmagAngle but uses global result (GR) pointers
begin
Bus_Get_puVmagAngle(DSSPrime.GR_DataPtr_PDouble, @DSSPrime.GR_Counts_PDouble[0])
end;
//------------------------------------------------------------------------------
procedure Bus_Get_VMagAngle(var ResultPtr: PDouble; ResultCount: PAPISize); CDECL;