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