Actual source code: ex9.c
1: static char help[] = "Demonstrates HD5 vector input/ouput\n\n";
3: /*T
4: Concepts: viewers; HDF5
5: Processors: n
6: T*/
7: #include petscsys.h
8: #include petscda.h
13: int main(int argc,char **argv)
14: {
16: PetscViewer viewer;
17: DA da;
18: Vec global,local,global2;
19: PetscMPIInt rank;
20: PetscTruth flg;
22: /*
23: Every PETSc routine should begin with the PetscInitialize() routine.
24: argc, argv - These command line arguments are taken to extract the options
25: supplied to PETSc and options supplied to MPI.
26: help - When PETSc executable is invoked with the option -help,
27: it prints the various options that can be applied at
28: runtime. The user can use the "help" variable place
29: additional help messages in this printout.
30: */
31: PetscInitialize(&argc,&argv,(char *)0,help);
33: /* Create a DA and an associated vector */
34: DACreate2d(PETSC_COMM_WORLD,DA_NONPERIODIC,DA_STENCIL_BOX,100,90,PETSC_DECIDE,PETSC_DECIDE,2,1,PETSC_NULL,PETSC_NULL,&da);
35: DACreateGlobalVector(da,&global);
36: DACreateLocalVector(da,&local);
37: VecSet(global,-1.0);
38: DAGlobalToLocalBegin(da,global,INSERT_VALUES,local);
39: DAGlobalToLocalEnd(da,global,INSERT_VALUES,local);
40: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
41: VecScale(local,rank+1);
42: DALocalToGlobal(da,local,ADD_VALUES,global);
44: /*
45: Write output file with PetscViewerHDF5 viewer.
47: */
48: PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_WRITE,&viewer);
49: VecView(global,viewer);
50: PetscViewerDestroy(viewer);
51:
52: VecDuplicate(global,&global2);
53: VecCopy(global,global2);
54: PetscViewerHDF5Open(PETSC_COMM_WORLD,"hdf5output",FILE_MODE_READ,&viewer);
55: VecLoadIntoVector(viewer,global);
56: PetscViewerDestroy(viewer);
57: VecEqual(global,global2,&flg);
58: if (flg) {
59: PetscPrintf(PETSC_COMM_WORLD,"Vectors are equal\n");
60: } else {
61: PetscPrintf(PETSC_COMM_WORLD,"Vectors are not equal\n");
62: }
64: /* clean up and exit */
65: DADestroy(da);
66: VecDestroy(local);
67: VecDestroy(global);
68: VecDestroy(global2);
69: PetscFinalize();
70: return 0;
71: }