xref: /petsc/src/sys/classes/viewer/impls/mathematica/mathematica.c (revision 5c6c1daec53e1d9ab0bec9db5309fd8fc7645b8d)
1*5c6c1daeSBarry Smith 
2*5c6c1daeSBarry Smith #include <petsc-private/viewerimpl.h>   /* "petscsys.h" */
3*5c6c1daeSBarry Smith #include <petsc-private/pcimpl.h>
4*5c6c1daeSBarry Smith #include <../src/mat/impls/aij/seq/aij.h>
5*5c6c1daeSBarry Smith #include <mathematica.h>
6*5c6c1daeSBarry Smith 
7*5c6c1daeSBarry Smith #if defined (PETSC_HAVE__SNPRINTF) && !defined(PETSC_HAVE_SNPRINTF)
8*5c6c1daeSBarry Smith #define snprintf _snprintf
9*5c6c1daeSBarry Smith #endif
10*5c6c1daeSBarry Smith 
11*5c6c1daeSBarry Smith PetscViewer  PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE = PETSC_NULL;
12*5c6c1daeSBarry Smith static void *mathematicaEnv                   = PETSC_NULL;
13*5c6c1daeSBarry Smith 
14*5c6c1daeSBarry Smith static PetscBool  PetscViewerMathematicaPackageInitialized = PETSC_FALSE;
15*5c6c1daeSBarry Smith #undef __FUNCT__
16*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaFinalizePackage"
17*5c6c1daeSBarry Smith /*@C
18*5c6c1daeSBarry Smith   PetscViewerMathematicaFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is
19*5c6c1daeSBarry Smith   called from PetscFinalize().
20*5c6c1daeSBarry Smith 
21*5c6c1daeSBarry Smith   Level: developer
22*5c6c1daeSBarry Smith 
23*5c6c1daeSBarry Smith .keywords: Petsc, destroy, package, mathematica
24*5c6c1daeSBarry Smith .seealso: PetscFinalize()
25*5c6c1daeSBarry Smith @*/
26*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaFinalizePackage(void)
27*5c6c1daeSBarry Smith {
28*5c6c1daeSBarry Smith   PetscFunctionBegin;
29*5c6c1daeSBarry Smith   if (mathematicaEnv) MLDeinitialize((MLEnvironment) mathematicaEnv);
30*5c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
31*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
32*5c6c1daeSBarry Smith }
33*5c6c1daeSBarry Smith 
34*5c6c1daeSBarry Smith #undef __FUNCT__
35*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaInitializePackage"
36*5c6c1daeSBarry Smith /*@C
37*5c6c1daeSBarry Smith   PetscViewerMathematicaInitializePackage - This function initializes everything in the Petsc interface to Mathematica. It is
38*5c6c1daeSBarry Smith   called from PetscDLLibraryRegister() when using dynamic libraries, and on the call to PetscInitialize()
39*5c6c1daeSBarry Smith   when using static libraries.
40*5c6c1daeSBarry Smith 
41*5c6c1daeSBarry Smith   Input Parameter:
42*5c6c1daeSBarry Smith   path - The dynamic library path, or PETSC_NULL
43*5c6c1daeSBarry Smith 
44*5c6c1daeSBarry Smith   Level: developer
45*5c6c1daeSBarry Smith 
46*5c6c1daeSBarry Smith .keywords: Petsc, initialize, package
47*5c6c1daeSBarry Smith .seealso: PetscSysInitializePackage(), PetscInitialize()
48*5c6c1daeSBarry Smith @*/
49*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaInitializePackage(const char path[])
50*5c6c1daeSBarry Smith {
51*5c6c1daeSBarry Smith   PetscError ierr;
52*5c6c1daeSBarry Smith 
53*5c6c1daeSBarry Smith   PetscFunctionBegin;
54*5c6c1daeSBarry Smith   if (PetscViewerMathematicaPackageInitialized) PetscFunctionReturn(0);
55*5c6c1daeSBarry Smith   PetscViewerMathematicaPackageInitialized = PETSC_TRUE;
56*5c6c1daeSBarry Smith   mathematicaEnv = (void*) MLInitialize(0);
57*5c6c1daeSBarry Smith   ierr = PetscRegisterFinalize(PetscViewerMathematicaFinalizePackage);CHKERRQ(ierr);
58*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
59*5c6c1daeSBarry Smith }
60*5c6c1daeSBarry Smith 
61*5c6c1daeSBarry Smith 
62*5c6c1daeSBarry Smith #undef __FUNCT__
63*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerInitializeMathematicaWorld_Private"
64*5c6c1daeSBarry Smith PetscErrorCode PetscViewerInitializeMathematicaWorld_Private()
65*5c6c1daeSBarry Smith {
66*5c6c1daeSBarry Smith   PetscErrorCode ierr;
67*5c6c1daeSBarry Smith 
68*5c6c1daeSBarry Smith   PetscFunctionBegin;
69*5c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) PetscFunctionReturn(0);
70*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaOpen(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_NULL, PETSC_NULL, &PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
71*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
72*5c6c1daeSBarry Smith }
73*5c6c1daeSBarry Smith 
74*5c6c1daeSBarry Smith #undef __FUNCT__
75*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroy_Mathematica"
76*5c6c1daeSBarry Smith static PetscErrorCode PetscViewerDestroy_Mathematica(PetscViewer viewer)
77*5c6c1daeSBarry Smith {
78*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
79*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
80*5c6c1daeSBarry Smith 
81*5c6c1daeSBarry Smith   PetscFunctionBegin;
82*5c6c1daeSBarry Smith   MLClose(vmath->link);
83*5c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkname);CHKERRQ(ierr);
84*5c6c1daeSBarry Smith   ierr = PetscFree(vmath->linkhost);CHKERRQ(ierr);
85*5c6c1daeSBarry Smith   ierr = PetscFree(vmath);CHKERRQ(ierr);
86*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
87*5c6c1daeSBarry Smith }
88*5c6c1daeSBarry Smith 
89*5c6c1daeSBarry Smith #undef __FUNCT__
90*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerDestroyMathematica_Private"
91*5c6c1daeSBarry Smith PetscErrorCode PetscViewerDestroyMathematica_Private(void)
92*5c6c1daeSBarry Smith {
93*5c6c1daeSBarry Smith   PetscErrorCode ierr;
94*5c6c1daeSBarry Smith 
95*5c6c1daeSBarry Smith   PetscFunctionBegin;
96*5c6c1daeSBarry Smith   if (PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE) {
97*5c6c1daeSBarry Smith     ierr = PetscViewerDestroy(PETSC_VIEWER_MATHEMATICA_WORLD_PRIVATE);CHKERRQ(ierr);
98*5c6c1daeSBarry Smith   }
99*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
100*5c6c1daeSBarry Smith }
101*5c6c1daeSBarry Smith 
102*5c6c1daeSBarry Smith #undef __FUNCT__
103*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetupConnection_Private"
104*5c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaSetupConnection_Private(PetscViewer v)
105*5c6c1daeSBarry Smith {
106*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
107*5c6c1daeSBarry Smith #ifdef MATHEMATICA_3_0
108*5c6c1daeSBarry Smith   int                     argc = 6;
109*5c6c1daeSBarry Smith   char                    *argv[6];
110*5c6c1daeSBarry Smith #else
111*5c6c1daeSBarry Smith   int                     argc = 5;
112*5c6c1daeSBarry Smith   char                    *argv[5];
113*5c6c1daeSBarry Smith #endif
114*5c6c1daeSBarry Smith   char                    hostname[256];
115*5c6c1daeSBarry Smith   long                    lerr;
116*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
117*5c6c1daeSBarry Smith 
118*5c6c1daeSBarry Smith   PetscFunctionBegin;
119*5c6c1daeSBarry Smith   /* Link name */
120*5c6c1daeSBarry Smith   argv[0] = "-linkname";
121*5c6c1daeSBarry Smith   if (!vmath->linkname) {
122*5c6c1daeSBarry Smith     argv[1] = "math -mathlink";
123*5c6c1daeSBarry Smith   } else {
124*5c6c1daeSBarry Smith     argv[1] = vmath->linkname;
125*5c6c1daeSBarry Smith   }
126*5c6c1daeSBarry Smith 
127*5c6c1daeSBarry Smith   /* Link host */
128*5c6c1daeSBarry Smith   argv[2] = "-linkhost";
129*5c6c1daeSBarry Smith   if (!vmath->linkhost) {
130*5c6c1daeSBarry Smith     ierr = PetscGetHostName(hostname, 255);CHKERRQ(ierr);
131*5c6c1daeSBarry Smith     argv[3] = hostname;
132*5c6c1daeSBarry Smith   } else {
133*5c6c1daeSBarry Smith     argv[3] = vmath->linkhost;
134*5c6c1daeSBarry Smith   }
135*5c6c1daeSBarry Smith 
136*5c6c1daeSBarry Smith   /* Link mode */
137*5c6c1daeSBarry Smith #ifdef MATHEMATICA_3_0
138*5c6c1daeSBarry Smith   argv[4] = "-linkmode";
139*5c6c1daeSBarry Smith   switch(vmath->linkmode) {
140*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
141*5c6c1daeSBarry Smith     argv[5] = "Create";
142*5c6c1daeSBarry Smith     break;
143*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
144*5c6c1daeSBarry Smith     argv[5] = "Connect";
145*5c6c1daeSBarry Smith     break;
146*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
147*5c6c1daeSBarry Smith     argv[5] = "Launch";
148*5c6c1daeSBarry Smith     break;
149*5c6c1daeSBarry Smith   }
150*5c6c1daeSBarry Smith #else
151*5c6c1daeSBarry Smith   switch(vmath->linkmode) {
152*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_CREATE:
153*5c6c1daeSBarry Smith     argv[4] = "-linkcreate";
154*5c6c1daeSBarry Smith     break;
155*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_CONNECT:
156*5c6c1daeSBarry Smith     argv[4] = "-linkconnect";
157*5c6c1daeSBarry Smith     break;
158*5c6c1daeSBarry Smith   case MATHEMATICA_LINK_LAUNCH:
159*5c6c1daeSBarry Smith     argv[4] = "-linklaunch";
160*5c6c1daeSBarry Smith     break;
161*5c6c1daeSBarry Smith   }
162*5c6c1daeSBarry Smith #endif
163*5c6c1daeSBarry Smith   vmath->link = MLOpenInEnv(mathematicaEnv, argc, argv, &lerr);
164*5c6c1daeSBarry Smith #endif
165*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
166*5c6c1daeSBarry Smith }
167*5c6c1daeSBarry Smith 
168*5c6c1daeSBarry Smith EXTERN_C_BEGIN
169*5c6c1daeSBarry Smith #undef __FUNCT__
170*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerCreate_Mathematica"
171*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerCreate_Mathematica(PetscViewer v)
172*5c6c1daeSBarry Smith {
173*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath;
174*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
175*5c6c1daeSBarry Smith 
176*5c6c1daeSBarry Smith   PetscFunctionBegin;
177*5c6c1daeSBarry Smith #ifndef PETSC_USE_DYNAMIC_LIBRARIES
178*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaInitializePackage(PETSC_NULL);CHKERRQ(ierr);
179*5c6c1daeSBarry Smith #endif
180*5c6c1daeSBarry Smith 
181*5c6c1daeSBarry Smith   ierr = PetscNewLog(v,PetscViewer_Mathematica, &vmath);CHKERRQ(ierr);
182*5c6c1daeSBarry Smith   v->data         = (void*) vmath;
183*5c6c1daeSBarry Smith   v->ops->destroy = PetscViewerDestroy_Mathematica;
184*5c6c1daeSBarry Smith   v->ops->flush   = 0;
185*5c6c1daeSBarry Smith   ierr = PetscStrallocpy(PETSC_VIEWER_MATHEMATICA, &((PetscObject)v)->type_name);CHKERRQ(ierr);
186*5c6c1daeSBarry Smith 
187*5c6c1daeSBarry Smith   vmath->linkname         = PETSC_NULL;
188*5c6c1daeSBarry Smith   vmath->linkhost         = PETSC_NULL;
189*5c6c1daeSBarry Smith   vmath->linkmode         = MATHEMATICA_LINK_CONNECT;
190*5c6c1daeSBarry Smith   vmath->graphicsType     = GRAPHICS_MOTIF;
191*5c6c1daeSBarry Smith   vmath->plotType         = MATHEMATICA_TRIANGULATION_PLOT;
192*5c6c1daeSBarry Smith   vmath->objName          = PETSC_NULL;
193*5c6c1daeSBarry Smith 
194*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetFromOptions(v);CHKERRQ(ierr);
195*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetupConnection_Private(v);CHKERRQ(ierr);
196*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
197*5c6c1daeSBarry Smith }
198*5c6c1daeSBarry Smith EXTERN_C_END
199*5c6c1daeSBarry Smith 
200*5c6c1daeSBarry Smith #undef __FUNCT__
201*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaParseLinkMode_Private"
202*5c6c1daeSBarry Smith PetscErrorCode PetscViewerMathematicaParseLinkMode_Private(char *modename, LinkMode *mode) {
203*5c6c1daeSBarry Smith   PetscBool      isCreate, isConnect, isLaunch;
204*5c6c1daeSBarry Smith   PetscErrorCode ierr;
205*5c6c1daeSBarry Smith 
206*5c6c1daeSBarry Smith   PetscFunctionBegin;
207*5c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Create",  &isCreate);CHKERRQ(ierr);
208*5c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Connect", &isConnect);CHKERRQ(ierr);
209*5c6c1daeSBarry Smith   ierr = PetscStrcasecmp(modename, "Launch",  &isLaunch);CHKERRQ(ierr);
210*5c6c1daeSBarry Smith   if (isCreate) {
211*5c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_CREATE;
212*5c6c1daeSBarry Smith   } else if (isConnect) {
213*5c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_CONNECT;
214*5c6c1daeSBarry Smith   } else if (isLaunch) {
215*5c6c1daeSBarry Smith     *mode = MATHEMATICA_LINK_LAUNCH;
216*5c6c1daeSBarry Smith   } else {
217*5c6c1daeSBarry Smith     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename);
218*5c6c1daeSBarry Smith   }
219*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
220*5c6c1daeSBarry Smith }
221*5c6c1daeSBarry Smith 
222*5c6c1daeSBarry Smith #undef __FUNCT__
223*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetFromOptions"
224*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetFromOptions(PetscViewer v)
225*5c6c1daeSBarry Smith {
226*5c6c1daeSBarry Smith   PetscViewer_Mathematica  *vmath = (PetscViewer_Mathematica *) v->data;
227*5c6c1daeSBarry Smith   char                     linkname[256];
228*5c6c1daeSBarry Smith   char                     modename[256];
229*5c6c1daeSBarry Smith   char                     hostname[256];
230*5c6c1daeSBarry Smith   char                     type[256];
231*5c6c1daeSBarry Smith   PetscInt                 numPorts;
232*5c6c1daeSBarry Smith   PetscInt                 *ports;
233*5c6c1daeSBarry Smith   PetscInt                 numHosts;
234*5c6c1daeSBarry Smith   int                      h;
235*5c6c1daeSBarry Smith   char                     **hosts;
236*5c6c1daeSBarry Smith   PetscMPIInt              size, rank;
237*5c6c1daeSBarry Smith   PetscBool                opt;
238*5c6c1daeSBarry Smith   PetscErrorCode           ierr;
239*5c6c1daeSBarry Smith 
240*5c6c1daeSBarry Smith   PetscFunctionBegin;
241*5c6c1daeSBarry Smith   ierr = MPI_Comm_size(((PetscObject)v)->comm, &size);CHKERRQ(ierr);
242*5c6c1daeSBarry Smith   ierr = MPI_Comm_rank(((PetscObject)v)->comm, &rank);CHKERRQ(ierr);
243*5c6c1daeSBarry Smith 
244*5c6c1daeSBarry Smith   /* Get link name */
245*5c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkname", linkname, 256, &opt);CHKERRQ(ierr);
246*5c6c1daeSBarry Smith   if (opt) {
247*5c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
248*5c6c1daeSBarry Smith   }
249*5c6c1daeSBarry Smith   /* Get link port */
250*5c6c1daeSBarry Smith   numPorts = size;
251*5c6c1daeSBarry Smith   ierr = PetscMalloc(size*sizeof(int), &ports);CHKERRQ(ierr);
252*5c6c1daeSBarry Smith   ierr = PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt);CHKERRQ(ierr);
253*5c6c1daeSBarry Smith   if (opt) {
254*5c6c1daeSBarry Smith     if (numPorts > rank) {
255*5c6c1daeSBarry Smith       snprintf(linkname, 255, "%6d", ports[rank]);
256*5c6c1daeSBarry Smith     } else {
257*5c6c1daeSBarry Smith       snprintf(linkname, 255, "%6d", ports[0]);
258*5c6c1daeSBarry Smith     }
259*5c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr);
260*5c6c1daeSBarry Smith   }
261*5c6c1daeSBarry Smith   ierr = PetscFree(ports);CHKERRQ(ierr);
262*5c6c1daeSBarry Smith   /* Get link host */
263*5c6c1daeSBarry Smith   numHosts = size;
264*5c6c1daeSBarry Smith   ierr = PetscMalloc(size*sizeof(char *), &hosts);CHKERRQ(ierr);
265*5c6c1daeSBarry Smith   ierr = PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt);CHKERRQ(ierr);
266*5c6c1daeSBarry Smith   if (opt) {
267*5c6c1daeSBarry Smith     if (numHosts > rank) {
268*5c6c1daeSBarry Smith       ierr = PetscStrncpy(hostname, hosts[rank], 255);CHKERRQ(ierr);
269*5c6c1daeSBarry Smith     } else {
270*5c6c1daeSBarry Smith       ierr = PetscStrncpy(hostname, hosts[0], 255);CHKERRQ(ierr);
271*5c6c1daeSBarry Smith     }
272*5c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkHost(v, hostname);CHKERRQ(ierr);
273*5c6c1daeSBarry Smith   }
274*5c6c1daeSBarry Smith   for (h = 0; h < numHosts; h++) {
275*5c6c1daeSBarry Smith     ierr = PetscFree(hosts[h]);CHKERRQ(ierr);
276*5c6c1daeSBarry Smith   }
277*5c6c1daeSBarry Smith   ierr = PetscFree(hosts);CHKERRQ(ierr);
278*5c6c1daeSBarry Smith   /* Get link mode */
279*5c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_linkmode", modename, 256, &opt);CHKERRQ(ierr);
280*5c6c1daeSBarry Smith   if (opt) {
281*5c6c1daeSBarry Smith     LinkMode mode;
282*5c6c1daeSBarry Smith 
283*5c6c1daeSBarry Smith     ierr = PetscViewerMathematicaParseLinkMode_Private(modename, &mode);CHKERRQ(ierr);
284*5c6c1daeSBarry Smith     ierr = PetscViewerMathematicaSetLinkMode(v, mode);CHKERRQ(ierr);
285*5c6c1daeSBarry Smith   }
286*5c6c1daeSBarry Smith   /* Get graphics type */
287*5c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_graphics", type, 256, &opt);CHKERRQ(ierr);
288*5c6c1daeSBarry Smith   if (opt) {
289*5c6c1daeSBarry Smith     PetscBool  isMotif, isPS, isPSFile;
290*5c6c1daeSBarry Smith 
291*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Motif",  &isMotif);CHKERRQ(ierr);
292*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PS",     &isPS);CHKERRQ(ierr);
293*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "PSFile", &isPSFile);CHKERRQ(ierr);
294*5c6c1daeSBarry Smith     if (isMotif) {
295*5c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_MOTIF;
296*5c6c1daeSBarry Smith     } else if (isPS) {
297*5c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_PS_STDOUT;
298*5c6c1daeSBarry Smith     } else if (isPSFile) {
299*5c6c1daeSBarry Smith       vmath->graphicsType = GRAPHICS_PS_FILE;
300*5c6c1daeSBarry Smith     }
301*5c6c1daeSBarry Smith   }
302*5c6c1daeSBarry Smith   /* Get plot type */
303*5c6c1daeSBarry Smith   ierr = PetscOptionsGetString("viewer_", "-math_type", type, 256, &opt);CHKERRQ(ierr);
304*5c6c1daeSBarry Smith   if (opt) {
305*5c6c1daeSBarry Smith     PetscBool  isTri, isVecTri, isVec, isSurface;
306*5c6c1daeSBarry Smith 
307*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Triangulation",       &isTri);CHKERRQ(ierr);
308*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "VectorTriangulation", &isVecTri);CHKERRQ(ierr);
309*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Vector",              &isVec);CHKERRQ(ierr);
310*5c6c1daeSBarry Smith     ierr = PetscStrcasecmp(type, "Surface",             &isSurface);CHKERRQ(ierr);
311*5c6c1daeSBarry Smith     if (isTri) {
312*5c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_TRIANGULATION_PLOT;
313*5c6c1daeSBarry Smith     } else if (isVecTri) {
314*5c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_VECTOR_TRIANGULATION_PLOT;
315*5c6c1daeSBarry Smith     } else if (isVec) {
316*5c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_VECTOR_PLOT;
317*5c6c1daeSBarry Smith     } else if (isSurface) {
318*5c6c1daeSBarry Smith       vmath->plotType     = MATHEMATICA_SURFACE_PLOT;
319*5c6c1daeSBarry Smith     }
320*5c6c1daeSBarry Smith   }
321*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
322*5c6c1daeSBarry Smith }
323*5c6c1daeSBarry Smith 
324*5c6c1daeSBarry Smith #undef __FUNCT__
325*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkName"
326*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name) {
327*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
328*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
329*5c6c1daeSBarry Smith 
330*5c6c1daeSBarry Smith   PetscFunctionBegin;
331*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
332*5c6c1daeSBarry Smith   PetscValidCharPointer(name,2);
333*5c6c1daeSBarry Smith   ierr = PetscStrallocpy(name, &vmath->linkname);CHKERRQ(ierr);
334*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
335*5c6c1daeSBarry Smith }
336*5c6c1daeSBarry Smith 
337*5c6c1daeSBarry Smith #undef __FUNCT__
338*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaLinkPort"
339*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkPort(PetscViewer v, int port) {
340*5c6c1daeSBarry Smith   char           name[16];
341*5c6c1daeSBarry Smith   PetscErrorCode ierr;
342*5c6c1daeSBarry Smith 
343*5c6c1daeSBarry Smith   PetscFunctionBegin;
344*5c6c1daeSBarry Smith   snprintf(name, 16, "%6d", port);
345*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkName(v, name);CHKERRQ(ierr);
346*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
347*5c6c1daeSBarry Smith }
348*5c6c1daeSBarry Smith 
349*5c6c1daeSBarry Smith #undef __FUNCT__
350*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkHost"
351*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host) {
352*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
353*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
354*5c6c1daeSBarry Smith 
355*5c6c1daeSBarry Smith   PetscFunctionBegin;
356*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1);
357*5c6c1daeSBarry Smith   PetscValidCharPointer(host,2);
358*5c6c1daeSBarry Smith   ierr = PetscStrallocpy(host, &vmath->linkhost);CHKERRQ(ierr);
359*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
360*5c6c1daeSBarry Smith }
361*5c6c1daeSBarry Smith 
362*5c6c1daeSBarry Smith #undef __FUNCT__
363*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetLinkHost"
364*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode) {
365*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data;
366*5c6c1daeSBarry Smith 
367*5c6c1daeSBarry Smith   PetscFunctionBegin;
368*5c6c1daeSBarry Smith   vmath->linkmode = mode;
369*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
370*5c6c1daeSBarry Smith }
371*5c6c1daeSBarry Smith 
372*5c6c1daeSBarry Smith /*----------------------------------------- Public Functions --------------------------------------------------------*/
373*5c6c1daeSBarry Smith #undef __FUNCT__
374*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaOpen"
375*5c6c1daeSBarry Smith /*@C
376*5c6c1daeSBarry Smith   PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink.
377*5c6c1daeSBarry Smith 
378*5c6c1daeSBarry Smith   Collective on comm
379*5c6c1daeSBarry Smith 
380*5c6c1daeSBarry Smith   Input Parameters:
381*5c6c1daeSBarry Smith + comm    - The MPI communicator
382*5c6c1daeSBarry Smith . port    - [optional] The port to connect on, or PETSC_DECIDE
383*5c6c1daeSBarry Smith . machine - [optional] The machine to run Mathematica on, or PETSC_NULL
384*5c6c1daeSBarry Smith - mode    - [optional] The connection mode, or PETSC_NULL
385*5c6c1daeSBarry Smith 
386*5c6c1daeSBarry Smith   Output Parameter:
387*5c6c1daeSBarry Smith . viewer  - The Mathematica viewer
388*5c6c1daeSBarry Smith 
389*5c6c1daeSBarry Smith   Level: intermediate
390*5c6c1daeSBarry Smith 
391*5c6c1daeSBarry Smith   Notes:
392*5c6c1daeSBarry Smith   Most users should employ the following commands to access the
393*5c6c1daeSBarry Smith   Mathematica viewers
394*5c6c1daeSBarry Smith $
395*5c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
396*5c6c1daeSBarry Smith $    MatView(Mat matrix, PetscViewer viewer)
397*5c6c1daeSBarry Smith $
398*5c6c1daeSBarry Smith $                or
399*5c6c1daeSBarry Smith $
400*5c6c1daeSBarry Smith $    PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer)
401*5c6c1daeSBarry Smith $    VecView(Vec vector, PetscViewer viewer)
402*5c6c1daeSBarry Smith 
403*5c6c1daeSBarry Smith    Options Database Keys:
404*5c6c1daeSBarry Smith $    -viewer_math_linkhost <machine> - The host machine for the kernel
405*5c6c1daeSBarry Smith $    -viewer_math_linkname <name>    - The full link name for the connection
406*5c6c1daeSBarry Smith $    -viewer_math_linkport <port>    - The port for the connection
407*5c6c1daeSBarry Smith $    -viewer_math_mode <mode>        - The mode, e.g. Launch, Connect
408*5c6c1daeSBarry Smith $    -viewer_math_type <type>        - The plot type, e.g. Triangulation, Vector
409*5c6c1daeSBarry Smith $    -viewer_math_graphics <output>  - The output type, e.g. Motif, PS, PSFile
410*5c6c1daeSBarry Smith 
411*5c6c1daeSBarry Smith .keywords: PetscViewer, Mathematica, open
412*5c6c1daeSBarry Smith 
413*5c6c1daeSBarry Smith .seealso: MatView(), VecView()
414*5c6c1daeSBarry Smith @*/
415*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v)
416*5c6c1daeSBarry Smith {
417*5c6c1daeSBarry Smith   PetscErrorCode ierr;
418*5c6c1daeSBarry Smith 
419*5c6c1daeSBarry Smith   PetscFunctionBegin;
420*5c6c1daeSBarry Smith   ierr = PetscViewerCreate(comm, v);CHKERRQ(ierr);
421*5c6c1daeSBarry Smith #if 0
422*5c6c1daeSBarry Smith   LinkMode linkmode;
423*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkPort(*v, port);CHKERRQ(ierr);
424*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkHost(*v, machine);CHKERRQ(ierr);
425*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaParseLinkMode_Private(mode, &linkmode);CHKERRQ(ierr);
426*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSetLinkMode(*v, linkmode);CHKERRQ(ierr);
427*5c6c1daeSBarry Smith #endif
428*5c6c1daeSBarry Smith   ierr = PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA);CHKERRQ(ierr);
429*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
430*5c6c1daeSBarry Smith }
431*5c6c1daeSBarry Smith 
432*5c6c1daeSBarry Smith #undef __FUNCT__
433*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetLink"
434*5c6c1daeSBarry Smith /*@C
435*5c6c1daeSBarry Smith   PetscViewerMathematicaGetLink - Returns the link to Mathematica
436*5c6c1daeSBarry Smith 
437*5c6c1daeSBarry Smith   Input Parameters:
438*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
439*5c6c1daeSBarry Smith . link   - The link to Mathematica
440*5c6c1daeSBarry Smith 
441*5c6c1daeSBarry Smith   Level: intermediate
442*5c6c1daeSBarry Smith 
443*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, link
444*5c6c1daeSBarry Smith .seealso PetscViewerMathematicaOpen()
445*5c6c1daeSBarry Smith @*/
446*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link)
447*5c6c1daeSBarry Smith {
448*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
449*5c6c1daeSBarry Smith 
450*5c6c1daeSBarry Smith   PetscFunctionBegin;
451*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
452*5c6c1daeSBarry Smith   *link = vmath->link;
453*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
454*5c6c1daeSBarry Smith }
455*5c6c1daeSBarry Smith 
456*5c6c1daeSBarry Smith #undef __FUNCT__
457*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSkipPackets"
458*5c6c1daeSBarry Smith /*@C
459*5c6c1daeSBarry Smith   PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received
460*5c6c1daeSBarry Smith 
461*5c6c1daeSBarry Smith   Input Parameters:
462*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
463*5c6c1daeSBarry Smith . type   - The packet type to search for, e.g RETURNPKT
464*5c6c1daeSBarry Smith 
465*5c6c1daeSBarry Smith   Level: advanced
466*5c6c1daeSBarry Smith 
467*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, packets
468*5c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaGetVector()
469*5c6c1daeSBarry Smith @*/
470*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type)
471*5c6c1daeSBarry Smith {
472*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
473*5c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
474*5c6c1daeSBarry Smith   int                     pkt;                 /* The packet type */
475*5c6c1daeSBarry Smith 
476*5c6c1daeSBarry Smith   PetscFunctionBegin;
477*5c6c1daeSBarry Smith   while((pkt = MLNextPacket(link)) && (pkt != type))
478*5c6c1daeSBarry Smith     MLNewPacket(link);
479*5c6c1daeSBarry Smith   if (!pkt) {
480*5c6c1daeSBarry Smith     MLClearError(link);
481*5c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char *) MLErrorMessage(link));
482*5c6c1daeSBarry Smith   }
483*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
484*5c6c1daeSBarry Smith }
485*5c6c1daeSBarry Smith 
486*5c6c1daeSBarry Smith #undef __FUNCT__
487*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetName"
488*5c6c1daeSBarry Smith /*@C
489*5c6c1daeSBarry Smith   PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica
490*5c6c1daeSBarry Smith 
491*5c6c1daeSBarry Smith   Input Parameter:
492*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
493*5c6c1daeSBarry Smith 
494*5c6c1daeSBarry Smith   Output Parameter:
495*5c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
496*5c6c1daeSBarry Smith 
497*5c6c1daeSBarry Smith   Level: intermediate
498*5c6c1daeSBarry Smith 
499*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
500*5c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
501*5c6c1daeSBarry Smith @*/
502*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetName(PetscViewer viewer, const char **name)
503*5c6c1daeSBarry Smith {
504*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
505*5c6c1daeSBarry Smith 
506*5c6c1daeSBarry Smith   PetscFunctionBegin;
507*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
508*5c6c1daeSBarry Smith   PetscValidPointer(name,2);
509*5c6c1daeSBarry Smith   *name = vmath->objName;
510*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
511*5c6c1daeSBarry Smith }
512*5c6c1daeSBarry Smith 
513*5c6c1daeSBarry Smith #undef __FUNCT__
514*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaSetName"
515*5c6c1daeSBarry Smith /*@C
516*5c6c1daeSBarry Smith   PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica
517*5c6c1daeSBarry Smith 
518*5c6c1daeSBarry Smith   Input Parameters:
519*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
520*5c6c1daeSBarry Smith . name   - The name for new objects created in Mathematica
521*5c6c1daeSBarry Smith 
522*5c6c1daeSBarry Smith   Level: intermediate
523*5c6c1daeSBarry Smith 
524*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
525*5c6c1daeSBarry Smith .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName()
526*5c6c1daeSBarry Smith @*/
527*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaSetName(PetscViewer viewer, const char name[])
528*5c6c1daeSBarry Smith {
529*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
530*5c6c1daeSBarry Smith 
531*5c6c1daeSBarry Smith   PetscFunctionBegin;
532*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
533*5c6c1daeSBarry Smith   PetscValidPointer(name,2);
534*5c6c1daeSBarry Smith   vmath->objName = name;
535*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
536*5c6c1daeSBarry Smith }
537*5c6c1daeSBarry Smith 
538*5c6c1daeSBarry Smith #undef __FUNCT__
539*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaClearName"
540*5c6c1daeSBarry Smith /*@C
541*5c6c1daeSBarry Smith   PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica
542*5c6c1daeSBarry Smith 
543*5c6c1daeSBarry Smith   Input Parameter:
544*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
545*5c6c1daeSBarry Smith 
546*5c6c1daeSBarry Smith   Level: intermediate
547*5c6c1daeSBarry Smith 
548*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, name
549*5c6c1daeSBarry Smith .seealso PetscViewerMathematicaGetName(), PetscViewerMathematicaSetName()
550*5c6c1daeSBarry Smith @*/
551*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaClearName(PetscViewer viewer)
552*5c6c1daeSBarry Smith {
553*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
554*5c6c1daeSBarry Smith 
555*5c6c1daeSBarry Smith   PetscFunctionBegin;
556*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
557*5c6c1daeSBarry Smith   vmath->objName = PETSC_NULL;
558*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
559*5c6c1daeSBarry Smith }
560*5c6c1daeSBarry Smith 
561*5c6c1daeSBarry Smith #undef __FUNCT__
562*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaGetVector"
563*5c6c1daeSBarry Smith /*@C
564*5c6c1daeSBarry Smith   PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica
565*5c6c1daeSBarry Smith 
566*5c6c1daeSBarry Smith   Input Parameter:
567*5c6c1daeSBarry Smith . viewer - The Mathematica viewer
568*5c6c1daeSBarry Smith 
569*5c6c1daeSBarry Smith   Output Parameter:
570*5c6c1daeSBarry Smith . v      - The vector
571*5c6c1daeSBarry Smith 
572*5c6c1daeSBarry Smith   Level: intermediate
573*5c6c1daeSBarry Smith 
574*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
575*5c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaPutVector()
576*5c6c1daeSBarry Smith @*/
577*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v) {
578*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
579*5c6c1daeSBarry Smith   MLINK                   link;   /* The link to Mathematica */
580*5c6c1daeSBarry Smith   char                    *name;
581*5c6c1daeSBarry Smith   PetscScalar             *mArray,*array;
582*5c6c1daeSBarry Smith   long                    mSize;
583*5c6c1daeSBarry Smith   int                     n;
584*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
585*5c6c1daeSBarry Smith 
586*5c6c1daeSBarry Smith   PetscFunctionBegin;
587*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1);
588*5c6c1daeSBarry Smith   PetscValidHeaderSpecific(v,      VEC_CLASSID,2);
589*5c6c1daeSBarry Smith 
590*5c6c1daeSBarry Smith   /* Determine the object name */
591*5c6c1daeSBarry Smith   if (!vmath->objName) {
592*5c6c1daeSBarry Smith     name = "vec";
593*5c6c1daeSBarry Smith   } else {
594*5c6c1daeSBarry Smith     name = (char *) vmath->objName;
595*5c6c1daeSBarry Smith   }
596*5c6c1daeSBarry Smith 
597*5c6c1daeSBarry Smith   link = vmath->link;
598*5c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
599*5c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
600*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
601*5c6c1daeSBarry Smith     MLPutSymbol(link, name);
602*5c6c1daeSBarry Smith   MLEndPacket(link);
603*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
604*5c6c1daeSBarry Smith   MLGetRealList(link, &mArray, &mSize);
605*5c6c1daeSBarry Smith   if (n != mSize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize);
606*5c6c1daeSBarry Smith   ierr = PetscMemcpy(array, mArray, mSize * sizeof(double));CHKERRQ(ierr);
607*5c6c1daeSBarry Smith   MLDisownRealList(link, mArray, mSize);
608*5c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
609*5c6c1daeSBarry Smith 
610*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
611*5c6c1daeSBarry Smith }
612*5c6c1daeSBarry Smith 
613*5c6c1daeSBarry Smith #undef __FUNCT__
614*5c6c1daeSBarry Smith #define __FUNCT__ "PetscViewerMathematicaPutVector"
615*5c6c1daeSBarry Smith /*@C
616*5c6c1daeSBarry Smith   PetscViewerMathematicaPutVector - Send a vector to Mathematica
617*5c6c1daeSBarry Smith 
618*5c6c1daeSBarry Smith   Input Parameters:
619*5c6c1daeSBarry Smith + viewer - The Mathematica viewer
620*5c6c1daeSBarry Smith - v      - The vector
621*5c6c1daeSBarry Smith 
622*5c6c1daeSBarry Smith   Level: intermediate
623*5c6c1daeSBarry Smith 
624*5c6c1daeSBarry Smith .keywords PetscViewer, Mathematica, vector
625*5c6c1daeSBarry Smith .seealso VecView(), PetscViewerMathematicaGetVector()
626*5c6c1daeSBarry Smith @*/
627*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v)
628*5c6c1daeSBarry Smith {
629*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
630*5c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
631*5c6c1daeSBarry Smith   char                    *name;
632*5c6c1daeSBarry Smith   PetscScalar             *array;
633*5c6c1daeSBarry Smith   int                     n;
634*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
635*5c6c1daeSBarry Smith 
636*5c6c1daeSBarry Smith   PetscFunctionBegin;
637*5c6c1daeSBarry Smith   /* Determine the object name */
638*5c6c1daeSBarry Smith   if (!vmath->objName) {
639*5c6c1daeSBarry Smith     name = "vec";
640*5c6c1daeSBarry Smith   } else {
641*5c6c1daeSBarry Smith     name = (char *) vmath->objName;
642*5c6c1daeSBarry Smith   }
643*5c6c1daeSBarry Smith   ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr);
644*5c6c1daeSBarry Smith   ierr = VecGetArray(v, &array);CHKERRQ(ierr);
645*5c6c1daeSBarry Smith 
646*5c6c1daeSBarry Smith   /* Send the Vector object */
647*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
648*5c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
649*5c6c1daeSBarry Smith       MLPutSymbol(link, name);
650*5c6c1daeSBarry Smith       MLPutRealList(link, array, n);
651*5c6c1daeSBarry Smith   MLEndPacket(link);
652*5c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
653*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
654*5c6c1daeSBarry Smith   /* Skip ReturnPacket */
655*5c6c1daeSBarry Smith   MLNewPacket(link);
656*5c6c1daeSBarry Smith 
657*5c6c1daeSBarry Smith   ierr = VecRestoreArray(v, &array);CHKERRQ(ierr);
658*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
659*5c6c1daeSBarry Smith }
660*5c6c1daeSBarry Smith 
661*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a)
662*5c6c1daeSBarry Smith {
663*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
664*5c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
665*5c6c1daeSBarry Smith   char                    *name;
666*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
667*5c6c1daeSBarry Smith 
668*5c6c1daeSBarry Smith   PetscFunctionBegin;
669*5c6c1daeSBarry Smith   /* Determine the object name */
670*5c6c1daeSBarry Smith   if (!vmath->objName) {
671*5c6c1daeSBarry Smith     name = "mat";
672*5c6c1daeSBarry Smith   } else {
673*5c6c1daeSBarry Smith     name = (char *) vmath->objName;
674*5c6c1daeSBarry Smith   }
675*5c6c1daeSBarry Smith 
676*5c6c1daeSBarry Smith   /* Send the dense matrix object */
677*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
678*5c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
679*5c6c1daeSBarry Smith       MLPutSymbol(link, name);
680*5c6c1daeSBarry Smith       MLPutFunction(link, "Transpose", 1);
681*5c6c1daeSBarry Smith         MLPutFunction(link, "Partition", 2);
682*5c6c1daeSBarry Smith           MLPutRealList(link, a, m*n);
683*5c6c1daeSBarry Smith           MLPutInteger(link, m);
684*5c6c1daeSBarry Smith   MLEndPacket(link);
685*5c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
686*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
687*5c6c1daeSBarry Smith   /* Skip ReturnPacket */
688*5c6c1daeSBarry Smith   MLNewPacket(link);
689*5c6c1daeSBarry Smith 
690*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
691*5c6c1daeSBarry Smith }
692*5c6c1daeSBarry Smith 
693*5c6c1daeSBarry Smith PetscErrorCode  PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a)
694*5c6c1daeSBarry Smith {
695*5c6c1daeSBarry Smith   PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data;
696*5c6c1daeSBarry Smith   MLINK                   link  = vmath->link; /* The link to Mathematica */
697*5c6c1daeSBarry Smith   const char              *symbol;
698*5c6c1daeSBarry Smith   char                    *name;
699*5c6c1daeSBarry Smith   PetscBool               match;
700*5c6c1daeSBarry Smith   PetscErrorCode          ierr;
701*5c6c1daeSBarry Smith 
702*5c6c1daeSBarry Smith   PetscFunctionBegin;
703*5c6c1daeSBarry Smith   /* Determine the object name */
704*5c6c1daeSBarry Smith   if (!vmath->objName) {
705*5c6c1daeSBarry Smith     name = "mat";
706*5c6c1daeSBarry Smith   } else {
707*5c6c1daeSBarry Smith     name = (char *) vmath->objName;
708*5c6c1daeSBarry Smith   }
709*5c6c1daeSBarry Smith 
710*5c6c1daeSBarry Smith   /* Make sure Mathematica recognizes sparse matrices */
711*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
712*5c6c1daeSBarry Smith     MLPutFunction(link, "Needs", 1);
713*5c6c1daeSBarry Smith       MLPutString(link, "LinearAlgebra`CSRMatrix`");
714*5c6c1daeSBarry Smith   MLEndPacket(link);
715*5c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
716*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
717*5c6c1daeSBarry Smith   /* Skip ReturnPacket */
718*5c6c1daeSBarry Smith   MLNewPacket(link);
719*5c6c1daeSBarry Smith 
720*5c6c1daeSBarry Smith   /* Send the CSRMatrix object */
721*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
722*5c6c1daeSBarry Smith     MLPutFunction(link, "Set", 2);
723*5c6c1daeSBarry Smith       MLPutSymbol(link, name);
724*5c6c1daeSBarry Smith       MLPutFunction(link, "CSRMatrix", 5);
725*5c6c1daeSBarry Smith         MLPutInteger(link, m);
726*5c6c1daeSBarry Smith         MLPutInteger(link, n);
727*5c6c1daeSBarry Smith         MLPutFunction(link, "Plus", 2);
728*5c6c1daeSBarry Smith           MLPutIntegerList(link, i, m+1);
729*5c6c1daeSBarry Smith           MLPutInteger(link, 1);
730*5c6c1daeSBarry Smith         MLPutFunction(link, "Plus", 2);
731*5c6c1daeSBarry Smith           MLPutIntegerList(link, j, i[m]);
732*5c6c1daeSBarry Smith           MLPutInteger(link, 1);
733*5c6c1daeSBarry Smith         MLPutRealList(link, a, i[m]);
734*5c6c1daeSBarry Smith   MLEndPacket(link);
735*5c6c1daeSBarry Smith   /* Skip packets until ReturnPacket */
736*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
737*5c6c1daeSBarry Smith   /* Skip ReturnPacket */
738*5c6c1daeSBarry Smith   MLNewPacket(link);
739*5c6c1daeSBarry Smith 
740*5c6c1daeSBarry Smith   /* Check that matrix is valid */
741*5c6c1daeSBarry Smith   MLPutFunction(link, "EvaluatePacket", 1);
742*5c6c1daeSBarry Smith     MLPutFunction(link, "ValidQ", 1);
743*5c6c1daeSBarry Smith       MLPutSymbol(link, name);
744*5c6c1daeSBarry Smith   MLEndPacket(link);
745*5c6c1daeSBarry Smith   ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr);
746*5c6c1daeSBarry Smith   MLGetSymbol(link, &symbol);
747*5c6c1daeSBarry Smith   ierr = PetscStrcmp("True", (char *) symbol, &match);CHKERRQ(ierr);
748*5c6c1daeSBarry Smith   if (!match) {
749*5c6c1daeSBarry Smith     MLDisownSymbol(link, symbol);
750*5c6c1daeSBarry Smith     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica");
751*5c6c1daeSBarry Smith   }
752*5c6c1daeSBarry Smith   MLDisownSymbol(link, symbol);
753*5c6c1daeSBarry Smith   /* Skip ReturnPacket */
754*5c6c1daeSBarry Smith   MLNewPacket(link);
755*5c6c1daeSBarry Smith 
756*5c6c1daeSBarry Smith   PetscFunctionReturn(0);
757*5c6c1daeSBarry Smith }
758*5c6c1daeSBarry Smith 
759