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

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

<admst:template match=":new:jacobian">
  <admst:value-of select="module"/>
  <admst:new datatype="jacobian" arguments="%p,%p,%p">
    <admst:push into="module/jacobian" select="." onduplicate="ignore"/>
    <admst:value-of select="column"/>
    <admst:if test="[row='%p']">
      <admst:value-to select="diagonal" value="yes"/>
    </admst:if>
    <admst:if test="../../..[dynamic='yes']">
      <admst:value-to select="dynamic" value="yes"/>
    </admst:if>
    <admst:if test="../../..[dynamic='no']">
      <admst:value-to select="static" value="yes"/>
    </admst:if>
  </admst:new>
</admst:template>

<admst:template match="expression:stringify:noprobe">
  <admst:apply-templates select="tree" match="subexpression:differentiate"/>
  <admst:value-of select="/simulator/tmp"/>
</admst:template>
<admst:template match="subexpression:stringify:noprobe">
  <admst:apply-templates select="." match="subexpression:differentiate"/>
  <admst:value-of select="/simulator/tmp"/>
</admst:template>
<admst:template match="subexpression:differentiate">
  <admst:value-of select="./adms/datatypename"/>
  <admst:apply-templates select="." match="%s"/>
  <admst:if test="/simulator/probe">
    <admst:choose>
      <admst:when test="adms[datatypename='probe']">
        <admst:choose>
          <admst:when test="[.=/simulator/probe]">
            <admst:value-to select="/simulator/ddx" value="1.0"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:otherwise>
        </admst:choose>
      </admst:when>
      <admst:when test="adms[datatypename='variable']">
        <admst:value-of select="probe"/>
        <admst:if-not-inside select="/simulator/probe" list="%p">
          <admst:value-to select="/simulator/ddx" value="0.0"/>
        </admst:if-not-inside>
        <admst:value-of select="probe"/>
        <admst:if-inside select="/simulator/probe" list="%p">
          <admst:choose>
            <admst:when test="[insource='yes']">
              <admst:value-of select="/simulator/probe/branch/nnode/name"/>
              <admst:value-of select="/simulator/probe/branch/pnode/name"/>
              <admst:value-of select="/simulator/probe/nature/access"/>
              <admst:value-of select="name"/>
              <admst:value-to select="/simulator/ddx" value="%s_%s%s_%s"/>
            </admst:when>
            <admst:otherwise>
              <admst:value-to select="/simulator/ddx" value="0.0"/>
            </admst:otherwise>
          </admst:choose>
        </admst:if-inside>
      </admst:when>
      <admst:when test="adms[ datatypename='number' or datatypename='variable']">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:when>
    </admst:choose>
  </admst:if>
</admst:template>

<!-- mapply_unary-->
<admst:template match="mapply_unary">
  <admst:choose>
    <admst:when test="[name='plus']">
      <admst:choose>
        <admst:when test="arg1/math[value=0.0]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(+%s)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='minus']">
      <admst:choose>
        <admst:when test="arg1/math[value=0.0]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(-%s)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='not']">
      <admst:choose>
        <admst:when test="arg1/math[value=0.0]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(!%s)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='bw_not']">
      <admst:choose>
        <admst:when test="arg1/math[value=0.0]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(~%s)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="name"/>
      <admst:error format="%s: function not handled\n"/>
    </admst:otherwise>
  </admst:choose>
  <admst:if test="/simulator/probe">
    <admst:choose>
      <admst:when test="/simulator[tmp='0.0']">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:when>
      <admst:otherwise>
        <admst:choose>
          <admst:when test="[name='plus']">
            <admst:choose>
              <admst:when test="/simulator[ddx!='0.0']">
                <admst:value-of select="/simulator/ddx"/>
                <admst:value-to select="/simulator/ddx" value="(+%s)"/>
              </admst:when>
            </admst:choose>
          </admst:when>
          <admst:when test="[name='minus']">
            <admst:choose>
              <admst:when test="/simulator[ddx!='0.0']">
                <admst:value-of select="/simulator/ddx"/>
                <admst:value-to select="/simulator/ddx" value="(-%s)"/>
              </admst:when>
            </admst:choose>
          </admst:when>
          <admst:when test="[name='not']">
            <admst:choose>
              <admst:when test="/simulator[ddx!='0.0']">
                <admst:value-of select="/simulator/ddx"/>
                <admst:value-to select="/simulator/ddx" value="(!%s)"/>
              </admst:when>
            </admst:choose>
          </admst:when>
          <admst:when test="[name='bw_not']">
            <admst:choose>
              <admst:when test="/simulator[ddx!='0.0']">
                <admst:value-of select="/simulator/ddx"/>
                <admst:value-to select="/simulator/ddx" value="(~%s)"/>
              </admst:when>
            </admst:choose>
          </admst:when>
        </admst:choose>
      </admst:otherwise>
    </admst:choose>
  </admst:if>
</admst:template>

