/* Stokes output: --------------------------------- Operator summary: K G f, h u p --------------------------------- Solution summary: max_u min_u average_u |r_1| |r_2| --------------------------------- Solver summary: name --------------------------------- Petsc build summary: */ PetscErrorCode BSSCR_stokes_output( PetscViewer v, Mat stokes_A, Vec stokes_b, Vec stokes_x, KSP ksp, PetscInt monitor_index ) { Mat K,G,D,C; Vec f,h, u,p; K = G = D = C = PETSC_NULL; f = h = PETSC_NULL; u = p = PETSC_NULL; MatNestGetSubMat( stokes_A, 0,0, &K ); MatNestGetSubMat( stokes_A, 0,1, &G ); MatNestGetSubMat( stokes_A, 1,0, &D ); MatNestGetSubMat( stokes_A, 1,1, &C ); VecNestGetSubVec( stokes_x, 0, &u ); VecNestGetSubVec( stokes_x, 1, &p ); VecNestGetSubVec( stokes_b, 0, &f ); VecNestGetSubVec( stokes_b, 1, &h ); PetscViewerASCIIPrintf( v, "Stokes Output:\n"); PetscViewerASCIIPushTab( v ); /*--------------------------------------------------------------------------------------------*/ PetscViewerASCIIPrintf( v, "--------------------------------------------------\n"); PetscViewerASCIIPrintf( v, "Operator summary:\n"); PetscViewerASCIIPushTab( v ); if (K) { BSSCR_MatInfoLog(v,K, "stokes_A11"); PetscViewerASCIIPrintf( v, "\n"); } if (G) { BSSCR_MatInfoLog(v,G, "stokes_A12"); PetscViewerASCIIPrintf( v, "\n"); } if (D) { BSSCR_MatInfoLog(v,D, "stokes_A21"); PetscViewerASCIIPrintf( v, "\n"); } if (C) { BSSCR_MatInfoLog(v,C, "stokes_A22"); PetscViewerASCIIPrintf( v, "\n"); } if (f) { BSSCR_VecInfoLog(v,f,"stokes_b1"); PetscViewerASCIIPrintf( v, "\n"); } if (h) { BSSCR_VecInfoLog(v,h,"stokes_b2"); PetscViewerASCIIPrintf( v, "\n"); } PetscViewerASCIIPopTab( v ); /*--------------------------------------------------------------------------------------------*/ PetscViewerASCIIPrintf( v, "--------------------------------------------------\n"); PetscViewerASCIIPrintf( v, "Solution summary:\n"); PetscViewerASCIIPushTab( v ); if (u) { BSSCR_VecInfoLog(v,u,"x1"); PetscViewerASCIIPrintf( v, "\n"); } if (p) { BSSCR_VecInfoLog(v,p,"x2"); PetscViewerASCIIPrintf( v, "\n"); } { PetscScalar s,sum; PetscReal r,max,min; PetscInt N, loc; double r1,r2; Vec K_d; PetscInt loc_max, loc_min; VecGetSize( u, &N ); VecMax( u, &loc, &r ); PetscViewerASCIIPrintf( v, "u_max: %1.12e [%d] \n", r, loc ); VecMin( u, &loc, &r ); PetscViewerASCIIPrintf( v, "u_min: %1.12e [%d] \n", r, loc ); VecDot( u,u, &s ); PetscViewerASCIIPrintf( v, "u_rms: %1.12e \n", sqrt( PetscRealPart(s) )/N ); VecDuplicate( u, &K_d ); MatGetDiagonal( K, K_d ); VecMax( K_d, &loc_max, &max ); VecMin( K_d, &loc_min, &min ); PetscViewerASCIIPrintf( v,"Algebraic contrast: max(K_d)=%.3e [%d] , min(K_d)=%.3e [%d] , max(K_d)/min(K_d) = %.8e \n", max,loc_max, min,loc_min, max/min ); MatGetRowMax( K, K_d, PETSC_NULL ); VecMax( K_d, &loc_max, &max ); MatGetRowMin( K, K_d, PETSC_NULL ); VecAbs( K_d ); VecMin( K_d, &loc_min, &min ); PetscViewerASCIIPrintf( v,"Algebraic contrast: max(K)=%.3e [%d] , |min(K)|=%.3e [%d] , max(K)/|min(K)| = %.8e \n", max,loc_max, min,loc_min, max/min ); Stg_VecDestroy(&K_d ); PetscViewerASCIIPrintf( v, "\n"); VecGetSize( p, &N ); VecMax( p, &loc, &r ); PetscViewerASCIIPrintf( v, "p_max: %1.12e [%d] \n", r, loc ); VecMin( p, &loc, &r ); PetscViewerASCIIPrintf( v, "p_min: %1.12e [%d] \n", r, loc ); VecDot( p,p, &s ); PetscViewerASCIIPrintf( v, "p_rms: %1.12e \n", sqrt( PetscRealPart(s) )/N ); VecSum( p, &sum ); PetscViewerASCIIPrintf( v, "sum(p): %1.12e \n", sum ); PetscViewerASCIIPrintf( v, "\n"); r1 = BSSCR_StokesMomentumResidual( K,G,f, u,p ); PetscViewerASCIIPrintf( v, "|r1| = %1.12e <momentum> \n", r1 ); r2 = BSSCR_StokesContinuityResidual( G,C,h, u,p ); PetscViewerASCIIPrintf( v, "|r2| = %1.12e <continuity> \n", r2 ); PetscViewerASCIIPrintf( v, "\n"); } PetscViewerASCIIPopTab( v ); /*--------------------------------------------------------------------------------------------*/ if (ksp) { PetscViewerASCIIPrintf( v, "--------------------------------------------------\n"); PetscViewerASCIIPrintf( v, "Solver summary:\n"); PetscViewerASCIIPushTab( v ); BSSCR_KSPLogSolve( v, monitor_index, ksp ); BSSCR_BSSCR_KSPLogSolveSummary( v, monitor_index, ksp ); PetscViewerASCIIPrintf( v, "\n"); PetscViewerASCIIPopTab( v ); } /*--------------------------------------------------------------------------------------------*/ PetscViewerASCIIPrintf( v, "--------------------------------------------------\n"); PetscViewerASCIIPrintf( v, "Petsc build summary:\n"); PetscViewerASCIIPushTab( v ); BSSCR_GeneratePetscHeader_for_viewer( v ); PetscViewerASCIIPrintf( v, "\n"); PetscViewerASCIIPopTab( v ); /*--------------------------------------------------------------------------------------------*/ PetscViewerASCIIPopTab(v); PetscFunctionReturn(0); }
PetscErrorCode BSSCR_MatStokesMVBlockReportOperatorScales( Mat A, PetscTruth sym ) { Vec rA, rG; PetscInt loc,M,N; PetscReal min, max; Mat K,G,D,C; PetscTruth is_block; /* check A is 2x2 block matrix */ Stg_PetscObjectTypeCompare( (PetscObject)A, "block", &is_block ); if (is_block==PETSC_FALSE) { Stg_SETERRQ( PETSC_ERR_SUP, "Only valid for MatType = block" ); } MatGetSize( A, &M, &N ); if ( (M!=2) || (N!=2) ) { Stg_SETERRQ2( PETSC_ERR_SUP, "Only valid for 2x2 block. Yours has dimension %Dx%D", M,N ); } MatNestGetSubMat( A, 0,0, &K ); MatNestGetSubMat( A, 0,1, &G ); MatNestGetSubMat( A, 1,0, &D ); MatNestGetSubMat( A, 1,1, &C ); MatGetVecs( K, PETSC_NULL, &rA ); VecDuplicate( rA, &rG ); /* Report the row max and mins */ if (K!=PETSC_NULL) { MatGetRowMax( K, rA, PETSC_NULL ); VecMax( rA, &loc, &max ); PetscPrintf( PETSC_COMM_WORLD, "Sup_max(K) = %g \n", max ); MatGetRowMinAbs( K, rA, PETSC_NULL ); VecMin( rA, &loc, &min ); PetscPrintf( PETSC_COMM_WORLD, "Sup_min(K) = %g \n\n", min ); } if( G != PETSC_NULL ) { MatGetRowMax( G, rG, PETSC_NULL ); VecMax( rG, &loc, &max ); PetscPrintf( PETSC_COMM_WORLD, "Sup_max(G) = %g \n", max ); MatGetRowMinAbs( G, rG, PETSC_NULL ); VecMin( rG, &loc, &min ); PetscPrintf( PETSC_COMM_WORLD, "Sup_min(G) = %g \n", min ); } if( D != PETSC_NULL && !sym ) { Vec rD; MatGetVecs( D, PETSC_NULL, &rD ); MatGetRowMax( D, rD, PETSC_NULL ); VecMax( rD, &loc, &max ); PetscPrintf( PETSC_COMM_WORLD, "Sup_max(D) = %g \n", max ); MatGetRowMinAbs( D, rD, PETSC_NULL ); VecMin( rD, &loc, &min ); PetscPrintf( PETSC_COMM_WORLD, "Sup_min(D) = %g \n", min ); Stg_VecDestroy(&rD ); } if( C != PETSC_NULL ) { Vec cG; MatGetVecs( G, &cG, PETSC_NULL ); MatGetRowMax( C, cG, PETSC_NULL ); VecMax( cG, &loc, &max ); PetscPrintf( PETSC_COMM_WORLD, "Sup_max(C) = %g \n", max ); MatGetRowMin( C, cG, PETSC_NULL ); VecMin( cG, &loc, &min ); PetscPrintf( PETSC_COMM_WORLD, "Sup_min(C) = %g \n\n", min ); Stg_VecDestroy(&cG); } Stg_VecDestroy(&rA ); Stg_VecDestroy(&rG ); PetscFunctionReturn(0); }
PETSC_EXTERN void PETSC_STDCALL matgetrowmin_(Mat *mat,Vec *v,PetscInt idx[], int *ierr ) { CHKFORTRANNULLINTEGER(idx); *ierr = MatGetRowMin(*mat,*v,idx); }
int main(int argc,char **args) { Mat A; Vec min,max,maxabs; PetscInt m,n; PetscInt imin[M],imax[M],imaxabs[M],indices[N],row; PetscScalar values[N]; PetscErrorCode ierr; MatType type; PetscMPIInt size; PetscBool doTest=PETSC_TRUE; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatSetUp(A);CHKERRQ(ierr); row = 0; indices[0] = 0; indices[1] = 1; indices[2] = 2; indices[3] = 3; indices[4] = 4; indices[5] = 5; values[0] = -1.0; values[1] = 0.0; values[2] = 1.0; values[3] = 3.0; values[4] = 4.0; values[5] = -5.0; ierr = MatSetValues(A,1,&row,6,indices,values,INSERT_VALUES);CHKERRQ(ierr); row = 1; ierr = MatSetValues(A,1,&row,3,indices,values,INSERT_VALUES);CHKERRQ(ierr); row = 4; ierr = MatSetValues(A,1,&row,1,indices+4,values+4,INSERT_VALUES);CHKERRQ(ierr); row = 4; ierr = MatSetValues(A,1,&row,2,indices+4,values+4,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = MatGetLocalSize(A, &m,&n);CHKERRQ(ierr); ierr = VecCreate(PETSC_COMM_WORLD,&min);CHKERRQ(ierr); ierr = VecSetSizes(min,m,PETSC_DECIDE);CHKERRQ(ierr); ierr = VecSetFromOptions(min);CHKERRQ(ierr); ierr = VecDuplicate(min,&max);CHKERRQ(ierr); ierr = VecDuplicate(min,&maxabs);CHKERRQ(ierr); /* Test MatGetRowMin, MatGetRowMax and MatGetRowMaxAbs */ if (size == 1) { ierr = MatGetRowMin(A,min,imin);CHKERRQ(ierr); ierr = MatGetRowMax(A,max,imax);CHKERRQ(ierr); ierr = MatGetRowMaxAbs(A,maxabs,imaxabs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");CHKERRQ(ierr); ierr = VecView(min,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");CHKERRQ(ierr); ierr = VecView(max,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");CHKERRQ(ierr); ierr = VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } else { ierr = MatGetType(A,&type);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"\nMatrix type: %s\n",type);CHKERRQ(ierr); /* AIJ */ ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIAIJ,&doTest);CHKERRQ(ierr); if (doTest) { ierr = MatGetRowMaxAbs(A,maxabs,NULL);CHKERRQ(ierr); ierr = MatGetRowMaxAbs(A,maxabs,imaxabs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");CHKERRQ(ierr); ierr = VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } /* BAIJ */ ierr = PetscObjectTypeCompare((PetscObject)A,MATMPIBAIJ,&doTest);CHKERRQ(ierr); if (doTest) { ierr = MatGetRowMaxAbs(A,maxabs,NULL);CHKERRQ(ierr); ierr = MatGetRowMaxAbs(A,maxabs,imaxabs);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values:\n");CHKERRQ(ierr); ierr = VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } } if (size == 1) { ierr = MatConvert(A,MATDENSE,MAT_INPLACE_MATRIX,&A);CHKERRQ(ierr); ierr = MatGetRowMin(A,min,imin);CHKERRQ(ierr); ierr = MatGetRowMax(A,max,imax);CHKERRQ(ierr); ierr = MatGetRowMaxAbs(A,maxabs,imaxabs);CHKERRQ(ierr); ierr = MatView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Minimums\n");CHKERRQ(ierr); ierr = VecView(min,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imin,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximums\n");CHKERRQ(ierr); ierr = VecView(max,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imax,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Row Maximum Absolute Values\n");CHKERRQ(ierr); ierr = VecView(maxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscIntView(5,imaxabs,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = VecDestroy(&min);CHKERRQ(ierr); ierr = VecDestroy(&max);CHKERRQ(ierr); ierr = VecDestroy(&maxabs);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }