xref: /petsc/src/mat/impls/aij/seq/ij.c (revision 416022c9818a71eecdf06d41c1abd586feaab60e)
1cb512458SBarry Smith #ifndef lint
2*416022c9SBarry Smith static char vcid[] = "$Id: ij.c,v 1.10 1995/09/21 20:10:16 bsmith Exp bsmith $";
3cb512458SBarry Smith #endif
4d6dfbf8fSBarry Smith 
52d9e4a2aSBarry Smith #include "aij.h"
62d9e4a2aSBarry Smith 
72d9e4a2aSBarry Smith /*
8ec8511deSBarry Smith   MatToSymmetricIJ_SeqAIJ - Convert a sparse AIJ matrix to IJ format
92d9e4a2aSBarry Smith            (ignore the "A" part)
102d9e4a2aSBarry Smith            Allocates the space needed. Uses only the lower triangular
112d9e4a2aSBarry Smith            part of the matrix.
122d9e4a2aSBarry Smith 
132d9e4a2aSBarry Smith     Description:
142d9e4a2aSBarry Smith     Take the data in the row-oriented sparse storage and build the
152d9e4a2aSBarry Smith     IJ data for the Matrix.  Return 0 on success, row + 1 on failure
162d9e4a2aSBarry Smith     at that row. Produces the ij for a symmetric matrix by only using
172d9e4a2aSBarry Smith     the lower triangular part of the matrix.
182d9e4a2aSBarry Smith 
192d9e4a2aSBarry Smith     Input Parameters:
202d9e4a2aSBarry Smith .   Matrix - matrix to convert
212d9e4a2aSBarry Smith 
222d9e4a2aSBarry Smith     Output Parameters:
232d9e4a2aSBarry Smith .   ia     - ia part of IJ representation (row information)
242d9e4a2aSBarry Smith .   ja     - ja part (column indices)
252d9e4a2aSBarry Smith 
262d9e4a2aSBarry Smith     Notes:
27464493b3SBarry Smith $    Both ia and ja maybe freed with PETSCFREE();
28464493b3SBarry Smith $    This routine is provided for ordering routines that require a
29464493b3SBarry Smith $    symmetric structure.  It is used in SpOrder (and derivatives) since
30464493b3SBarry Smith $    those routines call SparsePak routines that expect a symmetric
31464493b3SBarry Smith $    matrix.
322d9e4a2aSBarry Smith */
33*416022c9SBarry Smith int MatToSymmetricIJ_SeqAIJ( Mat_SeqAIJ *A, int **iia, int **jja )
342d9e4a2aSBarry Smith {
35*416022c9SBarry Smith   int *work,*ia,*ja,*j,i, nz, n = A->m, row, wr, col, shift = A->indexshift;
362d9e4a2aSBarry Smith 
372d9e4a2aSBarry Smith   /* allocate space for row pointers */
3878b31e54SBarry Smith   *iia = ia = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(ia);
39*416022c9SBarry Smith   PetscZero(ia,(n+1)*sizeof(int));
4078b31e54SBarry Smith   work = (int *) PETSCMALLOC( (n+1)*sizeof(int) ); CHKPTRQ(work);
412d9e4a2aSBarry Smith 
422d9e4a2aSBarry Smith   /* determine the number of columns in each row */
432d9e4a2aSBarry Smith   ia[0] = 1;
442d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
45*416022c9SBarry Smith     nz = A->i[row+1] - A->i[row];
46*416022c9SBarry Smith     j  = A->j + A->i[row] + shift;
472d9e4a2aSBarry Smith     while (nz--) {
48dbb450caSBarry Smith        col = *j++ + shift;
49*416022c9SBarry Smith        if ( col > row ) { break;}
502d9e4a2aSBarry Smith        if (col != row) ia[row+1]++;
512d9e4a2aSBarry Smith        ia[col+1]++;
522d9e4a2aSBarry Smith     }
532d9e4a2aSBarry Smith   }
542d9e4a2aSBarry Smith 
552d9e4a2aSBarry Smith   /* shift ia[i] to point to next row */
562d9e4a2aSBarry Smith   for ( i=1; i<n+1; i++ ) {
572d9e4a2aSBarry Smith     row       = ia[i-1];
582d9e4a2aSBarry Smith     ia[i]     += row;
592d9e4a2aSBarry Smith     work[i-1] = row - 1;
602d9e4a2aSBarry Smith   }
612d9e4a2aSBarry Smith 
622d9e4a2aSBarry Smith   /* allocate space for column pointers */
63dbb450caSBarry Smith   nz = ia[n] + (!shift);
6478b31e54SBarry Smith   *jja = ja = (int *) PETSCMALLOC( nz*sizeof(int) ); CHKPTRQ(ja);
652d9e4a2aSBarry Smith 
662d9e4a2aSBarry Smith   /* loop over lower triangular part putting into ja */
672d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
68*416022c9SBarry Smith     nz = A->i[row+1] - A->i[row];
69*416022c9SBarry Smith     j  = A->j + A->i[row] + shift;
702d9e4a2aSBarry Smith     while (nz--) {
71dbb450caSBarry Smith        col = *j++ + shift;
72*416022c9SBarry Smith        if ( col > row ) { break;}
73*416022c9SBarry Smith        if (col != row) {wr = work[col]; work[col] = wr + 1; ja[wr] = row + 1; }
742d9e4a2aSBarry Smith        wr = work[row]; work[row] = wr + 1;
752d9e4a2aSBarry Smith        ja[wr] = col + 1;
762d9e4a2aSBarry Smith     }
772d9e4a2aSBarry Smith   }
7878b31e54SBarry Smith   PETSCFREE(work);
792d9e4a2aSBarry Smith   return 0;
802d9e4a2aSBarry Smith }
812d9e4a2aSBarry Smith 
82