xref: /petsc/src/mat/impls/aij/seq/ij.c (revision cb51245867d6a442a15a98eb5f86fd750dfb6de4)
1*cb512458SBarry Smith #ifndef lint
2*cb512458SBarry Smith static char vcid[] = "$Id: $";
3*cb512458SBarry Smith #endif
4d6dfbf8fSBarry Smith 
52d9e4a2aSBarry Smith 
62d9e4a2aSBarry Smith #include "aij.h"
72d9e4a2aSBarry Smith 
82d9e4a2aSBarry Smith /*
92d9e4a2aSBarry Smith   SpToSymmetricIJ - Convert a sparse AIJ matrix to IJ format
102d9e4a2aSBarry Smith            (ignore the "A" part)
112d9e4a2aSBarry Smith            Allocates the space needed. Uses only the lower triangular
122d9e4a2aSBarry Smith            part of the matrix.
132d9e4a2aSBarry Smith 
142d9e4a2aSBarry Smith     Description:
152d9e4a2aSBarry Smith     Take the data in the row-oriented sparse storage and build the
162d9e4a2aSBarry Smith     IJ data for the Matrix.  Return 0 on success, row + 1 on failure
172d9e4a2aSBarry Smith     at that row. Produces the ij for a symmetric matrix by only using
182d9e4a2aSBarry Smith     the lower triangular part of the matrix.
192d9e4a2aSBarry Smith 
202d9e4a2aSBarry Smith     Input Parameters:
212d9e4a2aSBarry Smith .   Matrix - matrix to convert
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:
282d9e4a2aSBarry Smith     This routine is provided for ordering routines that require a
292d9e4a2aSBarry Smith     symmetric structure.  It is used in SpOrder (and derivatives) since
302d9e4a2aSBarry Smith     those routines call SparsePak routines that expect a symmetric
312d9e4a2aSBarry Smith     matrix.
322d9e4a2aSBarry Smith */
332d9e4a2aSBarry Smith int SpToSymmetricIJ( Matiaij *Matrix, int **iia, int **jja )
342d9e4a2aSBarry Smith {
352d9e4a2aSBarry Smith   int          *work,*ia,*ja,*j,i, nz, n, row, wr;
362d9e4a2aSBarry Smith   register int col;
372d9e4a2aSBarry Smith 
382d9e4a2aSBarry Smith   n  = Matrix->m;
392d9e4a2aSBarry Smith 
402d9e4a2aSBarry Smith   /* allocate space for row pointers */
412d9e4a2aSBarry Smith   *iia = ia = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(ia);
422d9e4a2aSBarry Smith   MEMSET(ia,0,(n+1)*sizeof(int));
432d9e4a2aSBarry Smith   work = (int *) MALLOC( (n+1)*sizeof(int) ); CHKPTR(work);
442d9e4a2aSBarry Smith 
452d9e4a2aSBarry Smith   /* determine the number of columns in each row */
462d9e4a2aSBarry Smith   ia[0] = 1;
472d9e4a2aSBarry Smith   for (row = 0; row < n; row++) {
482d9e4a2aSBarry Smith     nz = Matrix->i[row+1] - Matrix->i[row];
492d9e4a2aSBarry Smith     j  = Matrix->j + Matrix->i[row] - 1;
502d9e4a2aSBarry Smith     while (nz--) {
51289bc588SBarry Smith        col = *j++ - 1;
522d9e4a2aSBarry Smith        if ( col > row ) {
532d9e4a2aSBarry Smith           break;
542d9e4a2aSBarry Smith        }
552d9e4a2aSBarry Smith        if (col != row) ia[row+1]++;
562d9e4a2aSBarry Smith        ia[col+1]++;
572d9e4a2aSBarry Smith     }
582d9e4a2aSBarry Smith  }
592d9e4a2aSBarry Smith 
602d9e4a2aSBarry Smith  /* shift ia[i] to point to next row */
612d9e4a2aSBarry Smith  for ( i=1; i<n+1; i++ ) {
622d9e4a2aSBarry Smith    row       = ia[i-1];
632d9e4a2aSBarry Smith    ia[i]     += row;
642d9e4a2aSBarry Smith    work[i-1] = row - 1;
652d9e4a2aSBarry Smith  }
662d9e4a2aSBarry Smith 
672d9e4a2aSBarry Smith  /* allocate space for column pointers */
682d9e4a2aSBarry Smith  nz = ia[n];
692d9e4a2aSBarry Smith  *jja = ja = (int *) MALLOC( nz*sizeof(int) ); CHKPTR(ja);
702d9e4a2aSBarry Smith 
712d9e4a2aSBarry Smith  /* loop over lower triangular part putting into ja */
722d9e4a2aSBarry Smith  for (row = 0; row < n; row++) {
732d9e4a2aSBarry Smith     nz = Matrix->i[row+1] - Matrix->i[row];
742d9e4a2aSBarry Smith     j  = Matrix->j + Matrix->i[row] - 1;
752d9e4a2aSBarry Smith     while (nz--) {
76289bc588SBarry Smith        col = *j++ - 1;
772d9e4a2aSBarry Smith        if ( col > row ) {
782d9e4a2aSBarry Smith           break;
792d9e4a2aSBarry Smith        }
802d9e4a2aSBarry Smith        if (col != row) {wr = work[col]; work[col] = wr + 1;
812d9e4a2aSBarry Smith 			ja[wr] = row + 1; }
822d9e4a2aSBarry Smith        wr = work[row]; work[row] = wr + 1;
832d9e4a2aSBarry Smith        ja[wr] = col + 1;
842d9e4a2aSBarry Smith     }
85289bc588SBarry Smith 
862d9e4a2aSBarry Smith   }
872d9e4a2aSBarry Smith   FREE(work);
882d9e4a2aSBarry Smith   return 0;
892d9e4a2aSBarry Smith }
902d9e4a2aSBarry Smith 
91