<?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 
      */
{
  register $(module)model *model = ($(module)model*)inModel;
  register $(module)instance *here;
  
  /*  loop through all the $(module) device models */
  for ( ;model != NULL ;model = model->$(module)nextModel )
  {
<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 = model->$(module)instances ;here != NULL ; here = here->$(module)nextInstance )
    {
<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>
<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;
</admst:for-each>
      }
</admst:if>
      /* 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='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"/>
<admst:value-of select="name"/>
        if(here->%sNode == -1)
        {
          error=CKTmkVolt(ckt,&amp;tmp,here-&gt;$(module)name,&quot;$info&quot;);
          if(error) return(error);
          here->%sNode = tmp->number;
        }
</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);
}

</admst:template>

<admst:for-each select="/module">
  <admst:value-of select="attribute[name='ngspicename']/value"/>
  <admst:open file="%ssetup.c">
    <admst:value-of select="/simulator/package_string"/>
    <admst:text format="/***\n*** Interface: %s\n"/>
    <admst:value-of select="/simulator/currentdate"/>
    <admst:value-of select="/simulator/fullname"/>
    <admst:text format=" *** created by: %s - %s ***/\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>
