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