Thanks again for Ayumi’s pctube, I can now generate good-looking composite characteristic charts using R. I think this is an easier and better way than using SPICE. Since all the relevant tube data are already stored in the pctube program, it eliminates the need to build a full simulation circuit in SPICE, with multiple meters in order to generate the plots (although that was pretty clever also).
I am sure there are many, many more tasks that can be done with R, unfortunately, without the right sort of mind, I find it difficult to write these sub-routines, so I just have to rely on the smarts of people like Ayumi to come up with the proper codes…
It is a bit frustrating when the tools do not do what they are suppose to do… In an earlier post, I mentioned that TINA-TI could not even perform a transient analysis on a simple amplifier, well it happened again when I tried to run some simulations of the Baby Huey design found at diyaudio. Numerous circuit examples were simmed over the years, none ever complained that they had any issue with convergence.
But true to form, when I tried to run the simulation, TINA failed immediately, and changing the parameters did not help, so that’s out – it is really a basket case. Next up, SpiceOpus, it kinda worked but the responses were somewhat puzzling, it appeared that the circuit was either un-stable (with many warning messages) or it was not quite happy with the transient analysis parameters, with a text-only interface, it was hard to tell just what was happening, so it will require some sleuthing is needed to find out why…
Ok, this one is easy, I re-ran the sim on SpiceOpus, with longer time periods, as shown in the picture below, the cause of the in-stability was due to fact that the circuit did not reach steady-state, a common problem. Adjusting the period also gave different read outs for the THD, so the output needs to be completely stable before the Fourier analysis can be run.
Never say die! I next I tried Micro-Cap, which did better but only after I turned off the Period-Steady-State feature which was suppose to improve transient analysis convergence, instead it had exactly the opposite effect, hummm… yet another puzzle to solve.
I hate to say it, but perhaps there is a good reason for the popularity of LTSpice – it could just be its ability to converge. In any case, I still have no interest of using it, given the poor UI and awkward file management system. May be it will be upgraded soon, then I will give it a try.
While searching for other ways to generate THD/IMD vs output power plots, I came across some application notes from Spectrum Software, the maker of Micro-Cap, an expensive simulation package used by the pros. Luckily there is a student/trial version available for download so I can test its features.
With the program, it is very easy to generate the distortion vs output plots, which makes me wonder why the other programs do not offer the same… All Micro-Cap does AFAIK, is to store the various results of the distortion data in memory then plot them out afterwards – so the user is relieved from the hassle of exporting the data to another program like Excel to plot the curves.
Of course, the program has many more features besides THD/IMD, according to the company, components can be optimized to minimize the distortion, as well as many other parameters. The optimizer can also be used to build custom SPICE models based on the manufacturers’ datasheets, unfortunately, this feature is not enabled in the trial version of the program, so I can not test it out. But I can see how it could come in handy to do curve fitting and generate the tube models.
The full program is very expensive, with retail price around $5,000! Really?! For a souped-up version of Berkeley SPICE?! Come on! In any case, with some well thought-out coding, I think the distortion plots could be done just as easily with SpiceOpus or any other free or low cost SPICE programs. It comes down to how much time I want to spend on programming instead of actually using the simulator 😉 Oh well, there is no such thing as a free lunch, is there?!
While reading Dmitry Nizh’s great site, I found an article on parameter optimization using SPICE. In Dmitry’s case, he used an AWK script to pipe the output of the distortion analysis from SPICE to a plotting program, so the harmonics vs. power curves could be displayed and further analysed. To see what else can be done with SPICE, I googled “SPICE circuit optimization” and saw a bunch of papers published by Burmen and Tuma of University of Slovenia, which let me to discover their version of the Berkeley/XSPICE port for Windows – SpiceOpus, that contains a built-in optimize function. After going over some of the examples shown on the SpiceOpus website, I managed to try out the optimize function on a simple SPUD amp, the same one that Dmitry used in his SPICE optimization example. Here are few lines of code that I used for the optimization for both the preamp plate load and power amp load:
SpiceOpus (c) 45 -> define error(f) db(v(3)/230) - db(v(8)) SpiceOpus (c) 46 -> optimize parameter 0 @r6[resistance] low 10k high 50k initial 40k SpiceOpus (c) 47 -> optimize parameter 1 @r7[resistance] low 2k high 10k initial 5k SpiceOpus (c) 48 -> optimize analysis 0 ac dec 10 100 10k SpiceOpus (c) 49 -> optimize implicit 0 ac1.v(3) gt 230 SpiceOpus (c) 50 -> optimize cost mean(mag(error(frequency))^2) SpiceOpus (c) 51 -> optimize method complex SpiceOpus (c) 52 -> optimize Complex: stopped, simplex small enough. Time needed for optimisation: 0.062 seconds Number of iterations: 42 Lowest cost function value: 4.926709e-002 Optimal values: @r6[resistance] = 4.006276e+004 @r7[resistance] = 4.519619e+003
The results were very close to what Dmitry came up with, so I guess it worked. Since I am just using the ac sweep response as a proxy for distortion, I think perhaps the result could be further improved if I can figure out how to provide a more precise cost function to the optimization routine.
Since SpiceOpus has a discrete Fourier transform function, it is possible to calculate the total harmonic distortion without having to export the data to another program for processing. The THD can be calculated as follows:
Then the optimization codes can be written as follows:
SpiceOpus (c) 1 -> source 6f5p_opt.cir SpiceOpus (c) 2 -> save v(10) SpiceOpus (c) 3 -> optimize parameter 0 @r6[resistance] low 10k high 50k initial 40k SpiceOpus (c) 4 -> optimize parameter 1 @r7[resistance] low 4 high 16 initial 8 SpiceOpus (c) 5 -> optimize analysis 0 tran 1u 1m SpiceOpus (c) 6 -> optimize analysis 1 linearize 1u SpiceOpus (c) 7 -> optimize analysis 2 spec 0 5k 1k v(10) SpiceOpus (c) 8 -> optimize cost ((abs(v(10))^2+abs(v(10))^2+abs(v(10))^2+abs(v(10))^2)/abs(v(10))^2)^0.5 SpiceOpus (c) 9 -> optimize method complex SpiceOpus (c) 10 -> optimize Warning: Convergence problems at node (1). Complex: stopped, simplex small enough. Time needed for optimisation: 16.765 seconds Number of iterations: 48 Lowest cost function value: 3.290727e-001 Optimal values: @r6[resistance] = 3.998431e+004 @r7[resistance] = 6.879497e+000
R7 is the speaker load, so the equivalent plate load is 4.3k. Are the load resistances really “optimal”? Can’t tell until I build the circuit and test it on the bench. But it does look promising & fast too!
Why is something so simple, so difficult to do?!
I thought I found the perfect simulator for my needs – TINA by DesignSoft, alas, it failed in one important area – convergence! Even with a relatively simple 3-stage tube amp circuit, it would not converge, while SIMetrix and WinSPICE had no problem with it.
Ok, I thought, there is still a workaround – I will just use TINA as a schematic capture tool , which I like a lot, and export the netlist to run on WinSPICE. Well, it does work. BUT, there is always a but, the frigging netlist nodes do not even match up with the node numbers used within TINA, really!? So, if something needs adjusting, I had figure out just which part connects to what – that’s what the schematic is for!
I can not believe with all smart engineers working at DesignSoft, that something as simple as making sure the nodes match up does NOT even work, I am very disappointed, so close yet so far away…
Just tried something with TINA – export the schematic as a TINA netlist (it looks the same as the PSPICE version, anyway…) then use the Netlist Editor to compile it, guess what, the Transient analysis now runs just fine – no convergence problem, whoa?! Something is not right – must be a bug in the program, how can the netlist run differently than the source that generated it in the first place?! As as I said in the title, THIS IS JUST STUPID!
Man, time flies, three months have already gone by… This quarter was a pretty productive one – I evaluated many new design tools for tube amp design, in an effort to streamline the process, as well as improving the accuracy of the simulation. While it still has some short-comings, I have managed to narrow it down to three main tools – pctube, MathCAD
The “front-end” is based on the great pctube program by Ayumi Nakabayashi, which is responsible for building tube models, plotting characteristic charts, and some single-stage design work. TINA handles the circuit simulations, and MathCAD is used for symbolic analysis when the time comes for really digging into the math behind the simulations.
One area that still requires more work is TINA, it just does not perform as well as SIMetrix when it comes to transient analysis, so occasionally SIMetrix is still needed to tackle those “tough” simulations. Hopefully, the new release of TINA will make it converge better.
A side benefit to all this effort was the opportunity to bone up on some of the basic math and electrical engineering subjects which I have long forgotten, wish I was twenty years younger, it was so much easier then!