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