Actual source code: ex58.c

petsc-3.14.6 2021-03-30
Report Typos and Errors

  2: static char help[] = "Tests MatTranspose() and MatEqual() for MPIAIJ matrices.\n\n";

  4: #include <petscmat.h>

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

 15:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 16:   PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);
 17:   n    = m;

 19:   /* ------- Assemble matrix, --------- */

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

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

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

 52:   eq[0] = "not equal";
 53:   eq[1] = "equal";
 54:   PetscPrintf(PETSC_COMM_WORLD,"Matrices are %s\n",eq[equal]);

 56:   MatTranspose(A,MAT_REUSE_MATRIX,&B);
 57:   MatEqual(A,B,&equal);
 58:   if (!equal) { PetscPrintf(PETSC_COMM_WORLD,"MatTranspose with MAT_REUSE_MATRIX failed"); }

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


 65:   PetscFinalize();
 66:   return ierr;
 67: }

 69: /*TEST

 71:     test:

 73:     test:
 74:       suffix: 2
 75:       nsize: 2
 76:       output_file: output/ex58_1.out

 78: TEST*/