Author Topic: Ngspice Incremental Transient Analysis  (Read 5406 times)

0 Members and 1 Guest are viewing this topic.

Offline help_me_pick_usernameTopic starter

  • Contributor
  • Posts: 32
  • Country: au
Ngspice Incremental Transient Analysis
« on: November 28, 2021, 07:15:41 am »
Does Ngspice support transient analysis continually, while outputting current/voltage values at the components and nodes of a circuit?

I have looked through some documentation, and I can't seem to find this feature. The transient analysis command has the following structure:

Code: [Select]
tran tstep tstop <tstart <tmax >> <uic >
The tstop variable states how long the simulation should be run for. I'm wondering whether there is a way for me to omit this variable in order to run the analysis continually.

For example, consider the following scenario: An initial command is sent to begin the analysis, another command is sent such that Ngspice calculates the states of the nodes/components for the next time step of the analysis. At this stage, the states of any required nodes/components can be queried (e.g. "print i(V1)"). The previous command can be sent again to advance the simulation such that Ngspice calculates the values for the next time step.

Alternatively: A command is sent to begin the transient analysis, then Ngspice continually outputs the states of nodes/components until instructed to stop (essentially running the simulation for an unlimited length of time)

Does Ngspice support either of the scenarios above?


P.S. I'm quite new to SPICE, so apologies if these are basic questions.
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6175
  • Country: ro
Re: Ngspice Incremental Transient Analysis
« Reply #1 on: November 28, 2021, 08:27:37 am »
There are 3 main types of simulations in SPICE:  DC, Transient, and AC. 
https://techweb.rohm.com/knowledge/simulation/s-simulation/01-s-simulation/7916

Depending of the SPICE flavor, some of these can be mixed by whatever GUI is there on top of the SPICE engine.

There is also Transient with initial conditions (for example when a capacitor is already charged with some voltage, or a coil is already charged with some initial current, and .tran can take that initial charge into account or disregard the initial conditions before starting the .transient simulation).

Offline help_me_pick_usernameTopic starter

  • Contributor
  • Posts: 32
  • Country: au
Re: Ngspice Incremental Transient Analysis
« Reply #2 on: November 28, 2021, 08:43:49 am »
I am aware of these modes (and the initial state setting for transient). I was specifically asking about the transient analysis, and whether Ngspice could run transient analysis continuously, without having to set a fixed simulation duration.
« Last Edit: November 28, 2021, 08:46:00 am by help_me_pick_username »
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6175
  • Country: ro
Re: Ngspice Incremental Transient Analysis
« Reply #3 on: November 28, 2021, 11:34:19 am »
AFAIK no SPICE based simulators run "live", forever, like a real circuit, but I never used ngspice.

It is only possible to set in advance a specified duration of analysis, then the simulation engine will start crunching numbers, step by step, while storing on the disk the results for that specified duration only.  Only the maximum step can be specified in the .tran line, but the engine can go with much finer time-step resolution when needed.

Sometimes the simulator can run faster than the physical real circuit, some other times (especially when fast transitions are present in the signal, or when for some other reasons the result of a step tends to diverge) it can take many minutes to simulate a single second for the given circuit.

Offline help_me_pick_usernameTopic starter

  • Contributor
  • Posts: 32
  • Country: au
Re: Ngspice Incremental Transient Analysis
« Reply #4 on: November 28, 2021, 12:26:14 pm »
Hmm... That's a real shame if that's the case. Based on my understanding, it would seem pretty trivial, technically speaking, for the simulation to run continuously, while outputting a continuous stream of data.
 

Online Ian.M

  • Super Contributor
  • ***
  • Posts: 12849
Re: Ngspice Incremental Transient Analysis
« Reply #5 on: November 28, 2021, 01:07:04 pm »
Berkeley SPICE had its origins in the batch processing computing environment of the 1970s, and was originally written in FORTRAN, the then premier scientific programming language.   Eventually it was ported to C just as the C language became standardized, as SPICE 3 and released under the BSD license.   Just about every SPICE simulator on the market can trace its roots back to Berkeley SPICE 3 and tries to maintain some degree of compatibility with it which includes maintaining the batch processing paradigm, namely: prepare a netlist file, submit it to the SPICE 'engine' to run a specific analysis, then take the results file and do *whatever* to it to display it to the user.

