xref: /petsc/src/sys/classes/viewer/impls/mathematica/mathematica.c (revision 1baa6e3354bfe224b33a0c158f482508792a8a6e)
15c6c1daeSBarry Smith 
2af0996ceSBarry Smith #include <petsc/private/viewerimpl.h>   /* "petscsys.h" */
3af0996ceSBarry Smith #include <petsc/private/pcimpl.h>
45c6c1daeSBarry Smith #include <../src/mat/impls/aij/seq/aij.h>
55c6c1daeSBarry Smith #include <mathematica.h>
65c6c1daeSBarry Smith 
75c6c1daeSBarry Smith #if defined(PETSC_HAVE__SNPRINTF) && !defined(PETSC_HAVE_SNPRINTF)
85c6c1daeSBarry Smith #define snprintf _snprintf
95c6c1daeSBarry Smith #endif
105c6c1daeSBarry Smith 
110298fd71SBarry Smith PetscViewer PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE = NULL;
120298fd71SBarry Smith static void *mathematicaEnv                        = NULL;
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith static PetscBool PetscViewerMathematicaPackageInitialized = PETSC_FALSE;
155c6c1daeSBarry Smith /*@C
165c6c1daeSBarry Smith   PetscViewerMathematicaFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
175c6c1daeSBarry Smith   called from PetscFinalize().
185c6c1daeSBarry Smith 
195c6c1daeSBarry Smith   Level: developer
205c6c1daeSBarry Smith 
21db781477SPatrick Sanan .seealso: `PetscFinalize()`
225c6c1daeSBarry Smith @*/
235c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaFinalizePackage(void)
245c6c1daeSBarry Smith {
255c6c1daeSBarry Smith   PetscFunctionBegin;
265c6c1daeSBarry Smith   if (mathematicaEnv) MLDeinitialize((MLEnvironment) mathematicaEnv);
275c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
285c6c1daeSBarry Smith   PetscFunctionReturn(0);
295c6c1daeSBarry Smith }
305c6c1daeSBarry Smith 
315c6c1daeSBarry Smith /*@C
325c6c1daeSBarry Smith   PetscViewerMathematicaInitializePackage - This function initializes everything in the Petsc interface to Mathematica. It is
338a690491SBarry Smith   called from PetscViewerInitializePackage().
345c6c1daeSBarry Smith 
355c6c1daeSBarry Smith   Level: developer
365c6c1daeSBarry Smith 
37db781477SPatrick Sanan .seealso: `PetscSysInitializePackage()`, `PetscInitialize()`
385c6c1daeSBarry Smith @*/
39607a6623SBarry Smith PetscErrorCode  PetscViewerMathematicaInitializePackage(void)
405c6c1daeSBarry Smith {
415c6c1daeSBarry Smith   PetscError ierr;
425c6c1daeSBarry Smith 
435c6c1daeSBarry Smith   PetscFunctionBegin;
445c6c1daeSBarry Smith   if (PetscViewerMathematicaPackageInitialized) PetscFunctionReturn(0);
455c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
46a297a907SKarl Rupp 
475c6c1daeSBarry Smith   mathematicaEnv = (void*) MLInitialize(0);
48a297a907SKarl Rupp 
499566063dSJacob Faibussowitsch   PetscCall(PetscRegisterFinalize(PetscViewerMathematicaFinalizePackage));
505c6c1daeSBarry Smith   PetscFunctionReturn(0);
515c6c1daeSBarry Smith }
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith PetscErrorCode PetscViewerInitializeMathematicaWorld_Private()
545c6c1daeSBarry Smith {
555c6c1daeSBarry Smith   PetscFunctionBegin;
565c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscFunctionReturn(0);
579566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaOpen(PETSC_COMM_WORLD, PETSC_DECIDE, NULL, NULL, &PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE));
585c6c1daeSBarry Smith   PetscFunctionReturn(0);
595c6c1daeSBarry Smith }
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Mathematica(PetscViewer viewer)
625c6c1daeSBarry Smith {
635c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
645c6c1daeSBarry Smith 
655c6c1daeSBarry Smith   PetscFunctionBegin;
665c6c1daeSBarry Smith   MLClose(vmath->link);
679566063dSJacob Faibussowitsch   PetscCall(PetscFree(vmath->linkname));
689566063dSJacob Faibussowitsch   PetscCall(PetscFree(vmath->linkhost));
699566063dSJacob Faibussowitsch   PetscCall(PetscFree(vmath));
705c6c1daeSBarry Smith   PetscFunctionReturn(0);
715c6c1daeSBarry Smith }
725c6c1daeSBarry Smith 
735c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroyMathematica_Private(void)
745c6c1daeSBarry Smith {
755c6c1daeSBarry Smith   PetscFunctionBegin;
76*1baa6e33SBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscCall(PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE));
775c6c1daeSBarry Smith   PetscFunctionReturn(0);
785c6c1daeSBarry Smith }
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v)
815c6c1daeSBarry Smith {
825c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
83519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
845c6c1daeSBarry Smith   int                     argc = 6;
855c6c1daeSBarry Smith   char                    *argv[6];
865c6c1daeSBarry Smith #else
875c6c1daeSBarry Smith   int                     argc = 5;
885c6c1daeSBarry Smith   char                    *argv[5];
895c6c1daeSBarry Smith #endif
905c6c1daeSBarry Smith   char                    hostname[256];
915c6c1daeSBarry Smith   long                    lerr;
925c6c1daeSBarry Smith 
935c6c1daeSBarry Smith   PetscFunctionBegin;
945c6c1daeSBarry Smith   /* Link name */
955c6c1daeSBarry Smith   argv[0] = "-linkname";
96a297a907SKarl Rupp   if (!vmath->linkname) argv[1] = "math -mathlink";
97a297a907SKarl Rupp   else                  argv[1] = vmath->linkname;
985c6c1daeSBarry Smith 
995c6c1daeSBarry Smith   /* Link host */
1005c6c1daeSBarry Smith   argv[2] = "-linkhost";
1015c6c1daeSBarry Smith   if (!vmath->linkhost) {
1029566063dSJacob Faibussowitsch     PetscCall(PetscGetHostName(hostname, sizeof(hostname)));
1035c6c1daeSBarry Smith     argv[3] = hostname;
104a297a907SKarl Rupp   } else argv[3] = vmath->linkhost;
1055c6c1daeSBarry Smith 
1065c6c1daeSBarry Smith   /* Link mode */
107519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
1085c6c1daeSBarry Smith   argv[4] = "-linkmode";
1095c6c1daeSBarry Smith   switch (vmath->linkmode) {
1105c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1115c6c1daeSBarry Smith     argv[5] = "Create";
1125c6c1daeSBarry Smith     break;
1135c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1145c6c1daeSBarry Smith     argv[5] = "Connect";
1155c6c1daeSBarry Smith     break;
1165c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1175c6c1daeSBarry Smith     argv[5] = "Launch";
1185c6c1daeSBarry Smith     break;
1195c6c1daeSBarry Smith   }
1205c6c1daeSBarry Smith #else
1215c6c1daeSBarry Smith   switch (vmath->linkmode) {
1225c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1235c6c1daeSBarry Smith     argv[4] = "-linkcreate";
1245c6c1daeSBarry Smith     break;
1255c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1265c6c1daeSBarry Smith     argv[4] = "-linkconnect";
1275c6c1daeSBarry Smith     break;
1285c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1295c6c1daeSBarry Smith     argv[4] = "-linklaunch";
1305c6c1daeSBarry Smith     break;
1315c6c1daeSBarry Smith   }
1325c6c1daeSBarry Smith #endif
1335c6c1daeSBarry Smith   vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr);
1345c6c1daeSBarry Smith #endif
1355c6c1daeSBarry Smith   PetscFunctionReturn(0);
1365c6c1daeSBarry Smith }
1375c6c1daeSBarry Smith 
1388cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Mathematica(PetscViewer v)
1395c6c1daeSBarry Smith {
1405c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath;
1415c6c1daeSBarry Smith 
1425c6c1daeSBarry Smith   PetscFunctionBegin;
1439566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaInitializePackage());
1445c6c1daeSBarry Smith 
1459566063dSJacob Faibussowitsch   PetscCall(PetscNewLog(v,&vmath));
1465c6c1daeSBarry Smith   v->data         = (void*) vmath;
1475c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_Mathematica;
1485c6c1daeSBarry Smith   v->ops->flush   = 0;
1499566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name));
1505c6c1daeSBarry Smith 
1510298fd71SBarry Smith   vmath->linkname     = NULL;
1520298fd71SBarry Smith   vmath->linkhost     = NULL;
1535c6c1daeSBarry Smith   vmath->linkmode     = MATHEMATICA_LINK_CONNECT;
1545c6c1daeSBarry Smith   vmath->graphicsType = GRAPHICS_MOTIF;
1555c6c1daeSBarry Smith   vmath->plotType     = MATHEMATICA_TRIANGULATION_PLOT;
1560298fd71SBarry Smith   vmath->objName      = NULL;
1575c6c1daeSBarry Smith 
1589566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetFromOptions(v));
1599566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetupConnection_Private(v));
1605c6c1daeSBarry Smith   PetscFunctionReturn(0);
1615c6c1daeSBarry Smith }
1625c6c1daeSBarry Smith 
16395c0884eSLisandro Dalcin static PetscErrorCode PetscViewerMathematicaParseLinkMode(char *modename, LinkMode *mode)
1640adebc6cSBarry Smith {
1655c6c1daeSBarry Smith   PetscBool      isCreate, isConnect, isLaunch;
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith   PetscFunctionBegin;
1689566063dSJacob Faibussowitsch   PetscCall(PetscStrcasecmp(modename, "Create",  &isCreate));
1699566063dSJacob Faibussowitsch   PetscCall(PetscStrcasecmp(modename, "Connect", &isConnect));
1709566063dSJacob Faibussowitsch   PetscCall(PetscStrcasecmp(modename, "Launch",  &isLaunch));
171a297a907SKarl Rupp   if (isCreate)       *mode = MATHEMATICA_LINK_CREATE;
172a297a907SKarl Rupp   else if (isConnect) *mode = MATHEMATICA_LINK_CONNECT;
173a297a907SKarl Rupp   else if (isLaunch)  *mode = MATHEMATICA_LINK_LAUNCH;
17498921bdaSJacob Faibussowitsch   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename);
1755c6c1daeSBarry Smith   PetscFunctionReturn(0);
1765c6c1daeSBarry Smith }
1775c6c1daeSBarry Smith 
1785c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetFromOptions(PetscViewer v)
1795c6c1daeSBarry Smith {
1805c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
1815c6c1daeSBarry Smith   char                    linkname[256];
1825c6c1daeSBarry Smith   char                    modename[256];
1835c6c1daeSBarry Smith   char                    hostname[256];
1845c6c1daeSBarry Smith   char                    type[256];
1855c6c1daeSBarry Smith   PetscInt                numPorts;
1865c6c1daeSBarry Smith   PetscInt                *ports;
1875c6c1daeSBarry Smith   PetscInt                numHosts;
1885c6c1daeSBarry Smith   int                     h;
1895c6c1daeSBarry Smith   char                    **hosts;
1905c6c1daeSBarry Smith   PetscMPIInt             size, rank;
1915c6c1daeSBarry Smith   PetscBool               opt;
1925c6c1daeSBarry Smith 
1935c6c1daeSBarry Smith   PetscFunctionBegin;
1949566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size));
1959566063dSJacob Faibussowitsch   PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank));
1965c6c1daeSBarry Smith 
1975c6c1daeSBarry Smith   /* Get link name */
1989566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString("viewer_", "-math_linkname", linkname, sizeof(linkname), &opt));
199*1baa6e33SBarry Smith   if (opt) PetscCall(PetscViewerMathematicaSetLinkName(v, linkname));
2005c6c1daeSBarry Smith   /* Get link port */
2015c6c1daeSBarry Smith   numPorts = size;
2029566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(size, &ports));
2039566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt));
2045c6c1daeSBarry Smith   if (opt) {
205589a23caSBarry Smith     if (numPorts > rank) snprintf(linkname, sizeof(linkname), "%6d", ports[rank]);
206589a23caSBarry Smith     else                 snprintf(linkname, sizeof(linkname), "%6d", ports[0]);
2079566063dSJacob Faibussowitsch     PetscCall(PetscViewerMathematicaSetLinkName(v, linkname));
2085c6c1daeSBarry Smith   }
2099566063dSJacob Faibussowitsch   PetscCall(PetscFree(ports));
2105c6c1daeSBarry Smith   /* Get link host */
2115c6c1daeSBarry Smith   numHosts = size;
2129566063dSJacob Faibussowitsch   PetscCall(PetscMalloc1(size, &hosts));
2139566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt));
2145c6c1daeSBarry Smith   if (opt) {
2155c6c1daeSBarry Smith     if (numHosts > rank) {
2169566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(hostname, hosts[rank], sizeof(hostname)));
2175c6c1daeSBarry Smith     } else {
2189566063dSJacob Faibussowitsch       PetscCall(PetscStrncpy(hostname, hosts[0], sizeof(hostname)));
2195c6c1daeSBarry Smith     }
2209566063dSJacob Faibussowitsch     PetscCall(PetscViewerMathematicaSetLinkHost(v, hostname));
2215c6c1daeSBarry Smith   }
2225c6c1daeSBarry Smith   for (h = 0; h < numHosts; h++) {
2239566063dSJacob Faibussowitsch     PetscCall(PetscFree(hosts[h]));
2245c6c1daeSBarry Smith   }
2259566063dSJacob Faibussowitsch   PetscCall(PetscFree(hosts));
2265c6c1daeSBarry Smith   /* Get link mode */
2279566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString("viewer_", "-math_linkmode", modename, sizeof(modename), &opt));
2285c6c1daeSBarry Smith   if (opt) {
2295c6c1daeSBarry Smith     LinkMode mode;
2305c6c1daeSBarry Smith 
2319566063dSJacob Faibussowitsch     PetscCall(PetscViewerMathematicaParseLinkMode(modename, &mode));
2329566063dSJacob Faibussowitsch     PetscCall(PetscViewerMathematicaSetLinkMode(v, mode));
2335c6c1daeSBarry Smith   }
2345c6c1daeSBarry Smith   /* Get graphics type */
2359566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString("viewer_", "-math_graphics", type, sizeof(type), &opt));
2365c6c1daeSBarry Smith   if (opt) {
2375c6c1daeSBarry Smith     PetscBool isMotif, isPS, isPSFile;
2385c6c1daeSBarry Smith 
2399566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "Motif",  &isMotif));
2409566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "PS",     &isPS));
2419566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "PSFile", &isPSFile));
242a297a907SKarl Rupp     if (isMotif)       vmath->graphicsType = GRAPHICS_MOTIF;
243a297a907SKarl Rupp     else if (isPS)     vmath->graphicsType = GRAPHICS_PS_STDOUT;
244a297a907SKarl Rupp     else if (isPSFile) vmath->graphicsType = GRAPHICS_PS_FILE;
2455c6c1daeSBarry Smith   }
2465c6c1daeSBarry Smith   /* Get plot type */
2479566063dSJacob Faibussowitsch   PetscCall(PetscOptionsGetString("viewer_", "-math_type", type, sizeof(type), &opt));
2485c6c1daeSBarry Smith   if (opt) {
2495c6c1daeSBarry Smith     PetscBool isTri, isVecTri, isVec, isSurface;
2505c6c1daeSBarry Smith 
2519566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "Triangulation",       &isTri));
2529566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "VectorTriangulation", &isVecTri));
2539566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "Vector",              &isVec));
2549566063dSJacob Faibussowitsch     PetscCall(PetscStrcasecmp(type, "Surface",             &isSurface));
255a297a907SKarl Rupp     if (isTri)          vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT;
256a297a907SKarl Rupp     else if (isVecTri)  vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT;
257a297a907SKarl Rupp     else if (isVec)     vmath->plotType = MATHEMATICA_VECTOR_PLOT;
258a297a907SKarl Rupp     else if (isSurface) vmath->plotType = MATHEMATICA_SURFACE_PLOT;
2595c6c1daeSBarry Smith   }
2605c6c1daeSBarry Smith   PetscFunctionReturn(0);
2615c6c1daeSBarry Smith }
2625c6c1daeSBarry Smith 
2630adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name)
2640adebc6cSBarry Smith {
2655c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
2665c6c1daeSBarry Smith 
2675c6c1daeSBarry Smith   PetscFunctionBegin;
2685c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
2695c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
2709566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(name, &vmath->linkname));
2715c6c1daeSBarry Smith   PetscFunctionReturn(0);
2725c6c1daeSBarry Smith }
2735c6c1daeSBarry Smith 
2740adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkPort(PetscViewer v, int port)
2750adebc6cSBarry Smith {
2765c6c1daeSBarry Smith   char           name[16];
2775c6c1daeSBarry Smith 
2785c6c1daeSBarry Smith   PetscFunctionBegin;
2795c6c1daeSBarry Smith   snprintf(name, 16, "%6d", port);
2809566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetLinkName(v, name));
2815c6c1daeSBarry Smith   PetscFunctionReturn(0);
2825c6c1daeSBarry Smith }
2835c6c1daeSBarry Smith 
2840adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host)
2850adebc6cSBarry Smith {
2865c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
2875c6c1daeSBarry Smith 
2885c6c1daeSBarry Smith   PetscFunctionBegin;
2895c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
2905c6c1daeSBarry Smith   PetscValidCharPointer(host,2);
2919566063dSJacob Faibussowitsch   PetscCall(PetscStrallocpy(host, &vmath->linkhost));
2925c6c1daeSBarry Smith   PetscFunctionReturn(0);
2935c6c1daeSBarry Smith }
2945c6c1daeSBarry Smith 
2950adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode)
2960adebc6cSBarry Smith {
2975c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
2985c6c1daeSBarry Smith 
2995c6c1daeSBarry Smith   PetscFunctionBegin;
3005c6c1daeSBarry Smith   vmath->linkmode = mode;
3015c6c1daeSBarry Smith   PetscFunctionReturn(0);
3025c6c1daeSBarry Smith }
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith /*----------------------------------------- Public Functions --------------------------------------------------------*/
3055c6c1daeSBarry Smith /*@C
3065c6c1daeSBarry Smith   PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink.
3075c6c1daeSBarry Smith 
308d083f849SBarry Smith   Collective
3095c6c1daeSBarry Smith 
3105c6c1daeSBarry Smith   Input Parameters:
3115c6c1daeSBarry Smith + comm    - The MPI communicator
3125c6c1daeSBarry Smith . port    - [optional] The port to connect on, or PETSC_DECIDE
3130298fd71SBarry Smith . machine - [optional] The machine to run Mathematica on, or NULL
3140298fd71SBarry Smith - mode    - [optional] The connection mode, or NULL
3155c6c1daeSBarry Smith 
3165c6c1daeSBarry Smith   Output Parameter:
3175c6c1daeSBarry Smith . viewer  - The Mathematica viewer
3185c6c1daeSBarry Smith 
3195c6c1daeSBarry Smith   Level: intermediate
3205c6c1daeSBarry Smith 
3215c6c1daeSBarry Smith   Notes:
3225c6c1daeSBarry Smith   Most users should employ the following commands to access the
3235c6c1daeSBarry Smith   Mathematica viewers
3245c6c1daeSBarry Smith $
3255c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
3265c6c1daeSBarry Smith $    MatView(Mat matrix, PetscViewer viewer)
3275c6c1daeSBarry Smith $
3285c6c1daeSBarry Smith $                or
3295c6c1daeSBarry Smith $
3305c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
3315c6c1daeSBarry Smith $    VecView(Vec vector, PetscViewer viewer)
3325c6c1daeSBarry Smith 
3335c6c1daeSBarry Smith    Options Database Keys:
334e1bc860dSBarry Smith +    -viewer_math_linkhost <machine> - The host machine for the kernel
335e1bc860dSBarry Smith .    -viewer_math_linkname <name>    - The full link name for the connection
336e1bc860dSBarry Smith .    -viewer_math_linkport <port>    - The port for the connection
337e1bc860dSBarry Smith .    -viewer_math_mode <mode>        - The mode, e.g. Launch, Connect
338e1bc860dSBarry Smith .    -viewer_math_type <type>        - The plot type, e.g. Triangulation, Vector
339e1bc860dSBarry Smith -    -viewer_math_graphics <output>  - The output type, e.g. Motif, PS, PSFile
3405c6c1daeSBarry Smith 
341db781477SPatrick Sanan .seealso: `MatView()`, `VecView()`
3425c6c1daeSBarry Smith @*/
3435c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v)
3445c6c1daeSBarry Smith {
3455c6c1daeSBarry Smith   PetscFunctionBegin;
3469566063dSJacob Faibussowitsch   PetscCall(PetscViewerCreate(comm, v));
3475c6c1daeSBarry Smith #if 0
3485c6c1daeSBarry Smith   LinkMode linkmode;
3499566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetLinkPort(*v, port));
3509566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetLinkHost(*v, machine));
3519566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaParseLinkMode(mode, &linkmode));
3529566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSetLinkMode(*v, linkmode));
3535c6c1daeSBarry Smith #endif
3549566063dSJacob Faibussowitsch   PetscCall(PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA));
3555c6c1daeSBarry Smith   PetscFunctionReturn(0);
3565c6c1daeSBarry Smith }
3575c6c1daeSBarry Smith 
3585c6c1daeSBarry Smith /*@C
3595c6c1daeSBarry Smith   PetscViewerMathematicaGetLink - Returns the link to Mathematica
3605c6c1daeSBarry Smith 
3615c6c1daeSBarry Smith   Input Parameters:
362a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
363a2b725a8SWilliam Gropp - link   - The link to Mathematica
3645c6c1daeSBarry Smith 
3655c6c1daeSBarry Smith   Level: intermediate
3665c6c1daeSBarry Smith 
3675c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, link
368db781477SPatrick Sanan .seealso `PetscViewerMathematicaOpen()`
3695c6c1daeSBarry Smith @*/
3705c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link)
3715c6c1daeSBarry Smith {
3725c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
3735c6c1daeSBarry Smith 
3745c6c1daeSBarry Smith   PetscFunctionBegin;
3755c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
3765c6c1daeSBarry Smith   *link = vmath->link;
3775c6c1daeSBarry Smith   PetscFunctionReturn(0);
3785c6c1daeSBarry Smith }
3795c6c1daeSBarry Smith 
3805c6c1daeSBarry Smith /*@C
3815c6c1daeSBarry Smith   PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith   Input Parameters:
384a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
385a2b725a8SWilliam Gropp - type   - The packet type to search for, e.g RETURNPKT
3865c6c1daeSBarry Smith 
3875c6c1daeSBarry Smith   Level: advanced
3885c6c1daeSBarry Smith 
3895c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, packets
390db781477SPatrick Sanan .seealso `PetscViewerMathematicaSetName()`, `PetscViewerMathematicaGetVector()`
3915c6c1daeSBarry Smith @*/
3925c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type)
3935c6c1daeSBarry Smith {
3945c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
3955c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
3965c6c1daeSBarry Smith   int                     pkt;                 /* The packet type */
3975c6c1daeSBarry Smith 
3985c6c1daeSBarry Smith   PetscFunctionBegin;
399a297a907SKarl Rupp   while ((pkt = MLNextPacket(link)) && (pkt != type)) MLNewPacket(link);
4005c6c1daeSBarry Smith   if (!pkt) {
4015c6c1daeSBarry Smith     MLClearError(link);
4025c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char*) MLErrorMessage(link));
4035c6c1daeSBarry Smith   }
4045c6c1daeSBarry Smith   PetscFunctionReturn(0);
4055c6c1daeSBarry Smith }
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith /*@C
4085c6c1daeSBarry Smith   PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica
4095c6c1daeSBarry Smith 
4105c6c1daeSBarry Smith   Input Parameter:
4115c6c1daeSBarry Smith . viewer - The Mathematica viewer
4125c6c1daeSBarry Smith 
4135c6c1daeSBarry Smith   Output Parameter:
4145c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
4155c6c1daeSBarry Smith 
4165c6c1daeSBarry Smith   Level: intermediate
4175c6c1daeSBarry Smith 
4185c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
419db781477SPatrick Sanan .seealso `PetscViewerMathematicaSetName()`, `PetscViewerMathematicaClearName()`
4205c6c1daeSBarry Smith @*/
4215c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetName(PetscViewer viewer, const char **name)
4225c6c1daeSBarry Smith {
4235c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4245c6c1daeSBarry Smith 
4255c6c1daeSBarry Smith   PetscFunctionBegin;
4265c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4275c6c1daeSBarry Smith   PetscValidPointer(name,2);
4285c6c1daeSBarry Smith   *name = vmath->objName;
4295c6c1daeSBarry Smith   PetscFunctionReturn(0);
4305c6c1daeSBarry Smith }
4315c6c1daeSBarry Smith 
4325c6c1daeSBarry Smith /*@C
4335c6c1daeSBarry Smith   PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica
4345c6c1daeSBarry Smith 
4355c6c1daeSBarry Smith   Input Parameters:
436a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
437a2b725a8SWilliam Gropp - name   - The name for new objects created in Mathematica
4385c6c1daeSBarry Smith 
4395c6c1daeSBarry Smith   Level: intermediate
4405c6c1daeSBarry Smith 
4415c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
442db781477SPatrick Sanan .seealso `PetscViewerMathematicaSetName()`, `PetscViewerMathematicaClearName()`
4435c6c1daeSBarry Smith @*/
4445c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetName(PetscViewer viewer, const char name[])
4455c6c1daeSBarry Smith {
4465c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4475c6c1daeSBarry Smith 
4485c6c1daeSBarry Smith   PetscFunctionBegin;
4495c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4505c6c1daeSBarry Smith   PetscValidPointer(name,2);
4515c6c1daeSBarry Smith   vmath->objName = name;
4525c6c1daeSBarry Smith   PetscFunctionReturn(0);
4535c6c1daeSBarry Smith }
4545c6c1daeSBarry Smith 
4555c6c1daeSBarry Smith /*@C
4565c6c1daeSBarry Smith   PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica
4575c6c1daeSBarry Smith 
4585c6c1daeSBarry Smith   Input Parameter:
4595c6c1daeSBarry Smith . viewer - The Mathematica viewer
4605c6c1daeSBarry Smith 
4615c6c1daeSBarry Smith   Level: intermediate
4625c6c1daeSBarry Smith 
4635c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
464db781477SPatrick Sanan .seealso `PetscViewerMathematicaGetName()`, `PetscViewerMathematicaSetName()`
4655c6c1daeSBarry Smith @*/
4665c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaClearName(PetscViewer viewer)
4675c6c1daeSBarry Smith {
4685c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4695c6c1daeSBarry Smith 
4705c6c1daeSBarry Smith   PetscFunctionBegin;
4715c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4720298fd71SBarry Smith   vmath->objName = NULL;
4735c6c1daeSBarry Smith   PetscFunctionReturn(0);
4745c6c1daeSBarry Smith }
4755c6c1daeSBarry Smith 
4765c6c1daeSBarry Smith /*@C
4775c6c1daeSBarry Smith   PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica
4785c6c1daeSBarry Smith 
4795c6c1daeSBarry Smith   Input Parameter:
4805c6c1daeSBarry Smith . viewer - The Mathematica viewer
4815c6c1daeSBarry Smith 
4825c6c1daeSBarry Smith   Output Parameter:
4835c6c1daeSBarry Smith . v      - The vector
4845c6c1daeSBarry Smith 
4855c6c1daeSBarry Smith   Level: intermediate
4865c6c1daeSBarry Smith 
4875c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
488db781477SPatrick Sanan .seealso `VecView()`, `PetscViewerMathematicaPutVector()`
4895c6c1daeSBarry Smith @*/
4900adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v)
4910adebc6cSBarry Smith {
4925c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4935c6c1daeSBarry Smith   MLINK                   link;   /* The link to Mathematica */
4945c6c1daeSBarry Smith   char                    *name;
4955c6c1daeSBarry Smith   PetscScalar             *mArray,*array;
4965c6c1daeSBarry Smith   long                    mSize;
4975c6c1daeSBarry Smith   int                     n;
4985c6c1daeSBarry Smith 
4995c6c1daeSBarry Smith   PetscFunctionBegin;
5005c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5015c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,      VEC_CLASSID,2);
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   /* Determine the object name */
504a297a907SKarl Rupp   if (!vmath->objName) name = "vec";
505a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
5065c6c1daeSBarry Smith 
5075c6c1daeSBarry Smith   link = vmath->link;
5089566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(v, &n));
5099566063dSJacob Faibussowitsch   PetscCall(VecGetArray(v, &array));
5105c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5115c6c1daeSBarry Smith   MLPutSymbol(link, name);
5125c6c1daeSBarry Smith   MLEndPacket(link);
5139566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
5145c6c1daeSBarry Smith   MLGetRealList(link, &mArray, &mSize);
51508401ef6SPierre Jolivet   PetscCheck(n == mSize,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize);
5169566063dSJacob Faibussowitsch   PetscCall(PetscArraycpy(array, mArray, mSize));
5175c6c1daeSBarry Smith   MLDisownRealList(link, mArray, mSize);
5189566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(v, &array));
5195c6c1daeSBarry Smith   PetscFunctionReturn(0);
5205c6c1daeSBarry Smith }
5215c6c1daeSBarry Smith 
5225c6c1daeSBarry Smith /*@C
5235c6c1daeSBarry Smith   PetscViewerMathematicaPutVector - Send a vector to Mathematica
5245c6c1daeSBarry Smith 
5255c6c1daeSBarry Smith   Input Parameters:
5265c6c1daeSBarry Smith + viewer - The Mathematica viewer
5275c6c1daeSBarry Smith - v      - The vector
5285c6c1daeSBarry Smith 
5295c6c1daeSBarry Smith   Level: intermediate
5305c6c1daeSBarry Smith 
5315c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
532db781477SPatrick Sanan .seealso `VecView()`, `PetscViewerMathematicaGetVector()`
5335c6c1daeSBarry Smith @*/
5345c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v)
5355c6c1daeSBarry Smith {
5365c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5375c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
5385c6c1daeSBarry Smith   char                    *name;
5395c6c1daeSBarry Smith   PetscScalar             *array;
5405c6c1daeSBarry Smith   int                     n;
5415c6c1daeSBarry Smith 
5425c6c1daeSBarry Smith   PetscFunctionBegin;
5435c6c1daeSBarry Smith   /* Determine the object name */
544a297a907SKarl Rupp   if (!vmath->objName) name = "vec";
545a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
546a297a907SKarl Rupp 
5479566063dSJacob Faibussowitsch   PetscCall(VecGetLocalSize(v, &n));
5489566063dSJacob Faibussowitsch   PetscCall(VecGetArray(v, &array));
5495c6c1daeSBarry Smith 
5505c6c1daeSBarry Smith   /* Send the Vector object */
5515c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5525c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
5535c6c1daeSBarry Smith   MLPutSymbol(link, name);
5545c6c1daeSBarry Smith   MLPutRealList(link, array, n);
5555c6c1daeSBarry Smith   MLEndPacket(link);
5565c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
5579566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
5585c6c1daeSBarry Smith   /* Skip ReturnPacket */
5595c6c1daeSBarry Smith   MLNewPacket(link);
5605c6c1daeSBarry Smith 
5619566063dSJacob Faibussowitsch   PetscCall(VecRestoreArray(v, &array));
5625c6c1daeSBarry Smith   PetscFunctionReturn(0);
5635c6c1daeSBarry Smith }
5645c6c1daeSBarry Smith 
5655c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a)
5665c6c1daeSBarry Smith {
5675c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5685c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
5695c6c1daeSBarry Smith   char                    *name;
5705c6c1daeSBarry Smith 
5715c6c1daeSBarry Smith   PetscFunctionBegin;
5725c6c1daeSBarry Smith   /* Determine the object name */
573a297a907SKarl Rupp   if (!vmath->objName) name = "mat";
574a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
5755c6c1daeSBarry Smith 
5765c6c1daeSBarry Smith   /* Send the dense matrix object */
5775c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5785c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
5795c6c1daeSBarry Smith   MLPutSymbol(link, name);
5805c6c1daeSBarry Smith   MLPutFunction(link, "Transpose", 1);
5815c6c1daeSBarry Smith   MLPutFunction(link, "Partition", 2);
5825c6c1daeSBarry Smith   MLPutRealList(link, a, m*n);
5835c6c1daeSBarry Smith   MLPutInteger(link, m);
5845c6c1daeSBarry Smith   MLEndPacket(link);
5855c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
5869566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
5875c6c1daeSBarry Smith   /* Skip ReturnPacket */
5885c6c1daeSBarry Smith   MLNewPacket(link);
5895c6c1daeSBarry Smith   PetscFunctionReturn(0);
5905c6c1daeSBarry Smith }
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
5935c6c1daeSBarry Smith {
5945c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5955c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
5965c6c1daeSBarry Smith   const char              *symbol;
5975c6c1daeSBarry Smith   char                    *name;
5985c6c1daeSBarry Smith   PetscBool               match;
5995c6c1daeSBarry Smith 
6005c6c1daeSBarry Smith   PetscFunctionBegin;
6015c6c1daeSBarry Smith   /* Determine the object name */
602a297a907SKarl Rupp   if (!vmath->objName) name = "mat";
603a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
6045c6c1daeSBarry Smith 
6055c6c1daeSBarry Smith   /* Make sure Mathematica recognizes sparse matrices */
6065c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6075c6c1daeSBarry Smith   MLPutFunction(link, "Needs", 1);
6085c6c1daeSBarry Smith   MLPutString(link, "LinearAlgebra`CSRMatrix`");
6095c6c1daeSBarry Smith   MLEndPacket(link);
6105c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6119566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
6125c6c1daeSBarry Smith   /* Skip ReturnPacket */
6135c6c1daeSBarry Smith   MLNewPacket(link);
6145c6c1daeSBarry Smith 
6155c6c1daeSBarry Smith   /* Send the CSRMatrix object */
6165c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6175c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
6185c6c1daeSBarry Smith   MLPutSymbol(link, name);
6195c6c1daeSBarry Smith   MLPutFunction(link, "CSRMatrix", 5);
6205c6c1daeSBarry Smith   MLPutInteger(link, m);
6215c6c1daeSBarry Smith   MLPutInteger(link, n);
6225c6c1daeSBarry Smith   MLPutFunction(link, "Plus", 2);
6235c6c1daeSBarry Smith   MLPutIntegerList(link, i, m+1);
6245c6c1daeSBarry Smith   MLPutInteger(link, 1);
6255c6c1daeSBarry Smith   MLPutFunction(link, "Plus", 2);
6265c6c1daeSBarry Smith   MLPutIntegerList(link, j, i[m]);
6275c6c1daeSBarry Smith   MLPutInteger(link, 1);
6285c6c1daeSBarry Smith   MLPutRealList(link, a, i[m]);
6295c6c1daeSBarry Smith   MLEndPacket(link);
6305c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6319566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
6325c6c1daeSBarry Smith   /* Skip ReturnPacket */
6335c6c1daeSBarry Smith   MLNewPacket(link);
6345c6c1daeSBarry Smith 
6355c6c1daeSBarry Smith   /* Check that matrix is valid */
6365c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6375c6c1daeSBarry Smith   MLPutFunction(link, "ValidQ", 1);
6385c6c1daeSBarry Smith   MLPutSymbol(link, name);
6395c6c1daeSBarry Smith   MLEndPacket(link);
6409566063dSJacob Faibussowitsch   PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT));
6415c6c1daeSBarry Smith   MLGetSymbol(link, &symbol);
6429566063dSJacob Faibussowitsch   PetscCall(PetscStrcmp("True", (char*) symbol, &match));
6435c6c1daeSBarry Smith   if (!match) {
6445c6c1daeSBarry Smith     MLDisownSymbol(link, symbol);
6455c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
6465c6c1daeSBarry Smith   }
6475c6c1daeSBarry Smith   MLDisownSymbol(link, symbol);
6485c6c1daeSBarry Smith   /* Skip ReturnPacket */
6495c6c1daeSBarry Smith   MLNewPacket(link);
6505c6c1daeSBarry Smith   PetscFunctionReturn(0);
6515c6c1daeSBarry Smith }
652