1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) Scilab Enterprises - 2013 - Paul Bignier
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-en.txt
10 // =============================================================================
11 // Execute with exec("SCI/modules/differential_equations/tests/unit_tests/daskr.tst");
12 // or test_run('differential_equations', 'daskr', ['no_check_error_output']);
13 //C-----------------------------------------------------------------------
15 //C The initial value problem is..
16 //C DY/DT = ((2*LOG(Y) + 8)/T - 5)*Y, Y(1) = 1, 1 .LE. T .LE. 6
17 //C The solution is Y(T) = EXP(-T**2 + 5*T - 4), YPRIME(1) = 3
18 //C The two root functions are..
19 //C G1 = ((2*LOG(Y)+8)/T - 5)*Y (= DY/DT) (with root at T = 2.5),
20 //C G2 = LOG(Y) - 2.2491 (with roots at T = 2.47 and 2.53)
21 //C-----------------------------------------------------------------------
22 y0=1;t=2:6;t0=1;y0d=3;
23 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
24 atol=1.d-6;rtol=0;ng=2;
25 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info,'psol1','pjac1');
26 if abs(nn(1)-2.47)>0.001 then bugmes();quit;end
27 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
28 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info,'psol1','pjac1');
29 if abs(nn(1)-2.5)>0.001 then bugmes();quit;end
30 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
31 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
32 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info);
33 if abs(nn(1)-2.53)>0.001 then bugmes();quit;end
34 deff('[delta,ires]=res1(t,y,ydot)','ires=0;delta=ydot-((2*log(y)+8)/t-5)*y')
35 deff('[rts]=gr1(t,y,yd)','rts=[((2*log(y)+8)/t-5)*y;log(y)-2.2491]')
36 y0=1;t=2:6;t0=1;y0d=3;
37 atol=1.d-6;rtol=0;ng=2;
38 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
39 if abs(nn(1)-2.47)>0.001 then bugmes();quit;end
40 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
41 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
42 if abs(nn(1)-2.5)>0.001 then bugmes();quit;end
43 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
44 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
45 if abs(nn(1)-2.53)>0.001 then bugmes();quit;end
47 //C-----------------------------------------------------------------------
48 //C Second problem (Van Der Pol oscillator).
49 //C The initial value problem is..
50 //C DY1/DT = Y2, DY2/DT = 100*(1 - Y1**2)*Y2 - Y1,
51 //C Y1(0) = 2, Y2(0) = 0, 0 .LE. T .LE. 200
52 //C Y1PRIME(0) = 0, Y2PRIME(0) = -2
53 //C The root function is G = Y1.
54 //C An analytic solution is not known, but the zeros of Y1 are known
55 //C to 15 figures for purposes of checking the accuracy.
56 //C-----------------------------------------------------------------------
57 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
58 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
59 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
60 if abs(nn(1)-81.163512)>0.001 then bugmes();quit;end
61 deff('[delta,ires]=res2(t,y,ydot)',...
62 'ires=0;y1=y(1),y2=y(2),delta=[ydot-[y2;100*(1-y1*y1)*y2-y1]]')
63 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,'jac2',ng,'gr2',info);
64 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)]')
65 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info);
66 deff('s=gr2(t,y,yd)','s=y(1)')
67 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info);
69 [yy,nn,hotd]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
70 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
71 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,'res2','jac2',ng,'gr2',info,hotd);
72 if abs(nn(1)-162.57763)>0.004 then bugmes();quit;end
73 //Test of Dynamic link (Require f77!)
74 // 1 making the routines
76 ' SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR)';
77 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
79 ' DIMENSION Y(*), YDOT(*), DELTA(*)';
81 ' CALL F2(NEQ,T,Y,DELTA)';
83 ' DELTA(I) = YDOT(I) - DELTA(I)';
87 ' SUBROUTINE F2 (NEQ, T, Y, YDOT)';
88 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
90 ' DOUBLE PRECISION T, Y, YDOT';
91 ' DIMENSION Y(*), YDOT(*)';
93 ' YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1)';
98 ! SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR) !
100 ! IMPLICIT DOUBLE PRECISION (A-H,O-Z) !
104 ! DIMENSION Y(*), YDOT(*), DELTA(*) !
108 ! CALL F2(NEQ,T,Y,DELTA) !
112 ! DELTA(I) = YDOT(I) - DELTA(I) !
120 ! SUBROUTINE F2 (NEQ, T, Y, YDOT) !
122 ! IMPLICIT DOUBLE PRECISION (A-H,O-Z) !
126 ! DOUBLE PRECISION T, Y, YDOT !
128 ! DIMENSION Y(*), YDOT(*) !
132 ! YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1) !
138 ' SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR)';
140 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
142 ' DOUBLE PRECISION T, Y, PD';
143 ' PARAMETER (NROWPD=2)';
144 ' DIMENSION Y(2), PD(NROWPD,2)';
147 ' PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0';
148 ' PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1))';
149 ' PD(1,1) = CJ - PD(1,1)';
150 ' PD(1,2) = - PD(1,2)';
151 ' PD(2,1) = - PD(2,1)';
152 ' PD(2,2) = CJ - PD(2,2)';
157 ! SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR) !
161 ! IMPLICIT DOUBLE PRECISION (A-H,O-Z) !
165 ! DOUBLE PRECISION T, Y, PD !
167 ! PARAMETER (NROWPD=2) !
169 ! DIMENSION Y(2), PD(NROWPD,2) !
175 ! PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0 !
177 ! PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1)) !
179 ! PD(1,1) = CJ - PD(1,1) !
181 ! PD(1,2) = - PD(1,2) !
183 ! PD(2,1) = - PD(2,1) !
185 ! PD(2,2) = CJ - PD(2,2) !
191 ' SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR)';
192 ' IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
194 ' DOUBLE PRECISION T, Y, GROOT';
195 ' DIMENSION Y(*), GROOT(*)';
201 ! SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR) !
203 ! IMPLICIT DOUBLE PRECISION (A-H,O-Z) !
207 ! DOUBLE PRECISION T, Y, GROOT !
209 ! DIMENSION Y(*), GROOT(*) !
216 //Uncomment lines below: link may be machine dependent if some f77 libs are
218 //unix_g('cd /tmp;rm -f /tmp/res22.f');unix_g('cd /tmp;rm -f /tmp/gr22.f');
219 //unix_g('cd /tmp;rm -f /tmp/jac22.f');
220 //write('/tmp/res22.f',res22);write('/tmp/gr22.f',gr22);write('/tmp/jac22.f',jac22)
221 //unix_g("cd /tmp;make /tmp/res22.o");unix_g('cd /tmp;make /tmp/gr22.o');
222 //unix_g('cd /tmp;make /tmp/jac22.o');
223 // 2 Linking the routines
224 //link('/tmp/res22.o','res22');link('/tmp/jac22.o','jac22');link('/tmp/gr22.o','gr22')
225 //rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
226 //t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
227 // 3 Calling the routines by dasrt
228 //[yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
230 //[yy,nn,hotd]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
231 //t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
232 //[yy,nn,hotd]=dasrt([y01,y0d1],t01,t,atol,rtol,'res22','jac22',ng,'gr22',info,hotd);