<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) 2008 - INRIA
 * ...
 *
 * Copyright (C) 2012 - 2016 - Scilab Enterprises
 *
 * This file is hereby licensed under the terms of the GNU GPL v2.0,
 * pursuant to article 5.3.4 of the CeCILL v.2.1.
 * This file was originally licensed under the terms of the CeCILL v2.1,
 * and continues to be available under such terms.
 * For more information, see the COPYING file which you should have received
 * along with this program.
 *
 -->
<refentry xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:svg="http://www.w3.org/2000/svg" xmlns:ns5="http://www.w3.org/1999/xhtml" xmlns:mml="http://www.w3.org/1998/Math/MathML" xmlns:db="http://docbook.org/ns/docbook" xmlns:scilab="http://www.scilab.org" xml:id="dasrt" xml:lang="en">
    <refnamediv>
        <refname>dasrt</refname>
        <refpurpose>DAE solver with zero crossing</refpurpose>
    </refnamediv>
    <refsynopsisdiv>
        <title>Syntax</title>
        <synopsis>[r,nn,[,hd]] = dasrt(x0,t0,t [,atol,[rtol]],res [,jac],ng, surf [,info] [,hd])</synopsis>
    </refsynopsisdiv>
    <refsection>
        <title>Arguments</title>
        <variablelist>
            <varlistentry>
                <term>x0</term>
                <listitem>
                    <para>
                        is either <literal>y0</literal> (<literal>ydot0</literal> is
                        estimated by <literal>dassl</literal> with zero as first estimate)
                        or the matrix <literal>[y0 ydot0]</literal>.
                        <literal>g(t,y0,ydot0)</literal> must be equal to zero. If you only
                        know an estimate of <literal>ydot0</literal> set
                        <literal>info(7)=1</literal>.
                    </para>
                    <variablelist>
                        <varlistentry>
                            <term>y0</term>
                            <listitem>
                                <para>a real column vector of initial conditions.</para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>ydot0</term>
                            <listitem>
                                <para>a real column vector of the time derivative of
                                    <literal>y</literal> at <literal>t0</literal> (may be an
                                    estimate).
                                </para>
                            </listitem>
                        </varlistentry>
                    </variablelist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>t0</term>
                <listitem>
                    <para>a real number, it is the initial instant.</para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>t</term>
                <listitem>
                    <para>a real scalar or vector. Gives instants for which you want the
                        solution. Note that you can get solution at each dassl's step point
                        by setting <literal>info(2)=1</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>nn</term>
                <listitem>
                    <para>
                        a vector with two entries <literal>[times num]</literal>
                        <literal>times</literal> is the value of the time at which the
                        surface is crossed, <literal>num</literal> is the number of the
                        crossed surface.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>atol, rtol</term>
                <listitem>
                    <para>real scalars or column vectors of same size as
                        <literal>y</literal> or both of size 1. <literal>atol, rtol</literal> give respectively
                        absolute and relative error tolerances of solution. If vectors, the
                        tolerances are specified for each component of
                        <literal>y</literal>.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>res</term>
                <listitem>
                    <para>
                        <link linkend="external">external</link> (function or list or string). Computes the value of
                        <literal>g(t,y,ydot)</literal>. It may be :
                    </para>
                    <itemizedlist>
                        <listitem>
                            <para>A Scilab function.</para>
                            <para>Its syntax must be
                                <literal>[r,ires]=res(t,y,ydot)</literal> and
                                <literal>res</literal> must return the residue
                                <literal>r=g(t,y,ydot)</literal> and error flag
                                <literal>ires</literal>. <literal>ires = 0</literal> if
                                <literal>res</literal> succeeds to compute <literal>r</literal>,
                                <literal>=-1</literal> if residue is locally not defined for
                                <literal>(t,y,ydot)</literal>, <literal>=-2</literal> if
                                parameters are out of admissible range.
                            </para>
                        </listitem>
                        <listitem>
                            <para>A list.</para>
                            <para>This form allows to pass parameters other than t,y,ydot to
                                the function. It must be as follows:
                            </para>
                            <screen><![CDATA[
list(res,x1,x2,...)
]]></screen>
                            <para>where the syntax of the function
                                <literal>res</literal> is now
                            </para>
                            <screen><![CDATA[
r = res(t,y,ydot,x1,x2,...)
]]></screen>
                            <para>
                                <literal>res</literal> still returns
                                <literal>r=g(t,y,ydot)</literal> as a function of
                                <literal>(t,y,ydot,x1,x2,...)</literal>.
                            </para>
                            <para>
                                <warning>
                                    Warning: this form must not be used if there is no extra
                                    argument to pass to the function.
                                </warning>
                            </para>
                        </listitem>
                        <listitem>
                            <para>A string.</para>
                            <para>It must refer to the name of a C or Fortran subroutine
                                linked with Scilab.
                            </para>
                            <para>In C The syntax must be:</para>
                            <screen><![CDATA[
void res(double *t, double y[], double yd[], double r[],
         int *ires, double rpar[], int ipar[])
]]></screen>
                            <para>In Fortran it must be:</para>
                            <screen><![CDATA[
subroutine res(t,y,yd,r,ires,rpar,ipar)
double precision t, y(*),yd(*),r(*),rpar(*)
integer ires,ipar(*)
]]></screen>
                            <para>
                                The <literal>rpar</literal> and <literal>ipar</literal> arrays must be present but cannot be
                                used.
                            </para>
                        </listitem>
                    </itemizedlist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>jac</term>
                <listitem>
                    <para>
                        <link linkend="external">external</link> (function or list or string). Computes the value of
                        <literal>dg/dy + cj*dg/dydot</literal> for a given value of parameter
                        <literal>cj</literal>.
                    </para>
                    <itemizedlist>
                        <listitem>
                            <para>A Scilab function.</para>
                            <para>Its syntax must be
                                <literal>r=jac(t,y,ydot,cj)</literal> and the
                                <literal>jac</literal> function must return
                                <literal>r=dg(t,y,ydot)/dy+cj*dg(t,y,ydot)/dydot</literal> where
                                <literal>cj</literal> is a real scalar.
                            </para>
                        </listitem>
                        <listitem>
                            <para>A list.</para>
                            <para>It must be as follows</para>
                            <screen><![CDATA[
list(jac,x1,x2,...)
]]></screen>
                            <para>where the syntax of the function
                                <literal>jac</literal> is now
                            </para>
                            <screen><![CDATA[
r = jac(t,y,ydot,cj,x1,x2,...)
]]></screen>
                            <para>
                                <literal>jac</literal> still returns
                                <literal>dg/dy + cj*dg/dydot</literal> as a function of
                                <literal>(t,y,ydot,cj,x1,x2,...)</literal>.
                            </para>
                        </listitem>
                        <listitem>
                            <para>A character string.</para>
                            <para>It must refer to the name of a Fortran subroutine linked
                                with scilab
                            </para>
                            <para>In C The syntax must be:</para>
                            <screen><![CDATA[
void jac(double *t, double y[], double yd[], double pd[],
         double *cj, double rpar[], int ipar[])
]]></screen>
                            <para>In Fortran it must be:</para>
                            <screen><![CDATA[
subroutine jac(t,y,yd,pd,cj,rpar,ipar)
double precision t, y(*),yd(*),pd(*),cj,rpar(*)
integer ipar(*)
]]></screen>
                        </listitem>
                    </itemizedlist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>surf</term>
                <listitem>
                    <para>
                        <link linkend="external">external</link> (function or list or string). Computes the value of
                        the column vector <literal>surf(t,y)</literal> with
                        <literal>ng</literal> components. Each component defines a surface.
                        It may be defined by:
                    </para>
                    <itemizedlist>
                        <listitem>
                            <para>A Scilab function.</para>
                            <para>Its syntax must be
                                <literal>surf(t,y)</literal>
                            </para>
                        </listitem>
                        <listitem>
                            <para>A list.</para>
                            <para>It must be as follows</para>
                            <screen><![CDATA[
list(surf,x1,x2,...)
]]></screen>
                            <para>where the syntax of the function
                                <literal>surf</literal> is now
                            </para>
                            <screen><![CDATA[
r = surf(t,y,x1,x2,...)
]]></screen>
                        </listitem>
                        <listitem>
                            <para>A character string.</para>
                            <para>It must refer to the name of a Fortran subroutine linked
                                with scilab.
                            </para>
                            <para>In C the syntax must be:</para>
                            <screen><![CDATA[
void surf(int *ny, double *t, double y[], int *ng, double gout[])
]]></screen>
                            <para>In Fortran it must be:</para>
                            <screen><![CDATA[
subroutine surf(ny,t,y,ng,gout)
double precision t, y(*),gout(*)
integer ny,ng
]]></screen>
                        </listitem>
                    </itemizedlist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>info</term>
                <listitem>
                    <para>
                        list which contains <literal>7</literal> elements. Default
                        value is <literal>list([],0,[],[],[],0,0)</literal>.
                    </para>
                    <variablelist>
                        <varlistentry>
                            <term>info(1)</term>
                            <listitem>
                                <para>real scalar which gives the maximum time for which
                                    <literal>g</literal> is allowed to be evaluated or an empty
                                    matrix <literal>[]</literal> if no limits imposed for
                                    time.
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(2)</term>
                            <listitem>
                                <para>
                                    flag which indicates if <literal>dassl</literal> returns
                                    its intermediate computed values (<literal>flag=1</literal>)
                                    or only the user specified time point values
                                    (<literal>flag=0</literal>).
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(3)</term>
                            <listitem>
                                <para>
                                    <literal>2</literal> components vector which give the
                                    definition <literal>[ml,mu]</literal> of band matrix computed
                                    by <literal>jac</literal>; <literal>r(i - j + ml + mu + 1,j) =
                                        "dg(i)/dy(j)+cj*dg(i)/dydot(j)"
                                    </literal>
                                    .If <literal>jac</literal> returns a full matrix set
                                    <literal>info(3)=[]</literal>.
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(4)</term>
                            <listitem>
                                <para>real scalar which gives the maximum step size. Set
                                    <literal>info(4)=[]</literal> if no limitation.
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(5)</term>
                            <listitem>
                                <para>real scalar which gives the initial step size. Set
                                    <literal>info(5)=[]</literal> if not specified.
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(6)</term>
                            <listitem>
                                <para>
                                    set <literal>info(6)=1</literal> if the solution is
                                    known to be non negative, else set
                                    <literal>info(6)=0</literal>.
                                </para>
                            </listitem>
                        </varlistentry>
                        <varlistentry>
                            <term>info(7)</term>
                            <listitem>
                                <para>
                                    set <literal>info(7)=1</literal> if
                                    <literal>ydot0</literal> is just an estimation,
                                    <literal>info(7)=0</literal> if
                                    <literal>g(t0,y0,ydot0)=0</literal>.
                                </para>
                            </listitem>
                        </varlistentry>
                    </variablelist>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>hd</term>
                <listitem>
                    <para>
                        real vector which allows to store the <literal>dassl</literal>
                        context and to resume integration.
                    </para>
                </listitem>
            </varlistentry>
            <varlistentry>
                <term>r</term>
                <listitem>
                    <para>
                        real matrix . Each column is the vector <literal>[t;x(t);xdot(t)]</literal> where
                        <literal>t</literal> is time index for which the solution had been computed.
                    </para>
                </listitem>
            </varlistentry>
        </variablelist>
    </refsection>
    <refsection>
        <title>Description</title>
        <para>Solution of the implicit differential equation.</para>
        <screen><![CDATA[
g(t,y,ydot) = 0
y(t0) = y0  and   ydot(t0) = ydot0
]]></screen>
        <para>Returns the surface crossing instants and the number of the surface
            reached in <literal>nn</literal>.
        </para>
        <para>Detailed examples can be found in SCIDIR/tests/unit_tests/dassldasrt.tst</para>
    </refsection>
    <refsection>
        <title>Examples</title>
        <programlisting role="example"><![CDATA[
// dy/dt = ((2*log(y)+8)/t -5)*y,  y(1) = 1,  1<=t<=6
// g1 = ((2*log(y)+8)/t - 5)*y
// g2 = log(y) - 2.2491
y0 = 1; t = 2:6; t0 = 1; y0d = 3;
atol = 1.d-6; rtol = 0; ng = 2;

deff('[delta,ires] = res1(t,y,ydot)', 'ires=0; delta=ydot-((2*log(y)+8)/t-5)*y')
deff('rts = gr1(t,y)', 'rts=[((2*log(y)+8)/t-5)*y;log(y)-2.2491]')

[yy,nn] = dasrt([y0,y0d],t0,t,atol,rtol,res1,ng,gr1);
//(Should return nn=[2.4698972 2])
     ]]></programlisting>
    </refsection>
    <refsection role="see also">
        <title>See also</title>
        <simplelist type="inline">
            <member>
                <link linkend="ode">ode</link>
            </member>
            <member>
                <link linkend="dassl">dassl</link>
            </member>
            <member>
                <link linkend="daskr">daskr</link>
            </member>
            <member>
                <link linkend="impl">impl</link>
            </member>
            <member>
                <link linkend="call">call</link>
            </member>
            <member>
                <link linkend="link">link</link>
            </member>
            <member>
                <link linkend="external">external</link>
            </member>
        </simplelist>
    </refsection>
</refentry>
