00001 /* 00002 * ----------------------------------------------------------------- 00003 * $Revision: 1.3 $ 00004 * $Date: 2006/11/29 00:05:05 $ 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 header file for the CPBBDPRE module, for a 00014 * band-block-diagonal preconditioner, i.e. a block-diagonal 00015 * matrix with banded blocks, for use with one of the CPSPILS 00016 * linear solvers and the parallel implementation of the NVECTOR 00017 * module. 00018 * 00019 * Summary: 00020 * 00021 * These routines provide a preconditioner matrix that is 00022 * block-diagonal with banded blocks. The blocking corresponds to the 00023 * distribution of the dependent variable vector y among the processors. 00024 * Each preconditioner block is generated from the Jacobian of the local 00025 * part (on the current processor) of a given function g(t,y) approximating 00026 * f(t,y) (for explicit-form ODEs) or G(t,y,y') approximating F(t,y,y') 00027 * (for implicit-form ODEs). The blocks are generated by a difference 00028 * quotient scheme on each processor independently. This scheme utilizes 00029 * an assumed banded structure with given half-bandwidths, mudq and mldq. 00030 * However, the banded Jacobian block kept by the scheme has half-bandwiths 00031 * mukeep and mlkeep, which may be smaller. 00032 * 00033 * The user's calling program should have the following form: 00034 * 00035 * #include <nvector_parallel.h> 00036 * #include <cpodes/cpodes_bbdpre.h> 00037 * ... 00038 * void *cpode_mem; 00039 * void *bbd_data; 00040 * ... 00041 * Set y0 00042 * ... 00043 * cpode_mem = CPodeCreate(...); 00044 * ier = CPodeMalloc(...); 00045 * ... 00046 * bbd_data = CPBBDPrecAlloc(cpode_mem, Nlocal, mudq ,mldq, 00047 * mukeep, mlkeep, dqrely, gloc, cfn); 00048 * flag = CPBBDSpgmr(cpode_mem, pretype, maxl, bbd_data); 00049 * -or- 00050 * flag = CPBBDSpbcg(cpode_mem, pretype, maxl, bbd_data); 00051 * -or- 00052 * flag = CPBBDSptfqmr(cpode_mem, pretype, maxl, bbd_data); 00053 * ... 00054 * ier = CPode(...); 00055 * ... 00056 * CPBBDPrecFree(&bbd_data); 00057 * ... 00058 * CPodeFree(...); 00059 * 00060 * Free y0 00061 * 00062 * The user-supplied routines required are: 00063 * 00064 * f or F - function defining the ODE right-hand side f(t,y) 00065 * or the ODE residual F(t,y,y') 00066 * 00067 * gloc or Gloc - function defining the approximation g(t,y) 00068 * or G(t,y,y') 00069 * 00070 * cfn - function to perform communication need for gloc. 00071 * 00072 * Notes: 00073 * 00074 * 1) This header file is included by the user for the definition 00075 * of the CPBBDData type and for needed function prototypes. 00076 * 00077 * 2) The CPBBDPrecAlloc call includes half-bandwiths mudq and mldq 00078 * to be used in the difference quotient calculation of the 00079 * approximate Jacobian. They need not be the true 00080 * half-bandwidths of the Jacobian of the local block of g, 00081 * when smaller values may provide a greater efficiency. 00082 * Also, the half-bandwidths mukeep and mlkeep of the retained 00083 * banded approximate Jacobian block may be even smaller, 00084 * to reduce storage and computation costs further. 00085 * For all four half-bandwidths, the values need not be the 00086 * same on every processor. 00087 * 00088 * 3) The actual name of the user's f (or F) function is passed to 00089 * CPodeMalloc, and the names of the user's gloc (or Gloc) and 00090 * cfn functions are passed to CPBBDPrecAlloc. 00091 * 00092 * 4) The pointer to the user-defined data block f_data, which is 00093 * set through CPodeSetFdata is also available to the user in 00094 * gloc/Gloc and cfn. 00095 * 00096 * 5) For the CPSpgmr solver, the Gram-Schmidt type gstype, 00097 * is left to the user to specify through CPSpgmrSetGStype. 00098 * 00099 * 6) Optional outputs specific to this module are available by 00100 * way of routines listed below. These include work space sizes 00101 * and the cumulative number of gloc calls. The costs 00102 * associated with this module also include nsetups banded LU 00103 * factorizations, nlinsetups cfn calls, and npsolves banded 00104 * backsolve calls, where nlinsetups and npsolves are 00105 * integrator/CPSPGMR/CPSPBCG/CPSPTFQMR optional outputs. 00106 * ----------------------------------------------------------------- 00107 */ 00108 00109 #ifndef _CPBBDPRE_H 00110 #define _CPBBDPRE_H 00111 00112 #ifdef __cplusplus /* wrapper to enable C++ usage */ 00113 extern "C" { 00114 #endif 00115 00116 #include <sundials/sundials_nvector.h> 00117 00118 /* CPBBDPRE return values */ 00119 00120 #define CPBBDPRE_SUCCESS 0 00121 #define CPBBDPRE_PDATA_NULL -11 00122 #define CPBBDPRE_FUNC_UNRECVR -12 00123 00124 /* 00125 * ----------------------------------------------------------------- 00126 * Types: CPBBDLocalRhsFn and CPBBDLocalResFn 00127 * ----------------------------------------------------------------- 00128 * 00129 * For ODEs in explicit form, i.e., y' = f(t,y), the user must 00130 * supply a function g(t,y) which approximates the right-hand side 00131 * function f for the system y'=f(t,y), and which is computed locally 00132 * (without interprocess communication). The case where g is 00133 * mathematically identical to f is allowed. The implementation of 00134 * this function must have type CPBBDLocalRhsFn. 00135 * 00136 * This function takes as input the local vector size Nlocal, the 00137 * independent variable value t, the local real dependent variable 00138 * vector y, and a pointer to the user-defined data block f_data. 00139 * It is to compute the local part of g(t,y) and store this in the 00140 * vector gout. Allocation of memory for y and g is handled within 00141 * the preconditioner module. The f_data parameter is the same as 00142 * that specified by the user through the CPodeSetFdata routine. 00143 * 00144 * A CPBBDLocalRhsFn should return 0 if successful, a positive value 00145 * if a recoverable error occurred, and a negative value if an 00146 * unrecoverable error occurred. 00147 * 00148 * ----------------------------------------------------------------- 00149 * 00150 * For ODEs in implicit form, the user must supply a function 00151 * G(t,y,y') which approximates the function F for the system 00152 * F(t,y,y') = 0, and which is computed locally (without interprocess 00153 * communication. The case where G is mathematically identical to F 00154 * is allowed. The implementation of this function must have type 00155 * CPBBDLocalResFn. 00156 * 00157 * This function takes as input the independent variable value t, 00158 * the current solution vector y, the current solution derivative 00159 * vector yp, and a pointer to the user-defined data block f_data. 00160 * It is to compute the local part of G(t,y,y') and store it in the 00161 * vector gout. Providing memory for y, yp, and gout is handled within 00162 * this preconditioner module. It is expected that this routine will 00163 * save communicated data in work space defined by the user and made 00164 * available to the preconditioner function for the problem. The f_data 00165 * parameter is the same as that passed by the user to the CPodeSetFdata 00166 * routine. 00167 * 00168 * A CPBBDLocalResFn Gres is to return an int, defined in the same 00169 * way as for the residual function: 0 (success), +1 or -1 (fail). 00170 * ----------------------------------------------------------------- 00171 */ 00172 00173 typedef int (*CPBBDLocalRhsFn)(int Nlocal, realtype t, 00174 N_Vector y, 00175 N_Vector gout, void *f_data); 00176 00177 typedef int (*CPBBDLocalResFn)(int Nlocal, realtype t, 00178 N_Vector y, N_Vector yp, 00179 N_Vector gout, void *f_data); 00180 00181 /* 00182 * ----------------------------------------------------------------- 00183 * Type: CPBBDCommFn 00184 * ----------------------------------------------------------------- 00185 * The user may supply a function of type CPBBDCommFn which performs 00186 * all interprocess communication necessary to evaluate the 00187 * approximate right-hand side function described above. 00188 * 00189 * This function takes as input the local vector size Nlocal, 00190 * the independent variable value t, the dependent variable 00191 * vector y, and a pointer to the user-defined data block f_data. 00192 * The f_data parameter is the same as that specified by the user 00193 * through the CPodeSetFdata routine. A CPBBDCommFn cfn is 00194 * expected to save communicated data in space defined within the 00195 * structure f_data. A CPBBDCommFn cfn does not have a return value. 00196 * 00197 * Each call to the CPBBDCommFn cfn is preceded by a call to the 00198 * CPRhsFn f (or CPResFn F) with the same (t, y, y') arguments 00199 * (where y'=NULL for explicit-form ODEs). Thus cfn can omit any 00200 * communications done by f (or F) if relevant to the evaluation of 00201 * the local approximation. If all necessary communication was done 00202 * by f (respectively F), the user can pass NULL for cfn in 00203 * CPBBDPrecAlloc (see below). 00204 * 00205 * A CPBBDCommFn should return 0 if successful, a positive value if 00206 * a recoverable error occurred, and a negative value if an 00207 * unrecoverable error occurred. 00208 * ----------------------------------------------------------------- 00209 */ 00210 00211 typedef int (*CPBBDCommFn)(int Nlocal, realtype t, 00212 N_Vector y, N_Vector yp, void *f_data); 00213 00214 /* 00215 * ----------------------------------------------------------------- 00216 * Function : CPBBDPrecAlloc 00217 * ----------------------------------------------------------------- 00218 * CPBBDPrecAlloc allocates and initializes a CPBBDData structure 00219 * to be passed to CPSp* (and used by CPBBDPrecSetup and 00220 * CPBBDPrecSolve). 00221 * 00222 * The parameters of CPBBDPrecAlloc are as follows: 00223 * 00224 * cpode_mem - pointer to the integrator memory. 00225 * 00226 * Nlocal - length of the local vectors on the current processor. 00227 * 00228 * mudq, mldq - upper and lower half-bandwidths to be used in the 00229 * difference quotient computation of the local Jacobian block. 00230 * 00231 * mukeep, mlkeep - upper and lower half-bandwidths of the retained 00232 * banded approximation to the local Jacobian block. 00233 * 00234 * dqrely - optional input. It is the relative increment in components 00235 * of y used in the difference quotient approximations. To specify 00236 * the default value (square root of the unitroundoff), pass 0. 00237 * 00238 * gloc - name of the user-supplied function g(t,y) or G(t,y,y') that 00239 * approximates f (respectively F) and whose local Jacobian blocks 00240 * are to form the preconditioner. 00241 * 00242 * cfn - name of the user-defined function that performs necessary 00243 * interprocess communication for the execution of gloc. 00244 * 00245 * CPBBDPrecAlloc returns the storage allocated (type *void), or NULL 00246 * if the request for storage cannot be satisfied. 00247 * ----------------------------------------------------------------- 00248 */ 00249 00250 SUNDIALS_EXPORT void *CPBBDPrecAlloc(void *cpode_mem, int Nlocal, 00251 int mudq, int mldq, int mukeep, int mlkeep, 00252 realtype dqrely, 00253 void *gloc, CPBBDCommFn cfn); 00254 00255 /* 00256 * ----------------------------------------------------------------- 00257 * Function : CPBBDSptfqmr 00258 * ----------------------------------------------------------------- 00259 * CPBBDSptfqmr links the CPBBDPRE preconditioner to the CPSPTFQMR 00260 * linear solver. It performs the following actions: 00261 * 1) Calls the CPSPTFQMR specification routine and attaches the 00262 * CPSPTFQMR linear solver to the integrator memory; 00263 * 2) Sets the preconditioner data structure for CPSPTFQMR 00264 * 3) Sets the preconditioner setup routine for CPSPTFQMR 00265 * 4) Sets the preconditioner solve routine for CPSPTFQMR 00266 * 00267 * Its first 3 arguments are the same as for CPSptfqmr (see 00268 * cpsptfqmr.h). The last argument is the pointer to the CPBBDPRE 00269 * memory block returned by CPBBDPrecAlloc. Note that the user need 00270 * not call CPSptfqmr. 00271 * 00272 * Possible return values are: 00273 * CPSPILS_SUCCESS if successful 00274 * CPSPILS_MEM_NULL if the CPODES memory was NULL 00275 * CPSPILS_LMEM_NULL if the CPSPILS memory was NULL 00276 * CPSPILS_MEM_FAIL if there was a memory allocation failure 00277 * CPSPILS_ILL_INPUT if a required vector operation is missing 00278 * CPBBDPRE_PDATA_NULL if the bbd_data was NULL 00279 * ----------------------------------------------------------------- 00280 */ 00281 00282 SUNDIALS_EXPORT int CPBBDSptfqmr(void *cpode_mem, int pretype, int maxl, void *bbd_data); 00283 00284 /* 00285 * ----------------------------------------------------------------- 00286 * Function : CPBBDSpbcg 00287 * ----------------------------------------------------------------- 00288 * CPBBDSpbcg links the CPBBDPRE preconditioner to the CPSPBCG 00289 * linear solver. It performs the following actions: 00290 * 1) Calls the CPSPBCG specification routine and attaches the 00291 * CPSPBCG linear solver to the integrator memory; 00292 * 2) Sets the preconditioner data structure for CPSPBCG 00293 * 3) Sets the preconditioner setup routine for CPSPBCG 00294 * 4) Sets the preconditioner solve routine for CPSPBCG 00295 * 00296 * Its first 3 arguments are the same as for CPSpbcg (see 00297 * cpspbcg.h). The last argument is the pointer to the CPBBDPRE 00298 * memory block returned by CPBBDPrecAlloc. Note that the user need 00299 * not call CPSpbcg. 00300 * 00301 * Possible return values are: 00302 * CPSPILS_SUCCESS if successful 00303 * CPSPILS_MEM_NULL if the CPODES memory was NULL 00304 * CPSPILS_LMEM_NULL if the CPSPILS memory was NULL 00305 * CPSPILS_MEM_FAIL if there was a memory allocation failure 00306 * CPSPILS_ILL_INPUT if a required vector operation is missing 00307 * CPBBDPRE_PDATA_NULL if the bbd_data was NULL 00308 * ----------------------------------------------------------------- 00309 */ 00310 00311 SUNDIALS_EXPORT int CPBBDSpbcg(void *cpode_mem, int pretype, int maxl, void *bbd_data); 00312 00313 /* 00314 * ----------------------------------------------------------------- 00315 * Function : CPBBDSpgmr 00316 * ----------------------------------------------------------------- 00317 * CPBBDSpgmr links the CPBBDPRE preconditioner to the CPSPGMR 00318 * linear solver. It performs the following actions: 00319 * 1) Calls the CPSPGMR specification routine and attaches the 00320 * CPSPGMR linear solver to the integrator memory; 00321 * 2) Sets the preconditioner data structure for CPSPGMR 00322 * 3) Sets the preconditioner setup routine for CPSPGMR 00323 * 4) Sets the preconditioner solve routine for CPSPGMR 00324 * 00325 * Its first 3 arguments are the same as for CPSpgmr (see 00326 * cpspgmr.h). The last argument is the pointer to the CPBBDPRE 00327 * memory block returned by CPBBDPrecAlloc. Note that the user need 00328 * not call CPSpgmr. 00329 * 00330 * Possible return values are: 00331 * CPSPILS_SUCCESS if successful 00332 * CPSPILS_MEM_NULL if the CPODES memory was NULL 00333 * CPSPILS_LMEM_NULL if the CPSPILS memory was NULL 00334 * CPSPILS_MEM_FAIL if there was a memory allocation failure 00335 * CPSPILS_ILL_INPUT if a required vector operation is missing 00336 * CPBBDPRE_PDATA_NULL if the bbd_data was NULL 00337 * ----------------------------------------------------------------- 00338 */ 00339 00340 SUNDIALS_EXPORT int CPBBDSpgmr(void *cpode_mem, int pretype, int maxl, void *bbd_data); 00341 00342 /* 00343 * ----------------------------------------------------------------- 00344 * Function : CPBBDPrecReInit 00345 * ----------------------------------------------------------------- 00346 * CPBBDPrecReInit re-initializes the BBDPRE module when solving a 00347 * sequence of problems of the same size with CPSPGMR/CPBBDPRE or 00348 * CPSPBCG/CPBBDPRE or CPSPTFQMR/CPBBDPRE provided there is no change 00349 * in Nlocal, mukeep, or mlkeep. After solving one problem, and after 00350 * calling CPodeReInit to re-initialize the integrator for a subsequent 00351 * problem, call CPBBDPrecReInit. Then call CPSpgmrSet* or CPSpbcgSet* 00352 * or CPSptfqmrSet* functions if necessary for any changes to CPSPGMR, 00353 * CPSPBCG, or CPSPTFQMR parameters, before calling CPode. 00354 * 00355 * The first argument to CPBBDPrecReInit must be the pointer pdata 00356 * that was returned by CPBBDPrecAlloc. All other arguments have 00357 * the same names and meanings as those of CPBBDPrecAlloc. 00358 * 00359 * The return value of CPBBDPrecReInit is CPBBDPRE_SUCCESS, indicating 00360 * success, or CPBBDPRE_PDATA_NULL if bbd_data was NULL. 00361 * ----------------------------------------------------------------- 00362 */ 00363 00364 SUNDIALS_EXPORT int CPBBDPrecReInit(void *bbd_data, int mudq, int mldq, 00365 realtype dqrely, void *gloc, CPBBDCommFn cfn); 00366 00367 /* 00368 * ----------------------------------------------------------------- 00369 * Function : CPBBDPrecFree 00370 * ----------------------------------------------------------------- 00371 * CPBBDPrecFree frees the memory block bbd_data allocated by the 00372 * call to CPBBDAlloc. 00373 * ----------------------------------------------------------------- 00374 */ 00375 00376 SUNDIALS_EXPORT void CPBBDPrecFree(void **bbd_data); 00377 00378 /* 00379 * ----------------------------------------------------------------- 00380 * BBDPRE optional output extraction routines 00381 * ----------------------------------------------------------------- 00382 * CPBBDPrecGetWorkSpace returns the BBDPRE real and integer work space 00383 * sizes. 00384 * CPBBDPrecGetNumGfnEvals returns the number of calls to gfn. 00385 * 00386 * The return value of CPBBDPrecGet* is one of: 00387 * CPBBDPRE_SUCCESS if successful 00388 * CPBBDPRE_PDATA_NULL if the bbd_data memory was NULL 00389 * ----------------------------------------------------------------- 00390 */ 00391 00392 SUNDIALS_EXPORT int CPBBDPrecGetWorkSpace(void *bbd_data, long int *lenrwLS, long int *leniwLS); 00393 SUNDIALS_EXPORT int CPBBDPrecGetNumGfnEvals(void *bbd_data, long int *ngevalsBBDP); 00394 00395 /* 00396 * ----------------------------------------------------------------- 00397 * The following function returns the name of the constant 00398 * associated with a CPBBDPRE return flag 00399 * ----------------------------------------------------------------- 00400 */ 00401 00402 SUNDIALS_EXPORT char *CPBBDPrecGetReturnFlagName(int flag); 00403 00404 #ifdef __cplusplus 00405 } 00406 #endif 00407 00408 #endif