[petsc-users] Reading matrices into PETSc

Matthew Knepley knepley at gmail.com
Tue Mar 8 07:14:53 CST 2011


On Tue, Mar 8, 2011 at 4:38 AM, Tomasz Jankowski <tomjan at jay.au.poznan.pl>wrote:

> Matlab pseudo code
>> A = diag([1 1 1])
>>
>> So table is sth like this
>> i  j  val
>> 0 0 1
>> 1 1 1
>> 2 2 1
>>
>> see this
>>
>> Mat A; // of course, A should be created first and assembled after the set
>> operation
>> int m =3;
>> int n = 3;
>> PetscInt indxm [] ={0, 1, 2};
>> PetscInt indxn [] = {0, 1, 2};
>> PetscScalar vals[] = {1.,1.,1.}
>> MatSetValues(A, m, idxm, n, idxn, vals, INSERT_VALUES);
>>
>
>
> you example doen't work
> the result matrix is:
>
> row 0: (0, 1.1)  (1, 1.2)  (2, 1.3)
> row 1: (0, 2.07356e-317)  (1, 2.122e-314)  (2, 9.88131e-324)
> row 2: (0, 2.122e-314)  (1, 9.88131e-324)  (2, 1.40122e-316)
>
> tom
>
> here is the code for this example
>
>  Mat xx;
>  PetscInitialize(&argc,&argv,(char *)0,help);
>
>  MatCreateSeqAIJ(PETSC_COMM_SELF,3,3,1,PETSC_NULL,&xx);
>
>  int m =3;
>  int n = 3;
>
    ^^^^^^^^^^

This is wrong. You are claiming to provide a m x n array of values, but you
only give 3, not 9 values.
If you want a diagonal matrix, either change to MatSetDiagonal(), or use

 m = 1;
 n = 1;

 MatSetValues(xx,m,indxm,n,indxn,vals,INSERT_VALUES);
 MatSetValues(xx,m,&indxm[1],n,&indxn[1],&vals[1],INSERT_VALUES);
 MatSetValues(xx,m,&indxm[2],n,&indxn[2],&vals[2],INSERT_VALUES);

You set rows of values, or logically dense matrices of values (like element
matrices) at a time.

    Matt


>  PetscInt indxm [] ={0, 1, 2};
>  PetscInt indxn [] = {0, 1, 2};
>  PetscScalar vals[] = {1.1,1.2,1.3};
>
>  MatSetValues(xx,m,indxm,n,indxn,vals,INSERT_VALUES);
>
>  MatAssemblyBegin(xx,MAT_FINAL_ASSEMBLY);
>  MatAssemblyEnd(xx,MAT_FINAL_ASSEMBLY);
>
>  MatView(xx,PETSC_VIEWER_STDOUT_WORLD);
>  PetscFinalize();
>
> return EXIT_SUCCESS;
>



-- 
What most experimenters take for granted before they begin their experiments
is infinitely more interesting than any results to which their experiments
lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20110308/4ea83881/attachment.htm>


More information about the petsc-users mailing list