ODEPACK solvers (daskr): adding Scilab macro example for pjac and psol
[scilab.git] / scilab / modules / differential_equations / tests / unit_tests / daskr.dia.ref
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-en.txt
10 // =============================================================================
11 // Run with test_run('differential_equations', 'daskr', ['no_check_error_output']);
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 if abs(nn(1)-2.47)>0.001 then bugmes();quit;end
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 if abs(nn(1)-2.5)>0.001 then bugmes();quit;end
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 if abs(nn(1)-2.53)>0.001 then bugmes();quit;end
33 // Same problem, but using macro for the derivative evaluation function 'res1'
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
46 // Same problem, but using macros for the preconditioner evaluation and application functions 'pjac' and 'psol'
47 // pjac uses the macro res1 defined above.
48 function [r, ier] = psol(wp, iwp, b)
49     ier = 0;
50     //Compute the LU factorization of R.
51     sp = sparse(iwp, wp);
52     [h, rk] = lufact(sp);
53     //Solve the system LU*X = b
54     r = lusolve(h, b);
55     ludel(h);
56 endfunction
57 function [wp, iwp, ires] = pjac(neq, t, y, ydot, h, cj, rewt, savr)
58     ires = 0;
59     SQuround = 1.490D-08;
60     tx = t;
61     nrow = 0;
62     e = zeros(1, neq);
63     wp = zeros(neq*neq, 1);
64     iwp = zeros(neq*neq, 2);
65     for i=1:neq
66         del = max(SQuround*max(abs(y(i)), abs(h*ydot(i))), 1/rewt(i))
67         if h*ydot(i) < 0 then del = -del; end
68         ysave = y(i);
69         ypsave = ydot(i);
70         y(i) = y(i) + del;
71         ydot(i) = ydot(i) + cj*del;
72         [e ires]=res1(tx, y, ydot);
73         if ires < 0 then return; end
74         delinv = 1/del;
75         for j=1:neq
76             wp(nrow+j) = delinv*(e(j)-savr(j));
77             iwp(nrow+j,1) = i;
78             iwp(nrow+j,2) = j;
79         end
80         nrow = nrow + neq;
81         y(i) = ysave;
82         ydot(i) = ypsave;
83     end
84 endfunction
85 y0=1;t=2:6;t0=1;y0d=3;
86 info=list([],0,[],[],[],0,[],1,[],0,1,[],[],1);
87 atol=1.d-6;rtol=0;ng=2;
88 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
89 if abs(nn(1)-2.47)>0.001 then bugmes();quit;end
90 y0=yy(2,2);y0d=yy(3,2);t0=nn(1);t=[3,4,5,6];
91 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
92 if abs(nn(1)-2.5)>0.001 then bugmes();quit;end
93 y0=yy(2,1);y0d=yy(3,1);t0=nn(1);t=[3,4,5,6];
94 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res1,ng,'gr1',info,psol,pjac);
95 if abs(nn(1)-2.53)>0.001 then bugmes();quit;end
96 info=list([],0,[],[],[],0,[],0,[],0,0,[],[],1);
97 //C
98 //C-----------------------------------------------------------------------
99 //C Second problem (Van Der Pol oscillator).
100 //C The initial value problem is..
101 //C   DY1/DT = Y2,  DY2/DT = 100*(1 - Y1**2)*Y2 - Y1,
102 //C   Y1(0) = 2,  Y2(0) = 0,  0 .LE. T .LE. 200
103 //C   Y1PRIME(0) = 0, Y2PRIME(0) = -2
104 //C The root function is  G = Y1.
105 //C An analytic solution is not known, but the zeros of Y1 are known
106 //C to 15 figures for purposes of checking the accuracy.
107 //C-----------------------------------------------------------------------
108 rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
109 t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
110 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
111 if abs(nn(1)-81.163512)>0.001 then bugmes();quit;end
112 deff('[delta,ires]=res2(t,y,ydot)',...
113 'ires=0;y1=y(1),y2=y(2),delta=[ydot-[y2;100*(1-y1*y1)*y2-y1]]')
114 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,'jac2',ng,'gr2',info);
115 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)]')
116 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,'gr2',info);
117 deff('s=gr2(t,y,yd)','s=y(1)')
118 [yy,nn]=daskr([y0,y0d],t0,t,atol,rtol,res2,jac2,ng,gr2,info);
119 //           Hot Restart
120 [yy,nn,hotd]=daskr([y0,y0d],t0,t,atol,rtol,'res2','jac2',ng,'gr2',info);
121 t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
122 [yy,nn,hotd]=daskr([y01,y0d1],t01,t,atol,rtol,'res2','jac2',ng,'gr2',info,hotd);
123 if abs(nn(1)-162.57763)>0.004 then bugmes();quit;end
124 //Test of Dynamic link (Require f77!)
125 //         1 making the routines
126 res22=[...
127 '      SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR)';
128 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
129 '      INTEGER NEQ';
130 '      DIMENSION Y(*), YDOT(*), DELTA(*)';
131 '      NEQ=2';
132 '      CALL F2(NEQ,T,Y,DELTA)';
133 '      DO 10 I = 1,NEQ';
134 '         DELTA(I) = YDOT(I) - DELTA(I)';
135 ' 10   CONTINUE';
136 '      RETURN';
137 '      END';
138 '      SUBROUTINE F2 (NEQ, T, Y, YDOT)';
139 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
140 '      INTEGER NEQ';
141 '      DOUBLE PRECISION T, Y, YDOT';
142 '      DIMENSION Y(*), YDOT(*)';
143 '      YDOT(1) = Y(2)';
144 '      YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1)';
145 '      RETURN';
146 '      END';]
147  res22  =
148  
149 !      SUBROUTINE RES22(T,Y,YDOT,DELTA,IRES,RPAR,IPAR)    !
150 !                                                         !
151 !      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                !
152 !                                                         !
153 !      INTEGER NEQ                                        !
154 !                                                         !
155 !      DIMENSION Y(*), YDOT(*), DELTA(*)                  !
156 !                                                         !
157 !      NEQ=2                                              !
158 !                                                         !
159 !      CALL F2(NEQ,T,Y,DELTA)                             !
160 !                                                         !
161 !      DO 10 I = 1,NEQ                                    !
162 !                                                         !
163 !         DELTA(I) = YDOT(I) - DELTA(I)                   !
164 !                                                         !
165 ! 10   CONTINUE                                           !
166 !                                                         !
167 !      RETURN                                             !
168 !                                                         !
169 !      END                                                !
170 !                                                         !
171 !      SUBROUTINE F2 (NEQ, T, Y, YDOT)                    !
172 !                                                         !
173 !      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                !
174 !                                                         !
175 !      INTEGER NEQ                                        !
176 !                                                         !
177 !      DOUBLE PRECISION T, Y, YDOT                        !
178 !                                                         !
179 !      DIMENSION Y(*), YDOT(*)                            !
180 !                                                         !
181 !      YDOT(1) = Y(2)                                     !
182 !                                                         !
183 !      YDOT(2) = 100.0D0*(1.0D0 - Y(1)*Y(1))*Y(2) - Y(1)  !
184 !                                                         !
185 !      RETURN                                             !
186 !                                                         !
187 !      END                                                !
188 jac22=[...
189 '      SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR)';
190 ' ';
191 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
192 '      INTEGER  NROWPD';
193 '      DOUBLE PRECISION T, Y, PD';
194 '      PARAMETER (NROWPD=2)';
195 '      DIMENSION Y(2), PD(NROWPD,2)';
196 '      PD(1,1) = 0.0D0';
197 '      PD(1,2) = 1.0D0';
198 '      PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0';
199 '      PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1))';
200 '      PD(1,1) = CJ - PD(1,1)';
201 '      PD(1,2) =    - PD(1,2)';
202 '      PD(2,1) =    - PD(2,1)';
203 '      PD(2,2) = CJ - PD(2,2)';
204 '      RETURN';
205 '      END';]
206  jac22  =
207  
208 !      SUBROUTINE JAC22 (T, Y, ydot, PD, CJ, RPAR, IPAR)  !
209 !                                                         !
210 !                                                         !
211 !                                                         !
212 !      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                !
213 !                                                         !
214 !      INTEGER  NROWPD                                    !
215 !                                                         !
216 !      DOUBLE PRECISION T, Y, PD                          !
217 !                                                         !
218 !      PARAMETER (NROWPD=2)                               !
219 !                                                         !
220 !      DIMENSION Y(2), PD(NROWPD,2)                       !
221 !                                                         !
222 !      PD(1,1) = 0.0D0                                    !
223 !                                                         !
224 !      PD(1,2) = 1.0D0                                    !
225 !                                                         !
226 !      PD(2,1) = -200.0D0*Y(1)*Y(2) - 1.0D0               !
227 !                                                         !
228 !      PD(2,2) = 100.0D0*(1.0D0 - Y(1)*Y(1))              !
229 !                                                         !
230 !      PD(1,1) = CJ - PD(1,1)                             !
231 !                                                         !
232 !      PD(1,2) =    - PD(1,2)                             !
233 !                                                         !
234 !      PD(2,1) =    - PD(2,1)                             !
235 !                                                         !
236 !      PD(2,2) = CJ - PD(2,2)                             !
237 !                                                         !
238 !      RETURN                                             !
239 !                                                         !
240 !      END                                                !
241 gr22=[...
242 '      SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR)';
243 '      IMPLICIT DOUBLE PRECISION (A-H,O-Z)';
244 '      INTEGER NEQ, NG';
245 '      DOUBLE PRECISION T, Y, GROOT';
246 '      DIMENSION Y(*), GROOT(*)';
247 '      GROOT(1) = Y(1)';
248 '      RETURN';
249 '      END';]
250  gr22  =
251  
252 !      SUBROUTINE GR22 (NEQ, T, Y, NG, GROOT, RPAR, IPAR)  !
253 !                                                          !
254 !      IMPLICIT DOUBLE PRECISION (A-H,O-Z)                 !
255 !                                                          !
256 !      INTEGER NEQ, NG                                     !
257 !                                                          !
258 !      DOUBLE PRECISION T, Y, GROOT                        !
259 !                                                          !
260 !      DIMENSION Y(*), GROOT(*)                            !
261 !                                                          !
262 !      GROOT(1) = Y(1)                                     !
263 !                                                          !
264 !      RETURN                                              !
265 !                                                          !
266 !      END                                                 !
267 //Uncomment lines below: link may be machine dependent if some f77 libs are
268 //needed for linking
269 //unix_g('cd /tmp;rm -f /tmp/res22.f');unix_g('cd /tmp;rm -f /tmp/gr22.f');
270 //unix_g('cd /tmp;rm -f /tmp/jac22.f');
271 //write('/tmp/res22.f',res22);write('/tmp/gr22.f',gr22);write('/tmp/jac22.f',jac22)
272 //unix_g("cd /tmp;make /tmp/res22.o");unix_g('cd /tmp;make /tmp/gr22.o');
273 //unix_g('cd /tmp;make /tmp/jac22.o');
274 //          2  Linking the routines
275 //link('/tmp/res22.o','res22');link('/tmp/jac22.o','jac22');link('/tmp/gr22.o','gr22')
276 //rtol=[1.d-6;1.d-6];atol=[1.d-6;1.d-4];
277 //t0=0;y0=[2;0];y0d=[0;-2];t=[20:20:200];ng=1;
278 //          3 Calling the routines by dasrt
279 //[yy,nn]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
280 // hot restart
281 //[yy,nn,hotd]=dasrt([y0,y0d],t0,t,atol,rtol,'res22','jac22',ng,'gr22',info);
282 //t01=nn(1);t=100:20:200;[pp,qq]=size(yy);y01=yy(2:3,qq);y0d1=yy(3:4,qq);
283 //[yy,nn,hotd]=dasrt([y01,y0d1],t01,t,atol,rtol,'res22','jac22',ng,'gr22',info,hotd);