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