Exemple #1
0
static PetscErrorCode bfgs_apply(PC pc, Vec xin, Vec xout)
{
  TaoLMVMMat *M ;
  TaoVecPetsc Xin(xin);
  TaoVecPetsc Xout(xout);
  TaoTruth info2;
  int info;

  PetscFunctionBegin;

  PetscTruth VerbosePrint = PETSC_FALSE; 
  PetscOptionsGetTruth(PETSC_NULL,"-verboseapp",&VerbosePrint,PETSC_NULL);

  info = PCShellGetContext(pc,(void**)&M); CHKERRQ(info);

  PetscScalar solnNorm,solnDot;
  info = VecNorm(xin,NORM_2,&solnNorm); CHKERRQ(info)
  info=PetscPrintf(PETSC_COMM_WORLD,"bfgs_apply: ||Xin||_2 = %22.15e\n",solnNorm);
  if(VerbosePrint) VecView(xin,0);

  info = M->Solve(&Xin, &Xout, &info2); CHKERRQ(info);

  info = VecNorm(xout,NORM_2,&solnNorm); CHKERRQ(info)
  info = VecDot(xin,xout,&solnDot); CHKERRQ(info)
  info=PetscPrintf(PETSC_COMM_WORLD,"bfgs_apply: ||Xout||_2 = %22.15e, Xin^T Xout= %22.15e\n",solnNorm,solnDot);
  if(VerbosePrint) VecView(xout,0);

  PetscFunctionReturn(0);
}
Exemple #2
0
/*
   Setup for the custom preconditioner

 */
