PetscErrorCode DMView_DA_VTK(DM da, PetscViewer viewer) { PetscInt dim, dof, M = 0, N = 0, P = 0; PetscErrorCode ierr; PetscFunctionBegin; ierr = DMDAGetInfo(da, &dim, &M, &N, &P, NULL, NULL, NULL, &dof, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr); if (!da->coordinates) SETERRQ(PetscObjectComm((PetscObject)da),PETSC_ERR_SUP, "VTK output requires DMDA coordinates."); /* Write Header */ ierr = PetscViewerASCIIPrintf(viewer,"# vtk DataFile Version 2.0\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"Structured Mesh Example\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"ASCII\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"DATASET STRUCTURED_GRID\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"DIMENSIONS %d %d %d\n", M, N, P);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"POINTS %d double\n", M*N*P);CHKERRQ(ierr); if (da->coordinates) { DM dac; Vec natural; ierr = DMGetCoordinateDM(da, &dac);CHKERRQ(ierr); ierr = DMDACreateNaturalVector(dac, &natural);CHKERRQ(ierr); ierr = PetscObjectSetOptionsPrefix((PetscObject) natural, "coor_");CHKERRQ(ierr); ierr = DMDAGlobalToNaturalBegin(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); ierr = DMDAGlobalToNaturalEnd(dac, da->coordinates, INSERT_VALUES, natural);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_VTK_COORDS);CHKERRQ(ierr); ierr = VecView(natural, viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = VecDestroy(&natural);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C KSPMonitorRange - Prints the percentage of residual elements that are more then 10 percent of the maximum value. Collective on KSP Input Parameters: + ksp - iterative context . it - iteration number . rnorm - 2-norm (preconditioned) residual value (may be estimated). - dummy - an ASCII viewer Options Database Key: . -ksp_monitor_range - Activates KSPMonitorRange() Level: intermediate .keywords: KSP, default, monitor, residual .seealso: KSPMonitorSet(), KSPMonitorDefault(), KSPMonitorLGResidualNormCreate() @*/ PetscErrorCode KSPMonitorRange(KSP ksp,PetscInt it,PetscReal rnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; PetscReal perc,rel; PetscViewer viewer = dummy->viewer; /* should be in a MonitorRangeContext */ static PetscReal prev; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); if (!it) prev = rnorm; if (it == 0 && ((PetscObject)ksp)->prefix) { ierr = PetscViewerASCIIPrintf(viewer," Residual norms for %s solve.\n",((PetscObject)ksp)->prefix); CHKERRQ(ierr); } ierr = KSPMonitorRange_Private(ksp,it,&perc); CHKERRQ(ierr); rel = (prev - rnorm)/prev; prev = rnorm; ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP preconditioned resid norm %14.12e Percent values above 20 percent of maximum %5.2f relative decrease %5.2e ratio %5.2e \n",it,(double)rnorm,(double)(100.0*perc),(double)rel,(double)(rel/perc)); CHKERRQ(ierr); ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C KSPMonitorSingularValue - Prints the two norm of the true residual and estimation of the extreme singular values of the preconditioned problem at each iteration. Logically Collective on KSP Input Parameters: + ksp - the iterative context . n - the iteration - rnorm - the two norm of the residual Options Database Key: . -ksp_monitor_singular_value - Activates KSPMonitorSingularValue() Notes: The CG solver uses the Lanczos technique for eigenvalue computation, while GMRES uses the Arnoldi technique; other iterative methods do not currently compute singular values. Level: intermediate .keywords: KSP, CG, default, monitor, extreme, singular values, Lanczos, Arnoldi .seealso: KSPComputeExtremeSingularValues() @*/ PetscErrorCode KSPMonitorSingularValue(KSP ksp,PetscInt n,PetscReal rnorm,PetscViewerAndFormat *dummy) { PetscReal emin,emax,c; PetscErrorCode ierr; PetscViewer viewer = dummy->viewer; PetscFunctionBegin; PetscValidHeaderSpecific(ksp,KSP_CLASSID,1); PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); if (!ksp->calc_sings) { ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm %14.12e \n",n,(double)rnorm); CHKERRQ(ierr); } else { ierr = KSPComputeExtremeSingularValues(ksp,&emax,&emin); CHKERRQ(ierr); c = emax/emin; ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm %14.12e %% max %14.12e min %14.12e max/min %14.12e\n",n,(double)rnorm,(double)emax,(double)emin,(double)c); CHKERRQ(ierr); } ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode PCView_Factor(PC pc,PetscViewer viewer) { PC_Factor *factor = (PC_Factor*)pc->data; PetscErrorCode ierr; PetscBool isstring,iascii; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { if (factor->factortype == MAT_FACTOR_ILU || factor->factortype == MAT_FACTOR_ICC) { if (factor->info.dt > 0) { ierr = PetscViewerASCIIPrintf(viewer," drop tolerance %G\n",factor->info.dt);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," max nonzeros per row %D\n",factor->info.dtcount);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," column permutation tolerance %G\n",(PetscInt)factor->info.dtcol);CHKERRQ(ierr); } else if (factor->info.levels == 1) { ierr = PetscViewerASCIIPrintf(viewer," %D level of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer," %D levels of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); } } ierr = PetscViewerASCIIPrintf(viewer," tolerance for zero pivot %G\n",factor->info.zeropivot);CHKERRQ(ierr); if (factor->info.shifttype==(PetscReal)MAT_SHIFT_POSITIVE_DEFINITE) { ierr = PetscViewerASCIIPrintf(viewer," using Manteuffel shift\n");CHKERRQ(ierr); } if (factor->info.shifttype==(PetscReal)MAT_SHIFT_NONZERO) { ierr = PetscViewerASCIIPrintf(viewer," using diagonal shift to prevent zero pivot\n");CHKERRQ(ierr); } if (factor->info.shifttype==(PetscReal)MAT_SHIFT_INBLOCKS) { ierr = PetscViewerASCIIPrintf(viewer," using diagonal shift on blocks to prevent zero pivot\n");CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer," matrix ordering: %s\n",factor->ordering);CHKERRQ(ierr); if (factor->fact) { MatInfo info; ierr = MatGetInfo(factor->fact,MAT_LOCAL,&info);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," factor fill ratio given %G, needed %G\n",info.fill_ratio_given,info.fill_ratio_needed);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Factored matrix follows:\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = MatView(factor->fact,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } } else if (isstring) { MatFactorType t; ierr = MatGetFactorType(factor->fact,&t);CHKERRQ(ierr); if (t == MAT_FACTOR_ILU || t == MAT_FACTOR_ICC) { ierr = PetscViewerStringSPrintf(viewer," lvls=%D,order=%s",(PetscInt)factor->info.levels,factor->ordering);CHKERRQ(ierr);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
PetscErrorCode Assemble(MPI_Comm comm,PetscInt bs,const MatType mtype) { const PetscInt rc[] = {0,1,2,3}; const PetscScalar vals[] = {1, 2, 3, 4, 5, 6, 7, 8, 9,10,11,12,13,14,15,16, 17,18,19,20,21,22,23,24, 25,26,27,28,29,30,31,32, 33,34,35,36,37,38,39,40, 41,42,43,44,45,46,47,48, 49,50,51,52,53,54,55,56, 57,58,49,60,61,62,63,64}; Mat A; PetscViewer viewer; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatCreate(comm,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,4*bs,4*bs);CHKERRQ(ierr); ierr = MatSetType(A,mtype);CHKERRQ(ierr); ierr = MatMPIBAIJSetPreallocation(A,bs,2,NULL,2,NULL);CHKERRQ(ierr); ierr = MatMPISBAIJSetPreallocation(A,bs,2,NULL,2,NULL);CHKERRQ(ierr); ierr = MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);CHKERRQ(ierr); /* All processes contribute a global matrix */ ierr = MatSetValuesBlocked(A,4,rc,4,rc,vals,ADD_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = PetscPrintf(comm,"Matrix %s(%D)\n",mtype,bs);CHKERRQ(ierr); ierr = PetscViewerASCIIGetStdout(comm,&viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); ierr = MatView(A,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = MatView(A,viewer);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatView_LMVM(Mat B, PetscViewer pv) { Mat_LMVM *lmvm = (Mat_LMVM*)B->data; PetscErrorCode ierr; PetscBool isascii; MatType type; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)pv,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); if (isascii) { ierr = MatGetType(B, &type);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(pv,"Max. storage: %D\n",lmvm->m);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(pv,"Used storage: %D\n",lmvm->k+1);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(pv,"Number of updates: %D\n",lmvm->nupdates);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(pv,"Number of rejects: %D\n",lmvm->nrejects);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(pv,"Number of resets: %D\n",lmvm->nresets);CHKERRQ(ierr); if (lmvm->J0) { ierr = PetscViewerASCIIPrintf(pv,"J0 Matrix:\n");CHKERRQ(ierr); ierr = PetscViewerPushFormat(pv, PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = MatView(lmvm->J0, pv);CHKERRQ(ierr); ierr = PetscViewerPopFormat(pv);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
/*@ MatColoringApply - Apply the coloring to the matrix, producing index sets corresponding to a number of independent sets in the induced graph. Collective on MatColoring Input Parameters: . mc - the MatColoring context Output Parameter: . coloring - the ISColoring instance containing the coloring Level: beginner .keywords: Coloring, Apply .seealso: MatColoring, MatColoringCreate() @*/ PetscErrorCode MatColoringApply(MatColoring mc,ISColoring *coloring) { PetscErrorCode ierr; PetscBool flg; PetscViewerFormat format; PetscViewer viewer; PetscInt nc,ncolors; PetscFunctionBegin; PetscValidHeaderSpecific(mc,MAT_COLORING_CLASSID,1); ierr = PetscLogEventBegin(Mat_Coloring_Apply,mc,0,0,0);CHKERRQ(ierr); ierr = (*mc->ops->apply)(mc,coloring);CHKERRQ(ierr); ierr = PetscLogEventEnd(Mat_Coloring_Apply,mc,0,0,0);CHKERRQ(ierr); /* valid */ if (mc->valid) { ierr = MatColoringTestValid(mc,*coloring);CHKERRQ(ierr); } /* view */ ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)mc),((PetscObject)mc)->prefix,"-mat_coloring_view",&viewer,&format,&flg);CHKERRQ(ierr); if (flg && !PetscPreLoadingOn) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = MatColoringView(mc,viewer);CHKERRQ(ierr); ierr = MatGetSize(mc->mat,NULL,&nc);CHKERRQ(ierr); ierr = ISColoringGetIS(*coloring,&ncolors,NULL);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Number of colors %d\n",ncolors);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Number of total columns %d\n",nc);CHKERRQ(ierr); if (nc <= 1000) {ierr = ISColoringView(*coloring,viewer);CHKERRQ(ierr);} ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* Default (short) KSP Monitor, same as KSPMonitorDefault() except it prints fewer digits of the residual as the residual gets smaller. This is because the later digits are meaningless and are often different on different machines; by using this routine different machines will usually generate the same output. */ PetscErrorCode KSPMonitorDefaultShort(KSP ksp,PetscInt its,PetscReal fnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; PetscViewer viewer = dummy->viewer; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); if (its == 0 && ((PetscObject)ksp)->prefix) { ierr = PetscViewerASCIIPrintf(viewer," Residual norms for %s solve.\n",((PetscObject)ksp)->prefix); CHKERRQ(ierr); } if (fnorm > 1.e-9) { ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm %g \n",its,(double)fnorm); CHKERRQ(ierr); } else if (fnorm > 1.e-11) { ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm %5.3e \n",its,(double)fnorm); CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm < 1.e-11\n",its); CHKERRQ(ierr); } ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ MatSchurComplementComputeExplicitOperator - Compute the Schur complement matrix explicitly Collective on Mat Input Parameter: . M - the matrix obtained with MatCreateSchurComplement() Output Parameter: . S - the Schur complement matrix Note: This can be expensive, so it is mainly for testing Level: advanced .seealso: MatCreateSchurComplement(), MatSchurComplementUpdate() @*/ PetscErrorCode MatSchurComplementComputeExplicitOperator(Mat M, Mat *S) { Mat B, C, D; KSP ksp; PC pc; PetscBool isLU, isILU; PetscReal fill = 2.0; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatSchurComplementGetSubMatrices(M, NULL, NULL, &B, &C, &D);CHKERRQ(ierr); ierr = MatSchurComplementGetKSP(M, &ksp);CHKERRQ(ierr); ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject) pc, PCLU, &isLU);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject) pc, PCILU, &isILU);CHKERRQ(ierr); if (isLU || isILU) { Mat fact, Bd, AinvB, AinvBd; PetscReal eps = 1.0e-10; /* This can be sped up for banded LU */ ierr = KSPSetUp(ksp);CHKERRQ(ierr); ierr = PCFactorGetMatrix(pc, &fact);CHKERRQ(ierr); ierr = MatConvert(B, MATDENSE, MAT_INITIAL_MATRIX, &Bd);CHKERRQ(ierr); ierr = MatDuplicate(Bd, MAT_DO_NOT_COPY_VALUES, &AinvBd);CHKERRQ(ierr); ierr = MatMatSolve(fact, Bd, AinvBd);CHKERRQ(ierr); ierr = MatDestroy(&Bd);CHKERRQ(ierr); ierr = MatChop(AinvBd, eps);CHKERRQ(ierr); ierr = MatConvert(AinvBd, MATAIJ, MAT_INITIAL_MATRIX, &AinvB);CHKERRQ(ierr); ierr = MatDestroy(&AinvBd);CHKERRQ(ierr); ierr = MatMatMult(C, AinvB, MAT_INITIAL_MATRIX, fill, S);CHKERRQ(ierr); ierr = MatDestroy(&AinvB);CHKERRQ(ierr); } else { Mat Ainvd, Ainv; ierr = PCComputeExplicitOperator(pc, &Ainvd);CHKERRQ(ierr); ierr = MatConvert(Ainvd, MATAIJ, MAT_INITIAL_MATRIX, &Ainv);CHKERRQ(ierr); ierr = MatDestroy(&Ainvd);CHKERRQ(ierr); #if 0 /* Symmetric version */ ierr = MatPtAP(Ainv, B, MAT_INITIAL_MATRIX, fill, S);CHKERRQ(ierr); #else /* Nonsymmetric version */ ierr = MatMatMatMult(C, Ainv, B, MAT_INITIAL_MATRIX, fill, S);CHKERRQ(ierr); #endif ierr = MatDestroy(&Ainv);CHKERRQ(ierr); } ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD, PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = MatView(*S, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); if (D) { MatInfo info; ierr = MatGetInfo(D, MAT_GLOBAL_SUM, &info);CHKERRQ(ierr); if (info.nz_used) SETERRQ(PetscObjectComm((PetscObject) M), PETSC_ERR_SUP, "Not yet implemented"); } PetscFunctionReturn(0); }
/*@C BVView - Prints the BV data structure. Collective on BV Input Parameters: + bv - the BV context - viewer - optional visualization context Note: The available visualization contexts include + PETSC_VIEWER_STDOUT_SELF - standard output (default) - PETSC_VIEWER_STDOUT_WORLD - synchronized standard output where only the first processor opens the file. All other processors send their data to the first processor to print. The user can open an alternative visualization contexts with PetscViewerASCIIOpen() (output to a specified file). Level: beginner .seealso: PetscViewerASCIIOpen() @*/ PetscErrorCode BVView(BV bv,PetscViewer viewer) { PetscErrorCode ierr; PetscBool isascii; PetscViewerFormat format; const char *orthname[2] = {"classical","modified"}; const char *refname[3] = {"if needed","never","always"}; PetscFunctionBegin; PetscValidHeaderSpecific(bv,BV_CLASSID,1); PetscValidType(bv,1); if (!viewer) { ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)bv),&viewer);CHKERRQ(ierr); } PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);CHKERRQ(ierr); if (isascii) { ierr = PetscObjectPrintClassNamePrefixType((PetscObject)bv,viewer);CHKERRQ(ierr); ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { ierr = PetscViewerASCIIPrintf(viewer,"%D columns of global length %D\n",bv->m,bv->N);CHKERRQ(ierr); if (bv->nc>0) { ierr = PetscViewerASCIIPrintf(viewer,"number of constraints: %D\n",bv->nc);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer,"orthogonalization method: %s Gram-Schmidt\n",orthname[bv->orthog_type]);CHKERRQ(ierr); switch (bv->orthog_ref) { case BV_ORTHOG_REFINE_IFNEEDED: ierr = PetscViewerASCIIPrintf(viewer,"orthogonalization refinement: %s (eta: %g)\n",refname[bv->orthog_ref],(double)bv->orthog_eta);CHKERRQ(ierr); break; case BV_ORTHOG_REFINE_NEVER: case BV_ORTHOG_REFINE_ALWAYS: ierr = PetscViewerASCIIPrintf(viewer,"orthogonalization refinement: %s\n",refname[bv->orthog_ref]);CHKERRQ(ierr); break; } if (bv->matrix) { if (bv->indef) { ierr = PetscViewerASCIIPrintf(viewer,"indefinite inner product\n");CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer,"non-standard inner product\n");CHKERRQ(ierr); } ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = MatView(bv->matrix,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); } } else { if (bv->ops->view) { ierr = (*bv->ops->view)(bv,viewer);CHKERRQ(ierr); } else { ierr = BVView_Default(bv,viewer);CHKERRQ(ierr); } } ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } else { ierr = (*bv->ops->view)(bv,viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@ SVDSolve - Solves the singular value problem. Collective on SVD Input Parameter: . svd - singular value solver context obtained from SVDCreate() Options Database Keys: + -svd_view - print information about the solver used - -svd_view_mat binary - save the matrix to the default binary viewer Level: beginner .seealso: SVDCreate(), SVDSetUp(), SVDDestroy() @*/ PetscErrorCode SVDSolve(SVD svd) { PetscErrorCode ierr; PetscBool flg; PetscInt i,*workperm; PetscViewer viewer; PetscViewerFormat format; PetscFunctionBegin; PetscValidHeaderSpecific(svd,SVD_CLASSID,1); ierr = PetscLogEventBegin(SVD_Solve,svd,0,0,0);CHKERRQ(ierr); /* call setup */ ierr = SVDSetUp(svd);CHKERRQ(ierr); svd->its = 0; svd->nconv = 0; for (i=0;i<svd->ncv;i++) { svd->sigma[i] = 0.0; svd->errest[i] = 0.0; } ierr = SVDMonitor(svd,svd->its,svd->nconv,svd->sigma,svd->errest,svd->ncv);CHKERRQ(ierr); ierr = (*svd->ops->solve)(svd);CHKERRQ(ierr); /* sort singular triplets */ if (svd->which == SVD_SMALLEST) { for (i=0;i<svd->nconv;i++) svd->perm[i] = i; ierr = PetscSortRealWithPermutation(svd->nconv,svd->sigma,svd->perm);CHKERRQ(ierr); } else { ierr = PetscMalloc(sizeof(PetscInt)*svd->nconv,&workperm);CHKERRQ(ierr); for (i=0;i<svd->nconv;i++) workperm[i] = i; ierr = PetscSortRealWithPermutation(svd->nconv,svd->sigma,workperm);CHKERRQ(ierr); for (i=0;i<svd->nconv;i++) svd->perm[i] = workperm[svd->nconv-i-1]; ierr = PetscFree(workperm);CHKERRQ(ierr); } svd->lvecsavail = (svd->leftbasis)? PETSC_TRUE: PETSC_FALSE; ierr = PetscLogEventEnd(SVD_Solve,svd,0,0,0);CHKERRQ(ierr); /* various viewers */ ierr = MatViewFromOptions(svd->OP,((PetscObject)svd)->prefix,"-svd_view_mat");CHKERRQ(ierr); ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)svd),((PetscObject)svd)->prefix,"-svd_view",&viewer,&format,&flg);CHKERRQ(ierr); if (flg && !PetscPreLoadingOn) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = SVDView(svd,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } /* Remove the initial subspaces */ svd->nini = 0; svd->ninil = 0; PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscErrorCode ierr; DS ds; PetscScalar *A; PetscInt i,j,n=10,ld; PetscViewer viewer; PetscBool verbose; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscOptionsGetInt(NULL,"-n",&n,NULL);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Compute symmetric matrix exponential - dimension %D.\n",n);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,"-verbose",&verbose);CHKERRQ(ierr); /* Create DS object */ ierr = DSCreate(PETSC_COMM_WORLD,&ds);CHKERRQ(ierr); ierr = DSSetType(ds,DSHEP);CHKERRQ(ierr); ierr = DSSetFromOptions(ds);CHKERRQ(ierr); ld = n+2; /* test leading dimension larger than n */ ierr = DSAllocate(ds,ld);CHKERRQ(ierr); ierr = DSSetDimensions(ds,n,0,0,0);CHKERRQ(ierr); /* Set up viewer */ ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); ierr = DSView(ds,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); if (verbose) { ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); } /* Fill with a symmetric Toeplitz matrix */ ierr = DSGetArray(ds,DS_MAT_A,&A);CHKERRQ(ierr); for (i=0;i<n;i++) A[i+i*ld]=2.0; for (j=1;j<3;j++) { for (i=0;i<n-j;i++) { A[i+(i+j)*ld]=1.0; A[(i+j)+i*ld]=1.0; } } ierr = DSRestoreArray(ds,DS_MAT_A,&A);CHKERRQ(ierr); ierr = DSSetState(ds,DS_STATE_RAW);CHKERRQ(ierr); if (verbose) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Matrix A - - - - - - - -\n");CHKERRQ(ierr); ierr = DSView(ds,viewer);CHKERRQ(ierr); } /* Compute matrix exponential */ ierr = DSComputeFunction(ds,SLEPC_FUNCTION_EXP);CHKERRQ(ierr); if (verbose) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Computed f(A) - - - - - - -\n");CHKERRQ(ierr); ierr = DSViewMat(ds,viewer,DS_MAT_F);CHKERRQ(ierr); } ierr = DSDestroy(&ds);CHKERRQ(ierr); ierr = SlepcFinalize(); return 0; }
PetscErrorCode PCView_Factor(PC pc,PetscViewer viewer) { PC_Factor *factor = (PC_Factor*)pc->data; PetscErrorCode ierr; PetscBool isstring,iascii; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { if (factor->factortype == MAT_FACTOR_ILU || factor->factortype == MAT_FACTOR_ICC) { if (factor->info.dt > 0) { ierr = PetscViewerASCIIPrintf(viewer," drop tolerance %G\n",factor->info.dt);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," max nonzeros per row %D\n",factor->info.dtcount);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," column permutation tolerance %G\n",(PetscInt)factor->info.dtcol);CHKERRQ(ierr); } else if (factor->info.levels == 1) { ierr = PetscViewerASCIIPrintf(viewer," %D level of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer," %D levels of fill\n",(PetscInt)factor->info.levels);CHKERRQ(ierr); } } ierr = PetscViewerASCIIPrintf(viewer," tolerance for zero pivot %G\n",factor->info.zeropivot);CHKERRQ(ierr); if (MatFactorShiftTypesDetail[(int)factor->info.shifttype]) { /* Only print when using a nontrivial shift */ ierr = PetscViewerASCIIPrintf(viewer," using %s [%s]\n",MatFactorShiftTypesDetail[(int)factor->info.shifttype],MatFactorShiftTypes[(int)factor->info.shifttype]);CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer," matrix ordering: %s\n",factor->ordering);CHKERRQ(ierr); if (factor->fact) { MatInfo info; ierr = MatGetInfo(factor->fact,MAT_LOCAL,&info);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," factor fill ratio given %G, needed %G\n",info.fill_ratio_given,info.fill_ratio_needed);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Factored matrix follows:\n");CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = MatView(factor->fact,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); } } else if (isstring) { MatFactorType t; ierr = MatGetFactorType(factor->fact,&t);CHKERRQ(ierr); if (t == MAT_FACTOR_ILU || t == MAT_FACTOR_ICC) { ierr = PetscViewerStringSPrintf(viewer," lvls=%D,order=%s",(PetscInt)factor->info.levels,factor->ordering);CHKERRQ(ierr);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
int main(int argc,char **args) { PetscViewer viewer; PetscErrorCode ierr; PetscViewerFormat format; PetscBool iascii; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,NULL,"-myviewer",&viewer,&format,NULL);CHKERRQ(ierr); ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"Testing PetscViewerASCIIPrintf %d\n", 0);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,NULL,"-myviewer",&viewer,&format,NULL);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"Testing PetscViewerASCIIPrintf %d\n", 1);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); } ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc,char **args) { PetscViewer viewer; PetscErrorCode ierr; PetscViewerFormat format; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,NULL,"-myviewer",&viewer,&format,NULL);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = PetscViewerView(viewer,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode VecView_Seq_Draw(Vec xin,PetscViewer v) { PetscErrorCode ierr; PetscDraw draw; PetscBool isnull; PetscFunctionBegin; ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = PetscViewerPushFormat(v,PETSC_VIEWER_DRAW_LG);CHKERRQ(ierr); ierr = VecView_Seq_Draw_LG(xin,v);CHKERRQ(ierr); ierr = PetscViewerPopFormat(v);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode MatFDColoringViewFromOptions(MatFDColoring fd,const char optionname[]) { PetscErrorCode ierr; PetscBool flg; PetscViewer viewer; PetscViewerFormat format; PetscFunctionBegin; ierr = PetscOptionsGetViewer(((PetscObject)fd)->comm,((PetscObject)fd)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = MatFDColoringView(fd,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
// Write vector data into an ASCII file. PetscErrorCode ProbeVolume::writeVecASCII(const Vec &vec, const PetscReal &t) { PetscErrorCode ierr; PetscFunctionBeginUser; ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_SYMMODU); CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer, "\nt = %e\n", t); CHKERRQ(ierr); if (count != 0) // write number of time-steps accumulated { ierr = PetscViewerASCIIPrintf(viewer, "count = %d\n", count); CHKERRQ(ierr); } ierr = VecView(vec, viewer); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); } // ProbeVolume::writeVecASCII
/*@C KSPMonitorSolution - Monitors progress of the KSP solvers by calling VecView() for the approximate solution at each iteration. Collective on KSP Input Parameters: + ksp - the KSP context . its - iteration number . fgnorm - 2-norm of residual (or gradient) - dummy - a viewer Level: intermediate Notes: For some Krylov methods such as GMRES constructing the solution at each iteration is expensive, hence using this will slow the code. .keywords: KSP, nonlinear, vector, monitor, view .seealso: KSPMonitorSet(), KSPMonitorDefault(), VecView() @*/ PetscErrorCode KSPMonitorSolution(KSP ksp,PetscInt its,PetscReal fgnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; Vec x; PetscViewer viewer = dummy->viewer; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2); ierr = KSPBuildSolution(ksp,NULL,&x); CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = VecView(x,viewer); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ MFNSolve - Solves the matrix function problem. Given a vector b, the vector x = f(alpha*A)*b is returned. Collective on MFN Input Parameters: + mfn - matrix function context obtained from MFNCreate() - b - the right hand side vector Output Parameter: . x - the solution (this may be the same vector as b, then b will be overwritten with the answer) Options Database Keys: + -mfn_view - print information about the solver used . -mfn_view_mat binary - save the matrix to the default binary viewer . -mfn_view_rhs binary - save right hand side vector to the default binary viewer - -mfn_view_solution binary - save computed solution vector to the default binary viewer Notes: The matrix A is specified with MFNSetOperator(). The function f is specified with MFNSetFunction(). The scalar alpha is specified with MFNSetScaleFactor(). Level: beginner .seealso: MFNCreate(), MFNSetUp(), MFNDestroy(), MFNSetTolerances(), MFNSetOperator(), MFNSetFunction(), MFNSetScaleFactor() @*/ PetscErrorCode MFNSolve(MFN mfn,Vec b,Vec x) { PetscErrorCode ierr; PetscBool flg; PetscViewer viewer; PetscViewerFormat format; PetscFunctionBegin; PetscValidHeaderSpecific(mfn,MFN_CLASSID,1); PetscValidHeaderSpecific(b,VEC_CLASSID,2); PetscCheckSameComm(mfn,1,b,2); if (b!=x) PetscValidHeaderSpecific(x,VEC_CLASSID,3); if (b!=x) PetscCheckSameComm(mfn,1,x,3); /* call setup */ ierr = MFNSetUp(mfn);CHKERRQ(ierr); mfn->its = 0; ierr = MFNMonitor(mfn,mfn->its,0);CHKERRQ(ierr); /* call solver */ ierr = PetscLogEventBegin(MFN_Solve,mfn,b,x,0);CHKERRQ(ierr); ierr = (*mfn->ops->solve)(mfn,b,x);CHKERRQ(ierr); ierr = PetscLogEventEnd(MFN_Solve,mfn,b,x,0);CHKERRQ(ierr); if (!mfn->reason) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_PLIB,"Internal error, solver returned without setting converged reason"); if (mfn->errorifnotconverged && mfn->reason < 0) SETERRQ(PetscObjectComm((PetscObject)mfn),PETSC_ERR_NOT_CONVERGED,"MFNSolve has not converged"); /* various viewers */ ierr = MatViewFromOptions(mfn->A,((PetscObject)mfn)->prefix,"-mfn_view_mat");CHKERRQ(ierr); ierr = VecViewFromOptions(b,((PetscObject)mfn)->prefix,"-mfn_view_rhs");CHKERRQ(ierr); ierr = VecViewFromOptions(x,((PetscObject)mfn)->prefix,"-mfn_view_solution");CHKERRQ(ierr); ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)mfn),((PetscObject)mfn)->prefix,"-mfn_view",&viewer,&format,&flg);CHKERRQ(ierr); if (flg && !PetscPreLoadingOn) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = MFNView(mfn,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* ISColoringViewFromOptions - Processes command line options to determine if/how an ISColoring object is to be viewed. Collective on ISColoring Input Parameters: + obj - the ISColoring object . prefix - prefix to use for viewing, or NULL to use prefix of 'mat' - optionname - option to activate viewing Level: intermediate Developer Note: This cannot use PetscObjectViewFromOptions() because ISColoring is not a PetscObject */ PetscErrorCode ISColoringViewFromOptions(ISColoring obj,PetscObject bobj,const char optionname[]) { PetscErrorCode ierr; PetscViewer viewer; PetscBool flg; PetscViewerFormat format; char *prefix; PetscFunctionBegin; prefix = bobj ? bobj->prefix : NULL; ierr = PetscOptionsGetViewer(obj->comm,prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = ISColoringView(obj,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/* ISColoringViewFromOptions - Processes command line options to determine if/how an ISColoring object is to be viewed. Collective on ISColoring Input Parameters: + obj - the ISColoring object . prefix - prefix to use for viewing, or NULL to use prefix of 'mat' - optionname - option to activate viewing Level: intermediate Developer Note: This cannot use PetscObjectViewFromOptions() because ISColoring is not a PetscObject */ PetscErrorCode ISColoringViewFromOptions(ISColoring obj,const char prefix[],const char optionname[]) { PetscErrorCode ierr; PetscViewer viewer; PetscBool flg; static PetscBool incall = PETSC_FALSE; PetscViewerFormat format; PetscFunctionBegin; if (incall) PetscFunctionReturn(0); incall = PETSC_TRUE; ierr = PetscOptionsGetViewer(obj->comm,prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = ISColoringView(obj,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } incall = PETSC_FALSE; PetscFunctionReturn(0); }
/* PetscRandomViewFromOptions - This function visualizes the type and the seed of the generated random numbers based upon user options. Collective on PetscRandom Input Parameters: + rnd - The random number generator context . prefix - prefix to use for viewing, or NULL to use prefix of 'rnd' - optionname - option to activate viewing Level: intermediate .keywords: PetscRandom, view, options, database .seealso: PetscRandomSetFromOptions() */ PetscErrorCode PetscRandomViewFromOptions(PetscRandom rnd, const char prefix[], const char optionname[]) { PetscBool flg; PetscViewer viewer; PetscErrorCode ierr; PetscViewerFormat format; PetscFunctionBegin; if (prefix) { ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)rnd),prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); } else { ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject)rnd),((PetscObject)rnd)->prefix,optionname,&viewer,&format,&flg);CHKERRQ(ierr); } if (flg) { ierr = PetscViewerPushFormat(viewer,format);CHKERRQ(ierr); ierr = PetscRandomView(rnd,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } PetscFunctionReturn(0); }
/*@C KSPMonitorTrueResidualMaxNorm - Prints the true residual max norm each iteration of an iterative solver. Collective on KSP Input Parameters: + ksp - iterative context . n - iteration number . rnorm - norm (preconditioned) residual value (may be estimated). - dummy - an ASCII viewer Options Database Key: . -ksp_monitor_max - Activates KSPMonitorTrueResidualMaxNorm() Notes: This could be implemented (better) with a flag in ksp. Level: intermediate .keywords: KSP, default, monitor, residual .seealso: KSPMonitorSet(), KSPMonitorDefault(), KSPMonitorLGResidualNormCreate(),KSPMonitorTrueResidualNorm() @*/ PetscErrorCode KSPMonitorTrueResidualMaxNorm(KSP ksp,PetscInt n,PetscReal rnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; Vec resid; PetscReal truenorm,bnorm; PetscViewer viewer = dummy->viewer; char normtype[256]; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); if (n == 0 && ((PetscObject)ksp)->prefix) { ierr = PetscViewerASCIIPrintf(viewer," Residual norms (max) for %s solve.\n",((PetscObject)ksp)->prefix); CHKERRQ(ierr); } ierr = KSPBuildResidual(ksp,NULL,NULL,&resid); CHKERRQ(ierr); ierr = VecNorm(resid,NORM_INFINITY,&truenorm); CHKERRQ(ierr); ierr = VecDestroy(&resid); CHKERRQ(ierr); ierr = VecNorm(ksp->vec_rhs,NORM_INFINITY,&bnorm); CHKERRQ(ierr); ierr = PetscStrncpy(normtype,KSPNormTypes[ksp->normtype],sizeof(normtype)); CHKERRQ(ierr); ierr = PetscStrtolower(normtype); CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP true resid max norm %14.12e ||r(i)||/||b|| %14.12e\n",n,(double)truenorm,(double)(truenorm/bnorm)); CHKERRQ(ierr); ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode VecView_Seq_Draw(Vec xin,PetscViewer v) { PetscErrorCode ierr; PetscDraw draw; PetscBool isnull; PetscViewerFormat format; PetscFunctionBegin; ierr = PetscViewerDrawGetDraw(v,0,&draw);CHKERRQ(ierr); ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr); if (isnull) PetscFunctionReturn(0); ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr); /* Currently it only supports drawing to a line graph */ if (format != PETSC_VIEWER_DRAW_LG) { ierr = PetscViewerPushFormat(v,PETSC_VIEWER_DRAW_LG);CHKERRQ(ierr); } ierr = VecView_Seq_Draw_LG(xin,v);CHKERRQ(ierr); if (format != PETSC_VIEWER_DRAW_LG) { ierr = PetscViewerPopFormat(v);CHKERRQ(ierr); } PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat A; PetscViewer fd; /* viewer */ char file[PETSC_MAX_PATH_LEN]; /* input file name */ PetscErrorCode ierr; PetscBool flg; ierr = PetscInitialize(&argc,&args,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetString(NULL,NULL,"-f0",file,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) SETERRQ(PETSC_COMM_WORLD,1,"Must indicate binary file with the -f0 option"); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatLoad(A,fd);CHKERRQ(ierr); ierr = PetscViewerDestroy(&fd);CHKERRQ(ierr); ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_WORLD,PETSC_VIEWER_ASCII_MATRIXMARKET);CHKERRQ(ierr); ierr = MatView(A,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
/*@C KSPMonitorDefault - Print the residual norm at each iteration of an iterative solver. Collective on KSP Input Parameters: + ksp - iterative context . n - iteration number . rnorm - 2-norm (preconditioned) residual value (may be estimated). - dummy - an ASCII PetscViewer Level: intermediate .keywords: KSP, default, monitor, residual .seealso: KSPMonitorSet(), KSPMonitorTrueResidualNorm(), KSPMonitorLGResidualNormCreate() @*/ PetscErrorCode KSPMonitorDefault(KSP ksp,PetscInt n,PetscReal rnorm,PetscViewerAndFormat *dummy) { PetscErrorCode ierr; PetscViewer viewer = dummy->viewer; PetscFunctionBegin; PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,4); ierr = PetscViewerPushFormat(viewer,dummy->format); CHKERRQ(ierr); ierr = PetscViewerASCIIAddTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); if (n == 0 && ((PetscObject)ksp)->prefix) { ierr = PetscViewerASCIIPrintf(viewer," Residual norms for %s solve.\n",((PetscObject)ksp)->prefix); CHKERRQ(ierr); } ierr = PetscViewerASCIIPrintf(viewer,"%3D KSP Residual norm %14.12e \n",n,(double)rnorm); CHKERRQ(ierr); ierr = PetscViewerASCIISubtractTab(viewer,((PetscObject)ksp)->tablevel); CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer); CHKERRQ(ierr); PetscFunctionReturn(0); }
// Write the sub mesh into an ASCII file. PetscErrorCode ProbeVolume::writeSubMeshASCII(const std::string &filePath) { PetscErrorCode ierr; PetscFunctionBeginUser; // only the first process in the communicator write the sub-mesh into a file if (commRank == 0) { // because only one process is involved in writing the sub-mesh, // we need to create a temporary viewer PetscViewer viewer2; ierr = PetscViewerCreate(PETSC_COMM_SELF, &viewer2); CHKERRQ(ierr); ierr = PetscViewerSetType(viewer2, PETSCVIEWERASCII); CHKERRQ(ierr); ierr = PetscViewerPushFormat( viewer2, PETSC_VIEWER_ASCII_SYMMODU); CHKERRQ(ierr); ierr = PetscViewerFileSetMode(viewer2, FILE_MODE_WRITE); CHKERRQ(ierr); ierr = PetscViewerFileSetName( viewer2, filePath.c_str()); CHKERRQ(ierr); std::vector<std::string> dirs{"x", "y", "z"}; for (unsigned int d = 0; d < coord.size(); ++d) { Vec tmp; ierr = VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nPtsDir[d], &coord[d][0], &tmp); CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) tmp, dirs[d].c_str()); CHKERRQ(ierr); ierr = VecView(tmp, viewer2); CHKERRQ(ierr); ierr = VecDestroy(&tmp); CHKERRQ(ierr); } ierr = PetscViewerPopFormat(viewer2); CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer2); CHKERRQ(ierr); } ierr = MPI_Barrier(comm); CHKERRQ(ierr); PetscFunctionReturn(0); } // ProbeVolume::writeSubMeshASCII
static PetscErrorCode PCView_SupportGraph(PC pc,PetscViewer viewer) { PC_SupportGraph *sg = (PC_SupportGraph*)pc->data; PetscErrorCode ierr; PetscBool iascii; PetscFunctionBegin; ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); if (iascii) { ierr = PetscViewerASCIIPrintf(viewer," SupportGraph: maxCong = %f\n",sg->maxCong);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," SupportGraph: tol = %f\n",sg->tol);CHKERRQ(ierr); ierr = PetscViewerASCIIPrintf(viewer," Factored Matrix:\n");CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); ierr = MatView(sg->pre, viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); } else { SETERRQ1(((PetscObject)pc)->comm,PETSC_ERR_SUP,"Viewer type %s not supported for PCSupportGraph",((PetscObject)viewer)->type_name); } PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscErrorCode ierr; DS ds; SlepcSC sc; PetscScalar *A,*B,*wr,*wi; PetscReal re,im; PetscInt i,j,n=10,ld; PetscViewer viewer; PetscBool verbose; SlepcInitialize(&argc,&argv,(char*)0,help); ierr = PetscOptionsGetInt(NULL,"-n",&n,NULL);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Solve a Dense System of type GNHEP - dimension %D.\n",n);CHKERRQ(ierr); ierr = PetscOptionsHasName(NULL,"-verbose",&verbose);CHKERRQ(ierr); /* Create DS object */ ierr = DSCreate(PETSC_COMM_WORLD,&ds);CHKERRQ(ierr); ierr = DSSetType(ds,DSGNHEP);CHKERRQ(ierr); ierr = DSSetFromOptions(ds);CHKERRQ(ierr); ld = n+2; /* test leading dimension larger than n */ ierr = DSAllocate(ds,ld);CHKERRQ(ierr); ierr = DSSetDimensions(ds,n,0,0,0);CHKERRQ(ierr); /* Set up viewer */ ierr = PetscViewerASCIIGetStdout(PETSC_COMM_WORLD,&viewer);CHKERRQ(ierr); ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); ierr = DSView(ds,viewer);CHKERRQ(ierr); ierr = PetscViewerPopFormat(viewer);CHKERRQ(ierr); if (verbose) { ierr = PetscViewerPushFormat(viewer,PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); } /* Fill A with Grcar matrix */ ierr = DSGetArray(ds,DS_MAT_A,&A);CHKERRQ(ierr); ierr = PetscMemzero(A,sizeof(PetscScalar)*ld*n);CHKERRQ(ierr); for (i=1;i<n;i++) A[i+(i-1)*ld]=-1.0; for (j=0;j<4;j++) { for (i=0;i<n-j;i++) A[i+(i+j)*ld]=1.0; } ierr = DSRestoreArray(ds,DS_MAT_A,&A);CHKERRQ(ierr); /* Fill B with an identity matrix */ ierr = DSGetArray(ds,DS_MAT_B,&B);CHKERRQ(ierr); ierr = PetscMemzero(B,sizeof(PetscScalar)*ld*n);CHKERRQ(ierr); for (i=0;i<n;i++) B[i+i*ld]=1.0; ierr = DSRestoreArray(ds,DS_MAT_B,&B);CHKERRQ(ierr); if (verbose) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Initial - - - - - - - - -\n");CHKERRQ(ierr); ierr = DSView(ds,viewer);CHKERRQ(ierr); } /* Solve */ ierr = PetscMalloc2(n,&wr,n,&wi);CHKERRQ(ierr); ierr = DSGetSlepcSC(ds,&sc);CHKERRQ(ierr); sc->comparison = SlepcCompareLargestMagnitude; sc->comparisonctx = NULL; sc->map = NULL; sc->mapobj = NULL; ierr = DSSolve(ds,wr,wi);CHKERRQ(ierr); ierr = DSSort(ds,wr,wi,NULL,NULL,NULL);CHKERRQ(ierr); if (verbose) { ierr = PetscPrintf(PETSC_COMM_WORLD,"After solve - - - - - - - - -\n");CHKERRQ(ierr); ierr = DSView(ds,viewer);CHKERRQ(ierr); } /* Print eigenvalues */ ierr = PetscPrintf(PETSC_COMM_WORLD,"Computed eigenvalues =\n",n);CHKERRQ(ierr); for (i=0;i<n;i++) { #if defined(PETSC_USE_COMPLEX) re = PetscRealPart(wr[i]); im = PetscImaginaryPart(wr[i]); #else re = wr[i]; im = wi[i]; #endif if (PetscAbs(im)<1e-10) { ierr = PetscViewerASCIIPrintf(viewer," %.5f\n",(double)re);CHKERRQ(ierr); } else { ierr = PetscViewerASCIIPrintf(viewer," %.5f%+.5fi\n",(double)re,(double)im);CHKERRQ(ierr); } } ierr = PetscFree2(wr,wi);CHKERRQ(ierr); ierr = DSDestroy(&ds);CHKERRQ(ierr); ierr = SlepcFinalize(); return 0; }