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