schematic pict





Electronic Circuit Simulator

background

I recently took a C++ programming class were the final project was to write a library. Since the class was given by the physics department and we're all physicists (rather than computer scientists), the idea was to make a library that would be useful in scientific work. The course web site has links to all of the student projects; and anybody is free to check them out and use them. There should be documentation with each library, but I'm not promissing anything.

I chose to make a simple electronic circuit simulator(ECS). I wanted to do something that would demonstrate the usefulness of classes and yet not get in over my head in difficult math or physics. My ECS is aimed at the Halliday & Resnick level circuits. I'm going to start at the ground floor and work my way up. In addition to making a library that works some of the other considerations in writing the code are - friendly user interface, sufficient documentation, expandability, and effeciency. I'll let you judge how well I've done in satisfying the requirements.

After I finished the entire library and recieved feedback from both Prof. White and some of my fellow classmates, I've realized that my class structure could have been improved. Right now it is very difficult, if not impossible to add new components to the class. Looking back I would have liked to start with simpler building blocks for my components. Perhaps make resistors, etc out of generic resistors, etc and then inherit to specific configurations. Well, the reason I took the class was to learn c++ as I had no previous experience with the language. I think the project not only gave me good practice in basic coding, but also it helped to illustrate some of the nice features of c++ class structures.

The Code

You really want to see the code? Well, how about I just give you an overview here and then let you follow the links to see the details, okay? Remember what you see here are only the more important members and functions. BTW: there are some comments within the various files, but remember that they were written while I was coding, so they may or may not be useful.


Component.h

This class is what defines each part in the circuit and its behavior. The bulk of the code in the class is devoted to the printing routines.

{

The following help to define what the part is- type, value, etc
char type; (w- wire, r- resistor, c- capacitor, l- inductor, b- battery, n- nothing)
float coef;

These next variables are to represent whether or not the part is soldered to the one above, below, left and/or right. These solder joints will be used by the Circuit class to trace out a circuit.
int solderu, solderd, solderl, solderr;

The Printpic function is used to diplay the parts on the user's screen. The components are all displayed as 5 character x 5 character squares to simply the possible schematics.
void Printpic (int x); (x is simply the row number of the picture 1,...,5)

}


Circuit.h Circuit.cc

The circuit class is the one that stores the circuit, in the breadboard, and has all of the necesary functions to add parts and print the circuit. The are also routines that will trace out the circuit by following the solder joints and ultimately determine if the circuit is closed. All of these functions en collected into Build()- this is really the only one a user ever needs to use.

{

The breadboard is just what you may expect, a place where the componeents of the circuit are stored. I've used the array template class I wrote for one of our earlier homework assignments
Array2<Component> Breadboard;

Start, Current and Previous are markers for places on the Breadboard. They're used in the circuit tmapping functions. Again the point class is one from a previous class assignment.
Point Start, Current, Previous;

Addtoboard and Removefromboard step the user through the process of changing their circuit. The adding function is rather long as I've tried to make it as idiot-proof as possible. It once you select the part and location the function will determine whether or not it will fit there and if it does what its orientation should be. By doing this it is hoped that bad (non-closed) circuits can be eliminated (these would completely confuse the more & advance functions). I won't tell you how much time I spent searching for a missing '}' in the Addtoboard function- looking for the proverbial needle in the haystack to say the least.
void Addtoboard();
void Removefromboard();

In the spirit of an iterator class, I use the Start, Previous and Current points to help the Begin, More and Advance functions travel the circuit. One would use a for loop like for(mycir.Begin(); mycir.More(); mycir.Advnace()) to traverse the entire circuit.
void Begin();
int More();
void Advance();

As I mentioned above, the Build function is the complete package for creating, changnd displaying one's circuit.
void Build();

}

DEeqn.h DEeqn.cc

There is no great reason is named DEeqn raher than Deqn; it is a differential equation class. This class will store a second order equation (it actually solves it as if it was a pair of first order equations) and perform the necesary manipulations. The most useful function for studyi the time evolution of circuits would be Ivstime and Vvstime. They generate data files that my be plotted in your favorite graphics program.

