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 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; 765c6c1daeSBarry Smith if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) { 779566063dSJacob Faibussowitsch PetscCall(PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE)); 785c6c1daeSBarry Smith } 795c6c1daeSBarry Smith PetscFunctionReturn(0); 805c6c1daeSBarry Smith } 815c6c1daeSBarry Smith 825c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v) 835c6c1daeSBarry Smith { 845c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; 85519f805aSKarl Rupp #if defined(MATHEMATICA_3_0) 865c6c1daeSBarry Smith int argc = 6; 875c6c1daeSBarry Smith char *argv[6]; 885c6c1daeSBarry Smith #else 895c6c1daeSBarry Smith int argc = 5; 905c6c1daeSBarry Smith char *argv[5]; 915c6c1daeSBarry Smith #endif 925c6c1daeSBarry Smith char hostname[256]; 935c6c1daeSBarry Smith long lerr; 945c6c1daeSBarry Smith 955c6c1daeSBarry Smith PetscFunctionBegin; 965c6c1daeSBarry Smith /* Link name */ 975c6c1daeSBarry Smith argv[0] = "-linkname"; 98a297a907SKarl Rupp if (!vmath->linkname) argv[1] = "math -mathlink"; 99a297a907SKarl Rupp else argv[1] = vmath->linkname; 1005c6c1daeSBarry Smith 1015c6c1daeSBarry Smith /* Link host */ 1025c6c1daeSBarry Smith argv[2] = "-linkhost"; 1035c6c1daeSBarry Smith if (!vmath->linkhost) { 1049566063dSJacob Faibussowitsch PetscCall(PetscGetHostName(hostname, sizeof(hostname))); 1055c6c1daeSBarry Smith argv[3] = hostname; 106a297a907SKarl Rupp } else argv[3] = vmath->linkhost; 1075c6c1daeSBarry Smith 1085c6c1daeSBarry Smith /* Link mode */ 109519f805aSKarl Rupp #if defined(MATHEMATICA_3_0) 1105c6c1daeSBarry Smith argv[4] = "-linkmode"; 1115c6c1daeSBarry Smith switch (vmath->linkmode) { 1125c6c1daeSBarry Smith case MATHEMATICA_LINK_CREATE: 1135c6c1daeSBarry Smith argv[5] = "Create"; 1145c6c1daeSBarry Smith break; 1155c6c1daeSBarry Smith case MATHEMATICA_LINK_CONNECT: 1165c6c1daeSBarry Smith argv[5] = "Connect"; 1175c6c1daeSBarry Smith break; 1185c6c1daeSBarry Smith case MATHEMATICA_LINK_LAUNCH: 1195c6c1daeSBarry Smith argv[5] = "Launch"; 1205c6c1daeSBarry Smith break; 1215c6c1daeSBarry Smith } 1225c6c1daeSBarry Smith #else 1235c6c1daeSBarry Smith switch (vmath->linkmode) { 1245c6c1daeSBarry Smith case MATHEMATICA_LINK_CREATE: 1255c6c1daeSBarry Smith argv[4] = "-linkcreate"; 1265c6c1daeSBarry Smith break; 1275c6c1daeSBarry Smith case MATHEMATICA_LINK_CONNECT: 1285c6c1daeSBarry Smith argv[4] = "-linkconnect"; 1295c6c1daeSBarry Smith break; 1305c6c1daeSBarry Smith case MATHEMATICA_LINK_LAUNCH: 1315c6c1daeSBarry Smith argv[4] = "-linklaunch"; 1325c6c1daeSBarry Smith break; 1335c6c1daeSBarry Smith } 1345c6c1daeSBarry Smith #endif 1355c6c1daeSBarry Smith vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr); 1365c6c1daeSBarry Smith #endif 1375c6c1daeSBarry Smith PetscFunctionReturn(0); 1385c6c1daeSBarry Smith } 1395c6c1daeSBarry Smith 1408cc058d9SJed Brown PETSC_EXTERN PetscErrorCode PetscViewerCreate_Mathematica(PetscViewer v) 1415c6c1daeSBarry Smith { 1425c6c1daeSBarry Smith PetscViewer_Mathematica *vmath; 1435c6c1daeSBarry Smith 1445c6c1daeSBarry Smith PetscFunctionBegin; 1459566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaInitializePackage()); 1465c6c1daeSBarry Smith 1479566063dSJacob Faibussowitsch PetscCall(PetscNewLog(v,&vmath)); 1485c6c1daeSBarry Smith v->data = (void*) vmath; 1495c6c1daeSBarry Smith v->ops->destroy = PetscViewerDestroy_Mathematica; 1505c6c1daeSBarry Smith v->ops->flush = 0; 1519566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name)); 1525c6c1daeSBarry Smith 1530298fd71SBarry Smith vmath->linkname = NULL; 1540298fd71SBarry Smith vmath->linkhost = NULL; 1555c6c1daeSBarry Smith vmath->linkmode = MATHEMATICA_LINK_CONNECT; 1565c6c1daeSBarry Smith vmath->graphicsType = GRAPHICS_MOTIF; 1575c6c1daeSBarry Smith vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT; 1580298fd71SBarry Smith vmath->objName = NULL; 1595c6c1daeSBarry Smith 1609566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetFromOptions(v)); 1619566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetupConnection_Private(v)); 1625c6c1daeSBarry Smith PetscFunctionReturn(0); 1635c6c1daeSBarry Smith } 1645c6c1daeSBarry Smith 16595c0884eSLisandro Dalcin static PetscErrorCode PetscViewerMathematicaParseLinkMode(char *modename, LinkMode *mode) 1660adebc6cSBarry Smith { 1675c6c1daeSBarry Smith PetscBool isCreate, isConnect, isLaunch; 1685c6c1daeSBarry Smith 1695c6c1daeSBarry Smith PetscFunctionBegin; 1709566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(modename, "Create", &isCreate)); 1719566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(modename, "Connect", &isConnect)); 1729566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(modename, "Launch", &isLaunch)); 173a297a907SKarl Rupp if (isCreate) *mode = MATHEMATICA_LINK_CREATE; 174a297a907SKarl Rupp else if (isConnect) *mode = MATHEMATICA_LINK_CONNECT; 175a297a907SKarl Rupp else if (isLaunch) *mode = MATHEMATICA_LINK_LAUNCH; 17698921bdaSJacob Faibussowitsch else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename); 1775c6c1daeSBarry Smith PetscFunctionReturn(0); 1785c6c1daeSBarry Smith } 1795c6c1daeSBarry Smith 1805c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetFromOptions(PetscViewer v) 1815c6c1daeSBarry Smith { 1825c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; 1835c6c1daeSBarry Smith char linkname[256]; 1845c6c1daeSBarry Smith char modename[256]; 1855c6c1daeSBarry Smith char hostname[256]; 1865c6c1daeSBarry Smith char type[256]; 1875c6c1daeSBarry Smith PetscInt numPorts; 1885c6c1daeSBarry Smith PetscInt *ports; 1895c6c1daeSBarry Smith PetscInt numHosts; 1905c6c1daeSBarry Smith int h; 1915c6c1daeSBarry Smith char **hosts; 1925c6c1daeSBarry Smith PetscMPIInt size, rank; 1935c6c1daeSBarry Smith PetscBool opt; 1945c6c1daeSBarry Smith 1955c6c1daeSBarry Smith PetscFunctionBegin; 1969566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)v), &size)); 1979566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)v), &rank)); 1985c6c1daeSBarry Smith 1995c6c1daeSBarry Smith /* Get link name */ 2009566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString("viewer_", "-math_linkname", linkname, sizeof(linkname), &opt)); 2015c6c1daeSBarry Smith if (opt) { 2029566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkName(v, linkname)); 2035c6c1daeSBarry Smith } 2045c6c1daeSBarry Smith /* Get link port */ 2055c6c1daeSBarry Smith numPorts = size; 2069566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size, &ports)); 2079566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt)); 2085c6c1daeSBarry Smith if (opt) { 209589a23caSBarry Smith if (numPorts > rank) snprintf(linkname, sizeof(linkname), "%6d", ports[rank]); 210589a23caSBarry Smith else snprintf(linkname, sizeof(linkname), "%6d", ports[0]); 2119566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkName(v, linkname)); 2125c6c1daeSBarry Smith } 2139566063dSJacob Faibussowitsch PetscCall(PetscFree(ports)); 2145c6c1daeSBarry Smith /* Get link host */ 2155c6c1daeSBarry Smith numHosts = size; 2169566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(size, &hosts)); 2179566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt)); 2185c6c1daeSBarry Smith if (opt) { 2195c6c1daeSBarry Smith if (numHosts > rank) { 2209566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(hostname, hosts[rank], sizeof(hostname))); 2215c6c1daeSBarry Smith } else { 2229566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(hostname, hosts[0], sizeof(hostname))); 2235c6c1daeSBarry Smith } 2249566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkHost(v, hostname)); 2255c6c1daeSBarry Smith } 2265c6c1daeSBarry Smith for (h = 0; h < numHosts; h++) { 2279566063dSJacob Faibussowitsch PetscCall(PetscFree(hosts[h])); 2285c6c1daeSBarry Smith } 2299566063dSJacob Faibussowitsch PetscCall(PetscFree(hosts)); 2305c6c1daeSBarry Smith /* Get link mode */ 2319566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString("viewer_", "-math_linkmode", modename, sizeof(modename), &opt)); 2325c6c1daeSBarry Smith if (opt) { 2335c6c1daeSBarry Smith LinkMode mode; 2345c6c1daeSBarry Smith 2359566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaParseLinkMode(modename, &mode)); 2369566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkMode(v, mode)); 2375c6c1daeSBarry Smith } 2385c6c1daeSBarry Smith /* Get graphics type */ 2399566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString("viewer_", "-math_graphics", type, sizeof(type), &opt)); 2405c6c1daeSBarry Smith if (opt) { 2415c6c1daeSBarry Smith PetscBool isMotif, isPS, isPSFile; 2425c6c1daeSBarry Smith 2439566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "Motif", &isMotif)); 2449566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "PS", &isPS)); 2459566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "PSFile", &isPSFile)); 246a297a907SKarl Rupp if (isMotif) vmath->graphicsType = GRAPHICS_MOTIF; 247a297a907SKarl Rupp else if (isPS) vmath->graphicsType = GRAPHICS_PS_STDOUT; 248a297a907SKarl Rupp else if (isPSFile) vmath->graphicsType = GRAPHICS_PS_FILE; 2495c6c1daeSBarry Smith } 2505c6c1daeSBarry Smith /* Get plot type */ 2519566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetString("viewer_", "-math_type", type, sizeof(type), &opt)); 2525c6c1daeSBarry Smith if (opt) { 2535c6c1daeSBarry Smith PetscBool isTri, isVecTri, isVec, isSurface; 2545c6c1daeSBarry Smith 2559566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "Triangulation", &isTri)); 2569566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "VectorTriangulation", &isVecTri)); 2579566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "Vector", &isVec)); 2589566063dSJacob Faibussowitsch PetscCall(PetscStrcasecmp(type, "Surface", &isSurface)); 259a297a907SKarl Rupp if (isTri) vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT; 260a297a907SKarl Rupp else if (isVecTri) vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT; 261a297a907SKarl Rupp else if (isVec) vmath->plotType = MATHEMATICA_VECTOR_PLOT; 262a297a907SKarl Rupp else if (isSurface) vmath->plotType = MATHEMATICA_SURFACE_PLOT; 2635c6c1daeSBarry Smith } 2645c6c1daeSBarry Smith PetscFunctionReturn(0); 2655c6c1daeSBarry Smith } 2665c6c1daeSBarry Smith 2670adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name) 2680adebc6cSBarry Smith { 2695c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; 2705c6c1daeSBarry Smith 2715c6c1daeSBarry Smith PetscFunctionBegin; 2725c6c1daeSBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 2735c6c1daeSBarry Smith PetscValidCharPointer(name,2); 2749566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(name, &vmath->linkname)); 2755c6c1daeSBarry Smith PetscFunctionReturn(0); 2765c6c1daeSBarry Smith } 2775c6c1daeSBarry Smith 2780adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaSetLinkPort(PetscViewer v, int port) 2790adebc6cSBarry Smith { 2805c6c1daeSBarry Smith char name[16]; 2815c6c1daeSBarry Smith 2825c6c1daeSBarry Smith PetscFunctionBegin; 2835c6c1daeSBarry Smith snprintf(name, 16, "%6d", port); 2849566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkName(v, name)); 2855c6c1daeSBarry Smith PetscFunctionReturn(0); 2865c6c1daeSBarry Smith } 2875c6c1daeSBarry Smith 2880adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host) 2890adebc6cSBarry Smith { 2905c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; 2915c6c1daeSBarry Smith 2925c6c1daeSBarry Smith PetscFunctionBegin; 2935c6c1daeSBarry Smith PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 2945c6c1daeSBarry Smith PetscValidCharPointer(host,2); 2959566063dSJacob Faibussowitsch PetscCall(PetscStrallocpy(host, &vmath->linkhost)); 2965c6c1daeSBarry Smith PetscFunctionReturn(0); 2975c6c1daeSBarry Smith } 2985c6c1daeSBarry Smith 2990adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode) 3000adebc6cSBarry Smith { 3015c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) v->data; 3025c6c1daeSBarry Smith 3035c6c1daeSBarry Smith PetscFunctionBegin; 3045c6c1daeSBarry Smith vmath->linkmode = mode; 3055c6c1daeSBarry Smith PetscFunctionReturn(0); 3065c6c1daeSBarry Smith } 3075c6c1daeSBarry Smith 3085c6c1daeSBarry Smith /*----------------------------------------- Public Functions --------------------------------------------------------*/ 3095c6c1daeSBarry Smith /*@C 3105c6c1daeSBarry Smith PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink. 3115c6c1daeSBarry Smith 312d083f849SBarry Smith Collective 3135c6c1daeSBarry Smith 3145c6c1daeSBarry Smith Input Parameters: 3155c6c1daeSBarry Smith + comm - The MPI communicator 3165c6c1daeSBarry Smith . port - [optional] The port to connect on, or PETSC_DECIDE 3170298fd71SBarry Smith . machine - [optional] The machine to run Mathematica on, or NULL 3180298fd71SBarry Smith - mode - [optional] The connection mode, or NULL 3195c6c1daeSBarry Smith 3205c6c1daeSBarry Smith Output Parameter: 3215c6c1daeSBarry Smith . viewer - The Mathematica viewer 3225c6c1daeSBarry Smith 3235c6c1daeSBarry Smith Level: intermediate 3245c6c1daeSBarry Smith 3255c6c1daeSBarry Smith Notes: 3265c6c1daeSBarry Smith Most users should employ the following commands to access the 3275c6c1daeSBarry Smith Mathematica viewers 3285c6c1daeSBarry Smith $ 3295c6c1daeSBarry Smith $ PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer) 3305c6c1daeSBarry Smith $ MatView(Mat matrix, PetscViewer viewer) 3315c6c1daeSBarry Smith $ 3325c6c1daeSBarry Smith $ or 3335c6c1daeSBarry Smith $ 3345c6c1daeSBarry Smith $ PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer) 3355c6c1daeSBarry Smith $ VecView(Vec vector, PetscViewer viewer) 3365c6c1daeSBarry Smith 3375c6c1daeSBarry Smith Options Database Keys: 338e1bc860dSBarry Smith + -viewer_math_linkhost <machine> - The host machine for the kernel 339e1bc860dSBarry Smith . -viewer_math_linkname <name> - The full link name for the connection 340e1bc860dSBarry Smith . -viewer_math_linkport <port> - The port for the connection 341e1bc860dSBarry Smith . -viewer_math_mode <mode> - The mode, e.g. Launch, Connect 342e1bc860dSBarry Smith . -viewer_math_type <type> - The plot type, e.g. Triangulation, Vector 343e1bc860dSBarry Smith - -viewer_math_graphics <output> - The output type, e.g. Motif, PS, PSFile 3445c6c1daeSBarry Smith 3455c6c1daeSBarry Smith .seealso: MatView(), VecView() 3465c6c1daeSBarry Smith @*/ 3475c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v) 3485c6c1daeSBarry Smith { 3495c6c1daeSBarry Smith PetscFunctionBegin; 3509566063dSJacob Faibussowitsch PetscCall(PetscViewerCreate(comm, v)); 3515c6c1daeSBarry Smith #if 0 3525c6c1daeSBarry Smith LinkMode linkmode; 3539566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkPort(*v, port)); 3549566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkHost(*v, machine)); 3559566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaParseLinkMode(mode, &linkmode)); 3569566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSetLinkMode(*v, linkmode)); 3575c6c1daeSBarry Smith #endif 3589566063dSJacob Faibussowitsch PetscCall(PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA)); 3595c6c1daeSBarry Smith PetscFunctionReturn(0); 3605c6c1daeSBarry Smith } 3615c6c1daeSBarry Smith 3625c6c1daeSBarry Smith /*@C 3635c6c1daeSBarry Smith PetscViewerMathematicaGetLink - Returns the link to Mathematica 3645c6c1daeSBarry Smith 3655c6c1daeSBarry Smith Input Parameters: 366a2b725a8SWilliam Gropp + viewer - The Mathematica viewer 367a2b725a8SWilliam Gropp - link - The link to Mathematica 3685c6c1daeSBarry Smith 3695c6c1daeSBarry Smith Level: intermediate 3705c6c1daeSBarry Smith 3715c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, link 3725c6c1daeSBarry Smith .seealso PetscViewerMathematicaOpen() 3735c6c1daeSBarry Smith @*/ 3745c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link) 3755c6c1daeSBarry Smith { 3765c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 3775c6c1daeSBarry Smith 3785c6c1daeSBarry Smith PetscFunctionBegin; 3795c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 3805c6c1daeSBarry Smith *link = vmath->link; 3815c6c1daeSBarry Smith PetscFunctionReturn(0); 3825c6c1daeSBarry Smith } 3835c6c1daeSBarry Smith 3845c6c1daeSBarry Smith /*@C 3855c6c1daeSBarry Smith PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received 3865c6c1daeSBarry Smith 3875c6c1daeSBarry Smith Input Parameters: 388a2b725a8SWilliam Gropp + viewer - The Mathematica viewer 389a2b725a8SWilliam Gropp - type - The packet type to search for, e.g RETURNPKT 3905c6c1daeSBarry Smith 3915c6c1daeSBarry Smith Level: advanced 3925c6c1daeSBarry Smith 3935c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, packets 3945c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaGetVector() 3955c6c1daeSBarry Smith @*/ 3965c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type) 3975c6c1daeSBarry Smith { 3985c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 3995c6c1daeSBarry Smith MLINK link = vmath->link; /* The link to Mathematica */ 4005c6c1daeSBarry Smith int pkt; /* The packet type */ 4015c6c1daeSBarry Smith 4025c6c1daeSBarry Smith PetscFunctionBegin; 403a297a907SKarl Rupp while ((pkt = MLNextPacket(link)) && (pkt != type)) MLNewPacket(link); 4045c6c1daeSBarry Smith if (!pkt) { 4055c6c1daeSBarry Smith MLClearError(link); 4065c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char*) MLErrorMessage(link)); 4075c6c1daeSBarry Smith } 4085c6c1daeSBarry Smith PetscFunctionReturn(0); 4095c6c1daeSBarry Smith } 4105c6c1daeSBarry Smith 4115c6c1daeSBarry Smith /*@C 4125c6c1daeSBarry Smith PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica 4135c6c1daeSBarry Smith 4145c6c1daeSBarry Smith Input Parameter: 4155c6c1daeSBarry Smith . viewer - The Mathematica viewer 4165c6c1daeSBarry Smith 4175c6c1daeSBarry Smith Output Parameter: 4185c6c1daeSBarry Smith . name - The name for new objects created in Mathematica 4195c6c1daeSBarry Smith 4205c6c1daeSBarry Smith Level: intermediate 4215c6c1daeSBarry Smith 4225c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name 4235c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName() 4245c6c1daeSBarry Smith @*/ 4255c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaGetName(PetscViewer viewer, const char **name) 4265c6c1daeSBarry Smith { 4275c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 4285c6c1daeSBarry Smith 4295c6c1daeSBarry Smith PetscFunctionBegin; 4305c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 4315c6c1daeSBarry Smith PetscValidPointer(name,2); 4325c6c1daeSBarry Smith *name = vmath->objName; 4335c6c1daeSBarry Smith PetscFunctionReturn(0); 4345c6c1daeSBarry Smith } 4355c6c1daeSBarry Smith 4365c6c1daeSBarry Smith /*@C 4375c6c1daeSBarry Smith PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica 4385c6c1daeSBarry Smith 4395c6c1daeSBarry Smith Input Parameters: 440a2b725a8SWilliam Gropp + viewer - The Mathematica viewer 441a2b725a8SWilliam Gropp - name - The name for new objects created in Mathematica 4425c6c1daeSBarry Smith 4435c6c1daeSBarry Smith Level: intermediate 4445c6c1daeSBarry Smith 4455c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name 4465c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName() 4475c6c1daeSBarry Smith @*/ 4485c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetName(PetscViewer viewer, const char name[]) 4495c6c1daeSBarry Smith { 4505c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 4515c6c1daeSBarry Smith 4525c6c1daeSBarry Smith PetscFunctionBegin; 4535c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 4545c6c1daeSBarry Smith PetscValidPointer(name,2); 4555c6c1daeSBarry Smith vmath->objName = name; 4565c6c1daeSBarry Smith PetscFunctionReturn(0); 4575c6c1daeSBarry Smith } 4585c6c1daeSBarry Smith 4595c6c1daeSBarry Smith /*@C 4605c6c1daeSBarry Smith PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica 4615c6c1daeSBarry Smith 4625c6c1daeSBarry Smith Input Parameter: 4635c6c1daeSBarry Smith . viewer - The Mathematica viewer 4645c6c1daeSBarry Smith 4655c6c1daeSBarry Smith Level: intermediate 4665c6c1daeSBarry Smith 4675c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name 4685c6c1daeSBarry Smith .seealso PetscViewerMathematicaGetName(), PetscViewerMathematicaSetName() 4695c6c1daeSBarry Smith @*/ 4705c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaClearName(PetscViewer viewer) 4715c6c1daeSBarry Smith { 4725c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 4735c6c1daeSBarry Smith 4745c6c1daeSBarry Smith PetscFunctionBegin; 4755c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 4760298fd71SBarry Smith vmath->objName = NULL; 4775c6c1daeSBarry Smith PetscFunctionReturn(0); 4785c6c1daeSBarry Smith } 4795c6c1daeSBarry Smith 4805c6c1daeSBarry Smith /*@C 4815c6c1daeSBarry Smith PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica 4825c6c1daeSBarry Smith 4835c6c1daeSBarry Smith Input Parameter: 4845c6c1daeSBarry Smith . viewer - The Mathematica viewer 4855c6c1daeSBarry Smith 4865c6c1daeSBarry Smith Output Parameter: 4875c6c1daeSBarry Smith . v - The vector 4885c6c1daeSBarry Smith 4895c6c1daeSBarry Smith Level: intermediate 4905c6c1daeSBarry Smith 4915c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector 4925c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaPutVector() 4935c6c1daeSBarry Smith @*/ 4940adebc6cSBarry Smith PetscErrorCode PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v) 4950adebc6cSBarry Smith { 4965c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 4975c6c1daeSBarry Smith MLINK link; /* The link to Mathematica */ 4985c6c1daeSBarry Smith char *name; 4995c6c1daeSBarry Smith PetscScalar *mArray,*array; 5005c6c1daeSBarry Smith long mSize; 5015c6c1daeSBarry Smith int n; 5025c6c1daeSBarry Smith 5035c6c1daeSBarry Smith PetscFunctionBegin; 5045c6c1daeSBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 5055c6c1daeSBarry Smith PetscValidHeaderSpecific(v, VEC_CLASSID,2); 5065c6c1daeSBarry Smith 5075c6c1daeSBarry Smith /* Determine the object name */ 508a297a907SKarl Rupp if (!vmath->objName) name = "vec"; 509a297a907SKarl Rupp else name = (char*) vmath->objName; 5105c6c1daeSBarry Smith 5115c6c1daeSBarry Smith link = vmath->link; 5129566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &n)); 5139566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &array)); 5145c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 5155c6c1daeSBarry Smith MLPutSymbol(link, name); 5165c6c1daeSBarry Smith MLEndPacket(link); 5179566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 5185c6c1daeSBarry Smith MLGetRealList(link, &mArray, &mSize); 519*08401ef6SPierre Jolivet PetscCheck(n == mSize,PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize); 5209566063dSJacob Faibussowitsch PetscCall(PetscArraycpy(array, mArray, mSize)); 5215c6c1daeSBarry Smith MLDisownRealList(link, mArray, mSize); 5229566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &array)); 5235c6c1daeSBarry Smith PetscFunctionReturn(0); 5245c6c1daeSBarry Smith } 5255c6c1daeSBarry Smith 5265c6c1daeSBarry Smith /*@C 5275c6c1daeSBarry Smith PetscViewerMathematicaPutVector - Send a vector to Mathematica 5285c6c1daeSBarry Smith 5295c6c1daeSBarry Smith Input Parameters: 5305c6c1daeSBarry Smith + viewer - The Mathematica viewer 5315c6c1daeSBarry Smith - v - The vector 5325c6c1daeSBarry Smith 5335c6c1daeSBarry Smith Level: intermediate 5345c6c1daeSBarry Smith 5355c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector 5365c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaGetVector() 5375c6c1daeSBarry Smith @*/ 5385c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v) 5395c6c1daeSBarry Smith { 5405c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 5415c6c1daeSBarry Smith MLINK link = vmath->link; /* The link to Mathematica */ 5425c6c1daeSBarry Smith char *name; 5435c6c1daeSBarry Smith PetscScalar *array; 5445c6c1daeSBarry Smith int n; 5455c6c1daeSBarry Smith 5465c6c1daeSBarry Smith PetscFunctionBegin; 5475c6c1daeSBarry Smith /* Determine the object name */ 548a297a907SKarl Rupp if (!vmath->objName) name = "vec"; 549a297a907SKarl Rupp else name = (char*) vmath->objName; 550a297a907SKarl Rupp 5519566063dSJacob Faibussowitsch PetscCall(VecGetLocalSize(v, &n)); 5529566063dSJacob Faibussowitsch PetscCall(VecGetArray(v, &array)); 5535c6c1daeSBarry Smith 5545c6c1daeSBarry Smith /* Send the Vector object */ 5555c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 5565c6c1daeSBarry Smith MLPutFunction(link, "Set", 2); 5575c6c1daeSBarry Smith MLPutSymbol(link, name); 5585c6c1daeSBarry Smith MLPutRealList(link, array, n); 5595c6c1daeSBarry Smith MLEndPacket(link); 5605c6c1daeSBarry Smith /* Skip packets until ReturnPacket */ 5619566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 5625c6c1daeSBarry Smith /* Skip ReturnPacket */ 5635c6c1daeSBarry Smith MLNewPacket(link); 5645c6c1daeSBarry Smith 5659566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(v, &array)); 5665c6c1daeSBarry Smith PetscFunctionReturn(0); 5675c6c1daeSBarry Smith } 5685c6c1daeSBarry Smith 5695c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a) 5705c6c1daeSBarry Smith { 5715c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 5725c6c1daeSBarry Smith MLINK link = vmath->link; /* The link to Mathematica */ 5735c6c1daeSBarry Smith char *name; 5745c6c1daeSBarry Smith 5755c6c1daeSBarry Smith PetscFunctionBegin; 5765c6c1daeSBarry Smith /* Determine the object name */ 577a297a907SKarl Rupp if (!vmath->objName) name = "mat"; 578a297a907SKarl Rupp else name = (char*) vmath->objName; 5795c6c1daeSBarry Smith 5805c6c1daeSBarry Smith /* Send the dense matrix object */ 5815c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 5825c6c1daeSBarry Smith MLPutFunction(link, "Set", 2); 5835c6c1daeSBarry Smith MLPutSymbol(link, name); 5845c6c1daeSBarry Smith MLPutFunction(link, "Transpose", 1); 5855c6c1daeSBarry Smith MLPutFunction(link, "Partition", 2); 5865c6c1daeSBarry Smith MLPutRealList(link, a, m*n); 5875c6c1daeSBarry Smith MLPutInteger(link, m); 5885c6c1daeSBarry Smith MLEndPacket(link); 5895c6c1daeSBarry Smith /* Skip packets until ReturnPacket */ 5909566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 5915c6c1daeSBarry Smith /* Skip ReturnPacket */ 5925c6c1daeSBarry Smith MLNewPacket(link); 5935c6c1daeSBarry Smith PetscFunctionReturn(0); 5945c6c1daeSBarry Smith } 5955c6c1daeSBarry Smith 5965c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a) 5975c6c1daeSBarry Smith { 5985c6c1daeSBarry Smith PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica*) viewer->data; 5995c6c1daeSBarry Smith MLINK link = vmath->link; /* The link to Mathematica */ 6005c6c1daeSBarry Smith const char *symbol; 6015c6c1daeSBarry Smith char *name; 6025c6c1daeSBarry Smith PetscBool match; 6035c6c1daeSBarry Smith 6045c6c1daeSBarry Smith PetscFunctionBegin; 6055c6c1daeSBarry Smith /* Determine the object name */ 606a297a907SKarl Rupp if (!vmath->objName) name = "mat"; 607a297a907SKarl Rupp else name = (char*) vmath->objName; 6085c6c1daeSBarry Smith 6095c6c1daeSBarry Smith /* Make sure Mathematica recognizes sparse matrices */ 6105c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 6115c6c1daeSBarry Smith MLPutFunction(link, "Needs", 1); 6125c6c1daeSBarry Smith MLPutString(link, "LinearAlgebra`CSRMatrix`"); 6135c6c1daeSBarry Smith MLEndPacket(link); 6145c6c1daeSBarry Smith /* Skip packets until ReturnPacket */ 6159566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 6165c6c1daeSBarry Smith /* Skip ReturnPacket */ 6175c6c1daeSBarry Smith MLNewPacket(link); 6185c6c1daeSBarry Smith 6195c6c1daeSBarry Smith /* Send the CSRMatrix object */ 6205c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 6215c6c1daeSBarry Smith MLPutFunction(link, "Set", 2); 6225c6c1daeSBarry Smith MLPutSymbol(link, name); 6235c6c1daeSBarry Smith MLPutFunction(link, "CSRMatrix", 5); 6245c6c1daeSBarry Smith MLPutInteger(link, m); 6255c6c1daeSBarry Smith MLPutInteger(link, n); 6265c6c1daeSBarry Smith MLPutFunction(link, "Plus", 2); 6275c6c1daeSBarry Smith MLPutIntegerList(link, i, m+1); 6285c6c1daeSBarry Smith MLPutInteger(link, 1); 6295c6c1daeSBarry Smith MLPutFunction(link, "Plus", 2); 6305c6c1daeSBarry Smith MLPutIntegerList(link, j, i[m]); 6315c6c1daeSBarry Smith MLPutInteger(link, 1); 6325c6c1daeSBarry Smith MLPutRealList(link, a, i[m]); 6335c6c1daeSBarry Smith MLEndPacket(link); 6345c6c1daeSBarry Smith /* Skip packets until ReturnPacket */ 6359566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 6365c6c1daeSBarry Smith /* Skip ReturnPacket */ 6375c6c1daeSBarry Smith MLNewPacket(link); 6385c6c1daeSBarry Smith 6395c6c1daeSBarry Smith /* Check that matrix is valid */ 6405c6c1daeSBarry Smith MLPutFunction(link, "EvaluatePacket", 1); 6415c6c1daeSBarry Smith MLPutFunction(link, "ValidQ", 1); 6425c6c1daeSBarry Smith MLPutSymbol(link, name); 6435c6c1daeSBarry Smith MLEndPacket(link); 6449566063dSJacob Faibussowitsch PetscCall(PetscViewerMathematicaSkipPackets(viewer, RETURNPKT)); 6455c6c1daeSBarry Smith MLGetSymbol(link, &symbol); 6469566063dSJacob Faibussowitsch PetscCall(PetscStrcmp("True", (char*) symbol, &match)); 6475c6c1daeSBarry Smith if (!match) { 6485c6c1daeSBarry Smith MLDisownSymbol(link, symbol); 6495c6c1daeSBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica"); 6505c6c1daeSBarry Smith } 6515c6c1daeSBarry Smith MLDisownSymbol(link, symbol); 6525c6c1daeSBarry Smith /* Skip ReturnPacket */ 6535c6c1daeSBarry Smith MLNewPacket(link); 6545c6c1daeSBarry Smith PetscFunctionReturn(0); 6555c6c1daeSBarry Smith } 656