/usr/share/octave/site/m/sundialsTB/idas/examples_ser/midasRoberts_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 | function midasRoberts_ASAi_dns()
%midasRoberts_ASAi_dns - IDAS ASA example problem (serial, dense)
% The following is a simple example problem, with the coding
% needed for its solution by IDAS. 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
% 0 = y1 + y2 + y3 - 1
% on the interval from t = 0.0 to t = 4.e7, with initial
% conditions: y1 = 1.0, y2 = y3 = 0. The problem is stiff.
% While integrating the system, we also use the rootfinding
% feature to find the points at which y1 = 1e-4 or at which
% y3 = 0.01.
%
% 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.
%
% The gradient dG/dp is obtained as:
% dG/dp = [ int_t0^tf y1*(l1-l2) dt ,
% int_t0^tf -y2*y3*(l1-l2) dt ,
% int_t0^tf y2^2*l2 dt ]
%
% where l = [l1, l2, l3] is solutions of:
% dl1/dt = p1*l1 - p1*l2 + l3
% dl2/dt = -p2*y3*l1 + (p2*y3+2*p3*y2)*l2 + l3
% 0 = -p2*y2*l1 + p2*y2*l2 + l3 + 1
% with final conditions
% l1(tf) = l2(tf) = 0.0 and l3(tf) = -1.0
%
% All integrals (appearing in G and dG/dp) are computed using
% the quadrature integration features in IDAS.
% Radu Serban <radu@llnl.gov>
% Copyright (c) 2007, The Regents of the University of California.
% $Revision: 1.1 $Date: 2007/10/26 16:30:48 $
% Problem parameters
% ------------------
data.p = [0.04; 1.0e4; 3.0e7];
% Initialize forward problem
% --------------------------
options = IDASetOptions('UserData', data,...
'RelTol',1.e-4,...
'AbsTol',[1.e-8; 1.e-14; 1.e-6],...
'LinearSolver','Dense',...
'JacobianFn',@djacfn);
%mondata.sol = true;
%mondata.updt = 100;
%options = IDASetOptions(options,'MonitorFn',@IDAMonitor,'MonitorData',mondata);
t0 = 0.0;
y = [1.0;0.0;0.0];
yp = [-0.04;0.04;0.0];
IDAInit(@resfn,t0,y,yp,options);
% Initialize forward quadrature (G)
% ---------------------------------
optionsQ = IDAQuadSetOptions('ErrControl',true,...
'RelTol',1.e-4,'AbsTol',1.e-6);
q = 0.0;
IDAQuadInit(@quadfn, q, optionsQ);
% Activate ASA
% ------------
IDAAdjInit(150, 'Hermite');
% Forward integration
% -------------------
fprintf('Forward integration ');
tf = 4.e7;
[status, t, y, q] = IDASolve(tf,'Normal');
si = IDAGetStats;
fprintf('(%d steps)\n',si.nst);
fprintf('G = %12.4e\n',q(1));
% Initialize backward problem
% ---------------------------
optionsB = IDASetOptions('UserData',data,...
'RelTol',1.e-6,...
'AbsTol',1.e-3,...
'LinearSolver','Dense');
%mondataB = struct;
%optionsB = IDASetOptions(optionsB,'MonitorFn',@IDAMonitorB,'MonitorData',mondataB);
yB = [0.0 ; 0.0 ; -1.0];
yBp = [ -1.0 ; -1.0 ; 0.0 ];
idxB = IDAInitB(@resfnB,tf,yB,yBp,optionsB);
% Initialize backward quadratures (dG/dp)
% ---------------------------------------
optionsQB = IDAQuadSetOptions('ErrControl',true,...
'RelTol',1.e-6,'AbsTol',1.e-3);
qB = [0.0;0.0;0.0];
IDAQuadInitB(idxB, @quadfnB, qB, optionsQB);
% Backward integration
% --------------------
fprintf('Backward integration ');
[status, t, yB, qB] = IDASolveB(t0,'Normal');
siB = IDAGetStatsB(idxB);
fprintf('(%d steps)\n',siB.nst);
fprintf('dG/dp: %12.4e %12.4e %12.4e\n',...
-qB(1),-qB(2),-qB(3));
fprintf('lambda(t0): %12.4e %12.4e %12.4e\n',...
yB(1),yB(2),yB(3));
% Free IDAS memory
% ----------------
IDAFree;
return
% ===========================================================================
function [rr, flag, new_data] = resfn(t, y, yp, data)
% DAE residual function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
rr(1) = -r1*y(1) + r2*y(2)*y(3) - yp(1);
rr(2) = r1*y(1) - r2*y(2)*y(3) - r3*y(2)*y(2) - yp(2);
rr(3) = y(1) + y(2) + y(3) - 1.0;
flag = 0;
new_data = [];
% ===========================================================================
function [J, flag, new_data] = djacfn(t, y, yp, rr, cj, data)
% Dense Jacobian function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
J(1,1) = -r1 - cj;
J(2,1) = r1;
J(3,1) = 1.0;
J(1,2) = r2*y(3);
J(2,2) = -r2*y(3) - 2*r3*y(2) - cj;
J(3,2) = 1.0;
J(1,3) = r2*y(2);
J(2,3) = -r2*y(2);
J(3,3) = 1.0;
flag = 0;
new_data = [];
% ===========================================================================
function [qd, flag, new_data] = quadfn(t, y, yp, data)
% Forward quadrature integrand function
qd = y(3);
flag = 0;
new_data = [];
return
% ===========================================================================
function [rrB, flag, new_data] = resfnB(t, y, yp, yB, yBp, data)
% Adjoint residual function
r1 = data.p(1);
r2 = data.p(2);
r3 = data.p(3);
rrB(1) = yBp(1) - r1*(yB(1)-yB(2)) - yB(3);
rrB(2) = yBp(2) + r2*y(3)*(yB(1)-yB(2)) - 2.0*r3*y(2)*yB(2) - yB(3);
rrB(3) = -r2*y(2)*(yB(1)-yB(2)) + yB(3) + 1.0;
flag = 0;
new_data = [];
return
% ===========================================================================
function [qBd, flag, new_data] = quadfnB(t, y, yp, yB, ypB, data)
% Backward problem quadrature integrand function
qBd(1) = y(1)*(yB(1)-yB(2));
qBd(2) = -y(2)*y(3)*(yB(1)-yB(2));
qBd(3) = y(2)^2*yB(2);
flag = 0;
new_data = [];
return
|