int main(int argc, char **argv) { PetscErrorCode ierr; Vec U,cv,eta; DM da,da2; PetscViewer viewer,view_vtk_cv,view_vtk_eta; char filename[PETSC_MAX_PATH_LEN],cv_filename[PETSC_MAX_PATH_LEN],eta_filename[PETSC_MAX_PATH_LEN]; PetscBool flg,sflg = PETSC_FALSE; PetscInt i,n=10000; PetscInt seed; PetscInitialize(&argc,&argv, (char*)0, help); ierr = PetscOptionsSetValue("-viewer_binary_skip_info","true");CHKERRQ(ierr); ierr = PetscOptionsGetString(PETSC_NULL,"-f",filename,PETSC_MAX_PATH_LEN,&flg);CHKERRQ(ierr); if (!flg) { ierr = PetscOptionsGetInt(PETSC_NULL,"-random_seed",&seed,&sflg);CHKERRQ(ierr); if (!sflg) { ierr = PetscStrcpy(filename,"ex61.data");CHKERRQ(ierr); } else { sprintf(filename,"ex61.data.%d",seed); } } ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); /* Get physics and time parameters */ ierr = DMCreate(PETSC_COMM_WORLD,&da);CHKERRQ(ierr); ierr = DMLoad(da,viewer);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&U);CHKERRQ(ierr); ierr = DMDAGetReducedDA(da,1,&da2);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da2,&cv);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da2,&eta);CHKERRQ(ierr); for (i=0; i<n; i++) { /* when this fails it simply means the file is finished */ ierr = VecLoad(U,viewer);CHKERRQ(ierr); ierr = VecStrideGather(U,1,cv,INSERT_VALUES);CHKERRQ(ierr); ierr = VecStrideGather(U,4,eta,INSERT_VALUES);CHKERRQ(ierr); sprintf(cv_filename,"%s_cv_%d.vtk",filename,i); sprintf(eta_filename,"%s_eta_%d.vtk",filename,i); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,cv_filename,&view_vtk_cv);CHKERRQ(ierr); ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD,eta_filename,&view_vtk_eta);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view_vtk_cv, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr); ierr = PetscViewerSetFormat(view_vtk_eta, PETSC_VIEWER_ASCII_VTK);CHKERRQ(ierr); ierr = DMView(da2,view_vtk_cv);CHKERRQ(ierr); ierr = DMView(da2,view_vtk_eta);CHKERRQ(ierr); ierr = VecView(cv,view_vtk_cv);CHKERRQ(ierr); ierr = VecView(eta,view_vtk_eta);CHKERRQ(ierr); ierr = PetscViewerDestroy(&view_vtk_cv);CHKERRQ(ierr); ierr = PetscViewerDestroy(&view_vtk_eta);CHKERRQ(ierr); } ierr = VecDestroy(&U);CHKERRQ(ierr); ierr = VecDestroy(&cv);CHKERRQ(ierr); ierr = VecDestroy(&eta);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = DMDestroy(&da2);CHKERRQ(ierr); PetscFinalize(); return 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; }
/* 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); }