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 #if defined(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 #if defined(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 #if !defined(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 SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Invalid Mathematica link mode: %s", modename); 218 PetscFunctionReturn(0); 219 } 220 221 #undef __FUNCT__ 222 #define __FUNCT__ "PetscViewerMathematicaSetFromOptions" 223 PetscErrorCode PetscViewerMathematicaSetFromOptions(PetscViewer v) 224 { 225 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data; 226 char linkname[256]; 227 char modename[256]; 228 char hostname[256]; 229 char type[256]; 230 PetscInt numPorts; 231 PetscInt *ports; 232 PetscInt numHosts; 233 int h; 234 char **hosts; 235 PetscMPIInt size, rank; 236 PetscBool opt; 237 PetscErrorCode ierr; 238 239 PetscFunctionBegin; 240 ierr = MPI_Comm_size(((PetscObject)v)->comm, &size);CHKERRQ(ierr); 241 ierr = MPI_Comm_rank(((PetscObject)v)->comm, &rank);CHKERRQ(ierr); 242 243 /* Get link name */ 244 ierr = PetscOptionsGetString("viewer_", "-math_linkname", linkname, 256, &opt);CHKERRQ(ierr); 245 if (opt) { 246 ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr); 247 } 248 /* Get link port */ 249 numPorts = size; 250 ierr = PetscMalloc(size*sizeof(int), &ports);CHKERRQ(ierr); 251 ierr = PetscOptionsGetIntArray("viewer_", "-math_linkport", ports, &numPorts, &opt);CHKERRQ(ierr); 252 if (opt) { 253 if (numPorts > rank) { 254 snprintf(linkname, 255, "%6d", ports[rank]); 255 } else { 256 snprintf(linkname, 255, "%6d", ports[0]); 257 } 258 ierr = PetscViewerMathematicaSetLinkName(v, linkname);CHKERRQ(ierr); 259 } 260 ierr = PetscFree(ports);CHKERRQ(ierr); 261 /* Get link host */ 262 numHosts = size; 263 ierr = PetscMalloc(size*sizeof(char *), &hosts);CHKERRQ(ierr); 264 ierr = PetscOptionsGetStringArray("viewer_", "-math_linkhost", hosts, &numHosts, &opt);CHKERRQ(ierr); 265 if (opt) { 266 if (numHosts > rank) { 267 ierr = PetscStrncpy(hostname, hosts[rank], 255);CHKERRQ(ierr); 268 } else { 269 ierr = PetscStrncpy(hostname, hosts[0], 255);CHKERRQ(ierr); 270 } 271 ierr = PetscViewerMathematicaSetLinkHost(v, hostname);CHKERRQ(ierr); 272 } 273 for (h = 0; h < numHosts; h++) { 274 ierr = PetscFree(hosts[h]);CHKERRQ(ierr); 275 } 276 ierr = PetscFree(hosts);CHKERRQ(ierr); 277 /* Get link mode */ 278 ierr = PetscOptionsGetString("viewer_", "-math_linkmode", modename, 256, &opt);CHKERRQ(ierr); 279 if (opt) { 280 LinkMode mode; 281 282 ierr = PetscViewerMathematicaParseLinkMode_Private(modename, &mode);CHKERRQ(ierr); 283 ierr = PetscViewerMathematicaSetLinkMode(v, mode);CHKERRQ(ierr); 284 } 285 /* Get graphics type */ 286 ierr = PetscOptionsGetString("viewer_", "-math_graphics", type, 256, &opt);CHKERRQ(ierr); 287 if (opt) { 288 PetscBool isMotif, isPS, isPSFile; 289 290 ierr = PetscStrcasecmp(type, "Motif", &isMotif);CHKERRQ(ierr); 291 ierr = PetscStrcasecmp(type, "PS", &isPS);CHKERRQ(ierr); 292 ierr = PetscStrcasecmp(type, "PSFile", &isPSFile);CHKERRQ(ierr); 293 if (isMotif) { 294 vmath->graphicsType = GRAPHICS_MOTIF; 295 } else if (isPS) { 296 vmath->graphicsType = GRAPHICS_PS_STDOUT; 297 } else if (isPSFile) { 298 vmath->graphicsType = GRAPHICS_PS_FILE; 299 } 300 } 301 /* Get plot type */ 302 ierr = PetscOptionsGetString("viewer_", "-math_type", type, 256, &opt);CHKERRQ(ierr); 303 if (opt) { 304 PetscBool isTri, isVecTri, isVec, isSurface; 305 306 ierr = PetscStrcasecmp(type, "Triangulation", &isTri);CHKERRQ(ierr); 307 ierr = PetscStrcasecmp(type, "VectorTriangulation", &isVecTri);CHKERRQ(ierr); 308 ierr = PetscStrcasecmp(type, "Vector", &isVec);CHKERRQ(ierr); 309 ierr = PetscStrcasecmp(type, "Surface", &isSurface);CHKERRQ(ierr); 310 if (isTri) { 311 vmath->plotType = MATHEMATICA_TRIANGULATION_PLOT; 312 } else if (isVecTri) { 313 vmath->plotType = MATHEMATICA_VECTOR_TRIANGULATION_PLOT; 314 } else if (isVec) { 315 vmath->plotType = MATHEMATICA_VECTOR_PLOT; 316 } else if (isSurface) { 317 vmath->plotType = MATHEMATICA_SURFACE_PLOT; 318 } 319 } 320 PetscFunctionReturn(0); 321 } 322 323 #undef __FUNCT__ 324 #define __FUNCT__ "PetscViewerMathematicaSetLinkName" 325 PetscErrorCode PetscViewerMathematicaSetLinkName(PetscViewer v, const char *name) 326 { 327 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data; 328 PetscErrorCode ierr; 329 330 PetscFunctionBegin; 331 PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 332 PetscValidCharPointer(name,2); 333 ierr = PetscStrallocpy(name, &vmath->linkname);CHKERRQ(ierr); 334 PetscFunctionReturn(0); 335 } 336 337 #undef __FUNCT__ 338 #define __FUNCT__ "PetscViewerMathematicaLinkPort" 339 PetscErrorCode PetscViewerMathematicaSetLinkPort(PetscViewer v, int port) 340 { 341 char name[16]; 342 PetscErrorCode ierr; 343 344 PetscFunctionBegin; 345 snprintf(name, 16, "%6d", port); 346 ierr = PetscViewerMathematicaSetLinkName(v, name);CHKERRQ(ierr); 347 PetscFunctionReturn(0); 348 } 349 350 #undef __FUNCT__ 351 #define __FUNCT__ "PetscViewerMathematicaSetLinkHost" 352 PetscErrorCode PetscViewerMathematicaSetLinkHost(PetscViewer v, const char *host) 353 { 354 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data; 355 PetscErrorCode ierr; 356 357 PetscFunctionBegin; 358 PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,1); 359 PetscValidCharPointer(host,2); 360 ierr = PetscStrallocpy(host, &vmath->linkhost);CHKERRQ(ierr); 361 PetscFunctionReturn(0); 362 } 363 364 #undef __FUNCT__ 365 #define __FUNCT__ "PetscViewerMathematicaSetLinkHost" 366 PetscErrorCode PetscViewerMathematicaSetLinkMode(PetscViewer v, LinkMode mode) 367 { 368 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) v->data; 369 370 PetscFunctionBegin; 371 vmath->linkmode = mode; 372 PetscFunctionReturn(0); 373 } 374 375 /*----------------------------------------- Public Functions --------------------------------------------------------*/ 376 #undef __FUNCT__ 377 #define __FUNCT__ "PetscViewerMathematicaOpen" 378 /*@C 379 PetscViewerMathematicaOpen - Communicates with Mathemtica using MathLink. 380 381 Collective on comm 382 383 Input Parameters: 384 + comm - The MPI communicator 385 . port - [optional] The port to connect on, or PETSC_DECIDE 386 . machine - [optional] The machine to run Mathematica on, or PETSC_NULL 387 - mode - [optional] The connection mode, or PETSC_NULL 388 389 Output Parameter: 390 . viewer - The Mathematica viewer 391 392 Level: intermediate 393 394 Notes: 395 Most users should employ the following commands to access the 396 Mathematica viewers 397 $ 398 $ PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer) 399 $ MatView(Mat matrix, PetscViewer viewer) 400 $ 401 $ or 402 $ 403 $ PetscViewerMathematicaOpen(MPI_Comm comm, int port, char *machine, char *mode, PetscViewer &viewer) 404 $ VecView(Vec vector, PetscViewer viewer) 405 406 Options Database Keys: 407 $ -viewer_math_linkhost <machine> - The host machine for the kernel 408 $ -viewer_math_linkname <name> - The full link name for the connection 409 $ -viewer_math_linkport <port> - The port for the connection 410 $ -viewer_math_mode <mode> - The mode, e.g. Launch, Connect 411 $ -viewer_math_type <type> - The plot type, e.g. Triangulation, Vector 412 $ -viewer_math_graphics <output> - The output type, e.g. Motif, PS, PSFile 413 414 .keywords: PetscViewer, Mathematica, open 415 416 .seealso: MatView(), VecView() 417 @*/ 418 PetscErrorCode PetscViewerMathematicaOpen(MPI_Comm comm, int port, const char machine[], const char mode[], PetscViewer *v) 419 { 420 PetscErrorCode ierr; 421 422 PetscFunctionBegin; 423 ierr = PetscViewerCreate(comm, v);CHKERRQ(ierr); 424 #if 0 425 LinkMode linkmode; 426 ierr = PetscViewerMathematicaSetLinkPort(*v, port);CHKERRQ(ierr); 427 ierr = PetscViewerMathematicaSetLinkHost(*v, machine);CHKERRQ(ierr); 428 ierr = PetscViewerMathematicaParseLinkMode_Private(mode, &linkmode);CHKERRQ(ierr); 429 ierr = PetscViewerMathematicaSetLinkMode(*v, linkmode);CHKERRQ(ierr); 430 #endif 431 ierr = PetscViewerSetType(*v, PETSC_VIEWER_MATHEMATICA);CHKERRQ(ierr); 432 PetscFunctionReturn(0); 433 } 434 435 #undef __FUNCT__ 436 #define __FUNCT__ "PetscViewerMathematicaGetLink" 437 /*@C 438 PetscViewerMathematicaGetLink - Returns the link to Mathematica 439 440 Input Parameters: 441 . viewer - The Mathematica viewer 442 . link - The link to Mathematica 443 444 Level: intermediate 445 446 .keywords PetscViewer, Mathematica, link 447 .seealso PetscViewerMathematicaOpen() 448 @*/ 449 PetscErrorCode PetscViewerMathematicaGetLink(PetscViewer viewer, MLINK *link) 450 { 451 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 452 453 PetscFunctionBegin; 454 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 455 *link = vmath->link; 456 PetscFunctionReturn(0); 457 } 458 459 #undef __FUNCT__ 460 #define __FUNCT__ "PetscViewerMathematicaSkipPackets" 461 /*@C 462 PetscViewerMathematicaSkipPackets - Discard packets sent by Mathematica until a certain packet type is received 463 464 Input Parameters: 465 . viewer - The Mathematica viewer 466 . type - The packet type to search for, e.g RETURNPKT 467 468 Level: advanced 469 470 .keywords PetscViewer, Mathematica, packets 471 .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaGetVector() 472 @*/ 473 PetscErrorCode PetscViewerMathematicaSkipPackets(PetscViewer viewer, int type) 474 { 475 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 476 MLINK link = vmath->link; /* The link to Mathematica */ 477 int pkt; /* The packet type */ 478 479 PetscFunctionBegin; 480 while ((pkt = MLNextPacket(link)) && (pkt != type)) 481 MLNewPacket(link); 482 if (!pkt) { 483 MLClearError(link); 484 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_LIB, (char *) MLErrorMessage(link)); 485 } 486 PetscFunctionReturn(0); 487 } 488 489 #undef __FUNCT__ 490 #define __FUNCT__ "PetscViewerMathematicaGetName" 491 /*@C 492 PetscViewerMathematicaGetName - Retrieve the default name for objects communicated to Mathematica 493 494 Input Parameter: 495 . viewer - The Mathematica viewer 496 497 Output Parameter: 498 . name - The name for new objects created in Mathematica 499 500 Level: intermediate 501 502 .keywords PetscViewer, Mathematica, name 503 .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName() 504 @*/ 505 PetscErrorCode PetscViewerMathematicaGetName(PetscViewer viewer, const char **name) 506 { 507 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 508 509 PetscFunctionBegin; 510 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 511 PetscValidPointer(name,2); 512 *name = vmath->objName; 513 PetscFunctionReturn(0); 514 } 515 516 #undef __FUNCT__ 517 #define __FUNCT__ "PetscViewerMathematicaSetName" 518 /*@C 519 PetscViewerMathematicaSetName - Override the default name for objects communicated to Mathematica 520 521 Input Parameters: 522 . viewer - The Mathematica viewer 523 . name - The name for new objects created in Mathematica 524 525 Level: intermediate 526 527 .keywords PetscViewer, Mathematica, name 528 .seealso PetscViewerMathematicaSetName(), PetscViewerMathematicaClearName() 529 @*/ 530 PetscErrorCode PetscViewerMathematicaSetName(PetscViewer viewer, const char name[]) 531 { 532 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 533 534 PetscFunctionBegin; 535 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 536 PetscValidPointer(name,2); 537 vmath->objName = name; 538 PetscFunctionReturn(0); 539 } 540 541 #undef __FUNCT__ 542 #define __FUNCT__ "PetscViewerMathematicaClearName" 543 /*@C 544 PetscViewerMathematicaClearName - Use the default name for objects communicated to Mathematica 545 546 Input Parameter: 547 . viewer - The Mathematica viewer 548 549 Level: intermediate 550 551 .keywords PetscViewer, Mathematica, name 552 .seealso PetscViewerMathematicaGetName(), PetscViewerMathematicaSetName() 553 @*/ 554 PetscErrorCode PetscViewerMathematicaClearName(PetscViewer viewer) 555 { 556 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 557 558 PetscFunctionBegin; 559 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 560 vmath->objName = PETSC_NULL; 561 PetscFunctionReturn(0); 562 } 563 564 #undef __FUNCT__ 565 #define __FUNCT__ "PetscViewerMathematicaGetVector" 566 /*@C 567 PetscViewerMathematicaGetVector - Retrieve a vector from Mathematica 568 569 Input Parameter: 570 . viewer - The Mathematica viewer 571 572 Output Parameter: 573 . v - The vector 574 575 Level: intermediate 576 577 .keywords PetscViewer, Mathematica, vector 578 .seealso VecView(), PetscViewerMathematicaPutVector() 579 @*/ 580 PetscErrorCode PetscViewerMathematicaGetVector(PetscViewer viewer, Vec v) 581 { 582 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 583 MLINK link; /* The link to Mathematica */ 584 char *name; 585 PetscScalar *mArray,*array; 586 long mSize; 587 int n; 588 PetscErrorCode ierr; 589 590 PetscFunctionBegin; 591 PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID,1); 592 PetscValidHeaderSpecific(v, VEC_CLASSID,2); 593 594 /* Determine the object name */ 595 if (!vmath->objName) { 596 name = "vec"; 597 } else { 598 name = (char *) vmath->objName; 599 } 600 601 link = vmath->link; 602 ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 603 ierr = VecGetArray(v, &array);CHKERRQ(ierr); 604 MLPutFunction(link, "EvaluatePacket", 1); 605 MLPutSymbol(link, name); 606 MLEndPacket(link); 607 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 608 MLGetRealList(link, &mArray, &mSize); 609 if (n != mSize) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG, "Incompatible vector sizes %d %d",n,mSize); 610 ierr = PetscMemcpy(array, mArray, mSize * sizeof(double));CHKERRQ(ierr); 611 MLDisownRealList(link, mArray, mSize); 612 ierr = VecRestoreArray(v, &array);CHKERRQ(ierr); 613 614 PetscFunctionReturn(0); 615 } 616 617 #undef __FUNCT__ 618 #define __FUNCT__ "PetscViewerMathematicaPutVector" 619 /*@C 620 PetscViewerMathematicaPutVector - Send a vector to Mathematica 621 622 Input Parameters: 623 + viewer - The Mathematica viewer 624 - v - The vector 625 626 Level: intermediate 627 628 .keywords PetscViewer, Mathematica, vector 629 .seealso VecView(), PetscViewerMathematicaGetVector() 630 @*/ 631 PetscErrorCode PetscViewerMathematicaPutVector(PetscViewer viewer, Vec v) 632 { 633 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 634 MLINK link = vmath->link; /* The link to Mathematica */ 635 char *name; 636 PetscScalar *array; 637 int n; 638 PetscErrorCode ierr; 639 640 PetscFunctionBegin; 641 /* Determine the object name */ 642 if (!vmath->objName) { 643 name = "vec"; 644 } else { 645 name = (char *) vmath->objName; 646 } 647 ierr = VecGetLocalSize(v, &n);CHKERRQ(ierr); 648 ierr = VecGetArray(v, &array);CHKERRQ(ierr); 649 650 /* Send the Vector object */ 651 MLPutFunction(link, "EvaluatePacket", 1); 652 MLPutFunction(link, "Set", 2); 653 MLPutSymbol(link, name); 654 MLPutRealList(link, array, n); 655 MLEndPacket(link); 656 /* Skip packets until ReturnPacket */ 657 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 658 /* Skip ReturnPacket */ 659 MLNewPacket(link); 660 661 ierr = VecRestoreArray(v, &array);CHKERRQ(ierr); 662 PetscFunctionReturn(0); 663 } 664 665 PetscErrorCode PetscViewerMathematicaPutMatrix(PetscViewer viewer, int m, int n, PetscReal *a) 666 { 667 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 668 MLINK link = vmath->link; /* The link to Mathematica */ 669 char *name; 670 PetscErrorCode ierr; 671 672 PetscFunctionBegin; 673 /* Determine the object name */ 674 if (!vmath->objName) { 675 name = "mat"; 676 } else { 677 name = (char *) vmath->objName; 678 } 679 680 /* Send the dense matrix object */ 681 MLPutFunction(link, "EvaluatePacket", 1); 682 MLPutFunction(link, "Set", 2); 683 MLPutSymbol(link, name); 684 MLPutFunction(link, "Transpose", 1); 685 MLPutFunction(link, "Partition", 2); 686 MLPutRealList(link, a, m*n); 687 MLPutInteger(link, m); 688 MLEndPacket(link); 689 /* Skip packets until ReturnPacket */ 690 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 691 /* Skip ReturnPacket */ 692 MLNewPacket(link); 693 694 PetscFunctionReturn(0); 695 } 696 697 PetscErrorCode PetscViewerMathematicaPutCSRMatrix(PetscViewer viewer, int m, int n, int *i, int *j, PetscReal *a) 698 { 699 PetscViewer_Mathematica *vmath = (PetscViewer_Mathematica *) viewer->data; 700 MLINK link = vmath->link; /* The link to Mathematica */ 701 const char *symbol; 702 char *name; 703 PetscBool match; 704 PetscErrorCode ierr; 705 706 PetscFunctionBegin; 707 /* Determine the object name */ 708 if (!vmath->objName) { 709 name = "mat"; 710 } else { 711 name = (char *) vmath->objName; 712 } 713 714 /* Make sure Mathematica recognizes sparse matrices */ 715 MLPutFunction(link, "EvaluatePacket", 1); 716 MLPutFunction(link, "Needs", 1); 717 MLPutString(link, "LinearAlgebra`CSRMatrix`"); 718 MLEndPacket(link); 719 /* Skip packets until ReturnPacket */ 720 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 721 /* Skip ReturnPacket */ 722 MLNewPacket(link); 723 724 /* Send the CSRMatrix object */ 725 MLPutFunction(link, "EvaluatePacket", 1); 726 MLPutFunction(link, "Set", 2); 727 MLPutSymbol(link, name); 728 MLPutFunction(link, "CSRMatrix", 5); 729 MLPutInteger(link, m); 730 MLPutInteger(link, n); 731 MLPutFunction(link, "Plus", 2); 732 MLPutIntegerList(link, i, m+1); 733 MLPutInteger(link, 1); 734 MLPutFunction(link, "Plus", 2); 735 MLPutIntegerList(link, j, i[m]); 736 MLPutInteger(link, 1); 737 MLPutRealList(link, a, i[m]); 738 MLEndPacket(link); 739 /* Skip packets until ReturnPacket */ 740 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 741 /* Skip ReturnPacket */ 742 MLNewPacket(link); 743 744 /* Check that matrix is valid */ 745 MLPutFunction(link, "EvaluatePacket", 1); 746 MLPutFunction(link, "ValidQ", 1); 747 MLPutSymbol(link, name); 748 MLEndPacket(link); 749 ierr = PetscViewerMathematicaSkipPackets(viewer, RETURNPKT);CHKERRQ(ierr); 750 MLGetSymbol(link, &symbol); 751 ierr = PetscStrcmp("True", (char *) symbol, &match);CHKERRQ(ierr); 752 if (!match) { 753 MLDisownSymbol(link, symbol); 754 SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Invalid CSR matrix in Mathematica"); 755 } 756 MLDisownSymbol(link, symbol); 757 /* Skip ReturnPacket */ 758 MLNewPacket(link); 759 760 PetscFunctionReturn(0); 761 } 762 763