Actual source code: ex15.c

petsc-3.3-p7 2013-05-11
  1: /*$Id: ex15.c,v 1.50 2002/09/04 07:43:58 knepley Exp $*/

  3: static char help[] = "Tests Mathematica I/O of vectors and illustrates the use of user-defined event logging.\n\n";

  5: #include <petscvec.h>

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

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

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

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

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

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

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

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

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

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

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