static PetscErrorCode TSInterpolate_Sundials(TS ts,PetscReal t,Vec X) { TS_Sundials *cvode = (TS_Sundials*)ts->data; N_Vector y; PetscErrorCode ierr; PetscScalar *x_data; PetscInt glosize,locsize; PetscFunctionBegin; /* get the vector size */ ierr = VecGetSize(X,&glosize);CHKERRQ(ierr); ierr = VecGetLocalSize(X,&locsize);CHKERRQ(ierr); /* allocate the memory for N_Vec y */ y = N_VNew_Parallel(cvode->comm_sundials,locsize,glosize); if (!y) SETERRQ(PETSC_COMM_SELF,1,"Interpolated y is not allocated"); ierr = VecGetArray(X,&x_data);CHKERRQ(ierr); N_VSetArrayPointer((realtype *)x_data,y); ierr = CVodeGetDky(cvode->mem,t,0,y);CHKERRQ(ierr); ierr = VecRestoreArray(X,&x_data);CHKERRQ(ierr); PetscFunctionReturn(0); }
void FCV_DKY (realtype *t, int *k, realtype *dky, int *ier) { /* t is the t value where output is desired k is the derivative order F2C_vec is the N_Vector containing the solution derivative on return */ *ier = CVodeGetDky(CV_cvodemem, *t, *k, F2C_vec); dky = N_VGetArrayPointer(F2C_vec); }
PetscErrorCode TSStep_Sundials_Nonlinear(TS ts,int *steps,double *time) { TS_Sundials *cvode = (TS_Sundials*)ts->data; Vec sol = ts->vec_sol; PetscErrorCode ierr; PetscInt i,max_steps = ts->max_steps,flag; long int its; realtype t,tout; PetscScalar *y_data; void *mem; PetscFunctionBegin; mem = cvode->mem; tout = ts->max_time; ierr = VecGetArray(ts->vec_sol,&y_data);CHKERRQ(ierr); N_VSetArrayPointer((realtype *)y_data,cvode->y); ierr = VecRestoreArray(ts->vec_sol,PETSC_NULL);CHKERRQ(ierr); for (i = 0; i < max_steps; i++) { if (ts->ptime >= ts->max_time) break; ierr = TSPreStep(ts);CHKERRQ(ierr); if (cvode->monitorstep){ flag = CVode(mem,tout,cvode->y,&t,CV_ONE_STEP); } else { flag = CVode(mem,tout,cvode->y,&t,CV_NORMAL); } if (flag)SETERRQ1(PETSC_ERR_LIB,"CVode() fails, flag %d",flag); if (t > ts->max_time && cvode->exact_final_time) { /* interpolate to final requested time */ ierr = CVodeGetDky(mem,tout,0,cvode->y);CHKERRQ(ierr); t = tout; } ts->time_step = t - ts->ptime; ts->ptime = t; /* copy the solution from cvode->y to cvode->update and sol */ ierr = VecPlaceArray(cvode->w1,y_data); CHKERRQ(ierr); ierr = VecCopy(cvode->w1,cvode->update);CHKERRQ(ierr); ierr = VecResetArray(cvode->w1); CHKERRQ(ierr); ierr = VecCopy(cvode->update,sol);CHKERRQ(ierr); ierr = CVodeGetNumNonlinSolvIters(mem,&its);CHKERRQ(ierr); ts->nonlinear_its = its; ierr = CVSpilsGetNumLinIters(mem, &its); ts->linear_its = its; ts->steps++; ierr = TSPostStep(ts);CHKERRQ(ierr); ierr = TSMonitor(ts,ts->steps,t,sol);CHKERRQ(ierr); } *steps += ts->steps; *time = t; PetscFunctionReturn(0); }
int CVodeF(void *cvadj_mem, realtype tout, N_Vector yout, realtype *tret, int itask, int *ncheckPtr) { CVadjMem ca_mem; CVodeMem cv_mem; CkpntMem tmp; DtpntMem *dt_mem; int cv_itask, flag; booleantype iret, istop; if (cvadj_mem == NULL) return(CV_ADJMEM_NULL); ca_mem = (CVadjMem) cvadj_mem; cv_mem = ca_mem->cv_mem; dt_mem = ca_mem->dt_mem; iret = TRUE; cv_itask = CV_ONE_STEP; /* Interpret itask */ switch (itask) { case CV_NORMAL: iret = FALSE; istop = FALSE; cv_itask = CV_ONE_STEP; break; case CV_ONE_STEP: iret = TRUE; istop = FALSE; cv_itask = CV_ONE_STEP; break; case CV_NORMAL_TSTOP: iret = FALSE; istop = TRUE; cv_itask = CV_ONE_STEP_TSTOP; break; case CV_ONE_STEP_TSTOP: iret = TRUE; istop = TRUE; cv_itask = CV_ONE_STEP_TSTOP; break; } /* On the first step, load dt_mem[0] */ if ( nst == 0) { dt_mem[0]->t = ca_mem->ck_mem->ck_t0; storePnt(cv_mem, dt_mem[0]); } /* Integrate to tout (in CV_ONE_STEP mode) while loading check points */ loop { /* Perform one step of the integration */ flag = CVode(cv_mem, tout, yout, tret, cv_itask); if (flag < 0) break; /* Test if a new check point is needed */ if ( nst % nsteps == 0 ) { ca_mem->ck_mem->ck_t1 = *tret; /* Create a new check point, load it, and append it to the list */ tmp = CVAckpntNew(cv_mem); if (tmp == NULL) { flag = CV_MEM_FAIL; break; } tmp->ck_next = ca_mem->ck_mem; ca_mem->ck_mem = tmp; nckpnts++; forceSetup = TRUE; /* Reset i=0 and load dt_mem[0] */ dt_mem[0]->t = ca_mem->ck_mem->ck_t0; storePnt(cv_mem, dt_mem[0]); } else { /* Load next point in dt_mem */ dt_mem[nst%nsteps]->t = *tret; storePnt(cv_mem, dt_mem[nst%nsteps]); } /* Set t1 field of the current ckeck point structure for the case in which there will be no future check points */ ca_mem->ck_mem->ck_t1 = *tret; /* tfinal is now set to *tret */ tfinal = *tret; /* Return if in CV_ONE_STEP mode */ if (iret) break; /* Return if tout reached */ if ( (*tret - tout)*h >= ZERO ) { *tret = tout; CVodeGetDky(cv_mem, tout, 0, yout); break; } } /* end of loop() */ /* Get ncheck from ca_mem */ *ncheckPtr = nckpnts; /* Data is available for the last interval */ newData = TRUE; ckpntData = ca_mem->ck_mem; np = nst % nsteps + 1; return(flag); }
void Cvode::writeCVodeOutput(const double &time, const double &h, const int &stp) { #ifdef RUNTIME_PROFILING MEASURETIME_REGION_DEFINE(cvodeWriteOutputHandler, "CVodeWriteOutput"); if(MeasureTime::getInstance() != NULL) { MEASURETIME_START(measuredFunctionStartValues, cvodeWriteOutputHandler, "CVodeWriteOutput"); } #endif if (stp > 0) { if (_cvodesettings->getDenseOutput()) { _bWritten = false; /* double *oldValues = NULL;*/ //We have to find all output-points within the last solver step while (_tLastWrite + dynamic_cast<ISolverSettings*>(_cvodesettings)->getGlobalSettings()->gethOutput() <= time) { if (!_bWritten) { _continuous_system->restoreOldValues(); ////Rescue the calculated derivatives // oldValues = new double[_continuous_system->getDimRHS()]; // _continuous_system->getRHS(oldValues); } _bWritten = true; _tLastWrite = _tLastWrite + dynamic_cast<ISolverSettings*>(_cvodesettings)->getGlobalSettings()->gethOutput(); //Get the state vars at the output-point (interpolated) _idid = CVodeGetDky(_cvodeMem, _tLastWrite, 0, _CV_yWrite); _time_system->setTime(_tLastWrite); _continuous_system->setContinuousStates(NV_DATA_S(_CV_yWrite)); _continuous_system->evaluateAll(IContinuous::CONTINUOUS); SolverDefaultImplementation::writeToFile(stp, _tLastWrite, h); } //end if time -_tLastWritten if (_bWritten) { _time_system->setTime(time); _continuous_system->setContinuousStates(_z); _continuous_system->restoreNewValues(); /* _continuous_system->setRHS(oldValues); delete[] oldValues;*/ } else if (time == _tEnd && _tLastWrite != time) { _idid = CVodeGetDky(_cvodeMem, time, 0, _CV_y); _time_system->setTime(time); _continuous_system->setContinuousStates(NV_DATA_S(_CV_y)); _continuous_system->evaluateAll(IContinuous::CONTINUOUS); SolverDefaultImplementation::writeToFile(stp, _tEnd, h); } } else { SolverDefaultImplementation::writeToFile(stp, time, h); } } #ifdef RUNTIME_PROFILING if(MeasureTime::getInstance() != NULL) { MEASURETIME_END(measuredFunctionStartValues, measuredFunctionEndValues, (*measureTimeFunctionsArray)[2], cvodeWriteOutputHandler); } #endif }