How to add a new device?

  • mergan

    mergan - 2013-01-09


    I intend to add a new device to the Spice (e.g.  ngSpice ), which is implemented by 3 nonlinear differential equations. 
    I shoud say that I am a complete novice. I have installed the ngSpice25. The ngSpice is composed of many Programms and folders and subfolders. I have no idea how can I look through the construction of ngspice in order to add a  new device to it.
    My device is already  implemented   in Mathematica. If I implement it in C , is there any possibilty to connect it  to the Spice ?or is there any other way to do it, so that my device is also adaptable by Spice?

    I would be very grateful for your  hints in advance.
    Best regards

  • marcel hendrix

    marcel hendrix - 2013-01-09

    > or is there any other way to do it, so that my device is also adaptable by Spice?

    You can use a behavioral model and type the euations in directly? The reasons NOT to take this road would be that you need very high speed, or that the converge of your specific equations is very bad with the standard SPICE algorithms. In your own C/XSPICE code you can provide hints and influence the stepping explicitly.


  • mergan

    mergan - 2013-01-09

    Thank you Holger and  Marcel very much.

    I'm still a little bit confused.
      The device which is going to be implemented, is a  global model of an spatially  distributed system which is given by three linear  differential equations. I'm going to study all possibilities which you have introduced me.
    Thanks you a lot.

  • mergan

    mergan - 2013-01-10


    As far as i understand, There is at least two ways to add a device to Spice.

    The first method (as is used to add a memristor) is  explained in
    The second method is to use the XSpice which is explained briefly in this page

    In both case I must implement my Code in C which is already implemented in Mathematica.
    Is it a right way to implement the Code in C and then by the next Step to add C Code to the Spice ??
    Thank you in advance.
    Best regard

  • Holger Vogt

    Holger Vogt - 2013-01-10


    to repeat my statement from the former forum thread and clarify things:

    There are three methods to add devices to ngspice. All three methods have been used to implement a memristor as a new device into ngspice (or spice3) , which may therefore serve as an example for the complexity of each approach.

    The first method, adding a device similar to what people have done for transistors (e.g. the BSIM4 model), is etremely complex. You will have to obtain a detailed knowledge of the interior structur of ngspice, generate several C coded extra files, but also modify existing ngspice code.

    The second approach, using the XSPICE code model interface which is already integrated into ngspice, also allows you to write model equations in C, and then define the interface to ngspice. There is no need to modify existing ngspice code. There is basically no limit to the complexity of the new model, but the interface to ngspice has to follow stricht rules.

    The third approach  uses the B source, which allows to add a behavioral model to ngspice. B sources may source voltages or currents, depending on other voltages or currents in a linear or non-linear fashion. There is no need to write C code, everything is offered to ngspice via the input file. The complexity of this approach, however, is somewhat limited. Differential equations probably may be added, if converted to integral equations, via integrators using electrically equivalent circuit blocks, like in an "analog computer".

    I would suggest to drop the first approach, or you would need to spend some month for obtaining the necessary know how.

    Methods two and three are less complex, still require some learning. Method three probably is too limited. If not, this would be the easiest approach.

    So, as Marcel already suggested, an XSPICE code model is the way to go.
    Firstly you need to define the interface: ngspice sources and reads voltages on nodes or currents through nodes. Your model has to reflect that (like the memristor model, or any other analog model from the XSPICE codemodel library). Then you may define the C code, for example to describe the output current as function of input voltages. You will then also need to establish the (mathmatical) derivatives, e.g. dI/dV, to allow the simulation algorithm funtioning.

    Please have a look at the ngspice manual, chapts. 25 - 27 for details on how to add a code model, and what interface descriptions are required. And have a look at the existing code models. You may study simple examples like 'gain' element, 'integrator', or more complex like 'memristor'. Finally you will have to add only two files, ifspec.ifs and cfunc.mod, to obtain a new code model.


  • marcel hendrix

    marcel hendrix - 2013-01-11

    Dear Nina,

    Holger has given a perfect summary!
    A small addition w.r.t. the B-source/Behavioral model: It is only 'limited' in the sense that it becomes awkward to program if your circuit element is complex (like needing higher order differential equations). However, it is certainly possible to get such a subcircuit to function according to your specification. It may look ugly, very inelegant and may need some tweaks with the SPICE convergence parameters. It will not *by definition* run slower than C code.  I would advice to use B-sources first, and go the XSPICE route when that works and your not satisfied by the performance.


  • mergan

    mergan - 2013-01-15

    Dear guys,

    thank you very much again for your very helpful Hints and detailed explanation. 
    I think, i 'll use the XSpice method at first. Therefor, I've begun to implement my Code in C.
    So long and best,


Log in to post a comment.