xref: /petsc/src/mat/impls/aij/seq/aij.c (revision 1df811f522d1893ddffa0df88c65695ed063c3d8)
1992c00aaSSatish Balay #define PETSCMAT_DLL
2b3cc6726SBarry Smith 
3b377110cSBarry Smith 
4d5d45c9bSBarry Smith /*
53369ce9aSBarry Smith     Defines the basic matrix operations for the AIJ (compressed row)
6d5d45c9bSBarry Smith   matrix storage format.
7d5d45c9bSBarry Smith */
83369ce9aSBarry Smith 
97c4f633dSBarry Smith 
107c4f633dSBarry Smith #include "../src/mat/impls/aij/seq/aij.h"          /*I "petscmat.h" I*/
110a835dfdSSatish Balay #include "petscbt.h"
1217ab2063SBarry Smith 
134a2ae208SSatish Balay #undef __FUNCT__
1479299369SBarry Smith #define __FUNCT__ "MatDiagonalSet_SeqAIJ"
1579299369SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
1679299369SBarry Smith {
1779299369SBarry Smith   PetscErrorCode ierr;
1879299369SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) Y->data;
19d0f46423SBarry Smith   PetscInt       i,*diag, m = Y->rmap->n;
2054f21887SBarry Smith   MatScalar      *aa = aij->a;
2154f21887SBarry Smith   PetscScalar    *v;
2209f38230SBarry Smith   PetscTruth     missing;
2379299369SBarry Smith 
2479299369SBarry Smith   PetscFunctionBegin;
2509f38230SBarry Smith   if (Y->assembled) {
2609f38230SBarry Smith     ierr = MatMissingDiagonal_SeqAIJ(Y,&missing,PETSC_NULL);CHKERRQ(ierr);
2709f38230SBarry Smith     if (!missing) {
2879299369SBarry Smith       diag = aij->diag;
2979299369SBarry Smith       ierr = VecGetArray(D,&v);CHKERRQ(ierr);
3079299369SBarry Smith       if (is == INSERT_VALUES) {
3179299369SBarry Smith 	for (i=0; i<m; i++) {
3279299369SBarry Smith 	  aa[diag[i]] = v[i];
3379299369SBarry Smith 	}
3479299369SBarry Smith       } else {
3579299369SBarry Smith 	for (i=0; i<m; i++) {
3679299369SBarry Smith 	  aa[diag[i]] += v[i];
3779299369SBarry Smith 	}
3879299369SBarry Smith       }
3979299369SBarry Smith       ierr = VecRestoreArray(D,&v);CHKERRQ(ierr);
4079299369SBarry Smith       PetscFunctionReturn(0);
4179299369SBarry Smith     }
4209f38230SBarry Smith   }
4309f38230SBarry Smith   ierr = MatDiagonalSet_Default(Y,D,is);CHKERRQ(ierr);
4409f38230SBarry Smith   PetscFunctionReturn(0);
4509f38230SBarry Smith }
4679299369SBarry Smith 
4779299369SBarry Smith #undef __FUNCT__
484a2ae208SSatish Balay #define __FUNCT__ "MatGetRowIJ_SeqAIJ"
493acb8795SBarry Smith PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *m,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
5017ab2063SBarry Smith {
51416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
52dfbe8321SBarry Smith   PetscErrorCode ierr;
5397f1f81fSBarry Smith   PetscInt       i,ishift;
5417ab2063SBarry Smith 
553a40ed3dSBarry Smith   PetscFunctionBegin;
56d0f46423SBarry Smith   *m     = A->rmap->n;
573a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
58bfeeae90SHong Zhang   ishift = 0;
5953e63a63SBarry Smith   if (symmetric && !A->structurally_symmetric) {
60d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,ishift,oshift,ia,ja);CHKERRQ(ierr);
61bfeeae90SHong Zhang   } else if (oshift == 1) {
62d0f46423SBarry Smith     PetscInt nz = a->i[A->rmap->n];
633b2fbd54SBarry Smith     /* malloc space and  add 1 to i and j indices */
64d0f46423SBarry Smith     ierr = PetscMalloc((A->rmap->n+1)*sizeof(PetscInt),ia);CHKERRQ(ierr);
65d0f46423SBarry Smith     for (i=0; i<A->rmap->n+1; i++) (*ia)[i] = a->i[i] + 1;
66ecc77c7aSBarry Smith     if (ja) {
6797f1f81fSBarry Smith       ierr = PetscMalloc((nz+1)*sizeof(PetscInt),ja);CHKERRQ(ierr);
683b2fbd54SBarry Smith       for (i=0; i<nz; i++) (*ja)[i] = a->j[i] + 1;
69ecc77c7aSBarry Smith     }
706945ee14SBarry Smith   } else {
71ecc77c7aSBarry Smith     *ia = a->i;
72ecc77c7aSBarry Smith     if (ja) *ja = a->j;
73a2ce50c7SBarry Smith   }
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
75a2744918SBarry Smith }
76a2744918SBarry Smith 
774a2ae208SSatish Balay #undef __FUNCT__
784a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRowIJ_SeqAIJ"
793acb8795SBarry Smith PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
806945ee14SBarry Smith {
81dfbe8321SBarry Smith   PetscErrorCode ierr;
826945ee14SBarry Smith 
833a40ed3dSBarry Smith   PetscFunctionBegin;
843a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
85bfeeae90SHong Zhang   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
86606d414cSSatish Balay     ierr = PetscFree(*ia);CHKERRQ(ierr);
87ecc77c7aSBarry Smith     if (ja) {ierr = PetscFree(*ja);CHKERRQ(ierr);}
88bcd2baecSBarry Smith   }
893a40ed3dSBarry Smith   PetscFunctionReturn(0);
9017ab2063SBarry Smith }
9117ab2063SBarry Smith 
924a2ae208SSatish Balay #undef __FUNCT__
934a2ae208SSatish Balay #define __FUNCT__ "MatGetColumnIJ_SeqAIJ"
943acb8795SBarry Smith PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *nn,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
953b2fbd54SBarry Smith {
963b2fbd54SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
97dfbe8321SBarry Smith   PetscErrorCode ierr;
98d0f46423SBarry Smith   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
9997f1f81fSBarry Smith   PetscInt       nz = a->i[m],row,*jj,mr,col;
1003b2fbd54SBarry Smith 
1013a40ed3dSBarry Smith   PetscFunctionBegin;
102899cda47SBarry Smith   *nn = n;
1033a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1043b2fbd54SBarry Smith   if (symmetric) {
105d0f46423SBarry Smith     ierr = MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,0,oshift,ia,ja);CHKERRQ(ierr);
1063b2fbd54SBarry Smith   } else {
10797f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&collengths);CHKERRQ(ierr);
10897f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
10997f1f81fSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(PetscInt),&cia);CHKERRQ(ierr);
11097f1f81fSBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscInt),&cja);CHKERRQ(ierr);
1113b2fbd54SBarry Smith     jj = a->j;
1123b2fbd54SBarry Smith     for (i=0; i<nz; i++) {
113bfeeae90SHong Zhang       collengths[jj[i]]++;
1143b2fbd54SBarry Smith     }
1153b2fbd54SBarry Smith     cia[0] = oshift;
1163b2fbd54SBarry Smith     for (i=0; i<n; i++) {
1173b2fbd54SBarry Smith       cia[i+1] = cia[i] + collengths[i];
1183b2fbd54SBarry Smith     }
11997f1f81fSBarry Smith     ierr = PetscMemzero(collengths,n*sizeof(PetscInt));CHKERRQ(ierr);
1203b2fbd54SBarry Smith     jj   = a->j;
121a93ec695SBarry Smith     for (row=0; row<m; row++) {
122a93ec695SBarry Smith       mr = a->i[row+1] - a->i[row];
123a93ec695SBarry Smith       for (i=0; i<mr; i++) {
124bfeeae90SHong Zhang         col = *jj++;
1253b2fbd54SBarry Smith         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
1263b2fbd54SBarry Smith       }
1273b2fbd54SBarry Smith     }
128606d414cSSatish Balay     ierr = PetscFree(collengths);CHKERRQ(ierr);
1293b2fbd54SBarry Smith     *ia = cia; *ja = cja;
1303b2fbd54SBarry Smith   }
1313a40ed3dSBarry Smith   PetscFunctionReturn(0);
1323b2fbd54SBarry Smith }
1333b2fbd54SBarry Smith 
1344a2ae208SSatish Balay #undef __FUNCT__
1354a2ae208SSatish Balay #define __FUNCT__ "MatRestoreColumnIJ_SeqAIJ"
1363acb8795SBarry Smith PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscTruth symmetric,PetscTruth inodecompressed,PetscInt *n,PetscInt *ia[],PetscInt *ja[],PetscTruth *done)
1373b2fbd54SBarry Smith {
138dfbe8321SBarry Smith   PetscErrorCode ierr;
139606d414cSSatish Balay 
1403a40ed3dSBarry Smith   PetscFunctionBegin;
1413a40ed3dSBarry Smith   if (!ia) PetscFunctionReturn(0);
1423b2fbd54SBarry Smith 
143606d414cSSatish Balay   ierr = PetscFree(*ia);CHKERRQ(ierr);
144606d414cSSatish Balay   ierr = PetscFree(*ja);CHKERRQ(ierr);
1453b2fbd54SBarry Smith 
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473b2fbd54SBarry Smith }
1483b2fbd54SBarry Smith 
14987d4246cSBarry Smith #undef __FUNCT__
15087d4246cSBarry Smith #define __FUNCT__ "MatSetValuesRow_SeqAIJ"
15187d4246cSBarry Smith PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
15287d4246cSBarry Smith {
15387d4246cSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
15487d4246cSBarry Smith   PetscInt       *ai = a->i;
15587d4246cSBarry Smith   PetscErrorCode ierr;
15687d4246cSBarry Smith 
15787d4246cSBarry Smith   PetscFunctionBegin;
15887d4246cSBarry Smith   ierr = PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));CHKERRQ(ierr);
15987d4246cSBarry Smith   PetscFunctionReturn(0);
16087d4246cSBarry Smith }
16187d4246cSBarry Smith 
162227d817aSBarry Smith #define CHUNKSIZE   15
16317ab2063SBarry Smith 
1644a2ae208SSatish Balay #undef __FUNCT__
1654a2ae208SSatish Balay #define __FUNCT__ "MatSetValues_SeqAIJ"
16697f1f81fSBarry Smith PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
16717ab2063SBarry Smith {
168416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
169e2ee6c50SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
17097f1f81fSBarry Smith   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
1716849ba73SBarry Smith   PetscErrorCode ierr;
172e2ee6c50SBarry Smith   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
17354f21887SBarry Smith   MatScalar      *ap,value,*aa = a->a;
174edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
175273d9f13SBarry Smith   PetscTruth     roworiented = a->roworiented;
17617ab2063SBarry Smith 
1773a40ed3dSBarry Smith   PetscFunctionBegin;
17871fd2e92SBarry Smith   if (v) PetscValidScalarPointer(v,6);
17917ab2063SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
180416022c9SBarry Smith     row  = im[k];
1815ef9f2a5SBarry Smith     if (row < 0) continue;
1822515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
183d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
1843b2fbd54SBarry Smith #endif
185bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
18617ab2063SBarry Smith     rmax = imax[row]; nrow = ailen[row];
187416022c9SBarry Smith     low  = 0;
188c71e6ed7SBarry Smith     high = nrow;
18917ab2063SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
1905ef9f2a5SBarry Smith       if (in[l] < 0) continue;
1912515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
192d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
1933b2fbd54SBarry Smith #endif
194bfeeae90SHong Zhang       col = in[l];
19516371a99SBarry Smith       if (v) {
1964b0e389bSBarry Smith 	if (roworiented) {
1975ef9f2a5SBarry Smith 	  value = v[l + k*n];
198bef8e0ddSBarry Smith 	} else {
1994b0e389bSBarry Smith 	  value = v[k + l*m];
2004b0e389bSBarry Smith 	}
20116371a99SBarry Smith       } else {
20216371a99SBarry Smith         value = 0;
20316371a99SBarry Smith       }
204abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
20536db0b34SBarry Smith 
2067cd84e04SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
207e2ee6c50SBarry Smith       lastcol = col;
208416022c9SBarry Smith       while (high-low > 5) {
209416022c9SBarry Smith         t = (low+high)/2;
210416022c9SBarry Smith         if (rp[t] > col) high = t;
211416022c9SBarry Smith         else             low  = t;
21217ab2063SBarry Smith       }
213416022c9SBarry Smith       for (i=low; i<high; i++) {
21417ab2063SBarry Smith         if (rp[i] > col) break;
21517ab2063SBarry Smith         if (rp[i] == col) {
216416022c9SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
21717ab2063SBarry Smith           else                  ap[i] = value;
218e44c0bd4SBarry Smith           low = i + 1;
21917ab2063SBarry Smith           goto noinsert;
22017ab2063SBarry Smith         }
22117ab2063SBarry Smith       }
222abc0a331SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
223c2653b3dSLois Curfman McInnes       if (nonew == 1) goto noinsert;
224cc72174dSBarry Smith       if (nonew == -1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
225d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
226c03d1d03SSatish Balay       N = nrow++ - 1; a->nz++; high++;
227416022c9SBarry Smith       /* shift up all the later entries in this row */
228416022c9SBarry Smith       for (ii=N; ii>=i; ii--) {
22917ab2063SBarry Smith         rp[ii+1] = rp[ii];
23017ab2063SBarry Smith         ap[ii+1] = ap[ii];
23117ab2063SBarry Smith       }
23217ab2063SBarry Smith       rp[i] = col;
23317ab2063SBarry Smith       ap[i] = value;
234416022c9SBarry Smith       low   = i + 1;
235e44c0bd4SBarry Smith       noinsert:;
23617ab2063SBarry Smith     }
23717ab2063SBarry Smith     ailen[row] = nrow;
23817ab2063SBarry Smith   }
23988e51ccdSHong Zhang   A->same_nonzero = PETSC_FALSE;
2403a40ed3dSBarry Smith   PetscFunctionReturn(0);
24117ab2063SBarry Smith }
24217ab2063SBarry Smith 
24381824310SBarry Smith 
2444a2ae208SSatish Balay #undef __FUNCT__
2454a2ae208SSatish Balay #define __FUNCT__ "MatGetValues_SeqAIJ"
246a77337e4SBarry Smith PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
2477eb43aa7SLois Curfman McInnes {
2487eb43aa7SLois Curfman McInnes   Mat_SeqAIJ   *a = (Mat_SeqAIJ*)A->data;
24997f1f81fSBarry Smith   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
25097f1f81fSBarry Smith   PetscInt     *ai = a->i,*ailen = a->ilen;
25154f21887SBarry Smith   MatScalar    *ap,*aa = a->a;
2527eb43aa7SLois Curfman McInnes 
2533a40ed3dSBarry Smith   PetscFunctionBegin;
2547eb43aa7SLois Curfman McInnes   for (k=0; k<m; k++) { /* loop over rows */
2557eb43aa7SLois Curfman McInnes     row  = im[k];
25697e567efSBarry Smith     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
257d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
258bfeeae90SHong Zhang     rp   = aj + ai[row]; ap = aa + ai[row];
2597eb43aa7SLois Curfman McInnes     nrow = ailen[row];
2607eb43aa7SLois Curfman McInnes     for (l=0; l<n; l++) { /* loop over columns */
26197e567efSBarry Smith       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
262d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
263bfeeae90SHong Zhang       col = in[l] ;
2647eb43aa7SLois Curfman McInnes       high = nrow; low = 0; /* assume unsorted */
2657eb43aa7SLois Curfman McInnes       while (high-low > 5) {
2667eb43aa7SLois Curfman McInnes         t = (low+high)/2;
2677eb43aa7SLois Curfman McInnes         if (rp[t] > col) high = t;
2687eb43aa7SLois Curfman McInnes         else             low  = t;
2697eb43aa7SLois Curfman McInnes       }
2707eb43aa7SLois Curfman McInnes       for (i=low; i<high; i++) {
2717eb43aa7SLois Curfman McInnes         if (rp[i] > col) break;
2727eb43aa7SLois Curfman McInnes         if (rp[i] == col) {
273b49de8d1SLois Curfman McInnes           *v++ = ap[i];
2747eb43aa7SLois Curfman McInnes           goto finished;
2757eb43aa7SLois Curfman McInnes         }
2767eb43aa7SLois Curfman McInnes       }
27797e567efSBarry Smith       *v++ = 0.0;
2787eb43aa7SLois Curfman McInnes       finished:;
2797eb43aa7SLois Curfman McInnes     }
2807eb43aa7SLois Curfman McInnes   }
2813a40ed3dSBarry Smith   PetscFunctionReturn(0);
2827eb43aa7SLois Curfman McInnes }
2837eb43aa7SLois Curfman McInnes 
28417ab2063SBarry Smith 
2854a2ae208SSatish Balay #undef __FUNCT__
2864a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Binary"
287dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
28817ab2063SBarry Smith {
289416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2906849ba73SBarry Smith   PetscErrorCode ierr;
2916f69ff64SBarry Smith   PetscInt       i,*col_lens;
2926f69ff64SBarry Smith   int            fd;
29317ab2063SBarry Smith 
2943a40ed3dSBarry Smith   PetscFunctionBegin;
295b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
296d0f46423SBarry Smith   ierr = PetscMalloc((4+A->rmap->n)*sizeof(PetscInt),&col_lens);CHKERRQ(ierr);
297552e946dSBarry Smith   col_lens[0] = MAT_FILE_COOKIE;
298d0f46423SBarry Smith   col_lens[1] = A->rmap->n;
299d0f46423SBarry Smith   col_lens[2] = A->cmap->n;
300416022c9SBarry Smith   col_lens[3] = a->nz;
301416022c9SBarry Smith 
302416022c9SBarry Smith   /* store lengths of each row and write (including header) to file */
303d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
304416022c9SBarry Smith     col_lens[4+i] = a->i[i+1] - a->i[i];
30517ab2063SBarry Smith   }
306d0f46423SBarry Smith   ierr = PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);CHKERRQ(ierr);
307606d414cSSatish Balay   ierr = PetscFree(col_lens);CHKERRQ(ierr);
308416022c9SBarry Smith 
309416022c9SBarry Smith   /* store column indices (zero start index) */
3106f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
311416022c9SBarry Smith 
312416022c9SBarry Smith   /* store nonzero values */
3136f69ff64SBarry Smith   ierr = PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);CHKERRQ(ierr);
3143a40ed3dSBarry Smith   PetscFunctionReturn(0);
31517ab2063SBarry Smith }
316416022c9SBarry Smith 
317dfbe8321SBarry Smith EXTERN PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);
318cd155464SBarry Smith 
3194a2ae208SSatish Balay #undef __FUNCT__
3204a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_ASCII"
321dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
322416022c9SBarry Smith {
323416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
324dfbe8321SBarry Smith   PetscErrorCode    ierr;
325d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,shift=0;
326e060cb09SBarry Smith   const char        *name;
327f3ef73ceSBarry Smith   PetscViewerFormat format;
32817ab2063SBarry Smith 
3293a40ed3dSBarry Smith   PetscFunctionBegin;
330435da068SBarry Smith   ierr = PetscObjectGetName((PetscObject)A,&name);CHKERRQ(ierr);
331b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
33271c2f376SKris Buschelman   if (format == PETSC_VIEWER_ASCII_MATLAB) {
33397f1f81fSBarry Smith     PetscInt nofinalvalue = 0;
334d0f46423SBarry Smith     if ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-!shift)) {
335d00d2cf4SBarry Smith       nofinalvalue = 1;
336d00d2cf4SBarry Smith     }
337b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
338d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);CHKERRQ(ierr);
33977431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);CHKERRQ(ierr);
34077431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);CHKERRQ(ierr);
341b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"zzz = [\n");CHKERRQ(ierr);
34217ab2063SBarry Smith 
34317ab2063SBarry Smith     for (i=0; i<m; i++) {
344416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
345aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
34677431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e + %18.16ei \n",i+1,a->j[j]+!shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
34717ab2063SBarry Smith #else
34877431f27SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+!shift,a->a[j]);CHKERRQ(ierr);
34917ab2063SBarry Smith #endif
35017ab2063SBarry Smith       }
35117ab2063SBarry Smith     }
352d00d2cf4SBarry Smith     if (nofinalvalue) {
353d0f46423SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);CHKERRQ(ierr);
354d00d2cf4SBarry Smith     }
355fb9695e5SSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);CHKERRQ(ierr);
356b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
35768369a75SKris Buschelman   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
358cd155464SBarry Smith      PetscFunctionReturn(0);
359fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
360b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
36144cd7ae7SLois Curfman McInnes     for (i=0; i<m; i++) {
36277431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
36344cd7ae7SLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
364aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
36536db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
366a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36736db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
368a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
36936db0b34SBarry Smith         } else if (PetscRealPart(a->a[j]) != 0.0) {
370a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
3716831982aSBarry Smith         }
37244cd7ae7SLois Curfman McInnes #else
373a83599f4SBarry Smith         if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);}
37444cd7ae7SLois Curfman McInnes #endif
37544cd7ae7SLois Curfman McInnes       }
376b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
37744cd7ae7SLois Curfman McInnes     }
378b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
379fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
38097f1f81fSBarry Smith     PetscInt nzd=0,fshift=1,*sptr;
381b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
38297f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&sptr);CHKERRQ(ierr);
383496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
384496be53dSLois Curfman McInnes       sptr[i] = nzd+1;
385496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
386496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
387aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
38836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
389496be53dSLois Curfman McInnes #else
390496be53dSLois Curfman McInnes           if (a->a[j] != 0.0) nzd++;
391496be53dSLois Curfman McInnes #endif
392496be53dSLois Curfman McInnes         }
393496be53dSLois Curfman McInnes       }
394496be53dSLois Curfman McInnes     }
3952e44a96cSLois Curfman McInnes     sptr[m] = nzd+1;
39677431f27SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);CHKERRQ(ierr);
3972e44a96cSLois Curfman McInnes     for (i=0; i<m+1; i+=6) {
39877431f27SBarry Smith       if (i+4<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);CHKERRQ(ierr);}
39977431f27SBarry Smith       else if (i+3<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);CHKERRQ(ierr);}
40077431f27SBarry Smith       else if (i+2<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);CHKERRQ(ierr);}
40177431f27SBarry Smith       else if (i+1<m) {ierr = PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);CHKERRQ(ierr);}
40277431f27SBarry Smith       else if (i<m)   {ierr = PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);CHKERRQ(ierr);}
40377431f27SBarry Smith       else            {ierr = PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);CHKERRQ(ierr);}
404496be53dSLois Curfman McInnes     }
405b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
406606d414cSSatish Balay     ierr = PetscFree(sptr);CHKERRQ(ierr);
407496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
408496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
40977431f27SBarry Smith         if (a->j[j] >= i) {ierr = PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);CHKERRQ(ierr);}
410496be53dSLois Curfman McInnes       }
411b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
412496be53dSLois Curfman McInnes     }
413b0a32e0cSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
414496be53dSLois Curfman McInnes     for (i=0; i<m; i++) {
415496be53dSLois Curfman McInnes       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
416496be53dSLois Curfman McInnes         if (a->j[j] >= i) {
417aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
41836db0b34SBarry Smith           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
419b0a32e0cSBarry Smith             ierr = PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4206831982aSBarry Smith           }
421496be53dSLois Curfman McInnes #else
422b0a32e0cSBarry Smith           if (a->a[j] != 0.0) {ierr = PetscViewerASCIIPrintf(viewer," %18.16e ",a->a[j]);CHKERRQ(ierr);}
423496be53dSLois Curfman McInnes #endif
424496be53dSLois Curfman McInnes         }
425496be53dSLois Curfman McInnes       }
426b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
427496be53dSLois Curfman McInnes     }
428b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
429fb9695e5SSatish Balay   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
43097f1f81fSBarry Smith     PetscInt         cnt = 0,jcnt;
43187828ca2SBarry Smith     PetscScalar value;
43202594712SBarry Smith 
433b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
43402594712SBarry Smith     for (i=0; i<m; i++) {
43502594712SBarry Smith       jcnt = 0;
436d0f46423SBarry Smith       for (j=0; j<A->cmap->n; j++) {
437e24b481bSBarry Smith         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
43802594712SBarry Smith           value = a->a[cnt++];
439e24b481bSBarry Smith           jcnt++;
44002594712SBarry Smith         } else {
44102594712SBarry Smith           value = 0.0;
44202594712SBarry Smith         }
443aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
444b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",PetscRealPart(value),PetscImaginaryPart(value));CHKERRQ(ierr);
44502594712SBarry Smith #else
446b0a32e0cSBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," %7.5e ",value);CHKERRQ(ierr);
44702594712SBarry Smith #endif
44802594712SBarry Smith       }
449b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
45002594712SBarry Smith     }
451b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4523c215bfdSMatthew Knepley   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
4533c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
4543c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4553c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix complex general\n");CHKERRQ(ierr);
4563c215bfdSMatthew Knepley #else
4573c215bfdSMatthew Knepley     ierr = PetscViewerASCIIPrintf(viewer,"%%matrix real general\n");CHKERRQ(ierr);
4583c215bfdSMatthew Knepley #endif
459d0f46423SBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);CHKERRQ(ierr);
4603c215bfdSMatthew Knepley     for (i=0; i<m; i++) {
4613c215bfdSMatthew Knepley       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
4623c215bfdSMatthew Knepley #if defined(PETSC_USE_COMPLEX)
4633c215bfdSMatthew Knepley         if (PetscImaginaryPart(a->a[j]) > 0.0) {
4643c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4653c215bfdSMatthew Knepley         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
4663c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G -%G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4673c215bfdSMatthew Knepley         } else {
4683c215bfdSMatthew Knepley           ierr = PetscViewerASCIIPrintf(viewer,"%D %D, %G\n", i+shift,a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
4693c215bfdSMatthew Knepley         }
4703c215bfdSMatthew Knepley #else
4713c215bfdSMatthew Knepley         ierr = PetscViewerASCIIPrintf(viewer,"%D %D %G\n", i+shift, a->j[j]+shift, a->a[j]);CHKERRQ(ierr);
4723c215bfdSMatthew Knepley #endif
4733c215bfdSMatthew Knepley       }
4743c215bfdSMatthew Knepley     }
4753c215bfdSMatthew Knepley     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
4763a40ed3dSBarry Smith   } else {
477b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_NO);CHKERRQ(ierr);
47817ab2063SBarry Smith     for (i=0; i<m; i++) {
47977431f27SBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"row %D:",i);CHKERRQ(ierr);
480416022c9SBarry Smith       for (j=a->i[i]+shift; j<a->i[i+1]+shift; j++) {
481aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
48236db0b34SBarry Smith         if (PetscImaginaryPart(a->a[j]) > 0.0) {
483a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G + %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
48436db0b34SBarry Smith         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
485a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G - %G i)",a->j[j]+shift,PetscRealPart(a->a[j]),-PetscImaginaryPart(a->a[j]));CHKERRQ(ierr);
4863a40ed3dSBarry Smith         } else {
487a83599f4SBarry Smith           ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,PetscRealPart(a->a[j]));CHKERRQ(ierr);
48817ab2063SBarry Smith         }
48917ab2063SBarry Smith #else
490a83599f4SBarry Smith         ierr = PetscViewerASCIIPrintf(viewer," (%D, %G) ",a->j[j]+shift,a->a[j]);CHKERRQ(ierr);
49117ab2063SBarry Smith #endif
49217ab2063SBarry Smith       }
493b0a32e0cSBarry Smith       ierr = PetscViewerASCIIPrintf(viewer,"\n");CHKERRQ(ierr);
49417ab2063SBarry Smith     }
495b0a32e0cSBarry Smith     ierr = PetscViewerASCIIUseTabs(viewer,PETSC_YES);CHKERRQ(ierr);
49617ab2063SBarry Smith   }
497b0a32e0cSBarry Smith   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
4983a40ed3dSBarry Smith   PetscFunctionReturn(0);
499416022c9SBarry Smith }
500416022c9SBarry Smith 
5014a2ae208SSatish Balay #undef __FUNCT__
5024a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw_Zoom"
503dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
504416022c9SBarry Smith {
505480ef9eaSBarry Smith   Mat               A = (Mat) Aa;
506416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
507dfbe8321SBarry Smith   PetscErrorCode    ierr;
508d0f46423SBarry Smith   PetscInt          i,j,m = A->rmap->n,color;
50936db0b34SBarry Smith   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r,maxv = 0.0;
510b0a32e0cSBarry Smith   PetscViewer       viewer;
511f3ef73ceSBarry Smith   PetscViewerFormat format;
512cddf8d76SBarry Smith 
5133a40ed3dSBarry Smith   PetscFunctionBegin;
514480ef9eaSBarry Smith   ierr = PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);CHKERRQ(ierr);
515b0a32e0cSBarry Smith   ierr = PetscViewerGetFormat(viewer,&format);CHKERRQ(ierr);
51619bcc07fSBarry Smith 
517b0a32e0cSBarry Smith   ierr = PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);CHKERRQ(ierr);
518416022c9SBarry Smith   /* loop over matrix elements drawing boxes */
5190513a670SBarry Smith 
520fb9695e5SSatish Balay   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
5210513a670SBarry Smith     /* Blue for negative, Cyan for zero and  Red for positive */
522b0a32e0cSBarry Smith     color = PETSC_DRAW_BLUE;
523416022c9SBarry Smith     for (i=0; i<m; i++) {
524cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
525bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
526bfeeae90SHong Zhang         x_l = a->j[j] ; x_r = x_l + 1.0;
527aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
52836db0b34SBarry Smith         if (PetscRealPart(a->a[j]) >=  0.) continue;
529cddf8d76SBarry Smith #else
530cddf8d76SBarry Smith         if (a->a[j] >=  0.) continue;
531cddf8d76SBarry Smith #endif
532b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
533cddf8d76SBarry Smith       }
534cddf8d76SBarry Smith     }
535b0a32e0cSBarry Smith     color = PETSC_DRAW_CYAN;
536cddf8d76SBarry Smith     for (i=0; i<m; i++) {
537cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
538bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
539bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
540cddf8d76SBarry Smith         if (a->a[j] !=  0.) continue;
541b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
542cddf8d76SBarry Smith       }
543cddf8d76SBarry Smith     }
544b0a32e0cSBarry Smith     color = PETSC_DRAW_RED;
545cddf8d76SBarry Smith     for (i=0; i<m; i++) {
546cddf8d76SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
547bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
548bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
549aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
55036db0b34SBarry Smith         if (PetscRealPart(a->a[j]) <=  0.) continue;
551cddf8d76SBarry Smith #else
552cddf8d76SBarry Smith         if (a->a[j] <=  0.) continue;
553cddf8d76SBarry Smith #endif
554b0a32e0cSBarry Smith         ierr = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
555416022c9SBarry Smith       }
556416022c9SBarry Smith     }
5570513a670SBarry Smith   } else {
5580513a670SBarry Smith     /* use contour shading to indicate magnitude of values */
5590513a670SBarry Smith     /* first determine max of all nonzero values */
56097f1f81fSBarry Smith     PetscInt    nz = a->nz,count;
561b0a32e0cSBarry Smith     PetscDraw   popup;
56236db0b34SBarry Smith     PetscReal scale;
5630513a670SBarry Smith 
5640513a670SBarry Smith     for (i=0; i<nz; i++) {
5650513a670SBarry Smith       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
5660513a670SBarry Smith     }
567b0a32e0cSBarry Smith     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
568b0a32e0cSBarry Smith     ierr  = PetscDrawGetPopup(draw,&popup);CHKERRQ(ierr);
569b0a32e0cSBarry Smith     if (popup) {ierr  = PetscDrawScalePopup(popup,0.0,maxv);CHKERRQ(ierr);}
5700513a670SBarry Smith     count = 0;
5710513a670SBarry Smith     for (i=0; i<m; i++) {
5720513a670SBarry Smith       y_l = m - i - 1.0; y_r = y_l + 1.0;
573bfeeae90SHong Zhang       for (j=a->i[i]; j<a->i[i+1]; j++) {
574bfeeae90SHong Zhang         x_l = a->j[j]; x_r = x_l + 1.0;
57597f1f81fSBarry Smith         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
576b0a32e0cSBarry Smith         ierr  = PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);CHKERRQ(ierr);
5770513a670SBarry Smith         count++;
5780513a670SBarry Smith       }
5790513a670SBarry Smith     }
5800513a670SBarry Smith   }
581480ef9eaSBarry Smith   PetscFunctionReturn(0);
582480ef9eaSBarry Smith }
583cddf8d76SBarry Smith 
5844a2ae208SSatish Balay #undef __FUNCT__
5854a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ_Draw"
586dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
587480ef9eaSBarry Smith {
588dfbe8321SBarry Smith   PetscErrorCode ierr;
589b0a32e0cSBarry Smith   PetscDraw      draw;
59036db0b34SBarry Smith   PetscReal      xr,yr,xl,yl,h,w;
591480ef9eaSBarry Smith   PetscTruth     isnull;
592480ef9eaSBarry Smith 
593480ef9eaSBarry Smith   PetscFunctionBegin;
594b0a32e0cSBarry Smith   ierr = PetscViewerDrawGetDraw(viewer,0,&draw);CHKERRQ(ierr);
595b0a32e0cSBarry Smith   ierr = PetscDrawIsNull(draw,&isnull);CHKERRQ(ierr);
596480ef9eaSBarry Smith   if (isnull) PetscFunctionReturn(0);
597480ef9eaSBarry Smith 
598480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);CHKERRQ(ierr);
599d0f46423SBarry Smith   xr  = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
600480ef9eaSBarry Smith   xr += w;    yr += h;  xl = -w;     yl = -h;
601b0a32e0cSBarry Smith   ierr = PetscDrawSetCoordinates(draw,xl,yl,xr,yr);CHKERRQ(ierr);
602b0a32e0cSBarry Smith   ierr = PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);CHKERRQ(ierr);
603480ef9eaSBarry Smith   ierr = PetscObjectCompose((PetscObject)A,"Zoomviewer",PETSC_NULL);CHKERRQ(ierr);
6043a40ed3dSBarry Smith   PetscFunctionReturn(0);
605416022c9SBarry Smith }
606416022c9SBarry Smith 
6074a2ae208SSatish Balay #undef __FUNCT__
6084a2ae208SSatish Balay #define __FUNCT__ "MatView_SeqAIJ"
609dfbe8321SBarry Smith PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
610416022c9SBarry Smith {
611dfbe8321SBarry Smith   PetscErrorCode ierr;
6126805f65bSBarry Smith   PetscTruth     iascii,isbinary,isdraw;
613416022c9SBarry Smith 
6143a40ed3dSBarry Smith   PetscFunctionBegin;
61532077d6dSBarry Smith   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr);
616fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_BINARY,&isbinary);CHKERRQ(ierr);
617fb9695e5SSatish Balay   ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_DRAW,&isdraw);CHKERRQ(ierr);
618c45a1595SBarry Smith   if (iascii) {
6193a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_ASCII(A,viewer);CHKERRQ(ierr);
6200f5bd95cSBarry Smith   } else if (isbinary) {
6213a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Binary(A,viewer);CHKERRQ(ierr);
6220f5bd95cSBarry Smith   } else if (isdraw) {
6233a40ed3dSBarry Smith     ierr = MatView_SeqAIJ_Draw(A,viewer);CHKERRQ(ierr);
6245cd90555SBarry Smith   } else {
625958c9bccSBarry Smith     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported by SeqAIJ matrices",((PetscObject)viewer)->type_name);
62617ab2063SBarry Smith   }
6274108e4d5SBarry Smith   ierr = MatView_SeqAIJ_Inode(A,viewer);CHKERRQ(ierr);
6283a40ed3dSBarry Smith   PetscFunctionReturn(0);
62917ab2063SBarry Smith }
63019bcc07fSBarry Smith 
6314a2ae208SSatish Balay #undef __FUNCT__
6324a2ae208SSatish Balay #define __FUNCT__ "MatAssemblyEnd_SeqAIJ"
633dfbe8321SBarry Smith PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
63417ab2063SBarry Smith {
635416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
6366849ba73SBarry Smith   PetscErrorCode ierr;
63797f1f81fSBarry Smith   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
638d0f46423SBarry Smith   PetscInt       m = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
63954f21887SBarry Smith   MatScalar      *aa = a->a,*ap;
6403447b6efSHong Zhang   PetscReal      ratio=0.6;
64117ab2063SBarry Smith 
6423a40ed3dSBarry Smith   PetscFunctionBegin;
6433a40ed3dSBarry Smith   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(0);
64417ab2063SBarry Smith 
64543ee02c3SBarry Smith   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
64617ab2063SBarry Smith   for (i=1; i<m; i++) {
647416022c9SBarry Smith     /* move each row back by the amount of empty slots (fshift) before it*/
64817ab2063SBarry Smith     fshift += imax[i-1] - ailen[i-1];
64994a9d846SBarry Smith     rmax   = PetscMax(rmax,ailen[i]);
65017ab2063SBarry Smith     if (fshift) {
651bfeeae90SHong Zhang       ip = aj + ai[i] ;
652bfeeae90SHong Zhang       ap = aa + ai[i] ;
65317ab2063SBarry Smith       N  = ailen[i];
65417ab2063SBarry Smith       for (j=0; j<N; j++) {
65517ab2063SBarry Smith         ip[j-fshift] = ip[j];
65617ab2063SBarry Smith         ap[j-fshift] = ap[j];
65717ab2063SBarry Smith       }
65817ab2063SBarry Smith     }
65917ab2063SBarry Smith     ai[i] = ai[i-1] + ailen[i-1];
66017ab2063SBarry Smith   }
66117ab2063SBarry Smith   if (m) {
66217ab2063SBarry Smith     fshift += imax[m-1] - ailen[m-1];
66317ab2063SBarry Smith     ai[m]  = ai[m-1] + ailen[m-1];
66417ab2063SBarry Smith   }
66517ab2063SBarry Smith   /* reset ilen and imax for each row */
66617ab2063SBarry Smith   for (i=0; i<m; i++) {
66717ab2063SBarry Smith     ailen[i] = imax[i] = ai[i+1] - ai[i];
66817ab2063SBarry Smith   }
669bfeeae90SHong Zhang   a->nz = ai[m];
67028b2fa4aSMatthew Knepley   if (fshift && a->nounused == -1) {
67128b2fa4aSMatthew Knepley     SETERRQ3(PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);
67228b2fa4aSMatthew Knepley   }
67317ab2063SBarry Smith 
67409f38230SBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
675d0f46423SBarry Smith   ierr = PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);CHKERRQ(ierr);
676ae15b995SBarry Smith   ierr = PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);CHKERRQ(ierr);
677ae15b995SBarry Smith   ierr = PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);CHKERRQ(ierr);
678a0e1a203SBarry Smith 
679dd5f02e7SSatish Balay   a->reallocs          = 0;
6804e220ebcSLois Curfman McInnes   A->info.nz_unneeded  = (double)fshift;
68136db0b34SBarry Smith   a->rmax              = rmax;
6824e220ebcSLois Curfman McInnes 
683cb5d8e9eSHong Zhang   /* check for zero rows. If found a large number of zero rows, use CompressedRow functions */
684317fbc4cSHong Zhang   ierr = Mat_CheckCompressedRow(A,&a->compressedrow,a->i,m,ratio);CHKERRQ(ierr);
68588e51ccdSHong Zhang   A->same_nonzero = PETSC_TRUE;
68671c2f376SKris Buschelman 
6874108e4d5SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ_Inode(A,mode);CHKERRQ(ierr);
68871f1c65dSBarry Smith 
68971f1c65dSBarry Smith   a->idiagvalid = PETSC_FALSE;
6903a40ed3dSBarry Smith   PetscFunctionReturn(0);
69117ab2063SBarry Smith }
69217ab2063SBarry Smith 
6934a2ae208SSatish Balay #undef __FUNCT__
69499cafbc1SBarry Smith #define __FUNCT__ "MatRealPart_SeqAIJ"
69599cafbc1SBarry Smith PetscErrorCode MatRealPart_SeqAIJ(Mat A)
69699cafbc1SBarry Smith {
69799cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
69899cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
69954f21887SBarry Smith   MatScalar      *aa = a->a;
70099cafbc1SBarry Smith 
70199cafbc1SBarry Smith   PetscFunctionBegin;
70299cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
70399cafbc1SBarry Smith   PetscFunctionReturn(0);
70499cafbc1SBarry Smith }
70599cafbc1SBarry Smith 
70699cafbc1SBarry Smith #undef __FUNCT__
70799cafbc1SBarry Smith #define __FUNCT__ "MatImaginaryPart_SeqAIJ"
70899cafbc1SBarry Smith PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
70999cafbc1SBarry Smith {
71099cafbc1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
71199cafbc1SBarry Smith   PetscInt       i,nz = a->nz;
71254f21887SBarry Smith   MatScalar      *aa = a->a;
71399cafbc1SBarry Smith 
71499cafbc1SBarry Smith   PetscFunctionBegin;
71599cafbc1SBarry Smith   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
71699cafbc1SBarry Smith   PetscFunctionReturn(0);
71799cafbc1SBarry Smith }
71899cafbc1SBarry Smith 
71999cafbc1SBarry Smith #undef __FUNCT__
7204a2ae208SSatish Balay #define __FUNCT__ "MatZeroEntries_SeqAIJ"
721dfbe8321SBarry Smith PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
72217ab2063SBarry Smith {
723416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
724dfbe8321SBarry Smith   PetscErrorCode ierr;
7253a40ed3dSBarry Smith 
7263a40ed3dSBarry Smith   PetscFunctionBegin;
727d0f46423SBarry Smith   ierr = PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
7283a40ed3dSBarry Smith   PetscFunctionReturn(0);
72917ab2063SBarry Smith }
730416022c9SBarry Smith 
7314a2ae208SSatish Balay #undef __FUNCT__
7324a2ae208SSatish Balay #define __FUNCT__ "MatDestroy_SeqAIJ"
733dfbe8321SBarry Smith PetscErrorCode MatDestroy_SeqAIJ(Mat A)
73417ab2063SBarry Smith {
735416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
736dfbe8321SBarry Smith   PetscErrorCode ierr;
737d5d45c9bSBarry Smith 
7383a40ed3dSBarry Smith   PetscFunctionBegin;
739aa482453SBarry Smith #if defined(PETSC_USE_LOG)
740d0f46423SBarry Smith   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
74117ab2063SBarry Smith #endif
742e6b907acSBarry Smith   ierr = MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);CHKERRQ(ierr);
743c38d4ed2SBarry Smith   if (a->row) {
744c38d4ed2SBarry Smith     ierr = ISDestroy(a->row);CHKERRQ(ierr);
745c38d4ed2SBarry Smith   }
746c38d4ed2SBarry Smith   if (a->col) {
747c38d4ed2SBarry Smith     ierr = ISDestroy(a->col);CHKERRQ(ierr);
748c38d4ed2SBarry Smith   }
74905b42c5fSBarry Smith   ierr = PetscFree(a->diag);CHKERRQ(ierr);
75005b42c5fSBarry Smith   ierr = PetscFree2(a->imax,a->ilen);CHKERRQ(ierr);
75171f1c65dSBarry Smith   ierr = PetscFree3(a->idiag,a->mdiag,a->ssor_work);CHKERRQ(ierr);
75205b42c5fSBarry Smith   ierr = PetscFree(a->solve_work);CHKERRQ(ierr);
75382bf6240SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);}
75405b42c5fSBarry Smith   ierr = PetscFree(a->saved_values);CHKERRQ(ierr);
755cc8ba8e1SBarry Smith   if (a->coloring) {ierr = ISColoringDestroy(a->coloring);CHKERRQ(ierr);}
75605b42c5fSBarry Smith   ierr = PetscFree(a->xtoy);CHKERRQ(ierr);
757407f6b05SHong Zhang   if (a->XtoY) {ierr = MatDestroy(a->XtoY);CHKERRQ(ierr);}
758d487561eSHong Zhang   if (a->compressedrow.use){ierr = PetscFree(a->compressedrow.i);}
759a30b2313SHong Zhang 
7604108e4d5SBarry Smith   ierr = MatDestroy_SeqAIJ_Inode(A);CHKERRQ(ierr);
7614846f1f5SKris Buschelman 
762606d414cSSatish Balay   ierr = PetscFree(a);CHKERRQ(ierr);
763901853e0SKris Buschelman 
764dbd8c25aSHong Zhang   ierr = PetscObjectChangeTypeName((PetscObject)A,0);CHKERRQ(ierr);
765901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetColumnIndices_C","",PETSC_NULL);CHKERRQ(ierr);
766901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatStoreValues_C","",PETSC_NULL);CHKERRQ(ierr);
767901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatRetrieveValues_C","",PETSC_NULL);CHKERRQ(ierr);
768901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqsbaij_C","",PETSC_NULL);CHKERRQ(ierr);
769901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqbaij_C","",PETSC_NULL);CHKERRQ(ierr);
770ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatConvert_seqaij_seqcsrperm_C","",PETSC_NULL);CHKERRQ(ierr);
771901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatIsTranspose_C","",PETSC_NULL);CHKERRQ(ierr);
772901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocation_C","",PETSC_NULL);CHKERRQ(ierr);
773a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C","",PETSC_NULL);CHKERRQ(ierr);
774901853e0SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)A,"MatReorderForNonzeroDiagonal_C","",PETSC_NULL);CHKERRQ(ierr);
7753a40ed3dSBarry Smith   PetscFunctionReturn(0);
77617ab2063SBarry Smith }
77717ab2063SBarry Smith 
7784a2ae208SSatish Balay #undef __FUNCT__
7794a2ae208SSatish Balay #define __FUNCT__ "MatSetOption_SeqAIJ"
7804e0d8c25SBarry Smith PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscTruth flg)
78117ab2063SBarry Smith {
782416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
7834846f1f5SKris Buschelman   PetscErrorCode ierr;
7843a40ed3dSBarry Smith 
7853a40ed3dSBarry Smith   PetscFunctionBegin;
786a65d3064SKris Buschelman   switch (op) {
787a65d3064SKris Buschelman     case MAT_ROW_ORIENTED:
7884e0d8c25SBarry Smith       a->roworiented       = flg;
789a65d3064SKris Buschelman       break;
790a9817697SBarry Smith     case MAT_KEEP_NONZERO_PATTERN:
791a9817697SBarry Smith       a->keepnonzeropattern    = flg;
792a65d3064SKris Buschelman       break;
793512a5fc5SBarry Smith     case MAT_NEW_NONZERO_LOCATIONS:
794512a5fc5SBarry Smith       a->nonew             = (flg ? 0 : 1);
795a65d3064SKris Buschelman       break;
796a65d3064SKris Buschelman     case MAT_NEW_NONZERO_LOCATION_ERR:
7974e0d8c25SBarry Smith       a->nonew             = (flg ? -1 : 0);
798a65d3064SKris Buschelman       break;
799a65d3064SKris Buschelman     case MAT_NEW_NONZERO_ALLOCATION_ERR:
8004e0d8c25SBarry Smith       a->nonew             = (flg ? -2 : 0);
801a65d3064SKris Buschelman       break;
80228b2fa4aSMatthew Knepley     case MAT_UNUSED_NONZERO_LOCATION_ERR:
80328b2fa4aSMatthew Knepley       a->nounused          = (flg ? -1 : 0);
80428b2fa4aSMatthew Knepley       break;
805a65d3064SKris Buschelman     case MAT_IGNORE_ZERO_ENTRIES:
8064e0d8c25SBarry Smith       a->ignorezeroentries = flg;
8070df259c2SBarry Smith       break;
808d487561eSHong Zhang     case MAT_USE_COMPRESSEDROW:
8094e0d8c25SBarry Smith       a->compressedrow.use = flg;
810d487561eSHong Zhang       break;
8114e0d8c25SBarry Smith     case MAT_NEW_DIAGONALS:
812a65d3064SKris Buschelman     case MAT_IGNORE_OFF_PROC_ENTRIES:
813a65d3064SKris Buschelman     case MAT_USE_HASH_TABLE:
814290bbb0aSBarry Smith       ierr = PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);CHKERRQ(ierr);
815a65d3064SKris Buschelman       break;
816a65d3064SKris Buschelman     default:
81771c2f376SKris Buschelman       break;
818a65d3064SKris Buschelman   }
8194108e4d5SBarry Smith   ierr = MatSetOption_SeqAIJ_Inode(A,op,flg);CHKERRQ(ierr);
8203a40ed3dSBarry Smith   PetscFunctionReturn(0);
82117ab2063SBarry Smith }
82217ab2063SBarry Smith 
8234a2ae208SSatish Balay #undef __FUNCT__
8244a2ae208SSatish Balay #define __FUNCT__ "MatGetDiagonal_SeqAIJ"
825dfbe8321SBarry Smith PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
82617ab2063SBarry Smith {
827416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
8286849ba73SBarry Smith   PetscErrorCode ierr;
82997f1f81fSBarry Smith   PetscInt       i,j,n;
83087828ca2SBarry Smith   PetscScalar    *x,zero = 0.0;
83117ab2063SBarry Smith 
8323a40ed3dSBarry Smith   PetscFunctionBegin;
8332dcb1b2aSMatthew Knepley   ierr = VecSet(v,zero);CHKERRQ(ierr);
8341ebc52fbSHong Zhang   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
83536db0b34SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
836d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
837d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
838bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
839bfeeae90SHong Zhang       if (a->j[j] == i) {
840416022c9SBarry Smith         x[i] = a->a[j];
84117ab2063SBarry Smith         break;
84217ab2063SBarry Smith       }
84317ab2063SBarry Smith     }
84417ab2063SBarry Smith   }
8451ebc52fbSHong Zhang   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
8463a40ed3dSBarry Smith   PetscFunctionReturn(0);
84717ab2063SBarry Smith }
84817ab2063SBarry Smith 
849b377110cSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
8504a2ae208SSatish Balay #undef __FUNCT__
8514a2ae208SSatish Balay #define __FUNCT__ "MatMultTransposeAdd_SeqAIJ"
852dfbe8321SBarry Smith PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
85317ab2063SBarry Smith {
854416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
8555c897100SBarry Smith   PetscScalar       *x,*y;
856dfbe8321SBarry Smith   PetscErrorCode    ierr;
857d0f46423SBarry Smith   PetscInt          m = A->rmap->n;
8585c897100SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
859a77337e4SBarry Smith   MatScalar         *v;
860a77337e4SBarry Smith   PetscScalar       alpha;
86104fbf559SBarry Smith   PetscInt          n,i,j,*idx,*ii,*ridx=PETSC_NULL;
8623447b6efSHong Zhang   Mat_CompressedRow cprow = a->compressedrow;
8634eb6d288SHong Zhang   PetscTruth        usecprow = cprow.use;
8645c897100SBarry Smith #endif
86517ab2063SBarry Smith 
8663a40ed3dSBarry Smith   PetscFunctionBegin;
8672e8a6d31SBarry Smith   if (zz != yy) {ierr = VecCopy(zz,yy);CHKERRQ(ierr);}
8681ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
8691ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
8705c897100SBarry Smith 
8715c897100SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
872bfeeae90SHong Zhang   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
8735c897100SBarry Smith #else
8743447b6efSHong Zhang   if (usecprow){
8753447b6efSHong Zhang     m    = cprow.nrows;
8763447b6efSHong Zhang     ii   = cprow.i;
8777b2bb3b9SHong Zhang     ridx = cprow.rindex;
8783447b6efSHong Zhang   } else {
8793447b6efSHong Zhang     ii = a->i;
8803447b6efSHong Zhang   }
88117ab2063SBarry Smith   for (i=0; i<m; i++) {
8823447b6efSHong Zhang     idx   = a->j + ii[i] ;
8833447b6efSHong Zhang     v     = a->a + ii[i] ;
8843447b6efSHong Zhang     n     = ii[i+1] - ii[i];
8853447b6efSHong Zhang     if (usecprow){
8867b2bb3b9SHong Zhang       alpha = x[ridx[i]];
8873447b6efSHong Zhang     } else {
88817ab2063SBarry Smith       alpha = x[i];
8893447b6efSHong Zhang     }
89004fbf559SBarry Smith     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
89117ab2063SBarry Smith   }
8925c897100SBarry Smith #endif
893dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
8941ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
8951ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
8963a40ed3dSBarry Smith   PetscFunctionReturn(0);
89717ab2063SBarry Smith }
89817ab2063SBarry Smith 
8994a2ae208SSatish Balay #undef __FUNCT__
9005c897100SBarry Smith #define __FUNCT__ "MatMultTranspose_SeqAIJ"
901dfbe8321SBarry Smith PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
9025c897100SBarry Smith {
903dfbe8321SBarry Smith   PetscErrorCode ierr;
9045c897100SBarry Smith 
9055c897100SBarry Smith   PetscFunctionBegin;
906170fe5c8SBarry Smith   ierr = VecSet(yy,0.0);CHKERRQ(ierr);
9075c897100SBarry Smith   ierr = MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);CHKERRQ(ierr);
9085c897100SBarry Smith   PetscFunctionReturn(0);
9095c897100SBarry Smith }
9105c897100SBarry Smith 
911a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmult.h"
9125c897100SBarry Smith #undef __FUNCT__
9134a2ae208SSatish Balay #define __FUNCT__ "MatMult_SeqAIJ"
914dfbe8321SBarry Smith PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
91517ab2063SBarry Smith {
916416022c9SBarry Smith   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
917d9fead3dSBarry Smith   PetscScalar       *y;
91854f21887SBarry Smith   const PetscScalar *x;
91954f21887SBarry Smith   const MatScalar   *aa;
920dfbe8321SBarry Smith   PetscErrorCode    ierr;
921003131ecSBarry Smith   PetscInt          m=A->rmap->n;
922003131ecSBarry Smith   const PetscInt    *aj,*ii,*ridx=PETSC_NULL;
9238aee2decSHong Zhang   PetscInt          n,i,nonzerorow=0;
924362ced78SSatish Balay   PetscScalar       sum;
92597952fefSHong Zhang   PetscTruth        usecprow=a->compressedrow.use;
92617ab2063SBarry Smith 
927b6410449SSatish Balay #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
92897952fefSHong Zhang #pragma disjoint(*x,*y,*aa)
929fee21e36SBarry Smith #endif
930fee21e36SBarry Smith 
9313a40ed3dSBarry Smith   PetscFunctionBegin;
932d9fead3dSBarry Smith   ierr = VecGetArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9331ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
93497952fefSHong Zhang   aj  = a->j;
93597952fefSHong Zhang   aa  = a->a;
936416022c9SBarry Smith   ii  = a->i;
9374eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
93897952fefSHong Zhang     m    = a->compressedrow.nrows;
93997952fefSHong Zhang     ii   = a->compressedrow.i;
94097952fefSHong Zhang     ridx = a->compressedrow.rindex;
94197952fefSHong Zhang     for (i=0; i<m; i++){
94297952fefSHong Zhang       n   = ii[i+1] - ii[i];
94397952fefSHong Zhang       aj  = a->j + ii[i];
94497952fefSHong Zhang       aa  = a->a + ii[i];
94597952fefSHong Zhang       sum = 0.0;
946a46b3154SVictor Eijkhout       nonzerorow += (n>0);
947003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
948003131ecSBarry Smith       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
94997952fefSHong Zhang       y[*ridx++] = sum;
95097952fefSHong Zhang     }
95197952fefSHong Zhang   } else { /* do not use compressed row format */
952b05257ddSBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
953b05257ddSBarry Smith     fortranmultaij_(&m,x,ii,aj,aa,y);
954b05257ddSBarry Smith #else
95517ab2063SBarry Smith     for (i=0; i<m; i++) {
956003131ecSBarry Smith       n   = ii[i+1] - ii[i];
957003131ecSBarry Smith       aj  = a->j + ii[i];
958003131ecSBarry Smith       aa  = a->a + ii[i];
95917ab2063SBarry Smith       sum  = 0.0;
960a46b3154SVictor Eijkhout       nonzerorow += (n>0);
961003131ecSBarry Smith       PetscSparseDensePlusDot(sum,x,aa,aj,n);
96217ab2063SBarry Smith       y[i] = sum;
96317ab2063SBarry Smith     }
9648d195f9aSBarry Smith #endif
965b05257ddSBarry Smith   }
966dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz - nonzerorow);CHKERRQ(ierr);
967d9fead3dSBarry Smith   ierr = VecRestoreArray(xx,(PetscScalar**)&x);CHKERRQ(ierr);
9681ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
9693a40ed3dSBarry Smith   PetscFunctionReturn(0);
97017ab2063SBarry Smith }
97117ab2063SBarry Smith 
972a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h"
9734a2ae208SSatish Balay #undef __FUNCT__
9744a2ae208SSatish Balay #define __FUNCT__ "MatMultAdd_SeqAIJ"
975dfbe8321SBarry Smith PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
97617ab2063SBarry Smith {
977416022c9SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
97854f21887SBarry Smith   PetscScalar     *x,*y,*z;
97954f21887SBarry Smith   const MatScalar *aa;
980dfbe8321SBarry Smith   PetscErrorCode  ierr;
981d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*aj,*ii;
982aa482453SBarry Smith #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
98397952fefSHong Zhang   PetscInt        n,i,jrow,j,*ridx=PETSC_NULL;
984362ced78SSatish Balay   PetscScalar     sum;
98597952fefSHong Zhang   PetscTruth      usecprow=a->compressedrow.use;
986e36a17ebSSatish Balay #endif
9879ea0dfa2SSatish Balay 
9883a40ed3dSBarry Smith   PetscFunctionBegin;
9891ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
9901ebc52fbSHong Zhang   ierr = VecGetArray(yy,&y);CHKERRQ(ierr);
9912e8a6d31SBarry Smith   if (zz != yy) {
9921ebc52fbSHong Zhang     ierr = VecGetArray(zz,&z);CHKERRQ(ierr);
9932e8a6d31SBarry Smith   } else {
9942e8a6d31SBarry Smith     z = y;
9952e8a6d31SBarry Smith   }
996bfeeae90SHong Zhang 
99797952fefSHong Zhang   aj  = a->j;
99897952fefSHong Zhang   aa  = a->a;
999cddf8d76SBarry Smith   ii  = a->i;
1000aa482453SBarry Smith #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
100197952fefSHong Zhang   fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
100202ab625aSSatish Balay #else
10034eb6d288SHong Zhang   if (usecprow){ /* use compressed row format */
10044eb6d288SHong Zhang     if (zz != yy){
10054eb6d288SHong Zhang       ierr = PetscMemcpy(z,y,m*sizeof(PetscScalar));CHKERRQ(ierr);
10064eb6d288SHong Zhang     }
100797952fefSHong Zhang     m    = a->compressedrow.nrows;
100897952fefSHong Zhang     ii   = a->compressedrow.i;
100997952fefSHong Zhang     ridx = a->compressedrow.rindex;
101097952fefSHong Zhang     for (i=0; i<m; i++){
101197952fefSHong Zhang       n  = ii[i+1] - ii[i];
101297952fefSHong Zhang       aj  = a->j + ii[i];
101397952fefSHong Zhang       aa  = a->a + ii[i];
101497952fefSHong Zhang       sum = y[*ridx];
101597952fefSHong Zhang       for (j=0; j<n; j++) sum += (*aa++)*x[*aj++];
101697952fefSHong Zhang       z[*ridx++] = sum;
101797952fefSHong Zhang     }
101897952fefSHong Zhang   } else { /* do not use compressed row format */
101917ab2063SBarry Smith     for (i=0; i<m; i++) {
10209ea0dfa2SSatish Balay       jrow = ii[i];
10219ea0dfa2SSatish Balay       n    = ii[i+1] - jrow;
102217ab2063SBarry Smith       sum  = y[i];
10239ea0dfa2SSatish Balay       for (j=0; j<n; j++) {
102497952fefSHong Zhang         sum += aa[jrow]*x[aj[jrow]]; jrow++;
10259ea0dfa2SSatish Balay       }
102617ab2063SBarry Smith       z[i] = sum;
102717ab2063SBarry Smith     }
102897952fefSHong Zhang   }
102902ab625aSSatish Balay #endif
1030dc0b31edSSatish Balay   ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
10311ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
10321ebc52fbSHong Zhang   ierr = VecRestoreArray(yy,&y);CHKERRQ(ierr);
10332e8a6d31SBarry Smith   if (zz != yy) {
10341ebc52fbSHong Zhang     ierr = VecRestoreArray(zz,&z);CHKERRQ(ierr);
10352e8a6d31SBarry Smith   }
10363a40ed3dSBarry Smith   PetscFunctionReturn(0);
103717ab2063SBarry Smith }
103817ab2063SBarry Smith 
103917ab2063SBarry Smith /*
104017ab2063SBarry Smith      Adds diagonal pointers to sparse matrix structure.
104117ab2063SBarry Smith */
10424a2ae208SSatish Balay #undef __FUNCT__
10434a2ae208SSatish Balay #define __FUNCT__ "MatMarkDiagonal_SeqAIJ"
1044dfbe8321SBarry Smith PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
104517ab2063SBarry Smith {
1046416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
10476849ba73SBarry Smith   PetscErrorCode ierr;
1048d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n;
104917ab2063SBarry Smith 
10503a40ed3dSBarry Smith   PetscFunctionBegin;
105109f38230SBarry Smith   if (!a->diag) {
105209f38230SBarry Smith     ierr = PetscMalloc(m*sizeof(PetscInt),&a->diag);CHKERRQ(ierr);
10539518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(A, m*sizeof(PetscInt));CHKERRQ(ierr);
105409f38230SBarry Smith   }
1055d0f46423SBarry Smith   for (i=0; i<A->rmap->n; i++) {
105609f38230SBarry Smith     a->diag[i] = a->i[i+1];
1057bfeeae90SHong Zhang     for (j=a->i[i]; j<a->i[i+1]; j++) {
1058bfeeae90SHong Zhang       if (a->j[j] == i) {
105909f38230SBarry Smith         a->diag[i] = j;
106017ab2063SBarry Smith         break;
106117ab2063SBarry Smith       }
106217ab2063SBarry Smith     }
106317ab2063SBarry Smith   }
10643a40ed3dSBarry Smith   PetscFunctionReturn(0);
106517ab2063SBarry Smith }
106617ab2063SBarry Smith 
1067be5855fcSBarry Smith /*
1068be5855fcSBarry Smith      Checks for missing diagonals
1069be5855fcSBarry Smith */
10704a2ae208SSatish Balay #undef __FUNCT__
10714a2ae208SSatish Balay #define __FUNCT__ "MatMissingDiagonal_SeqAIJ"
107209f38230SBarry Smith PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscTruth *missing,PetscInt *d)
1073be5855fcSBarry Smith {
1074be5855fcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
107597f1f81fSBarry Smith   PetscInt       *diag,*jj = a->j,i;
1076be5855fcSBarry Smith 
1077be5855fcSBarry Smith   PetscFunctionBegin;
107809f38230SBarry Smith   *missing = PETSC_FALSE;
1079d0f46423SBarry Smith   if (A->rmap->n > 0 && !jj) {
108009f38230SBarry Smith     *missing  = PETSC_TRUE;
108109f38230SBarry Smith     if (d) *d = 0;
108209f38230SBarry Smith     PetscInfo(A,"Matrix has no entries therefor is missing diagonal");
108309f38230SBarry Smith   } else {
1084f1e2ffcdSBarry Smith     diag = a->diag;
1085d0f46423SBarry Smith     for (i=0; i<A->rmap->n; i++) {
1086bfeeae90SHong Zhang       if (jj[diag[i]] != i) {
108709f38230SBarry Smith 	*missing = PETSC_TRUE;
108809f38230SBarry Smith 	if (d) *d = i;
108909f38230SBarry Smith 	PetscInfo1(A,"Matrix is missing diagonal number %D",i);
109009f38230SBarry Smith       }
1091be5855fcSBarry Smith     }
1092be5855fcSBarry Smith   }
1093be5855fcSBarry Smith   PetscFunctionReturn(0);
1094be5855fcSBarry Smith }
1095be5855fcSBarry Smith 
109671f1c65dSBarry Smith EXTERN_C_BEGIN
109771f1c65dSBarry Smith #undef __FUNCT__
109871f1c65dSBarry Smith #define __FUNCT__ "MatInvertDiagonal_SeqAIJ"
109971f1c65dSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
110071f1c65dSBarry Smith {
110171f1c65dSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
110271f1c65dSBarry Smith   PetscErrorCode ierr;
1103d0f46423SBarry Smith   PetscInt       i,*diag,m = A->rmap->n;
110454f21887SBarry Smith   MatScalar      *v = a->a;
110554f21887SBarry Smith   PetscScalar    *idiag,*mdiag;
110671f1c65dSBarry Smith 
110771f1c65dSBarry Smith   PetscFunctionBegin;
110871f1c65dSBarry Smith   if (a->idiagvalid) PetscFunctionReturn(0);
110971f1c65dSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(A);CHKERRQ(ierr);
111071f1c65dSBarry Smith   diag = a->diag;
111171f1c65dSBarry Smith   if (!a->idiag) {
111271f1c65dSBarry Smith     ierr     = PetscMalloc3(m,PetscScalar,&a->idiag,m,PetscScalar,&a->mdiag,m,PetscScalar,&a->ssor_work);CHKERRQ(ierr);
111371f1c65dSBarry Smith     ierr     = PetscLogObjectMemory(A, 3*m*sizeof(PetscScalar));CHKERRQ(ierr);
111471f1c65dSBarry Smith     v        = a->a;
111571f1c65dSBarry Smith   }
111671f1c65dSBarry Smith   mdiag = a->mdiag;
111771f1c65dSBarry Smith   idiag = a->idiag;
111871f1c65dSBarry Smith 
1119028cd4eaSSatish Balay   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
112071f1c65dSBarry Smith     for (i=0; i<m; i++) {
112171f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112271f1c65dSBarry Smith       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
112371f1c65dSBarry Smith       idiag[i] = 1.0/v[diag[i]];
112471f1c65dSBarry Smith     }
112571f1c65dSBarry Smith     ierr = PetscLogFlops(m);CHKERRQ(ierr);
112671f1c65dSBarry Smith   } else {
112771f1c65dSBarry Smith     for (i=0; i<m; i++) {
112871f1c65dSBarry Smith       mdiag[i] = v[diag[i]];
112971f1c65dSBarry Smith       idiag[i] = omega/(fshift + v[diag[i]]);
113071f1c65dSBarry Smith     }
1131dc0b31edSSatish Balay     ierr = PetscLogFlops(2.0*m);CHKERRQ(ierr);
113271f1c65dSBarry Smith   }
113371f1c65dSBarry Smith   a->idiagvalid = PETSC_TRUE;
113471f1c65dSBarry Smith   PetscFunctionReturn(0);
113571f1c65dSBarry Smith }
11365a9745a3SMatthew Knepley EXTERN_C_END
113771f1c65dSBarry Smith 
1138a4005a5dSBarry Smith #include "../src/mat/impls/aij/seq/ftn-kernels/frelax.h"
11394a2ae208SSatish Balay #undef __FUNCT__
114041f059aeSBarry Smith #define __FUNCT__ "MatSOR_SeqAIJ"
114141f059aeSBarry Smith PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
114217ab2063SBarry Smith {
1143416022c9SBarry Smith   Mat_SeqAIJ         *a = (Mat_SeqAIJ*)A->data;
1144e6d1f457SBarry Smith   PetscScalar        *x,d,sum,*t,scale;
1145e6d1f457SBarry Smith   const MatScalar    *v = a->a,*idiag=0,*mdiag;
114654f21887SBarry Smith   const PetscScalar  *b, *bs,*xb, *ts;
1147dfbe8321SBarry Smith   PetscErrorCode     ierr;
1148d0f46423SBarry Smith   PetscInt           n = A->cmap->n,m = A->rmap->n,i;
114997f1f81fSBarry Smith   const PetscInt     *idx,*diag;
115017ab2063SBarry Smith 
11513a40ed3dSBarry Smith   PetscFunctionBegin;
1152b965ef7fSBarry Smith   its = its*lits;
115391723122SBarry Smith 
115471f1c65dSBarry Smith   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
115571f1c65dSBarry Smith   if (!a->idiagvalid) {ierr = MatInvertDiagonal_SeqAIJ(A,omega,fshift);CHKERRQ(ierr);}
115671f1c65dSBarry Smith   a->fshift = fshift;
115771f1c65dSBarry Smith   a->omega  = omega;
1158ed480e8bSBarry Smith 
115971f1c65dSBarry Smith   diag = a->diag;
116071f1c65dSBarry Smith   t     = a->ssor_work;
1161ed480e8bSBarry Smith   idiag = a->idiag;
116271f1c65dSBarry Smith   mdiag = a->mdiag;
1163ed480e8bSBarry Smith 
11641ebc52fbSHong Zhang   ierr = VecGetArray(xx,&x);CHKERRQ(ierr);
1165fb2e594dSBarry Smith   if (xx != bb) {
11661ebc52fbSHong Zhang     ierr = VecGetArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);
1167fb2e594dSBarry Smith   } else {
1168fb2e594dSBarry Smith     b = x;
1169fb2e594dSBarry Smith   }
117071f1c65dSBarry Smith   CHKMEMQ;
1171ed480e8bSBarry Smith   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
117217ab2063SBarry Smith   if (flag == SOR_APPLY_UPPER) {
117317ab2063SBarry Smith    /* apply (U + D/omega) to the vector */
1174ed480e8bSBarry Smith     bs = b;
117517ab2063SBarry Smith     for (i=0; i<m; i++) {
117671f1c65dSBarry Smith         d    = fshift + mdiag[i];
1177416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1178ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1179ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
118017ab2063SBarry Smith         sum  = b[i]*d/omega;
1181003131ecSBarry Smith         PetscSparseDensePlusDot(sum,bs,v,idx,n);
118217ab2063SBarry Smith         x[i] = sum;
118317ab2063SBarry Smith     }
11841ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
11851ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
1186efee365bSSatish Balay     ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
11873a40ed3dSBarry Smith     PetscFunctionReturn(0);
118817ab2063SBarry Smith   }
1189c783ea89SBarry Smith 
119048af12d7SBarry Smith   if (flag == SOR_APPLY_LOWER) {
119129bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
11923a40ed3dSBarry Smith   } else if (flag & SOR_EISENSTAT) {
119317ab2063SBarry Smith     /* Let  A = L + U + D; where L is lower trianglar,
1194887ee2caSBarry Smith     U is upper triangular, E = D/omega; This routine applies
119517ab2063SBarry Smith 
119617ab2063SBarry Smith             (L + E)^{-1} A (U + E)^{-1}
119717ab2063SBarry Smith 
1198887ee2caSBarry Smith     to a vector efficiently using Eisenstat's trick.
119917ab2063SBarry Smith     */
120017ab2063SBarry Smith     scale = (2.0/omega) - 1.0;
120117ab2063SBarry Smith 
120217ab2063SBarry Smith     /*  x = (E + U)^{-1} b */
120317ab2063SBarry Smith     for (i=m-1; i>=0; i--) {
1204416022c9SBarry Smith       n    = a->i[i+1] - diag[i] - 1;
1205ed480e8bSBarry Smith       idx  = a->j + diag[i] + 1;
1206ed480e8bSBarry Smith       v    = a->a + diag[i] + 1;
120717ab2063SBarry Smith       sum  = b[i];
1208e6d1f457SBarry Smith       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1209ed480e8bSBarry Smith       x[i] = sum*idiag[i];
121017ab2063SBarry Smith     }
121117ab2063SBarry Smith 
121217ab2063SBarry Smith     /*  t = b - (2*E - D)x */
1213416022c9SBarry Smith     v = a->a;
1214ed480e8bSBarry Smith     for (i=0; i<m; i++) { t[i] = b[i] - scale*(v[*diag++])*x[i]; }
121517ab2063SBarry Smith 
121617ab2063SBarry Smith     /*  t = (E + L)^{-1}t */
1217ed480e8bSBarry Smith     ts = t;
1218416022c9SBarry Smith     diag = a->diag;
121917ab2063SBarry Smith     for (i=0; i<m; i++) {
1220416022c9SBarry Smith       n    = diag[i] - a->i[i];
1221ed480e8bSBarry Smith       idx  = a->j + a->i[i];
1222ed480e8bSBarry Smith       v    = a->a + a->i[i];
122317ab2063SBarry Smith       sum  = t[i];
1224003131ecSBarry Smith       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1225ed480e8bSBarry Smith       t[i] = sum*idiag[i];
1226733d66baSBarry Smith       /*  x = x + t */
1227733d66baSBarry Smith       x[i] += t[i];
122817ab2063SBarry Smith     }
122917ab2063SBarry Smith 
1230dc0b31edSSatish Balay     ierr = PetscLogFlops(6.0*m-1 + 2.0*a->nz);CHKERRQ(ierr);
12311ebc52fbSHong Zhang     ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12321ebc52fbSHong Zhang     if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
12333a40ed3dSBarry Smith     PetscFunctionReturn(0);
123417ab2063SBarry Smith   }
123517ab2063SBarry Smith   if (flag & SOR_ZERO_INITIAL_GUESS) {
123617ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
123717ab2063SBarry Smith       for (i=0; i<m; i++) {
1238416022c9SBarry Smith         n    = diag[i] - a->i[i];
1239ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1240ed480e8bSBarry Smith         v    = a->a + a->i[i];
124117ab2063SBarry Smith         sum  = b[i];
1242e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
12435c99c7daSBarry Smith         t[i] = sum;
1244ed480e8bSBarry Smith         x[i] = sum*idiag[i];
124517ab2063SBarry Smith       }
12465c99c7daSBarry Smith       xb = t;
1247efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
12483a40ed3dSBarry Smith     } else xb = b;
124917ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
125017ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1251416022c9SBarry Smith         n    = a->i[i+1] - diag[i] - 1;
1252ed480e8bSBarry Smith         idx  = a->j + diag[i] + 1;
1253ed480e8bSBarry Smith         v    = a->a + diag[i] + 1;
125417ab2063SBarry Smith         sum  = xb[i];
1255e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
12565c99c7daSBarry Smith         if (xb == b) {
1257ed480e8bSBarry Smith           x[i] = sum*idiag[i];
12585c99c7daSBarry Smith         } else {
12595c99c7daSBarry Smith           x[i] = (1-omega)*x[i] + sum*idiag[i];
126017ab2063SBarry Smith         }
12615c99c7daSBarry Smith       }
1262efee365bSSatish Balay       ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
126317ab2063SBarry Smith     }
126417ab2063SBarry Smith     its--;
126517ab2063SBarry Smith   }
126617ab2063SBarry Smith   while (its--) {
126717ab2063SBarry Smith     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP){
126817ab2063SBarry Smith       for (i=0; i<m; i++) {
1269416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1270ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1271ed480e8bSBarry Smith         v    = a->a + a->i[i];
127217ab2063SBarry Smith         sum  = b[i];
1273e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1274ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
127517ab2063SBarry Smith       }
12769f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
127717ab2063SBarry Smith     }
127817ab2063SBarry Smith     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP){
127917ab2063SBarry Smith       for (i=m-1; i>=0; i--) {
1280416022c9SBarry Smith         n    = a->i[i+1] - a->i[i];
1281ed480e8bSBarry Smith         idx  = a->j + a->i[i];
1282ed480e8bSBarry Smith         v    = a->a + a->i[i];
128317ab2063SBarry Smith         sum  = b[i];
1284e6d1f457SBarry Smith         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1285ed480e8bSBarry Smith         x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
128617ab2063SBarry Smith       }
12879f863219SBarry Smith       ierr = PetscLogFlops(2.0*a->nz);CHKERRQ(ierr);
128817ab2063SBarry Smith     }
128917ab2063SBarry Smith   }
12901ebc52fbSHong Zhang   ierr = VecRestoreArray(xx,&x);CHKERRQ(ierr);
12911ebc52fbSHong Zhang   if (bb != xx) {ierr = VecRestoreArray(bb,(PetscScalar**)&b);CHKERRQ(ierr);}
129271f1c65dSBarry Smith   CHKMEMQ;  PetscFunctionReturn(0);
129317ab2063SBarry Smith }
129417ab2063SBarry Smith 
12952af78befSBarry Smith 
12964a2ae208SSatish Balay #undef __FUNCT__
12974a2ae208SSatish Balay #define __FUNCT__ "MatGetInfo_SeqAIJ"
1298dfbe8321SBarry Smith PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
129917ab2063SBarry Smith {
1300416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
13014e220ebcSLois Curfman McInnes 
13023a40ed3dSBarry Smith   PetscFunctionBegin;
13034e220ebcSLois Curfman McInnes   info->block_size     = 1.0;
13044e220ebcSLois Curfman McInnes   info->nz_allocated   = (double)a->maxnz;
13054e220ebcSLois Curfman McInnes   info->nz_used        = (double)a->nz;
13064e220ebcSLois Curfman McInnes   info->nz_unneeded    = (double)(a->maxnz - a->nz);
13074e220ebcSLois Curfman McInnes   info->assemblies     = (double)A->num_ass;
13084e220ebcSLois Curfman McInnes   info->mallocs        = (double)a->reallocs;
13097adad957SLisandro Dalcin   info->memory         = ((PetscObject)A)->mem;
13104e220ebcSLois Curfman McInnes   if (A->factor) {
13114e220ebcSLois Curfman McInnes     info->fill_ratio_given  = A->info.fill_ratio_given;
13124e220ebcSLois Curfman McInnes     info->fill_ratio_needed = A->info.fill_ratio_needed;
13134e220ebcSLois Curfman McInnes     info->factor_mallocs    = A->info.factor_mallocs;
13144e220ebcSLois Curfman McInnes   } else {
13154e220ebcSLois Curfman McInnes     info->fill_ratio_given  = 0;
13164e220ebcSLois Curfman McInnes     info->fill_ratio_needed = 0;
13174e220ebcSLois Curfman McInnes     info->factor_mallocs    = 0;
13184e220ebcSLois Curfman McInnes   }
13193a40ed3dSBarry Smith   PetscFunctionReturn(0);
132017ab2063SBarry Smith }
132117ab2063SBarry Smith 
13224a2ae208SSatish Balay #undef __FUNCT__
13234a2ae208SSatish Balay #define __FUNCT__ "MatZeroRows_SeqAIJ"
1324f4df32b1SMatthew Knepley PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag)
132517ab2063SBarry Smith {
1326416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1327d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n - 1,d;
13286849ba73SBarry Smith   PetscErrorCode ierr;
132909f38230SBarry Smith   PetscTruth     missing;
133017ab2063SBarry Smith 
13313a40ed3dSBarry Smith   PetscFunctionBegin;
1332a9817697SBarry Smith   if (a->keepnonzeropattern) {
1333f1e2ffcdSBarry Smith     for (i=0; i<N; i++) {
133477431f27SBarry Smith       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1335bfeeae90SHong Zhang       ierr = PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));CHKERRQ(ierr);
1336f1e2ffcdSBarry Smith     }
1337f4df32b1SMatthew Knepley     if (diag != 0.0) {
133809f38230SBarry Smith       ierr = MatMissingDiagonal_SeqAIJ(A,&missing,&d);CHKERRQ(ierr);
133909f38230SBarry Smith       if (missing) SETERRQ1(PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1340f1e2ffcdSBarry Smith       for (i=0; i<N; i++) {
1341f4df32b1SMatthew Knepley         a->a[a->diag[rows[i]]] = diag;
1342f1e2ffcdSBarry Smith       }
1343f1e2ffcdSBarry Smith     }
134488e51ccdSHong Zhang     A->same_nonzero = PETSC_TRUE;
1345f1e2ffcdSBarry Smith   } else {
1346f4df32b1SMatthew Knepley     if (diag != 0.0) {
134717ab2063SBarry Smith       for (i=0; i<N; i++) {
134877431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
13497ae801bdSBarry Smith         if (a->ilen[rows[i]] > 0) {
1350416022c9SBarry Smith           a->ilen[rows[i]]          = 1;
1351f4df32b1SMatthew Knepley           a->a[a->i[rows[i]]] = diag;
1352bfeeae90SHong Zhang           a->j[a->i[rows[i]]] = rows[i];
13537ae801bdSBarry Smith         } else { /* in case row was completely empty */
1354f4df32b1SMatthew Knepley           ierr = MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);CHKERRQ(ierr);
135517ab2063SBarry Smith         }
135617ab2063SBarry Smith       }
13573a40ed3dSBarry Smith     } else {
135817ab2063SBarry Smith       for (i=0; i<N; i++) {
135977431f27SBarry Smith         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1360416022c9SBarry Smith         a->ilen[rows[i]] = 0;
136117ab2063SBarry Smith       }
136217ab2063SBarry Smith     }
136388e51ccdSHong Zhang     A->same_nonzero = PETSC_FALSE;
1364f1e2ffcdSBarry Smith   }
136543a90d84SBarry Smith   ierr = MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
13663a40ed3dSBarry Smith   PetscFunctionReturn(0);
136717ab2063SBarry Smith }
136817ab2063SBarry Smith 
13694a2ae208SSatish Balay #undef __FUNCT__
13704a2ae208SSatish Balay #define __FUNCT__ "MatGetRow_SeqAIJ"
1371a77337e4SBarry Smith PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
137217ab2063SBarry Smith {
1373416022c9SBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
137497f1f81fSBarry Smith   PetscInt   *itmp;
137517ab2063SBarry Smith 
13763a40ed3dSBarry Smith   PetscFunctionBegin;
1377d0f46423SBarry Smith   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);
137817ab2063SBarry Smith 
1379416022c9SBarry Smith   *nz = a->i[row+1] - a->i[row];
1380bfeeae90SHong Zhang   if (v) *v = a->a + a->i[row];
138117ab2063SBarry Smith   if (idx) {
1382bfeeae90SHong Zhang     itmp = a->j + a->i[row];
1383bfeeae90SHong Zhang     if (*nz) {
13844e093b46SBarry Smith       *idx = itmp;
138517ab2063SBarry Smith     }
138617ab2063SBarry Smith     else *idx = 0;
138717ab2063SBarry Smith   }
13883a40ed3dSBarry Smith   PetscFunctionReturn(0);
138917ab2063SBarry Smith }
139017ab2063SBarry Smith 
1391bfeeae90SHong Zhang /* remove this function? */
13924a2ae208SSatish Balay #undef __FUNCT__
13934a2ae208SSatish Balay #define __FUNCT__ "MatRestoreRow_SeqAIJ"
1394a77337e4SBarry Smith PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
139517ab2063SBarry Smith {
13963a40ed3dSBarry Smith   PetscFunctionBegin;
13973a40ed3dSBarry Smith   PetscFunctionReturn(0);
139817ab2063SBarry Smith }
139917ab2063SBarry Smith 
14004a2ae208SSatish Balay #undef __FUNCT__
14014a2ae208SSatish Balay #define __FUNCT__ "MatNorm_SeqAIJ"
1402dfbe8321SBarry Smith PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
140317ab2063SBarry Smith {
1404416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
140554f21887SBarry Smith   MatScalar      *v = a->a;
140636db0b34SBarry Smith   PetscReal      sum = 0.0;
14076849ba73SBarry Smith   PetscErrorCode ierr;
140897f1f81fSBarry Smith   PetscInt       i,j;
140917ab2063SBarry Smith 
14103a40ed3dSBarry Smith   PetscFunctionBegin;
141117ab2063SBarry Smith   if (type == NORM_FROBENIUS) {
1412416022c9SBarry Smith     for (i=0; i<a->nz; i++) {
1413aa482453SBarry Smith #if defined(PETSC_USE_COMPLEX)
141436db0b34SBarry Smith       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
141517ab2063SBarry Smith #else
141617ab2063SBarry Smith       sum += (*v)*(*v); v++;
141717ab2063SBarry Smith #endif
141817ab2063SBarry Smith     }
1419064f8208SBarry Smith     *nrm = sqrt(sum);
14203a40ed3dSBarry Smith   } else if (type == NORM_1) {
142136db0b34SBarry Smith     PetscReal *tmp;
142297f1f81fSBarry Smith     PetscInt    *jj = a->j;
1423d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscReal),&tmp);CHKERRQ(ierr);
1424d0f46423SBarry Smith     ierr = PetscMemzero(tmp,A->cmap->n*sizeof(PetscReal));CHKERRQ(ierr);
1425064f8208SBarry Smith     *nrm = 0.0;
1426416022c9SBarry Smith     for (j=0; j<a->nz; j++) {
1427bfeeae90SHong Zhang         tmp[*jj++] += PetscAbsScalar(*v);  v++;
142817ab2063SBarry Smith     }
1429d0f46423SBarry Smith     for (j=0; j<A->cmap->n; j++) {
1430064f8208SBarry Smith       if (tmp[j] > *nrm) *nrm = tmp[j];
143117ab2063SBarry Smith     }
1432606d414cSSatish Balay     ierr = PetscFree(tmp);CHKERRQ(ierr);
14333a40ed3dSBarry Smith   } else if (type == NORM_INFINITY) {
1434064f8208SBarry Smith     *nrm = 0.0;
1435d0f46423SBarry Smith     for (j=0; j<A->rmap->n; j++) {
1436bfeeae90SHong Zhang       v = a->a + a->i[j];
143717ab2063SBarry Smith       sum = 0.0;
1438416022c9SBarry Smith       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1439cddf8d76SBarry Smith         sum += PetscAbsScalar(*v); v++;
144017ab2063SBarry Smith       }
1441064f8208SBarry Smith       if (sum > *nrm) *nrm = sum;
144217ab2063SBarry Smith     }
14433a40ed3dSBarry Smith   } else {
144429bbc08cSBarry Smith     SETERRQ(PETSC_ERR_SUP,"No support for two norm");
144517ab2063SBarry Smith   }
14463a40ed3dSBarry Smith   PetscFunctionReturn(0);
144717ab2063SBarry Smith }
144817ab2063SBarry Smith 
14494a2ae208SSatish Balay #undef __FUNCT__
14504a2ae208SSatish Balay #define __FUNCT__ "MatTranspose_SeqAIJ"
1451fc4dec0aSBarry Smith PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
145217ab2063SBarry Smith {
1453416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1454416022c9SBarry Smith   Mat            C;
14556849ba73SBarry Smith   PetscErrorCode ierr;
1456d0f46423SBarry Smith   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
145754f21887SBarry Smith   MatScalar      *array = a->a;
145817ab2063SBarry Smith 
14593a40ed3dSBarry Smith   PetscFunctionBegin;
1460d0f46423SBarry Smith   if (reuse == MAT_REUSE_MATRIX && A == *B && m != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");
1461fc4dec0aSBarry Smith 
1462fc4dec0aSBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
1463d0f46423SBarry Smith     ierr = PetscMalloc((1+A->cmap->n)*sizeof(PetscInt),&col);CHKERRQ(ierr);
1464d0f46423SBarry Smith     ierr = PetscMemzero(col,(1+A->cmap->n)*sizeof(PetscInt));CHKERRQ(ierr);
1465bfeeae90SHong Zhang 
1466bfeeae90SHong Zhang     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
14677adad957SLisandro Dalcin     ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1468d0f46423SBarry Smith     ierr = MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);CHKERRQ(ierr);
14697adad957SLisandro Dalcin     ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1470ab93d7beSBarry Smith     ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);CHKERRQ(ierr);
1471606d414cSSatish Balay     ierr = PetscFree(col);CHKERRQ(ierr);
1472a541d17aSBarry Smith   } else {
1473a541d17aSBarry Smith     C = *B;
1474a541d17aSBarry Smith   }
1475a541d17aSBarry Smith 
147617ab2063SBarry Smith   for (i=0; i<m; i++) {
147717ab2063SBarry Smith     len    = ai[i+1]-ai[i];
147887d4246cSBarry Smith     ierr   = MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);CHKERRQ(ierr);
1479b9b97703SBarry Smith     array += len;
1480b9b97703SBarry Smith     aj    += len;
148117ab2063SBarry Smith   }
14826d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
14836d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
148417ab2063SBarry Smith 
1485815cbec1SBarry Smith   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
1486416022c9SBarry Smith     *B = C;
148717ab2063SBarry Smith   } else {
1488273d9f13SBarry Smith     ierr = MatHeaderCopy(A,C);CHKERRQ(ierr);
148917ab2063SBarry Smith   }
14903a40ed3dSBarry Smith   PetscFunctionReturn(0);
149117ab2063SBarry Smith }
149217ab2063SBarry Smith 
1493cd0d46ebSvictorle EXTERN_C_BEGIN
1494cd0d46ebSvictorle #undef __FUNCT__
14955fbd3699SBarry Smith #define __FUNCT__ "MatIsTranspose_SeqAIJ"
1496be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
1497cd0d46ebSvictorle {
1498cd0d46ebSvictorle   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
149954f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
150054f21887SBarry Smith   MatScalar      *va,*vb;
15016849ba73SBarry Smith   PetscErrorCode ierr;
150297f1f81fSBarry Smith   PetscInt       ma,na,mb,nb, i;
1503cd0d46ebSvictorle 
1504cd0d46ebSvictorle   PetscFunctionBegin;
1505cd0d46ebSvictorle   bij = (Mat_SeqAIJ *) B->data;
1506cd0d46ebSvictorle 
1507cd0d46ebSvictorle   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
1508cd0d46ebSvictorle   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15095485867bSBarry Smith   if (ma!=nb || na!=mb){
15105485867bSBarry Smith     *f = PETSC_FALSE;
15115485867bSBarry Smith     PetscFunctionReturn(0);
15125485867bSBarry Smith   }
1513cd0d46ebSvictorle   aii = aij->i; bii = bij->i;
1514cd0d46ebSvictorle   adx = aij->j; bdx = bij->j;
1515cd0d46ebSvictorle   va  = aij->a; vb = bij->a;
151697f1f81fSBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
151797f1f81fSBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
1518cd0d46ebSvictorle   for (i=0; i<ma; i++) aptr[i] = aii[i];
1519cd0d46ebSvictorle   for (i=0; i<mb; i++) bptr[i] = bii[i];
1520cd0d46ebSvictorle 
1521cd0d46ebSvictorle   *f = PETSC_TRUE;
1522cd0d46ebSvictorle   for (i=0; i<ma; i++) {
1523cd0d46ebSvictorle     while (aptr[i]<aii[i+1]) {
152497f1f81fSBarry Smith       PetscInt         idc,idr;
15255485867bSBarry Smith       PetscScalar vc,vr;
1526cd0d46ebSvictorle       /* column/row index/value */
15275485867bSBarry Smith       idc = adx[aptr[i]];
15285485867bSBarry Smith       idr = bdx[bptr[idc]];
15295485867bSBarry Smith       vc  = va[aptr[i]];
15305485867bSBarry Smith       vr  = vb[bptr[idc]];
15315485867bSBarry Smith       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
15325485867bSBarry Smith 	*f = PETSC_FALSE;
15335485867bSBarry Smith         goto done;
1534cd0d46ebSvictorle       } else {
15355485867bSBarry Smith 	aptr[i]++;
15365485867bSBarry Smith         if (B || i!=idc) bptr[idc]++;
1537cd0d46ebSvictorle       }
1538cd0d46ebSvictorle     }
1539cd0d46ebSvictorle   }
1540cd0d46ebSvictorle  done:
1541cd0d46ebSvictorle   ierr = PetscFree(aptr);CHKERRQ(ierr);
15423aeef889SHong Zhang   if (B) {
15433aeef889SHong Zhang     ierr = PetscFree(bptr);CHKERRQ(ierr);
15443aeef889SHong Zhang   }
1545cd0d46ebSvictorle   PetscFunctionReturn(0);
1546cd0d46ebSvictorle }
1547cd0d46ebSvictorle EXTERN_C_END
1548cd0d46ebSvictorle 
15491cbb95d3SBarry Smith EXTERN_C_BEGIN
15501cbb95d3SBarry Smith #undef __FUNCT__
15511cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitianTranspose_SeqAIJ"
15521cbb95d3SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscTruth *f)
15531cbb95d3SBarry Smith {
15541cbb95d3SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *) A->data,*bij = (Mat_SeqAIJ*) A->data;
155554f21887SBarry Smith   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
155654f21887SBarry Smith   MatScalar      *va,*vb;
15571cbb95d3SBarry Smith   PetscErrorCode ierr;
15581cbb95d3SBarry Smith   PetscInt       ma,na,mb,nb, i;
15591cbb95d3SBarry Smith 
15601cbb95d3SBarry Smith   PetscFunctionBegin;
15611cbb95d3SBarry Smith   bij = (Mat_SeqAIJ *) B->data;
15621cbb95d3SBarry Smith 
15631cbb95d3SBarry Smith   ierr = MatGetSize(A,&ma,&na);CHKERRQ(ierr);
15641cbb95d3SBarry Smith   ierr = MatGetSize(B,&mb,&nb);CHKERRQ(ierr);
15651cbb95d3SBarry Smith   if (ma!=nb || na!=mb){
15661cbb95d3SBarry Smith     *f = PETSC_FALSE;
15671cbb95d3SBarry Smith     PetscFunctionReturn(0);
15681cbb95d3SBarry Smith   }
15691cbb95d3SBarry Smith   aii = aij->i; bii = bij->i;
15701cbb95d3SBarry Smith   adx = aij->j; bdx = bij->j;
15711cbb95d3SBarry Smith   va  = aij->a; vb = bij->a;
15721cbb95d3SBarry Smith   ierr = PetscMalloc(ma*sizeof(PetscInt),&aptr);CHKERRQ(ierr);
15731cbb95d3SBarry Smith   ierr = PetscMalloc(mb*sizeof(PetscInt),&bptr);CHKERRQ(ierr);
15741cbb95d3SBarry Smith   for (i=0; i<ma; i++) aptr[i] = aii[i];
15751cbb95d3SBarry Smith   for (i=0; i<mb; i++) bptr[i] = bii[i];
15761cbb95d3SBarry Smith 
15771cbb95d3SBarry Smith   *f = PETSC_TRUE;
15781cbb95d3SBarry Smith   for (i=0; i<ma; i++) {
15791cbb95d3SBarry Smith     while (aptr[i]<aii[i+1]) {
15801cbb95d3SBarry Smith       PetscInt         idc,idr;
15811cbb95d3SBarry Smith       PetscScalar vc,vr;
15821cbb95d3SBarry Smith       /* column/row index/value */
15831cbb95d3SBarry Smith       idc = adx[aptr[i]];
15841cbb95d3SBarry Smith       idr = bdx[bptr[idc]];
15851cbb95d3SBarry Smith       vc  = va[aptr[i]];
15861cbb95d3SBarry Smith       vr  = vb[bptr[idc]];
15871cbb95d3SBarry Smith       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
15881cbb95d3SBarry Smith 	*f = PETSC_FALSE;
15891cbb95d3SBarry Smith         goto done;
15901cbb95d3SBarry Smith       } else {
15911cbb95d3SBarry Smith 	aptr[i]++;
15921cbb95d3SBarry Smith         if (B || i!=idc) bptr[idc]++;
15931cbb95d3SBarry Smith       }
15941cbb95d3SBarry Smith     }
15951cbb95d3SBarry Smith   }
15961cbb95d3SBarry Smith  done:
15971cbb95d3SBarry Smith   ierr = PetscFree(aptr);CHKERRQ(ierr);
15981cbb95d3SBarry Smith   if (B) {
15991cbb95d3SBarry Smith     ierr = PetscFree(bptr);CHKERRQ(ierr);
16001cbb95d3SBarry Smith   }
16011cbb95d3SBarry Smith   PetscFunctionReturn(0);
16021cbb95d3SBarry Smith }
16031cbb95d3SBarry Smith EXTERN_C_END
16041cbb95d3SBarry Smith 
16059e29f15eSvictorle #undef __FUNCT__
16069e29f15eSvictorle #define __FUNCT__ "MatIsSymmetric_SeqAIJ"
1607dfbe8321SBarry Smith PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16089e29f15eSvictorle {
1609dfbe8321SBarry Smith   PetscErrorCode ierr;
16109e29f15eSvictorle   PetscFunctionBegin;
16115485867bSBarry Smith   ierr = MatIsTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16129e29f15eSvictorle   PetscFunctionReturn(0);
16139e29f15eSvictorle }
16149e29f15eSvictorle 
16154a2ae208SSatish Balay #undef __FUNCT__
16161cbb95d3SBarry Smith #define __FUNCT__ "MatIsHermitian_SeqAIJ"
16171cbb95d3SBarry Smith PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscTruth *f)
16181cbb95d3SBarry Smith {
16191cbb95d3SBarry Smith   PetscErrorCode ierr;
16201cbb95d3SBarry Smith   PetscFunctionBegin;
16211cbb95d3SBarry Smith   ierr = MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);CHKERRQ(ierr);
16221cbb95d3SBarry Smith   PetscFunctionReturn(0);
16231cbb95d3SBarry Smith }
16241cbb95d3SBarry Smith 
16251cbb95d3SBarry Smith #undef __FUNCT__
16264a2ae208SSatish Balay #define __FUNCT__ "MatDiagonalScale_SeqAIJ"
1627dfbe8321SBarry Smith PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
162817ab2063SBarry Smith {
1629416022c9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
163054f21887SBarry Smith   PetscScalar    *l,*r,x;
163154f21887SBarry Smith   MatScalar      *v;
1632dfbe8321SBarry Smith   PetscErrorCode ierr;
1633d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;
163417ab2063SBarry Smith 
16353a40ed3dSBarry Smith   PetscFunctionBegin;
163617ab2063SBarry Smith   if (ll) {
16373ea7c6a1SSatish Balay     /* The local size is used so that VecMPI can be passed to this routine
16383ea7c6a1SSatish Balay        by MatDiagonalScale_MPIAIJ */
1639e1311b90SBarry Smith     ierr = VecGetLocalSize(ll,&m);CHKERRQ(ierr);
1640d0f46423SBarry Smith     if (m != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
16411ebc52fbSHong Zhang     ierr = VecGetArray(ll,&l);CHKERRQ(ierr);
1642416022c9SBarry Smith     v = a->a;
164317ab2063SBarry Smith     for (i=0; i<m; i++) {
164417ab2063SBarry Smith       x = l[i];
1645416022c9SBarry Smith       M = a->i[i+1] - a->i[i];
164617ab2063SBarry Smith       for (j=0; j<M; j++) { (*v++) *= x;}
164717ab2063SBarry Smith     }
16481ebc52fbSHong Zhang     ierr = VecRestoreArray(ll,&l);CHKERRQ(ierr);
1649efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
165017ab2063SBarry Smith   }
165117ab2063SBarry Smith   if (rr) {
1652e1311b90SBarry Smith     ierr = VecGetLocalSize(rr,&n);CHKERRQ(ierr);
1653d0f46423SBarry Smith     if (n != A->cmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
16541ebc52fbSHong Zhang     ierr = VecGetArray(rr,&r);CHKERRQ(ierr);
1655416022c9SBarry Smith     v = a->a; jj = a->j;
165617ab2063SBarry Smith     for (i=0; i<nz; i++) {
1657bfeeae90SHong Zhang       (*v++) *= r[*jj++];
165817ab2063SBarry Smith     }
16591ebc52fbSHong Zhang     ierr = VecRestoreArray(rr,&r);CHKERRQ(ierr);
1660efee365bSSatish Balay     ierr = PetscLogFlops(nz);CHKERRQ(ierr);
166117ab2063SBarry Smith   }
16623a40ed3dSBarry Smith   PetscFunctionReturn(0);
166317ab2063SBarry Smith }
166417ab2063SBarry Smith 
16654a2ae208SSatish Balay #undef __FUNCT__
16664a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrix_SeqAIJ"
166797f1f81fSBarry Smith PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
166817ab2063SBarry Smith {
1669db02288aSLois Curfman McInnes   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
16706849ba73SBarry Smith   PetscErrorCode ierr;
1671d0f46423SBarry Smith   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
167297f1f81fSBarry Smith   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
16735d0c19d7SBarry Smith   const PetscInt *irow,*icol;
16745d0c19d7SBarry Smith   PetscInt       nrows,ncols;
167597f1f81fSBarry Smith   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
167654f21887SBarry Smith   MatScalar      *a_new,*mat_a;
1677416022c9SBarry Smith   Mat            C;
167814ca34e6SBarry Smith   PetscTruth     stride,sorted;
167917ab2063SBarry Smith 
16803a40ed3dSBarry Smith   PetscFunctionBegin;
168114ca34e6SBarry Smith   ierr = ISSorted(isrow,&sorted);CHKERRQ(ierr);
168214ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
168314ca34e6SBarry Smith   ierr = ISSorted(iscol,&sorted);CHKERRQ(ierr);
168414ca34e6SBarry Smith   if (!sorted) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");
168599141d43SSatish Balay 
168617ab2063SBarry Smith   ierr = ISGetIndices(isrow,&irow);CHKERRQ(ierr);
1687b9b97703SBarry Smith   ierr = ISGetLocalSize(isrow,&nrows);CHKERRQ(ierr);
1688b9b97703SBarry Smith   ierr = ISGetLocalSize(iscol,&ncols);CHKERRQ(ierr);
168917ab2063SBarry Smith 
1690fee21e36SBarry Smith   ierr = ISStrideGetInfo(iscol,&first,&step);CHKERRQ(ierr);
1691fee21e36SBarry Smith   ierr = ISStride(iscol,&stride);CHKERRQ(ierr);
1692fee21e36SBarry Smith   if (stride && step == 1) {
169302834360SBarry Smith     /* special case of contiguous rows */
169497f1f81fSBarry Smith     ierr   = PetscMalloc((2*nrows+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
169531ebf83bSSatish Balay     starts = lens + nrows;
169602834360SBarry Smith     /* loop over new rows determining lens and starting points */
169702834360SBarry Smith     for (i=0; i<nrows; i++) {
1698bfeeae90SHong Zhang       kstart  = ai[irow[i]];
1699a2744918SBarry Smith       kend    = kstart + ailen[irow[i]];
170002834360SBarry Smith       for (k=kstart; k<kend; k++) {
1701bfeeae90SHong Zhang         if (aj[k] >= first) {
170202834360SBarry Smith           starts[i] = k;
170302834360SBarry Smith           break;
170402834360SBarry Smith 	}
170502834360SBarry Smith       }
1706a2744918SBarry Smith       sum = 0;
170702834360SBarry Smith       while (k < kend) {
1708bfeeae90SHong Zhang         if (aj[k++] >= first+ncols) break;
1709a2744918SBarry Smith         sum++;
171002834360SBarry Smith       }
1711a2744918SBarry Smith       lens[i] = sum;
171202834360SBarry Smith     }
171302834360SBarry Smith     /* create submatrix */
1714cddf8d76SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
171597f1f81fSBarry Smith       PetscInt n_cols,n_rows;
171608480c60SBarry Smith       ierr = MatGetSize(*B,&n_rows,&n_cols);CHKERRQ(ierr);
171729bbc08cSBarry Smith       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
1718d8ced48eSBarry Smith       ierr = MatZeroEntries(*B);CHKERRQ(ierr);
171908480c60SBarry Smith       C = *B;
17203a40ed3dSBarry Smith     } else {
17217adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1722f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17237adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1724ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
172508480c60SBarry Smith     }
1726db02288aSLois Curfman McInnes     c = (Mat_SeqAIJ*)C->data;
1727db02288aSLois Curfman McInnes 
172802834360SBarry Smith     /* loop over rows inserting into submatrix */
1729db02288aSLois Curfman McInnes     a_new    = c->a;
1730db02288aSLois Curfman McInnes     j_new    = c->j;
1731db02288aSLois Curfman McInnes     i_new    = c->i;
1732bfeeae90SHong Zhang 
173302834360SBarry Smith     for (i=0; i<nrows; i++) {
1734a2744918SBarry Smith       ii    = starts[i];
1735a2744918SBarry Smith       lensi = lens[i];
1736a2744918SBarry Smith       for (k=0; k<lensi; k++) {
1737a2744918SBarry Smith         *j_new++ = aj[ii+k] - first;
173802834360SBarry Smith       }
173987828ca2SBarry Smith       ierr = PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));CHKERRQ(ierr);
1740a2744918SBarry Smith       a_new      += lensi;
1741a2744918SBarry Smith       i_new[i+1]  = i_new[i] + lensi;
1742a2744918SBarry Smith       c->ilen[i]  = lensi;
174302834360SBarry Smith     }
1744606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
17453a40ed3dSBarry Smith   } else {
174602834360SBarry Smith     ierr  = ISGetIndices(iscol,&icol);CHKERRQ(ierr);
174797f1f81fSBarry Smith     ierr  = PetscMalloc((1+oldcols)*sizeof(PetscInt),&smap);CHKERRQ(ierr);
1748bfeeae90SHong Zhang 
174997f1f81fSBarry Smith     ierr  = PetscMalloc((1+nrows)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
175097f1f81fSBarry Smith     ierr  = PetscMemzero(smap,oldcols*sizeof(PetscInt));CHKERRQ(ierr);
175117ab2063SBarry Smith     for (i=0; i<ncols; i++) smap[icol[i]] = i+1;
175202834360SBarry Smith     /* determine lens of each row */
175302834360SBarry Smith     for (i=0; i<nrows; i++) {
1754bfeeae90SHong Zhang       kstart  = ai[irow[i]];
175502834360SBarry Smith       kend    = kstart + a->ilen[irow[i]];
175602834360SBarry Smith       lens[i] = 0;
175702834360SBarry Smith       for (k=kstart; k<kend; k++) {
1758bfeeae90SHong Zhang         if (smap[aj[k]]) {
175902834360SBarry Smith           lens[i]++;
176002834360SBarry Smith         }
176102834360SBarry Smith       }
176202834360SBarry Smith     }
176317ab2063SBarry Smith     /* Create and fill new matrix */
1764a2744918SBarry Smith     if (scall == MAT_REUSE_MATRIX) {
17650f5bd95cSBarry Smith       PetscTruth equal;
17660f5bd95cSBarry Smith 
176799141d43SSatish Balay       c = (Mat_SeqAIJ *)((*B)->data);
1768d0f46423SBarry Smith       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
1769d0f46423SBarry Smith       ierr = PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);CHKERRQ(ierr);
17700f5bd95cSBarry Smith       if (!equal) {
177129bbc08cSBarry Smith         SETERRQ(PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
177299141d43SSatish Balay       }
1773d0f46423SBarry Smith       ierr = PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
177408480c60SBarry Smith       C = *B;
17753a40ed3dSBarry Smith     } else {
17767adad957SLisandro Dalcin       ierr = MatCreate(((PetscObject)A)->comm,&C);CHKERRQ(ierr);
1777f69a0ea3SMatthew Knepley       ierr = MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr);
17787adad957SLisandro Dalcin       ierr = MatSetType(C,((PetscObject)A)->type_name);CHKERRQ(ierr);
1779ab93d7beSBarry Smith       ierr = MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);CHKERRQ(ierr);
178008480c60SBarry Smith     }
178199141d43SSatish Balay     c = (Mat_SeqAIJ *)(C->data);
178217ab2063SBarry Smith     for (i=0; i<nrows; i++) {
178399141d43SSatish Balay       row    = irow[i];
1784bfeeae90SHong Zhang       kstart = ai[row];
178599141d43SSatish Balay       kend   = kstart + a->ilen[row];
1786bfeeae90SHong Zhang       mat_i  = c->i[i];
178799141d43SSatish Balay       mat_j  = c->j + mat_i;
178899141d43SSatish Balay       mat_a  = c->a + mat_i;
178999141d43SSatish Balay       mat_ilen = c->ilen + i;
179017ab2063SBarry Smith       for (k=kstart; k<kend; k++) {
1791bfeeae90SHong Zhang         if ((tcol=smap[a->j[k]])) {
1792ed480e8bSBarry Smith           *mat_j++ = tcol - 1;
179399141d43SSatish Balay           *mat_a++ = a->a[k];
179499141d43SSatish Balay           (*mat_ilen)++;
179599141d43SSatish Balay 
179617ab2063SBarry Smith         }
179717ab2063SBarry Smith       }
179817ab2063SBarry Smith     }
179902834360SBarry Smith     /* Free work space */
180002834360SBarry Smith     ierr = ISRestoreIndices(iscol,&icol);CHKERRQ(ierr);
1801606d414cSSatish Balay     ierr = PetscFree(smap);CHKERRQ(ierr);
1802606d414cSSatish Balay     ierr = PetscFree(lens);CHKERRQ(ierr);
180302834360SBarry Smith   }
18046d4a8577SBarry Smith   ierr = MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
18056d4a8577SBarry Smith   ierr = MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
180617ab2063SBarry Smith 
180717ab2063SBarry Smith   ierr = ISRestoreIndices(isrow,&irow);CHKERRQ(ierr);
1808416022c9SBarry Smith   *B = C;
18093a40ed3dSBarry Smith   PetscFunctionReturn(0);
181017ab2063SBarry Smith }
181117ab2063SBarry Smith 
1812*1df811f5SHong Zhang #undef __FUNCT__
1813*1df811f5SHong Zhang #define __FUNCT__ "MatILUFactor_SeqAIJ_newdatastruct"
1814*1df811f5SHong Zhang PetscErrorCode MatILUFactor_SeqAIJ_newdatastruct(Mat inA,IS row,IS col,const MatFactorInfo *info)
1815*1df811f5SHong Zhang {
1816*1df811f5SHong Zhang   PetscErrorCode ierr;
1817*1df811f5SHong Zhang   Mat            C;
1818*1df811f5SHong Zhang 
1819*1df811f5SHong Zhang   PetscFunctionBegin;
1820*1df811f5SHong Zhang   /* A new factored matrix C replaces original matrix inA, not a truely in-place ILUFactor yet */
1821*1df811f5SHong Zhang   ierr = MatGetFactor(inA,MAT_SOLVER_PETSC,MAT_FACTOR_LU,&C);CHKERRQ(ierr);
1822*1df811f5SHong Zhang   ierr = MatILUFactorSymbolic_SeqAIJ_newdatastruct(C,inA,row,col,info);CHKERRQ(ierr);
1823*1df811f5SHong Zhang   ierr = MatLUFactorNumeric_SeqAIJ_newdatastruct(C,inA,info);CHKERRQ(ierr);
1824*1df811f5SHong Zhang   inA->ops->solve            = C->ops->solve;
1825*1df811f5SHong Zhang   inA->ops->solvetranspose   = C->ops->solvetranspose;
1826*1df811f5SHong Zhang   ierr = MatHeaderCopy(inA,C);CHKERRQ(ierr);
1827*1df811f5SHong Zhang   ierr = PetscLogObjectParent(inA,((Mat_SeqAIJ*)(inA->data))->icol);CHKERRQ(ierr);
1828*1df811f5SHong Zhang   PetscFunctionReturn(0);
1829*1df811f5SHong Zhang }
1830*1df811f5SHong Zhang 
18314a2ae208SSatish Balay #undef __FUNCT__
18324a2ae208SSatish Balay #define __FUNCT__ "MatILUFactor_SeqAIJ"
18330481f469SBarry Smith PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
1834a871dcd8SBarry Smith {
183563b91edcSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1836dfbe8321SBarry Smith   PetscErrorCode ierr;
183763b91edcSBarry Smith   Mat            outA;
1838b8a78c4aSBarry Smith   PetscTruth     row_identity,col_identity;
1839*1df811f5SHong Zhang   PetscTruth     newdatastruct=PETSC_FALSE;
184063b91edcSBarry Smith 
18413a40ed3dSBarry Smith   PetscFunctionBegin;
1842*1df811f5SHong Zhang   ierr = PetscOptionsGetTruth(PETSC_NULL,"-ilu_new",&newdatastruct,PETSC_NULL);CHKERRQ(ierr);
1843*1df811f5SHong Zhang   if(newdatastruct){
1844*1df811f5SHong Zhang     ierr = MatILUFactor_SeqAIJ_newdatastruct(inA,row,col,info);CHKERRQ(ierr);
1845*1df811f5SHong Zhang     PetscFunctionReturn(0);
1846*1df811f5SHong Zhang   }
1847*1df811f5SHong Zhang 
1848d3d32019SBarry Smith   if (info->levels != 0) SETERRQ(PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");
1849*1df811f5SHong Zhang 
1850b8a78c4aSBarry Smith   ierr = ISIdentity(row,&row_identity);CHKERRQ(ierr);
1851b8a78c4aSBarry Smith   ierr = ISIdentity(col,&col_identity);CHKERRQ(ierr);
1852a871dcd8SBarry Smith 
185363b91edcSBarry Smith   outA          = inA;
1854*1df811f5SHong Zhang   outA->factor  = MAT_FACTOR_LU;
1855c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)row);CHKERRQ(ierr);
1856c3122656SLisandro Dalcin   if (a->row) { ierr = ISDestroy(a->row);CHKERRQ(ierr);}
1857c3122656SLisandro Dalcin   a->row = row;
1858c38d4ed2SBarry Smith   ierr = PetscObjectReference((PetscObject)col);CHKERRQ(ierr);
1859c3122656SLisandro Dalcin   if (a->col) { ierr = ISDestroy(a->col);CHKERRQ(ierr);}
1860c3122656SLisandro Dalcin   a->col = col;
186163b91edcSBarry Smith 
186236db0b34SBarry Smith   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
1863b9b97703SBarry Smith   if (a->icol) {ierr = ISDestroy(a->icol);CHKERRQ(ierr);} /* need to remove old one */
18644c49b128SBarry Smith   ierr = ISInvertPermutation(col,PETSC_DECIDE,&a->icol);CHKERRQ(ierr);
186552e6d16bSBarry Smith   ierr = PetscLogObjectParent(inA,a->icol);CHKERRQ(ierr);
1866f0ec6fceSSatish Balay 
186794a9d846SBarry Smith   if (!a->solve_work) { /* this matrix may have been factored before */
1868d0f46423SBarry Smith      ierr = PetscMalloc((inA->rmap->n+1)*sizeof(PetscScalar),&a->solve_work);CHKERRQ(ierr);
1869d0f46423SBarry Smith      ierr = PetscLogObjectMemory(inA, (inA->rmap->n+1)*sizeof(PetscScalar));CHKERRQ(ierr);
187094a9d846SBarry Smith   }
187163b91edcSBarry Smith 
1872f1e2ffcdSBarry Smith   ierr = MatMarkDiagonal_SeqAIJ(inA);CHKERRQ(ierr);
1873137fb511SHong Zhang   if (row_identity && col_identity) {
1874719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ(outA,inA,info);CHKERRQ(ierr);
1875137fb511SHong Zhang   } else {
1876719d5645SBarry Smith     ierr = MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);CHKERRQ(ierr);
1877137fb511SHong Zhang   }
18783a40ed3dSBarry Smith   PetscFunctionReturn(0);
1879a871dcd8SBarry Smith }
1880a871dcd8SBarry Smith 
1881d9eff348SSatish Balay #include "petscblaslapack.h"
18824a2ae208SSatish Balay #undef __FUNCT__
18834a2ae208SSatish Balay #define __FUNCT__ "MatScale_SeqAIJ"
1884f4df32b1SMatthew Knepley PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
1885f0b747eeSBarry Smith {
1886f0b747eeSBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
1887f4df32b1SMatthew Knepley   PetscScalar    oalpha = alpha;
1888efee365bSSatish Balay   PetscErrorCode ierr;
18890805154bSBarry Smith   PetscBLASInt   one = 1,bnz = PetscBLASIntCast(a->nz);
18903a40ed3dSBarry Smith 
18913a40ed3dSBarry Smith   PetscFunctionBegin;
1892f4df32b1SMatthew Knepley   BLASscal_(&bnz,&oalpha,a->a,&one);
1893efee365bSSatish Balay   ierr = PetscLogFlops(a->nz);CHKERRQ(ierr);
18943a40ed3dSBarry Smith   PetscFunctionReturn(0);
1895f0b747eeSBarry Smith }
1896f0b747eeSBarry Smith 
18974a2ae208SSatish Balay #undef __FUNCT__
18984a2ae208SSatish Balay #define __FUNCT__ "MatGetSubMatrices_SeqAIJ"
189997f1f81fSBarry Smith PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
1900cddf8d76SBarry Smith {
1901dfbe8321SBarry Smith   PetscErrorCode ierr;
190297f1f81fSBarry Smith   PetscInt       i;
1903cddf8d76SBarry Smith 
19043a40ed3dSBarry Smith   PetscFunctionBegin;
1905cddf8d76SBarry Smith   if (scall == MAT_INITIAL_MATRIX) {
1906b0a32e0cSBarry Smith     ierr = PetscMalloc((n+1)*sizeof(Mat),B);CHKERRQ(ierr);
1907cddf8d76SBarry Smith   }
1908cddf8d76SBarry Smith 
1909cddf8d76SBarry Smith   for (i=0; i<n; i++) {
19106a6a5d1dSBarry Smith     ierr = MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);CHKERRQ(ierr);
1911cddf8d76SBarry Smith   }
19123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1913cddf8d76SBarry Smith }
1914cddf8d76SBarry Smith 
19154a2ae208SSatish Balay #undef __FUNCT__
19164a2ae208SSatish Balay #define __FUNCT__ "MatIncreaseOverlap_SeqAIJ"
191797f1f81fSBarry Smith PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
19184dcbc457SBarry Smith {
1919e4d965acSSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19206849ba73SBarry Smith   PetscErrorCode ierr;
19215d0c19d7SBarry Smith   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
19225d0c19d7SBarry Smith   const PetscInt *idx;
192397f1f81fSBarry Smith   PetscInt       start,end,*ai,*aj;
1924f1af5d2fSBarry Smith   PetscBT        table;
1925bbd702dbSSatish Balay 
19263a40ed3dSBarry Smith   PetscFunctionBegin;
1927d0f46423SBarry Smith   m     = A->rmap->n;
1928e4d965acSSatish Balay   ai    = a->i;
1929bfeeae90SHong Zhang   aj    = a->j;
19308a047759SSatish Balay 
1931a45adfd6SMatthew Knepley   if (ov < 0)  SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");
193206763907SSatish Balay 
193397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&nidx);CHKERRQ(ierr);
19346831982aSBarry Smith   ierr = PetscBTCreate(m,table);CHKERRQ(ierr);
193506763907SSatish Balay 
1936e4d965acSSatish Balay   for (i=0; i<is_max; i++) {
1937b97fc60eSLois Curfman McInnes     /* Initialize the two local arrays */
1938e4d965acSSatish Balay     isz  = 0;
19396831982aSBarry Smith     ierr = PetscBTMemzero(m,table);CHKERRQ(ierr);
1940e4d965acSSatish Balay 
1941e4d965acSSatish Balay     /* Extract the indices, assume there can be duplicate entries */
19424dcbc457SBarry Smith     ierr = ISGetIndices(is[i],&idx);CHKERRQ(ierr);
1943b9b97703SBarry Smith     ierr = ISGetLocalSize(is[i],&n);CHKERRQ(ierr);
1944e4d965acSSatish Balay 
1945dd097bc3SLois Curfman McInnes     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
1946e4d965acSSatish Balay     for (j=0; j<n ; ++j){
1947f1af5d2fSBarry Smith       if(!PetscBTLookupSet(table,idx[j])) { nidx[isz++] = idx[j];}
19484dcbc457SBarry Smith     }
194906763907SSatish Balay     ierr = ISRestoreIndices(is[i],&idx);CHKERRQ(ierr);
195006763907SSatish Balay     ierr = ISDestroy(is[i]);CHKERRQ(ierr);
1951e4d965acSSatish Balay 
195204a348a9SBarry Smith     k = 0;
195304a348a9SBarry Smith     for (j=0; j<ov; j++){ /* for each overlap */
195404a348a9SBarry Smith       n = isz;
195506763907SSatish Balay       for (; k<n ; k++){ /* do only those rows in nidx[k], which are not done yet */
1956e4d965acSSatish Balay         row   = nidx[k];
1957e4d965acSSatish Balay         start = ai[row];
1958e4d965acSSatish Balay         end   = ai[row+1];
195904a348a9SBarry Smith         for (l = start; l<end ; l++){
1960efb16452SHong Zhang           val = aj[l] ;
1961f1af5d2fSBarry Smith           if (!PetscBTLookupSet(table,val)) {nidx[isz++] = val;}
1962e4d965acSSatish Balay         }
1963e4d965acSSatish Balay       }
1964e4d965acSSatish Balay     }
1965029af93fSBarry Smith     ierr = ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,(is+i));CHKERRQ(ierr);
1966e4d965acSSatish Balay   }
19676831982aSBarry Smith   ierr = PetscBTDestroy(table);CHKERRQ(ierr);
1968606d414cSSatish Balay   ierr = PetscFree(nidx);CHKERRQ(ierr);
19693a40ed3dSBarry Smith   PetscFunctionReturn(0);
19704dcbc457SBarry Smith }
197117ab2063SBarry Smith 
19720513a670SBarry Smith /* -------------------------------------------------------------- */
19734a2ae208SSatish Balay #undef __FUNCT__
19744a2ae208SSatish Balay #define __FUNCT__ "MatPermute_SeqAIJ"
1975dfbe8321SBarry Smith PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
19760513a670SBarry Smith {
19770513a670SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
19786849ba73SBarry Smith   PetscErrorCode ierr;
19795d0c19d7SBarry Smith   PetscInt       i,nz,m = A->rmap->n,n = A->cmap->n;
19805d0c19d7SBarry Smith   const PetscInt *row,*col;
19815d0c19d7SBarry Smith   PetscInt       *cnew,j,*lens;
198256cd22aeSBarry Smith   IS             icolp,irowp;
198397f1f81fSBarry Smith   PetscInt       *cwork;
198432ec9ce4SBarry Smith   PetscScalar    *vwork;
19850513a670SBarry Smith 
19863a40ed3dSBarry Smith   PetscFunctionBegin;
19874c49b128SBarry Smith   ierr = ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);CHKERRQ(ierr);
198856cd22aeSBarry Smith   ierr = ISGetIndices(irowp,&row);CHKERRQ(ierr);
19894c49b128SBarry Smith   ierr = ISInvertPermutation(colp,PETSC_DECIDE,&icolp);CHKERRQ(ierr);
199056cd22aeSBarry Smith   ierr = ISGetIndices(icolp,&col);CHKERRQ(ierr);
19910513a670SBarry Smith 
19920513a670SBarry Smith   /* determine lengths of permuted rows */
199397f1f81fSBarry Smith   ierr = PetscMalloc((m+1)*sizeof(PetscInt),&lens);CHKERRQ(ierr);
19940513a670SBarry Smith   for (i=0; i<m; i++) {
19950513a670SBarry Smith     lens[row[i]] = a->i[i+1] - a->i[i];
19960513a670SBarry Smith   }
19977adad957SLisandro Dalcin   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
1998f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*B,m,n,m,n);CHKERRQ(ierr);
19997adad957SLisandro Dalcin   ierr = MatSetType(*B,((PetscObject)A)->type_name);CHKERRQ(ierr);
2000ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);CHKERRQ(ierr);
2001606d414cSSatish Balay   ierr = PetscFree(lens);CHKERRQ(ierr);
20020513a670SBarry Smith 
200397f1f81fSBarry Smith   ierr = PetscMalloc(n*sizeof(PetscInt),&cnew);CHKERRQ(ierr);
20040513a670SBarry Smith   for (i=0; i<m; i++) {
200532ec9ce4SBarry Smith     ierr = MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20060513a670SBarry Smith     for (j=0; j<nz; j++) { cnew[j] = col[cwork[j]];}
2007cdc0ba36SBarry Smith     ierr = MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);CHKERRQ(ierr);
200832ec9ce4SBarry Smith     ierr = MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);CHKERRQ(ierr);
20090513a670SBarry Smith   }
2010606d414cSSatish Balay   ierr = PetscFree(cnew);CHKERRQ(ierr);
20113c7d62e4SBarry Smith   (*B)->assembled     = PETSC_FALSE;
20120513a670SBarry Smith   ierr = MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
20130513a670SBarry Smith   ierr = MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
201456cd22aeSBarry Smith   ierr = ISRestoreIndices(irowp,&row);CHKERRQ(ierr);
201556cd22aeSBarry Smith   ierr = ISRestoreIndices(icolp,&col);CHKERRQ(ierr);
201656cd22aeSBarry Smith   ierr = ISDestroy(irowp);CHKERRQ(ierr);
201756cd22aeSBarry Smith   ierr = ISDestroy(icolp);CHKERRQ(ierr);
20183a40ed3dSBarry Smith   PetscFunctionReturn(0);
20190513a670SBarry Smith }
20200513a670SBarry Smith 
20214a2ae208SSatish Balay #undef __FUNCT__
20224a2ae208SSatish Balay #define __FUNCT__ "MatCopy_SeqAIJ"
2023dfbe8321SBarry Smith PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2024cb5b572fSBarry Smith {
2025dfbe8321SBarry Smith   PetscErrorCode ierr;
2026cb5b572fSBarry Smith 
2027cb5b572fSBarry Smith   PetscFunctionBegin;
202833f4a19fSKris Buschelman   /* If the two matrices have the same copy implementation, use fast copy. */
202933f4a19fSKris Buschelman   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2030be6bf707SBarry Smith     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2031be6bf707SBarry Smith     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;
2032be6bf707SBarry Smith 
2033d0f46423SBarry Smith     if (a->i[A->rmap->n] != b->i[B->rmap->n]) {
2034634064b4SBarry Smith       SETERRQ(PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2035cb5b572fSBarry Smith     }
2036d0f46423SBarry Smith     ierr = PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));CHKERRQ(ierr);
2037cb5b572fSBarry Smith   } else {
2038cb5b572fSBarry Smith     ierr = MatCopy_Basic(A,B,str);CHKERRQ(ierr);
2039cb5b572fSBarry Smith   }
2040cb5b572fSBarry Smith   PetscFunctionReturn(0);
2041cb5b572fSBarry Smith }
2042cb5b572fSBarry Smith 
20434a2ae208SSatish Balay #undef __FUNCT__
20444a2ae208SSatish Balay #define __FUNCT__ "MatSetUpPreallocation_SeqAIJ"
2045dfbe8321SBarry Smith PetscErrorCode MatSetUpPreallocation_SeqAIJ(Mat A)
2046273d9f13SBarry Smith {
2047dfbe8321SBarry Smith   PetscErrorCode ierr;
2048273d9f13SBarry Smith 
2049273d9f13SBarry Smith   PetscFunctionBegin;
2050ab93d7beSBarry Smith   ierr =  MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);CHKERRQ(ierr);
2051273d9f13SBarry Smith   PetscFunctionReturn(0);
2052273d9f13SBarry Smith }
2053273d9f13SBarry Smith 
20544a2ae208SSatish Balay #undef __FUNCT__
20554a2ae208SSatish Balay #define __FUNCT__ "MatGetArray_SeqAIJ"
2056a77337e4SBarry Smith PetscErrorCode MatGetArray_SeqAIJ(Mat A,PetscScalar *array[])
20576c0721eeSBarry Smith {
20586c0721eeSBarry Smith   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
20596c0721eeSBarry Smith   PetscFunctionBegin;
20606c0721eeSBarry Smith   *array = a->a;
20616c0721eeSBarry Smith   PetscFunctionReturn(0);
20626c0721eeSBarry Smith }
20636c0721eeSBarry Smith 
20644a2ae208SSatish Balay #undef __FUNCT__
20654a2ae208SSatish Balay #define __FUNCT__ "MatRestoreArray_SeqAIJ"
2066dfbe8321SBarry Smith PetscErrorCode MatRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
20676c0721eeSBarry Smith {
20686c0721eeSBarry Smith   PetscFunctionBegin;
20696c0721eeSBarry Smith   PetscFunctionReturn(0);
20706c0721eeSBarry Smith }
2071273d9f13SBarry Smith 
2072ee4f033dSBarry Smith #undef __FUNCT__
2073ee4f033dSBarry Smith #define __FUNCT__ "MatFDColoringApply_SeqAIJ"
2074dfbe8321SBarry Smith PetscErrorCode MatFDColoringApply_SeqAIJ(Mat J,MatFDColoring coloring,Vec x1,MatStructure *flag,void *sctx)
2075ee4f033dSBarry Smith {
20766849ba73SBarry Smith   PetscErrorCode (*f)(void*,Vec,Vec,void*) = (PetscErrorCode (*)(void*,Vec,Vec,void *))coloring->f;
20776849ba73SBarry Smith   PetscErrorCode ierr;
207897f1f81fSBarry Smith   PetscInt       k,N,start,end,l,row,col,srow,**vscaleforrow,m1,m2;
2079efb30889SBarry Smith   PetscScalar    dx,*y,*xx,*w3_array;
208087828ca2SBarry Smith   PetscScalar    *vscale_array;
2081ee4f033dSBarry Smith   PetscReal      epsilon = coloring->error_rel,umin = coloring->umin;
2082ee4f033dSBarry Smith   Vec            w1,w2,w3;
2083ee4f033dSBarry Smith   void           *fctx = coloring->fctx;
208490d69ab7SBarry Smith   PetscTruth     flg = PETSC_FALSE;
2085ee4f033dSBarry Smith 
2086ee4f033dSBarry Smith   PetscFunctionBegin;
2087ee4f033dSBarry Smith   if (!coloring->w1) {
2088ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w1);CHKERRQ(ierr);
208952e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w1);CHKERRQ(ierr);
2090ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w2);CHKERRQ(ierr);
209152e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w2);CHKERRQ(ierr);
2092ee4f033dSBarry Smith     ierr = VecDuplicate(x1,&coloring->w3);CHKERRQ(ierr);
209352e6d16bSBarry Smith     ierr = PetscLogObjectParent(coloring,coloring->w3);CHKERRQ(ierr);
2094ee4f033dSBarry Smith   }
2095ee4f033dSBarry Smith   w1 = coloring->w1; w2 = coloring->w2; w3 = coloring->w3;
2096ee4f033dSBarry Smith 
2097ee4f033dSBarry Smith   ierr = MatSetUnfactored(J);CHKERRQ(ierr);
209890d69ab7SBarry Smith   ierr = PetscOptionsGetTruth(((PetscObject)coloring)->prefix,"-mat_fd_coloring_dont_rezero",&flg,PETSC_NULL);CHKERRQ(ierr);
2099ee4f033dSBarry Smith   if (flg) {
2100ae15b995SBarry Smith     ierr = PetscInfo(coloring,"Not calling MatZeroEntries()\n");CHKERRQ(ierr);
2101ee4f033dSBarry Smith   } else {
21020b9b6f31SBarry Smith     PetscTruth assembled;
21030b9b6f31SBarry Smith     ierr = MatAssembled(J,&assembled);CHKERRQ(ierr);
21040b9b6f31SBarry Smith     if (assembled) {
2105ee4f033dSBarry Smith       ierr = MatZeroEntries(J);CHKERRQ(ierr);
2106ee4f033dSBarry Smith     }
21070b9b6f31SBarry Smith   }
2108ee4f033dSBarry Smith 
2109ee4f033dSBarry Smith   ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
2110ee4f033dSBarry Smith   ierr = VecGetSize(x1,&N);CHKERRQ(ierr);
2111ee4f033dSBarry Smith 
2112ee4f033dSBarry Smith   /*
2113ee4f033dSBarry Smith        This is a horrible, horrible, hack. See DMMGComputeJacobian_Multigrid() it inproperly sets
2114ee4f033dSBarry Smith      coloring->F for the coarser grids from the finest
2115ee4f033dSBarry Smith   */
2116ee4f033dSBarry Smith   if (coloring->F) {
2117ee4f033dSBarry Smith     ierr = VecGetLocalSize(coloring->F,&m1);CHKERRQ(ierr);
2118ee4f033dSBarry Smith     ierr = VecGetLocalSize(w1,&m2);CHKERRQ(ierr);
2119ee4f033dSBarry Smith     if (m1 != m2) {
2120ee4f033dSBarry Smith       coloring->F = 0;
2121ee4f033dSBarry Smith     }
2122ee4f033dSBarry Smith   }
2123ee4f033dSBarry Smith 
2124ee4f033dSBarry Smith   if (coloring->F) {
2125ee4f033dSBarry Smith     w1          = coloring->F;
2126ee4f033dSBarry Smith     coloring->F = 0;
2127ee4f033dSBarry Smith   } else {
212866f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2129ee4f033dSBarry Smith     ierr = (*f)(sctx,x1,w1,fctx);CHKERRQ(ierr);
213066f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2131ee4f033dSBarry Smith   }
2132ee4f033dSBarry Smith 
2133ee4f033dSBarry Smith   /*
2134ee4f033dSBarry Smith       Compute all the scale factors and share with other processors
2135ee4f033dSBarry Smith   */
21361ebc52fbSHong Zhang   ierr = VecGetArray(x1,&xx);CHKERRQ(ierr);xx = xx - start;
21371ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);vscale_array = vscale_array - start;
2138ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
2139ee4f033dSBarry Smith     /*
2140ee4f033dSBarry Smith        Loop over each column associated with color adding the
2141ee4f033dSBarry Smith        perturbation to the vector w3.
2142ee4f033dSBarry Smith     */
2143ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2144ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2145ee4f033dSBarry Smith       dx  = xx[col];
2146ee4f033dSBarry Smith       if (dx == 0.0) dx = 1.0;
2147ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2148ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2149ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2150ee4f033dSBarry Smith #else
2151ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2152ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2153ee4f033dSBarry Smith #endif
2154ee4f033dSBarry Smith       dx                *= epsilon;
2155ee4f033dSBarry Smith       vscale_array[col] = 1.0/dx;
2156ee4f033dSBarry Smith     }
2157ee4f033dSBarry Smith   }
21581ebc52fbSHong Zhang   vscale_array = vscale_array + start;ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2159ee4f033dSBarry Smith   ierr = VecGhostUpdateBegin(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2160ee4f033dSBarry Smith   ierr = VecGhostUpdateEnd(coloring->vscale,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
2161ee4f033dSBarry Smith 
2162ee4f033dSBarry Smith   /*  ierr = VecView(coloring->vscale,PETSC_VIEWER_STDOUT_WORLD);
2163ee4f033dSBarry Smith       ierr = VecView(x1,PETSC_VIEWER_STDOUT_WORLD);*/
2164ee4f033dSBarry Smith 
2165ee4f033dSBarry Smith   if (coloring->vscaleforrow) vscaleforrow = coloring->vscaleforrow;
2166ee4f033dSBarry Smith   else                        vscaleforrow = coloring->columnsforrow;
2167ee4f033dSBarry Smith 
21681ebc52fbSHong Zhang   ierr = VecGetArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
2169ee4f033dSBarry Smith   /*
2170ee4f033dSBarry Smith       Loop over each color
2171ee4f033dSBarry Smith   */
2172ee4f033dSBarry Smith   for (k=0; k<coloring->ncolors; k++) {
217349b058dcSBarry Smith     coloring->currentcolor = k;
2174ee4f033dSBarry Smith     ierr = VecCopy(x1,w3);CHKERRQ(ierr);
21751ebc52fbSHong Zhang     ierr = VecGetArray(w3,&w3_array);CHKERRQ(ierr);w3_array = w3_array - start;
2176ee4f033dSBarry Smith     /*
2177ee4f033dSBarry Smith        Loop over each column associated with color adding the
2178ee4f033dSBarry Smith        perturbation to the vector w3.
2179ee4f033dSBarry Smith     */
2180ee4f033dSBarry Smith     for (l=0; l<coloring->ncolumns[k]; l++) {
2181ee4f033dSBarry Smith       col = coloring->columns[k][l];    /* column of the matrix we are probing for */
2182ee4f033dSBarry Smith       dx  = xx[col];
21835b8514ebSBarry Smith       if (dx == 0.0) dx = 1.0;
2184ee4f033dSBarry Smith #if !defined(PETSC_USE_COMPLEX)
2185ee4f033dSBarry Smith       if (dx < umin && dx >= 0.0)      dx = umin;
2186ee4f033dSBarry Smith       else if (dx < 0.0 && dx > -umin) dx = -umin;
2187ee4f033dSBarry Smith #else
2188ee4f033dSBarry Smith       if (PetscAbsScalar(dx) < umin && PetscRealPart(dx) >= 0.0)     dx = umin;
2189ee4f033dSBarry Smith       else if (PetscRealPart(dx) < 0.0 && PetscAbsScalar(dx) < umin) dx = -umin;
2190ee4f033dSBarry Smith #endif
2191ee4f033dSBarry Smith       dx            *= epsilon;
2192634064b4SBarry Smith       if (!PetscAbsScalar(dx)) SETERRQ(PETSC_ERR_PLIB,"Computed 0 differencing parameter");
2193ee4f033dSBarry Smith       w3_array[col] += dx;
2194ee4f033dSBarry Smith     }
21951ebc52fbSHong Zhang     w3_array = w3_array + start; ierr = VecRestoreArray(w3,&w3_array);CHKERRQ(ierr);
2196ee4f033dSBarry Smith 
2197ee4f033dSBarry Smith     /*
2198ee4f033dSBarry Smith        Evaluate function at x1 + dx (here dx is a vector of perturbations)
2199ee4f033dSBarry Smith     */
2200ee4f033dSBarry Smith 
220166f9b7ceSBarry Smith     ierr = PetscLogEventBegin(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2202ee4f033dSBarry Smith     ierr = (*f)(sctx,w3,w2,fctx);CHKERRQ(ierr);
220366f9b7ceSBarry Smith     ierr = PetscLogEventEnd(MAT_FDColoringFunction,0,0,0,0);CHKERRQ(ierr);
2204efb30889SBarry Smith     ierr = VecAXPY(w2,-1.0,w1);CHKERRQ(ierr);
2205ee4f033dSBarry Smith 
2206ee4f033dSBarry Smith     /*
2207ee4f033dSBarry Smith        Loop over rows of vector, putting results into Jacobian matrix
2208ee4f033dSBarry Smith     */
22091ebc52fbSHong Zhang     ierr = VecGetArray(w2,&y);CHKERRQ(ierr);
2210ee4f033dSBarry Smith     for (l=0; l<coloring->nrows[k]; l++) {
2211ee4f033dSBarry Smith       row    = coloring->rows[k][l];
2212ee4f033dSBarry Smith       col    = coloring->columnsforrow[k][l];
2213ee4f033dSBarry Smith       y[row] *= vscale_array[vscaleforrow[k][l]];
2214ee4f033dSBarry Smith       srow   = row + start;
2215ee4f033dSBarry Smith       ierr   = MatSetValues_SeqAIJ(J,1,&srow,1,&col,y+row,INSERT_VALUES);CHKERRQ(ierr);
2216ee4f033dSBarry Smith     }
22171ebc52fbSHong Zhang     ierr = VecRestoreArray(w2,&y);CHKERRQ(ierr);
2218ee4f033dSBarry Smith   }
221949b058dcSBarry Smith   coloring->currentcolor = k;
22201ebc52fbSHong Zhang   ierr = VecRestoreArray(coloring->vscale,&vscale_array);CHKERRQ(ierr);
22211ebc52fbSHong Zhang   xx = xx + start; ierr  = VecRestoreArray(x1,&xx);CHKERRQ(ierr);
2222ee4f033dSBarry Smith   ierr  = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2223ee4f033dSBarry Smith   ierr  = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
2224ee4f033dSBarry Smith   PetscFunctionReturn(0);
2225ee4f033dSBarry Smith }
2226ee4f033dSBarry Smith 
2227ac90fabeSBarry Smith #include "petscblaslapack.h"
2228ac90fabeSBarry Smith #undef __FUNCT__
2229ac90fabeSBarry Smith #define __FUNCT__ "MatAXPY_SeqAIJ"
2230f4df32b1SMatthew Knepley PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2231ac90fabeSBarry Smith {
2232dfbe8321SBarry Smith   PetscErrorCode ierr;
223397f1f81fSBarry Smith   PetscInt       i;
2234ac90fabeSBarry Smith   Mat_SeqAIJ     *x  = (Mat_SeqAIJ *)X->data,*y = (Mat_SeqAIJ *)Y->data;
22350805154bSBarry Smith   PetscBLASInt   one=1,bnz = PetscBLASIntCast(x->nz);
2236ac90fabeSBarry Smith 
2237ac90fabeSBarry Smith   PetscFunctionBegin;
2238ac90fabeSBarry Smith   if (str == SAME_NONZERO_PATTERN) {
2239f4df32b1SMatthew Knepley     PetscScalar alpha = a;
2240f4df32b1SMatthew Knepley     BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one);
2241c537a176SHong Zhang   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2242a30b2313SHong Zhang     if (y->xtoy && y->XtoY != X) {
2243a30b2313SHong Zhang       ierr = PetscFree(y->xtoy);CHKERRQ(ierr);
2244a30b2313SHong Zhang       ierr = MatDestroy(y->XtoY);CHKERRQ(ierr);
2245a30b2313SHong Zhang     }
2246a30b2313SHong Zhang     if (!y->xtoy) { /* get xtoy */
2247d0f46423SBarry Smith       ierr = MatAXPYGetxtoy_Private(X->rmap->n,x->i,x->j,PETSC_NULL, y->i,y->j,PETSC_NULL, &y->xtoy);CHKERRQ(ierr);
2248a30b2313SHong Zhang       y->XtoY = X;
2249407f6b05SHong Zhang       ierr = PetscObjectReference((PetscObject)X);CHKERRQ(ierr);
2250c537a176SHong Zhang     }
2251f4df32b1SMatthew Knepley     for (i=0; i<x->nz; i++) y->a[y->xtoy[i]] += a*(x->a[i]);
22521e2582c4SBarry Smith     ierr = PetscInfo3(Y,"ratio of nnz(X)/nnz(Y): %d/%d = %G\n",x->nz,y->nz,(PetscReal)(x->nz)/y->nz);CHKERRQ(ierr);
2253ac90fabeSBarry Smith   } else {
2254f4df32b1SMatthew Knepley     ierr = MatAXPY_Basic(Y,a,X,str);CHKERRQ(ierr);
2255ac90fabeSBarry Smith   }
2256ac90fabeSBarry Smith   PetscFunctionReturn(0);
2257ac90fabeSBarry Smith }
2258ac90fabeSBarry Smith 
2259521d7252SBarry Smith #undef __FUNCT__
2260521d7252SBarry Smith #define __FUNCT__ "MatSetBlockSize_SeqAIJ"
2261521d7252SBarry Smith PetscErrorCode MatSetBlockSize_SeqAIJ(Mat A,PetscInt bs)
2262521d7252SBarry Smith {
226341c166b1SJed Brown   PetscErrorCode ierr;
226441c166b1SJed Brown 
2265521d7252SBarry Smith   PetscFunctionBegin;
226641c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->rmap,bs);CHKERRQ(ierr);
226741c166b1SJed Brown   ierr = PetscLayoutSetBlockSize(A->cmap,bs);CHKERRQ(ierr);
2268521d7252SBarry Smith   PetscFunctionReturn(0);
2269521d7252SBarry Smith }
2270521d7252SBarry Smith 
2271354c94deSBarry Smith #undef __FUNCT__
2272354c94deSBarry Smith #define __FUNCT__ "MatConjugate_SeqAIJ"
2273354c94deSBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatConjugate_SeqAIJ(Mat mat)
2274354c94deSBarry Smith {
2275354c94deSBarry Smith #if defined(PETSC_USE_COMPLEX)
2276354c94deSBarry Smith   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2277354c94deSBarry Smith   PetscInt    i,nz;
2278354c94deSBarry Smith   PetscScalar *a;
2279354c94deSBarry Smith 
2280354c94deSBarry Smith   PetscFunctionBegin;
2281354c94deSBarry Smith   nz = aij->nz;
2282354c94deSBarry Smith   a  = aij->a;
2283354c94deSBarry Smith   for (i=0; i<nz; i++) {
2284354c94deSBarry Smith     a[i] = PetscConj(a[i]);
2285354c94deSBarry Smith   }
2286354c94deSBarry Smith #else
2287354c94deSBarry Smith   PetscFunctionBegin;
2288354c94deSBarry Smith #endif
2289354c94deSBarry Smith   PetscFunctionReturn(0);
2290354c94deSBarry Smith }
2291354c94deSBarry Smith 
2292e34fafa9SBarry Smith #undef __FUNCT__
2293985db425SBarry Smith #define __FUNCT__ "MatGetRowMaxAbs_SeqAIJ"
2294985db425SBarry Smith PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2295e34fafa9SBarry Smith {
2296e34fafa9SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2297e34fafa9SBarry Smith   PetscErrorCode ierr;
2298d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2299e34fafa9SBarry Smith   PetscReal      atmp;
2300985db425SBarry Smith   PetscScalar    *x;
2301e34fafa9SBarry Smith   MatScalar      *aa;
2302e34fafa9SBarry Smith 
2303e34fafa9SBarry Smith   PetscFunctionBegin;
2304e34fafa9SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2305e34fafa9SBarry Smith   aa   = a->a;
2306e34fafa9SBarry Smith   ai   = a->i;
2307e34fafa9SBarry Smith   aj   = a->j;
2308e34fafa9SBarry Smith 
2309985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2310e34fafa9SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2311e34fafa9SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2312d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2313e34fafa9SBarry Smith   for (i=0; i<m; i++) {
2314e34fafa9SBarry Smith     ncols = ai[1] - ai[0]; ai++;
23159189402eSHong Zhang     x[i] = 0.0;
2316e34fafa9SBarry Smith     for (j=0; j<ncols; j++){
2317985db425SBarry Smith       atmp = PetscAbsScalar(*aa);
2318985db425SBarry Smith       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2319985db425SBarry Smith       aa++; aj++;
2320985db425SBarry Smith     }
2321985db425SBarry Smith   }
2322985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2323985db425SBarry Smith   PetscFunctionReturn(0);
2324985db425SBarry Smith }
2325985db425SBarry Smith 
2326985db425SBarry Smith #undef __FUNCT__
2327985db425SBarry Smith #define __FUNCT__ "MatGetRowMax_SeqAIJ"
2328985db425SBarry Smith PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2329985db425SBarry Smith {
2330985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2331985db425SBarry Smith   PetscErrorCode ierr;
2332d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2333985db425SBarry Smith   PetscScalar    *x;
2334985db425SBarry Smith   MatScalar      *aa;
2335985db425SBarry Smith 
2336985db425SBarry Smith   PetscFunctionBegin;
2337985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2338985db425SBarry Smith   aa   = a->a;
2339985db425SBarry Smith   ai   = a->i;
2340985db425SBarry Smith   aj   = a->j;
2341985db425SBarry Smith 
2342985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2343985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2344985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2345d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2346985db425SBarry Smith   for (i=0; i<m; i++) {
2347985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2348d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2349985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2350985db425SBarry Smith     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2351985db425SBarry Smith       x[i] = 0.0;
2352985db425SBarry Smith       if (idx) {
2353985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2354985db425SBarry Smith         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2355985db425SBarry Smith           if (aj[j] > j) {
2356985db425SBarry Smith             idx[i] = j;
2357985db425SBarry Smith             break;
2358985db425SBarry Smith           }
2359985db425SBarry Smith         }
2360985db425SBarry Smith       }
2361985db425SBarry Smith     }
2362985db425SBarry Smith     for (j=0; j<ncols; j++){
2363985db425SBarry Smith       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2364985db425SBarry Smith       aa++; aj++;
2365985db425SBarry Smith     }
2366985db425SBarry Smith   }
2367985db425SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2368985db425SBarry Smith   PetscFunctionReturn(0);
2369985db425SBarry Smith }
2370985db425SBarry Smith 
2371985db425SBarry Smith #undef __FUNCT__
2372c87e5d42SMatthew Knepley #define __FUNCT__ "MatGetRowMinAbs_SeqAIJ"
2373c87e5d42SMatthew Knepley PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2374c87e5d42SMatthew Knepley {
2375c87e5d42SMatthew Knepley   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2376c87e5d42SMatthew Knepley   PetscErrorCode ierr;
2377c87e5d42SMatthew Knepley   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2378c87e5d42SMatthew Knepley   PetscReal      atmp;
2379c87e5d42SMatthew Knepley   PetscScalar    *x;
2380c87e5d42SMatthew Knepley   MatScalar      *aa;
2381c87e5d42SMatthew Knepley 
2382c87e5d42SMatthew Knepley   PetscFunctionBegin;
2383c87e5d42SMatthew Knepley   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2384c87e5d42SMatthew Knepley   aa   = a->a;
2385c87e5d42SMatthew Knepley   ai   = a->i;
2386c87e5d42SMatthew Knepley   aj   = a->j;
2387c87e5d42SMatthew Knepley 
2388c87e5d42SMatthew Knepley   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2389c87e5d42SMatthew Knepley   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2390c87e5d42SMatthew Knepley   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2391c87e5d42SMatthew Knepley   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2392c87e5d42SMatthew Knepley   for (i=0; i<m; i++) {
2393c87e5d42SMatthew Knepley     ncols = ai[1] - ai[0]; ai++;
2394289a08f5SMatthew Knepley     if (ncols) {
2395289a08f5SMatthew Knepley       /* Get first nonzero */
2396289a08f5SMatthew Knepley       for(j = 0; j < ncols; j++) {
2397289a08f5SMatthew Knepley         atmp = PetscAbsScalar(aa[j]);
2398289a08f5SMatthew Knepley         if (atmp > 1.0e-12) {x[i] = atmp; if (idx) idx[i] = aj[j]; break;}
2399289a08f5SMatthew Knepley       }
2400289a08f5SMatthew Knepley       if (j == ncols) {x[i] = *aa; if (idx) idx[i] = *aj;}
2401289a08f5SMatthew Knepley     } else {
2402289a08f5SMatthew Knepley       x[i] = 0.0; if (idx) idx[i] = 0;
2403289a08f5SMatthew Knepley     }
2404c87e5d42SMatthew Knepley     for(j = 0; j < ncols; j++) {
2405c87e5d42SMatthew Knepley       atmp = PetscAbsScalar(*aa);
2406289a08f5SMatthew Knepley       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2407c87e5d42SMatthew Knepley       aa++; aj++;
2408c87e5d42SMatthew Knepley     }
2409c87e5d42SMatthew Knepley   }
2410c87e5d42SMatthew Knepley   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2411c87e5d42SMatthew Knepley   PetscFunctionReturn(0);
2412c87e5d42SMatthew Knepley }
2413c87e5d42SMatthew Knepley 
2414c87e5d42SMatthew Knepley #undef __FUNCT__
2415985db425SBarry Smith #define __FUNCT__ "MatGetRowMin_SeqAIJ"
2416985db425SBarry Smith PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2417985db425SBarry Smith {
2418985db425SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2419985db425SBarry Smith   PetscErrorCode ierr;
2420d0f46423SBarry Smith   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2421985db425SBarry Smith   PetscScalar    *x;
2422985db425SBarry Smith   MatScalar      *aa;
2423985db425SBarry Smith 
2424985db425SBarry Smith   PetscFunctionBegin;
2425985db425SBarry Smith   if (A->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2426985db425SBarry Smith   aa   = a->a;
2427985db425SBarry Smith   ai   = a->i;
2428985db425SBarry Smith   aj   = a->j;
2429985db425SBarry Smith 
2430985db425SBarry Smith   ierr = VecSet(v,0.0);CHKERRQ(ierr);
2431985db425SBarry Smith   ierr = VecGetArray(v,&x);CHKERRQ(ierr);
2432985db425SBarry Smith   ierr = VecGetLocalSize(v,&n);CHKERRQ(ierr);
2433d0f46423SBarry Smith   if (n != A->rmap->n) SETERRQ(PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2434985db425SBarry Smith   for (i=0; i<m; i++) {
2435985db425SBarry Smith     ncols = ai[1] - ai[0]; ai++;
2436d0f46423SBarry Smith     if (ncols == A->cmap->n) { /* row is dense */
2437985db425SBarry Smith       x[i] = *aa; if (idx) idx[i] = 0;
2438985db425SBarry Smith     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2439985db425SBarry Smith       x[i] = 0.0;
2440985db425SBarry Smith       if (idx) {   /* find first implicit 0.0 in the row */
2441985db425SBarry Smith         idx[i] = 0; /* in case ncols is zero */
2442985db425SBarry Smith         for (j=0;j<ncols;j++) {
2443985db425SBarry Smith           if (aj[j] > j) {
2444985db425SBarry Smith             idx[i] = j;
2445985db425SBarry Smith             break;
2446985db425SBarry Smith           }
2447985db425SBarry Smith         }
2448985db425SBarry Smith       }
2449985db425SBarry Smith     }
2450985db425SBarry Smith     for (j=0; j<ncols; j++){
2451985db425SBarry Smith       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2452985db425SBarry Smith       aa++; aj++;
2453e34fafa9SBarry Smith     }
2454e34fafa9SBarry Smith   }
2455e34fafa9SBarry Smith   ierr = VecRestoreArray(v,&x);CHKERRQ(ierr);
2456e34fafa9SBarry Smith   PetscFunctionReturn(0);
2457e34fafa9SBarry Smith }
24583acb8795SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatFDColoringApply_AIJ(Mat,MatFDColoring,Vec,MatStructure*,void*);
2459682d7d0cSBarry Smith /* -------------------------------------------------------------------*/
24600a6ffc59SBarry Smith static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
2461cb5b572fSBarry Smith        MatGetRow_SeqAIJ,
2462cb5b572fSBarry Smith        MatRestoreRow_SeqAIJ,
2463cb5b572fSBarry Smith        MatMult_SeqAIJ,
246497304618SKris Buschelman /* 4*/ MatMultAdd_SeqAIJ,
24657c922b88SBarry Smith        MatMultTranspose_SeqAIJ,
24667c922b88SBarry Smith        MatMultTransposeAdd_SeqAIJ,
2467db4efbfdSBarry Smith        0,
2468db4efbfdSBarry Smith        0,
2469db4efbfdSBarry Smith        0,
2470db4efbfdSBarry Smith /*10*/ 0,
2471cb5b572fSBarry Smith        MatLUFactor_SeqAIJ,
2472cb5b572fSBarry Smith        0,
247341f059aeSBarry Smith        MatSOR_SeqAIJ,
247417ab2063SBarry Smith        MatTranspose_SeqAIJ,
247597304618SKris Buschelman /*15*/ MatGetInfo_SeqAIJ,
2476cb5b572fSBarry Smith        MatEqual_SeqAIJ,
2477cb5b572fSBarry Smith        MatGetDiagonal_SeqAIJ,
2478cb5b572fSBarry Smith        MatDiagonalScale_SeqAIJ,
2479cb5b572fSBarry Smith        MatNorm_SeqAIJ,
248097304618SKris Buschelman /*20*/ 0,
2481cb5b572fSBarry Smith        MatAssemblyEnd_SeqAIJ,
2482cb5b572fSBarry Smith        MatSetOption_SeqAIJ,
2483cb5b572fSBarry Smith        MatZeroEntries_SeqAIJ,
2484d519adbfSMatthew Knepley /*24*/ MatZeroRows_SeqAIJ,
2485db4efbfdSBarry Smith        0,
2486db4efbfdSBarry Smith        0,
2487db4efbfdSBarry Smith        0,
2488db4efbfdSBarry Smith        0,
2489d519adbfSMatthew Knepley /*29*/ MatSetUpPreallocation_SeqAIJ,
2490db4efbfdSBarry Smith        0,
2491db4efbfdSBarry Smith        0,
24926c0721eeSBarry Smith        MatGetArray_SeqAIJ,
24936c0721eeSBarry Smith        MatRestoreArray_SeqAIJ,
2494d519adbfSMatthew Knepley /*34*/ MatDuplicate_SeqAIJ,
2495cb5b572fSBarry Smith        0,
2496cb5b572fSBarry Smith        0,
2497cb5b572fSBarry Smith        MatILUFactor_SeqAIJ,
2498cb5b572fSBarry Smith        0,
2499d519adbfSMatthew Knepley /*39*/ MatAXPY_SeqAIJ,
2500cb5b572fSBarry Smith        MatGetSubMatrices_SeqAIJ,
2501cb5b572fSBarry Smith        MatIncreaseOverlap_SeqAIJ,
2502cb5b572fSBarry Smith        MatGetValues_SeqAIJ,
2503cb5b572fSBarry Smith        MatCopy_SeqAIJ,
2504d519adbfSMatthew Knepley /*44*/ MatGetRowMax_SeqAIJ,
2505cb5b572fSBarry Smith        MatScale_SeqAIJ,
2506cb5b572fSBarry Smith        0,
250779299369SBarry Smith        MatDiagonalSet_SeqAIJ,
25086945ee14SBarry Smith        MatILUDTFactor_SeqAIJ,
2509d519adbfSMatthew Knepley /*49*/ MatSetBlockSize_SeqAIJ,
25103b2fbd54SBarry Smith        MatGetRowIJ_SeqAIJ,
25113b2fbd54SBarry Smith        MatRestoreRowIJ_SeqAIJ,
25123b2fbd54SBarry Smith        MatGetColumnIJ_SeqAIJ,
2513a93ec695SBarry Smith        MatRestoreColumnIJ_SeqAIJ,
2514d519adbfSMatthew Knepley /*54*/ MatFDColoringCreate_SeqAIJ,
2515b9617806SBarry Smith        0,
25160513a670SBarry Smith        0,
2517cda55fadSBarry Smith        MatPermute_SeqAIJ,
2518cda55fadSBarry Smith        0,
2519d519adbfSMatthew Knepley /*59*/ 0,
2520b9b97703SBarry Smith        MatDestroy_SeqAIJ,
2521b9b97703SBarry Smith        MatView_SeqAIJ,
2522357abbc8SBarry Smith        0,
2523ee4f033dSBarry Smith        0,
2524d519adbfSMatthew Knepley /*64*/ 0,
2525ee4f033dSBarry Smith        0,
2526ee4f033dSBarry Smith        0,
2527ee4f033dSBarry Smith        0,
2528ee4f033dSBarry Smith        0,
2529d519adbfSMatthew Knepley /*69*/ MatGetRowMaxAbs_SeqAIJ,
2530c87e5d42SMatthew Knepley        MatGetRowMinAbs_SeqAIJ,
2531ee4f033dSBarry Smith        0,
2532ee4f033dSBarry Smith        MatSetColoring_SeqAIJ,
2533dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
2534ee4f033dSBarry Smith        MatSetValuesAdic_SeqAIJ,
2535dcf5cc72SBarry Smith #else
2536dcf5cc72SBarry Smith        0,
2537dcf5cc72SBarry Smith #endif
2538d519adbfSMatthew Knepley /*74*/ MatSetValuesAdifor_SeqAIJ,
25393acb8795SBarry Smith        MatFDColoringApply_AIJ,
254097304618SKris Buschelman        0,
254197304618SKris Buschelman        0,
254297304618SKris Buschelman        0,
2543d519adbfSMatthew Knepley /*79*/ 0,
254497304618SKris Buschelman        0,
254597304618SKris Buschelman        0,
254697304618SKris Buschelman        0,
2547bc011b1eSHong Zhang        MatLoad_SeqAIJ,
2548d519adbfSMatthew Knepley /*84*/ MatIsSymmetric_SeqAIJ,
25491cbb95d3SBarry Smith        MatIsHermitian_SeqAIJ,
25506284ec50SHong Zhang        0,
25516284ec50SHong Zhang        0,
2552bc011b1eSHong Zhang        0,
2553d519adbfSMatthew Knepley /*89*/ MatMatMult_SeqAIJ_SeqAIJ,
255426be0446SHong Zhang        MatMatMultSymbolic_SeqAIJ_SeqAIJ,
255526be0446SHong Zhang        MatMatMultNumeric_SeqAIJ_SeqAIJ,
2556d439da42SKris Buschelman        MatPtAP_Basic,
25577ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ,
2558d519adbfSMatthew Knepley /*94*/ MatPtAPNumeric_SeqAIJ,
2559bc011b1eSHong Zhang        MatMatMultTranspose_SeqAIJ_SeqAIJ,
2560bc011b1eSHong Zhang        MatMatMultTransposeSymbolic_SeqAIJ_SeqAIJ,
2561bc011b1eSHong Zhang        MatMatMultTransposeNumeric_SeqAIJ_SeqAIJ,
25627ba1a0bfSKris Buschelman        MatPtAPSymbolic_SeqAIJ_SeqAIJ,
2563d519adbfSMatthew Knepley /*99*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
2564609c6c4dSKris Buschelman        0,
2565609c6c4dSKris Buschelman        0,
256687d4246cSBarry Smith        MatConjugate_SeqAIJ,
256787d4246cSBarry Smith        0,
2568d519adbfSMatthew Knepley /*104*/MatSetValuesRow_SeqAIJ,
256999cafbc1SBarry Smith        MatRealPart_SeqAIJ,
2570f5edf698SHong Zhang        MatImaginaryPart_SeqAIJ,
2571f5edf698SHong Zhang        0,
25722bebee5dSHong Zhang        0,
2573d519adbfSMatthew Knepley /*109*/0,
2574985db425SBarry Smith        0,
25752af78befSBarry Smith        MatGetRowMin_SeqAIJ,
25762af78befSBarry Smith        0,
2577599ef60dSHong Zhang        MatMissingDiagonal_SeqAIJ,
2578d519adbfSMatthew Knepley /*114*/0,
2579599ef60dSHong Zhang        0,
25803c2a7987SHong Zhang        0,
25813c2a7987SHong Zhang        MatILUDTFactorSymbolic_SeqAIJ,
25823c2a7987SHong Zhang        MatILUDTFactorNumeric_SeqAIJ
25839e29f15eSvictorle };
258417ab2063SBarry Smith 
2585fb2e594dSBarry Smith EXTERN_C_BEGIN
25864a2ae208SSatish Balay #undef __FUNCT__
25874a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices_SeqAIJ"
2588be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
2589bef8e0ddSBarry Smith {
2590bef8e0ddSBarry Smith   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
259197f1f81fSBarry Smith   PetscInt   i,nz,n;
2592bef8e0ddSBarry Smith 
2593bef8e0ddSBarry Smith   PetscFunctionBegin;
2594bef8e0ddSBarry Smith 
2595bef8e0ddSBarry Smith   nz = aij->maxnz;
2596d0f46423SBarry Smith   n  = mat->rmap->n;
2597bef8e0ddSBarry Smith   for (i=0; i<nz; i++) {
2598bef8e0ddSBarry Smith     aij->j[i] = indices[i];
2599bef8e0ddSBarry Smith   }
2600bef8e0ddSBarry Smith   aij->nz = nz;
2601bef8e0ddSBarry Smith   for (i=0; i<n; i++) {
2602bef8e0ddSBarry Smith     aij->ilen[i] = aij->imax[i];
2603bef8e0ddSBarry Smith   }
2604bef8e0ddSBarry Smith 
2605bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2606bef8e0ddSBarry Smith }
2607fb2e594dSBarry Smith EXTERN_C_END
2608bef8e0ddSBarry Smith 
26094a2ae208SSatish Balay #undef __FUNCT__
26104a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetColumnIndices"
2611bef8e0ddSBarry Smith /*@
2612bef8e0ddSBarry Smith     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
2613bef8e0ddSBarry Smith        in the matrix.
2614bef8e0ddSBarry Smith 
2615bef8e0ddSBarry Smith   Input Parameters:
2616bef8e0ddSBarry Smith +  mat - the SeqAIJ matrix
2617bef8e0ddSBarry Smith -  indices - the column indices
2618bef8e0ddSBarry Smith 
261915091d37SBarry Smith   Level: advanced
262015091d37SBarry Smith 
2621bef8e0ddSBarry Smith   Notes:
2622bef8e0ddSBarry Smith     This can be called if you have precomputed the nonzero structure of the
2623bef8e0ddSBarry Smith   matrix and want to provide it to the matrix object to improve the performance
2624bef8e0ddSBarry Smith   of the MatSetValues() operation.
2625bef8e0ddSBarry Smith 
2626bef8e0ddSBarry Smith     You MUST have set the correct numbers of nonzeros per row in the call to
2627d1be2dadSMatthew Knepley   MatCreateSeqAIJ(), and the columns indices MUST be sorted.
2628bef8e0ddSBarry Smith 
2629bef8e0ddSBarry Smith     MUST be called before any calls to MatSetValues();
2630bef8e0ddSBarry Smith 
2631b9617806SBarry Smith     The indices should start with zero, not one.
2632b9617806SBarry Smith 
2633bef8e0ddSBarry Smith @*/
2634be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
2635bef8e0ddSBarry Smith {
263697f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt *);
2637bef8e0ddSBarry Smith 
2638bef8e0ddSBarry Smith   PetscFunctionBegin;
26394482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
26404482741eSBarry Smith   PetscValidPointer(indices,2);
2641c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatSeqAIJSetColumnIndices_C",(void (**)(void))&f);CHKERRQ(ierr);
2642bef8e0ddSBarry Smith   if (f) {
2643bef8e0ddSBarry Smith     ierr = (*f)(mat,indices);CHKERRQ(ierr);
2644bef8e0ddSBarry Smith   } else {
2645634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to set column indices");
2646bef8e0ddSBarry Smith   }
2647bef8e0ddSBarry Smith   PetscFunctionReturn(0);
2648bef8e0ddSBarry Smith }
2649bef8e0ddSBarry Smith 
2650be6bf707SBarry Smith /* ----------------------------------------------------------------------------------------*/
2651be6bf707SBarry Smith 
2652fb2e594dSBarry Smith EXTERN_C_BEGIN
26534a2ae208SSatish Balay #undef __FUNCT__
26544a2ae208SSatish Balay #define __FUNCT__ "MatStoreValues_SeqAIJ"
2655be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues_SeqAIJ(Mat mat)
2656be6bf707SBarry Smith {
2657be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
26586849ba73SBarry Smith   PetscErrorCode ierr;
2659d0f46423SBarry Smith   size_t         nz = aij->i[mat->rmap->n];
2660be6bf707SBarry Smith 
2661be6bf707SBarry Smith   PetscFunctionBegin;
2662be6bf707SBarry Smith   if (aij->nonew != 1) {
2663512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2664be6bf707SBarry Smith   }
2665be6bf707SBarry Smith 
2666be6bf707SBarry Smith   /* allocate space for values if not already there */
2667be6bf707SBarry Smith   if (!aij->saved_values) {
266887828ca2SBarry Smith     ierr = PetscMalloc((nz+1)*sizeof(PetscScalar),&aij->saved_values);CHKERRQ(ierr);
26699518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(mat,(nz+1)*sizeof(PetscScalar));CHKERRQ(ierr);
2670be6bf707SBarry Smith   }
2671be6bf707SBarry Smith 
2672be6bf707SBarry Smith   /* copy values over */
267387828ca2SBarry Smith   ierr = PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2674be6bf707SBarry Smith   PetscFunctionReturn(0);
2675be6bf707SBarry Smith }
2676fb2e594dSBarry Smith EXTERN_C_END
2677be6bf707SBarry Smith 
26784a2ae208SSatish Balay #undef __FUNCT__
2679b9617806SBarry Smith #define __FUNCT__ "MatStoreValues"
2680be6bf707SBarry Smith /*@
2681be6bf707SBarry Smith     MatStoreValues - Stashes a copy of the matrix values; this allows, for
2682be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2683be6bf707SBarry Smith        nonlinear portion.
2684be6bf707SBarry Smith 
2685be6bf707SBarry Smith    Collect on Mat
2686be6bf707SBarry Smith 
2687be6bf707SBarry Smith   Input Parameters:
26880e609b76SBarry Smith .  mat - the matrix (currently only AIJ matrices support this option)
2689be6bf707SBarry Smith 
269015091d37SBarry Smith   Level: advanced
269115091d37SBarry Smith 
2692be6bf707SBarry Smith   Common Usage, with SNESSolve():
2693be6bf707SBarry Smith $    Create Jacobian matrix
2694be6bf707SBarry Smith $    Set linear terms into matrix
2695be6bf707SBarry Smith $    Apply boundary conditions to matrix, at this time matrix must have
2696be6bf707SBarry Smith $      final nonzero structure (i.e. setting the nonlinear terms and applying
2697be6bf707SBarry Smith $      boundary conditions again will not change the nonzero structure
2698512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2699be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2700be6bf707SBarry Smith $    Call SNESSetJacobian() with matrix
2701be6bf707SBarry Smith $    In your Jacobian routine
2702be6bf707SBarry Smith $      ierr = MatRetrieveValues(mat);
2703be6bf707SBarry Smith $      Set nonlinear terms in matrix
2704be6bf707SBarry Smith 
2705be6bf707SBarry Smith   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
2706be6bf707SBarry Smith $    // build linear portion of Jacobian
2707512a5fc5SBarry Smith $    ierr = MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
2708be6bf707SBarry Smith $    ierr = MatStoreValues(mat);
2709be6bf707SBarry Smith $    loop over nonlinear iterations
2710be6bf707SBarry Smith $       ierr = MatRetrieveValues(mat);
2711be6bf707SBarry Smith $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
2712be6bf707SBarry Smith $       // call MatAssemblyBegin/End() on matrix
2713be6bf707SBarry Smith $       Solve linear system with Jacobian
2714be6bf707SBarry Smith $    endloop
2715be6bf707SBarry Smith 
2716be6bf707SBarry Smith   Notes:
2717be6bf707SBarry Smith     Matrix must already be assemblied before calling this routine
2718512a5fc5SBarry Smith     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
2719be6bf707SBarry Smith     calling this routine.
2720be6bf707SBarry Smith 
27210c468ba9SBarry Smith     When this is called multiple times it overwrites the previous set of stored values
27220c468ba9SBarry Smith     and does not allocated additional space.
27230c468ba9SBarry Smith 
2724be6bf707SBarry Smith .seealso: MatRetrieveValues()
2725be6bf707SBarry Smith 
2726be6bf707SBarry Smith @*/
2727be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatStoreValues(Mat mat)
2728be6bf707SBarry Smith {
2729dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2730be6bf707SBarry Smith 
2731be6bf707SBarry Smith   PetscFunctionBegin;
27324482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
273329bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
273429bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2735be6bf707SBarry Smith 
2736c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatStoreValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2737be6bf707SBarry Smith   if (f) {
2738be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2739be6bf707SBarry Smith   } else {
2740634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to store values");
2741be6bf707SBarry Smith   }
2742be6bf707SBarry Smith   PetscFunctionReturn(0);
2743be6bf707SBarry Smith }
2744be6bf707SBarry Smith 
2745fb2e594dSBarry Smith EXTERN_C_BEGIN
27464a2ae208SSatish Balay #undef __FUNCT__
27474a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues_SeqAIJ"
2748be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues_SeqAIJ(Mat mat)
2749be6bf707SBarry Smith {
2750be6bf707SBarry Smith   Mat_SeqAIJ     *aij = (Mat_SeqAIJ *)mat->data;
27516849ba73SBarry Smith   PetscErrorCode ierr;
2752d0f46423SBarry Smith   PetscInt       nz = aij->i[mat->rmap->n];
2753be6bf707SBarry Smith 
2754be6bf707SBarry Smith   PetscFunctionBegin;
2755be6bf707SBarry Smith   if (aij->nonew != 1) {
2756512a5fc5SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
2757be6bf707SBarry Smith   }
2758be6bf707SBarry Smith   if (!aij->saved_values) {
2759634064b4SBarry Smith     SETERRQ(PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
2760be6bf707SBarry Smith   }
2761be6bf707SBarry Smith   /* copy values over */
276287828ca2SBarry Smith   ierr = PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));CHKERRQ(ierr);
2763be6bf707SBarry Smith   PetscFunctionReturn(0);
2764be6bf707SBarry Smith }
2765fb2e594dSBarry Smith EXTERN_C_END
2766be6bf707SBarry Smith 
27674a2ae208SSatish Balay #undef __FUNCT__
27684a2ae208SSatish Balay #define __FUNCT__ "MatRetrieveValues"
2769be6bf707SBarry Smith /*@
2770be6bf707SBarry Smith     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
2771be6bf707SBarry Smith        example, reuse of the linear part of a Jacobian, while recomputing the
2772be6bf707SBarry Smith        nonlinear portion.
2773be6bf707SBarry Smith 
2774be6bf707SBarry Smith    Collect on Mat
2775be6bf707SBarry Smith 
2776be6bf707SBarry Smith   Input Parameters:
2777be6bf707SBarry Smith .  mat - the matrix (currently on AIJ matrices support this option)
2778be6bf707SBarry Smith 
277915091d37SBarry Smith   Level: advanced
278015091d37SBarry Smith 
2781be6bf707SBarry Smith .seealso: MatStoreValues()
2782be6bf707SBarry Smith 
2783be6bf707SBarry Smith @*/
2784be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatRetrieveValues(Mat mat)
2785be6bf707SBarry Smith {
2786dfbe8321SBarry Smith   PetscErrorCode ierr,(*f)(Mat);
2787be6bf707SBarry Smith 
2788be6bf707SBarry Smith   PetscFunctionBegin;
27894482741eSBarry Smith   PetscValidHeaderSpecific(mat,MAT_COOKIE,1);
279029bbc08cSBarry Smith   if (!mat->assembled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
279129bbc08cSBarry Smith   if (mat->factor) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2792be6bf707SBarry Smith 
2793c134de8dSSatish Balay   ierr = PetscObjectQueryFunction((PetscObject)mat,"MatRetrieveValues_C",(void (**)(void))&f);CHKERRQ(ierr);
2794be6bf707SBarry Smith   if (f) {
2795be6bf707SBarry Smith     ierr = (*f)(mat);CHKERRQ(ierr);
2796be6bf707SBarry Smith   } else {
2797634064b4SBarry Smith     SETERRQ(PETSC_ERR_SUP,"Wrong type of matrix to retrieve values");
2798be6bf707SBarry Smith   }
2799be6bf707SBarry Smith   PetscFunctionReturn(0);
2800be6bf707SBarry Smith }
2801be6bf707SBarry Smith 
2802f83d6046SBarry Smith 
2803be6bf707SBarry Smith /* --------------------------------------------------------------------------------*/
28044a2ae208SSatish Balay #undef __FUNCT__
28054a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJ"
280617ab2063SBarry Smith /*@C
2807682d7d0cSBarry Smith    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
28080d15e28bSLois Curfman McInnes    (the default parallel PETSc format).  For good matrix assembly performance
28096e62573dSLois Curfman McInnes    the user should preallocate the matrix storage by setting the parameter nz
281051c19458SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
28112bd5e0b2SLois Curfman McInnes    during matrix assembly can be increased by more than a factor of 50.
281217ab2063SBarry Smith 
2813db81eaa0SLois Curfman McInnes    Collective on MPI_Comm
2814db81eaa0SLois Curfman McInnes 
281517ab2063SBarry Smith    Input Parameters:
2816db81eaa0SLois Curfman McInnes +  comm - MPI communicator, set to PETSC_COMM_SELF
281717ab2063SBarry Smith .  m - number of rows
281817ab2063SBarry Smith .  n - number of columns
281917ab2063SBarry Smith .  nz - number of nonzeros per row (same for all rows)
282051c19458SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
28212bd5e0b2SLois Curfman McInnes          (possibly different for each row) or PETSC_NULL
282217ab2063SBarry Smith 
282317ab2063SBarry Smith    Output Parameter:
2824416022c9SBarry Smith .  A - the matrix
282517ab2063SBarry Smith 
2826175b88e8SBarry Smith    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
2827ae1d86c5SBarry Smith    MatXXXXSetPreallocation() paradgm instead of this routine directly.
2828175b88e8SBarry Smith    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]
2829175b88e8SBarry Smith 
2830b259b22eSLois Curfman McInnes    Notes:
283149a6f317SBarry Smith    If nnz is given then nz is ignored
283249a6f317SBarry Smith 
283317ab2063SBarry Smith    The AIJ format (also called the Yale sparse matrix format or
283417ab2063SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
28350002213bSLois Curfman McInnes    storage.  That is, the stored row and column indices can begin at
283644cd7ae7SLois Curfman McInnes    either one (as in Fortran) or zero.  See the users' manual for details.
283717ab2063SBarry Smith 
283817ab2063SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2839a40aa06bSLois Curfman McInnes    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
28403d323bbdSBarry Smith    allocation.  For large problems you MUST preallocate memory or you
28416da5968aSLois Curfman McInnes    will get TERRIBLE performance, see the users' manual chapter on matrices.
284217ab2063SBarry Smith 
2843682d7d0cSBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
28444fca80b9SLois Curfman McInnes    improve numerical efficiency of matrix-vector products and solves. We
2845682d7d0cSBarry Smith    search for consecutive rows with the same nonzero structure, thereby
28466c7ebb05SLois Curfman McInnes    reusing matrix information to achieve increased efficiency.
28476c7ebb05SLois Curfman McInnes 
28486c7ebb05SLois Curfman McInnes    Options Database Keys:
2849698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2850698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2851db81eaa0SLois Curfman McInnes -  -mat_aij_oneindex - Internally use indexing starting at 1
2852db81eaa0SLois Curfman McInnes         rather than 0.  Note that when calling MatSetValues(),
2853db81eaa0SLois Curfman McInnes         the user still MUST index entries starting at 0!
285417ab2063SBarry Smith 
2855027ccd11SLois Curfman McInnes    Level: intermediate
2856027ccd11SLois Curfman McInnes 
285736db0b34SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()
285836db0b34SBarry Smith 
285917ab2063SBarry Smith @*/
2860be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
286117ab2063SBarry Smith {
2862dfbe8321SBarry Smith   PetscErrorCode ierr;
28636945ee14SBarry Smith 
28643a40ed3dSBarry Smith   PetscFunctionBegin;
2865f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,A);CHKERRQ(ierr);
2866117016b1SBarry Smith   ierr = MatSetSizes(*A,m,n,m,n);CHKERRQ(ierr);
2867c4752a88SBarry Smith   ierr = MatSetType(*A,MATSEQAIJ);CHKERRQ(ierr);
2868ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,(PetscInt*)nnz);CHKERRQ(ierr);
2869273d9f13SBarry Smith   PetscFunctionReturn(0);
2870273d9f13SBarry Smith }
2871273d9f13SBarry Smith 
28724a2ae208SSatish Balay #undef __FUNCT__
28734a2ae208SSatish Balay #define __FUNCT__ "MatSeqAIJSetPreallocation"
2874273d9f13SBarry Smith /*@C
2875273d9f13SBarry Smith    MatSeqAIJSetPreallocation - For good matrix assembly performance
2876273d9f13SBarry Smith    the user should preallocate the matrix storage by setting the parameter nz
2877273d9f13SBarry Smith    (or the array nnz).  By setting these parameters accurately, performance
2878273d9f13SBarry Smith    during matrix assembly can be increased by more than a factor of 50.
2879273d9f13SBarry Smith 
2880273d9f13SBarry Smith    Collective on MPI_Comm
2881273d9f13SBarry Smith 
2882273d9f13SBarry Smith    Input Parameters:
2883117016b1SBarry Smith +  B - The matrix-free
2884273d9f13SBarry Smith .  nz - number of nonzeros per row (same for all rows)
2885273d9f13SBarry Smith -  nnz - array containing the number of nonzeros in the various rows
2886273d9f13SBarry Smith          (possibly different for each row) or PETSC_NULL
2887273d9f13SBarry Smith 
2888273d9f13SBarry Smith    Notes:
288949a6f317SBarry Smith      If nnz is given then nz is ignored
289049a6f317SBarry Smith 
2891273d9f13SBarry Smith     The AIJ format (also called the Yale sparse matrix format or
2892273d9f13SBarry Smith    compressed row storage), is fully compatible with standard Fortran 77
2893273d9f13SBarry Smith    storage.  That is, the stored row and column indices can begin at
2894273d9f13SBarry Smith    either one (as in Fortran) or zero.  See the users' manual for details.
2895273d9f13SBarry Smith 
2896273d9f13SBarry Smith    Specify the preallocated storage with either nz or nnz (not both).
2897273d9f13SBarry Smith    Set nz=PETSC_DEFAULT and nnz=PETSC_NULL for PETSc to control dynamic memory
2898273d9f13SBarry Smith    allocation.  For large problems you MUST preallocate memory or you
2899273d9f13SBarry Smith    will get TERRIBLE performance, see the users' manual chapter on matrices.
2900273d9f13SBarry Smith 
2901aa95bbe8SBarry Smith    You can call MatGetInfo() to get information on how effective the preallocation was;
2902aa95bbe8SBarry Smith    for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2903aa95bbe8SBarry Smith    You can also run with the option -info and look for messages with the string
2904aa95bbe8SBarry Smith    malloc in them to see if additional memory allocation was needed.
2905aa95bbe8SBarry Smith 
2906a96a251dSBarry Smith    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
2907a96a251dSBarry Smith    entries or columns indices
2908a96a251dSBarry Smith 
2909273d9f13SBarry Smith    By default, this format uses inodes (identical nodes) when possible, to
2910273d9f13SBarry Smith    improve numerical efficiency of matrix-vector products and solves. We
2911273d9f13SBarry Smith    search for consecutive rows with the same nonzero structure, thereby
2912273d9f13SBarry Smith    reusing matrix information to achieve increased efficiency.
2913273d9f13SBarry Smith 
2914273d9f13SBarry Smith    Options Database Keys:
2915698d4c6aSKris Buschelman +  -mat_no_inode  - Do not use inodes
2916698d4c6aSKris Buschelman .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
2917273d9f13SBarry Smith -  -mat_aij_oneindex - Internally use indexing starting at 1
2918273d9f13SBarry Smith         rather than 0.  Note that when calling MatSetValues(),
2919273d9f13SBarry Smith         the user still MUST index entries starting at 0!
2920273d9f13SBarry Smith 
2921273d9f13SBarry Smith    Level: intermediate
2922273d9f13SBarry Smith 
2923aa95bbe8SBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()
2924273d9f13SBarry Smith 
2925273d9f13SBarry Smith @*/
2926be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
2927273d9f13SBarry Smith {
292897f1f81fSBarry Smith   PetscErrorCode ierr,(*f)(Mat,PetscInt,const PetscInt[]);
2929a23d5eceSKris Buschelman 
2930a23d5eceSKris Buschelman   PetscFunctionBegin;
2931a23d5eceSKris Buschelman   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",(void (**)(void))&f);CHKERRQ(ierr);
2932a23d5eceSKris Buschelman   if (f) {
2933a23d5eceSKris Buschelman     ierr = (*f)(B,nz,nnz);CHKERRQ(ierr);
2934a23d5eceSKris Buschelman   }
2935a23d5eceSKris Buschelman   PetscFunctionReturn(0);
2936a23d5eceSKris Buschelman }
2937a23d5eceSKris Buschelman 
2938a23d5eceSKris Buschelman EXTERN_C_BEGIN
2939a23d5eceSKris Buschelman #undef __FUNCT__
2940a23d5eceSKris Buschelman #define __FUNCT__ "MatSeqAIJSetPreallocation_SeqAIJ"
2941be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,PetscInt *nnz)
2942a23d5eceSKris Buschelman {
2943273d9f13SBarry Smith   Mat_SeqAIJ     *b;
2944a43ee2ecSKris Buschelman   PetscTruth     skipallocation = PETSC_FALSE;
29456849ba73SBarry Smith   PetscErrorCode ierr;
294697f1f81fSBarry Smith   PetscInt       i;
2947273d9f13SBarry Smith 
2948273d9f13SBarry Smith   PetscFunctionBegin;
2949d5d45c9bSBarry Smith 
2950a96a251dSBarry Smith   if (nz == MAT_SKIP_ALLOCATION) {
2951c461c341SBarry Smith     skipallocation = PETSC_TRUE;
2952c461c341SBarry Smith     nz             = 0;
2953c461c341SBarry Smith   }
2954c461c341SBarry Smith 
295526283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->rmap,1);CHKERRQ(ierr);
295626283091SBarry Smith   ierr = PetscLayoutSetBlockSize(B->cmap,1);CHKERRQ(ierr);
295726283091SBarry Smith   ierr = PetscLayoutSetUp(B->rmap);CHKERRQ(ierr);
295826283091SBarry Smith   ierr = PetscLayoutSetUp(B->cmap);CHKERRQ(ierr);
2959899cda47SBarry Smith 
2960435da068SBarry Smith   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
2961435da068SBarry Smith   if (nz < 0) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
2962b73539f3SBarry Smith   if (nnz) {
2963d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) {
296429bbc08cSBarry Smith       if (nnz[i] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %d value %d",i,nnz[i]);
2965d0f46423SBarry Smith       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],B->cmap->n);
2966b73539f3SBarry Smith     }
2967b73539f3SBarry Smith   }
2968b73539f3SBarry Smith 
2969273d9f13SBarry Smith   B->preallocated = PETSC_TRUE;
2970273d9f13SBarry Smith   b = (Mat_SeqAIJ*)B->data;
2971273d9f13SBarry Smith 
2972ab93d7beSBarry Smith   if (!skipallocation) {
29732ee49352SLisandro Dalcin     if (!b->imax) {
2974d0f46423SBarry Smith       ierr = PetscMalloc2(B->rmap->n,PetscInt,&b->imax,B->rmap->n,PetscInt,&b->ilen);CHKERRQ(ierr);
2975d0f46423SBarry Smith       ierr = PetscLogObjectMemory(B,2*B->rmap->n*sizeof(PetscInt));CHKERRQ(ierr);
29762ee49352SLisandro Dalcin     }
2977273d9f13SBarry Smith     if (!nnz) {
2978435da068SBarry Smith       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
2979273d9f13SBarry Smith       else if (nz <= 0)        nz = 1;
2980d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
2981d0f46423SBarry Smith       nz = nz*B->rmap->n;
2982273d9f13SBarry Smith     } else {
2983273d9f13SBarry Smith       nz = 0;
2984d0f46423SBarry Smith       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
2985273d9f13SBarry Smith     }
2986ab93d7beSBarry Smith     /* b->ilen will count nonzeros in each row so far. */
2987d0f46423SBarry Smith     for (i=0; i<B->rmap->n; i++) { b->ilen[i] = 0; }
2988ab93d7beSBarry Smith 
2989273d9f13SBarry Smith     /* allocate the matrix space */
29902ee49352SLisandro Dalcin     ierr = MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);CHKERRQ(ierr);
2991d0f46423SBarry Smith     ierr = PetscMalloc3(nz,PetscScalar,&b->a,nz,PetscInt,&b->j,B->rmap->n+1,PetscInt,&b->i);CHKERRQ(ierr);
2992d0f46423SBarry Smith     ierr = PetscLogObjectMemory(B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));CHKERRQ(ierr);
2993bfeeae90SHong Zhang     b->i[0] = 0;
2994d0f46423SBarry Smith     for (i=1; i<B->rmap->n+1; i++) {
29955da197adSKris Buschelman       b->i[i] = b->i[i-1] + b->imax[i-1];
29965da197adSKris Buschelman     }
2997273d9f13SBarry Smith     b->singlemalloc = PETSC_TRUE;
2998e6b907acSBarry Smith     b->free_a       = PETSC_TRUE;
2999e6b907acSBarry Smith     b->free_ij      = PETSC_TRUE;
3000c461c341SBarry Smith   } else {
3001e6b907acSBarry Smith     b->free_a       = PETSC_FALSE;
3002e6b907acSBarry Smith     b->free_ij      = PETSC_FALSE;
3003c461c341SBarry Smith   }
3004273d9f13SBarry Smith 
3005273d9f13SBarry Smith   b->nz                = 0;
3006273d9f13SBarry Smith   b->maxnz             = nz;
3007273d9f13SBarry Smith   B->info.nz_unneeded  = (double)b->maxnz;
3008273d9f13SBarry Smith   PetscFunctionReturn(0);
3009273d9f13SBarry Smith }
3010a23d5eceSKris Buschelman EXTERN_C_END
3011273d9f13SBarry Smith 
3012a1661176SMatthew Knepley #undef  __FUNCT__
3013a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR"
301458d36128SBarry Smith /*@
3015a1661176SMatthew Knepley    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.
3016a1661176SMatthew Knepley 
3017a1661176SMatthew Knepley    Input Parameters:
3018a1661176SMatthew Knepley +  B - the matrix
3019a1661176SMatthew Knepley .  i - the indices into j for the start of each row (starts with zero)
3020a1661176SMatthew Knepley .  j - the column indices for each row (starts with zero) these must be sorted for each row
3021a1661176SMatthew Knepley -  v - optional values in the matrix
3022a1661176SMatthew Knepley 
3023a1661176SMatthew Knepley    Level: developer
3024a1661176SMatthew Knepley 
302558d36128SBarry Smith    The i,j,v values are COPIED with this routine; to avoid the copy use MatCreateSeqAIJWithArrays()
302658d36128SBarry Smith 
3027a1661176SMatthew Knepley .keywords: matrix, aij, compressed row, sparse, sequential
3028a1661176SMatthew Knepley 
3029a1661176SMatthew Knepley .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3030a1661176SMatthew Knepley @*/
3031a1661176SMatthew Knepley PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3032a1661176SMatthew Knepley {
3033a1661176SMatthew Knepley   PetscErrorCode (*f)(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]);
3034a1661176SMatthew Knepley   PetscErrorCode ierr;
3035a1661176SMatthew Knepley 
3036a1661176SMatthew Knepley   PetscFunctionBegin;
3037a1661176SMatthew Knepley   PetscValidHeaderSpecific(B,MAT_COOKIE,1);
3038a1661176SMatthew Knepley   ierr = PetscObjectQueryFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",(void (**)(void))&f);CHKERRQ(ierr);
3039a1661176SMatthew Knepley   if (f) {
3040a1661176SMatthew Knepley     ierr = (*f)(B,i,j,v);CHKERRQ(ierr);
3041a1661176SMatthew Knepley   }
3042a1661176SMatthew Knepley   PetscFunctionReturn(0);
3043a1661176SMatthew Knepley }
3044a1661176SMatthew Knepley 
3045a1661176SMatthew Knepley EXTERN_C_BEGIN
3046a1661176SMatthew Knepley #undef  __FUNCT__
3047a1661176SMatthew Knepley #define __FUNCT__  "MatSeqAIJSetPreallocationCSR_SeqAIJ"
3048b7940d39SSatish Balay PetscErrorCode PETSCMAT_DLLEXPORT MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3049a1661176SMatthew Knepley {
3050a1661176SMatthew Knepley   PetscInt       i;
3051a1661176SMatthew Knepley   PetscInt       m,n;
3052a1661176SMatthew Knepley   PetscInt       nz;
3053a1661176SMatthew Knepley   PetscInt       *nnz, nz_max = 0;
3054a1661176SMatthew Knepley   PetscScalar    *values;
3055a1661176SMatthew Knepley   PetscErrorCode ierr;
3056a1661176SMatthew Knepley 
3057a1661176SMatthew Knepley   PetscFunctionBegin;
3058a1661176SMatthew Knepley   ierr = MatGetSize(B, &m, &n);CHKERRQ(ierr);
3059a1661176SMatthew Knepley 
3060b7940d39SSatish Balay   if (Ii[0]) {
3061b7940d39SSatish Balay     SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);
3062a1661176SMatthew Knepley   }
3063a1661176SMatthew Knepley   ierr = PetscMalloc((m+1) * sizeof(PetscInt), &nnz);CHKERRQ(ierr);
3064a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3065b7940d39SSatish Balay     nz     = Ii[i+1]- Ii[i];
3066a1661176SMatthew Knepley     nz_max = PetscMax(nz_max, nz);
3067a1661176SMatthew Knepley     if (nz < 0) {
3068a1661176SMatthew Knepley       SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3069a1661176SMatthew Knepley     }
3070a1661176SMatthew Knepley     nnz[i] = nz;
3071a1661176SMatthew Knepley   }
3072a1661176SMatthew Knepley   ierr = MatSeqAIJSetPreallocation(B, 0, nnz);CHKERRQ(ierr);
3073a1661176SMatthew Knepley   ierr = PetscFree(nnz);CHKERRQ(ierr);
3074a1661176SMatthew Knepley 
3075a1661176SMatthew Knepley   if (v) {
3076a1661176SMatthew Knepley     values = (PetscScalar*) v;
3077a1661176SMatthew Knepley   } else {
3078a1661176SMatthew Knepley     ierr = PetscMalloc((nz_max+1)*sizeof(PetscScalar), &values);CHKERRQ(ierr);
3079a1661176SMatthew Knepley     ierr = PetscMemzero(values, nz_max*sizeof(PetscScalar));CHKERRQ(ierr);
3080a1661176SMatthew Knepley   }
3081a1661176SMatthew Knepley 
3082a1661176SMatthew Knepley   for(i = 0; i < m; i++) {
3083b7940d39SSatish Balay     nz  = Ii[i+1] - Ii[i];
3084b7940d39SSatish Balay     ierr = MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);CHKERRQ(ierr);
3085a1661176SMatthew Knepley   }
3086a1661176SMatthew Knepley 
3087a1661176SMatthew Knepley   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3088a1661176SMatthew Knepley   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3089a1661176SMatthew Knepley 
3090a1661176SMatthew Knepley   if (!v) {
3091a1661176SMatthew Knepley     ierr = PetscFree(values);CHKERRQ(ierr);
3092a1661176SMatthew Knepley   }
3093a1661176SMatthew Knepley   PetscFunctionReturn(0);
3094a1661176SMatthew Knepley }
3095a1661176SMatthew Knepley EXTERN_C_END
3096a1661176SMatthew Knepley 
30977c4f633dSBarry Smith #include "../src/mat/impls/dense/seq/dense.h"
3098be7314b0SBarry Smith #include "private/petscaxpy.h"
3099170fe5c8SBarry Smith 
3100170fe5c8SBarry Smith #undef __FUNCT__
3101170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultNumeric_SeqDense_SeqAIJ"
3102170fe5c8SBarry Smith /*
3103170fe5c8SBarry Smith     Computes (B'*A')' since computing B*A directly is untenable
3104170fe5c8SBarry Smith 
3105170fe5c8SBarry Smith                n                       p                          p
3106170fe5c8SBarry Smith         (              )       (              )         (                  )
3107170fe5c8SBarry Smith       m (      A       )  *  n (       B      )   =   m (         C        )
3108170fe5c8SBarry Smith         (              )       (              )         (                  )
3109170fe5c8SBarry Smith 
3110170fe5c8SBarry Smith */
3111170fe5c8SBarry Smith PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3112170fe5c8SBarry Smith {
3113170fe5c8SBarry Smith   PetscErrorCode     ierr;
3114170fe5c8SBarry Smith   Mat_SeqDense       *sub_a = (Mat_SeqDense*)A->data;
3115170fe5c8SBarry Smith   Mat_SeqAIJ         *sub_b = (Mat_SeqAIJ*)B->data;
3116170fe5c8SBarry Smith   Mat_SeqDense       *sub_c = (Mat_SeqDense*)C->data;
31171de00fd4SBarry Smith   PetscInt           i,n,m,q,p;
3118170fe5c8SBarry Smith   const PetscInt     *ii,*idx;
3119170fe5c8SBarry Smith   const PetscScalar  *b,*a,*a_q;
3120170fe5c8SBarry Smith   PetscScalar        *c,*c_q;
3121170fe5c8SBarry Smith 
3122170fe5c8SBarry Smith   PetscFunctionBegin;
3123d0f46423SBarry Smith   m = A->rmap->n;
3124d0f46423SBarry Smith   n = A->cmap->n;
3125d0f46423SBarry Smith   p = B->cmap->n;
3126170fe5c8SBarry Smith   a = sub_a->v;
3127170fe5c8SBarry Smith   b = sub_b->a;
3128170fe5c8SBarry Smith   c = sub_c->v;
3129170fe5c8SBarry Smith   ierr = PetscMemzero(c,m*p*sizeof(PetscScalar));CHKERRQ(ierr);
3130170fe5c8SBarry Smith 
3131170fe5c8SBarry Smith   ii  = sub_b->i;
3132170fe5c8SBarry Smith   idx = sub_b->j;
3133170fe5c8SBarry Smith   for (i=0; i<n; i++) {
3134170fe5c8SBarry Smith     q = ii[i+1] - ii[i];
3135170fe5c8SBarry Smith     while (q-->0) {
3136170fe5c8SBarry Smith       c_q = c + m*(*idx);
3137170fe5c8SBarry Smith       a_q = a + m*i;
3138be7314b0SBarry Smith       PetscAXPY(c_q,*b,a_q,m);
3139170fe5c8SBarry Smith       idx++;
3140170fe5c8SBarry Smith       b++;
3141170fe5c8SBarry Smith     }
3142170fe5c8SBarry Smith   }
3143170fe5c8SBarry Smith   PetscFunctionReturn(0);
3144170fe5c8SBarry Smith }
3145170fe5c8SBarry Smith 
3146170fe5c8SBarry Smith #undef __FUNCT__
3147170fe5c8SBarry Smith #define __FUNCT__ "MatMatMultSymbolic_SeqDense_SeqAIJ"
3148170fe5c8SBarry Smith PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3149170fe5c8SBarry Smith {
3150170fe5c8SBarry Smith   PetscErrorCode ierr;
3151d0f46423SBarry Smith   PetscInt       m=A->rmap->n,n=B->cmap->n;
3152170fe5c8SBarry Smith   Mat            Cmat;
3153170fe5c8SBarry Smith 
3154170fe5c8SBarry Smith   PetscFunctionBegin;
3155d0f46423SBarry Smith   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_ERR_ARG_SIZ,"A->cmap->n %d != B->rmap->n %d\n",A->cmap->n,B->rmap->n);
315639804f7cSBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,&Cmat);CHKERRQ(ierr);
3157170fe5c8SBarry Smith   ierr = MatSetSizes(Cmat,m,n,m,n);CHKERRQ(ierr);
3158170fe5c8SBarry Smith   ierr = MatSetType(Cmat,MATSEQDENSE);CHKERRQ(ierr);
3159170fe5c8SBarry Smith   ierr = MatSeqDenseSetPreallocation(Cmat,PETSC_NULL);CHKERRQ(ierr);
3160170fe5c8SBarry Smith   Cmat->assembled = PETSC_TRUE;
3161170fe5c8SBarry Smith   *C = Cmat;
3162170fe5c8SBarry Smith   PetscFunctionReturn(0);
3163170fe5c8SBarry Smith }
3164170fe5c8SBarry Smith 
3165170fe5c8SBarry Smith /* ----------------------------------------------------------------*/
3166170fe5c8SBarry Smith #undef __FUNCT__
3167170fe5c8SBarry Smith #define __FUNCT__ "MatMatMult_SeqDense_SeqAIJ"
3168170fe5c8SBarry Smith PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3169170fe5c8SBarry Smith {
3170170fe5c8SBarry Smith   PetscErrorCode ierr;
3171170fe5c8SBarry Smith 
3172170fe5c8SBarry Smith   PetscFunctionBegin;
3173170fe5c8SBarry Smith   if (scall == MAT_INITIAL_MATRIX){
3174170fe5c8SBarry Smith     ierr = MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);CHKERRQ(ierr);
3175170fe5c8SBarry Smith   }
3176170fe5c8SBarry Smith   ierr = MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);CHKERRQ(ierr);
3177170fe5c8SBarry Smith   PetscFunctionReturn(0);
3178170fe5c8SBarry Smith }
3179170fe5c8SBarry Smith 
3180170fe5c8SBarry Smith 
31810bad9183SKris Buschelman /*MC
3182fafad747SKris Buschelman    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
31830bad9183SKris Buschelman    based on compressed sparse row format.
31840bad9183SKris Buschelman 
31850bad9183SKris Buschelman    Options Database Keys:
31860bad9183SKris Buschelman . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
31870bad9183SKris Buschelman 
31880bad9183SKris Buschelman   Level: beginner
31890bad9183SKris Buschelman 
3190f587520bSBarry Smith .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
31910bad9183SKris Buschelman M*/
31920bad9183SKris Buschelman 
3193a6175056SHong Zhang EXTERN_C_BEGIN
3194b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3195b5e56a35SBarry Smith extern PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3196b5e56a35SBarry Smith #endif
319765460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3198af1023dbSSatish Balay extern PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat *);
3199af1023dbSSatish Balay #endif
320017667f90SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatConvert_SeqAIJ_SeqCRL(Mat,MatType,MatReuse,Mat*);
3201b24902e0SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3202db4efbfdSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscTruth *);
3203611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
32045c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_mumps(Mat,MatFactorType,Mat*);
3205611f576cSBarry Smith #endif
3206611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
32075c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3208611f576cSBarry Smith #endif
3209f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3210f3c0ef26SHong Zhang extern PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3211f3c0ef26SHong Zhang #endif
3212611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
32135c9eb25fSBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_spooles(Mat,MatFactorType,Mat*);
3214611f576cSBarry Smith #endif
3215eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3216eb3b5408SSatish Balay extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3217eb3b5408SSatish Balay #endif
3218719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3219719d5645SBarry Smith extern PetscErrorCode PETSCMAT_DLLEXPORT MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3220719d5645SBarry Smith #endif
322117667f90SBarry Smith EXTERN_C_END
322217667f90SBarry Smith 
3223b24902e0SBarry Smith 
322417667f90SBarry Smith EXTERN_C_BEGIN
32254a2ae208SSatish Balay #undef __FUNCT__
32264a2ae208SSatish Balay #define __FUNCT__ "MatCreate_SeqAIJ"
3227be1d678aSKris Buschelman PetscErrorCode PETSCMAT_DLLEXPORT MatCreate_SeqAIJ(Mat B)
3228273d9f13SBarry Smith {
3229273d9f13SBarry Smith   Mat_SeqAIJ     *b;
3230dfbe8321SBarry Smith   PetscErrorCode ierr;
323138baddfdSBarry Smith   PetscMPIInt    size;
3232273d9f13SBarry Smith 
3233273d9f13SBarry Smith   PetscFunctionBegin;
32347adad957SLisandro Dalcin   ierr = MPI_Comm_size(((PetscObject)B)->comm,&size);CHKERRQ(ierr);
3235273d9f13SBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");
3236273d9f13SBarry Smith 
323738f2d2fdSLisandro Dalcin   ierr = PetscNewLog(B,Mat_SeqAIJ,&b);CHKERRQ(ierr);
3238b0a32e0cSBarry Smith   B->data             = (void*)b;
3239549d3d68SSatish Balay   ierr = PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));CHKERRQ(ierr);
324090f02eecSBarry Smith   B->mapping          = 0;
3241416022c9SBarry Smith   b->row              = 0;
3242416022c9SBarry Smith   b->col              = 0;
324382bf6240SBarry Smith   b->icol             = 0;
3244b810aeb4SBarry Smith   b->reallocs         = 0;
324536db0b34SBarry Smith   b->ignorezeroentries = PETSC_FALSE;
3246f1e2ffcdSBarry Smith   b->roworiented       = PETSC_TRUE;
3247416022c9SBarry Smith   b->nonew             = 0;
3248416022c9SBarry Smith   b->diag              = 0;
3249416022c9SBarry Smith   b->solve_work        = 0;
32502a1b7f2aSHong Zhang   B->spptr             = 0;
3251be6bf707SBarry Smith   b->saved_values      = 0;
3252d7f994e1SBarry Smith   b->idiag             = 0;
325371f1c65dSBarry Smith   b->mdiag             = 0;
325471f1c65dSBarry Smith   b->ssor_work         = 0;
325571f1c65dSBarry Smith   b->omega             = 1.0;
325671f1c65dSBarry Smith   b->fshift            = 0.0;
325771f1c65dSBarry Smith   b->idiagvalid        = PETSC_FALSE;
3258a9817697SBarry Smith   b->keepnonzeropattern    = PETSC_FALSE;
3259a30b2313SHong Zhang   b->xtoy              = 0;
3260a30b2313SHong Zhang   b->XtoY              = 0;
326173e7a558SHong Zhang   b->compressedrow.use     = PETSC_FALSE;
3262d0f46423SBarry Smith   b->compressedrow.nrows   = B->rmap->n;
3263d487561eSHong Zhang   b->compressedrow.i       = PETSC_NULL;
3264d487561eSHong Zhang   b->compressedrow.rindex  = PETSC_NULL;
3265d487561eSHong Zhang   b->compressedrow.checked = PETSC_FALSE;
326688e51ccdSHong Zhang   B->same_nonzero          = PETSC_FALSE;
326717ab2063SBarry Smith 
326835d8aa7fSBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
3269b5e56a35SBarry Smith #if defined(PETSC_HAVE_PASTIX)
3270ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_pastix_C",
3271b5e56a35SBarry Smith 					   "MatGetFactor_seqaij_pastix",
3272b5e56a35SBarry Smith 					   MatGetFactor_seqaij_pastix);CHKERRQ(ierr);
3273b5e56a35SBarry Smith #endif
327465460251SBarry Smith #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_SCALAR_SINGLE) && !defined(PETSC_USE_SCALAR_MAT_SINGLE)
3275ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_essl_C",
3276719d5645SBarry Smith                                      "MatGetFactor_seqaij_essl",
3277719d5645SBarry Smith                                      MatGetFactor_seqaij_essl);CHKERRQ(ierr);
3278719d5645SBarry Smith #endif
3279611f576cSBarry Smith #if defined(PETSC_HAVE_SUPERLU)
3280ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_C",
32815c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_superlu",
32825c9eb25fSBarry Smith                                      MatGetFactor_seqaij_superlu);CHKERRQ(ierr);
3283611f576cSBarry Smith #endif
3284f3c0ef26SHong Zhang #if defined(PETSC_HAVE_SUPERLU_DIST)
3285ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_superlu_dist_C",
3286f3c0ef26SHong Zhang                                      "MatGetFactor_seqaij_superlu_dist",
3287f3c0ef26SHong Zhang                                      MatGetFactor_seqaij_superlu_dist);CHKERRQ(ierr);
3288f3c0ef26SHong Zhang #endif
3289611f576cSBarry Smith #if defined(PETSC_HAVE_SPOOLES)
3290ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_spooles_C",
32915c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_spooles",
32925c9eb25fSBarry Smith                                      MatGetFactor_seqaij_spooles);CHKERRQ(ierr);
3293611f576cSBarry Smith #endif
3294611f576cSBarry Smith #if defined(PETSC_HAVE_MUMPS)
3295ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_mumps_C",
32965c9eb25fSBarry Smith                                      "MatGetFactor_seqaij_mumps",
32975c9eb25fSBarry Smith                                      MatGetFactor_seqaij_mumps);CHKERRQ(ierr);
3298611f576cSBarry Smith #endif
3299eb3b5408SSatish Balay #if defined(PETSC_HAVE_UMFPACK)
3300ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_umfpack_C",
3301eb3b5408SSatish Balay                                      "MatGetFactor_seqaij_umfpack",
3302eb3b5408SSatish Balay                                      MatGetFactor_seqaij_umfpack);CHKERRQ(ierr);
3303eb3b5408SSatish Balay #endif
3304719d5645SBarry Smith #if defined(PETSC_HAVE_LUSOL)
3305ec1065edSBarry Smith     ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_lusol_C",
3306719d5645SBarry Smith                                      "MatGetFactor_seqaij_lusol",
3307719d5645SBarry Smith                                      MatGetFactor_seqaij_lusol);CHKERRQ(ierr);
3308719d5645SBarry Smith #endif
3309ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactor_petsc_C",
3310b24902e0SBarry Smith                                      "MatGetFactor_seqaij_petsc",
3311b24902e0SBarry Smith                                      MatGetFactor_seqaij_petsc);CHKERRQ(ierr);
3312ec1065edSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatGetFactorAvailable_petsc_C",
3313db4efbfdSBarry Smith                                      "MatGetFactorAvailable_seqaij_petsc",
3314db4efbfdSBarry Smith                                      MatGetFactorAvailable_seqaij_petsc);CHKERRQ(ierr);
3315f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetColumnIndices_C",
3316bef8e0ddSBarry Smith                                      "MatSeqAIJSetColumnIndices_SeqAIJ",
3317bc4b532fSSatish Balay                                      MatSeqAIJSetColumnIndices_SeqAIJ);CHKERRQ(ierr);
3318f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatStoreValues_C",
3319be6bf707SBarry Smith                                      "MatStoreValues_SeqAIJ",
3320bc4b532fSSatish Balay                                      MatStoreValues_SeqAIJ);CHKERRQ(ierr);
3321f1af5d2fSBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatRetrieveValues_C",
3322be6bf707SBarry Smith                                      "MatRetrieveValues_SeqAIJ",
3323bc4b532fSSatish Balay                                      MatRetrieveValues_SeqAIJ);CHKERRQ(ierr);
3324a6175056SHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",
3325a6175056SHong Zhang                                      "MatConvert_SeqAIJ_SeqSBAIJ",
3326a6175056SHong Zhang                                       MatConvert_SeqAIJ_SeqSBAIJ);CHKERRQ(ierr);
332785fc7724SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqbaij_C",
332885fc7724SBarry Smith                                      "MatConvert_SeqAIJ_SeqBAIJ",
332985fc7724SBarry Smith                                       MatConvert_SeqAIJ_SeqBAIJ);CHKERRQ(ierr);
3330ba03775dSHong Zhang   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcsrperm_C",
3331ba03775dSHong Zhang                                      "MatConvert_SeqAIJ_SeqCSRPERM",
3332ba03775dSHong Zhang                                       MatConvert_SeqAIJ_SeqCSRPERM);CHKERRQ(ierr);
333317667f90SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatConvert_seqaij_seqcrl_C",
333417667f90SBarry Smith                                      "MatConvert_SeqAIJ_SeqCRL",
333517667f90SBarry Smith                                       MatConvert_SeqAIJ_SeqCRL);CHKERRQ(ierr);
33365fbd3699SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsTranspose_C",
33375fbd3699SBarry Smith                                      "MatIsTranspose_SeqAIJ",
33385fbd3699SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
33391cbb95d3SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatIsHermitianTranspose_C",
33401cbb95d3SBarry Smith                                      "MatIsHermitianTranspose_SeqAIJ",
33411cbb95d3SBarry Smith                                       MatIsTranspose_SeqAIJ);CHKERRQ(ierr);
3342a23d5eceSKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocation_C",
3343a23d5eceSKris Buschelman                                      "MatSeqAIJSetPreallocation_SeqAIJ",
3344a23d5eceSKris Buschelman                                       MatSeqAIJSetPreallocation_SeqAIJ);CHKERRQ(ierr);
3345a1661176SMatthew Knepley   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",
3346a1661176SMatthew Knepley 				     "MatSeqAIJSetPreallocationCSR_SeqAIJ",
3347a1661176SMatthew Knepley 				      MatSeqAIJSetPreallocationCSR_SeqAIJ);CHKERRQ(ierr);
334805b94e36SKris Buschelman   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatReorderForNonzeroDiagonal_C",
334905b94e36SKris Buschelman                                      "MatReorderForNonzeroDiagonal_SeqAIJ",
335005b94e36SKris Buschelman                                       MatReorderForNonzeroDiagonal_SeqAIJ);CHKERRQ(ierr);
3351170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMult_seqdense_seqaij_C",
3352170fe5c8SBarry Smith                                      "MatMatMult_SeqDense_SeqAIJ",
3353170fe5c8SBarry Smith                                       MatMatMult_SeqDense_SeqAIJ);CHKERRQ(ierr);
3354170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",
3355170fe5c8SBarry Smith                                      "MatMatMultSymbolic_SeqDense_SeqAIJ",
3356170fe5c8SBarry Smith                                       MatMatMultSymbolic_SeqDense_SeqAIJ);CHKERRQ(ierr);
3357170fe5c8SBarry Smith   ierr = PetscObjectComposeFunctionDynamic((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",
3358170fe5c8SBarry Smith                                      "MatMatMultNumeric_SeqDense_SeqAIJ",
3359170fe5c8SBarry Smith                                       MatMatMultNumeric_SeqDense_SeqAIJ);CHKERRQ(ierr);
33604108e4d5SBarry Smith   ierr = MatCreate_SeqAIJ_Inode(B);CHKERRQ(ierr);
336117667f90SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);CHKERRQ(ierr);
33623a40ed3dSBarry Smith   PetscFunctionReturn(0);
336317ab2063SBarry Smith }
3364273d9f13SBarry Smith EXTERN_C_END
336517ab2063SBarry Smith 
33664a2ae208SSatish Balay #undef __FUNCT__
3367b24902e0SBarry Smith #define __FUNCT__ "MatDuplicateNoCreate_SeqAIJ"
3368b24902e0SBarry Smith /*
3369b24902e0SBarry Smith     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
3370b24902e0SBarry Smith */
3371f77e22a1SHong Zhang PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscTruth mallocmatspace)
337217ab2063SBarry Smith {
3373416022c9SBarry Smith   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
33746849ba73SBarry Smith   PetscErrorCode ierr;
3375d0f46423SBarry Smith   PetscInt       i,m = A->rmap->n;
337617ab2063SBarry Smith 
33773a40ed3dSBarry Smith   PetscFunctionBegin;
3378273d9f13SBarry Smith   c = (Mat_SeqAIJ*)C->data;
3379273d9f13SBarry Smith 
3380416022c9SBarry Smith   C->factor           = A->factor;
33816ad4291fSHong Zhang 
3382416022c9SBarry Smith   c->row            = 0;
3383416022c9SBarry Smith   c->col            = 0;
338482bf6240SBarry Smith   c->icol           = 0;
33856ad4291fSHong Zhang   c->reallocs       = 0;
338617ab2063SBarry Smith 
33876ad4291fSHong Zhang   C->assembled      = PETSC_TRUE;
338817ab2063SBarry Smith 
338926283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->rmap,1);CHKERRQ(ierr);
339026283091SBarry Smith   ierr = PetscLayoutSetBlockSize(C->cmap,1);CHKERRQ(ierr);
339126283091SBarry Smith   ierr = PetscLayoutSetUp(C->rmap);CHKERRQ(ierr);
339226283091SBarry Smith   ierr = PetscLayoutSetUp(C->cmap);CHKERRQ(ierr);
3393eec197d1SBarry Smith 
339433b91e9fSSatish Balay   ierr = PetscMalloc2(m,PetscInt,&c->imax,m,PetscInt,&c->ilen);CHKERRQ(ierr);
33959518dbb4SMatthew Knepley   ierr = PetscLogObjectMemory(C, 2*m*sizeof(PetscInt));CHKERRQ(ierr);
339617ab2063SBarry Smith   for (i=0; i<m; i++) {
3397416022c9SBarry Smith     c->imax[i] = a->imax[i];
3398416022c9SBarry Smith     c->ilen[i] = a->ilen[i];
339917ab2063SBarry Smith   }
340017ab2063SBarry Smith 
340117ab2063SBarry Smith   /* allocate the matrix space */
3402f77e22a1SHong Zhang   if (mallocmatspace){
3403a96a251dSBarry Smith     ierr = PetscMalloc3(a->i[m],PetscScalar,&c->a,a->i[m],PetscInt,&c->j,m+1,PetscInt,&c->i);CHKERRQ(ierr);
34049518dbb4SMatthew Knepley     ierr = PetscLogObjectMemory(C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
3405f1e2ffcdSBarry Smith     c->singlemalloc = PETSC_TRUE;
340697f1f81fSBarry Smith     ierr = PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
340717ab2063SBarry Smith     if (m > 0) {
340897f1f81fSBarry Smith       ierr = PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));CHKERRQ(ierr);
3409be6bf707SBarry Smith       if (cpvalues == MAT_COPY_VALUES) {
3410bfeeae90SHong Zhang         ierr = PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
3411be6bf707SBarry Smith       } else {
3412bfeeae90SHong Zhang         ierr = PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));CHKERRQ(ierr);
341317ab2063SBarry Smith       }
341408480c60SBarry Smith     }
3415f77e22a1SHong Zhang   }
341617ab2063SBarry Smith 
34176ad4291fSHong Zhang   c->ignorezeroentries = a->ignorezeroentries;
3418416022c9SBarry Smith   c->roworiented       = a->roworiented;
3419416022c9SBarry Smith   c->nonew             = a->nonew;
3420416022c9SBarry Smith   if (a->diag) {
342197f1f81fSBarry Smith     ierr = PetscMalloc((m+1)*sizeof(PetscInt),&c->diag);CHKERRQ(ierr);
342252e6d16bSBarry Smith     ierr = PetscLogObjectMemory(C,(m+1)*sizeof(PetscInt));CHKERRQ(ierr);
342317ab2063SBarry Smith     for (i=0; i<m; i++) {
3424416022c9SBarry Smith       c->diag[i] = a->diag[i];
342517ab2063SBarry Smith     }
34263a40ed3dSBarry Smith   } else c->diag           = 0;
34276ad4291fSHong Zhang   c->solve_work            = 0;
34286ad4291fSHong Zhang   c->saved_values          = 0;
34296ad4291fSHong Zhang   c->idiag                 = 0;
343071f1c65dSBarry Smith   c->ssor_work             = 0;
3431a9817697SBarry Smith   c->keepnonzeropattern    = a->keepnonzeropattern;
3432e6b907acSBarry Smith   c->free_a                = PETSC_TRUE;
3433e6b907acSBarry Smith   c->free_ij               = PETSC_TRUE;
34346ad4291fSHong Zhang   c->xtoy                  = 0;
34356ad4291fSHong Zhang   c->XtoY                  = 0;
34366ad4291fSHong Zhang 
3437416022c9SBarry Smith   c->nz                 = a->nz;
3438416022c9SBarry Smith   c->maxnz              = a->maxnz;
3439273d9f13SBarry Smith   C->preallocated       = PETSC_TRUE;
3440754ec7b1SSatish Balay 
34416ad4291fSHong Zhang   c->compressedrow.use     = a->compressedrow.use;
34426ad4291fSHong Zhang   c->compressedrow.nrows   = a->compressedrow.nrows;
34436ad4291fSHong Zhang   c->compressedrow.checked = a->compressedrow.checked;
34446ad4291fSHong Zhang   if ( a->compressedrow.checked && a->compressedrow.use){
34456ad4291fSHong Zhang     i = a->compressedrow.nrows;
34466ad4291fSHong Zhang     ierr = PetscMalloc((2*i+1)*sizeof(PetscInt),&c->compressedrow.i);CHKERRQ(ierr);
34476ad4291fSHong Zhang     c->compressedrow.rindex = c->compressedrow.i + i + 1;
34486ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));CHKERRQ(ierr);
34496ad4291fSHong Zhang     ierr = PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));CHKERRQ(ierr);
345027ea64f8SHong Zhang   } else {
345127ea64f8SHong Zhang     c->compressedrow.use    = PETSC_FALSE;
345227ea64f8SHong Zhang     c->compressedrow.i      = PETSC_NULL;
345327ea64f8SHong Zhang     c->compressedrow.rindex = PETSC_NULL;
34546ad4291fSHong Zhang   }
345588e51ccdSHong Zhang   C->same_nonzero = A->same_nonzero;
34564108e4d5SBarry Smith   ierr = MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);CHKERRQ(ierr);
34574846f1f5SKris Buschelman 
34587adad957SLisandro Dalcin   ierr = PetscFListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);CHKERRQ(ierr);
34593a40ed3dSBarry Smith   PetscFunctionReturn(0);
346017ab2063SBarry Smith }
346117ab2063SBarry Smith 
34624a2ae208SSatish Balay #undef __FUNCT__
3463b24902e0SBarry Smith #define __FUNCT__ "MatDuplicate_SeqAIJ"
3464b24902e0SBarry Smith PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
3465b24902e0SBarry Smith {
3466b24902e0SBarry Smith   PetscErrorCode ierr;
3467d0f46423SBarry Smith   PetscInt       n = A->rmap->n;
3468b24902e0SBarry Smith 
3469b24902e0SBarry Smith   PetscFunctionBegin;
3470b24902e0SBarry Smith   ierr = MatCreate(((PetscObject)A)->comm,B);CHKERRQ(ierr);
3471b24902e0SBarry Smith   ierr = MatSetSizes(*B,n,n,n,n);CHKERRQ(ierr);
3472b24902e0SBarry Smith   ierr = MatSetType(*B,MATSEQAIJ);CHKERRQ(ierr);
3473f77e22a1SHong Zhang   ierr = MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);CHKERRQ(ierr);
3474b24902e0SBarry Smith   PetscFunctionReturn(0);
3475b24902e0SBarry Smith }
3476b24902e0SBarry Smith 
3477b24902e0SBarry Smith #undef __FUNCT__
34784a2ae208SSatish Balay #define __FUNCT__ "MatLoad_SeqAIJ"
3479a313700dSBarry Smith PetscErrorCode MatLoad_SeqAIJ(PetscViewer viewer, const MatType type,Mat *A)
348017ab2063SBarry Smith {
3481416022c9SBarry Smith   Mat_SeqAIJ     *a;
3482416022c9SBarry Smith   Mat            B;
34836849ba73SBarry Smith   PetscErrorCode ierr;
34843c601197SSatish Balay   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N;
348538baddfdSBarry Smith   int            fd;
348638baddfdSBarry Smith   PetscMPIInt    size;
3487bcd2baecSBarry Smith   MPI_Comm       comm;
348817ab2063SBarry Smith 
34893a40ed3dSBarry Smith   PetscFunctionBegin;
3490e864ced6SBarry Smith   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
3491d132466eSBarry Smith   ierr = MPI_Comm_size(comm,&size);CHKERRQ(ierr);
349229bbc08cSBarry Smith   if (size > 1) SETERRQ(PETSC_ERR_ARG_SIZ,"view must have one processor");
3493b0a32e0cSBarry Smith   ierr = PetscViewerBinaryGetDescriptor(viewer,&fd);CHKERRQ(ierr);
34940752156aSBarry Smith   ierr = PetscBinaryRead(fd,header,4,PETSC_INT);CHKERRQ(ierr);
3495552e946dSBarry Smith   if (header[0] != MAT_FILE_COOKIE) SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
349617ab2063SBarry Smith   M = header[1]; N = header[2]; nz = header[3];
349717ab2063SBarry Smith 
3498d64ed03dSBarry Smith   if (nz < 0) {
349929bbc08cSBarry Smith     SETERRQ(PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");
3500d64ed03dSBarry Smith   }
3501d64ed03dSBarry Smith 
350217ab2063SBarry Smith   /* read in row lengths */
350397f1f81fSBarry Smith   ierr = PetscMalloc(M*sizeof(PetscInt),&rowlengths);CHKERRQ(ierr);
35040752156aSBarry Smith   ierr = PetscBinaryRead(fd,rowlengths,M,PETSC_INT);CHKERRQ(ierr);
350517ab2063SBarry Smith 
35063c601197SSatish Balay   /* check if sum of rowlengths is same as nz */
35073c601197SSatish Balay   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
35083c601197SSatish Balay   if (sum != nz) SETERRQ2(PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %d, sum-row-lengths = %d\n",nz,sum);
35093c601197SSatish Balay 
351017ab2063SBarry Smith   /* create our matrix */
3511f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,&B);CHKERRQ(ierr);
3512f69a0ea3SMatthew Knepley   ierr = MatSetSizes(B,PETSC_DECIDE,PETSC_DECIDE,M,N);CHKERRQ(ierr);
3513b3a1e11cSKris Buschelman   ierr = MatSetType(B,type);CHKERRQ(ierr);
3514ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(B,0,rowlengths);CHKERRQ(ierr);
3515416022c9SBarry Smith   a = (Mat_SeqAIJ*)B->data;
351617ab2063SBarry Smith 
35170752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->j,nz,PETSC_INT);CHKERRQ(ierr);
351817ab2063SBarry Smith 
351917ab2063SBarry Smith   /* read in nonzero values */
35200752156aSBarry Smith   ierr = PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);CHKERRQ(ierr);
352117ab2063SBarry Smith 
352217ab2063SBarry Smith   /* set matrix "i" values */
3523efb16452SHong Zhang   a->i[0] = 0;
352417ab2063SBarry Smith   for (i=1; i<= M; i++) {
3525416022c9SBarry Smith     a->i[i]      = a->i[i-1] + rowlengths[i-1];
3526416022c9SBarry Smith     a->ilen[i-1] = rowlengths[i-1];
352717ab2063SBarry Smith   }
3528606d414cSSatish Balay   ierr = PetscFree(rowlengths);CHKERRQ(ierr);
352917ab2063SBarry Smith 
35306d4a8577SBarry Smith   ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
35316d4a8577SBarry Smith   ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3532b3a1e11cSKris Buschelman   *A = B;
35333a40ed3dSBarry Smith   PetscFunctionReturn(0);
353417ab2063SBarry Smith }
353517ab2063SBarry Smith 
35364a2ae208SSatish Balay #undef __FUNCT__
3537b9617806SBarry Smith #define __FUNCT__ "MatEqual_SeqAIJ"
3538dfbe8321SBarry Smith PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscTruth* flg)
35397264ac53SSatish Balay {
35407264ac53SSatish Balay   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data,*b = (Mat_SeqAIJ *)B->data;
3541dfbe8321SBarry Smith   PetscErrorCode ierr;
3542eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3543eeffb40dSHong Zhang   PetscInt k;
3544eeffb40dSHong Zhang #endif
35457264ac53SSatish Balay 
35463a40ed3dSBarry Smith   PetscFunctionBegin;
3547bfeeae90SHong Zhang   /* If the  matrix dimensions are not equal,or no of nonzeros */
3548d0f46423SBarry Smith   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
3549ca44d042SBarry Smith     *flg = PETSC_FALSE;
3550ca44d042SBarry Smith     PetscFunctionReturn(0);
3551bcd2baecSBarry Smith   }
35527264ac53SSatish Balay 
35537264ac53SSatish Balay   /* if the a->i are the same */
3554d0f46423SBarry Smith   ierr = PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3555abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
35567264ac53SSatish Balay 
35577264ac53SSatish Balay   /* if a->j are the same */
355897f1f81fSBarry Smith   ierr = PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);CHKERRQ(ierr);
3559abc0a331SBarry Smith   if (!*flg) PetscFunctionReturn(0);
3560bcd2baecSBarry Smith 
3561bcd2baecSBarry Smith   /* if a->a are the same */
3562eeffb40dSHong Zhang #if defined(PETSC_USE_COMPLEX)
3563eeffb40dSHong Zhang   for (k=0; k<a->nz; k++){
3564eeffb40dSHong Zhang     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])){
3565eeffb40dSHong Zhang       *flg = PETSC_FALSE;
35663a40ed3dSBarry Smith       PetscFunctionReturn(0);
3567eeffb40dSHong Zhang     }
3568eeffb40dSHong Zhang   }
3569eeffb40dSHong Zhang #else
3570eeffb40dSHong Zhang   ierr = PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);CHKERRQ(ierr);
3571eeffb40dSHong Zhang #endif
3572eeffb40dSHong Zhang   PetscFunctionReturn(0);
35737264ac53SSatish Balay }
357436db0b34SBarry Smith 
35754a2ae208SSatish Balay #undef __FUNCT__
35764a2ae208SSatish Balay #define __FUNCT__ "MatCreateSeqAIJWithArrays"
357705869f15SSatish Balay /*@
357836db0b34SBarry Smith      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
357936db0b34SBarry Smith               provided by the user.
358036db0b34SBarry Smith 
3581c75a6043SHong Zhang       Collective on MPI_Comm
358236db0b34SBarry Smith 
358336db0b34SBarry Smith    Input Parameters:
358436db0b34SBarry Smith +   comm - must be an MPI communicator of size 1
358536db0b34SBarry Smith .   m - number of rows
358636db0b34SBarry Smith .   n - number of columns
358736db0b34SBarry Smith .   i - row indices
358836db0b34SBarry Smith .   j - column indices
358936db0b34SBarry Smith -   a - matrix values
359036db0b34SBarry Smith 
359136db0b34SBarry Smith    Output Parameter:
359236db0b34SBarry Smith .   mat - the matrix
359336db0b34SBarry Smith 
359436db0b34SBarry Smith    Level: intermediate
359536db0b34SBarry Smith 
359636db0b34SBarry Smith    Notes:
35970551d7c0SBarry Smith        The i, j, and a arrays are not copied by this routine, the user must free these arrays
359836db0b34SBarry Smith     once the matrix is destroyed
359936db0b34SBarry Smith 
360036db0b34SBarry Smith        You cannot set new nonzero locations into this matrix, that will generate an error.
360136db0b34SBarry Smith 
3602bfeeae90SHong Zhang        The i and j indices are 0 based
360336db0b34SBarry Smith 
3604a4552177SSatish Balay        The format which is used for the sparse matrix input, is equivalent to a
3605a4552177SSatish Balay     row-major ordering.. i.e for the following matrix, the input data expected is
3606a4552177SSatish Balay     as shown:
3607a4552177SSatish Balay 
3608a4552177SSatish Balay         1 0 0
3609a4552177SSatish Balay         2 0 3
3610a4552177SSatish Balay         4 5 6
3611a4552177SSatish Balay 
3612a4552177SSatish Balay         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
36139985e31cSBarry Smith         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
3614a4552177SSatish Balay         v =  {1,2,3,4,5,6}  [size = nz = 6]
3615a4552177SSatish Balay 
36169985e31cSBarry Smith 
36172fb0ec9aSBarry Smith .seealso: MatCreate(), MatCreateMPIAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()
361836db0b34SBarry Smith 
361936db0b34SBarry Smith @*/
3620a77337e4SBarry Smith PetscErrorCode PETSCMAT_DLLEXPORT MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt* i,PetscInt*j,PetscScalar *a,Mat *mat)
362136db0b34SBarry Smith {
3622dfbe8321SBarry Smith   PetscErrorCode ierr;
3623cbcfb4deSHong Zhang   PetscInt       ii;
362436db0b34SBarry Smith   Mat_SeqAIJ     *aij;
3625cbcfb4deSHong Zhang #if defined(PETSC_USE_DEBUG)
3626cbcfb4deSHong Zhang   PetscInt       jj;
3627cbcfb4deSHong Zhang #endif
362836db0b34SBarry Smith 
362936db0b34SBarry Smith   PetscFunctionBegin;
3630a96a251dSBarry Smith   if (i[0]) {
3631634064b4SBarry Smith     SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
363236db0b34SBarry Smith   }
3633f69a0ea3SMatthew Knepley   ierr = MatCreate(comm,mat);CHKERRQ(ierr);
3634f69a0ea3SMatthew Knepley   ierr = MatSetSizes(*mat,m,n,m,n);CHKERRQ(ierr);
3635ab93d7beSBarry Smith   ierr = MatSetType(*mat,MATSEQAIJ);CHKERRQ(ierr);
3636ab93d7beSBarry Smith   ierr = MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);CHKERRQ(ierr);
3637ab93d7beSBarry Smith   aij  = (Mat_SeqAIJ*)(*mat)->data;
3638ab93d7beSBarry Smith   ierr = PetscMalloc2(m,PetscInt,&aij->imax,m,PetscInt,&aij->ilen);CHKERRQ(ierr);
3639ab93d7beSBarry Smith 
364036db0b34SBarry Smith   aij->i = i;
364136db0b34SBarry Smith   aij->j = j;
364236db0b34SBarry Smith   aij->a = a;
364336db0b34SBarry Smith   aij->singlemalloc = PETSC_FALSE;
364436db0b34SBarry Smith   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
3645e6b907acSBarry Smith   aij->free_a       = PETSC_FALSE;
3646e6b907acSBarry Smith   aij->free_ij      = PETSC_FALSE;
364736db0b34SBarry Smith 
364836db0b34SBarry Smith   for (ii=0; ii<m; ii++) {
364936db0b34SBarry Smith     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
36502515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
365179a5c55eSBarry Smith     if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %d length = %d",ii,i[ii+1] - i[ii]);
36529985e31cSBarry Smith     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
36539985e31cSBarry Smith       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
36549985e31cSBarry Smith       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
36559985e31cSBarry Smith     }
365636db0b34SBarry Smith #endif
365736db0b34SBarry Smith   }
36582515c552SBarry Smith #if defined(PETSC_USE_DEBUG)
365936db0b34SBarry Smith   for (ii=0; ii<aij->i[m]; ii++) {
366079a5c55eSBarry Smith     if (j[ii] < 0) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
366179a5c55eSBarry Smith     if (j[ii] > n - 1) SETERRQ2(PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %d index = %d",ii,j[ii]);
366236db0b34SBarry Smith   }
366336db0b34SBarry Smith #endif
366436db0b34SBarry Smith 
3665b65db4caSBarry Smith   ierr = MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
3666b65db4caSBarry Smith   ierr = MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
366736db0b34SBarry Smith   PetscFunctionReturn(0);
366836db0b34SBarry Smith }
366936db0b34SBarry Smith 
3670cc8ba8e1SBarry Smith #undef __FUNCT__
3671ee4f033dSBarry Smith #define __FUNCT__ "MatSetColoring_SeqAIJ"
3672dfbe8321SBarry Smith PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
3673cc8ba8e1SBarry Smith {
3674dfbe8321SBarry Smith   PetscErrorCode ierr;
3675cc8ba8e1SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
367636db0b34SBarry Smith 
3677cc8ba8e1SBarry Smith   PetscFunctionBegin;
36788ee2e534SBarry Smith   if (coloring->ctype == IS_COLORING_GLOBAL) {
3679cc8ba8e1SBarry Smith     ierr        = ISColoringReference(coloring);CHKERRQ(ierr);
3680cc8ba8e1SBarry Smith     a->coloring = coloring;
368112c595b3SBarry Smith   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
368297f1f81fSBarry Smith     PetscInt             i,*larray;
368312c595b3SBarry Smith     ISColoring      ocoloring;
368408b6dcc0SBarry Smith     ISColoringValue *colors;
368512c595b3SBarry Smith 
368612c595b3SBarry Smith     /* set coloring for diagonal portion */
3687d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(PetscInt),&larray);CHKERRQ(ierr);
3688d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
368912c595b3SBarry Smith       larray[i] = i;
369012c595b3SBarry Smith     }
3691d0f46423SBarry Smith     ierr = ISGlobalToLocalMappingApply(A->mapping,IS_GTOLM_MASK,A->cmap->n,larray,PETSC_NULL,larray);CHKERRQ(ierr);
3692d0f46423SBarry Smith     ierr = PetscMalloc((A->cmap->n+1)*sizeof(ISColoringValue),&colors);CHKERRQ(ierr);
3693d0f46423SBarry Smith     for (i=0; i<A->cmap->n; i++) {
369412c595b3SBarry Smith       colors[i] = coloring->colors[larray[i]];
369512c595b3SBarry Smith     }
369612c595b3SBarry Smith     ierr = PetscFree(larray);CHKERRQ(ierr);
3697d0f46423SBarry Smith     ierr = ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);CHKERRQ(ierr);
369812c595b3SBarry Smith     a->coloring = ocoloring;
369912c595b3SBarry Smith   }
3700cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3701cc8ba8e1SBarry Smith }
3702cc8ba8e1SBarry Smith 
3703dcf5cc72SBarry Smith #if defined(PETSC_HAVE_ADIC)
3704ee4f033dSBarry Smith EXTERN_C_BEGIN
370529c1e371SBarry Smith #include "adic/ad_utils.h"
3706ee4f033dSBarry Smith EXTERN_C_END
3707cc8ba8e1SBarry Smith 
3708cc8ba8e1SBarry Smith #undef __FUNCT__
3709ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdic_SeqAIJ"
3710dfbe8321SBarry Smith PetscErrorCode MatSetValuesAdic_SeqAIJ(Mat A,void *advalues)
3711cc8ba8e1SBarry Smith {
3712cc8ba8e1SBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3713d0f46423SBarry Smith   PetscInt        m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j,nlen;
37144440f671SBarry Smith   PetscScalar     *v = a->a,*values = ((PetscScalar*)advalues)+1;
371508b6dcc0SBarry Smith   ISColoringValue *color;
3716cc8ba8e1SBarry Smith 
3717cc8ba8e1SBarry Smith   PetscFunctionBegin;
3718e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
37194440f671SBarry Smith   nlen  = PetscADGetDerivTypeSize()/sizeof(PetscScalar);
3720cc8ba8e1SBarry Smith   color = a->coloring->colors;
3721cc8ba8e1SBarry Smith   /* loop over rows */
3722cc8ba8e1SBarry Smith   for (i=0; i<m; i++) {
3723cc8ba8e1SBarry Smith     nz = ii[i+1] - ii[i];
3724cc8ba8e1SBarry Smith     /* loop over columns putting computed value into matrix */
3725cc8ba8e1SBarry Smith     for (j=0; j<nz; j++) {
3726cc8ba8e1SBarry Smith       *v++ = values[color[*jj++]];
3727cc8ba8e1SBarry Smith     }
37284440f671SBarry Smith     values += nlen; /* jump to next row of derivatives */
3729ee4f033dSBarry Smith   }
3730ee4f033dSBarry Smith   PetscFunctionReturn(0);
3731ee4f033dSBarry Smith }
3732ee4f033dSBarry Smith #endif
3733ee4f033dSBarry Smith 
3734ee4f033dSBarry Smith #undef __FUNCT__
3735ee4f033dSBarry Smith #define __FUNCT__ "MatSetValuesAdifor_SeqAIJ"
373697f1f81fSBarry Smith PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
3737ee4f033dSBarry Smith {
3738ee4f033dSBarry Smith   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3739d0f46423SBarry Smith   PetscInt         m = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
374054f21887SBarry Smith   MatScalar       *v = a->a;
374154f21887SBarry Smith   PetscScalar     *values = (PetscScalar *)advalues;
374208b6dcc0SBarry Smith   ISColoringValue *color;
3743ee4f033dSBarry Smith 
3744ee4f033dSBarry Smith   PetscFunctionBegin;
3745e005ede5SBarry Smith   if (!a->coloring) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
3746ee4f033dSBarry Smith   color = a->coloring->colors;
3747ee4f033dSBarry Smith   /* loop over rows */
3748ee4f033dSBarry Smith   for (i=0; i<m; i++) {
3749ee4f033dSBarry Smith     nz = ii[i+1] - ii[i];
3750ee4f033dSBarry Smith     /* loop over columns putting computed value into matrix */
3751ee4f033dSBarry Smith     for (j=0; j<nz; j++) {
3752ee4f033dSBarry Smith       *v++ = values[color[*jj++]];
3753ee4f033dSBarry Smith     }
3754ee4f033dSBarry Smith     values += nl; /* jump to next row of derivatives */
3755cc8ba8e1SBarry Smith   }
3756cc8ba8e1SBarry Smith   PetscFunctionReturn(0);
3757cc8ba8e1SBarry Smith }
375836db0b34SBarry Smith 
375981824310SBarry Smith /*
376081824310SBarry Smith     Special version for direct calls from Fortran
376181824310SBarry Smith */
376281824310SBarry Smith #if defined(PETSC_HAVE_FORTRAN_CAPS)
376381824310SBarry Smith #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
376481824310SBarry Smith #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
376581824310SBarry Smith #define matsetvaluesseqaij_ matsetvaluesseqaij
376681824310SBarry Smith #endif
376781824310SBarry Smith 
376881824310SBarry Smith /* Change these macros so can be used in void function */
376981824310SBarry Smith #undef CHKERRQ
37707adad957SLisandro Dalcin #define CHKERRQ(ierr) CHKERRABORT(((PetscObject)A)->comm,ierr)
377181824310SBarry Smith #undef SETERRQ2
37727adad957SLisandro Dalcin #define SETERRQ2(ierr,b,c,d) CHKERRABORT(((PetscObject)A)->comm,ierr)
377381824310SBarry Smith 
377481824310SBarry Smith EXTERN_C_BEGIN
377581824310SBarry Smith #undef __FUNCT__
377681824310SBarry Smith #define __FUNCT__ "matsetvaluesseqaij_"
37771f6cc5b2SSatish Balay void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
377881824310SBarry Smith {
377981824310SBarry Smith   Mat            A = *AA;
378081824310SBarry Smith   PetscInt       m = *mm, n = *nn;
378181824310SBarry Smith   InsertMode     is = *isis;
378281824310SBarry Smith   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
378381824310SBarry Smith   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
378481824310SBarry Smith   PetscInt       *imax,*ai,*ailen;
378581824310SBarry Smith   PetscErrorCode ierr;
378681824310SBarry Smith   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
378754f21887SBarry Smith   MatScalar      *ap,value,*aa;
3788edb03aefSBarry Smith   PetscTruth     ignorezeroentries = a->ignorezeroentries;
378981824310SBarry Smith   PetscTruth     roworiented = a->roworiented;
379081824310SBarry Smith 
379181824310SBarry Smith   PetscFunctionBegin;
3792d9e2c085SLisandro Dalcin   ierr = MatPreallocated(A);CHKERRQ(ierr);
379381824310SBarry Smith   imax = a->imax;
379481824310SBarry Smith   ai = a->i;
379581824310SBarry Smith   ailen = a->ilen;
379681824310SBarry Smith   aj = a->j;
379781824310SBarry Smith   aa = a->a;
379881824310SBarry Smith 
379981824310SBarry Smith   for (k=0; k<m; k++) { /* loop over added rows */
380081824310SBarry Smith     row  = im[k];
380181824310SBarry Smith     if (row < 0) continue;
380281824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3803d0f46423SBarry Smith     if (row >= A->rmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
380481824310SBarry Smith #endif
380581824310SBarry Smith     rp   = aj + ai[row]; ap = aa + ai[row];
380681824310SBarry Smith     rmax = imax[row]; nrow = ailen[row];
380781824310SBarry Smith     low  = 0;
380881824310SBarry Smith     high = nrow;
380981824310SBarry Smith     for (l=0; l<n; l++) { /* loop over added columns */
381081824310SBarry Smith       if (in[l] < 0) continue;
381181824310SBarry Smith #if defined(PETSC_USE_DEBUG)
3812d0f46423SBarry Smith       if (in[l] >= A->cmap->n) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
381381824310SBarry Smith #endif
381481824310SBarry Smith       col = in[l];
381581824310SBarry Smith       if (roworiented) {
381681824310SBarry Smith         value = v[l + k*n];
381781824310SBarry Smith       } else {
381881824310SBarry Smith         value = v[k + l*m];
381981824310SBarry Smith       }
382081824310SBarry Smith       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
382181824310SBarry Smith 
382281824310SBarry Smith       if (col <= lastcol) low = 0; else high = nrow;
382381824310SBarry Smith       lastcol = col;
382481824310SBarry Smith       while (high-low > 5) {
382581824310SBarry Smith         t = (low+high)/2;
382681824310SBarry Smith         if (rp[t] > col) high = t;
382781824310SBarry Smith         else             low  = t;
382881824310SBarry Smith       }
382981824310SBarry Smith       for (i=low; i<high; i++) {
383081824310SBarry Smith         if (rp[i] > col) break;
383181824310SBarry Smith         if (rp[i] == col) {
383281824310SBarry Smith           if (is == ADD_VALUES) ap[i] += value;
383381824310SBarry Smith           else                  ap[i] = value;
383481824310SBarry Smith           goto noinsert;
383581824310SBarry Smith         }
383681824310SBarry Smith       }
383781824310SBarry Smith       if (value == 0.0 && ignorezeroentries) goto noinsert;
383881824310SBarry Smith       if (nonew == 1) goto noinsert;
38397adad957SLisandro Dalcin       if (nonew == -1) SETERRABORT(((PetscObject)A)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
3840d0f46423SBarry Smith       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
384181824310SBarry Smith       N = nrow++ - 1; a->nz++; high++;
384281824310SBarry Smith       /* shift up all the later entries in this row */
384381824310SBarry Smith       for (ii=N; ii>=i; ii--) {
384481824310SBarry Smith         rp[ii+1] = rp[ii];
384581824310SBarry Smith         ap[ii+1] = ap[ii];
384681824310SBarry Smith       }
384781824310SBarry Smith       rp[i] = col;
384881824310SBarry Smith       ap[i] = value;
384981824310SBarry Smith       noinsert:;
385081824310SBarry Smith       low = i + 1;
385181824310SBarry Smith     }
385281824310SBarry Smith     ailen[row] = nrow;
385381824310SBarry Smith   }
385481824310SBarry Smith   A->same_nonzero = PETSC_FALSE;
385581824310SBarry Smith   PetscFunctionReturnVoid();
385681824310SBarry Smith }
385781824310SBarry Smith EXTERN_C_END
3858