Qucs / qucs

Qucs Project official mirror
http://qucs.sourceforge.net/
GNU General Public License v2.0
1.16k stars 213 forks source link

Spice4qucs. A tool to simulate Qucs circuit with ngspice #77

Closed ra3xdh closed 7 years ago

ra3xdh commented 10 years ago

There may be way to overcome #34 and #41. I started implementation of spice4qucs extension. This extension allows you to switch between qucsator simulation kernel and ngspice simulation kernel. You can simulate your Qucs circuits with ngspice using this extension. I am planning to add menu entry Simulation->Simulate with spice. Spice simulation will be available after execution of this menu. Spice simulation results will be converted to Qucs simulation data and diagrams will be available from the display page. With spice4qucs you can use good old spice to simulate Qucs circuits with it.

You can see current state of work at https://github.com/ra3xdh/qucs/tree/spice4qucs

Now implemented only conversion of the Qucs circuit to Spice netlist.

Not all components conversion is implemented. The list of spice-compatible components:

The list of supported simulations:

Sample schematic is available here: https://gist.github.com/ra3xdh/6c554e32e531c25e6e6b

To test spice4qucs open this schematic with Qucs, then execute Simulation->Simulate with spice. Spice netlist is located at $HOME/.qucs/spice4qucs.cir. Now switch to system terminal and execute ngspice:

ngspice $HOME/.qucs/spice4qucs.cir

Then run simulation and see waveforms:

run
plot v(drain)

The simulation fails for this schematic with Qucs, but ngspice simulates it correctly.

Have you any suggestions? Should I continue work in this direction?

My current task list:

nicolati commented 9 years ago

Hi Vadim, your work is great! :)

Fra

ra3xdh commented 9 years ago

Source code is now located on spice4qucs branch of main Qucs github repository: https://github.com/Qucs/qucs/tree/spice4qucs . You can propose your pull requests for this branch. Build instructions are on wikipage: https://github.com/Qucs/qucs/wiki/QEP%3A-Qucs-schematic-simulation-with-ngspice

ra3xdh commented 9 years ago

Added spice file components support. Now circuits with spice file components could be simulated. Here is simulation example. LM358 spice model from @in3otd was used. Spice code is here http://pastebin.com/1NJpGFYM . See last commits on spice4qucs branch.

spicefile

ra3xdh commented 9 years ago

Added three special components. that implement .PARAM .GLOABL_PARAM and .OPTIONS sections of spice netlist. These components could be used when it's need to pass custom parameter to spice simulation kernel directly. sp_par_opt

ra3xdh commented 9 years ago

The latest updates:

  1. Improved parameter sweep support for Ngspice
  2. Improved equations support
  3. @MikeBrinson added a large number of spice-specific components. You can see all of them on attached sreenshot.
  4. Added Ngspice custom simulation. Enabled multiline properties for components to store simulation script and implemented a special dialog to edit spice code and to add spice variables. spec_comps custom_dlg-1 bjt_custom
MikeBrinson commented 9 years ago

Added SPICE basic transformer models. These are subcircuits and can be found in Qucs project SPICE_transformer_models_prj in the Qucs examples directory. Two test examples are also included. The Qucs inductance symbol for use in SPICE simulations has been extended to include a DOT at one terminal of the symbol to indicate inductance winding polarity. The transformer symbols are:

basictransformermodels

The following test examples give an indication of the transformer model use: trancoupling fullwave

Non-linear transformer models to follow soon.

MikeBrinson commented 9 years ago

For Qucs users information the following map of a range of SPICE voltage and current generator test circuits give an idea of the capabilities of the new signal generators.

spice_examples

ra3xdh commented 9 years ago

This information will be useful!

MikeBrinson commented 9 years ago

Added ngspice and Xyce MESFET component. The MESFET model has been added to allow all three analogue simulators access to the different MESFET models distributed with each simlator. Ngspice level 1 model appears to be common to all three simulators. The following test example shows a set of DC device characteristics obtained from a ngspice simulation of the ngspice level 1 model. Note that the combination of Qucs GUI and the ngspice simulation engine allows one or two component or device parameters to changed while simulating.

mesfet1

