xref: /petsc/src/mat/impls/aij/seq/ij.c (revision 3439631b557b9d2230e65a2095f4b483ebd8a14e)
1cb512458SBarry Smith #ifndef lint
2*3439631bSBarry Smith static char vcid[] = "$Id: ij.c,v 1.14 1995/11/02 04:28:29 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
9d5d45c9bSBarry Smith            (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
202d9e4a2aSBarry Smith 
212d9e4a2aSBarry Smith     Output Parameters:
222d9e4a2aSBarry Smith .   ia     - ia part of IJ representation (row information)
232d9e4a2aSBarry Smith .   ja     - ja part (column indices)
242d9e4a2aSBarry Smith 
252d9e4a2aSBarry Smith     Notes:
260452661fSBarry Smith $    Both ia and ja may be freed with PetscFree();
27464493b3SBarry Smith $    This routine is provided for ordering routines that require a
28464493b3SBarry Smith $    symmetric structure.  It is used in SpOrder (and derivatives) since
29464493b3SBarry Smith $    those routines call SparsePak routines that expect a symmetric
30464493b3SBarry Smith $    matrix.
312d9e4a2aSBarry Smith */
32416022c9SBarry Smith int MatToSymmetricIJ_SeqAIJ( Mat_SeqAIJ *A, int **iia, int **jja )
332d9e4a2aSBarry Smith {
34*3439631bSBarry Smith   int *work,*ia,*ja,*j,i, nz, n = A->m, row, col, shift = A->indexshift;
35*3439631bSBarry Smith   int *ai = A->i, *aj = A->j + shift;
362d9e4a2aSBarry Smith 
372d9e4a2aSBarry Smith   /* allocate space for row pointers */
380452661fSBarry Smith   *iia = ia = (int *) PetscMalloc( (n+1)*sizeof(int) ); CHKPTRQ(ia);
39cddf8d76SBarry Smith   PetscMemzero(ia,(n+1)*sizeof(int));
400452661fSBarry 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*3439631bSBarry Smith     nz = ai[row+1] - ai[row];
46*3439631bSBarry Smith     j  = aj + ai[row];
472d9e4a2aSBarry Smith     while (nz--) {
48dbb450caSBarry Smith        col = *j++ + shift;
49416022c9SBarry 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);
640452661fSBarry 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*3439631bSBarry Smith     nz = ai[row+1] - ai[row];
69*3439631bSBarry Smith     j  = aj + ai[row];
702d9e4a2aSBarry Smith     while (nz--) {
71dbb450caSBarry Smith       col = *j++ + shift;
72416022c9SBarry Smith       if (col > row) { break;}
73*3439631bSBarry Smith       if (col != row) {ja[work[col]++] = row + 1; }
74*3439631bSBarry Smith       ja[work[row]++] = col + 1;
752d9e4a2aSBarry Smith     }
762d9e4a2aSBarry Smith   }
770452661fSBarry Smith   PetscFree(work);
782d9e4a2aSBarry Smith   return 0;
792d9e4a2aSBarry Smith }
802d9e4a2aSBarry Smith 
81d5d45c9bSBarry Smith 
82d5d45c9bSBarry Smith 
83