xref: /petsc/src/mat/interface/matrix.c (revision 93adecfc72bedd5e853cdc999a90879e248dbda8)
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*);
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   if (f) {
2700     ierr = (*f)(A,B,flg);CHKERRQ(ierr);
2701   }
2702   PetscFunctionReturn(0);
2703 }
2704 
2705 #undef __FUNCT__
2706 #define __FUNCT__ "MatPermute"
2707 /*@C
2708    MatPermute - Creates a new matrix with rows and columns permuted from the
2709    original.
2710 
2711    Collective on Mat
2712 
2713    Input Parameters:
2714 +  mat - the matrix to permute
2715 .  row - row permutation, each processor supplies only the permutation for its rows
2716 -  col - column permutation, each processor needs the entire column permutation, that is
2717          this is the same size as the total number of columns in the matrix
2718 
2719    Output Parameters:
2720 .  B - the permuted matrix
2721 
2722    Level: advanced
2723 
2724    Concepts: matrices^permuting
2725 
2726 .seealso: MatGetOrdering()
2727 @*/
2728 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2729 {
2730   int ierr;
2731 
2732   PetscFunctionBegin;
2733   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2734   PetscValidType(mat);
2735   MatPreallocated(mat);
2736   PetscValidHeaderSpecific(row,IS_COOKIE);
2737   PetscValidHeaderSpecific(col,IS_COOKIE);
2738   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2739   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2740   if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2741   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2742   PetscFunctionReturn(0);
2743 }
2744 
2745 #undef __FUNCT__
2746 #define __FUNCT__ "MatPermuteSparsify"
2747 /*@C
2748   MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the
2749   original and sparsified to the prescribed tolerance.
2750 
2751   Collective on Mat
2752 
2753   Input Parameters:
2754 + A    - The matrix to permute
2755 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE
2756 . frac - The half-bandwidth as a fraction of the total size, or 0.0
2757 . tol  - The drop tolerance
2758 . rowp - The row permutation
2759 - colp - The column permutation
2760 
2761   Output Parameter:
2762 . B    - The permuted, sparsified matrix
2763 
2764   Level: advanced
2765 
2766   Note:
2767   The default behavior (band = PETSC_DECIDE and frac = 0.0) is to
2768   restrict the half-bandwidth of the resulting matrix to 5% of the
2769   total matrix size.
2770 
2771 .keywords: matrix, permute, sparsify
2772 
2773 .seealso: MatGetOrdering(), MatPermute()
2774 @*/
2775 int MatPermuteSparsify(Mat A, int band, PetscReal frac, PetscReal tol, IS rowp, IS colp, Mat *B)
2776 {
2777   IS           irowp, icolp;
2778   int         *rows, *cols;
2779   int          M, N, locRowStart, locRowEnd;
2780   int          nz, newNz;
2781   int         *cwork, *cnew;
2782   PetscScalar *vwork, *vnew;
2783   int          bw, size;
2784   int          row, locRow, newRow, col, newCol;
2785   int          ierr;
2786 
2787   PetscFunctionBegin;
2788   PetscValidHeaderSpecific(A,    MAT_COOKIE);
2789   PetscValidHeaderSpecific(rowp, IS_COOKIE);
2790   PetscValidHeaderSpecific(colp, IS_COOKIE);
2791   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
2792   if (A->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2793   if (!A->ops->permutesparsify) {
2794     ierr = MatGetSize(A, &M, &N);                                                                         CHKERRQ(ierr);
2795     ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd);                                             CHKERRQ(ierr);
2796     ierr = ISGetSize(rowp, &size);                                                                        CHKERRQ(ierr);
2797     if (size != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for row permutation, should be %d", size, M);
2798     ierr = ISGetSize(colp, &size);                                                                        CHKERRQ(ierr);
2799     if (size != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for column permutation, should be %d", size, N);
2800     ierr = ISInvertPermutation(rowp, 0, &irowp);                                                          CHKERRQ(ierr);
2801     ierr = ISGetIndices(irowp, &rows);                                                                    CHKERRQ(ierr);
2802     ierr = ISInvertPermutation(colp, 0, &icolp);                                                          CHKERRQ(ierr);
2803     ierr = ISGetIndices(icolp, &cols);                                                                    CHKERRQ(ierr);
2804     ierr = PetscMalloc(N * sizeof(int),         &cnew);                                                   CHKERRQ(ierr);
2805     ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew);                                                   CHKERRQ(ierr);
2806 
2807     /* Setup bandwidth to include */
2808     if (band == PETSC_DECIDE) {
2809       if (frac <= 0.0)
2810         bw = (int) (M * 0.05);
2811       else
2812         bw = (int) (M * frac);
2813     } else {
2814       if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer");
2815       bw = band;
2816     }
2817 
2818     /* Put values into new matrix */
2819     ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B);                                                    CHKERRQ(ierr);
2820     for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) {
2821       ierr = MatGetRow(A, row, &nz, &cwork, &vwork);                                                      CHKERRQ(ierr);
2822       newRow   = rows[locRow]+locRowStart;
2823       for(col = 0, newNz = 0; col < nz; col++) {
2824         newCol = cols[cwork[col]];
2825         if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) {
2826           cnew[newNz] = newCol;
2827           vnew[newNz] = vwork[col];
2828           newNz++;
2829         }
2830       }
2831       ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES);                              CHKERRQ(ierr);
2832       ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork);                                                  CHKERRQ(ierr);
2833     }
2834     ierr = PetscFree(cnew);                                                                               CHKERRQ(ierr);
2835     ierr = PetscFree(vnew);                                                                               CHKERRQ(ierr);
2836     ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);                                                      CHKERRQ(ierr);
2837     ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);                                                        CHKERRQ(ierr);
2838     ierr = ISRestoreIndices(irowp, &rows);                                                                CHKERRQ(ierr);
2839     ierr = ISRestoreIndices(icolp, &cols);                                                                CHKERRQ(ierr);
2840     ierr = ISDestroy(irowp);                                                                              CHKERRQ(ierr);
2841     ierr = ISDestroy(icolp);                                                                              CHKERRQ(ierr);
2842   } else {
2843     ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B);                                 CHKERRQ(ierr);
2844   }
2845   PetscFunctionReturn(0);
2846 }
2847 
2848 #undef __FUNCT__
2849 #define __FUNCT__ "MatEqual"
2850 /*@
2851    MatEqual - Compares two matrices.
2852 
2853    Collective on Mat
2854 
2855    Input Parameters:
2856 +  A - the first matrix
2857 -  B - the second matrix
2858 
2859    Output Parameter:
2860 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2861 
2862    Level: intermediate
2863 
2864    Concepts: matrices^equality between
2865 @*/
2866 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2867 {
2868   int ierr;
2869 
2870   PetscFunctionBegin;
2871   PetscValidHeaderSpecific(A,MAT_COOKIE);
2872   PetscValidHeaderSpecific(B,MAT_COOKIE);
2873   PetscValidType(A);
2874   MatPreallocated(A);
2875   PetscValidType(B);
2876   MatPreallocated(B);
2877   PetscValidIntPointer(flg);
2878   PetscCheckSameComm(A,B);
2879   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2880   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2881   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);
2882   if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name);
2883   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2884   PetscFunctionReturn(0);
2885 }
2886 
2887 #undef __FUNCT__
2888 #define __FUNCT__ "MatDiagonalScale"
2889 /*@
2890    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2891    matrices that are stored as vectors.  Either of the two scaling
2892    matrices can be PETSC_NULL.
2893 
2894    Collective on Mat
2895 
2896    Input Parameters:
2897 +  mat - the matrix to be scaled
2898 .  l - the left scaling vector (or PETSC_NULL)
2899 -  r - the right scaling vector (or PETSC_NULL)
2900 
2901    Notes:
2902    MatDiagonalScale() computes A = LAR, where
2903    L = a diagonal matrix, R = a diagonal matrix
2904 
2905    Level: intermediate
2906 
2907    Concepts: matrices^diagonal scaling
2908    Concepts: diagonal scaling of matrices
2909 
2910 .seealso: MatScale()
2911 @*/
2912 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2913 {
2914   int ierr;
2915 
2916   PetscFunctionBegin;
2917   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2918   PetscValidType(mat);
2919   MatPreallocated(mat);
2920   if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2921   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2922   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2923   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2924   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2925 
2926   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2927   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2928   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2929   PetscFunctionReturn(0);
2930 }
2931 
2932 #undef __FUNCT__
2933 #define __FUNCT__ "MatScale"
2934 /*@
2935     MatScale - Scales all elements of a matrix by a given number.
2936 
2937     Collective on Mat
2938 
2939     Input Parameters:
2940 +   mat - the matrix to be scaled
2941 -   a  - the scaling value
2942 
2943     Output Parameter:
2944 .   mat - the scaled matrix
2945 
2946     Level: intermediate
2947 
2948     Concepts: matrices^scaling all entries
2949 
2950 .seealso: MatDiagonalScale()
2951 @*/
2952 int MatScale(PetscScalar *a,Mat mat)
2953 {
2954   int ierr;
2955 
2956   PetscFunctionBegin;
2957   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2958   PetscValidType(mat);
2959   MatPreallocated(mat);
2960   PetscValidScalarPointer(a);
2961   if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2962   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2963   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2964 
2965   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2966   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2967   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2968   PetscFunctionReturn(0);
2969 }
2970 
2971 #undef __FUNCT__
2972 #define __FUNCT__ "MatNorm"
2973 /*@
2974    MatNorm - Calculates various norms of a matrix.
2975 
2976    Collective on Mat
2977 
2978    Input Parameters:
2979 +  mat - the matrix
2980 -  type - the type of norm, NORM_1, NORM_FROBENIUS, NORM_INFINITY
2981 
2982    Output Parameters:
2983 .  nrm - the resulting norm
2984 
2985    Level: intermediate
2986 
2987    Concepts: matrices^norm
2988    Concepts: norm^of matrix
2989 @*/
2990 int MatNorm(Mat mat,NormType type,PetscReal *nrm)
2991 {
2992   int ierr;
2993 
2994   PetscFunctionBegin;
2995   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2996   PetscValidType(mat);
2997   MatPreallocated(mat);
2998   PetscValidScalarPointer(nrm);
2999 
3000   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3001   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3002   if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3003   ierr = (*mat->ops->norm)(mat,type,nrm);CHKERRQ(ierr);
3004   PetscFunctionReturn(0);
3005 }
3006 
3007 /*
3008      This variable is used to prevent counting of MatAssemblyBegin() that
3009    are called from within a MatAssemblyEnd().
3010 */
3011 static int MatAssemblyEnd_InUse = 0;
3012 #undef __FUNCT__
3013 #define __FUNCT__ "MatAssemblyBegin"
3014 /*@
3015    MatAssemblyBegin - Begins assembling the matrix.  This routine should
3016    be called after completing all calls to MatSetValues().
3017 
3018    Collective on Mat
3019 
3020    Input Parameters:
3021 +  mat - the matrix
3022 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
3023 
3024    Notes:
3025    MatSetValues() generally caches the values.  The matrix is ready to
3026    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
3027    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
3028    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
3029    using the matrix.
3030 
3031    Level: beginner
3032 
3033    Concepts: matrices^assembling
3034 
3035 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
3036 @*/
3037 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
3038 {
3039   int ierr;
3040 
3041   PetscFunctionBegin;
3042   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3043   PetscValidType(mat);
3044   MatPreallocated(mat);
3045   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
3046   if (mat->assembled) {
3047     mat->was_assembled = PETSC_TRUE;
3048     mat->assembled     = PETSC_FALSE;
3049   }
3050   if (!MatAssemblyEnd_InUse) {
3051     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
3052     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
3053     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
3054   } else {
3055     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
3056   }
3057   PetscFunctionReturn(0);
3058 }
3059 
3060 #undef __FUNCT__
3061 #define __FUNCT__ "MatAssembed"
3062 /*@
3063    MatAssembled - Indicates if a matrix has been assembled and is ready for
3064      use; for example, in matrix-vector product.
3065 
3066    Collective on Mat
3067 
3068    Input Parameter:
3069 .  mat - the matrix
3070 
3071    Output Parameter:
3072 .  assembled - PETSC_TRUE or PETSC_FALSE
3073 
3074    Level: advanced
3075 
3076    Concepts: matrices^assembled?
3077 
3078 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
3079 @*/
3080 int MatAssembled(Mat mat,PetscTruth *assembled)
3081 {
3082   PetscFunctionBegin;
3083   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3084   PetscValidType(mat);
3085   MatPreallocated(mat);
3086   *assembled = mat->assembled;
3087   PetscFunctionReturn(0);
3088 }
3089 
3090 #undef __FUNCT__
3091 #define __FUNCT__ "MatView_Private"
3092 /*
3093     Processes command line options to determine if/how a matrix
3094   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
3095 */
3096 int MatView_Private(Mat mat)
3097 {
3098   int               ierr;
3099   PetscTruth        flg;
3100   static PetscTruth incall = PETSC_FALSE;
3101 
3102   PetscFunctionBegin;
3103   if (incall) PetscFunctionReturn(0);
3104   incall = PETSC_TRUE;
3105   ierr = PetscOptionsBegin(mat->comm,mat->prefix,"Matrix Options","Mat");CHKERRQ(ierr);
3106     ierr = PetscOptionsName("-mat_view_info","Information on matrix size","MatView",&flg);CHKERRQ(ierr);
3107     if (flg) {
3108       ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
3109       ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3110       ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3111     }
3112     ierr = PetscOptionsName("-mat_view_info_detailed","Nonzeros in the matrix","MatView",&flg);CHKERRQ(ierr);
3113     if (flg) {
3114       ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_DETAIL);CHKERRQ(ierr);
3115       ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3116       ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3117     }
3118     ierr = PetscOptionsName("-mat_view","Print matrix to stdout","MatView",&flg);CHKERRQ(ierr);
3119     if (flg) {
3120       ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3121     }
3122     ierr = PetscOptionsName("-mat_view_matlab","Print matrix to stdout in a format Matlab can read","MatView",&flg);CHKERRQ(ierr);
3123     if (flg) {
3124       ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
3125       ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3126       ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3127     }
3128     ierr = PetscOptionsName("-mat_view_socket","Send matrix to socket (can be read from matlab)","MatView",&flg);CHKERRQ(ierr);
3129     if (flg) {
3130       ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
3131       ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
3132     }
3133     ierr = PetscOptionsName("-mat_view_binary","Save matrix to file in binary format","MatView",&flg);CHKERRQ(ierr);
3134     if (flg) {
3135       ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
3136       ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
3137     }
3138   ierr = PetscOptionsEnd();CHKERRQ(ierr);
3139   /* cannot have inside PetscOptionsBegin() because uses PetscOptionsBegin() */
3140   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
3141   if (flg) {
3142     ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
3143     if (flg) {
3144       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
3145     }
3146     ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3147     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3148     if (flg) {
3149       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3150     }
3151   }
3152   incall = PETSC_FALSE;
3153   PetscFunctionReturn(0);
3154 }
3155 
3156 #undef __FUNCT__
3157 #define __FUNCT__ "MatAssemblyEnd"
3158 /*@
3159    MatAssemblyEnd - Completes assembling the matrix.  This routine should
3160    be called after MatAssemblyBegin().
3161 
3162    Collective on Mat
3163 
3164    Input Parameters:
3165 +  mat - the matrix
3166 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
3167 
3168    Options Database Keys:
3169 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
3170 .  -mat_view_info_detailed - Prints more detailed info
3171 .  -mat_view - Prints matrix in ASCII format
3172 .  -mat_view_matlab - Prints matrix in Matlab format
3173 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
3174 .  -display <name> - Sets display name (default is host)
3175 .  -draw_pause <sec> - Sets number of seconds to pause after display
3176 .  -mat_view_socket - Sends matrix to socket, can be accessed from Matlab (see users manual)
3177 .  -viewer_socket_machine <machine>
3178 .  -viewer_socket_port <port>
3179 .  -mat_view_binary - save matrix to file in binary format
3180 -  -viewer_binary_filename <name>
3181 
3182    Notes:
3183    MatSetValues() generally caches the values.  The matrix is ready to
3184    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
3185    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
3186    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
3187    using the matrix.
3188 
3189    Level: beginner
3190 
3191 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled(), PetscViewerSocketOpen()
3192 @*/
3193 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
3194 {
3195   int        ierr;
3196   static int inassm = 0;
3197   PetscTruth flg;
3198 
3199   PetscFunctionBegin;
3200   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3201   PetscValidType(mat);
3202   MatPreallocated(mat);
3203 
3204   inassm++;
3205   MatAssemblyEnd_InUse++;
3206   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
3207     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
3208     if (mat->ops->assemblyend) {
3209       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
3210     }
3211     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
3212   } else {
3213     if (mat->ops->assemblyend) {
3214       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
3215     }
3216   }
3217 
3218   /* Flush assembly is not a true assembly */
3219   if (type != MAT_FLUSH_ASSEMBLY) {
3220     mat->assembled  = PETSC_TRUE; mat->num_ass++;
3221   }
3222   mat->insertmode = NOT_SET_VALUES;
3223   MatAssemblyEnd_InUse--;
3224 
3225   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
3226     ierr = MatView_Private(mat);CHKERRQ(ierr);
3227   }
3228   inassm--;
3229   ierr = PetscOptionsHasName(mat->prefix,"-help",&flg);CHKERRQ(ierr);
3230   if (flg) {
3231     ierr = MatPrintHelp(mat);CHKERRQ(ierr);
3232   }
3233   PetscFunctionReturn(0);
3234 }
3235 
3236 
3237 #undef __FUNCT__
3238 #define __FUNCT__ "MatCompress"
3239 /*@
3240    MatCompress - Tries to store the matrix in as little space as
3241    possible.  May fail if memory is already fully used, since it
3242    tries to allocate new space.
3243 
3244    Collective on Mat
3245 
3246    Input Parameters:
3247 .  mat - the matrix
3248 
3249    Level: advanced
3250 
3251 @*/
3252 int MatCompress(Mat mat)
3253 {
3254   int ierr;
3255 
3256   PetscFunctionBegin;
3257   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3258   PetscValidType(mat);
3259   MatPreallocated(mat);
3260   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
3261   PetscFunctionReturn(0);
3262 }
3263 
3264 #undef __FUNCT__
3265 #define __FUNCT__ "MatSetOption"
3266 /*@
3267    MatSetOption - Sets a parameter option for a matrix. Some options
3268    may be specific to certain storage formats.  Some options
3269    determine how values will be inserted (or added). Sorted,
3270    row-oriented input will generally assemble the fastest. The default
3271    is row-oriented, nonsorted input.
3272 
3273    Collective on Mat
3274 
3275    Input Parameters:
3276 +  mat - the matrix
3277 -  option - the option, one of those listed below (and possibly others),
3278              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
3279 
3280    Options Describing Matrix Structure:
3281 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
3282 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
3283 
3284    Options For Use with MatSetValues():
3285    Insert a logically dense subblock, which can be
3286 +    MAT_ROW_ORIENTED - row-oriented (default)
3287 .    MAT_COLUMN_ORIENTED - column-oriented
3288 .    MAT_ROWS_SORTED - sorted by row
3289 .    MAT_ROWS_UNSORTED - not sorted by row (default)
3290 .    MAT_COLUMNS_SORTED - sorted by column
3291 -    MAT_COLUMNS_UNSORTED - not sorted by column (default)
3292 
3293    Not these options reflect the data you pass in with MatSetValues(); it has
3294    nothing to do with how the data is stored internally in the matrix
3295    data structure.
3296 
3297    When (re)assembling a matrix, we can restrict the input for
3298    efficiency/debugging purposes.  These options include
3299 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
3300         allowed if they generate a new nonzero
3301 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
3302 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
3303          they generate a nonzero in a new diagonal (for block diagonal format only)
3304 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
3305 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
3306 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
3307 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
3308 
3309    Notes:
3310    Some options are relevant only for particular matrix types and
3311    are thus ignored by others.  Other options are not supported by
3312    certain matrix types and will generate an error message if set.
3313 
3314    If using a Fortran 77 module to compute a matrix, one may need to
3315    use the column-oriented option (or convert to the row-oriented
3316    format).
3317 
3318    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
3319    that would generate a new entry in the nonzero structure is instead
3320    ignored.  Thus, if memory has not alredy been allocated for this particular
3321    data, then the insertion is ignored. For dense matrices, in which
3322    the entire array is allocated, no entries are ever ignored.
3323    Set after the first MatAssemblyEnd()
3324 
3325    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
3326    that would generate a new entry in the nonzero structure instead produces
3327    an error. (Currently supported for AIJ and BAIJ formats only.)
3328    This is a useful flag when using SAME_NONZERO_PATTERN in calling
3329    SLESSetOperators() to ensure that the nonzero pattern truely does
3330    remain unchanged. Set after the first MatAssemblyEnd()
3331 
3332    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
3333    that would generate a new entry that has not been preallocated will
3334    instead produce an error. (Currently supported for AIJ and BAIJ formats
3335    only.) This is a useful flag when debugging matrix memory preallocation.
3336 
3337    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
3338    other processors should be dropped, rather than stashed.
3339    This is useful if you know that the "owning" processor is also
3340    always generating the correct matrix entries, so that PETSc need
3341    not transfer duplicate entries generated on another processor.
3342 
3343    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
3344    searches during matrix assembly. When this flag is set, the hash table
3345    is created during the first Matrix Assembly. This hash table is
3346    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
3347    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
3348    should be used with MAT_USE_HASH_TABLE flag. This option is currently
3349    supported by MATMPIBAIJ format only.
3350 
3351    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
3352    are kept in the nonzero structure
3353 
3354    MAT_IGNORE_ZERO_ENTRIES - for AIJ matrices this will stop zero values from creating
3355    a zero location in the matrix
3356 
3357    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
3358    ROWBS matrix types
3359 
3360    MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works
3361    with AIJ and ROWBS matrix types
3362 
3363    Level: intermediate
3364 
3365    Concepts: matrices^setting options
3366 
3367 @*/
3368 int MatSetOption(Mat mat,MatOption op)
3369 {
3370   int ierr;
3371 
3372   PetscFunctionBegin;
3373   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3374   PetscValidType(mat);
3375   MatPreallocated(mat);
3376   switch (op) {
3377   case MAT_SYMMETRIC:
3378     mat->symmetric              = PETSC_TRUE;
3379     mat->structurally_symmetric = PETSC_TRUE;
3380     break;
3381   case MAT_STRUCTURALLY_SYMMETRIC:
3382     mat->structurally_symmetric = PETSC_TRUE;
3383     break;
3384   default:
3385     if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
3386     break;
3387   }
3388   PetscFunctionReturn(0);
3389 }
3390 
3391 #undef __FUNCT__
3392 #define __FUNCT__ "MatZeroEntries"
3393 /*@
3394    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
3395    this routine retains the old nonzero structure.
3396 
3397    Collective on Mat
3398 
3399    Input Parameters:
3400 .  mat - the matrix
3401 
3402    Level: intermediate
3403 
3404    Concepts: matrices^zeroing
3405 
3406 .seealso: MatZeroRows()
3407 @*/
3408 int MatZeroEntries(Mat mat)
3409 {
3410   int ierr;
3411 
3412   PetscFunctionBegin;
3413   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3414   PetscValidType(mat);
3415   MatPreallocated(mat);
3416   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3417   if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3418 
3419   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3420   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
3421   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3422   PetscFunctionReturn(0);
3423 }
3424 
3425 #undef __FUNCT__
3426 #define __FUNCT__ "MatZeroRows"
3427 /*@C
3428    MatZeroRows - Zeros all entries (except possibly the main diagonal)
3429    of a set of rows of a matrix.
3430 
3431    Collective on Mat
3432 
3433    Input Parameters:
3434 +  mat - the matrix
3435 .  is - index set of rows to remove
3436 -  diag - pointer to value put in all diagonals of eliminated rows.
3437           Note that diag is not a pointer to an array, but merely a
3438           pointer to a single value.
3439 
3440    Notes:
3441    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
3442    but does not release memory.  For the dense and block diagonal
3443    formats this does not alter the nonzero structure.
3444 
3445    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3446    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3447    merely zeroed.
3448 
3449    The user can set a value in the diagonal entry (or for the AIJ and
3450    row formats can optionally remove the main diagonal entry from the
3451    nonzero structure as well, by passing a null pointer (PETSC_NULL
3452    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3453 
3454    For the parallel case, all processes that share the matrix (i.e.,
3455    those in the communicator used for matrix creation) MUST call this
3456    routine, regardless of whether any rows being zeroed are owned by
3457    them.
3458 
3459    For the SBAIJ matrix (since only the upper triangular half of the matrix
3460    is stored) the effect of this call is to also zero the corresponding
3461    column.
3462 
3463    Level: intermediate
3464 
3465    Concepts: matrices^zeroing rows
3466 
3467 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
3468 @*/
3469 int MatZeroRows(Mat mat,IS is,PetscScalar *diag)
3470 {
3471   int ierr;
3472 
3473   PetscFunctionBegin;
3474   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3475   PetscValidType(mat);
3476   MatPreallocated(mat);
3477   PetscValidHeaderSpecific(is,IS_COOKIE);
3478   if (diag) PetscValidScalarPointer(diag);
3479   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3480   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3481   if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3482 
3483   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
3484   ierr = MatView_Private(mat);CHKERRQ(ierr);
3485   PetscFunctionReturn(0);
3486 }
3487 
3488 #undef __FUNCT__
3489 #define __FUNCT__ "MatZeroRowsLocal"
3490 /*@C
3491    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
3492    of a set of rows of a matrix; using local numbering of rows.
3493 
3494    Collective on Mat
3495 
3496    Input Parameters:
3497 +  mat - the matrix
3498 .  is - index set of rows to remove
3499 -  diag - pointer to value put in all diagonals of eliminated rows.
3500           Note that diag is not a pointer to an array, but merely a
3501           pointer to a single value.
3502 
3503    Notes:
3504    Before calling MatZeroRowsLocal(), the user must first set the
3505    local-to-global mapping by calling MatSetLocalToGlobalMapping().
3506 
3507    For the AIJ matrix formats this removes the old nonzero structure,
3508    but does not release memory.  For the dense and block diagonal
3509    formats this does not alter the nonzero structure.
3510 
3511    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3512    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3513    merely zeroed.
3514 
3515    The user can set a value in the diagonal entry (or for the AIJ and
3516    row formats can optionally remove the main diagonal entry from the
3517    nonzero structure as well, by passing a null pointer (PETSC_NULL
3518    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3519 
3520    Level: intermediate
3521 
3522    Concepts: matrices^zeroing
3523 
3524 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
3525 @*/
3526 int MatZeroRowsLocal(Mat mat,IS is,PetscScalar *diag)
3527 {
3528   int ierr;
3529   IS  newis;
3530 
3531   PetscFunctionBegin;
3532   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3533   PetscValidType(mat);
3534   MatPreallocated(mat);
3535   PetscValidHeaderSpecific(is,IS_COOKIE);
3536   if (diag) PetscValidScalarPointer(diag);
3537   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3538   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3539 
3540   if (mat->ops->zerorowslocal) {
3541     ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr);
3542   } else {
3543     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
3544     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
3545     ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
3546     ierr = ISDestroy(newis);CHKERRQ(ierr);
3547   }
3548   PetscFunctionReturn(0);
3549 }
3550 
3551 #undef __FUNCT__
3552 #define __FUNCT__ "MatGetSize"
3553 /*@
3554    MatGetSize - Returns the numbers of rows and columns in a matrix.
3555 
3556    Not Collective
3557 
3558    Input Parameter:
3559 .  mat - the matrix
3560 
3561    Output Parameters:
3562 +  m - the number of global rows
3563 -  n - the number of global columns
3564 
3565    Level: beginner
3566 
3567    Concepts: matrices^size
3568 
3569 .seealso: MatGetLocalSize()
3570 @*/
3571 int MatGetSize(Mat mat,int *m,int* n)
3572 {
3573   PetscFunctionBegin;
3574   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3575   if (m) *m = mat->M;
3576   if (n) *n = mat->N;
3577   PetscFunctionReturn(0);
3578 }
3579 
3580 #undef __FUNCT__
3581 #define __FUNCT__ "MatGetLocalSize"
3582 /*@
3583    MatGetLocalSize - Returns the number of rows and columns in a matrix
3584    stored locally.  This information may be implementation dependent, so
3585    use with care.
3586 
3587    Not Collective
3588 
3589    Input Parameters:
3590 .  mat - the matrix
3591 
3592    Output Parameters:
3593 +  m - the number of local rows
3594 -  n - the number of local columns
3595 
3596    Level: beginner
3597 
3598    Concepts: matrices^local size
3599 
3600 .seealso: MatGetSize()
3601 @*/
3602 int MatGetLocalSize(Mat mat,int *m,int* n)
3603 {
3604   PetscFunctionBegin;
3605   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3606   if (m) *m = mat->m;
3607   if (n) *n = mat->n;
3608   PetscFunctionReturn(0);
3609 }
3610 
3611 #undef __FUNCT__
3612 #define __FUNCT__ "MatGetOwnershipRange"
3613 /*@
3614    MatGetOwnershipRange - Returns the range of matrix rows owned by
3615    this processor, assuming that the matrix is laid out with the first
3616    n1 rows on the first processor, the next n2 rows on the second, etc.
3617    For certain parallel layouts this range may not be well defined.
3618 
3619    Not Collective
3620 
3621    Input Parameters:
3622 .  mat - the matrix
3623 
3624    Output Parameters:
3625 +  m - the global index of the first local row
3626 -  n - one more than the global index of the last local row
3627 
3628    Level: beginner
3629 
3630    Concepts: matrices^row ownership
3631 @*/
3632 int MatGetOwnershipRange(Mat mat,int *m,int* n)
3633 {
3634   int ierr;
3635 
3636   PetscFunctionBegin;
3637   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3638   PetscValidType(mat);
3639   MatPreallocated(mat);
3640   if (m) PetscValidIntPointer(m);
3641   if (n) PetscValidIntPointer(n);
3642   ierr = PetscMapGetLocalRange(mat->rmap,m,n);CHKERRQ(ierr);
3643   PetscFunctionReturn(0);
3644 }
3645 
3646 #undef __FUNCT__
3647 #define __FUNCT__ "MatILUFactorSymbolic"
3648 /*@
3649    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
3650    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
3651    to complete the factorization.
3652 
3653    Collective on Mat
3654 
3655    Input Parameters:
3656 +  mat - the matrix
3657 .  row - row permutation
3658 .  column - column permutation
3659 -  info - structure containing
3660 $      levels - number of levels of fill.
3661 $      expected fill - as ratio of original fill.
3662 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3663                 missing diagonal entries)
3664 
3665    Output Parameters:
3666 .  fact - new matrix that has been symbolically factored
3667 
3668    Notes:
3669    See the users manual for additional information about
3670    choosing the fill factor for better efficiency.
3671 
3672    Most users should employ the simplified SLES interface for linear solvers
3673    instead of working directly with matrix algebra routines such as this.
3674    See, e.g., SLESCreate().
3675 
3676    Level: developer
3677 
3678   Concepts: matrices^symbolic LU factorization
3679   Concepts: matrices^factorization
3680   Concepts: LU^symbolic factorization
3681 
3682 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3683           MatGetOrdering(), MatFactorInfo
3684 
3685 @*/
3686 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatFactorInfo *info,Mat *fact)
3687 {
3688   int ierr;
3689 
3690   PetscFunctionBegin;
3691   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3692   PetscValidType(mat);
3693   MatPreallocated(mat);
3694   PetscValidPointer(fact);
3695   PetscValidHeaderSpecific(row,IS_COOKIE);
3696   PetscValidHeaderSpecific(col,IS_COOKIE);
3697   if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",(int)info->levels);
3698   if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3699   if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",mat->type_name);
3700   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3701   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3702 
3703   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3704   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3705   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3706   PetscFunctionReturn(0);
3707 }
3708 
3709 #undef __FUNCT__
3710 #define __FUNCT__ "MatICCFactorSymbolic"
3711 /*@
3712    MatICCFactorSymbolic - Performs symbolic incomplete
3713    Cholesky factorization for a symmetric matrix.  Use
3714    MatCholeskyFactorNumeric() to complete the factorization.
3715 
3716    Collective on Mat
3717 
3718    Input Parameters:
3719 +  mat - the matrix
3720 .  perm - row and column permutation
3721 -  info - structure containing
3722 $      levels - number of levels of fill.
3723 $      expected fill - as ratio of original fill.
3724 
3725    Output Parameter:
3726 .  fact - the factored matrix
3727 
3728    Notes:
3729    Currently only no-fill factorization is supported.
3730 
3731    Most users should employ the simplified SLES interface for linear solvers
3732    instead of working directly with matrix algebra routines such as this.
3733    See, e.g., SLESCreate().
3734 
3735    Level: developer
3736 
3737   Concepts: matrices^symbolic incomplete Cholesky factorization
3738   Concepts: matrices^factorization
3739   Concepts: Cholsky^symbolic factorization
3740 
3741 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor(), MatFactorInfo
3742 @*/
3743 int MatICCFactorSymbolic(Mat mat,IS perm,MatFactorInfo *info,Mat *fact)
3744 {
3745   int ierr;
3746 
3747   PetscFunctionBegin;
3748   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3749   PetscValidType(mat);
3750   MatPreallocated(mat);
3751   PetscValidPointer(fact);
3752   PetscValidHeaderSpecific(perm,IS_COOKIE);
3753   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3754   if (info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels negative %d",(int) info->levels);
3755   if (info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3756   if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",mat->type_name);
3757   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3758 
3759   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3760   ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,info,fact);CHKERRQ(ierr);
3761   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3762   PetscFunctionReturn(0);
3763 }
3764 
3765 #undef __FUNCT__
3766 #define __FUNCT__ "MatGetArray"
3767 /*@C
3768    MatGetArray - Returns a pointer to the element values in the matrix.
3769    The result of this routine is dependent on the underlying matrix data
3770    structure, and may not even work for certain matrix types.  You MUST
3771    call MatRestoreArray() when you no longer need to access the array.
3772 
3773    Not Collective
3774 
3775    Input Parameter:
3776 .  mat - the matrix
3777 
3778    Output Parameter:
3779 .  v - the location of the values
3780 
3781 
3782    Fortran Note:
3783    This routine is used differently from Fortran, e.g.,
3784 .vb
3785         Mat         mat
3786         PetscScalar mat_array(1)
3787         PetscOffset i_mat
3788         int         ierr
3789         call MatGetArray(mat,mat_array,i_mat,ierr)
3790 
3791   C  Access first local entry in matrix; note that array is
3792   C  treated as one dimensional
3793         value = mat_array(i_mat + 1)
3794 
3795         [... other code ...]
3796         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3797 .ve
3798 
3799    See the Fortran chapter of the users manual and
3800    petsc/src/mat/examples/tests for details.
3801 
3802    Level: advanced
3803 
3804    Concepts: matrices^access array
3805 
3806 .seealso: MatRestoreArray(), MatGetArrayF90()
3807 @*/
3808 int MatGetArray(Mat mat,PetscScalar **v)
3809 {
3810   int ierr;
3811 
3812   PetscFunctionBegin;
3813   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3814   PetscValidType(mat);
3815   MatPreallocated(mat);
3816   PetscValidPointer(v);
3817   if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3818   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3819   PetscFunctionReturn(0);
3820 }
3821 
3822 #undef __FUNCT__
3823 #define __FUNCT__ "MatRestoreArray"
3824 /*@C
3825    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3826 
3827    Not Collective
3828 
3829    Input Parameter:
3830 +  mat - the matrix
3831 -  v - the location of the values
3832 
3833    Fortran Note:
3834    This routine is used differently from Fortran, e.g.,
3835 .vb
3836         Mat         mat
3837         PetscScalar mat_array(1)
3838         PetscOffset i_mat
3839         int         ierr
3840         call MatGetArray(mat,mat_array,i_mat,ierr)
3841 
3842   C  Access first local entry in matrix; note that array is
3843   C  treated as one dimensional
3844         value = mat_array(i_mat + 1)
3845 
3846         [... other code ...]
3847         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3848 .ve
3849 
3850    See the Fortran chapter of the users manual and
3851    petsc/src/mat/examples/tests for details
3852 
3853    Level: advanced
3854 
3855 .seealso: MatGetArray(), MatRestoreArrayF90()
3856 @*/
3857 int MatRestoreArray(Mat mat,PetscScalar **v)
3858 {
3859   int ierr;
3860 
3861   PetscFunctionBegin;
3862   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3863   PetscValidType(mat);
3864   MatPreallocated(mat);
3865   PetscValidPointer(v);
3866   if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3867   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3868   PetscFunctionReturn(0);
3869 }
3870 
3871 #undef __FUNCT__
3872 #define __FUNCT__ "MatGetSubMatrices"
3873 /*@C
3874    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3875    points to an array of valid matrices, they may be reused to store the new
3876    submatrices.
3877 
3878    Collective on Mat
3879 
3880    Input Parameters:
3881 +  mat - the matrix
3882 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3883 .  irow, icol - index sets of rows and columns to extract
3884 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3885 
3886    Output Parameter:
3887 .  submat - the array of submatrices
3888 
3889    Notes:
3890    MatGetSubMatrices() can extract only sequential submatrices
3891    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3892    to extract a parallel submatrix.
3893 
3894    When extracting submatrices from a parallel matrix, each processor can
3895    form a different submatrix by setting the rows and columns of its
3896    individual index sets according to the local submatrix desired.
3897 
3898    When finished using the submatrices, the user should destroy
3899    them with MatDestroyMatrices().
3900 
3901    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3902    original matrix has not changed from that last call to MatGetSubMatrices().
3903 
3904    This routine creates the matrices in submat; you should NOT create them before
3905    calling it. It also allocates the array of matrix pointers submat.
3906 
3907    Fortran Note:
3908    The Fortran interface is slightly different from that given below; it
3909    requires one to pass in  as submat a Mat (integer) array of size at least m.
3910 
3911    Level: advanced
3912 
3913    Concepts: matrices^accessing submatrices
3914    Concepts: submatrices
3915 
3916 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3917 @*/
3918 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3919 {
3920   int        ierr;
3921 
3922   PetscFunctionBegin;
3923   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3924   PetscValidType(mat);
3925   MatPreallocated(mat);
3926   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3927   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3928   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3929 
3930   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3931   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3932   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3933   PetscFunctionReturn(0);
3934 }
3935 
3936 #undef __FUNCT__
3937 #define __FUNCT__ "MatDestroyMatrices"
3938 /*@C
3939    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3940 
3941    Collective on Mat
3942 
3943    Input Parameters:
3944 +  n - the number of local matrices
3945 -  mat - the matrices
3946 
3947    Level: advanced
3948 
3949     Notes: Frees not only the matrices, but also the array that contains the matrices
3950 
3951 .seealso: MatGetSubMatrices()
3952 @*/
3953 int MatDestroyMatrices(int n,Mat **mat)
3954 {
3955   int ierr,i;
3956 
3957   PetscFunctionBegin;
3958   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n);
3959   PetscValidPointer(mat);
3960   for (i=0; i<n; i++) {
3961     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3962   }
3963   /* memory is allocated even if n = 0 */
3964   ierr = PetscFree(*mat);CHKERRQ(ierr);
3965   PetscFunctionReturn(0);
3966 }
3967 
3968 #undef __FUNCT__
3969 #define __FUNCT__ "MatIncreaseOverlap"
3970 /*@
3971    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3972    replaces the index sets by larger ones that represent submatrices with
3973    additional overlap.
3974 
3975    Collective on Mat
3976 
3977    Input Parameters:
3978 +  mat - the matrix
3979 .  n   - the number of index sets
3980 .  is  - the array of pointers to index sets
3981 -  ov  - the additional overlap requested
3982 
3983    Level: developer
3984 
3985    Concepts: overlap
3986    Concepts: ASM^computing overlap
3987 
3988 .seealso: MatGetSubMatrices()
3989 @*/
3990 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov)
3991 {
3992   int ierr;
3993 
3994   PetscFunctionBegin;
3995   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3996   PetscValidType(mat);
3997   MatPreallocated(mat);
3998   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3999   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4000 
4001   if (!ov) PetscFunctionReturn(0);
4002   if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4003   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
4004   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
4005   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
4006   PetscFunctionReturn(0);
4007 }
4008 
4009 #undef __FUNCT__
4010 #define __FUNCT__ "MatPrintHelp"
4011 /*@
4012    MatPrintHelp - Prints all the options for the matrix.
4013 
4014    Collective on Mat
4015 
4016    Input Parameter:
4017 .  mat - the matrix
4018 
4019    Options Database Keys:
4020 +  -help - Prints matrix options
4021 -  -h - Prints matrix options
4022 
4023    Level: developer
4024 
4025 .seealso: MatCreate(), MatCreateXXX()
4026 @*/
4027 int MatPrintHelp(Mat mat)
4028 {
4029   static PetscTruth called = PETSC_FALSE;
4030   int               ierr;
4031 
4032   PetscFunctionBegin;
4033   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4034   PetscValidType(mat);
4035   MatPreallocated(mat);
4036 
4037   if (!called) {
4038     if (mat->ops->printhelp) {
4039       ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
4040     }
4041     called = PETSC_TRUE;
4042   }
4043   PetscFunctionReturn(0);
4044 }
4045 
4046 #undef __FUNCT__
4047 #define __FUNCT__ "MatGetBlockSize"
4048 /*@
4049    MatGetBlockSize - Returns the matrix block size; useful especially for the
4050    block row and block diagonal formats.
4051 
4052    Not Collective
4053 
4054    Input Parameter:
4055 .  mat - the matrix
4056 
4057    Output Parameter:
4058 .  bs - block size
4059 
4060    Notes:
4061    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
4062    Block row formats are MATSEQBAIJ, MATMPIBAIJ
4063 
4064    Level: intermediate
4065 
4066    Concepts: matrices^block size
4067 
4068 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
4069 @*/
4070 int MatGetBlockSize(Mat mat,int *bs)
4071 {
4072   int ierr;
4073 
4074   PetscFunctionBegin;
4075   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4076   PetscValidType(mat);
4077   MatPreallocated(mat);
4078   PetscValidIntPointer(bs);
4079   if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4080   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
4081   PetscFunctionReturn(0);
4082 }
4083 
4084 #undef __FUNCT__
4085 #define __FUNCT__ "MatGetRowIJ"
4086 /*@C
4087     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
4088 
4089    Collective on Mat
4090 
4091     Input Parameters:
4092 +   mat - the matrix
4093 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
4094 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4095                 symmetrized
4096 
4097     Output Parameters:
4098 +   n - number of rows in the (possibly compressed) matrix
4099 .   ia - the row pointers
4100 .   ja - the column indices
4101 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
4102 
4103     Level: developer
4104 
4105 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
4106 @*/
4107 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4108 {
4109   int ierr;
4110 
4111   PetscFunctionBegin;
4112   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4113   PetscValidType(mat);
4114   MatPreallocated(mat);
4115   if (ia) PetscValidIntPointer(ia);
4116   if (ja) PetscValidIntPointer(ja);
4117   PetscValidIntPointer(done);
4118   if (!mat->ops->getrowij) *done = PETSC_FALSE;
4119   else {
4120     *done = PETSC_TRUE;
4121     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4122   }
4123   PetscFunctionReturn(0);
4124 }
4125 
4126 #undef __FUNCT__
4127 #define __FUNCT__ "MatGetColumnIJ"
4128 /*@C
4129     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
4130 
4131     Collective on Mat
4132 
4133     Input Parameters:
4134 +   mat - the matrix
4135 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4136 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4137                 symmetrized
4138 
4139     Output Parameters:
4140 +   n - number of columns in the (possibly compressed) matrix
4141 .   ia - the column pointers
4142 .   ja - the row indices
4143 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
4144 
4145     Level: developer
4146 
4147 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
4148 @*/
4149 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4150 {
4151   int ierr;
4152 
4153   PetscFunctionBegin;
4154   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4155   PetscValidType(mat);
4156   MatPreallocated(mat);
4157   if (ia) PetscValidIntPointer(ia);
4158   if (ja) PetscValidIntPointer(ja);
4159   PetscValidIntPointer(done);
4160 
4161   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
4162   else {
4163     *done = PETSC_TRUE;
4164     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4165   }
4166   PetscFunctionReturn(0);
4167 }
4168 
4169 #undef __FUNCT__
4170 #define __FUNCT__ "MatRestoreRowIJ"
4171 /*@C
4172     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
4173     MatGetRowIJ().
4174 
4175     Collective on Mat
4176 
4177     Input Parameters:
4178 +   mat - the matrix
4179 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4180 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4181                 symmetrized
4182 
4183     Output Parameters:
4184 +   n - size of (possibly compressed) matrix
4185 .   ia - the row pointers
4186 .   ja - the column indices
4187 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
4188 
4189     Level: developer
4190 
4191 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
4192 @*/
4193 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4194 {
4195   int ierr;
4196 
4197   PetscFunctionBegin;
4198   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4199   PetscValidType(mat);
4200   MatPreallocated(mat);
4201   if (ia) PetscValidIntPointer(ia);
4202   if (ja) PetscValidIntPointer(ja);
4203   PetscValidIntPointer(done);
4204 
4205   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
4206   else {
4207     *done = PETSC_TRUE;
4208     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4209   }
4210   PetscFunctionReturn(0);
4211 }
4212 
4213 #undef __FUNCT__
4214 #define __FUNCT__ "MatRestoreColumnIJ"
4215 /*@C
4216     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
4217     MatGetColumnIJ().
4218 
4219     Collective on Mat
4220 
4221     Input Parameters:
4222 +   mat - the matrix
4223 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4224 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4225                 symmetrized
4226 
4227     Output Parameters:
4228 +   n - size of (possibly compressed) matrix
4229 .   ia - the column pointers
4230 .   ja - the row indices
4231 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
4232 
4233     Level: developer
4234 
4235 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
4236 @*/
4237 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4238 {
4239   int ierr;
4240 
4241   PetscFunctionBegin;
4242   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4243   PetscValidType(mat);
4244   MatPreallocated(mat);
4245   if (ia) PetscValidIntPointer(ia);
4246   if (ja) PetscValidIntPointer(ja);
4247   PetscValidIntPointer(done);
4248 
4249   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
4250   else {
4251     *done = PETSC_TRUE;
4252     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4253   }
4254   PetscFunctionReturn(0);
4255 }
4256 
4257 #undef __FUNCT__
4258 #define __FUNCT__ "MatColoringPatch"
4259 /*@C
4260     MatColoringPatch -Used inside matrix coloring routines that
4261     use MatGetRowIJ() and/or MatGetColumnIJ().
4262 
4263     Collective on Mat
4264 
4265     Input Parameters:
4266 +   mat - the matrix
4267 .   n   - number of colors
4268 -   colorarray - array indicating color for each column
4269 
4270     Output Parameters:
4271 .   iscoloring - coloring generated using colorarray information
4272 
4273     Level: developer
4274 
4275 .seealso: MatGetRowIJ(), MatGetColumnIJ()
4276 
4277 @*/
4278 int MatColoringPatch(Mat mat,int n,int ncolors,ISColoringValue *colorarray,ISColoring *iscoloring)
4279 {
4280   int ierr;
4281 
4282   PetscFunctionBegin;
4283   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4284   PetscValidType(mat);
4285   MatPreallocated(mat);
4286   PetscValidIntPointer(colorarray);
4287 
4288   if (!mat->ops->coloringpatch){
4289     ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr);
4290   } else {
4291     ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr);
4292   }
4293   PetscFunctionReturn(0);
4294 }
4295 
4296 
4297 #undef __FUNCT__
4298 #define __FUNCT__ "MatSetUnfactored"
4299 /*@
4300    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
4301 
4302    Collective on Mat
4303 
4304    Input Parameter:
4305 .  mat - the factored matrix to be reset
4306 
4307    Notes:
4308    This routine should be used only with factored matrices formed by in-place
4309    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
4310    format).  This option can save memory, for example, when solving nonlinear
4311    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
4312    ILU(0) preconditioner.
4313 
4314    Note that one can specify in-place ILU(0) factorization by calling
4315 .vb
4316      PCType(pc,PCILU);
4317      PCILUSeUseInPlace(pc);
4318 .ve
4319    or by using the options -pc_type ilu -pc_ilu_in_place
4320 
4321    In-place factorization ILU(0) can also be used as a local
4322    solver for the blocks within the block Jacobi or additive Schwarz
4323    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
4324    of these preconditioners in the users manual for details on setting
4325    local solver options.
4326 
4327    Most users should employ the simplified SLES interface for linear solvers
4328    instead of working directly with matrix algebra routines such as this.
4329    See, e.g., SLESCreate().
4330 
4331    Level: developer
4332 
4333 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
4334 
4335    Concepts: matrices^unfactored
4336 
4337 @*/
4338 int MatSetUnfactored(Mat mat)
4339 {
4340   int ierr;
4341 
4342   PetscFunctionBegin;
4343   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4344   PetscValidType(mat);
4345   MatPreallocated(mat);
4346   mat->factor = 0;
4347   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
4348   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
4349   PetscFunctionReturn(0);
4350 }
4351 
4352 /*MC
4353     MatGetArrayF90 - Accesses a matrix array from Fortran90.
4354 
4355     Synopsis:
4356     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4357 
4358     Not collective
4359 
4360     Input Parameter:
4361 .   x - matrix
4362 
4363     Output Parameters:
4364 +   xx_v - the Fortran90 pointer to the array
4365 -   ierr - error code
4366 
4367     Example of Usage:
4368 .vb
4369       PetscScalar, pointer xx_v(:)
4370       ....
4371       call MatGetArrayF90(x,xx_v,ierr)
4372       a = xx_v(3)
4373       call MatRestoreArrayF90(x,xx_v,ierr)
4374 .ve
4375 
4376     Notes:
4377     Not yet supported for all F90 compilers
4378 
4379     Level: advanced
4380 
4381 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
4382 
4383     Concepts: matrices^accessing array
4384 
4385 M*/
4386 
4387 /*MC
4388     MatRestoreArrayF90 - Restores a matrix array that has been
4389     accessed with MatGetArrayF90().
4390 
4391     Synopsis:
4392     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4393 
4394     Not collective
4395 
4396     Input Parameters:
4397 +   x - matrix
4398 -   xx_v - the Fortran90 pointer to the array
4399 
4400     Output Parameter:
4401 .   ierr - error code
4402 
4403     Example of Usage:
4404 .vb
4405        PetscScalar, pointer xx_v(:)
4406        ....
4407        call MatGetArrayF90(x,xx_v,ierr)
4408        a = xx_v(3)
4409        call MatRestoreArrayF90(x,xx_v,ierr)
4410 .ve
4411 
4412     Notes:
4413     Not yet supported for all F90 compilers
4414 
4415     Level: advanced
4416 
4417 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
4418 
4419 M*/
4420 
4421 
4422 #undef __FUNCT__
4423 #define __FUNCT__ "MatGetSubMatrix"
4424 /*@
4425     MatGetSubMatrix - Gets a single submatrix on the same number of processors
4426                       as the original matrix.
4427 
4428     Collective on Mat
4429 
4430     Input Parameters:
4431 +   mat - the original matrix
4432 .   isrow - rows this processor should obtain
4433 .   iscol - columns for all processors you wish to keep
4434 .   csize - number of columns "local" to this processor (does nothing for sequential
4435             matrices). This should match the result from VecGetLocalSize(x,...) if you
4436             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
4437 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4438 
4439     Output Parameter:
4440 .   newmat - the new submatrix, of the same type as the old
4441 
4442     Level: advanced
4443 
4444     Notes: the iscol argument MUST be the same on each processor. You might be
4445     able to create the iscol argument with ISAllGather().
4446 
4447       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
4448    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
4449    to this routine with a mat of the same nonzero structure will reuse the matrix
4450    generated the first time.
4451 
4452     Concepts: matrices^submatrices
4453 
4454 .seealso: MatGetSubMatrices(), ISAllGather()
4455 @*/
4456 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
4457 {
4458   int     ierr, size;
4459   Mat     *local;
4460 
4461   PetscFunctionBegin;
4462   PetscValidType(mat);
4463   MatPreallocated(mat);
4464   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4465   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4466 
4467   /* if original matrix is on just one processor then use submatrix generated */
4468   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
4469     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
4470     PetscFunctionReturn(0);
4471   } else if (!mat->ops->getsubmatrix && size == 1) {
4472     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
4473     *newmat = *local;
4474     ierr    = PetscFree(local);CHKERRQ(ierr);
4475     PetscFunctionReturn(0);
4476   }
4477 
4478   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4479   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
4480   PetscFunctionReturn(0);
4481 }
4482 
4483 #undef __FUNCT__
4484 #define __FUNCT__ "MatGetPetscMaps"
4485 /*@C
4486    MatGetPetscMaps - Returns the maps associated with the matrix.
4487 
4488    Not Collective
4489 
4490    Input Parameter:
4491 .  mat - the matrix
4492 
4493    Output Parameters:
4494 +  rmap - the row (right) map
4495 -  cmap - the column (left) map
4496 
4497    Level: developer
4498 
4499    Concepts: maps^getting from matrix
4500 
4501 @*/
4502 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap)
4503 {
4504   int ierr;
4505 
4506   PetscFunctionBegin;
4507   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4508   PetscValidType(mat);
4509   MatPreallocated(mat);
4510   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
4511   PetscFunctionReturn(0);
4512 }
4513 
4514 /*
4515       Version that works for all PETSc matrices
4516 */
4517 #undef __FUNCT__
4518 #define __FUNCT__ "MatGetPetscMaps_Petsc"
4519 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap)
4520 {
4521   PetscFunctionBegin;
4522   if (rmap) *rmap = mat->rmap;
4523   if (cmap) *cmap = mat->cmap;
4524   PetscFunctionReturn(0);
4525 }
4526 
4527 #undef __FUNCT__
4528 #define __FUNCT__ "MatSetStashInitialSize"
4529 /*@
4530    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
4531    used during the assembly process to store values that belong to
4532    other processors.
4533 
4534    Not Collective
4535 
4536    Input Parameters:
4537 +  mat   - the matrix
4538 .  size  - the initial size of the stash.
4539 -  bsize - the initial size of the block-stash(if used).
4540 
4541    Options Database Keys:
4542 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
4543 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
4544 
4545    Level: intermediate
4546 
4547    Notes:
4548      The block-stash is used for values set with VecSetValuesBlocked() while
4549      the stash is used for values set with VecSetValues()
4550 
4551      Run with the option -log_info and look for output of the form
4552      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
4553      to determine the appropriate value, MM, to use for size and
4554      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
4555      to determine the value, BMM to use for bsize
4556 
4557    Concepts: stash^setting matrix size
4558    Concepts: matrices^stash
4559 
4560 @*/
4561 int MatSetStashInitialSize(Mat mat,int size, int bsize)
4562 {
4563   int ierr;
4564 
4565   PetscFunctionBegin;
4566   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4567   PetscValidType(mat);
4568   MatPreallocated(mat);
4569   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
4570   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
4571   PetscFunctionReturn(0);
4572 }
4573 
4574 #undef __FUNCT__
4575 #define __FUNCT__ "MatInterpolateAdd"
4576 /*@
4577    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
4578      the matrix
4579 
4580    Collective on Mat
4581 
4582    Input Parameters:
4583 +  mat   - the matrix
4584 .  x,y - the vectors
4585 -  w - where the result is stored
4586 
4587    Level: intermediate
4588 
4589    Notes:
4590     w may be the same vector as y.
4591 
4592     This allows one to use either the restriction or interpolation (its transpose)
4593     matrix to do the interpolation
4594 
4595     Concepts: interpolation
4596 
4597 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4598 
4599 @*/
4600 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
4601 {
4602   int M,N,ierr;
4603 
4604   PetscFunctionBegin;
4605   PetscValidType(A);
4606   MatPreallocated(A);
4607   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4608   if (N > M) {
4609     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
4610   } else {
4611     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
4612   }
4613   PetscFunctionReturn(0);
4614 }
4615 
4616 #undef __FUNCT__
4617 #define __FUNCT__ "MatInterpolate"
4618 /*@
4619    MatInterpolate - y = A*x or A'*x depending on the shape of
4620      the matrix
4621 
4622    Collective on Mat
4623 
4624    Input Parameters:
4625 +  mat   - the matrix
4626 -  x,y - the vectors
4627 
4628    Level: intermediate
4629 
4630    Notes:
4631     This allows one to use either the restriction or interpolation (its transpose)
4632     matrix to do the interpolation
4633 
4634    Concepts: matrices^interpolation
4635 
4636 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4637 
4638 @*/
4639 int MatInterpolate(Mat A,Vec x,Vec y)
4640 {
4641   int M,N,ierr;
4642 
4643   PetscFunctionBegin;
4644   PetscValidType(A);
4645   MatPreallocated(A);
4646   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4647   if (N > M) {
4648     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4649   } else {
4650     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4651   }
4652   PetscFunctionReturn(0);
4653 }
4654 
4655 #undef __FUNCT__
4656 #define __FUNCT__ "MatRestrict"
4657 /*@
4658    MatRestrict - y = A*x or A'*x
4659 
4660    Collective on Mat
4661 
4662    Input Parameters:
4663 +  mat   - the matrix
4664 -  x,y - the vectors
4665 
4666    Level: intermediate
4667 
4668    Notes:
4669     This allows one to use either the restriction or interpolation (its transpose)
4670     matrix to do the restriction
4671 
4672    Concepts: matrices^restriction
4673 
4674 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
4675 
4676 @*/
4677 int MatRestrict(Mat A,Vec x,Vec y)
4678 {
4679   int M,N,ierr;
4680 
4681   PetscFunctionBegin;
4682   PetscValidType(A);
4683   MatPreallocated(A);
4684   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4685   if (N > M) {
4686     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4687   } else {
4688     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4689   }
4690   PetscFunctionReturn(0);
4691 }
4692 
4693 #undef __FUNCT__
4694 #define __FUNCT__ "MatNullSpaceAttach"
4695 /*@C
4696    MatNullSpaceAttach - attaches a null space to a matrix.
4697         This null space will be removed from the resulting vector whenever
4698         MatMult() is called
4699 
4700    Collective on Mat
4701 
4702    Input Parameters:
4703 +  mat - the matrix
4704 -  nullsp - the null space object
4705 
4706    Level: developer
4707 
4708    Notes:
4709       Overwrites any previous null space that may have been attached
4710 
4711    Concepts: null space^attaching to matrix
4712 
4713 .seealso: MatCreate(), MatNullSpaceCreate()
4714 @*/
4715 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
4716 {
4717   int ierr;
4718 
4719   PetscFunctionBegin;
4720   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4721   PetscValidType(mat);
4722   MatPreallocated(mat);
4723   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE);
4724 
4725   if (mat->nullsp) {
4726     ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr);
4727   }
4728   mat->nullsp = nullsp;
4729   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
4730   PetscFunctionReturn(0);
4731 }
4732 
4733 #undef __FUNCT__
4734 #define __FUNCT__ "MatICCFactor"
4735 /*@
4736    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
4737 
4738    Collective on Mat
4739 
4740    Input Parameters:
4741 +  mat - the matrix
4742 .  row - row/column permutation
4743 .  fill - expected fill factor >= 1.0
4744 -  level - level of fill, for ICC(k)
4745 
4746    Notes:
4747    Probably really in-place only when level of fill is zero, otherwise allocates
4748    new space to store factored matrix and deletes previous memory.
4749 
4750    Most users should employ the simplified SLES interface for linear solvers
4751    instead of working directly with matrix algebra routines such as this.
4752    See, e.g., SLESCreate().
4753 
4754    Level: developer
4755 
4756    Concepts: matrices^incomplete Cholesky factorization
4757    Concepts: Cholesky factorization
4758 
4759 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
4760 @*/
4761 int MatICCFactor(Mat mat,IS row,MatFactorInfo* info)
4762 {
4763   int ierr;
4764 
4765   PetscFunctionBegin;
4766   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4767   PetscValidType(mat);
4768   MatPreallocated(mat);
4769   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
4770   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4771   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4772   if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4773   ierr = (*mat->ops->iccfactor)(mat,row,info);CHKERRQ(ierr);
4774   PetscFunctionReturn(0);
4775 }
4776 
4777 #undef __FUNCT__
4778 #define __FUNCT__ "MatSetValuesAdic"
4779 /*@
4780    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
4781 
4782    Not Collective
4783 
4784    Input Parameters:
4785 +  mat - the matrix
4786 -  v - the values compute with ADIC
4787 
4788    Level: developer
4789 
4790    Notes:
4791      Must call MatSetColoring() before using this routine. Also this matrix must already
4792      have its nonzero pattern determined.
4793 
4794 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4795           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
4796 @*/
4797 int MatSetValuesAdic(Mat mat,void *v)
4798 {
4799   int ierr;
4800 
4801   PetscFunctionBegin;
4802   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4803   PetscValidType(mat);
4804 
4805   if (!mat->assembled) {
4806     SETERRQ(1,"Matrix must be already assembled");
4807   }
4808   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4809   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4810   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
4811   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4812   ierr = MatView_Private(mat);CHKERRQ(ierr);
4813   PetscFunctionReturn(0);
4814 }
4815 
4816 
4817 #undef __FUNCT__
4818 #define __FUNCT__ "MatSetColoring"
4819 /*@
4820    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
4821 
4822    Not Collective
4823 
4824    Input Parameters:
4825 +  mat - the matrix
4826 -  coloring - the coloring
4827 
4828    Level: developer
4829 
4830 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4831           MatSetValues(), MatSetValuesAdic()
4832 @*/
4833 int MatSetColoring(Mat mat,ISColoring coloring)
4834 {
4835   int ierr;
4836 
4837   PetscFunctionBegin;
4838   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4839   PetscValidType(mat);
4840 
4841   if (!mat->assembled) {
4842     SETERRQ(1,"Matrix must be already assembled");
4843   }
4844   if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4845   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
4846   PetscFunctionReturn(0);
4847 }
4848 
4849 #undef __FUNCT__
4850 #define __FUNCT__ "MatSetValuesAdifor"
4851 /*@
4852    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
4853 
4854    Not Collective
4855 
4856    Input Parameters:
4857 +  mat - the matrix
4858 .  nl - leading dimension of v
4859 -  v - the values compute with ADIFOR
4860 
4861    Level: developer
4862 
4863    Notes:
4864      Must call MatSetColoring() before using this routine. Also this matrix must already
4865      have its nonzero pattern determined.
4866 
4867 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4868           MatSetValues(), MatSetColoring()
4869 @*/
4870 int MatSetValuesAdifor(Mat mat,int nl,void *v)
4871 {
4872   int ierr;
4873 
4874   PetscFunctionBegin;
4875   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4876   PetscValidType(mat);
4877 
4878   if (!mat->assembled) {
4879     SETERRQ(1,"Matrix must be already assembled");
4880   }
4881   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4882   if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4883   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
4884   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4885   PetscFunctionReturn(0);
4886 }
4887 
4888 EXTERN int MatMPIAIJDiagonalScaleLocal(Mat,Vec);
4889 EXTERN int MatMPIBAIJDiagonalScaleLocal(Mat,Vec);
4890 
4891 #undef __FUNCT__
4892 #define __FUNCT__ "MatDiagonalScaleLocal"
4893 /*@
4894    MatDiagonalScaleLocal - Scales columns of a matrix given the scaling values including the
4895          ghosted ones.
4896 
4897    Not Collective
4898 
4899    Input Parameters:
4900 +  mat - the matrix
4901 -  diag = the diagonal values, including ghost ones
4902 
4903    Level: developer
4904 
4905    Notes: Works only for MPIAIJ and MPIBAIJ matrices
4906 
4907 .seealso: MatDiagonalScale()
4908 @*/
4909 int MatDiagonalScaleLocal(Mat mat,Vec diag)
4910 {
4911   int        ierr;
4912   PetscTruth flag;
4913 
4914   PetscFunctionBegin;
4915   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4916   PetscValidHeaderSpecific(diag,VEC_COOKIE);
4917   PetscValidType(mat);
4918 
4919   if (!mat->assembled) {
4920     SETERRQ(1,"Matrix must be already assembled");
4921   }
4922   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4923   ierr = PetscTypeCompare((PetscObject)mat,MATMPIAIJ,&flag);CHKERRQ(ierr);
4924   if (flag) {
4925     ierr = MatMPIAIJDiagonalScaleLocal(mat,diag);CHKERRQ(ierr);
4926   } else {
4927     ierr = PetscTypeCompare((PetscObject)mat,MATMPIBAIJ,&flag);CHKERRQ(ierr);
4928     if (flag) {
4929       ierr = MatMPIBAIJDiagonalScaleLocal(mat,diag);CHKERRQ(ierr);
4930     } else {
4931       int size;
4932       ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4933       if (size == 1) {
4934         int n,m;
4935         ierr = VecGetSize(diag,&n);CHKERRQ(ierr);
4936         ierr = MatGetSize(mat,0,&m);CHKERRQ(ierr);
4937         if (m == n) {
4938           ierr = MatDiagonalScale(mat,0,diag);CHKERRQ(ierr);
4939         } else {
4940           SETERRQ(1,"Only supprted for sequential matrices when no ghost points/periodic conditions");
4941         }
4942       } else {
4943         SETERRQ(1,"Only supported for MPIAIJ and MPIBAIJ parallel matrices");
4944       }
4945     }
4946   }
4947   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
4948   PetscFunctionReturn(0);
4949 }
4950 
4951 #undef __FUNCT__
4952 #define __FUNCT__ "MatGetInertia"
4953 /*@
4954    MatGetInertia - Gets the inertia from a factored matrix
4955 
4956    Collective on Mat
4957 
4958    Input Parameter:
4959 .  mat - the matrix
4960 
4961    Output Parameters:
4962 +   nneg - number of negative eigenvalues
4963 .   nzero - number of zero eigenvalues
4964 -   npos - number of positive eigenvalues
4965 
4966    Level: advanced
4967 
4968    Notes: Matrix must have been factored by MatCholeskyFactor()
4969 
4970 
4971 @*/
4972 int MatGetInertia(Mat mat,int *nneg,int *nzero,int *npos)
4973 {
4974   int        ierr;
4975 
4976   PetscFunctionBegin;
4977   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4978   PetscValidType(mat);
4979   if (!mat->factor)    SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
4980   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Numeric factor mat is not assembled");
4981   if (!mat->ops->getinertia) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4982   ierr = (*mat->ops->getinertia)(mat,nneg,nzero,npos);CHKERRQ(ierr);
4983   PetscFunctionReturn(0);
4984 }
4985 
4986 /* ----------------------------------------------------------------*/
4987 #undef __FUNCT__
4988 #define __FUNCT__ "MatSolves"
4989 /*@
4990    MatSolves - Solves A x = b, given a factored matrix, for a collection of vectors
4991 
4992    Collective on Mat and Vecs
4993 
4994    Input Parameters:
4995 +  mat - the factored matrix
4996 -  b - the right-hand-side vectors
4997 
4998    Output Parameter:
4999 .  x - the result vectors
5000 
5001    Notes:
5002    The vectors b and x cannot be the same.  I.e., one cannot
5003    call MatSolves(A,x,x).
5004 
5005    Notes:
5006    Most users should employ the simplified SLES interface for linear solvers
5007    instead of working directly with matrix algebra routines such as this.
5008    See, e.g., SLESCreate().
5009 
5010    Level: developer
5011 
5012    Concepts: matrices^triangular solves
5013 
5014 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd(), MatSolve()
5015 @*/
5016 int MatSolves(Mat mat,Vecs b,Vecs x)
5017 {
5018   int ierr;
5019 
5020   PetscFunctionBegin;
5021   PetscValidHeaderSpecific(mat,MAT_COOKIE);
5022   PetscValidType(mat);
5023   MatPreallocated(mat);
5024   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
5025   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
5026   if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0);
5027 
5028   if (!mat->ops->solves) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
5029   ierr = PetscLogEventBegin(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
5030   ierr = (*mat->ops->solves)(mat,b,x);CHKERRQ(ierr);
5031   ierr = PetscLogEventEnd(MAT_Solves,mat,0,0,0);CHKERRQ(ierr);
5032   PetscFunctionReturn(0);
5033 }
5034