PySpice-org / PySpice

Simulate electronic circuit using Python and the Ngspice / Xyce simulators
https://pyspice.fabrice-salvaire.fr
GNU General Public License v3.0
641 stars 169 forks source link

How to retrieve .measure results #346

Open infinitymdm opened 1 year ago

infinitymdm commented 1 year ago

The main problem I'm having is that I just don't know where to find the results of .measure calls after running a simulation using PySpice. I get pretty plots that show I'm reaching what should be my measurement triggers, but I don't know where to find the data captured by the .measure call.

Environment (OS, Python version, PySpice version, simulator)

Note: I am using a slightly tweaked version of Pyspice. However, my changes are very small and shouldn't affect the ability to get results from .measure calls. You can compare differences here: https://github.com/PySpice-org/PySpice/compare/master...infinitymdm:PySpice:master?expand=1

Expected Behaviour

Results from simulator.measure() will be returned from the measure() call, or be accessible via __getitem__() in the Waveform object after running the appropriate simulation (e.g. for .meas tran my_var_name [...], results should be accessible via analysis['my_var_name']).

Actual Behaviour

I can't find the results from .measure() calls anywhere. It's possible I'm just misunderstanding how this is meant to be used, but I also can't seem to find much documentation on this feature. Poring through the source code seems to indicate to me that simulator.analyses should have an object containing the .measure results, but I can't seem to find anything there either.

Steps to reproduce the behaviour

Run make after setting up CharLib at commit f4bd88d8. See characterizer/char_comb.py for relevant code for setting up and running the spice simulation.

infinitymdm commented 1 year ago

I've spent some time digging into this today. It seems to me that the Analysis class should have a member dedicated to storing results of .meas lines in spice simulations. But since the ngspice FFI doesn't seem to surface .meas results except through ngspice_Command run calls, the measurement results simply get dropped. Since the API doesn't surface them, the object doesn't make space to store them, and I as a user don't have access to the parameters I expect in my simulation.

One solution I can think of (at least when using the ngspice-shared backend, and not running ngspice in the background) is to parse the measurements out of the results returned by NgShared.exec_command('run'). This isn't a pretty solution, but it should work for me for now.