Electronic – start a LTSpice simulation using code in MacOS


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.

Best Answer

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.