{

The equation is an array of terms (yes, I used a 2-d array class when I only needed a 1-d, but it's what I had laying around within easy reach). The term is given as a structure that holds the information.
Struct Term
{
float coef;
int derivative;
}

Of course, the values of the charge, current and d/dt(current) are stored here. These are what are updated after each dt step by the Dteqn function.
float Charge;
float Current;
float Dcurrent;
void Dteqn(float delta_time);

Once the initial conditions have been inputted, the user may all either Ivstime or Vvstiem to getput files. The files are formatted with time current/ voltage for each time step on a new line. e label in the parameter list is to find the current through or voltage drop across part #label.
void Ivstime (float final_time, float delta_time, int label);
void Vvstime (float final_time, float delta_time, int label);

}


Point.h Point.cc Array2.h Array2.cc

For those who have completed the earlier homework for the c++ class, these will be familar classes. I have added (and removed) a few functions, but nothing too unusual for the reader to figure out.


Makefile Program3.cc

I am including these files here so the reader may download all of the files and try the classes for themselves. The makefile will generate an executible "program3" (program1 and program2 were Beta versions, in case you were wondering) which will show off all of the simliimulator's functions. I have compiled and run this code on the Suns, so any other machine may develop an upset stomach if you try to compile. If you are in need of a simple plotting program, try gnuplot. It's incredibly simple to plot the output file.

sun1% gnuplot

gnuplot> plot "output.dat"

Yup, it's that's easy. The only catch may be in choosing the best terminal type. To see the possible selections type set term at the prompt; it will then give you a listing of the choices. On my Mac, running NCSA Telnet v2.6, I used TEK 40XX generate the plots shown in the test run section. If all else fails you can use the DUMB type; this will use ASCII characters to make the plot on a VT100 terminal.

Test Runs

I've run the program3 several times and included the input/ output here for your viewing pleasure. After looking at Igor Cadez's web page for his project, I decided to go with the animated gif's as well. I spent much of my time working on the user interface and I'd hate for you not to get a chance to see it in action (and I know not everybody is reaching for their mouse to download the source code) I've included three simple cases which give snapshots of the program in action as well as the resulting plots from gnuplot.

Example #1 is a resistor- capacitor circuit which is given an initial charge and allowed to discharge.

Example #2 is an inductor- capacitor circuit. This circuit shows the voltage drop oscillating across the capactor. If you look at the plot, you will notice a period of approximately 12 seconds which agrees nicely with T= 2 pi sqrt(L C).

Example #3 is an RLC circuit. I've only included a couple of snapshots in this gif, but the final plot of the current does show the exponential decaying oscillations.

Future Plans

Hopefully, the future isn't full of debugging the program. I've tried to make it as thorough as possible and avoid possible errors. It's not quite " idiot proof," but maybe close enough to prevent too many run time errors. The main possible source of errors is from the user entering bogus circuits which is why the Addtoboard function is full of the million or so if statements. There may be a few other things that could be added to make it closer to "idiot proof."

Under the catagory of more significant improvements is adding a time dependent voltage source to the possible components. This would allow for AC circuits in addition to the current DC.

Another (and much larger) improvement may come from adding nodes to the possible configurations of wires (wires with more than 2 solder joints). By doing this the user could build parallel circuits and therefore greatly increase the number of possible circuits. But, it would require much more than just adding more to the PRintpic function. The circuit and deeqn classes would have to made much more intelligent; then they would need to identify branches and loops. There would be multiple equations with many different currents and charges (for each branch of the circuit).

The idea would be to have a list or array of equations for each elementary loop. The dteqn function would then be called for each equation. There would also be the need for node (current) equations which would need to be solved at each time step. You can see in the code a few places where a label variable is already in place. These would be used to help identify which branch a component lies on and/ or which current is flowing through it. I believe that the challenge would not come in handling the equations, but rather in writing them down (or identifying the lops and branches). This will require clever advance and more functions as well as a decision function which would make the choices of where to go when encouting a node.



phillips@uci.edu