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