/usr/share/octave/site/m/sundialsTB/cvodes/examples_ser/mcvsRoberts_ASAi_dns.m is in octave-sundials 2.5.0-3+b1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 | function mcvsRoberts_ASAi_dns()
%mcvsRoberts_ASAi_dns - CVODES adjoint sensitivity example problem (serial, dense)
% The following is a simple example problem, with the coding
% needed for its solution by CVODES. The problem is from
% chemical kinetics, and consists of the following three rate
% equations:
% dy1/dt = -p1*y1 + p2*y2*y3
% dy2/dt = p1*y1 - p2*y2*y3 - p3*(y2)^2
% dy3/dt = p3*(y2)^2
% on the interval from t = 0.0 to t = 4.e10, with initial
% conditions: y1 = 1.0, y2 = y3 = 0. The problem is stiff.
%
% This program solves the problem with the BDF method,
% Newton iteration with the CVDENSE dense linear solver, and a
% user-supplied Jacobian routine. It uses a scalar relative
% tolerance and a vector absolute tolerance.
%
% The gradient with respect to the problem parameters p1, p2,
% and p3 of the following quantity:
% G = int_t0^t1 y3(t) dt
% is computed using ASA.
%
% Writing the original ODE as:
% dy/dt = f(y,p)
% y(t0) = y0(p)
% then the gradient with respect to the parameters p of
% G(p) = int_t0^t1 g(y,p) dt
% is obtained as:
% dG/dp = int_t0^t1 (g_p + lambda^T f_p ) dt + lambda^T(t0)*y0_p
% = -xi^T(t0) + lambda^T(t0)*y0_p
% where lambda and xi are solutions of:
% d(lambda)/dt = - (f_y)^T * lambda - (g_y)^T
% lambda(t1) = 0
% and
% d(xi)/dt = (g_p)^T + (f_p)^T * lambda
% xi(t1) = 0
%
% During the forward integration, CVODES also evaluates G as
% G = q(t1)
% where
% dq/dt = g(t,y,p)
% q(t0) = 0
% Radu Serban <radu@llnl.gov>
% Copyright (c) 2005, The Regents of the University of California.
% $Revision: 1.2 $Date: 2009/04/26 23:26:45 $
% ----------------------------------------
% User data structure
% ----------------------------------------
data.p = [0.04; 1.0e4; 3.0e7];
% ----------------------------------------
% Forward CVODES options
% ----------------------------------------
options = CVodeSetOptions('UserData',data,...
'RelTol',1.e-6,...
'AbsTol',[1.e-8; 1.e-14; 1.e-6],...
'LinearSolver','Dense',...
'JacobianFn',@djacfn);
mondata = struct;
mondata.sol = true;
mondata.mode = 'both';
options = CVodeSetOptions(options,...
'MonitorFn',@CVodeMonitor,...
'MonitorData',mondata);
t0 = 0.0;
y0 = [1.0;0.0;0.0];
CVodeInit(@rhsfn, 'BDF', 'Newton', t0, y0, options);
optionsQ = CVodeQuadSetOptions('ErrControl',true,...
'RelTol',1.e-6,'AbsTol',1.e-6);
q0 = 0.0;
CVodeQuadInit(@quadfn, q0, optionsQ);
% ----------------------------------------
% Initialize ASA
% ----------------------------------------
CVodeAdjInit(150, 'Hermite');
% ----------------------------------------
% Forward integration
% ----------------------------------------
fprintf('Forward integration ');
tout = 4.e7;
[status,t,y,q] = CVode(tout,'Normal');
s = CVodeGetStats;
fprintf('(%d steps)\n',s.nst);
fprintf('G = %12.4e\n',q);
fprintf('\nCheck point info\n');
ck = CVodeGet('CheckPointsInfo');
fprintf([' t0 t1 nstep order step size\n']);
for i = 1:length(ck)
fprintf('%8.3e %8.3e %4d %1d %10.5e\n',...
ck(i).t0, ck(i).t1, ck(i).nstep, ck(i).order, ck(i).step);
end
fprintf('\n');
% ----------------------------------------
% Backward CVODES options
% ----------------------------------------
optionsB = CVodeSetOptions('UserData',data,...
'RelTol',1.e-6,...
'AbsTol',1.e-8,...
'LinearSolver','Dense',...
'JacobianFn',@djacBfn);
mondataB = struct;
mondataB.mode = 'both';
optionsB = CVodeSetOptions(optionsB,...
'MonitorFn','CVodeMonitorB',...
'MonitorData', mondataB);
tB1 = 4.e7;
yB1 = [0.0;0.0;0.0];
idxB = CVodeInitB(@rhsBfn, 'BDF', 'Newton', tB1, yB1, optionsB);
optionsQB = CVodeQuadSetOptions('ErrControl',true,...
'RelTol',1.e-6,'AbsTol',1.e-3);
qB1 = [0.0;0.0;0.0];
CVodeQuadInitB(idxB, @quadBfn, qB1, optionsQB);
% ----------------------------------------
% Backward integration
% ----------------------------------------
fprintf('Backward integration ');
[status,t,yB,qB] = CVodeB(t0,'Normal');
sB=CVodeGetStatsB(idxB);
fprintf('(%d steps)\n',sB.nst);
fprintf('tB1: %12.4e\n',tB1);
fprintf('dG/dp: %12.4e %12.4e %12.4e\n',...
-qB(1)+yB(1), -qB(2)+yB(2), -qB(3)+yB(3));
fprintf('lambda(t0): %12.4e %12.4e %12.4e\n',...
yB(1),yB(2),yB(3));
% ----------------------------------------
% Free memory
% ----------------------------------------
CVodeFree;
% ===========================================================================
function [yd, flag, new_data] = rhsfn(t, y, data)
% Right-hand side function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
yd(1) = -r1*y(1) + r2*y(2)*y(3);
yd(3) = r3*y(2)*y(2);
yd(2) = -yd(1) - yd(3);
flag = 0;
new_data = [];
return
% ===========================================================================
function [qd, flag, new_data] = quadfn(t, y, data)
% Forward quadrature integrand function
qd = y(3);
flag = 0;
new_data = [];
return
% ===========================================================================
function [J, flag, new_data] = djacfn(t, y, fy, data)
% Dense Jacobian function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
J(1,1) = -r1;
J(1,2) = r2*y(3);
J(1,3) = r2*y(2);
J(2,1) = r1;
J(2,2) = -r2*y(3) - 2*r3*y(2);
J(2,3) = -r2*y(2);
J(3,2) = 2*r3*y(2);
flag = 0;
new_data = [];
return
% ===========================================================================
function [yBd, flag, new_data] = rhsBfn(t, y, yB, data)
% Backward problem right-hand side function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
y1 = y(1);
y2 = y(2);
y3 = y(3);
l1 = yB(1);
l2 = yB(2);
l3 = yB(3);
l21 = l2-l1;
l32 = l3-l2;
y23 = y2*y3;
yBd(1) = - r1*l21;
yBd(2) = r2*y3*l21 - 2.0*r3*y2*l32;
yBd(3) = r2*y2*l21 - 1.0;
flag = 0;
new_data = [];
return
% ===========================================================================
function [qBd, flag, new_data] = quadBfn(t, y, yB, data)
% Backward problem quadrature integrand function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
y1 = y(1);
y2 = y(2);
y3 = y(3);
l1 = yB(1);
l2 = yB(2);
l3 = yB(3);
l21 = l2-l1;
l32 = l3-l2;
y23 = y2*y3;
qBd(1) = y1*l21;
qBd(2) = -y23*l21;
qBd(3) = l32*y2^2;
flag = 0;
new_data = [];
return
% ===========================================================================
function [JB, flag, new_data] = djacBfn(t, y, yB, fyB, data)
% Backward problem Jacobian function
J = djacfn(t,y,[],data);
JB = -J';
flag = 0;
new_data = [];
return
|