<!-- mapply_binary !-->
<admst:template match="mapply_binary">
  <admst:choose>
    <admst:when test="[name='bw_equr']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s^~%s)"/>
    </admst:when>
    <admst:when test="[name='bw_equl']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s~^%s)"/>
    </admst:when>
    <admst:when test="[name='bw_xor']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s^%s)"/>
    </admst:when>
    <admst:when test="[name='bw_or']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s|%s)"/>
    </admst:when>
    <admst:when test="[name='bw_and']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&amp;%s)"/>
    </admst:when>
    <admst:when test="[name='multmod']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s%%%s)"/>
    </admst:when>
    <admst:when test="[name='or']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s||%s)"/>
    </admst:when>
    <admst:when test="[name='and']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&amp;&amp;%s)"/>
    </admst:when>
    <admst:when test="[name='equ']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s==%s)"/>
    </admst:when>
    <admst:when test="[name='notequ']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s!=%s)"/>
    </admst:when>
    <admst:when test="[name='lt']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&lt;%s)"/>
    </admst:when>
    <admst:when test="[name='lt_equ']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&lt;=%s)"/>
    </admst:when>
    <admst:when test="[name='gt']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&gt;%s)"/>
    </admst:when>
    <admst:when test="[name='gt_equ']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&gt;=%s)"/>
    </admst:when>
    <admst:when test="[name='shiftr']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&gt;&gt;%s)"/>
    </admst:when>
    <admst:when test="[name='shiftl']">
      <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
      <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
      <admst:value-to select="/simulator/tmp" value="(%s&lt;&lt;%s)"/>
    </admst:when>
    <admst:when test="[name='addp']">
      <admst:choose>
        <admst:when test="[(arg1/math/value=0.0)and(arg2/math/value=0.0)]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="arg1/math[value=0.0]">
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(+%s)"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
            <admst:variable name="dx" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="arg2/math[value=0.0]">
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:variable name="x" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
          </admst:if>
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:variable name="y" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
          </admst:if>
          <admst:value-to select="/simulator/tmp" value="($x+$y)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='addm']">
      <admst:choose>
        <admst:when test="[(arg1/math/value=0.0)and(arg2/math/value=0.0)]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="arg1/math[value=0.0]">
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="(-%s)"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
            <admst:variable name="dx" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="arg2/math[value=0.0]">
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:value-to select="/simulator/tmp" value="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:variable name="x" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
          </admst:if>
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:variable name="y" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
          </admst:if>
          <admst:value-to select="/simulator/tmp" value="($x-$y)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='multtime']">
      <admst:variable name="x" select="0.0"/>
      <admst:variable name="y" select="0.0"/>
      <admst:choose>
        <admst:when test="[(arg1/math/value=0.0)or(arg2/math/value=0.0)]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="[(arg1/math/value=1.0)and(arg2/math/value=1.0)]">
          <admst:value-to select="/simulator/tmp" value="1.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:variable name="x" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
          </admst:if>
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
          </admst:if>
          <admst:variable name="y" select="%s"/>
          <admst:value-to select="/simulator/tmp" value="($x*$y)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='multdiv']">
      <admst:variable name="x" select="0.0"/>
      <admst:variable name="y" select="0.0"/>
      <admst:choose>
        <admst:when test="arg1/math[value=0.0]">
          <admst:value-to select="/simulator/tmp" value="0.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:when test="[(arg1/math/value=1.0)and(arg2/math/value=1.0)]">
          <admst:value-to select="/simulator/tmp" value="1.0"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dx" select="0.0"/>
            <admst:variable name="dy" select="0.0"/>
          </admst:if>
        </admst:when>
        <admst:otherwise>
          <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
          <admst:variable name="x" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dx" select="%s"/>
          </admst:if>
          <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
          <admst:variable name="y" select="%s"/>
          <admst:if test="/simulator/probe">
            <admst:value-of select="/simulator/ddx"/>
            <admst:variable name="dy" select="%s"/>
          </admst:if>
          <admst:value-to select="/simulator/tmp" value="($x/$y)"/>
        </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="name"/>
      <admst:error format="%s: function not handled\n"/>
    </admst:otherwise>
  </admst:choose>

  <admst:if test="/simulator/probe">
    <admst:choose>
      <admst:when test="[name='addp']">
        <admst:choose>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:value-to select="/simulator/ddx" value="(+$dy)"/>
          </admst:when>
          <admst:when test="[$dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="$dx"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-to select="/simulator/ddx" value="($dx+$dy)"/>
          </admst:otherwise>
        </admst:choose>
      </admst:when>
      <admst:when test="[name='addm']">
        <admst:choose>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:value-to select="/simulator/ddx" value="(-$dy)"/>
          </admst:when>
          <admst:when test="[$dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="$dx"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-to select="/simulator/ddx" value="($dx-$dy)"/>
          </admst:otherwise>
        </admst:choose>
      </admst:when>
      <admst:when test="[name='multtime']">
        <admst:choose>
          <admst:when test="[$x='0.0' and $y='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0' and $dy='1.0']">
            <admst:value-to select="/simulator/ddx" value="($x)"/>
          </admst:when>
          <admst:when test="[$dx='1.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="($y)"/>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:value-to select="/simulator/ddx" value="($x*$dy)"/>
          </admst:when>
          <admst:when test="[$dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="$dx*$y"/>
          </admst:when>
          <admst:when test="[$dx='1.0' and $dy='1.0']">
            <admst:value-to select="/simulator/ddx" value="($x+$y)"/>
          </admst:when>
          <admst:when test="[$dx='1.0']">
            <admst:value-to select="/simulator/ddx" value="($y+($dy*$x))"/>
          </admst:when>
          <admst:when test="[$dy='1.0']">
            <admst:value-to select="/simulator/ddx" value="($dx*$y)+$x"/>
          </admst:when>
          <admst:when test="[$x='1.0']">
            <admst:value-to select="/simulator/ddx" value="$dy"/>
          </admst:when>
          <admst:when test="[$y='1.0']">
            <admst:value-to select="/simulator/ddx" value="$dx"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-to select="/simulator/ddx" value="(($dx*$y)+($x*$dy))"/>
          </admst:otherwise>
        </admst:choose>
      </admst:when>
      <admst:when test="[name='multdiv']">
        <admst:choose>
          <admst:when test="[$x='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$x='1.0']">
            <admst:choose>
              <admst:when test="[$dy='1.0']">
                <admst:value-to select="/simulator/ddx" value="(-1/($y*$y))"/>
              </admst:when>
              <admst:otherwise>
                <admst:value-to select="/simulator/ddx" value="(-$dy/($y*$y))"/>
              </admst:otherwise>
            </admst:choose>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:choose>
              <admst:when test="[$dy='1.0']">
                <admst:value-to select="/simulator/ddx" value="(-$x/($y*$y))"/>
              </admst:when>
              <admst:otherwise>
                <admst:value-to select="/simulator/ddx" value="(-($x*$dy)/($y*$y))"/>
              </admst:otherwise>
            </admst:choose>
          </admst:when>
          <admst:when test="[$dx='1.0']">
            <admst:choose>
              <admst:when test="[$dy='0.0']">
                <admst:value-to select="/simulator/ddx" value="(1/$y)"/>
              </admst:when>
              <admst:when test="[$dy='1.0']">
                <admst:value-to select="/simulator/ddx" value="(($y-$x)/($y*$y))"/>
              </admst:when>
              <admst:otherwise>
                <admst:value-to select="/simulator/ddx" value="(($y-($x*$dy))/($y*$y))"/>
              </admst:otherwise>
            </admst:choose>
          </admst:when>
          <admst:otherwise>
            <admst:choose>
              <admst:when test="[$y='1.0']">
                <admst:value-to select="/simulator/ddx" value="$dx"/>
              </admst:when>
              <admst:when test="[$dy='0.0']">
                <admst:value-to select="/simulator/ddx" value="$dx/$y"/>
              </admst:when>
              <admst:when test="[$dy='1.0']">
                <admst:value-to select="/simulator/ddx" value="(($dx*$y)-$x)/($y*$y)"/>
              </admst:when>
              <admst:otherwise>
                <admst:value-to select="/simulator/ddx" value="($dx*$y-$x*$dy)/($y*$y)"/>
              </admst:otherwise>
            </admst:choose>
          </admst:otherwise>
        </admst:choose>
      </admst:when>
      <admst:otherwise>
        <admst:value-to select="/simulator/ddx" value=""/>
      </admst:otherwise>
    </admst:choose>
  </admst:if>

</admst:template>

<!-- mapply_ternary-->
<admst:template match="mapply_ternary">
  <admst:apply-templates select="arg3" match="subexpression:stringify:noprobe"/>
  <admst:value-of select="/simulator/ddx"/>
  <admst:variable name="dz" select="%s"/>
  <admst:apply-templates select="arg2" match="subexpression:stringify:noprobe"/>
  <admst:value-of select="/simulator/ddx"/>
  <admst:variable name="dy" select="%s"/>
  <admst:apply-templates select="arg1" match="subexpression:stringify:noprobe"/>
  <admst:variable name="x" select="%s"/>
  <admst:value-to select="/simulator/tmp" value="($x?%s:%s)"/>
  <admst:if test="/simulator/probe">
    <admst:value-to select="/simulator/ddx" value="($x?$dy:$dz)"/>
  </admst:if>
</admst:template>

<!-- functions-->
<admst:template match="function:assert:noarg">
  <admst:if test="[not(nilled(arguments))]">
    <admst:value-of select="name"/>
    <admst:error format="%s: should not have arguments\n"/>
  </admst:if>
</admst:template>
<admst:template match="function:assert:onearg">
  <admst:if test="arguments[not(count(.)=1)]">
    <admst:value-of select="name"/>
    <admst:error format="%s: should have one argument exactly\n"/>
  </admst:if>
</admst:template>

<!-- expression//function: mapping verilog-name == C-name of function -->
<admst:template match="function:getname">
  <admst:choose>
    <admst:when test="[name='abs']"><admst:value-of select="'fabs'"/></admst:when>
    <admst:when test="[name='log']"><admst:value-of select="'log10'"/></admst:when>
    <admst:when test="[name='ln']"><admst:value-of select="'logE'"/></admst:when>
    <admst:when test="[name='limexp']"><admst:value-of select="'limexp'"/></admst:when>
    <admst:when test="[name='\$limexp']"><admst:value-of select="'limexp'"/></admst:when>
    <admst:when test="[name='\$model']"><admst:value-of select="'_modelname'"/></admst:when>
    <admst:when test="[name='\$instance']"><admst:value-of select="'_instancename'"/></admst:when>
    <admst:when test="[name='\$temperature']"><admst:value-of select="'_circuit_temp'"/></admst:when>
    <admst:otherwise><admst:value-of select="name"/></admst:otherwise>
  </admst:choose>
</admst:template>

