LTSpice can export schematics in other formats besides its own. I tried to understand how the various formats available represent a schematic and found that schematics exported in Accel format are not that difficult to decipher. This format seems quite straightforward to understand. The bad news is ngspice does not support Accel format. So, I decided to write a software tool to convert Accel format into ngspice format. I already achieved some results, but the connections are disordered. This means, that my algorithm needs to do another pass to order the component pins. The format for ngspice to represent a component is "component pin1 pin2 pin3 .... model_number". In my application nets represented by pin1, pin2, pin3, etc are not in order which means to give an example the emitter of a transistor may be mixed with the collector or the base. Although at first, this seemed to be an easy task, this is proving to be quite hard to achieve.
The coding is in Delphi (or Lazarus) Object Pascal. I will post the source code when I am ready. The first version will be limited to a few component types but hopefully the source code will be extensible. I plan to post the code in this thread when it behaves better than it is at this moment.
Some of the code follows. This is only a snippet.
The coding is in Delphi (or Lazarus) Object Pascal. I will post the source code when I am ready. The first version will be limited to a few component types but hopefully the source code will be extensible. I plan to post the code in this thread when it behaves better than it is at this moment.
Some of the code follows. This is only a snippet.
Code:
procedure TForm1.BtnParseClick(Sender: TObject);
var
aline, compinst, compvalue, CompResult: string;
i, k: Integer;
DoCopy: Boolean;
comp_reslines, conn_reslines: TStringList;
NetFound: Boolean;
begin
DoCopy := false;
NetFound := false;
compvalue := '';
compinst := '';
CompResult := '';
comp_reslines := TStringList.Create;
conn_reslines := TStringList.Create;
For i := 0 to OrigMemo.Lines.Count - 1 do
begin
aline := OrigMemo.Lines[i];
if Pos('(compinst ', aline) > 0
then
begin
for k := 1 to length(aline) do
begin
if aline[k] = '"'
then
begin
DoCopy := (aline[k] = '"') and (not DoCopy);
continue;
end;
if DoCopy then compinst := compinst + aline[k];
end;
end
else if Pos('(compvalue ', aline) > 0
then
begin
for k := 1 to length(aline) do
begin
if aline[k] = '"'
then
begin
DoCopy := (aline[k] = '"') and (not DoCopy);
continue;
end;
if DoCopy then compvalue := compvalue + aline[k];
end;
end;
if (compinst <> '') and (compvalue <> '')
then
begin
CompResult := compinst + ' = ' + compvalue;
comp_reslines.Add(CompResult);
//showmessage(CompResult);
compinst := '';
compvalue := '';
CompResult := '';
Continue; //iterate the next
end;
If (NetFound = false)
then NetFound := IsNet(aline);
if NetFound and IsNet(aline)
then aline := Trim(aline);
if NetFound then conn_reslines.Add(aline);
//showmessage(aline);
end;
// The following reads reslines to get 'comp num num ... value' strings as
// required by ngspice
CompMemo.Clear;
ConnMemo.Clear;
For i := 0 To comp_reslines.Count - 1 do
CompMemo.Lines.Add(comp_reslines[i]);
For i := 0 to conn_reslines.Count - 1 do
ConnMemo.Lines.Add(conn_reslines[i]);
comp_reslines.Free;
conn_reslines.Free;
PageControl1.ActivePage := TabSheet2;
end;
I am debugging and testing the first version of the software. At this point, the software only supports the most common components. Other components will be added in later versions.
My Integrated Development Environment is the Lazarus free suite. To compile (get an executable from source code) the software install Lazarus. Yes, I know Lazarus is a large project and it takes space to install. I am attaching the project source code for those interested. Be warned that this software is still in its very first stage of debugging and is experimental.
The active code which does the actual conversion is in in Form1.pas. To use the application load an Accel netlist. Create this using LTSpice export function. To use the software first press 'Parse' and then 'Convert'. The Accel netlist is loaded in the first tab. In the second and third tabs the parsed output is displayed after pressing 'Parse'. The fourth tab is used to hold the converted ngspice code.
My version of LTSpice, which is not the latest version, does some omissions when an Accel netlist is exported. Please, check the Accel netlist for errors of omission. These are easy to spot and correct.
My Integrated Development Environment is the Lazarus free suite. To compile (get an executable from source code) the software install Lazarus. Yes, I know Lazarus is a large project and it takes space to install. I am attaching the project source code for those interested. Be warned that this software is still in its very first stage of debugging and is experimental.
The active code which does the actual conversion is in in Form1.pas. To use the application load an Accel netlist. Create this using LTSpice export function. To use the software first press 'Parse' and then 'Convert'. The Accel netlist is loaded in the first tab. In the second and third tabs the parsed output is displayed after pressing 'Parse'. The fourth tab is used to hold the converted ngspice code.
My version of LTSpice, which is not the latest version, does some omissions when an Accel netlist is exported. Please, check the Accel netlist for errors of omission. These are easy to spot and correct.
Attachments
Last edited:
Don't forget, spice also allows parameter passing. As a simple example, m=4 to say 4X the transistor. Subcircuits can also pass params.
I humbly ask readers to post working files for both LTSpice and their equivalent for ngspice. This will help me test the software on real examples using known ngspice files that work. Presently, the software does not know how to get the number of pins in an integrated circuit and what to do with a subcircuits. It is also not able to handle subcircuits. .MODEL declarations can also be automated. These are ideas which can be implemented in later versions.
I found working code examples online.
Now I have to add MOSFET and subcircuit support. I can also create more schematics using more components with LTSpice. At this point it is working but it definitely needs more functionality.
Now I have to add MOSFET and subcircuit support. I can also create more schematics using more components with LTSpice. At this point it is working but it definitely needs more functionality.
I am uploading the latest source code for this converter. Please, be advised that this software is still a works in progress. However, for converting Accel net files the code works but there may be syntax forms which may not yet be supported.
Attachments
I added more functionality to the GUI application which translates Accel net lists produced by LTSpice into ngspice net lists. The functions which do the conversion are now encapsulated in a class of their own. I also added a syntax checker for Accel net lists (produced by LTSpice). The syntax checker needs more work, but it should do its job of detecting errors. Syntax checking works independently of other functions of the application. This was done to allow partial conversions even for net lists with some errors. The first line which is a comment is omitted in conversion. Do not include this line for conversions.
The executable produced by compiling this source code is about 25MB. For a much smaller file do not include debug information. That should result in a much smaller file of approx 6MB.
The executable produced by compiling this source code is about 25MB. For a much smaller file do not include debug information. That should result in a much smaller file of approx 6MB.
Attachments
I would be interested in converting Micro-cap and Pspice into LTspice. Years ago, I did this sort of thing with AWK on Unix but today I would use C#. What is it about ngspice that you prefer over LTspice? I have considered doing this myself but it's not important enough to spare the time. It's easier to just re-enter circuits in LTspice.
LTSpice is a graphical application with many advantages. I tried to learn ngspice to have another tool with which I can simulate circuits. I almost know how to Plot an FFT but I must be doing something wrong. The resulting FFT plot is not the same like the one plotted by LTSpice. The sequence of commands I am using is:
Code:
tran 10u 12m 2m
set xbrushwidth=1
linearize out
let lin-tstep = 10u
fft out
plot db(out) ylabel FFT xlog xlimit 10 2000k
- Home
- Design & Build
- Software Tools
- Converting LTSpice schematics into ngspice schematics