1 static char help[] = "Demonstrates using PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB)\n\n"; 2 3 #include <petscsys.h> 4 #include <petscdm.h> 5 #include <petscdmda.h> 6 #include <petscbag.h> 7 8 typedef struct { 9 char filename[PETSC_MAX_PATH_LEN]; 10 PetscReal ra; 11 PetscInt ia; 12 PetscBool ta; 13 } Parameter; 14 15 int main(int argc,char **argv) 16 { 17 PetscBag bag; 18 Parameter *params; 19 PetscViewer viewer; 20 DM da; 21 Vec global,local; 22 PetscMPIInt rank; 23 24 /* 25 Every PETSc routine should begin with the PetscInitialize() routine. 26 argc, argv - These command line arguments are taken to extract the options 27 supplied to PETSc and options supplied to MPI. 28 help - When PETSc executable is invoked with the option -help, 29 it prints the various options that can be applied at 30 runtime. The user can use the "help" variable place 31 additional help messages in this printout. 32 */ 33 PetscFunctionBeginUser; 34 PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); 35 /* Create a DMDA and an associated vector */ 36 PetscCall(DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,10,10,PETSC_DECIDE,PETSC_DECIDE,2,1,NULL,NULL,&da)); 37 PetscCall(DMSetFromOptions(da)); 38 PetscCall(DMSetUp(da)); 39 PetscCall(DMCreateGlobalVector(da,&global)); 40 PetscCall(DMCreateLocalVector(da,&local)); 41 PetscCall(VecSet(global,-1.0)); 42 PetscCall(DMGlobalToLocalBegin(da,global,INSERT_VALUES,local)); 43 PetscCall(DMGlobalToLocalEnd(da,global,INSERT_VALUES,local)); 44 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 45 PetscCall(VecScale(local,rank+1)); 46 PetscCall(DMLocalToGlobalBegin(da,local,ADD_VALUES,global)); 47 PetscCall(DMLocalToGlobalEnd(da,local,ADD_VALUES,global)); 48 49 /* Create an empty bag */ 50 PetscCall(PetscBagCreate(PETSC_COMM_WORLD,sizeof(Parameter),&bag)); 51 PetscCall(PetscBagGetData(bag,(void**)¶ms)); 52 53 /* fill bag: register variables, defaults, names, help strings */ 54 PetscCall(PetscBagSetName(bag,"ParameterBag","contains problem parameters")); 55 PetscCall(PetscBagRegisterString(bag,¶ms->filename,PETSC_MAX_PATH_LEN,"output_file","filename","Name of secret file")); 56 PetscCall(PetscBagRegisterReal (bag,¶ms->ra,1.0,"param_1","The first parameter")); 57 PetscCall(PetscBagRegisterInt (bag,¶ms->ia,5,"param_2","The second parameter")); 58 PetscCall(PetscBagRegisterBool (bag,¶ms->ta,PETSC_TRUE,"do_output","Write output file (true/false)")); 59 60 /* 61 Write output file with PETSC_VIEWER_BINARY_MATLAB format 62 NOTE: the output generated with this viewer can be loaded into 63 MATLAB using $PETSC_DIR/share/petsc/matlab/PetscReadBinaryMatlab.m 64 */ 65 PetscCall(PetscViewerBinaryOpen(PETSC_COMM_WORLD,params->filename,FILE_MODE_WRITE,&viewer)); 66 PetscCall(PetscViewerPushFormat(viewer,PETSC_VIEWER_BINARY_MATLAB)); 67 PetscCall(PetscBagView(bag,viewer)); 68 PetscCall(DMDASetFieldName(da,0,"field1")); 69 PetscCall(DMDASetFieldName(da,1,"field2")); 70 PetscCall(PetscObjectSetName((PetscObject)global,"da1")); 71 PetscCall(VecView(global,viewer)); 72 PetscCall(PetscViewerPopFormat(viewer)); 73 PetscCall(PetscViewerDestroy(&viewer)); 74 75 /* clean up and exit */ 76 PetscCall(PetscBagDestroy(&bag)); 77 PetscCall(DMDestroy(&da)); 78 PetscCall(VecDestroy(&local)); 79 PetscCall(VecDestroy(&global)); 80 PetscCall(PetscFinalize()); 81 return 0; 82 } 83 84 /*TEST 85 86 test: 87 88 TEST*/ 89