void PETSC_STDCALL petscbagload_(PetscViewer *viewer,PetscBag *bag,PetscErrorCode *ierr) { PetscViewer v; PetscPatchDefaultViewers_Fortran(viewer,v); *ierr = PetscBagLoad(v,*bag); }
int main(int argc,char **argv) { PetscErrorCode ierr; PetscBag bag; Parameter *params; PetscViewer viewer; PetscBool flg; char filename[PETSC_MAX_PATH_LEN] = "binaryoutput"; /* 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);if (ierr) return ierr; /* Create an empty bag */ ierr = PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag);CHKERRQ(ierr); ierr = PetscBagGetData(bag,(void**)¶ms);CHKERRQ(ierr); /* register variables, defaults, names, help strings */ ierr = PetscBagSetName(bag,"ParameterBag","contains parameters for simulations of top-secret, dangerous physics");CHKERRQ(ierr); ierr = PetscBagSetOptionsPrefix(bag, "pbag_");CHKERRQ(ierr); ierr = PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"myfile","filename","Name of secret file");CHKERRQ(ierr); ierr = PetscBagRegisterReal (bag,¶ms->rho,3.0,"rho","Density, kg/m^3");CHKERRQ(ierr); ierr = PetscBagRegisterScalar(bag,¶ms->W, 5.0,"W","Vertical velocity, m/sec");CHKERRQ(ierr); ierr = PetscBagRegisterInt (bag,¶ms->Ii, 2,"modes_x","Number of modes in x-direction");CHKERRQ(ierr); params->iarray[0] = 1; params->iarray[1] = 2; params->iarray[2] = 3; ierr = PetscBagRegisterIntArray(bag,¶ms->iarray, 3,"int_array","Int array with 3 locations");CHKERRQ(ierr); params->rarray[0] = -1.0; params->rarray[1] = -2.0; ierr = PetscBagRegisterRealArray(bag,¶ms->rarray, 2,"real_array","Real array with 2 locations");CHKERRQ(ierr); ierr = PetscBagRegisterBool (bag,¶ms->T, PETSC_FALSE,"do_output","Write output file (yes/no)");CHKERRQ(ierr); ierr = PetscBagRegisterBoolArray(bag,¶ms->Tarray, 3,"bool_array","Bool array with 3 locations");CHKERRQ(ierr); ierr = PetscBagRegisterEnum (bag,¶ms->dt, PetscDataTypes,(PetscEnum)PETSC_INT,"dt","meaningless datatype");CHKERRQ(ierr); ierr = PetscBagRegisterReal (bag,¶ms->pos.x1,1.0,"x1","x position");CHKERRQ(ierr); ierr = PetscBagRegisterReal (bag,¶ms->pos.x2,1.9,"x2","y position");CHKERRQ(ierr); ierr = PetscBagRegisterEnum (bag,¶ms->which, EnumeratedChoices, (PetscEnum)THAT, "choose","Express yourself by choosing among enumerated things");CHKERRQ(ierr); /* This option allows loading user-provided PetscBag */ ierr = PetscOptionsGetString(NULL,NULL,"-f",filename,sizeof(filename),&flg);CHKERRQ(ierr); if (!flg) { /* write bag to stdio & binary file */ ierr = PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); ierr = PetscBagView(bag,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = PetscMemzero(params,sizeof(Parameter));CHKERRQ(ierr); /* load bag from file & write to stdio */ ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,filename,FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = PetscBagLoad(viewer,bag);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscBagSetFromOptions(bag);CHKERRQ(ierr); ierr = PetscBagView(bag,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); /* reuse the parameter struct */ ierr = PetscBagGetData(bag,(void**)¶ms);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"The value of rho after loading is: %f\n",(double)params->rho);CHKERRQ(ierr); /* clean up and exit */ ierr = PetscBagDestroy(&bag);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }