Updating Sparse Matrices every Time-Step Efficiently
5 views (last 30 days)
I am working on a numerical scheme to solve a fourth-order 2D partial differential equation. As such, my scheme boils down to solving the following linear system of equations at each time-step:
After solving this, I then need to update the coefficients in for the next time-step. For most simulations, I am using points in x and points in y which make the matrices quite large and so my code is quite slow at the moment. In trying to figure out how to optimize it, I came across this article: Creating Sparse Matrices.
However, I do not want to create a new matrix but rather only edit a certain number of entries. I set up my vectors so that . Now my question is how to make these changes without creating a new matrix and wiping out the non-zero elements that don't need to be updated. The command I tried was:
but this didn't work as the two sides were of different dimensions. I'm kind of at a loss for how else this could be accomplished without recreating the matrix from scratch each time step. For reference, at the above values of , the matrix contains non-zero elements. Ideally, I would like to increase those even more so building the matrix every time step seems like it wouldn't really speed things up.
My matrix does have a fairly basic structure to it, however, where the five main diagonals contain the majority of the information and then there are diagonals elements away in both directions with non-zero information as well.
James Tursa on 10 Jun 2022
Edited: James Tursa on 10 Jun 2022
To specify the sparse matrix size when creating it, use the following syntax:
where m is the number of rows and n is the number of columns.
That being said, this will of course create the sparse matrix from scratch each iteration using the I, J, K variables, so you will incur that overhead.
Changing the existing sparse matrix directly instead will also incur some overhead since the existing elements will have to be moved in memory. You will probably have to experiment to see which method is faster for you based on your particular matrix structure.
Question: When you update Q, do you simply modify existing non-zero values to different non-zero values, or do you insert/delete other non-zero values into the matrix? I.e., do you change the sparse matrix pattern at each iteration or does this sparse matrix pattern remain the same at each iteration?