1 #ifndef lint 2 static char vcid[] = "$Id: matrix.c,v 1.153 1996/03/18 00:39:40 bsmith Exp bsmith $"; 3 #endif 4 5 /* 6 This is where the abstract matrix operations are defined 7 */ 8 9 #include "petsc.h" 10 #include "matimpl.h" /*I "mat.h" I*/ 11 #include "vec/vecimpl.h" 12 #include "pinclude/pviewer.h" 13 #include "draw.h" 14 15 /*@C 16 MatGetReordering - Gets a reordering for a matrix to reduce fill or to 17 improve numerical stability of LU factorization. 18 19 Input Parameters: 20 . mat - the matrix 21 . type - type of reordering, one of the following: 22 $ ORDER_NATURAL - Natural 23 $ ORDER_ND - Nested Dissection 24 $ ORDER_1WD - One-way Dissection 25 $ ORDER_RCM - Reverse Cuthill-McGee 26 $ ORDER_QMD - Quotient Minimum Degree 27 28 Output Parameters: 29 . rperm - row permutation indices 30 . cperm - column permutation indices 31 32 Options Database Keys: 33 To specify the ordering through the options database, use one of 34 the following 35 $ -mat_order natural, -mat_order nd, -mat_order 1wd, 36 $ -mat_order rcm, -mat_order qmd 37 38 Notes: 39 If the column permutations and row permutations are the same, 40 then MatGetReordering() returns 0 in cperm. 41 42 The user can define additional orderings; see MatReorderingRegister(). 43 44 .keywords: matrix, set, ordering, factorization, direct, ILU, LU, 45 fill, reordering, natural, Nested Dissection, 46 One-way Dissection, Cholesky, Reverse Cuthill-McGee, 47 Quotient Minimum Degree 48 49 .seealso: MatGetReorderingTypeFromOptions(), MatReorderingRegister() 50 @*/ 51 int MatGetReordering(Mat mat,MatOrdering type,IS *rperm,IS *cperm) 52 { 53 int ierr; 54 PetscValidHeaderSpecific(mat,MAT_COOKIE); 55 if (!mat->assembled) SETERRQ(1,"MatGetReordering:Not for unassembled matrix"); 56 57 if (!mat->ops.getreordering) {*rperm = 0; *cperm = 0; return 0;} 58 PLogEventBegin(MAT_GetReordering,mat,0,0,0); 59 ierr = MatGetReorderingTypeFromOptions(0,&type); CHKERRQ(ierr); 60 ierr = (*mat->ops.getreordering)(mat,type,rperm,cperm); CHKERRQ(ierr); 61 PLogEventEnd(MAT_GetReordering,mat,0,0,0); 62 return 0; 63 } 64 65 /*@C 66 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 67 for each row that you get to ensure that your application does 68 not bleed memory. 69 70 Input Parameters: 71 . mat - the matrix 72 . row - the row to get 73 74 Output Parameters: 75 . ncols - the number of nonzeros in the row 76 . cols - if nonzero, the column numbers 77 . vals - if nonzero, the values 78 79 Notes: 80 This routine is provided for people who need to have direct access 81 to the structure of a matrix. We hope that we provide enough 82 high-level matrix routines that few users will need it. 83 84 For better efficiency, set cols and/or vals to PETSC_NULL if you do 85 not wish to extract these quantities. 86 87 The user can only examine the values extracted with MatGetRow(); 88 the values cannot be altered. To change the matrix entries, one 89 must use MatSetValues(). 90 91 .keywords: matrix, row, get, extract 92 93 .seealso: MatRestoreRow(), MatSetValues() 94 @*/ 95 int MatGetRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 96 { 97 int ierr; 98 PetscValidHeaderSpecific(mat,MAT_COOKIE); 99 if (!mat->assembled) SETERRQ(1,"MatGetRow:Not for unassembled matrix"); 100 PLogEventBegin(MAT_GetRow,mat,0,0,0); 101 ierr = (*mat->ops.getrow)(mat,row,ncols,cols,vals); CHKERRQ(ierr); 102 PLogEventEnd(MAT_GetRow,mat,0,0,0); 103 return 0; 104 } 105 106 /*@C 107 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 108 109 Input Parameters: 110 . mat - the matrix 111 . row - the row to get 112 . ncols, cols - the number of nonzeros and their columns 113 . vals - if nonzero the column values 114 115 .keywords: matrix, row, restore 116 117 .seealso: MatGetRow() 118 @*/ 119 int MatRestoreRow(Mat mat,int row,int *ncols,int **cols,Scalar **vals) 120 { 121 PetscValidHeaderSpecific(mat,MAT_COOKIE); 122 if (!mat->assembled) SETERRQ(1,"MatRestoreRow:Not for unassembled matrix"); 123 if (!mat->ops.restorerow) return 0; 124 return (*mat->ops.restorerow)(mat,row,ncols,cols,vals); 125 } 126 /*@ 127 MatView - Visualizes a matrix object. 128 129 Input Parameters: 130 . mat - the matrix 131 . ptr - visualization context 132 133 Notes: 134 The available visualization contexts include 135 $ STDOUT_VIEWER_SELF - standard output (default) 136 $ STDOUT_VIEWER_WORLD - synchronized standard 137 $ output where only the first processor opens 138 $ the file. All other processors send their 139 $ data to the first processor to print. 140 141 The user can open alternative vistualization contexts with 142 $ ViewerFileOpenASCII() - output to a specified file 143 $ ViewerFileOpenBinary() - output in binary to a 144 $ specified file; corresponding input uses MatLoad() 145 $ ViewerDrawOpenX() - output nonzero matrix structure to 146 $ an X window display 147 $ ViewerMatlabOpen() - output matrix to Matlab viewer. 148 $ Currently only the sequential dense and AIJ 149 $ matrix types support the Matlab viewer. 150 151 The user can call ViewerSetFormat() to specify the output 152 format of ASCII printed objects (when using STDOUT_VIEWER_SELF, 153 STDOUT_VIEWER_WORLD and ViewerFileOpenASCII). Available formats include 154 $ ASCII_FORMAT_DEFAULT - default, prints matrix contents 155 $ ASCII_FORMAT_MATLAB - Matlab format 156 $ ASCII_FORMAT_IMPL - implementation-specific format 157 $ (which is in many cases the same as the default) 158 $ ASCII_FORMAT_INFO - basic information about the matrix 159 $ size and structure (not the matrix entries) 160 $ ASCII_FORMAT_INFO_DETAILED - more detailed information about the 161 $ matrix structure 162 163 .keywords: matrix, view, visualize, output, print, write, draw 164 165 .seealso: ViewerSetFormat(), ViewerFileOpenASCII(), ViewerDrawOpenX(), 166 ViewerMatlabOpen(), ViewerFileOpenBinary(), MatLoad() 167 @*/ 168 int MatView(Mat mat,Viewer viewer) 169 { 170 int format, ierr, rows, cols,nz, nzalloc, mem; 171 FILE *fd; 172 char *cstr; 173 ViewerType vtype; 174 MPI_Comm comm = mat->comm; 175 176 PetscValidHeaderSpecific(mat,MAT_COOKIE); 177 if (!mat->assembled) SETERRQ(1,"MatView:Not for unassembled matrix"); 178 179 if (!viewer) { 180 viewer = STDOUT_VIEWER_SELF; 181 } 182 183 ierr = ViewerGetType(viewer,&vtype); 184 if (vtype == ASCII_FILE_VIEWER || vtype == ASCII_FILES_VIEWER) { 185 ierr = ViewerGetFormat(viewer,&format); CHKERRQ(ierr); 186 ierr = ViewerASCIIGetPointer(viewer,&fd); CHKERRQ(ierr); 187 if (format == ASCII_FORMAT_INFO || format == ASCII_FORMAT_INFO_DETAILED) { 188 PetscFPrintf(comm,fd,"Matrix Object:\n"); 189 ierr = MatGetType(mat,PETSC_NULL,&cstr); CHKERRQ(ierr); 190 ierr = MatGetSize(mat,&rows,&cols); CHKERRQ(ierr); 191 PetscFPrintf(comm,fd," type=%s, rows=%d, cols=%d\n",cstr,rows,cols); 192 if (mat->ops.getinfo) { 193 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&nz,&nzalloc,&mem); CHKERRQ(ierr); 194 PetscFPrintf(comm,fd," total: nonzeros=%d, allocated nonzeros=%d\n",nz, 195 nzalloc); 196 } 197 } 198 } 199 if (mat->view) {ierr = (*mat->view)((PetscObject)mat,viewer); CHKERRQ(ierr);} 200 return 0; 201 } 202 203 /*@C 204 MatDestroy - Frees space taken by a matrix. 205 206 Input Parameter: 207 . mat - the matrix 208 209 .keywords: matrix, destroy 210 @*/ 211 int MatDestroy(Mat mat) 212 { 213 PetscValidHeaderSpecific(mat,MAT_COOKIE); 214 return (*mat->destroy)((PetscObject)mat); 215 } 216 /*@ 217 MatValid - Checks whether a matrix object is valid. 218 219 Input Parameter: 220 . m - the matrix to check 221 222 Output Parameter: 223 flg - flag indicating matrix status, either 224 $ PETSC_TRUE if matrix is valid; 225 $ PETSC_FALSE otherwise. 226 227 .keywords: matrix, valid 228 @*/ 229 int MatValid(Mat m,PetscTruth *flg) 230 { 231 if (!m) *flg = PETSC_FALSE; 232 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 233 else *flg = PETSC_TRUE; 234 return 0; 235 } 236 237 /*@ 238 MatSetValues - Inserts or adds a block of values into a matrix. 239 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 240 MUST be called after all calls to MatSetValues() have been completed. 241 242 Input Parameters: 243 . mat - the matrix 244 . v - a logically two-dimensional array of values 245 . m, indexm - the number of rows and their global indices 246 . n, indexn - the number of columns and their global indices 247 . addv - either ADD_VALUES or INSERT_VALUES, where 248 $ ADD_VALUES - adds values to any existing entries 249 $ INSERT_VALUES - replaces existing entries with new values 250 251 Notes: 252 By default the values, v, are row-oriented and unsorted. 253 See MatSetOptions() for other options. 254 255 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 256 options cannot be mixed without intervening calls to the assembly 257 routines. 258 259 .keywords: matrix, insert, add, set, values 260 261 .seealso: MatSetOptions(), MatAssemblyBegin(), MatAssemblyEnd() 262 @*/ 263 int MatSetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v, 264 InsertMode addv) 265 { 266 int ierr; 267 PetscValidHeaderSpecific(mat,MAT_COOKIE); 268 269 if (mat->assembled) { 270 mat->was_assembled = PETSC_TRUE; 271 mat->assembled = PETSC_FALSE; 272 mat->same_nonzero = PETSC_TRUE; 273 } 274 275 PLogEventBegin(MAT_SetValues,mat,0,0,0); 276 ierr = (*mat->ops.setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 277 PLogEventEnd(MAT_SetValues,mat,0,0,0); 278 return 0; 279 } 280 281 /*@ 282 MatGetValues - Gets a block of values from a matrix. 283 284 Input Parameters: 285 . mat - the matrix 286 . v - a logically two-dimensional array for storing the values 287 . m, indexm - the number of rows and their global indices 288 . n, indexn - the number of columns and their global indices 289 290 Notes: 291 The user must allocate space (m*n Scalars) for the values, v. 292 The values, v, are then returned in a row-oriented format, 293 analogous to that used by default in MatSetValues(). 294 295 .keywords: matrix, get, values 296 297 .seealso: MatGetRow(), MatGetSubmatrix(), MatGetSubmatrices(), MatSetValues() 298 @*/ 299 int MatGetValues(Mat mat,int m,int *idxm,int n,int *idxn,Scalar *v) 300 { 301 int ierr; 302 303 PetscValidHeaderSpecific(mat,MAT_COOKIE); 304 if (!mat->assembled) SETERRQ(1,"MatGetValues:Not for unassembled matrix"); 305 306 PLogEventBegin(MAT_GetValues,mat,0,0,0); 307 ierr = (*mat->ops.getvalues)(mat,m,idxm,n,idxn,v); CHKERRQ(ierr); 308 PLogEventEnd(MAT_GetValues,mat,0,0,0); 309 return 0; 310 } 311 312 /* --------------------------------------------------------*/ 313 /*@ 314 MatMult - Computes matrix-vector product. 315 316 Input Parameters: 317 . mat - the matrix 318 . x - the vector to be multilplied 319 320 Output Parameters: 321 . y - the result 322 323 .keywords: matrix, multiply, matrix-vector product 324 325 .seealso: MatMultTrans(), MatMultAdd(), MatMultTransAdd() 326 @*/ 327 int MatMult(Mat mat,Vec x,Vec y) 328 { 329 int ierr; 330 PetscValidHeaderSpecific(mat,MAT_COOKIE); 331 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 332 if (!mat->assembled) SETERRQ(1,"MatMult:Not for unassembled matrix"); 333 if (x == y) SETERRQ(1,"MatMult:x and y must be different vectors"); 334 335 PLogEventBegin(MAT_Mult,mat,x,y,0); 336 ierr = (*mat->ops.mult)(mat,x,y); CHKERRQ(ierr); 337 PLogEventEnd(MAT_Mult,mat,x,y,0); 338 return 0; 339 } 340 /*@ 341 MatMultTrans - Computes matrix transpose times a vector. 342 343 Input Parameters: 344 . mat - the matrix 345 . x - the vector to be multilplied 346 347 Output Parameters: 348 . y - the result 349 350 .keywords: matrix, multiply, matrix-vector product, transpose 351 352 .seealso: MatMult(), MatMultAdd(), MatMultTransAdd() 353 @*/ 354 int MatMultTrans(Mat mat,Vec x,Vec y) 355 { 356 int ierr; 357 PetscValidHeaderSpecific(mat,MAT_COOKIE); 358 PetscValidHeaderSpecific(x,VEC_COOKIE); PetscValidHeaderSpecific(y,VEC_COOKIE); 359 if (!mat->assembled) SETERRQ(1,"MatMultTrans:Not for unassembled matrix"); 360 if (x == y) SETERRQ(1,"MatMultTrans:x and y must be different vectors"); 361 362 PLogEventBegin(MAT_MultTrans,mat,x,y,0); 363 ierr = (*mat->ops.multtrans)(mat,x,y); CHKERRQ(ierr); 364 PLogEventEnd(MAT_MultTrans,mat,x,y,0); 365 return 0; 366 } 367 /*@ 368 MatMultAdd - Computes v3 = v2 + A * v1. 369 370 Input Parameters: 371 . mat - the matrix 372 . v1, v2 - the vectors 373 374 Output Parameters: 375 . v3 - the result 376 377 .keywords: matrix, multiply, matrix-vector product, add 378 379 .seealso: MatMultTrans(), MatMult(), MatMultTransAdd() 380 @*/ 381 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 382 { 383 int ierr; 384 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v1,VEC_COOKIE); 385 PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE); 386 if (!mat->assembled) SETERRQ(1,"MatMultAdd:Not for unassembled matrix"); 387 388 PLogEventBegin(MAT_MultAdd,mat,v1,v2,v3); 389 if (v1 == v3) SETERRQ(1,"MatMultAdd:v1 and v3 must be different vectors"); 390 ierr = (*mat->ops.multadd)(mat,v1,v2,v3); CHKERRQ(ierr); 391 PLogEventEnd(MAT_MultAdd,mat,v1,v2,v3); 392 return 0; 393 } 394 /*@ 395 MatMultTransAdd - Computes v3 = v2 + A' * v1. 396 397 Input Parameters: 398 . mat - the matrix 399 . v1, v2 - the vectors 400 401 Output Parameters: 402 . v3 - the result 403 404 .keywords: matrix, multiply, matrix-vector product, transpose, add 405 406 .seealso: MatMultTrans(), MatMultAdd(), MatMult() 407 @*/ 408 int MatMultTransAdd(Mat mat,Vec v1,Vec v2,Vec v3) 409 { 410 int ierr; 411 PetscValidHeaderSpecific(mat,MAT_COOKIE); PetscValidHeaderSpecific(v1,VEC_COOKIE); 412 PetscValidHeaderSpecific(v2,VEC_COOKIE); PetscValidHeaderSpecific(v3,VEC_COOKIE); 413 if (!mat->assembled) SETERRQ(1,"MatMultTransAdd:Not for unassembled matrix"); 414 if (!mat->ops.multtransadd) SETERRQ(PETSC_ERR_SUP,"MatMultTransAdd"); 415 if (v1 == v3) SETERRQ(1,"MatMultTransAdd:v1 and v2 must be different vectors"); 416 417 PLogEventBegin(MAT_MultTransAdd,mat,v1,v2,v3); 418 ierr = (*mat->ops.multtransadd)(mat,v1,v2,v3); CHKERRQ(ierr); 419 PLogEventEnd(MAT_MultTransAdd,mat,v1,v2,v3); 420 return 0; 421 } 422 /* ------------------------------------------------------------*/ 423 /*@C 424 MatGetInfo - Returns information about matrix storage (number of 425 nonzeros, memory). 426 427 Input Parameters: 428 . mat - the matrix 429 430 Output Parameters: 431 . flag - flag indicating the type of parameters to be returned 432 $ flag = MAT_LOCAL: local matrix 433 $ flag = MAT_GLOBAL_MAX: maximum over all processors 434 $ flag = MAT_GLOBAL_SUM: sum over all processors 435 . nz - the number of nonzeros [or PETSC_NULL] 436 . nzalloc - the number of allocated nonzeros [or PETSC_NULL] 437 . mem - the memory used (in bytes) [or PETSC_NULL] 438 439 .keywords: matrix, get, info, storage, nonzeros, memory 440 @*/ 441 int MatGetInfo(Mat mat,MatInfoType flag,int *nz,int *nzalloc,int *mem) 442 { 443 PetscValidHeaderSpecific(mat,MAT_COOKIE); 444 if (!mat->ops.getinfo) SETERRQ(PETSC_ERR_SUP,"MatGetInfo"); 445 return (*mat->ops.getinfo)(mat,flag,nz,nzalloc,mem); 446 } 447 /* ----------------------------------------------------------*/ 448 /*@ 449 MatLUFactor - Performs in-place LU factorization of matrix. 450 451 Input Parameters: 452 . mat - the matrix 453 . row - row permutation 454 . col - column permutation 455 . f - expected fill as ratio of original fill. 456 457 .keywords: matrix, factor, LU, in-place 458 459 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 460 @*/ 461 int MatLUFactor(Mat mat,IS row,IS col,double f) 462 { 463 int ierr; 464 PetscValidHeaderSpecific(mat,MAT_COOKIE); 465 if (!mat->ops.lufactor) SETERRQ(PETSC_ERR_SUP,"MatLUFactor"); 466 if (!mat->assembled) SETERRQ(1,"MatLUFactor:Not for unassembled matrix"); 467 468 PLogEventBegin(MAT_LUFactor,mat,row,col,0); 469 ierr = (*mat->ops.lufactor)(mat,row,col,f); CHKERRQ(ierr); 470 PLogEventEnd(MAT_LUFactor,mat,row,col,0); 471 return 0; 472 } 473 /*@ 474 MatILUFactor - Performs in-place ILU factorization of matrix. 475 476 Input Parameters: 477 . mat - the matrix 478 . row - row permutation 479 . col - column permutation 480 . f - expected fill as ratio of original fill. 481 . level - number of levels of fill. 482 483 Note: probably really only in-place when level is zero. 484 .keywords: matrix, factor, ILU, in-place 485 486 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 487 @*/ 488 int MatILUFactor(Mat mat,IS row,IS col,double f,int level) 489 { 490 int ierr; 491 PetscValidHeaderSpecific(mat,MAT_COOKIE); 492 if (!mat->ops.ilufactor) SETERRQ(PETSC_ERR_SUP,"MatILUFactor"); 493 if (!mat->assembled) SETERRQ(1,"MatILUFactor:Not for unassembled matrix"); 494 495 PLogEventBegin(MAT_ILUFactor,mat,row,col,0); 496 ierr = (*mat->ops.ilufactor)(mat,row,col,f,level); CHKERRQ(ierr); 497 PLogEventEnd(MAT_ILUFactor,mat,row,col,0); 498 return 0; 499 } 500 501 /*@ 502 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 503 Call this routine before calling MatLUFactorNumeric(). 504 505 Input Parameters: 506 . mat - the matrix 507 . row, col - row and column permutations 508 . f - expected fill as ratio of the original number of nonzeros, 509 for example 3.0; choosing this parameter well can result in 510 more efficient use of time and space. 511 512 Output Parameter: 513 . fact - new matrix that has been symbolically factored 514 515 Options Database Key: 516 $ -mat_lu_fill <f>, where f is the fill ratio 517 518 Notes: 519 See the file $(PETSC_DIR)/Performace for additional information about 520 choosing the fill factor for better efficiency. 521 522 .keywords: matrix, factor, LU, symbolic, fill 523 524 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor() 525 @*/ 526 int MatLUFactorSymbolic(Mat mat,IS row,IS col,double f,Mat *fact) 527 { 528 int ierr,flg; 529 PetscValidHeaderSpecific(mat,MAT_COOKIE); 530 if (!fact) SETERRQ(1,"MatLUFactorSymbolic:Missing factor matrix argument"); 531 if (!mat->ops.lufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatLUFactorSymbolic"); 532 if (!mat->assembled) SETERRQ(1,"MatLUFactorSymbolic:Not for unassembled matrix"); 533 534 ierr = OptionsGetDouble(PETSC_NULL,"-mat_lu_fill",&f,&flg); CHKERRQ(ierr); 535 PLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0); 536 ierr = (*mat->ops.lufactorsymbolic)(mat,row,col,f,fact); CHKERRQ(ierr); 537 PLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0); 538 return 0; 539 } 540 /*@ 541 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 542 Call this routine after first calling MatLUFactorSymbolic(). 543 544 Input Parameters: 545 . mat - the matrix 546 . row, col - row and column permutations 547 548 Output Parameters: 549 . fact - symbolically factored matrix that must have been generated 550 by MatLUFactorSymbolic() 551 552 Notes: 553 See MatLUFactor() for in-place factorization. See 554 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 555 556 .keywords: matrix, factor, LU, numeric 557 558 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 559 @*/ 560 int MatLUFactorNumeric(Mat mat,Mat *fact) 561 { 562 int ierr,flg; 563 564 PetscValidHeaderSpecific(mat,MAT_COOKIE); 565 if (!fact) SETERRQ(1,"MatLUFactorNumeric:Missing factor matrix argument"); 566 if (!mat->ops.lufactornumeric) SETERRQ(PETSC_ERR_SUP,"MatLUFactorNumeric"); 567 if (!mat->assembled) SETERRQ(1,"MatLUFactorNumeric:Not for unassembled matrix"); 568 569 PLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0); 570 ierr = (*mat->ops.lufactornumeric)(mat,fact); CHKERRQ(ierr); 571 PLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0); 572 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 573 if (flg) { 574 Viewer viewer; 575 ierr = ViewerDrawOpenX((*fact)->comm,0,0,0,0,300,300,&viewer);CHKERRQ(ierr); 576 ierr = MatView(*fact,viewer); CHKERRQ(ierr); 577 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 578 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 579 } 580 return 0; 581 } 582 /*@ 583 MatCholeskyFactor - Performs in-place Cholesky factorization of a 584 symmetric matrix. 585 586 Input Parameters: 587 . mat - the matrix 588 . perm - row and column permutations 589 . f - expected fill as ratio of original fill 590 591 Notes: 592 See MatLUFactor() for the nonsymmetric case. See also 593 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 594 595 .keywords: matrix, factor, in-place, Cholesky 596 597 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 598 @*/ 599 int MatCholeskyFactor(Mat mat,IS perm,double f) 600 { 601 int ierr; 602 PetscValidHeaderSpecific(mat,MAT_COOKIE); 603 if (!mat->ops.choleskyfactor) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactor"); 604 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactor:Not for unassembled matrix"); 605 606 PLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0); 607 ierr = (*mat->ops.choleskyfactor)(mat,perm,f); CHKERRQ(ierr); 608 PLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0); 609 return 0; 610 } 611 /*@ 612 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 613 of a symmetric matrix. 614 615 Input Parameters: 616 . mat - the matrix 617 . perm - row and column permutations 618 . f - expected fill as ratio of original 619 620 Output Parameter: 621 . fact - the factored matrix 622 623 Notes: 624 See MatLUFactorSymbolic() for the nonsymmetric case. See also 625 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 626 627 .keywords: matrix, factor, factorization, symbolic, Cholesky 628 629 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 630 @*/ 631 int MatCholeskyFactorSymbolic(Mat mat,IS perm,double f,Mat *fact) 632 { 633 int ierr; 634 PetscValidHeaderSpecific(mat,MAT_COOKIE); 635 if (!fact) SETERRQ(1,"MatCholeskyFactorSymbolic:Missing factor matrix argument"); 636 if (!mat->ops.choleskyfactorsymbolic)SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorSymbolic"); 637 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorSymbolic:Not for unassembled matrix"); 638 639 PLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 640 ierr = (*mat->ops.choleskyfactorsymbolic)(mat,perm,f,fact); CHKERRQ(ierr); 641 PLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0); 642 return 0; 643 } 644 /*@ 645 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 646 of a symmetric matrix. Call this routine after first calling 647 MatCholeskyFactorSymbolic(). 648 649 Input Parameter: 650 . mat - the initial matrix 651 652 Output Parameter: 653 . fact - the factored matrix 654 655 .keywords: matrix, factor, numeric, Cholesky 656 657 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 658 @*/ 659 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 660 { 661 int ierr; 662 PetscValidHeaderSpecific(mat,MAT_COOKIE); 663 if (!fact) SETERRQ(1,"MatCholeskyFactorNumeric:Missing factor matrix argument"); 664 if (!mat->ops.choleskyfactornumeric) SETERRQ(PETSC_ERR_SUP,"MatCholeskyFactorNumeric"); 665 if (!mat->assembled) SETERRQ(1,"MatCholeskyFactorNumeric:Not for unassembled matrix"); 666 667 PLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 668 ierr = (*mat->ops.choleskyfactornumeric)(mat,fact); CHKERRQ(ierr); 669 PLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0); 670 return 0; 671 } 672 /* ----------------------------------------------------------------*/ 673 /*@ 674 MatSolve - Solves A x = b, given a factored matrix. 675 676 Input Parameters: 677 . mat - the factored matrix 678 . b - the right-hand-side vector 679 680 Output Parameter: 681 . x - the result vector 682 683 .keywords: matrix, linear system, solve, LU, Cholesky, triangular solve 684 685 .seealso: MatSolveAdd(), MatSolveTrans(), MatSolveTransAdd() 686 @*/ 687 int MatSolve(Mat mat,Vec b,Vec x) 688 { 689 int ierr; 690 PetscValidHeaderSpecific(mat,MAT_COOKIE); 691 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 692 if (x == b) SETERRQ(1,"MatSolve:x and y must be different vectors"); 693 if (!mat->factor) SETERRQ(1,"MatSolve:Unfactored matrix"); 694 695 if (!mat->ops.solve) SETERRQ(PETSC_ERR_SUP,"MatSolve"); 696 PLogEventBegin(MAT_Solve,mat,b,x,0); 697 ierr = (*mat->ops.solve)(mat,b,x); CHKERRQ(ierr); 698 PLogEventEnd(MAT_Solve,mat,b,x,0); 699 return 0; 700 } 701 702 /* @ 703 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 704 705 Input Parameters: 706 . mat - the factored matrix 707 . b - the right-hand-side vector 708 709 Output Parameter: 710 . x - the result vector 711 712 Notes: 713 MatSolve() should be used for most applications, as it performs 714 a forward solve followed by a backward solve. 715 716 .keywords: matrix, forward, LU, Cholesky, triangular solve 717 718 .seealso: MatSolve(), MatBackwardSolve() 719 @ */ 720 int MatForwardSolve(Mat mat,Vec b,Vec x) 721 { 722 int ierr; 723 PetscValidHeaderSpecific(mat,MAT_COOKIE); 724 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 725 if (x == b) SETERRQ(1,"MatForwardSolve:x and y must be different vectors"); 726 if (!mat->factor) SETERRQ(1,"MatForwardSolve:Unfactored matrix"); 727 if (!mat->ops.forwardsolve) SETERRQ(PETSC_ERR_SUP,"MatForwardSolve"); 728 729 PLogEventBegin(MAT_ForwardSolve,mat,b,x,0); 730 ierr = (*mat->ops.forwardsolve)(mat,b,x); CHKERRQ(ierr); 731 PLogEventEnd(MAT_ForwardSolve,mat,b,x,0); 732 return 0; 733 } 734 735 /* @ 736 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 737 738 Input Parameters: 739 . mat - the factored matrix 740 . b - the right-hand-side vector 741 742 Output Parameter: 743 . x - the result vector 744 745 Notes: 746 MatSolve() should be used for most applications, as it performs 747 a forward solve followed by a backward solve. 748 749 .keywords: matrix, backward, LU, Cholesky, triangular solve 750 751 .seealso: MatSolve(), MatForwardSolve() 752 @ */ 753 int MatBackwardSolve(Mat mat,Vec b,Vec x) 754 { 755 int ierr; 756 PetscValidHeaderSpecific(mat,MAT_COOKIE); 757 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 758 if (x == b) SETERRQ(1,"MatBackwardSolve:x and b must be different vectors"); 759 if (!mat->factor) SETERRQ(1,"MatBackwardSolve:Unfactored matrix"); 760 if (!mat->ops.backwardsolve) SETERRQ(PETSC_ERR_SUP,"MatBackwardSolve"); 761 762 PLogEventBegin(MAT_BackwardSolve,mat,b,x,0); 763 ierr = (*mat->ops.backwardsolve)(mat,b,x); CHKERRQ(ierr); 764 PLogEventEnd(MAT_BackwardSolve,mat,b,x,0); 765 return 0; 766 } 767 768 /*@ 769 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 770 771 Input Parameters: 772 . mat - the factored matrix 773 . b - the right-hand-side vector 774 . y - the vector to be added to 775 776 Output Parameter: 777 . x - the result vector 778 779 .keywords: matrix, linear system, solve, LU, Cholesky, add 780 781 .seealso: MatSolve(), MatSolveTrans(), MatSolveTransAdd() 782 @*/ 783 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 784 { 785 Scalar one = 1.0; 786 Vec tmp; 787 int ierr; 788 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 789 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 790 if (x == b) SETERRQ(1,"MatSolveAdd:x and b must be different vectors"); 791 if (!mat->factor) SETERRQ(1,"MatSolveAdd:Unfactored matrix"); 792 793 PLogEventBegin(MAT_SolveAdd,mat,b,x,y); 794 if (mat->ops.solveadd) { 795 ierr = (*mat->ops.solveadd)(mat,b,y,x); CHKERRQ(ierr); 796 } 797 else { 798 /* do the solve then the add manually */ 799 if (x != y) { 800 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 801 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 802 } 803 else { 804 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 805 PLogObjectParent(mat,tmp); 806 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 807 ierr = MatSolve(mat,b,x); CHKERRQ(ierr); 808 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 809 ierr = VecDestroy(tmp); CHKERRQ(ierr); 810 } 811 } 812 PLogEventEnd(MAT_SolveAdd,mat,b,x,y); 813 return 0; 814 } 815 /*@ 816 MatSolveTrans - Solves A' x = b, given a factored matrix. 817 818 Input Parameters: 819 . mat - the factored matrix 820 . b - the right-hand-side vector 821 822 Output Parameter: 823 . x - the result vector 824 825 .keywords: matrix, linear system, solve, LU, Cholesky, transpose 826 827 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransAdd() 828 @*/ 829 int MatSolveTrans(Mat mat,Vec b,Vec x) 830 { 831 int ierr; 832 PetscValidHeaderSpecific(mat,MAT_COOKIE); 833 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 834 if (!mat->factor) SETERRQ(1,"MatSolveTrans:Unfactored matrix"); 835 if (x == b) SETERRQ(1,"MatSolveTrans:x and b must be different vectors"); 836 if (!mat->ops.solvetrans) SETERRQ(PETSC_ERR_SUP,"MatSolveTrans"); 837 838 PLogEventBegin(MAT_SolveTrans,mat,b,x,0); 839 ierr = (*mat->ops.solvetrans)(mat,b,x); CHKERRQ(ierr); 840 PLogEventEnd(MAT_SolveTrans,mat,b,x,0); 841 return 0; 842 } 843 /*@ 844 MatSolveTransAdd - Computes x = y + inv(trans(A)) b, given a 845 factored matrix. 846 847 Input Parameters: 848 . mat - the factored matrix 849 . b - the right-hand-side vector 850 . y - the vector to be added to 851 852 Output Parameter: 853 . x - the result vector 854 855 .keywords: matrix, linear system, solve, LU, Cholesky, transpose, add 856 857 .seealso: MatSolve(), MatSolveAdd(), MatSolveTrans() 858 @*/ 859 int MatSolveTransAdd(Mat mat,Vec b,Vec y,Vec x) 860 { 861 Scalar one = 1.0; 862 int ierr; 863 Vec tmp; 864 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 865 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 866 if (x == b) SETERRQ(1,"MatSolveTransAdd:x and b must be different vectors"); 867 if (!mat->factor) SETERRQ(1,"MatSolveTransAdd:Unfactored matrix"); 868 869 PLogEventBegin(MAT_SolveTransAdd,mat,b,x,y); 870 if (mat->ops.solvetransadd) { 871 ierr = (*mat->ops.solvetransadd)(mat,b,y,x); CHKERRQ(ierr); 872 } 873 else { 874 /* do the solve then the add manually */ 875 if (x != y) { 876 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 877 ierr = VecAXPY(&one,y,x); CHKERRQ(ierr); 878 } 879 else { 880 ierr = VecDuplicate(x,&tmp); CHKERRQ(ierr); 881 PLogObjectParent(mat,tmp); 882 ierr = VecCopy(x,tmp); CHKERRQ(ierr); 883 ierr = MatSolveTrans(mat,b,x); CHKERRQ(ierr); 884 ierr = VecAXPY(&one,tmp,x); CHKERRQ(ierr); 885 ierr = VecDestroy(tmp); CHKERRQ(ierr); 886 } 887 } 888 PLogEventEnd(MAT_SolveTransAdd,mat,b,x,y); 889 return 0; 890 } 891 /* ----------------------------------------------------------------*/ 892 893 /*@ 894 MatRelax - Computes one relaxation sweep. 895 896 Input Parameters: 897 . mat - the matrix 898 . b - the right hand side 899 . omega - the relaxation factor 900 . flag - flag indicating the type of SOR, one of 901 $ SOR_FORWARD_SWEEP 902 $ SOR_BACKWARD_SWEEP 903 $ SOR_SYMMETRIC_SWEEP (SSOR method) 904 $ SOR_LOCAL_FORWARD_SWEEP 905 $ SOR_LOCAL_BACKWARD_SWEEP 906 $ SOR_LOCAL_SYMMETRIC_SWEEP (local SSOR) 907 $ SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 908 $ upper/lower triangular part of matrix to 909 $ vector (with omega) 910 $ SOR_ZERO_INITIAL_GUESS - zero initial guess 911 . shift - diagonal shift 912 . its - the number of iterations 913 914 Output Parameters: 915 . x - the solution (can contain an initial guess) 916 917 Notes: 918 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 919 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 920 on each processor. 921 922 Application programmers will not generally use MatRelax() directly, 923 but instead will employ the SLES/PC interface. 924 925 Notes for Advanced Users: 926 The flags are implemented as bitwise inclusive or operations. 927 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 928 to specify a zero initial guess for SSOR. 929 930 .keywords: matrix, relax, relaxation, sweep 931 @*/ 932 int MatRelax(Mat mat,Vec b,double omega,MatSORType flag,double shift, 933 int its,Vec x) 934 { 935 int ierr; 936 PetscValidHeaderSpecific(mat,MAT_COOKIE); 937 PetscValidHeaderSpecific(b,VEC_COOKIE); PetscValidHeaderSpecific(x,VEC_COOKIE); 938 if (!mat->ops.relax) SETERRQ(PETSC_ERR_SUP,"MatRelax"); 939 if (!mat->assembled) SETERRQ(1,"MatRelax:Not for unassembled matrix"); 940 941 PLogEventBegin(MAT_Relax,mat,b,x,0); 942 ierr =(*mat->ops.relax)(mat,b,omega,flag,shift,its,x); CHKERRQ(ierr); 943 PLogEventEnd(MAT_Relax,mat,b,x,0); 944 return 0; 945 } 946 947 /* 948 Default matrix copy routine. 949 */ 950 int MatCopy_Basic(Mat A,Mat B) 951 { 952 int ierr,i,rstart,rend,nz,*cwork; 953 Scalar *vwork; 954 955 ierr = MatZeroEntries(B); CHKERRQ(ierr); 956 ierr = MatGetOwnershipRange(A,&rstart,&rend); CHKERRQ(ierr); 957 for (i=rstart; i<rend; i++) { 958 ierr = MatGetRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 959 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES); CHKERRQ(ierr); 960 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork); CHKERRQ(ierr); 961 } 962 ierr = MatAssemblyBegin(B,FINAL_ASSEMBLY); CHKERRQ(ierr); 963 ierr = MatAssemblyEnd(B,FINAL_ASSEMBLY); CHKERRQ(ierr); 964 return 0; 965 } 966 967 /*@C 968 MatCopy - Copys a matrix to another matrix. 969 970 Input Parameters: 971 . A - the matrix 972 973 Output Parameter: 974 . B - where the copy is put 975 976 Notes: 977 MatCopy() copies the matrix entries of a matrix to another existing 978 matrix (after first zeroing the second matrix). A related routine is 979 MatConvert(), which first creates a new matrix and then copies the data. 980 981 .keywords: matrix, copy, convert 982 983 .seealso: MatConvert() 984 @*/ 985 int MatCopy(Mat A,Mat B) 986 { 987 int ierr; 988 PetscValidHeaderSpecific(A,MAT_COOKIE);PetscValidHeaderSpecific(B,MAT_COOKIE); 989 if (!A->assembled) SETERRQ(1,"MatCopy:Not for unassembled matrix"); 990 991 PLogEventBegin(MAT_Copy,A,B,0,0); 992 if (A->ops.copy) { 993 ierr = (*A->ops.copy)(A,B); CHKERRQ(ierr); 994 } 995 else { /* generic conversion */ 996 ierr = MatCopy_Basic(A,B); CHKERRQ(ierr); 997 } 998 PLogEventEnd(MAT_Copy,A,B,0,0); 999 return 0; 1000 } 1001 1002 /*@C 1003 MatConvert - Converts a matrix to another matrix, either of the same 1004 or different type. 1005 1006 Input Parameters: 1007 . mat - the matrix 1008 . newtype - new matrix type. Use MATSAME to create a new matrix of the 1009 same type as the original matrix. 1010 1011 Output Parameter: 1012 . M - pointer to place new matrix 1013 1014 Notes: 1015 MatConvert() first creates a new matrix and then copies the data from 1016 the first matrix. A related routine is MatCopy(), which copies the matrix 1017 entries of one matrix to another already existing matrix context. 1018 1019 .keywords: matrix, copy, convert 1020 1021 .seealso: MatCopy() 1022 @*/ 1023 int MatConvert(Mat mat,MatType newtype,Mat *M) 1024 { 1025 int ierr; 1026 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1027 if (!M) SETERRQ(1,"MatConvert:Bad new matrix address"); 1028 if (!mat->assembled) SETERRQ(1,"MatConvert:Not for unassembled matrix"); 1029 1030 PLogEventBegin(MAT_Convert,mat,0,0,0); 1031 if (newtype == mat->type || newtype == MATSAME) { 1032 if (mat->ops.convertsametype) { /* customized copy */ 1033 ierr = (*mat->ops.convertsametype)(mat,M,COPY_VALUES); CHKERRQ(ierr); 1034 } 1035 } 1036 else if (mat->ops.convert) { /* customized conversion */ 1037 ierr = (*mat->ops.convert)(mat,newtype,M); CHKERRQ(ierr); 1038 } 1039 else { /* generic conversion */ 1040 ierr = MatConvert_Basic(mat,newtype,M); CHKERRQ(ierr); 1041 } 1042 PLogEventEnd(MAT_Convert,mat,0,0,0); 1043 return 0; 1044 } 1045 1046 /*@ 1047 MatGetDiagonal - Gets the diagonal of a matrix. 1048 1049 Input Parameters: 1050 . mat - the matrix 1051 1052 Output Parameters: 1053 . v - the vector for storing the diagonal 1054 1055 .keywords: matrix, get, diagonal 1056 @*/ 1057 int MatGetDiagonal(Mat mat,Vec v) 1058 { 1059 PetscValidHeaderSpecific(mat,MAT_COOKIE);PetscValidHeaderSpecific(v,VEC_COOKIE); 1060 if (!mat->assembled) SETERRQ(1,"MatGetDiagonal:Not for unassembled matrix"); 1061 if (mat->ops.getdiagonal) return (*mat->ops.getdiagonal)(mat,v); 1062 SETERRQ(PETSC_ERR_SUP,"MatGetDiagonal"); 1063 } 1064 1065 /*@C 1066 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 1067 1068 Input Parameters: 1069 . mat - the matrix to transpose 1070 1071 Output Parameters: 1072 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 1073 1074 .keywords: matrix, transpose 1075 @*/ 1076 int MatTranspose(Mat mat,Mat *B) 1077 { 1078 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1079 if (!mat->assembled) SETERRQ(1,"MatTranspose:Not for unassembled matrix"); 1080 if (mat->ops.transpose) return (*mat->ops.transpose)(mat,B); 1081 SETERRQ(PETSC_ERR_SUP,"MatTranspose"); 1082 } 1083 1084 /*@ 1085 MatEqual - Compares two matrices. 1086 1087 Input Parameters: 1088 . mat1 - the first matrix 1089 . mat2 - the second matrix 1090 1091 Output Parameter: 1092 . flg : PETSC_TRUE if the matrices are equal; 1093 PETSC_FALSE otherwise. 1094 1095 .keywords: matrix, equal, equivalent 1096 @*/ 1097 int MatEqual(Mat mat1,Mat mat2,PetscTruth *flg) 1098 { 1099 PetscValidHeaderSpecific(mat1,MAT_COOKIE); PetscValidHeaderSpecific(mat2,MAT_COOKIE); 1100 if (!mat1->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1101 if (!mat2->assembled) SETERRQ(1,"MatEqual:Not for unassembled matrix"); 1102 if (mat1->ops.equal) return (*mat1->ops.equal)(mat1,mat2, flg); 1103 SETERRQ(PETSC_ERR_SUP,"MatEqual"); 1104 } 1105 1106 /*@ 1107 MatDiagonalScale - Scales a matrix on the left and right by diagonal 1108 matrices that are stored as vectors. Either of the two scaling 1109 matrices can be null. 1110 1111 Input Parameters: 1112 . mat - the matrix to be scaled 1113 . l - the left scaling vector 1114 . r - the right scaling vector 1115 1116 .keywords: matrix, scale 1117 @*/ 1118 int MatDiagonalScale(Mat mat,Vec l,Vec r) 1119 { 1120 int ierr; 1121 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1122 if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatDiagonalScale"); 1123 if (l) PetscValidHeaderSpecific(l,VEC_COOKIE); 1124 if (r) PetscValidHeaderSpecific(r,VEC_COOKIE); 1125 if (!mat->assembled) SETERRQ(1,"MatDiagonalScale:Not for unassembled matrix"); 1126 1127 PLogEventBegin(MAT_Scale,mat,0,0,0); 1128 ierr = (*mat->ops.diagonalscale)(mat,l,r); CHKERRQ(ierr); 1129 PLogEventEnd(MAT_Scale,mat,0,0,0); 1130 return 0; 1131 } 1132 1133 /*@ 1134 MatScale - Scales a matrix by a number. 1135 1136 Input Parameters: 1137 . mat - the matrix to be scaled 1138 . a - the number 1139 1140 Note: the name of this routine MUST change. 1141 .keywords: matrix, scale 1142 @*/ 1143 int MatScale(Scalar *a,Mat mat) 1144 { 1145 int ierr; 1146 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1147 if (!mat->ops.scale) SETERRQ(PETSC_ERR_SUP,"MatScale"); 1148 if (!mat->assembled) SETERRQ(1,"MatScale:Not for unassembled matrix"); 1149 1150 PLogEventBegin(MAT_Scale,mat,0,0,0); 1151 ierr = (*mat->ops.scale)(a,mat); CHKERRQ(ierr); 1152 PLogEventEnd(MAT_Scale,mat,0,0,0); 1153 return 0; 1154 } 1155 1156 /*@ 1157 MatNorm - Calculates various norms of a matrix. 1158 1159 Input Parameters: 1160 . mat - the matrix 1161 . type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY 1162 1163 Output Parameters: 1164 . norm - the resulting norm 1165 1166 .keywords: matrix, norm, Frobenius 1167 @*/ 1168 int MatNorm(Mat mat,NormType type,double *norm) 1169 { 1170 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1171 if (!norm) SETERRQ(1,"MatNorm:bad addess for value"); 1172 if (!mat->assembled) SETERRQ(1,"MatNorm:Not for unassembled matrix"); 1173 if (mat->ops.norm) return (*mat->ops.norm)(mat,type,norm); 1174 SETERRQ(PETSC_ERR_SUP,"MatNorm:Not for this matrix type"); 1175 } 1176 1177 /*@ 1178 MatAssemblyBegin - Begins assembling the matrix. This routine should 1179 be called after completing all calls to MatSetValues(). 1180 1181 Input Parameters: 1182 . mat - the matrix 1183 . type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY 1184 1185 Notes: 1186 MatSetValues() generally caches the values. The matrix is ready to 1187 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1188 Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use 1189 FINAL_ASSEMBLY for the final assembly before the matrix is used. 1190 1191 .keywords: matrix, assembly, assemble, begin 1192 1193 .seealso: MatAssemblyEnd(), MatSetValues() 1194 @*/ 1195 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 1196 { 1197 int ierr; 1198 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1199 PLogEventBegin(MAT_AssemblyBegin,mat,0,0,0); 1200 if (mat->ops.assemblybegin){ierr = (*mat->ops.assemblybegin)(mat,type);CHKERRQ(ierr);} 1201 PLogEventEnd(MAT_AssemblyBegin,mat,0,0,0); 1202 return 0; 1203 } 1204 1205 /*@ 1206 MatAssemblyEnd - Completes assembling the matrix. This routine should 1207 be called after all calls to MatSetValues() and after MatAssemblyBegin(). 1208 1209 Input Parameters: 1210 . mat - the matrix 1211 . type - type of assembly, either FLUSH_ASSEMBLY or FINAL_ASSEMBLY 1212 1213 Options Database Keys: 1214 $ -mat_view_draw : Draw nonzero structure of matrix at conclusion of MatEndAssembly(), 1215 using MatView() and DrawOpenX(). 1216 $ -mat_view_info : Prints info on matrix. 1217 $ -mat_view_info_detailed: More detailed information. 1218 $ -mat_view : Prints matrix out in ascii. 1219 $ -mat_view_matlab : Prints matrix out suitable for Matlab(TM). 1220 $ -display <name> : Set display name (default is host) 1221 $ -draw_pause <sec> : Set number of seconds to pause after display 1222 1223 Note: 1224 MatSetValues() generally caches the values. The matrix is ready to 1225 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 1226 Use FLUSH_ASSEMBLY when switching between ADD_VALUES and SetValues; use 1227 FINAL_ASSEMBLY for the final assembly before the matrix is used. 1228 1229 .keywords: matrix, assembly, assemble, end 1230 1231 .seealso: MatAssemblyBegin(), MatSetValues() 1232 @*/ 1233 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 1234 { 1235 int ierr,flg; 1236 static int inassm = 0; 1237 1238 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1239 inassm++; 1240 PLogEventBegin(MAT_AssemblyEnd,mat,0,0,0); 1241 if (mat->ops.assemblyend) {ierr = (*mat->ops.assemblyend)(mat,type); CHKERRQ(ierr);} 1242 mat->assembled = PETSC_TRUE; mat->num_ass++; 1243 PLogEventEnd(MAT_AssemblyEnd,mat,0,0,0); 1244 1245 if (inassm == 1) { 1246 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info",&flg); CHKERRQ(ierr); 1247 if (flg) { 1248 Viewer viewer; 1249 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1250 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO,0);CHKERRQ(ierr); 1251 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1252 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1253 } 1254 ierr = OptionsHasName(PETSC_NULL,"-mat_view_info_detailed",&flg);CHKERRQ(ierr); 1255 if (flg) { 1256 Viewer viewer; 1257 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1258 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_INFO_DETAILED,0);CHKERRQ(ierr); 1259 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1260 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1261 } 1262 ierr = OptionsHasName(PETSC_NULL,"-mat_view",&flg); CHKERRQ(ierr); 1263 if (flg) { 1264 Viewer viewer; 1265 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1266 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1267 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1268 } 1269 ierr = OptionsHasName(PETSC_NULL,"-mat_view_matlab",&flg); CHKERRQ(ierr); 1270 if (flg) { 1271 Viewer viewer; 1272 ierr = ViewerFileOpenASCII(mat->comm,"stdout",&viewer);CHKERRQ(ierr); 1273 ierr = ViewerSetFormat(viewer,ASCII_FORMAT_MATLAB,"M");CHKERRQ(ierr); 1274 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1275 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1276 } 1277 ierr = OptionsHasName(PETSC_NULL,"-mat_view_draw",&flg); CHKERRQ(ierr); 1278 if (flg) { 1279 Viewer viewer; 1280 ierr = ViewerDrawOpenX(mat->comm,0,0,0,0,300,300,&viewer); CHKERRQ(ierr); 1281 ierr = MatView(mat,viewer); CHKERRQ(ierr); 1282 ierr = ViewerFlush(viewer); CHKERRQ(ierr); 1283 ierr = ViewerDestroy(viewer); CHKERRQ(ierr); 1284 } 1285 } 1286 inassm--; 1287 return 0; 1288 } 1289 1290 /*@ 1291 MatCompress - Tries to store the matrix in as little space as 1292 possible. May fail if memory is already fully used, since it 1293 tries to allocate new space. 1294 1295 Input Parameters: 1296 . mat - the matrix 1297 1298 .keywords: matrix, compress 1299 @*/ 1300 int MatCompress(Mat mat) 1301 { 1302 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1303 if (mat->ops.compress) return (*mat->ops.compress)(mat); 1304 return 0; 1305 } 1306 /*@ 1307 MatSetOption - Sets a parameter option for a matrix. Some options 1308 may be specific to certain storage formats. Some options 1309 determine how values will be inserted (or added). Sorted, 1310 row-oriented input will generally assemble the fastest. The default 1311 is row-oriented, nonsorted input. 1312 1313 Input Parameters: 1314 . mat - the matrix 1315 . option - the option, one of the following: 1316 $ ROW_ORIENTED 1317 $ COLUMN_ORIENTED, 1318 $ ROWS_SORTED, 1319 $ COLUMNS_SORTED, 1320 $ NO_NEW_NONZERO_LOCATIONS, 1321 $ YES_NEW_NONZERO_LOCATIONS, 1322 $ SYMMETRIC_MATRIX, 1323 $ STRUCTURALLY_SYMMETRIC_MATRIX, 1324 $ NO_NEW_DIAGONALS, 1325 $ YES_NEW_DIAGONALS, 1326 $ and possibly others. 1327 1328 Notes: 1329 Some options are relevant only for particular matrix types and 1330 are thus ignored by others. Other options are not supported by 1331 certain matrix types and will generate an error message if set. 1332 1333 If using a Fortran 77 module to compute a matrix, one may need to 1334 use the column-oriented option (or convert to the row-oriented 1335 format). 1336 1337 NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 1338 that will generate a new entry in the nonzero structure is ignored. 1339 What this means is if memory is not allocated for this particular 1340 lot, then the insertion is ignored. For dense matrices, where 1341 the entire array is allocated, no entries are ever ignored. 1342 1343 .keywords: matrix, option, row-oriented, column-oriented, sorted, nonzero 1344 @*/ 1345 int MatSetOption(Mat mat,MatOption op) 1346 { 1347 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1348 if (mat->ops.setoption) return (*mat->ops.setoption)(mat,op); 1349 return 0; 1350 } 1351 1352 /*@ 1353 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 1354 this routine retains the old nonzero structure. 1355 1356 Input Parameters: 1357 . mat - the matrix 1358 1359 .keywords: matrix, zero, entries 1360 1361 .seealso: MatZeroRows() 1362 @*/ 1363 int MatZeroEntries(Mat mat) 1364 { 1365 int ierr; 1366 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1367 if (!mat->ops.zeroentries) SETERRQ(PETSC_ERR_SUP,"MatZeroEntries"); 1368 1369 PLogEventBegin(MAT_ZeroEntries,mat,0,0,0); 1370 ierr = (*mat->ops.zeroentries)(mat); CHKERRQ(ierr); 1371 PLogEventEnd(MAT_ZeroEntries,mat,0,0,0); 1372 return 0; 1373 } 1374 1375 /*@ 1376 MatZeroRows - Zeros all entries (except possibly the main diagonal) 1377 of a set of rows of a matrix. 1378 1379 Input Parameters: 1380 . mat - the matrix 1381 . is - index set of rows to remove 1382 . diag - pointer to value put in all diagonals of eliminated rows. 1383 Note that diag is not a pointer to an array, but merely a 1384 pointer to a single value. 1385 1386 Notes: 1387 For the AIJ matrix formats this removes the old nonzero structure, 1388 but does not release memory. For the dense and block diagonal 1389 formats this does not alter the nonzero structure. 1390 1391 The user can set a value in the diagonal entry (or for the AIJ and 1392 row formats can optionally remove the main diagonal entry from the 1393 nonzero structure as well, by passing a null pointer as the final 1394 argument). 1395 1396 .keywords: matrix, zero, rows, boundary conditions 1397 1398 .seealso: MatZeroEntries(), MatGetSubMatrix(), MatGetSubMatrixInPlace() 1399 @*/ 1400 int MatZeroRows(Mat mat,IS is, Scalar *diag) 1401 { 1402 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1403 if (!mat->assembled) SETERRQ(1,"MatZeroRows:Not for unassembled matrix"); 1404 if (mat->ops.zerorows) return (*mat->ops.zerorows)(mat,is,diag); 1405 SETERRQ(PETSC_ERR_SUP,"MatZeroRows"); 1406 } 1407 1408 /*@ 1409 MatGetSize - Returns the numbers of rows and columns in a matrix. 1410 1411 Input Parameter: 1412 . mat - the matrix 1413 1414 Output Parameters: 1415 . m - the number of global rows 1416 . n - the number of global columns 1417 1418 .keywords: matrix, dimension, size, rows, columns, global, get 1419 1420 .seealso: MatGetLocalSize() 1421 @*/ 1422 int MatGetSize(Mat mat,int *m,int* n) 1423 { 1424 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1425 if (!m || !n) SETERRQ(1,"MatGetSize:Bad address for result"); 1426 return (*mat->ops.getsize)(mat,m,n); 1427 } 1428 1429 /*@ 1430 MatGetLocalSize - Returns the number of rows and columns in a matrix 1431 stored locally. This information may be implementation dependent, so 1432 use with care. 1433 1434 Input Parameters: 1435 . mat - the matrix 1436 1437 Output Parameters: 1438 . m - the number of local rows 1439 . n - the number of local columns 1440 1441 .keywords: matrix, dimension, size, local, rows, columns, get 1442 1443 .seealso: MatGetSize() 1444 @*/ 1445 int MatGetLocalSize(Mat mat,int *m,int* n) 1446 { 1447 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1448 if (!m || !n) SETERRQ(1,"MatGetLocalSize:Bad address for result"); 1449 return (*mat->ops.getlocalsize)(mat,m,n); 1450 } 1451 1452 /*@ 1453 MatGetOwnershipRange - Returns the range of matrix rows owned by 1454 this processor, assuming that the matrix is laid out with the first 1455 n1 rows on the first processor, the next n2 rows on the second, etc. 1456 For certain parallel layouts this range may not be well-defined. 1457 1458 Input Parameters: 1459 . mat - the matrix 1460 1461 Output Parameters: 1462 . m - the first local row 1463 . n - one more then the last local row 1464 1465 .keywords: matrix, get, range, ownership 1466 @*/ 1467 int MatGetOwnershipRange(Mat mat,int *m,int* n) 1468 { 1469 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1470 if (!m || !n) SETERRQ(1,"MatGetOwnershipRange:Bad address for result"); 1471 if (mat->ops.getownershiprange) return (*mat->ops.getownershiprange)(mat,m,n); 1472 SETERRQ(PETSC_ERR_SUP,"MatGetOwnershipRange"); 1473 } 1474 1475 /*@ 1476 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 1477 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 1478 to complete the factorization. 1479 1480 Input Parameters: 1481 . mat - the matrix 1482 . row - row permutation 1483 . column - column permutation 1484 . fill - number of levels of fill 1485 . f - expected fill as ratio of the original number of nonzeros, 1486 for example 3.0; choosing this parameter well can result in 1487 more efficient use of time and space. 1488 1489 Output Parameters: 1490 . fact - new matrix that has been symbolically factored 1491 1492 Options Database Key: 1493 $ -mat_ilu_fill <f>, where f is the fill ratio 1494 1495 Notes: 1496 See the file $(PETSC_DIR)/Performace for additional information about 1497 choosing the fill factor for better efficiency. 1498 1499 .keywords: matrix, factor, incomplete, ILU, symbolic, fill 1500 1501 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric() 1502 @*/ 1503 int MatILUFactorSymbolic(Mat mat,IS row,IS col,double f,int fill,Mat *fact) 1504 { 1505 int ierr,flg; 1506 1507 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1508 if (fill < 0) SETERRQ(1,"MatILUFactorSymbolic:Levels of fill negative"); 1509 if (!fact) SETERRQ(1,"MatILUFactorSymbolic:Fact argument is missing"); 1510 if (!mat->ops.ilufactorsymbolic) SETERRQ(PETSC_ERR_SUP,"MatILUFactorSymbolic"); 1511 if (!mat->assembled) SETERRQ(1,"MatILUFactorSymbolic:Not for unassembled matrix"); 1512 1513 ierr = OptionsGetDouble(PETSC_NULL,"-mat_ilu_fill",&f,&flg); CHKERRQ(ierr); 1514 PLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0); 1515 ierr = (*mat->ops.ilufactorsymbolic)(mat,row,col,f,fill,fact); CHKERRQ(ierr); 1516 PLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0); 1517 return 0; 1518 } 1519 1520 /*@ 1521 MatIncompleteCholeskyFactorSymbolic - Performs symbolic incomplete 1522 Cholesky factorization for a symmetric matrix. Use 1523 MatCholeskyFactorNumeric() to complete the factorization. 1524 1525 Input Parameters: 1526 . mat - the matrix 1527 . perm - row and column permutation 1528 . fill - levels of fill 1529 . f - expected fill as ratio of original fill 1530 1531 Output Parameter: 1532 . fact - the factored matrix 1533 1534 Note: Currently only no-fill factorization is supported. 1535 1536 .keywords: matrix, factor, incomplete, ICC, Cholesky, symbolic, fill 1537 1538 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor() 1539 @*/ 1540 int MatIncompleteCholeskyFactorSymbolic(Mat mat,IS perm,double f,int fill, 1541 Mat *fact) 1542 { 1543 int ierr; 1544 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1545 if (fill < 0) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Fill negative"); 1546 if (!fact) SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Missing fact argument"); 1547 if (!mat->ops.incompletecholeskyfactorsymbolic) 1548 SETERRQ(PETSC_ERR_SUP,"MatIncompleteCholeskyFactorSymbolic"); 1549 if (!mat->assembled) 1550 SETERRQ(1,"MatIncompleteCholeskyFactorSymbolic:Not for unassembled matrix"); 1551 1552 PLogEventBegin(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1553 ierr = (*mat->ops.incompletecholeskyfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr); 1554 PLogEventEnd(MAT_IncompleteCholeskyFactorSymbolic,mat,perm,0,0); 1555 return 0; 1556 } 1557 1558 /*@C 1559 MatGetArray - Returns a pointer to the element values in the matrix. 1560 This routine is implementation dependent, and may not even work for 1561 certain matrix types. You MUST call MatRestoreArray() when you no 1562 longer need to access the array. 1563 1564 Input Parameter: 1565 . mat - the matrix 1566 1567 Output Parameter: 1568 . v - the location of the values 1569 1570 Fortran Note: 1571 The Fortran interface is slightly different from that given below. 1572 See the users manual and petsc/src/mat/examples for details. 1573 1574 .keywords: matrix, array, elements, values 1575 1576 .seeaols: MatRestoreArray() 1577 @*/ 1578 int MatGetArray(Mat mat,Scalar **v) 1579 { 1580 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1581 if (!v) SETERRQ(1,"MatGetArray:Bad input, array pointer location"); 1582 if (!mat->ops.getarray) SETERRQ(PETSC_ERR_SUP,"MatGetArray"); 1583 return (*mat->ops.getarray)(mat,v); 1584 } 1585 1586 /*@C 1587 MatRestoreArray - Restores the matrix after MatGetArray has been called. 1588 1589 Input Parameter: 1590 . mat - the matrix 1591 . v - the location of the values 1592 1593 Fortran Note: 1594 The Fortran interface is slightly different from that given below. 1595 See the users manual and petsc/src/mat/examples for details. 1596 1597 .keywords: matrix, array, elements, values, resrore 1598 1599 .seealso: MatGetArray() 1600 @*/ 1601 int MatRestoreArray(Mat mat,Scalar **v) 1602 { 1603 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1604 if (!v) SETERRQ(1,"MatRestoreArray:Bad input, array pointer location"); 1605 if (!mat->ops.restorearray) SETERRQ(PETSC_ERR_SUP,"MatResroreArray"); 1606 return (*mat->ops.restorearray)(mat,v); 1607 } 1608 1609 /*@C 1610 MatGetSubMatrix - Extracts a submatrix from a matrix. If submat points 1611 to a valid matrix, it may be reused. 1612 1613 Input Parameters: 1614 . mat - the matrix 1615 . irow, icol - index sets of rows and columns to extract 1616 . scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 1617 1618 Output Parameter: 1619 . submat - the submatrix 1620 1621 Notes: 1622 MatGetSubMatrix() can be useful in setting boundary conditions. 1623 1624 Use MatGetSubMatrices() to extract multiple submatrices. 1625 1626 .keywords: matrix, get, submatrix, boundary conditions 1627 1628 .seealso: MatZeroRows(), MatGetSubMatrixInPlace(), MatGetSubMatrices() 1629 @*/ 1630 int MatGetSubMatrix(Mat mat,IS irow,IS icol,MatGetSubMatrixCall scall,Mat *submat) 1631 { 1632 int ierr; 1633 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1634 if (scall == MAT_REUSE_MATRIX) { 1635 PetscValidHeaderSpecific(*submat,MAT_COOKIE); 1636 } 1637 if (!mat->ops.getsubmatrix) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrix"); 1638 if (!mat->assembled) SETERRQ(1,"MatGetSubMatrix:Not for unassembled matrix"); 1639 1640 /* PLogEventBegin(MAT_GetSubMatrix,mat,irow,icol,0); */ 1641 ierr = (*mat->ops.getsubmatrix)(mat,irow,icol,scall,submat); CHKERRQ(ierr); 1642 /* PLogEventEnd(MAT_GetSubMatrix,mat,irow,icol,0); */ 1643 return 0; 1644 } 1645 1646 /*@C 1647 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 1648 points to an array of valid matrices, it may be reused. 1649 1650 Input Parameters: 1651 . mat - the matrix 1652 . irow, icol - index sets of rows and columns to extract 1653 1654 Output Parameter: 1655 . submat - the submatrices 1656 1657 Note: 1658 Use MatGetSubMatrix() for extracting a sinble submatrix. 1659 1660 .keywords: matrix, get, submatrix, submatrices 1661 1662 .seealso: MatGetSubMatrix() 1663 @*/ 1664 int MatGetSubMatrices(Mat mat,int n, IS *irow,IS *icol,MatGetSubMatrixCall scall, 1665 Mat **submat) 1666 { 1667 int ierr; 1668 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1669 if (!mat->ops.getsubmatrices) SETERRQ(PETSC_ERR_SUP,"MatGetSubMatrices"); 1670 if (!mat->assembled) SETERRQ(1,"MatGetSubMatrices:Not for unassembled matrix"); 1671 1672 PLogEventBegin(MAT_GetSubMatrices,mat,0,0,0); 1673 ierr = (*mat->ops.getsubmatrices)(mat,n,irow,icol,scall,submat); CHKERRQ(ierr); 1674 PLogEventEnd(MAT_GetSubMatrices,mat,0,0,0); 1675 return 0; 1676 } 1677 1678 /*@ 1679 MatGetSubMatrixInPlace - Extracts a submatrix from a matrix, returning 1680 the submatrix in place of the original matrix. 1681 1682 Input Parameters: 1683 . mat - the matrix 1684 . irow, icol - index sets of rows and columns to extract 1685 1686 .keywords: matrix, get, submatrix, boundary conditions, in-place 1687 1688 .seealso: MatZeroRows(), MatGetSubMatrix() 1689 @*/ 1690 int MatGetSubMatrixInPlace(Mat mat,IS irow,IS icol) 1691 { 1692 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1693 if (!mat->assembled) SETERRQ(1,"MatGetSubMatrixInPlace:Not for unassembled matrix"); 1694 1695 if (!mat->ops.getsubmatrixinplace) SETERRQ(PETSC_ERR_SUP,"MatGetSubmatrixInPlace"); 1696 return (*mat->ops.getsubmatrixinplace)(mat,irow,icol); 1697 } 1698 1699 /*@ 1700 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 1701 replaces the index by larger ones that represent submatrices with more 1702 overlap. 1703 1704 Input Parameters: 1705 . mat - the matrix 1706 . n - the number of index sets 1707 . is - the array of pointers to index sets 1708 . ov - the additional overlap requested 1709 1710 .keywords: matrix, overlap, Schwarz 1711 1712 .seealso: MatGetSubMatrices() 1713 @*/ 1714 int MatIncreaseOverlap(Mat mat,int n, IS *is, int ov) 1715 { 1716 int ierr; 1717 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1718 if (!mat->assembled) SETERRQ(1,"MatIncreaseOverlap:Not for unassembled matrix"); 1719 1720 if (ov == 0) return 0; 1721 if (!mat->ops.increaseoverlap) SETERRQ(PETSC_ERR_SUP,"MatIncreaseOverlap"); 1722 PLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0); 1723 ierr = (*mat->ops.increaseoverlap)(mat,n,is,ov); CHKERRQ(ierr); 1724 PLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0); 1725 return 0; 1726 } 1727 1728 /*@ 1729 MatPrintHelp - Prints all the options for the matrix. 1730 1731 Input Parameter: 1732 . mat - the matrix 1733 1734 Options Database Keys: 1735 $ -help, -h 1736 1737 .keywords: mat, help 1738 1739 .seealso: MatCreate(), MatCreateXXX() 1740 @*/ 1741 int MatPrintHelp(Mat mat) 1742 { 1743 static int called = 0; 1744 MPI_Comm comm = mat->comm; 1745 1746 if (!called) { 1747 PetscPrintf(comm,"General matrix options:\n"); 1748 PetscPrintf(comm," -mat_view_info : view basic matrix info during MatAssemblyEnd()\n"); 1749 PetscPrintf(comm," -mat_view_info_detailed : view detailed matrix info during MatAssemblyEnd()\n"); 1750 PetscPrintf(comm," -mat_view_draw : draw nonzero matrix structure during MatAssemblyEnd()\n"); 1751 PetscPrintf(comm," -draw_pause <sec> : set seconds of display pause\n"); 1752 PetscPrintf(comm," -display <name> : set alternate display\n"); 1753 called = 1; 1754 } 1755 if (mat->ops.printhelp) (*mat->ops.printhelp)(mat); 1756 return 0; 1757 } 1758 1759