xref: /petsc/src/sys/classes/viewer/impls/mathematica/mathematica.c (revision 519f805a543c2a7f195bcba21173bb2cdfadb956)
15c6c1daeSBarry Smith 
25c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>   /* "petscsys.h" */
35c6c1daeSBarry 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 
115c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE = PETSC_NULL;
125c6c1daeSBarry Smith static void *mathematicaEnv                   = PETSC_NULL;
135c6c1daeSBarry Smith 
145c6c1daeSBarry Smith static PetscBool  PetscViewerMathematicaPackageInitialized = PETSC_FALSE;
155c6c1daeSBarry Smith #undef __FUNCT__
165c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaFinalizePackage"
175c6c1daeSBarry Smith /*@C
185c6c1daeSBarry Smith   PetscViewerMathematicaFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
195c6c1daeSBarry Smith   called from PetscFinalize().
205c6c1daeSBarry Smith 
215c6c1daeSBarry Smith   Level: developer
225c6c1daeSBarry Smith 
235c6c1daeSBarry Smith .keywords: Petsc, destroy, package, mathematica
245c6c1daeSBarry Smith .seealso: PetscFinalize()
255c6c1daeSBarry Smith @*/
265c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaFinalizePackage(void)
275c6c1daeSBarry Smith {
285c6c1daeSBarry Smith   PetscFunctionBegin;
295c6c1daeSBarry Smith   if (mathematicaEnv) MLDeinitialize((MLEnvironment) mathematicaEnv);
305c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
315c6c1daeSBarry Smith   PetscFunctionReturn(0);
325c6c1daeSBarry Smith }
335c6c1daeSBarry Smith 
345c6c1daeSBarry Smith #undef __FUNCT__
355c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaInitializePackage"
365c6c1daeSBarry Smith /*@C
375c6c1daeSBarry Smith   PetscViewerMathematicaInitializePackage - This function initializes everything in the Petsc interface to Mathematica. It is
385c6c1daeSBarry Smith   called from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
395c6c1daeSBarry Smith   when using static libraries.
405c6c1daeSBarry Smith 
415c6c1daeSBarry Smith   Input Parameter:
425c6c1daeSBarry Smith   path - The dynamic library path, or PETSC_NULL
435c6c1daeSBarry Smith 
445c6c1daeSBarry Smith   Level: developer
455c6c1daeSBarry Smith 
465c6c1daeSBarry Smith .keywords: Petsc, initialize, package
475c6c1daeSBarry Smith .seealso: PetscSysInitializePackage(), PetscInitialize()
485c6c1daeSBarry Smith @*/
495c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaInitializePackage(const char path[])
505c6c1daeSBarry Smith {
515c6c1daeSBarry Smith   PetscError ierr;
525c6c1daeSBarry Smith 
535c6c1daeSBarry Smith   PetscFunctionBegin;
545c6c1daeSBarry Smith   if (PetscViewerMathematicaPackageInitialized) PetscFunctionReturn(0);
555c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
565c6c1daeSBarry Smith   mathematicaEnv = (void*) MLInitialize(0);
575c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerMathematicaFinalizePackage);CHKERRQ(ierr);
585c6c1daeSBarry Smith   PetscFunctionReturn(0);
595c6c1daeSBarry Smith }
605c6c1daeSBarry Smith 
615c6c1daeSBarry Smith 
625c6c1daeSBarry Smith #undef __FUNCT__
635c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerInitializeMathematicaWorld_Private"
645c6c1daeSBarry Smith PetscErrorCode PetscViewerInitializeMathematicaWorld_Private()
655c6c1daeSBarry Smith {
665c6c1daeSBarry Smith   PetscErrorCode ierr;
675c6c1daeSBarry Smith 
685c6c1daeSBarry Smith   PetscFunctionBegin;
695c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscFunctionReturn(0);
705c6c1daeSBarry Smith   ierr = PetscViewerMathematicaOpen(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_NULL, PETSC_NULL, &PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
715c6c1daeSBarry Smith   PetscFunctionReturn(0);
725c6c1daeSBarry Smith }
735c6c1daeSBarry Smith 
745c6c1daeSBarry Smith #undef __FUNCT__
755c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Mathematica"
765c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Mathematica(PetscViewer viewer)
775c6c1daeSBarry Smith {
785c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
795c6c1daeSBarry Smith   PetscErrorCode          ierr;
805c6c1daeSBarry Smith 
815c6c1daeSBarry Smith   PetscFunctionBegin;
825c6c1daeSBarry Smith   MLClose(vmath->link);
835c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkname);CHKERRQ(ierr);
845c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkhost);CHKERRQ(ierr);
855c6c1daeSBarry Smith   ierr = PetscFree(vmath);CHKERRQ(ierr);
865c6c1daeSBarry Smith   PetscFunctionReturn(0);
875c6c1daeSBarry Smith }
885c6c1daeSBarry Smith 
895c6c1daeSBarry Smith #undef __FUNCT__
905c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroyMathematica_Private"
915c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroyMathematica_Private(void)
925c6c1daeSBarry Smith {
935c6c1daeSBarry Smith   PetscErrorCode ierr;
945c6c1daeSBarry Smith 
955c6c1daeSBarry Smith   PetscFunctionBegin;
965c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) {
975c6c1daeSBarry Smith     ierr = PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
985c6c1daeSBarry Smith   }
995c6c1daeSBarry Smith   PetscFunctionReturn(0);
1005c6c1daeSBarry Smith }
1015c6c1daeSBarry Smith 
1025c6c1daeSBarry Smith #undef __FUNCT__
1035c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetupConnection_Private"
1045c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v)
1055c6c1daeSBarry Smith {
1065c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
107*519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
1085c6c1daeSBarry Smith   int                     argc = 6;
1095c6c1daeSBarry Smith   char                    *argv[6];
1105c6c1daeSBarry Smith #else
1115c6c1daeSBarry Smith   int                     argc = 5;
1125c6c1daeSBarry Smith   char                    *argv[5];
1135c6c1daeSBarry Smith #endif
1145c6c1daeSBarry Smith   char                    hostname[256];
1155c6c1daeSBarry Smith   long                    lerr;
1165c6c1daeSBarry Smith   PetscErrorCode          ierr;
1175c6c1daeSBarry Smith 
1185c6c1daeSBarry Smith   PetscFunctionBegin;
1195c6c1daeSBarry Smith   /* Link name */
1205c6c1daeSBarry Smith   argv[0] = "-linkname";
1215c6c1daeSBarry Smith   if (!vmath->linkname) {
1225c6c1daeSBarry Smith     argv[1] = "math -mathlink";
1235c6c1daeSBarry Smith   } else {
1245c6c1daeSBarry Smith     argv[1] = vmath->linkname;
1255c6c1daeSBarry Smith   }
1265c6c1daeSBarry Smith 
1275c6c1daeSBarry Smith   /* Link host */
1285c6c1daeSBarry Smith   argv[2] = "-linkhost";
1295c6c1daeSBarry Smith   if (!vmath->linkhost) {
1305c6c1daeSBarry Smith     ierr = PetscGetHostName(hostname, 255);CHKERRQ(ierr);
1315c6c1daeSBarry Smith     argv[3] = hostname;
1325c6c1daeSBarry Smith   } else {
1335c6c1daeSBarry Smith     argv[3] = vmath->linkhost;
1345c6c1daeSBarry Smith   }
1355c6c1daeSBarry Smith 
1365c6c1daeSBarry Smith   /* Link mode */
137*519f805aSKarl Rupp #if defined(MATHEMATICA_3_0)
1385c6c1daeSBarry Smith   argv[4] = "-linkmode";
1395c6c1daeSBarry Smith   switch(vmath->linkmode) {
1405c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1415c6c1daeSBarry Smith     argv[5] = "Create";
1425c6c1daeSBarry Smith     break;
1435c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1445c6c1daeSBarry Smith     argv[5] = "Connect";
1455c6c1daeSBarry Smith     break;
1465c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1475c6c1daeSBarry Smith     argv[5] = "Launch";
1485c6c1daeSBarry Smith     break;
1495c6c1daeSBarry Smith   }
1505c6c1daeSBarry Smith #else
1515c6c1daeSBarry Smith   switch(vmath->linkmode) {
1525c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
1535c6c1daeSBarry Smith     argv[4] = "-linkcreate";
1545c6c1daeSBarry Smith     break;
1555c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
1565c6c1daeSBarry Smith     argv[4] = "-linkconnect";
1575c6c1daeSBarry Smith     break;
1585c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
1595c6c1daeSBarry Smith     argv[4] = "-linklaunch";
1605c6c1daeSBarry Smith     break;
1615c6c1daeSBarry Smith   }
1625c6c1daeSBarry Smith #endif
1635c6c1daeSBarry Smith   vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr);
1645c6c1daeSBarry Smith #endif
1655c6c1daeSBarry Smith   PetscFunctionReturn(0);
1665c6c1daeSBarry Smith }
1675c6c1daeSBarry Smith 
1685c6c1daeSBarry Smith EXTERN_C_BEGIN
1695c6c1daeSBarry Smith #undef __FUNCT__
1705c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Mathematica"
1715c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate_Mathematica(PetscViewer v)
1725c6c1daeSBarry Smith {
1735c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath;
1745c6c1daeSBarry Smith   PetscErrorCode          ierr;
1755c6c1daeSBarry Smith 
1765c6c1daeSBarry Smith   PetscFunctionBegin;
177*519f805aSKarl Rupp #if !defined(PETSC_USE_DYNAMIC_LIBRARIES)
1785c6c1daeSBarry Smith   ierr = PetscViewerMathematicaInitializePackage(PETSC_NULL);CHKERRQ(ierr);
1795c6c1daeSBarry Smith #endif
1805c6c1daeSBarry Smith 
1815c6c1daeSBarry Smith   ierr = PetscNewLog(v,PetscViewer_Mathematica, &vmath);CHKERRQ(ierr);
1825c6c1daeSBarry Smith   v->data         = (void*) vmath;
1835c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_Mathematica;
1845c6c1daeSBarry Smith   v->ops->flush   = 0;
1855c6c1daeSBarry Smith   ierr = PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name);CHKERRQ(ierr);
1865c6c1daeSBarry Smith 
1875c6c1daeSBarry Smith   vmath->linkname         = PETSC_NULL;
1885c6c1daeSBarry Smith   vmath->linkhost         = PETSC_NULL;
1895c6c1daeSBarry Smith   vmath->linkmode         = MATHEMATICA_LINK_CONNECT;
1905c6c1daeSBarry Smith   vmath->graphicsType     = GRAPHICS_MOTIF;
1915c6c1daeSBarry Smith   vmath->plotType         = MATHEMATICA_TRIANGULATION_PLOT;
1925c6c1daeSBarry Smith   vmath->objName          = PETSC_NULL;
1935c6c1daeSBarry Smith 
1945c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetFromOptions(v);CHKERRQ(ierr);
1955c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetupConnection_Private(v);CHKERRQ(ierr);
1965c6c1daeSBarry Smith   PetscFunctionReturn(0);
1975c6c1daeSBarry Smith }
1985c6c1daeSBarry Smith EXTERN_C_END
1995c6c1daeSBarry Smith 
2005c6c1daeSBarry Smith #undef __FUNCT__
2015c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaParseLinkMode_Private"
2020adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaParseLinkMode_Private(char *modename, LinkMode *mode)
2030adebc6cSBarry Smith {
2045c6c1daeSBarry Smith   PetscBool      isCreate, isConnect, isLaunch;
2055c6c1daeSBarry Smith   PetscErrorCode ierr;
2065c6c1daeSBarry Smith 
2075c6c1daeSBarry Smith   PetscFunctionBegin;
2085c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Create",  &isCreate);CHKERRQ(ierr);
2095c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Connect", &isConnect);CHKERRQ(ierr);
2105c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Launch",  &isLaunch);CHKERRQ(ierr);
2115c6c1daeSBarry Smith   if (isCreate) {
2125c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_CREATE;
2135c6c1daeSBarry Smith   } else if (isConnect) {
2145c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_CONNECT;
2155c6c1daeSBarry Smith   } else if (isLaunch) {
2165c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_LAUNCH;
2175c6c1daeSBarry Smith   } else {
2185c6c1daeSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename);
2195c6c1daeSBarry Smith   }
2205c6c1daeSBarry Smith   PetscFunctionReturn(0);
2215c6c1daeSBarry Smith }
2225c6c1daeSBarry Smith 
2235c6c1daeSBarry Smith #undef __FUNCT__
2245c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetFromOptions"
2255c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetFromOptions(PetscViewer v)
2265c6c1daeSBarry Smith {
2275c6c1daeSBarry Smith   PetscViewer_Mathematica  *vmath = (PetscViewer_Mathematica *) v->data;
2285c6c1daeSBarry Smith   char                     linkname[256];
2295c6c1daeSBarry Smith   char                     modename[256];
2305c6c1daeSBarry Smith   char                     hostname[256];
2315c6c1daeSBarry Smith   char                     type[256];
2325c6c1daeSBarry Smith   PetscInt                 numPorts;
2335c6c1daeSBarry Smith   PetscInt                 *ports;
2345c6c1daeSBarry Smith   PetscInt                 numHosts;
2355c6c1daeSBarry Smith   int                      h;
2365c6c1daeSBarry Smith   char                     **hosts;
2375c6c1daeSBarry Smith   PetscMPIInt              size, rank;
2385c6c1daeSBarry Smith   PetscBool                opt;
2395c6c1daeSBarry Smith   PetscErrorCode           ierr;
2405c6c1daeSBarry Smith 
2415c6c1daeSBarry Smith   PetscFunctionBegin;
2425c6c1daeSBarry Smith   ierr = MPI_Comm_size(((PetscObject)v)->comm, &size);CHKERRQ(ierr);
2435c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)v)->comm, &rank);CHKERRQ(ierr);
2445c6c1daeSBarry Smith 
2455c6c1daeSBarry Smith   /* Get link name */
2465c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkname", linkname, 256, &opt);CHKERRQ(ierr);
2475c6c1daeSBarry Smith   if (opt) {
2485c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
2495c6c1daeSBarry Smith   }
2505c6c1daeSBarry Smith   /* Get link port */
2515c6c1daeSBarry Smith   numPorts = size;
2525c6c1daeSBarry Smith   ierr = PetscMalloc(size*sizeof(int), &ports);CHKERRQ(ierr);
2535c6c1daeSBarry Smith   ierr = PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt);CHKERRQ(ierr);
2545c6c1daeSBarry Smith   if (opt) {
2555c6c1daeSBarry Smith     if (numPorts > rank) {
2565c6c1daeSBarry Smith       snprintf(linkname, 255, "%6d", ports[rank]);
2575c6c1daeSBarry Smith     } else {
2585c6c1daeSBarry Smith       snprintf(linkname, 255, "%6d", ports[0]);
2595c6c1daeSBarry Smith     }
2605c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
2615c6c1daeSBarry Smith   }
2625c6c1daeSBarry Smith   ierr = PetscFree(ports);CHKERRQ(ierr);
2635c6c1daeSBarry Smith   /* Get link host */
2645c6c1daeSBarry Smith   numHosts = size;
2655c6c1daeSBarry Smith   ierr = PetscMalloc(size*sizeof(char *), &hosts);CHKERRQ(ierr);
2665c6c1daeSBarry Smith   ierr = PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt);CHKERRQ(ierr);
2675c6c1daeSBarry Smith   if (opt) {
2685c6c1daeSBarry Smith     if (numHosts > rank) {
2695c6c1daeSBarry Smith       ierr = PetscStrncpy(hostname, hosts[rank], 255);CHKERRQ(ierr);
2705c6c1daeSBarry Smith     } else {
2715c6c1daeSBarry Smith       ierr = PetscStrncpy(hostname, hosts[0], 255);CHKERRQ(ierr);
2725c6c1daeSBarry Smith     }
2735c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkHost(v, hostname);CHKERRQ(ierr);
2745c6c1daeSBarry Smith   }
2755c6c1daeSBarry Smith   for (h = 0; h < numHosts; h++) {
2765c6c1daeSBarry Smith     ierr = PetscFree(hosts[h]);CHKERRQ(ierr);
2775c6c1daeSBarry Smith   }
2785c6c1daeSBarry Smith   ierr = PetscFree(hosts);CHKERRQ(ierr);
2795c6c1daeSBarry Smith   /* Get link mode */
2805c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkmode", modename, 256, &opt);CHKERRQ(ierr);
2815c6c1daeSBarry Smith   if (opt) {
2825c6c1daeSBarry Smith     LinkMode mode;
2835c6c1daeSBarry Smith 
2845c6c1daeSBarry Smith     ierr = PetscViewerMathematicaParseLinkMode_Private(modename, &mode);CHKERRQ(ierr);
2855c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkMode(v, mode);CHKERRQ(ierr);
2865c6c1daeSBarry Smith   }
2875c6c1daeSBarry Smith   /* Get graphics type */
2885c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_graphics", type, 256, &opt);CHKERRQ(ierr);
2895c6c1daeSBarry Smith   if (opt) {
2905c6c1daeSBarry Smith     PetscBool  isMotif, isPS, isPSFile;
2915c6c1daeSBarry Smith 
2925c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Motif",  &isMotif);CHKERRQ(ierr);
2935c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PS",     &isPS);CHKERRQ(ierr);
2945c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PSFile", &isPSFile);CHKERRQ(ierr);
2955c6c1daeSBarry Smith     if (isMotif) {
2965c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_MOTIF;
2975c6c1daeSBarry Smith     } else if (isPS) {
2985c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_PS_STDOUT;
2995c6c1daeSBarry Smith     } else if (isPSFile) {
3005c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_PS_FILE;
3015c6c1daeSBarry Smith     }
3025c6c1daeSBarry Smith   }
3035c6c1daeSBarry Smith   /* Get plot type */
3045c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_type", type, 256, &opt);CHKERRQ(ierr);
3055c6c1daeSBarry Smith   if (opt) {
3065c6c1daeSBarry Smith     PetscBool  isTri, isVecTri, isVec, isSurface;
3075c6c1daeSBarry Smith 
3085c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Triangulation",       &isTri);CHKERRQ(ierr);
3095c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "VectorTriangulation", &isVecTri);CHKERRQ(ierr);
3105c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Vector",              &isVec);CHKERRQ(ierr);
3115c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Surface",             &isSurface);CHKERRQ(ierr);
3125c6c1daeSBarry Smith     if (isTri) {
3135c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_TRIANGULATION_PLOT;
3145c6c1daeSBarry Smith     } else if (isVecTri) {
3155c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_VECTOR_TRIANGULATION_PLOT;
3165c6c1daeSBarry Smith     } else if (isVec) {
3175c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_VECTOR_PLOT;
3185c6c1daeSBarry Smith     } else if (isSurface) {
3195c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_SURFACE_PLOT;
3205c6c1daeSBarry Smith     }
3215c6c1daeSBarry Smith   }
3225c6c1daeSBarry Smith   PetscFunctionReturn(0);
3235c6c1daeSBarry Smith }
3245c6c1daeSBarry Smith 
3255c6c1daeSBarry Smith #undef __FUNCT__
3265c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkName"
3270adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name)
3280adebc6cSBarry Smith {
3295c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
3305c6c1daeSBarry Smith   PetscErrorCode          ierr;
3315c6c1daeSBarry Smith 
3325c6c1daeSBarry Smith   PetscFunctionBegin;
3335c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3345c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
3355c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, &vmath->linkname);CHKERRQ(ierr);
3365c6c1daeSBarry Smith   PetscFunctionReturn(0);
3375c6c1daeSBarry Smith }
3385c6c1daeSBarry Smith 
3395c6c1daeSBarry Smith #undef __FUNCT__
3405c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaLinkPort"
3410adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkPort(PetscViewer v, int port)
3420adebc6cSBarry Smith {
3435c6c1daeSBarry Smith   char           name[16];
3445c6c1daeSBarry Smith   PetscErrorCode ierr;
3455c6c1daeSBarry Smith 
3465c6c1daeSBarry Smith   PetscFunctionBegin;
3475c6c1daeSBarry Smith   snprintf(name, 16, "%6d", port);
3485c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkName(v, name);CHKERRQ(ierr);
3495c6c1daeSBarry Smith   PetscFunctionReturn(0);
3505c6c1daeSBarry Smith }
3515c6c1daeSBarry Smith 
3525c6c1daeSBarry Smith #undef __FUNCT__
3535c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkHost"
3540adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host)
3550adebc6cSBarry Smith {
3565c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
3575c6c1daeSBarry Smith   PetscErrorCode          ierr;
3585c6c1daeSBarry Smith 
3595c6c1daeSBarry Smith   PetscFunctionBegin;
3605c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
3615c6c1daeSBarry Smith   PetscValidCharPointer(host,2);
3625c6c1daeSBarry Smith   ierr = PetscStrallocpy(host, &vmath->linkhost);CHKERRQ(ierr);
3635c6c1daeSBarry Smith   PetscFunctionReturn(0);
3645c6c1daeSBarry Smith }
3655c6c1daeSBarry Smith 
3665c6c1daeSBarry Smith #undef __FUNCT__
3675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkHost"
3680adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode)
3690adebc6cSBarry Smith {
3705c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
3715c6c1daeSBarry Smith 
3725c6c1daeSBarry Smith   PetscFunctionBegin;
3735c6c1daeSBarry Smith   vmath->linkmode = mode;
3745c6c1daeSBarry Smith   PetscFunctionReturn(0);
3755c6c1daeSBarry Smith }
3765c6c1daeSBarry Smith 
3775c6c1daeSBarry Smith /*----------------------------------------- Public Functions --------------------------------------------------------*/
3785c6c1daeSBarry Smith #undef __FUNCT__
3795c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaOpen"
3805c6c1daeSBarry Smith /*@C
3815c6c1daeSBarry Smith   PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink.
3825c6c1daeSBarry Smith 
3835c6c1daeSBarry Smith   Collective on comm
3845c6c1daeSBarry Smith 
3855c6c1daeSBarry Smith   Input Parameters:
3865c6c1daeSBarry Smith + comm    - The MPI communicator
3875c6c1daeSBarry Smith . port    - [optional] The port to connect on, or PETSC_DECIDE
3885c6c1daeSBarry Smith . machine - [optional] The machine to run Mathematica on, or PETSC_NULL
3895c6c1daeSBarry Smith - mode    - [optional] The connection mode, or PETSC_NULL
3905c6c1daeSBarry Smith 
3915c6c1daeSBarry Smith   Output Parameter:
3925c6c1daeSBarry Smith . viewer  - The Mathematica viewer
3935c6c1daeSBarry Smith 
3945c6c1daeSBarry Smith   Level: intermediate
3955c6c1daeSBarry Smith 
3965c6c1daeSBarry Smith   Notes:
3975c6c1daeSBarry Smith   Most users should employ the following commands to access the
3985c6c1daeSBarry Smith   Mathematica viewers
3995c6c1daeSBarry Smith $
4005c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
4015c6c1daeSBarry Smith $    MatView(Mat matrix, PetscViewer viewer)
4025c6c1daeSBarry Smith $
4035c6c1daeSBarry Smith $                or
4045c6c1daeSBarry Smith $
4055c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
4065c6c1daeSBarry Smith $    VecView(Vec vector, PetscViewer viewer)
4075c6c1daeSBarry Smith 
4085c6c1daeSBarry Smith    Options Database Keys:
4095c6c1daeSBarry Smith $    -viewer_math_linkhost <machine> - The host machine for the kernel
4105c6c1daeSBarry Smith $    -viewer_math_linkname <name>    - The full link name for the connection
4115c6c1daeSBarry Smith $    -viewer_math_linkport <port>    - The port for the connection
4125c6c1daeSBarry Smith $    -viewer_math_mode <mode>        - The mode, e.g. Launch, Connect
4135c6c1daeSBarry Smith $    -viewer_math_type <type>        - The plot type, e.g. Triangulation, Vector
4145c6c1daeSBarry Smith $    -viewer_math_graphics <output>  - The output type, e.g. Motif, PS, PSFile
4155c6c1daeSBarry Smith 
4165c6c1daeSBarry Smith .keywords: PetscViewer, Mathematica, open
4175c6c1daeSBarry Smith 
4185c6c1daeSBarry Smith .seealso: MatView(), VecView()
4195c6c1daeSBarry Smith @*/
4205c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v)
4215c6c1daeSBarry Smith {
4225c6c1daeSBarry Smith   PetscErrorCode ierr;
4235c6c1daeSBarry Smith 
4245c6c1daeSBarry Smith   PetscFunctionBegin;
4255c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm, v);CHKERRQ(ierr);
4265c6c1daeSBarry Smith #if 0
4275c6c1daeSBarry Smith   LinkMode linkmode;
4285c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkPort(*v, port);CHKERRQ(ierr);
4295c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkHost(*v, machine);CHKERRQ(ierr);
4305c6c1daeSBarry Smith   ierr = PetscViewerMathematicaParseLinkMode_Private(mode, &linkmode);CHKERRQ(ierr);
4315c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkMode(*v, linkmode);CHKERRQ(ierr);
4325c6c1daeSBarry Smith #endif
4335c6c1daeSBarry Smith   ierr = PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA);CHKERRQ(ierr);
4345c6c1daeSBarry Smith   PetscFunctionReturn(0);
4355c6c1daeSBarry Smith }
4365c6c1daeSBarry Smith 
4375c6c1daeSBarry Smith #undef __FUNCT__
4385c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetLink"
4395c6c1daeSBarry Smith /*@C
4405c6c1daeSBarry Smith   PetscViewerMathematicaGetLink - Returns the link to Mathematica
4415c6c1daeSBarry Smith 
4425c6c1daeSBarry Smith   Input Parameters:
4435c6c1daeSBarry Smith . viewer - The Mathematica viewer
4445c6c1daeSBarry Smith . link   - The link to Mathematica
4455c6c1daeSBarry Smith 
4465c6c1daeSBarry Smith   Level: intermediate
4475c6c1daeSBarry Smith 
4485c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, link
4495c6c1daeSBarry Smith .seealso PetscViewerMathematicaOpen()
4505c6c1daeSBarry Smith @*/
4515c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link)
4525c6c1daeSBarry Smith {
4535c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
4545c6c1daeSBarry Smith 
4555c6c1daeSBarry Smith   PetscFunctionBegin;
4565c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
4575c6c1daeSBarry Smith   *link = vmath->link;
4585c6c1daeSBarry Smith   PetscFunctionReturn(0);
4595c6c1daeSBarry Smith }
4605c6c1daeSBarry Smith 
4615c6c1daeSBarry Smith #undef __FUNCT__
4625c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSkipPackets"
4635c6c1daeSBarry Smith /*@C
4645c6c1daeSBarry Smith   PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received
4655c6c1daeSBarry Smith 
4665c6c1daeSBarry Smith   Input Parameters:
4675c6c1daeSBarry Smith . viewer - The Mathematica viewer
4685c6c1daeSBarry Smith . type   - The packet type to search for, e.g RETURNPKT
4695c6c1daeSBarry Smith 
4705c6c1daeSBarry Smith   Level: advanced
4715c6c1daeSBarry Smith 
4725c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, packets
4735c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaGetVector()
4745c6c1daeSBarry Smith @*/
4755c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type)
4765c6c1daeSBarry Smith {
4775c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
4785c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
4795c6c1daeSBarry Smith   int                     pkt;                 /* The packet type */
4805c6c1daeSBarry Smith 
4815c6c1daeSBarry Smith   PetscFunctionBegin;
4825c6c1daeSBarry Smith   while((pkt = MLNextPacket(link)) && (pkt != type))
4835c6c1daeSBarry Smith     MLNewPacket(link);
4845c6c1daeSBarry Smith   if (!pkt) {
4855c6c1daeSBarry Smith     MLClearError(link);
4865c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char *) MLErrorMessage(link));
4875c6c1daeSBarry Smith   }
4885c6c1daeSBarry Smith   PetscFunctionReturn(0);
4895c6c1daeSBarry Smith }
4905c6c1daeSBarry Smith 
4915c6c1daeSBarry Smith #undef __FUNCT__
4925c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetName"
4935c6c1daeSBarry Smith /*@C
4945c6c1daeSBarry Smith   PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica
4955c6c1daeSBarry Smith 
4965c6c1daeSBarry Smith   Input Parameter:
4975c6c1daeSBarry Smith . viewer - The Mathematica viewer
4985c6c1daeSBarry Smith 
4995c6c1daeSBarry Smith   Output Parameter:
5005c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
5015c6c1daeSBarry Smith 
5025c6c1daeSBarry Smith   Level: intermediate
5035c6c1daeSBarry Smith 
5045c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
5055c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
5065c6c1daeSBarry Smith @*/
5075c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetName(PetscViewer viewer, const char **name)
5085c6c1daeSBarry Smith {
5095c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
5105c6c1daeSBarry Smith 
5115c6c1daeSBarry Smith   PetscFunctionBegin;
5125c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5135c6c1daeSBarry Smith   PetscValidPointer(name,2);
5145c6c1daeSBarry Smith   *name = vmath->objName;
5155c6c1daeSBarry Smith   PetscFunctionReturn(0);
5165c6c1daeSBarry Smith }
5175c6c1daeSBarry Smith 
5185c6c1daeSBarry Smith #undef __FUNCT__
5195c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetName"
5205c6c1daeSBarry Smith /*@C
5215c6c1daeSBarry Smith   PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica
5225c6c1daeSBarry Smith 
5235c6c1daeSBarry Smith   Input Parameters:
5245c6c1daeSBarry Smith . viewer - The Mathematica viewer
5255c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
5265c6c1daeSBarry Smith 
5275c6c1daeSBarry Smith   Level: intermediate
5285c6c1daeSBarry Smith 
5295c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
5305c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
5315c6c1daeSBarry Smith @*/
5325c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetName(PetscViewer viewer, const char name[])
5335c6c1daeSBarry Smith {
5345c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
5355c6c1daeSBarry Smith 
5365c6c1daeSBarry Smith   PetscFunctionBegin;
5375c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5385c6c1daeSBarry Smith   PetscValidPointer(name,2);
5395c6c1daeSBarry Smith   vmath->objName = name;
5405c6c1daeSBarry Smith   PetscFunctionReturn(0);
5415c6c1daeSBarry Smith }
5425c6c1daeSBarry Smith 
5435c6c1daeSBarry Smith #undef __FUNCT__
5445c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaClearName"
5455c6c1daeSBarry Smith /*@C
5465c6c1daeSBarry Smith   PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica
5475c6c1daeSBarry Smith 
5485c6c1daeSBarry Smith   Input Parameter:
5495c6c1daeSBarry Smith . viewer - The Mathematica viewer
5505c6c1daeSBarry Smith 
5515c6c1daeSBarry Smith   Level: intermediate
5525c6c1daeSBarry Smith 
5535c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
5545c6c1daeSBarry Smith .seealso PetscViewerMathematicaGetName(), PetscViewerMathematicaSetName()
5555c6c1daeSBarry Smith @*/
5565c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaClearName(PetscViewer viewer)
5575c6c1daeSBarry Smith {
5585c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
5595c6c1daeSBarry Smith 
5605c6c1daeSBarry Smith   PetscFunctionBegin;
5615c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5625c6c1daeSBarry Smith   vmath->objName = PETSC_NULL;
5635c6c1daeSBarry Smith   PetscFunctionReturn(0);
5645c6c1daeSBarry Smith }
5655c6c1daeSBarry Smith 
5665c6c1daeSBarry Smith #undef __FUNCT__
5675c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetVector"
5685c6c1daeSBarry Smith /*@C
5695c6c1daeSBarry Smith   PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica
5705c6c1daeSBarry Smith 
5715c6c1daeSBarry Smith   Input Parameter:
5725c6c1daeSBarry Smith . viewer - The Mathematica viewer
5735c6c1daeSBarry Smith 
5745c6c1daeSBarry Smith   Output Parameter:
5755c6c1daeSBarry Smith . v      - The vector
5765c6c1daeSBarry Smith 
5775c6c1daeSBarry Smith   Level: intermediate
5785c6c1daeSBarry Smith 
5795c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
5805c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaPutVector()
5815c6c1daeSBarry Smith @*/
5820adebc6cSBarry Smith PetscErrorCode  PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v)
5830adebc6cSBarry Smith {
5845c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
5855c6c1daeSBarry Smith   MLINK                   link;   /* The link to Mathematica */
5865c6c1daeSBarry Smith   char                    *name;
5875c6c1daeSBarry Smith   PetscScalar             *mArray,*array;
5885c6c1daeSBarry Smith   long                    mSize;
5895c6c1daeSBarry Smith   int                     n;
5905c6c1daeSBarry Smith   PetscErrorCode          ierr;
5915c6c1daeSBarry Smith 
5925c6c1daeSBarry Smith   PetscFunctionBegin;
5935c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
5945c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,      VEC_CLASSID,2);
5955c6c1daeSBarry Smith 
5965c6c1daeSBarry Smith   /* Determine the object name */
5975c6c1daeSBarry Smith   if (!vmath->objName) {
5985c6c1daeSBarry Smith     name = "vec";
5995c6c1daeSBarry Smith   } else {
6005c6c1daeSBarry Smith     name = (char *) vmath->objName;
6015c6c1daeSBarry Smith   }
6025c6c1daeSBarry Smith 
6035c6c1daeSBarry Smith   link = vmath->link;
6045c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
6055c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
6065c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6075c6c1daeSBarry Smith     MLPutSymbol(link, name);
6085c6c1daeSBarry Smith   MLEndPacket(link);
6095c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6105c6c1daeSBarry Smith   MLGetRealList(link, &mArray, &mSize);
6115c6c1daeSBarry Smith   if (n != mSize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize);
6125c6c1daeSBarry Smith   ierr = PetscMemcpy(array, mArray, mSize * sizeof(double));CHKERRQ(ierr);
6135c6c1daeSBarry Smith   MLDisownRealList(link, mArray, mSize);
6145c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
6155c6c1daeSBarry Smith 
6165c6c1daeSBarry Smith   PetscFunctionReturn(0);
6175c6c1daeSBarry Smith }
6185c6c1daeSBarry Smith 
6195c6c1daeSBarry Smith #undef __FUNCT__
6205c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaPutVector"
6215c6c1daeSBarry Smith /*@C
6225c6c1daeSBarry Smith   PetscViewerMathematicaPutVector - Send a vector to Mathematica
6235c6c1daeSBarry Smith 
6245c6c1daeSBarry Smith   Input Parameters:
6255c6c1daeSBarry Smith + viewer - The Mathematica viewer
6265c6c1daeSBarry Smith - v      - The vector
6275c6c1daeSBarry Smith 
6285c6c1daeSBarry Smith   Level: intermediate
6295c6c1daeSBarry Smith 
6305c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
6315c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaGetVector()
6325c6c1daeSBarry Smith @*/
6335c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v)
6345c6c1daeSBarry Smith {
6355c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
6365c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
6375c6c1daeSBarry Smith   char                    *name;
6385c6c1daeSBarry Smith   PetscScalar             *array;
6395c6c1daeSBarry Smith   int                     n;
6405c6c1daeSBarry Smith   PetscErrorCode          ierr;
6415c6c1daeSBarry Smith 
6425c6c1daeSBarry Smith   PetscFunctionBegin;
6435c6c1daeSBarry Smith   /* Determine the object name */
6445c6c1daeSBarry Smith   if (!vmath->objName) {
6455c6c1daeSBarry Smith     name = "vec";
6465c6c1daeSBarry Smith   } else {
6475c6c1daeSBarry Smith     name = (char *) vmath->objName;
6485c6c1daeSBarry Smith   }
6495c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
6505c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
6515c6c1daeSBarry Smith 
6525c6c1daeSBarry Smith   /* Send the Vector object */
6535c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6545c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
6555c6c1daeSBarry Smith       MLPutSymbol(link, name);
6565c6c1daeSBarry Smith       MLPutRealList(link, array, n);
6575c6c1daeSBarry Smith   MLEndPacket(link);
6585c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6595c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6605c6c1daeSBarry Smith   /* Skip ReturnPacket */
6615c6c1daeSBarry Smith   MLNewPacket(link);
6625c6c1daeSBarry Smith 
6635c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
6645c6c1daeSBarry Smith   PetscFunctionReturn(0);
6655c6c1daeSBarry Smith }
6665c6c1daeSBarry Smith 
6675c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a)
6685c6c1daeSBarry Smith {
6695c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
6705c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
6715c6c1daeSBarry Smith   char                    *name;
6725c6c1daeSBarry Smith   PetscErrorCode          ierr;
6735c6c1daeSBarry Smith 
6745c6c1daeSBarry Smith   PetscFunctionBegin;
6755c6c1daeSBarry Smith   /* Determine the object name */
6765c6c1daeSBarry Smith   if (!vmath->objName) {
6775c6c1daeSBarry Smith     name = "mat";
6785c6c1daeSBarry Smith   } else {
6795c6c1daeSBarry Smith     name = (char *) vmath->objName;
6805c6c1daeSBarry Smith   }
6815c6c1daeSBarry Smith 
6825c6c1daeSBarry Smith   /* Send the dense matrix object */
6835c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
6845c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
6855c6c1daeSBarry Smith       MLPutSymbol(link, name);
6865c6c1daeSBarry Smith       MLPutFunction(link, "Transpose", 1);
6875c6c1daeSBarry Smith         MLPutFunction(link, "Partition", 2);
6885c6c1daeSBarry Smith           MLPutRealList(link, a, m*n);
6895c6c1daeSBarry Smith           MLPutInteger(link, m);
6905c6c1daeSBarry Smith   MLEndPacket(link);
6915c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
6925c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
6935c6c1daeSBarry Smith   /* Skip ReturnPacket */
6945c6c1daeSBarry Smith   MLNewPacket(link);
6955c6c1daeSBarry Smith 
6965c6c1daeSBarry Smith   PetscFunctionReturn(0);
6975c6c1daeSBarry Smith }
6985c6c1daeSBarry Smith 
6995c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
7005c6c1daeSBarry Smith {
7015c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
7025c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
7035c6c1daeSBarry Smith   const char              *symbol;
7045c6c1daeSBarry Smith   char                    *name;
7055c6c1daeSBarry Smith   PetscBool               match;
7065c6c1daeSBarry Smith   PetscErrorCode          ierr;
7075c6c1daeSBarry Smith 
7085c6c1daeSBarry Smith   PetscFunctionBegin;
7095c6c1daeSBarry Smith   /* Determine the object name */
7105c6c1daeSBarry Smith   if (!vmath->objName) {
7115c6c1daeSBarry Smith     name = "mat";
7125c6c1daeSBarry Smith   } else {
7135c6c1daeSBarry Smith     name = (char *) vmath->objName;
7145c6c1daeSBarry Smith   }
7155c6c1daeSBarry Smith 
7165c6c1daeSBarry Smith   /* Make sure Mathematica recognizes sparse matrices */
7175c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
7185c6c1daeSBarry Smith     MLPutFunction(link, "Needs", 1);
7195c6c1daeSBarry Smith       MLPutString(link, "LinearAlgebra`CSRMatrix`");
7205c6c1daeSBarry Smith   MLEndPacket(link);
7215c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
7225c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
7235c6c1daeSBarry Smith   /* Skip ReturnPacket */
7245c6c1daeSBarry Smith   MLNewPacket(link);
7255c6c1daeSBarry Smith 
7265c6c1daeSBarry Smith   /* Send the CSRMatrix object */
7275c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
7285c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
7295c6c1daeSBarry Smith       MLPutSymbol(link, name);
7305c6c1daeSBarry Smith       MLPutFunction(link, "CSRMatrix", 5);
7315c6c1daeSBarry Smith         MLPutInteger(link, m);
7325c6c1daeSBarry Smith         MLPutInteger(link, n);
7335c6c1daeSBarry Smith         MLPutFunction(link, "Plus", 2);
7345c6c1daeSBarry Smith           MLPutIntegerList(link, i, m+1);
7355c6c1daeSBarry Smith           MLPutInteger(link, 1);
7365c6c1daeSBarry Smith         MLPutFunction(link, "Plus", 2);
7375c6c1daeSBarry Smith           MLPutIntegerList(link, j, i[m]);
7385c6c1daeSBarry Smith           MLPutInteger(link, 1);
7395c6c1daeSBarry Smith         MLPutRealList(link, a, i[m]);
7405c6c1daeSBarry Smith   MLEndPacket(link);
7415c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
7425c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
7435c6c1daeSBarry Smith   /* Skip ReturnPacket */
7445c6c1daeSBarry Smith   MLNewPacket(link);
7455c6c1daeSBarry Smith 
7465c6c1daeSBarry Smith   /* Check that matrix is valid */
7475c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
7485c6c1daeSBarry Smith     MLPutFunction(link, "ValidQ", 1);
7495c6c1daeSBarry Smith       MLPutSymbol(link, name);
7505c6c1daeSBarry Smith   MLEndPacket(link);
7515c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
7525c6c1daeSBarry Smith   MLGetSymbol(link, &symbol);
7535c6c1daeSBarry Smith   ierr = PetscStrcmp("True", (char *) symbol, &match);CHKERRQ(ierr);
7545c6c1daeSBarry Smith   if (!match) {
7555c6c1daeSBarry Smith     MLDisownSymbol(link, symbol);
7565c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
7575c6c1daeSBarry Smith   }
7585c6c1daeSBarry Smith   MLDisownSymbol(link, symbol);
7595c6c1daeSBarry Smith   /* Skip ReturnPacket */
7605c6c1daeSBarry Smith   MLNewPacket(link);
7615c6c1daeSBarry Smith 
7625c6c1daeSBarry Smith   PetscFunctionReturn(0);
7635c6c1daeSBarry Smith }
7645c6c1daeSBarry Smith 
765