Actual source code: ex114.c

petsc-3.13.6 2020-09-29
Report Typos and Errors

  2: static char help[] = "Tests MatGetRowMax(), MatGetRowMin(), MatGetRowMaxAbs()\n";

  4:  #include <petscmat.h>

  6: #define M 5
  7: #define N 6

  9: int main(int argc,char **args)
 10: {
 11:   Mat            A;
 12:   Vec            min,max,maxabs;
 13:   PetscInt       m,n;
 14:   PetscInt       imin[M],imax[M],imaxabs[M],indices[N],row;
 15:   PetscScalar    values[N];
 17:   MatType        type;
 18:   PetscMPIInt    size;
 19:   PetscBool      doTest=PETSC_TRUE;

 21:   PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr;
 22:   MPI_Comm_size(PETSC_COMM_WORLD,&size);

 24:   MatCreate(PETSC_COMM_WORLD,&A);
 25:   MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,M,N);
 26:   MatSetFromOptions(A);
 27:   MatSetUp(A);
 28:   row  = 0;

 30:   indices[0] = 0;   indices[1] = 1;  indices[2] = 2;  indices[3] = 3;  indices[4] = 4;  indices[5] = 5;
 31:   values[0]  = -1.0; values[1] = 0.0; values[2] = 1.0; values[3] = 3.0; values[4] = 4.0; values[5] = -5.0;

 33:   MatSetValues(A,1,&row,6,indices,values,INSERT_VALUES);
 34:   row  = 1;
 35:   MatSetValues(A,1,&row,3,indices,values,INSERT_VALUES);
 36:   row  = 4;
 37:   MatSetValues(A,1,&row,1,indices+4,values+4,INSERT_VALUES);
 38:   row  = 4;
 39:   MatSetValues(A,1,&row,2,indices+4,values+4,INSERT_VALUES);
 40:   MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
 41:   MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
 42:   MatView(A,PETSC_VIEWER_STDOUT_WORLD);

 44:   MatGetLocalSize(A, &m,&n);
 45:   VecCreate(PETSC_COMM_WORLD,&min);
 46:   VecSetSizes(min,m,PETSC_DECIDE);
 47:   VecSetFromOptions(min);
 48:   VecDuplicate(min,&max);
 49:   VecDuplicate(min,&maxabs);

 51:   /* Test MatGetRowMin, MatGetRowMax and MatGetRowMaxAbs */
 52:   if (size == 1) {
 53:     MatGetRowMin(A,min,imin);
 54:     MatGetRowMax(A,max,imax);
 55:     MatGetRowMaxAbs(A,maxabs,imaxabs);

 57:     PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");
 58:     VecView(min,PETSC_VIEWER_STDOUT_WORLD);
 59:     PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);
 60:     PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");
 61:     VecView(max,PETSC_VIEWER_STDOUT_WORLD);
 62:     PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);
 63:     PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");
 64:     VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
 65:     PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);

 67:   } else {
 68:     MatGetType(A,&type);
 69:     PetscPrintf(PETSC_COMM_WORLD,"\nMatrix type: %s\n",type);
 70:     /* AIJ */
 71:     PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&doTest);
 72:     if (doTest) {
 73:       MatGetRowMaxAbs(A,maxabs,NULL);
 74:       MatGetRowMaxAbs(A,maxabs,imaxabs);
 75:       PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");
 76:       VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
 77:     }
 78:     /* BAIJ */
 79:     PetscObjectTypeCompare((PetscObject)A,MATMPIBAIJ,&doTest);
 80:     if (doTest) {
 81:       MatGetRowMaxAbs(A,maxabs,NULL);
 82:       MatGetRowMaxAbs(A,maxabs,imaxabs);
 83:       PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");
 84:       VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
 85:     }
 86:   }

 88:   if (size == 1) {
 89:     MatConvert(A,MATDENSE,MAT_INPLACE_MATRIX,&A);

 91:     MatGetRowMin(A,min,imin);
 92:     MatGetRowMax(A,max,imax);
 93:     MatGetRowMaxAbs(A,maxabs,imaxabs);

 95:     MatView(A,PETSC_VIEWER_STDOUT_WORLD);
 96:     PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");
 97:     VecView(min,PETSC_VIEWER_STDOUT_WORLD);
 98:     PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);
 99:     PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");
100:     VecView(max,PETSC_VIEWER_STDOUT_WORLD);
101:     PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);
102:     PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");
103:     VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);
104:     PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);
105:   }

107:   VecDestroy(&min);
108:   VecDestroy(&max);
109:   VecDestroy(&maxabs);
110:   MatDestroy(&A);
111:   PetscFinalize();
112:   return ierr;
113: }



117: /*TEST

119:    test:
120:       output_file: output/ex114.out

122:    test:
123:       suffix: 2
124:       nsize: 2

126:    test:
127:       suffix: 3
128:       nsize: 2
129:       args: -mat_type baij

131: TEST*/