00001 /* 00002 * ----------------------------------------------------------------- 00003 * $Revision: 1.2 $ 00004 * $Date: 2006/11/29 00:05:07 $ 00005 * ----------------------------------------------------------------- 00006 * Programmer: Radu Serban @ LLNL 00007 * ----------------------------------------------------------------- 00008 * Copyright (c) 2006, The Regents of the University of California. 00009 * Produced at the Lawrence Livermore National Laboratory. 00010 * All rights reserved. 00011 * For details, see the LICENSE file. 00012 * ----------------------------------------------------------------- 00013 * This header file contains definitions and declarations for use by 00014 * generic direct linear solvers for Ax = b. It defines types for 00015 * dense and banded matrices and corresponding accessor macros. 00016 * ----------------------------------------------------------------- 00017 */ 00018 00019 #ifndef _SUNDIALS_DIRECT_H 00020 #define _SUNDIALS_DIRECT_H 00021 00022 #ifdef __cplusplus /* wrapper to enable C++ usage */ 00023 extern "C" { 00024 #endif 00025 00026 #include <sundials/sundials_types.h> 00027 00028 /* 00029 * ================================================================= 00030 * C O N S T A N T S 00031 * ================================================================= 00032 */ 00033 00034 /* 00035 * SUNDIALS_DENSE: dense matrix 00036 * SUNDIALS_BAND: banded matrix 00037 */ 00038 00039 #define SUNDIALS_DENSE 1 00040 #define SUNDIALS_BAND 2 00041 00042 /* 00043 * ================================================================== 00044 * Type definitions 00045 * ================================================================== 00046 */ 00047 00048 /* 00049 * ----------------------------------------------------------------- 00050 * Type : DlsMat 00051 * ----------------------------------------------------------------- 00052 * The type DlsMat is defined to be a pointer to a structure 00053 * with various sizes, a data field, and an array of pointers to 00054 * the columns which defines a dense or band matrix for use in 00055 * direct linear solvers. The M and N fields indicates the number 00056 * of rows and columns, respectively. The data field is a one 00057 * dimensional array used for component storage. The cols field 00058 * stores the pointers in data for the beginning of each column. 00059 * ----------------------------------------------------------------- 00060 * For DENSE matrices, the relevant fields in DlsMat are: 00061 * type = SUNDIALS_DENSE 00062 * M - number of rows 00063 * N - number of columns 00064 * ldim - leading dimension (ldim >= M) 00065 * data - pointer to a contiguous block of realtype variables 00066 * ldata - length of the data array =ldim*N 00067 * cols - array of pointers. cols[j] points to the first element 00068 * of the j-th column of the matrix in the array data. 00069 * 00070 * The elements of a dense matrix are stored columnwise (i.e columns 00071 * are stored one on top of the other in memory). 00072 * If A is of type DlsMat, then the (i,j)th element of A (with 00073 * 0 <= i < M and 0 <= j < N) is given by (A->data)[j*n+i]. 00074 * 00075 * The DENSE_COL and DENSE_ELEM macros below allow a user to access 00076 * efficiently individual matrix elements without writing out explicit 00077 * data structure references and without knowing too much about the 00078 * underlying element storage. The only storage assumption needed is 00079 * that elements are stored columnwise and that a pointer to the 00080 * jth column of elements can be obtained via the DENSE_COL macro. 00081 * ----------------------------------------------------------------- 00082 * For BAND matrices, the relevant fields in DlsMat are: 00083 * type = SUNDIALS_BAND 00084 * M - number of rows 00085 * N - number of columns 00086 * mu - upper bandwidth, 0 <= mu <= min(M,N) 00087 * ml - lower bandwidth, 0 <= ml <= min(M,N) 00088 * s_mu - storage upper bandwidth, mu <= s_mu <= N-1. 00089 * The dgbtrf routine writes the LU factors into the storage 00090 * for A. The upper triangular factor U, however, may have 00091 * an upper bandwidth as big as MIN(N-1,mu+ml) because of 00092 * partial pivoting. The s_mu field holds the upper 00093 * bandwidth allocated for A. 00094 * ldim - leading dimension (ldim >= s_mu) 00095 * data - pointer to a contiguous block of realtype variables 00096 * ldata - length of the data array =ldim*(s_mu+ml+1) 00097 * cols - array of pointers. cols[j] points to the first element 00098 * of the j-th column of the matrix in the array data. 00099 * 00100 * The BAND_COL, BAND_COL_ELEM, and BAND_ELEM macros below allow a 00101 * user to access individual matrix elements without writing out 00102 * explicit data structure references and without knowing too much 00103 * about the underlying element storage. The only storage assumption 00104 * needed is that elements are stored columnwise and that a pointer 00105 * into the jth column of elements can be obtained via the BAND_COL 00106 * macro. The BAND_COL_ELEM macro selects an element from a column 00107 * which has already been isolated via BAND_COL. The macro 00108 * BAND_COL_ELEM allows the user to avoid the translation 00109 * from the matrix location (i,j) to the index in the array returned 00110 * by BAND_COL at which the (i,j)th element is stored. 00111 * ----------------------------------------------------------------- 00112 */ 00113 00114 typedef struct _DlsMat { 00115 int type; 00116 int M; 00117 int N; 00118 int ldim; 00119 int mu; 00120 int ml; 00121 int s_mu; 00122 realtype *data; 00123 int ldata; 00124 realtype **cols; 00125 } *DlsMat; 00126 00127 /* 00128 * ================================================================== 00129 * Data accessor macros 00130 * ================================================================== 00131 */ 00132 00133 /* 00134 * ----------------------------------------------------------------- 00135 * DENSE_COL and DENSE_ELEM 00136 * ----------------------------------------------------------------- 00137 * 00138 * DENSE_COL(A,j) references the jth column of the M-by-N dense 00139 * matrix A, 0 <= j < N. The type of the expression DENSE_COL(A,j) 00140 * is (realtype *). After the assignment in the usage above, col_j 00141 * may be treated as an array indexed from 0 to M-1. The (i,j)-th 00142 * element of A is thus referenced by col_j[i]. 00143 * 00144 * DENSE_ELEM(A,i,j) references the (i,j)th element of the dense 00145 * M-by-N matrix A, 0 <= i < M ; 0 <= j < N. 00146 * 00147 * ----------------------------------------------------------------- 00148 */ 00149 00150 #define DENSE_COL(A,j) ((A->cols)[j]) 00151 #define DENSE_ELEM(A,i,j) ((A->cols)[j][i]) 00152 00153 /* 00154 * ----------------------------------------------------------------- 00155 * BAND_COL, BAND_COL_ELEM, and BAND_ELEM 00156 * ----------------------------------------------------------------- 00157 * 00158 * BAND_COL(A,j) references the diagonal element of the jth column 00159 * of the N by N band matrix A, 0 <= j <= N-1. The type of the 00160 * expression BAND_COL(A,j) is realtype *. The pointer returned by 00161 * the call BAND_COL(A,j) can be treated as an array which is 00162 * indexed from -(A->mu) to (A->ml). 00163 * 00164 * BAND_COL_ELEM references the (i,j)th entry of the band matrix A 00165 * when used in conjunction with BAND_COL. The index (i,j) should 00166 * satisfy j-(A->mu) <= i <= j+(A->ml). 00167 * 00168 * BAND_ELEM(A,i,j) references the (i,j)th element of the M-by-N 00169 * band matrix A, where 0 <= i,j <= N-1. The location (i,j) should 00170 * further satisfy j-(A->mu) <= i <= j+(A->ml). 00171 * 00172 * ----------------------------------------------------------------- 00173 */ 00174 00175 #define BAND_COL(A,j) (((A->cols)[j])+(A->s_mu)) 00176 #define BAND_COL_ELEM(col_j,i,j) (col_j[(i)-(j)]) 00177 #define BAND_ELEM(A,i,j) ((A->cols)[j][(i)-(j)+(A->s_mu)]) 00178 00179 /* 00180 * ================================================================== 00181 * Exported function prototypes (functions working on dlsMat) 00182 * ================================================================== 00183 */ 00184 00185 /* 00186 * ----------------------------------------------------------------- 00187 * Function: NewDenseMat 00188 * ----------------------------------------------------------------- 00189 * NewDenseMat allocates memory for an M-by-N dense matrix and 00190 * returns the storage allocated (type DlsMat). NewDenseMat 00191 * returns NULL if the request for matrix storage cannot be 00192 * satisfied. See the above documentation for the type DlsMat 00193 * for matrix storage details. 00194 * ----------------------------------------------------------------- 00195 */ 00196 00197 SUNDIALS_EXPORT DlsMat NewDenseMat(int M, int N); 00198 00199 /* 00200 * ----------------------------------------------------------------- 00201 * Function: NewBandMat 00202 * ----------------------------------------------------------------- 00203 * NewBandMat allocates memory for an M-by-N band matrix 00204 * with upper bandwidth mu, lower bandwidth ml, and storage upper 00205 * bandwidth smu. Pass smu as follows depending on whether A will 00206 * be LU factored: 00207 * 00208 * (1) Pass smu = mu if A will not be factored. 00209 * 00210 * (2) Pass smu = MIN(N-1,mu+ml) if A will be factored. 00211 * 00212 * NewBandMat returns the storage allocated (type DlsMat) or 00213 * NULL if the request for matrix storage cannot be satisfied. 00214 * See the documentation for the type DlsMat for matrix storage 00215 * details. 00216 * ----------------------------------------------------------------- 00217 */ 00218 00219 SUNDIALS_EXPORT DlsMat NewBandMat(int N, int mu, int ml, int smu); 00220 00221 /* 00222 * ----------------------------------------------------------------- 00223 * Functions: DestroyMat 00224 * ----------------------------------------------------------------- 00225 * DestroyMat frees the memory allocated by NewDenseMat or NewBandMat 00226 * ----------------------------------------------------------------- 00227 */ 00228 00229 SUNDIALS_EXPORT void DestroyMat(DlsMat A); 00230 00231 /* 00232 * ----------------------------------------------------------------- 00233 * Function: NewIntArray 00234 * ----------------------------------------------------------------- 00235 * NewIntArray allocates memory an array of N integers and returns 00236 * the pointer to the memory it allocates. If the request for 00237 * memory storage cannot be satisfied, it returns NULL. 00238 * ----------------------------------------------------------------- 00239 */ 00240 00241 SUNDIALS_EXPORT int *NewIntArray(int N); 00242 00243 /* 00244 * ----------------------------------------------------------------- 00245 * Function: NewRealArray 00246 * ----------------------------------------------------------------- 00247 * NewRealArray allocates memory an array of N realtype and returns 00248 * the pointer to the memory it allocates. If the request for 00249 * memory storage cannot be satisfied, it returns NULL. 00250 * ----------------------------------------------------------------- 00251 */ 00252 00253 SUNDIALS_EXPORT realtype *NewRealArray(int N); 00254 00255 /* 00256 * ----------------------------------------------------------------- 00257 * Function: DestroyArray 00258 * ----------------------------------------------------------------- 00259 * DestroyArray frees memory allocated by NewIntArray or by 00260 * NewRealArray. 00261 * ----------------------------------------------------------------- 00262 */ 00263 00264 SUNDIALS_EXPORT void DestroyArray(void *p); 00265 00266 /* 00267 * ----------------------------------------------------------------- 00268 * Functions: PrintMat 00269 * ----------------------------------------------------------------- 00270 * This function prints the M-by-N (dense or band) matrix A to 00271 * standard output as it would normally appear on paper. 00272 * It is intended as debugging tools with small values of M and N. 00273 * The elements are printed using the %g/%lg/%Lg option. 00274 * A blank line is printed before and after the matrix. 00275 * ----------------------------------------------------------------- 00276 */ 00277 00278 SUNDIALS_EXPORT void PrintMat(DlsMat A); 00279 00280 /* 00281 * ================================================================== 00282 * Exported function prototypes (functions working on realtype**) 00283 * ================================================================== 00284 */ 00285 00286 SUNDIALS_EXPORT realtype **newDenseMat(int m, int n); 00287 SUNDIALS_EXPORT realtype **newBandMat(int n, int smu, int ml); 00288 SUNDIALS_EXPORT void destroyMat(realtype **a); 00289 SUNDIALS_EXPORT int *newIntArray(int n); 00290 SUNDIALS_EXPORT realtype *newRealArray(int m); 00291 SUNDIALS_EXPORT void destroyArray(void *v); 00292 00293 #ifdef __cplusplus 00294 } 00295 #endif 00296 00297 #endif