c7c6aca00d083366fb182a1035a66007faa61bc2
[scilab.git] / scilab / modules / differential_equations / tests / unit_tests / dassldasrt.tst
1 // =============================================================================
2 // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 // Copyright (C) ????-2008 - INRIA
4 //
5 //  This file is distributed under the same license as the Scilab package.
6 // =============================================================================
7
8 // <-- CLI SHELL MODE -->
9
10 //DASSL
11 // PROBLEM 1..   LINEAR DIFFERENTIAL/ALGEBRAIC SYSTEM
12 //
13 //X1DOT + 10.0*X1 = 0  
14 //X1 + X2 = 1
15 //X1(0) = 1.0, X2(0) = 0.0
16 //
17 t=1:10;t0=0;y0=[1;0];y0d=[-10;0];
18 info=list([],0,[],[],[],0,0);
19 //    Calling Scilab functions
20 deff('[r,ires]=dres1(t,y,ydot)','r=[ydot(1)+10*y(1);y(2)+y(1)-1];ires=0')
21 deff('pd=djac1(t,y,ydot,cj)','pd=[cj+10,0;1,1]')
22 //   scilab function, without jacobian
23 yy0=dassl([y0,y0d],t0,t,dres1,info);
24 //   scilab functions, with jacobian
25 yy1=dassl([y0,y0d],t0,t,dres1,djac1,info);
26 // fortran routine, without jacobian
27 yy2=dassl([y0,y0d],t0,t,'dres1',info);   //=yy0
28 if norm(yy2-yy0,1)>1E-5 then pause,end
29 // fortran routines, with jacobian
30 yy3=dassl([y0,y0d],t0,t,'dres1','djac1',info);  //=yy1
31 if norm(yy3-yy1,1)>1E-5 then pause,end
32 yy3bis=dassl([y0,y0d],t0,t,'dres1',djac1,info); 
33 // call fortran dres1 and scilab's djac1
34 yy3ter=dassl([y0,y0d],t0,t,dres1,'djac1',info);
35 //
36 // with specific atol and rtol parameters
37 atol=1.d-6;rtol=0;
38 yy4=dassl([y0,y0d],t0,t,atol,rtol,dres1,info);
39 yy5=dassl([y0,y0d],t0,t,atol,rtol,'dres1',info); //=yy4
40 if norm(yy5-yy4,1)>1E-9 then pause,end
41 yy6=dassl([y0,y0d],t0,t,atol,rtol,dres1,djac1,info); 
42 yy7=dassl([y0,y0d],t0,t,atol,rtol,'dres1','djac1',info); //==yy6
43 if norm(yy7-yy6,1)>1E-12 then pause,end
44 //    
45 //   Testing E xdot - A x=0
46 //   x(0)=x0;   xdot(0)=xd0
47 rand('seed',0);
48 nx=5;
49 E=rand(nx,1)*rand(1,nx);A=rand(nx,nx);
50 //         Index 1
51 [Si,Pi,Di,o]=penlaur(E,A);pp=Si*E;[q,m]=fullrf(pp);x0=q(:,1);x0d=pinv(E)*A*x0;
52 deff('[r,ires]=g(t,x,xdot)','r=E*xdot-A*x;ires=0');
53 t=[1,2,3];t0=0;info=list([],0,[],[],[],0,0);
54 x=dassl([x0,x0d],t0,t,g,info);x1=x(2:nx+1,:);
55 if norm(pp*x1-x1,1)>1.d-5 then pause,end
56 //x(4) goes through 1 at  t=1.5409711;
57 t=1.5409711;ww=dassl([x0,x0d],t0,t,g,info);
58 if abs(ww(5)-1)>0.001 then pause,end
59 deff('[rt]=surface(t,y,yd)','rt=y(4)-1');nbsurf=1;
60 [yyy,nnn]=dasrt([x0,x0d],t0,t,g,nbsurf,surface,info);
61 deff('pd=j(t,y,ydot,cj)','pd=cj*E-A');
62 x=dassl([x0,x0d],t0,t,g,j,info);x2=x(2:nx+1,1);
63 if norm(x2-ww(2:nx+1,1),1)>0.0001 then pause,end
64 [yyy1,nnn]=dasrt([x0,x0d],t0,t,g,j,nbsurf,surface,info);
65 //x0d is not known:
66 x0d=ones(x0);info(7)=1;
67 x=dassl([x0,x0d],t0,t,g,info);
68 xn=dassl([x0,x0d],t0,t,g,j,info);
69 if norm(x-xn,1)>0.00001 then pause,end
70
71
72 //PROBLEM 2..
73
74 info=list([],0,[],[],[],0,0);
75 y0=zeros(25,1);y0(1)=1;
76 delta=0*y0;
77 //link('dres2.o','dres2');
78 //y0d=call('dres2',0,1,'d',y0,2,'d',delta,3,'d',0,5,'i',0,6,'d',0,7,'d','out',[25,1],4,'d');
79 y0d=zeros(y0);y0d(1)=-2;y0d(2)=1;y0d(6)=1;
80 t0=0;t=[0.01,0.1,1,10,100];
81 rtol=0;atol=1.d-6;
82 y=dassl([y0,y0d],t0,t,atol,rtol,'dres2',info);
83
84 //                 DASRT
85 // 
86 //C-----------------------------------------------------------------------
87 //C First problem.
88 //C The initial value problem is..
89 //C   DY/DT = ((2*LOG(Y) + 8)/T - 5)*Y,  Y(1) = 1,  1 .LE. T .LE. 6
90 //C The solution is  Y(T) = EXP(-T**2 + 5*T - 4), YPRIME(1) = 3
91 //C The two root functions are..
92 //C   G1 = ((2*LOG(Y)+8)/T - 5)*Y (= DY/DT)  (with root at T = 2.5),
93 //C   G2 = LOG(Y) - 2.2491  (with roots at T = 2.47 and 2.53)
94 //C-----------------------------------------------------------------------
95 y0=1;t=2:6;t0=1;y0d=3;
96 info=list([],0,[],[],[],0,0);
97 atol=1.d-6;rtol=0;ng=2;
98 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info);
99 if abs(nn(1)-2.47)>0.001 then pause,end
100 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
101 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info);
102 if abs(nn(1)-2.5)>0.001 then pause,end
103 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
104 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res1',ng,'gr1',info);
105 if abs(nn(1)-2.53)>0.001 then pause,end
106
107 deff('[delta,ires]=res1(t,y,ydot)','ires=0;delta=ydot-((2*log(y)+8)/t-5)*y')
108 deff('[rts]=gr1(t,y,yd)','rts=[((2*log(y)+8)/t-5)*y;log(y)-2.2491]')
109
110 y0=1;t=2:6;t0=1;y0d=3;
111 info=list([],0,[],[],[],0,0);
112 atol=1.d-6;rtol=0;ng=2;
113 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
114 if abs(nn(1)-2.47)>0.001 then pause,end
115 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
116 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
117 if abs(nn(1)-2.5)>0.001 then pause,end
118 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
119 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res1,ng,gr1,info);
120 if abs(nn(1)-2.53)>0.001 then pause,end
121
122 //C
123 //C-----------------------------------------------------------------------
124 //C Second problem (Van Der Pol oscillator).
125 //C The initial value problem is..
126 //C   DY1/DT = Y2,  DY2/DT = 100*(1 - Y1**2)*Y2 - Y1,
127 //C   Y1(0) = 2,  Y2(0) = 0,  0 .LE. T .LE. 200
128 //C   Y1PRIME(0) = 0, Y2PRIME(0) = -2
129 //C The root function is  G = Y1.
130 //C An analytic solution is not known, but the zeros of Y1 are known
131 //C to 15 figures for purposes of checking the accuracy.
132 //C-----------------------------------------------------------------------
133 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
134 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
135 info=list([],0,[],[],[],0,0);
136 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
137 if abs(nn(1)-81.163512)>0.001 then pause,end
138
139 deff('[delta,ires]=res2(t,y,ydot)',...
140 'ires=0;y1=y(1),y2=y(2),delta=[ydot-[y2;100*(1-y1*y1)*y2-y1]]')
141 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res2,'jac2',ng,'gr2',info);
142 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)]')
143 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info);
144 deff('s=gr2(t,y,yd)','s=y(1)')
145 [yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info);
146
147 //           Hot Restart
148
149 [yy,nn,hotd]=dasrt([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
150 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
151 [yy,nn,hotd]=dasrt([y01,y0d1],t01,t,atol,rtol,'res2','jac2',ng,'gr2',info,hotd);
152 if abs(nn(1)-162.57763)>0.004 then pause,end
153
154 //Test of Dynamic link (Require f77!)
155 //         1 making the routines
156 res22=[...
157 '      SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR)';
158 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
159 '      INTEGER NEQ';
160 '      DIMENSION Y(*), YDOT(*), DELTA(*)';
161 '      NEQ=2';
162 '      CALL F2(NEQ,T,Y,DELTA)';
163 '      DO 10 I = 1,NEQ';
164 '         DELTA(I) = YDOT(I) - DELTA(I)';
165 ' 10   CONTINUE';
166 '      RETURN';
167 '      END';
168 '      SUBROUTINE F2 (NEQ, T, Y, YDOT)';
169 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
170 '      INTEGER NEQ';
171 '      DOUBLE PRECISION T, Y, YDOT';
172 '      DIMENSION Y(*), YDOT(*)';
173 '      YDOT(1) = Y(2)';
174 '      YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1)';
175 '      RETURN';
176 '      END';]
177
178 jac22=[...
179 '      SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR)';
180 ' ';
181 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
182 '      INTEGER  NROWPD';
183 '      DOUBLE PRECISION T, Y, PD';
184 '      PARAMETER (NROWPD=2)';
185 '      DIMENSION Y(2), PD(NROWPD,2)';
186 '      PD(1,1) = 0.0D0';
187 '      PD(1,2) = 1.0D0';
188 '      PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0';
189 '      PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1))';
190 '      PD(1,1) = CJ - PD(1,1)';
191 '      PD(1,2) =    - PD(1,2)';
192 '      PD(2,1) =    - PD(2,1)';
193 '      PD(2,2) = CJ - PD(2,2)';
194 '      RETURN';
195 '      END';]
196
197
198 gr22=[...
199 '      SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR)';
200 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
201 '      INTEGER NEQ, NG';
202 '      DOUBLE PRECISION T, Y, GROOT';
203 '      DIMENSION Y(*), GROOT(*)';
204 '      GROOT(1) = Y(1)';
205 '      RETURN';
206 '      END';]
207
208 //Uncomment lines below: link may be machine dependent if some f77 libs are 
209 //needed for linking
210 //unix_g('cd /tmp;rm -f /tmp/res22.f');unix_g('cd /tmp;rm -f /tmp/gr22.f');
211 //unix_g('cd /tmp;rm -f /tmp/jac22.f');
212 //write('/tmp/res22.f',res22);write('/tmp/gr22.f',gr22);write('/tmp/jac22.f',jac22)
213 //unix_g("cd /tmp;make /tmp/res22.o");unix_g('cd /tmp;make /tmp/gr22.o');
214 //unix_g('cd /tmp;make /tmp/jac22.o');
215 //          2  Linking the routines
216 //link('/tmp/res22.o','res22');link('/tmp/jac22.o','jac22');link('/tmp/gr22.o','gr22')
217 //rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
218 //t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
219 //info=list([],0,[],[],[],0,0);
220 //          3 Calling the routines by dasrt
221 //[yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
222 // hot restart
223 //[yy,nn,hotd]=dasrt([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(2:3,qq);y0d1=yy(3:4,qq);
225 //[yy,nn,hotd]=dasrt([y01,y0d1],t01,t,atol,rtol,'res22','jac22',ng,'gr22',info,hotd);
226
227