int CVSpgmrSetGSTypeB(void *cvadj_mem, int gstypeB) { CVadjMem ca_mem; void *cvode_mem; int flag; if (cvadj_mem == NULL) return(CV_ADJMEM_NULL); ca_mem = (CVadjMem) cvadj_mem; cvode_mem = (void *) ca_mem->cvb_mem; flag = CVSpgmrSetGSType(cvode_mem,gstypeB); return(flag); }
void FCV_SPGMRREINIT(int *pretype, int *gstype, realtype *delt, int *ier) { /* pretype the preconditioner type gstype the Gram-Schmidt process type delt the linear convergence tolerance factor */ *ier = CVSpgmrSetPrecType(CV_cvodemem, *pretype); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetGSType(CV_cvodemem, *gstype); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetDelt(CV_cvodemem, *delt); if (*ier != CVSPGMR_SUCCESS) return; CV_ls = 4; }
void FCV_SPGMR(int *pretype, int *gstype, int *maxl, realtype *delt, int *ier) { /* pretype the preconditioner type maxl the maximum Krylov dimension gstype the Gram-Schmidt process type delt the linear convergence tolerance factor */ *ier = CVSpgmr(CV_cvodemem, *pretype, *maxl); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetGSType(CV_cvodemem, *gstype); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetDelt(CV_cvodemem, *delt); if (*ier != CVSPGMR_SUCCESS) return; CV_ls = 4; }
void FCV_BBDSPGMR(int *pretype, int *gstype, int *maxl, realtype *delt, int *ier) { /* Call CVBBDSpgmr to specify the SPGMR linear solver: pretype is the preconditioner type gstype is the Gram-Schmidt process type maxl is the maximum Krylov dimension delt is the linear convergence tolerance factor */ *ier = CVBBDSpgmr(CV_cvodemem, *pretype, *maxl, CVBBD_Data); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetGSType(CV_cvodemem, *gstype); if (*ier != CVSPGMR_SUCCESS) return; *ier = CVSpgmrSetDelt(CV_cvodemem, *delt); if (*ier != CVSPGMR_SUCCESS) return; CV_ls = 4; }
int main() { realtype abstol, reltol, t, tout; N_Vector u; UserData data; void *cvode_mem; int iout, flag; u = NULL; data = NULL; cvode_mem = NULL; /* Allocate memory, and set problem data, initial values, tolerances */ u = N_VNew_Serial(NEQ); if(check_flag((void *)u, "N_VNew_Serial", 0)) return(1); data = AllocUserData(); if(check_flag((void *)data, "AllocUserData", 2)) return(1); InitUserData(data); SetInitialProfiles(u, data->dx, data->dy); abstol=ATOL; reltol=RTOL; /* Call CvodeCreate to create the solver memory CV_BDF specifies the Backward Differentiation Formula CV_NEWTON specifies a Newton iteration A pointer to the integrator memory is returned and stored in cvode_mem. */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(check_flag((void *)cvode_mem, "CVodeCreate", 0)) return(1); /* Set the pointer to user-defined data */ flag = CVodeSetFdata(cvode_mem, data); if(check_flag(&flag, "CVodeSetFdata", 1)) return(1); /* Call CVodeMalloc to initialize the integrator memory: f is the user's right hand side function in u'=f(t,u) T0 is the initial time u is the initial dependent variable vector CV_SS specifies scalar relative and absolute tolerances reltol is the relative tolerance &abstol is a pointer to the scalar absolute tolerance */ flag = CVodeMalloc(cvode_mem, f, T0, u, CV_SS, reltol, &abstol); if(check_flag(&flag, "CVodeMalloc", 1)) return(1); /* Call CVSpgmr to specify the linear solver CVSPGMR with left preconditioning and the maximum Krylov dimension maxl */ flag = CVSpgmr(cvode_mem, PREC_LEFT, 0); if(check_flag(&flag, "CVSpgmr", 1)) return(1); /* Set modified Gram-Schmidt orthogonalization, preconditioner setup and solve routines Precond and PSolve, and the pointer to the user-defined block data */ flag = CVSpgmrSetGSType(cvode_mem, MODIFIED_GS); if(check_flag(&flag, "CVSpgmrSetGSType", 1)) return(1); flag = CVSpgmrSetPreconditioner(cvode_mem, Precond, PSolve, data); if(check_flag(&flag, "CVSpgmrSetPreconditioner", 1)) return(1); /* In loop over output points, call CVode, print results, test for error */ printf(" \n2-species diurnal advection-diffusion problem\n\n"); for (iout=1, tout = TWOHR; iout <= NOUT; iout++, tout += TWOHR) { flag = CVode(cvode_mem, tout, u, &t, CV_NORMAL); PrintOutput(cvode_mem, u, t); if(check_flag(&flag, "CVode", 1)) break; } PrintFinalStats(cvode_mem); /* Free memory */ N_VDestroy_Serial(u); FreeUserData(data); CVodeFree(cvode_mem); return(0); }
/* Main Function */ int main(int argc, char *argv[]) { char tmpLName[11],tmpFName[11]; /* rivFlux File names */ Model_Data mData; /* Model Data */ Control_Data cData; /* Solver Control Data */ N_Vector CV_Y; /* State Variables Vector */ void *cvode_mem; /* Model Data Pointer */ int flag; /* flag to test return value */ FILE *Ofile[22]; /* Output file */ char *ofn[22]; FILE *iproj; /* Project File */ int N; /* Problem size */ int i,j,k; /* loop index */ realtype t; /* simulation time */ realtype NextPtr, StepSize; /* stress period & step size */ clock_t start, end_r, end_s; /* system clock at points */ realtype cputime_r, cputime_s; /* for duration in realtype */ char *filename; /* Project Input Name */ if(argc!=2) { iproj=fopen("projectName.txt","r"); if(iproj==NULL) { printf("\t\nUsage ./pihm project_name"); printf("\t\n OR "); printf("\t\nUsage ./pihm, and have a file in the current directory named projectName.txt with the project name in it"); exit(0); } else { filename = (char *)malloc(15*sizeof(char)); fscanf(iproj,"%s",filename); } } else { /* get user specified file name in command line */ filename = (char *)malloc(strlen(argv[1])*sizeof(char)); strcpy(filename,argv[1]); } /* Open Output Files */ ofn[0] = (char *)malloc((strlen(filename)+3)*sizeof(char)); strcpy(ofn[0], filename); Ofile[0]=fopen(strcat(ofn[0], ".GW"),"w"); ofn[1] = (char *)malloc((strlen(filename)+5)*sizeof(char)); strcpy(ofn[1], filename); Ofile[1]=fopen(strcat(ofn[1], ".surf"),"w"); ofn[2] = (char *)malloc((strlen(filename)+4)*sizeof(char)); strcpy(ofn[2], filename); Ofile[2]=fopen(strcat(ofn[2], ".et0"),"w"); ofn[3] = (char *)malloc((strlen(filename)+4)*sizeof(char)); strcpy(ofn[3], filename); Ofile[3]=fopen(strcat(ofn[3], ".et1"),"w"); ofn[4] = (char *)malloc((strlen(filename)+4)*sizeof(char)); strcpy(ofn[4], filename); Ofile[4]=fopen(strcat(ofn[4], ".et2"),"w"); ofn[5] = (char *)malloc((strlen(filename)+3)*sizeof(char)); strcpy(ofn[5], filename); Ofile[5]=fopen(strcat(ofn[5], ".is"),"w"); ofn[6] = (char *)malloc((strlen(filename)+5)*sizeof(char)); strcpy(ofn[6], filename); Ofile[6]=fopen(strcat(ofn[6], ".snow"),"w"); for(i=0;i<11;i++) { sprintf(tmpLName,".rivFlx%d",i); strcpy(tmpFName,filename); strcat(tmpFName,tmpLName); Ofile[7+i]=fopen(tmpFName,"w"); } ofn[18] = (char *)malloc((strlen(filename)+6)*sizeof(char)); strcpy(ofn[18], filename); Ofile[18]=fopen(strcat(ofn[18], ".stage"),"w"); ofn[19] = (char *)malloc((strlen(filename)+6)*sizeof(char)); strcpy(ofn[19], filename); Ofile[19]=fopen(strcat(ofn[19], ".unsat"),"w"); ofn[20] = (char *)malloc((strlen(filename)+5)*sizeof(char)); strcpy(ofn[20], filename); Ofile[20]=fopen(strcat(ofn[20], ".Rech"),"w"); /* allocate memory for model data structure */ mData = (Model_Data)malloc(sizeof *mData); printf("\n ... PIHM 2.0 is starting ... \n"); /* read in 9 input files with "filename" as prefix */ read_alloc(filename, mData, &cData); /* if(mData->UnsatMode ==1) { } */ if(mData->UnsatMode ==2) { /* problem size */ N = 3*mData->NumEle + 2*mData->NumRiv; mData->DummyY=(realtype *)malloc((3*mData->NumEle+2*mData->NumRiv)*sizeof(realtype)); } /* initial state variable depending on machine*/ CV_Y = N_VNew_Serial(N); /* initialize mode data structure */ initialize(filename, mData, &cData, CV_Y); printf("\nSolving ODE system ... \n"); /* allocate memory for solver */ cvode_mem = CVodeCreate(CV_BDF, CV_NEWTON); if(cvode_mem == NULL) {printf("CVodeMalloc failed. \n"); return(1);} flag = CVodeSetFdata(cvode_mem, mData); flag = CVodeSetInitStep(cvode_mem,cData.InitStep); flag = CVodeSetStabLimDet(cvode_mem,TRUE); flag = CVodeSetMaxStep(cvode_mem,cData.MaxStep); flag = CVodeMalloc(cvode_mem, f, cData.StartTime, CV_Y, CV_SS, cData.reltol, &cData.abstol); flag = CVSpgmr(cvode_mem, PREC_NONE, 0); flag = CVSpgmrSetGSType(cvode_mem, MODIFIED_GS); /* set start time */ t = cData.StartTime; start = clock(); /* start solver in loops */ for(i=0; i<cData.NumSteps; i++) { /* if (cData.Verbose != 1) { printf(" Running: %-4.1f%% ... ", (100*(i+1)/((realtype) cData.NumSteps))); fflush(stdout); } */ /* inner loops to next output points with ET step size control */ while(t < cData.Tout[i+1]) { if (t + cData.ETStep >= cData.Tout[i+1]) { NextPtr = cData.Tout[i+1]; } else { NextPtr = t + cData.ETStep; } StepSize = NextPtr - t; /* calculate Interception Storage */ is_sm_et(t, StepSize, mData,CV_Y); printf("\n Tsteps = %f ",t); flag = CVode(cvode_mem, NextPtr, CV_Y, &t, CV_NORMAL); update(t,mData); } PrintData(Ofile,&cData,mData, CV_Y,t); } /* Free memory */ N_VDestroy_Serial(CV_Y); /* Free integrator memory */ CVodeFree(cvode_mem); free(mData); return 0; }