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