-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimserver.h
326 lines (298 loc) · 10.6 KB
/
simserver.h
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
// SPDX-FileCopyrightText: 2022 Pepijn de Vos
//
// SPDX-License-Identifier: MPL-2.0
#include "api/Simulator.capnp.h"
#include <kj/debug.h>
#include <kj/filesystem.h>
#include <kj/exception.h>
#include <kj/async-io.h>
#include <capnp/ez-rpc.h>
#include <capnp/message.h>
#include <iostream>
#include <vector>
#include <map>
#include <complex>
#include <sstream>
#include <ngspice/sharedspice.h>
class NgspiceCommandsImpl;
class ResultImpl final : public Sim::Result::Server
{
public:
ResultImpl(NgspiceCommandsImpl* cmd) : cmd(cmd) {}
kj::Promise<void> read(ReadContext context);
NgspiceCommandsImpl* cmd;
};
struct NgVectors {
std::string name;
std::vector<std::string> fieldnames;
std::vector<std::vector<double>> real_data;
std::vector<std::vector<std::complex<double>>> complex_data;
kj::Maybe<unsigned int> scale;
};
void save_vectors(capnp::List<capnp::Text, capnp::Kind::BLOB>::Reader vecs) {
/*
NgSpice is basically messed up somehow
save is supposed to set the variables unless you do save all
then it's supposed to do all plus whatever else you want
but it seems like after running a simulation it always adds stuff
So we first do a reset, and then do a save, which should set rather than add.
This is kinda inconvenient because it also appears that if you save +30 vectors
in a single command it kinda just breaks??
So I'd like to send individual save commands but that only saves a single vector...
Sometimes.
*/
ngSpice_Command((char*)"reset");
std::ostringstream ss;
ss << "save";
for (auto v : vecs) {
ss << " " << v.cStr();
}
auto savecmd = ss.str();
std::cout << savecmd << std::endl;
ngSpice_Command((char*)savecmd.c_str());
}
std::map<std::string, std::string> sim_names = {
{"Transient Simulation", "tran"},
{"Operating Point", "op"},
{"AC Analysis", "ac"},
// [...]
};
class NgspiceCommandsImpl final : public Sim::NgspiceCommands::Server
{
public:
NgspiceCommandsImpl(std::string name) : name(name) {
ngSpice_Init(&cbSendChar, &cbSendStat, &cbControlledExit, &cbSendData, &cbSendInitData, &cbBGThreadRunning, this);
ngSpice_Command((char*)("source " + name).c_str());
}
~NgspiceCommandsImpl() {
// need to halt the thread before destroying ourselves
// bg thread runs callbacks on us
if(ngSpice_running()) {
ngSpice_Command((char*)"bg_halt");
ngSpice_Command((char*)"quit");
}
}
kj::Promise<void> run(RunContext context)
{
save_vectors(context.getParams().getVectors());
vectors.lockExclusive()->clear();
ngSpice_Command((char*)"bg_run");
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
kj::Promise<void> tran(TranContext context)
{
auto params = context.getParams();
save_vectors(params.getVectors());
char buf[256];
snprintf(buf, 256, "bg_tran %f %f %f", params.getStep(), params.getStop(), params.getStart());
vectors.lockExclusive()->clear();
ngSpice_Command(buf);
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
kj::Promise<void> op(OpContext context)
{
save_vectors(context.getParams().getVectors());
vectors.lockExclusive()->clear();
ngSpice_Command((char*)"bg_op");
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
kj::Promise<void> dc(DcContext context)
{
auto params = context.getParams();
save_vectors(params.getVectors());
char buf[256];
snprintf(buf, 256, "bg_dc %s %f %f %f", params.getSrc().cStr(), params.getVstart(), params.getVstop(), params.getVincr());
std::cout << buf << std::endl;
vectors.lockExclusive()->clear();
ngSpice_Command(buf);
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
kj::Promise<void> ac(AcContext context)
{
auto params = context.getParams();
save_vectors(params.getVectors());
const char* mode;
switch (params.getMode())
{
case Sim::AcType::LIN:
mode = "lin";
break;
case Sim::AcType::OCT:
mode = "oct";
break;
case Sim::AcType::DEC:
mode = "dec";
break;
}
char buf[256];
snprintf(buf, 256, "bg_ac %s %d %f %f", mode, params.getNum(), params.getFstart(), params.getFstop());
std::cout << buf << std::endl;
vectors.lockExclusive()->clear();
ngSpice_Command(buf);
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
kj::Promise<void> noise(NoiseContext context)
{
auto params = context.getParams();
save_vectors(params.getVectors());
const char* mode;
switch (params.getMode())
{
case Sim::AcType::LIN:
mode = "lin";
break;
case Sim::AcType::OCT:
mode = "oct";
break;
case Sim::AcType::DEC:
mode = "dec";
break;
}
char buf[512];
snprintf(buf, 512, "bg_noise %s %s %s %d %f %f", params.getOutput().cStr(), params.getSrc().cStr(), mode, params.getNum(), params.getFstart(), params.getFstop());
std::cout << buf << std::endl;
vectors.lockExclusive()->clear();
ngSpice_Command(buf);
*is_running.lockExclusive() = true;
auto res = kj::heap<ResultImpl>(this);
context.getResults().setResult(kj::mv(res));
return kj::READY_NOW;
}
// Callback functions
static int cbSendChar( char* what, int id, void* user ) {
NgspiceCommandsImpl* cmd = reinterpret_cast<NgspiceCommandsImpl*>( user );
auto buf = cmd->outputbuf.lockExclusive();
buf->append(what);
buf->push_back('\n');
std::cout << "SendChar: " << what << std::endl;
return 0;
}
static int cbSendStat( char* what, int id, void* user ) {
NgspiceCommandsImpl* cmd = reinterpret_cast<NgspiceCommandsImpl*>( user );
auto buf = cmd->outputbuf.lockExclusive();
buf->append(what);
buf->push_back('\n');
std::cout << "SendStat: " << what << std::endl;
return 0;
}
static int cbBGThreadRunning( bool halted, int id, void* user ) {
NgspiceCommandsImpl* cmd = reinterpret_cast<NgspiceCommandsImpl*>( user );
*cmd->is_running.lockExclusive() = !halted;
if (halted) {
std::cout << "BGThreadRunning: not running" << std::endl;
} else {
std::cout << "BGThreadRunning: running" << std::endl;
}
return 0;
}
static int cbControlledExit( int status, bool immediate, bool exit_upon_quit, int id, void* user ) {
exit(1);
return 0;
}
static int cbSendInitData(pvecinfoall via, int id, void* user) {
std::cout << "init data\n";
NgspiceCommandsImpl* cmd = reinterpret_cast<NgspiceCommandsImpl*>( user );
NgVectors vec;
if(sim_names.count(via->name)) {
vec.name = sim_names[via->name];
} else {
vec.name = via->name;
}
std::cout << via->name << std::endl;
for(int i=0; i<via->veccount; i++) {
vec.fieldnames.push_back(via->vecs[i]->vecname);
std::cout << via->vecs[i]->vecname << std::endl;
}
vec.real_data.resize(via->veccount);
vec.complex_data.resize(via->veccount);
cmd->vectors.lockExclusive()->push_back(vec);
return 0;
}
static int cbSendData(pvecvaluesall vva, int len, int id, void* user) {
// std::cout << "send data\n";
NgspiceCommandsImpl* cmd = reinterpret_cast<NgspiceCommandsImpl*>( user );
NgVectors &vec = cmd->vectors.lockExclusive()->back();
for(int i=0; i<vva->veccount; i++) {
auto vecsa = vva->vecsa[i];
if(vecsa->is_scale) {
vec.scale = i;
}
if(vecsa->is_complex) {
vec.complex_data[i].push_back(std::complex(vecsa->creal, vecsa->cimag));
} else {
vec.real_data[i].push_back(vecsa->creal);
}
}
return 0;
}
std::string name;
kj::MutexGuarded<std::vector<NgVectors>> vectors;
kj::MutexGuarded<std::string> outputbuf;
kj::MutexGuarded<bool> is_running;
};
kj::Promise<void> ResultImpl::read(ReadContext context)
{
auto res = context.getResults();
res.setMore(*cmd->is_running.lockExclusive());
{
auto outputbuf = cmd->outputbuf.lockExclusive();
capnp::Data::Reader dat((const kj::byte*)outputbuf->data(), outputbuf->length());
res.setStdout(dat);
outputbuf->clear();
}
auto vecs = cmd->vectors.lockExclusive();
auto dat = res.initData(vecs->size());
for(size_t h = 0; h< vecs->size(); h++) {
NgVectors &vec = (*vecs)[h];
auto res = dat[h];
KJ_IF_MAYBE(scale, vec.scale) {
res.setScale(vec.fieldnames[*scale]);
}
res.setName(vec.name);
auto datlist = res.initData(vec.fieldnames.size());
for (size_t i = 0; i < vec.fieldnames.size(); i++)
{
datlist[i].setName(vec.fieldnames[i]);
auto dat = datlist[i].getData();
if (!vec.complex_data[i].empty())
{
auto simdat = vec.complex_data[i];
auto list = dat.initComplex(simdat.size());
for (size_t j = 0; j < simdat.size(); j++)
{
list[j].setReal(simdat[j].real());
list[j].setImag(simdat[j].imag());
}
}
else if (!vec.real_data[i].empty())
{
auto simdat = vec.real_data[i];
auto list = dat.initReal(simdat.size());
for (size_t j = 0; j < simdat.size(); j++)
{
list.set(j, simdat[j]);
}
} // else no data apparently
vec.real_data[i].clear();
vec.complex_data[i].clear();
}
}
return kj::READY_NOW;
}
typedef Sim::NgspiceCommands SimCommands;
typedef NgspiceCommandsImpl SimCommandsImpl;