PetscErrorCode STSetFromOptions_Shell(ST st) { PetscErrorCode ierr; PC pc; PCType pctype; KSPType ksptype; PetscFunctionBegin; if (!st->ksp) { ierr = STGetKSP(st,&st->ksp);CHKERRQ(ierr); } ierr = KSPGetPC(st->ksp,&pc);CHKERRQ(ierr); ierr = KSPGetType(st->ksp,&ksptype);CHKERRQ(ierr); ierr = PCGetType(pc,&pctype);CHKERRQ(ierr); if (!pctype && !ksptype) { if (st->shift_matrix == ST_MATMODE_SHELL) { /* in shell mode use GMRES with Jacobi as the default */ ierr = KSPSetType(st->ksp,KSPGMRES);CHKERRQ(ierr); ierr = PCSetType(pc,PCJACOBI);CHKERRQ(ierr); } else { /* use direct solver as default */ ierr = KSPSetType(st->ksp,KSPPREONLY);CHKERRQ(ierr); ierr = PCSetType(pc,PCREDUNDANT);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
static PetscErrorCode PCBDDCScalingSetUp_Deluxe_Private(PC pc) { PC_BDDC *pcbddc=(PC_BDDC*)pc->data; PCBDDCDeluxeScaling deluxe_ctx=pcbddc->deluxe_ctx; PCBDDCSubSchurs sub_schurs = pcbddc->sub_schurs; PetscErrorCode ierr; PetscFunctionBegin; if (!sub_schurs->n_subs) { PetscFunctionReturn(0); } /* Create work vectors for sequential part of deluxe */ ierr = MatCreateVecs(sub_schurs->S_Ej_all,&deluxe_ctx->seq_work1,&deluxe_ctx->seq_work2);CHKERRQ(ierr); /* Compute deluxe sequential scatter */ if (sub_schurs->reuse_mumps && !sub_schurs->is_dir) { PCBDDCReuseMumps reuse_mumps = sub_schurs->reuse_mumps; ierr = PetscObjectReference((PetscObject)reuse_mumps->correction_scatter_B);CHKERRQ(ierr); deluxe_ctx->seq_scctx = reuse_mumps->correction_scatter_B; } else { ierr = VecScatterCreate(pcbddc->work_scaling,sub_schurs->is_Ej_all,deluxe_ctx->seq_work1,NULL,&deluxe_ctx->seq_scctx);CHKERRQ(ierr); } /* Create Mat object for deluxe scaling */ ierr = PetscObjectReference((PetscObject)sub_schurs->S_Ej_all);CHKERRQ(ierr); deluxe_ctx->seq_mat = sub_schurs->S_Ej_all; if (sub_schurs->sum_S_Ej_all) { /* if this matrix is present, then we need to create the KSP object to invert it */ PC pc_temp; MatSolverPackage solver=NULL; char ksp_prefix[256]; size_t len; ierr = KSPCreate(PETSC_COMM_SELF,&deluxe_ctx->seq_ksp);CHKERRQ(ierr); ierr = KSPSetOperators(deluxe_ctx->seq_ksp,sub_schurs->sum_S_Ej_all,sub_schurs->sum_S_Ej_all);CHKERRQ(ierr); ierr = KSPSetType(deluxe_ctx->seq_ksp,KSPPREONLY);CHKERRQ(ierr); ierr = KSPGetPC(deluxe_ctx->seq_ksp,&pc_temp);CHKERRQ(ierr); ierr = PCSetType(pc_temp,PCLU);CHKERRQ(ierr); ierr = KSPGetPC(pcbddc->ksp_D,&pc_temp);CHKERRQ(ierr); ierr = PCFactorGetMatSolverPackage(pc_temp,(const MatSolverPackage*)&solver);CHKERRQ(ierr); if (solver) { PC new_pc; PCType type; ierr = PCGetType(pc_temp,&type);CHKERRQ(ierr); ierr = KSPGetPC(deluxe_ctx->seq_ksp,&new_pc);CHKERRQ(ierr); ierr = PCSetType(new_pc,type);CHKERRQ(ierr); ierr = PCFactorSetMatSolverPackage(new_pc,solver);CHKERRQ(ierr); } ierr = PetscStrlen(((PetscObject)(pcbddc->ksp_D))->prefix,&len);CHKERRQ(ierr); len -= 10; /* remove "dirichlet_" */ ierr = PetscStrncpy(ksp_prefix,((PetscObject)(pcbddc->ksp_D))->prefix,len+1);CHKERRQ(ierr); ierr = PetscStrcat(ksp_prefix,"deluxe_");CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(deluxe_ctx->seq_ksp,ksp_prefix);CHKERRQ(ierr); ierr = KSPSetFromOptions(deluxe_ctx->seq_ksp);CHKERRQ(ierr); ierr = KSPSetUp(deluxe_ctx->seq_ksp);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode PETSCSNES_DLLEXPORT DMMGSetUpLevel(DMMG *dmmg,KSP ksp,PetscInt nlevels) { PetscErrorCode ierr; PetscInt i; PC pc; PetscTruth ismg,ismf,isshell,ismffd; KSP lksp; /* solver internal to the multigrid preconditioner */ MPI_Comm *comms; PetscFunctionBegin; if (!dmmg) SETERRQ(PETSC_ERR_ARG_NULL,"Passing null as DMMG"); /* use fgmres on outer iteration by default */ ierr = KSPSetType(ksp,KSPFGMRES);CHKERRQ(ierr); ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCMG);CHKERRQ(ierr); ierr = PetscMalloc(nlevels*sizeof(MPI_Comm),&comms);CHKERRQ(ierr); for (i=0; i<nlevels; i++) { comms[i] = dmmg[i]->comm; } ierr = PCMGSetLevels(pc,nlevels,comms);CHKERRQ(ierr); ierr = PetscFree(comms);CHKERRQ(ierr); ierr = PCMGSetType(pc,PC_MG_FULL);CHKERRQ(ierr); ierr = PetscTypeCompare((PetscObject)pc,PCMG,&ismg);CHKERRQ(ierr); if (ismg) { /* set solvers for each level */ for (i=0; i<nlevels; i++) { if (i < nlevels-1) { /* don't set for finest level, they are set in PCApply_MG()*/ ierr = PCMGSetX(pc,i,dmmg[i]->x);CHKERRQ(ierr); ierr = PCMGSetRhs(pc,i,dmmg[i]->b);CHKERRQ(ierr); } if (i > 0) { ierr = PCMGSetR(pc,i,dmmg[i]->r);CHKERRQ(ierr); } /* If using a matrix free multiply and did not provide an explicit matrix to build the preconditioner then must use no preconditioner */ ierr = PetscTypeCompare((PetscObject)dmmg[i]->B,MATSHELL,&isshell);CHKERRQ(ierr); ierr = PetscTypeCompare((PetscObject)dmmg[i]->B,MATDAAD,&ismf);CHKERRQ(ierr); ierr = PetscTypeCompare((PetscObject)dmmg[i]->B,MATMFFD,&ismffd);CHKERRQ(ierr); if (isshell || ismf || ismffd) { PC lpc; ierr = PCMGGetSmoother(pc,i,&lksp);CHKERRQ(ierr); ierr = KSPGetPC(lksp,&lpc);CHKERRQ(ierr); ierr = PCSetType(lpc,PCNONE);CHKERRQ(ierr); } } /* Set interpolation/restriction between levels */ for (i=1; i<nlevels; i++) { ierr = PCMGSetInterpolation(pc,i,dmmg[i]->R);CHKERRQ(ierr); ierr = PCMGSetRestriction(pc,i,dmmg[i]->R);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
MGSolver_PETScData* MultigridSolver_CreateCoarseSolver( MultigridSolver* self, Mat matrix ) { //MatrixSolver* coarseSolver; MGSolver_PETScData* courseSolver; unsigned nProcs; PC pc; MPI_Comm_size( self->mgData->comm, (int*)&nProcs ); /* coarseSolver = (MatrixSolver*)PETScMatrixSolver_New( "" ); PETScMatrixSolver_SetKSPType( coarseSolver, PETScMatrixSolver_KSPType_PreOnly ); if( nProcs == 1 ) { PETScMatrixSolver_SetPCType( coarseSolver, PETScMatrixSolver_PCType_LU ); } else { PETScMatrixSolver_SetPCType( coarseSolver, PETScMatrixSolver_PCType_RedundantLU ); } MatrixSolver_SetMatrix( coarseSolver, matrix ); */ if( nProcs == 1 ){ KSPCreate( MPI_COMM_WORLD, &courseSolver->ksp ); KSPSetType( courseSolver->ksp, KSPPREONLY ); KSPGetPC( courseSolver->ksp, &pc ); PCSetType( pc, PCLU ); } else { PCSetType( pc, PCREDUNDANT ); #if ((PETSC_VERSION_MAJOR>=3) && (PETSC_VERSION_MINOR>=2) ) PCCreate( MPI_COMM_WORLD, &pc); PCRedundantGetKSP( pc, &courseSolver->ksp ); #else KSPCreate( MPI_COMM_WORLD, &courseSolver->ksp ); KSPSetType( courseSolver->ksp, KSPPREONLY ); KSPGetPC( courseSolver->ksp, &pc ); PCRedundantGetPC( pc, &pc ); #endif PCSetType( pc, PCLU ); } if( courseSolver->matrix != PETSC_NULL ){ Stg_MatDestroy(&courseSolver->matrix );} courseSolver->matrix = matrix; Stg_KSPSetOperators( courseSolver->ksp, matrix, matrix, DIFFERENT_NONZERO_PATTERN ); return courseSolver; }
static PetscErrorCode PCRedundantGetKSP_Redundant(PC pc,KSP *innerksp) { PetscErrorCode ierr; PC_Redundant *red = (PC_Redundant*)pc->data; MPI_Comm comm,subcomm; const char *prefix; PetscFunctionBegin; if (!red->psubcomm) { ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr); ierr = PetscSubcommCreate(comm,&red->psubcomm);CHKERRQ(ierr); ierr = PetscSubcommSetNumber(red->psubcomm,red->nsubcomm);CHKERRQ(ierr); ierr = PetscSubcommSetType(red->psubcomm,PETSC_SUBCOMM_INTERLACED);CHKERRQ(ierr); ierr = PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));CHKERRQ(ierr); /* create a new PC that processors in each subcomm have copy of */ subcomm = PetscSubcommChild(red->psubcomm); ierr = KSPCreate(subcomm,&red->ksp);CHKERRQ(ierr); ierr = KSPSetErrorIfNotConverged(red->ksp,pc->erroriffailure);CHKERRQ(ierr); ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)red->ksp);CHKERRQ(ierr); ierr = KSPSetType(red->ksp,KSPPREONLY);CHKERRQ(ierr); ierr = KSPGetPC(red->ksp,&red->pc);CHKERRQ(ierr); ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr); ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr); ierr = KSPAppendOptionsPrefix(red->ksp,"redundant_");CHKERRQ(ierr); } *innerksp = red->ksp; PetscFunctionReturn(0); }
END_TEST START_TEST( Generate2DLapacian_test ) { PetscErrorCode ierr; PetscInt d = 50, len = d*d; Mat m; PetscViewer view; Generate2DLapacian( d, d, &m); PetscViewerASCIIOpen(PETSC_COMM_SELF, "mat.dat", &view); MatView(m, view); KSP ksp; PC pc; ierr = KSPCreate(PETSC_COMM_SELF, &ksp); CHKERRQ(ierr); KSPSetType(ksp, KSPPREONLY); KSPGetPC(ksp, &pc); PCSetType(pc, PCCHOLESKY); KSPSetOperators(ksp, m, m, SAME_PRECONDITIONER); Vec v, s; VecCreateSeq(PETSC_COMM_SELF,len,&v); VecDuplicate(v, &s); VecSet(v, 1); ierr = KSPSolve(ksp, v, s); CHKERRQ(ierr); }
EXTERN_C_BEGIN #undef __FUNCT__ #define __FUNCT__ "PCRedundantGetPC_Redundant" PetscErrorCode PETSCKSP_DLLEXPORT PCRedundantGetPC_Redundant(PC pc,PC *innerpc) { PetscErrorCode ierr; PC_Redundant *red = (PC_Redundant*)pc->data; MPI_Comm comm,subcomm; const char *prefix; PetscFunctionBegin; if (!red->psubcomm) { ierr = PetscObjectGetComm((PetscObject)pc,&comm);CHKERRQ(ierr); ierr = PetscSubcommCreate(comm,red->nsubcomm,&red->psubcomm);CHKERRQ(ierr); ierr = PetscLogObjectMemory(pc,sizeof(PetscSubcomm));CHKERRQ(ierr); /* create a new PC that processors in each subcomm have copy of */ subcomm = red->psubcomm->comm; ierr = KSPCreate(subcomm,&red->ksp);CHKERRQ(ierr); ierr = PetscObjectIncrementTabLevel((PetscObject)red->ksp,(PetscObject)pc,1);CHKERRQ(ierr); ierr = PetscLogObjectParent(pc,red->ksp);CHKERRQ(ierr); ierr = KSPSetType(red->ksp,KSPPREONLY);CHKERRQ(ierr); ierr = KSPGetPC(red->ksp,&red->pc);CHKERRQ(ierr); ierr = PCSetType(red->pc,PCLU);CHKERRQ(ierr); ierr = PCGetOptionsPrefix(pc,&prefix);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(red->ksp,prefix);CHKERRQ(ierr); ierr = KSPAppendOptionsPrefix(red->ksp,"redundant_");CHKERRQ(ierr); } ierr = KSPGetPC(red->ksp,innerpc);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode StokesSetupPC(Stokes *s, KSP ksp) { KSP *subksp; PC pc, pc1; PetscInt n = 1; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); ierr = PCFieldSplitSetIS(pc, "0", s->isg[0]);CHKERRQ(ierr); ierr = PCFieldSplitSetIS(pc, "1", s->isg[1]);CHKERRQ(ierr); if (s->userPC) { ierr = PCFieldSplitSetSchurPre(pc, PC_FIELDSPLIT_SCHUR_PRE_USER, s->myS);CHKERRQ(ierr); } if (s->userKSP) { ierr = PCSetUp(pc);CHKERRQ(ierr); ierr = PCFieldSplitGetSubKSP(pc, &n, &subksp);CHKERRQ(ierr); ierr = KSPSetOperators(subksp[1], s->myS, s->myS);CHKERRQ(ierr); ierr = KSPGetPC(subksp[1], &pc1);CHKERRQ(ierr); ierr = PCSetUp(pc1);CHKERRQ(ierr); ierr = PCSetType(pc1,PCGAMG);CHKERRQ(ierr); ierr = PetscFree(subksp);CHKERRQ(ierr); } PetscFunctionReturn(0); }
void MGinit( const MgSmootherType &mg_smoother_type, const unsigned &levelMax ){ KSPCreate(PETSC_COMM_WORLD,&_ksp); KSPGetPC(_ksp,&_pc); PCSetType(_pc,PCMG); PCMGSetLevels(_pc,levelMax,NULL); if( mg_smoother_type == FULL ){ PCMGSetType(_pc, PC_MG_FULL); } else if( mg_smoother_type == MULTIPLICATIVE ){ PCMGSetType(_pc, PC_MG_MULTIPLICATIVE); } else if( mg_smoother_type == ADDITIVE ){ PCMGSetType(_pc, PC_MG_ADDITIVE); } else if( mg_smoother_type == KASKADE ){ PCMGSetType(_pc, PC_MG_KASKADE); } else{ std::cout <<"Wrong mg_type for PETSCsolve()"<<std::endl; abort(); } };
MGSolver_PETScData* MultigridSolver_CreateSmoother( MultigridSolver* self, Mat matrix ) { //MatrixSolver* smoother; MGSolver_PETScData* smoother = malloc( sizeof( MGSolver_PETScData ) ); //unsigned nBlocks; //KSP* ksps; PC pc; //PetscErrorCode ec; /* smoother = (MatrixSolver*)PETScMatrixSolver_New( "" ); PETScMatrixSolver_SetKSPType( smoother, PETScMatrixSolver_KSPType_Richardson ); PETScMatrixSolver_SetPCType( smoother, PETScMatrixSolver_PCType_SOR ); MatrixSolver_SetMatrix( smoother, matrix ); */ KSPCreate( MPI_COMM_WORLD, &smoother->ksp ); KSPSetType( smoother->ksp, KSPRICHARDSON ); KSPGetPC( smoother->ksp, &pc ); PCSetType( pc, PCSOR ); if( smoother->matrix != PETSC_NULL ) Stg_MatDestroy(&smoother->matrix ); smoother->matrix = matrix; Stg_KSPSetOperators( smoother->ksp, matrix, matrix, DIFFERENT_NONZERO_PATTERN ); return smoother; }
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; }
int main(int argc,char **args) { PC pc; PetscErrorCode ierr; PetscInt n = 5; Mat mat; ierr = PetscInitialize(&argc,&args,(char*)0,help); if (ierr) return ierr; ierr = PCCreate(PETSC_COMM_WORLD,&pc); CHKERRQ(ierr); ierr = PCSetType(pc,PCNONE); CHKERRQ(ierr); /* Vector and matrix must be set before calling PCSetUp */ ierr = MatCreateSeqAIJ(PETSC_COMM_SELF,n,n,3,NULL,&mat); CHKERRQ(ierr); ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = PCSetOperators(pc,mat,mat); CHKERRQ(ierr); ierr = PCSetUp(pc); CHKERRQ(ierr); ierr = MatDestroy(&mat); CHKERRQ(ierr); ierr = PCDestroy(&pc); CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
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); }
void PETSc::Solve_LSQR(void) { //start_clock("Before Assemble matrix and vector"); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); ierr = VecAssemblyBegin(x); ierr = VecAssemblyEnd(x); ierr = VecAssemblyBegin(b); ierr = VecAssemblyEnd(b); //stop_clock("After Assembly matrix and vector"); KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN); KSPSetType(ksp,KSPLSQR); KSPGetPC(ksp, &pc); PCSetType(pc, PCNONE); KSPSetFromOptions(ksp); KSPSetUp(ksp); //start_clock("Before KSPSolve"); KSPSolve(ksp,b,x); // stop_clock("After KSPSolve"); }
void createOuterPC(OuterContext* ctx) { PCCreate(((ctx->data)->commAll), &(ctx->outerPC)); PCSetType(ctx->outerPC, PCSHELL); PCShellSetName(ctx->outerPC, "RSD"); PCShellSetContext(ctx->outerPC, ctx); PCShellSetApply(ctx->outerPC, &outerPCapply); }
/*! \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); if( !prefix.empty() ) { KSPSetOptionsPrefix(lsolver, prefix.c_str()); PCSetOptionsPrefix(prec, prefix.c_str()); } KSPSetFromOptions(lsolver); }
/*@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); }
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); } }
PetscErrorCode DMCoarsen_AKKT_GAMG11(DM dm, Mat P0f0c, Mat *P1f1c_out) { PetscErrorCode ierr; DM_AKKT* kkt = (DM_AKKT*)(dm->data); Mat Aff = kkt->Aff; /* fine-level KKT matrix */ Mat A1f0f; /* fine-level dual (constraint) Jacobian */ Mat A1f0c; /* = A1f0f*P0f0c coarsen only primal indices */ Mat B1f1f; /* = A1f0c'*A1f0c */ PC gamg11;/* Use PCGAMG internally to get access to some of its methods to operate on B1f1f = A1f0c*A1f0c', where A1f0c = A1f0f*P0f0c. */ PC_GAMG* pc_gamg11; Mat G1f1f; /* = Graph(B1f1f) */ Mat P1f1c; /* = Prolongator(G1f1f); */ PetscCoarsenData *coarsening; PetscFunctionBegin; /* What is faster: - A0c1f = P0f0c'*A0f1f followed by B1f1f = A0c1f'*A0c1f, or - A1f0c = A1f0f*P0f0c followed by B1f1f = A1f0c*A1f0c'? My bet is on the latter: - fewer transpositions inside MatMatMult and row indices are always local. */ ierr = MatGetSubMatrix(Aff, kkt->isf[1], kkt->isf[0], MAT_INITIAL_MATRIX, &A1f0f); CHKERRQ(ierr); if(kkt->transposeP) { ierr = MatMatTransposeMult(A1f0f,P0f0c,MAT_INITIAL_MATRIX, PETSC_DEFAULT, &A1f0c); CHKERRQ(ierr); } ierr = MatMatMult(A1f0f,P0f0c,MAT_INITIAL_MATRIX, PETSC_DEFAULT, &A1f0c); CHKERRQ(ierr); ierr = MatMatTransposeMult(A1f0c, A1f0c, MAT_INITIAL_MATRIX, PETSC_DEFAULT, &B1f1f); CHKERRQ(ierr); /* We create PCGAMG here since it is only needed for coarsening and we don't want to have to carry the attendant data structures, if we don't need them. */ ierr = PCCreate(((PetscObject)dm)->comm, &gamg11); CHKERRQ(ierr); /* This must be an aggregating GAMG. */ ierr = PCSetType(gamg11, PCGAMG); CHKERRQ(ierr); ierr = PCGAMGSetSquareGraph(gamg11, PETSC_FALSE); CHKERRQ(ierr); /* Observe that we want to "square" A1f0c before passing it (B1f1f) to GAMG. This is not because we are not sure how GAMG will deal with a (potentially) non-square matrix, but rather because if we asked GAMG to square it, it would also smooth the resulting prolongator. At least PC_GAMG_AGG would, and we need an unsmoothed prolongator. */ ierr = PCSetOperators(gamg11, B1f1f, B1f1f, DIFFERENT_NONZERO_PATTERN); CHKERRQ(ierr); /* FIX: Currently there is no way to tell GAMG to coarsen onto a give comm, but it shouldn't be hard to hack that stuff in. */ pc_gamg11 = (PC_GAMG*)(gamg11->data); ierr = pc_gamg11->graph(gamg11, B1f1f, &G1f1f); CHKERRQ(ierr); ierr = pc_gamg11->coarsen(gamg11, &G1f1f, &coarsening); CHKERRQ(ierr); ierr = pc_gamg11->prolongator(gamg11, B1f1f, G1f1f, coarsening, &P1f1c); CHKERRQ(ierr); ierr = MatDestroy(&A1f0f); CHKERRQ(ierr); ierr = MatDestroy(&A1f0c); CHKERRQ(ierr); ierr = MatDestroy(&B1f1f); CHKERRQ(ierr); ierr = MatDestroy(&G1f1f); CHKERRQ(ierr); ierr = PCDestroy(&gamg11); CHKERRQ(ierr); *P1f1c_out = P1f1c; PetscFunctionReturn(0); }
PetscErrorCode RunTest(void) { PetscInt N = 100; PetscBool draw = PETSC_FALSE; PetscReal rnorm; Mat A; Vec b,x,r; KSP ksp; PC pc; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscOptionsGetInt(0,"-N",&N,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetBool(0,"-draw",&draw,NULL);CHKERRQ(ierr); ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,N,N);CHKERRQ(ierr); ierr = MatSetType(A,MATPYTHON);CHKERRQ(ierr); ierr = MatPythonSetType(A,"example1.py:Laplace1D");CHKERRQ(ierr); ierr = MatSetUp(A);CHKERRQ(ierr); ierr = MatGetVecs(A,&x,&b);CHKERRQ(ierr); ierr = VecSet(b,1);CHKERRQ(ierr); ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = KSPSetType(ksp,KSPPYTHON);CHKERRQ(ierr); ierr = KSPPythonSetType(ksp,"example1.py:ConjGrad");CHKERRQ(ierr); ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCPYTHON);CHKERRQ(ierr); ierr = PCPythonSetType(pc,"example1.py:Jacobi");CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); ierr = VecDuplicate(b,&r);CHKERRQ(ierr); ierr = MatMult(A,x,r);CHKERRQ(ierr); ierr = VecAYPX(r,-1,b);CHKERRQ(ierr); ierr = VecNorm(r,NORM_2,&rnorm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"error norm = %g\n",rnorm);CHKERRQ(ierr); if (draw) { ierr = VecView(x,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); ierr = PetscSleep(2);CHKERRQ(ierr); } ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = VecDestroy(&r);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = KSPDestroy(&ksp);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **args) { Mat mat; Vec b,u; PC pc; PetscErrorCode ierr; PetscInt n = 5,i,col[3]; PetscScalar value[3]; PetscInitialize(&argc,&args,(char *)0,help); /* Create vectors */ ierr = VecCreateSeq(PETSC_COMM_SELF,n,&b);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,n,&u);CHKERRQ(ierr); /* Create and assemble matrix */ ierr = MatCreateSeqDense(PETSC_COMM_SELF,n,n,PETSC_NULL,&mat);CHKERRQ(ierr); value[0] = -1.0; value[1] = 2.0; value[2] = -1.0; for (i=1; i<n-1; i++) { col[0] = i-1; col[1] = i; col[2] = i+1; ierr = MatSetValues(mat,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr); } i = n - 1; col[0] = n - 2; col[1] = n - 1; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); i = 0; col[0] = 0; col[1] = 1; value[0] = 2.0; value[1] = -1.0; ierr = MatSetValues(mat,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr); ierr = MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); ierr = MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); /* Create PC context and set up data structures */ ierr = PCCreate(PETSC_COMM_WORLD,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCSOR);CHKERRQ(ierr); ierr = PCSetFromOptions(pc);CHKERRQ(ierr); ierr = PCSetOperators(pc,mat,mat,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = PCSetUp(pc);CHKERRQ(ierr); value[0] = 1.0; for (i=0; i<n; i++) { ierr = VecSet(u,0.0);CHKERRQ(ierr); ierr = VecSetValues(u,1,&i,value,INSERT_VALUES);CHKERRQ(ierr); ierr = VecAssemblyBegin(u);CHKERRQ(ierr); ierr = VecAssemblyEnd(u);CHKERRQ(ierr); ierr = PCApply(pc,u,b);CHKERRQ(ierr); ierr = VecView(b,PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr); } /* Free data structures */ ierr = MatDestroy(&mat);CHKERRQ(ierr); ierr = PCDestroy(&pc);CHKERRQ(ierr); ierr = VecDestroy(&u);CHKERRQ(ierr); ierr = VecDestroy(&b);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
/* This function returns the suitable solver for the velocity linear system */ KSP Solver_get_velocity_solver(Mat lhs, Parameters *params) { KSP solver; PC pc; double rtol; int ierr; rtol = params->resmax; ierr = KSPCreate(PETSC_COMM_WORLD, &solver); PETScErrAct(ierr); ierr = KSPSetOperators(solver, lhs, lhs, SAME_NONZERO_PATTERN); PETScErrAct(ierr); //ierr = KSPSetType(solver, KSPGMRES); PETScErrAct(ierr); ierr = KSPSetType(solver, KSPIBCGS); PETScErrAct(ierr); ierr = KSPSetTolerances(solver, rtol, PETSC_DEFAULT, PETSC_DEFAULT, PETSC_DEFAULT); PETScErrAct(ierr); ierr = KSPGetPC(solver, &pc); PETScErrAct(ierr); ierr = PCSetType(pc, PCNONE); PETScErrAct(ierr); //ierr = PCSetType(pc, PCJACOBI); PETScErrAct(ierr); //ierr = PCSetType(pc, PCNONE); PETScErrAct(ierr); //ierr = PCSetType(pc, PCASM); PETScErrAct(ierr); //ierr = PCSetType(pc, PCBJACOBI); PETScErrAct(ierr); //ierr = PCSetType(pc, PCBJACOBI); PETScErrAct(ierr); //ierr = PCSetType(pc, PCLU); PETScErrAct(ierr); //ierr = PCSetType(pc, PCEISENSTAT); PETScErrAct(ierr); //ierr = PCSetType(pc, PCSOR); PETScErrAct(ierr); //ierr = PCSetType(pc, PCJACOBI); PETScErrAct(ierr); //ierr = PCSetType(pc, PCNONE); PETScErrAct(ierr); /* ierr = KSPGetPC(solver, &pc); PETScErrAct(ierr); ierr = PCSetType(pc, PCILU); PETScErrAct(ierr); ierr = PCFactorSetLevels(pc, 3); PETScErrAct(ierr); //ierr = PCFactorSetUseDropTolerance(pc, 1e-3, .1, 50); PETScErrAct(ierr); //ierr = PCFactorSetFill(pc, 30.7648); PETScErrAct(ierr); ierr = PCFactorSetReuseOrdering(pc, PETSC_TRUE); PETScErrAct(ierr); ierr = PCFactorSetReuseFill(pc, PETSC_TRUE); PETScErrAct(ierr); ierr = PCFactorSetAllowDiagonalFill(pc); PETScErrAct(ierr); //ierr = PCFactorSetUseInPlace(pc); PETScErrAct(ierr); */ ierr = KSPSetInitialGuessNonzero(solver, PETSC_TRUE); PETScErrAct(ierr); ierr = KSPSetFromOptions(solver); PETScErrAct(ierr); return solver; }
/*@ PCSetFromOptions - Sets PC options from the options database. This routine must be called before PCSetUp() if the user is to be allowed to set the preconditioner method. Collective on PC Input Parameter: . pc - the preconditioner context Options Database: . -pc_use_amat true,false see PCSetUseAmat() Level: developer .keywords: PC, set, from, options, database .seealso: PCSetUseAmat() @*/ PetscErrorCode PCSetFromOptions(PC pc) { PetscErrorCode ierr; char type[256]; const char *def; PetscBool flg; PetscFunctionBegin; PetscValidHeaderSpecific(pc,PC_CLASSID,1); ierr = PCRegisterAll();CHKERRQ(ierr); ierr = PetscObjectOptionsBegin((PetscObject)pc);CHKERRQ(ierr); if (!((PetscObject)pc)->type_name) { ierr = PCGetDefaultType_Private(pc,&def);CHKERRQ(ierr); } else { def = ((PetscObject)pc)->type_name; } ierr = PetscOptionsFList("-pc_type","Preconditioner","PCSetType",PCList,def,type,256,&flg);CHKERRQ(ierr); if (flg) { ierr = PCSetType(pc,type);CHKERRQ(ierr); } else if (!((PetscObject)pc)->type_name) { ierr = PCSetType(pc,def);CHKERRQ(ierr); } ierr = PetscObjectTypeCompare((PetscObject)pc,PCNONE,&flg);CHKERRQ(ierr); if (flg) goto skipoptions; ierr = PetscOptionsBool("-pc_use_amat","use Amat (instead of Pmat) to define preconditioner in nested inner solves","PCSetUseAmat",pc->useAmat,&pc->useAmat,NULL);CHKERRQ(ierr); if (pc->ops->setfromoptions) { ierr = (*pc->ops->setfromoptions)(PetscOptionsObject,pc);CHKERRQ(ierr); } skipoptions: /* process any options handlers added with PetscObjectAddOptionsHandler() */ ierr = PetscObjectProcessOptionsHandlers((PetscObject)pc);CHKERRQ(ierr); ierr = PetscOptionsEnd();CHKERRQ(ierr); pc->setfromoptionscalled++; PetscFunctionReturn(0); }
int main(int argc, char **args) { PetscErrorCode ierr; ierr = PetscInitialize(&argc, &args, (char *) 0, ""); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "Start %s\n", __FILE__); CHKERRQ(ierr); MPI_Comm comm = PETSC_COMM_WORLD; DA da; Mat mat; int MX = 100; int dof = 3; ierr = PetscPrintf(comm,"Started Assembly\n"); CHKERRQ(ierr); ierr = MatMake(MX,dof,&da, &mat); CHKERRQ(ierr); ierr = MatWrite("J",mat); CHKERRQ(ierr); ierr = PetscPrintf(comm,"Finished Assembly\n"); CHKERRQ(ierr); Vec rhs, sol; ierr = DACreateGlobalVector(da,&rhs); CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&sol); CHKERRQ(ierr); int i,j; int xs,ys,xm,ym; Field **array; ierr = DAVecGetArray(da,rhs,&array); CHKERRQ(ierr); ierr = DAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL); CHKERRQ(ierr); for (j = xs; j < xm; ++j) { for (i = ys; i < ym; ++i) { if( 3*MX/4 > i && i > MX/4 && 3*MX/4 > j && j > MX/4 ) { array[j][i].u = 100.; array[j][i].v = 100.; } } } ierr = DAVecRestoreArray(da,rhs,&array); CHKERRQ(ierr); KSP ksp; ierr = KSPCreate(comm,&ksp); CHKERRQ(ierr); ierr = KSPSetType(ksp,KSPPREONLY); CHKERRQ(ierr); ierr = KSPSetOperators(ksp,mat,mat,DIFFERENT_NONZERO_PATTERN); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); //Split pressure from velocity PC pc; ierr = KSPGetPC(ksp,&pc); CHKERRQ(ierr); ierr = PCSetType(pc, PCFIELDSPLIT); CHKERRQ(ierr); ierr = PCFieldSplitSetType(pc,PC_COMPOSITE_SCHUR); CHKERRQ(ierr); ierr = PCFieldSplitSetBlockSize(pc,3); CHKERRQ(ierr); ierr = PCFieldSplitSetFields(pc,2,(int[]){0,1}); CHKERRQ(ierr);
PETSC_EXTERN PetscErrorCode TSCreate_Sundials(TS ts) { TS_Sundials *cvode; PetscErrorCode ierr; PC pc; PetscFunctionBegin; ts->ops->reset = TSReset_Sundials; ts->ops->destroy = TSDestroy_Sundials; ts->ops->view = TSView_Sundials; ts->ops->setup = TSSetUp_Sundials; ts->ops->step = TSStep_Sundials; ts->ops->interpolate = TSInterpolate_Sundials; ts->ops->setfromoptions = TSSetFromOptions_Sundials; ierr = PetscNewLog(ts,TS_Sundials,&cvode);CHKERRQ(ierr); ts->data = (void*)cvode; cvode->cvode_type = SUNDIALS_BDF; cvode->gtype = SUNDIALS_CLASSICAL_GS; cvode->maxl = 5; cvode->linear_tol = .05; cvode->monitorstep = PETSC_TRUE; ierr = MPI_Comm_dup(PetscObjectComm((PetscObject)ts),&(cvode->comm_sundials));CHKERRQ(ierr); cvode->mindt = -1.; cvode->maxdt = -1.; /* set tolerance for Sundials */ cvode->reltol = 1e-6; cvode->abstol = 1e-6; /* set PCNONE as default pctype */ ierr = TSSundialsGetPC_Sundials(ts,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCNONE);CHKERRQ(ierr); if (ts->exact_final_time != TS_EXACTFINALTIME_STEPOVER) ts->exact_final_time = TS_EXACTFINALTIME_STEPOVER; ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetType_C",TSSundialsSetType_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxl_C",TSSundialsSetMaxl_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetLinearTolerance_C",TSSundialsSetLinearTolerance_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetGramSchmidtType_C",TSSundialsSetGramSchmidtType_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetTolerance_C",TSSundialsSetTolerance_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMinTimeStep_C",TSSundialsSetMinTimeStep_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsSetMaxTimeStep_C",TSSundialsSetMaxTimeStep_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetPC_C",TSSundialsGetPC_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsGetIterations_C",TSSundialsGetIterations_Sundials);CHKERRQ(ierr); ierr = PetscObjectComposeFunction((PetscObject)ts,"TSSundialsMonitorInternalSteps_C",TSSundialsMonitorInternalSteps_Sundials);CHKERRQ(ierr); PetscFunctionReturn(0); }
void PetscPreconditioner<T>::set_petsc_subpreconditioner_type(const PCType type, PC& pc) #endif { // For catching PETSc error return codes int ierr = 0; // get the communicator from the PETSc object Parallel::communicator comm; PetscObjectGetComm((PetscObject)pc, &comm); Parallel::Communicator communicator(comm); // All docs say must call KSPSetUp or PCSetUp before calling PCBJacobiGetSubKSP. // You must call PCSetUp after the preconditioner operators have been set, otherwise you get the: // // "Object is in wrong state!" // "Matrix must be set first." // // error messages... ierr = PCSetUp(pc); CHKERRABORT(comm,ierr); // To store array of local KSP contexts on this processor KSP* subksps; // the number of blocks on this processor PetscInt n_local; // The global number of the first block on this processor. // This is not used, so we just pass PETSC_NULL instead. // int first_local; // Fill array of local KSP contexts ierr = PCBJacobiGetSubKSP(pc, &n_local, PETSC_NULL, &subksps); CHKERRABORT(comm,ierr); // Loop over sub-ksp objects, set ILU preconditioner for (PetscInt i=0; i<n_local; ++i) { // Get pointer to sub KSP object's PC PC subpc; ierr = KSPGetPC(subksps[i], &subpc); CHKERRABORT(comm,ierr); // Set requested type on the sub PC ierr = PCSetType(subpc, type); CHKERRABORT(comm,ierr); } }
void Solve_Distance(UserCtx *user, int iter) { SNES snes_distance; KSP ksp; PC pc; Vec r; Mat J; double norm; int bi=0; VecDuplicate(LevelSet, &r); SNESCreate(PETSC_COMM_WORLD,&snes_distance); SNESSetFunction(snes_distance,r,FormFunction_Distance,(void *)&user[bi]); MatCreateSNESMF(snes_distance, &J); SNESSetJacobian(snes_distance,J,J,MatMFFDComputeJacobian,(void *)&user[bi]); SNESSetType(snes_distance, SNESTR); //SNESTR,SNESLS double tol=1.e-2; SNESSetMaxLinearSolveFailures(snes_distance,10000); SNESSetMaxNonlinearStepFailures(snes_distance,10000); SNESKSPSetUseEW(snes_distance, PETSC_TRUE); SNESKSPSetParametersEW(snes_distance,3,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); SNESSetTolerances(snes_distance,PETSC_DEFAULT,tol,PETSC_DEFAULT,5,50000); // snes iter SNESGetKSP(snes_distance, &ksp); KSPSetType(ksp, KSPGMRES); //KSPGMRESSetPreAllocateVectors(ksp); KSPGetPC(ksp,&pc); PCSetType(pc,PCNONE); //int maxits=10; int maxits=4; // ksp iter double rtol=tol, atol=PETSC_DEFAULT, dtol=PETSC_DEFAULT; KSPSetTolerances(ksp,rtol,atol,dtol,maxits); extern PetscErrorCode MySNESMonitor(SNES snes,PetscInt n,PetscReal rnorm,void *dummy); SNESMonitorSet(snes_distance,MySNESMonitor,PETSC_NULL,PETSC_NULL); SNESSolve(snes_distance, PETSC_NULL, LevelSet); SNESGetFunctionNorm(snes_distance, &norm); //PetscPrintf(PETSC_COMM_WORLD, "\nDistance SNES residual norm=%.5e\n\n", norm); VecDestroy(r); MatDestroy(J); SNESDestroy(snes_distance); };
void HeatSolverBTCS::pup(PUP::er &p) { Solver::pup(p); p|rhs; p|temp; if (p.isUnpacking()) { //unpack A two_d_heat_BTCS(A, ny, dy, nx, dx); KSPCreate(PETSC_COMM_WORLD, &ksp); KSPSetOperators(ksp, A, A); KSPGetPC(ksp, &pc); PCSetType(pc, PCLU); KSPSetFromOptions(ksp); } else { //pack A } }
virtual PetscErrorCode setup_KSP() override { PetscErrorCode ierr; PC pc; PetscFunctionBegin; ierr = KSPCreate(PETSC_COMM_WORLD, &ksp);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(ksp, "NtoD_");CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A);CHKERRQ(ierr); ierr = KSPGetPC(ksp, &pc);CHKERRQ(ierr); ierr = PCSetType(pc, PCNONE);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@ PCMGGetSmootherUp - Gets the KSP context to be used as smoother after coarse grid correction (post-smoother). Not Collective, KSP returned is parallel if PC is Input Parameters: + pc - the multigrid context - l - the level (0 is coarsest) to supply Ouput Parameters: . ksp - the smoother Level: advanced Notes: calling this will result in a different pre and post smoother so you may need to set options on the pre smoother also .keywords: MG, multigrid, get, smoother, up, post-smoother, level .seealso: PCMGGetSmootherUp(), PCMGGetSmootherDown() @*/ PetscErrorCode PCMGGetSmootherUp(PC pc,PetscInt l,KSP *ksp) { PC_MG *mg = (PC_MG*)pc->data; PC_MG_Levels **mglevels = mg->levels; PetscErrorCode ierr; const char *prefix; MPI_Comm comm; PetscFunctionBegin; PetscValidHeaderSpecific(pc,PC_CLASSID,1); /* This is called only if user wants a different pre-smoother from post. Thus we check if a different one has already been allocated, if not we allocate it. */ if (!l) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"There is no such thing as a up smoother on the coarse grid"); if (mglevels[l]->smoothu == mglevels[l]->smoothd) { KSPType ksptype; PCType pctype; PC ipc; PetscReal rtol,abstol,dtol; PetscInt maxits; KSPNormType normtype; ierr = PetscObjectGetComm((PetscObject)mglevels[l]->smoothd,&comm);CHKERRQ(ierr); ierr = KSPGetOptionsPrefix(mglevels[l]->smoothd,&prefix);CHKERRQ(ierr); ierr = KSPGetTolerances(mglevels[l]->smoothd,&rtol,&abstol,&dtol,&maxits);CHKERRQ(ierr); ierr = KSPGetType(mglevels[l]->smoothd,&ksptype);CHKERRQ(ierr); ierr = KSPGetNormType(mglevels[l]->smoothd,&normtype);CHKERRQ(ierr); ierr = KSPGetPC(mglevels[l]->smoothd,&ipc);CHKERRQ(ierr); ierr = PCGetType(ipc,&pctype);CHKERRQ(ierr); ierr = KSPCreate(comm,&mglevels[l]->smoothu);CHKERRQ(ierr); ierr = KSPSetErrorIfNotConverged(mglevels[l]->smoothu,pc->erroriffailure);CHKERRQ(ierr); ierr = PetscObjectIncrementTabLevel((PetscObject)mglevels[l]->smoothu,(PetscObject)pc,mglevels[0]->levels-l);CHKERRQ(ierr); ierr = KSPSetOptionsPrefix(mglevels[l]->smoothu,prefix);CHKERRQ(ierr); ierr = KSPSetTolerances(mglevels[l]->smoothu,rtol,abstol,dtol,maxits);CHKERRQ(ierr); ierr = KSPSetType(mglevels[l]->smoothu,ksptype);CHKERRQ(ierr); ierr = KSPSetNormType(mglevels[l]->smoothu,normtype);CHKERRQ(ierr); ierr = KSPSetConvergenceTest(mglevels[l]->smoothu,KSPConvergedSkip,NULL,NULL);CHKERRQ(ierr); ierr = KSPGetPC(mglevels[l]->smoothu,&ipc);CHKERRQ(ierr); ierr = PCSetType(ipc,pctype);CHKERRQ(ierr); ierr = PetscLogObjectParent((PetscObject)pc,(PetscObject)mglevels[l]->smoothu);CHKERRQ(ierr); ierr = PetscObjectComposedDataSetInt((PetscObject) mglevels[l]->smoothu, PetscMGLevelId, mglevels[l]->level);CHKERRQ(ierr); } if (ksp) *ksp = mglevels[l]->smoothu; PetscFunctionReturn(0); }