cvodes_spils.h

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

Generated on Fri Sep 26 07:44:09 2008 for SimTKcore by  doxygen 1.5.6