-
Notifications
You must be signed in to change notification settings - Fork 0
/
circuit.c
157 lines (134 loc) · 4.19 KB
/
circuit.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
/*
* this file contain function to deal with circuit main structure;
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "symbol_table.h"
#include "log.h"
#include "circuit.h"
#define PI 3.14159265358979323846
CKTcircuit *makeckt(){
CKTcircuit *circuit = NULL;
if(!(circuit = (CKTcircuit*)calloc(1, sizeof(CKTcircuit)))){
log_fatal("Circuit structure allocation FAILED!");
exit(EXIT_FAILURE);
}
circuit -> MNA_size = 0;
circuit -> simulate_type = NULL_SYM;
return circuit;
}
void free_ckt(CKTcircuit *circuit){
ES_mat_free(circuit -> MNAmat);
ES_mat_free(circuit -> RHSmat);
ES_mat_free(circuit -> RHSmat_prev);
free(circuit);
log_trace("Circuit structure free SUCCESS!");
}
void get_MNA_size(CKTcircuit *circuit, HASH_TAB *htab){
unsigned int size = htab -> n_numsyms - 1; //Number of nodes(minus GND node)
for(int i = 0;i < htab -> e_size;i++){
if(htab -> e_table[i] == NULL)
continue;
ELM_TAB *temp = htab -> e_table[i];
while(temp){
if(temp -> group == 2){
size++;
}
temp = temp -> next;
}
}
for(int i = 0;i < htab -> s_size;i++){
if(htab -> s_table[i] == NULL)
continue;
SRC_TAB *stemp = htab -> s_table[i];
while(stemp){
if(stemp -> group == 2){
size++;
}
stemp = stemp -> next;
}
}
circuit -> MNA_size = size;
}
int get_RHS_index(CKTcircuit *circuit){
circuit -> RHS_free_pointer++;
return circuit -> RHS_free_pointer;
}
double get_element_voltage(HASH_TAB *htab, ELM_TAB *element, int step){
double voltage = 0;
if(element -> node1 -> number != 0){
voltage += (element -> node1 -> voltage[step]);
}
if(element -> node2 -> number != 0){
voltage += -(element -> node2 -> voltage[step]);
}
return voltage;
}
void update_result(ES_mat *x, HASH_TAB *htab, int step){
for(int i = 0;i < htab -> n_size;i++){
if(htab -> n_table[i] == NULL)
continue;
NODE_TAB *node_temp = htab -> n_table[i];
while(node_temp){
if(node_temp -> number != 0){
node_temp -> voltage[step] = x -> data[node_temp -> number - 1][0];
}
node_temp = node_temp -> next;
}
}
for(int i = 0;i < htab -> e_size;i++){
if(htab -> e_table[i] == NULL)
continue;
ELM_TAB *elm_temp = htab -> e_table[i];
while(elm_temp){
if(elm_temp -> group == 2){
elm_temp -> current[step] = x -> data[elm_temp -> index_in_RHS - 1][0];
}
elm_temp = elm_temp -> next;
}
}
for(int i = 0;i < htab -> s_size;i++){
if(htab -> s_table[i] == NULL)
continue;
SRC_TAB *src_temp = htab -> s_table[i];
while(src_temp){
if(src_temp -> group == 2){
src_temp -> current[step] = x -> data[src_temp -> index_in_RHS - 1][0];
}
src_temp = src_temp -> next;
}
}
}
void print_result(HASH_TAB *htab){
printf("Simulation Completed!\n====================\n\n");
for(int i = 1;i < htab -> n_numsyms;i++){
char buf[11];
sprintf(buf,"%d",i);
NODE_TAB *node_temp = search_node(htab, buf);
printf("V(%s)\t=\t%lf\n", node_temp -> key, node_temp -> voltage[0]);
}
for(int i = 0;i < htab -> e_size;i++){
if(htab -> e_table[i] == NULL)
continue;
ELM_TAB *elm_temp = htab -> e_table[i];
while(elm_temp){
if(elm_temp -> group == 2){
printf("I(%s)\t=\t%lf\n", elm_temp -> key, elm_temp -> current[0]);
}
elm_temp = elm_temp -> next;
}
}
for(int j = 0;j < htab -> s_size;j++){
if(htab -> s_table[j] == NULL)
continue;
SRC_TAB *stemp = htab -> s_table[j];
while(stemp){
if(stemp -> group == 2){
printf("I(%s)\t=\t%lf\n", stemp -> sid, stemp -> current[0]);
}
stemp = stemp -> next;
}
}
printf("\n====================\n");
}