Setting up ode solver options to speed up compute time
Afficher commentaires plus anciens
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 commentaires
Deepa Maheshvare
le 21 Mai 2021
Bjorn Gustavsson
le 21 Mai 2021
When you write "Unfortunately, I cannot post my actual system here" you shout out most interested helpers. That phrase means that one would try to problem-shoot irrelevant toy-model-problems that will not help solving your real problem. That entire process will take much much more time to solve than necessary - this is frustrating for the kind souls that volunteer. You will now have to show enough interest in getting help by constructing a similar enough problem from a numerical standpoint that you share (same level of stiffness same everything else too except your "secret" parts.).
Torsten
le 21 Mai 2021
In your toy example, the Jacobian is constant over time. Is this also true for your real system ?
Deepa Maheshvare
le 21 Mai 2021
Deepa Maheshvare
le 21 Mai 2021
Torsten
le 21 Mai 2021
My guess is that specifying the pattern additionally to specifying the Jacobian itself might even slow down the solver because the nonzero positions have to be addressed in a complicated way. At least the gain in performance will be low in my opinion.
But what does your test example indicate ? I'm curious ...
Deepa Maheshvare
le 21 Mai 2021
Modifié(e) : Deepa Maheshvare
le 21 Mai 2021
Deepa Maheshvare
le 22 Mai 2021
Deepa Maheshvare
le 22 Mai 2021
Modifié(e) : Deepa Maheshvare
le 22 Mai 2021
Torsten
le 22 Mai 2021
I don't know about your problem and implementation. You could test by making random vectors for your solution variables and make Matlab generate the corresponding Jacobians.
Deepa Maheshvare
le 22 Mai 2021
Deepa Maheshvare
le 23 Mai 2021
Torsten
le 23 Mai 2021
de.mathworks.com/matlabcentral/fileexchange/15816-isalmost
Deepa Maheshvare
le 23 Mai 2021
Torsten
le 23 Mai 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
le 25 Mai 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.
Réponses (0)
Catégories
En savoir plus sur Mathematics dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!