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