int DoOutput(DMMG *dmmg, int n_plot) /*---------------------------------------------------------------------*/ { AppCtx *user = (AppCtx*)dmmg[0]->user; Parameter *param; int ierr; char filename[FNAME_LENGTH]; PetscViewer viewer; DA da; ierr = PetscBagGetData(user->bag,(void**)¶m); CHKERRQ(ierr); da = DMMGGetDA(dmmg); if (param->output_to_file) { /* send output to binary file */ /* generate filename for time t */ sprintf(filename,"%s_%3.3d",param->output_filename,n_plot); ierr = PetscPrintf(PETSC_COMM_WORLD,"Generating output: time t = %g, ",param->t); ierr = PetscPrintf(PETSC_COMM_WORLD,"file = \"%s\"\n",filename); /* make output files */ ierr = PetscViewerBinaryMatlabOpen(PETSC_COMM_WORLD,filename,&viewer); CHKERRQ(ierr); ierr = PetscViewerBinaryMatlabOutputBag(viewer,"par",user->bag); CHKERRQ(ierr); ierr = DASetFieldNames("u","v","phi",da); CHKERRQ(ierr); ierr = PetscViewerBinaryMatlabOutputVecDA(viewer,"field",DMMGGetx(dmmg),da); CHKERRQ(ierr); ierr = PetscViewerBinaryMatlabDestroy(viewer); CHKERRQ(ierr); } return 0; }
/* Visualize solutions */ PetscErrorCode MySolutionView(MPI_Comm comm,PetscInt phy_num,void *ctx) { PetscErrorCode ierr; AppCtx *user = (AppCtx*)ctx; DMMG *dmmg = user->dmmg; DA da=DMMGGetDA(dmmg); Field **x = user->x; Field1 **x1 = user->x1; PetscInt i,j,mx,xs,ys,xm,ym; PetscMPIInt size; PetscFunctionBegin; ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); ierr = DAGetCorners(da,&xs,&ys,PETSC_NULL,&xm,&ym,PETSC_NULL);CHKERRQ(ierr); switch (phy_num){ case 0: if (size == 1){ ierr = PetscPrintf(PETSC_COMM_SELF,"Original Physics %d U,V,Omega,Temp: \n",phy_num); ierr = PetscPrintf(PETSC_COMM_SELF,"-----------------------------------\n"); for (j=ys; j<ys+ym; j++) { for (i=xs; i<xs+xm; i++) { ierr = PetscPrintf(PETSC_COMM_SELF,"x[%d,%d] = %g, %g, %g, %g\n",j,i,x[j][i].u,x[j][i].v,x[j][i].omega,x[j][i].temp); } } } break; case 1: if (size == 1){ ierr = PetscPrintf(PETSC_COMM_SELF,"SubPhysics %d: U,V,Omega: \n",phy_num); ierr = PetscPrintf(PETSC_COMM_SELF,"------------------------\n"); DMMG *dmmg1=user->dmmg1; Vec solu_true = DMMGGetx(dmmg1); DA da=DMMGGetDA(dmmg1); Field1 **x1; ierr = DAVecGetArray(da,solu_true,&x1);CHKERRQ(ierr); for (j=ys; j<ys+ym; j++) { for (i=xs; i<xs+xm; i++) { ierr = PetscPrintf(PETSC_COMM_SELF,"x[%d,%d] = %g, %g, %g\n",j,i,x1[j][i].u,x1[j][i].v,x1[j][i].omega); } } ierr = DAVecRestoreArray(da,solu_true,&x1);CHKERRQ(ierr); } break; case 2: if (size == 1){ ierr = PetscPrintf(PETSC_COMM_SELF,"SubPhysics %d: Temperature: \n",phy_num); ierr = PetscPrintf(PETSC_COMM_SELF,"--------------------------\n"); DMMG *dmmg2=user->dmmg2; Vec solu_true = DMMGGetx(dmmg2); DA da=DMMGGetDA(dmmg2); Field2 **x2; ierr = DAVecGetArray(da,solu_true,&x2);CHKERRQ(ierr); for (j=ys; j<ys+ym; j++) { for (i=xs; i<xs+xm; i++) { ierr = PetscPrintf(PETSC_COMM_SELF,"x[%d,%d] = %g\n",j,i,x2[j][i].temp); } } ierr = DAVecRestoreArray(da,solu_true,&x2);CHKERRQ(ierr); } break; default: if (size == 1){ DMMG *dmmg_comp=user->dmmg_comp; DA da1,da2,da=DMMGGetDA(dmmg); Vec X1,X2,solu_true = DMMGGetx(dmmg); Field **x; Field1 **x1; Field2 **x2; DMComposite dm = (DMComposite)(*dmmg_comp)->dm; PetscReal err,err_tmp; if (phy_num == 3){ ierr = PetscPrintf(PETSC_COMM_SELF,"Composite physics %d, U,V,Omega,Temp: \n",phy_num); ierr = PetscPrintf(PETSC_COMM_SELF,"------------------------------------\n"); ierr = PetscPrintf(PETSC_COMM_SELF,"Composite physics, U,V,Omega,Temp: \n");CHKERRQ(ierr); } ierr = DAVecGetArray(da,solu_true,&x);CHKERRQ(ierr); ierr = DMCompositeGetEntries(dm,&da1,&da2);CHKERRQ(ierr); ierr = DMCompositeGetLocalVectors(dm,&X1,&X2);CHKERRQ(ierr); ierr = DAVecGetArray(da1,X1,(void**)&x1);CHKERRQ(ierr); ierr = DAVecGetArray(da2,X2,(void**)&x2);CHKERRQ(ierr); err = 0.0; for (j=ys; j<ys+ym; j++) { for (i=xs; i<xs+xm; i++) { err_tmp = PetscAbs(x[j][i].u-x1[j][i].u) + PetscAbs(x[j][i].v-x1[j][i].v) + PetscAbs(x[j][i].omega-x1[j][i].omega); err_tmp += PetscAbs(x[j][i].temp-x2[j][i].temp); if (err < err_tmp) err = err_tmp; if (phy_num == 3){ ierr = PetscPrintf(PETSC_COMM_SELF,"x[%d,%d] = %g, %g, %g, %g\n",j,i,x1[j][i].u,x1[j][i].v,x1[j][i].omega,x2[j][i].temp); } } } ierr = PetscPrintf(PETSC_COMM_SELF,"|solu - solu_comp| = %g\n",err);CHKERRQ(ierr); ierr = DAVecRestoreArray(da1,X1,(void**)&x1);CHKERRQ(ierr); ierr = DAVecRestoreArray(da2,X2,(void**)&x2);CHKERRQ(ierr); ierr = DMCompositeRestoreLocalVectors(dm,&X1,&X2);CHKERRQ(ierr); ierr = DAVecRestoreArray(da,solu_true,&x);CHKERRQ(ierr); } } PetscFunctionReturn(0); }
int DoSolve(DMMG *dmmg) /* ------------------------------------------------------------------- */ { AppCtx *user = (AppCtx*)dmmg[0]->user; Parameter *param; PetscReal t_output = 0.0; int ierr, n_plot = 0, Ncomponents, components[3]; DA da = DMMGGetDA(dmmg); Vec Xstar; Characteristic c; ierr = PetscBagGetData(user->bag,(void**)¶m); CHKERRQ(ierr); ierr = DAGetGlobalVector(da, &Xstar); CHKERRQ(ierr); /*------------ BEGIN CHARACTERISTIC SETUP ---------------*/ ierr = CharacteristicCreate(PETSC_COMM_WORLD, &c); CHKERRQ(ierr); /* set up the velocity interpolation system */ Ncomponents = 2; components[0] = 0; components[1] = 1; ierr = CharacteristicSetVelocityInterpolationLocal(c, da, DMMGGetx(dmmg), user->Xold, Ncomponents, components, InterpVelocity2D, user); CHKERRQ(ierr); /* set up the fields interpolation system */ Ncomponents = 1; components[0] = 2; ierr = CharacteristicSetFieldInterpolationLocal(c, da, user->Xold, Ncomponents, components, InterpFields2D, user); CHKERRQ(ierr); /*------------ END CHARACTERISTIC SETUP ----------------*/ /* output initial data */ PetscPrintf(PETSC_COMM_WORLD," Initialization, Time: %5.4g\n", param->t); if (param->verify) { ierr = DoVerification(dmmg,user); CHKERRQ(ierr); } ierr = DoOutput(dmmg,n_plot); CHKERRQ(ierr); t_output += param->t_output_interval; n_plot++; /* timestep loop */ for (param->t=param->dt; param->t<=param->t_max; param->t+=param->dt) { if (param->n > param->N_steps) { PetscPrintf(PETSC_COMM_WORLD,"EXCEEDED MAX NUMBER OF TIMESTEPS! EXITING SOLVE!\n"); return 0; } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve at time t & copy solution into solution vector. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ /* Copy in the velocities to Xstar */ ierr = VecCopy(DMMGGetx(dmmg), Xstar); CHKERRQ(ierr); /* Put \phi_* into Xstar */ ierr = CharacteristicSolve(c, param->dt, Xstar); CHKERRQ(ierr); /* Copy the advected field into the solution \phi_t = \phi_* */ ierr = VecCopy(Xstar, DMMGGetx(dmmg)); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Copy new solution to old solution in prep for the next timestep. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = VecCopy(DMMGGetx(dmmg), user->Xold); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Timestep complete, report and update counter. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscPrintf(PETSC_COMM_WORLD," Step: %d, Time: %5.4g\n", param->n, param->t); param->n++; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Verify and make output. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ if (param->verify) { ierr = DoVerification(dmmg,user); CHKERRQ(ierr); } if (param->t >= t_output) { ierr = DoOutput(dmmg,n_plot); CHKERRQ(ierr); t_output += param->t_output_interval; n_plot++; } } ierr = DARestoreGlobalVector(da, &Xstar); CHKERRQ(ierr); ierr = CharacteristicDestroy(c); CHKERRQ(ierr); return 0; }