Using JuMP to solve the heat equation - performance tips requested

Hello everyone! I am studying MSc Mechatronics and currently working on my scientific project. It is regarding making the Julia code of 1D Heat equation faster. With this message, I have also attached the Julia source code. I had some ideas which I can implement but unsure of that. I have listed them below. I request everyone to enlighten me whether I am going in the right direction. Your ideas and suggestions are also most welcomed.

(1) While defining the system matrix and matrix A, maybe I can use “Staticarrays.jl” instead of “spdiagm”. Is it possible? If yes, how can I implement it?

(2) Searching for other optimizers instead of “Ipopt”.

(3) Can I use “Import Ipopt.optimizer” instead of using the whole library in the code? (Using whole library may occupy a lot more space and can also make the program slower to run).

Thank you for your time and I hope to receive suggestions and ideas!

I think you should profile the code to see where the bottlenecks are. Unless the execution is really quick, (3) is probably irrelevant. (1) may or may not make any difference.

  • For N = 11 you can probably either use StaticArrays or just normal vectors! Sparse matrices will only slow down things here.

(I don’t know what the scope of your project is, but PDE constrained optimisation is a big topic with very elegant and useful algorithms to compute the gradients faster with so-called adjoint equations.)

Two recommendations: first, carefully read and apply the performance tips, particularly the admonition to avoid non-constant global variables. Second, please share your code as text wrapped in triple backticks so other users can easily read and copy-paste it (see here).

I mean, this is just a generally inefficient algorithm for solving the heat equation. If you’re really looking to speed it up, you might want to try starting with a more efficient algorithm, like writing it in Fourier space and just doing a diagonal solve.

1 Like

The suggestions for static arrays are unlikely to help due to the way JuMP constructs problems. From a purely JuMP perspective, the formulation looks okay.

Given the formulation, what makes you think it is slow? Ipopt is pretty good for this sort of stuff. As Chris says, there are alternate algorithmic approaches that may be more efficient.

Thank you for your message! How can I implement Staticarrays in my code? Can you please guide me through that?

Thank you for your message. Actually it takes around 12-15 minutes to get the result and graphs after running this code. I need to reduce the program execution time and that is my task.

Hello Chris. Thank you for your message. What specifically makes this inefficient? Is this the current Euler algorithm that is used in the code? Can you please help me how can I write this code using Fourier space and solving the diagonals?

Please read Please read: make it easier to help you and provide your code as text that can be copy-pasted.

Did you take a look at where the time is being spent? I guess most of the 12 minutes is inside Ipopt. You have ~10^6 variables and constraints.

If you continue to use Ipopt, you should try a more efficient linear solver: https://github.com/jump-dev/Ipopt.jl#linear-solvers

I modified the program using StaticArrays, but it seems it is not making a significant impact on the speed. I am more convinced that I will have to change the Ipopt optimzer. Can you suggest efficient optimizers rather than Ipopt?

See my previous posts:

  • StaticArrays won’t do anything due to how JuMP constructs problems internally
  • You should try a different linear solver for Ipopt