1 #ifdef PETSC_RCS_HEADER 2 static char vcid[] = "$Id: matrix.c,v 1.320 1999/03/02 18:59:40 bsmith Exp curfman $"; 3 #endif 4 5 /* 6 This is where the abstract matrix operations are defined 7 */ 8 9 #include "src/mat/matimpl.h" /*I "mat.h" I*/ 10 #include "src/vec/vecimpl.h" 11 12 #undef __FUNC__ 13 #define __FUNC__ "MatGetRow" 14 /*@C 15 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 16 for each row that you get to ensure that your application does 17 not bleed memory. 18 19 Not Collective 20 21 Input Parameters: 22 + mat - the matrix 23 - row - the row to get 24 25 Output Parameters: 26 + ncols - the number of nonzeros in the row 27 . cols - if nonzero, the column numbers 28 - vals - if nonzero, the values 29 30 Notes: 31 This routine is provided for people who need to have direct access 32 to the structure of a matrix. We hope that we provide enough 33 high-level matrix routines that few users will need it. 34 35 MatGetRow() always returns 0-based column indices, regardless of 36 whether the internal representation is 0-based (default) or 1-based. 37 38 For better efficiency, set cols and/or vals to PETSC_NULL if you do 39 not wish to extract these quantities. 40 41 The user can only examine the values extracted with MatGetRow(); 42 the values cannot be altered. To change the matrix entries, one 43 must use MatSetValues(). 44 45 You can only have one call to MatGetRow() outstanding for a particular 46 matrix at a time. 47 48 Fortran Notes: 49 The calling sequence from Fortran is 50 .vb 51 MatGetRow(matrix,row,ncols,cols,values,ierr) 52 Mat matrix (input) 53 integer row (input) 54 integer ncols (output) 55 integer cols(maxcols) (output) 56 double precision (or double complex) values(maxcols) output 57 .ve 58 where maxcols >= maximum nonzeros in any row of the matrix. 59 60 Caution: 61 Do not try to change the contents of the output arrays (cols and vals). 62 In some cases, this may corrupt the matrix. 63 64 Level: advanced 65 66 .keywords: matrix, row, get, extract 67 68 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal() 69 @*/ 70 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 71 { 72 int ierr; 73 74 PetscFunctionBegin; 75 PetscValidHeaderSpecific(mat,MAT_COOKIE); 76 PetscValidIntPointer(ncols); 77 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 78 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 79 if (!mat->ops->getrow) SETERRQ(PETSC_ERR_SUP,0,""); 80 PLogEventBegin(MAT_GetRow,mat,0,0,0); 81 ierr = (*mat->ops->getrow)(mat,row,ncols,cols,vals); CHKERRQ(ierr); 82 PLogEventEnd(MAT_GetRow,mat,0,0,0); 83 PetscFunctionReturn(0); 84 } 85 86 #undef __FUNC__ 87 #define __FUNC__ "MatRestoreRow" 88 /*@C 89 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 90 91 Not Collective 92 93 Input Parameters: 94 + mat - the matrix 95 . row - the row to get 96 . ncols, cols - the number of nonzeros and their columns 97 - vals - if nonzero the column values 98 99 Notes: 100 This routine should be called after you have finished examining the entries. 101 102 Fortran Notes: 103 The calling sequence from Fortran is 104 .vb 105 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 106 Mat matrix (input) 107 integer row (input) 108 integer ncols (output) 109 integer cols(maxcols) (output) 110 double precision (or double complex) values(maxcols) output 111 .ve 112 Where maxcols >= maximum nonzeros in any row of the matrix. 113 114 In Fortran MatRestoreRow() MUST be called after MatGetRow() 115 before another call to MatGetRow() can be made. 116 117 Level: advanced 118 119 .keywords: matrix, row, restore 120 121 .seealso: MatGetRow() 122 @*/ 123 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 124 { 125 int ierr; 126 127 PetscFunctionBegin; 128 PetscValidHeaderSpecific(mat,MAT_COOKIE); 129 PetscValidIntPointer(ncols); 130 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 131 if (!mat->ops->restorerow) PetscFunctionReturn(0); 132 ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 133 PetscFunctionReturn(0); 134 } 135 136 #undef __FUNC__ 137 #define __FUNC__ "MatView" 138 /*@C 139 MatView - Visualizes a matrix object. 140 141 Collective on Mat unless Viewer is VIEWER_STDOUT_SELF 142 143 Input Parameters: 144 + mat - the matrix 145 - ptr - visualization context 146 147 Notes: 148 The available visualization contexts include 149 + VIEWER_STDOUT_SELF - standard output (default) 150 . VIEWER_STDOUT_WORLD - synchronized standard 151 output where only the first processor opens 152 the file. All other processors send their 153 data to the first processor to print. 154 - VIEWER_DRAW_WORLD - graphical display of nonzero structure 155 156 The user can open alternative visualization contexts with 157 + ViewerASCIIOpen() - Outputs matrix to a specified file 158 . ViewerBinaryOpen() - Outputs matrix in binary to a 159 specified file; corresponding input uses MatLoad() 160 . ViewerDrawOpen() - Outputs nonzero matrix structure to 161 an X window display 162 - ViewerSocketOpen() - Outputs matrix to Socket viewer. 163 Currently only the sequential dense and AIJ 164 matrix types support the Socket viewer. 165 166 The user can call ViewerSetFormat() to specify the output 167 format of ASCII printed objects (when using VIEWER_STDOUT_SELF, 168 VIEWER_STDOUT_WORLD and ViewerASCIIOpen). Available formats include 169 + VIEWER_FORMAT_ASCII_DEFAULT - default, prints matrix contents 170 . VIEWER_FORMAT_ASCII_MATLAB - prints matrix contents in Matlab format 171 . VIEWER_FORMAT_ASCII_DENSE - prints entire matrix including zeros 172 . VIEWER_FORMAT_ASCII_COMMON - prints matrix contents, using a sparse 173 format common among all matrix types 174 . VIEWER_FORMAT_ASCII_IMPL - prints matrix contents, using an implementation-specific 175 format (which is in many cases the same as the default) 176 . VIEWER_FORMAT_ASCII_INFO - prints basic information about the matrix 177 size and structure (not the matrix entries) 178 - VIEWER_FORMAT_ASCII_INFO_LONG - prints more detailed information about 179 the matrix structure 180 181 Level: beginner 182 183 .keywords: matrix, view, visualize, output, print, write, draw 184 185 .seealso: ViewerSetFormat(), ViewerASCIIOpen(), ViewerDrawOpen(), 186 ViewerSocketOpen(), ViewerBinaryOpen(), MatLoad() 187 @*/ 188 int MatView(Mat mat,Viewer viewer) 189 { 190 int format, ierr, rows, cols; 191 char *cstr; 192 ViewerType vtype; 193 194 PetscFunctionBegin; 195 PetscValidHeaderSpecific(mat,MAT_COOKIE); 196 if (viewer) PetscValidHeaderSpecific(viewer,VIEWER_COOKIE); 197 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 198 199 if (!viewer) { 200 viewer = VIEWER_STDOUT_SELF; 201 } 202 203 ierr = ViewerGetType(viewer,&vtype); 204 if (PetscTypeCompare(vtype,ASCII_VIEWER)) { 205 ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 206 if (format == VIEWER_FORMAT_ASCII_INFO || format == VIEWER_FORMAT_ASCII_INFO_LONG) { 207 ViewerASCIIPrintf(viewer,"Matrix Object:\n"); 208 ierr = MatGetType(mat,PETSC_NULL,&cstr); CHKERRQ(ierr); 209 ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr); 210 ViewerASCIIPrintf(viewer," type=%s, rows=%d, cols=%d\n",cstr,rows,cols); 211 if (mat->ops->getinfo) { 212 MatInfo info; 213 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info); CHKERRQ(ierr); 214 ViewerASCIIPrintf(viewer," total: nonzeros=%d, allocated nonzeros=%d\n", 215 (int)info.nz_used,(int)info.nz_allocated); 216 } 217 } 218 } 219 if (mat->ops->view) { 220 ierr = ViewerASCIIPushTab(viewer);CHKERRQ(ierr); 221 ierr = (*mat->ops->view)(mat,viewer); CHKERRQ(ierr); 222 ierr = ViewerASCIIPopTab(viewer);CHKERRQ(ierr); 223 } 224 PetscFunctionReturn(0); 225 } 226 227 #undef __FUNC__ 228 #define __FUNC__ "MatScaleSystem" 229 /*@C 230 MatScaleSystem - Scale a vector solution and right hand side to 231 match the scaling of a scaled matrix. 232 233 Collective on Mat 234 235 Input Parameter: 236 + mat - the matrix 237 . x - solution vector (or PETSC_NULL) 238 + b - right hand side vector (or PETSC_NULL) 239 240 241 Notes: 242 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 243 internally scaled, so this does nothing. For MPIROWBS it 244 permutes and diagonally scales. 245 246 The SLES methods automatically call this routine when required 247 (via PCPreSolve()) so it is rarely used directly. 248 249 Level: Developer 250 251 .keywords: matrix, scale 252 253 .seealso: MatUseScaledForm(), MatUnScaleSystem() 254 @*/ 255 int MatScaleSystem(Mat mat,Vec x,Vec b) 256 { 257 int ierr; 258 259 PetscFunctionBegin; 260 PetscValidHeaderSpecific(mat,MAT_COOKIE); 261 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);} 262 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);} 263 if (mat->ops->scalesystem) { 264 ierr = (*mat->ops->scalesystem)(mat,x,b); CHKERRQ(ierr); 265 } 266 PetscFunctionReturn(0); 267 } 268 269 #undef __FUNC__ 270 #define __FUNC__ "MatUnScaleSystem" 271 /*@C 272 MatUnScaleSystem - Unscales a vector solution and right hand side to 273 match the original scaling of a scaled matrix. 274 275 Collective on Mat 276 277 Input Parameter: 278 + mat - the matrix 279 . x - solution vector (or PETSC_NULL) 280 + b - right hand side vector (or PETSC_NULL) 281 282 283 Notes: 284 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 285 internally scaled, so this does nothing. For MPIROWBS it 286 permutes and diagonally scales. 287 288 The SLES methods automatically call this routine when required 289 (via PCPreSolve()) so it is rarely used directly. 290 291 Level: Developer 292 293 .keywords: matrix, scale 294 295 .seealso: MatUseScaledForm(), MatScaleSystem() 296 @*/ 297 int MatUnScaleSystem(Mat mat,Vec x,Vec b) 298 { 299 int ierr; 300 301 PetscFunctionBegin; 302 PetscValidHeaderSpecific(mat,MAT_COOKIE); 303 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);} 304 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);} 305 if (mat->ops->unscalesystem) { 306 ierr = (*mat->ops->unscalesystem)(mat,x,b); CHKERRQ(ierr); 307 } 308 PetscFunctionReturn(0); 309 } 310 311 #undef __FUNC__ 312 #define __FUNC__ "MatUseScaledForm" 313 /*@C 314 MatUseScaledForm - For matrix storage formats that scale the 315 matrix (for example MPIRowBS matrices are diagonally scaled on 316 assembly) indicates matrix operations (MatMult() etc) are 317 applied using the scaled matrix. 318 319 Collective on Mat 320 321 Input Parameter: 322 + mat - the matrix 323 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 324 applying the original matrix 325 326 Notes: 327 For scaled matrix formats, applying the original, unscaled matrix 328 will be slightly more expensive 329 330 Level: Developer 331 332 .keywords: matrix, scale 333 334 .seealso: MatScaleSystem(), MatUnScaleSystem() 335 @*/ 336 int MatUseScaledForm(Mat mat,PetscTruth scaled) 337 { 338 int ierr; 339 340 PetscFunctionBegin; 341 PetscValidHeaderSpecific(mat,MAT_COOKIE); 342 if (mat->ops->usescaledform) { 343 ierr = (*mat->ops->usescaledform)(mat,scaled); CHKERRQ(ierr); 344 } 345 PetscFunctionReturn(0); 346 } 347 348 #undef __FUNC__ 349 #define __FUNC__ "MatDestroy" 350 /*@C 351 MatDestroy - Frees space taken by a matrix. 352 353 Collective on Mat 354 355 Input Parameter: 356 . mat - the matrix 357 358 Level: beginner 359 360 .keywords: matrix, destroy 361 @*/ 362 int MatDestroy(Mat mat) 363 { 364 int ierr; 365 366 PetscFunctionBegin; 367 PetscValidHeaderSpecific(mat,MAT_COOKIE); 368 ierr = (*mat->ops->destroy)(mat); CHKERRQ(ierr); 369 PetscFunctionReturn(0); 370 } 371 372 #undef __FUNC__ 373 #define __FUNC__ "MatValid" 374 /*@ 375 MatValid - Checks whether a matrix object is valid. 376 377 Collective on Mat 378 379 Input Parameter: 380 . m - the matrix to check 381 382 Output Parameter: 383 flg - flag indicating matrix status, either 384 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 385 386 Level: developer 387 388 .keywords: matrix, valid 389 @*/ 390 int MatValid(Mat m,PetscTruth *flg) 391 { 392 PetscFunctionBegin; 393 PetscValidIntPointer(flg); 394 if (!m) *flg = PETSC_FALSE; 395 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 396 else *flg = PETSC_TRUE; 397 PetscFunctionReturn(0); 398 } 399 400 #undef __FUNC__ 401 #define __FUNC__ "MatSetValues" 402 /*@ 403 MatSetValues - Inserts or adds a block of values into a matrix. 404 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 405 MUST be called after all calls to MatSetValues() have been completed. 406 407 Not Collective 408 409 Input Parameters: 410 + mat - the matrix 411 . v - a logically two-dimensional array of values 412 . m, idxm - the number of rows and their global indices 413 . n, idxn - the number of columns and their global indices 414 - addv - either ADD_VALUES or INSERT_VALUES, where 415 ADD_VALUES adds values to any existing entries, and 416 INSERT_VALUES replaces existing entries with new values 417 418 Notes: 419 By default the values, v, are row-oriented and unsorted. 420 See MatSetOption() for other options. 421 422 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 423 options cannot be mixed without intervening calls to the assembly 424 routines. 425 426 MatSetValues() uses 0-based row and column numbers in Fortran 427 as well as in C. 428 429 Negative indices may be passed in idxm and idxn, these rows and columns are 430 simply ignored. This allows easily inserting element stiffness matrices 431 with homogeneous Dirchlet boundary conditions that you don't want represented 432 in the matrix. 433 434 Efficiency Alert: 435 The routine MatSetValuesBlocked() may offer much better efficiency 436 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 437 438 Level: beginner 439 440 .keywords: matrix, insert, add, set, values 441 442 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 443 @*/ 444 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 445 { 446 int ierr; 447 448 PetscFunctionBegin; 449 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 450 PetscValidHeaderSpecific(mat,MAT_COOKIE); 451 PetscValidIntPointer(idxm); 452 PetscValidIntPointer(idxn); 453 PetscValidScalarPointer(v); 454 if (mat->insertmode == NOT_SET_VALUES) { 455 mat->insertmode = addv; 456 } 457 #if defined(USE_PETSC_BOPT_g) 458 else if (mat->insertmode != addv) { 459 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 460 } 461 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 462 #endif 463 464 if (mat->assembled) { 465 mat->was_assembled = PETSC_TRUE; 466 mat->assembled = PETSC_FALSE; 467 } 468 PLogEventBegin(MAT_SetValues,mat,0,0,0); 469 if (!mat->ops->setvalues) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type"); 470 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 471 PLogEventEnd(MAT_SetValues,mat,0,0,0); 472 PetscFunctionReturn(0); 473 } 474 475 #undef __FUNC__ 476 #define __FUNC__ "MatSetValuesBlocked" 477 /*@ 478 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 479 480 Not Collective 481 482 Input Parameters: 483 + mat - the matrix 484 . v - a logically two-dimensional array of values 485 . m, idxm - the number of block rows and their global block indices 486 . n, idxn - the number of block columns and their global block indices 487 - addv - either ADD_VALUES or INSERT_VALUES, where 488 ADD_VALUES adds values to any existing entries, and 489 INSERT_VALUES replaces existing entries with new values 490 491 Notes: 492 By default the values, v, are row-oriented and unsorted. So the layout of 493 v is the same as for MatSetValues(). See MatSetOption() for other options. 494 495 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 496 options cannot be mixed without intervening calls to the assembly 497 routines. 498 499 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 500 as well as in C. 501 502 Negative indices may be passed in idxm and idxn, these rows and columns are 503 simply ignored. This allows easily inserting element stiffness matrices 504 with homogeneous Dirchlet boundary conditions that you don't want represented 505 in the matrix. 506 507 Each time an entry is set within a sparse matrix via MatSetValues(), 508 internal searching must be done to determine where to place the the 509 data in the matrix storage space. By instead inserting blocks of 510 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 511 reduced. 512 513 Restrictions: 514 MatSetValuesBlocked() is currently supported only for the block AIJ 515 matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via 516 MatCreateSeqBAIJ() and MatCreateMPIBAIJ()). 517 518 Level: intermediate 519 520 .keywords: matrix, insert, add, set, values 521 522 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 523 @*/ 524 int MatSetValuesBlocked(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v,InsertMode addv) 525 { 526 int ierr; 527 528 PetscFunctionBegin; 529 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 530 PetscValidHeaderSpecific(mat,MAT_COOKIE); 531 PetscValidIntPointer(idxm); 532 PetscValidIntPointer(idxn); 533 PetscValidScalarPointer(v); 534 if (mat->insertmode == NOT_SET_VALUES) { 535 mat->insertmode = addv; 536 } 537 #if defined(USE_PETSC_BOPT_g) 538 else if (mat->insertmode != addv) { 539 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 540 } 541 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 542 #endif 543 544 if (mat->assembled) { 545 mat->was_assembled = PETSC_TRUE; 546 mat->assembled = PETSC_FALSE; 547 } 548 PLogEventBegin(MAT_SetValues,mat,0,0,0); 549 if (!mat->ops->setvaluesblocked) SETERRQ(PETSC_ERR_SUP,1,"Not supported for this matrix type"); 550 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 551 PLogEventEnd(MAT_SetValues,mat,0,0,0); 552 PetscFunctionReturn(0); 553 } 554 555 /*MC 556 MatSetValue - Set a single entry into a matrix. 557 558 Synopsis: 559 void MatSetValue(Mat m,int row,int col,Scalar value,InsertMode mode); 560 561 Not collective 562 563 Input Parameters: 564 + m - the matrix 565 . row - the row location of the entry 566 . col - the column location of the entry 567 . value - the value to insert 568 - mode - either INSERT_VALUES or ADD_VALUES 569 570 Notes: 571 For efficiency one should use MatSetValues() and set several or many 572 values simultaneously if possible. 573 574 Note that VecSetValue() does NOT return an error code (since this 575 is checked internally). 576 577 Level: beginner 578 579 .seealso: MatSetValues() 580 M*/ 581 582 #undef __FUNC__ 583 #define __FUNC__ "MatGetValues" 584 /*@ 585 MatGetValues - Gets a block of values from a matrix. 586 587 Not Collective; currently only returns a local block 588 589 Input Parameters: 590 + mat - the matrix 591 . v - a logically two-dimensional array for storing the values 592 . m, idxm - the number of rows and their global indices 593 - n, idxn - the number of columns and their global indices 594 595 Notes: 596 The user must allocate space (m*n Scalars) for the values, v. 597 The values, v, are then returned in a row-oriented format, 598 analogous to that used by default in MatSetValues(). 599 600 MatGetValues() uses 0-based row and column numbers in 601 Fortran as well as in C. 602 603 MatGetValues() requires that the matrix has been assembled 604 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 605 MatSetValues() and MatGetValues() CANNOT be made in succession 606 without intermediate matrix assembly. 607 608 Level: advanced 609 610 .keywords: matrix, get, values 611 612 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 613 @*/ 614 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 615 { 616 int ierr; 617 618 PetscFunctionBegin; 619 PetscValidHeaderSpecific(mat,MAT_COOKIE); 620 PetscValidIntPointer(idxm); 621 PetscValidIntPointer(idxn); 622 PetscValidScalarPointer(v); 623 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 624 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 625 if (!mat->ops->getvalues) SETERRQ(PETSC_ERR_SUP,0,""); 626 627 PLogEventBegin(MAT_GetValues,mat,0,0,0); 628 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr); 629 PLogEventEnd(MAT_GetValues,mat,0,0,0); 630 PetscFunctionReturn(0); 631 } 632 633 #undef __FUNC__ 634 #define __FUNC__ "MatSetLocalToGlobalMapping" 635 /*@ 636 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 637 the routine MatSetValuesLocal() to allow users to insert matrix entries 638 using a local (per-processor) numbering. 639 640 Not Collective 641 642 Input Parameters: 643 + x - the matrix 644 - mapping - mapping created with ISLocalToGlobalMappingCreate() 645 or ISLocalToGlobalMappingCreateIS() 646 647 Level: intermediate 648 649 .keywords: matrix, set, values, local, global, mapping 650 651 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 652 @*/ 653 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 654 { 655 PetscFunctionBegin; 656 PetscValidHeaderSpecific(x,MAT_COOKIE); 657 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 658 659 if (x->mapping) { 660 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix"); 661 } 662 663 x->mapping = mapping; 664 PetscObjectReference((PetscObject)mapping); 665 PetscFunctionReturn(0); 666 } 667 668 #undef __FUNC__ 669 #define __FUNC__ "MatSetLocalToGlobalMappingBlocked" 670 /*@ 671 MatSetLocalToGlobalMappingBlocked - Sets a local-to-global numbering for use 672 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 673 entries using a local (per-processor) numbering. 674 675 Not Collective 676 677 Input Parameters: 678 + x - the matrix 679 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 680 ISLocalToGlobalMappingCreateIS() 681 682 Level: intermediate 683 684 .keywords: matrix, set, values, local ordering 685 686 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 687 MatSetValuesBlocked(), MatSetValuesLocal() 688 @*/ 689 int MatSetLocalToGlobalMappingBlocked(Mat x,ISLocalToGlobalMapping mapping) 690 { 691 PetscFunctionBegin; 692 PetscValidHeaderSpecific(x,MAT_COOKIE); 693 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 694 695 if (x->bmapping) { 696 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Mapping already set for matrix"); 697 } 698 699 x->bmapping = mapping; 700 PetscObjectReference((PetscObject)mapping); 701 PetscFunctionReturn(0); 702 } 703 704 #undef __FUNC__ 705 #define __FUNC__ "MatSetValuesLocal" 706 /*@ 707 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 708 using a local ordering of the nodes. 709 710 Not Collective 711 712 Input Parameters: 713 + x - the matrix 714 . nrow, irow - number of rows and their local indices 715 . ncol, icol - number of columns and their local indices 716 . y - a logically two-dimensional array of values 717 - addv - either INSERT_VALUES or ADD_VALUES, where 718 ADD_VALUES adds values to any existing entries, and 719 INSERT_VALUES replaces existing entries with new values 720 721 Notes: 722 Before calling MatSetValuesLocal(), the user must first set the 723 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 724 725 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 726 options cannot be mixed without intervening calls to the assembly 727 routines. 728 729 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 730 MUST be called after all calls to MatSetValuesLocal() have been completed. 731 732 Level: intermediate 733 734 .keywords: matrix, set, values, local ordering 735 736 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping() 737 @*/ 738 int MatSetValuesLocal(Mat mat,int nrow,int *irow,int ncol, int *icol,Scalar *y,InsertMode addv) 739 { 740 int ierr,irowm[2048],icolm[2048]; 741 742 PetscFunctionBegin; 743 PetscValidHeaderSpecific(mat,MAT_COOKIE); 744 PetscValidIntPointer(irow); 745 PetscValidIntPointer(icol); 746 PetscValidScalarPointer(y); 747 748 if (mat->insertmode == NOT_SET_VALUES) { 749 mat->insertmode = addv; 750 } 751 #if defined(USE_PETSC_BOPT_g) 752 else if (mat->insertmode != addv) { 753 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 754 } 755 if (!mat->mapping) { 756 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMapping()"); 757 } 758 if (nrow > 2048 || ncol > 2048) { 759 SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 760 } 761 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 762 #endif 763 764 if (mat->assembled) { 765 mat->was_assembled = PETSC_TRUE; 766 mat->assembled = PETSC_FALSE; 767 } 768 PLogEventBegin(MAT_SetValues,mat,0,0,0); 769 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm); CHKERRQ(ierr); 770 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 771 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 772 PLogEventEnd(MAT_SetValues,mat,0,0,0); 773 PetscFunctionReturn(0); 774 } 775 776 #undef __FUNC__ 777 #define __FUNC__ "MatSetValuesBlockedLocal" 778 /*@ 779 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 780 using a local ordering of the nodes a block at a time. 781 782 Not Collective 783 784 Input Parameters: 785 + x - the matrix 786 . nrow, irow - number of rows and their local indices 787 . ncol, icol - number of columns and their local indices 788 . y - a logically two-dimensional array of values 789 - addv - either INSERT_VALUES or ADD_VALUES, where 790 ADD_VALUES adds values to any existing entries, and 791 INSERT_VALUES replaces existing entries with new values 792 793 Notes: 794 Before calling MatSetValuesBlockedLocal(), the user must first set the 795 local-to-global mapping by calling MatSetLocalToGlobalMappingBlocked(), 796 where the mapping MUST be set for matrix blocks, not for matrix elements. 797 798 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 799 options cannot be mixed without intervening calls to the assembly 800 routines. 801 802 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 803 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 804 805 Level: intermediate 806 807 .keywords: matrix, set, values, blocked, local 808 809 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlocked(), MatSetValuesBlocked() 810 @*/ 811 int MatSetValuesBlockedLocal(Mat mat,int nrow,int *irow,int ncol,int *icol,Scalar *y,InsertMode addv) 812 { 813 int ierr,irowm[2048],icolm[2048]; 814 815 PetscFunctionBegin; 816 PetscValidHeaderSpecific(mat,MAT_COOKIE); 817 PetscValidIntPointer(irow); 818 PetscValidIntPointer(icol); 819 PetscValidScalarPointer(y); 820 if (mat->insertmode == NOT_SET_VALUES) { 821 mat->insertmode = addv; 822 } 823 #if defined(USE_PETSC_BOPT_g) 824 else if (mat->insertmode != addv) { 825 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,1,"Cannot mix add values and insert values"); 826 } 827 if (!mat->bmapping) { 828 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Local to global never set with MatSetLocalToGlobalMappingBlocked()"); 829 } 830 if (nrow > 2048 || ncol > 2048) { 831 SETERRQ2(PETSC_ERR_SUP,0,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 832 } 833 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 834 #endif 835 836 if (mat->assembled) { 837 mat->was_assembled = PETSC_TRUE; 838 mat->assembled = PETSC_FALSE; 839 } 840 PLogEventBegin(MAT_SetValues,mat,0,0,0); 841 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm); CHKERRQ(ierr); 842 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm); CHKERRQ(ierr); 843 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 844 PLogEventEnd(MAT_SetValues,mat,0,0,0); 845 PetscFunctionReturn(0); 846 } 847 848 /* --------------------------------------------------------*/ 849 #undef __FUNC__ 850 #define __FUNC__ "MatMult" 851 /*@ 852 MatMult - Computes the matrix-vector product, y = Ax. 853 854 Collective on Mat and Vec 855 856 Input Parameters: 857 + mat - the matrix 858 - x - the vector to be multilplied 859 860 Output Parameters: 861 . y - the result 862 863 Notes: 864 The vectors x and y cannot be the same. I.e., one cannot 865 call MatMult(A,y,y). 866 867 Level: beginner 868 869 .keywords: matrix, multiply, matrix-vector product 870 871 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 872 @*/ 873 int MatMult(Mat mat,Vec x,Vec y) 874 { 875 int ierr; 876 877 PetscFunctionBegin; 878 PetscValidHeaderSpecific(mat,MAT_COOKIE); 879 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 880 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 881 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 882 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors"); 883 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 884 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 885 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 886 887 PLogEventBegin(MAT_Mult,mat,x,y,0); 888 ierr = (*mat->ops->mult)(mat,x,y); CHKERRQ(ierr); 889 PLogEventEnd(MAT_Mult,mat,x,y,0); 890 891 PetscFunctionReturn(0); 892 } 893 894 #undef __FUNC__ 895 #define __FUNC__ "MatMultTrans" 896 /*@ 897 MatMultTrans - Computes matrix transpose times a vector. 898 899 Collective on Mat and Vec 900 901 Input Parameters: 902 + mat - the matrix 903 - x - the vector to be multilplied 904 905 Output Parameters: 906 . y - the result 907 908 Notes: 909 The vectors x and y cannot be the same. I.e., one cannot 910 call MatMultTrans(A,y,y). 911 912 Level: beginner 913 914 .keywords: matrix, multiply, matrix-vector product, transpose 915 916 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 917 @*/ 918 int MatMultTrans(Mat mat,Vec x,Vec y) 919 { 920 int ierr; 921 922 PetscFunctionBegin; 923 PetscValidHeaderSpecific(mat,MAT_COOKIE); 924 PetscValidHeaderSpecific(x,VEC_COOKIE); PetscValidHeaderSpecific(y,VEC_COOKIE); 925 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 926 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 927 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"x and y must be different vectors"); 928 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 929 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 930 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 931 ierr = (*mat->ops->multtrans)(mat,x,y); CHKERRQ(ierr); 932 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 933 PetscFunctionReturn(0); 934 } 935 936 #undef __FUNC__ 937 #define __FUNC__ "MatMultAdd" 938 /*@ 939 MatMultAdd - Computes v3 = v2 + A * v1. 940 941 Collective on Mat and Vec 942 943 Input Parameters: 944 + mat - the matrix 945 - v1, v2 - the vectors 946 947 Output Parameters: 948 . v3 - the result 949 950 Notes: 951 The vectors v1 and v3 cannot be the same. I.e., one cannot 952 call MatMultAdd(A,v1,v2,v1). 953 954 Level: beginner 955 956 .keywords: matrix, multiply, matrix-vector product, add 957 958 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 959 @*/ 960 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 961 { 962 int ierr; 963 964 PetscFunctionBegin; 965 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 966 PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE); 967 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 968 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 969 if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N); 970 if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N); 971 if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N); 972 if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n); 973 if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n); 974 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors"); 975 976 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 977 ierr = (*mat->ops->multadd)(mat,v1,v2,v3); CHKERRQ(ierr); 978 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 979 PetscFunctionReturn(0); 980 } 981 982 #undef __FUNC__ 983 #define __FUNC__ "MatMultTransAdd" 984 /*@ 985 MatMultTransAdd - Computes v3 = v2 + A' * v1. 986 987 Collective on Mat and Vec 988 989 Input Parameters: 990 + mat - the matrix 991 - v1, v2 - the vectors 992 993 Output Parameters: 994 . v3 - the result 995 996 Notes: 997 The vectors v1 and v3 cannot be the same. I.e., one cannot 998 call MatMultTransAdd(A,v1,v2,v1). 999 1000 Level: beginner 1001 1002 .keywords: matrix, multiply, matrix-vector product, transpose, add 1003 1004 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 1005 @*/ 1006 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1007 { 1008 int ierr; 1009 1010 PetscFunctionBegin; 1011 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 1012 PetscValidHeaderSpecific(v2,VEC_COOKIE);PetscValidHeaderSpecific(v3,VEC_COOKIE); 1013 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1014 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1015 if (!mat->ops->multtransadd) SETERRQ(PETSC_ERR_SUP,0,""); 1016 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,0,"v1 and v3 must be different vectors"); 1017 if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N); 1018 if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N); 1019 if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N); 1020 1021 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 1022 ierr = (*mat->ops->multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr); 1023 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 1024 PetscFunctionReturn(0); 1025 } 1026 /* ------------------------------------------------------------*/ 1027 #undef __FUNC__ 1028 #define __FUNC__ "MatGetInfo" 1029 /*@C 1030 MatGetInfo - Returns information about matrix storage (number of 1031 nonzeros, memory, etc.). 1032 1033 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 1034 as the flag 1035 1036 Input Parameters: 1037 . mat - the matrix 1038 1039 Output Parameters: 1040 + flag - flag indicating the type of parameters to be returned 1041 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 1042 MAT_GLOBAL_SUM - sum over all processors) 1043 - info - matrix information context 1044 1045 Notes: 1046 The MatInfo context contains a variety of matrix data, including 1047 number of nonzeros allocated and used, number of mallocs during 1048 matrix assembly, etc. Additional information for factored matrices 1049 is provided (such as the fill ratio, number of mallocs during 1050 factorization, etc.). Much of this info is printed to STDOUT 1051 when using the runtime options 1052 $ -log_info -mat_view_info 1053 1054 Example for C/C++ Users: 1055 See the file ${PETSC_DIR}/include/mat.h for a complete list of 1056 data within the MatInfo context. For example, 1057 .vb 1058 MatInfo info; 1059 Mat A; 1060 double mal, nz_a, nz_u; 1061 1062 MatGetInfo(A,MAT_LOCAL,&info); 1063 mal = info.mallocs; 1064 nz_a = info.nz_allocated; 1065 .ve 1066 1067 Example for Fortran Users: 1068 Fortran users should declare info as a double precision 1069 array of dimension MAT_INFO_SIZE, and then extract the parameters 1070 of interest. See the file ${PETSC_DIR}/include/finclude/mat.h 1071 a complete list of parameter names. 1072 .vb 1073 double precision info(MAT_INFO_SIZE) 1074 double precision mal, nz_a 1075 Mat A 1076 integer ierr 1077 1078 call MatGetInfo(A,MAT_LOCAL,info,ierr) 1079 mal = info(MAT_INFO_MALLOCS) 1080 nz_a = info(MAT_INFO_NZ_ALLOCATED) 1081 .ve 1082 1083 Level: intermediate 1084 1085 .keywords: matrix, get, info, storage, nonzeros, memory, fill 1086 @*/ 1087 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 1088 { 1089 int ierr; 1090 1091 PetscFunctionBegin; 1092 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1093 PetscValidPointer(info); 1094 if (!mat->ops->getinfo) SETERRQ(PETSC_ERR_SUP,0,""); 1095 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 1096 PetscFunctionReturn(0); 1097 } 1098 1099 /* ----------------------------------------------------------*/ 1100 #undef __FUNC__ 1101 #define __FUNC__ "MatILUDTFactor" 1102 /*@ 1103 MatILUDTFactor - Performs a drop tolerance ILU factorization. 1104 1105 Collective on Mat 1106 1107 Input Parameters: 1108 + mat - the matrix 1109 . dt - the drop tolerance 1110 . maxnz - the maximum number of nonzeros per row allowed 1111 . row - row permutation 1112 - col - column permutation 1113 1114 Output Parameters: 1115 . fact - the factored matrix 1116 1117 Notes: 1118 Most users should employ the simplified SLES interface for linear solvers 1119 instead of working directly with matrix algebra routines such as this. 1120 See, e.g., SLESCreate(). 1121 1122 Level: developer 1123 1124 .keywords: matrix, factor, LU, in-place 1125 1126 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 1127 @*/ 1128 int MatILUDTFactor(Mat mat,double dt,int maxnz,IS row,IS col,Mat *fact) 1129 { 1130 int ierr; 1131 1132 PetscFunctionBegin; 1133 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1134 PetscValidPointer(fact); 1135 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1136 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1137 if (!mat->ops->iludtfactor) SETERRQ(PETSC_ERR_SUP,0,""); 1138 1139 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 1140 ierr = (*mat->ops->iludtfactor)(mat,dt,maxnz,row,col,fact); CHKERRQ(ierr); 1141 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 1142 1143 PetscFunctionReturn(0); 1144 } 1145 1146 #undef __FUNC__ 1147 #define __FUNC__ "MatLUFactor" 1148 /*@ 1149 MatLUFactor - Performs in-place LU factorization of matrix. 1150 1151 Collective on Mat 1152 1153 Input Parameters: 1154 + mat - the matrix 1155 . row - row permutation 1156 . col - column permutation 1157 - f - expected fill as ratio of original fill. 1158 1159 Notes: 1160 Most users should employ the simplified SLES interface for linear solvers 1161 instead of working directly with matrix algebra routines such as this. 1162 See, e.g., SLESCreate(). 1163 1164 Level: developer 1165 1166 .keywords: matrix, factor, LU, in-place 1167 1168 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 1169 @*/ 1170 int MatLUFactor(Mat mat,IS row,IS col,double f) 1171 { 1172 int ierr; 1173 1174 PetscFunctionBegin; 1175 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1176 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1177 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1178 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1179 if (!mat->ops->lufactor) SETERRQ(PETSC_ERR_SUP,0,""); 1180 1181 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 1182 ierr = (*mat->ops->lufactor)(mat,row,col,f); CHKERRQ(ierr); 1183 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 1184 PetscFunctionReturn(0); 1185 } 1186 1187 #undef __FUNC__ 1188 #define __FUNC__ "MatILUFactor" 1189 /*@ 1190 MatILUFactor - Performs in-place ILU factorization of matrix. 1191 1192 Collective on Mat 1193 1194 Input Parameters: 1195 + mat - the matrix 1196 . row - row permutation 1197 . col - column permutation 1198 - info - structure containing 1199 $ levels - number of levels of fill. 1200 $ expected fill - as ratio of original fill. 1201 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 1202 missing diagonal entries) 1203 1204 Notes: 1205 Probably really in-place only when level of fill is zero, otherwise allocates 1206 new space to store factored matrix and deletes previous memory. 1207 1208 Most users should employ the simplified SLES interface for linear solvers 1209 instead of working directly with matrix algebra routines such as this. 1210 See, e.g., SLESCreate(). 1211 1212 Level: developer 1213 1214 .keywords: matrix, factor, ILU, in-place 1215 1216 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 1217 @*/ 1218 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info) 1219 { 1220 int ierr; 1221 1222 PetscFunctionBegin; 1223 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1224 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1225 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1226 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1227 if (!mat->ops->ilufactor) SETERRQ(PETSC_ERR_SUP,0,""); 1228 1229 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 1230 ierr = (*mat->ops->ilufactor)(mat,row,col,info); CHKERRQ(ierr); 1231 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 1232 PetscFunctionReturn(0); 1233 } 1234 1235 #undef __FUNC__ 1236 #define __FUNC__ "MatLUFactorSymbolic" 1237 /*@ 1238 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 1239 Call this routine before calling MatLUFactorNumeric(). 1240 1241 Collective on Mat 1242 1243 Input Parameters: 1244 + mat - the matrix 1245 . row, col - row and column permutations 1246 - f - expected fill as ratio of the original number of nonzeros, 1247 for example 3.0; choosing this parameter well can result in 1248 more efficient use of time and space. Run with the option -log_info 1249 to determine an optimal value to use 1250 1251 Output Parameter: 1252 . fact - new matrix that has been symbolically factored 1253 1254 Notes: 1255 See the file ${PETSC_DIR}/Performance for additional information about 1256 choosing the fill factor for better efficiency. 1257 1258 Most users should employ the simplified SLES interface for linear solvers 1259 instead of working directly with matrix algebra routines such as this. 1260 See, e.g., SLESCreate(). 1261 1262 Level: developer 1263 1264 .keywords: matrix, factor, LU, symbolic, fill 1265 1266 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 1267 @*/ 1268 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 1269 { 1270 int ierr; 1271 1272 PetscFunctionBegin; 1273 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1274 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"matrix must be square"); 1275 PetscValidPointer(fact); 1276 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1277 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1278 if (!mat->ops->lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,""); 1279 1280 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 1281 ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr); 1282 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 1283 PetscFunctionReturn(0); 1284 } 1285 1286 #undef __FUNC__ 1287 #define __FUNC__ "MatLUFactorNumeric" 1288 /*@ 1289 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 1290 Call this routine after first calling MatLUFactorSymbolic(). 1291 1292 Collective on Mat 1293 1294 Input Parameters: 1295 + mat - the matrix 1296 - row, col - row and column permutations 1297 1298 Output Parameters: 1299 . fact - symbolically factored matrix that must have been generated 1300 by MatLUFactorSymbolic() 1301 1302 Notes: 1303 See MatLUFactor() for in-place factorization. See 1304 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 1305 1306 Most users should employ the simplified SLES interface for linear solvers 1307 instead of working directly with matrix algebra routines such as this. 1308 See, e.g., SLESCreate(). 1309 1310 Level: developer 1311 1312 .keywords: matrix, factor, LU, numeric 1313 1314 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 1315 @*/ 1316 int MatLUFactorNumeric(Mat mat,Mat *fact) 1317 { 1318 int ierr,flg; 1319 1320 PetscFunctionBegin; 1321 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1322 PetscValidPointer(fact); 1323 PetscValidHeaderSpecific(*fact,MAT_COOKIE); 1324 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1325 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1326 SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d", 1327 mat->M,(*fact)->M,mat->N,(*fact)->N); 1328 } 1329 if (!(*fact)->ops->lufactornumeric) SETERRQ(PETSC_ERR_SUP,0,""); 1330 1331 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 1332 ierr = (*(*fact)->ops->lufactornumeric)(mat,fact); CHKERRQ(ierr); 1333 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 1334 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 1335 if (flg) { 1336 ierr = OptionsHasName(0,"-mat_view_contour",&flg); CHKERRQ(ierr); 1337 if (flg) { 1338 ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr); 1339 } 1340 ierr = MatView(*fact,VIEWER_DRAW_(mat->comm)); CHKERRQ(ierr); 1341 ierr = ViewerFlush(VIEWER_DRAW_(mat->comm)); CHKERRQ(ierr); 1342 if (flg) { 1343 ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 1344 } 1345 } 1346 PetscFunctionReturn(0); 1347 } 1348 1349 #undef __FUNC__ 1350 #define __FUNC__ "MatCholeskyFactor" 1351 /*@ 1352 MatCholeskyFactor - Performs in-place Cholesky factorization of a 1353 symmetric matrix. 1354 1355 Collective on Mat 1356 1357 Input Parameters: 1358 + mat - the matrix 1359 . perm - row and column permutations 1360 - f - expected fill as ratio of original fill 1361 1362 Notes: 1363 See MatLUFactor() for the nonsymmetric case. See also 1364 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 1365 1366 Most users should employ the simplified SLES interface for linear solvers 1367 instead of working directly with matrix algebra routines such as this. 1368 See, e.g., SLESCreate(). 1369 1370 Level: developer 1371 1372 .keywords: matrix, factor, in-place, Cholesky 1373 1374 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 1375 @*/ 1376 int MatCholeskyFactor(Mat mat,IS perm,double f) 1377 { 1378 int ierr; 1379 1380 PetscFunctionBegin; 1381 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1382 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square"); 1383 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1384 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1385 if (!mat->ops->choleskyfactor) SETERRQ(PETSC_ERR_SUP,0,""); 1386 1387 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 1388 ierr = (*mat->ops->choleskyfactor)(mat,perm,f); CHKERRQ(ierr); 1389 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 1390 PetscFunctionReturn(0); 1391 } 1392 1393 #undef __FUNC__ 1394 #define __FUNC__ "MatCholeskyFactorSymbolic" 1395 /*@ 1396 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 1397 of a symmetric matrix. 1398 1399 Collective on Mat 1400 1401 Input Parameters: 1402 + mat - the matrix 1403 . perm - row and column permutations 1404 - f - expected fill as ratio of original 1405 1406 Output Parameter: 1407 . fact - the factored matrix 1408 1409 Notes: 1410 See MatLUFactorSymbolic() for the nonsymmetric case. See also 1411 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 1412 1413 Most users should employ the simplified SLES interface for linear solvers 1414 instead of working directly with matrix algebra routines such as this. 1415 See, e.g., SLESCreate(). 1416 1417 Level: developer 1418 1419 .keywords: matrix, factor, factorization, symbolic, Cholesky 1420 1421 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 1422 @*/ 1423 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 1424 { 1425 int ierr; 1426 1427 PetscFunctionBegin; 1428 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1429 PetscValidPointer(fact); 1430 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,0,"Matrix must be square"); 1431 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1432 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1433 if (!mat->ops->choleskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,""); 1434 1435 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 1436 ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr); 1437 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 1438 PetscFunctionReturn(0); 1439 } 1440 1441 #undef __FUNC__ 1442 #define __FUNC__ "MatCholeskyFactorNumeric" 1443 /*@ 1444 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 1445 of a symmetric matrix. Call this routine after first calling 1446 MatCholeskyFactorSymbolic(). 1447 1448 Collective on Mat 1449 1450 Input Parameter: 1451 . mat - the initial matrix 1452 1453 Output Parameter: 1454 . fact - the factored matrix 1455 1456 Notes: 1457 Most users should employ the simplified SLES interface for linear solvers 1458 instead of working directly with matrix algebra routines such as this. 1459 See, e.g., SLESCreate(). 1460 1461 Level: developer 1462 1463 .keywords: matrix, factor, numeric, Cholesky 1464 1465 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 1466 @*/ 1467 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 1468 { 1469 int ierr; 1470 1471 PetscFunctionBegin; 1472 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1473 PetscValidPointer(fact); 1474 if (!mat->ops->choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,0,""); 1475 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1476 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1477 SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d", 1478 mat->M,(*fact)->M,mat->N,(*fact)->N); 1479 } 1480 1481 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 1482 ierr = (*mat->ops->choleskyfactornumeric)(mat,fact); CHKERRQ(ierr); 1483 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 1484 PetscFunctionReturn(0); 1485 } 1486 1487 /* ----------------------------------------------------------------*/ 1488 #undef __FUNC__ 1489 #define __FUNC__ "MatSolve" 1490 /*@ 1491 MatSolve - Solves A x = b, given a factored matrix. 1492 1493 Collective on Mat and Vec 1494 1495 Input Parameters: 1496 + mat - the factored matrix 1497 - b - the right-hand-side vector 1498 1499 Output Parameter: 1500 . x - the result vector 1501 1502 Notes: 1503 The vectors b and x cannot be the same. I.e., one cannot 1504 call MatSolve(A,x,x). 1505 1506 Notes: 1507 Most users should employ the simplified SLES interface for linear solvers 1508 instead of working directly with matrix algebra routines such as this. 1509 See, e.g., SLESCreate(). 1510 1511 Level: developer 1512 1513 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 1514 1515 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 1516 @*/ 1517 int MatSolve(Mat mat,Vec b,Vec x) 1518 { 1519 int ierr; 1520 1521 PetscFunctionBegin; 1522 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1523 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1524 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1525 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1526 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1527 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1528 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1529 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 1530 1531 if (!mat->ops->solve) SETERRQ(PETSC_ERR_SUP,0,""); 1532 PLogEventBegin(MAT_Solve,mat,b,x,0); 1533 ierr = (*mat->ops->solve)(mat,b,x); CHKERRQ(ierr); 1534 PLogEventEnd(MAT_Solve,mat,b,x,0); 1535 PetscFunctionReturn(0); 1536 } 1537 1538 #undef __FUNC__ 1539 #define __FUNC__ "MatForwardSolve" 1540 /* @ 1541 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 1542 1543 Collective on Mat and Vec 1544 1545 Input Parameters: 1546 + mat - the factored matrix 1547 - b - the right-hand-side vector 1548 1549 Output Parameter: 1550 . x - the result vector 1551 1552 Notes: 1553 MatSolve() should be used for most applications, as it performs 1554 a forward solve followed by a backward solve. 1555 1556 The vectors b and x cannot be the same. I.e., one cannot 1557 call MatForwardSolve(A,x,x). 1558 1559 Most users should employ the simplified SLES interface for linear solvers 1560 instead of working directly with matrix algebra routines such as this. 1561 See, e.g., SLESCreate(). 1562 1563 Level: developer 1564 1565 .keywords: matrix, forward, LU, Cholesky, triangular solve 1566 1567 .seealso: MatSolve(), MatBackwardSolve() 1568 @ */ 1569 int MatForwardSolve(Mat mat,Vec b,Vec x) 1570 { 1571 int ierr; 1572 1573 PetscFunctionBegin; 1574 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1575 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1576 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1577 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1578 if (!mat->ops->forwardsolve) SETERRQ(PETSC_ERR_SUP,0,""); 1579 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1580 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1581 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1582 1583 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 1584 ierr = (*mat->ops->forwardsolve)(mat,b,x); CHKERRQ(ierr); 1585 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 1586 PetscFunctionReturn(0); 1587 } 1588 1589 #undef __FUNC__ 1590 #define __FUNC__ "MatBackwardSolve" 1591 /* @ 1592 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 1593 1594 Collective on Mat and Vec 1595 1596 Input Parameters: 1597 + mat - the factored matrix 1598 - b - the right-hand-side vector 1599 1600 Output Parameter: 1601 . x - the result vector 1602 1603 Notes: 1604 MatSolve() should be used for most applications, as it performs 1605 a forward solve followed by a backward solve. 1606 1607 The vectors b and x cannot be the same. I.e., one cannot 1608 call MatBackwardSolve(A,x,x). 1609 1610 Most users should employ the simplified SLES interface for linear solvers 1611 instead of working directly with matrix algebra routines such as this. 1612 See, e.g., SLESCreate(). 1613 1614 Level: developer 1615 1616 .keywords: matrix, backward, LU, Cholesky, triangular solve 1617 1618 .seealso: MatSolve(), MatForwardSolve() 1619 @ */ 1620 int MatBackwardSolve(Mat mat,Vec b,Vec x) 1621 { 1622 int ierr; 1623 1624 PetscFunctionBegin; 1625 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1626 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1627 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1628 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1629 if (!mat->ops->backwardsolve) SETERRQ(PETSC_ERR_SUP,0,""); 1630 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1631 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1632 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1633 1634 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 1635 ierr = (*mat->ops->backwardsolve)(mat,b,x); CHKERRQ(ierr); 1636 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 1637 PetscFunctionReturn(0); 1638 } 1639 1640 #undef __FUNC__ 1641 #define __FUNC__ "MatSolveAdd" 1642 /*@ 1643 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 1644 1645 Collective on Mat and Vec 1646 1647 Input Parameters: 1648 + mat - the factored matrix 1649 . b - the right-hand-side vector 1650 - y - the vector to be added to 1651 1652 Output Parameter: 1653 . x - the result vector 1654 1655 Notes: 1656 The vectors b and x cannot be the same. I.e., one cannot 1657 call MatSolveAdd(A,x,y,x). 1658 1659 Most users should employ the simplified SLES interface for linear solvers 1660 instead of working directly with matrix algebra routines such as this. 1661 See, e.g., SLESCreate(). 1662 1663 Level: developer 1664 1665 .keywords: matrix, linear system, solve, LU, Cholesky, add 1666 1667 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 1668 @*/ 1669 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 1670 { 1671 Scalar one = 1.0; 1672 Vec tmp; 1673 int ierr; 1674 1675 PetscFunctionBegin; 1676 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1677 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1678 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1679 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1680 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1681 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1682 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1683 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1684 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n); 1685 1686 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 1687 if (mat->ops->solveadd) { 1688 ierr = (*mat->ops->solveadd)(mat,b,y,x); CHKERRQ(ierr); 1689 } else { 1690 /* do the solve then the add manually */ 1691 if (x != y) { 1692 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 1693 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 1694 } else { 1695 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 1696 PLogObjectParent(mat,tmp); 1697 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 1698 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 1699 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 1700 ierr = VecDestroy(tmp); CHKERRQ(ierr); 1701 } 1702 } 1703 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 1704 PetscFunctionReturn(0); 1705 } 1706 1707 #undef __FUNC__ 1708 #define __FUNC__ "MatSolveTrans" 1709 /*@ 1710 MatSolveTrans - Solves A' x = b, given a factored matrix. 1711 1712 Collective on Mat and Vec 1713 1714 Input Parameters: 1715 + mat - the factored matrix 1716 - b - the right-hand-side vector 1717 1718 Output Parameter: 1719 . x - the result vector 1720 1721 Notes: 1722 The vectors b and x cannot be the same. I.e., one cannot 1723 call MatSolveTrans(A,x,x). 1724 1725 Most users should employ the simplified SLES interface for linear solvers 1726 instead of working directly with matrix algebra routines such as this. 1727 See, e.g., SLESCreate(). 1728 1729 Level: developer 1730 1731 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 1732 1733 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 1734 @*/ 1735 int MatSolveTrans(Mat mat,Vec b,Vec x) 1736 { 1737 int ierr; 1738 1739 PetscFunctionBegin; 1740 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1741 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1742 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1743 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1744 if (!mat->ops->solvetrans) SETERRQ(PETSC_ERR_SUP,0,""); 1745 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1746 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 1747 1748 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 1749 ierr = (*mat->ops->solvetrans)(mat,b,x); CHKERRQ(ierr); 1750 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 1751 PetscFunctionReturn(0); 1752 } 1753 1754 #undef __FUNC__ 1755 #define __FUNC__ "MatSolveTransAdd" 1756 /*@ 1757 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 1758 factored matrix. 1759 1760 Collective on Mat and Vec 1761 1762 Input Parameters: 1763 + mat - the factored matrix 1764 . b - the right-hand-side vector 1765 - y - the vector to be added to 1766 1767 Output Parameter: 1768 . x - the result vector 1769 1770 Notes: 1771 The vectors b and x cannot be the same. I.e., one cannot 1772 call MatSolveTransAdd(A,x,y,x). 1773 1774 Most users should employ the simplified SLES interface for linear solvers 1775 instead of working directly with matrix algebra routines such as this. 1776 See, e.g., SLESCreate(). 1777 1778 Level: developer 1779 1780 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 1781 1782 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 1783 @*/ 1784 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 1785 { 1786 Scalar one = 1.0; 1787 int ierr; 1788 Vec tmp; 1789 1790 PetscFunctionBegin; 1791 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1792 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1793 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,0,"x and b must be different vectors"); 1794 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Unfactored matrix"); 1795 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1796 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 1797 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 1798 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Vec x,Vec y: local dim %d %d",x->n,y->n); 1799 1800 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 1801 if (mat->ops->solvetransadd) { 1802 ierr = (*mat->ops->solvetransadd)(mat,b,y,x); CHKERRQ(ierr); 1803 } else { 1804 /* do the solve then the add manually */ 1805 if (x != y) { 1806 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 1807 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 1808 } else { 1809 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 1810 PLogObjectParent(mat,tmp); 1811 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 1812 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 1813 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 1814 ierr = VecDestroy(tmp); CHKERRQ(ierr); 1815 } 1816 } 1817 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 1818 PetscFunctionReturn(0); 1819 } 1820 /* ----------------------------------------------------------------*/ 1821 1822 #undef __FUNC__ 1823 #define __FUNC__ "MatRelax" 1824 /*@ 1825 MatRelax - Computes one relaxation sweep. 1826 1827 Collective on Mat and Vec 1828 1829 Input Parameters: 1830 + mat - the matrix 1831 . b - the right hand side 1832 . omega - the relaxation factor 1833 . flag - flag indicating the type of SOR (see below) 1834 . shift - diagonal shift 1835 - its - the number of iterations 1836 1837 Output Parameters: 1838 . x - the solution (can contain an initial guess) 1839 1840 SOR Flags: 1841 . SOR_FORWARD_SWEEP - forward SOR 1842 . SOR_BACKWARD_SWEEP - backward SOR 1843 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 1844 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 1845 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 1846 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 1847 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 1848 upper/lower triangular part of matrix to 1849 vector (with omega) 1850 . SOR_ZERO_INITIAL_GUESS - zero initial guess 1851 1852 Notes: 1853 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 1854 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 1855 on each processor. 1856 1857 Application programmers will not generally use MatRelax() directly, 1858 but instead will employ the SLES/PC interface. 1859 1860 Notes for Advanced Users: 1861 The flags are implemented as bitwise inclusive or operations. 1862 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 1863 to specify a zero initial guess for SSOR. 1864 1865 Most users should employ the simplified SLES interface for linear solvers 1866 instead of working directly with matrix algebra routines such as this. 1867 See, e.g., SLESCreate(). 1868 1869 Level: developer 1870 1871 .keywords: matrix, relax, relaxation, sweep 1872 @*/ 1873 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift, 1874 int its,Vec x) 1875 { 1876 int ierr; 1877 1878 PetscFunctionBegin; 1879 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1880 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 1881 if (!mat->ops->relax) SETERRQ(PETSC_ERR_SUP,0,""); 1882 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1883 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1884 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1885 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1886 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,0,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1887 1888 PLogEventBegin(MAT_Relax,mat,b,x,0); 1889 ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr); 1890 PLogEventEnd(MAT_Relax,mat,b,x,0); 1891 PetscFunctionReturn(0); 1892 } 1893 1894 #undef __FUNC__ 1895 #define __FUNC__ "MatCopy_Basic" 1896 /* 1897 Default matrix copy routine. 1898 */ 1899 int MatCopy_Basic(Mat A,Mat B,MatStructure str) 1900 { 1901 int ierr,i,rstart,rend,nz,*cwork; 1902 Scalar *vwork; 1903 1904 PetscFunctionBegin; 1905 ierr = MatZeroEntries(B); CHKERRQ(ierr); 1906 ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr); 1907 for (i=rstart; i<rend; i++) { 1908 ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1909 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 1910 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 1911 } 1912 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1913 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 1914 PetscFunctionReturn(0); 1915 } 1916 1917 #undef __FUNC__ 1918 #define __FUNC__ "MatCopy" 1919 /*@C 1920 MatCopy - Copys a matrix to another matrix. 1921 1922 Collective on Mat 1923 1924 Input Parameters: 1925 + A - the matrix 1926 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 1927 1928 Output Parameter: 1929 . B - where the copy is put 1930 1931 Notes: 1932 If you use SAME_NONZERO_PATTERN then the zero matrices had better have the 1933 same nonzero pattern or the routine will crash. 1934 1935 MatCopy() copies the matrix entries of a matrix to another existing 1936 matrix (after first zeroing the second matrix). A related routine is 1937 MatConvert(), which first creates a new matrix and then copies the data. 1938 1939 Level: intermediate 1940 1941 .keywords: matrix, copy, convert 1942 1943 .seealso: MatConvert() 1944 @*/ 1945 int MatCopy(Mat A,Mat B,MatStructure str) 1946 { 1947 int ierr; 1948 1949 PetscFunctionBegin; 1950 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 1951 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 1952 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 1953 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d",A->M,B->M, 1954 A->N,B->N); 1955 1956 PLogEventBegin(MAT_Copy,A,B,0,0); 1957 if (A->ops->copy) { 1958 ierr = (*A->ops->copy)(A,B,str); CHKERRQ(ierr); 1959 } else { /* generic conversion */ 1960 ierr = MatCopy_Basic(A,B,str); CHKERRQ(ierr); 1961 } 1962 PLogEventEnd(MAT_Copy,A,B,0,0); 1963 PetscFunctionReturn(0); 1964 } 1965 1966 static int MatConvertersSet = 0; 1967 static int (*MatConverters[MAX_MATRIX_TYPES][MAX_MATRIX_TYPES])(Mat,MatType,Mat*) = 1968 {{0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 1969 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 1970 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 1971 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 1972 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}, 1973 {0,0,0,0,0,0,0,0,0,0,0,0},{0,0,0,0,0,0,0,0,0,0,0,0}}; 1974 1975 #undef __FUNC__ 1976 #define __FUNC__ "MatConvertRegister" 1977 /*@C 1978 MatConvertRegister - Allows one to register a routine that converts between 1979 two matrix types. 1980 1981 Not Collective 1982 1983 Input Parameters: 1984 + intype - the type of matrix (defined in include/mat.h), for example, MATSEQAIJ. 1985 - outtype - new matrix type, or MATSAME 1986 1987 Level: advanced 1988 1989 .seealso: MatConvertRegisterAll() 1990 @*/ 1991 int MatConvertRegister(MatType intype,MatType outtype,int (*converter)(Mat,MatType,Mat*)) 1992 { 1993 PetscFunctionBegin; 1994 MatConverters[intype][outtype] = converter; 1995 MatConvertersSet = 1; 1996 PetscFunctionReturn(0); 1997 } 1998 1999 #undef __FUNC__ 2000 #define __FUNC__ "MatConvert" 2001 /*@C 2002 MatConvert - Converts a matrix to another matrix, either of the same 2003 or different type. 2004 2005 Collective on Mat 2006 2007 Input Parameters: 2008 + mat - the matrix 2009 - newtype - new matrix type. Use MATSAME to create a new matrix of the 2010 same type as the original matrix. 2011 2012 Output Parameter: 2013 . M - pointer to place new matrix 2014 2015 Notes: 2016 MatConvert() first creates a new matrix and then copies the data from 2017 the first matrix. A related routine is MatCopy(), which copies the matrix 2018 entries of one matrix to another already existing matrix context. 2019 2020 Level: intermediate 2021 2022 .keywords: matrix, copy, convert 2023 2024 .seealso: MatCopy(), MatDuplicate() 2025 @*/ 2026 int MatConvert(Mat mat,MatType newtype,Mat *M) 2027 { 2028 int ierr; 2029 2030 PetscFunctionBegin; 2031 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2032 PetscValidPointer(M); 2033 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2034 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2035 2036 if (newtype > MAX_MATRIX_TYPES || newtype < -1) { 2037 SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,1,"Not a valid matrix type"); 2038 } 2039 *M = 0; 2040 2041 if (!MatConvertersSet) { 2042 ierr = MatLoadRegisterAll(); CHKERRQ(ierr); 2043 } 2044 2045 PLogEventBegin(MAT_Convert,mat,0,0,0); 2046 if ((newtype == mat->type || newtype == MATSAME) && mat->ops->duplicate) { 2047 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M); CHKERRQ(ierr); 2048 } else { 2049 if (!MatConvertersSet) { 2050 ierr = MatConvertRegisterAll(); CHKERRQ(ierr); 2051 } 2052 if (!MatConverters[mat->type][newtype]) { 2053 SETERRQ(PETSC_ERR_ARG_WRONG,1,"Invalid matrix type, or matrix converter not registered"); 2054 } 2055 ierr = (*MatConverters[mat->type][newtype])(mat,newtype,M); CHKERRQ(ierr); 2056 } 2057 PLogEventEnd(MAT_Convert,mat,0,0,0); 2058 PetscFunctionReturn(0); 2059 } 2060 2061 #undef __FUNC__ 2062 #define __FUNC__ "MatDuplicate" 2063 /*@C 2064 MatDuplicate - Duplicates a matrix including the non-zero structure. 2065 2066 Collective on Mat 2067 2068 Input Parameters: 2069 + mat - the matrix 2070 - op - either MAT_DO_NO_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero 2071 values as well or not 2072 2073 Output Parameter: 2074 . M - pointer to place new matrix 2075 2076 Level: intermediate 2077 2078 .keywords: matrix, copy, convert, duplicate 2079 2080 .seealso: MatCopy(), MatConvert() 2081 @*/ 2082 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 2083 { 2084 int ierr; 2085 2086 PetscFunctionBegin; 2087 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2088 PetscValidPointer(M); 2089 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2090 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2091 2092 *M = 0; 2093 PLogEventBegin(MAT_Convert,mat,0,0,0); 2094 if (!mat->ops->duplicate) { 2095 SETERRQ(PETSC_ERR_SUP,1,"Not written for this matrix type"); 2096 } 2097 ierr = (*mat->ops->duplicate)(mat,op,M); CHKERRQ(ierr); 2098 PLogEventEnd(MAT_Convert,mat,0,0,0); 2099 PetscFunctionReturn(0); 2100 } 2101 2102 #undef __FUNC__ 2103 #define __FUNC__ "MatGetDiagonal" 2104 /*@ 2105 MatGetDiagonal - Gets the diagonal of a matrix. 2106 2107 Collective on Mat and Vec 2108 2109 Input Parameters: 2110 + mat - the matrix 2111 - v - the vector for storing the diagonal 2112 2113 Output Parameter: 2114 . v - the diagonal of the matrix 2115 2116 Notes: 2117 For the SeqAIJ matrix format, this routine may also be called 2118 on a LU factored matrix; in that case it routines the reciprocal of 2119 the diagonal entries in U. It returns the entries permuted by the 2120 row and column permutation used during the symbolic factorization. 2121 2122 Level: intermediate 2123 2124 .keywords: matrix, get, diagonal 2125 2126 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix() 2127 @*/ 2128 int MatGetDiagonal(Mat mat,Vec v) 2129 { 2130 int ierr; 2131 2132 PetscFunctionBegin; 2133 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v,VEC_COOKIE); 2134 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2135 if (!mat->ops->getdiagonal) SETERRQ(PETSC_ERR_SUP,0,""); 2136 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 2137 PetscFunctionReturn(0); 2138 } 2139 2140 #undef __FUNC__ 2141 #define __FUNC__ "MatTranspose" 2142 /*@C 2143 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 2144 2145 Collective on Mat 2146 2147 Input Parameter: 2148 . mat - the matrix to transpose 2149 2150 Output Parameters: 2151 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 2152 2153 Level: intermediate 2154 2155 .keywords: matrix, transpose 2156 2157 .seealso: MatMultTrans(), MatMultTransAdd() 2158 @*/ 2159 int MatTranspose(Mat mat,Mat *B) 2160 { 2161 int ierr; 2162 2163 PetscFunctionBegin; 2164 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2165 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2166 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2167 if (!mat->ops->transpose) SETERRQ(PETSC_ERR_SUP,0,""); 2168 ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr); 2169 PetscFunctionReturn(0); 2170 } 2171 2172 #undef __FUNC__ 2173 #define __FUNC__ "MatPermute" 2174 /*@C 2175 MatPermute - Creates a new matrix with rows and columns permuted from the 2176 original. 2177 2178 Collective on Mat 2179 2180 Input Parameters: 2181 + mat - the matrix to permute 2182 . row - row permutation 2183 - col - column permutation 2184 2185 Output Parameters: 2186 . B - the permuted matrix 2187 2188 Level: advanced 2189 2190 .keywords: matrix, transpose 2191 2192 .seealso: MatGetReordering() 2193 @*/ 2194 int MatPermute(Mat mat,IS row,IS col,Mat *B) 2195 { 2196 int ierr; 2197 2198 PetscFunctionBegin; 2199 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2200 PetscValidHeaderSpecific(row,IS_COOKIE); 2201 PetscValidHeaderSpecific(col,IS_COOKIE); 2202 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2203 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2204 if (!mat->ops->permute) SETERRQ(PETSC_ERR_SUP,0,""); 2205 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 2206 PetscFunctionReturn(0); 2207 } 2208 2209 #undef __FUNC__ 2210 #define __FUNC__ "MatEqual" 2211 /*@ 2212 MatEqual - Compares two matrices. 2213 2214 Collective on Mat 2215 2216 Input Parameters: 2217 + A - the first matrix 2218 - B - the second matrix 2219 2220 Output Parameter: 2221 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 2222 2223 Level: intermediate 2224 2225 .keywords: matrix, equal, equivalent 2226 @*/ 2227 int MatEqual(Mat A,Mat B,PetscTruth *flg) 2228 { 2229 int ierr; 2230 2231 PetscFunctionBegin; 2232 PetscValidHeaderSpecific(A,MAT_COOKIE); PetscValidHeaderSpecific(B,MAT_COOKIE); 2233 PetscValidIntPointer(flg); 2234 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2235 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2236 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,0,"Mat A,Mat B: global dim %d %d %d %d", 2237 A->M,B->M,A->N,B->N); 2238 if (!A->ops->equal) SETERRQ(PETSC_ERR_SUP,0,""); 2239 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 2240 PetscFunctionReturn(0); 2241 } 2242 2243 #undef __FUNC__ 2244 #define __FUNC__ "MatDiagonalScale" 2245 /*@ 2246 MatDiagonalScale - Scales a matrix on the left and right by diagonal 2247 matrices that are stored as vectors. Either of the two scaling 2248 matrices can be PETSC_NULL. 2249 2250 Collective on Mat 2251 2252 Input Parameters: 2253 + mat - the matrix to be scaled 2254 . l - the left scaling vector (or PETSC_NULL) 2255 - r - the right scaling vector (or PETSC_NULL) 2256 2257 Notes: 2258 MatDiagonalScale() computes A = LAR, where 2259 L = a diagonal matrix, R = a diagonal matrix 2260 2261 Level: intermediate 2262 2263 .keywords: matrix, diagonal, scale 2264 2265 .seealso: MatScale() 2266 @*/ 2267 int MatDiagonalScale(Mat mat,Vec l,Vec r) 2268 { 2269 int ierr; 2270 2271 PetscFunctionBegin; 2272 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2273 if (!mat->ops->diagonalscale) SETERRQ(PETSC_ERR_SUP,0,""); 2274 if (l) PetscValidHeaderSpecific(l,VEC_COOKIE); 2275 if (r) PetscValidHeaderSpecific(r,VEC_COOKIE); 2276 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2277 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2278 2279 PLogEventBegin(MAT_Scale,mat,0,0,0); 2280 ierr = (*mat->ops->diagonalscale)(mat,l,r); CHKERRQ(ierr); 2281 PLogEventEnd(MAT_Scale,mat,0,0,0); 2282 PetscFunctionReturn(0); 2283 } 2284 2285 #undef __FUNC__ 2286 #define __FUNC__ "MatScale" 2287 /*@ 2288 MatScale - Scales all elements of a matrix by a given number. 2289 2290 Collective on Mat 2291 2292 Input Parameters: 2293 + mat - the matrix to be scaled 2294 - a - the scaling value 2295 2296 Output Parameter: 2297 . mat - the scaled matrix 2298 2299 Level: intermediate 2300 2301 .keywords: matrix, scale 2302 2303 .seealso: MatDiagonalScale() 2304 @*/ 2305 int MatScale(Scalar *a,Mat mat) 2306 { 2307 int ierr; 2308 2309 PetscFunctionBegin; 2310 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2311 PetscValidScalarPointer(a); 2312 if (!mat->ops->scale) SETERRQ(PETSC_ERR_SUP,0,""); 2313 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2314 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2315 2316 PLogEventBegin(MAT_Scale,mat,0,0,0); 2317 ierr = (*mat->ops->scale)(a,mat); CHKERRQ(ierr); 2318 PLogEventEnd(MAT_Scale,mat,0,0,0); 2319 PetscFunctionReturn(0); 2320 } 2321 2322 #undef __FUNC__ 2323 #define __FUNC__ "MatNorm" 2324 /*@ 2325 MatNorm - Calculates various norms of a matrix. 2326 2327 Collective on Mat 2328 2329 Input Parameters: 2330 + mat - the matrix 2331 - type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 2332 2333 Output Parameters: 2334 . norm - the resulting norm 2335 2336 Level: intermediate 2337 2338 .keywords: matrix, norm, Frobenius 2339 @*/ 2340 int MatNorm(Mat mat,NormType type,double *norm) 2341 { 2342 int ierr; 2343 2344 PetscFunctionBegin; 2345 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2346 PetscValidScalarPointer(norm); 2347 2348 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2349 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2350 if (!mat->ops->norm) SETERRQ(PETSC_ERR_SUP,0,"Not for this matrix type"); 2351 ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr); 2352 PetscFunctionReturn(0); 2353 } 2354 2355 /* 2356 This variable is used to prevent counting of MatAssemblyBegin() that 2357 are called from within a MatAssemblyEnd(). 2358 */ 2359 static int MatAssemblyEnd_InUse = 0; 2360 #undef __FUNC__ 2361 #define __FUNC__ "MatAssemblyBegin" 2362 /*@ 2363 MatAssemblyBegin - Begins assembling the matrix. This routine should 2364 be called after completing all calls to MatSetValues(). 2365 2366 Collective on Mat 2367 2368 Input Parameters: 2369 + mat - the matrix 2370 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 2371 2372 Notes: 2373 MatSetValues() generally caches the values. The matrix is ready to 2374 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 2375 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 2376 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 2377 using the matrix. 2378 2379 Level: beginner 2380 2381 .keywords: matrix, assembly, assemble, begin 2382 2383 .seealso: MatAssemblyEnd(), MatSetValues() 2384 @*/ 2385 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 2386 { 2387 int ierr; 2388 2389 PetscFunctionBegin; 2390 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2391 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix.\n did you forget to call MatSetUnfactored()?"); 2392 if (mat->assembled) { 2393 mat->was_assembled = PETSC_TRUE; 2394 mat->assembled = PETSC_FALSE; 2395 } 2396 if (!MatAssemblyEnd_InUse) { 2397 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 2398 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 2399 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 2400 } else { 2401 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 2402 } 2403 PetscFunctionReturn(0); 2404 } 2405 2406 2407 #undef __FUNC__ 2408 #define __FUNC__ "MatView_Private" 2409 /* 2410 Processes command line options to determine if/how a matrix 2411 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 2412 */ 2413 int MatView_Private(Mat mat) 2414 { 2415 int ierr,flg; 2416 2417 PetscFunctionBegin; 2418 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr); 2419 if (flg) { 2420 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO,0);CHKERRQ(ierr); 2421 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm)); CHKERRQ(ierr); 2422 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2423 } 2424 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 2425 if (flg) { 2426 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_INFO_LONG,0);CHKERRQ(ierr); 2427 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm)); CHKERRQ(ierr); 2428 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2429 } 2430 ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg); CHKERRQ(ierr); 2431 if (flg) { 2432 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm)); CHKERRQ(ierr); 2433 } 2434 ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg); CHKERRQ(ierr); 2435 if (flg) { 2436 ierr = ViewerPushFormat(VIEWER_STDOUT_(mat->comm),VIEWER_FORMAT_ASCII_MATLAB,"M");CHKERRQ(ierr); 2437 ierr = MatView(mat,VIEWER_STDOUT_(mat->comm)); CHKERRQ(ierr); 2438 ierr = ViewerPopFormat(VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 2439 } 2440 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 2441 if (flg) { 2442 ierr = OptionsHasName(0,"-mat_view_contour",&flg); CHKERRQ(ierr); 2443 if (flg) { 2444 ViewerPushFormat(VIEWER_DRAW_(mat->comm),VIEWER_FORMAT_DRAW_CONTOUR,0);CHKERRQ(ierr); 2445 } 2446 ierr = MatView(mat,VIEWER_DRAW_(mat->comm)); CHKERRQ(ierr); 2447 ierr = ViewerFlush(VIEWER_DRAW_(mat->comm)); CHKERRQ(ierr); 2448 if (flg) { 2449 ViewerPopFormat(VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 2450 } 2451 } 2452 ierr = OptionsHasName(PETSC_NULL,"-mat_view_socket",&flg); CHKERRQ(ierr); 2453 if (flg) { 2454 ierr = MatView(mat,VIEWER_SOCKET_(mat->comm)); CHKERRQ(ierr); 2455 ierr = ViewerFlush(VIEWER_SOCKET_(mat->comm)); CHKERRQ(ierr); 2456 } 2457 PetscFunctionReturn(0); 2458 } 2459 2460 #undef __FUNC__ 2461 #define __FUNC__ "MatAssemblyEnd" 2462 /*@ 2463 MatAssemblyEnd - Completes assembling the matrix. This routine should 2464 be called after MatAssemblyBegin(). 2465 2466 Collective on Mat 2467 2468 Input Parameters: 2469 + mat - the matrix 2470 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 2471 2472 Options Database Keys: 2473 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 2474 . -mat_view_info_detailed - Prints more detailed info 2475 . -mat_view - Prints matrix in ASCII format 2476 . -mat_view_matlab - Prints matrix in Matlab format 2477 . -mat_view_draw - Draws nonzero structure of matrix, using MatView() and DrawOpenX(). 2478 . -display <name> - Sets display name (default is host) 2479 - -draw_pause <sec> - Sets number of seconds to pause after display 2480 2481 Notes: 2482 MatSetValues() generally caches the values. The matrix is ready to 2483 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 2484 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 2485 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 2486 using the matrix. 2487 2488 Level: beginner 2489 2490 .keywords: matrix, assembly, assemble, end 2491 2492 .seealso: MatAssemblyBegin(), MatSetValues(), DrawOpenX(), MatView() 2493 @*/ 2494 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 2495 { 2496 int ierr; 2497 static int inassm = 0; 2498 2499 PetscFunctionBegin; 2500 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2501 2502 inassm++; 2503 MatAssemblyEnd_InUse++; 2504 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 2505 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 2506 if (mat->ops->assemblyend) { 2507 ierr = (*mat->ops->assemblyend)(mat,type); CHKERRQ(ierr); 2508 } 2509 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 2510 } else { 2511 if (mat->ops->assemblyend) { 2512 ierr = (*mat->ops->assemblyend)(mat,type); CHKERRQ(ierr); 2513 } 2514 } 2515 2516 /* Flush assembly is not a true assembly */ 2517 if (type != MAT_FLUSH_ASSEMBLY) { 2518 mat->assembled = PETSC_TRUE; mat->num_ass++; 2519 } 2520 mat->insertmode = NOT_SET_VALUES; 2521 MatAssemblyEnd_InUse--; 2522 2523 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 2524 ierr = MatView_Private(mat); CHKERRQ(ierr); 2525 } 2526 inassm--; 2527 PetscFunctionReturn(0); 2528 } 2529 2530 2531 #undef __FUNC__ 2532 #define __FUNC__ "MatCompress" 2533 /*@ 2534 MatCompress - Tries to store the matrix in as little space as 2535 possible. May fail if memory is already fully used, since it 2536 tries to allocate new space. 2537 2538 Collective on Mat 2539 2540 Input Parameters: 2541 . mat - the matrix 2542 2543 Level: advanced 2544 2545 .keywords: matrix, compress 2546 @*/ 2547 int MatCompress(Mat mat) 2548 { 2549 int ierr; 2550 2551 PetscFunctionBegin; 2552 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2553 if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);} 2554 PetscFunctionReturn(0); 2555 } 2556 2557 #undef __FUNC__ 2558 #define __FUNC__ "MatSetOption" 2559 /*@ 2560 MatSetOption - Sets a parameter option for a matrix. Some options 2561 may be specific to certain storage formats. Some options 2562 determine how values will be inserted (or added). Sorted, 2563 row-oriented input will generally assemble the fastest. The default 2564 is row-oriented, nonsorted input. 2565 2566 Collective on Mat 2567 2568 Input Parameters: 2569 + mat - the matrix 2570 - option - the option, one of those listed below (and possibly others), 2571 e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR 2572 2573 Options Describing Matrix Structure: 2574 + MAT_SYMMETRIC - symmetric in terms of both structure and value 2575 - MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 2576 2577 Options For Use with MatSetValues(): 2578 Insert a logically dense subblock, which can be 2579 + MAT_ROW_ORIENTED - row-oriented 2580 . MAT_COLUMN_ORIENTED - column-oriented 2581 . MAT_ROWS_SORTED - sorted by row 2582 . MAT_ROWS_UNSORTED - not sorted by row 2583 . MAT_COLUMNS_SORTED - sorted by column 2584 - MAT_COLUMNS_UNSORTED - not sorted by column 2585 2586 Not these options reflect the data you pass in with MatSetValues(); it has 2587 nothing to do with how the data is stored internally in the matrix 2588 data structure. 2589 2590 When (re)assembling a matrix, we can restrict the input for 2591 efficiency/debugging purposes. These options include 2592 + MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be 2593 allowed if they generate a new nonzero 2594 . MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed 2595 . MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if 2596 they generate a nonzero in a new diagonal (for block diagonal format only) 2597 . MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 2598 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 2599 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 2600 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 2601 2602 Notes: 2603 Some options are relevant only for particular matrix types and 2604 are thus ignored by others. Other options are not supported by 2605 certain matrix types and will generate an error message if set. 2606 2607 If using a Fortran 77 module to compute a matrix, one may need to 2608 use the column-oriented option (or convert to the row-oriented 2609 format). 2610 2611 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 2612 that would generate a new entry in the nonzero structure is instead 2613 ignored. Thus, if memory has not alredy been allocated for this particular 2614 data, then the insertion is ignored. For dense matrices, in which 2615 the entire array is allocated, no entries are ever ignored. 2616 2617 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 2618 that would generate a new entry in the nonzero structure instead produces 2619 an error. (Currently supported for AIJ and BAIJ formats only.) 2620 This is a useful flag when using SAME_NONZERO_PATTERN in calling 2621 SLESSetOperators() to ensure that the nonzero pattern truely does 2622 remain unchanged. 2623 2624 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 2625 that would generate a new entry that has not been preallocated will 2626 instead produce an error. (Currently supported for AIJ and BAIJ formats 2627 only.) This is a useful flag when debugging matrix memory preallocation. 2628 2629 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 2630 other processors should be dropped, rather than stashed. 2631 This is useful if you know that the "owning" processor is also 2632 always generating the correct matrix entries, so that PETSc need 2633 not transfer duplicate entries generated on another processor. 2634 2635 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 2636 searches during matrix assembly. When this flag is set, the hash table 2637 is created during the first Matrix Assembly. This hash table is 2638 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 2639 to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag 2640 should be used with MAT_USE_HASH_TABLE flag. This option is currently 2641 supported by MATMPIBAIJ format only. 2642 2643 Level: intermediate 2644 2645 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 2646 @*/ 2647 int MatSetOption(Mat mat,MatOption op) 2648 { 2649 int ierr; 2650 2651 PetscFunctionBegin; 2652 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2653 if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);} 2654 PetscFunctionReturn(0); 2655 } 2656 2657 #undef __FUNC__ 2658 #define __FUNC__ "MatZeroEntries" 2659 /*@ 2660 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 2661 this routine retains the old nonzero structure. 2662 2663 Collective on Mat 2664 2665 Input Parameters: 2666 . mat - the matrix 2667 2668 Level: intermediate 2669 2670 .keywords: matrix, zero, entries 2671 2672 .seealso: MatZeroRows() 2673 @*/ 2674 int MatZeroEntries(Mat mat) 2675 { 2676 int ierr; 2677 2678 PetscFunctionBegin; 2679 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2680 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2681 if (!mat->ops->zeroentries) SETERRQ(PETSC_ERR_SUP,0,""); 2682 2683 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 2684 ierr = (*mat->ops->zeroentries)(mat); CHKERRQ(ierr); 2685 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 2686 PetscFunctionReturn(0); 2687 } 2688 2689 #undef __FUNC__ 2690 #define __FUNC__ "MatZeroRows" 2691 /*@ 2692 MatZeroRows - Zeros all entries (except possibly the main diagonal) 2693 of a set of rows of a matrix. 2694 2695 Collective on Mat 2696 2697 Input Parameters: 2698 + mat - the matrix 2699 . is - index set of rows to remove 2700 - diag - pointer to value put in all diagonals of eliminated rows. 2701 Note that diag is not a pointer to an array, but merely a 2702 pointer to a single value. 2703 2704 Notes: 2705 For the AIJ matrix formats this removes the old nonzero structure, 2706 but does not release memory. For the dense and block diagonal 2707 formats this does not alter the nonzero structure. 2708 2709 The user can set a value in the diagonal entry (or for the AIJ and 2710 row formats can optionally remove the main diagonal entry from the 2711 nonzero structure as well, by passing a null pointer as the final 2712 argument). 2713 2714 For the parallel case, all processes that share the matrix (i.e., 2715 those in the communicator used for matrix creation) MUST call this 2716 routine, regardless of whether any rows being zeroed are owned by 2717 them. 2718 2719 Level: intermediate 2720 2721 .keywords: matrix, zero, rows, boundary conditions 2722 2723 .seealso: MatZeroEntries(), 2724 @*/ 2725 int MatZeroRows(Mat mat,IS is, Scalar *diag) 2726 { 2727 int ierr; 2728 2729 PetscFunctionBegin; 2730 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2731 PetscValidHeaderSpecific(is,IS_COOKIE); 2732 if (diag) PetscValidScalarPointer(diag); 2733 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2734 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2735 if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,""); 2736 2737 ierr = (*mat->ops->zerorows)(mat,is,diag); CHKERRQ(ierr); 2738 ierr = MatView_Private(mat); CHKERRQ(ierr); 2739 PetscFunctionReturn(0); 2740 } 2741 2742 #undef __FUNC__ 2743 #define __FUNC__ "MatZeroRowsLocal" 2744 /*@ 2745 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 2746 of a set of rows of a matrix; using local numbering of rows. 2747 2748 Collective on Mat 2749 2750 Input Parameters: 2751 + mat - the matrix 2752 . is - index set of rows to remove 2753 - diag - pointer to value put in all diagonals of eliminated rows. 2754 Note that diag is not a pointer to an array, but merely a 2755 pointer to a single value. 2756 2757 Notes: 2758 For the AIJ matrix formats this removes the old nonzero structure, 2759 but does not release memory. For the dense and block diagonal 2760 formats this does not alter the nonzero structure. 2761 2762 The user can set a value in the diagonal entry (or for the AIJ and 2763 row formats can optionally remove the main diagonal entry from the 2764 nonzero structure as well, by passing a null pointer as the final 2765 argument). 2766 2767 Level: intermediate 2768 2769 .keywords: matrix, zero, rows, boundary conditions 2770 2771 .seealso: MatZeroEntries(), 2772 @*/ 2773 int MatZeroRowsLocal(Mat mat,IS is, Scalar *diag) 2774 { 2775 int ierr; 2776 IS newis; 2777 2778 PetscFunctionBegin; 2779 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2780 PetscValidHeaderSpecific(is,IS_COOKIE); 2781 if (diag) PetscValidScalarPointer(diag); 2782 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2783 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2784 if (!mat->ops->zerorows) SETERRQ(PETSC_ERR_SUP,0,""); 2785 2786 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis); CHKERRQ(ierr); 2787 ierr = (*mat->ops->zerorows)(mat,newis,diag); CHKERRQ(ierr); 2788 ierr = ISDestroy(newis); 2789 PetscFunctionReturn(0); 2790 } 2791 2792 #undef __FUNC__ 2793 #define __FUNC__ "MatGetSize" 2794 /*@ 2795 MatGetSize - Returns the numbers of rows and columns in a matrix. 2796 2797 Not Collective 2798 2799 Input Parameter: 2800 . mat - the matrix 2801 2802 Output Parameters: 2803 + m - the number of global rows 2804 - n - the number of global columns 2805 2806 Level: beginner 2807 2808 .keywords: matrix, dimension, size, rows, columns, global, get 2809 2810 .seealso: MatGetLocalSize() 2811 @*/ 2812 int MatGetSize(Mat mat,int *m,int* n) 2813 { 2814 int ierr; 2815 2816 PetscFunctionBegin; 2817 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2818 ierr = (*mat->ops->getsize)(mat,m,n);CHKERRQ(ierr); 2819 PetscFunctionReturn(0); 2820 } 2821 2822 #undef __FUNC__ 2823 #define __FUNC__ "MatGetLocalSize" 2824 /*@ 2825 MatGetLocalSize - Returns the number of rows and columns in a matrix 2826 stored locally. This information may be implementation dependent, so 2827 use with care. 2828 2829 Not Collective 2830 2831 Input Parameters: 2832 . mat - the matrix 2833 2834 Output Parameters: 2835 + m - the number of local rows 2836 - n - the number of local columns 2837 2838 Level: beginner 2839 2840 .keywords: matrix, dimension, size, local, rows, columns, get 2841 2842 .seealso: MatGetSize() 2843 @*/ 2844 int MatGetLocalSize(Mat mat,int *m,int* n) 2845 { 2846 int ierr; 2847 2848 PetscFunctionBegin; 2849 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2850 ierr = (*mat->ops->getlocalsize)(mat,m,n);CHKERRQ(ierr); 2851 PetscFunctionReturn(0); 2852 } 2853 2854 #undef __FUNC__ 2855 #define __FUNC__ "MatGetOwnershipRange" 2856 /*@ 2857 MatGetOwnershipRange - Returns the range of matrix rows owned by 2858 this processor, assuming that the matrix is laid out with the first 2859 n1 rows on the first processor, the next n2 rows on the second, etc. 2860 For certain parallel layouts this range may not be well defined. 2861 2862 Not Collective 2863 2864 Input Parameters: 2865 . mat - the matrix 2866 2867 Output Parameters: 2868 + m - the global index of the first local row 2869 - n - one more than the global index of the last local row 2870 2871 Level: beginner 2872 2873 .keywords: matrix, get, range, ownership 2874 @*/ 2875 int MatGetOwnershipRange(Mat mat,int *m,int* n) 2876 { 2877 int ierr; 2878 2879 PetscFunctionBegin; 2880 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2881 PetscValidIntPointer(m); 2882 PetscValidIntPointer(n); 2883 if (!mat->ops->getownershiprange) SETERRQ(PETSC_ERR_SUP,0,""); 2884 ierr = (*mat->ops->getownershiprange)(mat,m,n);CHKERRQ(ierr); 2885 PetscFunctionReturn(0); 2886 } 2887 2888 #undef __FUNC__ 2889 #define __FUNC__ "MatILUFactorSymbolic" 2890 /*@ 2891 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 2892 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 2893 to complete the factorization. 2894 2895 Collective on Mat 2896 2897 Input Parameters: 2898 + mat - the matrix 2899 . row - row permutation 2900 . column - column permutation 2901 - info - structure containing 2902 $ levels - number of levels of fill. 2903 $ expected fill - as ratio of original fill. 2904 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 2905 missing diagonal entries) 2906 2907 Output Parameters: 2908 . fact - new matrix that has been symbolically factored 2909 2910 Notes: 2911 See the file ${PETSC_DIR}/Performace for additional information about 2912 choosing the fill factor for better efficiency. 2913 2914 Most users should employ the simplified SLES interface for linear solvers 2915 instead of working directly with matrix algebra routines such as this. 2916 See, e.g., SLESCreate(). 2917 2918 Level: developer 2919 2920 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 2921 2922 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric() 2923 @*/ 2924 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact) 2925 { 2926 int ierr; 2927 2928 PetscFunctionBegin; 2929 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2930 PetscValidPointer(fact); 2931 if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Levels of fill negative %d",info->levels); 2932 if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",info->fill); 2933 if (!mat->ops->ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Only MatCreateMPIRowbs() matrices support parallel ILU"); 2934 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2935 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2936 2937 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 2938 ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact); CHKERRQ(ierr); 2939 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 2940 PetscFunctionReturn(0); 2941 } 2942 2943 #undef __FUNC__ 2944 #define __FUNC__ "MatIncompleteCholeskyFactorSymbolic" 2945 /*@ 2946 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 2947 Cholesky factorization for a symmetric matrix. Use 2948 MatCholeskyFactorNumeric() to complete the factorization. 2949 2950 Collective on Mat 2951 2952 Input Parameters: 2953 + mat - the matrix 2954 . perm - row and column permutation 2955 . fill - levels of fill 2956 - f - expected fill as ratio of original fill 2957 2958 Output Parameter: 2959 . fact - the factored matrix 2960 2961 Notes: 2962 Currently only no-fill factorization is supported. 2963 2964 Most users should employ the simplified SLES interface for linear solvers 2965 instead of working directly with matrix algebra routines such as this. 2966 See, e.g., SLESCreate(). 2967 2968 Level: developer 2969 2970 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 2971 2972 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 2973 @*/ 2974 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill,Mat *fact) 2975 { 2976 int ierr; 2977 2978 PetscFunctionBegin; 2979 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2980 PetscValidPointer(fact); 2981 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 2982 if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Fill negative %d",fill); 2983 if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Expected fill less than 1.0 %g",f); 2984 if (!mat->ops->incompletecholeskyfactorsymbolic) SETERRQ(PETSC_ERR_SUP,0,"Currently only MatCreateMPIRowbs() matrices support ICC in parallel"); 2985 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 2986 2987 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 2988 ierr = (*mat->ops->incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 2989 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 2990 PetscFunctionReturn(0); 2991 } 2992 2993 #undef __FUNC__ 2994 #define __FUNC__ "MatGetArray" 2995 /*@C 2996 MatGetArray - Returns a pointer to the element values in the matrix. 2997 The result of this routine is dependent on the underlying matrix data 2998 structure, and may not even work for certain matrix types. You MUST 2999 call MatRestoreArray() when you no longer need to access the array. 3000 3001 Not Collective 3002 3003 Input Parameter: 3004 . mat - the matrix 3005 3006 Output Parameter: 3007 . v - the location of the values 3008 3009 Currently returns an array only for the dense formats, giving access to 3010 the local portion of the matrix in the usual Fortran column-oriented format. 3011 3012 Fortran Note: 3013 This routine is used differently from Fortran, e.g., 3014 .vb 3015 Mat mat 3016 Scalar mat_array(1) 3017 PetscOffset i_mat 3018 int ierr 3019 call MatGetArray(mat,mat_array,i_mat,ierr) 3020 3021 C Access first local entry in matrix; note that array is 3022 C treated as one dimensional 3023 value = mat_array(i_mat + 1) 3024 3025 [... other code ...] 3026 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3027 .ve 3028 3029 See the Fortran chapter of the users manual and 3030 petsc/src/mat/examples/tests for details. 3031 3032 Level: advanced 3033 3034 .keywords: matrix, array, elements, values 3035 3036 .seealso: MatRestoreArray(), MatGetArrayF90() 3037 @*/ 3038 int MatGetArray(Mat mat,Scalar **v) 3039 { 3040 int ierr; 3041 3042 PetscFunctionBegin; 3043 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3044 PetscValidPointer(v); 3045 if (!mat->ops->getarray) SETERRQ(PETSC_ERR_SUP,0,""); 3046 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 3047 PetscFunctionReturn(0); 3048 } 3049 3050 #undef __FUNC__ 3051 #define __FUNC__ "MatRestoreArray" 3052 /*@C 3053 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 3054 3055 Not Collective 3056 3057 Input Parameter: 3058 + mat - the matrix 3059 - v - the location of the values 3060 3061 Fortran Note: 3062 This routine is used differently from Fortran, e.g., 3063 .vb 3064 Mat mat 3065 Scalar mat_array(1) 3066 PetscOffset i_mat 3067 int ierr 3068 call MatGetArray(mat,mat_array,i_mat,ierr) 3069 3070 C Access first local entry in matrix; note that array is 3071 C treated as one dimensional 3072 value = mat_array(i_mat + 1) 3073 3074 [... other code ...] 3075 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3076 .ve 3077 3078 See the Fortran chapter of the users manual and 3079 petsc/src/mat/examples/tests for details 3080 3081 Level: advanced 3082 3083 .keywords: matrix, array, elements, values, restore 3084 3085 .seealso: MatGetArray(), MatRestoreArrayF90() 3086 @*/ 3087 int MatRestoreArray(Mat mat,Scalar **v) 3088 { 3089 int ierr; 3090 3091 PetscFunctionBegin; 3092 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3093 PetscValidPointer(v); 3094 if (!mat->ops->restorearray) SETERRQ(PETSC_ERR_SUP,0,""); 3095 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 3096 PetscFunctionReturn(0); 3097 } 3098 3099 #undef __FUNC__ 3100 #define __FUNC__ "MatGetSubMatrices" 3101 /*@C 3102 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 3103 points to an array of valid matrices, they may be reused to store the new 3104 submatrices. 3105 3106 Collective on Mat 3107 3108 Input Parameters: 3109 + mat - the matrix 3110 . n - the number of submatrixes to be extracted 3111 . irow, icol - index sets of rows and columns to extract 3112 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3113 3114 Output Parameter: 3115 . submat - the array of submatrices 3116 3117 Notes: 3118 MatGetSubMatrices() can extract only sequential submatrices 3119 (from both sequential and parallel matrices). Use MatGetSubMatrix() 3120 to extract a parallel submatrix. 3121 3122 When extracting submatrices from a parallel matrix, each processor can 3123 form a different submatrix by setting the rows and columns of its 3124 individual index sets according to the local submatrix desired. 3125 3126 When finished using the submatrices, the user should destroy 3127 them with MatDestroySubMatrices(). 3128 3129 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 3130 original matrix has not changed from that last call to MatGetSubMatrices() 3131 3132 Fortran Note: 3133 The Fortran interface is slightly different from that given below, it 3134 requires one to pass in as submat a Mat (integer) array of size at least m. 3135 3136 Level: advanced 3137 3138 .keywords: matrix, get, submatrix, submatrices 3139 3140 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal() 3141 @*/ 3142 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat) 3143 { 3144 int ierr; 3145 3146 PetscFunctionBegin; 3147 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3148 if (!mat->ops->getsubmatrices) SETERRQ(PETSC_ERR_SUP,0,""); 3149 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3150 3151 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 3152 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr); 3153 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 3154 3155 PetscFunctionReturn(0); 3156 } 3157 3158 #undef __FUNC__ 3159 #define __FUNC__ "MatDestroyMatrices" 3160 /*@C 3161 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 3162 3163 Collective on Mat 3164 3165 Input Parameters: 3166 + n - the number of local matrices 3167 - mat - the matrices 3168 3169 Level: advanced 3170 3171 .keywords: matrix, destroy, submatrix, submatrices 3172 3173 .seealso: MatGetSubMatrices() 3174 @*/ 3175 int MatDestroyMatrices(int n,Mat **mat) 3176 { 3177 int ierr,i; 3178 3179 PetscFunctionBegin; 3180 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,1,"Trying to destroy negative number of matrices %d",n); 3181 PetscValidPointer(mat); 3182 for ( i=0; i<n; i++ ) { 3183 ierr = MatDestroy((*mat)[i]); CHKERRQ(ierr); 3184 } 3185 if (n) PetscFree(*mat); 3186 PetscFunctionReturn(0); 3187 } 3188 3189 #undef __FUNC__ 3190 #define __FUNC__ "MatIncreaseOverlap" 3191 /*@ 3192 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 3193 replaces the index sets by larger ones that represent submatrices with 3194 additional overlap. 3195 3196 Collective on Mat 3197 3198 Input Parameters: 3199 + mat - the matrix 3200 . n - the number of index sets 3201 . is - the array of pointers to index sets 3202 - ov - the additional overlap requested 3203 3204 Level: developer 3205 3206 .keywords: matrix, overlap, Schwarz 3207 3208 .seealso: MatGetSubMatrices() 3209 @*/ 3210 int MatIncreaseOverlap(Mat mat,int n, IS *is,int ov) 3211 { 3212 int ierr; 3213 3214 PetscFunctionBegin; 3215 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3216 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for unassembled matrix"); 3217 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Not for factored matrix"); 3218 3219 if (ov == 0) PetscFunctionReturn(0); 3220 if (!mat->ops->increaseoverlap) SETERRQ(PETSC_ERR_SUP,0,""); 3221 PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0); 3222 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr); 3223 PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0); 3224 PetscFunctionReturn(0); 3225 } 3226 3227 #undef __FUNC__ 3228 #define __FUNC__ "MatPrintHelp" 3229 /*@ 3230 MatPrintHelp - Prints all the options for the matrix. 3231 3232 Collective on Mat 3233 3234 Input Parameter: 3235 . mat - the matrix 3236 3237 Options Database Keys: 3238 + -help - Prints matrix options 3239 - -h - Prints matrix options 3240 3241 Level: developer 3242 3243 .keywords: mat, help 3244 3245 .seealso: MatCreate(), MatCreateXXX() 3246 @*/ 3247 int MatPrintHelp(Mat mat) 3248 { 3249 static int called = 0; 3250 int ierr; 3251 MPI_Comm comm; 3252 3253 PetscFunctionBegin; 3254 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3255 3256 comm = mat->comm; 3257 if (!called) { 3258 (*PetscHelpPrintf)(comm,"General matrix options:\n"); 3259 (*PetscHelpPrintf)(comm," -mat_view_info: view basic matrix info during MatAssemblyEnd()\n"); 3260 (*PetscHelpPrintf)(comm," -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n"); 3261 (*PetscHelpPrintf)(comm," -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n"); 3262 (*PetscHelpPrintf)(comm," -draw_pause <sec>: set seconds of display pause\n"); 3263 (*PetscHelpPrintf)(comm," -display <name>: set alternate display\n"); 3264 called = 1; 3265 } 3266 if (mat->ops->printhelp) { 3267 ierr = (*mat->ops->printhelp)(mat); CHKERRQ(ierr); 3268 } 3269 PetscFunctionReturn(0); 3270 } 3271 3272 #undef __FUNC__ 3273 #define __FUNC__ "MatGetBlockSize" 3274 /*@ 3275 MatGetBlockSize - Returns the matrix block size; useful especially for the 3276 block row and block diagonal formats. 3277 3278 Not Collective 3279 3280 Input Parameter: 3281 . mat - the matrix 3282 3283 Output Parameter: 3284 . bs - block size 3285 3286 Notes: 3287 Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG. 3288 Block row formats are MATSEQBAIJ, MATMPIBAIJ 3289 3290 Level: intermediate 3291 3292 .keywords: matrix, get, block, size 3293 3294 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 3295 @*/ 3296 int MatGetBlockSize(Mat mat,int *bs) 3297 { 3298 int ierr; 3299 3300 PetscFunctionBegin; 3301 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3302 PetscValidIntPointer(bs); 3303 if (!mat->ops->getblocksize) SETERRQ(PETSC_ERR_SUP,0,""); 3304 ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr); 3305 PetscFunctionReturn(0); 3306 } 3307 3308 #undef __FUNC__ 3309 #define __FUNC__ "MatGetRowIJ" 3310 /*@C 3311 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 3312 EXPERTS ONLY. 3313 3314 Collective on Mat 3315 3316 Input Parameters: 3317 + mat - the matrix 3318 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 3319 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3320 symmetrized 3321 3322 Output Parameters: 3323 + n - number of rows in the (possibly compressed) matrix 3324 . ia - the row pointers 3325 . ja - the column indices 3326 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 3327 3328 Level: developer 3329 3330 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 3331 @*/ 3332 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3333 { 3334 int ierr; 3335 3336 PetscFunctionBegin; 3337 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3338 if (ia) PetscValidIntPointer(ia); 3339 if (ja) PetscValidIntPointer(ja); 3340 PetscValidIntPointer(done); 3341 if (!mat->ops->getrowij) *done = PETSC_FALSE; 3342 else { 3343 *done = PETSC_TRUE; 3344 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 3345 } 3346 PetscFunctionReturn(0); 3347 } 3348 3349 #undef __FUNC__ 3350 #define __FUNC__ "MatGetColumnIJ" 3351 /*@C 3352 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 3353 EXPERTS ONLY. 3354 3355 Collective on Mat 3356 3357 Input Parameters: 3358 + mat - the matrix 3359 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3360 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3361 symmetrized 3362 3363 Output Parameters: 3364 + n - number of columns in the (possibly compressed) matrix 3365 . ia - the column pointers 3366 . ja - the row indices 3367 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 3368 3369 Level: developer 3370 3371 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 3372 @*/ 3373 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3374 { 3375 int ierr; 3376 3377 PetscFunctionBegin; 3378 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3379 if (ia) PetscValidIntPointer(ia); 3380 if (ja) PetscValidIntPointer(ja); 3381 PetscValidIntPointer(done); 3382 3383 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 3384 else { 3385 *done = PETSC_TRUE; 3386 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 3387 } 3388 PetscFunctionReturn(0); 3389 } 3390 3391 #undef __FUNC__ 3392 #define __FUNC__ "MatRestoreRowIJ" 3393 /*@C 3394 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 3395 MatGetRowIJ(). EXPERTS ONLY. 3396 3397 Collective on Mat 3398 3399 Input Parameters: 3400 + mat - the matrix 3401 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3402 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3403 symmetrized 3404 3405 Output Parameters: 3406 + n - size of (possibly compressed) matrix 3407 . ia - the row pointers 3408 . ja - the column indices 3409 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 3410 3411 Level: developer 3412 3413 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 3414 @*/ 3415 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3416 { 3417 int ierr; 3418 3419 PetscFunctionBegin; 3420 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3421 if (ia) PetscValidIntPointer(ia); 3422 if (ja) PetscValidIntPointer(ja); 3423 PetscValidIntPointer(done); 3424 3425 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 3426 else { 3427 *done = PETSC_TRUE; 3428 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 3429 } 3430 PetscFunctionReturn(0); 3431 } 3432 3433 #undef __FUNC__ 3434 #define __FUNC__ "MatRestoreColumnIJ" 3435 /*@C 3436 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 3437 MatGetColumnIJ(). EXPERTS ONLY. 3438 3439 Collective on Mat 3440 3441 Input Parameters: 3442 + mat - the matrix 3443 . shift - 1 or zero indicating we want the indices starting at 0 or 1 3444 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 3445 symmetrized 3446 3447 Output Parameters: 3448 + n - size of (possibly compressed) matrix 3449 . ia - the column pointers 3450 . ja - the row indices 3451 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 3452 3453 Level: developer 3454 3455 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 3456 @*/ 3457 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done) 3458 { 3459 int ierr; 3460 3461 PetscFunctionBegin; 3462 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3463 if (ia) PetscValidIntPointer(ia); 3464 if (ja) PetscValidIntPointer(ja); 3465 PetscValidIntPointer(done); 3466 3467 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 3468 else { 3469 *done = PETSC_TRUE; 3470 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done); CHKERRQ(ierr); 3471 } 3472 PetscFunctionReturn(0); 3473 } 3474 3475 #undef __FUNC__ 3476 #define __FUNC__ "MatColoringPatch" 3477 /*@C 3478 MatColoringPatch - EXPERTS ONLY, used inside matrix coloring routines that 3479 use MatGetRowIJ() and/or MatGetColumnIJ(). 3480 3481 Collective on Mat 3482 3483 Input Parameters: 3484 + mat - the matrix 3485 . n - number of colors 3486 - colorarray - array indicating color for each column 3487 3488 Output Parameters: 3489 . iscoloring - coloring generated using colorarray information 3490 3491 Level: developer 3492 3493 .seealso: MatGetRowIJ(), MatGetColumnIJ() 3494 3495 .keywords: mat, coloring, patch 3496 @*/ 3497 int MatColoringPatch(Mat mat,int n,int *colorarray,ISColoring *iscoloring) 3498 { 3499 int ierr; 3500 3501 PetscFunctionBegin; 3502 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3503 PetscValidIntPointer(colorarray); 3504 3505 if (!mat->ops->coloringpatch) {SETERRQ(PETSC_ERR_SUP,0,"");} 3506 else { 3507 ierr = (*mat->ops->coloringpatch)(mat,n,colorarray,iscoloring); CHKERRQ(ierr); 3508 } 3509 PetscFunctionReturn(0); 3510 } 3511 3512 3513 #undef __FUNC__ 3514 #define __FUNC__ "MatSetUnfactored" 3515 /*@ 3516 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 3517 3518 Collective on Mat 3519 3520 Input Parameter: 3521 . mat - the factored matrix to be reset 3522 3523 Notes: 3524 This routine should be used only with factored matrices formed by in-place 3525 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 3526 format). This option can save memory, for example, when solving nonlinear 3527 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 3528 ILU(0) preconditioner. 3529 3530 Note that one can specify in-place ILU(0) factorization by calling 3531 .vb 3532 PCType(pc,PCILU); 3533 PCILUSeUseInPlace(pc); 3534 .ve 3535 or by using the options -pc_type ilu -pc_ilu_in_place 3536 3537 In-place factorization ILU(0) can also be used as a local 3538 solver for the blocks within the block Jacobi or additive Schwarz 3539 methods (runtime option: -sub_pc_ilu_in_place). See the discussion 3540 of these preconditioners in the users manual for details on setting 3541 local solver options. 3542 3543 Most users should employ the simplified SLES interface for linear solvers 3544 instead of working directly with matrix algebra routines such as this. 3545 See, e.g., SLESCreate(). 3546 3547 Level: developer 3548 3549 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace() 3550 3551 .keywords: matrix-free, in-place ILU, in-place LU 3552 @*/ 3553 int MatSetUnfactored(Mat mat) 3554 { 3555 int ierr; 3556 3557 PetscFunctionBegin; 3558 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3559 mat->factor = 0; 3560 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 3561 ierr = (*mat->ops->setunfactored)(mat); CHKERRQ(ierr); 3562 PetscFunctionReturn(0); 3563 } 3564 3565 #undef __FUNC__ 3566 #define __FUNC__ "MatGetType" 3567 /*@C 3568 MatGetType - Gets the matrix type and name (as a string) from the matrix. 3569 3570 Not Collective 3571 3572 Input Parameter: 3573 . mat - the matrix 3574 3575 Output Parameter: 3576 + type - the matrix type (or use PETSC_NULL) 3577 - name - name of matrix type (or use PETSC_NULL) 3578 3579 Level: intermediate 3580 3581 .keywords: matrix, get, type, name 3582 @*/ 3583 int MatGetType(Mat mat,MatType *type,char **name) 3584 { 3585 int itype = (int)mat->type; 3586 char *matname[10]; 3587 3588 PetscFunctionBegin; 3589 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3590 3591 if (type) *type = (MatType) mat->type; 3592 if (name) { 3593 /* Note: Be sure that this list corresponds to the enum in mat.h */ 3594 matname[0] = "MATSEQDENSE"; 3595 matname[1] = "MATSEQAIJ"; 3596 matname[2] = "MATMPIAIJ"; 3597 matname[3] = "MATSHELL"; 3598 matname[4] = "MATMPIROWBS"; 3599 matname[5] = "MATSEQBDIAG"; 3600 matname[6] = "MATMPIBDIAG"; 3601 matname[7] = "MATMPIDENSE"; 3602 matname[8] = "MATSEQBAIJ"; 3603 matname[9] = "MATMPIBAIJ"; 3604 3605 if (itype < 0 || itype > 9) *name = "Unknown matrix type"; 3606 else *name = matname[itype]; 3607 } 3608 PetscFunctionReturn(0); 3609 } 3610 3611 /*MC 3612 MatGetArrayF90 - Accesses a matrix array from Fortran90. 3613 3614 Synopsis: 3615 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 3616 3617 Not collective 3618 3619 Input Parameter: 3620 . x - matrix 3621 3622 Output Parameters: 3623 + xx_v - the Fortran90 pointer to the array 3624 - ierr - error code 3625 3626 Example of Usage: 3627 .vb 3628 Scalar, pointer xx_v(:) 3629 .... 3630 call MatGetArrayF90(x,xx_v,ierr) 3631 a = xx_v(3) 3632 call MatRestoreArrayF90(x,xx_v,ierr) 3633 .ve 3634 3635 Notes: 3636 Not yet supported for all F90 compilers 3637 3638 Level: advanced 3639 3640 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 3641 3642 .keywords: matrix, array, f90 3643 M*/ 3644 3645 /*MC 3646 MatRestoreArrayF90 - Restores a matrix array that has been 3647 accessed with MatGetArrayF90(). 3648 3649 Synopsis: 3650 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 3651 3652 Not collective 3653 3654 Input Parameters: 3655 + x - matrix 3656 - xx_v - the Fortran90 pointer to the array 3657 3658 Output Parameter: 3659 . ierr - error code 3660 3661 Example of Usage: 3662 .vb 3663 Scalar, pointer xx_v(:) 3664 .... 3665 call MatGetArrayF90(x,xx_v,ierr) 3666 a = xx_v(3) 3667 call MatRestoreArrayF90(x,xx_v,ierr) 3668 .ve 3669 3670 Notes: 3671 Not yet supported for all F90 compilers 3672 3673 Level: advanced 3674 3675 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 3676 3677 .keywords: matrix, array, f90 3678 M*/ 3679 3680 3681 #undef __FUNC__ 3682 #define __FUNC__ "MatGetSubMatrix" 3683 /*@ 3684 MatGetSubMatrix - Gets a single submatrix on the same number of processors 3685 as the original matrix. 3686 3687 Collective on Mat 3688 3689 Input Parameters: 3690 + mat - the original matrix 3691 . isrow - rows this processor should obtain 3692 . iscol - columns for all processors you wish to keep 3693 . csize - number of columns "local" to this processor (does nothing for sequential 3694 matrices). This should match the result from VecGetLocalSize(x,...) if you 3695 plan to use the matrix in a A*x or use PETSC_DECIDE 3696 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3697 3698 Output Parameter: 3699 . newmat - the new submatrix, of the same type as the old 3700 3701 Level: advanced 3702 3703 .keywords: matrix, get, submatrix, submatrices 3704 3705 .seealso: MatGetSubMatrices() 3706 @*/ 3707 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat) 3708 { 3709 int ierr, size; 3710 Mat *local; 3711 3712 PetscFunctionBegin; 3713 MPI_Comm_size(mat->comm,&size); 3714 3715 /* if original matrix is on just one processor then use submatrix generated */ 3716 if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 3717 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 3718 PetscFunctionReturn(0); 3719 } else if (!mat->ops->getsubmatrix && size == 1) { 3720 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 3721 *newmat = *local; 3722 PetscFree(local); 3723 PetscFunctionReturn(0); 3724 } 3725 3726 if (!mat->ops->getsubmatrix) SETERRQ(PETSC_ERR_SUP,0,"Not currently implemented"); 3727 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr); 3728 PetscFunctionReturn(0); 3729 } 3730 3731 #undef __FUNC__ 3732 #define __FUNC__ "MatGetMaps" 3733 /*@C 3734 MatGetMaps - Returns the maps associated with the matrix. 3735 3736 Not Collective 3737 3738 Input Parameter: 3739 . mat - the matrix 3740 3741 Output Parameters: 3742 + rmap - the row (right) map 3743 - cmap - the column (left) map 3744 3745 Level: developer 3746 3747 .keywords: matrix, get, map 3748 @*/ 3749 int MatGetMaps(Mat mat,Map *rmap,Map *cmap) 3750 { 3751 int ierr; 3752 3753 PetscFunctionBegin; 3754 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3755 ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr); 3756 PetscFunctionReturn(0); 3757 } 3758 3759 /* 3760 Version that works for all PETSc matrices 3761 */ 3762 #undef __FUNC__ 3763 #define __FUNC__ "MatGetMaps_Petsc" 3764 int MatGetMaps_Petsc(Mat mat,Map *rmap,Map *cmap) 3765 { 3766 PetscFunctionBegin; 3767 if (rmap) *rmap = mat->rmap; 3768 if (cmap) *cmap = mat->cmap; 3769 PetscFunctionReturn(0); 3770 } 3771