Actual source code: mathematica.c

  1: #include <petsc/private/viewerimpl.h>
  2: #include <mathematica.h>

  4: #if PetscDefined(HAVE__SNPRINTF) && !PetscDefined(HAVE_SNPRINTF)
  5:   #define snprintf _snprintf
  6: #endif

  8: PetscViewer  PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE = NULL;
  9: static void *mathematicaEnv                         = NULL;

 11: static PetscBool PetscViewerMathematicaPackageInitialized = PETSC_FALSE;
 12: /*@
 13:   PetscViewerMathematicaFinalizePackage - This function destroys everything in the PETSc interface to Mathematica. It is
 14:   called from PetscFinalize().

 16:   Level: developer

 18: .seealso: `PetscFinalize()`
 19: @*/
 20: PetscErrorCode PetscViewerMathematicaFinalizePackage(void)
 21: {
 22:   PetscFunctionBegin;
 23:   if (mathematicaEnv) MLDeinitialize((MLEnvironment)mathematicaEnv);
 24:   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
 25:   PetscFunctionReturn(PETSC_SUCCESS);
 26: }

 28: /*@
 29:   PetscViewerMathematicaInitializePackage - This function initializes everything in the PETSc interface to Mathematica. It is
 30:   called from `PetscViewerInitializePackage()`.

 32:   Level: developer

 34: .seealso: `PetscSysInitializePackage()`, `PetscInitialize()`
 35: @*/
 36: PetscErrorCode PetscViewerMathematicaInitializePackage(void)
 37: {
 38:   PetscError ierr;

 40:   PetscFunctionBegin;
 41:   if (PetscViewerMathematicaPackageInitialized) PetscFunctionReturn(PETSC_SUCCESS);
 42:   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;

 44:   mathematicaEnv = (void *)MLInitialize(0);

 46:   PetscCall(PetscRegisterFinalize(PetscViewerMathematicaFinalizePackage));
 47:   PetscFunctionReturn(PETSC_SUCCESS);
 48: }

 50: static PetscErrorCode PetscViewerInitializeMathematicaWorld_Private()
 51: {
 52:   PetscFunctionBegin;
 53:   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscFunctionReturn(PETSC_SUCCESS);
 54:   PetscCall(PetscViewerMathematicaOpen(PETSC_COMM_WORLD, PETSC_DECIDE, NULL, NULL, &PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE));
 55:   PetscFunctionReturn(PETSC_SUCCESS);
 56: }

 58: static PetscErrorCode PetscViewerDestroy_Mathematica(PetscViewer viewer)
 59: {
 60:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;

 62:   PetscFunctionBegin;
 63:   MLClose(vmath->link);
 64:   PetscCall(PetscFree(vmath->linkname));
 65:   PetscCall(PetscFree(vmath->linkhost));
 66:   PetscCall(PetscFree(vmath));
 67:   PetscFunctionReturn(PETSC_SUCCESS);
 68: }

 70: static PetscErrorCode PetscViewerDestroyMathematica_Private(void)
 71: {
 72:   PetscFunctionBegin;
 73:   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscCall(PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE));
 74:   PetscFunctionReturn(PETSC_SUCCESS);
 75: }

 77: static PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v)
 78: {
 79:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)v->data;
 80: #if defined(MATHEMATICA_3_0)
 81:   int   argc = 6;
 82:   char *argv[6];
 83: #else
 84:   int   argc = 5;
 85:   char *argv[5];
 86: #endif
 87:   char hostname[256];
 88:   long lerr;

 90:   PetscFunctionBegin;
 91:   /* Link name */
 92:   argv[0] = "-linkname";
 93:   if (!vmath->linkname) argv[1] = "math -mathlink";
 94:   else argv[1] = vmath->linkname;

 96:   /* Link host */
 97:   argv[2] = "-linkhost";
 98:   if (!vmath->linkhost) {
 99:     PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
100:     argv[3] = hostname;
101:   } else argv[3] = vmath->linkhost;

103:   /* Link mode */
104: #if defined(MATHEMATICA_3_0)
105:   argv[4] = "-linkmode";
106:   switch (vmath->linkmode) {
107:   case MATHEMATICA_LINK_CREATE:
108:     argv[5] = "Create";
109:     break;
110:   case MATHEMATICA_LINK_CONNECT:
111:     argv[5] = "Connect";
112:     break;
113:   case MATHEMATICA_LINK_LAUNCH:
114:     argv[5] = "Launch";
115:     break;
116:   }
117: #else
118:   switch (vmath->linkmode) {
119:   case MATHEMATICA_LINK_CREATE:
120:     argv[4] = "-linkcreate";
121:     break;
122:   case MATHEMATICA_LINK_CONNECT:
123:     argv[4] = "-linkconnect";
124:     break;
125:   case MATHEMATICA_LINK_LAUNCH:
126:     argv[4] = "-linklaunch";
127:     break;
128:   }
129: #endif
130:   vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr);
131: #endif
132:   PetscFunctionReturn(PETSC_SUCCESS);
133: }

