-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcmd_wrapper.cc
488 lines (465 loc) · 12.9 KB
/
cmd_wrapper.cc
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
/* Copyright (C) 2015 Felix Salfelder
*
* This file is part of "Gnucap", the Gnu Circuit Analysis Package
*
* 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 3, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*------------------------------------------------------------------
* "order" and translate qucs commands
* this is essentially the gnucap driver.
* remember: qucs simulator drivers under construction. see qucs/
*/
#include "globals.h"
#include "u_lang.h"
#include "c_comand.h"
#include "l_qucs.h"
#include "u_prblst.h"
#include "u_sim_data.h"
#include "e_cardlist.h"
#include "u_parameter.h"
using std::string;
using std::stringstream;
/*--------------------------------------------------------------------------*/
namespace{
/*--------------------------------------------------------------------------*/
enum typeT{
tLin = 0,
tLog = 1
};
/*--------------------------------------------------------------------------*/
class AC_WRAP : public CMD {
public:
AC_WRAP(): CMD() {}
public:
typedef struct{
double _start;
double _stop;
std::string _args;
} data_t;
private:
double _start;
double _stop;
unsigned _points;
method_t _integration;
int _order;
double _initialstep;
double _dtmin;
unsigned _itl;
double _reltol;
double _abstol;
double _vntol;
void options(CS&);
void do_it(CS&cmd, CARD_LIST* cl)override {
assert(cl);
options(cmd);
data_t t;
t._start = _start;
t._stop = _stop;
if(_points<2){ untested();
incomplete();
}else if (_type == tLin) {
double range = _stop - _start;
double step = range / (_points-1);
incomplete();
t._args = "step " + to_string(step) + " ";
}else if (_type == tLog) {
double range = _stop / _start;
double step = exp ( log(range) / (_points-1));
// log b = log( stop / start ) / points
t._args = "* " + to_string(step) + " ";
}else{ untested();
incomplete();
}
_stash[short_label()] = t;
}
public: // GO
static std::map<string, data_t> _stash;
private:
typeT _type;
} pac;
std::map<string, AC_WRAP::data_t> AC_WRAP::_stash;
DISPATCHER<CMD>::INSTALL ddc(&command_dispatcher, "AC", &pac);
/*--------------------------------------------------------------------------*/
void AC_WRAP::options(CS& cmd)
{
_start = 0;
_stop = 0;
_points = 0;
// double _whatever; // incomplete
size_t here = cmd.cursor();
do{
trace1("options", cmd.tail());
ONE_OF
|| QucsGet(cmd, "Start", &_start)
|| QucsGet(cmd, "Stop", &_stop)
|| QucsGet(cmd, "Points", &_points)
|| (cmd.umatch( "Type") &&
(ONE_OF
|| QucsSet(cmd, "lin", &_type, tLin)
|| QucsSet(cmd, "log", &_type, tLog)
|| cmd.warn(bWARNING, "need lin, log... incomplete")
)
)
;
}while (cmd.more() && !cmd.stuck(&here));
cmd.check(bWARNING, "what's this (incomplete)?");
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class DC_WRAP : public CARD, public CMD {
public:
DC_WRAP(): CMD() {}
private:
DC_WRAP* clone()const override{return new DC_WRAP(*this);}
std::string value_name()const override{ untested();unreachable(); return "";}
public:
typedef struct{
double _start;
double _stop;
} data_t;
private:
double _start;
double _stop;
unsigned _points;
method_t _integration;
int _order;
double _initialstep;
double _dtmin;
unsigned _itl;
double _reltol;
double _abstol;
double _vntol;
void options(CS&){
incomplete();
}
void do_it(CS&cmd, CARD_LIST* Scope)override {
assert(Scope);
if(cmd >> "go"){
trace1("DC_WRAP go", cmd.fullstring());
stringstream x;
std::string outfile;
Get(cmd, "outfile", &outfile);
x << cmd.tail();
x << " trace=n basic > " << outfile << ".dc";
CS wcmd(CS::_STRING, x.str());
auto o = command_dispatcher["dc"];
// auto o = command_dispatcher["op"];
assert(o);
error(bTRACE, "calling op: " + wcmd.fullstring() + "\n");
o->do_it(wcmd, Scope);
}else{
// data_t t;
// t._start = _start;
// t._stop = _stop;
auto cl = clone();
cl->options(cmd);
// cl->options(cmd);
cl->CARD::set_label(CMD::short_label());
cl->set_owner(nullptr);
Scope->push_back(cl);
}
}
public: // GO
static std::map<string, data_t> _stash;
} pdc;
std::map<string, DC_WRAP::data_t> DC_WRAP::_stash;
DISPATCHER<CMD>::INSTALL dac(&command_dispatcher, "DC", (CMD*)&pdc);
/*--------------------------------------------------------------------------*/
class SP_WRAP : public CMD {
public:
SP_WRAP(): CMD() {}
public:
typedef struct{
double _start;
double _stop;
std::string _args;
} data_t;
private:
double _start;
double _stop;
unsigned _points;
method_t _integration;
int _order;
double _initialstep;
double _dtmin;
unsigned _itl;
double _reltol;
double _abstol;
double _vntol;
void options(CS&);
void do_it(CS&cmd, CARD_LIST* cl)override {
assert(cl);
options(cmd);
data_t t;
t._start = _start;
t._stop = _stop;
double range = _stop - _start;
if (_type == tLin) {
assert(_points>1);
double step = range / (_points-1);
incomplete();
t._args = "step " + to_string(step) + " ";
}else if (_type == tLog) {
range = _stop / _start;
double step = exp ( log(range) / (_points-1));
trace3("SP sweep log", range, _points, step);
t._args = "* " + to_string(step) + " ";
}else{ untested();
incomplete();
}
_stash[short_label()] = t;
}
public: // GO
static std::map<string, data_t> _stash;
private:
typeT _type;
} psp;
std::map<string, SP_WRAP::data_t> SP_WRAP::_stash;
DISPATCHER<CMD>::INSTALL dsp(&command_dispatcher, "SP", &psp);
/*--------------------------------------------------------------------------*/
void SP_WRAP::options(CS& cmd)
{
_order = -1;
_points = 0;
_dtmin = 0.;
_itl = 0;
_reltol = -1.;
_abstol = -1.;
_vntol = -1.;
size_t here = cmd.cursor();
// .SP:SP1 Type="lin" Start="1" Stop="2" Points="3" Noise="no" NoiseIP="1" NoiseOP="2" saveCVs="no" saveAll="no"
do{
trace1("options", cmd.tail());
ONE_OF
|| QucsGet(cmd, "Start", &_start)
|| QucsGet(cmd, "Stop", &_stop)
|| QucsGet(cmd, "Points", &_points)
|| (cmd.umatch( "Type") &&
(ONE_OF
|| QucsSet(cmd, "lin", &_type, tLin)
|| QucsSet(cmd, "log", &_type, tLog)
|| cmd.warn(bWARNING, "need lin, log... incomplete")
)
)
;
}while (cmd.more() && !cmd.stuck(&here));
cmd.check(bWARNING, "what's this (incomplete)?");
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
class TRAN_WRAP : public CMD {
public:
TRAN_WRAP(): CMD() {}
public:
typedef struct{
double _start;
double _stop;
} tran_t;
static std::map<string, tran_t> _stash;
private:
double _start;
double _stop;
unsigned _points;
method_t _integration;
int _order;
double _initialstep;
double _dtmin;
unsigned _itl;
double _reltol;
double _abstol;
double _vntol;
void options(CS&);
void do_it(CS&cmd, CARD_LIST* cl)override {
trace1("tran_wrap", cmd.fullstring());
assert(cl);
options(cmd);
tran_t t;
t._start = _start;
t._stop = _stop;
TRAN_WRAP::_stash[short_label()] = t;
}
} p8;
std::map<string, TRAN_WRAP::tran_t> TRAN_WRAP::_stash;
/*--------------------------------------------------------------------------*/
void TRAN_WRAP::options(CS& cmd)
{
_order = -1;
_points = 0;
_dtmin = 0.;
_itl = 0;
_reltol = -1.;
_abstol = -1.;
_vntol = -1.;
double _whatever; // incomplete
string type; // incomplete();
size_t here = cmd.cursor();
do{
trace1("tran_wrap options", cmd.tail());
ONE_OF
|| QucsGet(cmd, "Start", &_start)
|| QucsGet(cmd, "Stop", &_stop)
|| QucsGet(cmd, "Points", &_points)
|| QucsGet(cmd, "Type", &type)
|| (cmd.umatch( "IntegrationMethod") &&
(ONE_OF
|| QucsSet(cmd, "Trapezoidal", &_integration, meTRAP)
|| QucsSet(cmd, "Euler", &_integration, meEULER)
|| cmd.warn(bWARNING, "need Trapezoidal, Euler ... incomplete")
)
)
|| QucsGet(cmd, "Order", &_order)
|| QucsGet(cmd, "InitialStep",&_initialstep)
|| QucsGet(cmd, "MinStep", &_dtmin)
|| QucsGet(cmd, "MaxIter", &_itl)
|| QucsGet(cmd, "reltol", &_reltol)
|| QucsGet(cmd, "abstol", &_abstol)
|| QucsGet(cmd, "vntol", &_vntol)
|| QucsGet(cmd, "Temp", &_whatever)
|| QucsGet(cmd, "LTEreltol", &_whatever)
|| QucsGet(cmd, "LTEabstol", &_whatever)
|| QucsGet(cmd, "LTEfactor", &_whatever)
|| QucsGet(cmd, "Solver", &_whatever)
|| QucsGet(cmd, "relaxTSR", &_whatever)
|| QucsGet(cmd, "initialDC", &_whatever)
|| QucsGet(cmd, "MaxStep", &_whatever)
;
}while (cmd.more() && !cmd.stuck(&here));
cmd.check(bWARNING, "what's this (incomplete)?");
} // TRAN_WRAP::options
/*--------------------------------------------------------------------------*/
DISPATCHER<CMD>::INSTALL d8(&command_dispatcher, "TR", &p8);
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/
// go. run commands that are scattered
// BUG // that's what 'end' is supposed to do.
class GO : public CMD {
void do_it(CS&cmd, CARD_LIST*cl)override {
trace2("GO", _sim->is_first_expand(), _sim);
assert(cl);
cmd >> _outfile;
// std::string tail=cmd.tail();
CMD::command("print tran +v(nodes)", cl);
CMD::command("print op v(nodes)", cl);
CMD::command("print dc v(nodes)", cl);
CMD::command("print ac vr(nodes) vi(nodes)", cl);
CMD::command("print tran -v(gnd)", cl);
CMD::command("print op -v(gnd)", cl);
CMD::command("print dc -v(gnd)", cl);
CMD::command("print ac -vr(gnd) vi(gnd)", cl);
CMD* c = NULL;
CMD* s = NULL;
CMD* o = NULL;
CMD* a = NULL;
c = command_dispatcher["transient"];
s = command_dispatcher["sp"];
o = command_dispatcher["op"];
a = command_dispatcher["ac"];
if(!c){ untested();
error(bDANGER, "transient command missing, load plugin?\n");
}else{
}
if(!s){ untested();
error(bDANGER, "sp command missing, load plugin?\n");
}else{
}
if(!o){ untested();
error(bDANGER, "op command missing, load plugin?\n");
}else{
}
if(!a){ untested();
error(bDANGER, "ac command missing, load plugin?\n");
}else{
}
// what happens if there are multiple trans?
for(auto const&i : TRAN_WRAP::_stash){
stringstream x;
auto j = i.second;
x << j._start << " " << j._stop << " " << j._stop
<< " trace=a basic > " << _outfile << ".tr";
trace1("tran_wrap", x.str());
CS wcmd(CS::_STRING, x.str());
assert(c);
c->do_it(wcmd, cl);
}
if(TRAN_WRAP::_stash.empty()){
CS wcmd(CS::_STRING, " >/dev/null");
assert(o);
o->do_it(wcmd, cl);
}else{
}
PARAM_LIST* pl = cl->params();
assert(pl);
for(auto c1 : *cl){
if(auto c1cmd=dynamic_cast<CMD*>(c1)){
auto l = c1cmd->short_label();
auto omit = pl->deep_lookup("__omit_"+l);
if(omit.has_hard_value()){
}else{
CS go(CS::_STRING, "go outfile="+_outfile);
c1cmd->do_it(go, cl);
}
}else{
}
}
for(auto const&i : AC_WRAP::_stash){
stringstream x;
auto j = i.second;
x << j._start << " " << j._stop << " ";
x << j._args;
x << " basic > " << _outfile << ".ac";
CS wcmd(CS::_STRING, x.str());
trace1("run ac", wcmd.fullstring());
assert(a);
a->do_it(wcmd, cl);
}
for(auto&i : SP_WRAP::_stash){
stringstream x;
auto j = i.second;
x << "port * " << j._start << " " << j._stop << " " << j._args
<< " > " << _outfile << ".sp";
trace1("SP_WRAP running", x.str());
CS wcmd(CS::_STRING, x.str());
assert(s);
s->do_it(wcmd, cl);
}
cmd.reset();
#if 0 // probably not a good idea.
if(cmd >> "quit"){ itested();
quit(cl);
}else{untested();
}
#endif
}
// quit is invoked from main()
// exit is an alias, we overload quit and call exit.
void quit(CARD_LIST* cl)
{ untested();
trace0("exiting...");
CMD* c = NULL;
try { untested();
c = command_dispatcher["exit"];
}catch(Exception const&){ incomplete();
}
CS wcmd(CS::_STRING, "");
assert(c);
c->do_it(wcmd, cl);
}
private:
std::string _outfile;
}go;
DISPATCHER<CMD>::INSTALL d9(&command_dispatcher, "go", &go);
}
/*--------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------*/