This example also illustrates how multi-line component specifications can be attached to a Qucs schematic symbol, for example MESFET Z1.

MikeBrinson commented 9 years ago

Qucs-ngspice simulation now works with imported XSPICE code models. The following png screen dump shows how XSPICE analog.cm SPICE 2g6 non-linear POLY() statements can be added to Qucs behavioural G sources located in subcircuits. Note the ngnutmeg codemodel statement is used to load the analog.cm model library. qucs_ngspice_xspice_2g6poly_models

MikeBrinson commented 9 years ago

Added XSPICE Cmeter analogue component. It can be found in the "probes" group. Testing indicates that this component only works with transient analysis and circuits with standard C components connected to individual nodes. The following RCLadder network gives an indication of how the probe works.

rcladder

MikeBrinson commented 9 years ago

Added a lossless transmission line: ngspice and Xyce type T component. The example shown below indicates that the model functions as an ideal lossless transmission line. Please note its parameters are different to the Qucs 2 port, 4 terminal transmission line

losslesstline

ra3xdh commented 9 years ago

Added parameter sweep for 2 variables in nested loops. This works only with Ngspice.

MikeBrinson commented 9 years ago

Added the SPICE 3f5 style U type uniform distributed RC transmission line. This model appears to be only available with ngspice. udrc

MikeBrinson commented 9 years ago

Added lossy transmission line. Model type LTRA. This line model is implemented in both ngspice and Xyce. ltra

ra3xdh commented 9 years ago

Added Fourier (Ngspice and Xyce) and Distortion (Ngspice)analysis disto four

MikeBrinson commented 9 years ago

Added a SPICE diode symbol. The original Qucs diode model (the legacy model ) is retained and continues to work with Qucs, ngspice and Xyce. However, it does not have the SPICE LEVEL selection mechanism implemented. The additional SPICE diode model allows full selection of Xyce models: for example LEVEL = 1 selects the SPICE 3f5 model and LEVEL=2 selects the PSpice diode spicediode model. Xyce reverts to the SPICE 3f5 diode model if LEVEL is not defined. Note, the ngspice diode model does not include the LEVEL parameter. Inclusion of the SPICE diode model does provide however, access to all the implemented ngspice diode parameters.

The attached diode test circuit shows a basic example of a diode clamp circuit.

MikeBrinson commented 9 years ago

Added npn and pnp BJT three terminal models to spice4qucs. The legacy BJT models are not affected and should work with Qucs, ngspice and Xyce. The new models allow different SPICE BJT models to be selected via the SPICE LEVEL parameter, for example:

  1. ngspice: LEVEL=1 is the original SPICE BJT model, LEVEL=2 is a modified version of the original SPICE BJT model, and LEVEL=4 is an advanced VBIC BJT model.
  2. Xyce: LEVEL=1 is the standard SPICE 3f5 BJT model, LEVEL=10 is a version of the VBIC BJT model, and sawtooth LEVEL=22 is an experimental release of the FBH HBT_X JT model (Vesion 2.1).

The attached sawtooth generator circuit and simulation results indicates how the new SPICE BJT models are used.

MikeBrinson commented 9 years ago

Added njf and pjf SPICE models. Again the Qucs legacy models are not affected and should work. The ngspice and Xyce simulators allow the following models to be selected using the LEVEL parameter:

  1. ngspice: LEVEL=1 is a Schichman and Hodges style model (default) and LEVEL=2 is a modified Parker Skellern model.
  2. Xyce: LEVEL=1 is the SPICE 3f5 model (default) and LEVEL=2 is a Shockley model with additional physical effect parameters.

The attached RF single stage amplifier and simulation results demonstrate the use of the SPICE njf model. njfamp

ra3xdh commented 9 years ago

Added .NOISE analysis. It creates two variables onoise_total and inoise_total . noise

MikeBrinson commented 9 years ago

