cpodes_spils.h

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

Generated on Thu Aug 12 16:37:06 2010 for SimTKcore by  doxygen 1.6.1