-
Notifications
You must be signed in to change notification settings - Fork 214
/
Copy pathqf_comp.cpp
376 lines (326 loc) · 10.1 KB
/
qf_comp.cpp
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
/***************************************************************************
qf_comp.cpp
-------------
begin : Fri May 1 2009
copyright : (C) 2009 by Vincent Habchi, F5RCS
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "qf_common.h"
#include "qf_comp.h"
//Added by qt3to4:
#include <Q3TextStream>
// DUMP functions and utilities
static QString valstr (qf_double_t num) {
char c = 0;
num = fabs (num);
if ((num > 1e-18) && (num < 1e12)) {
qf_double_t cal = floor (log10 (num) / 3);
int expo = static_cast <int> (cal);
switch (expo) {
case -6: c = 'a'; break;
case -5: c = 'f'; break;
case -4: c = 'p'; break;
case -3: c = 'n'; break;
case -2: c = 'u'; break;
case -1: c = 'm'; break;
case 0: break;
case 1: c = 'k'; break;
case 2: c = 'M'; break;
case 3: c = 'G'; break;
case 4: c = 'T'; break;
}
if (c != 0) num /= pow (10.0, 3 * cal);
}
QString buffer;
Q3TextStream str (& buffer, QIODevice::WriteOnly);
str. precision (3);
str << num;
if (c)
str << c;
return buffer;
}
void qf_cmp1::dump (unsigned n1, unsigned n2,
Q3TextStream& com, Q3TextStream& wir) {
// type of component
if (name == "CAP")
com << "<C C1";
else if (name == "IND")
com << "<L L1";
else if (name == "RES")
com << "<R R1";
// single component to ground
if (n2 == 0) {
// wire
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
// position
com << " 1 " << x << " " << y;
com << " 17 -26 0 1";
// value of component
com << " \"" << valstr (val) << unit;
com << "\" 1>\n";
// place ground symbol here
com << "<GND * 1 " << x << " " << y+30 << " 0 0 0 0>\n";
}
// single component in series
else {
// wires
wir << "<" << x << " " << y-80 << " " << x+40 << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x+100 << " " << y-80 << " " << x+140 << " " << y-80
<< " \"\" 0 0 0>\n";
// position
com << " 1 " << x+70 << " " << y-80;
com << " -26 10 0 0";
// value of commponent
com << " \"" << valstr (val) << unit;
com << "\" 1>\n";
x += 140;
}
}
void qf_cmplc::dump (unsigned n1, unsigned n2,
Q3TextStream& com, Q3TextStream& wir) {
if (name == "PLC") {
// parallel L and C to ground
if (n2 == 0) {
// wire
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
// inductor
com << "<L L1";
// position
com << " 1 " << x << " " << y;
com << " 17 -26 0 1";
// value of component
com << " \"" << valstr (vL) << "H";
com << "\" 1>\n";
// place ground symbol here
com << "<GND * 1 " << x << " " << y+30 << " 0 0 0 0>\n";
// connecting above wire
wir << "<" << x << " " << y-80 << " " << x+110 << " " << y-80
<< " \"\" 0 0 0>\n";
x += 110;
// wire
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
// capacitor
com << "<C C1";
// position
com << " 1 " << x << " " << y;
com << " 17 -26 0 1";
// value of component
com << " \"" << valstr (vC) << "F";
com << "\" 1>\n";
// place ground symbol here
com << "<GND * 1 " << x << " " << y+30 << " 0 0 0 0>\n";
}
// parallel L and C in series
else {
// wires
wir << "<" << x << " " << y-80 << " " << x+40 << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x+100 << " " << y-80 << " " << x+140 << " " << y-80
<< " \"\" 0 0 0>\n";
// inductor
com << "<L L1";
// position
com << " 1 " << x+70 << " " << y-80;
com << " -26 10 0 0";
// value of commponent
com << " \"" << valstr (vL) << "H";
com << "\" 1>\n";
// wires
wir << "<" << x+40 << " " << y-80 << " " << x+40 << " " << y-120
<< " \"\" 0 0 0>\n";
wir << "<" << x+100 << " " << y-80 << " " << x+100 << " " << y-120
<< " \"\" 0 0 0>\n";
// capacitor
com << "<C C1";
// position
com << " 1 " << x+70 << " " << y-120;
com << " -26 -48 0 0";
// value of commponent
com << " \"" << valstr (vC) << "F";
com << "\" 1>\n";
x += 140;
}
}
else if (name == "SLC") {
// series L and C to ground
if (n2 == 0) {
// wire
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
// inductor
com << "<L L1";
// position
com << " 1 " << x << " " << y;
com << " 17 -26 0 1";
// value of component
com << " \"" << valstr (vL) << "H";
com << "\" 1>\n";
// capacitor
com << "<C C1";
// position
com << " 1 " << x << " " << y+60;
com << " 17 -26 0 1";
// value of component
com << " \"" << valstr (vC) << "F";
com << "\" 1>\n";
// place ground symbol here
com << "<GND * 1 " << x << " " << y+90 << " 0 0 0 0>\n";
}
// series L and C in series
else {
// wires
wir << "<" << x << " " << y-80 << " " << x+40 << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x+100 << " " << y-80 << " " << x+120 << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x+180 << " " << y-80 << " " << x+220 << " " << y-80
<< " \"\" 0 0 0>\n";
// inductor
com << "<L L1";
// position
com << " 1 " << x+70 << " " << y-80;
com << " -26 10 0 0";
// value of commponent
com << " \"" << valstr (vL) << "H";
com << "\" 1>\n";
// capacitor
com << "<C C1";
// position
com << " 1 " << x+150 << " " << y-80;
com << " -26 10 0 0";
// value of commponent
com << " \"" << valstr (vC) << "F";
com << "\" 1>\n";
x += 220;
}
}
}
void qf_pslc::dump (unsigned n1, unsigned n2,
Q3TextStream& s, Q3TextStream& wir) {
qf_slc (vC, vL, shnt, gnd). dump (n1, n2, s, wir);
qf_slc (vC2, vL2, shnt, gnd). dump (n1, n2, s, wir);
}
// Dumps the whole filter structure
int qf_lcmp::dump_node (int& p, list <qf_cmp*>::iterator& i,
Q3TextStream& com, Q3TextStream& wir) {
int n = p + 1;
int g = 0;
if ((*i) -> name == "END") return -n; // End of structure
while (i != lst. end ()) {
if ((*i) == NULL) return 0;
if ((*i) -> name == "END") return -n; // End of structure
if ((*i) -> gnd) {
if (g) {
// connecting wire for consecutive branches to ground
wir << "<" << x << " " << y-80 << " " << x+110 << " " << y-80
<< " \"\" 0 0 0>\n";
x += 110;
}
(*i) -> x = x;
(*i) -> y = y;
(*i) -> dump (p, 0, com, wir);
x = (*i) -> x;
y = (*i) -> y;
g = 1;
i ++;
continue;
}
g = 0;
(*i) -> x = x;
(*i) -> y = y;
(*i) -> dump (p, n, com, wir);
x = (*i) -> x;
y = (*i) -> y;
if ((*i) -> shnt) {
i ++;
n = dump_node (n, i, com, wir);
}
else {
i ++;
return n;
}
}
return -n;
}
void qf_lcmp::dump_all (Q3TextStream& out) {
if (lst.size () == 0) {
return;
}
int node = 1;
QByteArray buf1, buf2;
Q3TextStream com (buf1, QIODevice::ReadWrite);
Q3TextStream wir (buf2, QIODevice::ReadWrite);
x = 0;
y = 0;
// schematic start
out << "<Qucs Schematic " << PACKAGE_VERSION << ">\n";
// input port and according ground symbol
com << "<Pac P1 1 " << x << " " << y << " 18 -26 0 1 \"1\" 1 \""
<< r1 << " Ohm\" 1 \"0 dBm\" 0 \"1 GHz\" 0>\n";
com << "<GND * 1 " << x << " " << y+30 << " 0 0 0 0>\n";
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x << " " << y-80 << " " << x+110 << " " << y-80
<< " \"\" 0 0 0>\n";
x += 110;
for (list <qf_cmp*>::iterator i = lst. begin (); i != lst. end ();)
if ((node = dump_node (node, i, com, wir)) < 0) break;
// output port and according ground symbol
x += 110;
wir << "<" << x << " " << y-30 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
wir << "<" << x-110 << " " << y-80 << " " << x << " " << y-80
<< " \"\" 0 0 0>\n";
com << "<Pac P2 1 "<< x << " " << y << " 18 -26 0 1 \"2\" 1 \""
<< r2 << " Ohm\" 1 \"0 dBm\" 0 \"1 GHz\" 0>\n"
<< "<GND * 1 " << x << " " << y+30 << " 0 0 0 0>\n";
x = 0;
// S-parameter simulation box and equation
com << "<.SP SP1 1 " << x << " " << y-280 << " 0 50 0 0 \"log\" 1 \""
<< valstr(fc/2/pi/10) << "Hz\" 1 \"" << valstr(fc/2/pi*10)
<< "Hz\" 1 \"200\" 1 \"no\" 0 \"1\" 0 \"2\" 0>\n";
com << "<Eqn Eqn1 1 " << x+200 << " " << y-270
<< " -28 15 0 0 \"dBS21=dB(S[2,1])\" 1 "
<< "\"dBS11=dB(S[1,1])\" 1 \"yes\" 0>\n";
// components
out << "<Components>\n";
out << QString(buf1);
out << "</Components>\n";
// wires
out << "<Wires>\n";
out << QString(buf2);
out << "</Wires>\n";
// paintings
out << "<Paintings>\n";
out << "<Text " << x+310 << " " << y-280 << " 12 #000000 0 \""
<< tx << "\\n"
<< valstr(fc/2/pi) << "Hz cutoff/center frequency\\n"
<< "impedance matching Z_{IN}=" << valstr(r1) << "\\Omega, Z_{OUT}="
<< valstr(r2) << "\\Omega\">\n";
out << "</Paintings>\n";
}
// Access to the list of components
void qf_lcmp::init (void) {
iter = lst. begin ();
}
qf_cmp* qf_lcmp::next (void) {
qf_cmp* cmp = * iter;
if ((cmp == NULL) || (typeid (* cmp) == typeid (qf_end))) return NULL;
iter ++;
return cmp;
}