Differential_equations tests: daskr() with C code
[scilab.git] / scilab / modules / differential_equations / tests / unit_tests / daskr.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) Scilab Enterprises - 2013 - Paul Bignier
4 //
5 // This file must be used under the terms of the CeCILL.
6 // This source file is licensed as described in the file COPYING,
7 // which you should have received as part of this distribution.
8 // The terms are also available at
9 // http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10 // =============================================================================
11
12 //C-----------------------------------------------------------------------
13 //C First problem.
14 //C The initial value problem is..
15 //C   DY/DT = ((2*LOG(Y) + 8)/T - 5)*Y,  Y(1) = 1,  1 .LE. T .LE. 6
16 //C The solution is  Y(T) = EXP(-T**2 + 5*T - 4), YPRIME(1) = 3
17 //C The two root functions are..
18 //C   G1 = ((2*LOG(Y)+8)/T - 5)*Y (= DY/DT)  (with root at T = 2.5),
19 //C   G2 = LOG(Y) - 2.2491  (with roots at T = 2.47 and 2.53)
20 //C-----------------------------------------------------------------------
21 y0=1;t=2:6;t0=1;y0d=3;
22 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
23 atol=1.d-6;rtol=0;ng=2;
24 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,"res1",ng,"gr1",info,"psol1","pjac1");
25 assert_checkalmostequal(nn(1),2.47,0.001);
26 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
27 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,"res1",ng,"gr1",info,"psol1","pjac1");
28 assert_checkalmostequal(nn(1),2.5,0.001);
29 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
30 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
31 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,"res1",ng,"gr1",info);
32 assert_checkalmostequal(nn(1),2.53,0.001);
33
34 // Same problem, but using macro for the derivative evaluation function 'res1'
35 deff("[delta,ires]=res1(t,y,ydot)","ires=0;delta=ydot-((2.*log(y)+8)./t-5).*y")
36 deff("[rts]=gr1(t,y,yd)","rts=[((2*log(y)+8)/t-5)*y;log(y)-2.2491]")
37
38 y0=1;t=2:6;t0=1;y0d=3;
39 atol=1.d-6;rtol=0;ng=2;
40 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
41 assert_checkalmostequal(nn(1),2.47,0.001);
42 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
43 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
44 assert_checkalmostequal(nn(1),2.5,0.001);
45 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
46 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
47 assert_checkalmostequal(nn(1),2.53,0.001);
48
49 // Same problem, but using macros for the preconditioner evaluation and application functions 'pjac' and 'psol'
50 // pjac uses the macro res1 defined above.
51 function [wp, iwp, ires] = pjac(neq, t, y, ydot, h, cj, rewt, savr)
52     ires = 0;
53     SQuround = 1.490D-08;
54     tx = t;
55     nrow = 0;
56     e = zeros(1, neq);
57     wp = zeros(neq*neq, 1);
58     iwp = zeros(neq*neq, 2);
59     for i=1:neq
60         del = max(SQuround*max(abs(y(i)), abs(h*ydot(i))), 1/rewt(i))
61         if h*ydot(i) < 0 then del = -del; end
62         ysave = y(i);
63         ypsave = ydot(i);
64         y(i) = y(i) + del;
65         ydot(i) = ydot(i) + cj*del;
66         [e ires] = res1(tx, y, ydot);
67         if ires < 0 then
68             ires = -1;
69             return;
70         end
71         delinv = 1/del;
72         for j=1:neq
73             wp(nrow+j) = delinv*(e(j)-savr(j));
74             if isnan(wp(nrow+j)) then
75                 ires = -1;
76                 return;
77             end
78             iwp(nrow+j, 1) = i;
79             iwp(nrow+j, 2) = j;
80         end
81         nrow = nrow + neq;
82         y(i) = ysave;
83         ydot(i) = ypsave;
84     end
85 endfunction
86 function [r, ier] = psol(wp, iwp, b)
87     ier = 0;
88     //Compute the LU factorization of R.
89     sp = sparse(iwp, wp);
90     [h, rk] = lufact(sp);
91     //Solve the system LU*X = b
92     r = lusolve(h, b);
93     ludel(h);
94 endfunction
95
96 y0=1;t=2:6;t0=1;y0d=3;
97 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
98 atol=1.d-6;rtol=0;ng=2;
99 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,"gr1",info,psol,pjac);
100 assert_checkalmostequal(nn(1),2.47,0.001);
101 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
102 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,"gr1",info,psol,pjac);
103 assert_checkalmostequal(nn(1),2.5,0.001);
104 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
105 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,"gr1",info,psol,pjac);
106 assert_checkalmostequal(nn(1),2.53,0.001);
107
108 //C
109 //C-----------------------------------------------------------------------
110 //C Second problem (Van Der Pol oscillator).
111 //C The initial value problem is..
112 //C   DY1/DT = Y2,  DY2/DT = 100*(1 - Y1**2)*Y2 - Y1,
113 //C   Y1(0) = 2,  Y2(0) = 0,  0 .LE. T .LE. 200
114 //C   Y1PRIME(0) = 0, Y2PRIME(0) = -2
115 //C The root function is  G = Y1.
116 //C An analytic solution is not known, but the zeros of Y1 are known
117 //C to 15 figures for purposes of checking the accuracy.
118 //C-----------------------------------------------------------------------
119 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
120 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
121 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
122 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,"res2","jac2",ng,"gr2",info);
123 assert_checkalmostequal(nn(1),81.163512,0.001);
124
125 deff("[delta,ires]=res2(t,y,ydot)",...
126 "ires=0;y1=y(1),y2=y(2),delta=[ydot-[y2;100*(1-y1*y1)*y2-y1]]")
127 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,"jac2",ng,"gr2",info);
128 deff("J=jac2(t,y,ydot,c)","y1=y(1);y2=y(2);J=[c,-1;200*y1*y2+1,c-100*(1-y1*y1)]")
129 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,"gr2",info);
130 deff("s=gr2(t,y,yd)","s=y(1)")
131 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info);
132
133 // Same problem, with psol and pjac example routines
134
135 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
136 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,"gr2",info,"psol1","pjac1");
137 assert_checkalmostequal(nn(1),81.163512,0.009);
138 deff("s=gr2(t,y,yd)","s=y(1)")
139 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info,"psol1","pjac1");
140 assert_checkalmostequal(nn(1),81.163512,0.009);
141
142 // Same problem, with psol and pjac macros
143
144 // Redefine pjac to use res2
145 function [wp, iwp, ires] = pjac(neq, t, y, ydot, h, cj, rewt, savr)
146     ires = 0;
147     SQuround = 1.490D-08;
148     tx = t;
149     nrow = 0;
150     e = zeros(1, neq);
151     wp = zeros(neq*neq, 1);
152     iwp = zeros(neq*neq, 2);
153     for i=1:neq
154         del = max(SQuround*max(abs(y(i)), abs(h*ydot(i))), 1/rewt(i))
155         if h*ydot(i) < 0 then del = -del; end
156         ysave = y(i);
157         ypsave = ydot(i);
158         y(i) = y(i) + del;
159         ydot(i) = ydot(i) + cj*del;
160         [e ires]=res2(tx, y, ydot);
161         if ires < 0 then return; end
162         delinv = 1/del;
163         for j=1:neq
164             wp(nrow+j) = delinv*(e(j)-savr(j));
165             iwp(nrow+j,1) = i;
166             iwp(nrow+j,2) = j;
167         end
168         nrow = nrow + neq;
169         y(i) = ysave;
170         ydot(i) = ypsave;
171     end
172 endfunction
173 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,"gr2",info,psol,pjac);
174 assert_checkalmostequal(nn(1),81.163512,0.003);
175 deff("s=gr2(t,y,yd)","s=y(1)")
176 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info,psol,pjac);
177 assert_checkalmostequal(nn(1),81.163512,0.003);
178 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
179
180 //           Hot Restart
181
182 [yy,nn,hotd]=daskr([y0,y0d],t0,t,atol,rtol,"res2","jac2",ng,"gr2",info);
183 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(3:4,qq);y0d1=yy(4:5,qq);
184 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,"res2","jac2",ng,"gr2",info,hotd);
185 assert_checkalmostequal(nn(1),162.57763,0.004);
186
187 // Same with C code
188 ilib_verbose(0);
189
190 cd TMPDIR;
191 mkdir("daskr_test1");
192 cd("daskr_test1");
193
194 code=["#include <math.h>"
195 "void res22(double *t,double *y,double *yd,double *res,int *ires,double *rpar,int *ipar)"
196 "{res[0] = yd[0] - y[1];"
197 " res[1] = yd[1] - (100.0*(1.0 - y[0]*y[0])*y[1] - y[0]);}"
198 " "
199 "void jac22(double *t,double *y,double *yd,double *pd,double *cj,double *rpar,int *ipar)"
200 "{pd[0]=*cj - 0.0;"
201 " pd[1]=    - (-200.0*y[0]*y[1] - 1.0);"
202 " pd[2]=    - 1.0;"
203 " pd[3]=*cj - (100.0*(1.0 - y[0]*y[0]));}"
204 " "
205 "void gr22(int *neq, double *t, double *y, int *ng, double *groot, double *rpar, int *ipar)"
206 "{ groot[0] = y[0];}"];
207 mputl(code,"t22.c") ;
208 ilib_for_link(["res22" "jac22" "gr22"],"t22.c","","c");
209 exec("loader.sce");
210
211 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
212 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
213 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
214 // Hot restart
215 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(3:4,qq);y0d1=yy(4:5,qq);
216 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,"res22","jac22",ng,"gr22",info,hotd);
217
218 rtol=[1.d-6;1.d-6];
219 atol=[1.d-6;1.d-4];
220 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
221 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,"res22","jac22",ng,"gr22",info);
222 // Hot restart
223 [yy,nn,hotd]=daskr([y0,y0d],t0,t,atol,rtol,"res22","jac22",ng,"gr22",info);
224 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(3:4,qq);y0d1=yy(4:5,qq);
225 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,"res22","jac22",ng,"gr22",info,hotd);