Skip to content

Commit 9b7d7b4

Browse files
committed
A few microoptimizations
1 parent 6ea25cd commit 9b7d7b4

File tree

4 files changed

+54
-21
lines changed

4 files changed

+54
-21
lines changed

src/Common/CktElement.pas

+46-13
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ TDSSCktElement = class(TDSSObject)
3434
procedure Set_Freq(Value: Double); // set freq and recompute YPrim.
3535

3636
procedure Set_Nconds(Value: Int8);
37+
function Get_ActiveTerminal(): Int8; inline;
3738
procedure Set_ActiveTerminal(value: Int8);
38-
function Get_ConductorClosed(Index: Integer): Boolean;
39+
function Get_ConductorClosed(Index: Integer): Boolean; inline;
3940
procedure Set_YprimInvalid(const Value: Boolean);
4041
function Get_FirstBus: String;
4142
function Get_NextBus: String;
@@ -133,7 +134,7 @@ TDSSCktElement = class(TDSSObject)
133134
property MaxPower[idxTerm: Integer]: Complex READ Get_MaxPower; // Total power in active terminal
134135
property MaxCurrent[idxTerm: Integer]: Double READ Get_MaxCurrent; // Max current in active terminal
135136
property MaxVoltage[idxTerm: Integer]: Double READ Get_MaxVoltage; // Max voltage in active terminal
136-
property ActiveTerminalIdx: Int8 READ FActiveTerminal WRITE Set_ActiveTerminal;
137+
property ActiveTerminalIdx: Int8 READ Get_ActiveTerminal WRITE Set_ActiveTerminal;
137138
property Closed[Index: Integer]: Boolean READ Get_ConductorClosed WRITE Set_ConductorClosed;
138139
procedure SumCurrents;
139140

@@ -190,7 +191,7 @@ constructor TDSSCktElement.Create(ParClass: TDSSClass);
190191
// Make list for a small number of controls with an increment of 1
191192
ControlElementList := TDSSPointerList.Create(1);
192193

193-
FActiveTerminal := 1;
194+
FActiveTerminal := 0;
194195
// LastTerminalChecked := 0;
195196

196197
// Indicates which solution Itemp is computed for
@@ -241,12 +242,17 @@ procedure TDSSCktElement.Set_YprimInvalid(const Value: Boolean);
241242
ActiveCircuit.Solution.SystemYChanged := TRUE;
242243
end;
243244

245+
function TDSSCktElement.Get_ActiveTerminal(): Int8; inline;
246+
begin
247+
Result := FActiveTerminal + 1;
248+
end;
249+
244250
procedure TDSSCktElement.Set_ActiveTerminal(value: Int8);
245251
begin
246252
if (Value > 0) and (Value <= fNterms) then
247253
begin
248-
FActiveTerminal := Value;
249-
ActiveTerminal := @Terminals[Value - 1];
254+
FActiveTerminal := Value - 1;
255+
ActiveTerminal := @Terminals[FActiveTerminal];
250256
end;
251257
end;
252258

@@ -255,7 +261,7 @@ procedure TDSSCktElement.Set_Handle(value: Integer);
255261
FHandle := value;
256262
end;
257263

258-
function TDSSCktElement.Get_ConductorClosed(Index: Integer): Boolean;
264+
function TDSSCktElement.Get_ConductorClosed(Index: Integer): Boolean; inline;
259265
// return state of selected conductor
260266
// if index=0 return true if all phases closed, else false
261267
var
@@ -266,7 +272,7 @@ function TDSSCktElement.Get_ConductorClosed(Index: Integer): Boolean;
266272
Result := TRUE;
267273
for i := 1 to Fnphases do
268274
begin
269-
if not Terminals[FActiveTerminal - 1].ConductorsClosed[i - 1] then
275+
if not Terminals[FActiveTerminal].ConductorsClosed[i - 1] then
270276
begin
271277
Result := FALSE;
272278
Break;
@@ -275,7 +281,7 @@ function TDSSCktElement.Get_ConductorClosed(Index: Integer): Boolean;
275281
end
276282
else
277283
if (Index > 0) and (Index <= Fnconds) then
278-
Result := Terminals[FActiveTerminal - 1].ConductorsClosed[Index - 1]
284+
Result := Terminals[FActiveTerminal].ConductorsClosed[Index - 1]
279285
else
280286
Result := FALSE;
281287
end;
@@ -286,16 +292,16 @@ procedure TDSSCktElement.Set_ConductorClosed(Index: Integer; Value: Boolean);
286292
begin
287293
if (Index = 0) then
288294
begin // Do all conductors
289-
for i := 1 to Fnphases do
290-
Terminals[FActiveTerminal - 1].ConductorsClosed[i - 1] := Value;
295+
for i := 0 to Fnphases - 1 do
296+
Terminals[FActiveTerminal].ConductorsClosed[i] := Value;
291297
// DSS.ActiveCircuit.Solution.SystemYChanged := TRUE; // force Y matrix rebuild
292298
YPrimInvalid := TRUE; // this also sets the global SystemYChanged flag
293299
end
294300
else
295301
begin
296302
if (Index > 0) and (Index <= Fnconds) then
297303
begin
298-
Terminals[FActiveTerminal - 1].ConductorsClosed[index - 1] := Value;
304+
Terminals[FActiveTerminal].ConductorsClosed[index - 1] := Value;
299305
// DSS.ActiveCircuit.Solution.SystemYChanged := TRUE;
300306
YPrimInvalid := TRUE;
301307
end;
@@ -1054,15 +1060,42 @@ procedure TDSSCktElement.ComputeVterminal;
10541060
// Put terminal voltages in an array
10551061
var
10561062
i: Integer;
1063+
vterm: PDouble;
1064+
nref: PInteger;
1065+
nv0, nv: PDouble;
10571066
begin
10581067
with ActiveCircuit.solution do
1068+
begin
1069+
vterm := PDouble(VTerminal);
1070+
nref := PInteger(NodeRef);
1071+
nv0 := PDouble(NodeV);
10591072
for i := 1 to Yorder do
1060-
VTerminal^[i] := NodeV^[NodeRef^[i]];
1073+
begin
1074+
nv := nv0 + 2 * nref^;
1075+
vterm^ := nv^;
1076+
(vterm + 1)^ := (nv + 1)^;
1077+
inc(vterm, 2);
1078+
inc(nref);
1079+
// VTerminal^[i] := NodeV^[NodeRef^[i]];
1080+
end;
1081+
end;
10611082
end;
10621083

