Sparsity pattern building issue (dealii version 9.2.0)

58 views
Skip to first unread message

Sudip Kunda

unread,
Aug 5, 2024, 7:57:20 AM8/5/24
to deal.II User Group
Hi all,

I have a code that works fine for one element, but won't compile for even one level of refinement (8 elements). It solves a small deformation problem of elastic compression of a cube of side length 1. The edges of the cube are parallel to the coordinate axes and three of its faces are on the coordinate planes. The code assumes that the Young's modulus, Poisson's ratio and the speed at which the "top" surface of the cube is moving are defined in a parameters.h file.

I have attached my code to this message. When I try to refine the mesh (say 8 elements) I get the following message from the compiler 

--------------------------------------------------------
An error occurred in line <2014> of file </home/skunda/built-programs/dealii-9.2.0/install/include/deal.II/lac/sparse_matrix.h> in function
    number& dealii::SparseMatrix<number>::operator()(dealii::SparseMatrix<number>::size_type, dealii::SparseMatrix<number>::size_type) [with number = double; dealii::SparseMatrix<number>::size_type = unsigned int]
The violated condition was:
    cols->operator()(i, j) != SparsityPattern::invalid_entry
Additional information:
    You are trying to access the matrix entry with index <0,24>, but this entry does not exist in the sparsity pattern of this matrix.

The most common cause for this problem is that you used a method to build the sparsity pattern that did not (completely) take into account all of the entries you will later try to write into. An example would be building a sparsity pattern that does not include the entries you will write into due to constraints on degrees of freedom such as hanging nodes or periodic boundary conditions. In such cases, building the sparsity pattern will succeed, but you will get errors such as the current one at one point or other when trying to write into the entries of the matrix.

Stacktrace:
-----------
#0  ./main: dealii::SparseMatrix<double>::operator()(unsigned int, unsigned int)
#1  ./main: Problem<3>::assemble_system()
#2  ./main: Problem<3>::run()
#3  ./main: main
--------------------------------------------------------

I am using the following 4 lines to make the sparsity pattern and the system matrix

    DynamicSparsityPattern dsp(dof_handler.n_dofs(), dof_handler.n_dofs());
    DoFTools::make_sparsity_pattern(dof_handler, dsp);
    sparsity_pattern.copy_from(dsp);
    system_matrix.reinit(sparsity_pattern);

I cannot see the mistake I am making while building the sparsity pattern. Any help is appreciated.

Best Regards,
Sudip Kunda
main.cc

Marc Fehling

unread,
Aug 6, 2024, 8:37:53 AM8/6/24
to deal.II User Group
Hi Sudip,

this is the only place in assemble_system() in which you call SparseMatrix<double>::operator()

/*std::cout << "Before applying boundary conditions" << std::endl;*/
for (unsigned int i = 0; i < dof_handler.n_dofs(); i++) {
  for (unsigned int j = 0; j < dof_handler.n_dofs(); j++) { 
    text_output_file << system_matrix(i, j) << " "; 
  } text_output_file << std::endl;
}
/* std::cout << solution(i) << " " << system_rhs(i) << std::endl;*/

system_matrix is a Sparse Matrix which does not store all elements. You are asking for elements in the matrix that do not exist, and the function throws an exception.

Best,
Marc

Sudip Kunda

unread,
Aug 7, 2024, 9:48:55 AM8/7/24
to deal.II User Group
Hello Marc,

Thanks for that ! I forgot to account for the fact that I was printing the matrix to a text file naively using nested loops. I just came across the function print_formatted() in the SparseMatrix class which let's me accomplish what I was looking to do.

Best Regards,
Sudip Kunda

Reply all
Reply to author
Forward
0 new messages