Difference between revisions of "Qucs"

From the Linux and Unix Users Group at Virginia Teck Wiki
Jump to: navigation, search
imported>Pew
imported>Pew
(Replaced content with "Category:Pending deletion")
Line 1: Line 1:
TODO: Migrate to software page
+
[[Category:Pending deletion]]
 
 
'''Qucs''', the '''Quite Universal Circuit Simulator''', is a [[Free Software]] integrated schematic capture and simulation tool for digital and analog signals. It supports DC, transient, AC and digital simulations as well as parameter sweeping. It does not currently support Monte Carlo simulations, although with some crude Bash and [[QtOctave]] work, this can be overcome. If you don't mind using extra tools for Monte Carlo simulation and are willing to fudge the exact model of op-amps used in [[Lab-in-a-Box]] projects, Qucs and be used to complete assignments in [[ECE 2074 Electric Circuit Analysis Laboratory]].
 
 
 
=Screen Shot=
 
[[Image:Qucs_0.0.15.png]]
 
 
 
=Monte Carlo Simulation=
 
The following steps show how to produce voltage and current histograms in a simple sources-and-resistors circuit.
 
 
 
The first step is to draw your circuit and make sure it simulates as expected. Use a <code>dc simulation</code>, a current probe to measure the current you're interested in and two labels and a difference equation to define the specific voltage drop you're interested in measuring. An example is presented below.
 
[[Image:Monte_Carlo-DC_Bias.png]]
 
 
 
It is necessary to define a resistor with a variable value within a given tolerance. This can be accomplished with the following circuit. You can recreate this circuit yourself or download <code>res_tol.sch</code> from [http://vtluug.org/files/qucs/ http://vtluug.org/files/qucs].
 
[[Image:Monte_Carlo-Resistor_with_tolerance.png]]
 
 
 
Next, replace all the regular resistors in your original circuit with subcircuits of the previously made varying resistor. For randomized results on each run, it is necessary to provide a seed to the random number generator. For now, define an equation the likes of <code>z=srandom(1337)</code>. The actual value is just a placeholder and will be replaced when the simulations are run from the commandline.
 
[[Image:Monte_Carlo-Var_Circuit.png]]
 
 
 
Save and run the circuit. You should be able verify that the result changes slightly given different random number seeds. You'll now need to automate running the simulation with different seeds. Open up the netlist by selecting Simulation->Show Last Netlist. Copy the contents of this file into a text editor and save it as <code>monte_carlo_netlist.txt</code>. Then, paste the following bash script into a text editor and save it as <code>monte_carlo.sh</code>. The variables of the script can be edited to suit. The value of <code>REPLACEME</code> should be the unique placeholder value used as the pseudo-random number generator seed earlier. The value of <code>NUM</code> determines how many times the simulation is run.
 
<pre>
 
#!/bin/bash
 
 
 
INFILE="monte_carlo_netlist.txt"
 
REPLACE="1337"
 
OUTFILE="monte_carlo"
 
NUM=400
 
 
 
echo -n > "${OUTFILE}.temp"
 
for i in $(seq 1 $NUM) ; do
 
sed s/$REPLACE/$(date +%N)/ $INFILE | qucsator | sed -nr \
 
-e '/<indep Vdrop/,/<\/indep/p' \
 
-e '/<indep Pr1.I/,/<\/indep/p' \
 
| sed -nr '/-?[0-9]\.[0-9]+e[-+][0-9]+/ p' \
 
>> "${OUTFILE}.temp"
 
done
 
 
 
 
 
echo -n > "${OUTFILE}.dat"
 
echo -n "values=[" > "${OUTFILE}.m"
 
odd=1
 
for r in $(cat "${OUTFILE}.temp") ; do
 
if [ $odd -eq 1 ] ; then
 
echo -n "${r} " >> "${OUTFILE}.dat"
 
echo -n "${r} " >> "${OUTFILE}.m"
 
odd=0
 
else
 
echo $r >> "${OUTFILE}.dat"
 
echo $r >> "${OUTFILE}.m"
 
odd=1
 
fi
 
done
 
sed -i '$ s/$/]/' "${OUTFILE}.m"
 
</pre>
 
 
 
Make the script executable and run it:
 
<pre>
 
chmod +x monte_carlo.sh
 
./monte_carlo
 
</pre>
 
 
 
If all goes well, it should generate a <code>monte_carlo.m</code> file. Open this up with [[QtOctave]] and plot a histogram. The Plot->2D...->Histogram menu selection is a good start, although it seems a little buggy and you many want to start with the command it gives you and tweak the Octave plot command until you have proper labels and so on.
 
 
 
If you want to stitch the voltage and current plots together into one image, you can use ImageMagick for that:
 
<pre>
 
convert voltage.png current.png +append voltsamps.png
 
</pre>
 
 
 
Here is an example final product.
 
[[Image:Monte_Carlo-Voltsamps.png]]
 
 
 
=See Also=
 
* [[QtOctave]]
 
* [[ECE 2004 Electric Circuit Analysis]]
 
* [[ECE 2074 Electric Circuit Analysis Laboratory]]
 
 
 
=Links=
 
* [http://qucs.sourceforge.net/ Qucs homepage]
 
* [http://qucs.sourceforge.net/docs/workbook.pdf Qucs Workbook]
 
 
 
[[Category:Needs restoration]]
 

Revision as of 04:17, 4 January 2018