-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
368 lines (298 loc) · 13.6 KB
/
parser.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbol_table.h"
#include "circuit.h"
#include "log.h"
#include "hash.h"
#include "source.h"
extern FILE* yyin;
extern int yyparse();
extern int yylineno;
extern HASH_TAB *htab;
extern CKTcircuit *circuit;
int parse(const char *file_name){
if(!(yyin = fopen(file_name, "r"))){
log_fatal("can't open %s",file_name);
exit(EXIT_FAILURE);
}
yyparse();
}
void add_RLC(char *eid, int node1, int node2, double value,int group, double ic){
if(elm_insert(htab, eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NO_NODE, NO_NODE, value, group, ic)){
log_error("Duplicated Element %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_V(char *eid, int node1, int node2, double value){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
//if(elm_insert(htab, eid, NULL, node1, node2, NO_NODE, NO_NODE, value, 2)){
// log_error("Duplicated Voltage Source : %s in line %d",eid, (yylineno - 1));
// exit(EXIT_FAILURE);
//}
//new routine
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, DC, 2);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dc_V1] = value;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_sine_v(char *sid, int node1, int node2, double offset, double amp, double freq, double delay, double d_factor, double phase){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, SINE, 2);
stab -> src_coefficient = (double *)calloc(6, sizeof(double));
stab -> src_coefficient[sine_a] = d_factor;
stab -> src_coefficient[sine_Fo] = freq;
stab -> src_coefficient[sine_phi] = phase;
stab -> src_coefficient[sine_Td] = delay;
stab -> src_coefficient[sine_Va] = amp;
stab -> src_coefficient[sine_Vo] = offset;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_sine_i(char *sid, int node1, int node2, double offset, double amp, double freq, double delay, double d_factor, double phase, int group){
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, SINE, group);
stab -> src_coefficient = (double *)calloc(6, sizeof(double));
stab -> src_coefficient[sine_a] = d_factor;
stab -> src_coefficient[sine_Fo] = freq;
stab -> src_coefficient[sine_phi] = phase;
stab -> src_coefficient[sine_Td] = delay;
stab -> src_coefficient[sine_Va] = amp;
stab -> src_coefficient[sine_Vo] = offset;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_pulse_v(char *sid, int node1, int node2, double v_off, double v_on, double delay, double Tr, double Tf, double t_on, double t_period){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, PULSE, 2);
stab -> src_coefficient = (double *)calloc(7, sizeof(double));
stab -> src_coefficient[pulse_Td] = delay;
stab -> src_coefficient[pulse_Tf] = Tf;
stab -> src_coefficient[pulse_To] = t_period;
stab -> src_coefficient[pulse_Tr] = Tr;
stab -> src_coefficient[pulse_Tw] = t_on;
stab -> src_coefficient[pulse_V1] = v_on;
stab -> src_coefficient[pulse_Vo] = v_off;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_pulse_i(char *sid, int node1, int node2, double v_off, double v_on, double delay, double Tr, double Tf, double t_on, double t_period, int group){
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, PULSE, group);
stab -> src_coefficient = (double *)calloc(7, sizeof(double));
stab -> src_coefficient[pulse_Td] = delay;
stab -> src_coefficient[pulse_Tf] = Tf;
stab -> src_coefficient[pulse_To] = t_period;
stab -> src_coefficient[pulse_Tr] = Tr;
stab -> src_coefficient[pulse_Tw] = t_on;
stab -> src_coefficient[pulse_V1] = v_on;
stab -> src_coefficient[pulse_Vo] = v_off;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_ramp_v(char *sid, int node1, int node2, double delay){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, RAMP, 2);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[ramp_Td] = delay;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_ramp_i(char *sid, int node1, int node2, double delay, int group){
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, RAMP, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[ramp_Td] = delay;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_step_v(char *sid, int node1, int node2, double v_on, double delay){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, STEP, 2);
stab -> src_coefficient = (double *)calloc(2, sizeof(double));
stab -> src_coefficient[step_Td] = delay;
stab -> src_coefficient[step_V1] = v_on;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_step_i(char *sid, int node1, int node2, double v_on, double delay, int group){
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, STEP, group);
stab -> src_coefficient = (double *)calloc(2, sizeof(double));
stab -> src_coefficient[step_Td] = delay;
stab -> src_coefficient[step_V1] = v_on;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_ac_v(char *sid, int node1, int node2, double amp, double phase){
if(node1 == node2){
log_error("Consistency Requirement ERROR! SHORTED VOLTAGE SOURCE : %s in Line %d", sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, FREQ_DOMAIN, AC, 2);
stab -> src_coefficient = (double *)calloc(2, sizeof(double));
stab -> src_coefficient[ac_amp] = amp;
stab -> src_coefficient[ac_phi] = phase;
if(src_insert(htab, stab)){
log_error("Duplicated Voltage Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_ac_i(char *sid, int node1, int node2, double amp, double phase, int group){
SRC_TAB *stab = create_src(sid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, FREQ_DOMAIN, AC, group);
stab -> src_coefficient = (double *)calloc(2, sizeof(double));
stab -> src_coefficient[ac_amp] = amp;
stab -> src_coefficient[ac_phi] = phase;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",sid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_I(char *eid, int node1, int node2, double value, int group){
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, DC, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dc_V1] = value;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_VCCS(char *eid, int node1, int node2, int node3, int node4, double value, int group){
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), search_node_by_num(htab, node3), search_node_by_num(htab, node4), TIME_DOMAIN, DEPENDENT, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dep_value] = value;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_VCVS(char *eid, int node1, int node2, int node3, int node4, double value, int group){
//if(elm_insert(htab, eid, NULL, node1, node2, node3, node4, value, group)){
// log_error("Duplicated VCVS : %s in line %d",eid, (yylineno - 1));
// exit(EXIT_FAILURE);
//}
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), search_node_by_num(htab, node3), search_node_by_num(htab, node4), TIME_DOMAIN, DEPENDENT, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dep_value] = value;
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_CCCS(char *eid, char *cvs, int node1, int node2, double value, int group){
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, DEPENDENT, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dep_value] = value;
stab -> cvs = (char *)malloc((strlen(cvs) + 1) * sizeof(char));
if(stab -> cvs == NULL){
log_fatal("cvs Allocation FAILD!");
//safe exit
exit(EXIT_FAILURE);
}
strcpy(stab -> cvs, cvs);
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_CCVS(char *eid, char *cvs, int node1, int node2, double value, int group){
SRC_TAB *stab = create_src(eid, search_node_by_num(htab, node1), search_node_by_num(htab, node2), NULL, NULL, TIME_DOMAIN, DEPENDENT, group);
stab -> src_coefficient = (double *)calloc(1, sizeof(double));
stab -> src_coefficient[dep_value] = value;
stab -> cvs = (char *)malloc((strlen(cvs) + 1) * sizeof(char));
if(stab -> cvs == NULL){
log_fatal("cvs Allocation FAILD!");
//safe exit
exit(EXIT_FAILURE);
}
strcpy(stab -> cvs, cvs);
if(src_insert(htab, stab)){
log_error("Duplicated Current Source : %s in line %d",eid, (yylineno - 1));
exit(EXIT_FAILURE);
}
}
void add_node(int num){
char buf[11];
sprintf(buf,"%d",num);
if(node_exist(htab, buf)){
node_insert(htab,buf,num);
}
}
void set_simultaor_dc(){
if(!(circuit -> simulate_type == NULL_SYM)){
log_error("SIMULATOR Already SET!");
return;
}
circuit -> simulate_type = DC_SYM;
node_voltage_initial(htab, 1);
element_current_initial(htab, 1);
src_current_initial(htab, 1);
log_trace("current and voltage allocation success!");
}
void set_simultaor_tran(double Tstop, double Tstep){
if(circuit -> simulate_type != NULL_SYM){
log_error("SIMULATOR Already SET!");
return;
}
circuit -> simulate_type = TRAN_SYM;
circuit -> Tstart = 0;
circuit -> Tstep = Tstep;
circuit -> Tstop = Tstop;
circuit -> step_num = (int)(Tstop/Tstep);
//node_voltage_initial(htab, circuit -> step_num);
//element_current_initial(htab, circuit -> step_num);
//src_current_initial(htab, circuit -> step_num);
log_trace("current and voltage allocation success! step number = %d, Tstep = %f, Tstop = %f",circuit -> step_num,circuit -> Tstep, circuit -> Tstop);
}
void set_simulator_ac(double freq){
if(circuit -> simulate_type != NULL_SYM){
log_error("SIMULATOR Already SET!");
return;
}
circuit -> simulate_type = AC_SYM;
circuit -> frequency = freq;
circuit -> omega = 2 * PI * freq;
log_trace("AC Simulator set done!");
}
void set_output(char *out){
if(circuit -> out.id == NULL){
circuit -> out.id = (char *)malloc((strlen(out) + 1) * sizeof(char));
strcpy(circuit -> out.id, out);
}
else{
log_error("Output Already set!");
}
}
void yyerror(char *msg){
log_fatal("%s in line %d", msg, yylineno);
}