1 | /* |
---|
2 | * ----------------------------------------------------------------- |
---|
3 | * $Revision$ |
---|
4 | * $Date$ |
---|
5 | * ----------------------------------------------------------------- |
---|
6 | * Programmer: Carol Woodward, Slaven Peles @ LLNL, |
---|
7 | * Daniel R. Reynolds @ SMU. |
---|
8 | * ----------------------------------------------------------------- |
---|
9 | * For details, see the LICENSE file. |
---|
10 | * ----------------------------------------------------------------- |
---|
11 | * This header file contains definitions and declarations for use by |
---|
12 | * sparse linear solvers for Ax = b. |
---|
13 | * ----------------------------------------------------------------- |
---|
14 | */ |
---|
15 | |
---|
16 | #ifndef _SUNDIALS_SPARSE_H |
---|
17 | #define _SUNDIALS_SPARSE_H |
---|
18 | |
---|
19 | #include <stdio.h> |
---|
20 | |
---|
21 | #include <sundials/sundials_types.h> |
---|
22 | #include <sundials/sundials_direct.h> |
---|
23 | |
---|
24 | #ifdef __cplusplus /* wrapper to enable C++ usage */ |
---|
25 | extern "C" { |
---|
26 | #endif |
---|
27 | |
---|
28 | /* |
---|
29 | * ================================================================== |
---|
30 | * Type definitions |
---|
31 | * ================================================================== |
---|
32 | */ |
---|
33 | |
---|
34 | #define CSC_MAT 0 |
---|
35 | #define CSR_MAT 1 |
---|
36 | |
---|
37 | /* |
---|
38 | * ----------------------------------------------------------------- |
---|
39 | * Type : SlsMat |
---|
40 | * ----------------------------------------------------------------- |
---|
41 | * The type SlsMat is defined to be a pointer to a structure |
---|
42 | * with various sizes, a data field, and arrays for the row and |
---|
43 | * column information for the sparse matrix entries. |
---|
44 | * The M and N fields indicates the number |
---|
45 | * of rows and columns, respectively. The data field is a one |
---|
46 | * dimensional array used for component storage. The NNZ field indicates |
---|
47 | * the number of nonzero entries in the matrix. The integer array, asub, |
---|
48 | * holds the row index for each of the matrix entries. The integer |
---|
49 | * array, xa, holds the index entry for the starting value of each column. |
---|
50 | * ----------------------------------------------------------------- |
---|
51 | * The relevant fields in DlsMat are: |
---|
52 | * M - number of rows |
---|
53 | * N - number of columns |
---|
54 | * NNZ - the number of nonzero entries in the matrix |
---|
55 | * NP - number of index pointers |
---|
56 | * data - pointer to a contiguous block of realtype variables |
---|
57 | * sparsetype - type of sparse matrix: compressed sparse column or row |
---|
58 | * indexvals - indices of each nonzero entry (columns or rows) |
---|
59 | * indexptrs - starting index of the first entry in data for each slice |
---|
60 | * rowvals - pointer to row indices of each nonzero entry |
---|
61 | * colptrs - pointer to starting indices in data array for each column |
---|
62 | * colvals - pointer to column indices of each nonzero entry |
---|
63 | * rowptrs - pointer to starting indices in data array for each row |
---|
64 | * |
---|
65 | * The nonzero entries of the matrix are stored in |
---|
66 | * compressed column format. Row indices of entries in |
---|
67 | * column j are stored in rowvals[colptrs[j]] through rowvals[colptrs[j+i]-1] |
---|
68 | * and corresponding numerical values of the matrix are stored |
---|
69 | * in the same entries of data. |
---|
70 | * ----------------------------------------------------------------- |
---|
71 | */ |
---|
72 | |
---|
73 | typedef struct _SlsMat { |
---|
74 | int M; |
---|
75 | int N; |
---|
76 | int NNZ; |
---|
77 | int NP; |
---|
78 | realtype *data; |
---|
79 | int sparsetype; |
---|
80 | int *indexvals; |
---|
81 | int *indexptrs; |
---|
82 | /* CSC indices */ |
---|
83 | int **rowvals; |
---|
84 | int **colptrs; |
---|
85 | /* CSR indices */ |
---|
86 | int **colvals; |
---|
87 | int **rowptrs; |
---|
88 | } *SlsMat; |
---|
89 | |
---|
90 | /* |
---|
91 | * ================================================================== |
---|
92 | * Exported function prototypes (functions working on SlsMat) |
---|
93 | * ================================================================== |
---|
94 | */ |
---|
95 | |
---|
96 | /* |
---|
97 | * ----------------------------------------------------------------- |
---|
98 | * Function: SparseNewMat |
---|
99 | * ----------------------------------------------------------------- |
---|
100 | * SparseNewMat allocates memory for a compressed column sparse |
---|
101 | * matrix with M rows, N columns, NNZ nonzeros and of sparsetype |
---|
102 | * type (CSC or CSR matrix). SparseNewMat returns NULL if the |
---|
103 | * request for matrix storage cannot be satisfied. See the above |
---|
104 | * documentation for the type SlsMat for matrix storage details. |
---|
105 | * ----------------------------------------------------------------- |
---|
106 | */ |
---|
107 | |
---|
108 | SUNDIALS_EXPORT SlsMat SparseNewMat(int M, int N, int NNZ, int sparsetype); |
---|
109 | |
---|
110 | /* |
---|
111 | * ----------------------------------------------------------------- |
---|
112 | * Function: SparseFromDenseMat |
---|
113 | * ----------------------------------------------------------------- |
---|
114 | * SlsConvertDense creates a new CSC matrix from an existing |
---|
115 | * dense/band matrix by copying all nonzero values into the sparse |
---|
116 | * matrix structure. SlsConvertDense returns NULL if the request |
---|
117 | * for matrix storage cannot be satisfied. |
---|
118 | * ----------------------------------------------------------------- |
---|
119 | */ |
---|
120 | |
---|
121 | SUNDIALS_EXPORT SlsMat SparseFromDenseMat(const DlsMat A, int sparsetype); |
---|
122 | |
---|
123 | /* |
---|
124 | * ----------------------------------------------------------------- |
---|
125 | * Functions: SparseDestroyMat |
---|
126 | * ----------------------------------------------------------------- |
---|
127 | * SparseDestroyMat frees the memory allocated by SparseNewMat |
---|
128 | * ----------------------------------------------------------------- |
---|
129 | */ |
---|
130 | |
---|
131 | SUNDIALS_EXPORT int SparseDestroyMat(SlsMat A); |
---|
132 | |
---|
133 | /* |
---|
134 | * ----------------------------------------------------------------- |
---|
135 | * Function : SparseSetMatToZero |
---|
136 | * ----------------------------------------------------------------- |
---|
137 | * SetToZero sets all the elements of the sparse matrix A to 0.0. |
---|
138 | * ----------------------------------------------------------------- |
---|
139 | */ |
---|
140 | |
---|
141 | SUNDIALS_EXPORT int SparseSetMatToZero(SlsMat A); |
---|
142 | |
---|
143 | /* |
---|
144 | * ----------------------------------------------------------------- |
---|
145 | * Functions: SparseCopyMat |
---|
146 | * ----------------------------------------------------------------- |
---|
147 | * This function copies sparse matrix A into sparse matrix B. |
---|
148 | * ----------------------------------------------------------------- |
---|
149 | */ |
---|
150 | |
---|
151 | SUNDIALS_EXPORT int SparseCopyMat(const SlsMat A, SlsMat B); |
---|
152 | |
---|
153 | /* |
---|
154 | * ----------------------------------------------------------------- |
---|
155 | * Functions: SparseScaleMat |
---|
156 | * ----------------------------------------------------------------- |
---|
157 | * This function scales all data entries of a sparse matrix A |
---|
158 | * by the realtype number in b. |
---|
159 | * ----------------------------------------------------------------- |
---|
160 | */ |
---|
161 | |
---|
162 | SUNDIALS_EXPORT int SparseScaleMat(realtype b, SlsMat A); |
---|
163 | |
---|
164 | /* |
---|
165 | * ----------------------------------------------------------------- |
---|
166 | * Functions: SparseAddIdentityMat |
---|
167 | * ----------------------------------------------------------------- |
---|
168 | * This function adds 1 to every diagonal entry of A. |
---|
169 | * Note that the resulting matrix may have more nonzero entries than |
---|
170 | * the original. This is accounted for, so that the return matrix |
---|
171 | * may be larger than the one sent in. |
---|
172 | * ----------------------------------------------------------------- |
---|
173 | */ |
---|
174 | |
---|
175 | SUNDIALS_EXPORT int SparseAddIdentityMat(SlsMat A); |
---|
176 | |
---|
177 | /* |
---|
178 | * ----------------------------------------------------------------- |
---|
179 | * Functions: SparseAddMat |
---|
180 | * ----------------------------------------------------------------- |
---|
181 | * This function adds two sparse matrices: A = A+B. |
---|
182 | * Note that the resulting matrix may have more nonzero entries than |
---|
183 | * either of the original matrices. This is accounted for, so that |
---|
184 | * the return matrix may be larger than the ones sent in. Upon |
---|
185 | * successful completion, the return value is zero; otherwise 1 is |
---|
186 | * returned. |
---|
187 | * ----------------------------------------------------------------- |
---|
188 | */ |
---|
189 | |
---|
190 | SUNDIALS_EXPORT int SparseAddMat(SlsMat A, const SlsMat B); |
---|
191 | |
---|
192 | /* |
---|
193 | * ----------------------------------------------------------------- |
---|
194 | * Functions: SparseReallocMat |
---|
195 | * ----------------------------------------------------------------- |
---|
196 | * This function reallocs internal arrays so that the resulting matrix |
---|
197 | * holds colptrs[N] nonzeros. |
---|
198 | * ----------------------------------------------------------------- |
---|
199 | */ |
---|
200 | |
---|
201 | SUNDIALS_EXPORT int SparseReallocMat(SlsMat A); |
---|
202 | |
---|
203 | /* |
---|
204 | * ----------------------------------------------------------------- |
---|
205 | * Functions: SparseMatvec |
---|
206 | * ----------------------------------------------------------------- |
---|
207 | * This function computes the matrix-vector product, y=A*x, where A |
---|
208 | * is a sparse matrix of dimension MxN, x is a realtype array of |
---|
209 | * length N, and y is a realtype array of length M. Upon successful |
---|
210 | * completion, the return value is zero; otherwise 1 is returned. |
---|
211 | * ----------------------------------------------------------------- |
---|
212 | */ |
---|
213 | |
---|
214 | SUNDIALS_EXPORT int SparseMatvec(const SlsMat A, const realtype *x, realtype *y); |
---|
215 | |
---|
216 | /* |
---|
217 | * ----------------------------------------------------------------- |
---|
218 | * Functions: SparsePrintMat |
---|
219 | * ----------------------------------------------------------------- |
---|
220 | * This function prints the compressed column matrix information for |
---|
221 | * matrix A to standard output. |
---|
222 | * It is intended as a debugging tool with small values of NNZ. |
---|
223 | * The elements are printed using the %g/%lg/%Lg option. |
---|
224 | * A blank line is printed before and after the matrix. |
---|
225 | * ----------------------------------------------------------------- |
---|
226 | */ |
---|
227 | |
---|
228 | SUNDIALS_EXPORT void SparsePrintMat(const SlsMat A, FILE* outfile); |
---|
229 | |
---|
230 | |
---|
231 | |
---|
232 | |
---|
233 | #ifdef __cplusplus |
---|
234 | } |
---|
235 | #endif |
---|
236 | |
---|
237 | #endif |
---|