<admst:template match="function">
  <admst:choose>
    <admst:when test="[name='ddt']">
      <admst:for-each select="arguments[position(.)=1]">
        <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
        <admst:value-to select="/simulator/tmp" value="%s"/>
      </admst:for-each>
    </admst:when>
    <admst:when test="[name='\$given']">
      <admst:for-each select="arguments[position(.)=1]">
        <admst:if test="adms[datatypename!='variable']">
          <admst:error format="$given: argument is not a variable\n"/>
        </admst:if>
        <admst:if test="[input='no']">
          <admst:value-of select="name"/>
          <admst:error format="$given(%s): argument is not a parameter\n"/>
        </admst:if>
        <admst:choose>
          <admst:when test="[parametertype='model']">
            <admst:value-of select="name"/>
            <admst:value-to select="/simulator/tmp" value="model->%s_Given"/>
          </admst:when>
          <admst:when test="[parametertype='instance']">
            <admst:value-of select="name"/>
            <admst:value-to select="/simulator/tmp" value="here->%s_Given"/>
          </admst:when>
          <admst:otherwise>
            <admst:error format="$given(%s): should not be reached\n"/>
          </admst:otherwise>
        </admst:choose>
      </admst:for-each>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$model']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="%s"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$instance']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="%s"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$temperature']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="%s"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$nominal_temperature']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="%s"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$vt']">
      <admst:choose>
       <admst:when test="arguments">
        <admst:choose>
         <admst:when test="arguments[count(.)=1]">
          <admst:apply-templates select="." match="function:assert:onearg"/>
          <admst:for-each select="arguments[position(.)=1]">
            <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
            <admst:value-to select="/simulator/tmp" value="_vt(%s)"/>
          </admst:for-each>
         </admst:when>
         <admst:otherwise>
           <admst:error format="$vt(...): too many args"/>   
         </admst:otherwise>
        </admst:choose>
       </admst:when>
       <admst:otherwise>
         <admst:apply-templates select="." match="function:assert:noarg"/>
         <admst:value-to select="/simulator/tmp" value="_vt_nom"/>   
       </admst:otherwise>
      </admst:choose>
    </admst:when>
    <admst:when test="[name='\$scale']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:value-to select="/simulator/tmp" value="_scale"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$abstime']">
      <admst:apply-templates select="." match="function:assert:noarg"/>
      <admst:value-to select="/simulator/tmp" value="_abstime"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='\$options']">
      <admst:for-each select="arguments[position(.)=1]">
        <admst:if test="adms[datatypename!='string']">
          <admst:error format="$given: argument is not a string\n"/>
        </admst:if>
        <admst:choose>
          <admst:when test="[value='OPTm_hier']">
            <admst:value-to select="/simulator/tmp" value="_circuit_m_hier"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-of select="value"/>
            <admst:fatal format="$options(%s): bad argument []\n"/>
          </admst:otherwise>
        </admst:choose>
      </admst:for-each>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='ddx' or name='\$derivate']">
      <admst:for-each select="arguments"> 
        <admst:if test="[position(.)=2]">
          <admst:if test="adms[datatypename!='probe']">
            <admst:value-of select="../name"/>
            <admst:error format="%s: second argument is not a probe\n"/>
          </admst:if>
          <admst:value-of select="branch/nnode/name"/>
          <admst:value-of select="branch/pnode/name"/>
          <admst:value-of select="nature/access"/>
        </admst:if>
      </admst:for-each>
      <admst:for-each select="arguments"> 
        <admst:if test="[position(.)=1]">
          <admst:if test="adms[datatypename!='variable']">
            <admst:value-of select="../name"/>
            <admst:error format="%s: first argument is not a variable\n"/>
          </admst:if>
          <admst:value-of select="name"/>
        </admst:if>
      </admst:for-each>
      <admst:value-to select="/simulator/tmp" value="%s_%s%s_%s"/>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='floor']">
      <admst:apply-templates select="." match="function:assert:onearg"/>
      <admst:for-each select="arguments[position(.)=1]">
        <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
        <admst:value-to select="/simulator/tmp" value="floor(%s)"/>
      </admst:for-each>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='ceil']">
      <admst:apply-templates select="." match="function:assert:onearg"/>
      <admst:for-each select="arguments[position(.)=1]">
        <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
        <admst:value-to select="/simulator/tmp" value="ceil(%s)"/>
      </admst:for-each>
      <admst:if test="/simulator/probe">
        <admst:value-to select="/simulator/ddx" value="0.0"/>
      </admst:if>
    </admst:when>
    <admst:when test="[name='pow' or name='hypot' or name='min' or name='max']">
      <admst:value-of select="index(./subexpression/expression/function,.)"/>
      <admst:variable name="index" select="%s"/>
      <admst:if test="/simulator/probe">
        <admst:for-each select="arguments"> 
          <admst:choose>
            <admst:when test="[position(.)=1]">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="x" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dx" select="%s"/>
            </admst:when>
            <admst:when test="[position(.)=2]">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="y" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dy" select="%s"/>
            </admst:when>
            <admst:otherwise>
              <admst:count select="../arguments"/>
              <admst:value-of select="../name"/>
              <admst:error format="%s(...): two arguments expected - %s found(s) \n"/>
            </admst:otherwise>
          </admst:choose>
        </admst:for-each>
        <admst:choose>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:value-of select="name"/>
            <admst:value-to select="/simulator/ddx" value="(__dFy_%s_$index*$dy)"/>
          </admst:when>
          <admst:when test="[$dy='0.0']">
            <admst:value-of select="name"/>
            <admst:value-to select="/simulator/ddx" value="(__dFx_%s_$index*$dx)"/>
          </admst:when>
          <admst:otherwise>
            <admst:apply-templates select="." match="function:getname"/>
            <admst:apply-templates select="." match="function:getname"/>
            <admst:value-to select="/simulator/ddx" value="(__dFx_%s_$index*$dx+__dFy_%s_$index*$dy)"/>
          </admst:otherwise>
        </admst:choose>
      </admst:if>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="__%s_$index"/>
    </admst:when>
    <admst:when test="[name='div']">
      <admst:value-of select="index(./subexpression/expression/function,.)"/>
      <admst:variable name="index" select="%s"/>
      <admst:if test="/simulator/probe">
        <admst:for-each select="arguments"> 
          <admst:choose>
            <admst:when test="[position(.)=1]">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="x" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dx" select="%s"/>
            </admst:when>
            <admst:when test="[position(.)=2]">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="y" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dy" select="%s"/>
            </admst:when>
            <admst:otherwise>
              <admst:count select="../arguments"/>
              <admst:value-of select="../name"/>
              <admst:error format="%s(...): two arguments expected - %s found(s) \n"/>
            </admst:otherwise>
          </admst:choose>
        </admst:for-each>
        <admst:choose>
          <admst:when test="[$dx='0.0' and $dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:when test="[$dx='0.0']">
            <admst:value-to select="/simulator/ddx" value="(__dFy_%(name)_$index*$dy)"/>
          </admst:when>
          <admst:when test="[$dy='0.0']">
            <admst:value-to select="/simulator/ddx" value="(__dFx_%(name)_$index*$dx)"/>
          </admst:when>
          <admst:otherwise>
            <admst:value-to select="/simulator/ddx" value="(__dFx_%(name)_$index*$dx+__dFy_%(name)_$index*$dy)"/>
          </admst:otherwise>
        </admst:choose>
      </admst:if>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="__%s_$index"/>
    </admst:when>
    <admst:when test="[class='builtin']">
      <admst:value-of select="index(./subexpression/expression/function,.)"/>
      <admst:variable name="index" select="%s"/>
      <admst:if test="/simulator/probe">
        <admst:for-each select="arguments"> 
          <admst:choose>
            <admst:when test="[position(.)=1]">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="x" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dx" select="%s"/>
            </admst:when>
            <admst:otherwise>
              <admst:count select="../arguments"/>
              <admst:value-of select="../name"/>
              <admst:error format="%s(...): one argument expected - %s found(s) \n"/>
            </admst:otherwise>
          </admst:choose>
        </admst:for-each>
        <admst:choose>
          <admst:when test="[$dx='0.0']">
            <admst:value-to select="/simulator/ddx" value="0.0"/>
          </admst:when>
          <admst:otherwise>
            <admst:apply-templates select="." match="function:getname"/>
            <admst:value-to select="/simulator/ddx" value="$dx*__d_%s_$index"/>
          </admst:otherwise>
        </admst:choose>
      </admst:if>
      <admst:apply-templates select="." match="function:getname"/>
      <admst:value-to select="/simulator/tmp" value="__%s_$index"/>
    </admst:when>
    <admst:otherwise>
      <admst:choose>
        <admst:when test="[name='\$simparam']">
           <admst:apply-templates select="." match="function:simparam"/>
        </admst:when>
        <admst:when test="[name='analysis']">
           <admst:apply-templates select="." match="function:analysis"/>
        </admst:when>
        <admst:otherwise>
          <admst:value-of select="name"/>
          <admst:variable name="function" select="%s"/>
          <admst:variable name="args" select=""/>
          <admst:for-each select="arguments">
            <admst:value-of select="position(.)"/>
            <admst:variable name="index" select="%s"/>
            <admst:if test="[not($args='')]">
              <admst:variable name="args" select="$args,"/>
            </admst:if>
            <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
            <admst:variable name="arg$index" select="%s"/>
            <admst:variable name="args" select="$args$(arg$index)"/>
          </admst:for-each>
          <admst:value-to select="/simulator/tmp" value="$function($args)"/>
          <admst:if test="/simulator/probe">
            <admst:variable name="dargs" select="$args"/>
            <admst:for-each select="arguments">
              <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
              <admst:variable name="x" select="%s"/>
              <admst:value-of select="/simulator/ddx"/>
              <admst:variable name="dargs" select="$dargs,%s"/>
            </admst:for-each>
            <admst:value-to select="/simulator/ddx" value="d_$function($dargs)"/>
          </admst:if>
        </admst:otherwise>
      </admst:choose>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<admst:template match="function:analysis">
  <admst:value-of select="name"/>
  <admst:variable name="function" select="%s"/>
  <admst:variable name="args" select=""/>
  <admst:for-each select="arguments">
    <admst:value-of select="position(.)"/>
    <admst:variable name="index" select="%s"/>
    <admst:if test="[not($args='')]">
      <admst:variable name="args" select="$args,"/>
    </admst:if>
    <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
    <admst:variable name="arg$index" select="%s"/>
    <admst:variable name="args" select="$args$(arg$index)"/>
  </admst:for-each>
  <admst:choose>
    <admst:when test="[$arg1='&quot;noise&quot;']">
      <admst:value-to select="/simulator/tmp" value="0.0"/>
      <admst:error format="$function($args): replaced by 0.0\n"/>
    </admst:when>
    <admst:otherwise>
      <admst:error format="$function($args) -- not implemented in ngspice interface\n"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<admst:template match="function:simparam">
  <admst:value-of select="name"/>
  <admst:variable name="function" select="%s"/>
  <admst:variable name="args" select=""/>
  <admst:for-each select="arguments">
    <admst:value-of select="position(.)"/>
    <admst:variable name="index" select="%s"/>
    <admst:if test="[not($args='')]">
      <admst:variable name="args" select="$args,"/>
    </admst:if>
    <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
    <admst:variable name="arg$index" select="%s"/>
    <admst:variable name="args" select="$args$(arg$index)"/>
  </admst:for-each>
  <admst:choose>
    <admst:when test="[$arg1='&quot;gdev&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_gdev"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;gmin&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_gmin"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;imax&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_imax"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;imelt&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_imelt"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;iteration&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_iteration"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;scale&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_scale"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;shrink&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_shrink"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;simulatorSubversion&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_simulatorSubversion"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;simulatorVersion&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_simulatorVersion"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;sourceScaleFactor&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_sourceScaleFactor"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;tnom&quot;']">
      <admst:value-to select="/simulator/tmp" value="_circuit_tnom"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;checkjcap&quot;']">
      <admst:value-to select="/simulator/tmp" value="1.0"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;maxmosl&quot;']">
      <admst:value-to select="/simulator/tmp" value="1.0"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;maxmosw&quot;']">
      <admst:value-to select="/simulator/tmp" value="1.0"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;minmosl&quot;']">
      <admst:value-to select="/simulator/tmp" value="1.0e-12"/>
    </admst:when>
    <admst:when test="[$arg1='&quot;minmosw&quot;']">
      <admst:value-to select="/simulator/tmp" value="1.0e-12"/>
    </admst:when>
    <admst:otherwise>
      <admst:error format="$function($args) -- not implemented in ngspice interface\n"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>


