本文整理汇总了C++中KSPSetTolerances函数的典型用法代码示例。如果您正苦于以下问题:C++ KSPSetTolerances函数的具体用法?C++ KSPSetTolerances怎么用?C++ KSPSetTolerances使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了KSPSetTolerances函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: PCApplyRichardson_MG
static PetscErrorCode PCApplyRichardson_MG(PC pc,Vec b,Vec x,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its,PetscBool zeroguess,PetscInt *outits,PCRichardsonConvergedReason *reason)
{
PC_MG *mg = (PC_MG*)pc->data;
PC_MG_Levels **mglevels = mg->levels;
PetscErrorCode ierr;
PetscInt levels = mglevels[0]->levels,i;
PetscFunctionBegin;
/* When the DM is supplying the matrix then it will not exist until here */
for (i=0; i<levels; i++) {
if (!mglevels[i]->A) {
ierr = KSPGetOperators(mglevels[i]->smoothu,&mglevels[i]->A,NULL);CHKERRQ(ierr);
ierr = PetscObjectReference((PetscObject)mglevels[i]->A);CHKERRQ(ierr);
}
}
mglevels[levels-1]->b = b;
mglevels[levels-1]->x = x;
mg->rtol = rtol;
mg->abstol = abstol;
mg->dtol = dtol;
if (rtol) {
/* compute initial residual norm for relative convergence test */
PetscReal rnorm;
if (zeroguess) {
ierr = VecNorm(b,NORM_2,&rnorm);CHKERRQ(ierr);
} else {
ierr = (*mglevels[levels-1]->residual)(mglevels[levels-1]->A,b,x,w);CHKERRQ(ierr);
ierr = VecNorm(w,NORM_2,&rnorm);CHKERRQ(ierr);
}
mg->ttol = PetscMax(rtol*rnorm,abstol);
} else if (abstol) mg->ttol = abstol;
else mg->ttol = 0.0;
/* since smoother is applied to full system, not just residual we need to make sure that smoothers don't
stop prematurely due to small residual */
for (i=1; i<levels; i++) {
ierr = KSPSetTolerances(mglevels[i]->smoothu,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
if (mglevels[i]->smoothu != mglevels[i]->smoothd) {
ierr = KSPSetTolerances(mglevels[i]->smoothd,0,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT);CHKERRQ(ierr);
}
}
*reason = (PCRichardsonConvergedReason)0;
for (i=0; i<its; i++) {
ierr = PCMGMCycle_Private(pc,mglevels+levels-1,reason);CHKERRQ(ierr);
if (*reason) break;
}
if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
*outits = i;
PetscFunctionReturn(0);
}
示例2: number
/*@C
KSPMonitorDynamicTolerance - Recompute the inner tolerance in every
outer iteration in an adaptive way.
Collective on KSP
Input Parameters:
+ ksp - iterative context
. n - iteration number (not used)
. fnorm - the current residual norm
. dummy - some context as a C struct. fields:
coef: a scaling coefficient. default 1.0. can be passed through
-sub_ksp_dynamic_tolerance_param
bnrm: norm of the right-hand side. store it to avoid repeated calculation
Notes:
This may be useful for a flexibly preconditioner Krylov method to
control the accuracy of the inner solves needed to gaurantee the
convergence of the outer iterations.
Level: advanced
.keywords: KSP, inner tolerance
.seealso: KSPMonitorDynamicToleranceDestroy()
@*/
PetscErrorCode KSPMonitorDynamicTolerance(KSP ksp,PetscInt its,PetscReal fnorm,void *dummy)
{
PetscErrorCode ierr;
PC pc;
PetscReal outer_rtol, outer_abstol, outer_dtol, inner_rtol;
PetscInt outer_maxits,nksp,first,i;
KSPDynTolCtx *scale = (KSPDynTolCtx*)dummy;
KSP kspinner = NULL, *subksp = NULL;
PetscFunctionBegin;
ierr = KSPGetPC(ksp, &pc);
CHKERRQ(ierr);
/* compute inner_rtol */
if (scale->bnrm < 0.0) {
Vec b;
ierr = KSPGetRhs(ksp, &b);
CHKERRQ(ierr);
ierr = VecNorm(b, NORM_2, &(scale->bnrm));
CHKERRQ(ierr);
}
ierr = KSPGetTolerances(ksp, &outer_rtol, &outer_abstol, &outer_dtol, &outer_maxits);
CHKERRQ(ierr);
inner_rtol = PetscMin(scale->coef * scale->bnrm * outer_rtol / fnorm, 0.999);
/*ierr = PetscPrintf(PETSC_COMM_WORLD, " Inner rtol = %g\n", (double)inner_rtol);CHKERRQ(ierr);*/
/* if pc is ksp */
ierr = PCKSPGetKSP(pc, &kspinner);
CHKERRQ(ierr);
if (kspinner) {
ierr = KSPSetTolerances(kspinner, inner_rtol, outer_abstol, outer_dtol, outer_maxits);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/* if pc is bjacobi */
ierr = PCBJacobiGetSubKSP(pc, &nksp, &first, &subksp);
CHKERRQ(ierr);
if (subksp) {
for (i=0; i<nksp; i++) {
ierr = KSPSetTolerances(subksp[i], inner_rtol, outer_abstol, outer_dtol, outer_maxits);
CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
/* todo: dynamic tolerance may apply to other types of pc too */
PetscFunctionReturn(0);
}
示例3: MultigridSolver_CreateOuterSolver
MGSolver_PETScData* MultigridSolver_CreateOuterSolver( MultigridSolver* self, Mat matrix ) {
//MatrixSolver* outerSolver;
MGSolver_PETScData* outerSolver = malloc( sizeof(MGSolver_PETScData) );
PC pc;
/*
outerSolver = (MatrixSolver*)PETScMatrixSolver_New( "" );
PETScMatrixSolver_SetKSPType( outerSolver, PETScMatrixSolver_KSPType_Richardson );
PETScMatrixSolver_SetPCType( outerSolver, PETScMatrixSolver_PCType_SOR );
PETScMatrixSolver_SetMatrix( outerSolver, matrix );
PETScMatrixSolver_SetMaxIterations( outerSolver, 3 );
PETScMatrixSolver_SetUseInitialSolution( outerSolver, True );
PETScMatrixSolver_SetNormType( outerSolver, PETScMatrixSolver_NormType_Preconditioned );
*/
KSPCreate( MPI_COMM_WORLD, &outerSolver->ksp );
KSPSetType( outerSolver->ksp, KSPRICHARDSON );
KSPGetPC( outerSolver->ksp, &pc );
PCSetType( pc, PCSOR );
if( outerSolver->matrix != PETSC_NULL )
Stg_MatDestroy(&outerSolver->matrix );
outerSolver->matrix = matrix;
Stg_KSPSetOperators( outerSolver->ksp, matrix, matrix, DIFFERENT_NONZERO_PATTERN );
KSPSetTolerances( outerSolver->ksp, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, (PetscInt)3 );
KSPSetInitialGuessNonzero( outerSolver->ksp, (PetscTruth)True );
KSPSetNormType( outerSolver->ksp, MultigridSolver_NormType_Preconditioned );
return outerSolver;
}
示例4: KSPSetTolerances
int TaoLinearSolverPetsc::SetTolerances(double rtol, double atol,
double dtol, int maxits) {
int info;
PetscFunctionBegin;
info = KSPSetTolerances(ksp, rtol, atol, dtol, maxits); CHKERRQ(info);
PetscFunctionReturn(0);
}
示例5: TSMonitorSPEigCtxDestroy
/*@C
TSMonitorSPEigCtxCreate - Creates a context for use with TS to monitor the eigenvalues of the linearized operator
Collective on TS
Input Parameters:
+ host - the X display to open, or null for the local machine
. label - the title to put in the title bar
. x, y - the screen coordinates of the upper left coordinate of the window
. m, n - the screen width and height in pixels
- howoften - if positive then determines the frequency of the plotting, if -1 then only at the final time
Output Parameter:
. ctx - the context
Options Database Key:
. -ts_monitor_sp_eig - plot egienvalues of linearized right hand side
Notes:
Use TSMonitorSPEigCtxDestroy() to destroy.
Currently only works if the Jacobian is provided explicitly.
Currently only works for ODEs u_t - F(t,u) = 0; that is with no mass matrix.
Level: intermediate
.keywords: TS, monitor, line graph, residual, seealso
.seealso: TSMonitorSPEigTimeStep(), TSMonitorSet(), TSMonitorLGSolution(), TSMonitorLGError()
@*/
PetscErrorCode TSMonitorSPEigCtxCreate(MPI_Comm comm,const char host[],const char label[],int x,int y,int m,int n,PetscInt howoften,TSMonitorSPEigCtx *ctx)
{
PetscDraw win;
PetscErrorCode ierr;
PC pc;
PetscFunctionBegin;
ierr = PetscNew(ctx);CHKERRQ(ierr);
ierr = PetscRandomCreate(comm,&(*ctx)->rand);CHKERRQ(ierr);
ierr = PetscRandomSetFromOptions((*ctx)->rand);CHKERRQ(ierr);
ierr = PetscDrawCreate(comm,host,label,x,y,m,n,&win);CHKERRQ(ierr);
ierr = PetscDrawSetFromOptions(win);CHKERRQ(ierr);
ierr = PetscDrawSPCreate(win,1,&(*ctx)->drawsp);CHKERRQ(ierr);
ierr = KSPCreate(comm,&(*ctx)->ksp);CHKERRQ(ierr);
ierr = KSPSetOptionsPrefix((*ctx)->ksp,"ts_monitor_sp_eig_");CHKERRQ(ierr); /* this is wrong, used use also prefix from the TS */
ierr = KSPSetType((*ctx)->ksp,KSPGMRES);CHKERRQ(ierr);
ierr = KSPGMRESSetRestart((*ctx)->ksp,200);CHKERRQ(ierr);
ierr = KSPSetTolerances((*ctx)->ksp,1.e-10,PETSC_DEFAULT,PETSC_DEFAULT,200);CHKERRQ(ierr);
ierr = KSPSetComputeSingularValues((*ctx)->ksp,PETSC_TRUE);CHKERRQ(ierr);
ierr = KSPSetFromOptions((*ctx)->ksp);CHKERRQ(ierr);
ierr = KSPGetPC((*ctx)->ksp,&pc);CHKERRQ(ierr);
ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr);
(*ctx)->howoften = howoften;
(*ctx)->computeexplicitly = PETSC_FALSE;
ierr = PetscOptionsGetBool(NULL,"-ts_monitor_sp_eig_explicitly",&(*ctx)->computeexplicitly,NULL);CHKERRQ(ierr);
(*ctx)->comm = comm;
(*ctx)->xmin = -2.1;
(*ctx)->xmax = 1.1;
(*ctx)->ymin = -1.1;
(*ctx)->ymax = 1.1;
PetscFunctionReturn(0);
}
示例6: p_build
/// Do what is necessary to build this instance
void p_build(const std::string& option_prefix)
{
PetscErrorCode ierr;
try {
parallel::Communicator comm(this->communicator());
if (this->p_doSerial) {
comm = this->communicator().self();
}
ierr = KSPCreate(comm, &p_KSP); CHKERRXX(ierr);
if (!this->p_guessZero) {
ierr = KSPSetInitialGuessNonzero(p_KSP,PETSC_TRUE); CHKERRXX(ierr);
} else {
ierr = KSPSetInitialGuessNonzero(p_KSP,PETSC_FALSE); CHKERRXX(ierr);
}
ierr = KSPSetOptionsPrefix(p_KSP, option_prefix.c_str()); CHKERRXX(ierr);
PC pc;
ierr = KSPGetPC(p_KSP, &pc); CHKERRXX(ierr);
ierr = PCSetOptionsPrefix(pc, option_prefix.c_str()); CHKERRXX(ierr);
ierr = KSPSetTolerances(p_KSP,
LinearSolverImplementation<T, I>::p_relativeTolerance,
LinearSolverImplementation<T, I>::p_solutionTolerance,
PETSC_DEFAULT,
LinearSolverImplementation<T, I>::p_maxIterations); CHKERRXX(ierr);
ierr = KSPSetFromOptions(p_KSP);CHKERRXX(ierr);
} catch (const PETSC_EXCEPTION_TYPE& e) {
throw PETScException(ierr, e);
}
}
示例7: try_solve_complex_bj
/*! \brief try to solve the system with block jacobi pre-conditioner
*
*
*
*/
void try_solve_complex_bj(Mat & A_, const Vec & b_, Vec & x_)
{
PETSC_SAFE_CALL(KSPSetTolerances(ksp,rtol,abstol,dtol,5));
PETSC_SAFE_CALL(KSPSetConvergenceTest(ksp,KSPConvergedSkip,NULL,NULL));
solve_complex(A_,b_,x_);
}
示例8: SolvePressure
PetscErrorCode SolvePressure(UserContext *uc)
{
PetscErrorCode ierr;
PetscFunctionBegin;
PetscLogEventBegin(EVENT_SolvePressure,0,0,0,0);
ierr = KSPCreate(PETSC_COMM_WORLD, &uc->ksp); CHKERRQ(ierr);
ierr = KSPSetType(uc->ksp,KSPCG); CHKERRQ(ierr);
ierr = KSPSetOperators(uc->ksp,uc->A,uc->A,SAME_NONZERO_PATTERN); CHKERRQ(ierr);
ierr = KSPSetFromOptions(uc->ksp); CHKERRQ(ierr);
ierr = KSPSetTolerances(uc->ksp, 0.000001, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT); CHKERRQ(ierr);
ierr = KSPSetType( uc->ksp, KSPPREONLY); CHKERRQ(ierr);
PC pc;
ierr = KSPGetPC(uc->ksp, &pc); CHKERRQ(ierr);
ierr = PCSetType(pc, PCLU); CHKERRQ(ierr);
ierr = KSPSolve(uc->ksp,uc->b,uc->p);CHKERRQ(ierr);
KSPConvergedReason reason;
KSPGetConvergedReason(uc->ksp,&reason);
PetscPrintf(PETSC_COMM_WORLD,"Pressure KSPConvergedReason: %D\n", reason);
// if( reason < 0 ) SETERRQ(PETSC_ERR_CONV_FAILED, "Exiting: Failed to converge\n");
PetscLogEventEnd(EVENT_SolvePressure,0,0,0,0);
PetscFunctionReturn(0);
}
示例9: KSPChebyshevEstEigSet_Chebyshev
static PetscErrorCode KSPChebyshevEstEigSet_Chebyshev(KSP ksp,PetscReal a,PetscReal b,PetscReal c,PetscReal d)
{
KSP_Chebyshev *cheb = (KSP_Chebyshev*)ksp->data;
PetscErrorCode ierr;
PetscFunctionBegin;
if (a != 0.0 || b != 0.0 || c != 0.0 || d != 0.0) {
if (!cheb->kspest) { /* should this block of code be moved to KSPSetUp_Chebyshev()? */
ierr = KSPCreate(PetscObjectComm((PetscObject)ksp),&cheb->kspest);CHKERRQ(ierr);
ierr = PetscObjectIncrementTabLevel((PetscObject)cheb->kspest,(PetscObject)ksp,1);CHKERRQ(ierr);
ierr = KSPSetOptionsPrefix(cheb->kspest,((PetscObject)ksp)->prefix);CHKERRQ(ierr);
ierr = KSPAppendOptionsPrefix(cheb->kspest,"esteig_");CHKERRQ(ierr);
ierr = KSPSetSkipPCSetFromOptions(cheb->kspest,PETSC_TRUE);CHKERRQ(ierr);
ierr = KSPSetPC(cheb->kspest,ksp->pc);CHKERRQ(ierr);
ierr = KSPSetComputeEigenvalues(cheb->kspest,PETSC_TRUE);CHKERRQ(ierr);
/* We cannot turn off convergence testing because GMRES will break down if you attempt to keep iterating after a zero norm is obtained */
ierr = KSPSetTolerances(cheb->kspest,1.e-12,PETSC_DEFAULT,PETSC_DEFAULT,cheb->eststeps);CHKERRQ(ierr);
}
if (a >= 0) cheb->tform[0] = a;
if (b >= 0) cheb->tform[1] = b;
if (c >= 0) cheb->tform[2] = c;
if (d >= 0) cheb->tform[3] = d;
cheb->amatid = 0;
cheb->pmatid = 0;
cheb->amatstate = -1;
cheb->pmatstate = -1;
} else {
ierr = KSPDestroy(&cheb->kspest);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例10: KSPCreate
/*!
\brief KSP and PC type
KSPRICHARDSON "richardson"
KSPCHEBYCHEV "chebychev"
KSPCG "cg"
KSPCGNE "cgne"
KSPNASH "nash"
KSPSTCG "stcg"
KSPGLTR "gltr"
KSPGMRES "gmres"
KSPFGMRES "fgmres"
KSPLGMRES "lgmres"
KSPDGMRES "dgmres"
KSPTCQMR "tcqmr"
KSPBCGS "bcgs"
KSPIBCGS "ibcgs"
KSPBCGSL "bcgsl"
KSPCGS "cgs"
KSPTFQMR "tfqmr"
KSPCR "cr"
KSPLSQR "lsqr"
KSPPREONLY "preonly"
KSPQCG "qcg"
KSPBICG "bicg"
KSPMINRES "minres"
KSPSYMMLQ "symmlq"
KSPLCD "lcd"
KSPPYTHON "python"
KSPBROYDEN "broyden"
KSPGCR "gcr"
KSPNGMRES "ngmres"
KSPSPECEST "specest"
PCNONE "none"
PCJACOBI "jacobi"
PCSOR "sor"
PCLU "lu"
PCSHELL "shell"
PCBJACOBI "bjacobi"
PCMG "mg"
PCEISENSTAT "eisenstat"
PCILU "ilu"
PCICC "icc"
PCASM "asm"
PCGASM "gasm"
PCKSP "ksp"
PCCOMPOSITE "composite"
PCREDUNDANT "redundant"
PCSPAI "spai"
PCNN "nn"
PCCHOLESKY "cholesky"
PCPBJACOBI "pbjacobi"
PCMAT "mat"
PCHYPRE "hypre"
PCPARMS "parms"
PCFIELDSPLIT "fieldsplit"
PCTFS "tfs"
PCML "ml"
PCPROMETHEUS "prometheus"
PCGALERKIN "galerkin"
PCEXOTIC "exotic"
PCHMPI "hmpi"
PCSUPPORTGRAPH "supportgraph"
PCASA "asa"
PCCP "cp"
PCBFBT "bfbt"
PCLSC "lsc"
PCPYTHON "python"
PCPFMG "pfmg"
PCSYSPFMG "syspfmg"
PCREDISTRIBUTE "redistribute"
PCSACUSP "sacusp"
PCSACUSPPOLY "sacusppoly"
PCBICGSTABCUSP "bicgstabcusp"
PCSVD "svd"
PCAINVCUSP "ainvcusp"
PCGAMG "gamg"
*/
void PETScLinearSolver::Config(const PetscReal tol, const PetscInt maxits, const KSPType lsol,
const PCType prec_type, const std::string &prefix)
{
ltolerance = tol;
sol_type = lsol;
pc_type = prec_type;
KSPCreate(PETSC_COMM_WORLD,&lsolver);
#if (PETSC_VERSION_MAJOR == 3) && (PETSC_VERSION_MINOR > 4)
KSPSetOperators(lsolver, A, A);
#else
KSPSetOperators(lsolver, A, A, DIFFERENT_NONZERO_PATTERN);
#endif
KSPSetType(lsolver,lsol);
KSPGetPC(lsolver, &prec);
PCSetType(prec, prec_type); // PCJACOBI); //PCNONE);
KSPSetTolerances(lsolver,ltolerance, PETSC_DEFAULT, PETSC_DEFAULT, maxits);
//.........这里部分代码省略.........
示例11: MatDuplicate_LMVM
static PetscErrorCode MatDuplicate_LMVM(Mat B, MatDuplicateOption op, Mat *mat)
{
Mat_LMVM *bctx = (Mat_LMVM*)B->data;
Mat_LMVM *mctx;
PetscErrorCode ierr;
MatType lmvmType;
Mat A;
PetscFunctionBegin;
ierr = MatGetType(B, &lmvmType);CHKERRQ(ierr);
ierr = MatCreate(PetscObjectComm((PetscObject)B), mat);CHKERRQ(ierr);
ierr = MatSetType(*mat, lmvmType);CHKERRQ(ierr);
A = *mat;
mctx = (Mat_LMVM*)A->data;
mctx->m = bctx->m;
mctx->ksp_max_it = bctx->ksp_max_it;
mctx->ksp_rtol = bctx->ksp_rtol;
mctx->ksp_atol = bctx->ksp_atol;
mctx->shift = bctx->shift;
ierr = KSPSetTolerances(mctx->J0ksp, mctx->ksp_rtol, mctx->ksp_atol, PETSC_DEFAULT, mctx->ksp_max_it);CHKERRQ(ierr);
ierr = MatLMVMAllocate(*mat, bctx->Xprev, bctx->Fprev);CHKERRQ(ierr);
if (op == MAT_COPY_VALUES) {
ierr = MatCopy(B, *mat, SAME_NONZERO_PATTERN);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例12: PCASMGetSubKSP
void PetscPreconditioner::set_petsc_subpreconditioner_type(const PCType type, PC& pc) {
int ierr;
KSP* subksps;
int nlocal;
ierr = PCASMGetSubKSP(pc, &nlocal, PETSC_NULL, &subksps);
CHKERRABORT(MPI_COMM_WORLD, ierr);
PetscReal epsilon = 1.e-16;
for(int i = 0; i < nlocal; i++) {
PC subpc;
ierr = KSPGetPC(subksps[i], &subpc);
CHKERRABORT(MPI_COMM_WORLD, ierr);
ierr = KSPSetTolerances(subksps[i], PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT, 1);
CHKERRABORT(MPI_COMM_WORLD, ierr);
ierr = KSPSetFromOptions(subksps[i]);
CHKERRABORT(MPI_COMM_WORLD, ierr);
ierr = PCSetType(subpc, type);
CHKERRABORT(MPI_COMM_WORLD, ierr);
ierr = PCFactorSetZeroPivot(subpc, epsilon);
CHKERRABORT(MPI_COMM_WORLD, ierr);
ierr = PCFactorSetShiftType(subpc, MAT_SHIFT_NONZERO);
CHKERRABORT(MPI_COMM_WORLD, ierr);
}
}
示例13: example
/*@
KSPFGMRESModifyPCKSP - modifies the attributes of the
GMRES preconditioner. It serves as an example (not as something
useful!)
Input Parameters:
+ ksp - the ksp context being used.
. total_its - the total number of FGMRES iterations that have occurred.
. loc_its - the number of FGMRES iterations since last restart.
. res_norm - the current residual norm.
- dummy - context, not used here
Level: intermediate
Contributed by Allison Baker
This could be used as a template!
.seealso: KSPFGMRESSetModifyPC(), KSPFGMRESModifyPCKSP()
@*/
PetscErrorCode KSPFGMRESModifyPCKSP(KSP ksp,PetscInt total_its,PetscInt loc_its,PetscReal res_norm,void *dummy)
{
PC pc;
PetscErrorCode ierr;
PetscInt maxits;
KSP sub_ksp;
PetscReal rtol,abstol,dtol;
PetscBool isksp;
PetscFunctionBegin;
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)pc,PCKSP,&isksp);CHKERRQ(ierr);
if (isksp) {
ierr = PCKSPGetKSP(pc,&sub_ksp);CHKERRQ(ierr);
/* note that at this point you could check the type of KSP with KSPGetType() */
/* Now we can use functions such as KSPGMRESSetRestart() or
KSPGMRESSetOrthogonalization() or KSPSetTolerances() */
ierr = KSPGetTolerances(sub_ksp,&rtol,&abstol,&dtol,&maxits);CHKERRQ(ierr);
if (!loc_its) {
rtol = .1;
} else {
rtol *= .9;
}
ierr = KSPSetTolerances(sub_ksp,rtol,abstol,dtol,maxits);CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
示例14: DMCreateLibMesh
void PetscDMNonlinearSolver<T>::init()
{
PetscErrorCode ierr;
DM dm;
this->PetscNonlinearSolver<T>::init();
// Attaching a DM with the function and Jacobian callbacks to SNES.
ierr = DMCreateLibMesh(libMesh::COMM_WORLD, this->system(), &dm); CHKERRABORT(libMesh::COMM_WORLD, ierr);
ierr = DMSetFromOptions(dm); CHKERRABORT(libMesh::COMM_WORLD, ierr);
ierr = DMSetUp(dm); CHKERRABORT(libMesh::COMM_WORLD, ierr);
ierr = SNESSetDM(this->_snes, dm); CHKERRABORT(libMesh::COMM_WORLD, ierr);
// SNES now owns the reference to dm.
ierr = DMDestroy(&dm); CHKERRABORT(libMesh::COMM_WORLD, ierr);
KSP ksp;
ierr = SNESGetKSP (this->_snes, &ksp); CHKERRABORT(libMesh::COMM_WORLD,ierr);
// Set the tolerances for the iterative solver. Use the user-supplied
// tolerance for the relative residual & leave the others at default values
ierr = KSPSetTolerances (ksp, this->initial_linear_tolerance, PETSC_DEFAULT,PETSC_DEFAULT, this->max_linear_iterations); CHKERRABORT(libMesh::COMM_WORLD,ierr);
// Set the tolerances for the non-linear solver.
ierr = SNESSetTolerances(this->_snes,
this->absolute_residual_tolerance,
this->relative_residual_tolerance,
this->absolute_step_tolerance,
this->max_nonlinear_iterations,
this->max_function_evaluations);
CHKERRABORT(libMesh::COMM_WORLD,ierr);
//Pull in command-line options
KSPSetFromOptions(ksp);
SNESSetFromOptions(this->_snes);
}
示例15: SNESLineSearchSetPostCheck
/*
PostSetSubKSP - Optional user-defined routine that reset SubKSP options when hierarchical bjacobi PC is used
e.g,
mpiexec -n 8 ./ex3 -nox -n 10000 -ksp_type fgmres -pc_type bjacobi -pc_bjacobi_blocks 4 -sub_ksp_type gmres -sub_ksp_max_it 3 -post_setsubksp -sub_ksp_rtol 1.e-16
Set by SNESLineSearchSetPostCheck().
Input Parameters:
linesearch - the LineSearch context
xcurrent - current solution
y - search direction and length
x - the new candidate iterate
Output Parameters:
y - proposed step (search direction and length) (possibly changed)
x - current iterate (possibly modified)
*/
PetscErrorCode PostSetSubKSP(SNESLineSearch linesearch,Vec xcurrent,Vec y,Vec x,PetscBool *changed_y,PetscBool *changed_x, void * ctx)
{
PetscErrorCode ierr;
SetSubKSPCtx *check;
PetscInt iter,its,sub_its,maxit;
KSP ksp,sub_ksp,*sub_ksps;
PC pc;
PetscReal ksp_ratio;
SNES snes;
PetscFunctionBeginUser;
ierr = SNESLineSearchGetSNES(linesearch, &snes);CHKERRQ(ierr);
check = (SetSubKSPCtx*)ctx;
ierr = SNESGetIterationNumber(snes,&iter);CHKERRQ(ierr);
ierr = SNESGetKSP(snes,&ksp);CHKERRQ(ierr);
ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr);
ierr = PCBJacobiGetSubKSP(pc,NULL,NULL,&sub_ksps);CHKERRQ(ierr);
sub_ksp = sub_ksps[0];
ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); /* outer KSP iteration number */
ierr = KSPGetIterationNumber(sub_ksp,&sub_its);CHKERRQ(ierr); /* inner KSP iteration number */
if (iter) {
ierr = PetscPrintf(PETSC_COMM_WORLD," ...PostCheck snes iteration %D, ksp_it %d %d, subksp_it %d\n",iter,check->its0,its,sub_its);CHKERRQ(ierr);
ksp_ratio = ((PetscReal)(its))/check->its0;
maxit = (PetscInt)(ksp_ratio*sub_its + 0.5);
if (maxit < 2) maxit = 2;
ierr = KSPSetTolerances(sub_ksp,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,maxit);CHKERRQ(ierr);
ierr = PetscPrintf(PETSC_COMM_WORLD," ...ksp_ratio %g, new maxit %d\n\n",ksp_ratio,maxit);CHKERRQ(ierr);
}
check->its0 = its; /* save current outer KSP iteration number */
PetscFunctionReturn(0);
}