135: PETSC_EXTERN PetscErrorCode PetscViewerCreate_Mathematica(PetscViewer v)
136: {
137:   PetscViewer_Mathematica *vmath;

139:   PetscFunctionBegin;
140:   PetscCall(PetscViewerMathematicaInitializePackage());

142:   PetscCall(PetscNew(&vmath));
143:   v->data         = (void *)vmath;
144:   v->ops->destroy = PetscViewerDestroy_Mathematica;
145:   PetscCall(PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name));

147:   vmath->linkname     = NULL;
148:   vmath->linkhost     = NULL;
149:   vmath->linkmode     = MATHEMATICA_LINK_CONNECT;
150:   vmath->graphicsType = GRAPHICS_MOTIF;
151:   vmath->plotType     = MATHEMATICA_TRIANGULATION_PLOT;
152:   vmath->objName      = NULL;

154:   PetscCall(PetscViewerMathematicaSetFromOptions(v));
155:   PetscCall(PetscViewerMathematicaSetupConnection_Private(v));
156:   PetscFunctionReturn(PETSC_SUCCESS);
157: }

159: static PetscErrorCode PetscViewerMathematicaParseLinkMode(char *modename, LinkMode *mode)
160: {
161:   PetscBool isCreate, isConnect, isLaunch;

163:   PetscFunctionBegin;
164:   PetscCall(PetscStrcasecmp(modename, "Create", &isCreate));
165:   PetscCall(PetscStrcasecmp(modename, "Connect", &isConnect));
166:   PetscCall(PetscStrcasecmp(modename, "Launch", &isLaunch));
167:   if (isCreate) *mode = MATHEMATICA_LINK_CREATE;
168:   else if (isConnect) *mode = MATHEMATICA_LINK_CONNECT;
169:   else if (isLaunch) *mode = MATHEMATICA_LINK_LAUNCH;
170:   else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename);
171:   PetscFunctionReturn(PETSC_SUCCESS);
172: }

174: PetscErrorCode PetscViewerMathematicaSetFromOptions(PetscViewer v)
175: {
176:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)v->data;
177:   char                     linkname[256];
178:   char                     modename[256];
179:   char                     hostname[256];
180:   char                     type[256];
181:   PetscInt                 numPorts;
182:   PetscInt                *ports;
183:   PetscInt                 numHosts;
184:   int                      h;
185:   char                   **hosts;
186:   PetscMPIInt              size, rank;
187:   PetscBool                opt;

189:   PetscFunctionBegin;
190:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size));
191:   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank));

