-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
132 lines (110 loc) · 4.7 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <lapacke.h>
#include <complex.h>
#include "symbol_table.h"
#include "circuit.h"
#include "parser.h"
#include "log.h"
#include "hash.h"
#include "defs.h"
#include "simulator.h"
#include "source.h"
/*
Work Flow:
1) Initializing Simulator:
-Initializing Symbol table
-Initializing Circuit structure
2) Parsing netlist and adding elements to symbol table
3) Get MNA Matrix size and add it to Circuit structure
4) Initializing MNA and RHS Matrices with given size
5) Based on Analysis mode(ac, dc, Transient):
-Allocate enough memory for each node voltage
-Allocate enough memory for each Group2 Elemts current
6) Stamping Elements to MNA Matrix
7) Solve the Matrix
8) Update the voltages and currents in symbol table
9) Printing the Result to user given file
10) Free every matrices and structures we have allocated
*/
int main(int argc, char **argv){
switch (argc){
case 1:
printf("EDGE-SPICE v1.0\nAuther : Ali Esmaeily - University of Tabriz - Spring 2023\n"
"Use Following Format : espice <netlist> <output>\n");
return 0;
break;
case 2:
//Log redirection
FILE *log = fopen("log","w");
log_add_fp(log, LOG_TRACE);
log_set_level(LOG_ERROR);
//symbol table and circuit structure initial
htab = maketab(53, 127, 53, symtab_hash_pjw, node_cmp, elm_cmp, src_cmp);
circuit = makeckt();
log_trace("SPICE initialization SUCCESS!");
//Parsing netlist
parse(argv[1]);
print_element_table(htab, log);
print_node_table(htab, log);
print_src_table(htab, log);
//Set RHS pointer
circuit -> RHS_free_pointer = htab -> n_numsyms - 1; //Because of GND node
//MNA size
get_MNA_size(circuit, htab);
log_trace("MNA matrix size is %d", circuit -> MNA_size);
//simulate
switch (circuit -> simulate_type){
case DC_SYM:
//MNA and RHS matrices initialization
circuit -> MNAmat = ES_mat_new(circuit -> MNA_size, circuit -> MNA_size);
circuit -> RHSmat = ES_mat_new(circuit -> MNA_size, 1);
//circuit -> RHSmat_prev = ES_mat_new(circuit -> MNA_size, 1);
//circuit -> RESmat = ES_mat_new(circuit -> MNA_size, 1);
//circuit -> RESmat_prev = ES_mat_new(circuit -> MNA_size, 1);
simulate_DC(circuit, htab, log);
ES_mat_print(circuit -> MNAmat, log);
ES_mat_print(circuit -> RHSmat, log);
ES_mat_print(circuit -> RHSmat_prev, log);
break;
case TRAN_SYM:
//update sources
if(circuit -> out.id == NULL){
log_fatal("Output variable not defined! Simulate STOPED!");
exit(EXIT_FAILURE);
}
//MNA and RHS matrices initialization
circuit -> MNAmat = ES_mat_new(circuit -> MNA_size, circuit -> MNA_size);
circuit -> RHSmat = ES_mat_new(circuit -> MNA_size, 1);
circuit -> RHSmat_prev = ES_mat_new(circuit -> MNA_size, 1);
circuit -> RESmat = ES_mat_new(circuit -> MNA_size, 1);
circuit -> RESmat_prev = ES_mat_new(circuit -> MNA_size, 1);
transient_source_update(circuit, htab);
simulate_TRAN(circuit, htab, log);
ES_mat_print(circuit -> MNAmat, log);
ES_mat_print(circuit -> RHSmat, log);
ES_mat_print(circuit -> RHSmat_prev, log);
break;
case AC_SYM:
circuit -> MNAmat_comp = ES_mat_comp_new(circuit -> MNA_size, circuit -> MNA_size);
circuit -> RHSmat_comp = ES_mat_comp_new(circuit -> MNA_size, 1);
//circuit -> RHSmat_prev_comp = ES_mat_comp_new(circuit -> MNA_size, 1);
//circuit -> RESmat_comp = ES_mat_comp_new(circuit -> MNA_size, 1);
//circuit -> RESmat_prev_comp = ES_mat_comp_new(circuit -> MNA_size, 1);
simulate_AC(circuit, htab, log);
ES_mat_comp_print(circuit -> MNAmat_comp, log);
ES_mat_comp_print(circuit -> RHSmat_comp, log);
ES_mat_comp_print(circuit -> RHSmat_prev_comp, log);
break;
default:
log_fatal("Undefined Type Simulator!");
break;
}
free_hash_table(htab);
free_ckt(circuit);
fclose(log);
return 0;
default:
break;
}
}