int IDASpgmr(void *ida_mem, int maxl) { IDAMem IDA_mem; IDASpilsMem idaspils_mem; SpgmrMem spgmr_mem; int flag, maxl1; /* Return immediately if ida_mem is NULL */ if (ida_mem == NULL) { IDAProcessError(NULL, IDASPILS_MEM_NULL, "IDASPGMR", "IDASpgmr", MSGS_IDAMEM_NULL); return(IDASPILS_MEM_NULL); } IDA_mem = (IDAMem) ida_mem; /* Check if N_VDotProd is present */ if(vec_tmpl->ops->nvdotprod == NULL) { IDAProcessError(NULL, IDASPILS_ILL_INPUT, "IDASPGMR", "IDASpgmr", MSGS_BAD_NVECTOR); return(IDASPILS_ILL_INPUT); } if (lfree != NULL) flag = lfree((IDAMem) ida_mem); /* Set five main function fields in ida_mem */ linit = IDASpgmrInit; lsetup = IDASpgmrSetup; lsolve = IDASpgmrSolve; lperf = IDASpgmrPerf; lfree = IDASpgmrFree; /* Get memory for IDASpilsMemRec */ idaspils_mem = NULL; idaspils_mem = (IDASpilsMem) malloc(sizeof(IDASpilsMemRec)); if (idaspils_mem == NULL) { IDAProcessError(NULL, IDASPILS_MEM_FAIL, "IDASPGMR", "IDASpgmr", MSGS_MEM_FAIL); return(IDASPILS_MEM_FAIL); } /* Set ILS type */ idaspils_mem->s_type = SPILS_SPGMR; /* Set SPGMR parameters that were passed in call sequence */ maxl1 = (maxl <= 0) ? IDA_SPILS_MAXL : maxl; idaspils_mem->s_maxl = maxl1; /* Set default values for the rest of the Spgmr parameters */ idaspils_mem->s_gstype = MODIFIED_GS; idaspils_mem->s_maxrs = IDA_SPILS_MAXRS; idaspils_mem->s_eplifac = PT05; idaspils_mem->s_dqincfac = ONE; idaspils_mem->s_pset = NULL; idaspils_mem->s_psolve = NULL; idaspils_mem->s_pdata = NULL; idaspils_mem->s_jtimes = IDASpilsDQJtimes; idaspils_mem->s_jdata = ida_mem; idaspils_mem->s_last_flag = IDASPILS_SUCCESS; /* Set setupNonNull to FALSE */ setupNonNull = FALSE; /* Allocate memory for ytemp, yptemp, and xx */ ytemp = NULL; ytemp = N_VClone(vec_tmpl); if (ytemp == NULL) { IDAProcessError(NULL, IDASPILS_MEM_FAIL, "IDASPGMR", "IDASpgmr", MSGS_MEM_FAIL); free(idaspils_mem); idaspils_mem = NULL; return(IDASPILS_MEM_FAIL); } yptemp = NULL; yptemp = N_VClone(vec_tmpl); if (yptemp == NULL) { IDAProcessError(NULL, IDASPILS_MEM_FAIL, "IDASPGMR", "IDASpgmr", MSGS_MEM_FAIL); N_VDestroy(ytemp); free(idaspils_mem); idaspils_mem = NULL; return(IDASPILS_MEM_FAIL); } xx = NULL; xx = N_VClone(vec_tmpl); if (xx == NULL) { IDAProcessError(NULL, IDASPILS_MEM_FAIL, "IDASPGMR", "IDASpgmr", MSGS_MEM_FAIL); N_VDestroy(ytemp); N_VDestroy(yptemp); free(idaspils_mem); idaspils_mem = NULL; return(IDASPILS_MEM_FAIL); } /* Compute sqrtN from a dot product */ N_VConst(ONE, ytemp); sqrtN = RSqrt( N_VDotProd(ytemp, ytemp) ); /* Call SpgmrMalloc to allocate workspace for Spgmr */ spgmr_mem = NULL; spgmr_mem = SpgmrMalloc(maxl1, vec_tmpl); if (spgmr_mem == NULL) { IDAProcessError(NULL, IDASPILS_MEM_FAIL, "IDASPGMR", "IDASpgmr", MSGS_MEM_FAIL); N_VDestroy(ytemp); N_VDestroy(yptemp); N_VDestroy(xx); free(idaspils_mem); idaspils_mem = NULL; return(IDASPILS_MEM_FAIL); } /* Attach SPGMR memory to spils memory structure */ spils_mem = (void *)spgmr_mem; /* Attach linear solver memory to the integrator memory */ lmem = idaspils_mem; return(IDASPILS_SUCCESS); }
int CVSpgmr(void *cvode_mem, int pretype, int maxl) { CVodeMem cv_mem; CVSpgmrMem cvspgmr_mem; int mxl; /* Return immediately if cvode_mem is NULL */ if (cvode_mem == NULL) { fprintf(stderr, MSGS_CVMEM_NULL); return(CVSPGMR_MEM_NULL); } cv_mem = (CVodeMem) cvode_mem; /* Check if N_VDotProd is present */ if(vec_tmpl->ops->nvdotprod == NULL) { if(errfp!=NULL) fprintf(errfp, MSGS_BAD_NVECTOR); return(CVSPGMR_ILL_INPUT); } if (lfree != NULL) lfree(cv_mem); /* Set four main function fields in cv_mem */ linit = CVSpgmrInit; lsetup = CVSpgmrSetup; lsolve = CVSpgmrSolve; lfree = CVSpgmrFree; /* Get memory for CVSpgmrMemRec */ cvspgmr_mem = (CVSpgmrMem) malloc(sizeof(CVSpgmrMemRec)); if (cvspgmr_mem == NULL) { if(errfp!=NULL) fprintf(errfp, MSGS_MEM_FAIL); return(CVSPGMR_MEM_FAIL); } /* Set Spgmr parameters that have been passed in call sequence */ cvspgmr_mem->g_pretype = pretype; mxl = cvspgmr_mem->g_maxl = (maxl <= 0) ? CVSPGMR_MAXL : maxl; /* Set default values for the rest of the Spgmr parameters */ cvspgmr_mem->g_gstype = MODIFIED_GS; cvspgmr_mem->g_delt = CVSPGMR_DELT; cvspgmr_mem->g_P_data = NULL; cvspgmr_mem->g_pset = NULL; cvspgmr_mem->g_psolve = NULL; cvspgmr_mem->g_jtimes = CVSpgmrDQJtimes; cvspgmr_mem->g_j_data = cvode_mem; cvspgmr_mem->g_last_flag = CVSPGMR_SUCCESS; setupNonNull = FALSE; /* Check for legal pretype */ if ((pretype != PREC_NONE) && (pretype != PREC_LEFT) && (pretype != PREC_RIGHT) && (pretype != PREC_BOTH)) { if(errfp!=NULL) fprintf(errfp, MSGS_BAD_PRETYPE); return(CVSPGMR_ILL_INPUT); } /* Allocate memory for ytemp and x */ ytemp = N_VClone(vec_tmpl); if (ytemp == NULL) { if(errfp!=NULL) fprintf(errfp, MSGS_MEM_FAIL); return(CVSPGMR_MEM_FAIL); } x = N_VClone(vec_tmpl); if (x == NULL) { if(errfp!=NULL) fprintf(errfp, MSGS_MEM_FAIL); N_VDestroy(ytemp); return(CVSPGMR_MEM_FAIL); } /* Compute sqrtN from a dot product */ N_VConst(ONE, ytemp); sqrtN = RSqrt( N_VDotProd(ytemp, ytemp) ); /* Call SpgmrMalloc to allocate workspace for Spgmr */ spgmr_mem = SpgmrMalloc(mxl, vec_tmpl); if (spgmr_mem == NULL) { if(errfp!=NULL) fprintf(errfp, MSGS_MEM_FAIL); N_VDestroy(ytemp); N_VDestroy(x); return(CVSPGMR_MEM_FAIL); } /* Attach linear solver memory to integrator memory */ lmem = cvspgmr_mem; return(CVSPGMR_SUCCESS); }