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; }
PetscErrorCode interpPeriodicVector(PetscScalar tc, Vec *u, PetscScalar cyclePeriod, PetscInt numPeriods, PetscScalar *tdp, PeriodicVec *user, char *filename) { /* Function to interpolate a vector that is periodic in time with period cyclePeriod. */ /* tc is the current time and numPeriods is the number of instances per period */ /* at which data are available (to be read from files). */ /* IMPORTANT: Vectors u0 and u1 MUST have been created and preallocated before */ /* calling this routine. Use VecDuplicate to do this. */ #include <math.h> PetscScalar t,t1; PetscInt im,it0,it1; /* static PetscInt iCurrTimeReadLast=-1; */ PetscErrorCode ierr; PetscScalar alpha[2]; char tmpFile[PETSC_MAX_PATH_LEN]; PetscViewer fd; if (user->firstTime) { user->numPeriods = numPeriods; ierr = VecDuplicateVecs(*u,numPeriods,&user->up);CHKERRQ(ierr); for (im=0; im<numPeriods; im++) { strcpy(tmpFile,""); sprintf(tmpFile,"%s%02d",filename,im); ierr = PetscPrintf(PETSC_COMM_WORLD,"Reading vector from file %s\n", tmpFile);CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,tmpFile,FILE_MODE_READ,&fd);CHKERRQ(ierr); ierr = VecLoadIntoVector(fd,user->up[im]);CHKERRQ(ierr); ierr = PetscViewerDestroy(fd);CHKERRQ(ierr); } user->firstTime = PETSC_FALSE; } t=tc; /* current time */ if (t<0.) t=cyclePeriod+t; t1=t-cyclePeriod*floor(t/cyclePeriod); ierr=calcPeriodicInterpFactor(numPeriods,t1,tdp,&it0,&it1,&alpha[0],&alpha[1]); CHKERRQ(ierr); /* ierr = PetscPrintf(PETSC_COMM_WORLD,"tc=%lf,t1=%lf,it0=%d,it1=%d,a1=%17.16lf,a2=%17.16lf\n",tc,t1,it0,it1,alpha[0],alpha[1]);CHKERRQ(ierr); */ /* interpolate to current time */ ierr = VecAXPBYmy(alpha[0],alpha[1],user->up[it0],user->up[it1],u);CHKERRQ(ierr); return 0; }