193:   /* Get link name */
194:   PetscCall(PetscOptionsGetString("viewer_", "-math_linkname", linkname, sizeof(linkname), &opt));
195:   if (opt) PetscCall(PetscViewerMathematicaSetLinkName(v, linkname));
196:   /* Get link port */
197:   numPorts = size;
198:   PetscCall(PetscMalloc1(size, &ports));
199:   PetscCall(PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt));
200:   if (opt) {
201:     if (numPorts > rank) snprintf(linkname, sizeof(linkname), "%6d", ports[rank]);
202:     else snprintf(linkname, sizeof(linkname), "%6d", ports[0]);
203:     PetscCall(PetscViewerMathematicaSetLinkName(v, linkname));
204:   }
205:   PetscCall(PetscFree(ports));
206:   /* Get link host */
207:   numHosts = size;
208:   PetscCall(PetscMalloc1(size, &hosts));
209:   PetscCall(PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt));
210:   if (opt) {
211:     if (numHosts > rank) {
212:       PetscCall(PetscStrncpy(hostname, hosts[rank], sizeof(hostname)));
213:     } else {
214:       PetscCall(PetscStrncpy(hostname, hosts[0], sizeof(hostname)));
215:     }
216:     PetscCall(PetscViewerMathematicaSetLinkHost(v, hostname));
217:   }
218:   for (h = 0; h < numHosts; h++) PetscCall(PetscFree(hosts[h]));
219:   PetscCall(PetscFree(hosts));
220:   /* Get link mode */
221:   PetscCall(PetscOptionsGetString("viewer_", "-math_linkmode", modename, sizeof(modename), &opt));
222:   if (opt) {
223:     LinkMode mode;

225:     PetscCall(PetscViewerMathematicaParseLinkMode(modename, &mode));
226:     PetscCall(PetscViewerMathematicaSetLinkMode(v, mode));
227:   }
228:   /* Get graphics type */
229:   PetscCall(PetscOptionsGetString("viewer_", "-math_graphics", type, sizeof(type), &opt));
230:   if (opt) {
231:     PetscBool isMotif, isPS, isPSFile;

233:     PetscCall(PetscStrcasecmp(type, "Motif", &isMotif));
234:     PetscCall(PetscStrcasecmp(type, "PS", &isPS));
235:     PetscCall(PetscStrcasecmp(type, "PSFile", &isPSFile));
236:     if (isMotif) vmath->graphicsType = GRAPHICS_MOTIF;
237:     else if (isPS) vmath->graphicsType = GRAPHICS_PS_STDOUT;
238:     else if (isPSFile) vmath->graphicsType = GRAPHICS_PS_FILE;
239:   }
240:   /* Get plot type */
241:   PetscCall(PetscOptionsGetString("viewer_", "-math_type", type, sizeof(type), &opt));
242:   if (opt) {
243:     PetscBool isTri, isVecTri, isVec, isSurface;

245:     PetscCall(PetscStrcasecmp(type, "Triangulation", &isTri));
246:     PetscCall(PetscStrcasecmp(type, "VectorTriangulation", &isVecTri));
247:     PetscCall(PetscStrcasecmp(type, "Vector", &isVec));
248:     PetscCall(PetscStrcasecmp(type, "Surface", &isSurface));
249:     if (isTri) vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT;
250:     else if (isVecTri) vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT;
251:     else if (isVec) vmath->plotType = MATHEMATICA_VECTOR_PLOT;
252:     else if (isSurface) vmath->plotType = MATHEMATICA_SURFACE_PLOT;
253:   }
254:   PetscFunctionReturn(PETSC_SUCCESS);
255: }

257: PetscErrorCode PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name)
258: {
259:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)v->data;

261:   PetscFunctionBegin;
263:   PetscAssertPointer(name, 2);
264:   PetscCall(PetscStrallocpy(name, &vmath->linkname));
265:   PetscFunctionReturn(PETSC_SUCCESS);
266: }

268: PetscErrorCode PetscViewerMathematicaSetLinkPort(PetscViewer v, int port)
269: {
270:   char name[16];

272:   PetscFunctionBegin;
273:   snprintf(name, 16, "%6d", port);
274:   PetscCall(PetscViewerMathematicaSetLinkName(v, name));
275:   PetscFunctionReturn(PETSC_SUCCESS);
276: }

278: PetscErrorCode PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host)
279: {
280:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)v->data;

282:   PetscFunctionBegin;
284:   PetscAssertPointer(host, 2);
285:   PetscCall(PetscStrallocpy(host, &vmath->linkhost));
286:   PetscFunctionReturn(PETSC_SUCCESS);
287: }

289: PetscErrorCode PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode)
290: {
291:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)v->data;

293:   PetscFunctionBegin;
294:   vmath->linkmode = mode;
295:   PetscFunctionReturn(PETSC_SUCCESS);
296: }

298: /*@
299:   PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink.

301:   Collective

303:   Input Parameters:
304: + comm    - The MPI communicator
305: . port    - [optional] The port to connect on, or PETSC_DECIDE
306: . machine - [optional] The machine to run Mathematica on, or NULL
307: - mode    - [optional] The connection mode, or NULL

309:   Output Parameter:
310: . v - The Mathematica viewer

312:   Options Database Keys:
313: + -viewer_math_linkhost machine - The host machine for the kernel
314: . -viewer_math_linkname name    - The full link name for the connection
315: . -viewer_math_linkport port    - The port for the connection
316: . -viewer_math_linkmode mode    - The mode, e.g. Launch, Connect
317: . -viewer_math_type type        - The plot type, e.g. Triangulation, Vector
318: - -viewer_math_graphics output  - The output type, e.g. Motif, PS, PSFile

320:   Level: intermediate

322:   Note:
323:   Most users should employ the following commands to access the
324:   Mathematica viewers
325: .vb
326:     PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
327:     MatView(Mat matrix, PetscViewer viewer)

329:                 or

331:     PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
332:     VecView(Vec vector, PetscViewer viewer)
333: .ve

335: .seealso: `PETSCVIEWERMATHEMATICA`, `MatView()`, `VecView()`
336: @*/
337: PetscErrorCode PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v)
338: {
339:   PetscFunctionBegin;
340:   PetscCall(PetscViewerCreate(comm, v));
341: #if 0
342:   LinkMode linkmode;
343:   PetscCall(PetscViewerMathematicaSetLinkPort(*v, port));
344:   PetscCall(PetscViewerMathematicaSetLinkHost(*v, machine));
345:   PetscCall(PetscViewerMathematicaParseLinkMode(mode, &linkmode));
346:   PetscCall(PetscViewerMathematicaSetLinkMode(*v, linkmode));
347: #endif
348:   PetscCall(PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA));
349:   PetscFunctionReturn(PETSC_SUCCESS);
350: }

