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