Actual source code: ex20.c
2: static char help[] = "Tests converting a matrix to another format with MatConvert().\n\n";
4: #include <petscmat.h>
6: int main(int argc,char **args)
7: {
8: Mat C,A;
9: PetscInt i,j,m = 5,n = 4,Ii,J;
10: PetscMPIInt rank,size;
11: PetscScalar v;
12: char mtype[256];
14: PetscInitialize(&argc,&args,(char*)0,help);
15: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
17: /* This example does not work correctly for np > 2 */
18: MPI_Comm_size(PETSC_COMM_WORLD,&size);
21: /* Create the matrix for the five point stencil, YET AGAIN */
22: MatCreate(PETSC_COMM_WORLD,&C);
23: MatSetSizes(C,PETSC_DECIDE,PETSC_DECIDE,m*n,m*n);
24: MatSetFromOptions(C);
25: MatSetUp(C);
26: for (i=0; i<m; i++) {
27: for (j=2*rank; j<2*rank+2; j++) {
28: v = -1.0; Ii = j + n*i;
29: if (i>0) {J = Ii - n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
30: if (i<m-1) {J = Ii + n; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
31: if (j>0) {J = Ii - 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
32: if (j<n-1) {J = Ii + 1; MatSetValues(C,1,&Ii,1,&J,&v,INSERT_VALUES);}
33: v = 4.0; MatSetValues(C,1,&Ii,1,&Ii,&v,INSERT_VALUES);
34: }
35: }
37: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
38: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
39: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
40: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
41: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
42: MatView(C,PETSC_VIEWER_STDOUT_WORLD);
44: PetscStrcpy(mtype,MATSAME);
45: PetscOptionsGetString(NULL,NULL,"-conv_mat_type",mtype,sizeof(mtype),NULL);
46: MatConvert(C,mtype,MAT_INITIAL_MATRIX,&A);
47: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_INFO);
48: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
49: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
50: PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_IMPL);
51: MatView(A,PETSC_VIEWER_STDOUT_WORLD);
52: PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);
54: /* Free data structures */
55: MatDestroy(&A);
56: MatDestroy(&C);
58: PetscFinalize();
59: return 0;
60: }
62: /*TEST
64: test:
65: args: -conv_mat_type seqaij
67: TEST*/