void OpenSMOKE_CVODE_Sundials<T>::Status() const { int flag; long int nst, nfe, nsetups, netf, nni, ncfn, nje, nfeLS, nge; int qcurrent, qlast; double hcurrent, hlast; flag = CVodeGetNumSteps(cvode_mem_, &nst); check_flag(&flag, std::string("CVodeGetNumSteps"), 1); flag = CVDlsGetNumJacEvals(cvode_mem_, &nje); check_flag(&flag, std::string("CVDlsGetNumJacEvals"), 1); flag = CVodeGetNumRhsEvals(cvode_mem_, &nfe); check_flag(&flag, std::string("CVodeGetNumRhsEvals"), 1); flag = CVodeGetNumLinSolvSetups(cvode_mem_, &nsetups); check_flag(&flag, std::string("CVodeGetNumLinSolvSetups"), 1); flag = CVodeGetNumErrTestFails(cvode_mem_, &netf); check_flag(&flag, std::string("CVodeGetNumErrTestFails"), 1); flag = CVodeGetNumNonlinSolvIters(cvode_mem_, &nni); check_flag(&flag, std::string("CVodeGetNumNonlinSolvIters"), 1); flag = CVodeGetNumNonlinSolvConvFails(cvode_mem_, &ncfn); check_flag(&flag, std::string("CVodeGetNumNonlinSolvConvFails"), 1); flag = CVodeGetNumGEvals(cvode_mem_, &nge); check_flag(&flag, std::string("CVodeGetNumGEvals"), 1); flag = CVDlsGetNumRhsEvals(cvode_mem_, &nfeLS); check_flag(&flag, std::string("CVDlsGetNumRhsEvals"), 1); flag = CVodeGetLastOrder(cvode_mem_, &qlast); check_flag(&flag, std::string("CVodeGetLastOrder"), 1); flag = CVodeGetCurrentOrder(cvode_mem_, &qcurrent); check_flag(&flag, std::string("CVodeGetCurrentOrder"), 1); flag = CVodeGetLastStep(cvode_mem_, &hlast); check_flag(&flag, std::string("CVodeGetLastStep"), 1); flag = CVodeGetCurrentStep(cvode_mem_, &hcurrent); check_flag(&flag, std::string("CVodeGetCurrentStep"), 1); std::cout << "CVODE Sundials Status" << std::endl; std::cout << " * Absolute tolerance: " << this->absTolerance_[0] << std::endl; // Absolute tolerance std::cout << " * Relative tolerance: " << this->relTolerance_[0] << std::endl; // Relative tolerance std::cout << " * Number of steps: " << nst << std::endl; // Number of steps taken for the problem so far std::cout << " * Number of function evaluations: " << nfe << std::endl; // Number of f evaluations for the problem so far. std::cout << " * Number of Jacobians: " << nje << std::endl; // Number of Jacobian evaluations (and of matrix LU decompositions) for the problem so far. std::cout << " * Last step: " << hlast << std::endl; std::cout << " * Next step: " << hcurrent << std::endl; std::cout << " * Last order: " << qlast << std::endl; std::cout << " * Next order: " << qcurrent << std::endl; }
void handleError( void *cvode_mem, N_Vector y0, int flag, mxArray *plhs[], int nrhs, int sensitivity, N_Vector *yS0, realtype *sensitivities, struct mData *data ) { #ifdef DEBUG long int temp; realtype tempreal; #endif #ifdef DEBUG printf( "<<< DEBUG OUTPUT >>>\n" ); printf( "PARAMETERS: \n" ); for ( temp = 0; temp < N_PARAMS; temp++ ) { printf( "P(%d) = %f\n", temp, data->p[ temp ] ); } CVodeGetNumSteps(cvode_mem, &temp); printf( "Number of steps taken by the solver: %d\n", temp ); CVodeGetNumErrTestFails(cvode_mem, &temp); printf( "Number of local error test failures: %d\n", temp ); CVodeGetLastStep(cvode_mem, &tempreal); printf( "Last stepsize: %f\n", tempreal ); CVodeGetCurrentStep(cvode_mem, &tempreal); printf( "Last step: %f\n", tempreal ); #endif if ( sensitivity == 1 ) { if ( nrhs < 7 ) free( sensitivities ); free( data->p ); N_VDestroyVectorArray_Serial( yS0, N_STATES + N_PARAMS ); if ( plhs[2] != NULL ) mxDestroyArray(plhs[2]); } if ( plhs[1] != NULL ) mxDestroyArray(plhs[1]); if ( plhs[0] != NULL ) mxDestroyArray(plhs[0]); N_VDestroy_Serial( y0 ); /*printf( "Freeing..." );*/ /*CVodeFree( &cvode_mem );*/ /*printf( "Success!\n" );*/ switch( flag ) { case CV_MEM_NULL: printf( "ERROR: No memory was allocated for cvode_mem\n" ); break; case CV_NO_MALLOC: printf( "ERROR: Forgot or failed CVodeInit\n" ); break; case CV_ILL_INPUT: printf( "ERROR: Input for CVode was illegal\n" ); break; case CV_TOO_CLOSE: printf( "ERROR: Initial time too close to final time\n" ); break; case CV_TOO_MUCH_WORK: printf( "ERROR: Solver took maximum number of internal steps, but hasn't reached t_out\n" ); break; case CV_TOO_MUCH_ACC: printf( "ERROR: Could not attain desired accuracy\n" ); break; case CV_ERR_FAILURE: printf( "ERROR: Error tests failed too many times\n" ); break; case CV_CONV_FAILURE: printf( "ERROR: Convergence failure in solving the linear system\n" ); break; case CV_LINIT_FAIL: printf( "ERROR: Linear solver failed to initialize\n" ); break; case CV_LSETUP_FAIL: printf( "ERROR: Linear solver setup failed\n" ); break; case CV_RHSFUNC_FAIL: printf( "ERROR: Right hand side failed in an unrecoverable manner\n" ); break; case CV_REPTD_RHSFUNC_ERR: printf( "ERROR: Convergence test failures occured too many times in RHS\n" ); break; case CV_UNREC_RHSFUNC_ERR: printf( "ERROR: Unrecoverable error in the RHS\n" ); break; case CV_RTFUNC_FAIL: printf( "ERROR: Rootfinding function failed!\n" ); break; default: printf( "ERROR: I have no idea what's going on :(\n" ); break; } mexErrMsgTxt( "Aborting" ); }
int Cvode::calcJacobian(double t, long int N, N_Vector fHelp, N_Vector errorWeight, N_Vector jthCol, double* y, N_Vector fy, DlsMat Jac) { try { int l,g; double fnorm, minInc, *f_data, *fHelp_data, *errorWeight_data, h, srur, delta_inv; f_data = NV_DATA_S(fy); errorWeight_data = NV_DATA_S(errorWeight); fHelp_data = NV_DATA_S(fHelp); //Get relevant info _idid = CVodeGetErrWeights(_cvodeMem, errorWeight); if (_idid < 0) { _idid = -5; throw ModelicaSimulationError(SOLVER,"Cvode::calcJacobian()"); } _idid = CVodeGetCurrentStep(_cvodeMem, &h); if (_idid < 0) { _idid = -5; throw ModelicaSimulationError(SOLVER,"Cvode::calcJacobian()"); } srur = sqrt(UROUND); fnorm = N_VWrmsNorm(fy, errorWeight); minInc = (fnorm != 0.0) ? (1000.0 * abs(h) * UROUND * N * fnorm) : 1.0; for(int j=0;j<N;j++) { _delta[j] = max(srur*abs(y[j]), minInc/errorWeight_data[j]); } for(int j=0;j<N;j++) { _deltaInv[j] = 1/_delta[j]; } if (_jacobianANonzeros != 0) { for(int color=1; color <= _maxColors; color++) { for(int k=0; k < _dimSys; k++) { if((_colorOfColumn[k] ) == color) { _ysave[k] = y[k]; y[k]+= _delta[k]; } } calcFunction(t, y, fHelp_data); for (int k = 0; k < _dimSys; k++) { if((_colorOfColumn[k]) == color) { y[k] = _ysave[k]; int startOfColumn = k * _dimSys; for (int j = _jacobianALeadindex[k]; j < _jacobianALeadindex[k+1];j++) { l = _jacobianAIndex[j]; g = l + startOfColumn; Jac->data[g] = (fHelp_data[l] - f_data[l]) * _deltaInv[k]; } } } } } } /* //Calculation of J without colouring for (j = 0; j < N; j++) { //N_VSetArrayPointer(DENSE_COL(Jac,j), jthCol); _ysave[j] = y[j]; y[j] += _delta[j]; calcFunction(t, y, fHelp_data); y[j] = _ysave[j]; delta_inv = 1.0/_delta[j]; N_VLinearSum(delta_inv, fHelp, -delta_inv, fy, jthCol); for(int i=0; i<_dimSys; ++i) { Jac->data[i+j*_dimSys] = NV_Ith_S(jthCol,i); } //DENSE_COL(Jac,j) = N_VGetArrayPointer(jthCol); } */ //workaround until exception can be catch from c- libraries catch (std::exception & ex ) { cerr << "CVode integration error: " << ex.what(); return 1; } return 0; }