Actual source code: ex61gen.c

petsc-3.3-p7 2013-05-11
  1: static char help[] = "Generates random numbers for radioactive events for ex61.c.\n ./ex61gen [-random_seed <int>] \n";



 5:  #include petscsys.h
 6:  #include petscvec.h


 11: int main(int argc, char **argv)
 12: {
 13:   PetscErrorCode      ierr;
 14:   Vec                 x;
 15:   PetscRandom         rand;
 16:   PetscScalar         *values;
 17:   PetscInt            n = 10000,seed;
 18:   PetscViewer         viewer;
 19:   char                filename[PETSC_MAX_PATH_LEN];
 20:   PetscBool           flg;

 22:   PetscInitialize(&argc,&argv, (char*)0, help);
 23:   PetscRandomCreate(PETSC_COMM_WORLD,&rand);
 24:   PetscRandomSetFromOptions(rand);
 25:   VecCreateSeq(PETSC_COMM_SELF,4*n,&x);
 26:   VecSetRandom(x,rand);
 27:   PetscRandomDestroy(&rand);
 28:   VecGetArray(x,&values);
 29:   PetscOptionsGetInt(PETSC_NULL,"-random_seed",&seed,&flg);
 30:   if (flg) {
 31:     sprintf(filename,"ex61.random.%d",(int)seed);
 32:   } else {
 33:     PetscStrcpy(filename,"ex61.random");
 34:   }
 35:   PetscViewerBinaryOpen(PETSC_COMM_SELF,filename,FILE_MODE_WRITE,&viewer);
 36:   PetscViewerBinaryWrite(viewer,&n,1,PETSC_INT,PETSC_FALSE);
 37:   PetscViewerBinaryWrite(viewer,values,4*n,PETSC_DOUBLE,PETSC_TRUE);
 38:   PetscViewerDestroy(&viewer);
 39:   VecRestoreArray(x,&values);
 40:   VecDestroy(&x);
 41:   PetscFinalize();
 42:   return 0;
 43: }