Obviously this batch processing paradigm has problems for continuous simulation - its basically 'fire and forget' so once launched there is no general way to change parameters of the 'live' simulation.   For simulators that can plot results as they build up in the results file, and can also be stopped without abandoning the results display you can get some way towards your desired workflow by specifying an unfeasibly large stop time. e.g. 9E9, which is some 285 years away.   That might as well be infinity as the oldest continuously operating circuit in the world has only been passing current for some 180 years! The problem is however the underlying OS and machine your SPICE is running on.  It doesn't have infinite disk space so eventually the simulation will crash when it runs out of space and fails to extend the results file.

However the Ngspice 'engine' can also be invoked as a DLL or shared library, and your code can register callback functions to set the circuit initial conditions and to receive the results of every simulation step, and the circuit can include externally controlled sources, so it *IF* you are prepared to code your own UI, its possible to do co-simulation: e.g. your code emulating firmware running in a MCU interacts with an external circuit specified as a Ngspice netlist,  and given a fast enough PC to interact in real time with the user: e.g. simulate LEDs, switches, and other electronic controls and displays 'live' on screen.

The devil's in the details so you are going to need to spend a lot of time using Ngspice in the ordinary way till you 'grok' its capabilities and resource constraints, then spend many many hours studying its manual, and writing test code to interface with it.

 
The following users thanked this post: help_me_pick_username

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6175
  • Country: ro
Re: Ngspice Incremental Transient Analysis
« Reply #6 on: November 28, 2021, 01:47:16 pm »
Hmm... That's a real shame if that's the case. Based on my understanding, it would seem pretty trivial, technically speaking, for the simulation to run continuously, while outputting a continuous stream of data.

For sure you can redirect (pipe) the output files to another program, so to emulate a live streaming, just that back then it was not designed to work that way (SPICE is ancient).  SPICE is not like a DSP that can process a live datastream.

You can not just plug a soundstream into a SPICE simulation and expect to hear how the music will be after passing through your simulated circuit.  However, you can record an audio, feed a SPICE voltage source with the recorded signal, then convert back the output of SPICE to an audio format, but I'm not aware of any SPICE distro doing that by default, you have to do it manually.

Some SPICE have some extra features, for example, LTspice can start plotting your charts during the analysis, and it keeps re-scaling the plots on the way, and so on.

However, once a simulation starts, the schematic is frozen, you can not change anything.  The only features that can ad changes is related with parameters, but you can only predefine them, you can not change a parameter live, during simulation.

Another similar feature you may want to test is in QucsStudio (not SPICE based, but also a circuits simulator), has sliders and can run launch a new run in the background for each event coming from the sliders, which makes it appear "live" but it's not, it's just that the hardware nowadays is really fast so it can be abused that way by software, like this:



https://www.eevblog.com/forum/eda/qucs-qucs-s-and-qucsstudio-simulators-are-not-the-same-thing/


Unless you have a homework assignment to use SPICE in text mode, my advice is to use something else that is more productive, and comes with a nice GUI, like LTspice or QucsStudio.  I absolutly love the interactive sliders from QucsStudio, they can help you build an intuition very fast about how your circuit behaves when you change different parameters.  I use both LTspice and QucsStudio.

Both work on Windows or Linux (in Linux first you must install WINE, then it will work just like it would be on Windows), probably on MAC, too, but I never used the MAC version.
« Last Edit: November 28, 2021, 01:52:43 pm by RoGeorge »
 
The following users thanked this post: help_me_pick_username

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: Ngspice Incremental Transient Analysis
« Reply #7 on: November 28, 2021, 01:57:04 pm »
The source code for Ngspice is freely available, so in theory, it would be possible to create some sort of real-time simulator with custom source and sink devices, that read data from a socket / file / serial port, and output data into a file / socket / serial port at discrete time intervals. I do not really know how big a task that would be.

There seems to be Python bindings to some simulator engines:

