2
\$\begingroup\$

Help me please, I'm looking for the solution how to automate spice simulation with python on MacOS, currently, I'm thinking to use LTspice. However, I'm open to any proposal.

I want to make approximately <1000 simulations and extract data from simulation (e.g. value of the output voltage at the particular time) and after start a new simulation with another input parameters for the circuit.

I have checked a big number of pages, however, haven't find something which would give me a python + spice setup.

\$\endgroup\$
  • \$\begingroup\$ Normally you would use the .step command to run the simulation with different parameters each time. Can you use that? \$\endgroup\$ – Steve G Mar 23 '17 at 16:24
  • \$\begingroup\$ @SteveG sure, it would work, but how to start it automatically using command line, is the question. \$\endgroup\$ – Paddy Mar 23 '17 at 16:34
1
\$\begingroup\$

With spice, you'll have to roll your own code but there are ways to get data in and out of LT Spice and run it from a scripting language.

This works on other oses, having never run LT spice on a mac I don't know if the command line works on a mac.

If you run LT spice in command line mode, from a windows command line:

Run in batch mode. E.g. "scad3.exe –b deck.cir" will leave the data in file deck.raw

"scad3.exe –b deck.cir"

Or you'd probably want a .txt file which you could then import back into a scripting language

'ltsputil.exe -ca example.raw dete.txt'

Test this and see if you can run LT spice from the command line, here is some info on running exe's from a command line on a mac: Running command line exe's from a mac

You can run shell commands from python also

So generate a python script to generate raw files then use the utility to generate text files. (which you can then import back into python). You can edit the .cir file directly from python and change things (like add components or change values, its just text file and spice netlist after all).

So if you wanted to change a step command, all you would have to to is find the line of text in the .cir file change it, then rerun the simulation and look at the output.

Keep in mind that LT Spice is very powerful if you know how to use it:

  • B-sources can do some crazy math with nodes (like simulate bit quantization of ADC's and DAC's or laplace transforms

  • There are monte carlo simulations that randomize values.

  • .step commands with parameters can run multiple simulations

  • You can set resistors and other components to the value of a voltage node to create variable resistances\component values.

    • PWL files to change the voltage and current sources from a file.

If these don't work then run script a simulation.

\$\endgroup\$
  • \$\begingroup\$ Wow! Thank you for your time and answer! Today I tried LTspice with wine on MacOS, works with command line... And thank you for the hints, I definitely should consider them. \$\endgroup\$ – Paddy Mar 23 '17 at 20:12
2
\$\begingroup\$

I'm maybe a little too late for the party, but after reading the above, and having a similar problem I found that the package content of the LTspice app has an executable file found at:

/Applications/LTspice.app/Contents/MacOS/

I am not sure how to properly work with the command line help specified in laptop2d's answer, as it seems that LTspice saves as .asc on macOS, not .cir. When using the above executable, specifying the .asc file it fails. It appears to be having issues with the params and simulation commands in a given schematic - they are apparently not properly interpreted.

I circumvented this by generating the netlist manually (It seems that the macOS executable aren't capable of doing this, either). I then run the following command in terminal:

/Applications/LTspice.app/Contents/MacOS/LTspice -b myNetlist.net

I hope this helps others.

\$\endgroup\$
  • \$\begingroup\$ wow, thank you for the update! I'm going to check it, but it looks like the way to go \$\endgroup\$ – Paddy Nov 7 '17 at 12:44
  • \$\begingroup\$ one question, as I understood you right LTspice can read SPICE netlists? \$\endgroup\$ – Paddy Nov 7 '17 at 13:07
  • \$\begingroup\$ Yes, you can generate an arbitrary spice-netlist and then run LTspice on that netlist - however I am not a spice expert. If you want to mess with the spice solver, gmin settings and more, you probably have to look up a spice manual. I used LTspice to create my netlist (drew the schematic, saved the netlist). It runs a simulations with a signal (pwl) I generated using python - I can then modify the pwl-file and run LTspice from python. I have a bunch of .meas commands, that I then use python to read (from the .logfile), and put into .csv that functions as my results. \$\endgroup\$ – Hertel89 Nov 7 '17 at 14:00
  • \$\begingroup\$ thank you for the clarification, are you going to publish it somewhere like github? \$\endgroup\$ – Paddy Nov 7 '17 at 14:17
  • \$\begingroup\$ It is just a very small (and very crude) python script, nothing fancy, or even remotely useful for anything other than my application. The only thing noticeable is the command for running ltspice executable on macOS. If you're insisting, I could clean it up, and send you a zip, without much explanation. \$\endgroup\$ – Hertel89 Nov 9 '17 at 8:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.