10631084
procedure TDSSCktElement.ZeroITerminal; inline;
1085+
var
1086+
i: Integer;
1087+
it: PDouble;
10641088
begin
1065-
FillDWord(ITerminal^, Yorder * ((SizeOf(Double) * 2) div 4), 0);
1089+
// Somehow this is slower?! FillDWord(ITerminal^, Yorder * ((SizeOf(Double) * 2) div 4), 0);
1090+
it := PDouble(Iterminal);
1091+
for i := 1 to Yorder do
1092+
begin
1093+
it^ := 0;
1094+
(it + 1)^ := 0;
1095+
inc(it, 2);
1096+
end;
1097+
//for i := 1 to Yorder do
1098+
// ITerminal[i] := CZERO;
10661099
end;
10671100

10681101
procedure TDSSCktElement.MakeLike(OtherObj: Pointer);

src/PCElements/Load.pas

+2-2
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ TLoadObj = class(TPCElement)
200200
procedure DoZIPVModel;
201201
procedure DoMotorTypeLoad;
202202
function GrowthFactor(Year: Integer): Double;
203-
procedure StickCurrInTerminalArray(TermArray: pComplexArray; const Curr: Complex; i: Integer);
203+
procedure StickCurrInTerminalArray(TermArray: pComplexArray; const Curr: Complex; i: Integer); inline;
204204
function InterpolateY95_YLow(const Vmag: Double): Complex; inline;
205205
function InterpolateY95I_YLow(const Vmag: Double): Complex; inline; // ***Added by Celso & Paulo
206206
function Get_Unserved: Boolean;
@@ -1174,7 +1174,7 @@ procedure TLoadObj.CalcYPrim;
11741174
inherited CalcYPrim;
11751175
end;
11761176

1177-
procedure TLoadObj.StickCurrInTerminalArray(TermArray: pComplexArray; const Curr: Complex; i: Integer);
1177+
procedure TLoadObj.StickCurrInTerminalArray(TermArray: pComplexArray; const Curr: Complex; i: Integer); inline;
11781178
// Put the current into the proper location according to connection
11791179
var
11801180
j: Integer;

src/Shared/DSSUcomplex.pas

+4-4
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ polar = record
2626
cZERO: Complex = (re: 0.0; im: 0.0);
2727
cONE: Complex = (re: 1.0; im: 0.0);
2828

29-
function cmplx(const a, b: Double): complex;
30-
function cabs(const a: complex): Double;
29+
function cmplx(const a, b: Double): complex; inline;
30+
function cabs(const a: complex): Double; inline;
3131
Function cabs2(const a:complex):double; // best when you don't need sqrt -- TODO: rename?
3232
function cang(const a: complex): Double;
3333
function cdang(const a: complex): Double; // angle of complex number, degrees
@@ -40,13 +40,13 @@ function pclx(const magn, angle: Double): complex;
4040

4141
implementation
4242

43-
function CMPLX(const a, b: Double): complex;
43+
function CMPLX(const a, b: Double): complex; inline;
4444
begin
4545
Result.RE := A;
4646
Result.IM := B
4747
end;
4848

49-
function Cabs(const a: complex): Double;
49+
function Cabs(const a: complex): Double; inline;
5050
begin
5151
Result := SQRT(A.RE * A.RE + A.IM * A.IM)
5252
end;

src/Shared/Ucmatrix.pas

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ TcMatrix = class(TObject)
4141
procedure AddElemsym(i, j: Integer; Value: Complex);
4242
function GetElement(i, j: Integer): Complex;
4343
function GetErrorCode: Integer;
44-
procedure MVmult(b, x: pComplexArray); {inline;} {b = Ax}
44+
procedure MVmult(b, x: pComplexArray); inline; {b = Ax}
4545
function GetValuesArrayPtr(var Order: Integer): pComplexArray;
4646
procedure ZeroRow(iRow: Integer);
4747
procedure ZeroCol(iCol: Integer);
@@ -143,7 +143,7 @@ function TcMatrix.IsColRowZero(n: Integer): Boolean; // This only check for exac
143143
end;
144144
end;
145145

146-
procedure TcMatrix.MvMult(b, x: pComplexArray); {inline;}
146+
procedure TcMatrix.MvMult(b, x: pComplexArray); inline;
147147
{$IFDEF DSS_CAPI_MVMULT}
148148
begin
149149
KLUSolve.mvmult(Norder, b, values, x);

0 commit comments

Comments
 (0)