Setting up ode solver options to speed up compute time
    6 visualizaciones (últimos 30 días)
  
       Mostrar comentarios más antiguos
    
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 comentarios
  Torsten
      
      
 el 23 de Mayo de 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
      
 el 25 de Mayo de 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.
Respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