Added four terminal nmos and pmos SPICE models. The following LEVELs are defined:

  1. ngspice: LEVEL 1 = MOS1, LEVEL 2 = MOS2, LEVEL 3 = MOS3, LEVEL 4 = BSIM1, LEVEL 5 = BSIM2, LEVEL 6 = MOS6, LEVEL 9 = MOS9, LEVELs 8 and 49 = BSIM3v0, BSIM3v1, BSIM3v32, BSIM3, LEVELs 10 and 58 = B4SOI, LEVELs 14,54 = BSIM4v4,
    BSIM4v5, BSIM4v6, BSIM4, LEVEL 44 = EKV (v2.6), LEVEL 45 = PSP, LEVEL 55 = B3SOIFD, LEVEL 56 = B3SOIDD, LEVEL 57 = B3SOIPD, LEVEL 60 = STAG, LEVEL 61 = HiSIM2 and LEVEL 62 = HiSIM_HV,
  2. Xyce: LEVEL 1 = MOS1, LEVEL 2 = MOS2, LEVEL 3 = MOS3, LEVEL 6 = MOS6, LEVEL 9 = BSIM3v3.2.2, LEVEL10 = BSIMOIv3.2. LEVEL 14 = BSIM4v4.6.1. MORE ?

The following simple CMOS AND test circuit demonstrates the use of the nmos and pmos SPICE models (LEVEL 1 default). cmosnand

MikeBrinson commented 9 years ago

Style and colour of spice4qucs device symbols unified in preparation for next Qucs release, see following diagram:

spicecomponents

MikeBrinson commented 9 years ago

Started to add XPICE analogue blocks to spice4qucs, see following examples: single_ended_gain_block_test double_ended_gain_block_test

ra3xdh commented 9 years ago

I have just added PlotVs() function emulation for spice4qucs subsystem. PlotVs() support is critical for research tasks. PlotVs() processing was raised to GUI level instead of translation to postprocessor directives. It allows to unify X-axis variable definition procedure for all used simulators. Also it will allow to use Xyce without limitation, because Xyce has no postprocessor and PlorVs() invocation could not be translated to postprocessor directives for it. And this solution gives more intuitive interface.

Now special graph variable syntax is used to emulate PlotVs(). X-axis variable should be trailed by @ symbol and X-variable name as following:

Y-axis-variable@X-axis-variable

Old graph variables syntax and Qucs PlotVs() function are fully compatible with this extension and there is nothing broken.

User can select desired X-variable from drop-down list or leave default X-variable (if no specified).

Attached screenshot explains this concept. You can pull latest commits from spice4qucs_current branch to test this new feature. The code in the spice4qucs_current branch is not intended for inclusion in Qucs-0.0.19S release. plot_vs

MikeBrinson commented 9 years ago

Added XSPICE integrator (voltage single ended) model to spice4qucs. The following diagram indicates its use to generate a triangular voltage se_int_test png waveform from a square wave input signal.

MikeBrinson commented 9 years ago

Added a double ended (differential) XSPICE integrator to spice4qucs. The following test example shows how it works. This test example also illustrates the use of ngspice .model statements attached to a schematic rather than individual components/ de_v_int_test devices.

MikeBrinson commented 9 years ago

Added XSPICE single and double ended voltage d/dt models to spice4qucs. The following examples illustrates their use.

xspice_diff_test

ra3xdh commented 8 years ago

Pole-Zero analysis (.PZ) was added. You can plot pole/zero location on complex plane with it. .PZ works olny with Ngspice. Source code is available at spice4qucs_current branch.

How to use PZ-analysis:

  1. Place component "Pole/Zero simulation"
  2. Setup input and output nodes
  3. Run simulation and plot "pole" and "zero" variables on complex plane ("Locus curve" diagram)

Attached example illustrates poles/zeros location for Cauer passive filter. pz-ana

ra3xdh commented 8 years ago

I have rebased both rebased_spice4qucs and spice4qucs_current branches today to backport several critical bugfixes from master. Please update your local branches.

MikeBrinson commented 8 years ago

Added two and three input XSPICE voltage multipliers. The attached test examples show how they are used with Qucs GUI and Ngspice: axb axbxc

MikeBrinson commented 8 years ago

Added two and three input XSPICE voltage summers. The attached test examples show how they are used with Qucs GUI and Ngspice: aplusb aplusbplusc

MikeBrinson commented 8 years ago

Added the XSPICE voltage divide block: Vout = A/B. See XSPICE manual for specification, or Ngspice manual V26, p140. The example shown below indicates how to specify divide model parameters. div

