void RefSol(realtype tout, N_Vector yref) { void *cpode_mem; N_Vector yy, yp; realtype tol, t, th, thd; int flag; yy = N_VNew_Serial(2); yp = N_VNew_Serial(2); Ith(yy,1) = 0.0; /* theta */ Ith(yy,2) = 0.0; /* thetad */ tol = TOL_REF; cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeSetMaxNumSteps(cpode_mem, 100000); flag = CPodeInit(cpode_mem, (void *)fref, NULL, 0.0, yy, yp, CP_SS, tol, &tol); flag = CPDense(cpode_mem, 2); flag = CPodeSetStopTime(cpode_mem, tout); flag = CPode(cpode_mem, tout, &t, yy, yp, CP_NORMAL_TSTOP); th = Ith(yy,1); thd = Ith(yy,2); Ith(yref,1) = cos(th); Ith(yref,2) = sin(th); Ith(yref,3) = -thd*sin(th); Ith(yref,4) = thd*cos(th); N_VDestroy_Serial(yy); N_VDestroy_Serial(yp); CPodeFree(&cpode_mem); return; }
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); }
static void Problem2(void) { void *fct; void *cpode_mem; N_Vector y, yp; realtype reltol=RTOL, abstol=ATOL, t, tout, erm, hu; int flag, iout, qu; y = NULL; yp = NULL; cpode_mem = NULL; y = N_VNew_Serial(P2_NEQ); N_VConst(ZERO, y); NV_Ith_S(y,0) = ONE; yp = N_VNew_Serial(P2_NEQ); if (ODE == CP_EXPL) { fct = (void *)f2; } else { fct = (void *)res2; f2(P2_T0, y, yp, NULL); } cpode_mem = CPodeCreate(ODE, CP_ADAMS, CP_FUNCTIONAL); /* flag = CPodeSetInitStep(cpode_mem, 2.0e-9);*/ flag = CPodeInit(cpode_mem, fct, NULL, P2_T0, y, yp, CP_SS, reltol, &abstol); printf("\n t max.err qu hu \n"); for(iout=1, tout=P2_T1; iout <= P2_NOUT; iout++, tout*=P2_TOUT_MULT) { flag = CPode(cpode_mem, tout, &t, y, yp, CP_NORMAL); if (flag != CP_SUCCESS) break; erm = MaxError(y, t); flag = CPodeGetLastOrder(cpode_mem, &qu); flag = CPodeGetLastStep(cpode_mem, &hu); printf("%10.3f %12.4le %2d %12.4le\n", t, erm, qu, hu); } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); N_VDestroy_Serial(y); N_VDestroy_Serial(yp); return; }
int main() { void *fct; void *cpode_mem; N_Vector y, yp; realtype reltol=RTOL, abstol=ATOL, t, tout, hu; int flag, iout, qu; y = NULL; yp = NULL; cpode_mem = NULL; y = N_VNew_Serial(P1_NEQ); NV_Ith_S(y,0) = TWO; NV_Ith_S(y,1) = ZERO; yp = N_VNew_Serial(P1_NEQ); if (ODE == CP_EXPL) { fct = (void *)f; } else { fct = (void *)res; f(P1_T0, y, yp, NULL); } cpode_mem = CPodeCreate(ODE, CP_ADAMS, CP_FUNCTIONAL); /* flag = CPodeSetInitStep(cpode_mem, 4.0e-9);*/ flag = CPodeInit(cpode_mem, fct, NULL, P1_T0, y, yp, CP_SS, reltol, &abstol); printf("\n t x xdot qu hu \n"); for(iout=1, tout=P1_T1; iout <= P1_NOUT; iout++, tout += P1_DTOUT) { flag = CPode(cpode_mem, tout, &t, y, yp, CP_NORMAL); if (flag != CP_SUCCESS) break; flag = CPodeGetLastOrder(cpode_mem, &qu); flag = CPodeGetLastStep(cpode_mem, &hu); printf("%10.5f %12.5le %12.5le %2d %6.4le\n", t, NV_Ith_S(y,0), NV_Ith_S(y,1), qu, hu); } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); N_VDestroy_Serial(y); N_VDestroy_Serial(yp); return 0; }
int main() { void *cpode_mem; N_Vector yref, yy0; realtype tol, tout; int i, flag; tout = 30.0; /* Get reference solution */ yref = N_VNew_Serial(4); RefSol(tout, yref); /* Initialize solver */ tol = TOL; cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); yy0 = N_VNew_Serial(4); Ith(yy0,1) = 1.0; /* x */ Ith(yy0,2) = 0.0; /* y */ Ith(yy0,3) = 0.0; /* xd */ Ith(yy0,4) = 0.0; /* yd */ flag = CPodeInit(cpode_mem, (void *)f, NULL, 0.0, yy0, NULL, CP_SS, tol, &tol); flag = CPodeSetMaxNumSteps(cpode_mem, 50000); flag = CPodeSetStopTime(cpode_mem, tout); flag = CPodeProjDefine(cpode_mem, proj, NULL); flag = CPDense(cpode_mem, 4); for (i=0;i<5;i++) { printf("\n\n%.2e\n", tol); GetSol(cpode_mem, yy0, tol, tout, TRUE, yref); GetSol(cpode_mem, yy0, tol, tout, FALSE, yref); tol /= 10.0; } N_VDestroy_Serial(yref); CPodeFree(&cpode_mem); 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); }
int main() { void *cpode_mem; N_Vector yy, yp, ctols; realtype reltol, abstol, t, tout, Tout; realtype x, y, xd, yd, g; int iout, Nout, flag; printf("double %d\n",(int)sizeof(double)); printf("long double %d\n",(int)sizeof(long double)); printf("float %d\n",(int)sizeof(float)); printf("realtype %d\n",(int)sizeof(realtype)); yy = N_VNew_Serial(4); yp = N_VNew_Serial(4); /* Initialize y */ Ith(yy,1) = 1.0; /* x */ Ith(yy,2) = 0.0; /* y */ Ith(yy,3) = 0.0; /* xd */ Ith(yy,4) = 0.0; /* yd */ /* Set tolerances */ reltol = RTOL; abstol = ATOL; Nout = 30; Tout = Nout*1.0; /* Initialize solver */ cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cpode_mem, f, NULL, 0.0, yy, yp, CP_SS, reltol, &abstol); flag = CPodeSetMaxNumSteps(cpode_mem, 5000); flag = CPodeSetStopTime(cpode_mem, Tout); flag = CPDense(cpode_mem, 4); /* USER-PROVIDED PROJECTION FUNCTION */ /* flag = CPodeProjDefine(cpode_mem, proj, NULL); */ /* INTERNAL PROJECTION FUNCTION */ ctols = N_VNew_Serial(2); Ith(ctols,1) = 1.0e-8; Ith(ctols,2) = 1.0e-8; flag = CPodeProjInit(cpode_mem, CP_PROJ_L2NORM, CP_CNSTR_NONLIN, cfun, NULL, ctols); flag = CPodeSetProjTestCnstr(cpode_mem, TRUE); flag = CPDenseProj(cpode_mem, 2, 4, CPDIRECT_LU); flag = CPodeSetProjUpdateErrEst(cpode_mem, FALSE); /* DISABLE PROJECTION */ /* CPodeSetProjFrequency(cpode_mem, 0); */ /* INTEGRATE TO FINAL TIME */ /* flag = CPode(cpode_mem, Tout, &t, yy, yp, CP_NORMAL_TSTOP); x = Ith(yy,1); y = Ith(yy,2); xd = Ith(yy,3); yd = Ith(yy,4); g = x*x + y*y - 1.0; Ith(yy,1) = x - 1.0; Ith(yy,2) = y - 0.0; Ith(yy,3) = xd - 0.0; Ith(yy,4) = yd - 0.0; printf("%14.10e %14.10e %14.10e %14.10e | %14.10e\n", Ith(yy,1),Ith(yy,2),Ith(yy,3),Ith(yy,4),g); */ /* INTEGRATE THROUGH A SEQUENCE OF TIMES */ t = 0.0; for(iout=1; iout<=Nout; iout++) { tout = iout*1.0; flag = CPode(cpode_mem, tout, &t, yy, yp, CP_NORMAL_TSTOP); if (flag < 0) break; x = Ith(yy,1); y = Ith(yy,2); xd = Ith(yy,3); yd = Ith(yy,4); g = x*x + y*y - 1.0; printf(" -------------- %lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf\n", t, x,y,xd,yd,g); } PrintFinalStats(cpode_mem); N_VDestroy_Serial(yy); N_VDestroy_Serial(yp); N_VDestroy_Serial(ctols); CPodeFree(&cpode_mem); return(0); }
int main() { PbData data; realtype R, L, m, g, V0; void *cpode_mem; N_Vector yy, yp, ctols; realtype reltol, abstol; realtype t0, tf, t; realtype x1, y1, x2, y2; realtype vx1, vy1, vx2, vy2; int flag, Neq, Nc; int rdir[1], iroots[1]; FILE *fout; /* -------------------------------- * INITIALIZATIONS * -------------------------------- */ R = 0.1; /* ball radius */ L = 1.0; /* pendulum length */ m = 1.0; /* pendulum mass */ g = 9.8; /* gravitational acc. */ V0 = 3.0; /* initial velocity for pendulum 1 */ /* Set-up user data structure */ data = (PbData)malloc(sizeof *data); data->R = R; data->L = L; data->m = m; data->g = g; /* Problem dimensions */ Neq = 2*2*2; Nc = 2*2; /* Solution vectors */ yy = N_VNew_Serial(Neq); yp = N_VNew_Serial(Neq); /* Integration limits */ t0 = 0.0; tf = 6.0; /* Integration and projection tolerances */ reltol = 1.0e-8; abstol = 1.0e-8; ctols = N_VNew_Serial(Nc); N_VConst(1.0e-8, ctols); /* Direction of monitored events * (only zero-crossing with decreasing even function) */ rdir[0] = -1; /* -------------------------------- * CASE 1 * -------------------------------- */ fout = fopen("newton1.out","w"); /* Initial conditions */ NV_Ith_S(yy,0) = R; NV_Ith_S(yy,4) = -R; NV_Ith_S(yy,1) = -L; NV_Ith_S(yy,5) = -L; NV_Ith_S(yy,2) = V0; NV_Ith_S(yy,6) = 0.0; NV_Ith_S(yy,3) = 0.0; NV_Ith_S(yy,7) = 0.0; /* Initialize solver */ cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cpode_mem, ffun1, data, t0, yy, yp, CP_SS, reltol, &abstol); flag = CPDense(cpode_mem, Neq); flag = CPodeRootInit(cpode_mem, 1, gfun, data); flag = CPodeSetRootDirection(cpode_mem, rdir); /* Set-up the internal projection */ flag = CPodeProjInit(cpode_mem, CP_PROJ_L2NORM, CP_CNSTR_NONLIN, cfun, data, ctols); flag = CPodeSetProjTestCnstr(cpode_mem, TRUE); flag = CPDenseProj(cpode_mem, Nc, Neq, CPDIRECT_LU); /* Integrate in ONE_STEP mode, while monitoring events */ t = t0; while(t<tf) { flag = CPode(cpode_mem, tf, &t, yy, yp, CP_ONE_STEP); if (flag < 0) break; x1 = NV_Ith_S(yy,0); x2 = NV_Ith_S(yy,4); y1 = NV_Ith_S(yy,1); y2 = NV_Ith_S(yy,5); vx1 = NV_Ith_S(yy,2); vx2 = NV_Ith_S(yy,6); vy1 = NV_Ith_S(yy,3); vy2 = NV_Ith_S(yy,7); fprintf(fout, "%lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf", t, x1, y1, x2, y2, vx1, vy1, vx2, vy2); if (flag == CP_ROOT_RETURN) { CPodeGetRootInfo(cpode_mem, iroots); fprintf(fout, " %d\n", iroots[0]); /* Note: the test iroots[0]<0 is really needed ONLY if not using rdir */ if (iroots[0] < 0) { /* Update velocities in yy */ contact(yy, data); /* reinitialize CPODES solver */ flag = CPodeReInit(cpode_mem, ffun1, data, t, yy, yp, CP_SS, reltol, &abstol); } } else { fprintf(fout, " 0\n"); } } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); fclose(fout); /* -------------------------------- * CASE 2 * -------------------------------- */ fout = fopen("newton2.out","w"); /* Initial conditions */ NV_Ith_S(yy,0) = R; NV_Ith_S(yy,4) = -R; NV_Ith_S(yy,1) = -L; NV_Ith_S(yy,5) = -L; NV_Ith_S(yy,2) = 0.0; NV_Ith_S(yy,6) = 0.0; NV_Ith_S(yy,3) = 0.0; NV_Ith_S(yy,7) = 0.0; /* Initialize solver */ cpode_mem = CPodeCreate(CP_EXPL, CP_BDF, CP_NEWTON); flag = CPodeInit(cpode_mem, ffun2, data, t0, yy, yp, CP_SS, reltol, &abstol); flag = CPDense(cpode_mem, Neq); flag = CPodeRootInit(cpode_mem, 1, gfun, data); flag = CPodeSetRootDirection(cpode_mem, rdir); /* Set-up the internal projection */ flag = CPodeProjInit(cpode_mem, CP_PROJ_L2NORM, CP_CNSTR_NONLIN, cfun, data, ctols); flag = CPodeSetProjTestCnstr(cpode_mem, TRUE); flag = CPDenseProj(cpode_mem, Nc, Neq, CPDIRECT_LU); /* Integrate in ONE_STEP mode, while monitoring events */ t = t0; while(t<tf) { flag = CPode(cpode_mem, tf, &t, yy, yp, CP_ONE_STEP); if (flag < 0) break; x1 = NV_Ith_S(yy,0); x2 = NV_Ith_S(yy,4); y1 = NV_Ith_S(yy,1); y2 = NV_Ith_S(yy,5); vx1 = NV_Ith_S(yy,2); vx2 = NV_Ith_S(yy,6); vy1 = NV_Ith_S(yy,3); vy2 = NV_Ith_S(yy,7); fprintf(fout, "%lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf %14.10lf", t, x1, y1, x2, y2, vx1, vy1, vx2, vy2); if (flag == CP_ROOT_RETURN) { CPodeGetRootInfo(cpode_mem, iroots); fprintf(fout, " %d\n", iroots[0]); /* Note: the test iroots[0]<0 is really needed ONLY if not using rdir */ if (iroots[0] < 0) { /* Update velocities in yy */ contact(yy, data); /* reinitialize CPODES solver */ flag = CPodeReInit(cpode_mem, ffun2, data, t, yy, yp, CP_SS, reltol, &abstol); } } else { fprintf(fout, " 0\n"); } } PrintFinalStats(cpode_mem); CPodeFree(&cpode_mem); fclose(fout); /* -------------------------------- * CLEAN-UP * -------------------------------- */ free(data); N_VDestroy_Serial(yy); N_VDestroy_Serial(yp); N_VDestroy_Serial(ctols); return(0); }