int main( int argc, char **args) { PetscErrorCode ierr; ierr = PetscInitialize(&argc, &args, (char *) 0, ""); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "Start\n"); CHKERRQ(ierr); DA da; ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR, 64,64,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0, &da); CHKERRQ(ierr); int rank; MPI_Comm_rank(PETSC_COMM_WORLD, &rank); printf("[%d] rank\n", rank); Vec p; ierr = DACreateGlobalVector(da,&p); CHKERRQ(ierr); ierr = VecSet( p, 1.); char file[256] = "vec"; sprintf( file, "vec-%d", rank); PetscViewer view; ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,file,FILE_MODE_WRITE,&view ); CHKERRQ(ierr); ierr = VecView(p, view); CHKERRQ(ierr); ierr = PetscViewerDestroy(view); CHKERRQ(ierr); ierr = VecDestroy( p ); ierr = DADestroy( da ); ierr = PetscPrintf(PETSC_COMM_WORLD, "End\n"); CHKERRQ(ierr); ierr = PetscFinalize(); CHKERRQ(ierr); }
void createMG(LocalData* data) { assert(data != NULL); int nlevels = 1; PetscOptionsGetInt(PETSC_NULL, "-nlevels", &nlevels, PETSC_NULL); int coarseSize = 1 + (((data->N) - 1)>>(nlevels - 1)); int rank; MPI_Comm_rank((data->commAll), &rank); if(!rank) { std::cout<<"nlevels = "<<nlevels<<std::endl; std::cout<<"coarseSize = "<<coarseSize<<std::endl; } DMMGCreate(PETSC_COMM_SELF, -nlevels, PETSC_NULL, &(data->mgObj)); DMMGSetOptionsPrefix(data->mgObj, "loc_"); DA da; DACreate2d(PETSC_COMM_SELF, DA_NONPERIODIC, DA_STENCIL_BOX, coarseSize, coarseSize, PETSC_DECIDE, PETSC_DECIDE, (data->dofsPerNode), 1, PETSC_NULL, PETSC_NULL, &da); DMMGSetDM((data->mgObj), (DM)da); DADestroy(da); DMMGSetKSP((data->mgObj), PETSC_NULL, &computeMGmatrix); PetscObjectIncrementTabLevel((PetscObject)(DMMGGetKSP(data->mgObj)), PETSC_NULL, 2); }
int main(int argc,char **argv) { PetscInt dof = 2,M = 3,N = 3,m = PETSC_DECIDE,n = PETSC_DECIDE; PetscErrorCode ierr; DA da; Vec global,local; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscOptionsGetInt(0,"-dof",&dof,0);CHKERRQ(ierr); /* Create distributed array and get vectors */ ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX, M,N,m,n,dof,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = doit(da,global);CHKERRQ(ierr); ierr = VecView(global,0);CHKERRQ(ierr); /* Free memory */ ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 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); }
int main(int argc,char **argv) { Vec u,xy; DA da; PetscErrorCode ierr; PetscInt m = 10, n = 10, dof = 2; PF pf; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,m,n,PETSC_DECIDE,PETSC_DECIDE,dof,1,0,0,&da);CHKERRQ(ierr); ierr = DASetUniformCoordinates(da,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&u);CHKERRQ(ierr); ierr = DAGetCoordinates(da,&xy);CHKERRQ(ierr); ierr = DACreatePF(da,&pf);CHKERRQ(ierr); ierr = PFSet(pf,myfunction,0,0,0,0);CHKERRQ(ierr); ierr = PFSetFromOptions(pf);CHKERRQ(ierr); ierr = PFApplyVec(pf,xy,u);CHKERRQ(ierr); ierr = VecView(u,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); /* Free work space. All PETSc objects should be destroyed when they are no longer needed. */ ierr = VecDestroy(xy);CHKERRQ(ierr); ierr = PFDestroy(pf);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { DMMG *dmmg; /* multilevel grid structure */ AppCtx user; /* user-defined work context */ PetscInt mx,my,its; PetscErrorCode ierr; MPI_Comm comm; SNES snes; DA da2; PetscInitialize(&argc,&argv,(char *)0,help); comm = PETSC_COMM_WORLD; /* Problem parameters (velocity of lid, prandtl, and grashof numbers) */ ierr = PetscOptionsGetReal(PETSC_NULL,"-lidvelocity",&user.lidvelocity,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetReal(PETSC_NULL,"-prandtl",&user.prandtl,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetReal(PETSC_NULL,"-grashof",&user.grashof,PETSC_NULL);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create user context, set problem data, create vector data structures. Also, compute the initial guess. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Setup Physics 2: - Lap(T) + PR*Div([U*T,V*T]) = 0 where U and V are given by the given x.u and x.v - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da2);CHKERRQ(ierr); ierr = DASetFieldName(da2,0,"temperature");CHKERRQ(ierr); /* Create the solver object and attach the grid/physics info */ ierr = DMMGCreate(comm,1,&user,&dmmg);CHKERRQ(ierr); ierr = DMMGSetDM(dmmg,(DM)da2);CHKERRQ(ierr); ierr = DMMGSetISColoringType(dmmg,IS_COLORING_GLOBAL);CHKERRQ(ierr); ierr = DMMGSetInitialGuess(dmmg,FormInitialGuess);CHKERRQ(ierr); ierr = DMMGSetSNES(dmmg,FormFunction,0);CHKERRQ(ierr); ierr = DMMGSetFromOptions(dmmg);CHKERRQ(ierr); ierr = DAGetInfo(da2,PETSC_NULL,&mx,&my,0,0,0,0,0,0,0,0);CHKERRQ(ierr); user.lidvelocity = 1.0/(mx*my); user.prandtl = 1.0; user.grashof = 1.0; /* Solve the nonlinear system */ ierr = DMMGSolve(dmmg);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); ierr = PetscPrintf(comm,"Physics 2: Number of Newton iterations = %D\n\n", its);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free spaces - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DADestroy(da2);CHKERRQ(ierr); ierr = DMMGDestroy(dmmg);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscViewer viewer; DA da; Vec global,local,global2; PetscMPIInt rank; PetscTruth flg; /* Every PETSc routine should begin with the PetscInitialize() routine. argc, argv - These command line arguments are taken to extract the options supplied to PETSc and options supplied to MPI. help - When PETSc executable is invoked with the option -help, it prints the various options that can be applied at runtime. The user can use the "help" variable place additional help messages in this printout. */ ierr = PetscInitialize(&argc,&argv,(char *)0,help);CHKERRQ(ierr); /* Create a DA and an associated vector */ ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,2,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = VecSet(global,-1.0);CHKERRQ(ierr); ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = VecScale(local,rank+1);CHKERRQ(ierr); ierr = DALocalToGlobal(da,local,ADD_VALUES,global);CHKERRQ(ierr); /* Write output file with PetscViewerHDF5 viewer. */ ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_WRITE,&viewer); CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDuplicate(global,&global2);CHKERRQ(ierr); ierr = VecCopy(global,global2);CHKERRQ(ierr); ierr = PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_READ,&viewer); CHKERRQ(ierr); ierr = VecLoadIntoVector(viewer,global);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecEqual(global,global2,&flg);CHKERRQ(ierr); if (flg) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are equal\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD,"Vectors are not equal\n");CHKERRQ(ierr); } /* clean up and exit */ ierr = DADestroy(da);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = VecDestroy(global2);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
static PetscErrorCode TestQ2Q1DA( void ) { DA Q2_da,Q1_da,cda; PetscInt mx,my,mz; Vec coords,gcoords,gcoords2; PetscErrorCode ierr; mx=7; my=11; mz=13; ierr=DACreate3d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,mx,my,mz,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,3,2,0,0,0,&Q2_da);CHKERRQ(ierr); ierr = DASetUniformCoordinates(Q2_da,-1.0,1.0,-2.0,2.0,-3.0,3.0);CHKERRQ(ierr); ierr = DAGetCoordinates(Q2_da,&coords);CHKERRQ(ierr); ierr = DACreate3d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,mx,my,mz,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,3,1,0,0,0,&Q1_da);CHKERRQ(ierr); ierr = DASetCoordinates(Q1_da,coords);CHKERRQ(ierr); ierr = VecDestroy(coords);CHKERRQ(ierr); /* Get ghost coordinates one way */ ierr = DAGetGhostedCoordinates(Q1_da,&gcoords);CHKERRQ(ierr); /* And another */ ierr = DAGetCoordinates(Q1_da,&coords);CHKERRQ(ierr); ierr = DAGetCoordinateDA(Q1_da,&cda);CHKERRQ(ierr); ierr = DAGetLocalVector(cda,&gcoords2);CHKERRQ(ierr); ierr = DAGlobalToLocalBegin(cda,coords,INSERT_VALUES,gcoords2);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(cda,coords,INSERT_VALUES,gcoords2);CHKERRQ(ierr); ierr = CompareGhostedCoords(gcoords,gcoords2);CHKERRQ(ierr); ierr = DARestoreLocalVector(cda,&gcoords2);CHKERRQ(ierr); ierr = DADestroy(cda);CHKERRQ(ierr); ierr = VecScale(coords,10.0);CHKERRQ(ierr); ierr = VecScale(gcoords,10.0);CHKERRQ(ierr); ierr = DAGetGhostedCoordinates(Q1_da,&gcoords2);CHKERRQ(ierr); ierr = CompareGhostedCoords(gcoords,gcoords2);CHKERRQ(ierr); ierr = VecDestroy(coords);CHKERRQ(ierr); ierr = VecDestroy(gcoords2);CHKERRQ(ierr); ierr = VecDestroy(gcoords);CHKERRQ(ierr); ierr = DADestroy(Q2_da);CHKERRQ(ierr); ierr = DADestroy(Q1_da);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscMPIInt rank; PetscInt m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE,M = 3,N = 5,P=3,s=1; PetscInt *lx = PETSC_NULL,*ly = PETSC_NULL,*lz = PETSC_NULL; PetscErrorCode ierr; PetscTruth flg = PETSC_FALSE; DA da; Vec local,global,vslice; PetscScalar value; DAPeriodicType wrap = DA_XYPERIODIC; DAStencilType stencil_type = DA_STENCIL_BOX; VecScatter scatter; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); /* Read options */ ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-P",&P,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-p",&p,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetTruth(PETSC_NULL,"-star",&flg,PETSC_NULL);CHKERRQ(ierr); if (flg) stencil_type = DA_STENCIL_STAR; /* Create distributed array and get vectors */ ierr = DACreate3d(PETSC_COMM_WORLD,wrap,stencil_type,M,N,P,m,n,p,1,s, lx,ly,lz,&da);CHKERRQ(ierr); ierr = DAView(da,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = GenerateSliceScatter(da,&scatter,&vslice);CHKERRQ(ierr); /* Put the value rank+1 into all locations of vslice and transfer back to global vector */ value = 1.0 + rank; ierr = VecSet(vslice,value);CHKERRQ(ierr); ierr = VecScatterBegin(scatter,vslice,global,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); ierr = VecScatterEnd(scatter,vslice,global,INSERT_VALUES,SCATTER_REVERSE);CHKERRQ(ierr); ierr = VecView(global,PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { SNES snes; Vec x,f; Mat J; DA da; PetscInitialize(&argc,&argv,(char *)0,help); DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,8,1,1,PETSC_NULL,&da); DACreateGlobalVector(da,&x); VecDuplicate(x,&f); DAGetMatrix(da,MATAIJ,&J); SNESCreate(PETSC_COMM_WORLD,&snes); SNESSetFunction(snes,f,ComputeFunction,da); SNESSetJacobian(snes,J,J,ComputeJacobian,da); SNESSetFromOptions(snes); SNESSolve(snes,PETSC_NULL,x); MatDestroy(J); VecDestroy(x); VecDestroy(f); SNESDestroy(snes); DADestroy(da); PetscFinalize(); return 0;}
void InitializeDMMGSolver(State *BHD) { PC pc; DMMG *dmmg; int l; DMMGCreate(PETSC_COMM_WORLD,2,PETSC_NULL,&dmmg); DMMGSetDM(dmmg,(DM)BHD->da_dmmg); DADestroy(BHD->da_dmmg); for (l = 0; l < DMMGGetLevels(dmmg); l++) { DMMGSetUser(dmmg,l,(void*) BHD); } DMMGSetKSP(dmmg,ComputeRHSDMMG,InitializeLaplaceMatrix); BHD->dmmg = dmmg; }
int main(int argc,char **argv) /*-----------------------------------------------------------------------*/ { DMMG *dmmg; /* multilevel grid structure */ AppCtx *user; /* user-defined work context */ Parameter *param; GridInfo grid; int ierr,result; MPI_Comm comm; DA da; PetscInitialize(&argc,&argv,(char *)0,help); comm = PETSC_COMM_WORLD; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set up the problem parameters. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscMalloc(sizeof(AppCtx),&user); CHKERRQ(ierr); ierr = PetscBagCreate(comm,sizeof(Parameter),&(user->bag)); CHKERRQ(ierr); user->grid = &grid; ierr = SetParams(user); CHKERRQ(ierr); ierr = ReportParams(user); CHKERRQ(ierr); ierr = PetscBagGetData(user->bag,(void**)¶m); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create distributed array multigrid object (DMMG) to manage parallel grid and vectors for principal unknowns (x) and governing residuals (f) - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DMMGCreate(comm,grid.mglevels,user,&dmmg); CHKERRQ(ierr); ierr = DACreate2d(comm,grid.periodic,grid.stencil,grid.ni,grid.nj,PETSC_DECIDE,PETSC_DECIDE,grid.dof,grid.stencil_width,0,0,&da); CHKERRQ(ierr); ierr = DMMGSetDM(dmmg,(DM)da); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); ierr = DAGetInfo(da,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL,&(param->pi),&(param->pj),PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr); REG_INTG(user->bag,¶m->pi,param->pi ,"procs_x","<DO NOT SET> Processors in the x-direction"); REG_INTG(user->bag,¶m->pj,param->pj ,"procs_y","<DO NOT SET> Processors in the y-direction"); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create user context, set problem data, create vector data structures. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DAGetGlobalVector(da, &(user->Xold)); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initialize and solve the nonlinear system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = Initialize(dmmg); CHKERRQ(ierr); ierr = DoSolve(dmmg); CHKERRQ(ierr); if (param->verify) result = param->verify_result; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free work space. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DARestoreGlobalVector(da, &(user->Xold)); CHKERRQ(ierr); ierr = PetscBagDestroy(user->bag); CHKERRQ(ierr); ierr = PetscFree(user); CHKERRQ(ierr); ierr = DMMGDestroy(dmmg); CHKERRQ(ierr); ierr = PetscFinalize(); CHKERRQ(ierr); return result; }
int main(int argc,char **argv) { DMMG *dmmg; /* multilevel grid structure */ PetscErrorCode ierr; DA da; AppCtx app; PC pc; KSP ksp; PetscTruth isshell; PetscViewer v1; PetscInitialize(&argc,&argv,(char *)0,help); PreLoadBegin(PETSC_TRUE,"SetUp"); app.comm = PETSC_COMM_WORLD; app.nxv = 6; app.nyvf = 3; app.nyv = app.nyvf + 2; ierr = PetscOptionsBegin(app.comm,PETSC_NULL,"Options for Grid Sizes",PETSC_NULL); ierr = PetscOptionsInt("-nxv","Grid spacing in X direction",PETSC_NULL,app.nxv,&app.nxv,PETSC_NULL); CHKERRQ(ierr); ierr = PetscOptionsInt("-nyvf","Grid spacing in Y direction of Fuel",PETSC_NULL,app.nyvf,&app.nyvf,PETSC_NULL); CHKERRQ(ierr); ierr = PetscOptionsInt("-nyv","Total Grid spacing in Y direction of",PETSC_NULL,app.nyv,&app.nyv,PETSC_NULL); CHKERRQ(ierr); ierr = PetscOptionsEnd(); ierr = PetscViewerDrawOpen(app.comm,PETSC_NULL,"",-1,-1,-1,-1,&v1); CHKERRQ(ierr); /* Create the DMComposite object to manage the three grids/physics. We use a 1d decomposition along the y direction (since one of the grids is 1d). */ ierr = DMCompositeCreate(app.comm,&app.pack); CHKERRQ(ierr); /* 6 fluid unknowns, 3 ghost points on each end for either periodicity or simply boundary conditions */ ierr = DACreate1d(app.comm,DA_XPERIODIC,app.nxv,6,3,0,&da); CHKERRQ(ierr); ierr = DASetFieldName(da,0,"prss"); CHKERRQ(ierr); ierr = DASetFieldName(da,1,"ergg"); CHKERRQ(ierr); ierr = DASetFieldName(da,2,"ergf"); CHKERRQ(ierr); ierr = DASetFieldName(da,3,"alfg"); CHKERRQ(ierr); ierr = DASetFieldName(da,4,"velg"); CHKERRQ(ierr); ierr = DASetFieldName(da,5,"velf"); CHKERRQ(ierr); ierr = DMCompositeAddDM(app.pack,(DM)da); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); ierr = DACreate2d(app.comm,DA_YPERIODIC,DA_STENCIL_STAR,app.nxv,app.nyv,PETSC_DETERMINE,1,1,1,0,0,&da); CHKERRQ(ierr); ierr = DASetFieldName(da,0,"Tempature"); CHKERRQ(ierr); ierr = DMCompositeAddDM(app.pack,(DM)da); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); ierr = DACreate2d(app.comm,DA_XYPERIODIC,DA_STENCIL_STAR,app.nxv,app.nyvf,PETSC_DETERMINE,1,2,1,0,0,&da); CHKERRQ(ierr); ierr = DASetFieldName(da,0,"Phi"); CHKERRQ(ierr); ierr = DASetFieldName(da,1,"Pre"); CHKERRQ(ierr); ierr = DMCompositeAddDM(app.pack,(DM)da); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); app.pri = 1.0135e+5; app.ugi = 2.5065e+6; app.ufi = 4.1894e+5; app.agi = 1.00e-1; app.vgi = 1.0e-1 ; app.vfi = 1.0e-1; app.prin = 1.0135e+5; app.ugin = 2.5065e+6; app.ufin = 4.1894e+5; app.agin = 1.00e-1; app.vgin = 1.0e-1 ; app.vfin = 1.0e-1; app.prout = 1.0135e+5; app.ugout = 2.5065e+6; app.ufout = 4.1894e+5; app.agout = 3.0e-1; app.twi = 373.15e+0; app.phii = 1.0e+0; app.prei = 1.0e-5; /* Create the solver object and attach the grid/physics info */ ierr = DMMGCreate(app.comm,1,0,&dmmg); CHKERRQ(ierr); ierr = DMMGSetDM(dmmg,(DM)app.pack); CHKERRQ(ierr); ierr = DMMGSetUser(dmmg,0,&app); CHKERRQ(ierr); ierr = DMMGSetISColoringType(dmmg,IS_COLORING_GLOBAL); CHKERRQ(ierr); CHKMEMQ; ierr = DMMGSetInitialGuess(dmmg,FormInitialGuess); CHKERRQ(ierr); ierr = DMMGSetSNES(dmmg,FormFunction,0); CHKERRQ(ierr); ierr = DMMGSetFromOptions(dmmg); CHKERRQ(ierr); /* Supply custom shell preconditioner if requested */ ierr = SNESGetKSP(DMMGGetSNES(dmmg),&ksp); CHKERRQ(ierr); ierr = KSPGetPC(ksp,&pc); CHKERRQ(ierr); ierr = PetscTypeCompare((PetscObject)pc,PCSHELL,&isshell); CHKERRQ(ierr); if (isshell) { ierr = PCShellSetContext(pc,&app); CHKERRQ(ierr); ierr = PCShellSetSetUp(pc,MyPCSetUp); CHKERRQ(ierr); ierr = PCShellSetApply(pc,MyPCApply); CHKERRQ(ierr); ierr = PCShellSetDestroy(pc,MyPCDestroy); CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve the nonlinear system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PreLoadStage("Solve"); ierr = DMMGSolve(dmmg); CHKERRQ(ierr); ierr = VecView(DMMGGetx(dmmg),v1); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free work space. All PETSc objects should be destroyed when they are no longer needed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscViewerDestroy(v1); CHKERRQ(ierr); ierr = DMCompositeDestroy(app.pack); CHKERRQ(ierr); ierr = DMMGDestroy(dmmg); CHKERRQ(ierr); PreLoadEnd(); ierr = PetscFinalize(); CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; SNES snes; /* nonlinear solver */ Vec Hu,r; /* solution, residual vectors */ Mat J; /* Jacobian matrix */ AppCtx user; /* user-defined work context */ PetscInt its, i, tmpxs, tmpxm; /* iteration count, index, etc. */ PetscReal tmp1, tmp2, tmp3, tmp4, tmp5, errnorms[2], descaleNode[2]; PetscTruth eps_set = PETSC_FALSE, dump = PETSC_FALSE, exactinitial = PETSC_FALSE, snes_mf_set, snes_fd_set; MatFDColoring matfdcoloring = 0; ISColoring iscoloring; SNESConvergedReason reason; /* Check convergence */ PetscInitialize(&argc,&argv,(char *)0,help); ierr = MPI_Comm_rank(PETSC_COMM_WORLD, &user.rank); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "BODVARDSSON solves for thickness and velocity in 1D, steady ice stream\n" " [run with -help for info and options]\n");CHKERRQ(ierr); user.n = 3.0; /* Glen flow law exponent */ user.secpera = 31556926.0; user.rho = 910.0; /* kg m^-3 */ user.rhow = 1028.0; /* kg m^-3 */ user.g = 9.81; /* m s^-2 */ /* ask Test N for its parameters, but only those we need to solve */ ierr = params_exactN(&(user.H0), &tmp1, &(user.xc), &tmp2, &tmp3, &tmp4, &tmp5, &(user.Txc)); CHKERRQ(ierr); /* regularize using strain rate of 1/xc per year */ user.epsilon = (1.0 / user.secpera) / user.xc; /* tools for non-dimensionalizing to improve equation scaling */ user.scaleNode[0] = 1000.0; user.scaleNode[1] = 100.0 / user.secpera; ierr = PetscOptionsTruth("-snes_mf","","",PETSC_FALSE,&snes_mf_set,NULL);CHKERRQ(ierr); ierr = PetscOptionsTruth("-snes_fd","","",PETSC_FALSE,&snes_fd_set,NULL);CHKERRQ(ierr); if (!snes_mf_set && !snes_fd_set) { PetscPrintf(PETSC_COMM_WORLD, "\n***ERROR: bodvardsson needs one or zero of '-snes_mf', '-snes_fd'***\n\n" "USAGE FOLLOWS ...\n\n%s",help); PetscEnd(); } if (snes_fd_set) { ierr = PetscPrintf(PETSC_COMM_WORLD, " using approximate Jacobian; finite-differencing using coloring\n"); CHKERRQ(ierr); } else if (snes_mf_set) { ierr = PetscPrintf(PETSC_COMM_WORLD, " matrix free; no preconditioner\n"); CHKERRQ(ierr); } else { ierr = PetscPrintf(PETSC_COMM_WORLD, " true Jacobian\n"); CHKERRQ(ierr); } ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL, "bodvardsson program options",__FILE__);CHKERRQ(ierr); { ierr = PetscOptionsTruth("-bod_up_one","","",PETSC_FALSE,&user.upwind1,NULL);CHKERRQ(ierr); ierr = PetscOptionsTruth("-bod_exact_init","","",PETSC_FALSE,&exactinitial,NULL);CHKERRQ(ierr); ierr = PetscOptionsTruth("-bod_dump", "dump out exact and approximate solution and residual, as asci","", dump,&dump,NULL);CHKERRQ(ierr); ierr = PetscOptionsReal("-bod_epsilon","regularization (a strain rate in units of 1/a)","", user.epsilon * user.secpera,&user.epsilon,&eps_set);CHKERRQ(ierr); if (eps_set) user.epsilon *= 1.0 / user.secpera; } ierr = PetscOptionsEnd();CHKERRQ(ierr); /* Create machinery for parallel grid management (DA), nonlinear solver (SNES), and Vecs for fields (solution, RHS). Note default Mx=46 grid points means dx=10 km. Also degrees of freedom = 2 (thickness and velocity at each point) and stencil radius = ghost width = 2 for 2nd-order upwinding. */ user.solnghostwidth = 2; ierr = DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,-46,2,user.solnghostwidth,PETSC_NULL,&user.da); CHKERRQ(ierr); ierr = DASetUniformCoordinates(user.da,0.0,user.xc, PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); ierr = DASetFieldName(user.da,0,"ice thickness [non-dimensional]"); CHKERRQ(ierr); ierr = DASetFieldName(user.da,1,"ice velocity [non-dimensional]"); CHKERRQ(ierr); ierr = DAGetInfo(user.da,PETSC_IGNORE,&user.Mx,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE, PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE,PETSC_IGNORE); ierr = DAGetCorners(user.da,&user.xs,PETSC_NULL,PETSC_NULL,&user.xm,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr); user.dx = user.xc / (PetscReal)(user.Mx-1); /* another DA for scalar parameters, with same length */ ierr = DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,user.Mx,1,1,PETSC_NULL,&user.scalarda);CHKERRQ(ierr); ierr = DASetUniformCoordinates(user.scalarda,0.0,user.xc, PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); /* check that parallel layout of scalar DA is same as dof=2 DA */ ierr = DAGetCorners(user.scalarda,&tmpxs,PETSC_NULL,PETSC_NULL,&tmpxm,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr); if ((tmpxs != user.xs) || (tmpxm != user.xm)) { PetscPrintf(PETSC_COMM_SELF, "\n***ERROR: rank %d gets different ownership range for the two DAs! ENDING ...***\n\n", user.rank); PetscEnd(); } ierr = PetscPrintf(PETSC_COMM_WORLD, " Mx = %D points, dx = %.3f m\n H0 = %.2f m, xc = %.2f km, Txc = %.5e Pa m\n", user.Mx, user.dx, user.H0, user.xc/1000.0, user.Txc);CHKERRQ(ierr); /* Extract/allocate global vectors from DAs and duplicate for remaining same types */ ierr = DACreateGlobalVector(user.da,&Hu);CHKERRQ(ierr); ierr = VecSetBlockSize(Hu,2);CHKERRQ(ierr); ierr = VecDuplicate(Hu,&r);CHKERRQ(ierr); /* inherits block size */ ierr = VecDuplicate(Hu,&user.Huexact);CHKERRQ(ierr); /* ditto */ ierr = DACreateGlobalVector(user.scalarda,&user.M);CHKERRQ(ierr); ierr = VecDuplicate(user.M,&user.Bstag);CHKERRQ(ierr); ierr = VecDuplicate(user.M,&user.beta);CHKERRQ(ierr); ierr = DASetLocalFunction(user.da,(DALocalFunction1)scshell);CHKERRQ(ierr); ierr = DASetLocalJacobian(user.da,(DALocalFunction1)BodJacobianMatrixLocal);CHKERRQ(ierr); ierr = SNESCreate(PETSC_COMM_WORLD,&snes);CHKERRQ(ierr); ierr = SNESSetFunction(snes,r,SNESDAFormFunction,&user);CHKERRQ(ierr); /* setting up a matrix is only actually needed for -snes_fd case */ ierr = DAGetMatrix(user.da,MATAIJ,&J);CHKERRQ(ierr); if (snes_fd_set) { /* tools needed so DA can use sparse matrix for its F.D. Jacobian approx */ ierr = DAGetColoring(user.da,IS_COLORING_GLOBAL,MATAIJ,&iscoloring);CHKERRQ(ierr); ierr = MatFDColoringCreate(J,iscoloring,&matfdcoloring);CHKERRQ(ierr); ierr = ISColoringDestroy(iscoloring);CHKERRQ(ierr); ierr = MatFDColoringSetFunction(matfdcoloring, (PetscErrorCode (*)(void))SNESDAFormFunction,&user);CHKERRQ(ierr); ierr = MatFDColoringSetFromOptions(matfdcoloring);CHKERRQ(ierr); ierr = SNESSetJacobian(snes,J,J,SNESDefaultComputeJacobianColor,matfdcoloring);CHKERRQ(ierr); } else { ierr = SNESSetJacobian(snes,J,J,SNESDAComputeJacobian,&user);CHKERRQ(ierr); } ierr = SNESSetFromOptions(snes);CHKERRQ(ierr); /* the the Bodvardsson (1955) exact solution allows setting M(x), B(x), beta(x), T(xc) */ ierr = FillDistributedParams(&user);CHKERRQ(ierr); /* the exact thickness and exact ice velocity (user.uHexact) are known from Bodvardsson (1955) */ ierr = FillExactSoln(&user); CHKERRQ(ierr); if (exactinitial) { ierr = PetscPrintf(PETSC_COMM_WORLD," using exact solution as initial guess\n"); CHKERRQ(ierr); /* the initial guess is the exact continuum solution */ ierr = VecCopy(user.Huexact,Hu); CHKERRQ(ierr); } else { ierr = FillInitial(&user, &Hu); CHKERRQ(ierr); } /************ SOLVE NONLINEAR SYSTEM ************/ /* recall that RHS r is used internally by KSP, and is set by the SNES */ for (i = 0; i < 2; i++) descaleNode[i] = 1.0 / user.scaleNode[i]; ierr = VecStrideScaleAll(Hu,descaleNode); CHKERRQ(ierr); /* de-dimensionalize initial guess */ ierr = SNESSolve(snes,PETSC_NULL,Hu);CHKERRQ(ierr); ierr = VecStrideScaleAll(Hu,user.scaleNode); CHKERRQ(ierr); /* put back in "real" scale */ ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); ierr = SNESGetConvergedReason(snes,&reason);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, " %s Number of Newton iterations = %D\n", SNESConvergedReasons[reason],its);CHKERRQ(ierr); if (dump) { ierr = PetscPrintf(PETSC_COMM_WORLD, " viewing combined result Hu\n");CHKERRQ(ierr); ierr = VecView(Hu,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, " viewing combined exact result Huexact\n");CHKERRQ(ierr); ierr = VecView(user.Huexact,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, " viewing final combined residual at Hu\n");CHKERRQ(ierr); ierr = VecView(r,PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); } /* evaluate error relative to exact solution */ ierr = VecAXPY(Hu,-1.0,user.Huexact); CHKERRQ(ierr); /* Hu = - Huexact + Hu */ ierr = VecStrideNormAll(Hu,NORM_INFINITY,errnorms); CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "(dx,errHinf,erruinf) %.3f %.4e %.4e\n", user.dx,errnorms[0],errnorms[1]*user.secpera);CHKERRQ(ierr); ierr = VecDestroy(Hu);CHKERRQ(ierr); ierr = VecDestroy(r);CHKERRQ(ierr); ierr = VecDestroy(user.Huexact);CHKERRQ(ierr); ierr = VecDestroy(user.M);CHKERRQ(ierr); ierr = VecDestroy(user.Bstag);CHKERRQ(ierr); ierr = VecDestroy(user.beta);CHKERRQ(ierr); ierr = MatDestroy(J); CHKERRQ(ierr); ierr = SNESDestroy(snes);CHKERRQ(ierr); ierr = DADestroy(user.da);CHKERRQ(ierr); ierr = DADestroy(user.scalarda);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt nredundant1 = 5,nredundant2 = 2,i,*ridx1,*ridx2,*lidx1,*lidx2,nlocal; PetscMPIInt rank; PetscScalar *redundant1,*redundant2; DMComposite packer; Vec global,local1,local2; PF pf; DA da1,da2; PetscViewer sviewer; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = DMCompositeCreate(PETSC_COMM_WORLD,&packer);CHKERRQ(ierr); ierr = PetscMalloc(nredundant1*sizeof(PetscScalar),&redundant1);CHKERRQ(ierr); ierr = DMCompositeAddArray(packer,0,nredundant1);CHKERRQ(ierr); ierr = DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,8,1,1,PETSC_NULL,&da1);CHKERRQ(ierr); ierr = DACreateLocalVector(da1,&local1);CHKERRQ(ierr); ierr = DMCompositeAddDM(packer,(DM)da1);CHKERRQ(ierr); ierr = PetscMalloc(nredundant2*sizeof(PetscScalar),&redundant2);CHKERRQ(ierr); ierr = DMCompositeAddArray(packer,0,nredundant2);CHKERRQ(ierr); ierr = DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,6,1,1,PETSC_NULL,&da2);CHKERRQ(ierr); ierr = DACreateLocalVector(da2,&local2);CHKERRQ(ierr); ierr = DMCompositeAddDM(packer,(DM)da2);CHKERRQ(ierr); ierr = DMCompositeCreateGlobalVector(packer,&global);CHKERRQ(ierr); ierr = PFCreate(PETSC_COMM_WORLD,1,1,&pf);CHKERRQ(ierr); ierr = PFSetType(pf,PFIDENTITY,PETSC_NULL);CHKERRQ(ierr); ierr = PFApplyVec(pf,PETSC_NULL,global);CHKERRQ(ierr); ierr = PFDestroy(pf);CHKERRQ(ierr); ierr = VecView(global,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = DMCompositeScatter(packer,global,redundant1,local1,redundant2,local2);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant1 array\n",rank);CHKERRQ(ierr); ierr = PetscScalarView(nredundant1,redundant1,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da1 vector\n",rank);CHKERRQ(ierr); ierr = PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);CHKERRQ(ierr); ierr = VecView(local1,sviewer);CHKERRQ(ierr); ierr = PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of redundant2 array\n",rank);CHKERRQ(ierr); ierr = PetscScalarView(nredundant2,redundant2,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] My part of da2 vector\n",rank);CHKERRQ(ierr); ierr = PetscViewerGetSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);CHKERRQ(ierr); ierr = VecView(local2,sviewer);CHKERRQ(ierr); ierr = PetscViewerRestoreSingleton(PETSC_VIEWER_STDOUT_WORLD,&sviewer);CHKERRQ(ierr); for (i=0; i<nredundant1; i++) redundant1[i] = (rank+2)*i; for (i=0; i<nredundant2; i++) redundant2[i] = (rank+10)*i; ierr = DMCompositeGather(packer,global,redundant1,local1,redundant2,local2);CHKERRQ(ierr); ierr = VecView(global,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); /* get the global numbering for each subvector/array element */ ierr = DMCompositeGetGlobalIndices(packer,&ridx1,&lidx1,&ridx2,&lidx2);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of redundant1 array\n",rank);CHKERRQ(ierr); ierr = PetscIntView(nredundant1,ridx1,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of local1 vector\n",rank);CHKERRQ(ierr); ierr = VecGetSize(local1,&nlocal);CHKERRQ(ierr); ierr = PetscIntView(nlocal,lidx1,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of redundant2 array\n",rank);CHKERRQ(ierr); ierr = PetscIntView(nredundant2,ridx2,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerASCIISynchronizedPrintf(PETSC_VIEWER_STDOUT_WORLD,"[%d] Global numbering of local2 vector\n",rank);CHKERRQ(ierr); ierr = VecGetSize(local2,&nlocal);CHKERRQ(ierr); ierr = PetscIntView(nlocal,lidx2,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscFree(ridx1);CHKERRQ(ierr); ierr = PetscFree(lidx1);CHKERRQ(ierr); ierr = PetscFree(ridx2);CHKERRQ(ierr); ierr = PetscFree(lidx2);CHKERRQ(ierr); ierr = DADestroy(da1);CHKERRQ(ierr); ierr = DADestroy(da2);CHKERRQ(ierr); ierr = VecDestroy(local1);CHKERRQ(ierr); ierr = VecDestroy(local2);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DMCompositeDestroy(packer);CHKERRQ(ierr); ierr = PetscFree(redundant1);CHKERRQ(ierr); ierr = PetscFree(redundant2);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { DMMG *dmmg_comp; /* multilevel grid structure */ AppCtx user; /* user-defined work context */ PetscInt mx,my,its,max_its,i; PetscErrorCode ierr; MPI_Comm comm; SNES snes; DA da1,da2; DMComposite pack; DMMG *dmmg1,*dmmg2; PetscTruth SolveSubPhysics=PETSC_FALSE,GaussSeidel=PETSC_TRUE,Jacobi=PETSC_FALSE; Vec X1,X1_local,X2,X2_local; PetscViewer viewer; PetscInitialize(&argc,&argv,(char *)0,help); comm = PETSC_COMM_WORLD; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create user context, set problem data, create vector data structures. Also, compute the initial guess. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Setup Physics 1: - Lap(U) - Grad_y(Omega) = 0 - Lap(V) + Grad_x(Omega) = 0 - Lap(Omega) + Div([U*Omega,V*Omega]) - GR*Grad_x(T) = 0 where T is given by the given x.temp - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,3,1,0,0,&da1);CHKERRQ(ierr); ierr = DASetFieldName(da1,0,"x-velocity");CHKERRQ(ierr); ierr = DASetFieldName(da1,1,"y-velocity");CHKERRQ(ierr); ierr = DASetFieldName(da1,2,"Omega");CHKERRQ(ierr); /* Create the solver object and attach the grid/physics info */ ierr = DMMGCreate(comm,1,&user,&dmmg1);CHKERRQ(ierr); ierr = DMMGSetDM(dmmg1,(DM)da1);CHKERRQ(ierr); ierr = DMMGSetISColoringType(dmmg1,IS_COLORING_GLOBAL);CHKERRQ(ierr); ierr = DMMGSetInitialGuess(dmmg1,FormInitialGuess1);CHKERRQ(ierr); ierr = DMMGSetSNES(dmmg1,FormFunction1,0);CHKERRQ(ierr); ierr = DMMGSetFromOptions(dmmg1);CHKERRQ(ierr); /* Set problem parameters (velocity of lid, prandtl, and grashof numbers) */ ierr = DAGetInfo(da1,PETSC_NULL,&mx,&my,0,0,0,0,0,0,0,0);CHKERRQ(ierr); user.lidvelocity = 1.0/(mx*my); user.prandtl = 1.0; user.grashof = 1000.0; ierr = PetscOptionsGetReal(PETSC_NULL,"-lidvelocity",&user.lidvelocity,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetReal(PETSC_NULL,"-prandtl",&user.prandtl,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetReal(PETSC_NULL,"-grashof",&user.grashof,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-solvesubphysics",&SolveSubPhysics);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-Jacobi",&Jacobi);CHKERRQ(ierr); if (Jacobi) GaussSeidel=PETSC_FALSE; ierr = PetscPrintf(comm,"grashof: %g, ",user.grashof);CHKERRQ(ierr); if (GaussSeidel){ ierr = PetscPrintf(comm,"use Block Gauss-Seidel\n");CHKERRQ(ierr); } else { ierr = PetscPrintf(comm,"use Block Jacobi\n");CHKERRQ(ierr); } ierr = PetscPrintf(comm,"===========================================\n");CHKERRQ(ierr); /* Solve the nonlinear system 1 */ if (SolveSubPhysics){ ierr = DMMGSolve(dmmg1);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg1); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); ierr = PetscPrintf(comm,"Physics 1: Number of Newton iterations = %D\n\n", its);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Setup Physics 2: - Lap(T) + PR*Div([U*T,V*T]) = 0 where U and V are given by the given x.u and x.v - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DACreate2d(comm,DA_NONPERIODIC,DA_STENCIL_STAR,-4,-4,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,&da2);CHKERRQ(ierr); ierr = DASetFieldName(da2,0,"temperature");CHKERRQ(ierr); /* Create the solver object and attach the grid/physics info */ ierr = DMMGCreate(comm,1,&user,&dmmg2);CHKERRQ(ierr); ierr = DMMGSetDM(dmmg2,(DM)da2);CHKERRQ(ierr); ierr = DMMGSetISColoringType(dmmg2,IS_COLORING_GLOBAL);CHKERRQ(ierr); ierr = DMMGSetInitialGuess(dmmg2,FormInitialGuess2);CHKERRQ(ierr); ierr = DMMGSetSNES(dmmg2,FormFunction2,0);CHKERRQ(ierr); ierr = DMMGSetFromOptions(dmmg2);CHKERRQ(ierr); /* Solve the nonlinear system 2 */ if (SolveSubPhysics){ ierr = DMMGSolve(dmmg2);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg2); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); ierr = PetscPrintf(comm,"Physics 2: Number of Newton iterations = %D\n\n", its);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve system 1 and 2 iteratively - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DACreateLocalVector(da1,&X1_local);CHKERRQ(ierr); ierr = DACreateLocalVector(da2,&X2_local);CHKERRQ(ierr); /* Only 1 snes iteration is allowed for each subphysics */ /* snes = DMMGGetSNES(dmmg1); ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1,PETSC_DEFAULT);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg2); ierr = SNESSetTolerances(snes,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT,1,PETSC_DEFAULT);CHKERRQ(ierr); */ max_its = 5; ierr = PetscOptionsGetInt(PETSC_NULL,"-mp_max_it",&max_its,PETSC_NULL);CHKERRQ(ierr); user.nsolve = 0; for (i=0; i<max_its; i++){ ierr = PetscPrintf(comm,"\nIterative nsolve %D ...\n", user.nsolve);CHKERRQ(ierr); if (!GaussSeidel){ /* get the ghosted X1_local for Physics 2 */ X1 = DMMGGetx(dmmg1); //Jacobian if (i){ierr = DAVecRestoreArray(da1,X1_local,(Field1 **)&user.x1);CHKERRQ(ierr);} ierr = DAGlobalToLocalBegin(da1,X1,INSERT_VALUES,X1_local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da1,X1,INSERT_VALUES,X1_local);CHKERRQ(ierr); ierr = DAVecGetArray(da1,X1_local,(Field1 **)&user.x1);CHKERRQ(ierr); } ierr = DMMGSolve(dmmg1);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg1); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); if (GaussSeidel){ /* get the ghosted X1_local for Physics 2 */ X1 = DMMGGetx(dmmg1); if (i){ierr = DAVecRestoreArray(da1,X1_local,(Field1 **)&user.x1);CHKERRQ(ierr);} ierr = DAGlobalToLocalBegin(da1,X1,INSERT_VALUES,X1_local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da1,X1,INSERT_VALUES,X1_local);CHKERRQ(ierr); ierr = DAVecGetArray(da1,X1_local,(Field1 **)&user.x1);CHKERRQ(ierr); } ierr = PetscPrintf(comm," Iterative physics 1: Number of Newton iterations = %D\n", its);CHKERRQ(ierr); user.nsolve++; ierr = DMMGSolve(dmmg2);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg2); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); /* get the ghosted X2_local for Physics 1 */ X2 = DMMGGetx(dmmg2); if (i){ierr = DAVecRestoreArray(da2,X2_local,(Field2 **)&user.x2);CHKERRQ(ierr);} ierr = DAGlobalToLocalBegin(da2,X2,INSERT_VALUES,X2_local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da2,X2,INSERT_VALUES,X2_local);CHKERRQ(ierr); ierr = DAVecGetArray(da2,X2_local,(Field2 **)&user.x2);CHKERRQ(ierr); ierr = PetscPrintf(comm," Iterative physics 2: Number of Newton iterations = %D\n", its);CHKERRQ(ierr); //user.nsolve++; } ierr = DAVecRestoreArray(da1,X1_local,(Field1 **)&user.x1);CHKERRQ(ierr); ierr = DAVecRestoreArray(da2,X2_local,(Field2 **)&user.x2);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create the DMComposite object to manage the two grids/physics. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscPrintf(comm," \n\n DMComposite iteration......\n");CHKERRQ(ierr); ierr = DMCompositeCreate(comm,&pack);CHKERRQ(ierr); ierr = DMCompositeAddDM(pack,(DM)da1);CHKERRQ(ierr); ierr = DMCompositeAddDM(pack,(DM)da2);CHKERRQ(ierr); /* Create the solver object and attach the grid/physics info */ ierr = DMMGCreate(comm,1,&user,&dmmg_comp);CHKERRQ(ierr); ierr = DMMGSetDM(dmmg_comp,(DM)pack);CHKERRQ(ierr); ierr = DMMGSetISColoringType(dmmg_comp,IS_COLORING_GLOBAL);CHKERRQ(ierr); ierr = DMMGSetInitialGuess(dmmg_comp,FormInitialGuessComp);CHKERRQ(ierr); ierr = DMMGSetSNES(dmmg_comp,FormFunctionComp,0);CHKERRQ(ierr); ierr = DMMGSetFromOptions(dmmg_comp);CHKERRQ(ierr); /* Solve the nonlinear system */ /* ierr = DMMGSolve(dmmg_comp);CHKERRQ(ierr); snes = DMMGGetSNES(dmmg_comp); ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); ierr = PetscPrintf(comm,"Composite Physics: Number of Newton iterations = %D\n\n", its);CHKERRQ(ierr);*/ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free spaces - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DMCompositeDestroy(pack);CHKERRQ(ierr); ierr = DADestroy(da1);CHKERRQ(ierr); ierr = DADestroy(da2);CHKERRQ(ierr); ierr = DMMGDestroy(dmmg_comp);CHKERRQ(ierr); ierr = PetscViewerASCIIOpen(comm,"log.py",&viewer);CHKERRQ(ierr); /* -log_summary */ ierr = PetscLogPrintSummaryToPy(comm,viewer);CHKERRQ(ierr); /* -snes_view */ //snes = DMMGGetSNES(dmmg1);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = DMMGDestroy(dmmg1);CHKERRQ(ierr); ierr = DMMGDestroy(dmmg2);CHKERRQ(ierr); ierr = VecDestroy(X1_local);CHKERRQ(ierr); ierr = VecDestroy(X2_local);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **args) { PetscErrorCode ierr; PetscInitialize(&argc,&args,(char *)0,help); PetscInt m = 10; /* default number of rows and columns IN GRID, but matrix is (m^2) x (m^2) */ ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr); MPI_Comm com = PETSC_COMM_WORLD; PetscMPIInt rank, size; ierr = MPI_Comm_rank(com, &rank); CHKERRQ(ierr); ierr = MPI_Comm_size(com, &size); CHKERRQ(ierr); /* create m x m two-dimensional grid for periodic boundary condition problem */ DA da2; PetscInt dof=1, stencilwidth=1; ierr = DACreate2d(com, DA_XYPERIODIC, DA_STENCIL_STAR, m,m,PETSC_DECIDE,PETSC_DECIDE, dof,stencilwidth,PETSC_NULL,PETSC_NULL,&da2); CHKERRQ(ierr); /* get da2-managed Vecs */ Vec x,b,u; ierr = DACreateGlobalVector(da2,&x); CHKERRQ(ierr); ierr = VecDuplicate(x,&b); CHKERRQ(ierr); ierr = VecDuplicate(x,&u); CHKERRQ(ierr); Mat A; ierr = DAGetMatrix(da2, MATMPIAIJ, &A); CHKERRQ(ierr); /* alternative call below is not quite same as result from DAGetMatrix(), because of nonzero allocation; the Mat ownership ranges are same */ /* ierr = MatCreateMPIAIJ(com, mlocal, mlocal, m*m, m*m, 5, PETSC_NULL, 4, PETSC_NULL, &A); CHKERRQ(ierr) */ ierr = MatSetFromOptions(A);CHKERRQ(ierr); /* report on ownership range */ PetscInt rstart,rend,mlocal; ierr = VecGetOwnershipRange(x,&rstart,&rend);CHKERRQ(ierr); ierr = VecGetLocalSize(x,&mlocal);CHKERRQ(ierr); PetscInt A_rstart,A_rend; ierr = MatGetOwnershipRange(A,&A_rstart,&A_rend);CHKERRQ(ierr); if ((rstart != A_rstart) || (rend != A_rend)) { ierr = PetscPrintf(com, "Vec and Mat ownership ranges different!!! ending ...\n"); CHKERRQ(ierr); PetscEnd(); } else { ierr = PetscSynchronizedPrintf(com, "rank=%d has Vec and Mat ownership: mlocal=%d, rstart=%d, rend=%d\n", rank,mlocal,rstart,rend); CHKERRQ(ierr); } PetscSynchronizedFlush(com); /* get local part of grid */ PetscInt xm,ym,xs,ys; DAGetCorners(da2,&xs,&ys,0,&xm,&ym,0); /* report on local part of grid */ ierr = PetscSynchronizedPrintf(com, "rank=%d has da2-managed-Vec local ranges: xs=%d, xm=%d, ys=%d, ym=%d\n", rank,xs,xm,ys,ym); CHKERRQ(ierr); PetscSynchronizedFlush(com); /* set up linear system */ PetscScalar **barr, **uarr; /* RHS and exact soln, resp. */ DAVecGetArray(da2, b, &barr); DAVecGetArray(da2, u, &uarr); PetscScalar dx = 1.0/(double)m, dy = dx, pi = 3.14159265358979; PetscScalar xi,yj; PetscInt diag=0,north=1,east=2,south=3,west=4; PetscScalar vals[5] = {-4.0 + dx * dx, 1.0, 1.0, 1.0, 1.0}; MatStencil row, col[5]; /* these are not "stencils" at all, but local grid to global indices helpers */ PetscInt i,j,num; for (j=ys; j<ys+ym; j++) { for(i=xs; i<xs+xm; i++) { /* entries of matrix A */ row.i = i; row.j = j; row.c = 0; /* dof = 1 so first component; note row.k is for 3d DAs */ for (num=0; num<5; num++) col[num].c = 0; /* set diag first, then go through stencil neighbors */ col[diag].i = i; col[diag].j = j; col[north].i = i; col[north].j = j+1; col[east].i = i+1; col[east].j = j; col[south].i = i; col[south].j = j-1; col[west].i = i-1; col[west].j = j; ierr = MatSetValuesStencil(A,1,&row,5,col,vals,INSERT_VALUES); CHKERRQ(ierr); /* entries of vectors: exact solution u and right-hand-side b */ xi = (double)i * dx; yj = (double)j * dy; uarr[j][i] = sin(2.0 * pi * xi) * cos(4.0 * pi * yj); barr[j][i] = (1.0 - 20.0 * pi * pi) * uarr[j][i]; barr[j][i] *= dx * dx; } } DAVecRestoreArray(da2, b, &barr); DAVecRestoreArray(da2, u, &uarr); ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); ierr = VecAssemblyBegin(b); CHKERRQ(ierr); ierr = VecAssemblyEnd(b); CHKERRQ(ierr); ierr = VecAssemblyBegin(u); CHKERRQ(ierr); ierr = VecAssemblyEnd(u); CHKERRQ(ierr); /* uncomment for dense view; -mat_view default format is good enough for most purposes PetscViewer viewer; PetscViewerCreate(com, &viewer); PetscViewerSetType(viewer, PETSC_VIEWER_ASCII); PetscViewerSetFormat(viewer, PETSC_VIEWER_ASCII_DENSE); MatView(A,viewer); PetscViewerDestroy(viewer); */ /* setup solver context now that Mat and Vec are assembled */ KSP ksp; ierr = KSPCreate(com,&ksp);CHKERRQ(ierr); /* Set "operators". Here the matrix that defines the linear system also serves as the preconditioning matrix. But we do not assert a relationship between their nonzero patterns.(???) */ ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); /* Following is optional; parameters could be set at runtime. */ ierr = KSPSetTolerances(ksp,1.e-7,PETSC_DEFAULT,PETSC_DEFAULT,PETSC_DEFAULT); CHKERRQ(ierr); /* Set runtime options, e.g., -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol> */ ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); /* Solve linear system */ ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); /* Compute and report the error (and the iteration count and reason). */ PetscScalar norminf, normtwo, neg_one=-1.0; PetscInt its; KSPConvergedReason reason; ierr = VecAXPY(x,neg_one,u);CHKERRQ(ierr); // x = x - u ierr = VecNorm(x,NORM_INFINITY,&norminf);CHKERRQ(ierr); ierr = VecNorm(x,NORM_2,&normtwo);CHKERRQ(ierr); // discrete norm normtwo *= dx * dy; // integral norm ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); ierr = KSPGetConvergedReason(ksp,&reason); CHKERRQ(ierr); ierr = PetscPrintf(com, "Error norms ||err||_inf = %.3e, ||err||_2 = %.3e;\n" "Iterations = %d; Reason = %d\n", norminf, normtwo, its, (int) reason);CHKERRQ(ierr); /* destroy */ ierr = KSPDestroy(ksp);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); ierr = VecDestroy(x);CHKERRQ(ierr); ierr = VecDestroy(u);CHKERRQ(ierr); ierr = VecDestroy(b);CHKERRQ(ierr); ierr = DADestroy(da2);CHKERRQ(ierr); /* Always call PetscFinalize() before exiting a program. */ ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscInt M = 13,dof=1,s=1,wrap=0,i,n,j; PetscErrorCode ierr; DA da; PetscViewer viewer; Vec local,locala,global,coors; PetscScalar *x,*alocal; PetscDraw draw; char fname[16]; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); /* Create viewers */ ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",PETSC_DECIDE,PETSC_DECIDE,600,200,&viewer);CHKERRQ(ierr); ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr); /* Read options */ ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-s",&s,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-periodic",&wrap,PETSC_NULL);CHKERRQ(ierr); /* Create distributed array and get vectors */ ierr = DACreate1d(PETSC_COMM_WORLD,(DAPeriodicType)wrap,M,dof,s,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DASetUniformCoordinates(da,0.0,1.0,0.0,0.0,0.0,0.0);CHKERRQ(ierr); for (i=0; i<dof; i++) { sprintf(fname,"Field %d",(int)i); ierr = DASetFieldName(da,i,fname); } ierr = DAView(da,viewer);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&locala);CHKERRQ(ierr); ierr = DAGetCoordinates(da,&coors);CHKERRQ(ierr); ierr = VecGetArray(coors,&x);CHKERRQ(ierr); /* Set values into global vectors */ ierr = VecGetArray(global,&alocal);CHKERRQ(ierr); ierr = VecGetLocalSize(global,&n);CHKERRQ(ierr); n = n/dof; for (j=0; j<dof; j++) { for (i=0; i<n; i++) { alocal[j+dof*i] = PetscSinScalar(2*PETSC_PI*(j+1)*x[i]); } } ierr = VecRestoreArray(global,&alocal);CHKERRQ(ierr); ierr = VecRestoreArray(coors,&x);CHKERRQ(ierr); ierr = VecDestroy(coords);CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); /* Send ghost points to local vectors */ ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,locala);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,locala);CHKERRQ(ierr); /* Free memory */ ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(locala);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscMPIInt rank; PetscInt M = -10,N = -8; PetscErrorCode ierr; PetscTruth flg = PETSC_FALSE; DA da; PetscViewer viewer; Vec local,global; PetscScalar value; DAPeriodicType ptype = DA_NONPERIODIC; DAStencilType stype = DA_STENCIL_BOX; #if defined(PETSC_HAVE_MATLAB_ENGINE) PetscViewer mviewer; #endif ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) ierr = PetscViewerMatlabOpen(PETSC_COMM_WORLD,"tmp.mat",FILE_MODE_WRITE,&mviewer);CHKERRQ(ierr); #endif ierr = PetscOptionsGetTruth(PETSC_NULL,"-star_stencil",&flg,PETSC_NULL);CHKERRQ(ierr); if (flg) stype = DA_STENCIL_STAR; /* Create distributed array and get vectors */ ierr = DACreate2d(PETSC_COMM_WORLD,ptype,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); value = -3.0; ierr = VecSet(global,value);CHKERRQ(ierr); ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); value = rank+1; ierr = VecScale(local,value);CHKERRQ(ierr); ierr = DALocalToGlobal(da,local,ADD_VALUES,global);CHKERRQ(ierr); flg = PETSC_FALSE; ierr = PetscOptionsGetTruth(PETSC_NULL, "-view_global", &flg,PETSC_NULL);CHKERRQ(ierr); if (flg) { /* view global vector in natural ordering */ ierr = VecView(global,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); } ierr = DAView(da,viewer);CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); #if defined(PETSC_HAVE_MATLAB_ENGINE) ierr = DAView(da,mviewer);CHKERRQ(ierr); ierr = VecView(global,mviewer);CHKERRQ(ierr); #endif /* Free memory */ #if defined(PETSC_HAVE_MATLAB_ENGINE) ierr = PetscViewerDestroy(mviewer);CHKERRQ(ierr); #endif ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; KSP ksp; PC pc; Vec x,b; DA da; Mat A,Atrans; PetscInt dof=1,M=-8; PetscTruth flg,trans=PETSC_FALSE; PetscInitialize(&argc,&argv,(char *)0,help); ierr = PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetTruth(PETSC_NULL,"-trans",&trans,PETSC_NULL);CHKERRQ(ierr); ierr = DACreate(PETSC_COMM_WORLD,&da);CHKERRQ(ierr); ierr = DASetDim(da,3);CHKERRQ(ierr); ierr = DASetPeriodicity(da,DA_NONPERIODIC);CHKERRQ(ierr); ierr = DASetStencilType(da,DA_STENCIL_STAR);CHKERRQ(ierr); ierr = DASetSizes(da,M,M,M);CHKERRQ(ierr); ierr = DASetNumProcs(da,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE);CHKERRQ(ierr); ierr = DASetDof(da,dof);CHKERRQ(ierr); ierr = DASetStencilWidth(da,1);CHKERRQ(ierr); ierr = DASetVertexDivision(da,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); ierr = DASetFromOptions(da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&x);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&b);CHKERRQ(ierr); ierr = ComputeRHS(da,b);CHKERRQ(ierr); ierr = DAGetMatrix(da,MATBAIJ,&A);CHKERRQ(ierr); ierr = ComputeMatrix(da,A);CHKERRQ(ierr); /* A is non-symmetric. Make A = 0.5*(A + Atrans) symmetric for testing icc and cholesky */ ierr = MatTranspose(A,MAT_INITIAL_MATRIX,&Atrans);CHKERRQ(ierr); ierr = MatAXPY(A,1.0,Atrans,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); ierr = MatScale(A,0.5);CHKERRQ(ierr); ierr = MatDestroy(Atrans);CHKERRQ(ierr); /* Test sbaij matrix */ flg = PETSC_FALSE; ierr = PetscOptionsGetTruth(PETSC_NULL, "-test_sbaij1", &flg,PETSC_NULL);CHKERRQ(ierr); if (flg){ Mat sA; ierr = MatConvert(A,MATSBAIJ,MAT_INITIAL_MATRIX,&sA);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); A = sA; } ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A,SAME_NONZERO_PATTERN);CHKERRQ(ierr); ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); ierr = PCSetDA(pc,da);CHKERRQ(ierr); if (trans) { ierr = KSPSolveTranspose(ksp,b,x);CHKERRQ(ierr); } else { ierr = KSPSolve(ksp,b,x);CHKERRQ(ierr); } /* check final residual */ flg = PETSC_FALSE; ierr = PetscOptionsGetTruth(PETSC_NULL, "-check_final_residual", &flg,PETSC_NULL);CHKERRQ(ierr); if (flg){ Vec b1; PetscReal norm; ierr = KSPGetSolution(ksp,&x);CHKERRQ(ierr); ierr = VecDuplicate(b,&b1);CHKERRQ(ierr); ierr = MatMult(A,x,b1);CHKERRQ(ierr); ierr = VecAXPY(b1,-1.0,b);CHKERRQ(ierr); ierr = VecNorm(b1,NORM_2,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Final residual %g\n",norm);CHKERRQ(ierr); ierr = VecDestroy(b1);CHKERRQ(ierr); } ierr = KSPDestroy(ksp);CHKERRQ(ierr); ierr = VecDestroy(x);CHKERRQ(ierr); ierr = VecDestroy(b);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **args) { Vec u; Mat A; PetscErrorCode ierr; mv_MultiVectorPtr eigenvectors; PetscScalar * eigs; PetscScalar * eigs_hist; double * resid; double * resid_hist; int iterations; PetscMPIInt rank; int n_eigs = 1; int seed = 1; int i,j; PetscLogDouble t1,t2,elapsed_time; DA da; double tol=1e-08; PetscTruth option_present; PetscTruth freepart=PETSC_FALSE; PetscTruth full_output=PETSC_FALSE; PetscInt m,n,p; KSP ksp; lobpcg_Tolerance lobpcg_tol; int maxIt = 100; mv_InterfaceInterpreter ii; lobpcg_BLASLAPACKFunctions blap_fn; aux_data_struct aux_data; /* PetscViewer viewer; */ PetscInt tmp_int; mv_TempMultiVector * xe; PetscInt N; PetscScalar * xx; PetscInitialize(&argc,&args,(char *)0,help); ierr = PetscOptionsGetInt(PETSC_NULL,"-n_eigs",&tmp_int,&option_present);CHKERRQ(ierr); if (option_present) n_eigs = tmp_int; ierr = PetscOptionsGetReal(PETSC_NULL,"-tol", &tol,PETSC_NULL); CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-freepart",&freepart); CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-full_out",&full_output); CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-seed",&tmp_int,&option_present);CHKERRQ(ierr); if (option_present) seed = tmp_int; ierr = PetscOptionsGetInt(PETSC_NULL,"-itr",&tmp_int,&option_present);CHKERRQ(ierr); if (option_present) maxIt = tmp_int; if (seed<1) seed=1; /* we actually run our code twice: first time we solve small problem just to make sure that all program code is actually loaded into memory; then we solve the problem we are interested in; this trick is done for accurate timing */ PreLoadBegin(PETSC_TRUE,"grid and matrix assembly"); /* "create" the grid and stencil data; on first run we form small problem */ if (PreLoadIt==0) { /* small problem */ ierr=DACreate3d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,10,10,10, 1,PETSC_DECIDE,1,1,1,0,0,0,&da); CHKERRQ(ierr); } else { /* actual problem */ if (freepart) /* petsc determines partitioning */ { ierr=DACreate3d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,-10,-10,-10, PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,0,&da); CHKERRQ(ierr); } else /* (1,NP,1) partitioning */ { ierr=DACreate3d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,-10,-10,-10, 1,PETSC_DECIDE,1,1,1,0,0,0,&da); CHKERRQ(ierr); } /* now we print what partitioning is chosen */ ierr=DAGetInfo(da,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL,&m, &n,&p,PETSC_NULL,PETSC_NULL,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr); PetscPrintf(PETSC_COMM_WORLD,"Partitioning: %u %u %u\n",m,n,p); } /* create matrix, whose nonzero structure and probably partitioning corresponds to grid and stencil structure */ ierr=DAGetMatrix(da,MATMPIAIJ,&A); CHKERRQ(ierr); /* fill the matrix with values. I intend to build 7-pt Laplas */ /* this procedure includes matrix assembly */ ierr=FillMatrix(da,A); CHKERRQ(ierr); /* PetscViewerBinaryOpen(PETSC_COMM_WORLD,"matrix.dat",FILE_MODE_WRITE,&viewer); MatView(A,PETSC_VIEWER_STDOUT_WORLD); PetscViewerDestroy(viewer); */ /* Create parallel vectors. - We form 1 vector from scratch and then duplicate as needed. */ ierr = DACreateGlobalVector(da,&u); CHKERRQ(ierr); /* ierr = VecSetFromOptions(u);CHKERRQ(ierr); */ /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create the linear solver and set various options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Here we START measuring time for preconditioner setup */ PreLoadStage("preconditioner setup"); ierr = PetscGetTime(&t1);CHKERRQ(ierr); /* Create linear solver context */ ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); /* Set operators. Here the matrix that defines the linear system also serves as the preconditioning matrix. */ ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); /* Set runtime options, e.g., -ksp_type <type> -pc_type <type> -ksp_monitor -ksp_rtol <rtol> These options will override those specified above as long as KSPSetFromOptions() is called _after_ any other customization routines. */ ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); /* probably this call actually builds the preconditioner */ ierr = KSPSetUp(ksp);CHKERRQ(ierr); /* Here we STOP measuring time for preconditioner setup */ PreLoadStage("solution"); ierr = PetscGetTime(&t2);CHKERRQ(ierr); elapsed_time=t2-t1; if (PreLoadIt==1) PetscPrintf(PETSC_COMM_WORLD,"Preconditioner setup, seconds: %f\n",elapsed_time); /* request memory for eig-vals */ ierr = PetscMalloc(sizeof(PetscScalar)*n_eigs,&eigs); CHKERRQ(ierr); /* request memory for eig-vals history */ ierr = PetscMalloc(sizeof(PetscScalar)*n_eigs*(maxIt+1),&eigs_hist); CHKERRQ(ierr); /* request memory for resid. norms */ ierr = PetscMalloc(sizeof(double)*n_eigs,&resid); CHKERRQ(ierr); /* request memory for resid. norms hist. */ ierr = PetscMalloc(sizeof(double)*n_eigs*(maxIt+1),&resid_hist); CHKERRQ(ierr); LOBPCG_InitRandomContext(); MPI_Comm_rank(PETSC_COMM_WORLD,&rank); PETSCSetupInterpreter( &ii ); eigenvectors = mv_MultiVectorCreateFromSampleVector(&ii, n_eigs,u); xe = (mv_TempMultiVector *) mv_MultiVectorGetData( eigenvectors ); /* VecView( (Vec)xe->vector[0],PETSC_VIEWER_STDOUT_WORLD); */ for (i=0; i<seed; i++) /* this cycle is to imitate changing random seed */ mv_MultiVectorSetRandom (eigenvectors, 1234); /* VecView( (Vec)xe->vector[0],PETSC_VIEWER_STDOUT_WORLD); */ VecGetSize( (Vec)xe->vector[0], &N ); N=mv_TempMultiVectorHeight( xe ); VecGetArray( (Vec)xe->vector[0],&xx); lobpcg_tol.absolute = tol; lobpcg_tol.relative = 1e-50; #ifdef PETSC_USE_COMPLEX blap_fn.zpotrf = PETSC_zpotrf_interface; blap_fn.zhegv = PETSC_zsygv_interface; #else blap_fn.dpotrf = PETSC_dpotrf_interface; blap_fn.dsygv = PETSC_dsygv_interface; #endif aux_data.A = A; aux_data.ksp = ksp; aux_data.ii = ii; /* Here we START measuring time for solution process */ ierr = PetscGetTime(&t1);CHKERRQ(ierr); #ifdef PETSC_USE_COMPLEX lobpcg_solve_complex( eigenvectors, /*input-initial guess of e-vectors */ &aux_data, /*input-matrix A */ OperatorAMultiVector, /*input-operator A */ NULL, /*input-matrix B */ NULL, /*input-operator B */ &aux_data, /*input-matrix T */ Precond_FnMultiVector, /*input-operator T */ NULL, /*input-matrix Y */ blap_fn, /*input-lapack functions */ lobpcg_tol, /*input-tolerances */ PreLoadIt? maxIt:1, /*input-max iterations */ !rank && PreLoadIt, /*input-verbosity level */ &iterations, /*output-actual iterations */ (komplex *) eigs, /*output-eigenvalues */ (komplex *) eigs_hist, /*output-eigenvalues history */ n_eigs, /*output-history global height */ resid, /*output-residual norms */ resid_hist , /*output-residual norms history */ n_eigs /*output-history global height */ ); #else lobpcg_solve_double( eigenvectors, &aux_data, OperatorAMultiVector, NULL, NULL, &aux_data, Precond_FnMultiVector, NULL, blap_fn, lobpcg_tol, PreLoadIt? maxIt:1, !rank && PreLoadIt, &iterations, eigs, /* eigenvalues; "lambda_values" should point to array containing <blocksize> doubles where <blocksize> is the width of multivector "blockVectorX" */ eigs_hist, /* eigenvalues history; a pointer to the entries of the <blocksize>-by-(<maxIterations>+1) matrix stored in fortran-style. (i.e. column-wise) The matrix may be a submatrix of a larger matrix, see next argument */ n_eigs, /* global height of the matrix (stored in fotran-style) specified by previous argument */ resid, /* residual norms; argument should point to array of <blocksize> doubles */ resid_hist , /* residual norms history; a pointer to the entries of the <blocksize>-by-(<maxIterations>+1) matrix stored in fortran-style. (i.e. column-wise) The matrix may be a submatrix of a larger matrix, see next argument */ n_eigs /* global height of the matrix (stored in fotran-style) specified by previous argument */ ); #endif /* Here we STOP measuring time for solution process */ ierr = PetscGetTime(&t2);CHKERRQ(ierr); elapsed_time=t2-t1; if (PreLoadIt) PetscPrintf(PETSC_COMM_WORLD,"Solution process, seconds: %e\n",elapsed_time); if (PreLoadIt && full_output) { PetscPrintf(PETSC_COMM_WORLD,"Output from LOBPCG, eigenvalues:\n"); for (i=0;i<n_eigs;i++) { ierr = PetscPrintf(PETSC_COMM_WORLD,"%e\n",PetscRealPart(eigs[i])); CHKERRQ(ierr); } PetscPrintf(PETSC_COMM_WORLD,"Output from LOBPCG, eigenvalues history:\n"); for (j=0; j<iterations+1; j++) for (i=0;i<n_eigs;i++) { ierr = PetscPrintf(PETSC_COMM_WORLD,"%e\n",PetscRealPart(*(eigs_hist+j*n_eigs+i))); CHKERRQ(ierr); } PetscPrintf(PETSC_COMM_WORLD,"Output from LOBPCG, residual norms:\n"); for (i=0;i<n_eigs;i++) { ierr = PetscPrintf(PETSC_COMM_WORLD,"%e\n",resid[i]); CHKERRQ(ierr); } PetscPrintf(PETSC_COMM_WORLD,"Output from LOBPCG, residual norms history:\n"); for (j=0; j<iterations+1; j++) for (i=0;i<n_eigs;i++) { ierr = PetscPrintf(PETSC_COMM_WORLD,"%e\n",*(resid_hist+j*n_eigs+i)); CHKERRQ(ierr); } } /* Free work space. All PETSc objects should be destroyed when they are no longer needed. */ ierr = VecDestroy(u);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); ierr = KSPDestroy(ksp);CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); LOBPCG_DestroyRandomContext(); mv_MultiVectorDestroy(eigenvectors); /* free memory used for eig-vals */ ierr = PetscFree(eigs); CHKERRQ(ierr); ierr = PetscFree(eigs_hist); CHKERRQ(ierr); ierr = PetscFree(resid); CHKERRQ(ierr); ierr = PetscFree(resid_hist); CHKERRQ(ierr); /* Always call PetscFinalize() before exiting a program. This routine - finalizes the PETSc libraries as well as MPI - provides summary and diagnostic information if certain runtime options are chosen (e.g., -log_summary). */ PreLoadEnd(); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt its,n,Nx=PETSC_DECIDE,Ny=PETSC_DECIDE,nlocal,i; PetscMPIInt size; PC pc; PetscInt mx,my; Mat A; GridCtx fine_ctx; KSP ksp; PetscTruth flg; PetscInitialize(&argc,&argv,PETSC_NULL,help); /* set up discretization matrix for fine grid */ /* ML requires input of fine-grid matrix. It determines nlevels. */ fine_ctx.mx = 9; fine_ctx.my = 9; ierr = PetscOptionsGetInt(PETSC_NULL,"-mx",&mx,&flg);CHKERRQ(ierr); if (flg) fine_ctx.mx = mx; ierr = PetscOptionsGetInt(PETSC_NULL,"-my",&my,&flg);CHKERRQ(ierr); if (flg) fine_ctx.my = my; ierr = PetscPrintf(PETSC_COMM_WORLD,"Fine grid size %D by %D\n",fine_ctx.mx,fine_ctx.my);CHKERRQ(ierr); n = fine_ctx.mx*fine_ctx.my; MPI_Comm_size(PETSC_COMM_WORLD,&size); ierr = PetscOptionsGetInt(PETSC_NULL,"-Nx",&Nx,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-Ny",&Ny,PETSC_NULL);CHKERRQ(ierr); ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR,fine_ctx.mx, fine_ctx.my,Nx,Ny,1,1,PETSC_NULL,PETSC_NULL,&fine_ctx.da);CHKERRQ(ierr); ierr = DACreateGlobalVector(fine_ctx.da,&fine_ctx.x);CHKERRQ(ierr); ierr = VecDuplicate(fine_ctx.x,&fine_ctx.b);CHKERRQ(ierr); ierr = VecGetLocalSize(fine_ctx.x,&nlocal);CHKERRQ(ierr); ierr = DACreateLocalVector(fine_ctx.da,&fine_ctx.localX);CHKERRQ(ierr); ierr = VecDuplicate(fine_ctx.localX,&fine_ctx.localF);CHKERRQ(ierr); ierr = MatCreateMPIAIJ(PETSC_COMM_WORLD,nlocal,nlocal,n,n,5,PETSC_NULL,3,PETSC_NULL,&A);CHKERRQ(ierr); ierr = FormJacobian_Grid(&fine_ctx,&A);CHKERRQ(ierr); /* create linear solver */ ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = KSPGetPC(ksp,&pc);CHKERRQ(ierr); ierr = PCSetType(pc,PCML);CHKERRQ(ierr); /* set options, then solve system */ ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); /* calls PCSetFromOptions_MG/ML */ for (i=0; i<3; i++){ if (i<2){ /* test DIFFERENT_NONZERO_PATTERN */ /* set values for rhs vector */ ierr = VecSet(fine_ctx.b,i+1.0);CHKERRQ(ierr); /* modify A */ ierr = MatShift(A,1.0);CHKERRQ(ierr); ierr = MatScale(A,2.0);CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr); } else { /* test SAME_NONZERO_PATTERN */ ierr = KSPSetOperators(ksp,A,A,SAME_NONZERO_PATTERN);CHKERRQ(ierr); } ierr = KSPSolve(ksp,fine_ctx.b,fine_ctx.x);CHKERRQ(ierr); ierr = KSPGetIterationNumber(ksp,&its);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Number of iterations = %D\n",its);CHKERRQ(ierr); } /* free data structures */ ierr = VecDestroy(fine_ctx.x);CHKERRQ(ierr); ierr = VecDestroy(fine_ctx.b);CHKERRQ(ierr); ierr = DADestroy(fine_ctx.da);CHKERRQ(ierr); ierr = VecDestroy(fine_ctx.localX);CHKERRQ(ierr); ierr = VecDestroy(fine_ctx.localF);CHKERRQ(ierr); ierr = MatDestroy(A);CHKERRQ(ierr); ierr = KSPDestroy(ksp);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
int main(int argc, char **argv) { PetscInitialize(&argc, &argv, "wave.opt", help); int rank; MPI_Comm_rank(MPI_COMM_WORLD, &rank); double startTime, endTime; int Ns = 32; unsigned int dof = 1; // double dtratio = 1.0; DA da; // Underlying DA Vec rho; // density - elemental scalar Vec nu; // Lame parameter - lambda - elemental scalar std::vector < std::vector<Vec> > fBasis; // the scalar activation - nodal scalar // std::vector<Vec> truth; // the ground truth. // Initial conditions Vec initialDisplacement; Vec initialVelocity; timeInfo ti; // get Ns CHKERRQ ( PetscOptionsGetInt(0,"-Ns", &Ns,0) ); double t0 = 0.0; double dt = 1.0/(Ns); double t1 = 1.0; double nuVal = 1.0; double beta = 0.0001; int numParams = 5; CHKERRQ ( PetscOptionsGetInt(0,"-nump",&numParams,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-t0",&t0,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-t1",&t1,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-dt",&dt,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-nu",&nuVal,0) ); CHKERRQ ( PetscOptionsGetScalar(0,"-beta",&beta,0) ); // CHKERRQ ( PetscOptionsGetString(PETSC_NULL, "-pn", problemName, PETSC_MAX_PATH_LEN-1, PETSC_NULL)); // Time info for timestepping ti.start = t0; ti.stop = t1; ti.step = dt; if (!rank) { std::cout << "Problem size is " << Ns+1 << " spatially and NT = " << (int)ceil(1.0/dt) << std::endl << std::endl; std::cout << "Number of parameters is " << numParams << std::endl; } // create DA CHKERRQ ( DACreate3d ( PETSC_COMM_WORLD, DA_NONPERIODIC, DA_STENCIL_BOX, Ns+1, Ns+1, Ns+1, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 1, 1, 0, 0, 0, &da) ); massMatrix *Mass = new massMatrix(feMat::PETSC); // Mass Matrix stiffnessMatrix *Stiffness = new stiffnessMatrix(feMat::PETSC); // Stiffness matrix waveDamping *Damping = new waveDamping(feMat::PETSC); // Damping Matrix fdynamicVector *Force = new fdynamicVector(feVec::PETSC); // Force Vector // create vectors CHKERRQ( DACreateGlobalVector(da, &rho) ); CHKERRQ( DACreateGlobalVector(da, &nu) ); CHKERRQ( DACreateGlobalVector(da, &initialDisplacement) ); CHKERRQ( DACreateGlobalVector(da, &initialVelocity) ); // Set initial conditions CHKERRQ( VecSet ( initialDisplacement, 0.0) ); CHKERRQ( VecSet ( initialVelocity, 0.0) ); VecZeroEntries( nu ); VecZeroEntries( rho ); CHKERRQ( VecSet ( nu, nuVal) ); CHKERRQ( VecSet ( rho, 1.0) ); int x, y, z, m, n, p; int mx,my,mz; CHKERRQ( DAGetCorners(da, &x, &y, &z, &m, &n, &p) ); CHKERRQ( DAGetInfo(da,0, &mx, &my, &mz, 0,0,0,0,0,0,0) ); double acx,acy,acz; double hx = 1.0/((double)Ns); // allocate for temporary buffers ... // unsigned int elemSize = Ns*Ns*Ns; // std::cout << "Elem size is " << elemSize << std::endl; // unsigned int nodeSize = (Ns+1)*(Ns+1)*(Ns+1); // Now set the activation ... unsigned int numSteps = (unsigned int)(ceil(( ti.stop - ti.start)/ti.step)); // Vec tauVec; // PetscScalar ***tauArray; unsigned int paramTimeSteps = (unsigned int)(ceil(( (double)(numSteps))/ ((double)(2*numParams)) )); /* for (int b=0; b<numParams; b++) { std::vector<Vec> tau; unsigned int tBegin = paramTimeSteps*b; unsigned int tEnd = tBegin + numSteps/2; // paramTimeSteps*(b+2); // std::cout << "For param " << b << ": Time step range is " << tBegin << " -> " << tEnd << std::endl; for (unsigned int t=0; t<numSteps+1; t++) { double newTime = (dt*(t-tBegin)*numSteps)/((double)(paramTimeSteps)); // double fff = 0.0; CHKERRQ( DACreateGlobalVector(da, &tauVec) ); CHKERRQ( VecSet( tauVec, 0.0)); if ( (t>=tBegin) && (t<=tEnd)) { CHKERRQ(DAVecGetArray(da, tauVec, &tauArray)); for (int k = z; k < z + p ; k++) { for (int j = y; j < y + n; j++) { for (int i = x; i < x + m; i++) { acx = (i)*hx; acy = (j)*hx; acz = (k)*hx; tauArray[k][j][i] = sin(M_PI*newTime)*cos(2*M_PI*acx)*cos(2*M_PI*acy)*cos(2*M_PI*acz); } } } CHKERRQ( DAVecRestoreArray ( da, tauVec, &tauArray ) ); } tau.push_back(tauVec); } fBasis.push_back(tau); } */ // std::cout << "Finished setting basis" << std::endl; /* // Set initial velocity ... CHKERRQ(DAVecGetArray(da, initialVelocity, &solArray)); for (int k = z; k < z + p ; k++) { for (int j = y; j < y + n; j++) { for (int i = x; i < x + m; i++) { acx = (i)*hx; acy = (j)*hx; acz = (k)*hx; solArray[k][j][i] = M_PI*cos(2*M_PI*acx)*cos(2*M_PI*acy)*cos(2*M_PI*acz); } } } CHKERRQ( DAVecRestoreArray ( da, initialVelocity, &solArray ) ); */ std::vector<Vec> newF; Vec alpha; PetscScalar *avec; VecCreateSeq(PETSC_COMM_SELF, numParams, &alpha); /* VecCreate(PETSC_COMM_WORLD, &alpha); VecSetSizes(alpha, numParams, PETSC_DECIDE); VecSetFromOptions(alpha); */ VecGetArray(alpha, &avec); for (int j=0; j<numParams; j++) avec[j] = 0.5 + 0.5*j; VecRestoreArray(alpha, &avec); // getForces(alpha, fBasis, newF); getForces(alpha, newF, da, ti, numParams); // Setup Matrices and Force Vector ... Mass->setProblemDimensions(1.0, 1.0, 1.0); Mass->setDA(da); Mass->setDof(dof); Mass->setNuVec(rho); Stiffness->setProblemDimensions(1.0, 1.0, 1.0); Stiffness->setDA(da); Stiffness->setDof(dof); Stiffness->setNuVec(nu); Damping->setAlpha(0.0); Damping->setBeta(0.00075); Damping->setMassMatrix(Mass); Damping->setStiffnessMatrix(Stiffness); Damping->setDA(da); Damping->setDof(dof); // Force Vector Force->setProblemDimensions(1.0,1.0,1.0); Force->setDA(da); Force->setFDynamic(newF); Force->setTimeInfo(&ti); // Newmark time stepper ... newmark *ts = new newmark; ts->setMassMatrix(Mass); ts->setDampingMatrix(Damping); ts->setStiffnessMatrix(Stiffness); ts->damp(false); ts->setTimeFrames(1); ts->storeVec(true); ts->setAdjoint(false); ts->setForceVector(Force); ts->setInitialDisplacement(initialDisplacement); ts->setInitialVelocity(initialVelocity); ts->setTimeInfo(&ti); ts->setAdjoint(false); // set if adjoint or forward ts->init(); // initialize IMPORTANT // if (!rank) // std::cout << RED"Starting initial forward solve"NRM << std::endl; ts->solve();// solve // if (!rank) // std::cout << GRN"Finished with initial forward solve"NRM << std::endl; std::vector<Vec> solvec = ts->getSolution(); // Now lets check the error ... // Vec nr; // concatenateVecs(solvec, nr); // VecDestroy(nr); // VecDestroy(gt); // std::cout << std::endl; /************* * INVERSE * *************/ // True solution is tau ... we want to recover it. // The observations in this case are, solvec /* Set very initial guess for the inverse problem*/ // Now can clear memory ... /* for (int i=0; i<newF.size(); i++) { if (newF[i] != NULL) { VecDestroy(newF[i]); } } newF.clear(); for (int i=0; i<solvec.size(); i++) { if (solvec[i] != NULL) { VecDestroy(solvec[i]); } } solvec.clear(); ts->destroy(); VecDestroy(rho); VecDestroy(nu); VecDestroy(initialDisplacement); VecDestroy(initialVelocity); VecDestroy(alpha); DADestroy(da); PetscFinalize(); return 0; */ Vec gt, nr; concatenateVecs(solvec, gt); Vec guess; VecDuplicate(alpha, &guess); VecZeroEntries(guess); // VecDuplicate(guess, &Out); // VecZeroEntries(Out); // double norm; /* PetscRandom rctx; PetscRandomCreate(PETSC_COMM_WORLD, &rctx); PetscRandomSetFromOptions(rctx); VecSetRandom(guess, rctx); VecNorm(guess, NORM_2, &norm); PetscPrintf(0, "guess norm = %g\n", norm); */ // double errnorm; // double exsolnorm; // Inverse solver set up // std::cout << RED"Setting up Inverse Solver"NRM << std::endl; parametricWaveInverse* hyperInv = new parametricWaveInverse; // std::cout << GRN"Finished setting up Inverse Solver"NRM << std::endl; hyperInv->setTimeStepper(ts); // set the timestepper hyperInv->setForwardInitialConditions(initialDisplacement, initialVelocity); // std::cout << RED"Setting initial guess"NRM << std::endl; // hyperInv->setInitialGuess(truth);// set the initial guess hyperInv->setInitialGuess(guess);// set the initial guess // std::cout << GRN"Done setting initial guess"NRM << std::endl; hyperInv->setRegularizationParameter(beta); // set the regularization paramter hyperInv->setAdjoints(solvec); // set the data for the problem // hyperInv->setForceBasis(fBasis); hyperInv->setNumberOfParameter(numParams); // std::cout << RED"Initializing Inverse Solver"NRM << std::endl; hyperInv->init(); // initialize the inverse solver // if (!rank) // std::cout << RED"Starting Inverse Solve"NRM << std::endl; startTime = MPI_Wtime(); hyperInv->solve(); // solve endTime = MPI_Wtime(); // if (!rank) // std::cout << GRN"FINISHED HESSIAN SOLVE"NRM << std::endl; hyperInv->getCurrentControl(guess); // get the solution hyperInv->destroy(); /* for (int i=0; i<solvec.size(); i++) { if (solvec[i] != NULL) { VecDestroy(solvec[i]); } } solvec.clear(); */ // VecView(guess, 0); if (!rank) std::cout << std::endl << "Error Norms " << std::endl; Vec Err; double gtNorm, solNorm, errNorm; VecDuplicate(guess, &Err); VecWAXPY(Err, -1.0, guess, alpha); VecNorm(alpha, NORM_2, >Norm); VecNorm(guess, NORM_2, &solNorm); VecNorm(Err, NORM_2, &errNorm); if (!rank) { std::cout << "The norms are " << gtNorm << ", " << solNorm << ", " << errNorm << std::endl; std::cout << "Relative error is " << errNorm/gtNorm << std::endl; } // Now we shall do another forward solve ... getForces(guess, newF, da, ti, numParams); Force->setFDynamic(newF); ts->setInitialDisplacement(initialDisplacement); ts->setInitialVelocity(initialVelocity); ts->setAdjoint(false); ts->clearMonitor(); ts->solve(); std::vector<Vec> solvec2 = ts->getSolution(); ts->destroy(); concatenateVecs(solvec2, nr); // Now can clear memory ... for (int i=0; i<solvec2.size(); i++) { if (solvec2[i] != NULL) { VecDestroy(solvec2[i]); } } solvec2.clear(); // Now can clear memory ... for (int i=0; i<newF.size(); i++) { if (newF[i] != NULL) { VecDestroy(newF[i]); } } newF.clear(); /* for (unsigned int i=0; i<truth.size(); i++) { VecNorm(truth[i], NORM_2, >Norm); VecNorm(solvec[i], NORM_2, &solNorm); VecAXPY(solvec[i], -1.0, truth[i]); VecNorm(solvec[i], NORM_2, &errNorm); PetscPrintf(0, "Ground truth at timestep %d is %g, %g, %g\n", i, gtNorm, solNorm, errNorm); // PetscPrintf(0, "Relative Error at timestep %d is %g\n", i, errNorm/gtNorm); } */ VecNorm(gt, NORM_2, >Norm); VecAXPY(nr, -1.0, gt); VecNorm(nr, NORM_2, &errNorm); if (!rank) std::cout << "Total Relative error on state is " << errNorm/gtNorm << std::endl; if (!rank) std::cout << "Wall time is " << endTime - startTime << std::endl; VecDestroy(gt); VecDestroy(nr); VecDestroy(Err); VecDestroy(alpha); VecDestroy(guess); VecDestroy(rho); VecDestroy(nu); VecDestroy(initialDisplacement); VecDestroy(initialVelocity); DADestroy(da); PetscFinalize(); }
int main(int argc,char **argv) { PetscMPIInt size,rank; PetscInt M=8,dof=1,stencil_width=1,i,start,end,P=5,N = 6,m=PETSC_DECIDE,n=PETSC_DECIDE,p=PETSC_DECIDE,pt = 0,st = 0; PetscErrorCode ierr; PetscTruth flg2,flg3,flg; DAPeriodicType periodic = DA_NONPERIODIC; DAStencilType stencil_type = DA_STENCIL_STAR; DA da; SDA sda; Vec local,global,local_copy; PetscScalar value,*in,*out; PetscReal norm,work; PetscViewer viewer; char filename[PETSC_MAX_PATH_LEN]; FILE *file; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-P",&P,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-dof",&dof,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-stencil_width",&stencil_width,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-periodic",&pt,PETSC_NULL);CHKERRQ(ierr); periodic = (DAPeriodicType) pt; ierr = PetscOptionsGetInt(PETSC_NULL,"-stencil_type",&st,PETSC_NULL);CHKERRQ(ierr); stencil_type = (DAStencilType) st; ierr = PetscOptionsHasName(PETSC_NULL,"-1d",&flg2);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-2d",&flg2);CHKERRQ(ierr); ierr = PetscOptionsHasName(PETSC_NULL,"-3d",&flg3);CHKERRQ(ierr); if (flg2) { ierr = DACreate2d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,m,n,dof,stencil_width,0,0,&da);CHKERRQ(ierr); ierr = SDACreate2d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,m,n,dof,stencil_width,0,0,&sda);CHKERRQ(ierr); } else if (flg3) { ierr = DACreate3d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&da);CHKERRQ(ierr); ierr = SDACreate3d(PETSC_COMM_WORLD,periodic,stencil_type,M,N,P,m,n,p,dof,stencil_width,0,0,0,&sda);CHKERRQ(ierr); } else { ierr = DACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,PETSC_NULL,&da);CHKERRQ(ierr); ierr = SDACreate1d(PETSC_COMM_WORLD,periodic,M,dof,stencil_width,PETSC_NULL,&sda);CHKERRQ(ierr); } ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = VecDuplicate(local,&local_copy);CHKERRQ(ierr); /* zero out vectors so that ghostpoints are zero */ value = 0; ierr = VecSet(local,value);CHKERRQ(ierr); ierr = VecSet(local_copy,value);CHKERRQ(ierr); ierr = VecGetOwnershipRange(global,&start,&end);CHKERRQ(ierr); for (i=start; i<end; i++) { value = i + 1; ierr = VecSetValues(global,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecAssemblyBegin(global);CHKERRQ(ierr); ierr = VecAssemblyEnd(global);CHKERRQ(ierr); ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); flg = PETSC_FALSE; ierr = PetscOptionsGetTruth(PETSC_NULL,"-same_array",&flg,PETSC_NULL);CHKERRQ(ierr); if (flg) { /* test the case where the input and output array is the same */ ierr = VecCopy(local,local_copy);CHKERRQ(ierr); ierr = VecGetArray(local_copy,&in);CHKERRQ(ierr); ierr = VecRestoreArray(local_copy,PETSC_NULL);CHKERRQ(ierr); ierr = SDALocalToLocalBegin(sda,in,INSERT_VALUES,in);CHKERRQ(ierr); ierr = SDALocalToLocalEnd(sda,in,INSERT_VALUES,in);CHKERRQ(ierr); } else { ierr = VecGetArray(local,&out);CHKERRQ(ierr); ierr = VecRestoreArray(local,PETSC_NULL);CHKERRQ(ierr); ierr = VecGetArray(local_copy,&in);CHKERRQ(ierr); ierr = VecRestoreArray(local_copy,PETSC_NULL);CHKERRQ(ierr); ierr = SDALocalToLocalBegin(sda,out,INSERT_VALUES,in);CHKERRQ(ierr); ierr = SDALocalToLocalEnd(sda,out,INSERT_VALUES,in);CHKERRQ(ierr); } flg = PETSC_FALSE; ierr = PetscOptionsGetTruth(PETSC_NULL,"-save",&flg,PETSC_NULL);CHKERRQ(ierr); if (flg) { ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); sprintf(filename,"local.%d",rank); ierr = PetscViewerASCIIOpen(PETSC_COMM_SELF,filename,&viewer);CHKERRQ(ierr); ierr = PetscViewerASCIIGetPointer(viewer,&file);CHKERRQ(ierr); ierr = VecView(local,viewer);CHKERRQ(ierr); fprintf(file,"Vector with correct ghost points\n"); ierr = VecView(local_copy,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); } ierr = VecAXPY(local_copy,-1.0,local);CHKERRQ(ierr); ierr = VecNorm(local_copy,NORM_MAX,&work);CHKERRQ(ierr); ierr = MPI_Allreduce(&work,&norm,1,MPIU_REAL,MPI_MAX,PETSC_COMM_WORLD);CHKERRQ(ierr); if (norm != 0.0) { ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Norm of difference %G should be zero\n",norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," Number of processors %d\n",size);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," M,N,P,dof %D %D %D %D\n",M,N,P,dof);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," stencil_width %D stencil_type %d periodic %d\n",stencil_width,(int)stencil_type,(int)periodic);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD," dimension %d\n",1 + (int) flg2 + (int) flg3);CHKERRQ(ierr); } ierr = DADestroy(da);CHKERRQ(ierr); ierr = SDADestroy(sda);CHKERRQ(ierr); ierr = VecDestroy(local_copy);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
PetscErrorCode vizGA2DA() { PetscErrorCode ierr; int rank; MPI_Comm_rank(PETSC_COMM_WORLD,&rank); int d1 = 40, d2 = 50; DA da; Vec vec; const PetscInt *lx, *ly, *lz; PetscInt m,n,p; DALocalInfo info; ierr = DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_STAR, d1,d2,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0, &da); CHKERRQ(ierr); ierr = DACreateGlobalVector(da, &vec); CHKERRQ(ierr); ierr = DAGetOwnershipRanges(da, &lx, &ly, &lz); CHKERRQ(ierr); ierr = DAGetLocalInfo(da,&info); CHKERRQ(ierr); ierr = DAGetInfo(da,0,0,0,0,&m,&n,&p,0,0,0,0); CHKERRQ(ierr); /**/ ierr = DAView(da, PETSC_VIEWER_STDOUT_WORLD); CHKERRQ(ierr); for (int i = 0; i < m; ++i) { PetscPrintf(PETSC_COMM_WORLD,"%d\tlx: %d\n",i,lx[i]); } for (int i = 0; i < n; ++i) { PetscPrintf(PETSC_COMM_WORLD,"%d\tly: %d\n",i,ly[i]); } /**/ int ga = GA_Create_handle(); int ndim = 2; int dims[2] = {d2,d1}; GA_Set_data(ga,2,dims,MT_DBL); int *map; PetscMalloc( sizeof(int)*(m+n), &map); map[0] = 0; for( int i = 1; i < n; i++ ) { map[i] = ly[i-1] + map[i-1]; } map[n] = 0; for( int i = n+1; i < m+n; i++ ) { map[i] = lx[i-n-1] + map[i-1]; } /* correct ordering, but nodeid's dont line up with mpi rank for petsc's da * DA: +---+---+ GA: +---+---+ * +-2-+-3-+ +-1-+-3-+ * +---+---+ +---+---+ * +-0-+-1-+ +-0-+-2-+ * +---+---+ +---+---+ int *map; PetscMalloc( sizeof(int)*(m+n), &map); map[0] = 0; for( int i = 1; i < m; i++ ) { map[i] = lx[i] + map[i-1]; } map[m] = 0; for( int i = m+1; i < m+n; i++ ) { map[i] = ly[i-m] + map[i-1]; } */ int block[2] = {n,m}; GA_Set_irreg_distr(ga,map,block); ierr = GA_Allocate( ga ); if( !ierr ) GA_Error("\n\n\nga allocaltion failed\n\n",ierr); if( !ga ) GA_Error("\n\n\n ga null \n\n",ierr); if( rank != GA_Nodeid() ) GA_Error("MPI rank does not match GA_Nodeid()",1); GA_Print_distribution(ga); int lo[2], hi[2]; NGA_Distribution(ga,rank,lo,hi); if( lo[1] != info.xs || hi[1] != info.xs+info.xm-1 || lo[0] != info.ys || hi[0] != info.ys+info.ym-1 ) { PetscSynchronizedPrintf(PETSC_COMM_SELF,"[%d] lo:(%2d,%2d) hi:(%2d,%2d) \t DA: (%2d,%2d), (%2d, %2d)\n", rank, lo[1], lo[0], hi[1], hi[0], info.xs, info.ys, info.xs+info.xm-1, info.ys+info.ym-1); } PetscBarrier(0); PetscSynchronizedFlush(PETSC_COMM_WORLD); AO ao; DAGetAO(da,&ao); if( rank == 0 ) { int *idx, len = d1*d2; PetscReal *val; PetscMalloc(sizeof(PetscReal)*len, &val); PetscMalloc(sizeof(int)*len, &idx); for (int j = 0; j < d2; ++j) { for (int i = 0; i < d1; ++i) { idx[i + d1*j] = i + d1*j; val[i + d1*j] = i + d1*j; } } AOApplicationToPetsc(ao,len,idx); VecSetValues(vec,len,idx,val,INSERT_VALUES); int a[2], b[2],ld[1]={0}; double c = 0; for (int j = 0; j < d2; ++j) { for (int i = 0; i < d1; ++i) { a[0] = j; a[1] = i; // printf("%5.0f ",c); NGA_Put(ga,a,a,&c,ld); c++; } } } // GA_Print(ga); VecAssemblyBegin(vec); VecAssemblyEnd(vec); int ld; double *ptr; NGA_Access(ga,lo,hi,&ptr,&ld); PetscReal **d; int c=0; ierr = DAVecGetArray(da,vec,&d); CHKERRQ(ierr); for (int j = info.ys; j < info.ys+info.ym; ++j) { for (int i = info.xs; i < info.xs+info.xm; ++i) { if( d[j][i] != ptr[(i-info.xs)+ld*(j-info.ys)] ) GA_Error("DA array is not equal to GA array",1); // printf("%d (%d,%d):\t%3.0f\t%3.0f\n", c, i, j, d[j][i], ptr[(i-info.xs)+ld*(j-info.ys)]); c++; } } ierr = DAVecRestoreArray(da,vec,&d); CHKERRQ(ierr); c=0; PetscReal *v; int start, end; VecGetOwnershipRange(vec, &start, &end); VecGetArray( vec, &v ); for( int i = start; i < end; i++) { // printf("%d:\t%3.0f\t%3.0f\t%s\n", start, v[i-start], ptr[i-start], (v[i-start]-ptr[i-start]==0?"":"NO") ); } VecRestoreArray( vec, &v ); NGA_Release_update(ga,lo,hi); Vec gada; VecCreateMPIWithArray(((PetscObject)da)->comm,da->Nlocal,PETSC_DETERMINE,ptr,&gada); VecView(gada,PETSC_VIEWER_STDOUT_SELF); GA_Destroy(ga); ierr = VecDestroy(vec); CHKERRQ(ierr); ierr = DADestroy(da); CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscInt i,j,M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE; PetscMPIInt rank; PetscErrorCode ierr; PetscTruth flg = PETSC_FALSE; DA da; PetscViewer viewer; Vec localall,global; PetscScalar value,*vlocal; DAPeriodicType ptype = DA_NONPERIODIC; DAStencilType stype = DA_STENCIL_BOX; VecScatter tolocalall,fromlocalall; PetscInt start,end; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);CHKERRQ(ierr); /* Read options */ ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-N",&N,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-m",&m,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetTruth(PETSC_NULL,"-star_stencil",&flg,PETSC_NULL);CHKERRQ(ierr); if (flg) stype = DA_STENCIL_STAR; /* Create distributed array and get vectors */ ierr = DACreate2d(PETSC_COMM_WORLD,ptype,stype, M,N,m,n,1,1,PETSC_NULL,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = VecCreateSeq(PETSC_COMM_SELF,M*N,&localall);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = VecGetOwnershipRange(global,&start,&end);CHKERRQ(ierr); for (i=start; i<end; i++) { value = 5.0*rank; ierr = VecSetValues(global,1,&i,&value,INSERT_VALUES);CHKERRQ(ierr); } ierr = VecView(global,viewer);CHKERRQ(ierr); /* Create Scatter from global DA parallel vector to local vector that contains all entries */ ierr = DAGlobalToNaturalAllCreate(da,&tolocalall);CHKERRQ(ierr); ierr = DANaturalAllToGlobalCreate(da,&fromlocalall);CHKERRQ(ierr); ierr = VecScatterBegin(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(tolocalall,global,localall,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecGetArray(localall,&vlocal);CHKERRQ(ierr); for (j=0; j<N; j++) { for (i=0; i<M; i++) { *vlocal++ += i + j*M; } } ierr = VecRestoreArray(localall,&vlocal);CHKERRQ(ierr); /* scatter back to global vector */ ierr = VecScatterBegin(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecScatterEnd(fromlocalall,localall,global,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr); ierr = VecView(global,viewer);CHKERRQ(ierr); /* Free memory */ ierr = VecScatterDestroy(tolocalall);CHKERRQ(ierr); ierr = VecScatterDestroy(fromlocalall);CHKERRQ(ierr); ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDestroy(localall);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }
/*@ DADestroy - Destroys a distributed array. Collective on DA Input Parameter: . da - the distributed array to destroy Level: beginner .keywords: distributed array, destroy .seealso: DACreate1d(), DACreate2d(), DACreate3d() @*/ PetscErrorCode PETSCDM_DLLEXPORT DADestroy(DA da) { PetscErrorCode ierr; PetscErrorCode i; PetscTruth done; PetscFunctionBegin; PetscValidHeaderSpecific(da,DM_COOKIE,1); ierr = DMDestroy_Private((DM)da,&done);CHKERRQ(ierr); if (!done) PetscFunctionReturn(0); /* destroy the internal part */ if (da->ops->destroy) { ierr = (*da->ops->destroy)(da);CHKERRQ(ierr); } /* destroy the external/common part */ for (i=0; i<DA_MAX_AD_ARRAYS; i++) { ierr = PetscFree(da->adstartghostedout[i]);CHKERRQ(ierr); ierr = PetscFree(da->adstartghostedin[i]);CHKERRQ(ierr); ierr = PetscFree(da->adstartout[i]);CHKERRQ(ierr); ierr = PetscFree(da->adstartin[i]);CHKERRQ(ierr); } for (i=0; i<DA_MAX_AD_ARRAYS; i++) { ierr = PetscFree(da->admfstartghostedout[i]);CHKERRQ(ierr); ierr = PetscFree(da->admfstartghostedin[i]);CHKERRQ(ierr); ierr = PetscFree(da->admfstartout[i]);CHKERRQ(ierr); ierr = PetscFree(da->admfstartin[i]);CHKERRQ(ierr); } for (i=0; i<DA_MAX_WORK_ARRAYS; i++) { ierr = PetscFree(da->startghostedout[i]);CHKERRQ(ierr); ierr = PetscFree(da->startghostedin[i]);CHKERRQ(ierr); ierr = PetscFree(da->startout[i]);CHKERRQ(ierr); ierr = PetscFree(da->startin[i]);CHKERRQ(ierr); } /* if memory was published with AMS then destroy it */ ierr = PetscObjectDepublish(da);CHKERRQ(ierr); if (da->ltog) {ierr = VecScatterDestroy(da->ltog);CHKERRQ(ierr);} if (da->gtol) {ierr = VecScatterDestroy(da->gtol);CHKERRQ(ierr);} if (da->ltol) {ierr = VecScatterDestroy(da->ltol);CHKERRQ(ierr);} if (da->natural){ ierr = VecDestroy(da->natural);CHKERRQ(ierr); } if (da->gton) { ierr = VecScatterDestroy(da->gton);CHKERRQ(ierr); } if (da->ao) { ierr = AODestroy(da->ao);CHKERRQ(ierr); } if (da->ltogmap) { ierr = ISLocalToGlobalMappingDestroy(da->ltogmap);CHKERRQ(ierr); } if (da->ltogmapb) { ierr = ISLocalToGlobalMappingDestroy(da->ltogmapb);CHKERRQ(ierr); } ierr = PetscFree(da->lx);CHKERRQ(ierr); ierr = PetscFree(da->ly);CHKERRQ(ierr); ierr = PetscFree(da->lz);CHKERRQ(ierr); if (da->fieldname) { for (i=0; i<da->w; i++) { ierr = PetscStrfree(da->fieldname[i]);CHKERRQ(ierr); } ierr = PetscFree(da->fieldname);CHKERRQ(ierr); } if (da->localcoloring) { ierr = ISColoringDestroy(da->localcoloring);CHKERRQ(ierr); } if (da->ghostedcoloring) { ierr = ISColoringDestroy(da->ghostedcoloring);CHKERRQ(ierr); } if (da->coordinates) {ierr = VecDestroy(da->coordinates);CHKERRQ(ierr);} if (da->ghosted_coordinates) {ierr = VecDestroy(da->ghosted_coordinates);CHKERRQ(ierr);} if (da->da_coordinates && da != da->da_coordinates) {ierr = DADestroy(da->da_coordinates);CHKERRQ(ierr);} ierr = PetscFree(da->neighbors);CHKERRQ(ierr); ierr = PetscFree(da->dfill);CHKERRQ(ierr); ierr = PetscFree(da->ofill);CHKERRQ(ierr); ierr = PetscFree(da->e);CHKERRQ(ierr); ierr = PetscHeaderDestroy(da);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscMPIInt rank,size; PetscErrorCode ierr; PetscInt M = 14,time_steps = 1000,w=1,s=1,localsize,j,i,mybase,myend; DA da; PetscViewer viewer; PetscDraw draw; Vec local,global,copy; PetscScalar *localptr,*copyptr; PetscReal h,k; ierr = PetscInitialize(&argc,&argv,(char*)0,help);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-M",&M,PETSC_NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(PETSC_NULL,"-time",&time_steps,PETSC_NULL);CHKERRQ(ierr); /* Set up the array */ ierr = DACreate1d(PETSC_COMM_WORLD,DA_NONPERIODIC,M,w,s,PETSC_NULL,&da);CHKERRQ(ierr); ierr = DACreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DACreateLocalVector(da,&local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); /* Make copy of local array for doing updates */ ierr = VecDuplicate(local,©);CHKERRQ(ierr); /* Set Up Display to Show Heat Graph */ ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",80,480,500,160,&viewer);CHKERRQ(ierr); ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr); ierr = PetscDrawSetDoubleBuffer(draw);CHKERRQ(ierr); /* determine starting point of each processor */ ierr = VecGetOwnershipRange(global,&mybase,&myend);CHKERRQ(ierr); /* Initialize the Array */ ierr = VecGetLocalSize (local,&localsize);CHKERRQ(ierr); ierr = VecGetArray (local,&localptr);CHKERRQ(ierr); ierr = VecGetArray (copy,©ptr);CHKERRQ(ierr); localptr[0] = copyptr[0] = 0.0; localptr[localsize-1] = copyptr[localsize-1] = 1.0; for (i=1; i<localsize-1; i++) { j=(i-1)+mybase; localptr[i] = sin((PETSC_PI*j*6)/((PetscReal)M) + 1.2 * sin((PETSC_PI*j*2)/((PetscReal)M))) * 4+4; } ierr = VecRestoreArray(local,&localptr);CHKERRQ(ierr); ierr = VecRestoreArray(copy,©ptr);CHKERRQ(ierr); ierr = DALocalToGlobal(da,local,INSERT_VALUES,global);CHKERRQ(ierr); /* Assign Parameters */ h= 1.0/M; k= h*h/2.2; for (j=0; j<time_steps; j++) { /* Global to Local */ ierr = DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); /*Extract local array */ ierr = VecGetArray(local,&localptr);CHKERRQ(ierr); ierr = VecGetArray (copy,©ptr);CHKERRQ(ierr); /* Update Locally - Make array of new values */ /* Note: I don't do anything for the first and last entry */ for (i=1; i< localsize-1; i++) { copyptr[i] = localptr[i] + (k/(h*h)) * (localptr[i+1]-2.0*localptr[i]+localptr[i-1]); } ierr = VecRestoreArray(copy,©ptr);CHKERRQ(ierr); ierr = VecRestoreArray(local,&localptr);CHKERRQ(ierr); /* Local to Global */ ierr = DALocalToGlobal(da,copy,INSERT_VALUES,global);CHKERRQ(ierr); /* View Wave */ ierr = VecView(global,viewer);CHKERRQ(ierr); } ierr = PetscViewerDestroy(viewer);CHKERRQ(ierr); ierr = VecDestroy(copy);CHKERRQ(ierr); ierr = VecDestroy(local);CHKERRQ(ierr); ierr = VecDestroy(global);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; }