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