Actual source code: ex15.c
petsc-3.8.4 2018-03-24
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. */
10: int main(int argc, char *argv[])
11: {
12: PetscViewer viewer;
13: Vec u;
14: PetscScalar v;
15: int VECTOR_GENERATE, VECTOR_READ;
16: int i, m = 10, rank, size, low, high, ldim, iglobal;
17: int ierr;
19: PetscInitialize(&argc, &argv, NULL, help);
20: MPI_Comm_rank(PETSC_COMM_WORLD, &rank);
21: MPI_Comm_size(PETSC_COMM_WORLD, &size);
22: PetscOptionsGetInt(NULL,NULL, "-m", &m, NULL);
24: /* PART 1: Generate vector, then write it to Mathematica */
26: PetscLogEventRegister("Generate Vector", VEC_CLASSID,&VECTOR_GENERATE);
27: PetscLogEventBegin(VECTOR_GENERATE, 0, 0, 0, 0);
28: /* Generate vector */
29: VecCreate(PETSC_COMM_WORLD, &u);
30: VecSetSizes(u, PETSC_DECIDE, m);
31: VecSetFromOptions(u);
32: VecGetOwnershipRange(u, &low, &high);
33: VecGetLocalSize(u, &ldim);
34: for (i = 0; i < ldim; i++) {
35: iglobal = i + low;
36: v = (PetscScalar) (i + 100*rank);
37: VecSetValues(u, 1, &iglobal, &v, INSERT_VALUES);
38: }
39: VecAssemblyBegin(u);
40: VecAssemblyEnd(u);
41: VecView(u, PETSC_VIEWER_STDOUT_WORLD);
43: PetscPrintf(PETSC_COMM_WORLD, "writing vector to Mathematica...\n");
45: #if 0
46: PetscViewerMathematicaOpen(PETSC_COMM_WORLD, 8000, "192.168.119.1", "Connect", &viewer);
47: VecView(u, viewer);
48: #else
49: VecView(u, PETSC_VIEWER_MATHEMATICA_WORLD);
50: #endif
51: v = 0.0;
52: VecSet(u,v);
53: PetscLogEventEnd(VECTOR_GENERATE, 0, 0, 0, 0);
55: /* All processors wait until test vector has been dumped */
56: MPI_Barrier(PETSC_COMM_WORLD);
57: PetscSleep(10);
59: /* PART 2: Read in vector in from Mathematica */
61: PetscLogEventRegister("Read Vector", VEC_CLASSID,&VECTOR_READ);
62: PetscLogEventBegin(VECTOR_READ, 0, 0, 0, 0);
63: PetscPrintf(PETSC_COMM_WORLD, "reading vector from Mathematica...\n");
64: /* Read new vector in binary format */
65: #if 0
66: PetscViewerMathematicaGetVector(viewer, u);
67: PetscViewerDestroy(&viewer);
68: #else
69: PetscViewerMathematicaGetVector(PETSC_VIEWER_MATHEMATICA_WORLD, u);
70: #endif
71: PetscLogEventEnd(VECTOR_READ, 0, 0, 0, 0);
72: VecView(u, PETSC_VIEWER_STDOUT_WORLD);
74: /* Free data structures */
75: VecDestroy(&u);
76: PetscFinalize();
77: return ierr;
78: }