[9/27/23] Version 4.1.0 of the PyLTSpice library removed the now deprecated LTSpice_RawRead.py, LTSpice_RawWrite.py and LTSpiceBatch.py files and respective classes so the example code below does not work unless Version 4.0.6 is installed. We will update the examples to conform to the latest version of PyLTSpice which looks to be better.
LTspice is a powerful tool for simulating LED performance. Adding the ability to use Python to execute LTspice as a process and set and retrieve data values leads to a large number of intriguing possibilities.
All of the input files for an LTspice model are text files which can be written by a Python script. The model output is a mix of text and binary files which can be decoded by existing Python open-source libraries.
Code Repos
The difficulty is figuring out how to make this work using google search results. We have found a good starting place is provided by Michael Wiebusch "acidbourbon" on Github with the https://github.com/acidbourbon/numpy_ltspice_filter set of Python scripts. We cloned this repo and, after setting up the correct library pip imports in our environment, got it to run in about 10 minutes.
The circuit modeled in the "filter_demo.py" script is below. The capacitance and inductance are set for each of the two runs using Python to write a file "param.txt" before each run. The input pulse is also set in Python by programmatically creating the sig_in.csv file. There are a number of other elements not discussed here that can be figured out by reading the code.
This is a simple example. A more interesting example would be modifying this code to look at random samples of distributions for both C and L values for thousands of cases.
Circuit for this example.
Plot of the CSV file data written by the script.
Screenshot of the matplotlib output after the script runs.
The example above writes files to control the input values and Nuno Brum's PyLTSpice package to extract the trace values needed for the plot routine.
LED example
The PyLTSpice library has methods to set parameters and run the LTspice engine directly. We have provided a Python script and LTspice schematic file in the downloads section that gives an example of sweeping an LED for different shunt resistance values and plotting the output using PyLTSpice methods.
LTspice schematic file "Voltage_Source_LED_sweep_parametric.asc".
Matplotlib output figure from "LED_parametric.py"
-----------------------------------------------------------------------------------------------
Related Luminus Help Center Articles
Electrical – How do I extract Spice IV parameters from an LED datasheet?
Electrical – Can I simulate a set of LED IV curves that have a single Vf bin?
Electrical – Can I calculate LED lumens with Spice?
Electrical - What is Current Hogging in Series Parallel Designs?
Electrical - How do I sweep an LED IV curve in Spice?
Electrical - How do I insert a diode file into LTspice
Electrical - Can I add reference lines to Spice plot panes?
Data Analysis - Using Python to run LTspice as a remote process.
------------------------------------------------------------------------------------------------
These links are being provided as a convenience and for informational purposes only; they do not constitute an endorsement or an approval by Luminus of any of the products, services or opinions of the corporation or organization or individual. Luminus bears no responsibility for the accuracy, legality or content of the external site or for that of subsequent links. Contact the external site for answers to questions regarding its content.
Comments
0 comments
Please sign in to leave a comment.