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