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