<?xml version="1.0" encoding="ISO-8859-1"?>

<!--
     Written by Laurent Lemaitre - r29173@users.sourceforge.net
     Documentation:
       http://sourceforge.net/docman/display_doc.php?docid=18993&group_id=84289
     Target Interface: ngspice
     Supported by adms-1.x.x
     This file is used by adms - http://sourceforge.net/projects/mot-adms.
     adms is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published by
     the Free Software Foundation; either version 2 of the License, or
     (at your option) any later version.
     adms is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     GNU General Public License for more details.
     You should have received a copy of the GNU General Public License
     along with adms; if not, write to the Free Software
     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
-->

<!DOCTYPE admst SYSTEM "admst.dtd">
<admst version="2.3.0" xmlns:admst="http://mot-adms.sourceforge.net/xml-files/admst">

<admst:template match="dectype">
  <admst:choose>
    <admst:when test="[type='real']">
      <admst:text format="  double "/>
    </admst:when>
    <admst:when test="[type='integer']">
      <admst:text format="  int "/>
    </admst:when>
    <admst:otherwise>
      <admst:text format="  char* "/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<admst:template match="code">
<admst:value-of select="attribute[name='ngspicename']/value"/>
<admst:variable name="module" select="%s"/>

#include &quot;ngspice/ngspice.h&quot;
#include &quot;ngspice/cktdefs.h&quot;
#include &quot;ngspice/smpdefs.h&quot;
#include &quot;$(module)defs.h&quot;
#include &quot;ngspice/const.h&quot;
#include &quot;ngspice/sperror.h&quot;
#include &quot;ngspice/ifsim.h&quot;
#include &quot;ngspice/suffix.h&quot;