<admst:template match="variable:declaration">
  <admst:for-each select="module/evaluation/variable">
    <admst:assert test="adms[datatypename='variable']" format="expecting datatypename=variable\n"/>
    <admst:if test="[scope='local']">
      <admst:if test="[static='no' and dynamic='yes']">#if defined(_DYNAMIC)\n</admst:if>
      <admst:value-of select="name"/>
      <admst:if test="[type='integer']">int %s;\n</admst:if>
      <admst:if test="[type='real']">double %s=0.0/0.0;\n</admst:if>
      <admst:if test="[type='string']">char* %s;\n</admst:if>
      <admst:if test="[insource='yes']">
        <admst:if test="probe">
          <admst:text format="#if defined(_DERIVATE)\n"/>
          <admst:for-each select="probe">
            <admst:value-of select="branch/nnode/name"/>
            <admst:value-of select="branch/pnode/name"/>
            <admst:value-of select="nature/access"/>
            <admst:value-of select="../name"/>
            <admst:text format="double %s_%s%s_%s=0.0;\n"/>
          </admst:for-each>
          <admst:text format="#endif /*_DERIVATE*/\n"/>
        </admst:if>
      </admst:if>
      <admst:if test="[static='no' and dynamic='yes']">#endif /*_DYNAMIC*/\n</admst:if>
    </admst:if>
    <admst:if test="[scope!='local']">
      <admst:if test="[insource='yes']">
        <admst:if test="probe">
          <admst:text format="#if defined(_DERIVATE)\n"/>
          <admst:for-each select="probe">
            <admst:value-of select="branch/nnode/name"/>
            <admst:value-of select="branch/pnode/name"/>
            <admst:value-of select="nature/access"/>
            <admst:value-of select="../name"/>
            <admst:text format="double %s_%s%s_%s=0.0;\n"/>
          </admst:for-each>
          <admst:text format="#endif /*_DERIVATE*/\n"/>
        </admst:if>
      </admst:if>
    </admst:if>
  </admst:for-each>
  <admst:reset select="module/evaluation/variable"/>
</admst:template>

<!-- save all variables used for local declaration -->
<admst:template match="block:local:declaration">
  <admst:choose>
    <admst:when test="adms[datatypename='assignment']">
      <admst:push into="module/evaluation/variable" select="lhs" onduplicate="ignore"/>
    </admst:when>
    <admst:when test="adms[datatypename='block']">
      <admst:for-each select="item">
        <admst:apply-templates select="." match="block:local:declaration" required="yes"/>
      </admst:for-each>
    </admst:when>
    <admst:when test="adms[datatypename='conditional']">
      <admst:apply-templates select="then" match="block:local:declaration" required="yes"/>
      <admst:apply-templates select="else" match="block:local:declaration" required="yes"/>
    </admst:when>
    <admst:when test="adms[datatypename='whileloop']">
      <admst:apply-templates select="whileblock" match="block:local:declaration" required="yes"/>
    </admst:when>
    <admst:when test="adms[datatypename='contribution']">
    </admst:when>
    <admst:when test="adms[datatypename='nilled']">
    </admst:when>
    <admst:when test="adms[datatypename='callfunction']">
    </admst:when>
    <admst:when test="adms[datatypename='case']">
      <admst:error format="case statement: please implement me! (local declaration)\n"/>
    </admst:when>
    <admst:when test="adms[datatypename='blockvariable']"/>
    <admst:otherwise>
      <admst:value-of select="admst(.)"/>
      <admst:value-of select="adms/datatypename"/>
      <admst:error format="'datatypename=%s': should not be reached %s\n"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<!-- analog//blockvariable -->
<admst:template match="blockvariable">
  <admst:for-each select="variable">
    <admst:if test="[type='integer']">int %(name);\n</admst:if>
    <admst:if test="[type='real']">double %(name);\n</admst:if>
    <admst:if test="[type='string']">char* %(name);\n</admst:if>
    <admst:text test="[insource='yes']" select="probe" format="double %(../name)_%(nature/access)%(branch/pnode/name)_%(branch/nnode/name);\n"/>
  </admst:for-each>
</admst:template>

<!-- analog//block -->
<admst:template match="block">
  <admst:assert test="[name!='/']" format="expecting subblock\n"/>
  <admst:text format="{\n"/>
  <admst:for-each select="item">
    <admst:value-of select="./adms/datatypename"/>
    <admst:apply-templates select="." match="%s" required="yes"/>
  </admst:for-each>
  <admst:text format="}\n"/>
