Simulation

The ability to generate any kind of text based netlist together with the possibility to script macros makes it possible to integrate nearly any simulators or other 3rd party tool into the user interface. This integration can simply be done by writing a short macro and adding it to the main menu of the SchematicEditor. For some common simulators a macro is shipped with the SchematicEditor and already added to the default main menu. The macro file itself is located in the macro folder and can be adjusted with the included TextEditor. The exact location of the macro folder depends on your platform (e.g. "/opt/layout" on most Linux systems, under "Program Files/LayoutEditor" on Windows and in the LayoutEditor application folder on Mac OS X). A macro set for these simulators is included:

ngspice

Ngspice is a mixed-level/mixed-signal circuit simulator. Its code is based on three open source software packages: Spice3f5, Cider1b1 and Xspice. The simulator is GPL licensed and runs on most platforms. It is shipped as source code, but there are binary packages for Microsoft Windows and it is included in some Linux distributions. If it is shipped with your distribution the shipped macros will run without any adjustments. Also it will run on Windows without adjustment, if you uncompress it in the same folder level as your LayoutEditor installation . Please check the file permissions, if you install it in the 'Program Files' folder. In all other cases you need to adjust the macro to your installation path. Three macros for ngspice are shipped: trigger the simulation with waveform viewer, trigger the simulation with text output and view only the ngspice netlist. You will find these macros at the menu item Utilities of the SchematicEditor and TextEditor. More information on the integration is here.

LTspice

LTspice is a free high performance Spice III simulator from Linear Technology. It is programmed for Microsoft Windows, but works well under Linux using Wine. It is shipped with a installer and will by default installation on drive C: in the Program Files folder. If you don't change this default path you can run the shipped LTspice macros without any setup. If you have installed it in a different location, please adjust the line f.filename="/your/LTspice/path"; in the macros. Please see the comments in the macros for further instructions. The shiped macros are added by default to the main menu of the SchematicEditor and TextEditor and are located under utilities/LTSpice. There are different macros: one to trigger the simulation with waveform viewer after simulation, trigger the simulation with text output and view only the netlist. More information on the integration is here.

Qucs

As with LTspice and ngspice there are tree macros to trigger a Qucs simulation: trigger the simulation with waveform viewer, trigger the simulation with text output and view only the netlist. You will find these macros at the menu item Utilities of the SchematicEditor. In most cases you need to adjust a path in the macro to use it. Please see the comments inside the macro files for further instruction on adjusting the path.

Integrating Other Simulators and Tools

With the existing integration you have a great template for integration further simulators and other tools. So let us go through one integration and explain the single steps. You will find the shown macros code inside the macro folder of the LayoutEditor package.

#!/usr/bin/layout
#name=Simulate + Probe
#help=Starts a simulation with ngspice

string temp;

int main(){
  file f;
  temp=f.tempPath()+"/";
  f.filename=temp+"ng-data.txt";
  f.remove();
  string callbackDir=f.currentPath()+"/../../../callback/";
  setup::checkNetlistSetup("spice");
  setup::checkNetlistSetup("ngspice");
  schematic->saveNetList(temp+"ng-netlist.cir","ngspice");
  stringList sl;
  if (temp.contains(":")) {
    //windows
    sl.append("-i");
    }
  else {
    sl.append("-b");
    }
  sl.append(temp+"ng-netlist.cir");
  sl.append("-r");
  sl.append(temp+"ng-data.raw");
  bool b=process::externalTool("ngspice","ngspice",sl,callbackDir+"ngspice-sim1.layout");
  if (!b) b=process::externalTool("ngspice/bin","ngspice",sl,callbackDir+"ngspice-sim1.layout");
  if (!b) b=process::externalTool("spice","ngspice",sl,callbackDir+"ngspice-sim1.layout");
  if (!b) b=process::externalTool("spice_c","ngspice",sl,callbackDir+"ngspice-sim1.layout");
  if (!b) b=process::externalTool("ngspice/spice","ngspice",sl,callbackDir+"ngspice-sim1.layout");
  if (!b) b=process::externalTool("ngspice23_110605-2/spice","ngspice",sl,"callbackDir+ngspice-sim1.layout");
  if (!b) {
    string s=schematic->generateNetList("ngspice");
    string s1="**************************************************\n* ngspice was not found on your system!\n* Install it ( http://ngspice.sourceforge.net ) or \n* adjust this macro to start the simulation with the schematic!\n**************************************************\n\n\n* ngspice netlist:\n\n"+s;
    textEdit *te=project::getCentralTextEditor();
    te->newDoc();
    te->drawing->setText(s1);
    te->drawing->setLexer("spice");
    te->drawing->title="netlist";
    }
  return 0;

}

The first three lines are the header to detect a macro by the LayoutEditor and enable its integration into the menu. In line 5 a variable is defined for latest use. The lines 9 - 16 will extract the netlist and stores it inside the temporary folder. The lines 17 - 27 will prepare the parameters required to run the simulation. Line 28 - 33 will start the simulation. The ngspice may be stored within different folder on different system. Known locations will be check until a working installation of ngspice is found. A callback macro is set. It is called when the simulation is completed. In line 34 - 42 a error message is shown on case ngspice was not found and the netlist is output to trigger the simulation by hand.

A callback macro that outputs the results of a simulation may look like:

#!/usr/bin/layout
#name=callback ngspice simulation
#help=callback macro called after completing the ngspice simualtion

string temp;

int main(){
  file f;
  temp=f.tempPath()+"/";
  string callbackDir=f.currentPath()+"/";
  stringList sl;
  sl.append("b");
  sl.append(temp+"ng-data.raw");
  sl.append("a");
  sl.append(temp+"ng-data.txt");
  bool b=process::externalTool("ngspice","ngsconvert",sl,callbackDir+"ngspice-sim2.layout");
  if (!b) b=process::externalTool("ngspice/bin","ngsconvert",sl,"ngspice-sim2.layout");
  string s=process::toolResult();
  if (s.size()>1){
      textEdit *te=project::getCentralTextEditor();
      if (te->drawing->text()!="") {
            te->newFile();
            te->setFile(te->countFiles()-1);
            }
      te->setText(s);
      te->drawing->title="NGSpice simulation report";
      te->drawing->setModifySaved();
  }
  return 0;
}

Here once again the lines 1-3 are to detect the code as a LayoutEditor macro. In line 10 to 19 the conversion tool is called to convert the simulation data into an ASCII code and a second callback macro is setup that will process the converted data once it has been finished. The lines 20 - 30 will display the simulation report in the integrated TextEditor.