4

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.

JRE
  • 67,678
  • 8
  • 104
  • 179
  • 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. – Elliot Alderson Apr 29 '20 at 23:16
  • 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. – Neil_UK Apr 30 '20 at 04:21
  • @Elliot, thanks for your remark. I have just edited the post. –  Apr 30 '20 at 06:34
  • @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 :) –  Apr 30 '20 at 06:37

1 Answers1

4

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.

JRE
  • 67,678
  • 8
  • 104
  • 179
  • I didn't even know this existed ! – efox29 Apr 30 '20 at 08:28
  • @efox29: Basics of programming. Always look for documentation for the libraries you use. – JRE Apr 30 '20 at 10:12
  • I meant that python can integrate somewhat with ltspice – efox29 Apr 30 '20 at 10:13
  • @efox29: The way you were writing about it, I thought you were using the Python ltspice library. – JRE Apr 30 '20 at 11:28
  • 1
    I mean, Python has a library for [**everything.**](https://xkcd.com/353/) It even has a [library for accessing the XKCD comics.](https://pypi.org/project/xkcd/) – JRE Apr 30 '20 at 11:48
  • I didn't know this existed either! – efox29 Apr 30 '20 at 11:49
  • This example simulates a circuit. I am going about it the other way: instead of using the GUI to create my circuit, I generate a netlist that I want to run in LTspice using python (this I don't know how to do). Once the netlist run, I can use the syntax in this example that will enable me to read the .raw files and extract the output I want. Not sure if you got my question or no.. –  Apr 30 '20 at 13:04
  • Apparently not. – JRE Apr 30 '20 at 13:06
  • What part is it you don't understand? –  Apr 30 '20 at 13:11
  • 1
    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? –  May 03 '20 at 10:29