</admst:template>

<!-- analog/[initializeModel|initializeInstance|initial_model|initial_instance|initial_step|noise] -->
<admst:template match="block:initial">
  <admst:assert test="adms[datatypename='block']" format="expecting datatypename=block\n"/>
  <admst:apply-templates select="." match="block:local:declaration"/>
  <admst:apply-templates select="." match="variable:declaration"/>
  <admst:apply-templates select="." match="block" required="yes"/>
</admst:template>
<admst:template match="analog:initial_instance">
  <admst:if test="code">
    <admst:if test="code/adms[datatypename='block']">
      <admst:for-each select="code/item">
        <admst:if test="adms[datatypename='block']">
          <admst:apply-templates select="[name='initial_instance' or name='initializeInstance']" match="block:initial"/>
        </admst:if>
      </admst:for-each>
    </admst:if>
  </admst:if>
</admst:template>
<admst:template match="analog:initial_model">
  <admst:if test="code">
    <admst:if test="code/adms[datatypename='block']">
      <admst:for-each select="code/item">
        <admst:if test="adms[datatypename='block']">
          <admst:apply-templates select="[name='initial_model' or name='initializeModel']" match="block:initial"/>
        </admst:if>
      </admst:for-each>
    </admst:if>
  </admst:if>
</admst:template>
<admst:template match="analog:initial_step">
  <admst:if test="code">
    <admst:if test="code/adms[datatypename='block']">
      <admst:for-each select="code/item">
        <admst:if test="adms[datatypename='block']">
          <admst:apply-templates select="[name='initial_step']" match="block:initial"/>
        </admst:if>
      </admst:for-each>
    </admst:if>
  </admst:if>
</admst:template>
<admst:template match="analog:noise">
  <admst:if test="code">
    <admst:if test="code/adms[datatypename='block']">
      <admst:for-each select="code/item">
        <admst:if test="adms[datatypename='block']">
          <admst:apply-templates select="[name='noise']" match="block:initial"/>
        </admst:if>
      </admst:for-each>
    </admst:if>
  </admst:if>
</admst:template>

<!-- analog//function: local assignment handling -->
<admst:template match="function:assignment">
  <admst:for-each select="function[class='builtin']">
    <admst:choose>
      <admst:when test="arguments[count(.)=1]">
        <admst:value-of select="position(.)-1"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:text format="_%s(__%s_%s,"/>
        <admst:join select="arguments" separator=",">
          <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
          <admst:text format="(%s)"/>
        </admst:join>
        <admst:text format=")\n"/>
        <admst:value-of select="position(.)-1"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:text format="EXIT_IF_ISNAN(__%s_%s)\n"/>
      </admst:when>
      <admst:when test="arguments[count(.)=2]">
        <admst:value-of select="position(.)-1"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:text format="_%s"/>
        <admst:text test="[name='div']" format="0"/>
        <admst:text format="(__%s_%s,"/>
        <admst:join select="arguments" separator=",">
          <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
          <admst:text format="%s"/>
        </admst:join>
        <admst:text format=")\n"/>
        <admst:value-of select="position(.)-1"/>
        <admst:apply-templates select="." match="function:getname"/>
        <admst:text format="EXIT_IF_ISNAN(__%s_%s)\n"/>
      </admst:when>
      <admst:otherwise>
        <admst:value-of select="name"/>
        <admst:error format="%s: function not handled\n"/>
      </admst:otherwise>
    </admst:choose>
  </admst:for-each>
</admst:template>

<!-- analog//function: ddx handling -->
<admst:template match="ddx:function:computation">
  <admst:if test="lhs[insource='yes']">
    <admst:if test="rhs[hasVoltageDependentFunction='yes']">
      <admst:text format="#if defined(_DERIVATE)\n"/>
      <admst:for-each select="rhs/function">
        <admst:if test="arguments[count(.)=1]">
          <admst:for-each select="arguments[position(.)=1]">
            <admst:if test="math[dependency!='constant']">
              <admst:value-of select="../position(.)-1"/>
              <admst:apply-templates select=".." match="function:getname"/>
              <admst:text format="double __d_%s_%s=0.0;\n"/>
            </admst:if>
          </admst:for-each>
        </admst:if>
        <admst:if test="arguments[count(.)=2]">
          <admst:for-each select="arguments">
            <admst:if test="[position(.)=1]">
              <admst:if test="[(../name='div') or (math/dependency!='constant')]">
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="double __dFx_%s_%s=0.0;\n"/>
              </admst:if>
            </admst:if>
            <admst:if test="[position(.)=2]">
              <admst:if test="math[dependency!='constant']">
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="double __dFy_%s_%s=0.0;\n"/>
              </admst:if>
            </admst:if>
          </admst:for-each>
        </admst:if>
      </admst:for-each>
      <admst:text format="#endif /* _DERIVATE */\n"/>
      <admst:text format="#if defined(_DERIVATE)\n"/>
      <admst:for-each select="rhs/function">
        <admst:if test="arguments[count(.)=1]">
          <admst:for-each select="arguments[position(.)=1]">
            <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
            <admst:choose>
              <admst:when test="math[dependency!='constant']">
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="_d_%s(__%s_%s,__d_%s_%s,(%s))\n"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="EXIT_IF_ISNAN(__%s_%s)\n"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="EXIT_IF_ISNAN(__d_%s_%s)\n"/>
              </admst:when>
              <admst:otherwise>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="_%s(__%s_%s,(%s))\n"/>
              </admst:otherwise>
            </admst:choose>
          </admst:for-each>
        </admst:if>
        <admst:if test="arguments[count(.)=2]">
          <admst:value-of select="./position(.)-1"/>
          <admst:apply-templates select="." match="function:getname"/>
          <admst:apply-templates select="." match="function:getname"/>
          <admst:text format="_%s(__%s_%s,"/>
          <admst:text test="[name='div']" format="__dFx_%(name)_%(position(.)-1),"/>
          <admst:join select="arguments" separator=",">
            <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
            <admst:text format="%s"/>
          </admst:join>
          <admst:text format=")\n"/>
          <admst:for-each select="arguments">
            <admst:if test="[position(.)=1]">
              <admst:if test="math[dependency!='constant']">
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="_dx_%s(__dFx_%s_%s,__%s_%s,"/>
                <admst:join select="../arguments" separator=",">
                  <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
                  <admst:text format="%s"/>
                </admst:join>
                <admst:text format=")\n"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="EXIT_IF_ISNAN(__dFx_%s_%s)\n"/>
              </admst:if>
            </admst:if>
            <admst:if test="[position(.)=2]">
              <admst:if test="math[dependency!='constant']">
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="_dy_%s(__dFy_%s_%s,"/>
                <admst:text test="[../name='div']" format="__dFx_%(../name)_%(../position(.)-1),"/>
                <admst:text format="__%s_%s,"/>
                <admst:join select="../arguments" separator=",">
                  <admst:apply-templates select="." match="subexpression:stringify:noprobe"/>
                  <admst:text format="%s"/>
                </admst:join>
                <admst:text format=")\n"/>
                <admst:value-of select="../position(.)-1"/>
                <admst:apply-templates select=".." match="function:getname"/>
                <admst:text format="EXIT_IF_ISNAN(__dFy_%s_%s)\n"/>
              </admst:if>
            </admst:if>
          </admst:for-each>
          <admst:value-of select="position(.)-1"/>
          <admst:apply-templates select="." match="function:getname"/>
          <admst:text format="EXIT_IF_ISNAN(__%s_%s)\n"/>
        </admst:if>
      </admst:for-each>
      <admst:text format="#else\n"/>
    </admst:if>
  </admst:if>
  <admst:apply-templates select="rhs" match="function:assignment"/>
  <admst:if test="lhs[insource='yes']">
    <admst:if test="rhs[hasVoltageDependentFunction='yes']">
      <admst:text format="#endif\n"/>
    </admst:if>
  </admst:if>
</admst:template>