int $(module)setup (SMPmatrix *matrix, GENmodel *inModel, CKTcircuit *ckt, int *states)
     /* load the $(module) device structure with those pointers needed later 
      * for fast matrix loading 
      */
{
  $(module)model *model = ($(module)model*)inModel;
  $(module)instance *here;
  
  /*  loop through all the $(module) device models */
  for ( ;model != NULL ;model = $(module)nextModel(model) )
  {
<admst:for-each select="variable[parametertype='model' and input='yes']">
  <admst:if test="default[not(nilled(function[class='builtin']))]">
    <admst:text format="{\n"/>
    <admst:for-each select="default/function">
      <admst:value-of select="position(.)-1"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:text format="double __%s_%s=0.0;\n"/>
    </admst:for-each>
    <admst:apply-templates select="default" match="function:assignment"/>
  </admst:if>
  <admst:apply-templates select="default" match="expression:stringify:noprobe"/>
<admst:value-of select="name"/>
<admst:value-of select="name"/>
    if(model-&gt;%s_Given == FALSE) model-&gt;%s=%s;
<admst:if test="default[not(nilled(function[class='builtin']))]">
  <admst:text format="}\n"/>
</admst:if>
</admst:for-each>
    for ( here = $(module)instances(model) ;here != NULL ; here = $(module)nextInstance(here) )
    {
<admst:for-each select="variable[parametertype='instance' and input='yes']">
  <admst:if test="default[not(nilled(function[class='builtin']))]">
    <admst:text format="{\n"/>
    <admst:for-each select="default/function">
      <admst:value-of select="position(.)-1"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:text format="double __%s_%s=0.0;\n"/>
    </admst:for-each>
    <admst:apply-templates select="default" match="function:assignment"/>
  </admst:if>
  <admst:apply-templates select="default" match="expression:stringify:noprobe"/>
<admst:value-of select="name"/>
<admst:value-of select="name"/>
    if(here-&gt;%s_Given == FALSE) here-&gt;%s=%s;
<admst:if test="default[not(nilled(function[class='builtin']))]">
  <admst:text format="}\n"/>
</admst:if>
</admst:for-each>
      /* External Nodes, connected ? */
<admst:for-each select="node[location='external']">
      here->%(name)Node_connected = (here->%(name)Node &gt;= 0);
</admst:for-each>
<admst:if test="node[location='internal']">
      /* Internal Nodes */
      {
<admst:for-each select="node[location='internal']">
<admst:variable name="info" select="no info"/>
<admst:for-each select="attribute[name='info']">
  <admst:value-of select="value"/>
  <admst:variable name="info" select="%s"/>
</admst:for-each>
<admst:value-of select="name"/>
        here->%sNode = -1;
        here->%(name)Node_mine = 0;
</admst:for-each>
      }
</admst:if>
       ((GENinstance*)here)->GENstate = *states;
       *states += $(module)numStates;
      /* set states */
<admst:for-each select="source[dynamic='yes']">
<admst:value-of select="branch/nnode/name"/>
<admst:value-of select="branch/pnode/name"/>
      here->state_%s_%s = *states; *states += 2;
</admst:for-each>
      /* set Sparse Matrix Pointers */
<admst:for-each select="jacobian">
<admst:value-of select="column/name"/>
<admst:value-of select="row/name"/>
      here-&gt;PTR_J_%s_%s_required=0;
</admst:for-each>

      $(module)guesstopology(matrix,ckt,model,here);

      /* Internal Nodes */
      {
        int error;
        CKTnode *tmp;
<admst:for-each select="node[location!='ground']">
<admst:variable name="info" select="no info"/>
<admst:for-each select="attribute[name='info']">
  <admst:value-of select="value"/>
  <admst:variable name="info" select="%s"/>
</admst:for-each>
        if(here->%(name)Node == -1)
        {
          error=CKTmkVolt(ckt,&amp;tmp,here-&gt;$(module)name,&quot;X%(name)X&quot;);
          if(error) return(error);
          here->%(name)Node = tmp->number;
          here->%(name)Node_mine = 1;
        }
</admst:for-each>
      }
<admst:for-each select="jacobian">
<admst:value-of select="column/name"/>
<admst:value-of select="row/name"/>
        if(here-&gt;PTR_J_%s_%s_required==1)
        {
<admst:value-of select="column/name"/>
<admst:value-of select="row/name"/>
<admst:value-of select="column/name"/>
<admst:value-of select="row/name"/>
          here-&gt;PTR_J_%s_%s=SMPmakeElt(matrix,here-&gt;%sNode,here-&gt;%sNode);
        }
</admst:for-each>

    }
  }
  return(OK);
}


int
$(module)unsetup(GENmodel *inModel, CKTcircuit *ckt)
{
  $(module)model *model = ($(module)model*)inModel;
  $(module)instance *here;

  /*  loop through all the $(module) device models */
  for ( ;model != NULL ;model = $(module)nextModel(model) )
  {
    for ( here = $(module)instances(model) ;here != NULL ; here = $(module)nextInstance(here) )
    {
<admst:for-each select="node[location='external']">
        if (here->%(name)Node_mine) {
            if (here->%(name)Node > 0)
                CKTdltNNum(ckt, here->%(name)Node);
            here->%(name)Node_mine = 0;
        }
        if (!here->%(name)Node_connected)
            here->%(name)Node = -1;
</admst:for-each>
<admst:for-each select="reverse(node[location='internal'])">
        if (here->%(name)Node_mine)
            if (here->%(name)Node > 0)
                CKTdltNNum(ckt, here->%(name)Node);
        here->%(name)Node = -1;
        here->%(name)Node_mine = 0;
</admst:for-each>
    }
  }
  return OK;
}

</admst:template>

<admst:for-each select="/module">
  <admst:value-of select="attribute[name='ngspicename']/value"/>
  <admst:open file="%ssetup.c">
    <admst:text format="/***\n*** Interface: %(/simulator/package_string)\n"/>
    <admst:text format=" *** created by: %(/simulator/fullname) ***/\n"/>
    <admst:apply-templates select="." match="code"/>
  </admst:open>
  <admst:value-of select="attribute[name='ngspicename']/value"/>
  <admst:message format="%ssetup.c: file created\n"/>
</admst:for-each>

</admst>
