Setting up ode solver options to speed up compute time

2 views (last 30 days)
Hi All,
I'm specifying the `'JPattern', sparsity_pattern` in the ode options to speed up the compute time of my actual system. I am sharing a
sample code below to show how I set up the system using a toy example. Specifying the `JPattern` helped me in reducing the compute time from 2 hours to 7 min for my real system. I'd like to know if there are options (in addition to `JPatthen`) that I can specify to further decrease the compute time . I found the `Jacobian` option but I am not sure how to compute the Jacobian easily for my real system.
global mat1 mat2
mat1=[
1 -2 1 0 0 0 0 0 0 0;
0 1 -2 1 0 0 0 0 0 0;
0 0 1 -2 1 0 0 0 0 0;
0 0 0 1 -2 1 0 0 0 0;
0 0 0 0 1 -2 1 0 0 0;
0 0 0 0 0 1 -2 1 0 0;
0 0 0 0 0 0 1 -2 1 0;
0 0 0 0 0 0 0 1 -2 1;
];
mat2 = [
1 -1 0 0 0 0 0 0 0 0;
0 1 -1 0 0 0 0 0 0 0;
0 0 1 -1 0 0 0 0 0 0;
0 0 0 1 -1 0 0 0 0 0;
0 0 0 0 1 -1 0 0 0 0;
0 0 0 0 0 1 -1 0 0 0;
0 0 0 0 0 0 1 -1 0 0;
0 0 0 0 0 0 0 1 -1 0;
];
x0 = [1 0 0 0 0 0 0 0 0 0]';
tspan = 0:0.01:5;
f0 = fun(0, x0);
joptions = struct('diffvar', 2, 'vectvars', [], 'thresh', 1e-8, 'fac', []);
J = odenumjac(@fun,{0 x0}, f0, joptions);
sparsity_pattern = sparse(J~=0.);
options = odeset('Stats', 'on', 'Vectorized', 'on', 'JPattern', sparsity_pattern);
ttic = tic();
[t, sol] = ode15s(@(t,x) fun(t,x), tspan , x0, options);
ttoc = toc(ttic)
fprintf('runtime %f seconds ...\n', ttoc)
plot(t, sol)
function f = fun(t,x)
global mat1 mat2
% f = zeros('like', x)
% size(f)
f = zeros(size(x), 'like', x);
size(f);
f(1,:) = 0;
f(2:9,:) = mat1*x + mat2*x;
f(10,:) = 2*(x(end-1) - x(end));
% df = [f(1, :); f(2:9, :); f(10, :)];
end
Are there inbuilt options available for computing the Jacobian?
I tried something like the below
x = sym('x', [5 1]);
s = mat1*x + mat2*x;
J1 = jacobian(s, x)
But this takes huge time for large system.
Suggestions will be really appreciated.
Side note:
I would also like to know if there is someone on the forum to whom I can demonstrate my code and seek help to resolve the issue mentioned above.
Unfortunately, I cannot post my actual system here .
  19 Comments
Torsten
Torsten on 23 May 2021
Analytical Jacobian should be Jac_ana = advMat + diffMat.
Maybe you can just output J, J1 and Jac_ana and compare them directly.
Bjorn Gustavsson
Bjorn Gustavsson on 25 May 2021
@Deepa Maheshvare - if you're solving a diffusion-advection problem then maybe it is worthwhile to look at the PDE-solvers, if you have access to the pde-toolbox.

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Products


Release

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!