<!-- analog//assignment -->
<admst:template match="assignment">
  <admst:if test="rhs[not(nilled(function[class='builtin']))]">
    <admst:choose>
      <admst:when test="[dynamic='yes']">
        <admst:text format="#if defined(_DYNAMIC)\n"/>
      </admst:when>
    </admst:choose>
    <admst:text format="{\n"/>
    <admst:for-each select="rhs/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="." match="ddx:function:computation"/>
  </admst:if>
  <admst:if test="lhs[derivate='yes']">
    <admst:text format="#if defined(_DERIVATE)\n"/>
  </admst:if>
  <admst:if test="lhs[insource='yes']">
    <admst:if test="rhs/probe">
      <admst:text format="#if defined(_DERIVATE)\n"/>
      <admst:for-each select="rhs/probe">
        <admst:value-of select="."/>
        <admst:value-to select="/simulator/probe" value="%p"/>
        <admst:apply-templates select="../tree" match="subexpression:differentiate"/>
        <admst:value-of select="/simulator/ddx"/>
        <admst:value-of select="branch/nnode/name"/>
        <admst:value-of select="branch/pnode/name"/>
        <admst:value-of select="nature/access"/>
        <admst:value-of select="../../lhs/name"/>
        <admst:text format="%s_%s%s_%s=%s;\n"/>
        <admst:value-of select="branch/nnode/name"/>
        <admst:value-of select="branch/pnode/name"/>
        <admst:value-of select="nature/access"/>
        <admst:value-of select="../../lhs/name"/>
        <admst:text format="EXIT_IF_ISNAN(%s_%s%s_%s)\n"/>
      </admst:for-each>
      <admst:text format="#endif /*_DERIVATE*/\n"/>
    </admst:if>
  </admst:if>
  <admst:choose>
    <admst:when test="[dynamic='yes']">
      <admst:text format="#if defined(_DYNAMIC)\n"/>
    </admst:when>
  </admst:choose>
  <admst:apply-templates select="lhs" match="variable:lhs" required="yes"/>
  <admst:apply-templates select="rhs" match="expression:stringify:noprobe"/>
  <admst:text format="=%s;\n"/>
  <admst:text format="EXIT_IF_ISNAN("/>
  <admst:apply-templates select="lhs" match="variable:lhs" required="yes"/>
  <admst:text format=")\n"/>
  <admst:choose>
    <admst:when test="[dynamic='yes']">
      <admst:text format="#endif /*_DYNAMIC*/\n"/>
    </admst:when>
  </admst:choose>
  <admst:if test="lhs[derivate='yes']">
    <admst:text format="#endif /*_DERIVATE*/\n"/>
  </admst:if>
  <admst:if test="lhs[insource='yes']">
    <admst:value-of select="rhs/probe"/>
    <admst:if-inside select="lhs/probe" list="%p">
      <admst:if test="lhs/probe">
        <admst:text format="#if defined(_DERIVATE)\n"/>
        <admst:for-each select="lhs/probe">
          <admst:value-of select="../../rhs/probe"/>
          <admst:if-not-inside select="." list="%p">
            <admst:value-of select="branch/nnode/name"/>
            <admst:value-of select="branch/pnode/name"/>
            <admst:value-of select="nature/access"/>
            <admst:value-of select="../name"/>
            <admst:text format="%s_%s%s_%s=0.0;\n"/>
          </admst:if-not-inside>
        </admst:for-each>
        <admst:text format="#endif /*_DERIVATE*/\n"/>
      </admst:if>
    </admst:if-inside>
  </admst:if>
  <admst:if test="rhs[not(nilled(function[class='builtin']))]">
    <admst:text format="}\n"/>
    <admst:choose>
      <admst:when test="[dynamic='yes']">
        <admst:text format="#endif /* _DYNAMIC */\n"/>
      </admst:when>
    </admst:choose>
  </admst:if>
</admst:template>

<!-- analog//contribution -->
<admst:template match="contribution">
  <admst:choose>
    <admst:when test="[whitenoise='no' and flickernoise='no']">
      <admst:apply-templates select="." match="contribution:nonoise" required="yes"/>
    </admst:when>
    <admst:otherwise>
      <admst:apply-templates select="." match="contribution:noise" required="yes"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<admst:template match="contribution:nonoise">
  <admst:if test="rhs[not(nilled(function[class='builtin']))]">
    <admst:choose>
      <admst:when test="[dynamic='yes']">
        <admst:text format="#if defined(_DYNAMIC)\n"/>
      </admst:when>
    </admst:choose>
    <admst:text format="{\n"/>
    <admst:for-each select="rhs/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="." match="ddx:function:computation"/>
  </admst:if>
  <admst:apply-templates select="rhs" match="expression:stringify:noprobe"/>
  <admst:choose>
    <admst:when test="[dynamic='yes']">
      <admst:text format="  _load_dynamic_"/>
    </admst:when>
    <admst:otherwise>
      <admst:text format="  _load_static_"/>
    </admst:otherwise>
  </admst:choose>
  <admst:choose>
    <admst:when test="lhs/branch/nnode[grounded='no']">
      <admst:value-of select="lhs/branch/nnode/name"/>
      <admst:value-of select="lhs/branch/pnode/name"/>
      <admst:text format="residual2(%s,%s,%s)\n"/>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="lhs/branch/pnode/name"/>
      <admst:text format="residual1(%s,%s)\n"/>
    </admst:otherwise>
  </admst:choose>
  <admst:for-each select="rhs/probe">
    <admst:value-of select="."/>
    <admst:value-to select="/simulator/probe" value="%p"/>
    <admst:apply-templates select="../tree" match="subexpression:differentiate"/>
    <admst:value-of select="/simulator/ddx"/>
    <admst:choose>
      <admst:when test="..[dynamic='yes']">
        <admst:text format="  _load_dynamic_"/>
      </admst:when>
      <admst:otherwise>
        <admst:text format="  _load_static_"/>
      </admst:otherwise>
    </admst:choose>
    <admst:if test="branch/pnode[grounded='no']">
      <admst:if test="../../lhs/branch/pnode[grounded='no']">
        <admst:choose>
          <admst:when test="branch/nnode[grounded='no']">
            <admst:if test="../../lhs/branch/nnode[grounded='no']">
              <admst:value-of select="branch/nnode/name"/>
              <admst:value-of select="branch/pnode/name"/>
              <admst:value-of select="../../lhs/branch/nnode/name"/>
              <admst:value-of select="../../lhs/branch/pnode/name"/>
              <admst:text format="jacobian4(%s,%s,%s,%s,%s)\n"/>
            </admst:if>
            <admst:if test="../../lhs/branch/nnode[grounded='yes']">
              <admst:value-of select="branch/nnode/name"/>
              <admst:value-of select="branch/pnode/name"/>
              <admst:value-of select="../../lhs/branch/pnode/name"/>
              <admst:text format="jacobian2p(%s,%s,%s,%s)\n"/>
            </admst:if>
          </admst:when>
          <admst:otherwise>
            <admst:if test="../../lhs/branch/nnode[grounded='no']">
              <admst:value-of select="branch/pnode/name"/>
              <admst:value-of select="../../lhs/branch/nnode/name"/>
              <admst:value-of select="../../lhs/branch/pnode/name"/>
              <admst:text format="jacobian2s(%s,%s,%s,%s)\n"/>
            </admst:if>
            <admst:if test="../../lhs/branch/nnode[grounded='yes']">
              <admst:value-of select="branch/pnode/name"/>
              <admst:value-of select="../../lhs/branch/pnode/name"/>
              <admst:text format="jacobian1(%s,%s,%s)\n"/>
            </admst:if>
          </admst:otherwise>
        </admst:choose>
      </admst:if>
    </admst:if>
  </admst:for-each>
  <admst:if test="rhs[not(nilled(function[class='builtin']))]">
    <admst:text format="}\n"/>
    <admst:if test="[dynamic='yes']">
      <admst:text format="#endif /* _DYNAMIC */\n"/>
    </admst:if>
  </admst:if>
</admst:template>