352: /*
353:   PetscViewerMathematicaGetLink - Returns the link to Mathematica from a `PETSCVIEWERMATHEMATICA`

355:   Input Parameters:
356: + viewer - The Mathematica viewer
357: - link   - The link to Mathematica

359:   Level: intermediate

361: .seealso: `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaOpen()`
362: */
363: static PetscErrorCode PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link)
364: {
365:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;

367:   PetscFunctionBegin;
369:   *link = vmath->link;
370:   PetscFunctionReturn(PETSC_SUCCESS);
371: }

373: /*@
374:   PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received

376:   Input Parameters:
377: + viewer - The Mathematica viewer
378: - type   - The packet type to search for, e.g RETURNPKT

380:   Level: advanced

382: .seealso: `PetscViewerMathematicaSetName()`
383: @*/
384: PetscErrorCode PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type)
385: {
386:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;
387:   MLINK                    link  = vmath->link; /* The link to Mathematica */
388:   int                      pkt;                 /* The packet type */

390:   PetscFunctionBegin;
391:   while ((pkt = MLNextPacket(link)) && (pkt != type)) MLNewPacket(link);
392:   if (!pkt) {
393:     MLClearError(link);
394:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, (char *)MLErrorMessage(link));
395:   }
396:   PetscFunctionReturn(PETSC_SUCCESS);
397: }

399: /*@
400:   PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica via `PETSCVIEWERMATHEMATICA`

402:   Input Parameter:
403: . viewer - The Mathematica viewer

405:   Output Parameter:
406: . name - The name for new objects created in Mathematica

408:   Level: intermediate

410: .seealso: `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaSetName()`, `PetscViewerMathematicaClearName()`
411: @*/
412: PetscErrorCode PetscViewerMathematicaGetName(PetscViewer viewer, const char *name[])
413: {
414:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;

416:   PetscFunctionBegin;
418:   PetscAssertPointer(name, 2);
419:   *name = vmath->objName;
420:   PetscFunctionReturn(PETSC_SUCCESS);
421: }

423: /*@
424:   PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica via `PETSCVIEWERMATHEMATICA`

426:   Input Parameters:
427: + viewer - The Mathematica viewer
428: - name   - The name for new objects created in Mathematica

430:   Level: intermediate

432: .seealso: `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaClearName()`
433: @*/
434: PetscErrorCode PetscViewerMathematicaSetName(PetscViewer viewer, const char name[])
435: {
436:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;

438:   PetscFunctionBegin;
440:   PetscAssertPointer(name, 2);
441:   vmath->objName = name;
442:   PetscFunctionReturn(PETSC_SUCCESS);
443: }

445: /*@
446:   PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica

448:   Input Parameter:
449: . viewer - The Mathematica viewer

451:   Level: intermediate

453: .seealso: `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaGetName()`, `PetscViewerMathematicaSetName()`
454: @*/
455: PetscErrorCode PetscViewerMathematicaClearName(PetscViewer viewer)
456: {
457:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;

459:   PetscFunctionBegin;
461:   vmath->objName = NULL;
462:   PetscFunctionReturn(PETSC_SUCCESS);
463: }

