-
Notifications
You must be signed in to change notification settings - Fork 1
/
linnetwk.pas
395 lines (339 loc) · 11.2 KB
/
linnetwk.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
unit LinNetwk;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, ComplexMath;
type
{ TLinearDipole }
{ Class template for linear RLC networks }
TLinearDipole = class(TObject)
private
FFrequency: Extended;
FInstanceName: string;
function GetAngularFrequency: Extended;
function GetCapacitiveReactance: Extended; virtual; abstract;
function GetInductiveReactance: Extended; virtual; abstract;
procedure SetAngularFrequency(AValue: Extended); virtual;
procedure SetCapacitiveReactance(AValue: Extended);
procedure SetImpedance(AValue: TComplexNumber);
procedure SetInductiveReactance(AValue: Extended);
private
function GetAdmittance: TComplexNumber; virtual; abstract;
function GetCapacitance: Extended; virtual; abstract;
function GetCurrent: TComplexNumber; virtual; abstract;
function GetReactance: Extended; virtual;
function GetResistance: Extended; virtual; abstract;
function GetInductance: Extended; virtual; abstract;
function GetVoltage: TComplexNumber; virtual; abstract;
procedure SetCapacitance(AValue: Extended); virtual; abstract;
procedure SetCurrent(AValue: TComplexNumber); virtual; abstract;
procedure SetFrequency(AValue: Extended); virtual;
procedure SetResistance(AValue: Extended); virtual; abstract;
procedure SetInductance(AValue: Extended); virtual; abstract;
procedure SetVoltage(AValue: TComplexNumber); virtual; abstract;
property AngularFrequency: Extended read GetAngularFrequency write SetAngularFrequency;
property Voltage: TComplexNumber read GetVoltage write SetVoltage;
property Current: TComplexNumber read GetCurrent write SetCurrent;
property Inductance: Extended read GetInductance write SetInductance;
property InductiveReactance: Extended read GetInductiveReactance
write SetInductiveReactance;
property CapacitiveReactance: Extended read GetCapacitiveReactance
write SetCapacitiveReactance;
protected
function GetImpedance: TComplexNumber; virtual; abstract;
public
constructor Create;
property Admittance: TComplexNumber read GetAdmittance;
property Frequency: Extended read FFrequency write SetFrequency;
property Impedance: TComplexNumber read GetImpedance write SetImpedance;
property Capacitance: Extended read GetCapacitance write SetCapacitance;
property InstanceName: string read FInstanceName write FInstanceName; {can
be used to identify an instance during debugging}
property Reactance: Extended read GetReactance;
property Resistance: Extended read GetResistance
write SetResistance;
end;
{ TStaticLinearDipole }
{ consists of a resistor, capacitor and inductorin a row with constant
resistance, capacitance and inductance }
TStaticLinearDipole = class(TLinearDipole)
private
FAdmittance, FImpedance: TComplexNumber;
FCapacitance, FInductance, FResistance: Extended;
function GetAdmittance: TComplexNumber; override;
function GetCapacitance: Extended; override;
function GetCapacitiveReactance: Extended; override;
function GetInductance: Extended; override;
function GetInductiveReactance: Extended; override;
function GetResistance: Extended; override;
procedure SetCapacitance(AValue: Extended); override;
procedure SetResistance(AValue: Extended); override;
procedure SetInductance(AValue: Extended); override;
protected
function GetImpedance: TComplexNumber; override;
public
destructor Destroy; override;
end;
{ TLinearDipoleCollection }
{ circuit that consists of several TLinearDipole instances }
TLinearDipoleCollection = class(TLinearDipole)
private
FAdmittance: TComplexNumber;
FImpedance: TComplexNumber;
function GetDipoleList: TList;
private
FDipoleList: TList;
function GetCapacitance: Extended; override;
function GetCapacitiveReactance: Extended; override;
function GetDipoleCount: Integer;
function GetDipoles(AnIndex: Integer): TLinearDipole;
function GetInductance: Extended; override;
function GetInductiveReactance: Extended; override;
function GetResistance: Extended; override;
procedure SetFrequency(AValue: Extended); override;
property DipoleList: TList read GetDipoleList;
public
destructor Destroy; override;
procedure AddDipole(ADipole: TLinearDipole);
procedure RemoveDipole(ADipole: TLinearDipole);
function Response(Dipole: TLinearDipole; F: Extended): TComplexNumber;
virtual; abstract;
function Response(ADipole: Integer; F: Extended): TComplexNumber;
property DipoleCount: Integer read GetDipoleCount;
property Dipoles[AnIndex: Integer]: TLinearDipole read GetDipoles;
end;
{ TSerialDipoleCollection }
{ circuit of several TLinearDipole instances in a row }
TSerialDipoleCollection = class(TLinearDipoleCollection)
private
FResponse: TComplexQuotient;
function GetAdmittance: TComplexNumber; override;
protected
function GetImpedance: TComplexNumber; override;
public
destructor Destroy; override;
function Response(ADipole: TLinearDipole; F: Extended): TComplexNumber; override;
end;
{ TParallelDipoleCollection }
{ circuit of several parallel TLinearDipole instances }
TParallelDipoleCollection = class(TLinearDipoleCollection)
private
function GetAdmittance: TComplexNumber; override;
protected
function GetImpedance: TComplexNumber; override;
public
end;
implementation
{ TSerialDipoleCollection }
function TSerialDipoleCollection.GetAdmittance: TComplexNumber;
begin
Result := Impedance.Reciprocal(FAdmittance)
end;
function TSerialDipoleCollection.GetImpedance: TComplexNumber;
var
i: Integer;
begin
if not Assigned(FImpedance) then FImpedance := TComplexSum.Create
else (FImpedance as TComplexSum).RemoveOperands;
for i := 0 to DipoleCount - 1 do begin
(FImpedance as TComplexSum).AddOperand(Dipoles[i].Impedance);
end;
(FImpedance as TComplexSum).Operate;
Result := FImpedance
end;
destructor TSerialDipoleCollection.Destroy;
begin
FResponse.Free;
inherited Destroy;
end;
function TSerialDipoleCollection.Response(ADipole: TLinearDipole; F: Extended
): TComplexNumber;
begin
if not Assigned(FResponse) then begin
FResponse := TComplexQuotient.Create;
FResponse.AddOperand(ADipole.Impedance);
FResponse.AddOperand(Impedance);
end
else FResponse.Operands[0] := ADipole.Impedance;
FResponse.Operate;
Result := FResponse
end;
{ TParallelDipoleCollection }
function TParallelDipoleCollection.GetAdmittance: TComplexNumber;
var
i: Integer;
begin
if not Assigned(FAdmittance) then FAdmittance := TComplexSum.Create
else (FAdmittance as TComplexSum).RemoveOperands;
for i := 0 to DipoleCount - 1 do
(FAdmittance as TComplexSum).AddOperand(Dipoles[i].Admittance);
(FAdmittance as TComplexSum).Operate;
Result := FAdmittance
end;
function TParallelDipoleCollection.GetImpedance: TComplexNumber;
begin
Result := Admittance.Reciprocal(FImpedance) as TComplexNumber;
end;
{ TStaticLinearDipole }
function TStaticLinearDipole.GetAdmittance: TComplexNumber;
begin
Result := Impedance.Reciprocal(FAdmittance);
end;
function TStaticLinearDipole.GetCapacitance: Extended;
begin
Result := FCapacitance
end;
function TStaticLinearDipole.GetCapacitiveReactance: Extended;
begin
if Capacitance = 0 then Result := 0
else Result := 1 / AngularFrequency / Capacitance
end;
function TStaticLinearDipole.GetImpedance: TComplexNumber;
begin
if not Assigned(FImpedance) then FImpedance := TComplexNumber.Create;
FImpedance.Re := Resistance;
FImpedance.Im := Reactance;
Result := FImpedance
end;
function TStaticLinearDipole.GetInductance: Extended;
begin
Result := FInductance
end;
function TStaticLinearDipole.GetInductiveReactance: Extended;
begin
Result := AngularFrequency * Inductance
end;
function TStaticLinearDipole.GetResistance: Extended;
begin
Result := FResistance
end;
procedure TStaticLinearDipole.SetCapacitance(AValue: Extended);
begin
FCapacitance := AValue;
end;
procedure TStaticLinearDipole.SetResistance(AValue: Extended);
begin
FResistance := AValue
end;
procedure TStaticLinearDipole.SetInductance(AValue: Extended);
begin
FInductance := AValue
end;
destructor TStaticLinearDipole.Destroy;
begin
FAdmittance.Free;
FImpedance.Free;
inherited Destroy;
end;
{ TLinearDipoleCollection }
function TLinearDipoleCollection.GetDipoleList: TList;
begin
if not Assigned(FDipoleList) then FDipoleList := TList.Create;
Result := FDipoleList;
end;
function TLinearDipoleCollection.GetCapacitance: Extended;
begin
if CapacitiveReactance > 0 then Result := 1 / CapacitiveReactance / AngularFrequency
else Result := 0
end;
function TLinearDipoleCollection.GetCapacitiveReactance: Extended;
begin
with Impedance do if Im < 0 then Result := -Im else Result := 0
end;
function TLinearDipoleCollection.GetDipoles(AnIndex: Integer): TLinearDipole;
begin
Pointer(Result) := DipoleList[AnIndex]
end;
function TLinearDipoleCollection.GetInductance: Extended;
begin
with Impedance do
if Im > 0 then Result := Im / AngularFrequency
else Result := 0
end;
function TLinearDipoleCollection.GetInductiveReactance: Extended;
begin
with Impedance do if Im > 0 then Result := Im else Result := 0
end;
function TLinearDipoleCollection.GetResistance: Extended;
begin
Result := Impedance.Re
end;
procedure TLinearDipoleCollection.SetFrequency(AValue: Extended);
var
i: Integer;
begin
inherited SetFrequency(AValue);
for i := 0 to DipoleCount - 1 do Dipoles[i].Frequency := AValue;
end;
function TLinearDipoleCollection.GetDipoleCount: Integer;
begin
if Assigned(FDipoleList) then Result := FDipoleList.Count
else Result := 0
end;
destructor TLinearDipoleCollection.Destroy;
var
i: Integer;
begin
FAdmittance.Free;
FImpedance.Free;
for i := 0 to DipoleCount - 1 do Dipoles[i].Free;
FDipoleList.Free;
inherited Destroy;
end;
procedure TLinearDipoleCollection.AddDipole(ADipole: TLinearDipole);
begin
ADipole.Frequency := FFrequency;
DipoleList.Add(ADipole);
end;
procedure TLinearDipoleCollection.RemoveDipole(ADipole: TLinearDipole);
begin
DipoleList.Remove(ADipole);
end;
function TLinearDipoleCollection.Response(ADipole: Integer; F: Extended
): TComplexNumber;
begin
Result := Response(Dipoles[ADipole], F)
end;
{ TLinearDipole }
function TLinearDipole.GetAngularFrequency: Extended;
begin
Result := 2 * Pi * Frequency
end;
procedure TLinearDipole.SetAngularFrequency(AValue: Extended);
begin
Frequency := AValue / 2 / Pi
end;
procedure TLinearDipole.SetCapacitiveReactance(AValue: Extended);
begin
Capacitance := 1 / AngularFrequency / AValue
end;
procedure TLinearDipole.SetImpedance(AValue: TComplexNumber);
begin
Resistance := AValue.Re;
if AValue.Im < 0 then begin
CapacitiveReactance := -AValue.Im;
InductiveReactance := 0
end
else begin
CapacitiveReactance := 0;
InductiveReactance := AValue.Im
end;
end;
procedure TLinearDipole.SetInductiveReactance(AValue: Extended);
begin
Inductance := AValue / AngularFrequency
end;
function TLinearDipole.GetReactance: Extended;
begin
Result := InductiveReactance - CapacitiveReactance;
end;
procedure TLinearDipole.SetFrequency(AValue: Extended);
begin
FFrequency := AValue
end;
constructor TLinearDipole.Create;
begin
inherited Create;
Frequency := 10;
end;
end.