Actual source code: ex15.c

petsc-3.6.1 2015-08-06
Report Typos and Errors
  2: static char help[] = "Tests Mathematica I/O of vectors and illustrates the use of user-defined event logging.\n\n";

  4: #include <petscvec.h>

  6: /* Note:  Most applications would not read and write a vector within
  7:   the same program.  This example is intended only to demonstrate
  8:   both input and output. */

 12: int main(int argc, char *argv[])
 13: {
 14:   PetscViewer viewer;
 15:   Vec         u;
 16:   PetscScalar v;
 17:   int         VECTOR_GENERATE, VECTOR_READ;
 18:   int         i, m = 10, rank, size, low, high, ldim, iglobal;
 19:   int         ierr;

 21:   PetscInitialize(&argc, &argv, NULL, help);
 22:   MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
 23:   MPI_Comm_size(PETSC_COMM_WORLD, &size);
 24:   PetscOptionsGetInt(NULL, "-m", &m, NULL);

 26:   /* PART 1:  Generate vector, then write it to Mathematica */

 28:   PetscLogEventRegister("Generate Vector", VEC_CLASSID,&VECTOR_GENERATE);
 29:   PetscLogEventBegin(VECTOR_GENERATE, 0, 0, 0, 0);
 30:   /* Generate vector */
 31:   VecCreate(PETSC_COMM_WORLD, &u);
 32:   VecSetSizes(u, PETSC_DECIDE, m);
 33:   VecSetFromOptions(u);
 34:   VecGetOwnershipRange(u, &low, &high);
 35:   VecGetLocalSize(u, &ldim);
 36:   for (i = 0; i < ldim; i++) {
 37:     iglobal = i + low;
 38:     v       = (PetscScalar) (i + 100*rank);
 39:     VecSetValues(u, 1, &iglobal, &v, INSERT_VALUES);
 40:   }
 41:   VecAssemblyBegin(u);
 42:   VecAssemblyEnd(u);
 43:   VecView(u, PETSC_VIEWER_STDOUT_WORLD);

 45:   PetscPrintf(PETSC_COMM_WORLD, "writing vector to Mathematica...\n");

 47: #if 0
 48:   PetscViewerMathematicaOpen(PETSC_COMM_WORLD, 8000, "192.168.119.1", "Connect", &viewer);
 49:   VecView(u, viewer);
 50: #else
 51:   VecView(u, PETSC_VIEWER_MATHEMATICA_WORLD);
 52: #endif
 53:   v    = 0.0;
 54:   VecSet(u,v);
 55:   PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0);

 57:   /* All processors wait until test vector has been dumped */
 58:   MPI_Barrier(PETSC_COMM_WORLD);
 59:   PetscSleep(10);

 61:   /* PART 2:  Read in vector in from Mathematica */

 63:   PetscLogEventRegister("Read Vector", VEC_CLASSID,&VECTOR_READ);
 64:   PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0);
 65:   PetscPrintf(PETSC_COMM_WORLD, "reading vector from Mathematica...\n");
 66:   /* Read new vector in binary format */
 67: #if 0
 68:   PetscViewerMathematicaGetVector(viewer, u);
 69:   PetscViewerDestroy(&viewer);
 70: #else
 71:   PetscViewerMathematicaGetVector(PETSC_VIEWER_MATHEMATICA_WORLD, u);
 72: #endif
 73:   PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0);
 74:   VecView(u, PETSC_VIEWER_STDOUT_WORLD);

 76:   /* Free data structures */
 77:   VecDestroy(&u);
 78:   PetscFinalize();
 79:   return 0;
 80: }