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