Пример #1
0
int main(int argc,char **argv)
{
  PetscErrorCode ierr;
  Vec            v,s;               /* vectors */
  PetscInt       n = 20;
  PetscScalar    one = 1.0;

  PetscInitialize(&argc,&argv,(char*)0,help);
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);

  /*
      Create multi-component vector with 2 components
  */
  ierr = VecCreate(PETSC_COMM_WORLD,&v);CHKERRQ(ierr);
  ierr = VecSetSizes(v,PETSC_DECIDE,n);CHKERRQ(ierr);
  ierr = VecSetBlockSize(v,2);CHKERRQ(ierr);
  ierr = VecSetFromOptions(v);CHKERRQ(ierr);

  /*
      Create single-component vector
  */
  ierr = VecCreate(PETSC_COMM_WORLD,&s);CHKERRQ(ierr);
  ierr = VecSetSizes(s,PETSC_DECIDE,n/2);CHKERRQ(ierr);
  ierr = VecSetFromOptions(s);CHKERRQ(ierr);

  /*
     Set the vectors to entries to a constant value.
  */
  ierr = VecSet(v,one);CHKERRQ(ierr);

  /*
     Get the first component from the multi-component vector to the single vector
  */
  ierr = VecStrideGather(v,0,s,INSERT_VALUES);CHKERRQ(ierr);

  ierr = VecView(s,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);

  /*
     Put the values back into the second component
  */
  ierr = VecStrideScatter(s,1,v,ADD_VALUES);CHKERRQ(ierr);

  ierr = VecView(v,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr);

  /*
     Free work space.  All PETSc objects should be destroyed when they
     are no longer needed.
  */
  ierr = VecDestroy(&v);CHKERRQ(ierr);
  ierr = VecDestroy(&s);CHKERRQ(ierr);
  ierr = PetscFinalize();
  return 0;
}
Пример #2
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);
}