PetscErrorCode MyPCSetUp(PC pc)
{
    AppCtx         *app;
    PetscErrorCode ierr;
    DA             da;

    PetscFunctionBegin;
    ierr = PCShellGetContext(pc,(void**)&app);
    CHKERRQ(ierr);
    /* create the linear solver for the Neutron diffusion */
    ierr = DMMGCreate(app->comm,1,0,&app->fdmmg);
    CHKERRQ(ierr);
    ierr = DMMGSetOptionsPrefix(app->fdmmg,"phi_");
    CHKERRQ(ierr);
    ierr = DMMGSetUser(app->fdmmg,0,app);
    CHKERRQ(ierr);
    ierr = DACreate2d(app->comm,DA_NONPERIODIC,DA_STENCIL_STAR,app->nxv,app->nyvf,PETSC_DETERMINE,1,1,1,0,0,&da);
    CHKERRQ(ierr);
    ierr = DMMGSetDM(app->fdmmg,(DM)da);
    CHKERRQ(ierr);
    ierr = DMMGSetKSP(app->fdmmg,PETSC_NULL,MyFormMatrix);
    CHKERRQ(ierr);
    app->dx = DMMGGetRHS(app->fdmmg);
    app->dy = DMMGGetx(app->fdmmg);
    ierr = VecDuplicate(app->dy,&app->c);
    CHKERRQ(ierr);
    ierr = DADestroy(da);
    CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
Exemple #3
0
static PetscErrorCode ourshellsetupctx(PC pc)
{
  PetscErrorCode ierr = 0;
  void           *ctx;
  ierr = PCShellGetContext(pc,&ctx);CHKERRQ(ierr);
  (*(void (PETSC_STDCALL *)(PC*,void*,PetscErrorCode*))(((PetscObject)pc)->fortran_func_pointers[3]))(&pc,ctx,&ierr);CHKERRQ(ierr);
  return 0;
}
 PetscErrorCode __feel_petsc_preconditioner_view( PC pc, PetscViewer viewer)
 {
     void *ctx;
     PetscErrorCode ierr = PCShellGetContext( pc,&ctx );
     CHKERRQ( ierr );
     Preconditioner<double> * preconditioner = static_cast<Preconditioner<double>*>( ctx );
     preconditioner->view();
     return 0;
 }
Exemple #5
0
PetscErrorCode PCShellApply_Matinv(PC pc,Vec xin,Vec xout)
{
  PetscErrorCode ierr;
  Mat            X;

  PetscFunctionBeginUser;
  ierr = PCShellGetContext(pc,(void**)&X);CHKERRQ(ierr);
  ierr = MatMult(X,xin,xout);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #6
0
PetscErrorCode PCDestroy_Noise(PC pc)
{
  PetscErrorCode ierr;
  PCNoise_Ctx    *ctx;

  PetscFunctionBeginUser;
  ierr = PCShellGetContext(pc,(void**)&ctx);CHKERRQ(ierr);
  ierr = PetscRandomDestroy(&ctx->random);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #7
0
/*
   SampleShellPCApply - This routine demonstrates the use of a
   user-provided preconditioner.

   Input Parameters:
+  pc - preconditioner object
-  x - input vector

   Output Parameter:
.  y - preconditioned vector

   Notes:
   This code implements the Jacobi preconditioner, merely as an
   example of working with a PCSHELL.  Note that the Jacobi method
   is already provided within PETSc.
*/
PetscErrorCode SampleShellPCApply(PC pc,Vec x,Vec y)
{
  SampleShellPC  *shell;
  PetscErrorCode ierr;

  ierr = PCShellGetContext(pc,(void**)&shell);CHKERRQ(ierr);
  ierr = VecPointwiseMult(y,x,shell->diag);CHKERRQ(ierr);

  return 0;
}
 PetscErrorCode __feel_petsc_preconditioner_setup ( PC pc )
 {
     void *ctx;
     PetscErrorCode ierr = PCShellGetContext( pc,&ctx );
     CHKERRQ( ierr );
     Preconditioner<double> * preconditioner = static_cast<Preconditioner<double>*>( ctx );
     preconditioner->init();
     VLOG(2) << "__feel_petsc_preconditioner_setup: init prec\n";
     return 0;
 }
Exemple #9
0
/*
   SampleShellPCDestroy - This routine destroys a user-defined
   preconditioner context.

   Input Parameter:
.  shell - user-defined preconditioner context
*/
PetscErrorCode SampleShellPCDestroy(PC pc)
{
  SampleShellPC  *shell;
  PetscErrorCode ierr;

  ierr = PCShellGetContext(pc,(void**)&shell);CHKERRQ(ierr);
  ierr = VecDestroy(&shell->diag);CHKERRQ(ierr);
  ierr = PetscFree(shell);CHKERRQ(ierr);

  return 0;
}
Exemple #10
0
PetscErrorCode PCSetup_Noise(PC pc)
{
  PetscErrorCode ierr;
  PCNoise_Ctx    *ctx;

  PetscFunctionBeginUser;
  ierr = PCShellGetContext(pc,(void**)&ctx);CHKERRQ(ierr);
  ierr = PetscRandomCreate(PETSC_COMM_WORLD,&ctx->random);CHKERRQ(ierr);
  ierr = PetscRandomSetInterval(ctx->random,-1.0,1.0);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #11
0
static PetscErrorCode MatLMVMSolveShell(PC pc, Vec b, Vec x)
{
    PetscErrorCode ierr;
    Mat M;
    PetscFunctionBegin;
    PetscValidHeaderSpecific(pc,PC_CLASSID,1);
    PetscValidHeaderSpecific(b,VEC_CLASSID,2);
    PetscValidHeaderSpecific(x,VEC_CLASSID,3);
    ierr = PCShellGetContext(pc,(void**)&M);CHKERRQ(ierr);
    ierr = MatLMVMSolve(M, b, x);CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
    PetscErrorCode __feel_petsc_preconditioner_apply( PC pc, Vec x, Vec y )
    {
        void *ctx;
        PetscErrorCode ierr = PCShellGetContext( pc,&ctx );
        CHKERRQ( ierr );
        Preconditioner<double> * preconditioner = static_cast<Preconditioner<double>*>( ctx );
        VectorPetsc<double> x_vec( x );
        VectorPetsc<double> y_vec( y );

        preconditioner->apply( x_vec,y_vec );

        return 0;
    }
PetscErrorCode
IBImplicitStaggeredHierarchyIntegrator::compositeIBPCApply_SAMRAI(PC pc, Vec x, Vec y)
{
    PetscErrorCode ierr;
    void* ctx;
    ierr = PCShellGetContext(pc, &ctx);
    IBTK_CHKERRQ(ierr);
    IBImplicitStaggeredHierarchyIntegrator* ib_integrator =
        static_cast<IBImplicitStaggeredHierarchyIntegrator*>(ctx);
    ierr = ib_integrator->compositeIBPCApply(x, y);
    IBTK_CHKERRQ(ierr);
    return ierr;
} // compositeIBPCApply_SAMRAI
Exemple #14
0
PetscErrorCode MyPCDestroy(PC pc)
{
    AppCtx         *app;
    PetscErrorCode ierr;

    PetscFunctionBegin;
    ierr = PCShellGetContext(pc,(void**)&app);
    CHKERRQ(ierr);
    ierr = DMMGDestroy(app->fdmmg);
    CHKERRQ(ierr);
    ierr = VecDestroy(app->c);
    CHKERRQ(ierr);
    PetscFunctionReturn(0);
}
Exemple #15
0
/*
   SampleShellPCSetUp - This routine sets up a user-defined
   preconditioner context.

   Input Parameters:
.  pc    - preconditioner object
.  pmat  - preconditioner matrix
.  x     - vector

   Output Parameter:
.  shell - fully set up user-defined preconditioner context

   Notes:
   In this example, we define the shell preconditioner to be Jacobi's
   method.  Thus, here we create a work vector for storing the reciprocal
   of the diagonal of the preconditioner matrix; this vector is then
   used within the routine SampleShellPCApply().
*/
PetscErrorCode SampleShellPCSetUp(PC pc,Mat pmat,Vec x)
{
  SampleShellPC  *shell;
  Vec            diag;
  PetscErrorCode ierr;

  ierr = PCShellGetContext(pc,(void**)&shell);CHKERRQ(ierr);
  ierr = VecDuplicate(x,&diag);CHKERRQ(ierr);
  ierr = MatGetDiagonal(pmat,diag);CHKERRQ(ierr);
  ierr = VecReciprocal(diag);CHKERRQ(ierr);

  shell->diag = diag;
  return 0;
}
Exemple #16
0
PetscErrorCode PCBDDCDestroyFETIDPPC(PC pc)
{
  FETIDPPC_ctx   pc_ctx;
  PetscErrorCode ierr;

  PetscFunctionBegin;
  ierr = PCShellGetContext(pc,(void**)&pc_ctx);CHKERRQ(ierr);
  ierr = VecDestroy(&pc_ctx->lambda_local);CHKERRQ(ierr);
  ierr = MatDestroy(&pc_ctx->B_Ddelta);CHKERRQ(ierr);
  ierr = VecScatterDestroy(&pc_ctx->l2g_lambda);CHKERRQ(ierr);
  ierr = MatDestroy(&pc_ctx->S_j);CHKERRQ(ierr);
  ierr = PCDestroy(&pc_ctx->pc);CHKERRQ(ierr); /* decrease PCBDDC reference count */
  ierr = PetscFree(pc_ctx);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #17
0
PetscErrorCode PCApply_Noise(PC pc,Vec xin,Vec xout)
{
  PetscErrorCode ierr;
  PCNoise_Ctx    *ctx;
  PetscReal      nrmin, nrmnoise;

  PetscFunctionBeginUser;
  ierr = PCShellGetContext(pc,(void**)&ctx);CHKERRQ(ierr);

  /* xout is ||xin|| * ctx->eta*  f, where f is a pseudorandom unit vector
    (Note that this should always be combined additively with another PC) */
  ierr = VecSetRandom(xout,ctx->random);CHKERRQ(ierr);
  ierr = VecNorm(xin,NORM_2,&nrmin);CHKERRQ(ierr);
  ierr = VecNorm(xout,NORM_2,&nrmnoise);CHKERRQ(ierr);
  ierr = VecScale(xout,ctx->eta*(nrmin/nrmnoise));CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #18
0
static PetscErrorCode PCBDDCDestroyNullSpaceCorrectionPC(PC pc)
{
  NullSpaceCorrection_ctx pc_ctx;
  PetscErrorCode          ierr;

  PetscFunctionBegin;
  ierr = PCShellGetContext(pc,(void**)&pc_ctx);CHKERRQ(ierr);
  ierr = VecDestroy(&pc_ctx->work_small_1);CHKERRQ(ierr);
  ierr = VecDestroy(&pc_ctx->work_small_2);CHKERRQ(ierr);
  ierr = VecDestroy(&pc_ctx->work_full_1);CHKERRQ(ierr);
  ierr = VecDestroy(&pc_ctx->work_full_2);CHKERRQ(ierr);
  ierr = MatDestroy(&pc_ctx->basis_mat);CHKERRQ(ierr);
  ierr = MatDestroy(&pc_ctx->Lbasis_mat);CHKERRQ(ierr);
  ierr = MatDestroy(&pc_ctx->Kbasis_mat);CHKERRQ(ierr);
  ierr = PCDestroy(&pc_ctx->local_pc);CHKERRQ(ierr);
  ierr = PetscFree(pc_ctx);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #19
0
static PetscErrorCode PCBDDCApplyNullSpaceCorrectionPC(PC pc,Vec x,Vec y)
{
  NullSpaceCorrection_ctx pc_ctx;
  PetscErrorCode          ierr;

  PetscFunctionBegin;
  ierr = PCShellGetContext(pc,(void**)&pc_ctx);CHKERRQ(ierr);
  /* E */
  ierr = MatMultTranspose(pc_ctx->Lbasis_mat,x,pc_ctx->work_small_2);CHKERRQ(ierr);
  ierr = MatMultAdd(pc_ctx->Kbasis_mat,pc_ctx->work_small_2,x,pc_ctx->work_full_1);CHKERRQ(ierr);
  /* P^-1 */
  ierr = PCApply(pc_ctx->local_pc,pc_ctx->work_full_1,pc_ctx->work_full_2);CHKERRQ(ierr);
  /* E^T */
  ierr = MatMultTranspose(pc_ctx->Kbasis_mat,pc_ctx->work_full_2,pc_ctx->work_small_1);CHKERRQ(ierr);
  ierr = VecScale(pc_ctx->work_small_1,-1.0);CHKERRQ(ierr);
  ierr = MatMultAdd(pc_ctx->Lbasis_mat,pc_ctx->work_small_1,pc_ctx->work_full_2,pc_ctx->work_full_1);CHKERRQ(ierr);
  /* Sum contributions */
  ierr = MatMultAdd(pc_ctx->basis_mat,pc_ctx->work_small_2,pc_ctx->work_full_1,y);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #20
0
PetscErrorCode FETIDPPCApplyTranspose(PC fetipc, Vec x, Vec y)
{
  FETIDPPC_ctx   pc_ctx;
  PC_IS          *pcis;
  PetscErrorCode ierr;

  PetscFunctionBegin;
  ierr = PCShellGetContext(fetipc,(void**)&pc_ctx);
  pcis = (PC_IS*)pc_ctx->pc->data;
  /* Application of B_Ddelta^T */
  ierr = VecScatterBegin(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
  ierr = VecScatterEnd(pc_ctx->l2g_lambda,x,pc_ctx->lambda_local,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr);
  ierr = VecSet(pcis->vec2_B,0.0);CHKERRQ(ierr);
  ierr = MatMultTranspose(pc_ctx->B_Ddelta,pc_ctx->lambda_local,pcis->vec2_B);CHKERRQ(ierr);
  /* Application of local Schur complement */
  ierr = MatMultTranspose(pc_ctx->S_j,pcis->vec2_B,pcis->vec1_B);CHKERRQ(ierr);
  /* Application of B_Ddelta */
  ierr = MatMult(pc_ctx->B_Ddelta,pcis->vec1_B,pc_ctx->lambda_local);CHKERRQ(ierr);
  ierr = VecSet(y,0.0);CHKERRQ(ierr);
  ierr = VecScatterBegin(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
  ierr = VecScatterEnd(pc_ctx->l2g_lambda,pc_ctx->lambda_local,y,ADD_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
  PetscFunctionReturn(0);
}
Exemple #21
0
PetscErrorCode ShellApplyML(PC pc,Vec x,Vec y)
{
  PetscErrorCode  ierr;
  ML_Epetra::MultiLevelPreconditioner *mlp = 0;
  void* ctx;

  ierr = PCShellGetContext(pc,&ctx); CHKERRQ(ierr);  
  mlp = (ML_Epetra::MultiLevelPreconditioner*)ctx;
#endif
  /* Wrap x and y as Epetra_Vectors. */
  PetscScalar *xvals,*yvals;
  ierr = VecGetArray(x,&xvals);CHKERRQ(ierr);
  Epetra_Vector epx(View,mlp->OperatorDomainMap(),xvals);
  ierr = VecGetArray(y,&yvals);CHKERRQ(ierr);
  Epetra_Vector epy(View,mlp->OperatorRangeMap(),yvals);

  /* Apply ML. */
  mlp->ApplyInverse(epx,epy);
  
  /* Clean up and return. */
  ierr = VecRestoreArray(x,&xvals);CHKERRQ(ierr);
  ierr = VecRestoreArray(y,&yvals);CHKERRQ(ierr);
  return 0;
} /*ShellApplyML*/
Exemple #22
0
/*
   Here is my custom preconditioner

    Capital vectors: X, X1 are global vectors
    Small vectors: x, x1 are local ghosted vectors
    Prefixed a: ax1, aY1 are arrays that access the vector values (either local (ax1) or global aY1)

 */
PetscErrorCode MyPCApply(PC pc,Vec X,Vec Y)
{
    AppCtx         *app;
    PetscErrorCode ierr;
    Vec            X1,X2,X3,x1,x2,Y1,Y2,Y3;
    DALocalInfo    info1,info2,info3;
    DA             da1,da2,da3;
    PetscInt       i,j;
    FluidField     *ax1,*aY1;
    PetscScalar    **ax2,**aY2;

    PetscFunctionBegin;
    ierr = PCShellGetContext(pc,(void**)&app);
    CHKERRQ(ierr);
    /* obtain information about the three meshes */
    ierr = DMCompositeGetEntries(app->pack,&da1,&da2,&da3);
    CHKERRQ(ierr);
    ierr = DAGetLocalInfo(da1,&info1);
    CHKERRQ(ierr);
    ierr = DAGetLocalInfo(da2,&info2);
    CHKERRQ(ierr);
    ierr = DAGetLocalInfo(da3,&info3);
    CHKERRQ(ierr);

    /* get ghosted version of fluid and thermal conduction, global for phi and C */
    ierr = DMCompositeGetAccess(app->pack,X,&X1,&X2,&X3);
    CHKERRQ(ierr);
    ierr = DMCompositeGetLocalVectors(app->pack,&x1,&x2,PETSC_NULL);
    CHKERRQ(ierr);
    ierr = DAGlobalToLocalBegin(da1,X1,INSERT_VALUES,x1);
    CHKERRQ(ierr);
    ierr = DAGlobalToLocalEnd(da1,X1,INSERT_VALUES,x1);
    CHKERRQ(ierr);
    ierr = DAGlobalToLocalBegin(da2,X2,INSERT_VALUES,x2);
    CHKERRQ(ierr);
    ierr = DAGlobalToLocalEnd(da2,X2,INSERT_VALUES,x2);
    CHKERRQ(ierr);

    /* get global version of result vector */
    ierr = DMCompositeGetAccess(app->pack,Y,&Y1,&Y2,&Y3);
    CHKERRQ(ierr);

    /* pull out the phi and C values */
    ierr = VecStrideGather(X3,0,app->dx,INSERT_VALUES);
    CHKERRQ(ierr);
    ierr = VecStrideGather(X3,1,app->c,INSERT_VALUES);
    CHKERRQ(ierr);

    /* update C via formula 38; put back into return vector */
    ierr = VecAXPY(app->c,0.0,app->dx);
    CHKERRQ(ierr);
    ierr = VecScale(app->c,1.0);
    CHKERRQ(ierr);
    ierr = VecStrideScatter(app->c,1,Y3,INSERT_VALUES);
    CHKERRQ(ierr);

    /* form the right hand side of the phi equation; solve system; put back into return vector */
    ierr = VecAXPBY(app->dx,0.0,1.0,app->c);
    CHKERRQ(ierr);
    ierr = DMMGSolve(app->fdmmg);
    CHKERRQ(ierr);
    ierr = VecStrideScatter(app->dy,0,Y3,INSERT_VALUES);
    CHKERRQ(ierr);

    /* access the ghosted x1 and x2 as arrays */
    ierr = DAVecGetArray(da1,x1,&ax1);
    CHKERRQ(ierr);
    ierr = DAVecGetArray(da2,x2,&ax2);
    CHKERRQ(ierr);

    /* access global y1 and y2 as arrays */
    ierr = DAVecGetArray(da1,Y1,&aY1);
    CHKERRQ(ierr);
    ierr = DAVecGetArray(da2,Y2,&aY2);
    CHKERRQ(ierr);

    for (i=info1.xs; i<info1.xs+info1.xm; i++) {
        aY1[i].prss = ax1[i].prss;
        aY1[i].ergg = ax1[i].ergg;
        aY1[i].ergf = ax1[i].ergf;
        aY1[i].alfg = ax1[i].alfg;
        aY1[i].velg = ax1[i].velg;
        aY1[i].velf = ax1[i].velf;
    }

    for (j=info2.ys; j<info2.ys+info2.ym; j++) {
        for (i=info2.xs; i<info2.xs+info2.xm; i++) {
            aY2[j][i] = ax2[j][i];
        }
    }

    ierr = DAVecRestoreArray(da1,x1,&ax1);
    CHKERRQ(ierr);
    ierr = DAVecRestoreArray(da2,x2,&ax2);
    CHKERRQ(ierr);
    ierr = DAVecRestoreArray(da1,Y1,&aY1);
    CHKERRQ(ierr);
    ierr = DAVecRestoreArray(da2,Y2,&aY2);
    CHKERRQ(ierr);

    ierr = DMCompositeRestoreLocalVectors(app->pack,&x1,&x2,PETSC_NULL);
    CHKERRQ(ierr);
    ierr = DMCompositeRestoreAccess(app->pack,X,&X1,&X2,&X3);
    CHKERRQ(ierr);
    ierr = DMCompositeRestoreAccess(app->pack,Y,&Y1,&Y2,&Y3);
    CHKERRQ(ierr);

    PetscFunctionReturn(0);
}
Exemple #23
0
PETSC_EXTERN void PETSC_STDCALL pcshellgetcontext_(PC *pc,void **ctx,PetscErrorCode *ierr)
{
  *ierr = PCShellGetContext(*pc,ctx);
}