LTSpice from command line
How to run LTSpice in batch mode and automate simulations and post-processing
LTSpice installation
First of all you have to ensure that LTSpice is properly installed and configured. To do this, go in the installation folder and check if scad3.exe, XVII64.exe or LTspice.exe are present and, if not, install it from here. Now, if you are working on a Windows machine, you have to add this location to the "Path" environment variable; in this way, your LTspice executable file will be callable from anywhere.
Python installation
For this article I used Python3 since it is very simple and straightforward for string manipulation. Install it from here.
After Python3 installation you also need to get pip and matplotlib. To install the first one you can open a terminal and type
0
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Wait for the installation to complete. Now type
0
python -m pip help
You should see an installation path. You have to add it manually to the 'Path' system environment variable. After you do this, close the terminal and open it again, then type
0
pip install matplotlib
Now you should be ready to go, if no error showed up during the process.
LTSpice batch mode
LTSpice simulations can be run in batch mode very easily; to start a simulation of a circuit you previously designed, just open a terminal in the folder where MyCircuit.asc is located and type on the command line
0
XVII64 -b -Run MyCircuit.asc
In your working directory you should see some new files: .raw, .log, .net, .op.raw. They are exactly the same files generated when you press the Run button in the GUI.
Here you can find the description of all batch mode parameters you can pass to LTSpice executable through command line:
- -b Force LTSpice to execute in batch mode; no GUI will open if you pass this parameter. If you omit this parameter, LTSpice will show up
- -big If you didn't added -b as parameter, this option will maximize the window.
- -Run This parameter says that you launched LTSpice to run a simulation. If no error is encountered, all the typical LTSpice files are generated.
- -FastAccess This parameter needs a .raw file as final argument: it converts a normal binary .raw file in a fast access file, which is an LTSpice format built so that access to data points is very fast and efficient.
- -alt Use the alternate solver instead of the normal (default) one. Remember that alternate solver is way more accurate but it takes something like the double of the time to complete a simulation.
- -norm Use the normal solver.
- -I Must always be placed as last parameter; you have to specifiy a valid path to a folder containing symbols and subcircuits in case you included (.inc) something that is not in the default search path.
- -ascii .raw files are generated as ASCII (human readable) files rather than in binary format. This is very good for simple circuits since RAW files are typically small, but, when the design becomes complex or you run simulations with steppes parameters, .raw files could become huge and postprocessing could be very tricky.
- -encrypt This parameter encrypts a library file passed as last parameter.
The generic syntax for LTSpice commands is
0
XVII64 parameters file_to_process -I path
Sample Python script
This sample Python3 script is intended as starting point to develop your own automatic simulation and postprocessing tool. I will update this code from time to time, whenever I add some modifications that I find useful in my activities.
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
import matplotlib.pyplot as plt import os import time import multiprocessing as mproc ERR_RAWNOTFOUND = "File RAW not found" ERR_TRACENOTFOUND = "The given trace doesn't exists" ERR_INVALIDCMD = "Invalid command" RAW_UMTRANSLATION = {"time":"s","voltage":"Voltage [V]","device_current":"Current [A]"} """ Title """ def title(): print() print("*********************************") print(" LTSPICE-Automator ") print("*********************************") """ PARAMETERS filename: name of the RAW file to be parsed RETURN array of array with names of the traces and and measure units, array of array for all the traces in the simulation """ def readRAWfile(filename): try: fp = open(filename,"r") except: print(ERR_RAWNOTFOUND) return -1 head = [next(fp) for x in range(4)] nVars = int((fp.readline().split(":"))[1]) varNameType = [[],[]] for junk in fp: if "Variables:" in junk: break for row in fp: if "Values:" in row: break tmp = row.replace("\n","").split("\t") varNameType[0].append(tmp[2]) varNameType[1].append(RAW_UMTRANSLATION[tmp[3]]) orderedBuffer = [] for i in range(nVars): orderedBuffer.append([]) i=0 for row in fp: if(i%nVars==0): x = float(row.split("\t\t")[1]) else: x = float(row) orderedBuffer[i%nVars].append(x) i += 1 fp.close() return varNameType,orderedBuffer """ PARAMETERS filename: name of the LOG file to be parsed RETURN array of array with the content of each measurement done """ def readLOGfile(filename): try: fp = open(filename,"r") except: print(ERR_RAWNOTFOUND) return -1 line = fp.readline() meas = [] while line != "": if "Measurement:" in line: measName = line.split()[1] line = fp.readline() measTmp = [] while line != "\n": tmp = line.split() measTmp.append(tmp) line = fp.readline() meas.append(measTmp) line = fp.readline() fp.close() return meas """ PARAMETERS t: array for the time arr: array of array for all the traces to be plotted on the same plot arrNames: array with name of the trace (node) for each array in arr arrUM: array with measure unit for each array in arr fig: integer number of the figure RETURN None """ def plotData(t,arr,arrNames,arrUM,fig): plt.figure(fig) i = 0 while i<len(arr): plt.plot(t,arr[i],label=arrNames[i]) i += 1 plt.grid(visible=True,which='both') plt.xlabel("time [s]") plt.ylabel(arrUM) plt.legend(loc="upper right") plt.draw() plt.show(block=True) """ PARAMETERS ls: array containing two arrays, first column is the name and second is the type RETURN None """ def printSignalList(ls): iMax = len(ls[0]) i = 0 while i < iMax: print(ls[0][i]+"\t"+ls[1][i]) i += 1 """ Implement here your data manipulation function """ def yourManipFunction1(): return 0 """ Implement here your data manipulation function """ def yourManipFunction2(): return 0 if __name__ == "__main__": title() fileasc = "Draft1" print("Executing LTspice...") os.system("XVIIx64.exe -b -Run -ascii "+fileasc+".asc") print("Simulation done") time.sleep(0.1) print("Reading RAW file...") varNameType,data = readRAWfile(fileasc+".raw") print("Reading done") prcs = [] while 1: cmd = input("CMD >> ") if cmd == "list": printSignalList(varNameType) elif cmd[0:4] == "plot": parsedCmd = cmd.split() t = data[0] arr = [] arrNames = [] arrUM = [] for i in range(1,len(parsedCmd)-1): try: ai = varNameType[0].index(parsedCmd[i]) except: print(ERR_TRACENOTFOUND) continue try: arr.append( data[ai] ) except: print(ERR_TRACENOTFOUND) continue arrNames.append(parsedCmd[i]) arrUM.append( varNameType[1][ai] ) fig = parsedCmd[-1] p = mproc.Process(target=plotData,args=(t,arr,arrNames,arrUM,fig)) prcs.append(p) p.start() elif cmd == "log": m=readLOGfile(fileasc+".log") print(m) elif cmd == "help": print("list") print("plot trace1 trace2 ... fign") elif cmd == "exit": break else: print(ERR_INVALIDCMD) for p in prcs: p.join()
Comments
Please, remember to always be polite and respectful in the comments section. In case of doubts, read this before posting.
Posted comments ⮧
Comment section still empty.
INDEX
INFO


STATISTICS

CONTACTS
SHARE