<!-- analog//conditional -->
<admst:template match="conditional">
  <admst:if test="if[dynamic='yes']">
    <admst:choose>
      <admst:when test="[nilled(else)]">
        <admst:text format="#ifdef _DYNAMIC /*&lt;dynamic_ifthen&gt;*/\n"/>
      </admst:when>
      <admst:otherwise>
        <admst:text format="#ifdef _DYNAMIC /*&lt;dynamic_ifthenelse&gt;*/\n"/>
      </admst:otherwise>
    </admst:choose>
  </admst:if>
  <admst:if test="if[not(nilled(function[class='builtin']))]">
    <admst:text format="{\n"/>
    <admst:for-each select="if/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="if" match="function:assignment"/>
  </admst:if>
  <admst:apply-templates select="if" match="expression:stringify:noprobe"/>
  <admst:text format="if\n(%s)\n"/>
  <admst:if test="then/adms[datatypename!='block']">
    <admst:text format="{\n"/>
  </admst:if>
  <admst:value-of select="then/adms/datatypename"/>
  <admst:apply-templates select="then" match="%s" required="yes"/>
  <admst:if test="then/adms[datatypename!='block']">
    <admst:text format="}\n"/>
  </admst:if>
  <admst:if test="else">
    <admst:text format="else\n"/>
    <admst:choose>
      <admst:when test="else/adms[datatypename='block']">
        <admst:value-of select="else/adms/datatypename"/>
        <admst:apply-templates select="else" match="%s" required="yes"/>
      </admst:when>
      <admst:otherwise>
        <admst:text format="{\n"/>
        <admst:value-of select="else/adms/datatypename"/>
        <admst:apply-templates select="else" match="%s" required="yes"/>
        <admst:text format="}\n"/>
      </admst:otherwise>
    </admst:choose>
  </admst:if>
  <admst:if test="if[not(nilled(function[class='builtin']))]">
    <admst:text format="}\n"/>
  </admst:if>
  <admst:if test="if[dynamic='yes']">
    <admst:choose>
      <admst:when test="[nilled(else)]">
        <admst:text format="#endif /*&lt;/dynamic_ifthen&gt;*/\n"/>
      </admst:when>
      <admst:otherwise>
        <admst:text format="#endif /*&lt;/dynamic_ifthenelse&gt;*/\n"/>
      </admst:otherwise>
    </admst:choose>
  </admst:if>
</admst:template>

<!-- analog//case -->
<admst:template match="case">
  <admst:error format="case statement: please implement me! (inside block)\n"/>
  <admst:text format="/*CASE*/;\n"/>
</admst:template>

<!-- analog//nilled -->
<admst:template match="nilled">
  <admst:text format=";\n"/>
</admst:template>

<!-- analog//whileloop -->
<admst:template match="whileloop">
  <admst:if test="while[dynamic='yes']">
    <admst:text format="#ifdef _DYNAMIC /*&lt;dynamic_while&gt;*/\n"/>
  </admst:if>
  <admst:if test="while[not(nilled(function[class='builtin']))]">
    <admst:text format="{\n"/>
    <admst:for-each select="while/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="while" match="function:assignment"/>
  </admst:if>
  <admst:apply-templates select="while" match="expression:stringify:noprobe"/>
  <admst:text format="while\n(%s)\n"/>
  <admst:if test="whileblock/adms[datatypename!='block']">
    <admst:text format="{\n"/>
  </admst:if>
  <admst:value-of select="whileblock/adms/datatypename"/>
  <admst:apply-templates select="whileblock" match="%s" required="yes"/>
  <admst:if test="whileblock/adms[datatypename!='block']">
    <admst:text format="}\n"/>
  </admst:if>
  <admst:if test="while[not(nilled(function[class='builtin']))]">
    <admst:text format="}\n"/>
  </admst:if>
  <admst:if test="while[dynamic='yes']">
    <admst:text format="#endif /*&lt;/dynamic_while&gt;*/\n"/>
  </admst:if>
</admst:template>

<!-- analog//callfunctions -->
<admst:template match="callfunction">
  <admst:choose>
    <admst:when test="function[name='\$strobe']">
      <admst:text format="_strobe("/>
    </admst:when>
    <admst:when test="function[name='\$warning']">
      <admst:text format="_warning("/>
    </admst:when>
    <admst:when test="function[name='\$error']">
      <admst:text format="_error("/>
    </admst:when>
    <admst:when test="function[name='\$finish']">
      <admst:text format="_finish("/>
    </admst:when>
    <admst:when test="function[name='\$stop']">
      <admst:text format="_stop("/>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="/simulator/tmp"/>
      <admst:value-of select="function/name"/>
      <admst:error format="function not supported: %s(%s)\n"/>
    </admst:otherwise>
  </admst:choose>
  <admst:reset select="/simulator/tmp"/>
  <admst:join select="function/arguments" separator=",">
    <admst:apply-templates select="." match="expression:stringify:noprobe"/>%s</admst:join>
  <admst:text format=");\n"/>
</admst:template>

<!-- expression//probe -->
<admst:template match="probe">
  <admst:choose>
    <admst:when test="branch/nnode[grounded='no']">
      <admst:value-of select="branch/nnode/name"/>
      <admst:value-of select="branch/pnode/name"/>
      <admst:value-to select="/simulator/tmp" value="BP(%s,%s)"/>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="branch/pnode/name"/>
      <admst:value-to select="/simulator/tmp" value="NP(%s)"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<!-- expression//node -->
<admst:template match="node">
  <admst:value-of select="name"/>
  <admst:error format="module node not expected here ... %s\n"/>
</admst:template>

<!-- expression//string -->
<admst:template match="string">
  <admst:value-of select="value"/>
  <admst:value-to select="/simulator/tmp" value="&quot;%s&quot;"/>
</admst:template>

<!-- expression//number -->
<admst:template match="number">
  <admst:choose>
    <admst:when test="[scalingunit='1']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="%s"/>
    </admst:when>
    <admst:when test="[scalingunit='E']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+18)"/>
    </admst:when>
    <admst:when test="[scalingunit='P']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+15)"/>
    </admst:when>
    <admst:when test="[scalingunit='T']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+12)"/>
    </admst:when>
    <admst:when test="[scalingunit='G']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+9)"/>
    </admst:when>
    <admst:when test="[scalingunit='M']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+6)"/>
    </admst:when>
    <admst:when test="[scalingunit='k']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+3)"/>
    </admst:when>
    <admst:when test="[scalingunit='h']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+2)"/>
    </admst:when>
    <admst:when test="[scalingunit='D']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e+1)"/>
    </admst:when>
    <admst:when test="[scalingunit='d']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-1)"/>
    </admst:when>
    <admst:when test="[scalingunit='c']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-2)"/>
    </admst:when>
    <admst:when test="[scalingunit='m']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-3)"/>
    </admst:when>
    <admst:when test="[scalingunit='u']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-6)"/>
    </admst:when>
    <admst:when test="[scalingunit='n']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-9)"/>
    </admst:when>
    <admst:when test="[scalingunit='A']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-10)"/>
    </admst:when>
    <admst:when test="[scalingunit='p']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-12)"/>
    </admst:when>
    <admst:when test="[scalingunit='f']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-15)"/>
    </admst:when>
    <admst:when test="[scalingunit='a']">
      <admst:value-of select="value"/>
      <admst:value-to select="/simulator/tmp" value="(%s*1.0e-18)"/>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="scalingunit"/>
      <admst:error format="scaling unit not supported: %s\n"/>
    </admst:otherwise>
  </admst:choose>
</admst:template>

<!-- analog//contribution[noise] -->
<admst:template match="contribution:noise">
  <admst:if test="[flickernoise='yes']">
    <admst:text format="ngspice_flickernoise(%(lhs/branch/pnode/name),%(lhs/branch/nnode/name)"/>
    <admst:for-each select="rhs/tree/arguments">
      <admst:apply-templates select="." match="%(datatypename)"/>
      <admst:value-of select="/simulator/tmp"/>
      <admst:text format=",%s"/>
    </admst:for-each>
    <admst:text test="[count(rhs/tree/arguments)=2]" format=",NULL"/>
  </admst:if>
  <admst:if test="[whitenoise='yes']">
    <admst:text format="ngspice_whitenoise(%(lhs/branch/pnode/name),%(lhs/branch/nnode/name)"/>
    <admst:for-each select="rhs/tree/arguments">
      <admst:apply-templates select="." match="%(datatypename)"/>
      <admst:value-of select="/simulator/tmp"/>
      <admst:text format=",%s"/>
    </admst:for-each>
    <admst:text test="[count(rhs/tree/arguments)=1]" format=",NULL"/>
  </admst:if>
  <admst:text format=")\n"/>
