00001 /* 00002 * ----------------------------------------------------------------- 00003 * $Revision: 1.4 $ 00004 * $Date: 2006/11/29 00:05:06 $ 00005 * ----------------------------------------------------------------- 00006 * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh and 00007 * Radu Serban @ LLNL 00008 * ----------------------------------------------------------------- 00009 * Copyright (c) 2002, The Regents of the University of California. 00010 * Produced at the Lawrence Livermore National Laboratory. 00011 * All rights reserved. 00012 * For details, see the LICENSE file. 00013 * ----------------------------------------------------------------- 00014 * This is the common header file for the Scaled, Preconditioned 00015 * Iterative Linear Solvers in CVODE. 00016 * ----------------------------------------------------------------- 00017 */ 00018 00019 #ifndef _CVSPILS_H 00020 #define _CVSPILS_H 00021 00022 #ifdef __cplusplus /* wrapper to enable C++ usage */ 00023 extern "C" { 00024 #endif 00025 00026 #include <sundials/sundials_iterative.h> 00027 #include <sundials/sundials_nvector.h> 00028 00029 /* 00030 * ----------------------------------------------------------------- 00031 * CVSPILS solver constants 00032 * ----------------------------------------------------------------- 00033 * CVSPILS_MAXL : default value for the maximum Krylov 00034 * dimension 00035 * 00036 * CVSPILS_MSBPRE : maximum number of steps between 00037 * preconditioner evaluations 00038 * 00039 * CVSPILS_DGMAX : maximum change in gamma between 00040 * preconditioner evaluations 00041 * 00042 * CVSPILS_DELT : default value for factor by which the 00043 * tolerance on the nonlinear iteration is 00044 * multiplied to get a tolerance on the linear 00045 * iteration 00046 * ----------------------------------------------------------------- 00047 */ 00048 00049 #define CVSPILS_MAXL 5 00050 #define CVSPILS_MSBPRE 50 00051 #define CVSPILS_DGMAX RCONST(0.2) 00052 #define CVSPILS_DELT RCONST(0.05) 00053 00054 /* 00055 * ----------------------------------------------------------------- 00056 * Type : CVSpilsPrecSetupFn 00057 * ----------------------------------------------------------------- 00058 * The user-supplied preconditioner setup function PrecSetup and 00059 * the user-supplied preconditioner solve function PrecSolve 00060 * together must define left and right preconditoner matrices 00061 * P1 and P2 (either of which may be trivial), such that the 00062 * product P1*P2 is an approximation to the Newton matrix 00063 * M = I - gamma*J. Here J is the system Jacobian J = df/dy, 00064 * and gamma is a scalar proportional to the integration step 00065 * size h. The solution of systems P z = r, with P = P1 or P2, 00066 * is to be carried out by the PrecSolve function, and PrecSetup 00067 * is to do any necessary setup operations. 00068 * 00069 * The user-supplied preconditioner setup function PrecSetup 00070 * is to evaluate and preprocess any Jacobian-related data 00071 * needed by the preconditioner solve function PrecSolve. 00072 * This might include forming a crude approximate Jacobian, 00073 * and performing an LU factorization on the resulting 00074 * approximation to M. This function will not be called in 00075 * advance of every call to PrecSolve, but instead will be called 00076 * only as often as necessary to achieve convergence within the 00077 * Newton iteration. If the PrecSolve function needs no 00078 * preparation, the PrecSetup function can be NULL. 00079 * 00080 * For greater efficiency, the PrecSetup function may save 00081 * Jacobian-related data and reuse it, rather than generating it 00082 * from scratch. In this case, it should use the input flag jok 00083 * to decide whether to recompute the data, and set the output 00084 * flag *jcurPtr accordingly. 00085 * 00086 * Each call to the PrecSetup function is preceded by a call to 00087 * the RhsFn f with the same (t,y) arguments. Thus the PrecSetup 00088 * function can use any auxiliary data that is computed and 00089 * saved by the f function and made accessible to PrecSetup. 00090 * 00091 * A function PrecSetup must have the prototype given below. 00092 * Its parameters are as follows: 00093 * 00094 * t is the current value of the independent variable. 00095 * 00096 * y is the current value of the dependent variable vector, 00097 * namely the predicted value of y(t). 00098 * 00099 * fy is the vector f(t,y). 00100 * 00101 * jok is an input flag indicating whether Jacobian-related 00102 * data needs to be recomputed, as follows: 00103 * jok == FALSE means recompute Jacobian-related data 00104 * from scratch. 00105 * jok == TRUE means that Jacobian data, if saved from 00106 * the previous PrecSetup call, can be reused 00107 * (with the current value of gamma). 00108 * A Precset call with jok == TRUE can only occur after 00109 * a call with jok == FALSE. 00110 * 00111 * jcurPtr is a pointer to an output integer flag which is 00112 * to be set by PrecSetup as follows: 00113 * Set *jcurPtr = TRUE if Jacobian data was recomputed. 00114 * Set *jcurPtr = FALSE if Jacobian data was not recomputed, 00115 * but saved data was reused. 00116 * 00117 * gamma is the scalar appearing in the Newton matrix. 00118 * 00119 * P_data is a pointer to user data - the same as the P_data 00120 * parameter passed to the CV*SetPreconditioner function. 00121 * 00122 * tmp1, tmp2, and tmp3 are pointers to memory allocated 00123 * for N_Vectors which can be used by 00124 * CVSpilsPrecSetupFn as temporary storage or 00125 * work space. 00126 * 00127 * NOTE: If the user's preconditioner needs other quantities, 00128 * they are accessible as follows: hcur (the current stepsize) 00129 * and ewt (the error weight vector) are accessible through 00130 * CVodeGetCurrentStep and CVodeGetErrWeights, respectively). 00131 * The unit roundoff is available as UNIT_ROUNDOFF defined in 00132 * sundials_types.h. 00133 * 00134 * Returned value: 00135 * The value to be returned by the PrecSetup function is a flag 00136 * indicating whether it was successful. This value should be 00137 * 0 if successful, 00138 * > 0 for a recoverable error (step will be retried), 00139 * < 0 for an unrecoverable error (integration is halted). 00140 * ----------------------------------------------------------------- 00141 */ 00142 00143 typedef int (*CVSpilsPrecSetupFn)(realtype t, N_Vector y, N_Vector fy, 00144 booleantype jok, booleantype *jcurPtr, 00145 realtype gamma, void *P_data, 00146 N_Vector tmp1, N_Vector tmp2, 00147 N_Vector tmp3); 00148 00149 /* 00150 * ----------------------------------------------------------------- 00151 * Type : CVSpilsPrecSolveFn 00152 * ----------------------------------------------------------------- 00153 * The user-supplied preconditioner solve function PrecSolve 00154 * is to solve a linear system P z = r in which the matrix P is 00155 * one of the preconditioner matrices P1 or P2, depending on the 00156 * type of preconditioning chosen. 00157 * 00158 * A function PrecSolve must have the prototype given below. 00159 * Its parameters are as follows: 00160 * 00161 * t is the current value of the independent variable. 00162 * 00163 * y is the current value of the dependent variable vector. 00164 * 00165 * fy is the vector f(t,y). 00166 * 00167 * r is the right-hand side vector of the linear system. 00168 * 00169 * z is the output vector computed by PrecSolve. 00170 * 00171 * gamma is the scalar appearing in the Newton matrix. 00172 * 00173 * delta is an input tolerance for use by PSolve if it uses 00174 * an iterative method in its solution. In that case, 00175 * the residual vector Res = r - P z of the system 00176 * should be made less than delta in weighted L2 norm, 00177 * i.e., sqrt [ Sum (Res[i]*ewt[i])^2 ] < delta. 00178 * Note: the error weight vector ewt can be obtained 00179 * through a call to the routine CVodeGetErrWeights. 00180 * 00181 * lr is an input flag indicating whether PrecSolve is to use 00182 * the left preconditioner P1 or right preconditioner 00183 * P2: lr = 1 means use P1, and lr = 2 means use P2. 00184 * 00185 * P_data is a pointer to user data - the same as the P_data 00186 * parameter passed to the CV*SetPreconditioner function. 00187 * 00188 * tmp is a pointer to memory allocated for an N_Vector 00189 * which can be used by PSolve for work space. 00190 * 00191 * Returned value: 00192 * The value to be returned by the PrecSolve function is a flag 00193 * indicating whether it was successful. This value should be 00194 * 0 if successful, 00195 * positive for a recoverable error (step will be retried), 00196 * negative for an unrecoverable error (integration is halted). 00197 * ----------------------------------------------------------------- 00198 */ 00199 00200 typedef int (*CVSpilsPrecSolveFn)(realtype t, N_Vector y, N_Vector fy, 00201 N_Vector r, N_Vector z, 00202 realtype gamma, realtype delta, 00203 int lr, void *P_data, N_Vector tmp); 00204 00205 /* 00206 * ----------------------------------------------------------------- 00207 * Type : CVSpilsJacTimesVecFn 00208 * ----------------------------------------------------------------- 00209 * The user-supplied function jtimes is to generate the product 00210 * J*v for given v, where J is the Jacobian df/dy, or an 00211 * approximation to it, and v is a given vector. It should return 00212 * 0 if successful a positive value for a recoverable error or 00213 * a negative value for an unrecoverable failure. 00214 * 00215 * A function jtimes must have the prototype given below. Its 00216 * parameters are as follows: 00217 * 00218 * v is the N_Vector to be multiplied by J. 00219 * 00220 * Jv is the output N_Vector containing J*v. 00221 * 00222 * t is the current value of the independent variable. 00223 * 00224 * y is the current value of the dependent variable 00225 * vector. 00226 * 00227 * fy is the vector f(t,y). 00228 * 00229 * jac_data is a pointer to user Jacobian data, the same as the 00230 * jac_data parameter passed to the CV*SetJacTimesVecFn 00231 * function. 00232 * 00233 * tmp is a pointer to memory allocated for an N_Vector 00234 * which can be used by Jtimes for work space. 00235 * ----------------------------------------------------------------- 00236 */ 00237 00238 typedef int (*CVSpilsJacTimesVecFn)(N_Vector v, N_Vector Jv, realtype t, 00239 N_Vector y, N_Vector fy, 00240 void *jac_data, N_Vector tmp); 00241 00242 00243 00244 /* 00245 * ----------------------------------------------------------------- 00246 * Optional inputs to the CVSPILS linear solver 00247 * ----------------------------------------------------------------- 00248 * 00249 * CVSpilsSetPrecType resets the type of preconditioner, pretype, 00250 * from the value previously set. 00251 * This must be one of PREC_NONE, PREC_LEFT, 00252 * PREC_RIGHT, or PREC_BOTH. 00253 * 00254 * CVSpilsSetGSType specifies the type of Gram-Schmidt 00255 * orthogonalization to be used. This must be one of 00256 * the two enumeration constants MODIFIED_GS or 00257 * CLASSICAL_GS defined in iterative.h. These correspond 00258 * to using modified Gram-Schmidt and classical 00259 * Gram-Schmidt, respectively. 00260 * Default value is MODIFIED_GS. 00261 * 00262 * CVSpilsSetMaxl resets the maximum Krylov subspace size, maxl, 00263 * from the value previously set. 00264 * An input value <= 0, gives the default value. 00265 * 00266 * CVSpilsSetDelt specifies the factor by which the tolerance on 00267 * the nonlinear iteration is multiplied to get a 00268 * tolerance on the linear iteration. 00269 * Default value is 0.05. 00270 * 00271 * CVSpilsSetPreconditioner specifies the PrecSetup and PrecSolve functions. 00272 * as well as a pointer to user preconditioner data. 00273 * This pointer is passed to PrecSetup and PrecSolve 00274 * every time these routines are called. 00275 * Default is NULL for al three arguments. 00276 * 00277 * CVSpilsSetJacTimesVecFn specifies the jtimes function and a pointer to 00278 * user Jacobian data. This pointer is passed to jtimes every 00279 * time the jtimes routine is called. 00280 * Default is to use an internal finite difference 00281 * approximation routine. 00282 * 00283 * The return value of CVSpilsSet* is one of: 00284 * CVSPILS_SUCCESS if successful 00285 * CVSPILS_MEM_NULL if the cvode memory was NULL 00286 * CVSPILS_LMEM_NULL if the linear solver memory was NULL 00287 * CVSPILS_ILL_INPUT if an input has an illegal value 00288 * ----------------------------------------------------------------- 00289 */ 00290 00291 SUNDIALS_EXPORT int CVSpilsSetPrecType(void *cvode_mem, int pretype); 00292 SUNDIALS_EXPORT int CVSpilsSetGSType(void *cvode_mem, int gstype); 00293 SUNDIALS_EXPORT int CVSpilsSetMaxl(void *cvode_mem, int maxl); 00294 SUNDIALS_EXPORT int CVSpilsSetDelt(void *cvode_mem, realtype delt); 00295 SUNDIALS_EXPORT int CVSpilsSetPreconditioner(void *cvode_mem, CVSpilsPrecSetupFn pset, 00296 CVSpilsPrecSolveFn psolve, void *P_data); 00297 SUNDIALS_EXPORT int CVSpilsSetJacTimesVecFn(void *cvode_mem, 00298 CVSpilsJacTimesVecFn jtimes, void *jac_data); 00299 00300 /* 00301 * ----------------------------------------------------------------- 00302 * Optional outputs from the CVSPILS linear solver 00303 * ----------------------------------------------------------------- 00304 * CVSpilsGetWorkSpace returns the real and integer workspace used 00305 * by the SPILS module. 00306 * 00307 * CVSpilsGetNumPrecEvals returns the number of preconditioner 00308 * evaluations, i.e. the number of calls made 00309 * to PrecSetup with jok==FALSE. 00310 * 00311 * CVSpilsGetNumPrecSolves returns the number of calls made to 00312 * PrecSolve. 00313 * 00314 * CVSpilsGetNumLinIters returns the number of linear iterations. 00315 * 00316 * CVSpilsGetNumConvFails returns the number of linear 00317 * convergence failures. 00318 * 00319 * CVSpilsGetNumJtimesEvals returns the number of calls to jtimes. 00320 * 00321 * CVSpilsGetNumRhsEvals returns the number of calls to the user 00322 * f routine due to finite difference Jacobian 00323 * times vector evaluation. 00324 * 00325 * CVSpilsGetLastFlag returns the last error flag set by any of 00326 * the CVSPILS interface functions. 00327 * 00328 * The return value of CVSpilsGet* is one of: 00329 * CVSPILS_SUCCESS if successful 00330 * CVSPILS_MEM_NULL if the cvode memory was NULL 00331 * CVSPILS_LMEM_NULL if the linear solver memory was NULL 00332 * ----------------------------------------------------------------- 00333 */ 00334 00335 SUNDIALS_EXPORT int CVSpilsGetWorkSpace(void *cvode_mem, long int *lenrwLS, long int *leniwLS); 00336 SUNDIALS_EXPORT int CVSpilsGetNumPrecEvals(void *cvode_mem, long int *npevals); 00337 SUNDIALS_EXPORT int CVSpilsGetNumPrecSolves(void *cvode_mem, long int *npsolves); 00338 SUNDIALS_EXPORT int CVSpilsGetNumLinIters(void *cvode_mem, long int *nliters); 00339 SUNDIALS_EXPORT int CVSpilsGetNumConvFails(void *cvode_mem, long int *nlcfails); 00340 SUNDIALS_EXPORT int CVSpilsGetNumJtimesEvals(void *cvode_mem, long int *njvevals); 00341 SUNDIALS_EXPORT int CVSpilsGetNumRhsEvals(void *cvode_mem, long int *nfevalsLS); 00342 SUNDIALS_EXPORT int CVSpilsGetLastFlag(void *cvode_mem, int *flag); 00343 00344 /* 00345 * ----------------------------------------------------------------- 00346 * The following function returns the name of the constant 00347 * associated with a CVSPILS return flag 00348 * ----------------------------------------------------------------- 00349 */ 00350 00351 SUNDIALS_EXPORT char *CVSpilsGetReturnFlagName(int flag); 00352 00353 /* CVSPILS return values */ 00354 00355 #define CVSPILS_SUCCESS 0 00356 #define CVSPILS_MEM_NULL -1 00357 #define CVSPILS_LMEM_NULL -2 00358 #define CVSPILS_ILL_INPUT -3 00359 #define CVSPILS_MEM_FAIL -4 00360 00361 #ifdef __cplusplus 00362 } 00363 #endif 00364 00365 #endif