Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 10 additions & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,30 +1,16 @@
# UCR EE 213 Fall 16
# UCR EE 213 winter 2018: Computer-Aided Electronic Circuit Simulation
This is a repository for EE213 final project.

---
The framework of starter-code in my work directory if forked form: https://github.com/sheldonucr/ucr-ee213

This is a repository for final project submission.
What I do in the project:
1. Finish all parser-related files in starter-code, so that the parser can parse the input circuit and generate a pre-simulate file for Matlab for further simulation.
2. Create a MNA time-domain analysis in Matlab.
3. Create a MNA frequency-domain analysis in Matlab.

---

### Step 1: Fork this repo
The folder of 'netlist' contains a test file.

In the top-right corner of the page, click **Fork**.
![fork](https://help.github.com/assets/images/help/repository/fork_button.jpg)
Folder 'starter-code' contains all the execution code for paerser. Further instuction is included to run the code.

### Step 2: Create a local clone of your fork
```
$ git clone https://github.com/YOUR-USERNAME/ucr-ee213
```

### Step 3: Create your work directory under project/
```
$ mkdir project/YOUR-NETID
```

You can copy the starter-code into your work directory and work on it.
DO NOT change anything elsewhere.

### Step 4: Submit your project

Make sure you didn't change anything outside your work directory.
Click the green button **New Pull Request**.
'Time analysis.zip' and 'frequency analysis.zip' are Matlab files with corresponding input data from parser. You can use your own data from parser.
Binary file not shown.
Binary file added frequency analysis.zip
Binary file not shown.
2 changes: 2 additions & 0 deletions starter_code/matlab_spice_parser/C&L.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2 8.000000
1 6.000000
3 changes: 3 additions & 0 deletions starter_code/matlab_spice_parser/MNA_Equation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.200000 -2.200000 2.000000 -1.000000 -0.200000 2.200000 -2.000000 0.000000 0.000000 0.000000 0.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 0.166667 -0.166667 0.000000 0.000000 -0.166667 0.166667 0.000000 0.000000 0.000000 0.000000 -8.000000
-10.000000 0.000000 10.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
155 changes: 146 additions & 9 deletions starter_code/matlab_spice_parser/MNA_Matrix.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,41 @@
#include <string.h>
#include "Symbol_Table.h"
#include "MNA_Matrix.h"
#include "parse_func.h"

#define WARNING_DIV_BY_ZERO \
printf("\nWarning: divide by zero.");

int MatrixSize = 0; // size of the MNA matrix (i.e., the max dimension)
double **MNAMatrix = NULL;
double *RHS = NULL;

/**
Assign indexes to all nodes in the node table.
The ground node (with name "0") must be assigned index zero.
The rest nodes are assigned indexes from 1, 2, 3 continuously.
*/

int basic_size;// basic_size is the #node+ #inductors + #independent voltage source
void Index_All_Nodes()
{
MatrixSize = 3;

Node_Entry *n = *NodeTable;
for(int i=0;i!=NodeTableSize;i++){
n->index = NameHash(n->name,NodeTableSize);
n=n->next;
}
Node_Entry *cur = *NodeTable;
int common_node_size=0;
while(cur != NULL){
if(cur->index>common_node_size)
common_node_size=cur->index;
cur=cur->next;
}
MatrixSize=common_node_size+nInd+nVsrc;
basic_size=common_node_size;
}



int Get_Matrix_Size()
{
return MatrixSize;
Expand Down Expand Up @@ -68,6 +84,8 @@ void Get_MNA_System(double **A, double **b)

(!!) Students can try SPARSE matrix format.
*/
double **MNA_C_Matrix = NULL;//MNA_C_Matrix contains the Capacaitor and inductor value for frequency domain MNA matrix.
double **MNA_T_Matrix = NULL;//MNA_T_Matrix contains the Capacaitor and inductor value for time domain MNA matrix.
void Init_MNA_System()
{
#if 1
Expand All @@ -87,10 +105,34 @@ void Init_MNA_System()
// Initialize to zero
for (i = 0; i <= MatrixSize; i++) {
for (j = 0; j <= MatrixSize; j++) {
MNAMatrix[i][j] = 10.0;
MNAMatrix[i][j] = 0.0;
}
RHS[i] = 0.0;
}



MNA_C_Matrix = (double**) malloc( (MatrixSize+1) * sizeof(double*) );
for (i = 0; i <= MatrixSize; i++) {
MNA_C_Matrix[i] = (double*) malloc( (MatrixSize+1) * sizeof(double) );
}
for (i = 0; i <= MatrixSize; i++) {
for (j = 0; j <= MatrixSize; j++) {
MNA_C_Matrix[i][j] = 0.0;
}
}

MNA_T_Matrix = (double**) malloc( (MatrixSize+1) * sizeof(double*) );
for (i = 0; i <= MatrixSize; i++) {
MNA_T_Matrix[i] = (double*) malloc( (MatrixSize+1) * sizeof(double) );
}
for (i = 0; i <= MatrixSize; i++) {
for (j = 0; j <= MatrixSize; j++) {
MNA_T_Matrix[i][j] = 0.0;
}
RHS[i] = 20.0;
}


#endif
}

Expand All @@ -104,25 +146,120 @@ void Init_MNA_System()
*/
void Create_MNA_Matrix()
{
Device_Entry *n = *DeviceTable;
FILE *f2;
f2=fopen("C&L.txt","w+");
while(n!= NULL){
switch(n->name[0]){
case 'R'://resistor
MNAMatrix[n->nodelist[0]->index][n->nodelist[0]->index] += 1.0/(n->value);
MNAMatrix[n->nodelist[1]->index][n->nodelist[1]->index] += 1.0/(n->value);
MNAMatrix[n->nodelist[0]->index][n->nodelist[1]->index] -= 1.0/(n->value);
MNAMatrix[n->nodelist[1]->index][n->nodelist[0]->index] -= 1.0/(n->value);break;

case 'G': //VCCS
MNAMatrix[n->nodelist[0]->index][n->nodelist[2]->index] += (n->value);
MNAMatrix[n->nodelist[0]->index][n->nodelist[3]->index] -= (n->value);
MNAMatrix[n->nodelist[1]->index][n->nodelist[2]->index] -= (n->value);
MNAMatrix[n->nodelist[1]->index][n->nodelist[3]->index] += (n->value);break;



case 'C': //capacitor
MNA_C_Matrix[n->nodelist[0]->index][n->nodelist[0]->index] += 1.0/(n->value);
MNA_C_Matrix[n->nodelist[1]->index][n->nodelist[1]->index] += 1.0/(n->value);
MNA_C_Matrix[n->nodelist[0]->index][n->nodelist[1]->index] -= 1.0/(n->value);
MNA_C_Matrix[n->nodelist[1]->index][n->nodelist[0]->index] -= 1.0/(n->value);

MNA_T_Matrix[n->nodelist[0]->index][n->nodelist[0]->index] += (n->value);// actually it is (capacitor->value)/h, h is the time step and I let h=1.
MNA_T_Matrix[n->nodelist[1]->index][n->nodelist[1]->index] += (n->value);
MNA_T_Matrix[n->nodelist[0]->index][n->nodelist[1]->index] -= (n->value);
MNA_T_Matrix[n->nodelist[1]->index][n->nodelist[0]->index] -= (n->value);
fprintf(f2,"\t%d\t%f\n", n->nodelist[0]->index, n->value);
break;

case 'L': // inductor
basic_size+=1;
MNAMatrix[n->nodelist[0]->index][basic_size] = 1;
MNAMatrix[n->nodelist[1]->index][basic_size] = -1;
MNAMatrix[basic_size][n->nodelist[0]->index] = 1;
MNAMatrix[basic_size][n->nodelist[1]->index] = -1;
MNA_C_Matrix[basic_size][basic_size] -= n->value;

MNA_T_Matrix[basic_size][basic_size] -= n->value;
fprintf(f2,"\t%d\t%f\n", n->nodelist[0]->index, n->value);
break;
case 'I': //current source
RHS[n->nodelist[0]->index] -= n->value;
RHS[n->nodelist[1]->index] += n->value; break;
case 'V': //independent coltage source
basic_size+=1;
MNAMatrix[n->nodelist[0]->index][basic_size] = 1;
MNAMatrix[n->nodelist[1]->index][basic_size] = -1;
MNAMatrix[basic_size][n->nodelist[0]->index] = 1;
MNAMatrix[basic_size][n->nodelist[1]->index] = -1;
RHS[basic_size] += n->value;break;

};

n = n->next;
}
fclose(f2);
}

void printfile(){
int i,j;
FILE *f;
FILE *f3;
f=fopen("MNA_Equation.txt","w+");
f3=fopen("MNA_Time.txt","w+");
for (i = 0; i <= MatrixSize; i++){
for (j = 0; j <= MatrixSize; j++) {
fprintf(f,"\t%f", MNAMatrix[i][j]);
fprintf(f3,"\t%f",MNAMatrix[i][j]);
}
}
fprintf(f,"\n");
fprintf(f3,"\n");
for (i = 0; i <= MatrixSize; i++) {
for (j = 0; j <= MatrixSize; j++) {
fprintf(f,"\t%f", MNA_C_Matrix[i][j]);
fprintf(f3,"\t%f", MNA_T_Matrix[i][j]);
}
}
fprintf(f,"\n");
fprintf(f3,"\n");
for (i = 0; i <= MatrixSize; i++) {
fprintf(f,"\t%f", RHS[i]);
fprintf(f3,"\t%f", RHS[i]);
}
for(i=0;i<=MatrixSize;i++){
for(j=1;j<=MatrixSize;j++){
fprintf(f,"\t%f", 0);
fprintf(f3,"\t%f", 0);
}

}
fclose(f);
fclose(f3);

}
void Print_MNA_System()
{
int i, j;

printf("\n\n");
for (j = 0; j <= MatrixSize; j++) {
printf("\t%-12d", j);
printf("\t%-18d", j);
}
printf("\tRHS");

for (i = 0; i <= MatrixSize; i++) {
printf("\n[%-3d]", i);
for (j = 0; j <= MatrixSize; j++) {
printf("\t%-12f", MNAMatrix[i][j]);
printf("\t%-3f+%-3fs", MNAMatrix[i][j],MNA_C_Matrix[i][j]);
}
printf("\t%-12f", RHS[i]);
}
printfile();
}


3 changes: 3 additions & 0 deletions starter_code/matlab_spice_parser/MNA_Time.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
0.200000 -2.200000 2.000000 -1.000000 -0.200000 2.200000 -2.000000 0.000000 0.000000 0.000000 0.000000 1.000000 -1.000000 0.000000 1.000000 0.000000
0.000000 0.000000 0.000000 0.000000 0.000000 6.000000 -6.000000 0.000000 0.000000 -6.000000 6.000000 0.000000 0.000000 0.000000 0.000000 -8.000000
-10.000000 0.000000 10.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000 0.000000
31 changes: 30 additions & 1 deletion starter_code/matlab_spice_parser/README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,34 @@
How to compile and run my code! (under Ubuntu/ee-213/starter_code/matlab_spice_parser)

Step1 $ rm Symbol_Table.o main.o MNA_Matrix.o parse_func.o
(If you change any code under this folder, it's better to delete all .o files and complie again.)

Step2 $ make
(Compile)

Step3 $./runparse netlist_t1.sp
(netlist_t1.sp needs to be placed under /matlab_spice_parser)





Please also read ee213_final project_Lab record(实验记录)-Yibo Liu.pdf.
This pdf records how to solve possible errors while compiling the project under ubuntu.











*****************************************************************************************************
README - a brief documentation on the package (student version)
Jan. 2015
March. 2018
by Guoyong SHI: [email protected]

This circuit parser provides an interface to MATLAB so that students can
Expand Down
Loading