static char help[] = "Solves 1D wave equation using multigrid.\n\n"; #include "petscda.h" #include "petscksp.h" #include "petscdmmg.h" extern PetscErrorCode ComputeJacobian(DMMG,Mat,Mat); extern PetscErrorCode ComputeRHS(DMMG,Vec); extern PetscErrorCode ComputeInitialSolution(DMMG*); #undef __FUNCT__ #define __FUNCT__ "main" int main(int argc,char **argv) { PetscErrorCode ierr; PetscInt i; DMMG *dmmg; PetscReal norm; DA da; PetscInitialize(&argc,&argv,(char *)0,help); ierr = DMMGCreate(PETSC_COMM_WORLD,3,PETSC_NULL,&dmmg);CHKERRQ(ierr); ierr = DACreate1d(PETSC_COMM_WORLD,DA_XPERIODIC,-3,2,1,0,&da);CHKERRQ(ierr); ierr = DMMGSetDM(dmmg,(DM)da);CHKERRQ(ierr); ierr = DADestroy(da);CHKERRQ(ierr); ierr = DMMGSetKSP(dmmg,ComputeRHS,ComputeJacobian);CHKERRQ(ierr); ierr = ComputeInitialSolution(dmmg);CHKERRQ(ierr); VecView(DMMGGetx(dmmg),PETSC_VIEWER_DRAW_WORLD); for (i=0; i<1000; i++) { ierr = DMMGSolve(dmmg);CHKERRQ(ierr); ierr = VecView(DMMGGetx(dmmg),PETSC_VIEWER_DRAW_WORLD);CHKERRQ(ierr); } ierr = MatMult(DMMGGetJ(dmmg),DMMGGetx(dmmg),DMMGGetr(dmmg));CHKERRQ(ierr); ierr = VecAXPY(DMMGGetr(dmmg),-1.0,DMMGGetRHS(dmmg));CHKERRQ(ierr); ierr = VecNorm(DMMGGetr(dmmg),NORM_2,&norm);CHKERRQ(ierr); /* ierr = PetscPrintf(PETSC_COMM_WORLD,"Residual norm %G\n",norm);CHKERRQ(ierr); */ ierr = DMMGDestroy(dmmg);CHKERRQ(ierr); ierr = PetscFinalize();CHKERRQ(ierr); return 0; } #undef __FUNCT__ #define __FUNCT__ "ComputeInitialSolution" PetscErrorCode ComputeInitialSolution(DMMG *dmmg) { PetscErrorCode ierr; PetscInt mx,col[2],xs,xm,i; PetscScalar Hx,val[2]; Vec x = DMMGGetx(dmmg); PetscFunctionBegin; ierr = DAGetInfo(DMMGGetDA(dmmg),0,&mx,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr); Hx = 2.0*PETSC_PI / (PetscReal)(mx); ierr = DAGetCorners(DMMGGetDA(dmmg),&xs,0,0,&xm,0,0);CHKERRQ(ierr); for(i=xs; idm,0,&mx,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr); h = 2.0*PETSC_PI/((mx)); ierr = VecCopy(dmmg->x,b);CHKERRQ(ierr); ierr = VecScale(b,h);CHKERRQ(ierr); PetscFunctionReturn(0); } #undef __FUNCT__ #define __FUNCT__ "ComputeJacobian" PetscErrorCode ComputeJacobian(DMMG dmmg,Mat J,Mat jac) { DA da = (DA)dmmg->dm; PetscErrorCode ierr; PetscInt i,mx,xm,xs; PetscScalar v[7],Hx; MatStencil row,col[7]; PetscScalar lambda; ierr = PetscMemzero(col,7*sizeof(MatStencil));CHKERRQ(ierr); ierr = DAGetInfo(da,0,&mx,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr); Hx = 2.0*PETSC_PI / (PetscReal)(mx); ierr = DAGetCorners(da,&xs,0,0,&xm,0,0);CHKERRQ(ierr); lambda = 2.0*Hx; for(i=xs; i