Hi all,

I am having a similar problem. I want to take a 3x3 BlockSparseMatrix m33 
and copy four of its blocks into a 2x2 BlockSparseMatrix m22. For instance:

m33 = [ A B C
             D E F
             G H I ]

>From this matrix, I would like to obtain the following:

m22 = [ E F
             H I ]

Timo, I have tried the solutions that you suggest, but none of them work, I 
keep getting a segmentation fault error. Could you please give me an 
alternative option?

Thank you so much.

Regards,

Alex

On Wednesday, April 29, 2015 at 9:28:59 AM UTC-6, Timo Heister wrote:
>
> Hey Afonso, 
>
> sorry, that doesn't work. 
>
> Why do you have two different sparsity patterns in the first place if 
> they have the exact same nonzero pattern? You can fix this by 
> replacing 
>
>    sparsity_pattern_55.reinit(5,5); 
>    sparsity_pattern_55.block(0,0).reinit (n_dofs_0, n_dofs_1, 
> dof_handler_55.max_couplings_between_dofs()); 
>    ... 
>
> by 
>
>    sparsity_pattern_55.reinit(sparsity_pattern_22); 
>
> If you can not do this for whatever reason, you have several options to 
> copy: 
> 1. replace the sparsity pattern after the fact (if you know they 
> should be identical): 
>
>             dst.block(0,0).reinit(src.block(0,0).get_sparsity_pattern()); 
>             dst.block(0,0).copy_from( src.block(0,0)); 
>
> 2. Copy element by element (this works even when the sparsity patterns 
> are different, but all values fit in): 
>
>             dst.block(0,0) = 0; 
>             SparseMatrix<double>::iterator it = src.block(0,0).begin(), 
>                 end = src.block(0,0).end(); 
>             for (; it!=end; ++it) 
>               { 
>                 dst.block(0,0).set(it->row(), it->column(), it->value()); 
>               } 
>
> Let me know if that helps. 
>
>
>
> On Mon, Apr 27, 2015 at 11:39 AM, Afonso Londero <[email protected] 
> <javascript:>> wrote: 
> > Hi Timo, 
> > Thanks for your answer. However now I get this error related to 
> > matrix_22.block(0,0).end(): 
> > 
> > /.../include/deal.II/lac/sparse_matrix.h:2022:59: error: no matching 
> > function for call to ‘distance(const 
> > dealii::SparseMatrixIterators::Iterator<double, false>&, const 
> > dealii::SparseMatrixIterators::Iterator<double, false>&)’ 
> >    Assert (static_cast<size_type>(std::distance (begin, end)) == m(), 
> >                                                            ^ 
> > /include/deal.II/base/exceptions.h:295:11: note: in definition of macro 
> > ‘Assert’ 
> >      if (!(cond)) 
> > \ 
> >            ^ 
> > /include/deal.II/lac/sparse_matrix.h:2022:59: note: candidate is: 
> >    Assert (static_cast<size_type>(std::distance (begin, end)) == m(), 
> > 
> >                                                            ^ 
> > 
> > 
> > 
> > On Saturday, April 25, 2015 at 2:53:30 PM UTC+2, Timo Heister wrote: 
> >> 
> >> Afonso, 
> >> 
> >> can you try: 
> >> 
> >> matrix_55.block(0,0).clear(); 
> >> matrix_55.block(0,0).copy_from(matrix_22.block(0,0).begin(), 
> >> matrix_22.block(0,0).end()); 
> >> 
> >> On Fri, Apr 24, 2015 at 6:19 AM, Afonso Londero <[email protected]> 
> wrote: 
> >> > Dear all, 
> >> > 
> >> > I have a block sparse matrix with 2x2 blocks (matrix_22). I would 
> like 
> >> > to 
> >> > copy these blocks into a 5x5 block sparse matrix (matrix_55), for 
> >> > example: 
> >> > 
> >> > matrix_22 = [A B] 
> >> >                    [C D] 
> >> > 
> >> > matrix_55 = [A B B B B] 
> >> >                    [B A B B B] 
> >> >                    [B C D .....] 
> >> >                    [      ...      ] 
> >> > 
> >> > I tried: 
> >> > 
> >> > 
> >> > BlockSparsityPattern          sparsity_pattern_55; 
> >> > BlockSparseMatrix<double>  matrix_55; 
> >> > 
> >> > BlockSparsityPattern       sparsity_pattern_22; 
> >> > BlockSparseMatrix<double>  matrix_22; 
> >> > 
> >> > 
> >> > sparsity_pattern_22.reinit(2,2); 
> >> > sparsity_pattern_22.block(0,0).reinit (n_dofs_0, n_dofs_1, 
> >> > dof_handler_22.max_couplings_between_dofs()); 
> >> > //... the same for other blocks 
> >> > 
> >> > sparsity_pattern_22.collect_sizes(); 
> >> > 
> >> > DoFTools::make_flux_sparsity_pattern (dof_handler_22, 
> >> > sparsity_pattern_22); 
> >> > sparsity_pattern_22.compress(); 
> >> > 
> >> > matrix_22.reinit (sparsity_pattern_55); 
> >> > 
> >> > // Now, the same procedure for the matrix 5x5, using a different 
> >> > dof_handler 
> >> > and sparsity pattern 
> >> > 
> >> > sparsity_pattern_55.reinit(5,5); 
> >> > sparsity_pattern_55.block(0,0).reinit (n_dofs_0, n_dofs_1, 
> >> > dof_handler_55.max_couplings_between_dofs()); 
> >> > ... the same for other blocks 
> >> > 
> >> > sparsity_pattern_55.collect_sizes(); 
> >> > 
> >> > DoFTools::make_flux_sparsity_pattern (dof_handler_55, 
> >> > sparsity_pattern_55); 
> >> > sparsity_pattern_55.compress(); 
> >> > 
> >> > matrix_55.reinit (sparsity_pattern_55); 
> >> > 
> >> > // Later, I fill matrix_22 with the appropriate entries. Now I want 
> to 
> >> > copy 
> >> > some blocks of matrix_22 into matrix_55 
> >> > 
> >> > matrix_55.block(0,0).copy_from(matrix_22.block(0,0); 
> >> > 
> >> > // But I get the error ExcDifferentSparsityPatterns() , because even 
> >> > though 
> >> > the sparsity patterns of these two blocks are the same, they point to 
> >> > different sparsity pattern objects. 
> >> > 
> >> > // I can get around by doing 
> >> > 
> >> > FullMatrix<double> aux; 
> >> > aux.copy_from(matrix_22.block(0,0)); 
> >> > matrix_55.block(m,m).copy_from(aux); 
> >> > 
> >> > 
> >> > This workaround doesn't look very smart and seems to defeat the 
> purpose 
> >> > of 
> >> > using sparse matrix ... 
> >> > Can anyone point me an alternative? Thank you. 
> >> > 
> >> > -- 
> >> > The deal.II project is located at http://www.dealii.org/ 
> >> > For mailing list/forum options, see 
> >> > https://groups.google.com/d/forum/dealii?hl=en 
> >> > --- 
> >> > You received this message because you are subscribed to the Google 
> >> > Groups 
> >> > "deal.II User Group" group. 
> >> > To unsubscribe from this group and stop receiving emails from it, 
> send 
> >> > an 
> >> > email to [email protected]. 
> >> > For more options, visit https://groups.google.com/d/optout. 
> >> 
> >> 
> >> 
> >> -- 
> >> Timo Heister 
> >> http://www.math.clemson.edu/~heister/ 
> > 
> > -- 
> > The deal.II project is located at http://www.dealii.org/ 
> > For mailing list/forum options, see 
> > https://groups.google.com/d/forum/dealii?hl=en 
> > --- 
> > You received this message because you are subscribed to the Google 
> Groups 
> > "deal.II User Group" group. 
> > To unsubscribe from this group and stop receiving emails from it, send 
> an 
> > email to [email protected] <javascript:>. 
> > For more options, visit https://groups.google.com/d/optout. 
>
>
>
> -- 
> Timo Heister 
> http://www.math.clemson.edu/~heister/ 
>

-- 
The deal.II project is located at http://www.dealii.org/
For mailing list/forum options, see 
https://groups.google.com/d/forum/dealii?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"deal.II User Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to