xref: /petsc/src/sys/classes/viewer/impls/mathematica/mathematica.c (revision 98921bda46e76d7aaed9e0138c5ff9d0ce93f355)
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 
215c6c1daeSBarry Smith .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 
375c6c1daeSBarry Smith .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 
495c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerMathematicaFinalizePackage);CHKERRQ(ierr);
505c6c1daeSBarry Smith   PetscFunctionReturn(0);
515c6c1daeSBarry Smith }
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith PetscErrorCode PetscViewerInitializeMathematicaWorld_Private()
545c6c1daeSBarry Smith {
555c6c1daeSBarry Smith   PetscErrorCode ierr;
565c6c1daeSBarry Smith 
575c6c1daeSBarry Smith   PetscFunctionBegin;
585c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscFunctionReturn(0);
590298fd71SBarry Smith   ierr = PetscViewerMathematicaOpen(PETSC_COMM_WORLD, PETSC_DECIDE, NULL, NULL, &PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
605c6c1daeSBarry Smith   PetscFunctionReturn(0);
615c6c1daeSBarry Smith }
625c6c1daeSBarry Smith 
635c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Mathematica(PetscViewer viewer)
645c6c1daeSBarry Smith {
655c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
665c6c1daeSBarry Smith   PetscErrorCode          ierr;
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith   PetscFunctionBegin;
695c6c1daeSBarry Smith   MLClose(vmath->link);
705c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkname);CHKERRQ(ierr);
715c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkhost);CHKERRQ(ierr);
725c6c1daeSBarry Smith   ierr = PetscFree(vmath);CHKERRQ(ierr);
735c6c1daeSBarry Smith   PetscFunctionReturn(0);
745c6c1daeSBarry Smith }
755c6c1daeSBarry Smith 
765c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroyMathematica_Private(void)
775c6c1daeSBarry Smith {
785c6c1daeSBarry Smith   PetscErrorCode ierr;
795c6c1daeSBarry Smith 
805c6c1daeSBarry Smith   PetscFunctionBegin;
815c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) {
825c6c1daeSBarry Smith     ierr = PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
835c6c1daeSBarry Smith   }
845c6c1daeSBarry Smith   PetscFunctionReturn(0);
855c6c1daeSBarry Smith }
865c6c1daeSBarry Smith 
875c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v)
885c6c1daeSBarry Smith {
895c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
90519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
915c6c1daeSBarry Smith   int                     argc = 6;
925c6c1daeSBarry Smith   char                    *argv[6];
935c6c1daeSBarry Smith #else
945c6c1daeSBarry Smith   int                     argc = 5;
955c6c1daeSBarry Smith   char                    *argv[5];
965c6c1daeSBarry Smith #endif
975c6c1daeSBarry Smith   char                    hostname[256];
985c6c1daeSBarry Smith   long                    lerr;
995c6c1daeSBarry Smith   PetscErrorCode          ierr;
1005c6c1daeSBarry Smith 
1015c6c1daeSBarry Smith   PetscFunctionBegin;
1025c6c1daeSBarry Smith   /* Link name */
1035c6c1daeSBarry Smith   argv[0] = "-linkname";
104a297a907SKarl Rupp   if (!vmath->linkname) argv[1] = "math -mathlink";
105a297a907SKarl Rupp   else                  argv[1] = vmath->linkname;
1065c6c1daeSBarry Smith 
1075c6c1daeSBarry Smith   /* Link host */
1085c6c1daeSBarry Smith   argv[2] = "-linkhost";
1095c6c1daeSBarry Smith   if (!vmath->linkhost) {
110589a23caSBarry Smith     ierr    = PetscGetHostName(hostname, sizeof(hostname));CHKERRQ(ierr);
1115c6c1daeSBarry Smith     argv[3] = hostname;
112a297a907SKarl Rupp   } else argv[3] = vmath->linkhost;
1135c6c1daeSBarry Smith 
1145c6c1daeSBarry Smith   /* Link mode */
115519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
1165c6c1daeSBarry Smith   argv[4] = "-linkmode";
1175c6c1daeSBarry Smith   switch (vmath->linkmode) {
1185c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1195c6c1daeSBarry Smith     argv[5] = "Create";
1205c6c1daeSBarry Smith     break;
1215c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1225c6c1daeSBarry Smith     argv[5] = "Connect";
1235c6c1daeSBarry Smith     break;
1245c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1255c6c1daeSBarry Smith     argv[5] = "Launch";
1265c6c1daeSBarry Smith     break;
1275c6c1daeSBarry Smith   }
1285c6c1daeSBarry Smith #else
1295c6c1daeSBarry Smith   switch (vmath->linkmode) {
1305c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1315c6c1daeSBarry Smith     argv[4] = "-linkcreate";
1325c6c1daeSBarry Smith     break;
1335c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1345c6c1daeSBarry Smith     argv[4] = "-linkconnect";
1355c6c1daeSBarry Smith     break;
1365c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1375c6c1daeSBarry Smith     argv[4] = "-linklaunch";
1385c6c1daeSBarry Smith     break;
1395c6c1daeSBarry Smith   }
1405c6c1daeSBarry Smith #endif
1415c6c1daeSBarry Smith   vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr);
1425c6c1daeSBarry Smith #endif
1435c6c1daeSBarry Smith   PetscFunctionReturn(0);
1445c6c1daeSBarry Smith }
1455c6c1daeSBarry Smith 
1468cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Mathematica(PetscViewer v)
1475c6c1daeSBarry Smith {
1485c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath;
1495c6c1daeSBarry Smith   PetscErrorCode          ierr;
1505c6c1daeSBarry Smith 
1515c6c1daeSBarry Smith   PetscFunctionBegin;
152607a6623SBarry Smith   ierr = PetscViewerMathematicaInitializePackage();CHKERRQ(ierr);
1535c6c1daeSBarry Smith 
154b00a9115SJed Brown   ierr            = PetscNewLog(v,&vmath);CHKERRQ(ierr);
1555c6c1daeSBarry Smith   v->data         = (void*) vmath;
1565c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_Mathematica;
1575c6c1daeSBarry Smith   v->ops->flush   = 0;
1585c6c1daeSBarry Smith   ierr            = PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name);CHKERRQ(ierr);
1595c6c1daeSBarry Smith 
1600298fd71SBarry Smith   vmath->linkname     = NULL;
1610298fd71SBarry Smith   vmath->linkhost     = NULL;
1625c6c1daeSBarry Smith   vmath->linkmode     = MATHEMATICA_LINK_CONNECT;
1635c6c1daeSBarry Smith   vmath->graphicsType = GRAPHICS_MOTIF;
1645c6c1daeSBarry Smith   vmath->plotType     = MATHEMATICA_TRIANGULATION_PLOT;
1650298fd71SBarry Smith   vmath->objName      = NULL;
1665c6c1daeSBarry Smith 
1675c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetFromOptions(v);CHKERRQ(ierr);
1685c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetupConnection_Private(v);CHKERRQ(ierr);
1695c6c1daeSBarry Smith   PetscFunctionReturn(0);
1705c6c1daeSBarry Smith }
1715c6c1daeSBarry Smith 
17295c0884eSLisandro Dalcin static PetscErrorCode PetscViewerMathematicaParseLinkMode(char *modename, LinkMode *mode)
1730adebc6cSBarry Smith {
1745c6c1daeSBarry Smith   PetscBool      isCreate, isConnect, isLaunch;
1755c6c1daeSBarry Smith   PetscErrorCode ierr;
1765c6c1daeSBarry Smith 
1775c6c1daeSBarry Smith   PetscFunctionBegin;
1785c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Create",  &isCreate);CHKERRQ(ierr);
1795c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Connect", &isConnect);CHKERRQ(ierr);
1805c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Launch",  &isLaunch);CHKERRQ(ierr);
181a297a907SKarl Rupp   if (isCreate)       *mode = MATHEMATICA_LINK_CREATE;
182a297a907SKarl Rupp   else if (isConnect) *mode = MATHEMATICA_LINK_CONNECT;
183a297a907SKarl Rupp   else if (isLaunch)  *mode = MATHEMATICA_LINK_LAUNCH;
184*98921bdaSJacob Faibussowitsch   else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename);
1855c6c1daeSBarry Smith   PetscFunctionReturn(0);
1865c6c1daeSBarry Smith }
1875c6c1daeSBarry Smith 
1885c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetFromOptions(PetscViewer v)
1895c6c1daeSBarry Smith {
1905c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
1915c6c1daeSBarry Smith   char                    linkname[256];
1925c6c1daeSBarry Smith   char                    modename[256];
1935c6c1daeSBarry Smith   char                    hostname[256];
1945c6c1daeSBarry Smith   char                    type[256];
1955c6c1daeSBarry Smith   PetscInt                numPorts;
1965c6c1daeSBarry Smith   PetscInt                *ports;
1975c6c1daeSBarry Smith   PetscInt                numHosts;
1985c6c1daeSBarry Smith   int                     h;
1995c6c1daeSBarry Smith   char                    **hosts;
2005c6c1daeSBarry Smith   PetscMPIInt             size, rank;
2015c6c1daeSBarry Smith   PetscBool               opt;
2025c6c1daeSBarry Smith   PetscErrorCode          ierr;
2035c6c1daeSBarry Smith 
2045c6c1daeSBarry Smith   PetscFunctionBegin;
205ffc4695bSBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)v), &size);CHKERRMPI(ierr);
206ffc4695bSBarry Smith   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank);CHKERRMPI(ierr);
2075c6c1daeSBarry Smith 
2085c6c1daeSBarry Smith   /* Get link name */
209589a23caSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkname", linkname, sizeof(linkname), &opt);CHKERRQ(ierr);
2105c6c1daeSBarry Smith   if (opt) {
2115c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
2125c6c1daeSBarry Smith   }
2135c6c1daeSBarry Smith   /* Get link port */
2145c6c1daeSBarry Smith   numPorts = size;
215785e854fSJed Brown   ierr     = PetscMalloc1(size, &ports);CHKERRQ(ierr);
2165c6c1daeSBarry Smith   ierr     = PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt);CHKERRQ(ierr);
2175c6c1daeSBarry Smith   if (opt) {
218589a23caSBarry Smith     if (numPorts > rank) snprintf(linkname, sizeof(linkname), "%6d", ports[rank]);
219589a23caSBarry Smith     else                 snprintf(linkname, sizeof(linkname), "%6d", ports[0]);
2205c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
2215c6c1daeSBarry Smith   }
2225c6c1daeSBarry Smith   ierr = PetscFree(ports);CHKERRQ(ierr);
2235c6c1daeSBarry Smith   /* Get link host */
2245c6c1daeSBarry Smith   numHosts = size;
225785e854fSJed Brown   ierr     = PetscMalloc1(size, &hosts);CHKERRQ(ierr);
2265c6c1daeSBarry Smith   ierr     = PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt);CHKERRQ(ierr);
2275c6c1daeSBarry Smith   if (opt) {
2285c6c1daeSBarry Smith     if (numHosts > rank) {
229589a23caSBarry Smith       ierr = PetscStrncpy(hostname, hosts[rank], sizeof(hostname));CHKERRQ(ierr);
2305c6c1daeSBarry Smith     } else {
231589a23caSBarry Smith       ierr = PetscStrncpy(hostname, hosts[0], sizeof(hostname));CHKERRQ(ierr);
2325c6c1daeSBarry Smith     }
2335c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkHost(v, hostname);CHKERRQ(ierr);
2345c6c1daeSBarry Smith   }
2355c6c1daeSBarry Smith   for (h = 0; h < numHosts; h++) {
2365c6c1daeSBarry Smith     ierr = PetscFree(hosts[h]);CHKERRQ(ierr);
2375c6c1daeSBarry Smith   }
2385c6c1daeSBarry Smith   ierr = PetscFree(hosts);CHKERRQ(ierr);
2395c6c1daeSBarry Smith   /* Get link mode */
240589a23caSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkmode", modename, sizeof(modename), &opt);CHKERRQ(ierr);
2415c6c1daeSBarry Smith   if (opt) {
2425c6c1daeSBarry Smith     LinkMode mode;
2435c6c1daeSBarry Smith 
24495c0884eSLisandro Dalcin     ierr = PetscViewerMathematicaParseLinkMode(modename, &mode);CHKERRQ(ierr);
2455c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkMode(v, mode);CHKERRQ(ierr);
2465c6c1daeSBarry Smith   }
2475c6c1daeSBarry Smith   /* Get graphics type */
248589a23caSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_graphics", type, sizeof(type), &opt);CHKERRQ(ierr);
2495c6c1daeSBarry Smith   if (opt) {
2505c6c1daeSBarry Smith     PetscBool isMotif, isPS, isPSFile;
2515c6c1daeSBarry Smith 
2525c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Motif",  &isMotif);CHKERRQ(ierr);
2535c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PS",     &isPS);CHKERRQ(ierr);
2545c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PSFile", &isPSFile);CHKERRQ(ierr);
255a297a907SKarl Rupp     if (isMotif)       vmath->graphicsType = GRAPHICS_MOTIF;
256a297a907SKarl Rupp     else if (isPS)     vmath->graphicsType = GRAPHICS_PS_STDOUT;
257a297a907SKarl Rupp     else if (isPSFile) vmath->graphicsType = GRAPHICS_PS_FILE;
2585c6c1daeSBarry Smith   }
2595c6c1daeSBarry Smith   /* Get plot type */
260589a23caSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_type", type, sizeof(type), &opt);CHKERRQ(ierr);
2615c6c1daeSBarry Smith   if (opt) {
2625c6c1daeSBarry Smith     PetscBool isTri, isVecTri, isVec, isSurface;
2635c6c1daeSBarry Smith 
2645c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Triangulation",       &isTri);CHKERRQ(ierr);
2655c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "VectorTriangulation", &isVecTri);CHKERRQ(ierr);
2665c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Vector",              &isVec);CHKERRQ(ierr);
2675c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Surface",             &isSurface);CHKERRQ(ierr);
268a297a907SKarl Rupp     if (isTri)          vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT;
269a297a907SKarl Rupp     else if (isVecTri)  vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT;
270a297a907SKarl Rupp     else if (isVec)     vmath->plotType = MATHEMATICA_VECTOR_PLOT;
271a297a907SKarl Rupp     else if (isSurface) vmath->plotType = MATHEMATICA_SURFACE_PLOT;
2725c6c1daeSBarry Smith   }
2735c6c1daeSBarry Smith   PetscFunctionReturn(0);
2745c6c1daeSBarry Smith }
2755c6c1daeSBarry Smith 
2760adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name)
2770adebc6cSBarry Smith {
2785c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
2795c6c1daeSBarry Smith   PetscErrorCode          ierr;
2805c6c1daeSBarry Smith 
2815c6c1daeSBarry Smith   PetscFunctionBegin;
2825c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
2835c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
2845c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, &vmath->linkname);CHKERRQ(ierr);
2855c6c1daeSBarry Smith   PetscFunctionReturn(0);
2865c6c1daeSBarry Smith }
2875c6c1daeSBarry Smith 
2880adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkPort(PetscViewer v, int port)
2890adebc6cSBarry Smith {
2905c6c1daeSBarry Smith   char           name[16];
2915c6c1daeSBarry Smith   PetscErrorCode ierr;
2925c6c1daeSBarry Smith 
2935c6c1daeSBarry Smith   PetscFunctionBegin;
2945c6c1daeSBarry Smith   snprintf(name, 16, "%6d", port);
2955c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkName(v, name);CHKERRQ(ierr);
2965c6c1daeSBarry Smith   PetscFunctionReturn(0);
2975c6c1daeSBarry Smith }
2985c6c1daeSBarry Smith 
2990adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host)
3000adebc6cSBarry Smith {
3015c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
3025c6c1daeSBarry Smith   PetscErrorCode          ierr;
3035c6c1daeSBarry Smith 
3045c6c1daeSBarry Smith   PetscFunctionBegin;
3055c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3065c6c1daeSBarry Smith   PetscValidCharPointer(host,2);
3075c6c1daeSBarry Smith   ierr = PetscStrallocpy(host, &vmath->linkhost);CHKERRQ(ierr);
3085c6c1daeSBarry Smith   PetscFunctionReturn(0);
3095c6c1daeSBarry Smith }
3105c6c1daeSBarry Smith 
3110adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode)
3120adebc6cSBarry Smith {
3135c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data;
3145c6c1daeSBarry Smith 
3155c6c1daeSBarry Smith   PetscFunctionBegin;
3165c6c1daeSBarry Smith   vmath->linkmode = mode;
3175c6c1daeSBarry Smith   PetscFunctionReturn(0);
3185c6c1daeSBarry Smith }
3195c6c1daeSBarry Smith 
3205c6c1daeSBarry Smith /*----------------------------------------- Public Functions --------------------------------------------------------*/
3215c6c1daeSBarry Smith /*@C
3225c6c1daeSBarry Smith   PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink.
3235c6c1daeSBarry Smith 
324d083f849SBarry Smith   Collective
3255c6c1daeSBarry Smith 
3265c6c1daeSBarry Smith   Input Parameters:
3275c6c1daeSBarry Smith + comm    - The MPI communicator
3285c6c1daeSBarry Smith . port    - [optional] The port to connect on, or PETSC_DECIDE
3290298fd71SBarry Smith . machine - [optional] The machine to run Mathematica on, or NULL
3300298fd71SBarry Smith - mode    - [optional] The connection mode, or NULL
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith   Output Parameter:
3335c6c1daeSBarry Smith . viewer  - The Mathematica viewer
3345c6c1daeSBarry Smith 
3355c6c1daeSBarry Smith   Level: intermediate
3365c6c1daeSBarry Smith 
3375c6c1daeSBarry Smith   Notes:
3385c6c1daeSBarry Smith   Most users should employ the following commands to access the
3395c6c1daeSBarry Smith   Mathematica viewers
3405c6c1daeSBarry Smith $
3415c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
3425c6c1daeSBarry Smith $    MatView(Mat matrix, PetscViewer viewer)
3435c6c1daeSBarry Smith $
3445c6c1daeSBarry Smith $                or
3455c6c1daeSBarry Smith $
3465c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
3475c6c1daeSBarry Smith $    VecView(Vec vector, PetscViewer viewer)
3485c6c1daeSBarry Smith 
3495c6c1daeSBarry Smith    Options Database Keys:
350e1bc860dSBarry Smith +    -viewer_math_linkhost <machine> - The host machine for the kernel
351e1bc860dSBarry Smith .    -viewer_math_linkname <name>    - The full link name for the connection
352e1bc860dSBarry Smith .    -viewer_math_linkport <port>    - The port for the connection
353e1bc860dSBarry Smith .    -viewer_math_mode <mode>        - The mode, e.g. Launch, Connect
354e1bc860dSBarry Smith .    -viewer_math_type <type>        - The plot type, e.g. Triangulation, Vector
355e1bc860dSBarry Smith -    -viewer_math_graphics <output>  - The output type, e.g. Motif, PS, PSFile
3565c6c1daeSBarry Smith 
3575c6c1daeSBarry Smith .seealso: MatView(), VecView()
3585c6c1daeSBarry Smith @*/
3595c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v)
3605c6c1daeSBarry Smith {
3615c6c1daeSBarry Smith   PetscErrorCode ierr;
3625c6c1daeSBarry Smith 
3635c6c1daeSBarry Smith   PetscFunctionBegin;
3645c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm, v);CHKERRQ(ierr);
3655c6c1daeSBarry Smith #if 0
3665c6c1daeSBarry Smith   LinkMode linkmode;
3675c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkPort(*v, port);CHKERRQ(ierr);
3685c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkHost(*v, machine);CHKERRQ(ierr);
36995c0884eSLisandro Dalcin   ierr = PetscViewerMathematicaParseLinkMode(mode, &linkmode);CHKERRQ(ierr);
3705c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkMode(*v, linkmode);CHKERRQ(ierr);
3715c6c1daeSBarry Smith #endif
3725c6c1daeSBarry Smith   ierr = PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA);CHKERRQ(ierr);
3735c6c1daeSBarry Smith   PetscFunctionReturn(0);
3745c6c1daeSBarry Smith }
3755c6c1daeSBarry Smith 
3765c6c1daeSBarry Smith /*@C
3775c6c1daeSBarry Smith   PetscViewerMathematicaGetLink - Returns the link to Mathematica
3785c6c1daeSBarry Smith 
3795c6c1daeSBarry Smith   Input Parameters:
380a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
381a2b725a8SWilliam Gropp - link   - The link to Mathematica
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith   Level: intermediate
3845c6c1daeSBarry Smith 
3855c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, link
3865c6c1daeSBarry Smith .seealso PetscViewerMathematicaOpen()
3875c6c1daeSBarry Smith @*/
3885c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link)
3895c6c1daeSBarry Smith {
3905c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
3915c6c1daeSBarry Smith 
3925c6c1daeSBarry Smith   PetscFunctionBegin;
3935c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
3945c6c1daeSBarry Smith   *link = vmath->link;
3955c6c1daeSBarry Smith   PetscFunctionReturn(0);
3965c6c1daeSBarry Smith }
3975c6c1daeSBarry Smith 
3985c6c1daeSBarry Smith /*@C
3995c6c1daeSBarry Smith   PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received
4005c6c1daeSBarry Smith 
4015c6c1daeSBarry Smith   Input Parameters:
402a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
403a2b725a8SWilliam Gropp - type   - The packet type to search for, e.g RETURNPKT
4045c6c1daeSBarry Smith 
4055c6c1daeSBarry Smith   Level: advanced
4065c6c1daeSBarry Smith 
4075c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, packets
4085c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaGetVector()
4095c6c1daeSBarry Smith @*/
4105c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type)
4115c6c1daeSBarry Smith {
4125c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4135c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
4145c6c1daeSBarry Smith   int                     pkt;                 /* The packet type */
4155c6c1daeSBarry Smith 
4165c6c1daeSBarry Smith   PetscFunctionBegin;
417a297a907SKarl Rupp   while ((pkt = MLNextPacket(link)) && (pkt != type)) MLNewPacket(link);
4185c6c1daeSBarry Smith   if (!pkt) {
4195c6c1daeSBarry Smith     MLClearError(link);
4205c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char*) MLErrorMessage(link));
4215c6c1daeSBarry Smith   }
4225c6c1daeSBarry Smith   PetscFunctionReturn(0);
4235c6c1daeSBarry Smith }
4245c6c1daeSBarry Smith 
4255c6c1daeSBarry Smith /*@C
4265c6c1daeSBarry Smith   PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica
4275c6c1daeSBarry Smith 
4285c6c1daeSBarry Smith   Input Parameter:
4295c6c1daeSBarry Smith . viewer - The Mathematica viewer
4305c6c1daeSBarry Smith 
4315c6c1daeSBarry Smith   Output Parameter:
4325c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
4335c6c1daeSBarry Smith 
4345c6c1daeSBarry Smith   Level: intermediate
4355c6c1daeSBarry Smith 
4365c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
4375c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
4385c6c1daeSBarry Smith @*/
4395c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetName(PetscViewer viewer, const char **name)
4405c6c1daeSBarry Smith {
4415c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4425c6c1daeSBarry Smith 
4435c6c1daeSBarry Smith   PetscFunctionBegin;
4445c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4455c6c1daeSBarry Smith   PetscValidPointer(name,2);
4465c6c1daeSBarry Smith   *name = vmath->objName;
4475c6c1daeSBarry Smith   PetscFunctionReturn(0);
4485c6c1daeSBarry Smith }
4495c6c1daeSBarry Smith 
4505c6c1daeSBarry Smith /*@C
4515c6c1daeSBarry Smith   PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica
4525c6c1daeSBarry Smith 
4535c6c1daeSBarry Smith   Input Parameters:
454a2b725a8SWilliam Gropp + viewer - The Mathematica viewer
455a2b725a8SWilliam Gropp - name   - The name for new objects created in Mathematica
4565c6c1daeSBarry Smith 
4575c6c1daeSBarry Smith   Level: intermediate
4585c6c1daeSBarry Smith 
4595c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
4605c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
4615c6c1daeSBarry Smith @*/
4625c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetName(PetscViewer viewer, const char name[])
4635c6c1daeSBarry Smith {
4645c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4655c6c1daeSBarry Smith 
4665c6c1daeSBarry Smith   PetscFunctionBegin;
4675c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4685c6c1daeSBarry Smith   PetscValidPointer(name,2);
4695c6c1daeSBarry Smith   vmath->objName = name;
4705c6c1daeSBarry Smith   PetscFunctionReturn(0);
4715c6c1daeSBarry Smith }
4725c6c1daeSBarry Smith 
4735c6c1daeSBarry Smith /*@C
4745c6c1daeSBarry Smith   PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica
4755c6c1daeSBarry Smith 
4765c6c1daeSBarry Smith   Input Parameter:
4775c6c1daeSBarry Smith . viewer - The Mathematica viewer
4785c6c1daeSBarry Smith 
4795c6c1daeSBarry Smith   Level: intermediate
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
4825c6c1daeSBarry Smith .seealso PetscViewerMathematicaGetName(), PetscViewerMathematicaSetName()
4835c6c1daeSBarry Smith @*/
4845c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaClearName(PetscViewer viewer)
4855c6c1daeSBarry Smith {
4865c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
4875c6c1daeSBarry Smith 
4885c6c1daeSBarry Smith   PetscFunctionBegin;
4895c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4900298fd71SBarry Smith   vmath->objName = NULL;
4915c6c1daeSBarry Smith   PetscFunctionReturn(0);
4925c6c1daeSBarry Smith }
4935c6c1daeSBarry Smith 
4945c6c1daeSBarry Smith /*@C
4955c6c1daeSBarry Smith   PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica
4965c6c1daeSBarry Smith 
4975c6c1daeSBarry Smith   Input Parameter:
4985c6c1daeSBarry Smith . viewer - The Mathematica viewer
4995c6c1daeSBarry Smith 
5005c6c1daeSBarry Smith   Output Parameter:
5015c6c1daeSBarry Smith . v      - The vector
5025c6c1daeSBarry Smith 
5035c6c1daeSBarry Smith   Level: intermediate
5045c6c1daeSBarry Smith 
5055c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
5065c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaPutVector()
5075c6c1daeSBarry Smith @*/
5080adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v)
5090adebc6cSBarry Smith {
5105c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5115c6c1daeSBarry Smith   MLINK                   link;   /* The link to Mathematica */
5125c6c1daeSBarry Smith   char                    *name;
5135c6c1daeSBarry Smith   PetscScalar             *mArray,*array;
5145c6c1daeSBarry Smith   long                    mSize;
5155c6c1daeSBarry Smith   int                     n;
5165c6c1daeSBarry Smith   PetscErrorCode          ierr;
5175c6c1daeSBarry Smith 
5185c6c1daeSBarry Smith   PetscFunctionBegin;
5195c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5205c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,      VEC_CLASSID,2);
5215c6c1daeSBarry Smith 
5225c6c1daeSBarry Smith   /* Determine the object name */
523a297a907SKarl Rupp   if (!vmath->objName) name = "vec";
524a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
5255c6c1daeSBarry Smith 
5265c6c1daeSBarry Smith   link = vmath->link;
5275c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
5285c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5295c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5305c6c1daeSBarry Smith   MLPutSymbol(link, name);
5315c6c1daeSBarry Smith   MLEndPacket(link);
5325c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
5335c6c1daeSBarry Smith   MLGetRealList(link, &mArray, &mSize);
534*98921bdaSJacob Faibussowitsch   if (n != mSize) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize);
535580bdb30SBarry Smith   ierr = PetscArraycpy(array, mArray, mSize);CHKERRQ(ierr);
5365c6c1daeSBarry Smith   MLDisownRealList(link, mArray, mSize);
5375c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5385c6c1daeSBarry Smith   PetscFunctionReturn(0);
5395c6c1daeSBarry Smith }
5405c6c1daeSBarry Smith 
5415c6c1daeSBarry Smith /*@C
5425c6c1daeSBarry Smith   PetscViewerMathematicaPutVector - Send a vector to Mathematica
5435c6c1daeSBarry Smith 
5445c6c1daeSBarry Smith   Input Parameters:
5455c6c1daeSBarry Smith + viewer - The Mathematica viewer
5465c6c1daeSBarry Smith - v      - The vector
5475c6c1daeSBarry Smith 
5485c6c1daeSBarry Smith   Level: intermediate
5495c6c1daeSBarry Smith 
5505c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
5515c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaGetVector()
5525c6c1daeSBarry Smith @*/
5535c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v)
5545c6c1daeSBarry Smith {
5555c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5565c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
5575c6c1daeSBarry Smith   char                    *name;
5585c6c1daeSBarry Smith   PetscScalar             *array;
5595c6c1daeSBarry Smith   int                     n;
5605c6c1daeSBarry Smith   PetscErrorCode          ierr;
5615c6c1daeSBarry Smith 
5625c6c1daeSBarry Smith   PetscFunctionBegin;
5635c6c1daeSBarry Smith   /* Determine the object name */
564a297a907SKarl Rupp   if (!vmath->objName) name = "vec";
565a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
566a297a907SKarl Rupp 
5675c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
5685c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
5695c6c1daeSBarry Smith 
5705c6c1daeSBarry Smith   /* Send the Vector object */
5715c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5725c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
5735c6c1daeSBarry Smith   MLPutSymbol(link, name);
5745c6c1daeSBarry Smith   MLPutRealList(link, array, n);
5755c6c1daeSBarry Smith   MLEndPacket(link);
5765c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
5775c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
5785c6c1daeSBarry Smith   /* Skip ReturnPacket */
5795c6c1daeSBarry Smith   MLNewPacket(link);
5805c6c1daeSBarry Smith 
5815c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
5825c6c1daeSBarry Smith   PetscFunctionReturn(0);
5835c6c1daeSBarry Smith }
5845c6c1daeSBarry Smith 
5855c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a)
5865c6c1daeSBarry Smith {
5875c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
5885c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
5895c6c1daeSBarry Smith   char                    *name;
5905c6c1daeSBarry Smith   PetscErrorCode          ierr;
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith   PetscFunctionBegin;
5935c6c1daeSBarry Smith   /* Determine the object name */
594a297a907SKarl Rupp   if (!vmath->objName) name = "mat";
595a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
5965c6c1daeSBarry Smith 
5975c6c1daeSBarry Smith   /* Send the dense matrix object */
5985c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
5995c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
6005c6c1daeSBarry Smith   MLPutSymbol(link, name);
6015c6c1daeSBarry Smith   MLPutFunction(link, "Transpose", 1);
6025c6c1daeSBarry Smith   MLPutFunction(link, "Partition", 2);
6035c6c1daeSBarry Smith   MLPutRealList(link, a, m*n);
6045c6c1daeSBarry Smith   MLPutInteger(link, m);
6055c6c1daeSBarry Smith   MLEndPacket(link);
6065c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6075c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6085c6c1daeSBarry Smith   /* Skip ReturnPacket */
6095c6c1daeSBarry Smith   MLNewPacket(link);
6105c6c1daeSBarry Smith   PetscFunctionReturn(0);
6115c6c1daeSBarry Smith }
6125c6c1daeSBarry Smith 
6135c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
6145c6c1daeSBarry Smith {
6155c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data;
6165c6c1daeSBarry Smith   MLINK                   link   = vmath->link; /* The link to Mathematica */
6175c6c1daeSBarry Smith   const char              *symbol;
6185c6c1daeSBarry Smith   char                    *name;
6195c6c1daeSBarry Smith   PetscBool               match;
6205c6c1daeSBarry Smith   PetscErrorCode          ierr;
6215c6c1daeSBarry Smith 
6225c6c1daeSBarry Smith   PetscFunctionBegin;
6235c6c1daeSBarry Smith   /* Determine the object name */
624a297a907SKarl Rupp   if (!vmath->objName) name = "mat";
625a297a907SKarl Rupp   else                 name = (char*) vmath->objName;
6265c6c1daeSBarry Smith 
6275c6c1daeSBarry Smith   /* Make sure Mathematica recognizes sparse matrices */
6285c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6295c6c1daeSBarry Smith   MLPutFunction(link, "Needs", 1);
6305c6c1daeSBarry Smith   MLPutString(link, "LinearAlgebra`CSRMatrix`");
6315c6c1daeSBarry Smith   MLEndPacket(link);
6325c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6335c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6345c6c1daeSBarry Smith   /* Skip ReturnPacket */
6355c6c1daeSBarry Smith   MLNewPacket(link);
6365c6c1daeSBarry Smith 
6375c6c1daeSBarry Smith   /* Send the CSRMatrix object */
6385c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6395c6c1daeSBarry Smith   MLPutFunction(link, "Set", 2);
6405c6c1daeSBarry Smith   MLPutSymbol(link, name);
6415c6c1daeSBarry Smith   MLPutFunction(link, "CSRMatrix", 5);
6425c6c1daeSBarry Smith   MLPutInteger(link, m);
6435c6c1daeSBarry Smith   MLPutInteger(link, n);
6445c6c1daeSBarry Smith   MLPutFunction(link, "Plus", 2);
6455c6c1daeSBarry Smith   MLPutIntegerList(link, i, m+1);
6465c6c1daeSBarry Smith   MLPutInteger(link, 1);
6475c6c1daeSBarry Smith   MLPutFunction(link, "Plus", 2);
6485c6c1daeSBarry Smith   MLPutIntegerList(link, j, i[m]);
6495c6c1daeSBarry Smith   MLPutInteger(link, 1);
6505c6c1daeSBarry Smith   MLPutRealList(link, a, i[m]);
6515c6c1daeSBarry Smith   MLEndPacket(link);
6525c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6535c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6545c6c1daeSBarry Smith   /* Skip ReturnPacket */
6555c6c1daeSBarry Smith   MLNewPacket(link);
6565c6c1daeSBarry Smith 
6575c6c1daeSBarry Smith   /* Check that matrix is valid */
6585c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6595c6c1daeSBarry Smith   MLPutFunction(link, "ValidQ", 1);
6605c6c1daeSBarry Smith   MLPutSymbol(link, name);
6615c6c1daeSBarry Smith   MLEndPacket(link);
6625c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6635c6c1daeSBarry Smith   MLGetSymbol(link, &symbol);
6645c6c1daeSBarry Smith   ierr = PetscStrcmp("True", (char*) symbol, &match);CHKERRQ(ierr);
6655c6c1daeSBarry Smith   if (!match) {
6665c6c1daeSBarry Smith     MLDisownSymbol(link, symbol);
6675c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
6685c6c1daeSBarry Smith   }
6695c6c1daeSBarry Smith   MLDisownSymbol(link, symbol);
6705c6c1daeSBarry Smith   /* Skip ReturnPacket */
6715c6c1daeSBarry Smith   MLNewPacket(link);
6725c6c1daeSBarry Smith   PetscFunctionReturn(0);
6735c6c1daeSBarry Smith }
6745c6c1daeSBarry Smith 
675