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