MikeBrinson commented 8 years ago

Added XSPICE lcouple and core blocks. These blocks allow models of magnetic devices ranging from simple transformers to devices with multiple primary and secondary windings to be constructed. To aid Qucs users understand the principles of magnetic component modelling using lcouple and core an introduction to this important subject will be added to spice4qucs-help. A small library of magnetic components will also be added to qucs-0.0.19S. The following diagrams give an indication of lcouple and core model structures and their application in the simulation of a full wave rectifier circuit where the transformer has core saturation features included. xtranfig1 xtranfig2

ra3xdh commented 8 years ago

@timofonic , Thespice4qucs patchset will be not merged in 0.0.19 release. There are multiple reasons for such solution. There is a group of users that don't need SPICE. We came to the following compromiss solution. Two sets of Qucs packages will be released in parallels:

Merging will be considered during 0.0.20 development cycle. It's need to implement at least a "Default Simulator" setting. It will allow to turn-off Qucsator if user don't need it. And to turn-off SPICE if user don't need it. In other words it will allow to switch between Qucs and Qucs-S during runtime or compile-rime.

MikeBrinson commented 8 years ago

Added a transformer library to Qucs-0.0.19 system libraries . This adds non-linear effects in transformers, including resistive losses, fringing inductance and core saturation. The attached diagram shows the new model symbols. A more detailed introduction to these models, with examples of their use, will be posted in spice4qucs-help in the near future. libfig1

MikeBrinson commented 8 years ago

Added the XSPICE S domain transfer function block to spice4qucs. This component allows filters and control system components specified by S parameter transfer functions to be simulated in the small signal AC and transient domains. The following example gives an indication of how to specify the S domain transfer function blocks. More details can be found in the Ngspice and XSPICE user manuals.

s_filter

MikeBrinson commented 8 years ago

Improved Transformer library drop down parameter descriptions, see diagram below:

transformerparamdescriptions

MikeBrinson commented 8 years ago

Added a library of magnetic cores, with different core materials, to spice4qucs. These allow users to construct a range of non-linear multi-winding transformers. The following Figures show the core synbols, B/H curves and a basic test circuit which illustrates the influence that different core materials have on circuit distortion.

cores_library coresfig2 coresfig3

MikeBrinson commented 8 years ago

Added single and double ended XSPICE A type limiters. The following test examples illustrate their operation:

test_se_v_limit test_de_v_limit

ra3xdh commented 8 years ago

@timofonic The merging of spice4qucs patchset is under discussion. This discussion will be continued after Qucs-0.0.19 will be released. We don't planning merging before 0.0.19. You can use also my unofficial packages ("S"-series) for two platforms to test it: https://github.com/ra3xdh/qucs/releases/tag/0.0.19S-rc4 The latest is RC4.

The next development tasks are:

  1. Implement switchable GUI for different simulators (show/hide simulator-incompatible elements)
  2. Add direct support of SPICE libraries (*.lib)
  3. Allow user to construct their own XSPICE devices.
  4. Add more components
MikeBrinson commented 8 years ago

Added a first test example illustrating how users can construct their own XSPICE CodeModels. The example device is called "diodecap". It emulates a SPICE 2g6/3f5 non-linear diode, modelling Id/Vd, Depletion and diffusion capacitance, reverse breakdown and AREA and Temp effects.

The test model (diodecap.mod and diodecap.ifs) can be found at:

./examples/ngspice/XSPICE_CM/diodecap_prj in the spice4qucs_current branch.

The following test screen dumps give an indication of the test model performance. accuracy_diode_breakdown xspice_cm_diode_ac xspice_cm_diode_tran xspice_cm_diode_temp

hovercraft-github commented 8 years ago

Thank you very much for your effort to put spice into Qucs! It was somehow useful even without it, but something important was certainly missed. I compiled and tested qucs-0.0.19S-rc2, then qucs-0.0.19S-rc3, and qucs-0.0.19S-rc4, all those under Linux, with gcc 4.7.2 as this is latest gcc for my Linux distribution (AltLinux P7).

To be able to compile successfully, I had every time do this small change in the code: remove " = default" attribute from the ./qucs-core/src/nodelist.h:47 nodelist_t(nodelist_t &c) constructor as it results in "declared to take non-const reference cannot be defaulted in the class body" error.

