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