Registered Member
|
I have a project for solving transient heat transfer problem. I use matrices and vectors (both with large sizes) to store state of the heat transfer during each time level. During next time level, I have to reinitialize those matrices and vectors to zero with same sparsity of the matrices.
One way to reuse those matrices and vectors is simply to assign a new matrix or vector to them with 0 initialized coeffs. For example, [code] m=SparseMatrix<size1, size2); // For sparse matrix m= MatrixXd::Zero(size1, size2); // For dense matrix v=Vectorxd:::Zero(size); // For dense vector [/quote] However this method will cause a lot of unnecessary memory de-allocation. I am wondering if there is any faster and elegant way to simply reinitialization of matrices and vectors for reuse with memory de-allocation? |
Moderator
|
For dense objects this is easy because m= MatrixXd::Zero(size1, size2); does not allocate anything. It's like a memset to 0.
For sparse objects, re-exploiting the sparcity is a bit more tricky and depend on how you specify the values. I see two simple options: 1 - use mat.coeffRef(i,j) = ...; to overwrite the value. 2- loop over the non-zeros using an iterator and set the values through the iterator: it.valueRef() = ....; (see the tutorial if you don't know about sparse iterators). You might starts with 2 to set the values to zero, and then 1 to accumulate new values. However, it might also be faster to simply trash your data and directly re-set it with setFromTriplets(...); |
Registered Member
|
The setFromTriplets method is what I have been using for reinitializing the sparse matrix. It seems that I am doing OK based on what you explained. Thanks. |
Registered users: Baidu [Spider], Bing [Bot], Google [Bot]