Actual source code: ex58.c

petsc-3.7.3 2016-08-01
Report Typos and Errors
  2: static char help[] = "Tests MatTranspose() and MatEqual() for MPIAIJ matrices.\n\n";

  4: #include <petscmat.h>


  9: int main(int argc,char **argv)
 10: {
 11:   Mat            A,B;
 12:   PetscInt       m = 7,n,i,rstart,rend,cols[3];
 14:   PetscScalar    v[3];
 15:   PetscBool      equal;
 16:   const char     *eq[2];

 18:   PetscInitialize(&argc,&argv,(char*)0,help);
 19:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 20:   n    = m;

 22:   /* ------- Assemble matrix, --------- */

 24:   MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,m,n,0,0,0,0,&A);
 25:   MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_FALSE);
 26:   MatGetOwnershipRange(A,&rstart,&rend);
 27:   if (!rstart) {
 28:     cols[0] = 0;
 29:     cols[1] = 1;
 30:     v[0]    = 2.0; v[1] = -1.0;
 31:     MatSetValues(A,1,&rstart,2,cols,v,INSERT_VALUES);
 32:     rstart++;
 33:   }
 34:   if (rend == m) {
 35:     rend--;
 36:     cols[0] = rend-1;
 37:     cols[1] = rend;
 38:     v[0]    = -1.0; v[1] = 2.0;
 39:     MatSetValues(A,1,&rend,2,cols,v,INSERT_VALUES);
 40:   }
 41:   v[0] = -1.0; v[1] = 2.0; v[2] = -1.0;
 42:   for (i=rstart; i<rend; i++) {
 43:     cols[0] = i-1;
 44:     cols[1] = i;
 45:     cols[2] = i+1;
 46:     MatSetValues(A,1,&i,3,cols,v,INSERT_VALUES);
 47:   }
 48:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 49:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);

 51:   MatTranspose(A,MAT_INITIAL_MATRIX,&B);

 53:   MatEqual(A,B,&equal);

 55:   eq[0] = "not equal";
 56:   eq[1] = "equal";
 57:   PetscPrintf(PETSC_COMM_WORLD,"Matrices are %s\n",eq[equal]);

 59:   /* Free data structures */
 60:   MatDestroy(&A);
 61:   MatDestroy(&B);


 64:   PetscFinalize();
 65:   return 0;
 66: }