1. PySpice "support Ngspice and Xyce circuit simulators": https://pyspice.fabrice-salvaire.fr/releases/v1.5/ and https://pypi.org/project/PySpice/
There seems to be some PySpice videos available on Youtube.

2. "LTSPICE & NUMPY – PART 1: SEAMLESS INTEGRATION OF LTSPICE IN PYTHON SIGNAL PROCESSING":
https://acidbourbon.wordpress.com/2019/11/26/seamless-integration-of-ltspice-in-python-numpy-signal-processing/

Unfortunately I do not know how well these suit your needs and how well they support real-time processing and "indefinitely" long simulation times.
 
The following users thanked this post: help_me_pick_username

Offline Kalvin

  • Super Contributor
  • ***
  • Posts: 2145
  • Country: fi
  • Embedded SW/HW.
Re: Ngspice Incremental Transient Analysis
« Reply #8 on: November 28, 2021, 02:23:12 pm »
There is also PyAMS:

http://www.ijeetc.com/uploadfile/2021/0611/20210611102139169.pdf

https://doc.pyams.org

Looks like the SPICE-compatible component models from component manufacturers (transistors, op amps etc.) need to translated to PyAMS before they can be used in simulation.
 

Offline evac

  • Contributor
  • Posts: 26
  • Country: pt
Re: Ngspice Incremental Transient Analysis
« Reply #9 on: December 07, 2021, 11:06:39 pm »
No commercial or open-source SPICE simulator that I know of has this feature.

What's typically done when you have a simulation that may take an unknown amount of time until you get the results you are looking for, is to set tstop to a large amount of time, and then use a Verilog-A module to check for some event and use $finish() to stop the simulation.
Of course you an also monitor the results live and just stop the simulation when you want.

I also don't see many practical aplications for your scenario, as I would guess most useful circuits people would be designing will eventually either reach steady state or a periodic steady state, so more simulation time after that won't provide any different results (transient noise analysis could be an exception).
 

Offline PlainName

  • Super Contributor
  • ***
  • Posts: 6818
  • Country: va
Re: Ngspice Incremental Transient Analysis
« Reply #10 on: December 08, 2021, 04:30:56 pm »
Quote
No commercial or open-source SPICE simulator that I know of has this feature.

I think Proteus comes very close:

https://www.labcenter.com/simulation/

Quote
Proteus VSM includes a number of virtual instruments including an Oscilloscope, Logic Analyser, Function Generator, Pattern Generator, Counter Timer and Virtual Terminal as well as simple voltmeters and ammeters. In addition, we provide dedicated Master/Slave/Monitor mode protocol analysers for SPI and I2C - simply wire them onto the serial lines and monitor or interact with the data live during simulation. A truly invaluable (and inexpensive!) way to get your communication software right prior to hardware prototyping.

 

Offline hvogt

  • Newbie
  • Posts: 9
  • Country: de
Re: Ngspice Incremental Transient Analysis
« Reply #11 on: January 31, 2022, 10:42:55 pm »
You may use ngspice as a shared library.

The caller has fulll access to the simulator and to many of its internal data.

Simulation may be started in a second thread. ngspice activates a callback function after every successful time step. The caller (the user) then may decide what to do (stop, continue, alter some instance or model parameter, write out the current nodes voltages (for another restart at this point e.tc).

Saving simulation results in ngspice memory may be suppressed. So a very large TSTOP might be possible without overflowing the computer.

For a detailed description please have a look at the ngspice manual, chapter 19.

If you have any questions concerning ngspice, you may use its discussion forum at https://sourceforge.net/p/ngspice/discussion/


 

Offline hvogt

  • Newbie
  • Posts: 9
  • Country: de
Re: Ngspice Incremental Transient Analysis
« Reply #12 on: February 01, 2022, 11:46:11 am »
One thing to add, however:

There is no universal caller to call shared ngspice. So you have to program one yourself. Some hints and examples are given here: http://ngspice.sourceforge.net/shared.html .

You might check PySpice (https://pypi.org/project/PySpice/) does allow to do what you want to do. It uses shared ngspice.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf