xref: /petsc/src/mat/impls/aij/seq/ij.c (revision c8a8475e04bcaa43590892a5c3e60c6f87bc31f7)
1 /*$Id: ij.c,v 1.38 2001/03/23 23:21:51 balay Exp $*/
2 
3 #include "src/mat/impls/aij/seq/aij.h"
4 
5 #undef __FUNCT__
6 #define __FUNCT__ "MatToSymmetricIJ_SeqAIJ"
7 /*
8   MatToSymmetricIJ_SeqAIJ - Convert a (generally nonsymmetric) sparse AIJ matrix
9            to IJ format (ignore the "A" part) Allocates the space needed. Uses only
10            the lower triangular part of the matrix.
11 
12     Description:
13     Take the data in the row-oriented sparse storage and build the
14     IJ data for the Matrix.  Return 0 on success,row + 1 on failure
15     at that row. Produces the ij for a symmetric matrix by only using
16     the lower triangular part of the matrix.
17 
18     Input Parameters:
19 .   Matrix - matrix to convert
20 .   shiftin - the shift for the original matrix (0 or 1)
21 .   shiftout - the shift required for the ordering routine (0 or 1)
22 
23     Output Parameters:
24 .   ia     - ia part of IJ representation (row information)
25 .   ja     - ja part (column indices)
26 
27     Notes:
28     Both ia and ja may be freed with PetscFree();
29     This routine is provided for ordering routines that require a
30     symmetric structure.  It is required since those routines call
31     SparsePak routines that expect a symmetric  matrix.
32 */
33 int MatToSymmetricIJ_SeqAIJ(int m,int *ai,int *aj,int shiftin,int shiftout,int **iia,int **jja)
34 {
35   int *work,*ia,*ja,*j,i,nz,row,col,ierr;
36 
37   PetscFunctionBegin;
38   /* allocate space for row pointers */
39   ierr = PetscMalloc((m+1)*sizeof(int),&ia);CHKERRQ(ierr);
40   *iia = ia;
41   ierr = PetscMemzero(ia,(m+1)*sizeof(int));CHKERRQ(ierr);
42   ierr = PetscMalloc((m+1)*sizeof(int),&work);CHKERRQ(ierr);
43 
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   }
56 
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   }
63 
64   /* allocate space for column pointers */
65   nz   = ia[m] + (!shiftin);
66   ierr = PetscMalloc(nz*sizeof(int),&ja);CHKERRQ(ierr);
67   *jja = ja;
68 
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   ierr = PetscFree(work);CHKERRQ(ierr);
81   PetscFunctionReturn(0);
82 }
83 
84 
85 
86