Actual source code: ij.c

petsc-3.6.4 2016-04-12
Report Typos and Errors
  2: #include <../src/mat/impls/aij/seq/aij.h>

  6: /*
  7:   MatToSymmetricIJ_SeqAIJ - Convert a (generally nonsymmetric) sparse AIJ matrix
  8:            to IJ format (ignore the "A" part) Allocates the space needed. Uses only
  9:            the lower triangular part of the matrix.

 11:     Description:
 12:     Take the data in the row-oriented sparse storage and build the
 13:     IJ data for the Matrix.  Return 0 on success,row + 1 on failure
 14:     at that row. Produces the ij for a symmetric matrix by only using
 15:     the lower triangular part of the matrix.

 17:     Input Parameters:
 18: .   Matrix - matrix to convert
 19: .   shiftin - the shift for the original matrix (0 or 1)
 20: .   shiftout - the shift required for the ordering routine (0 or 1)

 22:     Output Parameters:
 23: .   ia     - ia part of IJ representation (row information)
 24: .   ja     - ja part (column indices)

 26:     Notes:
 27:     Both ia and ja may be freed with PetscFree();
 28:     This routine is provided for ordering routines that require a
 29:     symmetric structure.  It is required since those routines call
 30:     SparsePak routines that expect a symmetric  matrix.
 31: */
 32: PetscErrorCode MatToSymmetricIJ_SeqAIJ(PetscInt m,PetscInt *ai,PetscInt *aj,PetscInt shiftin,PetscInt shiftout,PetscInt **iia,PetscInt **jja)
 33: {
 35:   PetscInt       *work,*ia,*ja,*j,i,nz,row,col;

 38:   /* allocate space for row pointers */
 39:   PetscMalloc1(m+1,&ia);
 40:   *iia = ia;
 41:   PetscMemzero(ia,(m+1)*sizeof(PetscInt));
 42:   PetscMalloc1(m+1,&work);

 44:   /* determine the number of columns in each row */
 45:   ia[0] = shiftout;
 46:   for (row = 0; row < m; row++) {
 47:     nz = ai[row+1] - ai[row];
 48:     j  = aj + ai[row] + shiftin;
 49:     while (nz--) {
 50:       col = *j++ + shiftin;
 51:       if (col > row) break;
 52:       if (col != row) ia[row+1]++;
 53:       ia[col+1]++;
 54:     }
 55:   }

 57:   /* shiftin ia[i] to point to next row */
 58:   for (i=1; i<m+1; i++) {
 59:     row       = ia[i-1];
 60:     ia[i]    += row;
 61:     work[i-1] = row - shiftout;
 62:   }

 64:   /* allocate space for column pointers */
 65:   nz   = ia[m] + (!shiftin);
 66:   PetscMalloc1(nz,&ja);
 67:   *jja = ja;

 69:   /* loop over lower triangular part putting into ja */
 70:   for (row = 0; row < m; row++) {
 71:     nz = ai[row+1] - ai[row];
 72:     j  = aj + ai[row] + shiftin;
 73:     while (nz--) {
 74:       col = *j++ + shiftin;
 75:       if (col > row) break;
 76:       if (col != row) ja[work[col]++] = row + shiftout;
 77:       ja[work[row]++] = col + shiftout;
 78:     }
 79:   }
 80:   PetscFree(work);
 81:   return(0);
 82: }