xref: /petsc/src/mat/impls/aij/seq/ij.c (revision 329f5518e9d4bb7ce96c0c5576cc53785c973973)
1*329f5518SBarry Smith /*$Id: ij.c,v 1.33 1999/10/24 14:02:14 bsmith Exp bsmith $*/
2d6dfbf8fSBarry Smith 
370f55243SBarry Smith #include "src/mat/impls/aij/seq/aij.h"
42d9e4a2aSBarry Smith 
55615d1e5SSatish Balay #undef __FUNC__
65615d1e5SSatish Balay #define __FUNC__ "MatToSymmetricIJ_SeqAIJ"
72d9e4a2aSBarry Smith /*
83b2fbd54SBarry Smith   MatToSymmetricIJ_SeqAIJ - Convert a (generally nonsymmetric) sparse AIJ matrix
93b2fbd54SBarry Smith            to IJ format (ignore the "A" part) Allocates the space needed. Uses only
10d5d45c9bSBarry Smith            the lower triangular part of the matrix.
112d9e4a2aSBarry Smith 
122d9e4a2aSBarry Smith     Description:
132d9e4a2aSBarry Smith     Take the data in the row-oriented sparse storage and build the
142d9e4a2aSBarry Smith     IJ data for the Matrix.  Return 0 on success,row + 1 on failure
152d9e4a2aSBarry Smith     at that row. Produces the ij for a symmetric matrix by only using
162d9e4a2aSBarry Smith     the lower triangular part of the matrix.
172d9e4a2aSBarry Smith 
182d9e4a2aSBarry Smith     Input Parameters:
192d9e4a2aSBarry Smith .   Matrix - matrix to convert
20bcd2baecSBarry Smith .   shiftin - the shift for the original matrix (0 or 1)
2191e9ee9fSBarry Smith .   shiftout - the shift required for the ordering routine (0 or 1)
222d9e4a2aSBarry Smith 
232d9e4a2aSBarry Smith     Output Parameters:
242d9e4a2aSBarry Smith .   ia     - ia part of IJ representation (row information)
252d9e4a2aSBarry Smith .   ja     - ja part (column indices)
262d9e4a2aSBarry Smith 
272d9e4a2aSBarry Smith     Notes:
28b833fc0dSLois Curfman McInnes     Both ia and ja may be freed with PetscFree();
29b833fc0dSLois Curfman McInnes     This routine is provided for ordering routines that require a
30b833fc0dSLois Curfman McInnes     symmetric structure.  It is required since those routines call
31b833fc0dSLois Curfman McInnes     SparsePak routines that expect a symmetric  matrix.
322d9e4a2aSBarry Smith */
33*329f5518SBarry Smith int MatToSymmetricIJ_SeqAIJ(int m,int *ai,int *aj,int shiftin,int shiftout,int **iia,int **jja)
342d9e4a2aSBarry Smith {
35549d3d68SSatish Balay   int *work,*ia,*ja,*j,i,nz,row,col,ierr;
362d9e4a2aSBarry Smith 
373a40ed3dSBarry Smith   PetscFunctionBegin;
382d9e4a2aSBarry Smith   /* allocate space for row pointers */
3931625ec5SSatish Balay   *iia = ia = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(ia);
40549d3d68SSatish Balay   ierr = PetscMemzero(ia,(m+1)*sizeof(int));CHKERRQ(ierr);
4131625ec5SSatish Balay   work = (int*)PetscMalloc((m+1)*sizeof(int));CHKPTRQ(work);
422d9e4a2aSBarry Smith 
432d9e4a2aSBarry Smith   /* determine the number of columns in each row */
443b2fbd54SBarry Smith   ia[0] = shiftout;
4531625ec5SSatish Balay   for (row = 0; row < m; row++) {
463439631bSBarry Smith     nz = ai[row+1] - ai[row];
470b6503f3SSatish Balay     j  = aj + ai[row] + shiftin;
482d9e4a2aSBarry Smith     while (nz--) {
4917603e33SSatish Balay        col = *j++ + shiftin;
50416022c9SBarry Smith        if (col > row) { break;}
512d9e4a2aSBarry Smith        if (col != row) ia[row+1]++;
522d9e4a2aSBarry Smith        ia[col+1]++;
532d9e4a2aSBarry Smith     }
542d9e4a2aSBarry Smith   }
552d9e4a2aSBarry Smith 
56bcd2baecSBarry Smith   /* shiftin ia[i] to point to next row */
5731625ec5SSatish Balay   for (i=1; i<m+1; i++) {
580b6503f3SSatish Balay     row       = ia[i-1];
592d9e4a2aSBarry Smith     ia[i]     += row;
600b6503f3SSatish Balay     work[i-1] = row - shiftout;
612d9e4a2aSBarry Smith   }
622d9e4a2aSBarry Smith 
632d9e4a2aSBarry Smith   /* allocate space for column pointers */
6431625ec5SSatish Balay   nz = ia[m] + (!shiftin);
650452661fSBarry Smith   *jja = ja = (int*)PetscMalloc(nz*sizeof(int));CHKPTRQ(ja);
662d9e4a2aSBarry Smith 
672d9e4a2aSBarry Smith   /* loop over lower triangular part putting into ja */
6831625ec5SSatish Balay   for (row = 0; row < m; row++) {
693439631bSBarry Smith     nz = ai[row+1] - ai[row];
700b6503f3SSatish Balay     j  = aj + ai[row] + shiftin;
712d9e4a2aSBarry Smith     while (nz--) {
7217603e33SSatish Balay       col = *j++ + shiftin;
73416022c9SBarry Smith       if (col > row) { break;}
743b2fbd54SBarry Smith       if (col != row) {ja[work[col]++] = row + shiftout; }
753b2fbd54SBarry Smith       ja[work[row]++] = col + shiftout;
762d9e4a2aSBarry Smith     }
772d9e4a2aSBarry Smith   }
78606d414cSSatish Balay   ierr = PetscFree(work);CHKERRQ(ierr);
793a40ed3dSBarry Smith   PetscFunctionReturn(0);
802d9e4a2aSBarry Smith }
812d9e4a2aSBarry Smith 
82d5d45c9bSBarry Smith 
83d5d45c9bSBarry Smith 
84