xref: /petsc/src/mat/interface/matrix.c (revision 325e03ae252f1b9b8bd112fd0e09178d7d92063d)
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   ierr = PetscLogEventBegin(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1172   ierr = (*mat->ops->multtranspose)(mat,x,y);CHKERRQ(ierr);
1173   ierr = PetscLogEventEnd(MAT_MultTranspose,mat,x,y,0);CHKERRQ(ierr);
1174   PetscFunctionReturn(0);
1175 }
1176 
1177 #undef __FUNCT__
1178 #define __FUNCT__ "MatMultAdd"
1179 /*@
1180     MatMultAdd -  Computes v3 = v2 + A * v1.
1181 
1182     Collective on Mat and Vec
1183 
1184     Input Parameters:
1185 +   mat - the matrix
1186 -   v1, v2 - the vectors
1187 
1188     Output Parameters:
1189 .   v3 - the result
1190 
1191     Notes:
1192     The vectors v1 and v3 cannot be the same.  I.e., one cannot
1193     call MatMultAdd(A,v1,v2,v1).
1194 
1195     Level: beginner
1196 
1197     Concepts: matrix vector product^addition
1198 
1199 .seealso: MatMultTranspose(), MatMult(), MatMultTransposeAdd()
1200 @*/
1201 int MatMultAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1202 {
1203   int ierr;
1204 
1205   PetscFunctionBegin;
1206   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1207   PetscValidType(mat);
1208   MatPreallocated(mat);
1209   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1210   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1211   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1212 
1213   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1214   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1215   if (mat->N != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->N,v1->N);
1216   if (mat->M != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->M,v2->N);
1217   if (mat->M != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->M,v3->N);
1218   if (mat->m != v3->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: local dim %d %d",mat->m,v3->n);
1219   if (mat->m != v2->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: local dim %d %d",mat->m,v2->n);
1220   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1221 
1222   ierr = PetscLogEventBegin(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1223   ierr = (*mat->ops->multadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1224   ierr = PetscLogEventEnd(MAT_MultAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1225   PetscFunctionReturn(0);
1226 }
1227 
1228 #undef __FUNCT__
1229 #define __FUNCT__ "MatMultTransposeAdd"
1230 /*@
1231    MatMultTransposeAdd - Computes v3 = v2 + A' * v1.
1232 
1233    Collective on Mat and Vec
1234 
1235    Input Parameters:
1236 +  mat - the matrix
1237 -  v1, v2 - the vectors
1238 
1239    Output Parameters:
1240 .  v3 - the result
1241 
1242    Notes:
1243    The vectors v1 and v3 cannot be the same.  I.e., one cannot
1244    call MatMultTransposeAdd(A,v1,v2,v1).
1245 
1246    Level: beginner
1247 
1248    Concepts: matrix vector product^transpose and addition
1249 
1250 .seealso: MatMultTranspose(), MatMultAdd(), MatMult()
1251 @*/
1252 int MatMultTransposeAdd(Mat mat,Vec v1,Vec v2,Vec v3)
1253 {
1254   int ierr;
1255 
1256   PetscFunctionBegin;
1257   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1258   PetscValidType(mat);
1259   MatPreallocated(mat);
1260   PetscValidHeaderSpecific(v1,VEC_COOKIE);
1261   PetscValidHeaderSpecific(v2,VEC_COOKIE);
1262   PetscValidHeaderSpecific(v3,VEC_COOKIE);
1263 
1264   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1265   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1266   if (!mat->ops->multtransposeadd) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1267   if (v1 == v3) SETERRQ(PETSC_ERR_ARG_IDN,"v1 and v3 must be different vectors");
1268   if (mat->M != v1->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v1: global dim %d %d",mat->M,v1->N);
1269   if (mat->N != v2->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v2: global dim %d %d",mat->N,v2->N);
1270   if (mat->N != v3->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec v3: global dim %d %d",mat->N,v3->N);
1271 
1272   ierr = PetscLogEventBegin(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1273   ierr = (*mat->ops->multtransposeadd)(mat,v1,v2,v3);CHKERRQ(ierr);
1274   ierr = PetscLogEventEnd(MAT_MultTransposeAdd,mat,v1,v2,v3);CHKERRQ(ierr);
1275   PetscFunctionReturn(0);
1276 }
1277 
1278 #undef __FUNCT__
1279 #define __FUNCT__ "MatMultConstrained"
1280 /*@
1281    MatMultConstrained - The inner multiplication routine for a
1282    constrained matrix P^T A P.
1283 
1284    Collective on Mat and Vec
1285 
1286    Input Parameters:
1287 +  mat - the matrix
1288 -  x   - the vector to be multilplied
1289 
1290    Output Parameters:
1291 .  y - the result
1292 
1293    Notes:
1294    The vectors x and y cannot be the same.  I.e., one cannot
1295    call MatMult(A,y,y).
1296 
1297    Level: beginner
1298 
1299 .keywords: matrix, multiply, matrix-vector product, constraint
1300 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd()
1301 @*/
1302 int MatMultConstrained(Mat mat,Vec x,Vec y)
1303 {
1304   int ierr;
1305 
1306   PetscFunctionBegin;
1307   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1308   PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE);
1309   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1310   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1311   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1312   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1313   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1314   if (mat->m != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: local dim %d %d",mat->m,y->n);
1315 
1316   ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1317   ierr = (*mat->ops->multconstrained)(mat,x,y); CHKERRQ(ierr);
1318   ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1319 
1320   PetscFunctionReturn(0);
1321 }
1322 
1323 #undef __FUNCT__
1324 #define __FUNCT__ "MatMultTransposeConstrained"
1325 /*@
1326    MatMultTransposeConstrained - The inner multiplication routine for a
1327    constrained matrix P^T A^T P.
1328 
1329    Collective on Mat and Vec
1330 
1331    Input Parameters:
1332 +  mat - the matrix
1333 -  x   - the vector to be multilplied
1334 
1335    Output Parameters:
1336 .  y - the result
1337 
1338    Notes:
1339    The vectors x and y cannot be the same.  I.e., one cannot
1340    call MatMult(A,y,y).
1341 
1342    Level: beginner
1343 
1344 .keywords: matrix, multiply, matrix-vector product, constraint
1345 .seealso: MatMult(), MatMultTrans(), MatMultAdd(), MatMultTransAdd()
1346 @*/
1347 int MatMultTransposeConstrained(Mat mat,Vec x,Vec y)
1348 {
1349   int ierr;
1350 
1351   PetscFunctionBegin;
1352   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1353   PetscValidHeaderSpecific(x,VEC_COOKIE);PetscValidHeaderSpecific(y,VEC_COOKIE);
1354   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1355   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1356   if (x == y) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"x and y must be different vectors");
1357   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1358   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
1359 
1360   ierr = PetscLogEventBegin(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1361   ierr = (*mat->ops->multtransposeconstrained)(mat,x,y);CHKERRQ(ierr);
1362   ierr = PetscLogEventEnd(MAT_MultConstrained,mat,x,y,0);CHKERRQ(ierr);
1363 
1364   PetscFunctionReturn(0);
1365 }
1366 /* ------------------------------------------------------------*/
1367 #undef __FUNCT__
1368 #define __FUNCT__ "MatGetInfo"
1369 /*@C
1370    MatGetInfo - Returns information about matrix storage (number of
1371    nonzeros, memory, etc.).
1372 
1373    Collective on Mat if MAT_GLOBAL_MAX or MAT_GLOBAL_SUM is used
1374    as the flag
1375 
1376    Input Parameters:
1377 .  mat - the matrix
1378 
1379    Output Parameters:
1380 +  flag - flag indicating the type of parameters to be returned
1381    (MAT_LOCAL - local matrix, MAT_GLOBAL_MAX - maximum over all processors,
1382    MAT_GLOBAL_SUM - sum over all processors)
1383 -  info - matrix information context
1384 
1385    Notes:
1386    The MatInfo context contains a variety of matrix data, including
1387    number of nonzeros allocated and used, number of mallocs during
1388    matrix assembly, etc.  Additional information for factored matrices
1389    is provided (such as the fill ratio, number of mallocs during
1390    factorization, etc.).  Much of this info is printed to STDOUT
1391    when using the runtime options
1392 $       -log_info -mat_view_info
1393 
1394    Example for C/C++ Users:
1395    See the file ${PETSC_DIR}/include/petscmat.h for a complete list of
1396    data within the MatInfo context.  For example,
1397 .vb
1398       MatInfo info;
1399       Mat     A;
1400       double  mal, nz_a, nz_u;
1401 
1402       MatGetInfo(A,MAT_LOCAL,&info);
1403       mal  = info.mallocs;
1404       nz_a = info.nz_allocated;
1405 .ve
1406 
1407    Example for Fortran Users:
1408    Fortran users should declare info as a double precision
1409    array of dimension MAT_INFO_SIZE, and then extract the parameters
1410    of interest.  See the file ${PETSC_DIR}/include/finclude/petscmat.h
1411    a complete list of parameter names.
1412 .vb
1413       double  precision info(MAT_INFO_SIZE)
1414       double  precision mal, nz_a
1415       Mat     A
1416       integer ierr
1417 
1418       call MatGetInfo(A,MAT_LOCAL,info,ierr)
1419       mal = info(MAT_INFO_MALLOCS)
1420       nz_a = info(MAT_INFO_NZ_ALLOCATED)
1421 .ve
1422 
1423     Level: intermediate
1424 
1425     Concepts: matrices^getting information on
1426 
1427 @*/
1428 int MatGetInfo(Mat mat,MatInfoType flag,MatInfo *info)
1429 {
1430   int ierr;
1431 
1432   PetscFunctionBegin;
1433   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1434   PetscValidType(mat);
1435   MatPreallocated(mat);
1436   PetscValidPointer(info);
1437   if (!mat->ops->getinfo) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1438   ierr = (*mat->ops->getinfo)(mat,flag,info);CHKERRQ(ierr);
1439   PetscFunctionReturn(0);
1440 }
1441 
1442 /* ----------------------------------------------------------*/
1443 #undef __FUNCT__
1444 #define __FUNCT__ "MatILUDTFactor"
1445 /*@C
1446    MatILUDTFactor - Performs a drop tolerance ILU factorization.
1447 
1448    Collective on Mat
1449 
1450    Input Parameters:
1451 +  mat - the matrix
1452 .  info - information about the factorization to be done
1453 .  row - row permutation
1454 -  col - column permutation
1455 
1456    Output Parameters:
1457 .  fact - the factored matrix
1458 
1459    Level: developer
1460 
1461    Notes:
1462    Most users should employ the simplified SLES interface for linear solvers
1463    instead of working directly with matrix algebra routines such as this.
1464    See, e.g., SLESCreate().
1465 
1466    This is currently only supported for the SeqAIJ matrix format using code
1467    from Yousef Saad's SPARSEKIT2  package (translated to C with f2c) and/or
1468    Matlab. SPARSEKIT2 is copyrighted by Yousef Saad with the GNU copyright
1469    and thus can be distributed with PETSc.
1470 
1471     Concepts: matrices^ILUDT factorization
1472 
1473 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatILUInfo
1474 @*/
1475 int MatILUDTFactor(Mat mat,MatILUInfo *info,IS row,IS col,Mat *fact)
1476 {
1477   int ierr;
1478 
1479   PetscFunctionBegin;
1480   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1481   PetscValidType(mat);
1482   MatPreallocated(mat);
1483   PetscValidPointer(fact);
1484   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1485   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1486   if (!mat->ops->iludtfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1487 
1488   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1489   ierr = (*mat->ops->iludtfactor)(mat,info,row,col,fact);CHKERRQ(ierr);
1490   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1491 
1492   PetscFunctionReturn(0);
1493 }
1494 
1495 #undef __FUNCT__
1496 #define __FUNCT__ "MatLUFactor"
1497 /*@
1498    MatLUFactor - Performs in-place LU factorization of matrix.
1499 
1500    Collective on Mat
1501 
1502    Input Parameters:
1503 +  mat - the matrix
1504 .  row - row permutation
1505 .  col - column permutation
1506 -  info - options for factorization, includes
1507 $          fill - expected fill as ratio of original fill.
1508 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1509 $                   Run with the option -log_info to determine an optimal value to use
1510 
1511    Notes:
1512    Most users should employ the simplified SLES interface for linear solvers
1513    instead of working directly with matrix algebra routines such as this.
1514    See, e.g., SLESCreate().
1515 
1516    This changes the state of the matrix to a factored matrix; it cannot be used
1517    for example with MatSetValues() unless one first calls MatSetUnfactored().
1518 
1519    Level: developer
1520 
1521    Concepts: matrices^LU factorization
1522 
1523 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(),
1524           MatGetOrdering(), MatSetUnfactored(), MatLUInfo
1525 
1526 @*/
1527 int MatLUFactor(Mat mat,IS row,IS col,MatLUInfo *info)
1528 {
1529   int ierr;
1530 
1531   PetscFunctionBegin;
1532   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1533   PetscValidType(mat);
1534   MatPreallocated(mat);
1535   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1536   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1537   if (!mat->ops->lufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1538 
1539   ierr = PetscLogEventBegin(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1540   ierr = (*mat->ops->lufactor)(mat,row,col,info);CHKERRQ(ierr);
1541   ierr = PetscLogEventEnd(MAT_LUFactor,mat,row,col,0);CHKERRQ(ierr);
1542   PetscFunctionReturn(0);
1543 }
1544 
1545 #undef __FUNCT__
1546 #define __FUNCT__ "MatILUFactor"
1547 /*@
1548    MatILUFactor - Performs in-place ILU factorization of matrix.
1549 
1550    Collective on Mat
1551 
1552    Input Parameters:
1553 +  mat - the matrix
1554 .  row - row permutation
1555 .  col - column permutation
1556 -  info - structure containing
1557 $      levels - number of levels of fill.
1558 $      expected fill - as ratio of original fill.
1559 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
1560                 missing diagonal entries)
1561 
1562    Notes:
1563    Probably really in-place only when level of fill is zero, otherwise allocates
1564    new space to store factored matrix and deletes previous memory.
1565 
1566    Most users should employ the simplified SLES interface for linear solvers
1567    instead of working directly with matrix algebra routines such as this.
1568    See, e.g., SLESCreate().
1569 
1570    Level: developer
1571 
1572    Concepts: matrices^ILU factorization
1573 
1574 .seealso: MatILUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor(), MatILUInfo
1575 @*/
1576 int MatILUFactor(Mat mat,IS row,IS col,MatILUInfo *info)
1577 {
1578   int ierr;
1579 
1580   PetscFunctionBegin;
1581   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1582   PetscValidType(mat);
1583   MatPreallocated(mat);
1584   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
1585   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1586   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1587   if (!mat->ops->ilufactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1588 
1589   ierr = PetscLogEventBegin(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1590   ierr = (*mat->ops->ilufactor)(mat,row,col,info);CHKERRQ(ierr);
1591   ierr = PetscLogEventEnd(MAT_ILUFactor,mat,row,col,0);CHKERRQ(ierr);
1592   PetscFunctionReturn(0);
1593 }
1594 
1595 #undef __FUNCT__
1596 #define __FUNCT__ "MatLUFactorSymbolic"
1597 /*@
1598    MatLUFactorSymbolic - Performs symbolic LU factorization of matrix.
1599    Call this routine before calling MatLUFactorNumeric().
1600 
1601    Collective on Mat
1602 
1603    Input Parameters:
1604 +  mat - the matrix
1605 .  row, col - row and column permutations
1606 -  info - options for factorization, includes
1607 $          fill - expected fill as ratio of original fill.
1608 $          dtcol - pivot tolerance (0 no pivot, 1 full column pivoting)
1609 $                   Run with the option -log_info to determine an optimal value to use
1610 
1611    Output Parameter:
1612 .  fact - new matrix that has been symbolically factored
1613 
1614    Notes:
1615    See the users manual for additional information about
1616    choosing the fill factor for better efficiency.
1617 
1618    Most users should employ the simplified SLES interface for linear solvers
1619    instead of working directly with matrix algebra routines such as this.
1620    See, e.g., SLESCreate().
1621 
1622    Level: developer
1623 
1624    Concepts: matrices^LU symbolic factorization
1625 
1626 .seealso: MatLUFactor(), MatLUFactorNumeric(), MatCholeskyFactor(), MatLUInfo
1627 @*/
1628 int MatLUFactorSymbolic(Mat mat,IS row,IS col,MatLUInfo *info,Mat *fact)
1629 {
1630   int ierr;
1631 
1632   PetscFunctionBegin;
1633   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1634   PetscValidType(mat);
1635   MatPreallocated(mat);
1636   PetscValidPointer(fact);
1637   PetscValidHeaderSpecific(row,IS_COOKIE);
1638   PetscValidHeaderSpecific(col,IS_COOKIE);
1639   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1640   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1641   if (!mat->ops->lufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic LU",mat->type_name);
1642 
1643   ierr = PetscLogEventBegin(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1644   ierr = (*mat->ops->lufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
1645   ierr = PetscLogEventEnd(MAT_LUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
1646   PetscFunctionReturn(0);
1647 }
1648 
1649 #undef __FUNCT__
1650 #define __FUNCT__ "MatLUFactorNumeric"
1651 /*@
1652    MatLUFactorNumeric - Performs numeric LU factorization of a matrix.
1653    Call this routine after first calling MatLUFactorSymbolic().
1654 
1655    Collective on Mat
1656 
1657    Input Parameters:
1658 +  mat - the matrix
1659 -  fact - the matrix generated for the factor, from MatLUFactorSymbolic()
1660 
1661    Notes:
1662    See MatLUFactor() for in-place factorization.  See
1663    MatCholeskyFactorNumeric() for the symmetric, positive definite case.
1664 
1665    Most users should employ the simplified SLES interface for linear solvers
1666    instead of working directly with matrix algebra routines such as this.
1667    See, e.g., SLESCreate().
1668 
1669    Level: developer
1670 
1671    Concepts: matrices^LU numeric factorization
1672 
1673 .seealso: MatLUFactorSymbolic(), MatLUFactor(), MatCholeskyFactor()
1674 @*/
1675 int MatLUFactorNumeric(Mat mat,Mat *fact)
1676 {
1677   int        ierr;
1678   PetscTruth flg;
1679 
1680   PetscFunctionBegin;
1681   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1682   PetscValidType(mat);
1683   MatPreallocated(mat);
1684   PetscValidPointer(fact);
1685   PetscValidHeaderSpecific(*fact,MAT_COOKIE);
1686   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1687   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1688     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dimensions are different %d should = %d %d should = %d",
1689             mat->M,(*fact)->M,mat->N,(*fact)->N);
1690   }
1691   if (!(*fact)->ops->lufactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1692 
1693   ierr = PetscLogEventBegin(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1694   ierr = (*(*fact)->ops->lufactornumeric)(mat,fact);CHKERRQ(ierr);
1695   ierr = PetscLogEventEnd(MAT_LUFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1696   ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_draw",&flg);CHKERRQ(ierr);
1697   if (flg) {
1698     ierr = PetscOptionsHasName(PETSC_NULL,"-mat_view_contour",&flg);CHKERRQ(ierr);
1699     if (flg) {
1700       ierr = PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
1701     }
1702     ierr = MatView(*fact,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1703     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1704     if (flg) {
1705       ierr = PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
1706     }
1707   }
1708   PetscFunctionReturn(0);
1709 }
1710 
1711 #undef __FUNCT__
1712 #define __FUNCT__ "MatCholeskyFactor"
1713 /*@
1714    MatCholeskyFactor - Performs in-place Cholesky factorization of a
1715    symmetric matrix.
1716 
1717    Collective on Mat
1718 
1719    Input Parameters:
1720 +  mat - the matrix
1721 .  perm - row and column permutations
1722 -  f - expected fill as ratio of original fill
1723 
1724    Notes:
1725    See MatLUFactor() for the nonsymmetric case.  See also
1726    MatCholeskyFactorSymbolic(), and MatCholeskyFactorNumeric().
1727 
1728    Most users should employ the simplified SLES interface for linear solvers
1729    instead of working directly with matrix algebra routines such as this.
1730    See, e.g., SLESCreate().
1731 
1732    Level: developer
1733 
1734    Concepts: matrices^Cholesky factorization
1735 
1736 .seealso: MatLUFactor(), MatCholeskyFactorSymbolic(), MatCholeskyFactorNumeric()
1737           MatGetOrdering()
1738 
1739 @*/
1740 int MatCholeskyFactor(Mat mat,IS perm,PetscReal f)
1741 {
1742   int ierr;
1743 
1744   PetscFunctionBegin;
1745   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1746   PetscValidType(mat);
1747   MatPreallocated(mat);
1748   PetscValidHeaderSpecific(perm,IS_COOKIE);
1749   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1750   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1751   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1752   if (!mat->ops->choleskyfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1753 
1754   ierr = PetscLogEventBegin(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1755   ierr = (*mat->ops->choleskyfactor)(mat,perm,f);CHKERRQ(ierr);
1756   ierr = PetscLogEventEnd(MAT_CholeskyFactor,mat,perm,0,0);CHKERRQ(ierr);
1757   PetscFunctionReturn(0);
1758 }
1759 
1760 #undef __FUNCT__
1761 #define __FUNCT__ "MatCholeskyFactorSymbolic"
1762 /*@
1763    MatCholeskyFactorSymbolic - Performs symbolic Cholesky factorization
1764    of a symmetric matrix.
1765 
1766    Collective on Mat
1767 
1768    Input Parameters:
1769 +  mat - the matrix
1770 .  perm - row and column permutations
1771 -  f - expected fill as ratio of original
1772 
1773    Output Parameter:
1774 .  fact - the factored matrix
1775 
1776    Notes:
1777    See MatLUFactorSymbolic() for the nonsymmetric case.  See also
1778    MatCholeskyFactor() and MatCholeskyFactorNumeric().
1779 
1780    Most users should employ the simplified SLES interface for linear solvers
1781    instead of working directly with matrix algebra routines such as this.
1782    See, e.g., SLESCreate().
1783 
1784    Level: developer
1785 
1786    Concepts: matrices^Cholesky symbolic factorization
1787 
1788 .seealso: MatLUFactorSymbolic(), MatCholeskyFactor(), MatCholeskyFactorNumeric()
1789           MatGetOrdering()
1790 
1791 @*/
1792 int MatCholeskyFactorSymbolic(Mat mat,IS perm,PetscReal f,Mat *fact)
1793 {
1794   int ierr;
1795 
1796   PetscFunctionBegin;
1797   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1798   PetscValidType(mat);
1799   MatPreallocated(mat);
1800   PetscValidPointer(fact);
1801   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"Matrix must be square");
1802   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1803   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
1804   if (!mat->ops->choleskyfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1805 
1806   ierr = PetscLogEventBegin(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1807   ierr = (*mat->ops->choleskyfactorsymbolic)(mat,perm,f,fact);CHKERRQ(ierr);
1808   ierr = PetscLogEventEnd(MAT_CholeskyFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
1809   PetscFunctionReturn(0);
1810 }
1811 
1812 #undef __FUNCT__
1813 #define __FUNCT__ "MatCholeskyFactorNumeric"
1814 /*@
1815    MatCholeskyFactorNumeric - Performs numeric Cholesky factorization
1816    of a symmetric matrix. Call this routine after first calling
1817    MatCholeskyFactorSymbolic().
1818 
1819    Collective on Mat
1820 
1821    Input Parameter:
1822 .  mat - the initial matrix
1823 
1824    Output Parameter:
1825 .  fact - the factored matrix
1826 
1827    Notes:
1828    Most users should employ the simplified SLES interface for linear solvers
1829    instead of working directly with matrix algebra routines such as this.
1830    See, e.g., SLESCreate().
1831 
1832    Level: developer
1833 
1834    Concepts: matrices^Cholesky numeric factorization
1835 
1836 .seealso: MatCholeskyFactorSymbolic(), MatCholeskyFactor(), MatLUFactorNumeric()
1837 @*/
1838 int MatCholeskyFactorNumeric(Mat mat,Mat *fact)
1839 {
1840   int ierr;
1841 
1842   PetscFunctionBegin;
1843   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1844   PetscValidType(mat);
1845   MatPreallocated(mat);
1846   PetscValidPointer(fact);
1847   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
1848   if (!(*fact)->ops->choleskyfactornumeric) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1849   if (mat->M != (*fact)->M || mat->N != (*fact)->N) {
1850     SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat mat,Mat *fact: global dim %d should = %d %d should = %d",
1851             mat->M,(*fact)->M,mat->N,(*fact)->N);
1852   }
1853 
1854   ierr = PetscLogEventBegin(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1855   ierr = (*(*fact)->ops->choleskyfactornumeric)(mat,fact);CHKERRQ(ierr);
1856   ierr = PetscLogEventEnd(MAT_CholeskyFactorNumeric,mat,*fact,0,0);CHKERRQ(ierr);
1857   PetscFunctionReturn(0);
1858 }
1859 
1860 /* ----------------------------------------------------------------*/
1861 #undef __FUNCT__
1862 #define __FUNCT__ "MatSolve"
1863 /*@
1864    MatSolve - Solves A x = b, given a factored matrix.
1865 
1866    Collective on Mat and Vec
1867 
1868    Input Parameters:
1869 +  mat - the factored matrix
1870 -  b - the right-hand-side vector
1871 
1872    Output Parameter:
1873 .  x - the result vector
1874 
1875    Notes:
1876    The vectors b and x cannot be the same.  I.e., one cannot
1877    call MatSolve(A,x,x).
1878 
1879    Notes:
1880    Most users should employ the simplified SLES interface for linear solvers
1881    instead of working directly with matrix algebra routines such as this.
1882    See, e.g., SLESCreate().
1883 
1884    Level: developer
1885 
1886    Concepts: matrices^triangular solves
1887 
1888 .seealso: MatSolveAdd(), MatSolveTranspose(), MatSolveTransposeAdd()
1889 @*/
1890 int MatSolve(Mat mat,Vec b,Vec x)
1891 {
1892   int ierr;
1893 
1894   PetscFunctionBegin;
1895   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1896   PetscValidType(mat);
1897   MatPreallocated(mat);
1898   PetscValidHeaderSpecific(b,VEC_COOKIE);
1899   PetscValidHeaderSpecific(x,VEC_COOKIE);
1900   PetscCheckSameComm(mat,b);
1901   PetscCheckSameComm(mat,x);
1902   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1903   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1904   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1905   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1906   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1907   if (mat->M == 0 && mat->N == 0) PetscFunctionReturn(0);
1908 
1909   if (!mat->ops->solve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1910   ierr = PetscLogEventBegin(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1911   ierr = (*mat->ops->solve)(mat,b,x);CHKERRQ(ierr);
1912   ierr = PetscLogEventEnd(MAT_Solve,mat,b,x,0);CHKERRQ(ierr);
1913   PetscFunctionReturn(0);
1914 }
1915 
1916 #undef __FUNCT__
1917 #define __FUNCT__ "MatForwardSolve"
1918 /* @
1919    MatForwardSolve - Solves L x = b, given a factored matrix, A = LU.
1920 
1921    Collective on Mat and Vec
1922 
1923    Input Parameters:
1924 +  mat - the factored matrix
1925 -  b - the right-hand-side vector
1926 
1927    Output Parameter:
1928 .  x - the result vector
1929 
1930    Notes:
1931    MatSolve() should be used for most applications, as it performs
1932    a forward solve followed by a backward solve.
1933 
1934    The vectors b and x cannot be the same.  I.e., one cannot
1935    call MatForwardSolve(A,x,x).
1936 
1937    Most users should employ the simplified SLES interface for linear solvers
1938    instead of working directly with matrix algebra routines such as this.
1939    See, e.g., SLESCreate().
1940 
1941    Level: developer
1942 
1943    Concepts: matrices^forward solves
1944 
1945 .seealso: MatSolve(), MatBackwardSolve()
1946 @ */
1947 int MatForwardSolve(Mat mat,Vec b,Vec x)
1948 {
1949   int ierr;
1950 
1951   PetscFunctionBegin;
1952   PetscValidHeaderSpecific(mat,MAT_COOKIE);
1953   PetscValidType(mat);
1954   MatPreallocated(mat);
1955   PetscValidHeaderSpecific(b,VEC_COOKIE);
1956   PetscValidHeaderSpecific(x,VEC_COOKIE);
1957   PetscCheckSameComm(mat,b);
1958   PetscCheckSameComm(mat,x);
1959   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
1960   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
1961   if (!mat->ops->forwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
1962   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
1963   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
1964   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
1965 
1966   ierr = PetscLogEventBegin(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1967   ierr = (*mat->ops->forwardsolve)(mat,b,x);CHKERRQ(ierr);
1968   ierr = PetscLogEventEnd(MAT_ForwardSolve,mat,b,x,0);CHKERRQ(ierr);
1969   PetscFunctionReturn(0);
1970 }
1971 
1972 #undef __FUNCT__
1973 #define __FUNCT__ "MatBackwardSolve"
1974 /* @
1975    MatBackwardSolve - Solves U x = b, given a factored matrix, A = LU.
1976 
1977    Collective on Mat and Vec
1978 
1979    Input Parameters:
1980 +  mat - the factored matrix
1981 -  b - the right-hand-side vector
1982 
1983    Output Parameter:
1984 .  x - the result vector
1985 
1986    Notes:
1987    MatSolve() should be used for most applications, as it performs
1988    a forward solve followed by a backward solve.
1989 
1990    The vectors b and x cannot be the same.  I.e., one cannot
1991    call MatBackwardSolve(A,x,x).
1992 
1993    Most users should employ the simplified SLES interface for linear solvers
1994    instead of working directly with matrix algebra routines such as this.
1995    See, e.g., SLESCreate().
1996 
1997    Level: developer
1998 
1999    Concepts: matrices^backward solves
2000 
2001 .seealso: MatSolve(), MatForwardSolve()
2002 @ */
2003 int MatBackwardSolve(Mat mat,Vec b,Vec x)
2004 {
2005   int ierr;
2006 
2007   PetscFunctionBegin;
2008   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2009   PetscValidType(mat);
2010   MatPreallocated(mat);
2011   PetscValidHeaderSpecific(b,VEC_COOKIE);
2012   PetscValidHeaderSpecific(x,VEC_COOKIE);
2013   PetscCheckSameComm(mat,b);
2014   PetscCheckSameComm(mat,x);
2015   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2016   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2017   if (!mat->ops->backwardsolve) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2018   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
2019   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
2020   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
2021 
2022   ierr = PetscLogEventBegin(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
2023   ierr = (*mat->ops->backwardsolve)(mat,b,x);CHKERRQ(ierr);
2024   ierr = PetscLogEventEnd(MAT_BackwardSolve,mat,b,x,0);CHKERRQ(ierr);
2025   PetscFunctionReturn(0);
2026 }
2027 
2028 #undef __FUNCT__
2029 #define __FUNCT__ "MatSolveAdd"
2030 /*@
2031    MatSolveAdd - Computes x = y + inv(A)*b, given a factored matrix.
2032 
2033    Collective on Mat and Vec
2034 
2035    Input Parameters:
2036 +  mat - the factored matrix
2037 .  b - the right-hand-side vector
2038 -  y - the vector to be added to
2039 
2040    Output Parameter:
2041 .  x - the result vector
2042 
2043    Notes:
2044    The vectors b and x cannot be the same.  I.e., one cannot
2045    call MatSolveAdd(A,x,y,x).
2046 
2047    Most users should employ the simplified SLES interface for linear solvers
2048    instead of working directly with matrix algebra routines such as this.
2049    See, e.g., SLESCreate().
2050 
2051    Level: developer
2052 
2053    Concepts: matrices^triangular solves
2054 
2055 .seealso: MatSolve(), MatSolveTranspose(), MatSolveTransposeAdd()
2056 @*/
2057 int MatSolveAdd(Mat mat,Vec b,Vec y,Vec x)
2058 {
2059   PetscScalar one = 1.0;
2060   Vec    tmp;
2061   int    ierr;
2062 
2063   PetscFunctionBegin;
2064   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2065   PetscValidType(mat);
2066   MatPreallocated(mat);
2067   PetscValidHeaderSpecific(y,VEC_COOKIE);
2068   PetscValidHeaderSpecific(b,VEC_COOKIE);
2069   PetscValidHeaderSpecific(x,VEC_COOKIE);
2070   PetscCheckSameComm(mat,b);
2071   PetscCheckSameComm(mat,y);
2072   PetscCheckSameComm(mat,x);
2073   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2074   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2075   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
2076   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
2077   if (mat->M != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->M,y->N);
2078   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
2079   if (x->n != y->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
2080 
2081   ierr = PetscLogEventBegin(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
2082   if (mat->ops->solveadd)  {
2083     ierr = (*mat->ops->solveadd)(mat,b,y,x);CHKERRQ(ierr);
2084   } else {
2085     /* do the solve then the add manually */
2086     if (x != y) {
2087       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
2088       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
2089     } else {
2090       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
2091       PetscLogObjectParent(mat,tmp);
2092       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
2093       ierr = MatSolve(mat,b,x);CHKERRQ(ierr);
2094       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
2095       ierr = VecDestroy(tmp);CHKERRQ(ierr);
2096     }
2097   }
2098   ierr = PetscLogEventEnd(MAT_SolveAdd,mat,b,x,y);CHKERRQ(ierr);
2099   PetscFunctionReturn(0);
2100 }
2101 
2102 #undef __FUNCT__
2103 #define __FUNCT__ "MatSolveTranspose"
2104 /*@
2105    MatSolveTranspose - Solves A' x = b, given a factored matrix.
2106 
2107    Collective on Mat and Vec
2108 
2109    Input Parameters:
2110 +  mat - the factored matrix
2111 -  b - the right-hand-side vector
2112 
2113    Output Parameter:
2114 .  x - the result vector
2115 
2116    Notes:
2117    The vectors b and x cannot be the same.  I.e., one cannot
2118    call MatSolveTranspose(A,x,x).
2119 
2120    Most users should employ the simplified SLES interface for linear solvers
2121    instead of working directly with matrix algebra routines such as this.
2122    See, e.g., SLESCreate().
2123 
2124    Level: developer
2125 
2126    Concepts: matrices^triangular solves
2127 
2128 .seealso: MatSolve(), MatSolveAdd(), MatSolveTransposeAdd()
2129 @*/
2130 int MatSolveTranspose(Mat mat,Vec b,Vec x)
2131 {
2132   int ierr;
2133 
2134   PetscFunctionBegin;
2135   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2136   PetscValidType(mat);
2137   MatPreallocated(mat);
2138   PetscValidHeaderSpecific(b,VEC_COOKIE);
2139   PetscValidHeaderSpecific(x,VEC_COOKIE);
2140   PetscCheckSameComm(mat,b);
2141   PetscCheckSameComm(mat,x);
2142   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2143   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2144   if (!mat->ops->solvetranspose) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s",mat->type_name);
2145   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
2146   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
2147 
2148   ierr = PetscLogEventBegin(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2149   ierr = (*mat->ops->solvetranspose)(mat,b,x);CHKERRQ(ierr);
2150   ierr = PetscLogEventEnd(MAT_SolveTranspose,mat,b,x,0);CHKERRQ(ierr);
2151   PetscFunctionReturn(0);
2152 }
2153 
2154 #undef __FUNCT__
2155 #define __FUNCT__ "MatSolveTransposeAdd"
2156 /*@
2157    MatSolveTransposeAdd - Computes x = y + inv(Transpose(A)) b, given a
2158                       factored matrix.
2159 
2160    Collective on Mat and Vec
2161 
2162    Input Parameters:
2163 +  mat - the factored matrix
2164 .  b - the right-hand-side vector
2165 -  y - the vector to be added to
2166 
2167    Output Parameter:
2168 .  x - the result vector
2169 
2170    Notes:
2171    The vectors b and x cannot be the same.  I.e., one cannot
2172    call MatSolveTransposeAdd(A,x,y,x).
2173 
2174    Most users should employ the simplified SLES interface for linear solvers
2175    instead of working directly with matrix algebra routines such as this.
2176    See, e.g., SLESCreate().
2177 
2178    Level: developer
2179 
2180    Concepts: matrices^triangular solves
2181 
2182 .seealso: MatSolve(), MatSolveAdd(), MatSolveTranspose()
2183 @*/
2184 int MatSolveTransposeAdd(Mat mat,Vec b,Vec y,Vec x)
2185 {
2186   PetscScalar one = 1.0;
2187   int         ierr;
2188   Vec         tmp;
2189 
2190   PetscFunctionBegin;
2191   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2192   PetscValidType(mat);
2193   MatPreallocated(mat);
2194   PetscValidHeaderSpecific(y,VEC_COOKIE);
2195   PetscValidHeaderSpecific(b,VEC_COOKIE);
2196   PetscValidHeaderSpecific(x,VEC_COOKIE);
2197   PetscCheckSameComm(mat,b);
2198   PetscCheckSameComm(mat,y);
2199   PetscCheckSameComm(mat,x);
2200   if (x == b) SETERRQ(PETSC_ERR_ARG_IDN,"x and b must be different vectors");
2201   if (!mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Unfactored matrix");
2202   if (mat->M != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->M,x->N);
2203   if (mat->N != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->N,b->N);
2204   if (mat->N != y->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec y: global dim %d %d",mat->N,y->N);
2205   if (x->n != y->n)   SETERRQ2(PETSC_ERR_ARG_SIZ,"Vec x,Vec y: local dim %d %d",x->n,y->n);
2206 
2207   ierr = PetscLogEventBegin(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2208   if (mat->ops->solvetransposeadd) {
2209     ierr = (*mat->ops->solvetransposeadd)(mat,b,y,x);CHKERRQ(ierr);
2210   } else {
2211     /* do the solve then the add manually */
2212     if (x != y) {
2213       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2214       ierr = VecAXPY(&one,y,x);CHKERRQ(ierr);
2215     } else {
2216       ierr = VecDuplicate(x,&tmp);CHKERRQ(ierr);
2217       PetscLogObjectParent(mat,tmp);
2218       ierr = VecCopy(x,tmp);CHKERRQ(ierr);
2219       ierr = MatSolveTranspose(mat,b,x);CHKERRQ(ierr);
2220       ierr = VecAXPY(&one,tmp,x);CHKERRQ(ierr);
2221       ierr = VecDestroy(tmp);CHKERRQ(ierr);
2222     }
2223   }
2224   ierr = PetscLogEventEnd(MAT_SolveTransposeAdd,mat,b,x,y);CHKERRQ(ierr);
2225   PetscFunctionReturn(0);
2226 }
2227 /* ----------------------------------------------------------------*/
2228 
2229 #undef __FUNCT__
2230 #define __FUNCT__ "MatRelax"
2231 /*@
2232    MatRelax - Computes one relaxation sweep.
2233 
2234    Collective on Mat and Vec
2235 
2236    Input Parameters:
2237 +  mat - the matrix
2238 .  b - the right hand side
2239 .  omega - the relaxation factor
2240 .  flag - flag indicating the type of SOR (see below)
2241 .  shift -  diagonal shift
2242 -  its - the number of iterations
2243 -  lits - the number of local iterations
2244 
2245    Output Parameters:
2246 .  x - the solution (can contain an initial guess)
2247 
2248    SOR Flags:
2249 .     SOR_FORWARD_SWEEP - forward SOR
2250 .     SOR_BACKWARD_SWEEP - backward SOR
2251 .     SOR_SYMMETRIC_SWEEP - SSOR (symmetric SOR)
2252 .     SOR_LOCAL_FORWARD_SWEEP - local forward SOR
2253 .     SOR_LOCAL_BACKWARD_SWEEP - local forward SOR
2254 .     SOR_LOCAL_SYMMETRIC_SWEEP - local SSOR
2255 .     SOR_APPLY_UPPER, SOR_APPLY_LOWER - applies
2256          upper/lower triangular part of matrix to
2257          vector (with omega)
2258 .     SOR_ZERO_INITIAL_GUESS - zero initial guess
2259 
2260    Notes:
2261    SOR_LOCAL_FORWARD_SWEEP, SOR_LOCAL_BACKWARD_SWEEP, and
2262    SOR_LOCAL_SYMMETRIC_SWEEP perform seperate independent smoothings
2263    on each processor.
2264 
2265    Application programmers will not generally use MatRelax() directly,
2266    but instead will employ the SLES/PC interface.
2267 
2268    Notes for Advanced Users:
2269    The flags are implemented as bitwise inclusive or operations.
2270    For example, use (SOR_ZERO_INITIAL_GUESS | SOR_SYMMETRIC_SWEEP)
2271    to specify a zero initial guess for SSOR.
2272 
2273    Most users should employ the simplified SLES interface for linear solvers
2274    instead of working directly with matrix algebra routines such as this.
2275    See, e.g., SLESCreate().
2276 
2277    Level: developer
2278 
2279    Concepts: matrices^relaxation
2280    Concepts: matrices^SOR
2281    Concepts: matrices^Gauss-Seidel
2282 
2283 @*/
2284 int MatRelax(Mat mat,Vec b,PetscReal omega,MatSORType flag,PetscReal shift,int its,int lits,Vec x)
2285 {
2286   int ierr;
2287 
2288   PetscFunctionBegin;
2289   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2290   PetscValidType(mat);
2291   MatPreallocated(mat);
2292   PetscValidHeaderSpecific(b,VEC_COOKIE);
2293   PetscValidHeaderSpecific(x,VEC_COOKIE);
2294   PetscCheckSameComm(mat,b);
2295   PetscCheckSameComm(mat,x);
2296   if (!mat->ops->relax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2297   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2298   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2299   if (mat->N != x->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec x: global dim %d %d",mat->N,x->N);
2300   if (mat->M != b->N) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: global dim %d %d",mat->M,b->N);
2301   if (mat->m != b->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"Mat mat,Vec b: local dim %d %d",mat->m,b->n);
2302 
2303   ierr = PetscLogEventBegin(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2304   ierr =(*mat->ops->relax)(mat,b,omega,flag,shift,its,lits,x);CHKERRQ(ierr);
2305   ierr = PetscLogEventEnd(MAT_Relax,mat,b,x,0);CHKERRQ(ierr);
2306   PetscFunctionReturn(0);
2307 }
2308 
2309 #undef __FUNCT__
2310 #define __FUNCT__ "MatCopy_Basic"
2311 /*
2312       Default matrix copy routine.
2313 */
2314 int MatCopy_Basic(Mat A,Mat B,MatStructure str)
2315 {
2316   int         ierr,i,rstart,rend,nz,*cwork;
2317   PetscScalar *vwork;
2318 
2319   PetscFunctionBegin;
2320   ierr = MatZeroEntries(B);CHKERRQ(ierr);
2321   ierr = MatGetOwnershipRange(A,&rstart,&rend);CHKERRQ(ierr);
2322   for (i=rstart; i<rend; i++) {
2323     ierr = MatGetRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2324     ierr = MatSetValues(B,1,&i,nz,cwork,vwork,INSERT_VALUES);CHKERRQ(ierr);
2325     ierr = MatRestoreRow(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
2326   }
2327   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2328   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2329   PetscFunctionReturn(0);
2330 }
2331 
2332 #undef __FUNCT__
2333 #define __FUNCT__ "MatCopy"
2334 /*@C
2335    MatCopy - Copys a matrix to another matrix.
2336 
2337    Collective on Mat
2338 
2339    Input Parameters:
2340 +  A - the matrix
2341 -  str - SAME_NONZERO_PATTERN or DIFFERENT_NONZERO_PATTERN
2342 
2343    Output Parameter:
2344 .  B - where the copy is put
2345 
2346    Notes:
2347    If you use SAME_NONZERO_PATTERN then the zero matrices had better have the
2348    same nonzero pattern or the routine will crash.
2349 
2350    MatCopy() copies the matrix entries of a matrix to another existing
2351    matrix (after first zeroing the second matrix).  A related routine is
2352    MatConvert(), which first creates a new matrix and then copies the data.
2353 
2354    Level: intermediate
2355 
2356    Concepts: matrices^copying
2357 
2358 .seealso: MatConvert()
2359 @*/
2360 int MatCopy(Mat A,Mat B,MatStructure str)
2361 {
2362   int ierr;
2363 
2364   PetscFunctionBegin;
2365   PetscValidHeaderSpecific(A,MAT_COOKIE);
2366   PetscValidHeaderSpecific(B,MAT_COOKIE);
2367   PetscValidType(A);
2368   MatPreallocated(A);
2369   PetscValidType(B);
2370   MatPreallocated(B);
2371   PetscCheckSameComm(A,B);
2372   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2373   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2374   if (A->M != B->M || A->N != B->N) SETERRQ4(PETSC_ERR_ARG_SIZ,"Mat A,Mat B: global dim %d %d",A->M,B->M,
2375                                              A->N,B->N);
2376 
2377   ierr = PetscLogEventBegin(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2378   if (A->ops->copy) {
2379     ierr = (*A->ops->copy)(A,B,str);CHKERRQ(ierr);
2380   } else { /* generic conversion */
2381     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2382   }
2383   ierr = PetscLogEventEnd(MAT_Copy,A,B,0,0);CHKERRQ(ierr);
2384   PetscFunctionReturn(0);
2385 }
2386 
2387 #include "petscsys.h"
2388 PetscTruth MatConvertRegisterAllCalled = PETSC_FALSE;
2389 PetscFList MatConvertList              = 0;
2390 
2391 #undef __FUNCT__
2392 #define __FUNCT__ "MatConvertRegister"
2393 /*@C
2394     MatConvertRegister - Allows one to register a routine that reads matrices
2395         from a binary file for a particular matrix type.
2396 
2397   Not Collective
2398 
2399   Input Parameters:
2400 +   type - the type of matrix (defined in include/petscmat.h), for example, MATSEQAIJ.
2401 -   Converter - the function that reads the matrix from the binary file.
2402 
2403   Level: developer
2404 
2405 .seealso: MatConvertRegisterAll(), MatConvert()
2406 
2407 @*/
2408 int MatConvertRegister(char *sname,char *path,char *name,int (*function)(Mat,MatType,Mat*))
2409 {
2410   int  ierr;
2411   char fullname[256];
2412 
2413   PetscFunctionBegin;
2414   ierr = PetscFListConcat(path,name,fullname);CHKERRQ(ierr);
2415   ierr = PetscFListAdd(&MatConvertList,sname,fullname,(void (*)(void))function);CHKERRQ(ierr);
2416   PetscFunctionReturn(0);
2417 }
2418 
2419 #undef __FUNCT__
2420 #define __FUNCT__ "MatConvert"
2421 /*@C
2422    MatConvert - Converts a matrix to another matrix, either of the same
2423    or different type.
2424 
2425    Collective on Mat
2426 
2427    Input Parameters:
2428 +  mat - the matrix
2429 -  newtype - new matrix type.  Use MATSAME to create a new matrix of the
2430    same type as the original matrix.
2431 
2432    Output Parameter:
2433 .  M - pointer to place new matrix
2434 
2435    Notes:
2436    MatConvert() first creates a new matrix and then copies the data from
2437    the first matrix.  A related routine is MatCopy(), which copies the matrix
2438    entries of one matrix to another already existing matrix context.
2439 
2440    Level: intermediate
2441 
2442    Concepts: matrices^converting between storage formats
2443 
2444 .seealso: MatCopy(), MatDuplicate()
2445 @*/
2446 int MatConvert(Mat mat,MatType newtype,Mat *M)
2447 {
2448   int        ierr;
2449   PetscTruth sametype,issame,flg;
2450   char       convname[256],mtype[256];
2451 
2452   PetscFunctionBegin;
2453   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2454   PetscValidType(mat);
2455   MatPreallocated(mat);
2456   PetscValidPointer(M);
2457   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2458   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2459 
2460   ierr = PetscOptionsGetString(PETSC_NULL,"-matconvert_type",mtype,256,&flg);CHKERRQ(ierr);
2461   if (flg) {
2462     newtype = mtype;
2463   }
2464   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2465 
2466   ierr = PetscTypeCompare((PetscObject)mat,newtype,&sametype);CHKERRQ(ierr);
2467   ierr = PetscStrcmp(newtype,"same",&issame);CHKERRQ(ierr);
2468   if ((sametype || issame) && mat->ops->duplicate) {
2469     ierr = (*mat->ops->duplicate)(mat,MAT_COPY_VALUES,M);CHKERRQ(ierr);
2470   } else {
2471     int (*conv)(Mat,MatType,Mat*);
2472     if (!MatConvertRegisterAllCalled) {
2473       ierr = MatConvertRegisterAll(PETSC_NULL);CHKERRQ(ierr);
2474     }
2475     ierr = PetscFListFind(mat->comm,MatConvertList,newtype,(void(**)(void))&conv);CHKERRQ(ierr);
2476     if (conv) {
2477       ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2478     } else {
2479       ierr = PetscStrcpy(convname,"MatConvert_");CHKERRQ(ierr);
2480       ierr = PetscStrcat(convname,mat->type_name);CHKERRQ(ierr);
2481       ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
2482       ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
2483       ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
2484       ierr = PetscObjectQueryFunction((PetscObject)mat,convname,(void (**)(void))&conv);CHKERRQ(ierr);
2485       if (conv) {
2486         ierr = (*conv)(mat,newtype,M);CHKERRQ(ierr);
2487       } else {
2488         if (mat->ops->convert) {
2489           ierr = (*mat->ops->convert)(mat,newtype,M);CHKERRQ(ierr);
2490         } else {
2491           ierr = MatConvert_Basic(mat,newtype,M);CHKERRQ(ierr);
2492         }
2493       }
2494     }
2495   }
2496   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2497   PetscFunctionReturn(0);
2498 }
2499 
2500 
2501 #undef __FUNCT__
2502 #define __FUNCT__ "MatDuplicate"
2503 /*@C
2504    MatDuplicate - Duplicates a matrix including the non-zero structure.
2505 
2506    Collective on Mat
2507 
2508    Input Parameters:
2509 +  mat - the matrix
2510 -  op - either MAT_DO_NOT_COPY_VALUES or MAT_COPY_VALUES, cause it to copy nonzero
2511         values as well or not
2512 
2513    Output Parameter:
2514 .  M - pointer to place new matrix
2515 
2516    Level: intermediate
2517 
2518    Concepts: matrices^duplicating
2519 
2520 .seealso: MatCopy(), MatConvert()
2521 @*/
2522 int MatDuplicate(Mat mat,MatDuplicateOption op,Mat *M)
2523 {
2524   int ierr;
2525 
2526   PetscFunctionBegin;
2527   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2528   PetscValidType(mat);
2529   MatPreallocated(mat);
2530   PetscValidPointer(M);
2531   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2532   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2533 
2534   *M  = 0;
2535   ierr = PetscLogEventBegin(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2536   if (!mat->ops->duplicate) {
2537     SETERRQ(PETSC_ERR_SUP,"Not written for this matrix type");
2538   }
2539   ierr = (*mat->ops->duplicate)(mat,op,M);CHKERRQ(ierr);
2540   ierr = PetscLogEventEnd(MAT_Convert,mat,0,0,0);CHKERRQ(ierr);
2541   PetscFunctionReturn(0);
2542 }
2543 
2544 #undef __FUNCT__
2545 #define __FUNCT__ "MatGetDiagonal"
2546 /*@
2547    MatGetDiagonal - Gets the diagonal of a matrix.
2548 
2549    Collective on Mat and Vec
2550 
2551    Input Parameters:
2552 +  mat - the matrix
2553 -  v - the vector for storing the diagonal
2554 
2555    Output Parameter:
2556 .  v - the diagonal of the matrix
2557 
2558    Notes:
2559    For the SeqAIJ matrix format, this routine may also be called
2560    on a LU factored matrix; in that case it routines the reciprocal of
2561    the diagonal entries in U. It returns the entries permuted by the
2562    row and column permutation used during the symbolic factorization.
2563 
2564    Level: intermediate
2565 
2566    Concepts: matrices^accessing diagonals
2567 
2568 .seealso: MatGetRow(), MatGetSubmatrices(), MatGetSubmatrix(), MatGetRowMax()
2569 @*/
2570 int MatGetDiagonal(Mat mat,Vec v)
2571 {
2572   int ierr;
2573 
2574   PetscFunctionBegin;
2575   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2576   PetscValidType(mat);
2577   MatPreallocated(mat);
2578   PetscValidHeaderSpecific(v,VEC_COOKIE);
2579   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2580   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2581   if (!mat->ops->getdiagonal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2582 
2583   ierr = (*mat->ops->getdiagonal)(mat,v);CHKERRQ(ierr);
2584   PetscFunctionReturn(0);
2585 }
2586 
2587 #undef __FUNCT__
2588 #define __FUNCT__ "MatGetRowMax"
2589 /*@
2590    MatGetRowMax - Gets the maximum value (in absolute value) of each
2591         row of the matrix
2592 
2593    Collective on Mat and Vec
2594 
2595    Input Parameters:
2596 .  mat - the matrix
2597 
2598    Output Parameter:
2599 .  v - the vector for storing the maximums
2600 
2601    Level: intermediate
2602 
2603    Concepts: matrices^getting row maximums
2604 
2605 .seealso: MatGetDiagonal(), MatGetSubmatrices(), MatGetSubmatrix()
2606 @*/
2607 int MatGetRowMax(Mat mat,Vec v)
2608 {
2609   int ierr;
2610 
2611   PetscFunctionBegin;
2612   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2613   PetscValidType(mat);
2614   MatPreallocated(mat);
2615   PetscValidHeaderSpecific(v,VEC_COOKIE);
2616   /* PetscCheckSameComm(mat,v); Could be MPI vector but Seq matrix cause of two submatrix storage */
2617   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2618   if (!mat->ops->getrowmax) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2619 
2620   ierr = (*mat->ops->getrowmax)(mat,v);CHKERRQ(ierr);
2621   PetscFunctionReturn(0);
2622 }
2623 
2624 #undef __FUNCT__
2625 #define __FUNCT__ "MatTranspose"
2626 /*@C
2627    MatTranspose - Computes an in-place or out-of-place transpose of a matrix.
2628 
2629    Collective on Mat
2630 
2631    Input Parameter:
2632 .  mat - the matrix to transpose
2633 
2634    Output Parameters:
2635 .  B - the transpose (or pass in PETSC_NULL for an in-place transpose)
2636 
2637    Level: intermediate
2638 
2639    Concepts: matrices^transposing
2640 
2641 .seealso: MatMultTranspose(), MatMultTransposeAdd()
2642 @*/
2643 int MatTranspose(Mat mat,Mat *B)
2644 {
2645   int ierr;
2646 
2647   PetscFunctionBegin;
2648   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2649   PetscValidType(mat);
2650   MatPreallocated(mat);
2651   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2652   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2653   if (!mat->ops->transpose) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2654   ierr = (*mat->ops->transpose)(mat,B);CHKERRQ(ierr);
2655   PetscFunctionReturn(0);
2656 }
2657 
2658 #undef __FUNCT__
2659 #define __FUNCT__ "MatPermute"
2660 /*@C
2661    MatPermute - Creates a new matrix with rows and columns permuted from the
2662    original.
2663 
2664    Collective on Mat
2665 
2666    Input Parameters:
2667 +  mat - the matrix to permute
2668 .  row - row permutation, each processor supplies only the permutation for its rows
2669 -  col - column permutation, each processor needs the entire column permutation, that is
2670          this is the same size as the total number of columns in the matrix
2671 
2672    Output Parameters:
2673 .  B - the permuted matrix
2674 
2675    Level: advanced
2676 
2677    Concepts: matrices^permuting
2678 
2679 .seealso: MatGetOrdering()
2680 @*/
2681 int MatPermute(Mat mat,IS row,IS col,Mat *B)
2682 {
2683   int ierr;
2684 
2685   PetscFunctionBegin;
2686   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2687   PetscValidType(mat);
2688   MatPreallocated(mat);
2689   PetscValidHeaderSpecific(row,IS_COOKIE);
2690   PetscValidHeaderSpecific(col,IS_COOKIE);
2691   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2692   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2693   if (!mat->ops->permute) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2694   ierr = (*mat->ops->permute)(mat,row,col,B);CHKERRQ(ierr);
2695   PetscFunctionReturn(0);
2696 }
2697 
2698 #undef __FUNCT__
2699 #define __FUNCT__ "MatPermuteSparsify"
2700 /*@C
2701   MatPermuteSparsify - Creates a new matrix with rows and columns permuted from the
2702   original and sparsified to the prescribed tolerance.
2703 
2704   Collective on Mat
2705 
2706   Input Parameters:
2707 + A    - The matrix to permute
2708 . band - The half-bandwidth of the sparsified matrix, or PETSC_DECIDE
2709 . frac - The half-bandwidth as a fraction of the total size, or 0.0
2710 . tol  - The drop tolerance
2711 . rowp - The row permutation
2712 - colp - The column permutation
2713 
2714   Output Parameter:
2715 . B    - The permuted, sparsified matrix
2716 
2717   Level: advanced
2718 
2719   Note:
2720   The default behavior (band = PETSC_DECIDE and frac = 0.0) is to
2721   restrict the half-bandwidth of the resulting matrix to 5% of the
2722   total matrix size.
2723 
2724 .keywords: matrix, permute, sparsify
2725 
2726 .seealso: MatGetOrdering(), MatPermute()
2727 @*/
2728 int MatPermuteSparsify(Mat A, int band, double frac, double tol, IS rowp, IS colp, Mat *B)
2729 {
2730   IS           irowp, icolp;
2731   int         *rows, *cols;
2732   int          M, N, locRowStart, locRowEnd;
2733   int          nz, newNz;
2734   int         *cwork, *cnew;
2735   PetscScalar *vwork, *vnew;
2736   int          bw, size;
2737   int          row, locRow, newRow, col, newCol;
2738   int          ierr;
2739 
2740   PetscFunctionBegin;
2741   PetscValidHeaderSpecific(A,    MAT_COOKIE);
2742   PetscValidHeaderSpecific(rowp, IS_COOKIE);
2743   PetscValidHeaderSpecific(colp, IS_COOKIE);
2744   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
2745   if (A->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2746   if (!A->ops->permutesparsify) {
2747     ierr = MatGetSize(A, &M, &N);                                                                         CHKERRQ(ierr);
2748     ierr = MatGetOwnershipRange(A, &locRowStart, &locRowEnd);                                             CHKERRQ(ierr);
2749     ierr = ISGetSize(rowp, &size);                                                                        CHKERRQ(ierr);
2750     if (size != M) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for row permutation, should be %d", size, M);
2751     ierr = ISGetSize(colp, &size);                                                                        CHKERRQ(ierr);
2752     if (size != N) SETERRQ2(PETSC_ERR_ARG_WRONG, "Wrong size %d for column permutation, should be %d", size, N);
2753     ierr = ISInvertPermutation(rowp, 0, &irowp);                                                          CHKERRQ(ierr);
2754     ierr = ISGetIndices(irowp, &rows);                                                                    CHKERRQ(ierr);
2755     ierr = ISInvertPermutation(colp, 0, &icolp);                                                          CHKERRQ(ierr);
2756     ierr = ISGetIndices(icolp, &cols);                                                                    CHKERRQ(ierr);
2757     ierr = PetscMalloc(N * sizeof(int),         &cnew);                                                   CHKERRQ(ierr);
2758     ierr = PetscMalloc(N * sizeof(PetscScalar), &vnew);                                                   CHKERRQ(ierr);
2759 
2760     /* Setup bandwidth to include */
2761     if (band == PETSC_DECIDE) {
2762       if (frac <= 0.0)
2763         bw = (int) (M * 0.05);
2764       else
2765         bw = (int) (M * frac);
2766     } else {
2767       if (band <= 0) SETERRQ(PETSC_ERR_ARG_WRONG, "Bandwidth must be a positive integer");
2768       bw = band;
2769     }
2770 
2771     /* Put values into new matrix */
2772     ierr = MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, B);                                                    CHKERRQ(ierr);
2773     for(row = locRowStart, locRow = 0; row < locRowEnd; row++, locRow++) {
2774       ierr = MatGetRow(A, row, &nz, &cwork, &vwork);                                                      CHKERRQ(ierr);
2775       newRow   = rows[locRow]+locRowStart;
2776       for(col = 0, newNz = 0; col < nz; col++) {
2777         newCol = cols[cwork[col]];
2778         if ((newCol >= newRow - bw) && (newCol < newRow + bw) && (PetscAbsScalar(vwork[col]) >= tol)) {
2779           cnew[newNz] = newCol;
2780           vnew[newNz] = vwork[col];
2781           newNz++;
2782         }
2783       }
2784       ierr = MatSetValues(*B, 1, &newRow, newNz, cnew, vnew, INSERT_VALUES);                              CHKERRQ(ierr);
2785       ierr = MatRestoreRow(A, row, &nz, &cwork, &vwork);                                                  CHKERRQ(ierr);
2786     }
2787     ierr = PetscFree(cnew);                                                                               CHKERRQ(ierr);
2788     ierr = PetscFree(vnew);                                                                               CHKERRQ(ierr);
2789     ierr = MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);                                                      CHKERRQ(ierr);
2790     ierr = MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);                                                        CHKERRQ(ierr);
2791     ierr = ISRestoreIndices(irowp, &rows);                                                                CHKERRQ(ierr);
2792     ierr = ISRestoreIndices(icolp, &cols);                                                                CHKERRQ(ierr);
2793     ierr = ISDestroy(irowp);                                                                              CHKERRQ(ierr);
2794     ierr = ISDestroy(icolp);                                                                              CHKERRQ(ierr);
2795   } else {
2796     ierr = (*A->ops->permutesparsify)(A, band, frac, tol, rowp, colp, B);                                 CHKERRQ(ierr);
2797   }
2798   PetscFunctionReturn(0);
2799 }
2800 
2801 #undef __FUNCT__
2802 #define __FUNCT__ "MatEqual"
2803 /*@
2804    MatEqual - Compares two matrices.
2805 
2806    Collective on Mat
2807 
2808    Input Parameters:
2809 +  A - the first matrix
2810 -  B - the second matrix
2811 
2812    Output Parameter:
2813 .  flg - PETSC_TRUE if the matrices are equal; PETSC_FALSE otherwise.
2814 
2815    Level: intermediate
2816 
2817    Concepts: matrices^equality between
2818 @*/
2819 int MatEqual(Mat A,Mat B,PetscTruth *flg)
2820 {
2821   int ierr;
2822 
2823   PetscFunctionBegin;
2824   PetscValidHeaderSpecific(A,MAT_COOKIE);
2825   PetscValidHeaderSpecific(B,MAT_COOKIE);
2826   PetscValidType(A);
2827   MatPreallocated(A);
2828   PetscValidType(B);
2829   MatPreallocated(B);
2830   PetscValidIntPointer(flg);
2831   PetscCheckSameComm(A,B);
2832   if (!A->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2833   if (!B->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2834   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);
2835   if (!A->ops->equal) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",A->type_name);
2836   ierr = (*A->ops->equal)(A,B,flg);CHKERRQ(ierr);
2837   PetscFunctionReturn(0);
2838 }
2839 
2840 #undef __FUNCT__
2841 #define __FUNCT__ "MatDiagonalScale"
2842 /*@
2843    MatDiagonalScale - Scales a matrix on the left and right by diagonal
2844    matrices that are stored as vectors.  Either of the two scaling
2845    matrices can be PETSC_NULL.
2846 
2847    Collective on Mat
2848 
2849    Input Parameters:
2850 +  mat - the matrix to be scaled
2851 .  l - the left scaling vector (or PETSC_NULL)
2852 -  r - the right scaling vector (or PETSC_NULL)
2853 
2854    Notes:
2855    MatDiagonalScale() computes A = LAR, where
2856    L = a diagonal matrix, R = a diagonal matrix
2857 
2858    Level: intermediate
2859 
2860    Concepts: matrices^diagonal scaling
2861    Concepts: diagonal scaling of matrices
2862 
2863 .seealso: MatScale()
2864 @*/
2865 int MatDiagonalScale(Mat mat,Vec l,Vec r)
2866 {
2867   int ierr;
2868 
2869   PetscFunctionBegin;
2870   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2871   PetscValidType(mat);
2872   MatPreallocated(mat);
2873   if (!mat->ops->diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2874   if (l) {PetscValidHeaderSpecific(l,VEC_COOKIE);PetscCheckSameComm(mat,l);}
2875   if (r) {PetscValidHeaderSpecific(r,VEC_COOKIE);PetscCheckSameComm(mat,r);}
2876   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2877   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2878 
2879   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2880   ierr = (*mat->ops->diagonalscale)(mat,l,r);CHKERRQ(ierr);
2881   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2882   PetscFunctionReturn(0);
2883 }
2884 
2885 #undef __FUNCT__
2886 #define __FUNCT__ "MatScale"
2887 /*@
2888     MatScale - Scales all elements of a matrix by a given number.
2889 
2890     Collective on Mat
2891 
2892     Input Parameters:
2893 +   mat - the matrix to be scaled
2894 -   a  - the scaling value
2895 
2896     Output Parameter:
2897 .   mat - the scaled matrix
2898 
2899     Level: intermediate
2900 
2901     Concepts: matrices^scaling all entries
2902 
2903 .seealso: MatDiagonalScale()
2904 @*/
2905 int MatScale(PetscScalar *a,Mat mat)
2906 {
2907   int ierr;
2908 
2909   PetscFunctionBegin;
2910   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2911   PetscValidType(mat);
2912   MatPreallocated(mat);
2913   PetscValidScalarPointer(a);
2914   if (!mat->ops->scale) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2915   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2916   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2917 
2918   ierr = PetscLogEventBegin(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2919   ierr = (*mat->ops->scale)(a,mat);CHKERRQ(ierr);
2920   ierr = PetscLogEventEnd(MAT_Scale,mat,0,0,0);CHKERRQ(ierr);
2921   PetscFunctionReturn(0);
2922 }
2923 
2924 #undef __FUNCT__
2925 #define __FUNCT__ "MatNorm"
2926 /*@
2927    MatNorm - Calculates various norms of a matrix.
2928 
2929    Collective on Mat
2930 
2931    Input Parameters:
2932 +  mat - the matrix
2933 -  type - the type of norm, NORM_1, NORM_2, NORM_FROBENIUS, NORM_INFINITY
2934 
2935    Output Parameters:
2936 .  norm - the resulting norm
2937 
2938    Level: intermediate
2939 
2940    Concepts: matrices^norm
2941    Concepts: norm^of matrix
2942 @*/
2943 int MatNorm(Mat mat,NormType type,PetscReal *norm)
2944 {
2945   int ierr;
2946 
2947   PetscFunctionBegin;
2948   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2949   PetscValidType(mat);
2950   MatPreallocated(mat);
2951   PetscValidScalarPointer(norm);
2952 
2953   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
2954   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2955   if (!mat->ops->norm) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
2956   ierr = (*mat->ops->norm)(mat,type,norm);CHKERRQ(ierr);
2957   PetscFunctionReturn(0);
2958 }
2959 
2960 /*
2961      This variable is used to prevent counting of MatAssemblyBegin() that
2962    are called from within a MatAssemblyEnd().
2963 */
2964 static int MatAssemblyEnd_InUse = 0;
2965 #undef __FUNCT__
2966 #define __FUNCT__ "MatAssemblyBegin"
2967 /*@
2968    MatAssemblyBegin - Begins assembling the matrix.  This routine should
2969    be called after completing all calls to MatSetValues().
2970 
2971    Collective on Mat
2972 
2973    Input Parameters:
2974 +  mat - the matrix
2975 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
2976 
2977    Notes:
2978    MatSetValues() generally caches the values.  The matrix is ready to
2979    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
2980    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
2981    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
2982    using the matrix.
2983 
2984    Level: beginner
2985 
2986    Concepts: matrices^assembling
2987 
2988 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssembled()
2989 @*/
2990 int MatAssemblyBegin(Mat mat,MatAssemblyType type)
2991 {
2992   int ierr;
2993 
2994   PetscFunctionBegin;
2995   PetscValidHeaderSpecific(mat,MAT_COOKIE);
2996   PetscValidType(mat);
2997   MatPreallocated(mat);
2998   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix.\nDid you forget to call MatSetUnfactored()?");
2999   if (mat->assembled) {
3000     mat->was_assembled = PETSC_TRUE;
3001     mat->assembled     = PETSC_FALSE;
3002   }
3003   if (!MatAssemblyEnd_InUse) {
3004     ierr = PetscLogEventBegin(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
3005     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
3006     ierr = PetscLogEventEnd(MAT_AssemblyBegin,mat,0,0,0);CHKERRQ(ierr);
3007   } else {
3008     if (mat->ops->assemblybegin){ierr = (*mat->ops->assemblybegin)(mat,type);CHKERRQ(ierr);}
3009   }
3010   PetscFunctionReturn(0);
3011 }
3012 
3013 #undef __FUNCT__
3014 #define __FUNCT__ "MatAssembed"
3015 /*@
3016    MatAssembled - Indicates if a matrix has been assembled and is ready for
3017      use; for example, in matrix-vector product.
3018 
3019    Collective on Mat
3020 
3021    Input Parameter:
3022 .  mat - the matrix
3023 
3024    Output Parameter:
3025 .  assembled - PETSC_TRUE or PETSC_FALSE
3026 
3027    Level: advanced
3028 
3029    Concepts: matrices^assembled?
3030 
3031 .seealso: MatAssemblyEnd(), MatSetValues(), MatAssemblyBegin()
3032 @*/
3033 int MatAssembled(Mat mat,PetscTruth *assembled)
3034 {
3035   PetscFunctionBegin;
3036   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3037   PetscValidType(mat);
3038   MatPreallocated(mat);
3039   *assembled = mat->assembled;
3040   PetscFunctionReturn(0);
3041 }
3042 
3043 #undef __FUNCT__
3044 #define __FUNCT__ "MatView_Private"
3045 /*
3046     Processes command line options to determine if/how a matrix
3047   is to be viewed. Called by MatAssemblyEnd() and MatLoad().
3048 */
3049 int MatView_Private(Mat mat)
3050 {
3051   int        ierr;
3052   PetscTruth flg;
3053 
3054   PetscFunctionBegin;
3055   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info",&flg);CHKERRQ(ierr);
3056   if (flg) {
3057     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO);CHKERRQ(ierr);
3058     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3059     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3060   }
3061   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_info_detailed",&flg);CHKERRQ(ierr);
3062   if (flg) {
3063     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_INFO_LONG);CHKERRQ(ierr);
3064     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3065     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3066   }
3067   ierr = PetscOptionsHasName(mat->prefix,"-mat_view",&flg);CHKERRQ(ierr);
3068   if (flg) {
3069     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3070   }
3071   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_matlab",&flg);CHKERRQ(ierr);
3072   if (flg) {
3073     ierr = PetscViewerPushFormat(PETSC_VIEWER_STDOUT_(mat->comm),PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr);
3074     ierr = MatView(mat,PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3075     ierr = PetscViewerPopFormat(PETSC_VIEWER_STDOUT_(mat->comm));CHKERRQ(ierr);
3076   }
3077   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_draw",&flg);CHKERRQ(ierr);
3078   if (flg) {
3079     ierr = PetscOptionsHasName(mat->prefix,"-mat_view_contour",&flg);CHKERRQ(ierr);
3080     if (flg) {
3081       PetscViewerPushFormat(PETSC_VIEWER_DRAW_(mat->comm),PETSC_VIEWER_DRAW_CONTOUR);CHKERRQ(ierr);
3082     }
3083     ierr = MatView(mat,PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3084     ierr = PetscViewerFlush(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3085     if (flg) {
3086       PetscViewerPopFormat(PETSC_VIEWER_DRAW_(mat->comm));CHKERRQ(ierr);
3087     }
3088   }
3089   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_socket",&flg);CHKERRQ(ierr);
3090   if (flg) {
3091     ierr = MatView(mat,PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
3092     ierr = PetscViewerFlush(PETSC_VIEWER_SOCKET_(mat->comm));CHKERRQ(ierr);
3093   }
3094   ierr = PetscOptionsHasName(mat->prefix,"-mat_view_binary",&flg);CHKERRQ(ierr);
3095   if (flg) {
3096     ierr = MatView(mat,PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
3097     ierr = PetscViewerFlush(PETSC_VIEWER_BINARY_(mat->comm));CHKERRQ(ierr);
3098   }
3099   PetscFunctionReturn(0);
3100 }
3101 
3102 #undef __FUNCT__
3103 #define __FUNCT__ "MatAssemblyEnd"
3104 /*@
3105    MatAssemblyEnd - Completes assembling the matrix.  This routine should
3106    be called after MatAssemblyBegin().
3107 
3108    Collective on Mat
3109 
3110    Input Parameters:
3111 +  mat - the matrix
3112 -  type - type of assembly, either MAT_FLUSH_ASSEMBLY or MAT_FINAL_ASSEMBLY
3113 
3114    Options Database Keys:
3115 +  -mat_view_info - Prints info on matrix at conclusion of MatEndAssembly()
3116 .  -mat_view_info_detailed - Prints more detailed info
3117 .  -mat_view - Prints matrix in ASCII format
3118 .  -mat_view_matlab - Prints matrix in Matlab format
3119 .  -mat_view_draw - PetscDraws nonzero structure of matrix, using MatView() and PetscDrawOpenX().
3120 .  -display <name> - Sets display name (default is host)
3121 -  -draw_pause <sec> - Sets number of seconds to pause after display
3122 
3123    Notes:
3124    MatSetValues() generally caches the values.  The matrix is ready to
3125    use only after MatAssemblyBegin() and MatAssemblyEnd() have been called.
3126    Use MAT_FLUSH_ASSEMBLY when switching between ADD_VALUES and INSERT_VALUES
3127    in MatSetValues(); use MAT_FINAL_ASSEMBLY for the final assembly before
3128    using the matrix.
3129 
3130    Level: beginner
3131 
3132 .seealso: MatAssemblyBegin(), MatSetValues(), PetscDrawOpenX(), MatView(), MatAssembled()
3133 @*/
3134 int MatAssemblyEnd(Mat mat,MatAssemblyType type)
3135 {
3136   int        ierr;
3137   static int inassm = 0;
3138 
3139   PetscFunctionBegin;
3140   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3141   PetscValidType(mat);
3142   MatPreallocated(mat);
3143 
3144   inassm++;
3145   MatAssemblyEnd_InUse++;
3146   if (MatAssemblyEnd_InUse == 1) { /* Do the logging only the first time through */
3147     ierr = PetscLogEventBegin(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
3148     if (mat->ops->assemblyend) {
3149       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
3150     }
3151     ierr = PetscLogEventEnd(MAT_AssemblyEnd,mat,0,0,0);CHKERRQ(ierr);
3152   } else {
3153     if (mat->ops->assemblyend) {
3154       ierr = (*mat->ops->assemblyend)(mat,type);CHKERRQ(ierr);
3155     }
3156   }
3157 
3158   /* Flush assembly is not a true assembly */
3159   if (type != MAT_FLUSH_ASSEMBLY) {
3160     mat->assembled  = PETSC_TRUE; mat->num_ass++;
3161   }
3162   mat->insertmode = NOT_SET_VALUES;
3163   MatAssemblyEnd_InUse--;
3164 
3165   if (inassm == 1 && type != MAT_FLUSH_ASSEMBLY) {
3166     ierr = MatView_Private(mat);CHKERRQ(ierr);
3167   }
3168   inassm--;
3169   PetscFunctionReturn(0);
3170 }
3171 
3172 
3173 #undef __FUNCT__
3174 #define __FUNCT__ "MatCompress"
3175 /*@
3176    MatCompress - Tries to store the matrix in as little space as
3177    possible.  May fail if memory is already fully used, since it
3178    tries to allocate new space.
3179 
3180    Collective on Mat
3181 
3182    Input Parameters:
3183 .  mat - the matrix
3184 
3185    Level: advanced
3186 
3187 @*/
3188 int MatCompress(Mat mat)
3189 {
3190   int ierr;
3191 
3192   PetscFunctionBegin;
3193   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3194   PetscValidType(mat);
3195   MatPreallocated(mat);
3196   if (mat->ops->compress) {ierr = (*mat->ops->compress)(mat);CHKERRQ(ierr);}
3197   PetscFunctionReturn(0);
3198 }
3199 
3200 #undef __FUNCT__
3201 #define __FUNCT__ "MatSetOption"
3202 /*@
3203    MatSetOption - Sets a parameter option for a matrix. Some options
3204    may be specific to certain storage formats.  Some options
3205    determine how values will be inserted (or added). Sorted,
3206    row-oriented input will generally assemble the fastest. The default
3207    is row-oriented, nonsorted input.
3208 
3209    Collective on Mat
3210 
3211    Input Parameters:
3212 +  mat - the matrix
3213 -  option - the option, one of those listed below (and possibly others),
3214              e.g., MAT_ROWS_SORTED, MAT_NEW_NONZERO_LOCATION_ERR
3215 
3216    Options Describing Matrix Structure:
3217 +    MAT_SYMMETRIC - symmetric in terms of both structure and value
3218 -    MAT_STRUCTURALLY_SYMMETRIC - symmetric nonzero structure
3219 
3220    Options For Use with MatSetValues():
3221    Insert a logically dense subblock, which can be
3222 +    MAT_ROW_ORIENTED - row-oriented
3223 .    MAT_COLUMN_ORIENTED - column-oriented
3224 .    MAT_ROWS_SORTED - sorted by row
3225 .    MAT_ROWS_UNSORTED - not sorted by row
3226 .    MAT_COLUMNS_SORTED - sorted by column
3227 -    MAT_COLUMNS_UNSORTED - not sorted by column
3228 
3229    Not these options reflect the data you pass in with MatSetValues(); it has
3230    nothing to do with how the data is stored internally in the matrix
3231    data structure.
3232 
3233    When (re)assembling a matrix, we can restrict the input for
3234    efficiency/debugging purposes.  These options include
3235 +    MAT_NO_NEW_NONZERO_LOCATIONS - additional insertions will not be
3236         allowed if they generate a new nonzero
3237 .    MAT_YES_NEW_NONZERO_LOCATIONS - additional insertions will be allowed
3238 .    MAT_NO_NEW_DIAGONALS - additional insertions will not be allowed if
3239          they generate a nonzero in a new diagonal (for block diagonal format only)
3240 .    MAT_YES_NEW_DIAGONALS - new diagonals will be allowed (for block diagonal format only)
3241 .    MAT_IGNORE_OFF_PROC_ENTRIES - drops off-processor entries
3242 .    MAT_NEW_NONZERO_LOCATION_ERR - generates an error for new matrix entry
3243 -    MAT_USE_HASH_TABLE - uses a hash table to speed up matrix assembly
3244 
3245    Notes:
3246    Some options are relevant only for particular matrix types and
3247    are thus ignored by others.  Other options are not supported by
3248    certain matrix types and will generate an error message if set.
3249 
3250    If using a Fortran 77 module to compute a matrix, one may need to
3251    use the column-oriented option (or convert to the row-oriented
3252    format).
3253 
3254    MAT_NO_NEW_NONZERO_LOCATIONS indicates that any add or insertion
3255    that would generate a new entry in the nonzero structure is instead
3256    ignored.  Thus, if memory has not alredy been allocated for this particular
3257    data, then the insertion is ignored. For dense matrices, in which
3258    the entire array is allocated, no entries are ever ignored.
3259 
3260    MAT_NEW_NONZERO_LOCATION_ERR indicates that any add or insertion
3261    that would generate a new entry in the nonzero structure instead produces
3262    an error. (Currently supported for AIJ and BAIJ formats only.)
3263    This is a useful flag when using SAME_NONZERO_PATTERN in calling
3264    SLESSetOperators() to ensure that the nonzero pattern truely does
3265    remain unchanged.
3266 
3267    MAT_NEW_NONZERO_ALLOCATION_ERR indicates that any add or insertion
3268    that would generate a new entry that has not been preallocated will
3269    instead produce an error. (Currently supported for AIJ and BAIJ formats
3270    only.) This is a useful flag when debugging matrix memory preallocation.
3271 
3272    MAT_IGNORE_OFF_PROC_ENTRIES indicates entries destined for
3273    other processors should be dropped, rather than stashed.
3274    This is useful if you know that the "owning" processor is also
3275    always generating the correct matrix entries, so that PETSc need
3276    not transfer duplicate entries generated on another processor.
3277 
3278    MAT_USE_HASH_TABLE indicates that a hash table be used to improve the
3279    searches during matrix assembly. When this flag is set, the hash table
3280    is created during the first Matrix Assembly. This hash table is
3281    used the next time through, during MatSetVaules()/MatSetVaulesBlocked()
3282    to improve the searching of indices. MAT_NO_NEW_NONZERO_LOCATIONS flag
3283    should be used with MAT_USE_HASH_TABLE flag. This option is currently
3284    supported by MATMPIBAIJ format only.
3285 
3286    MAT_KEEP_ZEROED_ROWS indicates when MatZeroRows() is called the zeroed entries
3287    are kept in the nonzero structure
3288 
3289    MAT_IGNORE_ZERO_ENTRIES - when using ADD_VALUES for AIJ matrices this will stop
3290    zero values from creating a zero location in the matrix
3291 
3292    MAT_USE_INODES - indicates using inode version of the code - works with AIJ and
3293    ROWBS matrix types
3294 
3295    MAT_DO_NOT_USE_INODES - indicates not using inode version of the code - works
3296    with AIJ and ROWBS matrix types
3297 
3298    Level: intermediate
3299 
3300    Concepts: matrices^setting options
3301 
3302 @*/
3303 int MatSetOption(Mat mat,MatOption op)
3304 {
3305   int ierr;
3306 
3307   PetscFunctionBegin;
3308   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3309   PetscValidType(mat);
3310   MatPreallocated(mat);
3311   switch (op) {
3312   case MAT_SYMMETRIC:
3313     mat->symmetric              = PETSC_TRUE;
3314     mat->structurally_symmetric = PETSC_TRUE;
3315     break;
3316   case MAT_STRUCTURALLY_SYMMETRIC:
3317     mat->structurally_symmetric = PETSC_TRUE;
3318     break;
3319   default:
3320     if (mat->ops->setoption) {ierr = (*mat->ops->setoption)(mat,op);CHKERRQ(ierr);}
3321     break;
3322   }
3323   PetscFunctionReturn(0);
3324 }
3325 
3326 #undef __FUNCT__
3327 #define __FUNCT__ "MatZeroEntries"
3328 /*@
3329    MatZeroEntries - Zeros all entries of a matrix.  For sparse matrices
3330    this routine retains the old nonzero structure.
3331 
3332    Collective on Mat
3333 
3334    Input Parameters:
3335 .  mat - the matrix
3336 
3337    Level: intermediate
3338 
3339    Concepts: matrices^zeroing
3340 
3341 .seealso: MatZeroRows()
3342 @*/
3343 int MatZeroEntries(Mat mat)
3344 {
3345   int ierr;
3346 
3347   PetscFunctionBegin;
3348   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3349   PetscValidType(mat);
3350   MatPreallocated(mat);
3351   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3352   if (!mat->ops->zeroentries) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3353 
3354   ierr = PetscLogEventBegin(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3355   ierr = (*mat->ops->zeroentries)(mat);CHKERRQ(ierr);
3356   ierr = PetscLogEventEnd(MAT_ZeroEntries,mat,0,0,0);CHKERRQ(ierr);
3357   PetscFunctionReturn(0);
3358 }
3359 
3360 #undef __FUNCT__
3361 #define __FUNCT__ "MatZeroRows"
3362 /*@C
3363    MatZeroRows - Zeros all entries (except possibly the main diagonal)
3364    of a set of rows of a matrix.
3365 
3366    Collective on Mat
3367 
3368    Input Parameters:
3369 +  mat - the matrix
3370 .  is - index set of rows to remove
3371 -  diag - pointer to value put in all diagonals of eliminated rows.
3372           Note that diag is not a pointer to an array, but merely a
3373           pointer to a single value.
3374 
3375    Notes:
3376    For the AIJ and BAIJ matrix formats this removes the old nonzero structure,
3377    but does not release memory.  For the dense and block diagonal
3378    formats this does not alter the nonzero structure.
3379 
3380    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3381    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3382    merely zeroed.
3383 
3384    The user can set a value in the diagonal entry (or for the AIJ and
3385    row formats can optionally remove the main diagonal entry from the
3386    nonzero structure as well, by passing a null pointer (PETSC_NULL
3387    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3388 
3389    For the parallel case, all processes that share the matrix (i.e.,
3390    those in the communicator used for matrix creation) MUST call this
3391    routine, regardless of whether any rows being zeroed are owned by
3392    them.
3393 
3394 
3395    Level: intermediate
3396 
3397    Concepts: matrices^zeroing rows
3398 
3399 .seealso: MatZeroEntries(), MatZeroRowsLocal(), MatSetOption()
3400 @*/
3401 int MatZeroRows(Mat mat,IS is,PetscScalar *diag)
3402 {
3403   int ierr;
3404 
3405   PetscFunctionBegin;
3406   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3407   PetscValidType(mat);
3408   MatPreallocated(mat);
3409   PetscValidHeaderSpecific(is,IS_COOKIE);
3410   if (diag) PetscValidScalarPointer(diag);
3411   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3412   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3413   if (!mat->ops->zerorows) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3414 
3415   ierr = (*mat->ops->zerorows)(mat,is,diag);CHKERRQ(ierr);
3416   ierr = MatView_Private(mat);CHKERRQ(ierr);
3417   PetscFunctionReturn(0);
3418 }
3419 
3420 #undef __FUNCT__
3421 #define __FUNCT__ "MatZeroRowsLocal"
3422 /*@C
3423    MatZeroRowsLocal - Zeros all entries (except possibly the main diagonal)
3424    of a set of rows of a matrix; using local numbering of rows.
3425 
3426    Collective on Mat
3427 
3428    Input Parameters:
3429 +  mat - the matrix
3430 .  is - index set of rows to remove
3431 -  diag - pointer to value put in all diagonals of eliminated rows.
3432           Note that diag is not a pointer to an array, but merely a
3433           pointer to a single value.
3434 
3435    Notes:
3436    Before calling MatZeroRowsLocal(), the user must first set the
3437    local-to-global mapping by calling MatSetLocalToGlobalMapping().
3438 
3439    For the AIJ matrix formats this removes the old nonzero structure,
3440    but does not release memory.  For the dense and block diagonal
3441    formats this does not alter the nonzero structure.
3442 
3443    If the option MatSetOption(mat,MAT_KEEP_ZEROED_ROWS) the nonzero structure
3444    of the matrix is not changed (even for AIJ and BAIJ matrices) the values are
3445    merely zeroed.
3446 
3447    The user can set a value in the diagonal entry (or for the AIJ and
3448    row formats can optionally remove the main diagonal entry from the
3449    nonzero structure as well, by passing a null pointer (PETSC_NULL
3450    in C or PETSC_NULL_SCALAR in Fortran) as the final argument).
3451 
3452    Level: intermediate
3453 
3454    Concepts: matrices^zeroing
3455 
3456 .seealso: MatZeroEntries(), MatZeroRows(), MatSetLocalToGlobalMapping
3457 @*/
3458 int MatZeroRowsLocal(Mat mat,IS is,PetscScalar *diag)
3459 {
3460   int ierr;
3461   IS  newis;
3462 
3463   PetscFunctionBegin;
3464   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3465   PetscValidType(mat);
3466   MatPreallocated(mat);
3467   PetscValidHeaderSpecific(is,IS_COOKIE);
3468   if (diag) PetscValidScalarPointer(diag);
3469   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3470   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3471 
3472   if (mat->ops->zerorowslocal) {
3473     ierr = (*mat->ops->zerorowslocal)(mat,is,diag);CHKERRQ(ierr);
3474   } else {
3475     if (!mat->mapping) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Need to provide local to global mapping to matrix first");
3476     ierr = ISLocalToGlobalMappingApplyIS(mat->mapping,is,&newis);CHKERRQ(ierr);
3477     ierr = (*mat->ops->zerorows)(mat,newis,diag);CHKERRQ(ierr);
3478     ierr = ISDestroy(newis);CHKERRQ(ierr);
3479   }
3480   PetscFunctionReturn(0);
3481 }
3482 
3483 #undef __FUNCT__
3484 #define __FUNCT__ "MatGetSize"
3485 /*@
3486    MatGetSize - Returns the numbers of rows and columns in a matrix.
3487 
3488    Not Collective
3489 
3490    Input Parameter:
3491 .  mat - the matrix
3492 
3493    Output Parameters:
3494 +  m - the number of global rows
3495 -  n - the number of global columns
3496 
3497    Level: beginner
3498 
3499    Concepts: matrices^size
3500 
3501 .seealso: MatGetLocalSize()
3502 @*/
3503 int MatGetSize(Mat mat,int *m,int* n)
3504 {
3505   PetscFunctionBegin;
3506   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3507   if (m) *m = mat->M;
3508   if (n) *n = mat->N;
3509   PetscFunctionReturn(0);
3510 }
3511 
3512 #undef __FUNCT__
3513 #define __FUNCT__ "MatGetLocalSize"
3514 /*@
3515    MatGetLocalSize - Returns the number of rows and columns in a matrix
3516    stored locally.  This information may be implementation dependent, so
3517    use with care.
3518 
3519    Not Collective
3520 
3521    Input Parameters:
3522 .  mat - the matrix
3523 
3524    Output Parameters:
3525 +  m - the number of local rows
3526 -  n - the number of local columns
3527 
3528    Level: beginner
3529 
3530    Concepts: matrices^local size
3531 
3532 .seealso: MatGetSize()
3533 @*/
3534 int MatGetLocalSize(Mat mat,int *m,int* n)
3535 {
3536   PetscFunctionBegin;
3537   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3538   if (m) *m = mat->m;
3539   if (n) *n = mat->n;
3540   PetscFunctionReturn(0);
3541 }
3542 
3543 #undef __FUNCT__
3544 #define __FUNCT__ "MatGetOwnershipRange"
3545 /*@
3546    MatGetOwnershipRange - Returns the range of matrix rows owned by
3547    this processor, assuming that the matrix is laid out with the first
3548    n1 rows on the first processor, the next n2 rows on the second, etc.
3549    For certain parallel layouts this range may not be well defined.
3550 
3551    Not Collective
3552 
3553    Input Parameters:
3554 .  mat - the matrix
3555 
3556    Output Parameters:
3557 +  m - the global index of the first local row
3558 -  n - one more than the global index of the last local row
3559 
3560    Level: beginner
3561 
3562    Concepts: matrices^row ownership
3563 @*/
3564 int MatGetOwnershipRange(Mat mat,int *m,int* n)
3565 {
3566   int ierr;
3567 
3568   PetscFunctionBegin;
3569   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3570   PetscValidType(mat);
3571   MatPreallocated(mat);
3572   if (m) PetscValidIntPointer(m);
3573   if (n) PetscValidIntPointer(n);
3574   ierr = PetscMapGetLocalRange(mat->rmap,m,n);CHKERRQ(ierr);
3575   PetscFunctionReturn(0);
3576 }
3577 
3578 #undef __FUNCT__
3579 #define __FUNCT__ "MatILUFactorSymbolic"
3580 /*@
3581    MatILUFactorSymbolic - Performs symbolic ILU factorization of a matrix.
3582    Uses levels of fill only, not drop tolerance. Use MatLUFactorNumeric()
3583    to complete the factorization.
3584 
3585    Collective on Mat
3586 
3587    Input Parameters:
3588 +  mat - the matrix
3589 .  row - row permutation
3590 .  column - column permutation
3591 -  info - structure containing
3592 $      levels - number of levels of fill.
3593 $      expected fill - as ratio of original fill.
3594 $      1 or 0 - indicating force fill on diagonal (improves robustness for matrices
3595                 missing diagonal entries)
3596 
3597    Output Parameters:
3598 .  fact - new matrix that has been symbolically factored
3599 
3600    Notes:
3601    See the users manual for additional information about
3602    choosing the fill factor for better efficiency.
3603 
3604    Most users should employ the simplified SLES interface for linear solvers
3605    instead of working directly with matrix algebra routines such as this.
3606    See, e.g., SLESCreate().
3607 
3608    Level: developer
3609 
3610   Concepts: matrices^symbolic LU factorization
3611   Concepts: matrices^factorization
3612   Concepts: LU^symbolic factorization
3613 
3614 .seealso: MatLUFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
3615           MatGetOrdering(), MatILUInfo
3616 
3617 @*/
3618 int MatILUFactorSymbolic(Mat mat,IS row,IS col,MatILUInfo *info,Mat *fact)
3619 {
3620   int ierr;
3621 
3622   PetscFunctionBegin;
3623   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3624   PetscValidType(mat);
3625   MatPreallocated(mat);
3626   PetscValidPointer(fact);
3627   PetscValidHeaderSpecific(row,IS_COOKIE);
3628   PetscValidHeaderSpecific(col,IS_COOKIE);
3629   if (info && info->levels < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Levels of fill negative %d",info->levels);
3630   if (info && info->fill < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",info->fill);
3631   if (!mat->ops->ilufactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ILU",mat->type_name);
3632   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3633   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3634 
3635   ierr = PetscLogEventBegin(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3636   ierr = (*mat->ops->ilufactorsymbolic)(mat,row,col,info,fact);CHKERRQ(ierr);
3637   ierr = PetscLogEventEnd(MAT_ILUFactorSymbolic,mat,row,col,0);CHKERRQ(ierr);
3638   PetscFunctionReturn(0);
3639 }
3640 
3641 #undef __FUNCT__
3642 #define __FUNCT__ "MatICCFactorSymbolic"
3643 /*@
3644    MatICCFactorSymbolic - Performs symbolic incomplete
3645    Cholesky factorization for a symmetric matrix.  Use
3646    MatCholeskyFactorNumeric() to complete the factorization.
3647 
3648    Collective on Mat
3649 
3650    Input Parameters:
3651 +  mat - the matrix
3652 .  perm - row and column permutation
3653 .  fill - levels of fill
3654 -  f - expected fill as ratio of original fill
3655 
3656    Output Parameter:
3657 .  fact - the factored matrix
3658 
3659    Notes:
3660    Currently only no-fill factorization is supported.
3661 
3662    Most users should employ the simplified SLES interface for linear solvers
3663    instead of working directly with matrix algebra routines such as this.
3664    See, e.g., SLESCreate().
3665 
3666    Level: developer
3667 
3668   Concepts: matrices^symbolic incomplete Cholesky factorization
3669   Concepts: matrices^factorization
3670   Concepts: Cholsky^symbolic factorization
3671 
3672 .seealso: MatCholeskyFactorNumeric(), MatCholeskyFactor()
3673 @*/
3674 int MatICCFactorSymbolic(Mat mat,IS perm,PetscReal f,int fill,Mat *fact)
3675 {
3676   int ierr;
3677 
3678   PetscFunctionBegin;
3679   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3680   PetscValidType(mat);
3681   MatPreallocated(mat);
3682   PetscValidPointer(fact);
3683   PetscValidHeaderSpecific(perm,IS_COOKIE);
3684   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3685   if (fill < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Fill negative %d",fill);
3686   if (f < 1.0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Expected fill less than 1.0 %g",f);
3687   if (!mat->ops->iccfactorsymbolic) SETERRQ1(PETSC_ERR_SUP,"Matrix type %s  symbolic ICC",mat->type_name);
3688   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3689 
3690   ierr = PetscLogEventBegin(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3691   ierr = (*mat->ops->iccfactorsymbolic)(mat,perm,f,fill,fact);CHKERRQ(ierr);
3692   ierr = PetscLogEventEnd(MAT_ICCFactorSymbolic,mat,perm,0,0);CHKERRQ(ierr);
3693   PetscFunctionReturn(0);
3694 }
3695 
3696 #undef __FUNCT__
3697 #define __FUNCT__ "MatGetArray"
3698 /*@C
3699    MatGetArray - Returns a pointer to the element values in the matrix.
3700    The result of this routine is dependent on the underlying matrix data
3701    structure, and may not even work for certain matrix types.  You MUST
3702    call MatRestoreArray() when you no longer need to access the array.
3703 
3704    Not Collective
3705 
3706    Input Parameter:
3707 .  mat - the matrix
3708 
3709    Output Parameter:
3710 .  v - the location of the values
3711 
3712 
3713    Fortran Note:
3714    This routine is used differently from Fortran, e.g.,
3715 .vb
3716         Mat         mat
3717         PetscScalar mat_array(1)
3718         PetscOffset i_mat
3719         int         ierr
3720         call MatGetArray(mat,mat_array,i_mat,ierr)
3721 
3722   C  Access first local entry in matrix; note that array is
3723   C  treated as one dimensional
3724         value = mat_array(i_mat + 1)
3725 
3726         [... other code ...]
3727         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3728 .ve
3729 
3730    See the Fortran chapter of the users manual and
3731    petsc/src/mat/examples/tests for details.
3732 
3733    Level: advanced
3734 
3735    Concepts: matrices^access array
3736 
3737 .seealso: MatRestoreArray(), MatGetArrayF90()
3738 @*/
3739 int MatGetArray(Mat mat,PetscScalar **v)
3740 {
3741   int ierr;
3742 
3743   PetscFunctionBegin;
3744   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3745   PetscValidType(mat);
3746   MatPreallocated(mat);
3747   PetscValidPointer(v);
3748   if (!mat->ops->getarray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3749   ierr = (*mat->ops->getarray)(mat,v);CHKERRQ(ierr);
3750   PetscFunctionReturn(0);
3751 }
3752 
3753 #undef __FUNCT__
3754 #define __FUNCT__ "MatRestoreArray"
3755 /*@C
3756    MatRestoreArray - Restores the matrix after MatGetArray() has been called.
3757 
3758    Not Collective
3759 
3760    Input Parameter:
3761 +  mat - the matrix
3762 -  v - the location of the values
3763 
3764    Fortran Note:
3765    This routine is used differently from Fortran, e.g.,
3766 .vb
3767         Mat         mat
3768         PetscScalar mat_array(1)
3769         PetscOffset i_mat
3770         int         ierr
3771         call MatGetArray(mat,mat_array,i_mat,ierr)
3772 
3773   C  Access first local entry in matrix; note that array is
3774   C  treated as one dimensional
3775         value = mat_array(i_mat + 1)
3776 
3777         [... other code ...]
3778         call MatRestoreArray(mat,mat_array,i_mat,ierr)
3779 .ve
3780 
3781    See the Fortran chapter of the users manual and
3782    petsc/src/mat/examples/tests for details
3783 
3784    Level: advanced
3785 
3786 .seealso: MatGetArray(), MatRestoreArrayF90()
3787 @*/
3788 int MatRestoreArray(Mat mat,PetscScalar **v)
3789 {
3790   int ierr;
3791 
3792   PetscFunctionBegin;
3793   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3794   PetscValidType(mat);
3795   MatPreallocated(mat);
3796   PetscValidPointer(v);
3797   if (!mat->ops->restorearray) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3798   ierr = (*mat->ops->restorearray)(mat,v);CHKERRQ(ierr);
3799   PetscFunctionReturn(0);
3800 }
3801 
3802 #undef __FUNCT__
3803 #define __FUNCT__ "MatGetSubMatrices"
3804 /*@C
3805    MatGetSubMatrices - Extracts several submatrices from a matrix. If submat
3806    points to an array of valid matrices, they may be reused to store the new
3807    submatrices.
3808 
3809    Collective on Mat
3810 
3811    Input Parameters:
3812 +  mat - the matrix
3813 .  n   - the number of submatrixes to be extracted (on this processor, may be zero)
3814 .  irow, icol - index sets of rows and columns to extract
3815 -  scall - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
3816 
3817    Output Parameter:
3818 .  submat - the array of submatrices
3819 
3820    Notes:
3821    MatGetSubMatrices() can extract only sequential submatrices
3822    (from both sequential and parallel matrices). Use MatGetSubMatrix()
3823    to extract a parallel submatrix.
3824 
3825    When extracting submatrices from a parallel matrix, each processor can
3826    form a different submatrix by setting the rows and columns of its
3827    individual index sets according to the local submatrix desired.
3828 
3829    When finished using the submatrices, the user should destroy
3830    them with MatDestroyMatrices().
3831 
3832    MAT_REUSE_MATRIX can only be used when the nonzero structure of the
3833    original matrix has not changed from that last call to MatGetSubMatrices().
3834 
3835    This routine creates the matrices submat; you should NOT create them before
3836    calling it.
3837 
3838    Fortran Note:
3839    The Fortran interface is slightly different from that given below; it
3840    requires one to pass in  as submat a Mat (integer) array of size at least m.
3841 
3842    Level: advanced
3843 
3844    Concepts: matrices^accessing submatrices
3845    Concepts: submatrices
3846 
3847 .seealso: MatDestroyMatrices(), MatGetSubMatrix(), MatGetRow(), MatGetDiagonal()
3848 @*/
3849 int MatGetSubMatrices(Mat mat,int n,IS *irow,IS *icol,MatReuse scall,Mat **submat)
3850 {
3851   int        ierr;
3852 
3853   PetscFunctionBegin;
3854   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3855   PetscValidType(mat);
3856   MatPreallocated(mat);
3857   if (!mat->ops->getsubmatrices) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3858   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3859 
3860   ierr = PetscLogEventBegin(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3861   ierr = (*mat->ops->getsubmatrices)(mat,n,irow,icol,scall,submat);CHKERRQ(ierr);
3862   ierr = PetscLogEventEnd(MAT_GetSubMatrices,mat,0,0,0);CHKERRQ(ierr);
3863   PetscFunctionReturn(0);
3864 }
3865 
3866 #undef __FUNCT__
3867 #define __FUNCT__ "MatDestroyMatrices"
3868 /*@C
3869    MatDestroyMatrices - Destroys a set of matrices obtained with MatGetSubMatrices().
3870 
3871    Collective on Mat
3872 
3873    Input Parameters:
3874 +  n - the number of local matrices
3875 -  mat - the matrices
3876 
3877    Level: advanced
3878 
3879     Notes: Frees not only the matrices, but also the array that contains the matrices
3880 
3881 .seealso: MatGetSubMatrices()
3882 @*/
3883 int MatDestroyMatrices(int n,Mat **mat)
3884 {
3885   int ierr,i;
3886 
3887   PetscFunctionBegin;
3888   if (n < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Trying to destroy negative number of matrices %d",n);
3889   PetscValidPointer(mat);
3890   for (i=0; i<n; i++) {
3891     ierr = MatDestroy((*mat)[i]);CHKERRQ(ierr);
3892   }
3893   /* memory is allocated even if n = 0 */
3894   ierr = PetscFree(*mat);CHKERRQ(ierr);
3895   PetscFunctionReturn(0);
3896 }
3897 
3898 #undef __FUNCT__
3899 #define __FUNCT__ "MatIncreaseOverlap"
3900 /*@
3901    MatIncreaseOverlap - Given a set of submatrices indicated by index sets,
3902    replaces the index sets by larger ones that represent submatrices with
3903    additional overlap.
3904 
3905    Collective on Mat
3906 
3907    Input Parameters:
3908 +  mat - the matrix
3909 .  n   - the number of index sets
3910 .  is  - the array of pointers to index sets
3911 -  ov  - the additional overlap requested
3912 
3913    Level: developer
3914 
3915    Concepts: overlap
3916    Concepts: ASM^computing overlap
3917 
3918 .seealso: MatGetSubMatrices()
3919 @*/
3920 int MatIncreaseOverlap(Mat mat,int n,IS *is,int ov)
3921 {
3922   int ierr;
3923 
3924   PetscFunctionBegin;
3925   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3926   PetscValidType(mat);
3927   MatPreallocated(mat);
3928   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3929   if (mat->factor)     SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3930 
3931   if (!ov) PetscFunctionReturn(0);
3932   if (!mat->ops->increaseoverlap) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
3933   ierr = PetscLogEventBegin(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3934   ierr = (*mat->ops->increaseoverlap)(mat,n,is,ov);CHKERRQ(ierr);
3935   ierr = PetscLogEventEnd(MAT_IncreaseOverlap,mat,0,0,0);CHKERRQ(ierr);
3936   PetscFunctionReturn(0);
3937 }
3938 
3939 #undef __FUNCT__
3940 #define __FUNCT__ "MatPrintHelp"
3941 /*@
3942    MatPrintHelp - Prints all the options for the matrix.
3943 
3944    Collective on Mat
3945 
3946    Input Parameter:
3947 .  mat - the matrix
3948 
3949    Options Database Keys:
3950 +  -help - Prints matrix options
3951 -  -h - Prints matrix options
3952 
3953    Level: developer
3954 
3955 .seealso: MatCreate(), MatCreateXXX()
3956 @*/
3957 int MatPrintHelp(Mat mat)
3958 {
3959   static PetscTruth called = PETSC_FALSE;
3960   int               ierr;
3961   MPI_Comm          comm;
3962 
3963   PetscFunctionBegin;
3964   PetscValidHeaderSpecific(mat,MAT_COOKIE);
3965   PetscValidType(mat);
3966   MatPreallocated(mat);
3967 
3968   comm = mat->comm;
3969   if (!called) {
3970     ierr = (*PetscHelpPrintf)(comm,"General matrix options:\n");CHKERRQ(ierr);
3971     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info: view basic matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3972     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_info_detailed: view detailed matrix info during MatAssemblyEnd()\n");CHKERRQ(ierr);
3973     ierr = (*PetscHelpPrintf)(comm,"  -mat_view_draw: draw nonzero matrix structure during MatAssemblyEnd()\n");CHKERRQ(ierr);
3974     ierr = (*PetscHelpPrintf)(comm,"      -draw_pause <sec>: set seconds of display pause\n");CHKERRQ(ierr);
3975     ierr = (*PetscHelpPrintf)(comm,"      -display <name>: set alternate display\n");CHKERRQ(ierr);
3976     called = PETSC_TRUE;
3977   }
3978   if (mat->ops->printhelp) {
3979     ierr = (*mat->ops->printhelp)(mat);CHKERRQ(ierr);
3980   }
3981   PetscFunctionReturn(0);
3982 }
3983 
3984 #undef __FUNCT__
3985 #define __FUNCT__ "MatGetBlockSize"
3986 /*@
3987    MatGetBlockSize - Returns the matrix block size; useful especially for the
3988    block row and block diagonal formats.
3989 
3990    Not Collective
3991 
3992    Input Parameter:
3993 .  mat - the matrix
3994 
3995    Output Parameter:
3996 .  bs - block size
3997 
3998    Notes:
3999    Block diagonal formats are MATSEQBDIAG, MATMPIBDIAG.
4000    Block row formats are MATSEQBAIJ, MATMPIBAIJ
4001 
4002    Level: intermediate
4003 
4004    Concepts: matrices^block size
4005 
4006 .seealso: MatCreateSeqBAIJ(), MatCreateMPIBAIJ(), MatCreateSeqBDiag(), MatCreateMPIBDiag()
4007 @*/
4008 int MatGetBlockSize(Mat mat,int *bs)
4009 {
4010   int ierr;
4011 
4012   PetscFunctionBegin;
4013   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4014   PetscValidType(mat);
4015   MatPreallocated(mat);
4016   PetscValidIntPointer(bs);
4017   if (!mat->ops->getblocksize) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4018   ierr = (*mat->ops->getblocksize)(mat,bs);CHKERRQ(ierr);
4019   PetscFunctionReturn(0);
4020 }
4021 
4022 #undef __FUNCT__
4023 #define __FUNCT__ "MatGetRowIJ"
4024 /*@C
4025     MatGetRowIJ - Returns the compressed row storage i and j indices for sequential matrices.
4026 
4027    Collective on Mat
4028 
4029     Input Parameters:
4030 +   mat - the matrix
4031 .   shift -  0 or 1 indicating we want the indices starting at 0 or 1
4032 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4033                 symmetrized
4034 
4035     Output Parameters:
4036 +   n - number of rows in the (possibly compressed) matrix
4037 .   ia - the row pointers
4038 .   ja - the column indices
4039 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
4040 
4041     Level: developer
4042 
4043 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
4044 @*/
4045 int MatGetRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4046 {
4047   int ierr;
4048 
4049   PetscFunctionBegin;
4050   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4051   PetscValidType(mat);
4052   MatPreallocated(mat);
4053   if (ia) PetscValidIntPointer(ia);
4054   if (ja) PetscValidIntPointer(ja);
4055   PetscValidIntPointer(done);
4056   if (!mat->ops->getrowij) *done = PETSC_FALSE;
4057   else {
4058     *done = PETSC_TRUE;
4059     ierr  = (*mat->ops->getrowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4060   }
4061   PetscFunctionReturn(0);
4062 }
4063 
4064 #undef __FUNCT__
4065 #define __FUNCT__ "MatGetColumnIJ"
4066 /*@C
4067     MatGetColumnIJ - Returns the compressed column storage i and j indices for sequential matrices.
4068 
4069     Collective on Mat
4070 
4071     Input Parameters:
4072 +   mat - the matrix
4073 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4074 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4075                 symmetrized
4076 
4077     Output Parameters:
4078 +   n - number of columns in the (possibly compressed) matrix
4079 .   ia - the column pointers
4080 .   ja - the row indices
4081 -   done - PETSC_TRUE or PETSC_FALSE, indicating whether the values have been returned
4082 
4083     Level: developer
4084 
4085 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
4086 @*/
4087 int MatGetColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4088 {
4089   int ierr;
4090 
4091   PetscFunctionBegin;
4092   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4093   PetscValidType(mat);
4094   MatPreallocated(mat);
4095   if (ia) PetscValidIntPointer(ia);
4096   if (ja) PetscValidIntPointer(ja);
4097   PetscValidIntPointer(done);
4098 
4099   if (!mat->ops->getcolumnij) *done = PETSC_FALSE;
4100   else {
4101     *done = PETSC_TRUE;
4102     ierr  = (*mat->ops->getcolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4103   }
4104   PetscFunctionReturn(0);
4105 }
4106 
4107 #undef __FUNCT__
4108 #define __FUNCT__ "MatRestoreRowIJ"
4109 /*@C
4110     MatRestoreRowIJ - Call after you are completed with the ia,ja indices obtained with
4111     MatGetRowIJ().
4112 
4113     Collective on Mat
4114 
4115     Input Parameters:
4116 +   mat - the matrix
4117 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4118 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4119                 symmetrized
4120 
4121     Output Parameters:
4122 +   n - size of (possibly compressed) matrix
4123 .   ia - the row pointers
4124 .   ja - the column indices
4125 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
4126 
4127     Level: developer
4128 
4129 .seealso: MatGetRowIJ(), MatRestoreColumnIJ()
4130 @*/
4131 int MatRestoreRowIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4132 {
4133   int ierr;
4134 
4135   PetscFunctionBegin;
4136   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4137   PetscValidType(mat);
4138   MatPreallocated(mat);
4139   if (ia) PetscValidIntPointer(ia);
4140   if (ja) PetscValidIntPointer(ja);
4141   PetscValidIntPointer(done);
4142 
4143   if (!mat->ops->restorerowij) *done = PETSC_FALSE;
4144   else {
4145     *done = PETSC_TRUE;
4146     ierr  = (*mat->ops->restorerowij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4147   }
4148   PetscFunctionReturn(0);
4149 }
4150 
4151 #undef __FUNCT__
4152 #define __FUNCT__ "MatRestoreColumnIJ"
4153 /*@C
4154     MatRestoreColumnIJ - Call after you are completed with the ia,ja indices obtained with
4155     MatGetColumnIJ().
4156 
4157     Collective on Mat
4158 
4159     Input Parameters:
4160 +   mat - the matrix
4161 .   shift - 1 or zero indicating we want the indices starting at 0 or 1
4162 -   symmetric - PETSC_TRUE or PETSC_FALSE indicating the matrix data structure should be
4163                 symmetrized
4164 
4165     Output Parameters:
4166 +   n - size of (possibly compressed) matrix
4167 .   ia - the column pointers
4168 .   ja - the row indices
4169 -   done - PETSC_TRUE or PETSC_FALSE indicated that the values have been returned
4170 
4171     Level: developer
4172 
4173 .seealso: MatGetColumnIJ(), MatRestoreRowIJ()
4174 @*/
4175 int MatRestoreColumnIJ(Mat mat,int shift,PetscTruth symmetric,int *n,int **ia,int** ja,PetscTruth *done)
4176 {
4177   int ierr;
4178 
4179   PetscFunctionBegin;
4180   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4181   PetscValidType(mat);
4182   MatPreallocated(mat);
4183   if (ia) PetscValidIntPointer(ia);
4184   if (ja) PetscValidIntPointer(ja);
4185   PetscValidIntPointer(done);
4186 
4187   if (!mat->ops->restorecolumnij) *done = PETSC_FALSE;
4188   else {
4189     *done = PETSC_TRUE;
4190     ierr  = (*mat->ops->restorecolumnij)(mat,shift,symmetric,n,ia,ja,done);CHKERRQ(ierr);
4191   }
4192   PetscFunctionReturn(0);
4193 }
4194 
4195 #undef __FUNCT__
4196 #define __FUNCT__ "MatColoringPatch"
4197 /*@C
4198     MatColoringPatch -Used inside matrix coloring routines that
4199     use MatGetRowIJ() and/or MatGetColumnIJ().
4200 
4201     Collective on Mat
4202 
4203     Input Parameters:
4204 +   mat - the matrix
4205 .   n   - number of colors
4206 -   colorarray - array indicating color for each column
4207 
4208     Output Parameters:
4209 .   iscoloring - coloring generated using colorarray information
4210 
4211     Level: developer
4212 
4213 .seealso: MatGetRowIJ(), MatGetColumnIJ()
4214 
4215 @*/
4216 int MatColoringPatch(Mat mat,int n,int ncolors,int *colorarray,ISColoring *iscoloring)
4217 {
4218   int ierr;
4219 
4220   PetscFunctionBegin;
4221   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4222   PetscValidType(mat);
4223   MatPreallocated(mat);
4224   PetscValidIntPointer(colorarray);
4225 
4226   if (!mat->ops->coloringpatch){
4227     ierr = ISColoringCreate(mat->comm,n,colorarray,iscoloring);CHKERRQ(ierr);
4228   } else {
4229     ierr = (*mat->ops->coloringpatch)(mat,n,ncolors,colorarray,iscoloring);CHKERRQ(ierr);
4230   }
4231   PetscFunctionReturn(0);
4232 }
4233 
4234 
4235 #undef __FUNCT__
4236 #define __FUNCT__ "MatSetUnfactored"
4237 /*@
4238    MatSetUnfactored - Resets a factored matrix to be treated as unfactored.
4239 
4240    Collective on Mat
4241 
4242    Input Parameter:
4243 .  mat - the factored matrix to be reset
4244 
4245    Notes:
4246    This routine should be used only with factored matrices formed by in-place
4247    factorization via ILU(0) (or by in-place LU factorization for the MATSEQDENSE
4248    format).  This option can save memory, for example, when solving nonlinear
4249    systems with a matrix-free Newton-Krylov method and a matrix-based, in-place
4250    ILU(0) preconditioner.
4251 
4252    Note that one can specify in-place ILU(0) factorization by calling
4253 .vb
4254      PCType(pc,PCILU);
4255      PCILUSeUseInPlace(pc);
4256 .ve
4257    or by using the options -pc_type ilu -pc_ilu_in_place
4258 
4259    In-place factorization ILU(0) can also be used as a local
4260    solver for the blocks within the block Jacobi or additive Schwarz
4261    methods (runtime option: -sub_pc_ilu_in_place).  See the discussion
4262    of these preconditioners in the users manual for details on setting
4263    local solver options.
4264 
4265    Most users should employ the simplified SLES interface for linear solvers
4266    instead of working directly with matrix algebra routines such as this.
4267    See, e.g., SLESCreate().
4268 
4269    Level: developer
4270 
4271 .seealso: PCILUSetUseInPlace(), PCLUSetUseInPlace()
4272 
4273    Concepts: matrices^unfactored
4274 
4275 @*/
4276 int MatSetUnfactored(Mat mat)
4277 {
4278   int ierr;
4279 
4280   PetscFunctionBegin;
4281   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4282   PetscValidType(mat);
4283   MatPreallocated(mat);
4284   mat->factor = 0;
4285   if (!mat->ops->setunfactored) PetscFunctionReturn(0);
4286   ierr = (*mat->ops->setunfactored)(mat);CHKERRQ(ierr);
4287   PetscFunctionReturn(0);
4288 }
4289 
4290 /*MC
4291     MatGetArrayF90 - Accesses a matrix array from Fortran90.
4292 
4293     Synopsis:
4294     MatGetArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4295 
4296     Not collective
4297 
4298     Input Parameter:
4299 .   x - matrix
4300 
4301     Output Parameters:
4302 +   xx_v - the Fortran90 pointer to the array
4303 -   ierr - error code
4304 
4305     Example of Usage:
4306 .vb
4307       PetscScalar, pointer xx_v(:)
4308       ....
4309       call MatGetArrayF90(x,xx_v,ierr)
4310       a = xx_v(3)
4311       call MatRestoreArrayF90(x,xx_v,ierr)
4312 .ve
4313 
4314     Notes:
4315     Not yet supported for all F90 compilers
4316 
4317     Level: advanced
4318 
4319 .seealso:  MatRestoreArrayF90(), MatGetArray(), MatRestoreArray()
4320 
4321     Concepts: matrices^accessing array
4322 
4323 M*/
4324 
4325 /*MC
4326     MatRestoreArrayF90 - Restores a matrix array that has been
4327     accessed with MatGetArrayF90().
4328 
4329     Synopsis:
4330     MatRestoreArrayF90(Mat x,{Scalar, pointer :: xx_v(:)},integer ierr)
4331 
4332     Not collective
4333 
4334     Input Parameters:
4335 +   x - matrix
4336 -   xx_v - the Fortran90 pointer to the array
4337 
4338     Output Parameter:
4339 .   ierr - error code
4340 
4341     Example of Usage:
4342 .vb
4343        PetscScalar, pointer xx_v(:)
4344        ....
4345        call MatGetArrayF90(x,xx_v,ierr)
4346        a = xx_v(3)
4347        call MatRestoreArrayF90(x,xx_v,ierr)
4348 .ve
4349 
4350     Notes:
4351     Not yet supported for all F90 compilers
4352 
4353     Level: advanced
4354 
4355 .seealso:  MatGetArrayF90(), MatGetArray(), MatRestoreArray()
4356 
4357 M*/
4358 
4359 
4360 #undef __FUNCT__
4361 #define __FUNCT__ "MatGetSubMatrix"
4362 /*@
4363     MatGetSubMatrix - Gets a single submatrix on the same number of processors
4364                       as the original matrix.
4365 
4366     Collective on Mat
4367 
4368     Input Parameters:
4369 +   mat - the original matrix
4370 .   isrow - rows this processor should obtain
4371 .   iscol - columns for all processors you wish to keep
4372 .   csize - number of columns "local" to this processor (does nothing for sequential
4373             matrices). This should match the result from VecGetLocalSize(x,...) if you
4374             plan to use the matrix in a A*x; alternatively, you can use PETSC_DECIDE
4375 -   cll - either MAT_INITIAL_MATRIX or MAT_REUSE_MATRIX
4376 
4377     Output Parameter:
4378 .   newmat - the new submatrix, of the same type as the old
4379 
4380     Level: advanced
4381 
4382     Notes: the iscol argument MUST be the same on each processor.
4383 
4384       The first time this is called you should use a cll of MAT_INITIAL_MATRIX,
4385    the MatGetSubMatrix() routine will create the newmat for you. Any additional calls
4386    to this routine with a mat of the same nonzero structure will reuse the matrix
4387    generated the first time.
4388 
4389     Concepts: matrices^submatrices
4390 
4391 .seealso: MatGetSubMatrices()
4392 @*/
4393 int MatGetSubMatrix(Mat mat,IS isrow,IS iscol,int csize,MatReuse cll,Mat *newmat)
4394 {
4395   int     ierr, size;
4396   Mat     *local;
4397 
4398   PetscFunctionBegin;
4399   PetscValidType(mat);
4400   MatPreallocated(mat);
4401   ierr = MPI_Comm_size(mat->comm,&size);CHKERRQ(ierr);
4402 
4403   /* if original matrix is on just one processor then use submatrix generated */
4404   if (!mat->ops->getsubmatrix && size == 1 && cll == MAT_REUSE_MATRIX) {
4405     ierr = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_REUSE_MATRIX,&newmat);CHKERRQ(ierr);
4406     PetscFunctionReturn(0);
4407   } else if (!mat->ops->getsubmatrix && size == 1) {
4408     ierr    = MatGetSubMatrices(mat,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&local);CHKERRQ(ierr);
4409     *newmat = *local;
4410     ierr    = PetscFree(local);CHKERRQ(ierr);
4411     PetscFunctionReturn(0);
4412   }
4413 
4414   if (!mat->ops->getsubmatrix) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4415   ierr = (*mat->ops->getsubmatrix)(mat,isrow,iscol,csize,cll,newmat);CHKERRQ(ierr);
4416   PetscFunctionReturn(0);
4417 }
4418 
4419 #undef __FUNCT__
4420 #define __FUNCT__ "MatGetPetscMaps"
4421 /*@C
4422    MatGetPetscMaps - Returns the maps associated with the matrix.
4423 
4424    Not Collective
4425 
4426    Input Parameter:
4427 .  mat - the matrix
4428 
4429    Output Parameters:
4430 +  rmap - the row (right) map
4431 -  cmap - the column (left) map
4432 
4433    Level: developer
4434 
4435    Concepts: maps^getting from matrix
4436 
4437 @*/
4438 int MatGetPetscMaps(Mat mat,PetscMap *rmap,PetscMap *cmap)
4439 {
4440   int ierr;
4441 
4442   PetscFunctionBegin;
4443   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4444   PetscValidType(mat);
4445   MatPreallocated(mat);
4446   ierr = (*mat->ops->getmaps)(mat,rmap,cmap);CHKERRQ(ierr);
4447   PetscFunctionReturn(0);
4448 }
4449 
4450 /*
4451       Version that works for all PETSc matrices
4452 */
4453 #undef __FUNCT__
4454 #define __FUNCT__ "MatGetPetscMaps_Petsc"
4455 int MatGetPetscMaps_Petsc(Mat mat,PetscMap *rmap,PetscMap *cmap)
4456 {
4457   PetscFunctionBegin;
4458   if (rmap) *rmap = mat->rmap;
4459   if (cmap) *cmap = mat->cmap;
4460   PetscFunctionReturn(0);
4461 }
4462 
4463 #undef __FUNCT__
4464 #define __FUNCT__ "MatSetStashInitialSize"
4465 /*@
4466    MatSetStashInitialSize - sets the sizes of the matrix stash, that is
4467    used during the assembly process to store values that belong to
4468    other processors.
4469 
4470    Not Collective
4471 
4472    Input Parameters:
4473 +  mat   - the matrix
4474 .  size  - the initial size of the stash.
4475 -  bsize - the initial size of the block-stash(if used).
4476 
4477    Options Database Keys:
4478 +   -matstash_initial_size <size> or <size0,size1,...sizep-1>
4479 -   -matstash_block_initial_size <bsize>  or <bsize0,bsize1,...bsizep-1>
4480 
4481    Level: intermediate
4482 
4483    Notes:
4484      The block-stash is used for values set with VecSetValuesBlocked() while
4485      the stash is used for values set with VecSetValues()
4486 
4487      Run with the option -log_info and look for output of the form
4488      MatAssemblyBegin_MPIXXX:Stash has MM entries, uses nn mallocs.
4489      to determine the appropriate value, MM, to use for size and
4490      MatAssemblyBegin_MPIXXX:Block-Stash has BMM entries, uses nn mallocs.
4491      to determine the value, BMM to use for bsize
4492 
4493    Concepts: stash^setting matrix size
4494    Concepts: matrices^stash
4495 
4496 @*/
4497 int MatSetStashInitialSize(Mat mat,int size, int bsize)
4498 {
4499   int ierr;
4500 
4501   PetscFunctionBegin;
4502   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4503   PetscValidType(mat);
4504   MatPreallocated(mat);
4505   ierr = MatStashSetInitialSize_Private(&mat->stash,size);CHKERRQ(ierr);
4506   ierr = MatStashSetInitialSize_Private(&mat->bstash,bsize);CHKERRQ(ierr);
4507   PetscFunctionReturn(0);
4508 }
4509 
4510 #undef __FUNCT__
4511 #define __FUNCT__ "MatInterpolateAdd"
4512 /*@
4513    MatInterpolateAdd - w = y + A*x or A'*x depending on the shape of
4514      the matrix
4515 
4516    Collective on Mat
4517 
4518    Input Parameters:
4519 +  mat   - the matrix
4520 .  x,y - the vectors
4521 -  w - where the result is stored
4522 
4523    Level: intermediate
4524 
4525    Notes:
4526     w may be the same vector as y.
4527 
4528     This allows one to use either the restriction or interpolation (its transpose)
4529     matrix to do the interpolation
4530 
4531     Concepts: interpolation
4532 
4533 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4534 
4535 @*/
4536 int MatInterpolateAdd(Mat A,Vec x,Vec y,Vec w)
4537 {
4538   int M,N,ierr;
4539 
4540   PetscFunctionBegin;
4541   PetscValidType(A);
4542   MatPreallocated(A);
4543   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4544   if (N > M) {
4545     ierr = MatMultTransposeAdd(A,x,y,w);CHKERRQ(ierr);
4546   } else {
4547     ierr = MatMultAdd(A,x,y,w);CHKERRQ(ierr);
4548   }
4549   PetscFunctionReturn(0);
4550 }
4551 
4552 #undef __FUNCT__
4553 #define __FUNCT__ "MatInterpolate"
4554 /*@
4555    MatInterpolate - y = A*x or A'*x depending on the shape of
4556      the matrix
4557 
4558    Collective on Mat
4559 
4560    Input Parameters:
4561 +  mat   - the matrix
4562 -  x,y - the vectors
4563 
4564    Level: intermediate
4565 
4566    Notes:
4567     This allows one to use either the restriction or interpolation (its transpose)
4568     matrix to do the interpolation
4569 
4570    Concepts: matrices^interpolation
4571 
4572 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatRestrict()
4573 
4574 @*/
4575 int MatInterpolate(Mat A,Vec x,Vec y)
4576 {
4577   int M,N,ierr;
4578 
4579   PetscFunctionBegin;
4580   PetscValidType(A);
4581   MatPreallocated(A);
4582   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4583   if (N > M) {
4584     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4585   } else {
4586     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4587   }
4588   PetscFunctionReturn(0);
4589 }
4590 
4591 #undef __FUNCT__
4592 #define __FUNCT__ "MatRestrict"
4593 /*@
4594    MatRestrict - y = A*x or A'*x
4595 
4596    Collective on Mat
4597 
4598    Input Parameters:
4599 +  mat   - the matrix
4600 -  x,y - the vectors
4601 
4602    Level: intermediate
4603 
4604    Notes:
4605     This allows one to use either the restriction or interpolation (its transpose)
4606     matrix to do the restriction
4607 
4608    Concepts: matrices^restriction
4609 
4610 .seealso: MatMultAdd(), MatMultTransposeAdd(), MatInterpolate()
4611 
4612 @*/
4613 int MatRestrict(Mat A,Vec x,Vec y)
4614 {
4615   int M,N,ierr;
4616 
4617   PetscFunctionBegin;
4618   PetscValidType(A);
4619   MatPreallocated(A);
4620   ierr = MatGetSize(A,&M,&N);CHKERRQ(ierr);
4621   if (N > M) {
4622     ierr = MatMult(A,x,y);CHKERRQ(ierr);
4623   } else {
4624     ierr = MatMultTranspose(A,x,y);CHKERRQ(ierr);
4625   }
4626   PetscFunctionReturn(0);
4627 }
4628 
4629 #undef __FUNCT__
4630 #define __FUNCT__ "MatNullSpaceAttach"
4631 /*@C
4632    MatNullSpaceAttach - attaches a null space to a matrix.
4633         This null space will be removed from the resulting vector whenever
4634         MatMult() is called
4635 
4636    Collective on Mat
4637 
4638    Input Parameters:
4639 +  mat - the matrix
4640 -  nullsp - the null space object
4641 
4642    Level: developer
4643 
4644    Notes:
4645       Overwrites any previous null space that may have been attached
4646 
4647    Concepts: null space^attaching to matrix
4648 
4649 .seealso: MatCreate(), MatNullSpaceCreate()
4650 @*/
4651 int MatNullSpaceAttach(Mat mat,MatNullSpace nullsp)
4652 {
4653   int ierr;
4654 
4655   PetscFunctionBegin;
4656   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4657   PetscValidType(mat);
4658   MatPreallocated(mat);
4659   PetscValidHeaderSpecific(nullsp,MAT_NULLSPACE_COOKIE);
4660 
4661   if (mat->nullsp) {
4662     ierr = MatNullSpaceDestroy(mat->nullsp);CHKERRQ(ierr);
4663   }
4664   mat->nullsp = nullsp;
4665   ierr = PetscObjectReference((PetscObject)nullsp);CHKERRQ(ierr);
4666   PetscFunctionReturn(0);
4667 }
4668 
4669 #undef __FUNCT__
4670 #define __FUNCT__ "MatICCFactor"
4671 /*@
4672    MatICCFactor - Performs in-place incomplete Cholesky factorization of matrix.
4673 
4674    Collective on Mat
4675 
4676    Input Parameters:
4677 +  mat - the matrix
4678 .  row - row/column permutation
4679 .  fill - expected fill factor >= 1.0
4680 -  level - level of fill, for ICC(k)
4681 
4682    Notes:
4683    Probably really in-place only when level of fill is zero, otherwise allocates
4684    new space to store factored matrix and deletes previous memory.
4685 
4686    Most users should employ the simplified SLES interface for linear solvers
4687    instead of working directly with matrix algebra routines such as this.
4688    See, e.g., SLESCreate().
4689 
4690    Level: developer
4691 
4692    Concepts: matrices^incomplete Cholesky factorization
4693    Concepts: Cholesky factorization
4694 
4695 .seealso: MatICCFactorSymbolic(), MatLUFactorNumeric(), MatCholeskyFactor()
4696 @*/
4697 int MatICCFactor(Mat mat,IS row,PetscReal fill,int level)
4698 {
4699   int ierr;
4700 
4701   PetscFunctionBegin;
4702   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4703   PetscValidType(mat);
4704   MatPreallocated(mat);
4705   if (mat->M != mat->N) SETERRQ(PETSC_ERR_ARG_WRONG,"matrix must be square");
4706   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
4707   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
4708   if (!mat->ops->iccfactor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4709   ierr = (*mat->ops->iccfactor)(mat,row,fill,level);CHKERRQ(ierr);
4710   PetscFunctionReturn(0);
4711 }
4712 
4713 #undef __FUNCT__
4714 #define __FUNCT__ "MatSetValuesAdic"
4715 /*@
4716    MatSetValuesAdic - Sets values computed with ADIC automatic differentiation into a matrix.
4717 
4718    Not Collective
4719 
4720    Input Parameters:
4721 +  mat - the matrix
4722 -  v - the values compute with ADIC
4723 
4724    Level: developer
4725 
4726    Notes:
4727      Must call MatSetColoring() before using this routine. Also this matrix must already
4728      have its nonzero pattern determined.
4729 
4730 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4731           MatSetValues(), MatSetColoring(), MatSetValuesAdifor()
4732 @*/
4733 int MatSetValuesAdic(Mat mat,void *v)
4734 {
4735   int ierr;
4736 
4737   PetscFunctionBegin;
4738   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4739   PetscValidType(mat);
4740 
4741   if (!mat->assembled) {
4742     SETERRQ(1,"Matrix must be already assembled");
4743   }
4744   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4745   if (!mat->ops->setvaluesadic) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4746   ierr = (*mat->ops->setvaluesadic)(mat,v);CHKERRQ(ierr);
4747   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4748   ierr = MatView_Private(mat);CHKERRQ(ierr);
4749   PetscFunctionReturn(0);
4750 }
4751 
4752 
4753 #undef __FUNCT__
4754 #define __FUNCT__ "MatSetColoring"
4755 /*@
4756    MatSetColoring - Sets a coloring used by calls to MatSetValuesAdic()
4757 
4758    Not Collective
4759 
4760    Input Parameters:
4761 +  mat - the matrix
4762 -  coloring - the coloring
4763 
4764    Level: developer
4765 
4766 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4767           MatSetValues(), MatSetValuesAdic()
4768 @*/
4769 int MatSetColoring(Mat mat,ISColoring coloring)
4770 {
4771   int ierr;
4772 
4773   PetscFunctionBegin;
4774   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4775   PetscValidType(mat);
4776 
4777   if (!mat->assembled) {
4778     SETERRQ(1,"Matrix must be already assembled");
4779   }
4780   if (!mat->ops->setcoloring) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4781   ierr = (*mat->ops->setcoloring)(mat,coloring);CHKERRQ(ierr);
4782   PetscFunctionReturn(0);
4783 }
4784 
4785 #undef __FUNCT__
4786 #define __FUNCT__ "MatSetValuesAdifor"
4787 /*@
4788    MatSetValuesAdifor - Sets values computed with automatic differentiation into a matrix.
4789 
4790    Not Collective
4791 
4792    Input Parameters:
4793 +  mat - the matrix
4794 .  nl - leading dimension of v
4795 -  v - the values compute with ADIFOR
4796 
4797    Level: developer
4798 
4799    Notes:
4800      Must call MatSetColoring() before using this routine. Also this matrix must already
4801      have its nonzero pattern determined.
4802 
4803 .seealso: MatSetOption(), MatAssemblyBegin(), MatAssemblyEnd(), MatSetValuesBlocked(), MatSetValuesLocal(),
4804           MatSetValues(), MatSetColoring()
4805 @*/
4806 int MatSetValuesAdifor(Mat mat,int nl,void *v)
4807 {
4808   int ierr;
4809 
4810   PetscFunctionBegin;
4811   PetscValidHeaderSpecific(mat,MAT_COOKIE);
4812   PetscValidType(mat);
4813 
4814   if (!mat->assembled) {
4815     SETERRQ(1,"Matrix must be already assembled");
4816   }
4817   ierr = PetscLogEventBegin(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4818   if (!mat->ops->setvaluesadifor) SETERRQ1(PETSC_ERR_SUP,"Mat type %s",mat->type_name);
4819   ierr = (*mat->ops->setvaluesadifor)(mat,nl,v);CHKERRQ(ierr);
4820   ierr = PetscLogEventEnd(MAT_SetValues,mat,0,0,0);CHKERRQ(ierr);
4821   PetscFunctionReturn(0);
4822 }
4823