Coupling to external circuits using ngspice - vonkarmaninstitute/pantera-pic-dsmc GitHub Wiki

Conductive surfaces in Pantera can be virtually connected to external electrical circuits, which are simulated using ngspice, an open source spice simulator for electric and electronic circuits based on the SPICE code originally developed at UC Berkeley.

You should have ngspice installed on your system. Pantera has been tested using version ngspice-44.2.

When a surface is given the spicenode boundary condition using the Boundary_condition: keyword, it is treated as a conductor at a uniform potential, and at each time step during the simulation particle currents on that surface are accumulated. The coupling with ngspice occurs by treating this conductive surface as a node of the circuit, where Pantera provides the current entering the node, and ngspice returns the voltage computed accordingly.

In SPICE (and so in ngspice), circuits are represented as "netlists", a text-based representation of the nodes and components that form the circuit. A netlist can be created manually, or using one of the many online and offline graphical interfaces available.

In addition to assigning the boundary condition, the simulation folder must contain the following three files:

  • circuit.cir, the file containing the netlist of the external circuit. Here, the nodes that are connected to a Pantera surface, must be named with the same name of the physical group assigned at the mesh generation stage. This name must be in lowercase characters, or it will not be recognized by ngspice. For instance, the following circuit

immagine

is encoded as follows

.title external circuit

VIN vin 0 SIN(0 50 13.56e6 0 0 0)
R1 vin bal 50 
C1 bal anode 100p

I1 0 anode 0

.end

Notice that in this circuit definition, current sources such as I1 should be added to each node that represents a surface in Pantera, in order to include particle currents. Notice that the order of the nodes for the current source matters, and it should always be ground first (N+) and then the physical surface (N-), such that a positive (negative) current corresponds to ion (electron) flow into the surface. The ground node must always be present, and named either 0 or gnd.

  • initial.cir contains the configuration for ngspice to obtain the initial solution. It should follow the following template:
* SCRIPT: initial circuit solution

.include circuit.cir

.ic V(bal)=0
.ic V(anode)=0

.control
stop when time > $tsmall
tran $tsmall $tend uic
snsave snapshot.snap
wrnodev voltages
quit
.endc

Here, initial conditions for the circuit can be included using the standard SPICE directives .ic. The parameters $tsmall and $tend are passed by Pantera, and are respectively a small time step to obtain the initial solution, and the final time of the simulation. The intermediate state of the circuit in ngspice will be saved to file and reloaded at each time to resume the integration with the correct node particle currents.

  • update.cir contains the configuration for ngspice to integrate the solution in time. It should follow the following template:
* SCRIPT: update circuit solution

.control
snload circuit.cir snapshot.snap
alter I1 $ianode
stop when time > $tf
resume
snsave snapshot.snap
wrnodev voltages
quit
.endc

Here, an alter command should be included for each particle current source, in order to assign the value computed by Pantera. This will be a variable named $i<nodename>, in this case $ianode. The additional parameter $tf is also passed by Pantera, which tells ngspice at what time to stop the integration.

The intermediate state of the circuit is stored by ngspice in the snapshot.snap file, and the node voltages are written in the voltages file, which is then read by Pantera to complete the coupling. Make sure to delete these two files before starting a new simulation.

⚠️ **GitHub.com Fallback** ⚠️