</admst:template>

<!-- variable:rhs -->
<admst:template match="variable">
  <admst:value-of select="name"/>
  <admst:if test="[parametertype='analogfunction']">
    <admst:value-to select="/simulator/tmp" value="%s"/>
  </admst:if>
  <admst:if test="[input='yes' and parametertype='model']">
    <admst:value-to select="/simulator/tmp" value="model-&gt;%s"/>
  </admst:if>
  <admst:if test="[input='yes' and parametertype='instance']">
    <admst:value-to select="/simulator/tmp" value="here-&gt;%s"/>
  </admst:if>
  <admst:if test="[input='no' and scope='global_model']">
    <admst:value-to select="/simulator/tmp" value="model-&gt;%s"/>
  </admst:if>
  <admst:if test="[input='no' and scope='global_instance']">
    <admst:value-to select="/simulator/tmp" value="here-&gt;%s"/>
  </admst:if>
  <admst:if test="[parametertype!='analogfunction' and scope='local']">
    <admst:value-to select="/simulator/tmp" value="%s"/>
  </admst:if>
</admst:template>
<!-- variable:lhs -->
<admst:template match="variable:lhs">
  <admst:text test="[input='yes' and parametertype='model']" format="model-&gt;%(name)"/>
  <admst:text test="[input='yes' and parametertype='instance']" format="here-&gt;%(name)"/>
  <admst:text test="[input='no' and scope='global_model']" format="model-&gt;%(name)"/>
  <admst:text test="[input='no' and scope='global_instance']" format="here-&gt;%(name)"/>
  <admst:text test="[scope='local']" format="%(name)"/>
</admst:template>
<!-- handle analog//callfunctions -->
<admst:template match="callfunction">
  <admst:choose>
    <admst:when test="function[name='\$strobe']">
      <admst:text format="fprintf(stdout"/>
    </admst:when>
    <admst:otherwise>
      <admst:value-of select="function/name"/>
      <admst:text format="%s: not supported by this interface\n"/>
    </admst:otherwise>
  </admst:choose>
  <admst:for-each select="function/arguments">
    <admst:apply-templates select="." match="expression:stringify:noprobe"/>
    <admst:text format=",%s"/>
  </admst:for-each>
  <admst:text format=");\n"/>
  <admst:choose>
    <admst:when test="function[name='\$strobe']">
      <admst:text format="fprintf(stdout,&quot;\\n&quot;);\n"/>
    </admst:when>
  </admst:choose>
</admst:template>

  <admst:value-to select="/simulator/package_name" value="ngspice"/>
  <admst:value-to select="/simulator/package_tarname" value="ngspice"/>
  <admst:value-to select="/simulator/package_version" value="1.0.0.0"/>
  <admst:value-to select="/simulator/package_string" value="ngspice 1.0.0.0"/>
  <admst:value-to select="/simulator/package_bugreport" value="r29173@users.sourceforge.net"/>

  <admst:for-each select="/module">
    <admst:new datatype="list" arguments="fnoise">
      <admst:variable name="fnoise" select="%(.)"/>
    </admst:new>
    <admst:new datatype="list" arguments="tnoise">
      <admst:variable name="tnoise" select="%(.)"/>
    </admst:new>
    <admst:new datatype="list" arguments="wnoise">
      <admst:variable name="wnoise" select="%(.)"/>
    </admst:new>
    <admst:for-each select="contribution">
      <admst:variable name="contribution" select="%(.)"/>
      <admst:variable name="dependency" select="%(math/dependency)"/>
      <admst:choose>
        <admst:when test="rhs/tree/adms[datatypename='function']/..[name='flicker_noise']">
          <admst:push into="$fnoise/item" select="$contribution" onduplicate="ignore"/>
        </admst:when>
        <admst:when test="[$dependency='constant']/rhs/tree/adms[datatypename='function']/..[name='white_noise']">
          <admst:push into="$tnoise/item" select="$contribution" onduplicate="ignore"/>
        </admst:when>
        <admst:when test="[$dependency!='constant']/rhs/tree/adms[datatypename='function']/..[name='white_noise']">
          <admst:push into="$wnoise/item" select="$contribution" onduplicate="ignore"/>
        </admst:when>
      </admst:choose>
    </admst:for-each>
    <admst:choose>
      <admst:when test="[name='hic0_full']">
        <admst:new datatype="attribute" arguments="ngspicename">
          <admst:push into="../attribute" select="." onduplicate="abort"/>
          <admst:value-to select="value" value="hicum0"/>
        </admst:new>
      </admst:when>
      <admst:when test="[name='hic2_full']">
        <admst:new datatype="attribute" arguments="ngspicename">
          <admst:push into="../attribute" select="." onduplicate="abort"/>
          <admst:value-to select="value" value="hicum2"/>
        </admst:new>
      </admst:when>
      <admst:when test="[name='bjt504tva']">
        <admst:new datatype="attribute" arguments="ngspicename">
          <admst:push into="../attribute" select="." onduplicate="abort"/>
          <admst:value-to select="value" value="bjt504t"/>
        </admst:new>
      </admst:when>
      <admst:when test="[lower-case(name)='psp102va']">
        <admst:new datatype="attribute" arguments="ngspicename">
          <admst:push into="../attribute" select="." onduplicate="abort"/>
          <admst:value-to select="value" value="psp102"/>
        </admst:new>
      </admst:when>
      <admst:otherwise>
        <admst:new datatype="attribute" arguments="ngspicename">
          <admst:push into="../attribute" select="." onduplicate="abort"/>
          <admst:value-of select="../name"/>
          <admst:value-to select="value" value="%s"/>
        </admst:new>
      </admst:otherwise>
    </admst:choose>
    <admst:choose>
      <admst:when test="[name='r2_et_cmc' or name='r2_cmc']">
      </admst:when>
      <admst:when test="[name='mosvar']">
        <admst:if test="[nilled(variable[name='c'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,c,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
      </admst:when>
      <admst:when test="[name='juncap2']">
        <admst:if test="[nilled(variable[name='d'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,d,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
      </admst:when>
      <admst:when test="[name='vbic' or name='hic0_full' or name='hic2_full' or name='bjt504tva']">
        <admst:if test="[nilled(variable[name='npn'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,npn,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
        <admst:if test="[nilled(variable[name='pnp'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,pnp,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="0">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
      </admst:when>
      <admst:when test="[lower-case(name)='psp102va']">
        <admst:if test="[nilled(variable[name='nmos'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,nmos,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
        <admst:if test="[nilled(variable[name='pmos'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,pmos,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
      </admst:when>
      <admst:when test="[name='ekv']">
        <admst:if test="[nilled(variable[name='nmos'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,nmos,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
        <admst:if test="[nilled(variable[name='pmos'])]">
          <admst:value-of select="analog"/>
          <admst:value-of select="."/>
          <admst:new datatype="variable" arguments="%p,pmos,%p">
            <admst:push into="module/variable" select="." onduplicate="ignore"/>
            <admst:value-to select="sizetype" value="scalar"/>
            <admst:value-to select="type" value="integer"/>
            <admst:value-to select="input" value="yes"/>
            <admst:value-to select="output" value="yes"/>
            <admst:value-to select="parametertype" value="model"/>
            <admst:value-to select="scope" value="global_model"/>
            <admst:new datatype="number" arguments="1">
              <admst:value-to select="scalingunit" value="1"/>
              <admst:value-of select="."/>
              <admst:value-of select="../module"/>
              <admst:new datatype="expression" arguments="%p,%p">
                <admst:value-of select="."/>
                <admst:value-to select="../../default" value="%p"/>
              </admst:new>
            </admst:new>
          </admst:new>
        </admst:if>
      </admst:when>

      <admst:otherwise>
        <admst:value-of select="name"/>
        <admst:fatal format="%s: device not handled by the adms ngspice interface\n"/>
      </admst:otherwise>
    </admst:choose>
  </admst:for-each>

</admst>
