5
\$\begingroup\$

I am new to Python, and I want to simulate LTspice circuits automatically using Python.

I added the library ltspice and scripted a fair amount in order for Python to automatically generate an LTspice netlist I want.

The problem I am facing is that I don't know what command I should use to simulate the netlist in LTspice using Python so I can plot the output voltages and currents.

Looking forward to your suggestions.

\$\endgroup\$
4
  • \$\begingroup\$ Are you talking about a Python library called ltspice? You keep mixing up the capitalization of "ltspice" and I can't tell what you are talking about. If there is some Python library you are using then add a link to its documentation. \$\endgroup\$ Apr 29, 2020 at 23:16
  • \$\begingroup\$ It sounds like the ltspice python module parses data out of a pre-existing .asc file generated by a run of LTSpice. If LTSpice has a command line mode, then you could run it using python's subprocess module to shell out, don't know whether it does. Alternatively, the PySpice module is designed to interface to the similar Ngspice and xyce simulators, which sounds more hopeful for doing all you want. \$\endgroup\$
    – Neil_UK
    Apr 30, 2020 at 4:21
  • \$\begingroup\$ @Elliot, thanks for your remark. I have just edited the post. \$\endgroup\$
    – user251019
    Apr 30, 2020 at 6:34
  • \$\begingroup\$ @Neil, yes exactly I was thinking of switching to Ngspice since this has taken me so long, and I also guess that the LTspice netlists are easily read by Ngspice. I guess I'll just switch. Thank you :) \$\endgroup\$
    – user251019
    Apr 30, 2020 at 6:37

1 Answer 1

4
\$\begingroup\$

Errrm.

What's unclear about the examples on the Python ltspice library page?

Example circuit:

enter image description here

Example code:

import ltspice
import matplotlib.pyplot as plt
import numpy as np
import os

l = ltspice.Ltspice(os.path.dirname(__file__)+'\\rc.raw') 
# Make sure that the .raw file is located in the correct path
l.parse() 

time = l.getTime()
V_source = l.getData('V(source)')
V_cap = l.getData('V(cap)')

plt.plot(time, V_source)
plt.plot(time, V_cap)
plt.show()

Example output:

enter image description here

You have named nodes (cap and source) and ask for the data using the following line:

l.getData('V(source)')

"getData" is the function. "V()" tells LTspice you want the voltage, and "source" says which node.


From the comments, it seems that the ltspice library only does part of what you need.

This project seems to cover the other half - namely, making LTspice execute a simulation from within a Python program.

\$\endgroup\$
10
  • \$\begingroup\$ I didn't even know this existed ! \$\endgroup\$
    – efox29
    Apr 30, 2020 at 8:28
  • \$\begingroup\$ @efox29: Basics of programming. Always look for documentation for the libraries you use. \$\endgroup\$
    – JRE
    Apr 30, 2020 at 10:12
  • \$\begingroup\$ I meant that python can integrate somewhat with ltspice \$\endgroup\$
    – efox29
    Apr 30, 2020 at 10:13
  • 1
    \$\begingroup\$ I mean, Python has a library for everything. It even has a library for accessing the XKCD comics. \$\endgroup\$
    – JRE
    Apr 30, 2020 at 11:48
  • 1
    \$\begingroup\$ I actually used the command : scad3.exe -b netlist.net in the cli and it worked perfectly. However, when I use the equivalent syntax in python : import subprocess subprocess.run(['scad3.exe', '-b', 'netlist.net']) I have a generated log file that says Could not open input deck for reading: netlist.log. Any ideas of where I went wrong? \$\endgroup\$
    – user251019
    May 3, 2020 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.