How do I speed up ODE15s?

Hello everybody I am trying to speed up a MATLAB code which numerically solves for deactivation process in a catalyst. The algorithm of the code which simply solves mass and momentum conservation equations plus an equation for chemical reaction over time and space(1D) is like below:
main()
statements
Initial values and boundary conditions
Timespan=[0 3600] // By discretizing equations in Z direction we get ODEs and then implement stiff ode solver to sweep in time
[t,w]=ODE15s(@transientfunction,timespan,w0)
outputs+graphs
end
transientfunction()
"FOR" loop // "for" loop is implemented to allocate the differential values at each cell which is around 100 cells.
end
***************************************************************Problem******************************************************
My problem here is that simulation of one hour (3600s) of deactivation process takes two days!!!(at the end I want to simulate it for 5years which I guess my grandson can check the result and be proud of his grandpa) I have tried to use "parfor" in transientfunction with 12 workers but not only the speed did not increase but also decreased! Time step in ODE15s solver is,specially at the beginning, gravely small. Is there any suggestion or advice out there that might help me or help this nice and lovely ode solver to speed up I look forward to your suggestions Thank you all Rasoul

4 Comments

Hello Rasoul,
Is there a reason that you chose to use ode15s over one of the other ODE solvers? Are you sure that your system is stiff? Is it less efficient or less accurate if you use ode45?
One of the things slowing it down is likely the for loop in the function called at each timestep. Is there any way you can vectorize the code?
-Cam
Rasoul Sheikhi
Rasoul Sheikhi on 4 Oct 2017
Edited: Rasoul Sheikhi on 4 Oct 2017
Hello Cam Thank you for your response. I chose ode15s due to accuracy. ode45 does not provide very accurate results and in any way the computation time is not decreased noticeably by using ode45. On the other hand,the statements in the "for" loop inside the function are so short that by using "parfor" the computation time increases. As far as I know about vectorization the code is already vectorized. If interested, I can send you part of the code to have your generous comments on it.
Regards
Rasoul
Hey Rasoul,
Maybe just post a couple of snippets, showing the for loop limits and a couple of assignments if possible. I can try to have a look on here.
I did just notice you mentioning assigning about 100 cells. I'm assuming that means you're returning a vector of length ~100 from your transientfunction? That's a lot of variables to solve over, and fairly uncommon for these types of problems. I'm not sure if it's possible to narrow that down (very problem dependent) or maybe use a different framework (optimization to get close first or something)?
Definitely don't go the way of the parfor in the transient function. Way too much overhead for something that gets called that often.
-Cam
Hello Cam Thank you for your response. Here is the ode15s command I am using:
tspan = [0 3600];
tic
% solver
[t,w] = ode15s(@transadfunc,tspan,w0);
toc
and here is what "transadfunc" returns: dwdt(1:n,1) = dcCOdt(1:n,1);
dwdt(n+1:2*n,1) = dcH2dt(1:n,1);
dwdt(2*n+1:3*n,1) = dcH2Odt(1:n,1);
dwdt(3*n+1:4*n,1) = dcCH4dt(1:n,1);
dwdt(4*n+1:5*n,1) = dcCO2dt(1:n,1);
dwdt(5*n+1:6*n,1) = dcN2dt(1:n,1);
dwdt(6*n+1:7*n,1) = dTdt(1:n,1);
dwdt(7*n+1:8*n,1) = dcSdt(1:n,1);
dwdt(8*n+1:9*n,1) = dadt(1:n,1); n is number of discretized cells in the domain and it can have values about 1000-2000.
Regards Rasoul

Sign in to comment.

 Accepted Answer

Josh Meyer
Josh Meyer on 21 Sep 2017

2 votes

Some things to try...
  1. For stiff problems you should always specify the Jacobian or its sparsity pattern via the options structure. Sometimes this can greatly improve performance since the solver estimates it numerically when you don't specify it. See odeset for more info.
  2. Try some of the other stiff solvers since they use a slightly cruder tolerance to calculate steps ( ode23s, ode23t, ode23tb). Since your ODE function contains a loop the fastest solver is likely to be the one with the fewest function evaluations. You can check that by setting 'Stats','on' with odeset and comparing the stats of each solver.
  3. If your equations include some conservation equations then you might have a system of Differential Algebraic Equations. If that's the case then you can continue using ode15s but might need to set some options as outlined on the linked page.

1 Comment

Hello Joshua Thank you for your answer. My case exactly lies on 3rd scenario you mentioned i.e. a system of DAEs. I have tried manipulating some options such as Relative and Absolute tolerances but they did not work! Guess I need to change my approach :/

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!