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