/usr/share/octave/site/m/sundialsTB/idas/examples_ser/midasBruss_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 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 | function midasBruss_dns
%midasBruss_dns Brusselator example
% This example solves the 2D Brusselator example on an (mx)x(my)
% grid of the square with side L, using the MOL with central
% finite-differences for the semidiscretization in space.
% Homogeneous BC on all sides are incorporated as algebraic
% constraints.
% 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 data
%--------------
eps = 2.0e-3; % diffusion param
A = 1.0;
B = 3.4;
% Spatial length 0 <= x,y, <= L
L = 1.0;
% grid size
mx = 20;
my = 20;
dx = L/mx;
dy = L/my;
% coefficients in central FD
hdif = eps/dx^2;
vdif = eps/dy^2;
% problem dimension
nx = mx+1;
ny = my+1;
n = 2*nx*ny;
x = linspace(0,L,nx);
y = linspace(0,L,ny);
data.eps = eps;
data.A = A;
data.B = B;
data.L = L;
data.dx = dx;
data.dy = dy;
data.nx = nx;
data.ny = ny;
data.x = x;
data.y = y;
data.hdif = hdif;
data.vdif = vdif;
%-------------------
% Initial conditions
%-------------------
[u0, v0] = BRUSic(data);
Y0 = UV2Y(u0, v0, data);
Yp0 = zeros(n,1);
% ---------------------
% Initialize integrator
% ---------------------
% Integration limits
t0 = 0.0;
tf = 1.0;
% Specify algebraic variables
u_id = ones(ny,nx);
u_id(1,:) = 0;
u_id(ny,:) = 0;
u_id(:,1) = 0;
u_id(:,nx) = 0;
v_id = ones(ny,nx);
v_id(1,:) = 0;
v_id(ny,:) = 0;
v_id(:,1) = 0;
v_id(:,nx) = 0;
id = UV2Y(u_id, v_id, data);
options = IDASetOptions('UserData',data,...
'RelTol',1.e-5,...
'AbsTol',1.e-5,...
'VariableTypes',id,...
'suppressAlgVars','on',...
'MaxNumSteps', 1000,...
'LinearSolver','Dense');
% Initialize IDAS
IDAInit(@BRUSres,t0,Y0,Yp0,options);
% Compute consistent I.C.
[status, Y0, Yp0] = IDACalcIC(tf, 'FindAlgebraic');
% ---------------
% Integrate to tf
% ---------------
plotSol(t0,Y0,data);
[status, t, Y] = IDASolve(tf, 'Normal');
plotSol(t,Y,data);
% -----------
% Free memory
% -----------
IDAFree;
%%save foo.mat t Y data
return
% ====================================================================================
% Initial conditions
% ====================================================================================
function [u0, v0] = BRUSic(data)
dx = data.dx;
dy = data.dy;
nx = data.nx;
ny = data.ny;
L = data.L;
x = data.x;
y = data.y;
n = 2*nx*ny;
[x2D , y2D] = meshgrid(x,y);
u0 = 1.0 - 0.5 * cos(pi*y2D/L);
u0(1,:) = u0(2,:);
u0(ny,:) = u0(ny-1,:);
u0(:,1) = u0(:,2);
u0(:,nx) = u0(:,nx-1);
v0 = 3.5 - 2.5*cos(pi*x2D/L);
v0(1,:) = v0(2,:);
v0(ny,:) = v0(ny-1,:);
v0(:,1) = v0(:,2);
v0(:,nx) = v0(:,nx-1);
return
% ====================================================================================
% 1D <-> 2D conversion functions
% ====================================================================================
function y = UV2Y(u, v, data)
nx = data.nx;
ny = data.ny;
u1 = reshape(u, 1, nx*ny);
v1 = reshape(v, 1, nx*ny);
y = reshape([u1;v1], 2*nx*ny,1);
return
function [u,v] = Y2UV(y, data)
nx = data.nx;
ny = data.ny;
y2 = reshape(y, 2, nx*ny);
u = reshape(y2(1,:), ny, nx);
v = reshape(y2(2,:), ny, nx);
return
% ====================================================================================
% Residual function
% ====================================================================================
function [res, flag, new_data] = BRUSres(t,Y,Yp,data)
dx = data.dx;
dy = data.dy;
nx = data.nx;
ny = data.ny;
eps = data.eps;
A = data.A;
B = data.B;
L = data.L;
hdif = data.hdif;
vdif = data.vdif;
% Convert Y and Yp to (u,v) and (up, vp)
[u,v] = Y2UV(Y,data);
[up,vp] = Y2UV(Yp,data);
% 2D residuals
ru = zeros(ny,nx);
rv = zeros(ny,nx);
% Inside the domain
for iy = 2:ny-1
for ix = 2:nx-1
uu = u(iy,ix);
vv = v(iy,ix);
ru(iy,ix) = up(iy,ix) - ...
hdif * ( u(iy,ix+1) - 2*uu + u(iy,ix-1) ) - ...
vdif * ( u(iy+1,ix) - 2*uu + u(iy-1,ix) ) - ...
A + (B+1)*uu - uu^2 * vv;
rv(iy,ix) = vp(iy,ix) - ...
hdif * ( v(iy,ix+1) - 2*vv + v(iy,ix-1) ) - ...
vdif * ( v(iy+1,ix) - 2*vv + v(iy-1,ix) ) - ...
B*uu + uu^2 * vv;
end
end
% Boundary conditions
ru(1,:) = u(1,:) - u(2,:);
ru(ny,:) = u(ny,:) - u(ny-1,:);
ru(:,1) = u(:,1) - u(:,2);
ru(:,nx) = u(:,nx) - u(:,nx-1);
rv(1,:) = v(1,:) - v(2,:);
rv(ny,:) = v(ny,:) - v(ny-1,:);
rv(:,1) = v(:,1) - v(:,2);
rv(:,nx) = v(:,nx) - v(:,nx-1);
% Convert (ru,rv) to res
res = UV2Y(ru,rv,data);
% Return flag and pb. data
flag = 0;
new_data = [];
% ====================================================================================
% Plot (u,v)
% ====================================================================================
function plotSol(t,Y,data)
x = data.x;
y = data.y;
[u,v] = Y2UV(Y, data);
figure;
set(gcf,'position',[600 600 650 300])
subplot(1,2,1)
surfc(x,y,u);
shading interp
%view(0,90)
view(-15,35)
axis tight
box on
grid off
xlabel('x');
ylabel('y');
title(sprintf('u(x,y,%g)',t))
%colorbar('horiz');
subplot(1,2,2)
surfc(x,y,v);
shading interp
%view(0,90)
view(-15,35)
axis tight
box on
grid off
xlabel('x');
ylabel('y');
title(sprintf('v(x,y,%g)',t))
%colorbar('horiz');
return
|