|
|
![]() |
|
|
|
||
| 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. | ||
|
|
||
| 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.hThis 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
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.
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.
}
| ||
|
|
||
|
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. | ||
|
|
||
|
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. | ||
|
![]() |
|
|