I have spent the last 2 days trying to figure out how to generalize the process of subcircuits for the SPICE software. I've gone through a few design possibilities and finally came to the conclusion that simple netlist style templates will work perfectly.
So instead of reinventing the wheel, I'll use the classic technique of netlisting. I will end up using a different netlist for different simulation types.
Example:
The capacitor companion model for a ...
Transient Analysis: Consists of a resistor in parallel with a current source.
DC Analysis: Consists of either a perfect open or very high value resistor (> 1 Gigaohm)
Small Signal or AC Analysis: Consists of a resistor with equation R = 1/(2*pi*f*C)
So I'll have different models for different simulation types as you can see. The biggest question will be how to define the equations for the individual parameters. How should I implement these into classes inside the program? Creating text file models is the easy task. It's a little more difficult to implement into Classes.
That's the extent of where I am currently.
--------------------
Justin Coulston
justin.coulston@gmail.com
As an Engineer, this site will provide new ways to perform old tasks specifically in Electrical and Software Engineering. Some things will be informational on RF technologies while others will be new altogether. Enjoy
Search This Blog
Showing posts with label circuit simulation. Show all posts
Showing posts with label circuit simulation. Show all posts
Sunday, April 18, 2010
Tuesday, April 13, 2010
Simulation: Update on the Complex Parser
This is a quick update on the software. I was able to separate the string into separate tokens. There are 3 types of tokens:
What will happen on a later date, this "Evaluate" subroutine will turn the real token into a complex number and the imaginary token into a complex number. The inner token will go back through the "Evaluate" subroutine (without the parenthesis) until a complex number is returned. This method is using recursion to evaluate a full complex string.
The next step will be to perform the math in operator precedence. This may get a little tricky since they aren't in order when I produce the tokens. Based on my current structure, this isn't easily done. But now that I have a structure I can possibly work around this. Wish me luck
---------
Justin Coulston
Justin.Coulston@gmail.com
0 = Real Token
1 = Imaginary Token
2 = Inner Token (within parenthesis).
What will happen on a later date, this "Evaluate" subroutine will turn the real token into a complex number and the imaginary token into a complex number. The inner token will go back through the "Evaluate" subroutine (without the parenthesis) until a complex number is returned. This method is using recursion to evaluate a full complex string.
The next step will be to perform the math in operator precedence. This may get a little tricky since they aren't in order when I produce the tokens. Based on my current structure, this isn't easily done. But now that I have a structure I can possibly work around this. Wish me luck
---------
Justin Coulston
Justin.Coulston@gmail.com
Labels:
circuit simulation,
Complex,
Mathematics,
SPICE,
tokens
Monday, April 12, 2010
Simulation: Complex Math Parser
Introduction
In my project to build a SPICE program, I ran across a problem; I need to be able to perform complex math from strings. This has already been done a thousand times before but finding a free, good parser isn't easy. So I decided to do this myself. This article will explain briefly the process I'm using to parse a complex math string.
Basic Process
In my project to build a SPICE program, I ran across a problem; I need to be able to perform complex math from strings. This has already been done a thousand times before but finding a free, good parser isn't easy. So I decided to do this myself. This article will explain briefly the process I'm using to parse a complex math string.
Basic Process
- First check the string for incorrect characters. This can be done while performing step 2
- Define every character in the string as a type (ie. 0=operator, 1=real number, 2=imaginary number, 3=left parenthesis, 4=right parenthesis). Define this into a separate string. The reason you do this is to take in different number types (real, imaginary, scientific notation, etc.)
- Next separate the string into tokens to be evaluated separately. The most open parenthesis should be it's own token. (ie. str = "2*2j+(20+2j)^12" will turn into tokens "@1*@2+@3^@4") This is only an example. I doubt i'll have a string like this.
- Evaluate each token by recursion through the same function until it outputs a complex number. (a custom class)
- With a complex number for the tokens you can then evaluate in precedent order of functions.
- Output complex number
That's the quick and easy of it. I have only completed steps 1 and 2 so far. The rest will hopefully be completed in the week. Hope this is helpful...
Labels:
circuit simulation,
Complex,
Mathematics,
parser,
programming,
software,
SPICE,
tokens
Saturday, April 10, 2010
Simulation: My SPICE software
This blog is just to update and tell you guys that I have started a SPICE software package. Of course it'll be simple and won't do things PSPICE or B2SPICE can do but it's a great way to learn the process easier. I have already implemented the portion of the program that can simulate current sources and resistors. You can have as many nodes as you desire and it seems to work perfect (confirmed with B2SPICE). So I think that's good...
As for the software structure. I had to create a simple Complex Class and Matrix Class. The most difficult part was creating the inversion routine for the matrix. I didn't realize how complicated this was until I started to write it. I used the slowest and most real, the reduced diagonal method. I basically used linear algebra directly without any optimizations.
Anyways, this project will take a while. Once I can develop a simple engine, I'll put a GUI around it and make it better in a number of ways (if possible). Any suggestions, let me know
-----------------------
Justin Coulston
justin.coulston@gmail.com
As for the software structure. I had to create a simple Complex Class and Matrix Class. The most difficult part was creating the inversion routine for the matrix. I didn't realize how complicated this was until I started to write it. I used the slowest and most real, the reduced diagonal method. I basically used linear algebra directly without any optimizations.
Anyways, this project will take a while. Once I can develop a simple engine, I'll put a GUI around it and make it better in a number of ways (if possible). Any suggestions, let me know
-----------------------
Justin Coulston
justin.coulston@gmail.com
Labels:
circuit simulation,
matrix,
Simulation,
software,
SPICE
Friday, April 9, 2010
Simulation: Basics of SPICE Modeling (Matrix Templates) - 2 of 6
Introduction
This is a continuation of the SPICE modeling series. I will attempt to explain how SPICE uses Matrix Templates to create a main matrix to perform the calculation.
SPICE Templates
Before I explain how voltage sources are used I should first explain how SPICE knows where to put information based on node numbers.
Each component in SPICE has an associated Matrix template that is added to a final matrix that performs the final calculation. As repeated from the last SPICE tutorial: [G][U]=[K] are the [G], conductance matrix, [U], unknowns matrix, and [K], knowns matrix, or the matrix with the known sources (voltage and current sources).
The initial matrix for the conductances starts with inserting the conductances between the nodes first. Each row and column corresponds with a particular node number. So we can easily construct the matrix by summing the conductances between the nodes.
This is a continuation of the SPICE modeling series. I will attempt to explain how SPICE uses Matrix Templates to create a main matrix to perform the calculation.
SPICE Templates
Before I explain how voltage sources are used I should first explain how SPICE knows where to put information based on node numbers.
Each component in SPICE has an associated Matrix template that is added to a final matrix that performs the final calculation. As repeated from the last SPICE tutorial: [G][U]=[K] are the [G], conductance matrix, [U], unknowns matrix, and [K], knowns matrix, or the matrix with the known sources (voltage and current sources).
The initial matrix for the conductances starts with inserting the conductances between the nodes first. Each row and column corresponds with a particular node number. So we can easily construct the matrix by summing the conductances between the nodes.
where n is the total number of nodes (not including ground)
So any components connected to node 1 will be summed at G11=1/R1+1/R2+ ... +1/Rn.
Now that you see how this first matrix works you can can understand how the templates can work. For example, the resistor template has a node A and node B connection points with a resistance R. Below is the matrix template
Now of course the template won't have the words node A and node B in them. So if the resistor was between nodes 3 and 6 then you would add T11 to G33, T22 to G66, T12 to G36, and T21 to G63 of the main matrix.
So you follow this for all the components. All components have some sort of matrix template. Below I will the capacitive and current sources. Inductor templates use a voltage source so we will talk about that later.
So you follow this for all the components. All components have some sort of matrix template. Below I will the capacitive and current sources. Inductor templates use a voltage source so we will talk about that later.
Labels:
circuit simulation,
Companion Model,
matrices,
matrix,
Matrix Templates,
Modeling,
Simulation,
SPICE
Monday, April 5, 2010
Simulation: Basics of SPICE Modeling (Current Sources) - 1 of 6
Introduction
There has been a few people that have talked to me recently about SPICE modeling. It seems a lot of people use it but no one really knows how it works. There are a number of books available that explain how it works. One of the better (and older to today's standards) books is the Inside SPICE: Overcoming The Obstacles of Circuit Simulation. This is a great book detailing the workings of the basic SPICE simulation. There is a link to amazon below for it. I will attempt to explain the first part of a series of posts about SPICE software. This one will discuss the basics of Nodal Analysis (NA).
Nodal Analysis (NA)
Nodal Analysis is just as it sounds, the standard current summation at the various interconnection nodes. Electrical Engineers learn this in their first circuits class. NA is all that's needed to do DC and AC SPICE simulations. I will discuss here how to perform basic Nodal Analysis.
You should note that in this method you can only have Current Sources and Resistors in the circuit. In order to use voltage sources you must use Modified Nodal Analysis and to use the capacitors and inductors you must use companion models with numeric integration techniques. But both of these are based off this first simulation style of Nodal Analysis. I will discuss the prior techniques in later posts...
Now you see that we have 2 unknowns and two equations. The next step is to set up the matrices. You will end up with 3 main matrices, a conductances matrix, unknowns matrix, and a knowns matrix. After you separate the matrices you should get something that looks like this:
So the matrices with the 1/R-Values is the Conductances Matrix [G] and the matrix with voltages is the unknowns matrix [V] while the right-hand side matrix is the knowns matrix [I]. So how do you get results? You have to do matrix manipulation. The best way to do this is to do take the inverse of the G-matrix and multiply it to both sides.
![\bg_black [G]^{-1}\cdot[G]\cdot [V]=[G]^{-1}\cdot[I]](https://lh3.googleusercontent.com/blogger_img_proxy/AEn0k_uEeOMCu0pybAo4aQ55nFpk0Wg34inF9_audatBP0w1f1FTCEZ-_ocpvzu6y6zKSF4m94puFeZjyl2CyvZGXiNbEHem6f6Fo4wP5XhGYTCM-UFd9CnV2sx51sjGmF0BaKtzWiuZWS2NXMw55qweGVlZlJFdTpmiep2r59_dnyt0qzh25bzu1URSf-EE2K6-vscjMMd6jMEWg_aH8dlBqiBjqBDBxg=s0-d)
Step 2:
There has been a few people that have talked to me recently about SPICE modeling. It seems a lot of people use it but no one really knows how it works. There are a number of books available that explain how it works. One of the better (and older to today's standards) books is the Inside SPICE: Overcoming The Obstacles of Circuit Simulation. This is a great book detailing the workings of the basic SPICE simulation. There is a link to amazon below for it. I will attempt to explain the first part of a series of posts about SPICE software. This one will discuss the basics of Nodal Analysis (NA).
Nodal Analysis (NA)
Nodal Analysis is just as it sounds, the standard current summation at the various interconnection nodes. Electrical Engineers learn this in their first circuits class. NA is all that's needed to do DC and AC SPICE simulations. I will discuss here how to perform basic Nodal Analysis.
You should note that in this method you can only have Current Sources and Resistors in the circuit. In order to use voltage sources you must use Modified Nodal Analysis and to use the capacitors and inductors you must use companion models with numeric integration techniques. But both of these are based off this first simulation style of Nodal Analysis. I will discuss the prior techniques in later posts...
Figure 1: Basic Nodal Analysis Example
As you can notice in Figure 1, this circuit has a current source, and resistors. You can do the node voltage technique to analyze this circuit. This is what SPICE uses. So to analyze this, find the two nodal equations:
Now you see that we have 2 unknowns and two equations. The next step is to set up the matrices. You will end up with 3 main matrices, a conductances matrix, unknowns matrix, and a knowns matrix. After you separate the matrices you should get something that looks like this:
So the matrices with the 1/R-Values is the Conductances Matrix [G] and the matrix with voltages is the unknowns matrix [V] while the right-hand side matrix is the knowns matrix [I]. So how do you get results? You have to do matrix manipulation. The best way to do this is to do take the inverse of the G-matrix and multiply it to both sides.
Step 1:
Step 2:
Once you perform this calculation, you have solved for the unknowns V1 and V2. And that's it. I provided above the solution in the schematic. You can use these numbers to test it yourself.
If you want to do quick analysis of this, place it in MS Excel and use the MMINVERSE and MMULT commands. Google this and you should find plenty on it. I may post a blog later on using these.
Conclusion
It's amazing how simple this method is. Everything else is based off this method. Even the MNA is based on this method. I will continue and show you guys the MNA method in the next post.
This is the one I have. But there is a newer version below
This is the second edition.
--------------------
Justin Coulston
justin.coulston@gmail.com
Labels:
circuit simulation,
Current Sources,
DC Analysis,
Excel,
MMINVERSE,
MMULT,
Modeling,
Nodal Analysis,
Resistors,
Simulation,
SPICE
Subscribe to:
Posts (Atom)