int main(void) { realtype dx, dy, reltol, abstol, t, tout, umax; N_Vector u, up; UserData data; void *cvode_mem; int iout, flag; long int nst; u = NULL; data = NULL; cvode_mem = NULL; u = N_VNew_Serial(NEQ); up = N_VNew_Serial(NEQ); reltol = ZERO; abstol = ATOL; data = (UserData) malloc(sizeof *data); dx = data->dx = XMAX/(MX+1); dy = data->dy = YMAX/(MY+1); data->hdcoef = ONE/(dx*dx); data->hacoef = HALF/(TWO*dx); data->vdcoef = ONE/(dy*dy); SetIC(u, data); cvode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cvode_mem, (void *)f, data, T0, u, NULL, CP_SS, reltol, &abstol); flag = CPLapackBand(cvode_mem, NEQ, MY, MY); flag = CPDlsSetJacFn(cvode_mem, (void *)Jac, data); /* In loop over output points: call CPode, print results, test for errors */ umax = N_VMaxNorm(u); PrintHeader(reltol, abstol, umax); for(iout=1, tout=T1; iout <= NOUT; iout++, tout += DTOUT) { flag = CPode(cvode_mem, tout, &t, u, up, CP_NORMAL); umax = N_VMaxNorm(u); flag = CPodeGetNumSteps(cvode_mem, &nst); PrintOutput(t, umax, nst); } PrintFinalStats(cvode_mem); N_VDestroy_Serial(u); CPodeFree(&cvode_mem); free(data); return(0); }
int main() { void *fct, *jac; void *cpode_mem; N_Vector yy, yp, abstol; realtype reltol, t, tout; int flag, flagr, iout; int rootsfound[2]; /* Create serial vectors of length NEQ for I.C. and abstol */ yy = N_VNew_Serial(NEQ); yp = N_VNew_Serial(NEQ); abstol = N_VNew_Serial(NEQ); /* Initialize y */ Ith(yy,1) = Y1; Ith(yy,2) = Y2; Ith(yy,3) = Y3; /* Set tolerances */ reltol = RTOL; Ith(abstol,1) = ATOL1; Ith(abstol,2) = ATOL2; Ith(abstol,3) = ATOL3; if (ODE == CP_EXPL) { fct = (void *)f; jac = (void *)jacE; } else { f(T0, yy, yp, NULL); fct = (void *)res; jac = (void *)jacI; } /* Initialize solver */ cpode_mem = CPodeCreate(ODE, CP_BDF, CP_NEWTON); flag = CPodeInit(cpode_mem, fct, NULL, T0, yy, yp, CP_SV, reltol, abstol); /* Set initial step size */ /* { realtype h0; h0 = 8.5e-14; flag = CPodeSetInitStep(cpode_mem, h0); } */ /* Call CPodeRootInit to specify the root function g with 2 components */ flag = CPodeRootInit(cpode_mem, 2, g, NULL); /* Call CPDense to specify the CPDENSE dense linear solver */ flag = CPDense(cpode_mem, NEQ); /* Set the Jacobian routine (comment out for internal DQ) */ flag = CPDlsSetJacFn(cpode_mem, jac, NULL); /* In loop, call CPode, print results, and test for error. Break out of loop when NOUT preset output times have been reached. */ iout = 0; tout = T1; while(1) { flag = CPode(cpode_mem, tout, &t, yy, yp, CP_NORMAL); printf("At t = %0.4le y =%14.6le %14.6le %14.6le\n", t, Ith(yy,1), Ith(yy,2), Ith(yy,3)); if (flag < 0) break; if (flag == CP_ROOT_RETURN) { flagr = CPodeGetRootInfo(cpode_mem, rootsfound); printf(" rootsfound[] = %3d %3d\n", rootsfound[0],rootsfound[1]); } if (flag == CP_SUCCESS) { iout++; tout *= TMULT; } if (iout == NOUT) break; } /* Print some final statistics */ PrintFinalStats(cpode_mem); /* Free memory */ N_VDestroy_Serial(yy); N_VDestroy_Serial(yp); N_VDestroy_Serial(abstol); CPodeFree(&cpode_mem); return(0); }