465: /*@
466:   PetscViewerMathematicaPutMatrix - Send a dense matrix to a Mathematica kernel as a two-dimensional array
467:   bound to a symbol in the kernel's namespace.

469:   Not Collective

471:   Input Parameters:
472: + viewer - the `PETSCVIEWERMATHEMATICA` viewer
473: . m      - the number of rows
474: . n      - the number of columns
475: - a      - the matrix values, in column-major order, of length `m` * `n`

477:   Level: developer

479:   Note:
480:   The Mathematica symbol name defaults to `mat` and can be changed with `PetscViewerMathematicaSetName()`.

482: .seealso: `PetscViewer`, `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaPutCSRMatrix()`, `PetscViewerMathematicaSetName()`
483: @*/
484: PetscErrorCode PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a)
485: {
486:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;
487:   MLINK                    link  = vmath->link; /* The link to Mathematica */
488:   char                    *name;

490:   PetscFunctionBegin;
491:   /* Determine the object name */
492:   if (!vmath->objName) name = "mat";
493:   else name = (char *)vmath->objName;

495:   /* Send the dense matrix object */
496:   MLPutFunction(link, "EvaluatePacket", 1);
497:   MLPutFunction(link, "Set", 2);
498:   MLPutSymbol(link, name);
499:   MLPutFunction(link, "Transpose", 1);
500:   MLPutFunction(link, "Partition", 2);
501:   MLPutRealList(link, a, m * n);
502:   MLPutInteger(link, m);
503:   MLEndPacket(link);
504:   /* Skip packets until ReturnPacket */
505:   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
506:   /* Skip ReturnPacket */
507:   MLNewPacket(link);
508:   PetscFunctionReturn(PETSC_SUCCESS);
509: }

511: /*@
512:   PetscViewerMathematicaPutCSRMatrix - Send a sparse matrix in CSR format to a Mathematica kernel as a
513:   LinearAlgebraCSRMatrix bound to a symbol in the kernel's namespace.

515:   Not Collective

517:   Input Parameters:
518: + viewer - the `PETSCVIEWERMATHEMATICA` viewer
519: . m      - the number of rows
520: . n      - the number of columns
521: . i      - CSR row pointers of length `m + 1`
522: . j      - CSR column indices
523: - a      - CSR nonzero values

525:   Level: developer

527:   Note:
528:   The Mathematica symbol name defaults to `mat` and can be changed with `PetscViewerMathematicaSetName()`.

530: .seealso: `PetscViewer`, `PETSCVIEWERMATHEMATICA`, `PetscViewerMathematicaPutMatrix()`, `PetscViewerMathematicaSetName()`
531: @*/
532: PetscErrorCode PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
533: {
534:   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *)viewer->data;
535:   MLINK                    link  = vmath->link; /* The link to Mathematica */
536:   const char              *symbol;
537:   char                    *name;
538:   PetscBool                match;

540:   PetscFunctionBegin;
541:   /* Determine the object name */
542:   if (!vmath->objName) name = "mat";
543:   else name = (char *)vmath->objName;

545:   /* Make sure Mathematica recognizes sparse matrices */
546:   MLPutFunction(link, "EvaluatePacket", 1);
547:   MLPutFunction(link, "Needs", 1);
548:   MLPutString(link, "LinearAlgebra`CSRMatrix`");
549:   MLEndPacket(link);
550:   /* Skip packets until ReturnPacket */
551:   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
552:   /* Skip ReturnPacket */
553:   MLNewPacket(link);

555:   /* Send the CSRMatrix object */
556:   MLPutFunction(link, "EvaluatePacket", 1);
557:   MLPutFunction(link, "Set", 2);
558:   MLPutSymbol(link, name);
559:   MLPutFunction(link, "CSRMatrix", 5);
560:   MLPutInteger(link, m);
561:   MLPutInteger(link, n);
562:   MLPutFunction(link, "Plus", 2);
563:   MLPutIntegerList(link, i, m + 1);
564:   MLPutInteger(link, 1);
565:   MLPutFunction(link, "Plus", 2);
566:   MLPutIntegerList(link, j, i[m]);
567:   MLPutInteger(link, 1);
568:   MLPutRealList(link, a, i[m]);
569:   MLEndPacket(link);
570:   /* Skip packets until ReturnPacket */
571:   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
572:   /* Skip ReturnPacket */
573:   MLNewPacket(link);

575:   /* Check that matrix is valid */
576:   MLPutFunction(link, "EvaluatePacket", 1);
577:   MLPutFunction(link, "ValidQ", 1);
578:   MLPutSymbol(link, name);
579:   MLEndPacket(link);
580:   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
581:   MLGetSymbol(link, &symbol);
582:   PetscCall(PetscStrcmp("True", (char *)symbol, &match));
583:   if (!match) {
584:     MLDisownSymbol(link, symbol);
585:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
586:   }
587:   MLDisownSymbol(link, symbol);
588:   /* Skip ReturnPacket */
589:   MLNewPacket(link);
590:   PetscFunctionReturn(PETSC_SUCCESS);
591: }