After this change I been able to compile & install Qucs, and my models (ngspice as well as Qucsator) mostly work as expected, but some inconsistencies exists nevertheless. Can it be the result of my code patch, and if so, what is the right way to make this code my-compiler-friendly ? I'm not yet ready to switch my Linux distribution to something else (Ubuntu?) just due to this small problem. Sorry if this is not a right place to report this kind of issues. Please point me

ra3xdh commented 8 years ago

@hovercraft-github , Files at qucs-core subdirectory are related to Qucs simulation engine Qucsator . Spice4qucs doesn't change anything in qucsator subsystem. Qucsator is kept untouched in Qucs-S packages. I think that your bug also should be represented in master version of Qucs. It seems it is AltLinux specific bug. I haven't AltLinux now to reproduce it. I am using gcc-4.8.2@Slackware for development and qucsator always compiles successfully. The same for Debian-8. It's need to test it with older GCC.

hovercraft-github commented 8 years ago

Your answer obviated my doubts. Probably I'm just using in my model components which not completely compatible with Qucs spice subsystem. Qucsator AC simulation with the following model (red curve) gives relevant results, whereas ngspice gives very weak (about 1e-6) response (blue curve). Very strange, but the same model in the ngspice transient simulation gives more or less correct output waveform. lpf

ra3xdh commented 8 years ago

SPICE may not process switches correctly. Relay contacts may be treated as open. Try to replace relay contacts by short circuit and remove V2 source section.

hovercraft-github commented 8 years ago

Ok. Anyway, with your help Qucs becomes interesting and long awaited tool. Thank you.

r2com commented 8 years ago

Hi downloaded the spice4qucs release of qucs from here: here:https://github.com/ra3xdh/qucs/releases/tag/0.0.19S-rc3

I also downloaded Ngspice, and followed basically instructions described here: https://qucs-help.readthedocs.org/en/spice4qucs/BasSim.html

however, all I see in the Simulation console is "Ngspice started..." message, and simulation does not run properly, here is my printout from the Ngspice window which pops up right after i press "Simulate" button:

Initial Transient Solution
--------------------------
 Node                                   Voltage
----                                   -------
_net0                                        0
_net1                                        0
_net2                                        0
l1#branch                                    0
v1#branch                                    0

No. of Data Rows : 3996
ASCII raw file
Doing analysis at TEMP = 27.000000 and TNOM = 27.000000

No. of Data Rows : 1000
ASCII raw file
exit: no such command available in ngspice
ngspice 1 ->`

Any ideas why would it not work? it also assigned 0 voltages to all nodes...

For reference, I run it all on Win7/10
ra3xdh commented 8 years ago

@r2com , exit command is defined in spinit file. Standard location is C:\spice\share\ngspice\scripts\spinit for Windows. If Ngspice was relocated, it fails to find spinit file and shows this warning message. You can simple ignore it and press Exit in Ngspice window. Simulator output will be processed and plots will be shown in Qucs GUI. Alternatively you can install Ngspice into C:\Spice and this error will gone.

r2com commented 8 years ago

okay, after moving folder to default location, Ngspice did start and exited with exit command, but no simulation seems to be running, the console window shows only "Ngspice started..."

and no output plots either, any idea?

ra3xdh commented 8 years ago

It's not a bug. You are doing something wrong.

Default Windows version of Ngspice cannot output text into terminal and cannot communicate with other processes. Simulator log goes only into Windows GUI for Win ngspice . And Qucs GUI will show only "Ngspice started" message and 100% progress when it will be finished. Ngspice window will be shown only at simulation time and will be closed automatically on Ngspice exit. But you may not see it, if simulation runs very fast. Such Ngspice for Windows behavior may be misleading, but it's normal. You can consider it as featurebug of Win Ngspice. I will consider shipping custom Ngspice Windows build with Qucs-S that will support terminal output and disable Windows GUI.

After simulation is finished, you need to press Exit button in dialog, drag new diagram, select Ngspice in the diagram dialog, and select curve. Plot will appear. Plots will not appear automatically. After new plot is created it will be automatically replotted at every new simulation.