[petsc-users] Insert values into matrix using MatSetValuesStencil

TAY wee-beng zonexo at gmail.com
Wed Sep 23 03:37:24 CDT 2015


Hi Timothée,

The matrix is created with 3 dof - u,v,w. So for each i,j,k, there are 3 
values. Actually I got 3 eqns from the u,v,w momentum eqns. They are not 
coupled together so I can also solve them individually. But I was told 
it's faster to group them together. The global indices is something like 
this:

i, j, k, ijk global indices, dof
1 1 1 1 u
1 1 1 2 v
1 1 1 3 w
2 1 1 4 u

...

Hope it's clearer now.

Ok, I changed the stencil, it's now working.

Thank you

Yours sincerely,

TAY wee-beng

On 23/9/2015 4:22 PM, Timothée Nicolas wrote:
> Can you also tell how you created the matrix ? Just in case you 
> created it with the 1 dof DMDA, it would not work if you try to input 
> values at places where it is not allocated (which could explain the 
> error message)
>
> 2015-09-23 17:18 GMT+09:00 Timothée Nicolas 
> <timothee.nicolas at gmail.com <mailto:timothee.nicolas at gmail.com>>:
>
>     The first thing that strikes me is your definition of the stencils
>
>     */MatStencil  :: row(6,1),col(6,7)/**/
>
>     /*
>     Why is it not defined with
>
>     */MatStencil  :: row(4,1),col(4,7)/**/
>     /*
>     instead ?
>
>     Where does the 6 come from ?
>
>     Timothée
>
>
>     2015-09-23 17:14 GMT+09:00 TAY wee-beng <zonexo at gmail.com
>     <mailto:zonexo at gmail.com>>:
>
>         Hi,
>
>         I have successfully used MatSetValuesStencil to insert values
>         into a Poisson eqn matrix which has 1 DOF (pressure). Now I'm
>         trying to insert values in a momentum eqn matrix which has 3
>         DOF (u,v,w)
>
>         However, I got the error:
>
>         /*[0]PETSC ERROR: --------------------- Error Message
>         ----------------------------*//*
>         *//*----------------------------------*//*
>         *//*[0]PETSC ERROR: Argument out of range*//*
>         *//*[0]PETSC ERROR: Inserting a new nonzero at (111,5) in the
>         matrix*//*
>         *//*[0]PETSC ERROR: See
>         http://www.mcs.anl.gov/petsc/documentation/faq.html for trou*//*
>         *//*ble shooting.*/
>
>         I wonder what's wrong.  For the momentum eqn, for each DOF, at
>         at node (dof,i,j,k), I have coupling i +/- 1, j +/- 1 and k +/- 1.
>
>         The error happens at 111,5, which corresponds to i = 2, j = 2,
>         k = 2, which is an internal node.
>
>         Here's part of my code below. Hope someone can help. Thanks!
>         */
>         /**/PetscScalar :: value_insert(7)/**/
>         /**/
>         /**/MatStencil  :: row(6,1),col(6,7)/**/
>         /**/
>         /**/ione = 1;   iseven = 7/**/
>         /**/
>         /**/if (cell_type == 'u') then/**/
>         /**/
>         /**/    offset = 1/**/
>         /**//**/
>         /**/else if (cell_type == 'v') then/**/
>         /**/
>         /**/    offset = 2/**/
>         /**//**/
>         /**/else if (cell_type == 'w') then/**/
>         /**/
>         /**/    offset = 3/**/
>         /**//**/
>         /**/end if/**/
>         /**/
>         /**/do k=ksta2,kend2/**/
>         /**/
>         /**/    do j = jsta2,jend2/**/
>         /**/
>         /**/        do i=2,size_x-1/**/
>         /**//**/
>         /**/            row(MatStencil_i,1) = i - 1/**/
>         /**//**/
>         /**/            row(MatStencil_j,1) = j - 1/**/
>         /**//**/
>         /**/            row(MatStencil_k,1) = k - 1/**/
>         /**//**/
>         /**/            row(MatStencil_c,1) = offset - 1/**/
>         /**//**/
>         /**/            value_insert = 0.d0/**/
>         /**//**/
>         /**/            col(MatStencil_i,3) = i + 1 - 1 !east/**/
>         /**//**/
>         /**/            col(MatStencil_j,3) = j - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,3) = k - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,3) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(3) = -(
>         1./(cell_x(i)%pd_E+cell_x(i+1)%pd_W))*(c_yz(j,k)%fc_E)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,5) = i - 1 - 1 !west/**/
>         /**//**/
>         /**/            col(MatStencil_j,5) = j - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,5) = k - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,5) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(5) = -(
>         1./(cell_x(i)%pd_W+cell_x(i-1)%pd_E))*(c_yz(j,k)%fc_E)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,2) = i - 1 !north/**/
>         /**//**/
>         /**/            col(MatStencil_j,2) = j + 1 - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,2) = k - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,2) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(2) = -(
>         1./(cell_y(j)%pd_N+cell_y(j+1)%pd_S))*(c_zx(i,k)%fc_N)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,4) = i - 1 !south/**/
>         /**//**/
>         /**/            col(MatStencil_j,4) = j - 1 - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,4) = k - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,4) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(4) = -(
>         1./(cell_y(j)%pd_S+cell_y(j-1)%pd_N))*(c_zx(i,k)%fc_N)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,6) = i - 1 !front/**/
>         /**//**/
>         /**/            col(MatStencil_j,6) = j - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,6) = k + 1 - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,6) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(6) = -(
>         1./(cell_z(k)%pd_F+cell_z(k+1)%pd_B))*(c_xy(i,j)%fc_F)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,7) = i - 1 !back/**/
>         /**//**/
>         /**/            col(MatStencil_j,7) = j - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,7) = k - 1 - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,7) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(7) = -(
>         1./(cell_z(k)%pd_B+cell_z(k-1)%pd_F))*(c_xy(i,j)%fc_F)*inv_Re/**/
>         /**//**/
>         /**/            col(MatStencil_i,1) = i - 1/**/
>         /**//**/
>         /**/            col(MatStencil_j,1) = j - 1/**/
>         /**//**/
>         /**/            col(MatStencil_k,1) = k - 1/**/
>         /**//**/
>         /**/            col(MatStencil_c,1) = offset - 1/**/
>         /**//**/
>         /**/            value_insert(1) = 2.*c(i,j,k)%vol/del_t -
>         (value_insert(2)+value_insert(3)+value_insert(4)+value_insert(5)+value_insert(6)+value_insert(7))/**/
>         /**//**/
>         /**/            call
>         MatSetValuesStencil(A_semi_xyz,ione,row,iseven,col,value_insert,INSERT_VALUES,ierr)/**/
>         /**//**/
>         /**/        end do/**/
>         /**//**/
>         /**/    end do/**/
>         /**/
>         /**/end do /*
>
>         Thank you
>
>         Yours sincerely,
>
>         TAY wee-beng
>
>
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20150923/d18fe6c2/attachment-0001.html>


More information about the petsc-users mailing list