Behavioral Source

  • mergan

    mergan - 2013-02-12

    Hallo everybody,

    I try to simulate an electronic circuit depicted in the  annexed file.

    This circuit contains  two  nonlinear devices :
    These two devices are depending on the charge density  and are defined as follows:
    Vs = K * Q^2
    Id = kd1 * exp(-Q/kd2)
    where  I = dQ / dt

    Is it possible to use the Behavioral Source?
    According to the ngspice manual 25 , the nonlinear Source are only depending on the node voltages and branch current.
    In my case the nonlinear Source or device depends on the integration of the branch current.
    Is it possible to  define a B-source which depends on the  integration of the branch current ?

    Many thanks in advance !

    Best regards

  • Robert Larice

    Robert Larice - 2013-02-12


      the given url is not readable,
         and your specification is incomplete.

         you can combine B (actually ASRC) devices
         with capacitors to model devices with voltages
          and currents depending in nonlinear ways on charges.

    a sketch might be (syntax probably incorrect, untested)
        B12   n+  nsens   V = 1.001 * v(q)  + 2.003 * v(q)^3
        V12   nsens n-    DC=0
        H12   q 0  V12    42e-3
        C12   q 0            1uF
      you will need a .ic (initial condition) for node q of course



  • mergan

    mergan - 2013-02-13

    Please note this picture,  the nodes are also numbered:

    The problem is that the  nonlinear device doesn't depend on the  voltage or current directly as in your example, but it depends on the charge which is the integration of the current.
    I have tried  to use the Integrator which is implemented by xspice as follows:

    *measure charge by integrating current
    aint 3 2 time_count
    .model  time_count int(in_offset=0.0 gain=1.0 out_lower_limit=-1e12 out_upper_limit=1e12 limit_range=1e-9 out_ic=0.0)

    the question is , is charge saved in aint ? if yes can I use it as variable in the Behavioral Source as follows :

    Bs 3 2 V=aint * aint

  • Robert Larice

    Robert Larice - 2013-02-13

    Hello mergan,

        look again at the sketch which I've provided.
        it probes and then integrates a current in an auxiliary capacitor,
          whose voltage thus represents your desired Q.
        and then uses a ASRC to implement a voltage
          which is a non linear funktion of this Q.

       with this sort of trick you can model
          capacitive two ports, whose capacitance
       depends on the applied voltage.

       of course you can integrate with xspice too,
          but it is not necessary for this problem.

  • mergan

    mergan - 2013-02-13

    Hello Robert,

    thanks for the answer, but I'm sorry , I haven't understand your solution not quite because of my poor knowledge of circuits…
    I can understand your nice trick to calculate the charge on a node but  I would like to ask you the following questions

    1. Shouldn't be   the capacitance   set to 1, in order to equalize  the voltage and the  charge ?
    U  =  1/C * int (I)        and if C = 1    =>  U = int (I) = Q

    2. why have you set the value of the transresistance to   42e-3 ~ 2.091 (H12 q 0 V12 42e-3)?

      r = Delta u / Delta i = (U2 -U1) /( i2 -i1)

    Thank you very much in advance

    Best regards

  • Robert Larice

    Robert Larice - 2013-02-13

    Hello mergan,

       I did choose these numbers for no particular reason,
          you can choose anything you want,
          as long as you adjust your nonlinear expression accordingly.
      but you should be aware of all spices expectation
         that voltages and currents have reasonable magnitudes.
      (read about  ABSTOL for example, in spice literature,
          and our manual)
      thus you will need to use these figures to scale your
         Q representation to something acceptable.
       (avoid Giga Volts and Atto Volts …)
       the same goes for currents (take care of the transresistance)


  • mergan

    mergan - 2013-02-21

    Hallo Robert,

    Thank you very much for your detailed answer.
    According to your proposal, I have written a netlist as given in the following. But I'm wondering why the current as the output of a nonlinear circuit is still sinusoidal by a sinusoidal excitation voltage. I don't know, if there is  any logical or technical problem in the code.
    Thanks in advance!

    My netlist:
    *Series RLC Circuit
    V1 0 1 SIN(0 200 0.1k)
    *V1 0 1 AC 200 0
    C0 2 1 1n
    X1 3 2 nonlinDevice
    L1 4 3 0.0125
    R1 5 4 200
    Vg 0 5 DC=30
    .subckt nonlinDevice n1 n2
    VDC n1 n0 DC=0
    BVs n0 n2 V=v(ng)*v(ng)
    BIs n1 n2 I=exp(-v(ng)*v(ng))
    Bint 0 ng I=i(VDC)
    Cint ng 0 1
    Rint ng 0 100G
    tran 0.01m 8m
    plot i(l1)

  • marcel hendrix

    marcel hendrix - 2013-02-21

    The generic answer is that the series LC circuit is (probably) designed to act as a filter, passing through only the base harmonic. If the inductor is made very small and the series capacitor very large, the result will probably better match your expectation.

  • Robert Larice

    Robert Larice - 2013-02-21

    Hello mergan,

      first I've put you a bit on the wrong track.
      actually we have already implemented nonlinear capacitors
         in ngspice quite some time ago.
      I forgot that, and I forgot the details how the implementation
         was done.
      I used currents back than, not voltages as I've suggested to you.
      If you want to know, you can set a variable ngdebug in your spinit file, and
         will get a dump of the actual implementation in a file debug-out3.txt
    Then, I've not looked to much at your circuit, but it seems you have
        hacked your non-linearity twice, the second incarnation canceling the first one.
      Instead I've here a testbench for a nonlinear capacitor with
          Q = sqrt(v)
      which you can use for further steps.
      Maybe you have to be a bit careful with your sqrt() device
        because for voltage 0 the derivative
        dQ/dV will be infinite
    You might reconsider your specification
      concerning this. and as well think about negative voltage.

      anyway here is a testbench for a Q=sqrt(v) device,
        excited with positive voltages.

    precision of various C= implementations
    * (compile (concat "w32/src/ngspice "  buffer-file-name) t)
    * wanted circuit is:
    *   Q = sqrt(V)
    * which yields
    *   dQ/dV = 1/2 * 1/sqrt(V)
    * use this for the behavioural C= device
    * connect this to a sin voltage source
    *   V = 2 + sin(omega*t)
    *   Q = sqrt(2 + sin(omega*t))
    * yields:
    *   I = dQ/dt
    *     = 1/2 * 1/sqrt(2 + sin(omega*t)) * omega * cos(omega*t)
    * test voltage
    Bsrc     t 0  V = 2 + sin(time*6)
    * current probe
    Vsrc     t 1  DC = 0
    * test device
    Ctest  1 0   C = {0.5 / sqrt(v(1))}
    set reltol = 1e-6
    print $reltol
    set vntol = 1e-9
    print $vntol
    set abstol = 1e-9
    print $abstol
    set chgtol = 1e-19
    print $chgtol
    tran 1ms 2s
    * thats our golden expected answer
    let Igold = 0.5 / sqrt(2 + sin(time*6)) * 6 * cos(time*6)
    * plot gold, actual current, and deviation
    plot Igold
    plot Vsrc#branch
    plot Vsrc#branch - Igold    ylimit -100u 100u



Log in to post a comment.

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

JavaScript is required for this form.

No, thanks