@@ -17,9 +17,9 @@ bool mpm::AssemblerEigenImplicitLinear<Tdim>::assemble_stiffness_matrix() {
1717 stiffness_matrix_.resize (active_dof_ * Tdim, active_dof_ * Tdim);
1818 stiffness_matrix_.setZero ();
1919
20- // Reserve storage for sparse matrix
21- stiffness_matrix_. reserve (
22- Eigen::VectorXi::Constant (active_dof_ * Tdim, sparse_row_size_ * Tdim) );
20+ // Triplets and reserve storage for sparse matrix
21+ std::vector<Eigen::Triplet< double >> tripletList;
22+ tripletList. reserve (active_dof_ * Tdim * sparse_row_size_ * Tdim);
2323
2424 // Cell pointer
2525 const auto & cells = mesh_->cells ();
@@ -43,17 +43,23 @@ bool mpm::AssemblerEigenImplicitLinear<Tdim>::assemble_stiffness_matrix() {
4343 for (unsigned j = 0 ; j < nids.size (); ++j) {
4444 for (unsigned k = 0 ; k < Tdim; ++k) {
4545 for (unsigned l = 0 ; l < Tdim; ++l) {
46- stiffness_matrix_.coeffRef (
47- nactive_node * k + global_node_indices_.at (cid)(i),
48- nactive_node * l + global_node_indices_.at (cid)(j)) +=
49- cell_stiffness (Tdim * i + k, Tdim * j + l);
46+ if (std::abs (cell_stiffness (Tdim * i + k, Tdim * j + l)) >
47+ std::numeric_limits<double >::epsilon ())
48+ tripletList.push_back (Eigen::Triplet<double >(
49+ nactive_node * k + global_node_indices_.at (cid)(i),
50+ nactive_node * l + global_node_indices_.at (cid)(j),
51+ cell_stiffness (Tdim * i + k, Tdim * j + l)));
5052 }
5153 }
5254 }
5355 }
5456 ++cid;
5557 }
5658 }
59+
60+ // Fast assembly from triplets
61+ stiffness_matrix_.setFromTriplets (tripletList.begin (), tripletList.end ());
62+
5763 } catch (std::exception& exception) {
5864 console_->error (" {} #{}: {}\n " , __FILE__, __LINE__, exception.what ());
5965 status = false ;
0 commit comments