|
From: Matt F. <fl...@Ma...> - 2005-09-28 09:34:55
|
Alot more needs to be done to make these logic patches work seamlessly
with subckts. Unfortunately I don't have the time to walk myself through
the ngspice parser in detail. So these logic patches mark a point of
starting the introduction of logic to the parser and also the
ng-SPICE engine.
Before I leave this topic, I would like to make note of a few things
which must be considered for a fully working logic parser.
Within the parsing engine is the process that builds an execution tree
for the spice engine. This execution tree is linked together using
pointers to nodes. The parsing lexer must be able to correctly check
syntax on 'if then else' and work out whether all is sweet syntacticly.
The parsing of the comparison operators is probably less challenging in
this syntactical regard.
During the construction, and I am not sure why or exactly whats going on
there, but ... the derivatives of each of the operators {+,-,$,v,*,/,^,
etc etc} are addressed. Which leads to the next topic on logical
differentiation.
This topic addresses specifically the function :
static INPparseNode *PTdifferentiate(INPparseNode * p, int varnum);
from the file :
src/spicelib/parser/inpptree.c
If it is necessary to evaluate logical differentiation, then it is
important to realise that logic is either !=0 OR == 0. It may be safe to
assume that logic is either =1 or =0.
If this is the case, then at the point where logic turns from a 1 to a
zero, there is infinite bandwidth and consequently the derivative or
rate of change at that point is potentially HUGE !
If you consider a logic function as a unit step function, as in the
operators {>,>=,<,<=} with the step occurring at the origin, then the
derivative of a unit step is the delta function. This is realiseable in
some limiting fashion ... otherwise the limit can go to infinity.
PTdifferentiate passes this responsibility onto 'mkb', like so :
case PT_GREATERTHAN:
case PT_GREATERTHANEQ:
case PT_LESSTHAN:
case PT_LESSTHANEQ:
case PT_EQUALTO:
case PT_NOTEQUALTO:
newp = mkb(p->type, left, right);
break;
and inside mkb we add :
case PT_GREATERTHAN:
case PT_GREATERTHANEQ:
if (left->constant == right->constant)
return mkcon((double)1);
case PT_LESSTHAN:
case PT_LESSTHANEQ:
if (left->constant == right->constant)
return mkcon((double)-1);
above, the negative one is related to the orientation of the step
function in range. Either a step up, or a step down.
When the step gets shifted off the origin, as in x > 16, then the
derivative of the shifted step function is returned. This is a shifted
delta function. and the previous concept still stands, however must be
altered to :
case PT_GREATERTHAN:
case PT_GREATERTHANEQ:
if (left->constant == right->constant) && condition for const.
return mkcon((double)1);
case PT_LESSTHAN:
case PT_LESSTHANEQ:
if (left->constant == right->constant)
return mkcon((double)-1);
Where conditions on constants are of the form (right->constant == 16)
The return mkcon(1.0) or whatever is necessary.
Finally the case of equal to and not equal-to ... this assumes a unit
delta function and the derivative of this becomes infinite at the
origin. This needs more work.
Lastly are the if and then equal to operators {?,:}
I have not begun to think about these in terms of differentiation, so I
leave this for the next person to tackle.
giving the love back to ngspice
Matt
On Mon, Sep 26, 2005 at 04:59:53PM +1000, Matt Flax wrote:
> Hello,
>
> I have introduced new logic opertors to ngspice. The attatched patches
> will allow you to successfully patch both CVS and rework-17. CVS fails
> to compile for me at the moment - even before I apply the patches !
> If you apply them to rework-17, reject the AUTHORS file patch which
> isn't important.
>
> I have not altered the previous source much at all ... analysis of the
> patches should let you see that pretty much all of the previous code is
> untouched ... the 'prectable' has been expanded, but the previous table
> values are unchanged.
>
> Firstly you can now declare if statements in sources like so :
> Bs 3 0 v = (v(1) > v(2)) ? (1) : (2)
>
> I am sure you can do alot more stuff with the new operators as well.
> Some of the operators need work in the differentiation stage of the
> inpptree.c file. This will come later.
>
> Read the README in the attatchment for more info.
>
> ============== example useage =====================================
> * circuit to test if then else. Control is the undefined signal.
>
> Bite 2 0 v=(abs(v(1))>50u)?((v(1)/(1-exp(-v(1)))):(1/exp(-v(1))))
> Bcontrol 3 0 v=v(1)/(1-exp(-v(1)))
> Rsing2 2 0 1e12
> Rsing3 3 0 1e12
>
> .dc Vin -1 1 0.001
> .control
> run
> *tran .1m 1m
> plot v(2) ylabel if_then_else_fix
> plot v(3) ylabel undefind_value
> .endc
> .end
> =========================== end example useage ===================
>
> Matt
>
> On Sun, Sep 25, 2005 at 02:58:23AM +1000, Matt Flax wrote:
> > Hello,
> >
> > I have now had some success ... I can embedd the following operator :
> > Btest 1 0 v=(20)?(2)
> >
> > At each point in time, the '?' operator's function is called and is
> > expected to return a double value - as is the same for '+', '-' and all
> > the other operators ...
> >
> > Left to do now is the 'then or else' operator ....
> > so I now have to work out the best method for implementing either of
> > these:
> > a] v=cond?then:else
> > b] v=cond?(then,else)
> >
> > The current issue in my mind is that spicelib isn't constructed to
> > support three parameter passing ... maximum parameter passing is 2. I
> > will look into this further though.
> >
> > For other's interest (and possibly desire to help), I list the basic
> > introductions I have made to spicelib :
> > The operator table list has changed to this :
> > static struct op {
> > int number;
> > char *name;
> > double (*funcptr) ();
> > } ops[] = {
> > {
> > PT_COMMA, ",", NULL}, {
> > PT_PLUS, "+", PTplus}, {
> > PT_MINUS, "-", PTminus}, {
> > PT_TIMES, "*", PTtimes}, {
> > PT_DIVIDE, "/", PTdivide}, {
> > PT_POWER, "^", PTpower}, {
> > NULL, NULL, NULL}, { /* these are added to pad to the next */
> > NULL, NULL, NULL}, { /* available function spot - flatmax */
> > NULL, NULL, NULL}, {
> > NULL, NULL, NULL}, {
> > NULL, NULL, NULL}, {
> > PT_IFTHENELSE, "?", PTifthenelse} /* flatmax adds this new operator */
> > };
> >
> > And the precedence (execution ordering) table (which will need some
> > tweaking - help requested) now looks like this :
> > static char prectable[12][12] = {
> > /* $ + - * / ^ u- ( ) v , ? */
> > /* $ */ {R, L, L, L, L, L, L, L, R, L, R, L},
> > /* + */ {G, G, G, L, L, L, L, L, G, L, G, L},
> > /* - */ {G, G, G, L, L, L, L, L, G, L, G, L},
> > /* * */ {G, G, G, G, G, L, L, L, G, L, G, L},
> > /* / */ {G, G, G, G, G, L, L, L, G, L, G, L},
> > /* ^ */ {G, G, G, G, G, L, L, L, G, L, G, L},
> > /* u-*/ {G, G, G, G, G, G, G, L, G, L, R, G},
> > /* ( */ {R, L, L, L, L, L, L, L, E, L, L, L},
> > /* ) */ {G, G, G, G, G, G, G, R, G, R, G, G},
> > /* v */ {G, G, G, G, G, G, G, G, G, R, G, G},
> > /* , */ {G, L, L, L, L, L, L, L, G, L, G, L},
> > /* ? */ {G, G, G, G, G, G, L, L, G, L, G, G}
> > };
> >
> > Matt
> >
> > On Fri, Sep 23, 2005 at 03:37:00PM +1000, Matt Flax wrote:
> > > Hello,
> > >
> > > I am in the process of introducing an 'if then else' operator.
> > > I have managed to get ngspice to recognise the '?' token by adding the
> > > following :
> > > case '?':
> > > printf("TOK_IFTHENELSE found\n");
> > > el.token = TOK_IFTHENELSE;
> > > sbuf++;
> > > break;
> > >
> > > where in inptree.h :
> > > #define TOK_IFTHENELSE 11
> > >
> > > It is also aware of the opertor
> > > #define PT_IFTHENELSE 11
> > >
> > > I have also begun to implement the code in the 'PTdifferentiate'
> > > function ...
> > >
> > > The idea is to be able to write :
> > >
> > > Bite 1 0 v=v(cond)?casesHere
> > >
> > > which will evaluate which 'casesHere' to return depending on the state
> > > of v(cond) ... this is identical to the C 'cond?then:else' operator.
> > >
> > > My problem is that when I compile and run ngspice, it evaluates the Bite
> > > line as returns :
> > > Error on line 3 : bite 1 0 v=v(2)?40
> > > parameter value out of range or the wrong type
> > >
> > > It appears that there is something I am missing ???
> > >
> > > I know it recognises the 'TOK_IFTHENELSE' token, but I don't think I am
> > > doing the parameter estimation correctly ...
> > >
> > > thanks
> > > Matt
> > > --
> > > http://www.flatmax.org
> > >
> > > Public Projects :
> > > http://sourceforge.net/search/?type_of_search=soft&words=mffm
> > >
> > >
> > >
> > > -------------------------------------------------------
> > > SF.Net email is sponsored by:
> > > Tame your development challenges with Apache's Geronimo App Server.
> > > Download it for free - -and be entered to win a 42" plasma tv or your very
> > > own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
> > > _______________________________________________
> > > Ngspice-devel mailing list
> > > Ngs...@li...
> > > https://lists.sourceforge.net/lists/listinfo/ngspice-devel
> >
> > --
> > http://www.flatmax.org
> >
> > Public Projects :
> > http://sourceforge.net/search/?type_of_search=soft&words=mffm
> >
> >
> >
> > -------------------------------------------------------
> > SF.Net email is sponsored by:
> > Tame your development challenges with Apache's Geronimo App Server.
> > Download it for free - -and be entered to win a 42" plasma tv or your very
> > own Sony(tm)PSP. Click here to play: http://sourceforge.net/geronimo.php
> > _______________________________________________
> > Ngspice-devel mailing list
> > Ngs...@li...
> > https://lists.sourceforge.net/lists/listinfo/ngspice-devel
>
> --
> http://www.flatmax.org
>
> Public Projects :
> http://sourceforge.net/search/?type_of_search=soft&words=mffm
>
--
http://www.flatmax.org
Public Projects :
http://sourceforge.net/search/?type_of_search=soft&words=mffm
|