Open eps82 opened 7 years ago
Sorry to comment here but could you please tell me how can I check if the simulation done using bg_run is complete?
Thanks in advance
Hello, no problem in commenting here, especially because it's a particularly pertinent question! I'll check from a proper computer when possible (holidays!) but there's a callback that's currently -and incorrectly- commented out, set to "None" instead. If you check lines 354+:
self._shared.ngSpice_Init(self._SendChar,
self._SendStat,
self._ControlledExit,
self._SendData,
self._SendInitData,
None, #self._BGThreadRunning,
py_object(self))
I can't remember why I left that out but given self._BGThreadRunning is (apparently properly) defined you should be able to call it and at the very least poll it¹. Note this is just a direct and not very pretty wrapper of ngspice's function so you can check its documentation (in their manual there's a section about their API). I should probably define a simpler .is_finished() method or something like that on top of it I guess...
Also, there's probably a good reason for the background mode, but to be honest I just run it as a blocking call with a normal run(), and then if/when parallelism is needed (a GUI or something) launch it from my own python thread with my own callback for when it's finished. If you're trying to run several simulations in parallel beware ngspice itself doesn't support it very well, although it's possible. Once again you'll have to check their manual. This also means I haven't played a lot with the background mode if at all, so I should look for possible mistakes.
I'm adding it to my TODO and will probably have a look at it in the next few days (besides re-reading this in case I didn't make any sense) even if just to remove some # pdb.set_trace() I forgot...
¹ See corrections below
Hi eps82,
Thank you so much for your quick reply. I am actually very new to programming so I am not sure how does this callback functions work.
_BGThreadRunning(is_running, lib_id, self)
Do I call it like this:
ng.BGThreadRunning(True,1,ng)
I am actually trying to generate netlists automatically using code and then test them for the desired output using lyngspice. I want to use bg_run() so that I am able to halt the simulation if it is taking too long to finish and I guess that is not possible with the normal run().
I am also having another issue that when I run some netlist that have a singularity problem, the ngspice would collapse because of that. When ngspice collapses lyngspice returns an OSError: access violation reading and the program would break. Is it possible for lyngspice to attach() ngspice again without breaking the python program?
Once again I thank you for your help and apologize for bugging you during your holidays. Feel free to reply when you are back from your vacations and I wish you a very happy new year. :)
I just figured out that it is possible to check the status of background thread using the following command:
ng._shared.ngSpice_running()
It would return 1 if the function is still running.
On top of that, from what I see it's even easier than I remembered (I did mess up my explanation a bit; I get forgetful with stuff I write and then not use). Assuming you changed the "None, #" part in lyngspice.py, you should be able to do something like this:
def cooking(is_running, lib_id):
print("Ding!", is_running)
return 0
ng.set_thread_callback(cooking)
ng.bg_run(netlist)
The function "cooking" would be called once by ngspice when the simulation starts, passing is_running=False, and then when it finishes it will call it again with is_running=True. For a timeout you can either ask ngspice directly if it's done or not or simply check a flag set by your "cooking" function.
And thanks, happy new year in advance to you too :)
Got it. Thanks for your help :)
I had entirely forgotten about the BG mode; I've updated the source and tested it on ngspice 28. Still no luck with FreeBSD support.