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); }
// // Writes the solution the the file $path$. // The solution is written for $pointNo$ points. // void OdeProb::WriteSol(const char* path, size_t pointNo) const { const double dx = (XBack() - XFront()) / (pointNo -1); double x; FILE* out; out = fopen(path, "wt"); assert(out); x = XFront(); for(size_t i = 0; i < pointNo - 1; i++) { fprintf(out, "%20lf\t%20lf\n", x, GetSol(x)); x += dx; } // The last point must be written (to avoid the rounding errors) x = XBack(); fprintf(out, "%20lf\t%20lf\n", x, GetSol(x)); fclose(out); }