1 /*$Id: matrix.c,v 1.414 2001/09/28 18:57:28 balay Exp $*/ 2 3 /* 4 This is where the abstract matrix operations are defined 5 */ 6 7 #include "src/mat/matimpl.h" /*I "petscmat.h" I*/ 8 #include "src/vec/vecimpl.h" 9 10 /* Logging support */ 11 int MAT_COOKIE; 12 int MATSNESMFCTX_COOKIE; 13 int MAT_Mult, MAT_MultMatrixFree, MAT_Mults, MAT_MultConstrained, MAT_MultAdd, MAT_MultTranspose; 14 int MAT_MultTransposeConstrained, MAT_MultTransposeAdd, MAT_Solve, MAT_Solves, MAT_SolveAdd, MAT_SolveTranspose; 15 int MAT_SolveTransposeAdd, MAT_Relax, MAT_ForwardSolve, MAT_BackwardSolve, MAT_LUFactor, MAT_LUFactorSymbolic; 16 int MAT_LUFactorNumeric, MAT_CholeskyFactor, MAT_CholeskyFactorSymbolic, MAT_CholeskyFactorNumeric, MAT_ILUFactor; 17 int MAT_ILUFactorSymbolic, MAT_ICCFactorSymbolic, MAT_Copy, MAT_Convert, MAT_Scale, MAT_AssemblyBegin; 18 int MAT_AssemblyEnd, MAT_SetValues, MAT_GetValues, MAT_GetRow, MAT_GetSubMatrices, MAT_GetColoring, MAT_GetOrdering; 19 int MAT_IncreaseOverlap, MAT_Partitioning, MAT_ZeroEntries, MAT_Load, MAT_View, MAT_AXPY, MAT_FDColoringCreate; 20 int MAT_FDColoringApply,MAT_Transpose,MAT_FDColoringFunction; 21 22 #undef __FUNCT__ 23 #define __FUNCT__ "MatGetRow" 24 /*@C 25 MatGetRow - Gets a row of a matrix. You MUST call MatRestoreRow() 26 for each row that you get to ensure that your application does 27 not bleed memory. 28 29 Not Collective 30 31 Input Parameters: 32 + mat - the matrix 33 - row - the row to get 34 35 Output Parameters: 36 + ncols - if not NULL, the number of nonzeros in the row 37 . cols - if not NULL, the column numbers 38 - vals - if not NULL, the values 39 40 Notes: 41 This routine is provided for people who need to have direct access 42 to the structure of a matrix. We hope that we provide enough 43 high-level matrix routines that few users will need it. 44 45 MatGetRow() always returns 0-based column indices, regardless of 46 whether the internal representation is 0-based (default) or 1-based. 47 48 For better efficiency, set cols and/or vals to PETSC_NULL if you do 49 not wish to extract these quantities. 50 51 The user can only examine the values extracted with MatGetRow(); 52 the values cannot be altered. To change the matrix entries, one 53 must use MatSetValues(). 54 55 You can only have one call to MatGetRow() outstanding for a particular 56 matrix at a time, per processor. MatGetRow() can only obtained rows 57 associated with the given processor, it cannot get rows from the 58 other processors; for that we suggest using MatGetSubMatrices(), then 59 MatGetRow() on the submatrix. The row indix passed to MatGetRows() 60 is in the global number of rows. 61 62 Fortran Notes: 63 The calling sequence from Fortran is 64 .vb 65 MatGetRow(matrix,row,ncols,cols,values,ierr) 66 Mat matrix (input) 67 integer row (input) 68 integer ncols (output) 69 integer cols(maxcols) (output) 70 double precision (or double complex) values(maxcols) output 71 .ve 72 where maxcols >= maximum nonzeros in any row of the matrix. 73 74 Caution: 75 Do not try to change the contents of the output arrays (cols and vals). 76 In some cases, this may corrupt the matrix. 77 78 Level: advanced 79 80 Concepts: matrices^row access 81 82 .seealso: MatRestoreRow(), MatSetValues(), MatGetValues(), MatGetSubmatrices(), MatGetDiagonal() 83 @*/ 84 int MatGetRow(Mat mat,int row,int *ncols,int *cols[],PetscScalar *vals[]) 85 { 86 int incols,ierr; 87 88 PetscFunctionBegin; 89 PetscValidHeaderSpecific(mat,MAT_COOKIE); 90 PetscValidType(mat); 91 MatPreallocated(mat); 92 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 93 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 94 if (!mat->ops->getrow) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 95 ierr = PetscLogEventBegin(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 96 ierr = (*mat->ops->getrow)(mat,row,&incols,cols,vals);CHKERRQ(ierr); 97 if (ncols) *ncols = incols; 98 ierr = PetscLogEventEnd(MAT_GetRow,mat,0,0,0);CHKERRQ(ierr); 99 PetscFunctionReturn(0); 100 } 101 102 #undef __FUNCT__ 103 #define __FUNCT__ "MatRestoreRow" 104 /*@C 105 MatRestoreRow - Frees any temporary space allocated by MatGetRow(). 106 107 Not Collective 108 109 Input Parameters: 110 + mat - the matrix 111 . row - the row to get 112 . ncols, cols - the number of nonzeros and their columns 113 - vals - if nonzero the column values 114 115 Notes: 116 This routine should be called after you have finished examining the entries. 117 118 Fortran Notes: 119 The calling sequence from Fortran is 120 .vb 121 MatRestoreRow(matrix,row,ncols,cols,values,ierr) 122 Mat matrix (input) 123 integer row (input) 124 integer ncols (output) 125 integer cols(maxcols) (output) 126 double precision (or double complex) values(maxcols) output 127 .ve 128 Where maxcols >= maximum nonzeros in any row of the matrix. 129 130 In Fortran MatRestoreRow() MUST be called after MatGetRow() 131 before another call to MatGetRow() can be made. 132 133 Level: advanced 134 135 .seealso: MatGetRow() 136 @*/ 137 int MatRestoreRow(Mat mat,int row,int *ncols,int *cols[],PetscScalar *vals[]) 138 { 139 int ierr; 140 141 PetscFunctionBegin; 142 PetscValidHeaderSpecific(mat,MAT_COOKIE); 143 PetscValidIntPointer(ncols); 144 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 145 if (!mat->ops->restorerow) PetscFunctionReturn(0); 146 ierr = (*mat->ops->restorerow)(mat,row,ncols,cols,vals);CHKERRQ(ierr); 147 PetscFunctionReturn(0); 148 } 149 150 #undef __FUNCT__ 151 #define __FUNCT__ "MatView" 152 /*@C 153 MatView - Visualizes a matrix object. 154 155 Collective on Mat 156 157 Input Parameters: 158 + mat - the matrix 159 - viewer - visualization context 160 161 Notes: 162 The available visualization contexts include 163 + PETSC_VIEWER_STDOUT_SELF - standard output (default) 164 . PETSC_VIEWER_STDOUT_WORLD - synchronized standard 165 output where only the first processor opens 166 the file. All other processors send their 167 data to the first processor to print. 168 - PETSC_VIEWER_DRAW_WORLD - graphical display of nonzero structure 169 170 The user can open alternative visualization contexts with 171 + PetscViewerASCIIOpen() - Outputs matrix to a specified file 172 . PetscViewerBinaryOpen() - Outputs matrix in binary to a 173 specified file; corresponding input uses MatLoad() 174 . PetscViewerDrawOpen() - Outputs nonzero matrix structure to 175 an X window display 176 - PetscViewerSocketOpen() - Outputs matrix to Socket viewer. 177 Currently only the sequential dense and AIJ 178 matrix types support the Socket viewer. 179 180 The user can call PetscViewerSetFormat() to specify the output 181 format of ASCII printed objects (when using PETSC_VIEWER_STDOUT_SELF, 182 PETSC_VIEWER_STDOUT_WORLD and PetscViewerASCIIOpen). Available formats include 183 + PETSC_VIEWER_ASCII_DEFAULT - default, prints matrix contents 184 . PETSC_VIEWER_ASCII_MATLAB - prints matrix contents in Matlab format 185 . PETSC_VIEWER_ASCII_DENSE - prints entire matrix including zeros 186 . PETSC_VIEWER_ASCII_COMMON - prints matrix contents, using a sparse 187 format common among all matrix types 188 . PETSC_VIEWER_ASCII_IMPL - prints matrix contents, using an implementation-specific 189 format (which is in many cases the same as the default) 190 . PETSC_VIEWER_ASCII_INFO - prints basic information about the matrix 191 size and structure (not the matrix entries) 192 . PETSC_VIEWER_ASCII_INFO_DETAIL - prints more detailed information about 193 the matrix structure 194 195 Options Database Keys: 196 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 197 . -mat_view_info_detailed - Prints more detailed info 198 . -mat_view - Prints matrix in ASCII format 199 . -mat_view_matlab - Prints matrix in Matlab format 200 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 201 . -display <name> - Sets display name (default is host) 202 . -draw_pause <sec> - Sets number of seconds to pause after display 203 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 204 . -viewer_socket_machine <machine> 205 . -viewer_socket_port <port> 206 . -mat_view_binary - save matrix to file in binary format 207 - -viewer_binary_filename <name> 208 Level: beginner 209 210 Concepts: matrices^viewing 211 Concepts: matrices^plotting 212 Concepts: matrices^printing 213 214 .seealso: PetscViewerSetFormat(), PetscViewerASCIIOpen(), PetscViewerDrawOpen(), 215 PetscViewerSocketOpen(), PetscViewerBinaryOpen(), MatLoad() 216 @*/ 217 int MatView(Mat mat,PetscViewer viewer) 218 { 219 int ierr,rows,cols; 220 PetscTruth isascii; 221 char *cstr; 222 PetscViewerFormat format; 223 224 PetscFunctionBegin; 225 PetscValidHeaderSpecific(mat,MAT_COOKIE); 226 PetscValidType(mat); 227 MatPreallocated(mat); 228 if (!viewer) viewer = PETSC_VIEWER_STDOUT_(mat->comm); 229 PetscValidHeaderSpecific(viewer,PETSC_VIEWER_COOKIE); 230 PetscCheckSameComm(mat,viewer); 231 if (!mat->assembled) SETERRQ(1,"Must call MatAssemblyBegin/End() before viewing matrix"); 232 233 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr); 234 if (isascii) { 235 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 236 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 237 if (mat->prefix) { 238 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:(%s)\n",mat->prefix);CHKERRQ(ierr); 239 } else { 240 ierr = PetscViewerASCIIPrintf(viewer,"Matrix Object:\n");CHKERRQ(ierr); 241 } 242 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 243 ierr = MatGetType(mat,&cstr);CHKERRQ(ierr); 244 ierr = MatGetSize(mat,&rows,&cols);CHKERRQ(ierr); 245 ierr = PetscViewerASCIIPrintf(viewer,"type=%s, rows=%d, cols=%d\n",cstr,rows,cols);CHKERRQ(ierr); 246 if (mat->ops->getinfo) { 247 MatInfo info; 248 ierr = MatGetInfo(mat,MAT_GLOBAL_SUM,&info);CHKERRQ(ierr); 249 ierr = PetscViewerASCIIPrintf(viewer,"total: nonzeros=%d, allocated nonzeros=%d\n", 250 (int)info.nz_used,(int)info.nz_allocated);CHKERRQ(ierr); 251 } 252 } 253 } 254 if (mat->ops->view) { 255 ierr = PetscViewerASCIIPushTab(viewer);CHKERRQ(ierr); 256 ierr = (*mat->ops->view)(mat,viewer);CHKERRQ(ierr); 257 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 258 } else if (!isascii) { 259 SETERRQ1(1,"Viewer type %s not supported",((PetscObject)viewer)->type_name); 260 } 261 if (isascii) { 262 ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr); 263 if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) { 264 ierr = PetscViewerASCIIPopTab(viewer);CHKERRQ(ierr); 265 } 266 } 267 PetscFunctionReturn(0); 268 } 269 270 #undef __FUNCT__ 271 #define __FUNCT__ "MatScaleSystem" 272 /*@C 273 MatScaleSystem - Scale a vector solution and right hand side to 274 match the scaling of a scaled matrix. 275 276 Collective on Mat 277 278 Input Parameter: 279 + mat - the matrix 280 . x - solution vector (or PETSC_NULL) 281 - b - right hand side vector (or PETSC_NULL) 282 283 284 Notes: 285 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 286 internally scaled, so this does nothing. For MPIROWBS it 287 permutes and diagonally scales. 288 289 The SLES methods automatically call this routine when required 290 (via PCPreSolve()) so it is rarely used directly. 291 292 Level: Developer 293 294 Concepts: matrices^scaling 295 296 .seealso: MatUseScaledForm(), MatUnScaleSystem() 297 @*/ 298 int MatScaleSystem(Mat mat,Vec x,Vec b) 299 { 300 int ierr; 301 302 PetscFunctionBegin; 303 PetscValidHeaderSpecific(mat,MAT_COOKIE); 304 PetscValidType(mat); 305 MatPreallocated(mat); 306 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 307 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);} 308 309 if (mat->ops->scalesystem) { 310 ierr = (*mat->ops->scalesystem)(mat,x,b);CHKERRQ(ierr); 311 } 312 PetscFunctionReturn(0); 313 } 314 315 #undef __FUNCT__ 316 #define __FUNCT__ "MatUnScaleSystem" 317 /*@C 318 MatUnScaleSystem - Unscales a vector solution and right hand side to 319 match the original scaling of a scaled matrix. 320 321 Collective on Mat 322 323 Input Parameter: 324 + mat - the matrix 325 . x - solution vector (or PETSC_NULL) 326 - b - right hand side vector (or PETSC_NULL) 327 328 329 Notes: 330 For AIJ, BAIJ, and BDiag matrix formats, the matrices are not 331 internally scaled, so this does nothing. For MPIROWBS it 332 permutes and diagonally scales. 333 334 The SLES methods automatically call this routine when required 335 (via PCPreSolve()) so it is rarely used directly. 336 337 Level: Developer 338 339 .seealso: MatUseScaledForm(), MatScaleSystem() 340 @*/ 341 int MatUnScaleSystem(Mat mat,Vec x,Vec b) 342 { 343 int ierr; 344 345 PetscFunctionBegin; 346 PetscValidHeaderSpecific(mat,MAT_COOKIE); 347 PetscValidType(mat); 348 MatPreallocated(mat); 349 if (x) {PetscValidHeaderSpecific(x,VEC_COOKIE);PetscCheckSameComm(mat,x);} 350 if (b) {PetscValidHeaderSpecific(b,VEC_COOKIE);PetscCheckSameComm(mat,b);} 351 if (mat->ops->unscalesystem) { 352 ierr = (*mat->ops->unscalesystem)(mat,x,b);CHKERRQ(ierr); 353 } 354 PetscFunctionReturn(0); 355 } 356 357 #undef __FUNCT__ 358 #define __FUNCT__ "MatUseScaledForm" 359 /*@C 360 MatUseScaledForm - For matrix storage formats that scale the 361 matrix (for example MPIRowBS matrices are diagonally scaled on 362 assembly) indicates matrix operations (MatMult() etc) are 363 applied using the scaled matrix. 364 365 Collective on Mat 366 367 Input Parameter: 368 + mat - the matrix 369 - scaled - PETSC_TRUE for applying the scaled, PETSC_FALSE for 370 applying the original matrix 371 372 Notes: 373 For scaled matrix formats, applying the original, unscaled matrix 374 will be slightly more expensive 375 376 Level: Developer 377 378 .seealso: MatScaleSystem(), MatUnScaleSystem() 379 @*/ 380 int MatUseScaledForm(Mat mat,PetscTruth scaled) 381 { 382 int ierr; 383 384 PetscFunctionBegin; 385 PetscValidHeaderSpecific(mat,MAT_COOKIE); 386 PetscValidType(mat); 387 MatPreallocated(mat); 388 if (mat->ops->usescaledform) { 389 ierr = (*mat->ops->usescaledform)(mat,scaled);CHKERRQ(ierr); 390 } 391 PetscFunctionReturn(0); 392 } 393 394 #undef __FUNCT__ 395 #define __FUNCT__ "MatDestroy" 396 /*@C 397 MatDestroy - Frees space taken by a matrix. 398 399 Collective on Mat 400 401 Input Parameter: 402 . A - the matrix 403 404 Level: beginner 405 406 @*/ 407 int MatDestroy(Mat A) 408 { 409 int ierr; 410 411 PetscFunctionBegin; 412 PetscValidHeaderSpecific(A,MAT_COOKIE); 413 PetscValidType(A); 414 MatPreallocated(A); 415 if (--A->refct > 0) PetscFunctionReturn(0); 416 417 /* if memory was published with AMS then destroy it */ 418 ierr = PetscObjectDepublish(A);CHKERRQ(ierr); 419 if (A->mapping) { 420 ierr = ISLocalToGlobalMappingDestroy(A->mapping);CHKERRQ(ierr); 421 } 422 if (A->bmapping) { 423 ierr = ISLocalToGlobalMappingDestroy(A->bmapping);CHKERRQ(ierr); 424 } 425 if (A->rmap) { 426 ierr = PetscMapDestroy(A->rmap);CHKERRQ(ierr); 427 } 428 if (A->cmap) { 429 ierr = PetscMapDestroy(A->cmap);CHKERRQ(ierr); 430 } 431 432 ierr = (*A->ops->destroy)(A);CHKERRQ(ierr); 433 PetscLogObjectDestroy(A); 434 PetscHeaderDestroy(A); 435 PetscFunctionReturn(0); 436 } 437 438 #undef __FUNCT__ 439 #define __FUNCT__ "MatValid" 440 /*@ 441 MatValid - Checks whether a matrix object is valid. 442 443 Collective on Mat 444 445 Input Parameter: 446 . m - the matrix to check 447 448 Output Parameter: 449 flg - flag indicating matrix status, either 450 PETSC_TRUE if matrix is valid, or PETSC_FALSE otherwise. 451 452 Level: developer 453 454 Concepts: matrices^validity 455 @*/ 456 int MatValid(Mat m,PetscTruth *flg) 457 { 458 PetscFunctionBegin; 459 PetscValidIntPointer(flg); 460 if (!m) *flg = PETSC_FALSE; 461 else if (m->cookie != MAT_COOKIE) *flg = PETSC_FALSE; 462 else *flg = PETSC_TRUE; 463 PetscFunctionReturn(0); 464 } 465 466 #undef __FUNCT__ 467 #define __FUNCT__ "MatSetValues" 468 /*@ 469 MatSetValues - Inserts or adds a block of values into a matrix. 470 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 471 MUST be called after all calls to MatSetValues() have been completed. 472 473 Not Collective 474 475 Input Parameters: 476 + mat - the matrix 477 . v - a logically two-dimensional array of values 478 . m, idxm - the number of rows and their global indices 479 . n, idxn - the number of columns and their global indices 480 - addv - either ADD_VALUES or INSERT_VALUES, where 481 ADD_VALUES adds values to any existing entries, and 482 INSERT_VALUES replaces existing entries with new values 483 484 Notes: 485 By default the values, v, are row-oriented and unsorted. 486 See MatSetOption() for other options. 487 488 Calls to MatSetValues() with the INSERT_VALUES and ADD_VALUES 489 options cannot be mixed without intervening calls to the assembly 490 routines. 491 492 MatSetValues() uses 0-based row and column numbers in Fortran 493 as well as in C. 494 495 Negative indices may be passed in idxm and idxn, these rows and columns are 496 simply ignored. This allows easily inserting element stiffness matrices 497 with homogeneous Dirchlet boundary conditions that you don't want represented 498 in the matrix. 499 500 Efficiency Alert: 501 The routine MatSetValuesBlocked() may offer much better efficiency 502 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 503 504 Level: beginner 505 506 Concepts: matrices^putting entries in 507 508 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 509 @*/ 510 int MatSetValues(Mat mat,int m,const int idxm[],int n,const int idxn[],const PetscScalar v[],InsertMode addv) 511 { 512 int ierr; 513 514 PetscFunctionBegin; 515 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 516 PetscValidHeaderSpecific(mat,MAT_COOKIE); 517 PetscValidType(mat); 518 MatPreallocated(mat); 519 PetscValidIntPointer(idxm); 520 PetscValidIntPointer(idxn); 521 PetscValidScalarPointer(v); 522 if (mat->insertmode == NOT_SET_VALUES) { 523 mat->insertmode = addv; 524 } 525 #if defined(PETSC_USE_BOPT_g) 526 else if (mat->insertmode != addv) { 527 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 528 } 529 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 530 #endif 531 532 if (mat->assembled) { 533 mat->was_assembled = PETSC_TRUE; 534 mat->assembled = PETSC_FALSE; 535 } 536 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 537 if (!mat->ops->setvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 538 ierr = (*mat->ops->setvalues)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 539 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 540 PetscFunctionReturn(0); 541 } 542 543 #undef __FUNCT__ 544 #define __FUNCT__ "MatSetValuesStencil" 545 /*@C 546 MatSetValuesStencil - Inserts or adds a block of values into a matrix. 547 Using structured grid indexing 548 549 Not Collective 550 551 Input Parameters: 552 + mat - the matrix 553 . v - a logically two-dimensional array of values 554 . m - number of rows being entered 555 . idxm - grid coordinates (and component number when dof > 1) for matrix rows being entered 556 . n - number of columns being entered 557 . idxn - grid coordinates (and component number when dof > 1) for matrix columns being entered 558 - addv - either ADD_VALUES or INSERT_VALUES, where 559 ADD_VALUES adds values to any existing entries, and 560 INSERT_VALUES replaces existing entries with new values 561 562 Notes: 563 By default the values, v, are row-oriented and unsorted. 564 See MatSetOption() for other options. 565 566 Calls to MatSetValuesStencil() with the INSERT_VALUES and ADD_VALUES 567 options cannot be mixed without intervening calls to the assembly 568 routines. 569 570 The grid coordinates are across the entire grid, not just the local portion 571 572 MatSetValuesStencil() uses 0-based row and column numbers in Fortran 573 as well as in C. 574 575 For setting/accessing vector values via array coordinates you can use the DAVecGetArray() routine 576 577 In order to use this routine you must either obtain the matrix with DAGetMatrix() 578 or call MatSetLocalToGlobalMapping() and MatSetStencil() first. 579 580 The columns and rows in the stencil passed in MUST be contained within the 581 ghost region of the given process as set with DACreateXXX() or MatSetStencil(). For example, 582 if you create a DA with an overlap of one grid level and on a particular process its first 583 local nonghost x logical coordinate is 6 (so its first ghost x logical coordinate is 5) the 584 first i index you can use in your column and row indices in MatSetStencil() is 5. 585 586 In Fortran idxm and idxn should be declared as 587 $ MatStencil idxm(4,m),idxn(4,n) 588 and the values inserted using 589 $ idxm(MatStencil_i,1) = i 590 $ idxm(MatStencil_j,1) = j 591 $ idxm(MatStencil_k,1) = k 592 $ idxm(MatStencil_c,1) = c 593 etc 594 595 Negative indices may be passed in idxm and idxn, these rows and columns are 596 simply ignored. This allows easily inserting element stiffness matrices 597 with homogeneous Dirchlet boundary conditions that you don't want represented 598 in the matrix. 599 600 Inspired by the structured grid interface to the HYPRE package 601 (http://www.llnl.gov/CASC/hypre) 602 603 Efficiency Alert: 604 The routine MatSetValuesBlockedStencil() may offer much better efficiency 605 for users of block sparse formats (MATSEQBAIJ and MATMPIBAIJ). 606 607 Level: beginner 608 609 Concepts: matrices^putting entries in 610 611 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 612 MatSetValues(), MatSetValuesBlockedStencil(), MatSetStencil(), DAGetMatrix(), DAVecGetArray() 613 @*/ 614 int MatSetValuesStencil(Mat mat,int m,const MatStencil idxm[],int n,const MatStencil idxn[],const PetscScalar v[],InsertMode addv) 615 { 616 int j,i,ierr,jdxm[128],jdxn[256],dim = mat->stencil.dim,*dims = mat->stencil.dims+1,tmp; 617 int *starts = mat->stencil.starts,*dxm = (int*)idxm,*dxn = (int*)idxn,sdim = dim - (1 - (int)mat->stencil.noc); 618 619 PetscFunctionBegin; 620 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 621 PetscValidHeaderSpecific(mat,MAT_COOKIE); 622 PetscValidType(mat); 623 PetscValidIntPointer(idxm); 624 PetscValidIntPointer(idxn); 625 PetscValidScalarPointer(v); 626 627 if (m > 128) SETERRQ1(1,"Can only set 128 rows at a time; trying to set %d",m); 628 if (n > 128) SETERRQ1(1,"Can only set 256 columns at a time; trying to set %d",n); 629 630 for (i=0; i<m; i++) { 631 for (j=0; j<3-sdim; j++) dxm++; 632 tmp = *dxm++ - starts[0]; 633 for (j=0; j<dim-1; j++) { 634 tmp = tmp*dims[j] + *dxm++ - starts[j+1]; 635 } 636 if (mat->stencil.noc) dxm++; 637 jdxm[i] = tmp; 638 } 639 for (i=0; i<n; i++) { 640 for (j=0; j<3-sdim; j++) dxn++; 641 tmp = *dxn++ - starts[0]; 642 for (j=0; j<dim-1; j++) { 643 tmp = tmp*dims[j] + *dxn++ - starts[j+1]; 644 } 645 if (mat->stencil.noc) dxn++; 646 jdxn[i] = tmp; 647 } 648 ierr = MatSetValuesLocal(mat,m,jdxm,n,jdxn,v,addv);CHKERRQ(ierr); 649 PetscFunctionReturn(0); 650 } 651 652 #undef __FUNCT__ 653 #define __FUNCT__ "MatSetStencil" 654 /*@ 655 MatSetStencil - Sets the grid information for setting values into a matrix via 656 MatSetValuesStencil() 657 658 Not Collective 659 660 Input Parameters: 661 + mat - the matrix 662 . dim - dimension of the grid 1, 2, or 3 663 . dims - number of grid points in x, y, and z direction, including ghost points on your processor 664 . starts - starting point of ghost nodes on your processor in x, y, and z direction 665 - dof - number of degrees of freedom per node 666 667 668 Inspired by the structured grid interface to the HYPRE package 669 (www.llnl.gov/CASC/hyper) 670 671 For matrices generated with DAGetMatrix() this routine is automatically called and so not needed by the 672 user. 673 674 Level: beginner 675 676 Concepts: matrices^putting entries in 677 678 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal() 679 MatSetValues(), MatSetValuesBlockedStencil(), MatSetValuesStencil() 680 @*/ 681 int MatSetStencil(Mat mat,int dim,const int dims[],const int starts[],int dof) 682 { 683 int i; 684 685 PetscFunctionBegin; 686 PetscValidHeaderSpecific(mat,MAT_COOKIE); 687 PetscValidIntPointer(dims); 688 PetscValidIntPointer(starts); 689 690 mat->stencil.dim = dim + (dof > 1); 691 for (i=0; i<dim; i++) { 692 mat->stencil.dims[i] = dims[dim-i-1]; /* copy the values in backwards */ 693 mat->stencil.starts[i] = starts[dim-i-1]; 694 } 695 mat->stencil.dims[dim] = dof; 696 mat->stencil.starts[dim] = 0; 697 mat->stencil.noc = (PetscTruth)(dof == 1); 698 PetscFunctionReturn(0); 699 } 700 701 #undef __FUNCT__ 702 #define __FUNCT__ "MatSetValuesBlocked" 703 /*@ 704 MatSetValuesBlocked - Inserts or adds a block of values into a matrix. 705 706 Not Collective 707 708 Input Parameters: 709 + mat - the matrix 710 . v - a logically two-dimensional array of values 711 . m, idxm - the number of block rows and their global block indices 712 . n, idxn - the number of block columns and their global block indices 713 - addv - either ADD_VALUES or INSERT_VALUES, where 714 ADD_VALUES adds values to any existing entries, and 715 INSERT_VALUES replaces existing entries with new values 716 717 Notes: 718 By default the values, v, are row-oriented and unsorted. So the layout of 719 v is the same as for MatSetValues(). See MatSetOption() for other options. 720 721 Calls to MatSetValuesBlocked() with the INSERT_VALUES and ADD_VALUES 722 options cannot be mixed without intervening calls to the assembly 723 routines. 724 725 MatSetValuesBlocked() uses 0-based row and column numbers in Fortran 726 as well as in C. 727 728 Negative indices may be passed in idxm and idxn, these rows and columns are 729 simply ignored. This allows easily inserting element stiffness matrices 730 with homogeneous Dirchlet boundary conditions that you don't want represented 731 in the matrix. 732 733 Each time an entry is set within a sparse matrix via MatSetValues(), 734 internal searching must be done to determine where to place the the 735 data in the matrix storage space. By instead inserting blocks of 736 entries via MatSetValuesBlocked(), the overhead of matrix assembly is 737 reduced. 738 739 Restrictions: 740 MatSetValuesBlocked() is currently supported only for the block AIJ 741 matrix format (MATSEQBAIJ and MATMPIBAIJ, which are created via 742 MatCreateSeqBAIJ() and MatCreateMPIBAIJ()). 743 744 Level: intermediate 745 746 Concepts: matrices^putting entries in blocked 747 748 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal() 749 @*/ 750 int MatSetValuesBlocked(Mat mat,int m,const int idxm[],int n,const int idxn[],const PetscScalar v[],InsertMode addv) 751 { 752 int ierr; 753 754 PetscFunctionBegin; 755 if (!m || !n) PetscFunctionReturn(0); /* no values to insert */ 756 PetscValidHeaderSpecific(mat,MAT_COOKIE); 757 PetscValidType(mat); 758 MatPreallocated(mat); 759 PetscValidIntPointer(idxm); 760 PetscValidIntPointer(idxn); 761 PetscValidScalarPointer(v); 762 if (mat->insertmode == NOT_SET_VALUES) { 763 mat->insertmode = addv; 764 } 765 #if defined(PETSC_USE_BOPT_g) 766 else if (mat->insertmode != addv) { 767 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 768 } 769 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 770 #endif 771 772 if (mat->assembled) { 773 mat->was_assembled = PETSC_TRUE; 774 mat->assembled = PETSC_FALSE; 775 } 776 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 777 if (!mat->ops->setvaluesblocked) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 778 ierr = (*mat->ops->setvaluesblocked)(mat,m,idxm,n,idxn,v,addv);CHKERRQ(ierr); 779 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 780 PetscFunctionReturn(0); 781 } 782 783 #undef __FUNCT__ 784 #define __FUNCT__ "MatGetValues" 785 /*@ 786 MatGetValues - Gets a block of values from a matrix. 787 788 Not Collective; currently only returns a local block 789 790 Input Parameters: 791 + mat - the matrix 792 . v - a logically two-dimensional array for storing the values 793 . m, idxm - the number of rows and their global indices 794 - n, idxn - the number of columns and their global indices 795 796 Notes: 797 The user must allocate space (m*n PetscScalars) for the values, v. 798 The values, v, are then returned in a row-oriented format, 799 analogous to that used by default in MatSetValues(). 800 801 MatGetValues() uses 0-based row and column numbers in 802 Fortran as well as in C. 803 804 MatGetValues() requires that the matrix has been assembled 805 with MatAssemblyBegin()/MatAssemblyEnd(). Thus, calls to 806 MatSetValues() and MatGetValues() CANNOT be made in succession 807 without intermediate matrix assembly. 808 809 Level: advanced 810 811 Concepts: matrices^accessing values 812 813 .seealso: MatGetRow(), MatGetSubmatrices(), MatSetValues() 814 @*/ 815 int MatGetValues(Mat mat,int m,const int idxm[],int n,const int idxn[],PetscScalar v[]) 816 { 817 int ierr; 818 819 PetscFunctionBegin; 820 PetscValidHeaderSpecific(mat,MAT_COOKIE); 821 PetscValidType(mat); 822 MatPreallocated(mat); 823 PetscValidIntPointer(idxm); 824 PetscValidIntPointer(idxn); 825 PetscValidScalarPointer(v); 826 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 827 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 828 if (!mat->ops->getvalues) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 829 830 ierr = PetscLogEventBegin(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 831 ierr = (*mat->ops->getvalues)(mat,m,idxm,n,idxn,v);CHKERRQ(ierr); 832 ierr = PetscLogEventEnd(MAT_GetValues,mat,0,0,0);CHKERRQ(ierr); 833 PetscFunctionReturn(0); 834 } 835 836 #undef __FUNCT__ 837 #define __FUNCT__ "MatSetLocalToGlobalMapping" 838 /*@ 839 MatSetLocalToGlobalMapping - Sets a local-to-global numbering for use by 840 the routine MatSetValuesLocal() to allow users to insert matrix entries 841 using a local (per-processor) numbering. 842 843 Not Collective 844 845 Input Parameters: 846 + x - the matrix 847 - mapping - mapping created with ISLocalToGlobalMappingCreate() 848 or ISLocalToGlobalMappingCreateIS() 849 850 Level: intermediate 851 852 Concepts: matrices^local to global mapping 853 Concepts: local to global mapping^for matrices 854 855 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesLocal() 856 @*/ 857 int MatSetLocalToGlobalMapping(Mat x,ISLocalToGlobalMapping mapping) 858 { 859 int ierr; 860 PetscFunctionBegin; 861 PetscValidHeaderSpecific(x,MAT_COOKIE); 862 PetscValidType(x); 863 MatPreallocated(x); 864 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 865 if (x->mapping) { 866 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 867 } 868 869 if (x->ops->setlocaltoglobalmapping) { 870 ierr = (*x->ops->setlocaltoglobalmapping)(x,mapping);CHKERRQ(ierr); 871 } else { 872 x->mapping = mapping; 873 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 874 } 875 PetscFunctionReturn(0); 876 } 877 878 #undef __FUNCT__ 879 #define __FUNCT__ "MatSetLocalToGlobalMappingBlock" 880 /*@ 881 MatSetLocalToGlobalMappingBlock - Sets a local-to-global numbering for use 882 by the routine MatSetValuesBlockedLocal() to allow users to insert matrix 883 entries using a local (per-processor) numbering. 884 885 Not Collective 886 887 Input Parameters: 888 + x - the matrix 889 - mapping - mapping created with ISLocalToGlobalMappingCreate() or 890 ISLocalToGlobalMappingCreateIS() 891 892 Level: intermediate 893 894 Concepts: matrices^local to global mapping blocked 895 Concepts: local to global mapping^for matrices, blocked 896 897 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetValuesBlockedLocal(), 898 MatSetValuesBlocked(), MatSetValuesLocal() 899 @*/ 900 int MatSetLocalToGlobalMappingBlock(Mat x,ISLocalToGlobalMapping mapping) 901 { 902 int ierr; 903 PetscFunctionBegin; 904 PetscValidHeaderSpecific(x,MAT_COOKIE); 905 PetscValidType(x); 906 MatPreallocated(x); 907 PetscValidHeaderSpecific(mapping,IS_LTOGM_COOKIE); 908 if (x->bmapping) { 909 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Mapping already set for matrix"); 910 } 911 912 x->bmapping = mapping; 913 ierr = PetscObjectReference((PetscObject)mapping);CHKERRQ(ierr); 914 PetscFunctionReturn(0); 915 } 916 917 #undef __FUNCT__ 918 #define __FUNCT__ "MatSetValuesLocal" 919 /*@ 920 MatSetValuesLocal - Inserts or adds values into certain locations of a matrix, 921 using a local ordering of the nodes. 922 923 Not Collective 924 925 Input Parameters: 926 + x - the matrix 927 . nrow, irow - number of rows and their local indices 928 . ncol, icol - number of columns and their local indices 929 . y - a logically two-dimensional array of values 930 - addv - either INSERT_VALUES or ADD_VALUES, where 931 ADD_VALUES adds values to any existing entries, and 932 INSERT_VALUES replaces existing entries with new values 933 934 Notes: 935 Before calling MatSetValuesLocal(), the user must first set the 936 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 937 938 Calls to MatSetValuesLocal() with the INSERT_VALUES and ADD_VALUES 939 options cannot be mixed without intervening calls to the assembly 940 routines. 941 942 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 943 MUST be called after all calls to MatSetValuesLocal() have been completed. 944 945 Level: intermediate 946 947 Concepts: matrices^putting entries in with local numbering 948 949 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValues(), MatSetLocalToGlobalMapping(), 950 MatSetValueLocal() 951 @*/ 952 int MatSetValuesLocal(Mat mat,int nrow,const int irow[],int ncol,const int icol[],const PetscScalar y[],InsertMode addv) 953 { 954 int ierr,irowm[2048],icolm[2048]; 955 956 PetscFunctionBegin; 957 PetscValidHeaderSpecific(mat,MAT_COOKIE); 958 PetscValidType(mat); 959 MatPreallocated(mat); 960 PetscValidIntPointer(irow); 961 PetscValidIntPointer(icol); 962 PetscValidScalarPointer(y); 963 964 if (mat->insertmode == NOT_SET_VALUES) { 965 mat->insertmode = addv; 966 } 967 #if defined(PETSC_USE_BOPT_g) 968 else if (mat->insertmode != addv) { 969 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 970 } 971 if (!mat->ops->setvalueslocal && (nrow > 2048 || ncol > 2048)) { 972 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 973 } 974 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 975 #endif 976 977 if (mat->assembled) { 978 mat->was_assembled = PETSC_TRUE; 979 mat->assembled = PETSC_FALSE; 980 } 981 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 982 if (!mat->ops->setvalueslocal) { 983 ierr = ISLocalToGlobalMappingApply(mat->mapping,nrow,irow,irowm);CHKERRQ(ierr); 984 ierr = ISLocalToGlobalMappingApply(mat->mapping,ncol,icol,icolm);CHKERRQ(ierr); 985 ierr = (*mat->ops->setvalues)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 986 } else { 987 ierr = (*mat->ops->setvalueslocal)(mat,nrow,irow,ncol,icol,y,addv);CHKERRQ(ierr); 988 } 989 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 990 PetscFunctionReturn(0); 991 } 992 993 #undef __FUNCT__ 994 #define __FUNCT__ "MatSetValuesBlockedLocal" 995 /*@ 996 MatSetValuesBlockedLocal - Inserts or adds values into certain locations of a matrix, 997 using a local ordering of the nodes a block at a time. 998 999 Not Collective 1000 1001 Input Parameters: 1002 + x - the matrix 1003 . nrow, irow - number of rows and their local indices 1004 . ncol, icol - number of columns and their local indices 1005 . y - a logically two-dimensional array of values 1006 - addv - either INSERT_VALUES or ADD_VALUES, where 1007 ADD_VALUES adds values to any existing entries, and 1008 INSERT_VALUES replaces existing entries with new values 1009 1010 Notes: 1011 Before calling MatSetValuesBlockedLocal(), the user must first set the 1012 local-to-global mapping by calling MatSetLocalToGlobalMappingBlock(), 1013 where the mapping MUST be set for matrix blocks, not for matrix elements. 1014 1015 Calls to MatSetValuesBlockedLocal() with the INSERT_VALUES and ADD_VALUES 1016 options cannot be mixed without intervening calls to the assembly 1017 routines. 1018 1019 These values may be cached, so MatAssemblyBegin() and MatAssemblyEnd() 1020 MUST be called after all calls to MatSetValuesBlockedLocal() have been completed. 1021 1022 Level: intermediate 1023 1024 Concepts: matrices^putting blocked values in with local numbering 1025 1026 .seealso: MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesLocal(), MatSetLocalToGlobalMappingBlock(), MatSetValuesBlocked() 1027 @*/ 1028 int MatSetValuesBlockedLocal(Mat mat,int nrow,const int irow[],int ncol,const int icol[],const PetscScalar y[],InsertMode addv) 1029 { 1030 int ierr,irowm[2048],icolm[2048]; 1031 1032 PetscFunctionBegin; 1033 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1034 PetscValidType(mat); 1035 MatPreallocated(mat); 1036 PetscValidIntPointer(irow); 1037 PetscValidIntPointer(icol); 1038 PetscValidScalarPointer(y); 1039 if (mat->insertmode == NOT_SET_VALUES) { 1040 mat->insertmode = addv; 1041 } 1042 #if defined(PETSC_USE_BOPT_g) 1043 else if (mat->insertmode != addv) { 1044 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Cannot mix add values and insert values"); 1045 } 1046 if (!mat->bmapping) { 1047 SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Local to global never set with MatSetLocalToGlobalMappingBlock()"); 1048 } 1049 if (nrow > 2048 || ncol > 2048) { 1050 SETERRQ2(PETSC_ERR_SUP,"Number column/row indices must be <= 2048: are %d %d",nrow,ncol); 1051 } 1052 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1053 #endif 1054 1055 if (mat->assembled) { 1056 mat->was_assembled = PETSC_TRUE; 1057 mat->assembled = PETSC_FALSE; 1058 } 1059 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1060 ierr = ISLocalToGlobalMappingApply(mat->bmapping,nrow,irow,irowm);CHKERRQ(ierr); 1061 ierr = ISLocalToGlobalMappingApply(mat->bmapping,ncol,icol,icolm);CHKERRQ(ierr); 1062 ierr = (*mat->ops->setvaluesblocked)(mat,nrow,irowm,ncol,icolm,y,addv);CHKERRQ(ierr); 1063 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 1064 PetscFunctionReturn(0); 1065 } 1066 1067 /* --------------------------------------------------------*/ 1068 #undef __FUNCT__ 1069 #define __FUNCT__ "MatMult" 1070 /*@ 1071 MatMult - Computes the matrix-vector product, y = Ax. 1072 1073 Collective on Mat and Vec 1074 1075 Input Parameters: 1076 + mat - the matrix 1077 - x - the vector to be multilplied 1078 1079 Output Parameters: 1080 . y - the result 1081 1082 Notes: 1083 The vectors x and y cannot be the same. I.e., one cannot 1084 call MatMult(A,y,y). 1085 1086 Level: beginner 1087 1088 Concepts: matrix-vector product 1089 1090 .seealso: MatMultTranspose(), MatMultAdd(), MatMultTransposeAdd() 1091 @*/ 1092 int MatMult(Mat mat,Vec x,Vec y) 1093 { 1094 int ierr; 1095 1096 PetscFunctionBegin; 1097 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1098 PetscValidType(mat); 1099 MatPreallocated(mat); 1100 PetscValidHeaderSpecific(x,VEC_COOKIE); 1101 PetscValidHeaderSpecific(y,VEC_COOKIE); 1102 1103 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1104 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1105 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1106 #ifndef PETSC_HAVE_CONSTRAINTS 1107 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1108 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1109 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1110 #endif 1111 1112 if (mat->nullsp) { 1113 ierr = MatNullSpaceRemove(mat->nullsp,x,&x);CHKERRQ(ierr); 1114 } 1115 1116 ierr = PetscLogEventBegin(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1117 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1118 ierr = PetscLogEventEnd(MAT_Mult,mat,x,y,0);CHKERRQ(ierr); 1119 1120 if (mat->nullsp) { 1121 ierr = MatNullSpaceRemove(mat->nullsp,y,PETSC_NULL);CHKERRQ(ierr); 1122 } 1123 PetscFunctionReturn(0); 1124 } 1125 1126 #undef __FUNCT__ 1127 #define __FUNCT__ "MatMultTranspose" 1128 /*@ 1129 MatMultTranspose - Computes matrix transpose times a vector. 1130 1131 Collective on Mat and Vec 1132 1133 Input Parameters: 1134 + mat - the matrix 1135 - x - the vector to be multilplied 1136 1137 Output Parameters: 1138 . y - the result 1139 1140 Notes: 1141 The vectors x and y cannot be the same. I.e., one cannot 1142 call MatMultTranspose(A,y,y). 1143 1144 Level: beginner 1145 1146 Concepts: matrix vector product^transpose 1147 1148 .seealso: MatMult(), MatMultAdd(), MatMultTransposeAdd() 1149 @*/ 1150 int MatMultTranspose(Mat mat,Vec x,Vec y) 1151 { 1152 int ierr; 1153 PetscTruth flg1, flg2; 1154 1155 PetscFunctionBegin; 1156 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1157 PetscValidType(mat); 1158 MatPreallocated(mat); 1159 PetscValidHeaderSpecific(x,VEC_COOKIE); 1160 PetscValidHeaderSpecific(y,VEC_COOKIE); 1161 1162 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1163 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1164 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1165 #ifndef PETSC_HAVE_CONSTRAINTS 1166 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 1167 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 1168 #endif 1169 1170 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP, "Operation not supported"); 1171 ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1172 if (!mat->ops->multtranspose) SETERRQ(PETSC_ERR_SUP,"This matrix type does not have a multiply tranpose defined"); 1173 1174 ierr = PetscTypeCompare((PetscObject)mat,MATSEQSBAIJ,&flg1); 1175 ierr = PetscTypeCompare((PetscObject)mat,MATMPISBAIJ,&flg2); 1176 if (flg1 || flg2) { /* mat is in sbaij format */ 1177 ierr = (*mat->ops->mult)(mat,x,y);CHKERRQ(ierr); 1178 } else { 1179 ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr); 1180 } 1181 ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr); 1182 PetscFunctionReturn(0); 1183 } 1184 1185 #undef __FUNCT__ 1186 #define __FUNCT__ "MatMultAdd" 1187 /*@ 1188 MatMultAdd - Computes v3 = v2 + A * v1. 1189 1190 Collective on Mat and Vec 1191 1192 Input Parameters: 1193 + mat - the matrix 1194 - v1, v2 - the vectors 1195 1196 Output Parameters: 1197 . v3 - the result 1198 1199 Notes: 1200 The vectors v1 and v3 cannot be the same. I.e., one cannot 1201 call MatMultAdd(A,v1,v2,v1). 1202 1203 Level: beginner 1204 1205 Concepts: matrix vector product^addition 1206 1207 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd() 1208 @*/ 1209 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1210 { 1211 int ierr; 1212 1213 PetscFunctionBegin; 1214 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1215 PetscValidType(mat); 1216 MatPreallocated(mat); 1217 PetscValidHeaderSpecific(v1,VEC_COOKIE); 1218 PetscValidHeaderSpecific(v2,VEC_COOKIE); 1219 PetscValidHeaderSpecific(v3,VEC_COOKIE); 1220 1221 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1222 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1223 if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N); 1224 if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N); 1225 if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N); 1226 if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n); 1227 if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n); 1228 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1229 1230 ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1231 ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1232 ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1233 PetscFunctionReturn(0); 1234 } 1235 1236 #undef __FUNCT__ 1237 #define __FUNCT__ "MatMultTransposeAdd" 1238 /*@ 1239 MatMultTransposeAdd - Computes v3 = v2 + A' * v1. 1240 1241 Collective on Mat and Vec 1242 1243 Input Parameters: 1244 + mat - the matrix 1245 - v1, v2 - the vectors 1246 1247 Output Parameters: 1248 . v3 - the result 1249 1250 Notes: 1251 The vectors v1 and v3 cannot be the same. I.e., one cannot 1252 call MatMultTransposeAdd(A,v1,v2,v1). 1253 1254 Level: beginner 1255 1256 Concepts: matrix vector product^transpose and addition 1257 1258 .seealso: MatMultTranspose(), MatMultAdd(), MatMult() 1259 @*/ 1260 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3) 1261 { 1262 int ierr; 1263 1264 PetscFunctionBegin; 1265 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1266 PetscValidType(mat); 1267 MatPreallocated(mat); 1268 PetscValidHeaderSpecific(v1,VEC_COOKIE); 1269 PetscValidHeaderSpecific(v2,VEC_COOKIE); 1270 PetscValidHeaderSpecific(v3,VEC_COOKIE); 1271 1272 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1273 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1274 if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1275 if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors"); 1276 if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N); 1277 if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N); 1278 if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N); 1279 1280 ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1281 ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr); 1282 ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr); 1283 PetscFunctionReturn(0); 1284 } 1285 1286 #undef __FUNCT__ 1287 #define __FUNCT__ "MatMultConstrained" 1288 /*@ 1289 MatMultConstrained - The inner multiplication routine for a 1290 constrained matrix P^T A P. 1291 1292 Collective on Mat and Vec 1293 1294 Input Parameters: 1295 + mat - the matrix 1296 - x - the vector to be multilplied 1297 1298 Output Parameters: 1299 . y - the result 1300 1301 Notes: 1302 The vectors x and y cannot be the same. I.e., one cannot 1303 call MatMult(A,y,y). 1304 1305 Level: beginner 1306 1307 .keywords: matrix, multiply, matrix-vector product, constraint 1308 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1309 @*/ 1310 int MatMultConstrained(Mat mat,Vec x,Vec y) 1311 { 1312 int ierr; 1313 1314 PetscFunctionBegin; 1315 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1316 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1317 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1318 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1319 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1320 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1321 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1322 if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n); 1323 1324 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1325 ierr = (*mat->ops->multconstrained)(mat,x,y); CHKERRQ(ierr); 1326 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1327 1328 PetscFunctionReturn(0); 1329 } 1330 1331 #undef __FUNCT__ 1332 #define __FUNCT__ "MatMultTransposeConstrained" 1333 /*@ 1334 MatMultTransposeConstrained - The inner multiplication routine for a 1335 constrained matrix P^T A^T P. 1336 1337 Collective on Mat and Vec 1338 1339 Input Parameters: 1340 + mat - the matrix 1341 - x - the vector to be multilplied 1342 1343 Output Parameters: 1344 . y - the result 1345 1346 Notes: 1347 The vectors x and y cannot be the same. I.e., one cannot 1348 call MatMult(A,y,y). 1349 1350 Level: beginner 1351 1352 .keywords: matrix, multiply, matrix-vector product, constraint 1353 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd() 1354 @*/ 1355 int MatMultTransposeConstrained(Mat mat,Vec x,Vec y) 1356 { 1357 int ierr; 1358 1359 PetscFunctionBegin; 1360 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1361 PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE); 1362 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1363 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1364 if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors"); 1365 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1366 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 1367 1368 ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1369 ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr); 1370 ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr); 1371 1372 PetscFunctionReturn(0); 1373 } 1374 /* ------------------------------------------------------------*/ 1375 #undef __FUNCT__ 1376 #define __FUNCT__ "MatGetInfo" 1377 /*@C 1378 MatGetInfo - Returns information about matrix storage (number of 1379 nonzeros, memory, etc.). 1380 1381 Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used 1382 as the flag 1383 1384 Input Parameters: 1385 . mat - the matrix 1386 1387 Output Parameters: 1388 + flag - flag indicating the type of parameters to be returned 1389 (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors, 1390 MAT_GLOBAL_SUM - sum over all processors) 1391 - info - matrix information context 1392 1393 Notes: 1394 The MatInfo context contains a variety of matrix data, including 1395 number of nonzeros allocated and used, number of mallocs during 1396 matrix assembly, etc. Additional information for factored matrices 1397 is provided (such as the fill ratio, number of mallocs during 1398 factorization, etc.). Much of this info is printed to STDOUT 1399 when using the runtime options 1400 $ -log_info -mat_view_info 1401 1402 Example for C/C++ Users: 1403 See the file ${PETSC_DIR}/include/petscmat.h for a complete list of 1404 data within the MatInfo context. For example, 1405 .vb 1406 MatInfo info; 1407 Mat A; 1408 double mal, nz_a, nz_u; 1409 1410 MatGetInfo(A,MAT_LOCAL,&info); 1411 mal = info.mallocs; 1412 nz_a = info.nz_allocated; 1413 .ve 1414 1415 Example for Fortran Users: 1416 Fortran users should declare info as a double precision 1417 array of dimension MAT_INFO_SIZE, and then extract the parameters 1418 of interest. See the file ${PETSC_DIR}/include/finclude/petscmat.h 1419 a complete list of parameter names. 1420 .vb 1421 double precision info(MAT_INFO_SIZE) 1422 double precision mal, nz_a 1423 Mat A 1424 integer ierr 1425 1426 call MatGetInfo(A,MAT_LOCAL,info,ierr) 1427 mal = info(MAT_INFO_MALLOCS) 1428 nz_a = info(MAT_INFO_NZ_ALLOCATED) 1429 .ve 1430 1431 Level: intermediate 1432 1433 Concepts: matrices^getting information on 1434 1435 @*/ 1436 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info) 1437 { 1438 int ierr; 1439 1440 PetscFunctionBegin; 1441 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1442 PetscValidType(mat); 1443 MatPreallocated(mat); 1444 PetscValidPointer(info); 1445 if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1446 ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr); 1447 PetscFunctionReturn(0); 1448 } 1449 1450 /* ----------------------------------------------------------*/ 1451 #undef __FUNCT__ 1452 #define __FUNCT__ "MatILUDTFactor" 1453 /*@C 1454 MatILUDTFactor - Performs a drop tolerance ILU factorization. 1455 1456 Collective on Mat 1457 1458 Input Parameters: 1459 + mat - the matrix 1460 . info - information about the factorization to be done 1461 . row - row permutation 1462 - col - column permutation 1463 1464 Output Parameters: 1465 . fact - the factored matrix 1466 1467 Level: developer 1468 1469 Notes: 1470 Most users should employ the simplified SLES interface for linear solvers 1471 instead of working directly with matrix algebra routines such as this. 1472 See, e.g., SLESCreate(). 1473 1474 This is currently only supported for the SeqAIJ matrix format using code 1475 from Yousef Saad's SPARSEKIT2 package (translated to C with f2c) and/or 1476 Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright 1477 and thus can be distributed with PETSc. 1478 1479 Concepts: matrices^ILUDT factorization 1480 1481 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1482 @*/ 1483 int MatILUDTFactor(Mat mat,MatFactorInfo *info,IS row,IS col,Mat *fact) 1484 { 1485 int ierr; 1486 1487 PetscFunctionBegin; 1488 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1489 PetscValidType(mat); 1490 MatPreallocated(mat); 1491 PetscValidPointer(fact); 1492 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1493 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1494 if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1495 1496 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1497 ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr); 1498 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1499 1500 PetscFunctionReturn(0); 1501 } 1502 1503 #undef __FUNCT__ 1504 #define __FUNCT__ "MatLUFactor" 1505 /*@ 1506 MatLUFactor - Performs in-place LU factorization of matrix. 1507 1508 Collective on Mat 1509 1510 Input Parameters: 1511 + mat - the matrix 1512 . row - row permutation 1513 . col - column permutation 1514 - info - options for factorization, includes 1515 $ fill - expected fill as ratio of original fill. 1516 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1517 $ Run with the option -log_info to determine an optimal value to use 1518 1519 Notes: 1520 Most users should employ the simplified SLES interface for linear solvers 1521 instead of working directly with matrix algebra routines such as this. 1522 See, e.g., SLESCreate(). 1523 1524 This changes the state of the matrix to a factored matrix; it cannot be used 1525 for example with MatSetValues() unless one first calls MatSetUnfactored(). 1526 1527 Level: developer 1528 1529 Concepts: matrices^LU factorization 1530 1531 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), 1532 MatGetOrdering(), MatSetUnfactored(), MatFactorInfo 1533 1534 @*/ 1535 int MatLUFactor(Mat mat,IS row,IS col,MatFactorInfo *info) 1536 { 1537 int ierr; 1538 1539 PetscFunctionBegin; 1540 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1541 PetscValidType(mat); 1542 MatPreallocated(mat); 1543 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1544 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1545 if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1546 1547 ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1548 ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr); 1549 ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr); 1550 PetscFunctionReturn(0); 1551 } 1552 1553 #undef __FUNCT__ 1554 #define __FUNCT__ "MatILUFactor" 1555 /*@ 1556 MatILUFactor - Performs in-place ILU factorization of matrix. 1557 1558 Collective on Mat 1559 1560 Input Parameters: 1561 + mat - the matrix 1562 . row - row permutation 1563 . col - column permutation 1564 - info - structure containing 1565 $ levels - number of levels of fill. 1566 $ expected fill - as ratio of original fill. 1567 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 1568 missing diagonal entries) 1569 1570 Notes: 1571 Probably really in-place only when level of fill is zero, otherwise allocates 1572 new space to store factored matrix and deletes previous memory. 1573 1574 Most users should employ the simplified SLES interface for linear solvers 1575 instead of working directly with matrix algebra routines such as this. 1576 See, e.g., SLESCreate(). 1577 1578 Level: developer 1579 1580 Concepts: matrices^ILU factorization 1581 1582 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1583 @*/ 1584 int MatILUFactor(Mat mat,IS row,IS col,MatFactorInfo *info) 1585 { 1586 int ierr; 1587 1588 PetscFunctionBegin; 1589 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1590 PetscValidType(mat); 1591 MatPreallocated(mat); 1592 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 1593 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1594 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1595 if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1596 1597 ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1598 ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr); 1599 ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr); 1600 PetscFunctionReturn(0); 1601 } 1602 1603 #undef __FUNCT__ 1604 #define __FUNCT__ "MatLUFactorSymbolic" 1605 /*@ 1606 MatLUFactorSymbolic - Performs symbolic LU factorization of matrix. 1607 Call this routine before calling MatLUFactorNumeric(). 1608 1609 Collective on Mat 1610 1611 Input Parameters: 1612 + mat - the matrix 1613 . row, col - row and column permutations 1614 - info - options for factorization, includes 1615 $ fill - expected fill as ratio of original fill. 1616 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1617 $ Run with the option -log_info to determine an optimal value to use 1618 1619 Output Parameter: 1620 . fact - new matrix that has been symbolically factored 1621 1622 Notes: 1623 See the users manual for additional information about 1624 choosing the fill factor for better efficiency. 1625 1626 Most users should employ the simplified SLES interface for linear solvers 1627 instead of working directly with matrix algebra routines such as this. 1628 See, e.g., SLESCreate(). 1629 1630 Level: developer 1631 1632 Concepts: matrices^LU symbolic factorization 1633 1634 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 1635 @*/ 1636 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatFactorInfo *info,Mat *fact) 1637 { 1638 int ierr; 1639 1640 PetscFunctionBegin; 1641 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1642 PetscValidType(mat); 1643 MatPreallocated(mat); 1644 PetscValidPointer(fact); 1645 PetscValidHeaderSpecific(row,IS_COOKIE); 1646 PetscValidHeaderSpecific(col,IS_COOKIE); 1647 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1648 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1649 if (!mat->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic LU",mat->type_name); 1650 1651 ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1652 ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 1653 ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 1654 PetscFunctionReturn(0); 1655 } 1656 1657 #undef __FUNCT__ 1658 #define __FUNCT__ "MatLUFactorNumeric" 1659 /*@ 1660 MatLUFactorNumeric - Performs numeric LU factorization of a matrix. 1661 Call this routine after first calling MatLUFactorSymbolic(). 1662 1663 Collective on Mat 1664 1665 Input Parameters: 1666 + mat - the matrix 1667 - fact - the matrix generated for the factor, from MatLUFactorSymbolic() 1668 1669 Notes: 1670 See MatLUFactor() for in-place factorization. See 1671 MatCholeskyFactorNumeric() for the symmetric, positive definite case. 1672 1673 Most users should employ the simplified SLES interface for linear solvers 1674 instead of working directly with matrix algebra routines such as this. 1675 See, e.g., SLESCreate(). 1676 1677 Level: developer 1678 1679 Concepts: matrices^LU numeric factorization 1680 1681 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor() 1682 @*/ 1683 int MatLUFactorNumeric(Mat mat,Mat *fact) 1684 { 1685 int ierr; 1686 1687 PetscFunctionBegin; 1688 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1689 PetscValidType(mat); 1690 MatPreallocated(mat); 1691 PetscValidPointer(fact); 1692 PetscValidHeaderSpecific(*fact,MAT_COOKIE); 1693 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1694 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1695 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d", 1696 mat->M,(*fact)->M,mat->N,(*fact)->N); 1697 } 1698 if (!(*fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1699 1700 ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1701 ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr); 1702 ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1703 1704 ierr = MatView_Private(*fact);CHKERRQ(ierr); 1705 PetscFunctionReturn(0); 1706 } 1707 1708 #undef __FUNCT__ 1709 #define __FUNCT__ "MatCholeskyFactor" 1710 /*@ 1711 MatCholeskyFactor - Performs in-place Cholesky factorization of a 1712 symmetric matrix. 1713 1714 Collective on Mat 1715 1716 Input Parameters: 1717 + mat - the matrix 1718 . perm - row and column permutations 1719 - f - expected fill as ratio of original fill 1720 1721 Notes: 1722 See MatLUFactor() for the nonsymmetric case. See also 1723 MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric(). 1724 1725 Most users should employ the simplified SLES interface for linear solvers 1726 instead of working directly with matrix algebra routines such as this. 1727 See, e.g., SLESCreate(). 1728 1729 Level: developer 1730 1731 Concepts: matrices^Cholesky factorization 1732 1733 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric() 1734 MatGetOrdering() 1735 1736 @*/ 1737 int MatCholeskyFactor(Mat mat,IS perm,MatFactorInfo *info) 1738 { 1739 int ierr; 1740 1741 PetscFunctionBegin; 1742 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1743 PetscValidType(mat); 1744 MatPreallocated(mat); 1745 PetscValidHeaderSpecific(perm,IS_COOKIE); 1746 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1747 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1748 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1749 if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1750 1751 ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1752 ierr = (*mat->ops->choleskyfactor)(mat,perm,info);CHKERRQ(ierr); 1753 ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr); 1754 PetscFunctionReturn(0); 1755 } 1756 1757 #undef __FUNCT__ 1758 #define __FUNCT__ "MatCholeskyFactorSymbolic" 1759 /*@ 1760 MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization 1761 of a symmetric matrix. 1762 1763 Collective on Mat 1764 1765 Input Parameters: 1766 + mat - the matrix 1767 . perm - row and column permutations 1768 - info - options for factorization, includes 1769 $ fill - expected fill as ratio of original fill. 1770 $ dtcol - pivot tolerance (0 no pivot, 1 full column pivoting) 1771 $ Run with the option -log_info to determine an optimal value to use 1772 1773 Output Parameter: 1774 . fact - the factored matrix 1775 1776 Notes: 1777 See MatLUFactorSymbolic() for the nonsymmetric case. See also 1778 MatCholeskyFactor() and MatCholeskyFactorNumeric(). 1779 1780 Most users should employ the simplified SLES interface for linear solvers 1781 instead of working directly with matrix algebra routines such as this. 1782 See, e.g., SLESCreate(). 1783 1784 Level: developer 1785 1786 Concepts: matrices^Cholesky symbolic factorization 1787 1788 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric() 1789 MatGetOrdering() 1790 1791 @*/ 1792 int MatCholeskyFactorSymbolic(Mat mat,IS perm,MatFactorInfo *info,Mat *fact) 1793 { 1794 int ierr; 1795 1796 PetscFunctionBegin; 1797 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1798 PetscValidType(mat); 1799 MatPreallocated(mat); 1800 PetscValidPointer(fact); 1801 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square"); 1802 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1803 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 1804 if (!mat->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1805 1806 ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1807 ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,info,fact);CHKERRQ(ierr); 1808 ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 1809 PetscFunctionReturn(0); 1810 } 1811 1812 #undef __FUNCT__ 1813 #define __FUNCT__ "MatCholeskyFactorNumeric" 1814 /*@ 1815 MatCholeskyFactorNumeric - Performs numeric Cholesky factorization 1816 of a symmetric matrix. Call this routine after first calling 1817 MatCholeskyFactorSymbolic(). 1818 1819 Collective on Mat 1820 1821 Input Parameter: 1822 . mat - the initial matrix 1823 1824 Output Parameter: 1825 . fact - the factored matrix 1826 1827 Notes: 1828 Most users should employ the simplified SLES interface for linear solvers 1829 instead of working directly with matrix algebra routines such as this. 1830 See, e.g., SLESCreate(). 1831 1832 Level: developer 1833 1834 Concepts: matrices^Cholesky numeric factorization 1835 1836 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric() 1837 @*/ 1838 int MatCholeskyFactorNumeric(Mat mat,Mat *fact) 1839 { 1840 int ierr; 1841 1842 PetscFunctionBegin; 1843 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1844 PetscValidType(mat); 1845 MatPreallocated(mat); 1846 PetscValidPointer(fact); 1847 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 1848 if (!(*fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1849 if (mat->M != (*fact)->M || mat->N != (*fact)->N) { 1850 SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d", 1851 mat->M,(*fact)->M,mat->N,(*fact)->N); 1852 } 1853 1854 ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1855 ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr); 1856 ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr); 1857 PetscFunctionReturn(0); 1858 } 1859 1860 /* ----------------------------------------------------------------*/ 1861 #undef __FUNCT__ 1862 #define __FUNCT__ "MatSolve" 1863 /*@ 1864 MatSolve - Solves A x = b, given a factored matrix. 1865 1866 Collective on Mat and Vec 1867 1868 Input Parameters: 1869 + mat - the factored matrix 1870 - b - the right-hand-side vector 1871 1872 Output Parameter: 1873 . x - the result vector 1874 1875 Notes: 1876 The vectors b and x cannot be the same. I.e., one cannot 1877 call MatSolve(A,x,x). 1878 1879 Notes: 1880 Most users should employ the simplified SLES interface for linear solvers 1881 instead of working directly with matrix algebra routines such as this. 1882 See, e.g., SLESCreate(). 1883 1884 Level: developer 1885 1886 Concepts: matrices^triangular solves 1887 1888 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd() 1889 @*/ 1890 int MatSolve(Mat mat,Vec b,Vec x) 1891 { 1892 int ierr; 1893 1894 PetscFunctionBegin; 1895 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1896 PetscValidType(mat); 1897 MatPreallocated(mat); 1898 PetscValidHeaderSpecific(b,VEC_COOKIE); 1899 PetscValidHeaderSpecific(x,VEC_COOKIE); 1900 PetscCheckSameComm(mat,b); 1901 PetscCheckSameComm(mat,x); 1902 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 1903 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 1904 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1905 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1906 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1907 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 1908 1909 if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1910 ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 1911 ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr); 1912 ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr); 1913 PetscFunctionReturn(0); 1914 } 1915 1916 #undef __FUNCT__ 1917 #define __FUNCT__ "MatForwardSolve" 1918 /* @ 1919 MatForwardSolve - Solves L x = b, given a factored matrix, A = LU. 1920 1921 Collective on Mat and Vec 1922 1923 Input Parameters: 1924 + mat - the factored matrix 1925 - b - the right-hand-side vector 1926 1927 Output Parameter: 1928 . x - the result vector 1929 1930 Notes: 1931 MatSolve() should be used for most applications, as it performs 1932 a forward solve followed by a backward solve. 1933 1934 The vectors b and x cannot be the same. I.e., one cannot 1935 call MatForwardSolve(A,x,x). 1936 1937 Most users should employ the simplified SLES interface for linear solvers 1938 instead of working directly with matrix algebra routines such as this. 1939 See, e.g., SLESCreate(). 1940 1941 Level: developer 1942 1943 Concepts: matrices^forward solves 1944 1945 .seealso: MatSolve(), MatBackwardSolve() 1946 @ */ 1947 int MatForwardSolve(Mat mat,Vec b,Vec x) 1948 { 1949 int ierr; 1950 1951 PetscFunctionBegin; 1952 PetscValidHeaderSpecific(mat,MAT_COOKIE); 1953 PetscValidType(mat); 1954 MatPreallocated(mat); 1955 PetscValidHeaderSpecific(b,VEC_COOKIE); 1956 PetscValidHeaderSpecific(x,VEC_COOKIE); 1957 PetscCheckSameComm(mat,b); 1958 PetscCheckSameComm(mat,x); 1959 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 1960 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 1961 if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 1962 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 1963 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 1964 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 1965 1966 ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 1967 ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr); 1968 ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr); 1969 PetscFunctionReturn(0); 1970 } 1971 1972 #undef __FUNCT__ 1973 #define __FUNCT__ "MatBackwardSolve" 1974 /* @ 1975 MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU. 1976 1977 Collective on Mat and Vec 1978 1979 Input Parameters: 1980 + mat - the factored matrix 1981 - b - the right-hand-side vector 1982 1983 Output Parameter: 1984 . x - the result vector 1985 1986 Notes: 1987 MatSolve() should be used for most applications, as it performs 1988 a forward solve followed by a backward solve. 1989 1990 The vectors b and x cannot be the same. I.e., one cannot 1991 call MatBackwardSolve(A,x,x). 1992 1993 Most users should employ the simplified SLES interface for linear solvers 1994 instead of working directly with matrix algebra routines such as this. 1995 See, e.g., SLESCreate(). 1996 1997 Level: developer 1998 1999 Concepts: matrices^backward solves 2000 2001 .seealso: MatSolve(), MatForwardSolve() 2002 @ */ 2003 int MatBackwardSolve(Mat mat,Vec b,Vec x) 2004 { 2005 int ierr; 2006 2007 PetscFunctionBegin; 2008 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2009 PetscValidType(mat); 2010 MatPreallocated(mat); 2011 PetscValidHeaderSpecific(b,VEC_COOKIE); 2012 PetscValidHeaderSpecific(x,VEC_COOKIE); 2013 PetscCheckSameComm(mat,b); 2014 PetscCheckSameComm(mat,x); 2015 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2016 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2017 if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2018 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2019 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2020 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2021 2022 ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2023 ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr); 2024 ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr); 2025 PetscFunctionReturn(0); 2026 } 2027 2028 #undef __FUNCT__ 2029 #define __FUNCT__ "MatSolveAdd" 2030 /*@ 2031 MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix. 2032 2033 Collective on Mat and Vec 2034 2035 Input Parameters: 2036 + mat - the factored matrix 2037 . b - the right-hand-side vector 2038 - y - the vector to be added to 2039 2040 Output Parameter: 2041 . x - the result vector 2042 2043 Notes: 2044 The vectors b and x cannot be the same. I.e., one cannot 2045 call MatSolveAdd(A,x,y,x). 2046 2047 Most users should employ the simplified SLES interface for linear solvers 2048 instead of working directly with matrix algebra routines such as this. 2049 See, e.g., SLESCreate(). 2050 2051 Level: developer 2052 2053 Concepts: matrices^triangular solves 2054 2055 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd() 2056 @*/ 2057 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x) 2058 { 2059 PetscScalar one = 1.0; 2060 Vec tmp; 2061 int ierr; 2062 2063 PetscFunctionBegin; 2064 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2065 PetscValidType(mat); 2066 MatPreallocated(mat); 2067 PetscValidHeaderSpecific(y,VEC_COOKIE); 2068 PetscValidHeaderSpecific(b,VEC_COOKIE); 2069 PetscValidHeaderSpecific(x,VEC_COOKIE); 2070 PetscCheckSameComm(mat,b); 2071 PetscCheckSameComm(mat,y); 2072 PetscCheckSameComm(mat,x); 2073 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2074 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2075 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2076 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2077 if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N); 2078 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2079 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2080 2081 ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2082 if (mat->ops->solveadd) { 2083 ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr); 2084 } else { 2085 /* do the solve then the add manually */ 2086 if (x != y) { 2087 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2088 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2089 } else { 2090 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2091 PetscLogObjectParent(mat,tmp); 2092 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2093 ierr = MatSolve(mat,b,x);CHKERRQ(ierr); 2094 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2095 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2096 } 2097 } 2098 ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr); 2099 PetscFunctionReturn(0); 2100 } 2101 2102 #undef __FUNCT__ 2103 #define __FUNCT__ "MatSolveTranspose" 2104 /*@ 2105 MatSolveTranspose - Solves A' x = b, given a factored matrix. 2106 2107 Collective on Mat and Vec 2108 2109 Input Parameters: 2110 + mat - the factored matrix 2111 - b - the right-hand-side vector 2112 2113 Output Parameter: 2114 . x - the result vector 2115 2116 Notes: 2117 The vectors b and x cannot be the same. I.e., one cannot 2118 call MatSolveTranspose(A,x,x). 2119 2120 Most users should employ the simplified SLES interface for linear solvers 2121 instead of working directly with matrix algebra routines such as this. 2122 See, e.g., SLESCreate(). 2123 2124 Level: developer 2125 2126 Concepts: matrices^triangular solves 2127 2128 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd() 2129 @*/ 2130 int MatSolveTranspose(Mat mat,Vec b,Vec x) 2131 { 2132 int ierr; 2133 2134 PetscFunctionBegin; 2135 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2136 PetscValidType(mat); 2137 MatPreallocated(mat); 2138 PetscValidHeaderSpecific(b,VEC_COOKIE); 2139 PetscValidHeaderSpecific(x,VEC_COOKIE); 2140 PetscCheckSameComm(mat,b); 2141 PetscCheckSameComm(mat,x); 2142 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2143 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2144 if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name); 2145 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2146 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2147 2148 ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2149 ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr); 2150 ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr); 2151 PetscFunctionReturn(0); 2152 } 2153 2154 #undef __FUNCT__ 2155 #define __FUNCT__ "MatSolveTransposeAdd" 2156 /*@ 2157 MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a 2158 factored matrix. 2159 2160 Collective on Mat and Vec 2161 2162 Input Parameters: 2163 + mat - the factored matrix 2164 . b - the right-hand-side vector 2165 - y - the vector to be added to 2166 2167 Output Parameter: 2168 . x - the result vector 2169 2170 Notes: 2171 The vectors b and x cannot be the same. I.e., one cannot 2172 call MatSolveTransposeAdd(A,x,y,x). 2173 2174 Most users should employ the simplified SLES interface for linear solvers 2175 instead of working directly with matrix algebra routines such as this. 2176 See, e.g., SLESCreate(). 2177 2178 Level: developer 2179 2180 Concepts: matrices^triangular solves 2181 2182 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose() 2183 @*/ 2184 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x) 2185 { 2186 PetscScalar one = 1.0; 2187 int ierr; 2188 Vec tmp; 2189 2190 PetscFunctionBegin; 2191 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2192 PetscValidType(mat); 2193 MatPreallocated(mat); 2194 PetscValidHeaderSpecific(y,VEC_COOKIE); 2195 PetscValidHeaderSpecific(b,VEC_COOKIE); 2196 PetscValidHeaderSpecific(x,VEC_COOKIE); 2197 PetscCheckSameComm(mat,b); 2198 PetscCheckSameComm(mat,y); 2199 PetscCheckSameComm(mat,x); 2200 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 2201 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 2202 if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N); 2203 if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N); 2204 if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N); 2205 if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n); 2206 2207 ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2208 if (mat->ops->solvetransposeadd) { 2209 ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr); 2210 } else { 2211 /* do the solve then the add manually */ 2212 if (x != y) { 2213 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2214 ierr = VecAXPY(&one,y,x);CHKERRQ(ierr); 2215 } else { 2216 ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr); 2217 PetscLogObjectParent(mat,tmp); 2218 ierr = VecCopy(x,tmp);CHKERRQ(ierr); 2219 ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr); 2220 ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr); 2221 ierr = VecDestroy(tmp);CHKERRQ(ierr); 2222 } 2223 } 2224 ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr); 2225 PetscFunctionReturn(0); 2226 } 2227 /* ----------------------------------------------------------------*/ 2228 2229 #undef __FUNCT__ 2230 #define __FUNCT__ "MatRelax" 2231 /*@ 2232 MatRelax - Computes one relaxation sweep. 2233 2234 Collective on Mat and Vec 2235 2236 Input Parameters: 2237 + mat - the matrix 2238 . b - the right hand side 2239 . omega - the relaxation factor 2240 . flag - flag indicating the type of SOR (see below) 2241 . shift - diagonal shift 2242 - its - the number of iterations 2243 - lits - the number of local iterations 2244 2245 Output Parameters: 2246 . x - the solution (can contain an initial guess) 2247 2248 SOR Flags: 2249 . SOR_FORWARD_SWEEP - forward SOR 2250 . SOR_BACKWARD_SWEEP - backward SOR 2251 . SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR) 2252 . SOR_LOCAL_FORWARD_SWEEP - local forward SOR 2253 . SOR_LOCAL_BACKWARD_SWEEP - local forward SOR 2254 . SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR 2255 . SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies 2256 upper/lower triangular part of matrix to 2257 vector (with omega) 2258 . SOR_ZERO_INITIAL_GUESS - zero initial guess 2259 2260 Notes: 2261 SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and 2262 SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings 2263 on each processor. 2264 2265 Application programmers will not generally use MatRelax() directly, 2266 but instead will employ the SLES/PC interface. 2267 2268 Notes for Advanced Users: 2269 The flags are implemented as bitwise inclusive or operations. 2270 For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP) 2271 to specify a zero initial guess for SSOR. 2272 2273 Most users should employ the simplified SLES interface for linear solvers 2274 instead of working directly with matrix algebra routines such as this. 2275 See, e.g., SLESCreate(). 2276 2277 Level: developer 2278 2279 Concepts: matrices^relaxation 2280 Concepts: matrices^SOR 2281 Concepts: matrices^Gauss-Seidel 2282 2283 @*/ 2284 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,int lits,Vec x) 2285 { 2286 int ierr; 2287 2288 PetscFunctionBegin; 2289 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2290 PetscValidType(mat); 2291 MatPreallocated(mat); 2292 PetscValidHeaderSpecific(b,VEC_COOKIE); 2293 PetscValidHeaderSpecific(x,VEC_COOKIE); 2294 PetscCheckSameComm(mat,b); 2295 PetscCheckSameComm(mat,x); 2296 if (!mat->ops->relax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2297 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2298 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2299 if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N); 2300 if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N); 2301 if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n); 2302 2303 ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2304 ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr); 2305 ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr); 2306 PetscFunctionReturn(0); 2307 } 2308 2309 #undef __FUNCT__ 2310 #define __FUNCT__ "MatCopy_Basic" 2311 /* 2312 Default matrix copy routine. 2313 */ 2314 int MatCopy_Basic(Mat A,Mat B,MatStructure str) 2315 { 2316 int ierr,i,rstart,rend,nz,*cwork; 2317 PetscScalar *vwork; 2318 2319 PetscFunctionBegin; 2320 ierr = MatZeroEntries(B);CHKERRQ(ierr); 2321 ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr); 2322 for (i=rstart; i<rend; i++) { 2323 ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2324 ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr); 2325 ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr); 2326 } 2327 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2328 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr); 2329 PetscFunctionReturn(0); 2330 } 2331 2332 #undef __FUNCT__ 2333 #define __FUNCT__ "MatCopy" 2334 /*@C 2335 MatCopy - Copys a matrix to another matrix. 2336 2337 Collective on Mat 2338 2339 Input Parameters: 2340 + A - the matrix 2341 - str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN 2342 2343 Output Parameter: 2344 . B - where the copy is put 2345 2346 Notes: 2347 If you use SAME_NONZERO_PATTERN then the two matrices had better have the 2348 same nonzero pattern or the routine will crash. 2349 2350 MatCopy() copies the matrix entries of a matrix to another existing 2351 matrix (after first zeroing the second matrix). A related routine is 2352 MatConvert(), which first creates a new matrix and then copies the data. 2353 2354 Level: intermediate 2355 2356 Concepts: matrices^copying 2357 2358 .seealso: MatConvert(), MatDuplicate() 2359 2360 @*/ 2361 int MatCopy(Mat A,Mat B,MatStructure str) 2362 { 2363 int ierr; 2364 2365 PetscFunctionBegin; 2366 PetscValidHeaderSpecific(A,MAT_COOKIE); 2367 PetscValidHeaderSpecific(B,MAT_COOKIE); 2368 PetscValidType(A); 2369 MatPreallocated(A); 2370 PetscValidType(B); 2371 MatPreallocated(B); 2372 PetscCheckSameComm(A,B); 2373 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2374 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2375 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim (%d,%d) (%d,%d)",A->M,B->M, 2376 A->N,B->N); 2377 2378 ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2379 if (A->ops->copy) { 2380 ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr); 2381 } else { /* generic conversion */ 2382 ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr); 2383 } 2384 if (A->mapping) { 2385 if (B->mapping) {ierr = ISLocalToGlobalMappingDestroy(B->mapping);CHKERRQ(ierr);B->mapping = 0;} 2386 ierr = MatSetLocalToGlobalMapping(B,A->mapping);CHKERRQ(ierr); 2387 } 2388 if (A->bmapping) { 2389 if (B->bmapping) {ierr = ISLocalToGlobalMappingDestroy(B->bmapping);CHKERRQ(ierr);B->bmapping = 0;} 2390 ierr = MatSetLocalToGlobalMappingBlock(B,A->mapping);CHKERRQ(ierr); 2391 } 2392 ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr); 2393 PetscFunctionReturn(0); 2394 } 2395 2396 #include "petscsys.h" 2397 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE; 2398 PetscFList MatConvertList = 0; 2399 2400 #undef __FUNCT__ 2401 #define __FUNCT__ "MatConvertRegister" 2402 /*@C 2403 MatConvertRegister - Allows one to register a routine that converts a sparse matrix 2404 from one format to another. 2405 2406 Not Collective 2407 2408 Input Parameters: 2409 + type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ. 2410 - Converter - the function that reads the matrix from the binary file. 2411 2412 Level: developer 2413 2414 .seealso: MatConvertRegisterAll(), MatConvert() 2415 2416 @*/ 2417 int MatConvertRegister(char *sname,char *path,char *name,int (*function)(Mat,MatType,Mat*)) 2418 { 2419 int ierr; 2420 char fullname[PETSC_MAX_PATH_LEN]; 2421 2422 PetscFunctionBegin; 2423 ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr); 2424 ierr = PetscFListAdd(&MatConvertList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr); 2425 PetscFunctionReturn(0); 2426 } 2427 2428 #undef __FUNCT__ 2429 #define __FUNCT__ "MatConvert" 2430 /*@C 2431 MatConvert - Converts a matrix to another matrix, either of the same 2432 or different type. 2433 2434 Collective on Mat 2435 2436 Input Parameters: 2437 + mat - the matrix 2438 - newtype - new matrix type. Use MATSAME to create a new matrix of the 2439 same type as the original matrix. 2440 2441 Output Parameter: 2442 . M - pointer to place new matrix 2443 2444 Notes: 2445 MatConvert() first creates a new matrix and then copies the data from 2446 the first matrix. A related routine is MatCopy(), which copies the matrix 2447 entries of one matrix to another already existing matrix context. 2448 2449 Level: intermediate 2450 2451 Concepts: matrices^converting between storage formats 2452 2453 .seealso: MatCopy(), MatDuplicate() 2454 @*/ 2455 int MatConvert(Mat mat,MatType newtype,Mat *M) 2456 { 2457 int ierr; 2458 PetscTruth sametype,issame,flg; 2459 char convname[256],mtype[256]; 2460 2461 PetscFunctionBegin; 2462 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2463 PetscValidType(mat); 2464 MatPreallocated(mat); 2465 PetscValidPointer(M); 2466 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2467 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2468 2469 ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr); 2470 if (flg) { 2471 newtype = mtype; 2472 } 2473 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2474 2475 ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr); 2476 ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr); 2477 if ((sametype || issame) && mat->ops->duplicate) { 2478 ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr); 2479 } else { 2480 int (*conv)(Mat,MatType,Mat*); 2481 if (!MatConvertRegisterAllCalled) { 2482 ierr = MatConvertRegisterAll(PETSC_NULL);CHKERRQ(ierr); 2483 } 2484 ierr = PetscFListFind(mat->comm,MatConvertList,newtype,(void(**)(void))&conv);CHKERRQ(ierr); 2485 if (conv) { 2486 ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr); 2487 } else { 2488 ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr); 2489 ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr); 2490 ierr = PetscStrcat(convname,"_");CHKERRQ(ierr); 2491 ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr); 2492 ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr); 2493 ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr); 2494 if (conv) { 2495 ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr); 2496 } else { 2497 if (mat->ops->convert) { 2498 ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr); 2499 } else { 2500 ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr); 2501 } 2502 } 2503 } 2504 } 2505 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2506 PetscFunctionReturn(0); 2507 } 2508 2509 2510 #undef __FUNCT__ 2511 #define __FUNCT__ "MatDuplicate" 2512 /*@C 2513 MatDuplicate - Duplicates a matrix including the non-zero structure. 2514 2515 Collective on Mat 2516 2517 Input Parameters: 2518 + mat - the matrix 2519 - op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero 2520 values as well or not 2521 2522 Output Parameter: 2523 . M - pointer to place new matrix 2524 2525 Level: intermediate 2526 2527 Concepts: matrices^duplicating 2528 2529 .seealso: MatCopy(), MatConvert() 2530 @*/ 2531 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M) 2532 { 2533 int ierr; 2534 2535 PetscFunctionBegin; 2536 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2537 PetscValidType(mat); 2538 MatPreallocated(mat); 2539 PetscValidPointer(M); 2540 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2541 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2542 2543 *M = 0; 2544 ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2545 if (!mat->ops->duplicate) { 2546 SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type"); 2547 } 2548 ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr); 2549 if (mat->mapping) { 2550 ierr = MatSetLocalToGlobalMapping(*M,mat->mapping);CHKERRQ(ierr); 2551 } 2552 if (mat->bmapping) { 2553 ierr = MatSetLocalToGlobalMappingBlock(*M,mat->mapping);CHKERRQ(ierr); 2554 } 2555 ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr); 2556 PetscFunctionReturn(0); 2557 } 2558 2559 #undef __FUNCT__ 2560 #define __FUNCT__ "MatGetDiagonal" 2561 /*@ 2562 MatGetDiagonal - Gets the diagonal of a matrix. 2563 2564 Collective on Mat and Vec 2565 2566 Input Parameters: 2567 + mat - the matrix 2568 - v - the vector for storing the diagonal 2569 2570 Output Parameter: 2571 . v - the diagonal of the matrix 2572 2573 Notes: 2574 For the SeqAIJ matrix format, this routine may also be called 2575 on a LU factored matrix; in that case it routines the reciprocal of 2576 the diagonal entries in U. It returns the entries permuted by the 2577 row and column permutation used during the symbolic factorization. 2578 2579 Level: intermediate 2580 2581 Concepts: matrices^accessing diagonals 2582 2583 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax() 2584 @*/ 2585 int MatGetDiagonal(Mat mat,Vec v) 2586 { 2587 int ierr; 2588 2589 PetscFunctionBegin; 2590 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2591 PetscValidType(mat); 2592 MatPreallocated(mat); 2593 PetscValidHeaderSpecific(v,VEC_COOKIE); 2594 /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */ 2595 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2596 if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2597 2598 ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr); 2599 PetscFunctionReturn(0); 2600 } 2601 2602 #undef __FUNCT__ 2603 #define __FUNCT__ "MatGetRowMax" 2604 /*@ 2605 MatGetRowMax - Gets the maximum value (in absolute value) of each 2606 row of the matrix 2607 2608 Collective on Mat and Vec 2609 2610 Input Parameters: 2611 . mat - the matrix 2612 2613 Output Parameter: 2614 . v - the vector for storing the maximums 2615 2616 Level: intermediate 2617 2618 Concepts: matrices^getting row maximums 2619 2620 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix() 2621 @*/ 2622 int MatGetRowMax(Mat mat,Vec v) 2623 { 2624 int ierr; 2625 2626 PetscFunctionBegin; 2627 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2628 PetscValidType(mat); 2629 MatPreallocated(mat); 2630 PetscValidHeaderSpecific(v,VEC_COOKIE); 2631 /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */ 2632 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2633 if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2634 2635 ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr); 2636 PetscFunctionReturn(0); 2637 } 2638 2639 #undef __FUNCT__ 2640 #define __FUNCT__ "MatTranspose" 2641 /*@C 2642 MatTranspose - Computes an in-place or out-of-place transpose of a matrix. 2643 2644 Collective on Mat 2645 2646 Input Parameter: 2647 . mat - the matrix to transpose 2648 2649 Output Parameters: 2650 . B - the transpose (or pass in PETSC_NULL for an in-place transpose) 2651 2652 Level: intermediate 2653 2654 Concepts: matrices^transposing 2655 2656 .seealso: MatMultTranspose(), MatMultTransposeAdd(), MatIsSymmetric() 2657 @*/ 2658 int MatTranspose(Mat mat,Mat *B) 2659 { 2660 int ierr; 2661 2662 PetscFunctionBegin; 2663 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2664 PetscValidType(mat); 2665 MatPreallocated(mat); 2666 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2667 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2668 if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2669 2670 ierr = PetscLogEventBegin(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2671 ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr); 2672 ierr = PetscLogEventEnd(MAT_Transpose,mat,0,0,0);CHKERRQ(ierr); 2673 PetscFunctionReturn(0); 2674 } 2675 2676 #undef __FUNCT__ 2677 #define __FUNCT__ "MatIsSymmetric" 2678 /*@C 2679 MatIsSymmetric - Test whether a matrix is another one's transpose, 2680 or its own, in which case it tests symmetry. 2681 2682 Collective on Mat 2683 2684 Input Parameter: 2685 + A - the matrix to test 2686 - B - the matrix to test against, this can equal the first parameter 2687 2688 Output Parameters: 2689 . flg - the result 2690 2691 Notes: 2692 Only available for SeqAIJ/MPIAIJ matrices. The sequential algorithm 2693 has a running time of the order of the number of nonzeros; the parallel 2694 test involves parallel copies of the block-offdiagonal parts of the matrix. 2695 2696 Level: intermediate 2697 2698 Concepts: matrices^transposing, matrix^symmetry 2699 2700 .seealso: MatTranspose() 2701 @*/ 2702 int MatIsSymmetric(Mat A,Mat B,PetscTruth *flg) 2703 { 2704 int ierr,(*f)(Mat,Mat,PetscTruth*),(*g)(Mat,Mat,PetscTruth*); 2705 2706 PetscFunctionBegin; 2707 PetscValidHeaderSpecific(A,MAT_COOKIE); 2708 PetscValidHeaderSpecific(B,MAT_COOKIE); 2709 ierr = PetscObjectQueryFunction((PetscObject)A,"MatIsSymmetric_C",(void (**)(void))&f);CHKERRQ(ierr); 2710 ierr = PetscObjectQueryFunction((PetscObject)B,"MatIsSymmetric_C",(void (**)(void))&g);CHKERRQ(ierr); 2711 if (f && g) { 2712 if (f==g) { 2713 ierr = (*f)(A,B,flg);CHKERRQ(ierr); 2714 } else { 2715 SETERRQ(1,"Matrices do not have the same comparator for symmetry test"); 2716 } 2717 } 2718 PetscFunctionReturn(0); 2719 } 2720 2721 #undef __FUNCT__ 2722 #define __FUNCT__ "MatPermute" 2723 /*@C 2724 MatPermute - Creates a new matrix with rows and columns permuted from the 2725 original. 2726 2727 Collective on Mat 2728 2729 Input Parameters: 2730 + mat - the matrix to permute 2731 . row - row permutation, each processor supplies only the permutation for its rows 2732 - col - column permutation, each processor needs the entire column permutation, that is 2733 this is the same size as the total number of columns in the matrix 2734 2735 Output Parameters: 2736 . B - the permuted matrix 2737 2738 Level: advanced 2739 2740 Concepts: matrices^permuting 2741 2742 .seealso: MatGetOrdering() 2743 @*/ 2744 int MatPermute(Mat mat,IS row,IS col,Mat *B) 2745 { 2746 int ierr; 2747 2748 PetscFunctionBegin; 2749 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2750 PetscValidType(mat); 2751 MatPreallocated(mat); 2752 PetscValidHeaderSpecific(row,IS_COOKIE); 2753 PetscValidHeaderSpecific(col,IS_COOKIE); 2754 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2755 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2756 if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2757 ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr); 2758 PetscFunctionReturn(0); 2759 } 2760 2761 #undef __FUNCT__ 2762 #define __FUNCT__ "MatPermuteSparsify" 2763 /*@C 2764 MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the 2765 original and sparsified to the prescribed tolerance. 2766 2767 Collective on Mat 2768 2769 Input Parameters: 2770 + A - The matrix to permute 2771 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE 2772 . frac - The half-bandwidth as a fraction of the total size, or 0.0 2773 . tol - The drop tolerance 2774 . rowp - The row permutation 2775 - colp - The column permutation 2776 2777 Output Parameter: 2778 . B - The permuted, sparsified matrix 2779 2780 Level: advanced 2781 2782 Note: 2783 The default behavior (band = PETSC_DECIDE and frac = 0.0) is to 2784 restrict the half-bandwidth of the resulting matrix to 5% of the 2785 total matrix size. 2786 2787 .keywords: matrix, permute, sparsify 2788 2789 .seealso: MatGetOrdering(), MatPermute() 2790 @*/ 2791 int MatPermuteSparsify(Mat A, int band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B) 2792 { 2793 IS irowp, icolp; 2794 int *rows, *cols; 2795 int M, N, locRowStart, locRowEnd; 2796 int nz, newNz; 2797 int *cwork, *cnew; 2798 PetscScalar *vwork, *vnew; 2799 int bw, size; 2800 int row, locRow, newRow, col, newCol; 2801 int ierr; 2802 2803 PetscFunctionBegin; 2804 PetscValidHeaderSpecific(A, MAT_COOKIE); 2805 PetscValidHeaderSpecific(rowp, IS_COOKIE); 2806 PetscValidHeaderSpecific(colp, IS_COOKIE); 2807 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix"); 2808 if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix"); 2809 if (!A->ops->permutesparsify) { 2810 ierr = MatGetSize(A, &M, &N); CHKERRQ(ierr); 2811 ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd); CHKERRQ(ierr); 2812 ierr = ISGetSize(rowp, &size); CHKERRQ(ierr); 2813 if (size != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for row permutation, should be %d", size, M); 2814 ierr = ISGetSize(colp, &size); CHKERRQ(ierr); 2815 if (size != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for column permutation, should be %d", size, N); 2816 ierr = ISInvertPermutation(rowp, 0, &irowp); CHKERRQ(ierr); 2817 ierr = ISGetIndices(irowp, &rows); CHKERRQ(ierr); 2818 ierr = ISInvertPermutation(colp, 0, &icolp); CHKERRQ(ierr); 2819 ierr = ISGetIndices(icolp, &cols); CHKERRQ(ierr); 2820 ierr = PetscMalloc(N * sizeof(int), &cnew); CHKERRQ(ierr); 2821 ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew); CHKERRQ(ierr); 2822 2823 /* Setup bandwidth to include */ 2824 if (band == PETSC_DECIDE) { 2825 if (frac <= 0.0) 2826 bw = (int) (M * 0.05); 2827 else 2828 bw = (int) (M * frac); 2829 } else { 2830 if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer"); 2831 bw = band; 2832 } 2833 2834 /* Put values into new matrix */ 2835 ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B); CHKERRQ(ierr); 2836 for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) { 2837 ierr = MatGetRow(A, row, &nz, &cwork, &vwork); CHKERRQ(ierr); 2838 newRow = rows[locRow]+locRowStart; 2839 for(col = 0, newNz = 0; col < nz; col++) { 2840 newCol = cols[cwork[col]]; 2841 if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) { 2842 cnew[newNz] = newCol; 2843 vnew[newNz] = vwork[col]; 2844 newNz++; 2845 } 2846 } 2847 ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES); CHKERRQ(ierr); 2848 ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork); CHKERRQ(ierr); 2849 } 2850 ierr = PetscFree(cnew); CHKERRQ(ierr); 2851 ierr = PetscFree(vnew); CHKERRQ(ierr); 2852 ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2853 ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY); CHKERRQ(ierr); 2854 ierr = ISRestoreIndices(irowp, &rows); CHKERRQ(ierr); 2855 ierr = ISRestoreIndices(icolp, &cols); CHKERRQ(ierr); 2856 ierr = ISDestroy(irowp); CHKERRQ(ierr); 2857 ierr = ISDestroy(icolp); CHKERRQ(ierr); 2858 } else { 2859 ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B); CHKERRQ(ierr); 2860 } 2861 PetscFunctionReturn(0); 2862 } 2863 2864 #undef __FUNCT__ 2865 #define __FUNCT__ "MatEqual" 2866 /*@ 2867 MatEqual - Compares two matrices. 2868 2869 Collective on Mat 2870 2871 Input Parameters: 2872 + A - the first matrix 2873 - B - the second matrix 2874 2875 Output Parameter: 2876 . flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise. 2877 2878 Level: intermediate 2879 2880 Concepts: matrices^equality between 2881 @*/ 2882 int MatEqual(Mat A,Mat B,PetscTruth *flg) 2883 { 2884 int ierr; 2885 2886 PetscFunctionBegin; 2887 PetscValidHeaderSpecific(A,MAT_COOKIE); 2888 PetscValidHeaderSpecific(B,MAT_COOKIE); 2889 PetscValidType(A); 2890 MatPreallocated(A); 2891 PetscValidType(B); 2892 MatPreallocated(B); 2893 PetscValidIntPointer(flg); 2894 PetscCheckSameComm(A,B); 2895 if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2896 if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2897 if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d %d %d",A->M,B->M,A->N,B->N); 2898 if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name); 2899 if (!B->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",B->type_name); 2900 if (A->ops->equal != B->ops->equal) SETERRQ2(PETSC_ERR_ARG_INCOMP,"A is type: %s\nB is type: %s",A->type_name,B->type_name); 2901 ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr); 2902 PetscFunctionReturn(0); 2903 } 2904 2905 #undef __FUNCT__ 2906 #define __FUNCT__ "MatDiagonalScale" 2907 /*@ 2908 MatDiagonalScale - Scales a matrix on the left and right by diagonal 2909 matrices that are stored as vectors. Either of the two scaling 2910 matrices can be PETSC_NULL. 2911 2912 Collective on Mat 2913 2914 Input Parameters: 2915 + mat - the matrix to be scaled 2916 . l - the left scaling vector (or PETSC_NULL) 2917 - r - the right scaling vector (or PETSC_NULL) 2918 2919 Notes: 2920 MatDiagonalScale() computes A = LAR, where 2921 L = a diagonal matrix, R = a diagonal matrix 2922 2923 Level: intermediate 2924 2925 Concepts: matrices^diagonal scaling 2926 Concepts: diagonal scaling of matrices 2927 2928 .seealso: MatScale() 2929 @*/ 2930 int MatDiagonalScale(Mat mat,Vec l,Vec r) 2931 { 2932 int ierr; 2933 2934 PetscFunctionBegin; 2935 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2936 PetscValidType(mat); 2937 MatPreallocated(mat); 2938 if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2939 if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);} 2940 if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);} 2941 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2942 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2943 2944 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2945 ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr); 2946 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2947 PetscFunctionReturn(0); 2948 } 2949 2950 #undef __FUNCT__ 2951 #define __FUNCT__ "MatScale" 2952 /*@ 2953 MatScale - Scales all elements of a matrix by a given number. 2954 2955 Collective on Mat 2956 2957 Input Parameters: 2958 + mat - the matrix to be scaled 2959 - a - the scaling value 2960 2961 Output Parameter: 2962 . mat - the scaled matrix 2963 2964 Level: intermediate 2965 2966 Concepts: matrices^scaling all entries 2967 2968 .seealso: MatDiagonalScale() 2969 @*/ 2970 int MatScale(const PetscScalar *a,Mat mat) 2971 { 2972 int ierr; 2973 2974 PetscFunctionBegin; 2975 PetscValidHeaderSpecific(mat,MAT_COOKIE); 2976 PetscValidType(mat); 2977 MatPreallocated(mat); 2978 PetscValidScalarPointer(a); 2979 if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 2980 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 2981 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 2982 2983 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2984 ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr); 2985 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 2986 PetscFunctionReturn(0); 2987 } 2988 2989 #undef __FUNCT__ 2990 #define __FUNCT__ "MatNorm" 2991 /*@ 2992 MatNorm - Calculates various norms of a matrix. 2993 2994 Collective on Mat 2995 2996 Input Parameters: 2997 + mat - the matrix 2998 - type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY 2999 3000 Output Parameters: 3001 . nrm - the resulting norm 3002 3003 Level: intermediate 3004 3005 Concepts: matrices^norm 3006 Concepts: norm^of matrix 3007 @*/ 3008 int MatNorm(Mat mat,NormType type,PetscReal *nrm) 3009 { 3010 int ierr; 3011 3012 PetscFunctionBegin; 3013 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3014 PetscValidType(mat); 3015 MatPreallocated(mat); 3016 PetscValidScalarPointer(nrm); 3017 3018 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3019 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3020 if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3021 ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr); 3022 PetscFunctionReturn(0); 3023 } 3024 3025 /* 3026 This variable is used to prevent counting of MatAssemblyBegin() that 3027 are called from within a MatAssemblyEnd(). 3028 */ 3029 static int MatAssemblyEnd_InUse = 0; 3030 #undef __FUNCT__ 3031 #define __FUNCT__ "MatAssemblyBegin" 3032 /*@ 3033 MatAssemblyBegin - Begins assembling the matrix. This routine should 3034 be called after completing all calls to MatSetValues(). 3035 3036 Collective on Mat 3037 3038 Input Parameters: 3039 + mat - the matrix 3040 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 3041 3042 Notes: 3043 MatSetValues() generally caches the values. The matrix is ready to 3044 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 3045 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 3046 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 3047 using the matrix. 3048 3049 Level: beginner 3050 3051 Concepts: matrices^assembling 3052 3053 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled() 3054 @*/ 3055 int MatAssemblyBegin(Mat mat,MatAssemblyType type) 3056 { 3057 int ierr; 3058 3059 PetscFunctionBegin; 3060 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3061 PetscValidType(mat); 3062 MatPreallocated(mat); 3063 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?"); 3064 if (mat->assembled) { 3065 mat->was_assembled = PETSC_TRUE; 3066 mat->assembled = PETSC_FALSE; 3067 } 3068 if (!MatAssemblyEnd_InUse) { 3069 ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3070 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3071 ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr); 3072 } else { 3073 if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);} 3074 } 3075 PetscFunctionReturn(0); 3076 } 3077 3078 #undef __FUNCT__ 3079 #define __FUNCT__ "MatAssembed" 3080 /*@ 3081 MatAssembled - Indicates if a matrix has been assembled and is ready for 3082 use; for example, in matrix-vector product. 3083 3084 Collective on Mat 3085 3086 Input Parameter: 3087 . mat - the matrix 3088 3089 Output Parameter: 3090 . assembled - PETSC_TRUE or PETSC_FALSE 3091 3092 Level: advanced 3093 3094 Concepts: matrices^assembled? 3095 3096 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin() 3097 @*/ 3098 int MatAssembled(Mat mat,PetscTruth *assembled) 3099 { 3100 PetscFunctionBegin; 3101 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3102 PetscValidType(mat); 3103 MatPreallocated(mat); 3104 *assembled = mat->assembled; 3105 PetscFunctionReturn(0); 3106 } 3107 3108 #undef __FUNCT__ 3109 #define __FUNCT__ "MatView_Private" 3110 /* 3111 Processes command line options to determine if/how a matrix 3112 is to be viewed. Called by MatAssemblyEnd() and MatLoad(). 3113 */ 3114 int MatView_Private(Mat mat) 3115 { 3116 int ierr; 3117 PetscTruth flg; 3118 static PetscTruth incall = PETSC_FALSE; 3119 3120 PetscFunctionBegin; 3121 if (incall) PetscFunctionReturn(0); 3122 incall = PETSC_TRUE; 3123 ierr = PetscOptionsBegin(mat->comm,mat->prefix,"Matrix Options","Mat");CHKERRQ(ierr); 3124 ierr = PetscOptionsName("-mat_view_info","Information on matrix size","MatView",&flg);CHKERRQ(ierr); 3125 if (flg) { 3126 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr); 3127 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3128 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3129 } 3130 ierr = PetscOptionsName("-mat_view_info_detailed","Nonzeros in the matrix","MatView",&flg);CHKERRQ(ierr); 3131 if (flg) { 3132 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr); 3133 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3134 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3135 } 3136 ierr = PetscOptionsName("-mat_view","Print matrix to stdout","MatView",&flg);CHKERRQ(ierr); 3137 if (flg) { 3138 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3139 } 3140 ierr = PetscOptionsName("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",&flg);CHKERRQ(ierr); 3141 if (flg) { 3142 ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); 3143 ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3144 ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr); 3145 } 3146 ierr = PetscOptionsName("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",&flg);CHKERRQ(ierr); 3147 if (flg) { 3148 ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3149 ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr); 3150 } 3151 ierr = PetscOptionsName("-mat_view_binary","Save matrix to file in binary format","MatView",&flg);CHKERRQ(ierr); 3152 if (flg) { 3153 ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3154 ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr); 3155 } 3156 ierr = PetscOptionsEnd();CHKERRQ(ierr); 3157 /* cannot have inside PetscOptionsBegin() because uses PetscOptionsBegin() */ 3158 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr); 3159 if (flg) { 3160 ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr); 3161 if (flg) { 3162 PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr); 3163 } 3164 ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3165 ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3166 if (flg) { 3167 PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr); 3168 } 3169 } 3170 incall = PETSC_FALSE; 3171 PetscFunctionReturn(0); 3172 } 3173 3174 #undef __FUNCT__ 3175 #define __FUNCT__ "MatAssemblyEnd" 3176 /*@ 3177 MatAssemblyEnd - Completes assembling the matrix. This routine should 3178 be called after MatAssemblyBegin(). 3179 3180 Collective on Mat 3181 3182 Input Parameters: 3183 + mat - the matrix 3184 - type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY 3185 3186 Options Database Keys: 3187 + -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly() 3188 . -mat_view_info_detailed - Prints more detailed info 3189 . -mat_view - Prints matrix in ASCII format 3190 . -mat_view_matlab - Prints matrix in Matlab format 3191 . -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX(). 3192 . -display <name> - Sets display name (default is host) 3193 . -draw_pause <sec> - Sets number of seconds to pause after display 3194 . -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual) 3195 . -viewer_socket_machine <machine> 3196 . -viewer_socket_port <port> 3197 . -mat_view_binary - save matrix to file in binary format 3198 - -viewer_binary_filename <name> 3199 3200 Notes: 3201 MatSetValues() generally caches the values. The matrix is ready to 3202 use only after MatAssemblyBegin() and MatAssemblyEnd() have been called. 3203 Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES 3204 in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before 3205 using the matrix. 3206 3207 Level: beginner 3208 3209 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled(), PetscViewerSocketOpen() 3210 @*/ 3211 int MatAssemblyEnd(Mat mat,MatAssemblyType type) 3212 { 3213 int ierr; 3214 static int inassm = 0; 3215 PetscTruth flg; 3216 3217 PetscFunctionBegin; 3218 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3219 PetscValidType(mat); 3220 MatPreallocated(mat); 3221 3222 inassm++; 3223 MatAssemblyEnd_InUse++; 3224 if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */ 3225 ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3226 if (mat->ops->assemblyend) { 3227 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3228 } 3229 ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr); 3230 } else { 3231 if (mat->ops->assemblyend) { 3232 ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr); 3233 } 3234 } 3235 3236 /* Flush assembly is not a true assembly */ 3237 if (type != MAT_FLUSH_ASSEMBLY) { 3238 mat->assembled = PETSC_TRUE; mat->num_ass++; 3239 } 3240 mat->insertmode = NOT_SET_VALUES; 3241 MatAssemblyEnd_InUse--; 3242 3243 if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) { 3244 ierr = MatView_Private(mat);CHKERRQ(ierr); 3245 } 3246 inassm--; 3247 ierr = PetscOptionsHasName(mat->prefix,"-help",&flg);CHKERRQ(ierr); 3248 if (flg) { 3249 ierr = MatPrintHelp(mat);CHKERRQ(ierr); 3250 } 3251 PetscFunctionReturn(0); 3252 } 3253 3254 3255 #undef __FUNCT__ 3256 #define __FUNCT__ "MatCompress" 3257 /*@ 3258 MatCompress - Tries to store the matrix in as little space as 3259 possible. May fail if memory is already fully used, since it 3260 tries to allocate new space. 3261 3262 Collective on Mat 3263 3264 Input Parameters: 3265 . mat - the matrix 3266 3267 Level: advanced 3268 3269 @*/ 3270 int MatCompress(Mat mat) 3271 { 3272 int ierr; 3273 3274 PetscFunctionBegin; 3275 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3276 PetscValidType(mat); 3277 MatPreallocated(mat); 3278 if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);} 3279 PetscFunctionReturn(0); 3280 } 3281 3282 #undef __FUNCT__ 3283 #define __FUNCT__ "MatSetOption" 3284 /*@ 3285 MatSetOption - Sets a parameter option for a matrix. Some options 3286 may be specific to certain storage formats. Some options 3287 determine how values will be inserted (or added). Sorted, 3288 row-oriented input will generally assemble the fastest. The default 3289 is row-oriented, nonsorted input. 3290 3291 Collective on Mat 3292 3293 Input Parameters: 3294 + mat - the matrix 3295 - option - the option, one of those listed below (and possibly others), 3296 e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR 3297 3298 Options Describing Matrix Structure: 3299 + MAT_SYMMETRIC - symmetric in terms of both structure and value 3300 - MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure 3301 3302 Options For Use with MatSetValues(): 3303 Insert a logically dense subblock, which can be 3304 + MAT_ROW_ORIENTED - row-oriented (default) 3305 . MAT_COLUMN_ORIENTED - column-oriented 3306 . MAT_ROWS_SORTED - sorted by row 3307 . MAT_ROWS_UNSORTED - not sorted by row (default) 3308 . MAT_COLUMNS_SORTED - sorted by column 3309 - MAT_COLUMNS_UNSORTED - not sorted by column (default) 3310 3311 Not these options reflect the data you pass in with MatSetValues(); it has 3312 nothing to do with how the data is stored internally in the matrix 3313 data structure. 3314 3315 When (re)assembling a matrix, we can restrict the input for 3316 efficiency/debugging purposes. These options include 3317 + MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be 3318 allowed if they generate a new nonzero 3319 . MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed 3320 . MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if 3321 they generate a nonzero in a new diagonal (for block diagonal format only) 3322 . MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only) 3323 . MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries 3324 . MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry 3325 - MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly 3326 3327 Notes: 3328 Some options are relevant only for particular matrix types and 3329 are thus ignored by others. Other options are not supported by 3330 certain matrix types and will generate an error message if set. 3331 3332 If using a Fortran 77 module to compute a matrix, one may need to 3333 use the column-oriented option (or convert to the row-oriented 3334 format). 3335 3336 MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion 3337 that would generate a new entry in the nonzero structure is instead 3338 ignored. Thus, if memory has not alredy been allocated for this particular 3339 data, then the insertion is ignored. For dense matrices, in which 3340 the entire array is allocated, no entries are ever ignored. 3341 Set after the first MatAssemblyEnd() 3342 3343 MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion 3344 that would generate a new entry in the nonzero structure instead produces 3345 an error. (Currently supported for AIJ and BAIJ formats only.) 3346 This is a useful flag when using SAME_NONZERO_PATTERN in calling 3347 SLESSetOperators() to ensure that the nonzero pattern truely does 3348 remain unchanged. Set after the first MatAssemblyEnd() 3349 3350 MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion 3351 that would generate a new entry that has not been preallocated will 3352 instead produce an error. (Currently supported for AIJ and BAIJ formats 3353 only.) This is a useful flag when debugging matrix memory preallocation. 3354 3355 MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for 3356 other processors should be dropped, rather than stashed. 3357 This is useful if you know that the "owning" processor is also 3358 always generating the correct matrix entries, so that PETSc need 3359 not transfer duplicate entries generated on another processor. 3360 3361 MAT_USE_HASH_TABLE indicates that a hash table be used to improve the 3362 searches during matrix assembly. When this flag is set, the hash table 3363 is created during the first Matrix Assembly. This hash table is 3364 used the next time through, during MatSetVaules()/MatSetVaulesBlocked() 3365 to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag 3366 should be used with MAT_USE_HASH_TABLE flag. This option is currently 3367 supported by MATMPIBAIJ format only. 3368 3369 MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries 3370 are kept in the nonzero structure 3371 3372 MAT_IGNORE_ZERO_ENTRIES - for AIJ matrices this will stop zero values from creating 3373 a zero location in the matrix 3374 3375 MAT_USE_INODES - indicates using inode version of the code - works with AIJ and 3376 ROWBS matrix types 3377 3378 MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works 3379 with AIJ and ROWBS matrix types 3380 3381 Level: intermediate 3382 3383 Concepts: matrices^setting options 3384 3385 @*/ 3386 int MatSetOption(Mat mat,MatOption op) 3387 { 3388 int ierr; 3389 3390 PetscFunctionBegin; 3391 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3392 PetscValidType(mat); 3393 MatPreallocated(mat); 3394 switch (op) { 3395 case MAT_SYMMETRIC: 3396 mat->symmetric = PETSC_TRUE; 3397 mat->structurally_symmetric = PETSC_TRUE; 3398 break; 3399 case MAT_STRUCTURALLY_SYMMETRIC: 3400 mat->structurally_symmetric = PETSC_TRUE; 3401 break; 3402 default: 3403 if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);} 3404 break; 3405 } 3406 PetscFunctionReturn(0); 3407 } 3408 3409 #undef __FUNCT__ 3410 #define __FUNCT__ "MatZeroEntries" 3411 /*@ 3412 MatZeroEntries - Zeros all entries of a matrix. For sparse matrices 3413 this routine retains the old nonzero structure. 3414 3415 Collective on Mat 3416 3417 Input Parameters: 3418 . mat - the matrix 3419 3420 Level: intermediate 3421 3422 Concepts: matrices^zeroing 3423 3424 .seealso: MatZeroRows() 3425 @*/ 3426 int MatZeroEntries(Mat mat) 3427 { 3428 int ierr; 3429 3430 PetscFunctionBegin; 3431 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3432 PetscValidType(mat); 3433 MatPreallocated(mat); 3434 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3435 if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3436 3437 ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3438 ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr); 3439 ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr); 3440 PetscFunctionReturn(0); 3441 } 3442 3443 #undef __FUNCT__ 3444 #define __FUNCT__ "MatZeroRows" 3445 /*@C 3446 MatZeroRows - Zeros all entries (except possibly the main diagonal) 3447 of a set of rows of a matrix. 3448 3449 Collective on Mat 3450 3451 Input Parameters: 3452 + mat - the matrix 3453 . is - index set of rows to remove 3454 - diag - pointer to value put in all diagonals of eliminated rows. 3455 Note that diag is not a pointer to an array, but merely a 3456 pointer to a single value. 3457 3458 Notes: 3459 For the AIJ and BAIJ matrix formats this removes the old nonzero structure, 3460 but does not release memory. For the dense and block diagonal 3461 formats this does not alter the nonzero structure. 3462 3463 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3464 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3465 merely zeroed. 3466 3467 The user can set a value in the diagonal entry (or for the AIJ and 3468 row formats can optionally remove the main diagonal entry from the 3469 nonzero structure as well, by passing a null pointer (PETSC_NULL 3470 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3471 3472 For the parallel case, all processes that share the matrix (i.e., 3473 those in the communicator used for matrix creation) MUST call this 3474 routine, regardless of whether any rows being zeroed are owned by 3475 them. 3476 3477 For the SBAIJ matrix (since only the upper triangular half of the matrix 3478 is stored) the effect of this call is to also zero the corresponding 3479 column. 3480 3481 Level: intermediate 3482 3483 Concepts: matrices^zeroing rows 3484 3485 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption() 3486 @*/ 3487 int MatZeroRows(Mat mat,IS is,const PetscScalar *diag) 3488 { 3489 int ierr; 3490 3491 PetscFunctionBegin; 3492 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3493 PetscValidType(mat); 3494 MatPreallocated(mat); 3495 PetscValidHeaderSpecific(is,IS_COOKIE); 3496 if (diag) PetscValidScalarPointer(diag); 3497 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3498 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3499 if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3500 3501 ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr); 3502 ierr = MatView_Private(mat);CHKERRQ(ierr); 3503 PetscFunctionReturn(0); 3504 } 3505 3506 #undef __FUNCT__ 3507 #define __FUNCT__ "MatZeroRowsLocal" 3508 /*@C 3509 MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal) 3510 of a set of rows of a matrix; using local numbering of rows. 3511 3512 Collective on Mat 3513 3514 Input Parameters: 3515 + mat - the matrix 3516 . is - index set of rows to remove 3517 - diag - pointer to value put in all diagonals of eliminated rows. 3518 Note that diag is not a pointer to an array, but merely a 3519 pointer to a single value. 3520 3521 Notes: 3522 Before calling MatZeroRowsLocal(), the user must first set the 3523 local-to-global mapping by calling MatSetLocalToGlobalMapping(). 3524 3525 For the AIJ matrix formats this removes the old nonzero structure, 3526 but does not release memory. For the dense and block diagonal 3527 formats this does not alter the nonzero structure. 3528 3529 If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure 3530 of the matrix is not changed (even for AIJ and BAIJ matrices) the values are 3531 merely zeroed. 3532 3533 The user can set a value in the diagonal entry (or for the AIJ and 3534 row formats can optionally remove the main diagonal entry from the 3535 nonzero structure as well, by passing a null pointer (PETSC_NULL 3536 in C or PETSC_NULL_SCALAR in Fortran) as the final argument). 3537 3538 Level: intermediate 3539 3540 Concepts: matrices^zeroing 3541 3542 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping 3543 @*/ 3544 int MatZeroRowsLocal(Mat mat,IS is,const PetscScalar *diag) 3545 { 3546 int ierr; 3547 IS newis; 3548 3549 PetscFunctionBegin; 3550 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3551 PetscValidType(mat); 3552 MatPreallocated(mat); 3553 PetscValidHeaderSpecific(is,IS_COOKIE); 3554 if (diag) PetscValidScalarPointer(diag); 3555 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3556 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3557 3558 if (mat->ops->zerorowslocal) { 3559 ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr); 3560 } else { 3561 if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first"); 3562 ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr); 3563 ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr); 3564 ierr = ISDestroy(newis);CHKERRQ(ierr); 3565 } 3566 PetscFunctionReturn(0); 3567 } 3568 3569 #undef __FUNCT__ 3570 #define __FUNCT__ "MatGetSize" 3571 /*@ 3572 MatGetSize - Returns the numbers of rows and columns in a matrix. 3573 3574 Not Collective 3575 3576 Input Parameter: 3577 . mat - the matrix 3578 3579 Output Parameters: 3580 + m - the number of global rows 3581 - n - the number of global columns 3582 3583 Level: beginner 3584 3585 Concepts: matrices^size 3586 3587 .seealso: MatGetLocalSize() 3588 @*/ 3589 int MatGetSize(Mat mat,int *m,int* n) 3590 { 3591 PetscFunctionBegin; 3592 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3593 if (m) *m = mat->M; 3594 if (n) *n = mat->N; 3595 PetscFunctionReturn(0); 3596 } 3597 3598 #undef __FUNCT__ 3599 #define __FUNCT__ "MatGetLocalSize" 3600 /*@ 3601 MatGetLocalSize - Returns the number of rows and columns in a matrix 3602 stored locally. This information may be implementation dependent, so 3603 use with care. 3604 3605 Not Collective 3606 3607 Input Parameters: 3608 . mat - the matrix 3609 3610 Output Parameters: 3611 + m - the number of local rows 3612 - n - the number of local columns 3613 3614 Level: beginner 3615 3616 Concepts: matrices^local size 3617 3618 .seealso: MatGetSize() 3619 @*/ 3620 int MatGetLocalSize(Mat mat,int *m,int* n) 3621 { 3622 PetscFunctionBegin; 3623 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3624 if (m) *m = mat->m; 3625 if (n) *n = mat->n; 3626 PetscFunctionReturn(0); 3627 } 3628 3629 #undef __FUNCT__ 3630 #define __FUNCT__ "MatGetOwnershipRange" 3631 /*@ 3632 MatGetOwnershipRange - Returns the range of matrix rows owned by 3633 this processor, assuming that the matrix is laid out with the first 3634 n1 rows on the first processor, the next n2 rows on the second, etc. 3635 For certain parallel layouts this range may not be well defined. 3636 3637 Not Collective 3638 3639 Input Parameters: 3640 . mat - the matrix 3641 3642 Output Parameters: 3643 + m - the global index of the first local row 3644 - n - one more than the global index of the last local row 3645 3646 Level: beginner 3647 3648 Concepts: matrices^row ownership 3649 @*/ 3650 int MatGetOwnershipRange(Mat mat,int *m,int* n) 3651 { 3652 int ierr; 3653 3654 PetscFunctionBegin; 3655 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3656 PetscValidType(mat); 3657 MatPreallocated(mat); 3658 if (m) PetscValidIntPointer(m); 3659 if (n) PetscValidIntPointer(n); 3660 ierr = PetscMapGetLocalRange(mat->rmap,m,n);CHKERRQ(ierr); 3661 PetscFunctionReturn(0); 3662 } 3663 3664 #undef __FUNCT__ 3665 #define __FUNCT__ "MatILUFactorSymbolic" 3666 /*@ 3667 MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix. 3668 Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric() 3669 to complete the factorization. 3670 3671 Collective on Mat 3672 3673 Input Parameters: 3674 + mat - the matrix 3675 . row - row permutation 3676 . column - column permutation 3677 - info - structure containing 3678 $ levels - number of levels of fill. 3679 $ expected fill - as ratio of original fill. 3680 $ 1 or 0 - indicating force fill on diagonal (improves robustness for matrices 3681 missing diagonal entries) 3682 3683 Output Parameters: 3684 . fact - new matrix that has been symbolically factored 3685 3686 Notes: 3687 See the users manual for additional information about 3688 choosing the fill factor for better efficiency. 3689 3690 Most users should employ the simplified SLES interface for linear solvers 3691 instead of working directly with matrix algebra routines such as this. 3692 See, e.g., SLESCreate(). 3693 3694 Level: developer 3695 3696 Concepts: matrices^symbolic LU factorization 3697 Concepts: matrices^factorization 3698 Concepts: LU^symbolic factorization 3699 3700 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 3701 MatGetOrdering(), MatFactorInfo 3702 3703 @*/ 3704 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatFactorInfo *info,Mat *fact) 3705 { 3706 int ierr; 3707 3708 PetscFunctionBegin; 3709 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3710 PetscValidType(mat); 3711 MatPreallocated(mat); 3712 PetscValidPointer(fact); 3713 PetscValidHeaderSpecific(row,IS_COOKIE); 3714 PetscValidHeaderSpecific(col,IS_COOKIE); 3715 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",(int)info->levels); 3716 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill); 3717 if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ILU",mat->type_name); 3718 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3719 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3720 3721 ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3722 ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr); 3723 ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr); 3724 PetscFunctionReturn(0); 3725 } 3726 3727 #undef __FUNCT__ 3728 #define __FUNCT__ "MatICCFactorSymbolic" 3729 /*@ 3730 MatICCFactorSymbolic - Performs symbolic incomplete 3731 Cholesky factorization for a symmetric matrix. Use 3732 MatCholeskyFactorNumeric() to complete the factorization. 3733 3734 Collective on Mat 3735 3736 Input Parameters: 3737 + mat - the matrix 3738 . perm - row and column permutation 3739 - info - structure containing 3740 $ levels - number of levels of fill. 3741 $ expected fill - as ratio of original fill. 3742 3743 Output Parameter: 3744 . fact - the factored matrix 3745 3746 Notes: 3747 Currently only no-fill factorization is supported. 3748 3749 Most users should employ the simplified SLES interface for linear solvers 3750 instead of working directly with matrix algebra routines such as this. 3751 See, e.g., SLESCreate(). 3752 3753 Level: developer 3754 3755 Concepts: matrices^symbolic incomplete Cholesky factorization 3756 Concepts: matrices^factorization 3757 Concepts: Cholsky^symbolic factorization 3758 3759 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo 3760 @*/ 3761 int MatICCFactorSymbolic(Mat mat,IS perm,MatFactorInfo *info,Mat *fact) 3762 { 3763 int ierr; 3764 3765 PetscFunctionBegin; 3766 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3767 PetscValidType(mat); 3768 MatPreallocated(mat); 3769 PetscValidPointer(fact); 3770 PetscValidHeaderSpecific(perm,IS_COOKIE); 3771 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3772 if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %d",(int) info->levels); 3773 if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill); 3774 if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s symbolic ICC",mat->type_name); 3775 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3776 3777 ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3778 ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,info,fact);CHKERRQ(ierr); 3779 ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr); 3780 PetscFunctionReturn(0); 3781 } 3782 3783 #undef __FUNCT__ 3784 #define __FUNCT__ "MatGetArray" 3785 /*@C 3786 MatGetArray - Returns a pointer to the element values in the matrix. 3787 The result of this routine is dependent on the underlying matrix data 3788 structure, and may not even work for certain matrix types. You MUST 3789 call MatRestoreArray() when you no longer need to access the array. 3790 3791 Not Collective 3792 3793 Input Parameter: 3794 . mat - the matrix 3795 3796 Output Parameter: 3797 . v - the location of the values 3798 3799 3800 Fortran Note: 3801 This routine is used differently from Fortran, e.g., 3802 .vb 3803 Mat mat 3804 PetscScalar mat_array(1) 3805 PetscOffset i_mat 3806 int ierr 3807 call MatGetArray(mat,mat_array,i_mat,ierr) 3808 3809 C Access first local entry in matrix; note that array is 3810 C treated as one dimensional 3811 value = mat_array(i_mat + 1) 3812 3813 [... other code ...] 3814 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3815 .ve 3816 3817 See the Fortran chapter of the users manual and 3818 petsc/src/mat/examples/tests for details. 3819 3820 Level: advanced 3821 3822 Concepts: matrices^access array 3823 3824 .seealso: MatRestoreArray(), MatGetArrayF90() 3825 @*/ 3826 int MatGetArray(Mat mat,PetscScalar *v[]) 3827 { 3828 int ierr; 3829 3830 PetscFunctionBegin; 3831 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3832 PetscValidType(mat); 3833 MatPreallocated(mat); 3834 PetscValidPointer(v); 3835 if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3836 ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr); 3837 PetscFunctionReturn(0); 3838 } 3839 3840 #undef __FUNCT__ 3841 #define __FUNCT__ "MatRestoreArray" 3842 /*@C 3843 MatRestoreArray - Restores the matrix after MatGetArray() has been called. 3844 3845 Not Collective 3846 3847 Input Parameter: 3848 + mat - the matrix 3849 - v - the location of the values 3850 3851 Fortran Note: 3852 This routine is used differently from Fortran, e.g., 3853 .vb 3854 Mat mat 3855 PetscScalar mat_array(1) 3856 PetscOffset i_mat 3857 int ierr 3858 call MatGetArray(mat,mat_array,i_mat,ierr) 3859 3860 C Access first local entry in matrix; note that array is 3861 C treated as one dimensional 3862 value = mat_array(i_mat + 1) 3863 3864 [... other code ...] 3865 call MatRestoreArray(mat,mat_array,i_mat,ierr) 3866 .ve 3867 3868 See the Fortran chapter of the users manual and 3869 petsc/src/mat/examples/tests for details 3870 3871 Level: advanced 3872 3873 .seealso: MatGetArray(), MatRestoreArrayF90() 3874 @*/ 3875 int MatRestoreArray(Mat mat,PetscScalar *v[]) 3876 { 3877 int ierr; 3878 3879 PetscFunctionBegin; 3880 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3881 PetscValidType(mat); 3882 MatPreallocated(mat); 3883 PetscValidPointer(v); 3884 if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3885 ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr); 3886 PetscFunctionReturn(0); 3887 } 3888 3889 #undef __FUNCT__ 3890 #define __FUNCT__ "MatGetSubMatrices" 3891 /*@C 3892 MatGetSubMatrices - Extracts several submatrices from a matrix. If submat 3893 points to an array of valid matrices, they may be reused to store the new 3894 submatrices. 3895 3896 Collective on Mat 3897 3898 Input Parameters: 3899 + mat - the matrix 3900 . n - the number of submatrixes to be extracted (on this processor, may be zero) 3901 . irow, icol - index sets of rows and columns to extract 3902 - scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 3903 3904 Output Parameter: 3905 . submat - the array of submatrices 3906 3907 Notes: 3908 MatGetSubMatrices() can extract only sequential submatrices 3909 (from both sequential and parallel matrices). Use MatGetSubMatrix() 3910 to extract a parallel submatrix. 3911 3912 When extracting submatrices from a parallel matrix, each processor can 3913 form a different submatrix by setting the rows and columns of its 3914 individual index sets according to the local submatrix desired. 3915 3916 When finished using the submatrices, the user should destroy 3917 them with MatDestroyMatrices(). 3918 3919 MAT_REUSE_MATRIX can only be used when the nonzero structure of the 3920 original matrix has not changed from that last call to MatGetSubMatrices(). 3921 3922 This routine creates the matrices in submat; you should NOT create them before 3923 calling it. It also allocates the array of matrix pointers submat. 3924 3925 Fortran Note: 3926 The Fortran interface is slightly different from that given below; it 3927 requires one to pass in as submat a Mat (integer) array of size at least m. 3928 3929 Level: advanced 3930 3931 Concepts: matrices^accessing submatrices 3932 Concepts: submatrices 3933 3934 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal() 3935 @*/ 3936 int MatGetSubMatrices(Mat mat,int n,const IS irow[],const IS icol[],MatReuse scall,Mat *submat[]) 3937 { 3938 int ierr; 3939 3940 PetscFunctionBegin; 3941 PetscValidHeaderSpecific(mat,MAT_COOKIE); 3942 PetscValidType(mat); 3943 MatPreallocated(mat); 3944 if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 3945 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 3946 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 3947 3948 ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 3949 ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr); 3950 ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr); 3951 PetscFunctionReturn(0); 3952 } 3953 3954 #undef __FUNCT__ 3955 #define __FUNCT__ "MatDestroyMatrices" 3956 /*@C 3957 MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices(). 3958 3959 Collective on Mat 3960 3961 Input Parameters: 3962 + n - the number of local matrices 3963 - mat - the matrices (note that this is a pointer to the array of matrices, just to match the calling 3964 sequence of MatGetSubMatrices()) 3965 3966 Level: advanced 3967 3968 Notes: Frees not only the matrices, but also the array that contains the matrices 3969 3970 .seealso: MatGetSubMatrices() 3971 @*/ 3972 int MatDestroyMatrices(int n,Mat *mat[]) 3973 { 3974 int ierr,i; 3975 3976 PetscFunctionBegin; 3977 if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n); 3978 PetscValidPointer(mat); 3979 for (i=0; i<n; i++) { 3980 ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr); 3981 } 3982 /* memory is allocated even if n = 0 */ 3983 ierr = PetscFree(*mat);CHKERRQ(ierr); 3984 PetscFunctionReturn(0); 3985 } 3986 3987 #undef __FUNCT__ 3988 #define __FUNCT__ "MatIncreaseOverlap" 3989 /*@ 3990 MatIncreaseOverlap - Given a set of submatrices indicated by index sets, 3991 replaces the index sets by larger ones that represent submatrices with 3992 additional overlap. 3993 3994 Collective on Mat 3995 3996 Input Parameters: 3997 + mat - the matrix 3998 . n - the number of index sets 3999 . is - the array of index sets (these index sets will changed during the call) 4000 - ov - the additional overlap requested 4001 4002 Level: developer 4003 4004 Concepts: overlap 4005 Concepts: ASM^computing overlap 4006 4007 .seealso: MatGetSubMatrices() 4008 @*/ 4009 int MatIncreaseOverlap(Mat mat,int n,IS is[],int ov) 4010 { 4011 int ierr; 4012 4013 PetscFunctionBegin; 4014 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4015 PetscValidType(mat); 4016 MatPreallocated(mat); 4017 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4018 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4019 4020 if (!ov) PetscFunctionReturn(0); 4021 if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4022 ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 4023 ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr); 4024 ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr); 4025 PetscFunctionReturn(0); 4026 } 4027 4028 #undef __FUNCT__ 4029 #define __FUNCT__ "MatPrintHelp" 4030 /*@ 4031 MatPrintHelp - Prints all the options for the matrix. 4032 4033 Collective on Mat 4034 4035 Input Parameter: 4036 . mat - the matrix 4037 4038 Options Database Keys: 4039 + -help - Prints matrix options 4040 - -h - Prints matrix options 4041 4042 Level: developer 4043 4044 .seealso: MatCreate(), MatCreateXXX() 4045 @*/ 4046 int MatPrintHelp(Mat mat) 4047 { 4048 static PetscTruth called = PETSC_FALSE; 4049 int ierr; 4050 4051 PetscFunctionBegin; 4052 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4053 PetscValidType(mat); 4054 MatPreallocated(mat); 4055 4056 if (!called) { 4057 if (mat->ops->printhelp) { 4058 ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr); 4059 } 4060 called = PETSC_TRUE; 4061 } 4062 PetscFunctionReturn(0); 4063 } 4064 4065 #undef __FUNCT__ 4066 #define __FUNCT__ "MatGetBlockSize" 4067 /*@ 4068 MatGetBlockSize - Returns the matrix block size; useful especially for the 4069 block row and block diagonal formats. 4070 4071 Not Collective 4072 4073 Input Parameter: 4074 . mat - the matrix 4075 4076 Output Parameter: 4077 . bs - block size 4078 4079 Notes: 4080 Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG. 4081 Block row formats are MATSEQBAIJ, MATMPIBAIJ 4082 4083 Level: intermediate 4084 4085 Concepts: matrices^block size 4086 4087 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag() 4088 @*/ 4089 int MatGetBlockSize(Mat mat,int *bs) 4090 { 4091 int ierr; 4092 4093 PetscFunctionBegin; 4094 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4095 PetscValidType(mat); 4096 MatPreallocated(mat); 4097 PetscValidIntPointer(bs); 4098 if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4099 ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr); 4100 PetscFunctionReturn(0); 4101 } 4102 4103 #undef __FUNCT__ 4104 #define __FUNCT__ "MatGetRowIJ" 4105 /*@C 4106 MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices. 4107 4108 Collective on Mat 4109 4110 Input Parameters: 4111 + mat - the matrix 4112 . shift - 0 or 1 indicating we want the indices starting at 0 or 1 4113 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4114 symmetrized 4115 4116 Output Parameters: 4117 + n - number of rows in the (possibly compressed) matrix 4118 . ia - the row pointers 4119 . ja - the column indices 4120 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4121 4122 Level: developer 4123 4124 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4125 @*/ 4126 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4127 { 4128 int ierr; 4129 4130 PetscFunctionBegin; 4131 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4132 PetscValidType(mat); 4133 MatPreallocated(mat); 4134 if (ia) PetscValidIntPointer(ia); 4135 if (ja) PetscValidIntPointer(ja); 4136 PetscValidIntPointer(done); 4137 if (!mat->ops->getrowij) *done = PETSC_FALSE; 4138 else { 4139 *done = PETSC_TRUE; 4140 ierr = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4141 } 4142 PetscFunctionReturn(0); 4143 } 4144 4145 #undef __FUNCT__ 4146 #define __FUNCT__ "MatGetColumnIJ" 4147 /*@C 4148 MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices. 4149 4150 Collective on Mat 4151 4152 Input Parameters: 4153 + mat - the matrix 4154 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4155 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4156 symmetrized 4157 4158 Output Parameters: 4159 + n - number of columns in the (possibly compressed) matrix 4160 . ia - the column pointers 4161 . ja - the row indices 4162 - done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned 4163 4164 Level: developer 4165 4166 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4167 @*/ 4168 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4169 { 4170 int ierr; 4171 4172 PetscFunctionBegin; 4173 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4174 PetscValidType(mat); 4175 MatPreallocated(mat); 4176 if (ia) PetscValidIntPointer(ia); 4177 if (ja) PetscValidIntPointer(ja); 4178 PetscValidIntPointer(done); 4179 4180 if (!mat->ops->getcolumnij) *done = PETSC_FALSE; 4181 else { 4182 *done = PETSC_TRUE; 4183 ierr = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4184 } 4185 PetscFunctionReturn(0); 4186 } 4187 4188 #undef __FUNCT__ 4189 #define __FUNCT__ "MatRestoreRowIJ" 4190 /*@C 4191 MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with 4192 MatGetRowIJ(). 4193 4194 Collective on Mat 4195 4196 Input Parameters: 4197 + mat - the matrix 4198 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4199 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4200 symmetrized 4201 4202 Output Parameters: 4203 + n - size of (possibly compressed) matrix 4204 . ia - the row pointers 4205 . ja - the column indices 4206 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4207 4208 Level: developer 4209 4210 .seealso: MatGetRowIJ(), MatRestoreColumnIJ() 4211 @*/ 4212 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4213 { 4214 int ierr; 4215 4216 PetscFunctionBegin; 4217 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4218 PetscValidType(mat); 4219 MatPreallocated(mat); 4220 if (ia) PetscValidIntPointer(ia); 4221 if (ja) PetscValidIntPointer(ja); 4222 PetscValidIntPointer(done); 4223 4224 if (!mat->ops->restorerowij) *done = PETSC_FALSE; 4225 else { 4226 *done = PETSC_TRUE; 4227 ierr = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4228 } 4229 PetscFunctionReturn(0); 4230 } 4231 4232 #undef __FUNCT__ 4233 #define __FUNCT__ "MatRestoreColumnIJ" 4234 /*@C 4235 MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with 4236 MatGetColumnIJ(). 4237 4238 Collective on Mat 4239 4240 Input Parameters: 4241 + mat - the matrix 4242 . shift - 1 or zero indicating we want the indices starting at 0 or 1 4243 - symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be 4244 symmetrized 4245 4246 Output Parameters: 4247 + n - size of (possibly compressed) matrix 4248 . ia - the column pointers 4249 . ja - the row indices 4250 - done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned 4251 4252 Level: developer 4253 4254 .seealso: MatGetColumnIJ(), MatRestoreRowIJ() 4255 @*/ 4256 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int *ia[],int* ja[],PetscTruth *done) 4257 { 4258 int ierr; 4259 4260 PetscFunctionBegin; 4261 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4262 PetscValidType(mat); 4263 MatPreallocated(mat); 4264 if (ia) PetscValidIntPointer(ia); 4265 if (ja) PetscValidIntPointer(ja); 4266 PetscValidIntPointer(done); 4267 4268 if (!mat->ops->restorecolumnij) *done = PETSC_FALSE; 4269 else { 4270 *done = PETSC_TRUE; 4271 ierr = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr); 4272 } 4273 PetscFunctionReturn(0); 4274 } 4275 4276 #undef __FUNCT__ 4277 #define __FUNCT__ "MatColoringPatch" 4278 /*@C 4279 MatColoringPatch -Used inside matrix coloring routines that 4280 use MatGetRowIJ() and/or MatGetColumnIJ(). 4281 4282 Collective on Mat 4283 4284 Input Parameters: 4285 + mat - the matrix 4286 . n - number of colors 4287 - colorarray - array indicating color for each column 4288 4289 Output Parameters: 4290 . iscoloring - coloring generated using colorarray information 4291 4292 Level: developer 4293 4294 .seealso: MatGetRowIJ(), MatGetColumnIJ() 4295 4296 @*/ 4297 int MatColoringPatch(Mat mat,int n,int ncolors,const ISColoringValue colorarray[],ISColoring *iscoloring) 4298 { 4299 int ierr; 4300 4301 PetscFunctionBegin; 4302 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4303 PetscValidType(mat); 4304 MatPreallocated(mat); 4305 PetscValidIntPointer(colorarray); 4306 4307 if (!mat->ops->coloringpatch){ 4308 ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr); 4309 } else { 4310 ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr); 4311 } 4312 PetscFunctionReturn(0); 4313 } 4314 4315 4316 #undef __FUNCT__ 4317 #define __FUNCT__ "MatSetUnfactored" 4318 /*@ 4319 MatSetUnfactored - Resets a factored matrix to be treated as unfactored. 4320 4321 Collective on Mat 4322 4323 Input Parameter: 4324 . mat - the factored matrix to be reset 4325 4326 Notes: 4327 This routine should be used only with factored matrices formed by in-place 4328 factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE 4329 format). This option can save memory, for example, when solving nonlinear 4330 systems with a matrix-free Newton-Krylov method and a matrix-based, in-place 4331 ILU(0) preconditioner. 4332 4333 Note that one can specify in-place ILU(0) factorization by calling 4334 .vb 4335 PCType(pc,PCILU); 4336 PCILUSeUseInPlace(pc); 4337 .ve 4338 or by using the options -pc_type ilu -pc_ilu_in_place 4339 4340 In-place factorization ILU(0) can also be used as a local 4341 solver for the blocks within the block Jacobi or additive Schwarz 4342 methods (runtime option: -sub_pc_ilu_in_place). See the discussion 4343 of these preconditioners in the users manual for details on setting 4344 local solver options. 4345 4346 Most users should employ the simplified SLES interface for linear solvers 4347 instead of working directly with matrix algebra routines such as this. 4348 See, e.g., SLESCreate(). 4349 4350 Level: developer 4351 4352 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace() 4353 4354 Concepts: matrices^unfactored 4355 4356 @*/ 4357 int MatSetUnfactored(Mat mat) 4358 { 4359 int ierr; 4360 4361 PetscFunctionBegin; 4362 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4363 PetscValidType(mat); 4364 MatPreallocated(mat); 4365 mat->factor = 0; 4366 if (!mat->ops->setunfactored) PetscFunctionReturn(0); 4367 ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr); 4368 PetscFunctionReturn(0); 4369 } 4370 4371 /*MC 4372 MatGetArrayF90 - Accesses a matrix array from Fortran90. 4373 4374 Synopsis: 4375 MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4376 4377 Not collective 4378 4379 Input Parameter: 4380 . x - matrix 4381 4382 Output Parameters: 4383 + xx_v - the Fortran90 pointer to the array 4384 - ierr - error code 4385 4386 Example of Usage: 4387 .vb 4388 PetscScalar, pointer xx_v(:) 4389 .... 4390 call MatGetArrayF90(x,xx_v,ierr) 4391 a = xx_v(3) 4392 call MatRestoreArrayF90(x,xx_v,ierr) 4393 .ve 4394 4395 Notes: 4396 Not yet supported for all F90 compilers 4397 4398 Level: advanced 4399 4400 .seealso: MatRestoreArrayF90(), MatGetArray(), MatRestoreArray() 4401 4402 Concepts: matrices^accessing array 4403 4404 M*/ 4405 4406 /*MC 4407 MatRestoreArrayF90 - Restores a matrix array that has been 4408 accessed with MatGetArrayF90(). 4409 4410 Synopsis: 4411 MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr) 4412 4413 Not collective 4414 4415 Input Parameters: 4416 + x - matrix 4417 - xx_v - the Fortran90 pointer to the array 4418 4419 Output Parameter: 4420 . ierr - error code 4421 4422 Example of Usage: 4423 .vb 4424 PetscScalar, pointer xx_v(:) 4425 .... 4426 call MatGetArrayF90(x,xx_v,ierr) 4427 a = xx_v(3) 4428 call MatRestoreArrayF90(x,xx_v,ierr) 4429 .ve 4430 4431 Notes: 4432 Not yet supported for all F90 compilers 4433 4434 Level: advanced 4435 4436 .seealso: MatGetArrayF90(), MatGetArray(), MatRestoreArray() 4437 4438 M*/ 4439 4440 4441 #undef __FUNCT__ 4442 #define __FUNCT__ "MatGetSubMatrix" 4443 /*@ 4444 MatGetSubMatrix - Gets a single submatrix on the same number of processors 4445 as the original matrix. 4446 4447 Collective on Mat 4448 4449 Input Parameters: 4450 + mat - the original matrix 4451 . isrow - rows this processor should obtain 4452 . iscol - columns for all processors you wish to keep 4453 . csize - number of columns "local" to this processor (does nothing for sequential 4454 matrices). This should match the result from VecGetLocalSize(x,...) if you 4455 plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE 4456 - cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX 4457 4458 Output Parameter: 4459 . newmat - the new submatrix, of the same type as the old 4460 4461 Level: advanced 4462 4463 Notes: the iscol argument MUST be the same on each processor. You might be 4464 able to create the iscol argument with ISAllGather(). 4465 4466 The first time this is called you should use a cll of MAT_INITIAL_MATRIX, 4467 the MatGetSubMatrix() routine will create the newmat for you. Any additional calls 4468 to this routine with a mat of the same nonzero structure will reuse the matrix 4469 generated the first time. 4470 4471 Concepts: matrices^submatrices 4472 4473 .seealso: MatGetSubMatrices(), ISAllGather() 4474 @*/ 4475 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat) 4476 { 4477 int ierr, size; 4478 Mat *local; 4479 4480 PetscFunctionBegin; 4481 PetscValidType(mat); 4482 MatPreallocated(mat); 4483 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4484 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 4485 4486 /* if original matrix is on just one processor then use submatrix generated */ 4487 if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) { 4488 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr); 4489 PetscFunctionReturn(0); 4490 } else if (!mat->ops->getsubmatrix && size == 1) { 4491 ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr); 4492 *newmat = *local; 4493 ierr = PetscFree(local);CHKERRQ(ierr); 4494 PetscFunctionReturn(0); 4495 } 4496 4497 if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4498 ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr); 4499 PetscFunctionReturn(0); 4500 } 4501 4502 #undef __FUNCT__ 4503 #define __FUNCT__ "MatGetPetscMaps" 4504 /*@C 4505 MatGetPetscMaps - Returns the maps associated with the matrix. 4506 4507 Not Collective 4508 4509 Input Parameter: 4510 . mat - the matrix 4511 4512 Output Parameters: 4513 + rmap - the row (right) map 4514 - cmap - the column (left) map 4515 4516 Level: developer 4517 4518 Concepts: maps^getting from matrix 4519 4520 @*/ 4521 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap) 4522 { 4523 int ierr; 4524 4525 PetscFunctionBegin; 4526 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4527 PetscValidType(mat); 4528 MatPreallocated(mat); 4529 ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr); 4530 PetscFunctionReturn(0); 4531 } 4532 4533 /* 4534 Version that works for all PETSc matrices 4535 */ 4536 #undef __FUNCT__ 4537 #define __FUNCT__ "MatGetPetscMaps_Petsc" 4538 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap) 4539 { 4540 PetscFunctionBegin; 4541 if (rmap) *rmap = mat->rmap; 4542 if (cmap) *cmap = mat->cmap; 4543 PetscFunctionReturn(0); 4544 } 4545 4546 #undef __FUNCT__ 4547 #define __FUNCT__ "MatSetStashInitialSize" 4548 /*@ 4549 MatSetStashInitialSize - sets the sizes of the matrix stash, that is 4550 used during the assembly process to store values that belong to 4551 other processors. 4552 4553 Not Collective 4554 4555 Input Parameters: 4556 + mat - the matrix 4557 . size - the initial size of the stash. 4558 - bsize - the initial size of the block-stash(if used). 4559 4560 Options Database Keys: 4561 + -matstash_initial_size <size> or <size0,size1,...sizep-1> 4562 - -matstash_block_initial_size <bsize> or <bsize0,bsize1,...bsizep-1> 4563 4564 Level: intermediate 4565 4566 Notes: 4567 The block-stash is used for values set with VecSetValuesBlocked() while 4568 the stash is used for values set with VecSetValues() 4569 4570 Run with the option -log_info and look for output of the form 4571 MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs. 4572 to determine the appropriate value, MM, to use for size and 4573 MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs. 4574 to determine the value, BMM to use for bsize 4575 4576 Concepts: stash^setting matrix size 4577 Concepts: matrices^stash 4578 4579 @*/ 4580 int MatSetStashInitialSize(Mat mat,int size, int bsize) 4581 { 4582 int ierr; 4583 4584 PetscFunctionBegin; 4585 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4586 PetscValidType(mat); 4587 MatPreallocated(mat); 4588 ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr); 4589 ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr); 4590 PetscFunctionReturn(0); 4591 } 4592 4593 #undef __FUNCT__ 4594 #define __FUNCT__ "MatInterpolateAdd" 4595 /*@ 4596 MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of 4597 the matrix 4598 4599 Collective on Mat 4600 4601 Input Parameters: 4602 + mat - the matrix 4603 . x,y - the vectors 4604 - w - where the result is stored 4605 4606 Level: intermediate 4607 4608 Notes: 4609 w may be the same vector as y. 4610 4611 This allows one to use either the restriction or interpolation (its transpose) 4612 matrix to do the interpolation 4613 4614 Concepts: interpolation 4615 4616 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4617 4618 @*/ 4619 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w) 4620 { 4621 int M,N,ierr; 4622 4623 PetscFunctionBegin; 4624 PetscValidType(A); 4625 MatPreallocated(A); 4626 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4627 if (N > M) { 4628 ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr); 4629 } else { 4630 ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr); 4631 } 4632 PetscFunctionReturn(0); 4633 } 4634 4635 #undef __FUNCT__ 4636 #define __FUNCT__ "MatInterpolate" 4637 /*@ 4638 MatInterpolate - y = A*x or A'*x depending on the shape of 4639 the matrix 4640 4641 Collective on Mat 4642 4643 Input Parameters: 4644 + mat - the matrix 4645 - x,y - the vectors 4646 4647 Level: intermediate 4648 4649 Notes: 4650 This allows one to use either the restriction or interpolation (its transpose) 4651 matrix to do the interpolation 4652 4653 Concepts: matrices^interpolation 4654 4655 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict() 4656 4657 @*/ 4658 int MatInterpolate(Mat A,Vec x,Vec y) 4659 { 4660 int M,N,ierr; 4661 4662 PetscFunctionBegin; 4663 PetscValidType(A); 4664 MatPreallocated(A); 4665 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4666 if (N > M) { 4667 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 4668 } else { 4669 ierr = MatMult(A,x,y);CHKERRQ(ierr); 4670 } 4671 PetscFunctionReturn(0); 4672 } 4673 4674 #undef __FUNCT__ 4675 #define __FUNCT__ "MatRestrict" 4676 /*@ 4677 MatRestrict - y = A*x or A'*x 4678 4679 Collective on Mat 4680 4681 Input Parameters: 4682 + mat - the matrix 4683 - x,y - the vectors 4684 4685 Level: intermediate 4686 4687 Notes: 4688 This allows one to use either the restriction or interpolation (its transpose) 4689 matrix to do the restriction 4690 4691 Concepts: matrices^restriction 4692 4693 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate() 4694 4695 @*/ 4696 int MatRestrict(Mat A,Vec x,Vec y) 4697 { 4698 int M,N,ierr; 4699 4700 PetscFunctionBegin; 4701 PetscValidType(A); 4702 MatPreallocated(A); 4703 ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr); 4704 if (N > M) { 4705 ierr = MatMult(A,x,y);CHKERRQ(ierr); 4706 } else { 4707 ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr); 4708 } 4709 PetscFunctionReturn(0); 4710 } 4711 4712 #undef __FUNCT__ 4713 #define __FUNCT__ "MatNullSpaceAttach" 4714 /*@C 4715 MatNullSpaceAttach - attaches a null space to a matrix. 4716 This null space will be removed from the resulting vector whenever 4717 MatMult() is called 4718 4719 Collective on Mat 4720 4721 Input Parameters: 4722 + mat - the matrix 4723 - nullsp - the null space object 4724 4725 Level: developer 4726 4727 Notes: 4728 Overwrites any previous null space that may have been attached 4729 4730 Concepts: null space^attaching to matrix 4731 4732 .seealso: MatCreate(), MatNullSpaceCreate() 4733 @*/ 4734 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp) 4735 { 4736 int ierr; 4737 4738 PetscFunctionBegin; 4739 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4740 PetscValidType(mat); 4741 MatPreallocated(mat); 4742 PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE); 4743 4744 if (mat->nullsp) { 4745 ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr); 4746 } 4747 mat->nullsp = nullsp; 4748 ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr); 4749 PetscFunctionReturn(0); 4750 } 4751 4752 #undef __FUNCT__ 4753 #define __FUNCT__ "MatICCFactor" 4754 /*@ 4755 MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix. 4756 4757 Collective on Mat 4758 4759 Input Parameters: 4760 + mat - the matrix 4761 . row - row/column permutation 4762 . fill - expected fill factor >= 1.0 4763 - level - level of fill, for ICC(k) 4764 4765 Notes: 4766 Probably really in-place only when level of fill is zero, otherwise allocates 4767 new space to store factored matrix and deletes previous memory. 4768 4769 Most users should employ the simplified SLES interface for linear solvers 4770 instead of working directly with matrix algebra routines such as this. 4771 See, e.g., SLESCreate(). 4772 4773 Level: developer 4774 4775 Concepts: matrices^incomplete Cholesky factorization 4776 Concepts: Cholesky factorization 4777 4778 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor() 4779 @*/ 4780 int MatICCFactor(Mat mat,IS row,MatFactorInfo* info) 4781 { 4782 int ierr; 4783 4784 PetscFunctionBegin; 4785 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4786 PetscValidType(mat); 4787 MatPreallocated(mat); 4788 if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square"); 4789 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix"); 4790 if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix"); 4791 if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4792 ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr); 4793 PetscFunctionReturn(0); 4794 } 4795 4796 #undef __FUNCT__ 4797 #define __FUNCT__ "MatSetValuesAdic" 4798 /*@ 4799 MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix. 4800 4801 Not Collective 4802 4803 Input Parameters: 4804 + mat - the matrix 4805 - v - the values compute with ADIC 4806 4807 Level: developer 4808 4809 Notes: 4810 Must call MatSetColoring() before using this routine. Also this matrix must already 4811 have its nonzero pattern determined. 4812 4813 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4814 MatSetValues(), MatSetColoring(), MatSetValuesAdifor() 4815 @*/ 4816 int MatSetValuesAdic(Mat mat,void *v) 4817 { 4818 int ierr; 4819 4820 PetscFunctionBegin; 4821 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4822 PetscValidType(mat); 4823 4824 if (!mat->assembled) { 4825 SETERRQ(1,"Matrix must be already assembled"); 4826 } 4827 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4828 if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4829 ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr); 4830 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4831 ierr = MatView_Private(mat);CHKERRQ(ierr); 4832 PetscFunctionReturn(0); 4833 } 4834 4835 4836 #undef __FUNCT__ 4837 #define __FUNCT__ "MatSetColoring" 4838 /*@ 4839 MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic() 4840 4841 Not Collective 4842 4843 Input Parameters: 4844 + mat - the matrix 4845 - coloring - the coloring 4846 4847 Level: developer 4848 4849 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4850 MatSetValues(), MatSetValuesAdic() 4851 @*/ 4852 int MatSetColoring(Mat mat,ISColoring coloring) 4853 { 4854 int ierr; 4855 4856 PetscFunctionBegin; 4857 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4858 PetscValidType(mat); 4859 4860 if (!mat->assembled) { 4861 SETERRQ(1,"Matrix must be already assembled"); 4862 } 4863 if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4864 ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr); 4865 PetscFunctionReturn(0); 4866 } 4867 4868 #undef __FUNCT__ 4869 #define __FUNCT__ "MatSetValuesAdifor" 4870 /*@ 4871 MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix. 4872 4873 Not Collective 4874 4875 Input Parameters: 4876 + mat - the matrix 4877 . nl - leading dimension of v 4878 - v - the values compute with ADIFOR 4879 4880 Level: developer 4881 4882 Notes: 4883 Must call MatSetColoring() before using this routine. Also this matrix must already 4884 have its nonzero pattern determined. 4885 4886 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(), 4887 MatSetValues(), MatSetColoring() 4888 @*/ 4889 int MatSetValuesAdifor(Mat mat,int nl,void *v) 4890 { 4891 int ierr; 4892 4893 PetscFunctionBegin; 4894 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4895 PetscValidType(mat); 4896 4897 if (!mat->assembled) { 4898 SETERRQ(1,"Matrix must be already assembled"); 4899 } 4900 ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4901 if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4902 ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr); 4903 ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr); 4904 PetscFunctionReturn(0); 4905 } 4906 4907 EXTERN int MatMPIAIJDiagonalScaleLocal(Mat,Vec); 4908 EXTERN int MatMPIBAIJDiagonalScaleLocal(Mat,Vec); 4909 4910 #undef __FUNCT__ 4911 #define __FUNCT__ "MatDiagonalScaleLocal" 4912 /*@ 4913 MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the 4914 ghosted ones. 4915 4916 Not Collective 4917 4918 Input Parameters: 4919 + mat - the matrix 4920 - diag = the diagonal values, including ghost ones 4921 4922 Level: developer 4923 4924 Notes: Works only for MPIAIJ and MPIBAIJ matrices 4925 4926 .seealso: MatDiagonalScale() 4927 @*/ 4928 int MatDiagonalScaleLocal(Mat mat,Vec diag) 4929 { 4930 int ierr,size; 4931 4932 PetscFunctionBegin; 4933 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4934 PetscValidHeaderSpecific(diag,VEC_COOKIE); 4935 PetscValidType(mat); 4936 4937 if (!mat->assembled) { 4938 SETERRQ(1,"Matrix must be already assembled"); 4939 } 4940 ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4941 ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr); 4942 if (size == 1) { 4943 int n,m; 4944 ierr = VecGetSize(diag,&n);CHKERRQ(ierr); 4945 ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr); 4946 if (m == n) { 4947 ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr); 4948 } else { 4949 SETERRQ(1,"Only supprted for sequential matrices when no ghost points/periodic conditions"); 4950 } 4951 } else { 4952 int (*f)(Mat,Vec); 4953 ierr = PetscObjectQueryFunction((PetscObject)mat,"MatDiagonalScaleLocal_C",(void (**)(void))&f);CHKERRQ(ierr); 4954 if (f) { 4955 ierr = (*f)(mat,diag);CHKERRQ(ierr); 4956 } else { 4957 SETERRQ(1,"Only supported for MPIAIJ and MPIBAIJ parallel matrices"); 4958 } 4959 } 4960 ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr); 4961 PetscFunctionReturn(0); 4962 } 4963 4964 #undef __FUNCT__ 4965 #define __FUNCT__ "MatGetInertia" 4966 /*@ 4967 MatGetInertia - Gets the inertia from a factored matrix 4968 4969 Collective on Mat 4970 4971 Input Parameter: 4972 . mat - the matrix 4973 4974 Output Parameters: 4975 + nneg - number of negative eigenvalues 4976 . nzero - number of zero eigenvalues 4977 - npos - number of positive eigenvalues 4978 4979 Level: advanced 4980 4981 Notes: Matrix must have been factored by MatCholeskyFactor() 4982 4983 4984 @*/ 4985 int MatGetInertia(Mat mat,int *nneg,int *nzero,int *npos) 4986 { 4987 int ierr; 4988 4989 PetscFunctionBegin; 4990 PetscValidHeaderSpecific(mat,MAT_COOKIE); 4991 PetscValidType(mat); 4992 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 4993 if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled"); 4994 if (!mat->ops->getinertia) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 4995 ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr); 4996 PetscFunctionReturn(0); 4997 } 4998 4999 /* ----------------------------------------------------------------*/ 5000 #undef __FUNCT__ 5001 #define __FUNCT__ "MatSolves" 5002 /*@ 5003 MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors 5004 5005 Collective on Mat and Vecs 5006 5007 Input Parameters: 5008 + mat - the factored matrix 5009 - b - the right-hand-side vectors 5010 5011 Output Parameter: 5012 . x - the result vectors 5013 5014 Notes: 5015 The vectors b and x cannot be the same. I.e., one cannot 5016 call MatSolves(A,x,x). 5017 5018 Notes: 5019 Most users should employ the simplified SLES interface for linear solvers 5020 instead of working directly with matrix algebra routines such as this. 5021 See, e.g., SLESCreate(). 5022 5023 Level: developer 5024 5025 Concepts: matrices^triangular solves 5026 5027 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve() 5028 @*/ 5029 int MatSolves(Mat mat,Vecs b,Vecs x) 5030 { 5031 int ierr; 5032 5033 PetscFunctionBegin; 5034 PetscValidHeaderSpecific(mat,MAT_COOKIE); 5035 PetscValidType(mat); 5036 MatPreallocated(mat); 5037 if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors"); 5038 if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix"); 5039 if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0); 5040 5041 if (!mat->ops->solves) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name); 5042 ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 5043 ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr); 5044 ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr); 5045 PetscFunctionReturn(0); 5046 } 5047