I noticed this post, where spalloc and sparse are recommended for efficient assembly in Matlab. I personally use sparse assembling for simple cases.

However, when it comes to the case of coupled PDE, say, 3-PDE coupled, then the scalar unknown becomes a $3\times 3$ tensor. In this case, I can't figure out a way to exploit sparse(), and have to use for-loops to assemble.

When the final assembled sparse matrix is as large as $30\text{k}\times 30\text{k}$(on PC), the assembling process becomes really slow (~10min), while the final matrix solving step is still fast(~less than 3 seconds).

Is there any suggestion? Any generic solution is appreciated (not necessarily Matlab)

  • $\begingroup$ The point of that comment is to pre-generate row and column indices and the corresponding values as linear arrays which you then pass to sparse(). You do this in for-loops. So you should be able to replace your existing for-loops with ones that generate the indices and then pass the whole group to sparse(). $\endgroup$
    – Bill Barth
    Commented Aug 5, 2014 at 11:58
  • $\begingroup$ @BillBarth thanks, then for c/c++ implementation, what kind of procedure is used for assembling? $\endgroup$
    – lorniper
    Commented Aug 5, 2014 at 12:14
  • $\begingroup$ Well, you need a sparse library. PETSc, for example, has examples of how to do so. $\endgroup$
    – Bill Barth
    Commented Aug 5, 2014 at 12:34
  • 2
    $\begingroup$ For a system of PDE, it is more common to use a block sparse matrix format, which saves you a lot of memory and time in assembling the matrix. These are implemented in PETSc but not in matlab, so if you're getting to big matrices you may want to make the switch to new software. $\endgroup$ Commented Aug 5, 2014 at 15:58
  • $\begingroup$ Assembly through generating row and column indices is definitely the way to go. If you need to save memory (i.e. number of non-zero entries is large), you could try using (my) technique of writing the indices to disk and assembling the sparse matrix directly in memory. $\endgroup$ Commented Aug 6, 2014 at 20:34


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.