12 #include "../../ext/cvode/include/llnltyps.h"
13 #include "../../ext/cvode/include/llnlmath.h"
14 #include "../../ext/cvode/include/cvode.h"
15 #include "../../ext/cvode/include/cvdense.h"
16 #include "../../ext/cvode/include/cvdiag.h"
17 #include "../../ext/cvode/include/cvspgmr.h"
18 #include "../../ext/cvode/include/nvector.h"
19 #include "../../ext/cvode/include/cvode.h"
23 inline static N_Vector nv(
void* x)
25 return reinterpret_cast<N_Vector
>(x);
39 static void cvode_rhs(integer N, real t, N_Vector y, N_Vector ydot,
42 double* ydata = N_VDATA(y);
43 double* ydotdata = N_VDATA(ydot);
45 f->
eval(t, ydata, ydotdata, NULL);
54 static void cvode_jac(integer N, DenseMat J, RhsFn f,
void* f_data,
55 real t, N_Vector y, N_Vector fy, N_Vector ewt, real h, real uround,
56 void* jac_data,
long int* nfePtr, N_Vector vtemp1, N_Vector vtemp2,
60 double* ydata = N_VDATA(y);
61 double* fydata = N_VDATA(fy);
62 double* ewtdata = N_VDATA(ewt);
63 double* ydot = N_VDATA(vtemp1);
70 for (j=0; j < N; j++) {
74 ydata[j] = ysave + dy;
75 dy = ydata[j] - ysave;
76 func->
eval(t, ydata, ydot, NULL);
77 for (i=0; i < N; i++) {
78 col_j[i] = (ydot[i] - fydata[i])/dy;
93 CVodeInt::CVodeInt() : m_neq(0),
109 m_ropt.resize(OPT_SIZE,0.0);
110 m_iopt =
new long[OPT_SIZE];
111 fill(m_iopt, m_iopt+OPT_SIZE,0);
119 CVodeFree(m_cvode_mem);
125 N_VFree(nv(m_abstol));
132 return N_VIth(nv(m_y),
int(k));
136 return N_VDATA(nv(m_y));
143 if (m_nabs != m_neq) {
145 N_VFree(nv(m_abstol));
147 m_abstol =
reinterpret_cast<void*
>(N_VNew(m_nabs, 0));
149 for (
int i=0; i<m_nabs; i++) {
150 N_VIth(nv(m_abstol), i) = abstol[i];
190 void CVodeInt::setMaxSteps(
int nmax)
193 m_iopt[MXSTEP] = m_maxsteps;
209 m_neq = int(func.
neq());
215 m_y =
reinterpret_cast<void*
>(N_VNew(m_neq, 0));
217 if (m_itol == 1 && m_nabs < m_neq) {
218 throw CVodeErr(
"not enough absolute tolerance values specified.");
223 m_iopt[MXSTEP] = m_maxsteps;
224 m_iopt[MAXORD] = m_maxord;
225 m_ropt[HMAX] = m_hmax;
228 CVodeFree(m_cvode_mem);
232 m_data = (
void*)&func;
235 m_cvode_mem = CVodeMalloc(m_neq,
cvode_rhs, m_t0, nv(m_y), m_method,
236 m_iter, m_itol, &m_reltol,
237 nv(m_abstol), m_data, NULL, 1, m_iopt,
240 m_cvode_mem = CVodeMalloc(m_neq,
cvode_rhs, m_t0, nv(m_y), m_method,
241 m_iter, m_itol, &m_reltol,
242 &m_abstols, m_data, NULL, 1, m_iopt,
247 throw CVodeErr(
"CVodeMalloc failed.");
250 if (m_type == DENSE + NOJAC) {
251 CVDense(m_cvode_mem, NULL, NULL);
252 }
else if (m_type == DENSE + JAC) {
254 }
else if (m_type == DIAG) {
256 }
else if (m_type == GMRES) {
257 CVSpgmr(m_cvode_mem, NONE, MODIFIED_GS, 0, 0.0,
260 throw CVodeErr(
"unsupported option");
265 void CVodeInt::reinitialize(
double t0,
FuncEval& func)
271 m_iopt[MXSTEP] = m_maxsteps;
272 m_iopt[MAXORD] = m_maxord;
273 m_ropt[HMAX] = m_hmax;
278 m_data = (
void*)&func;
281 result = CVReInit(m_cvode_mem,
cvode_rhs, m_t0, nv(m_y), m_method,
282 m_iter, m_itol, &m_reltol,
283 nv(m_abstol), m_data, NULL, 1, m_iopt,
286 result = CVReInit(m_cvode_mem,
cvode_rhs, m_t0, nv(m_y), m_method,
287 m_iter, m_itol, &m_reltol,
288 &m_abstols, m_data, NULL, 1, m_iopt,
293 throw CVodeErr(
"CVReInit failed.");
296 if (m_type == DENSE + NOJAC) {
297 CVDense(m_cvode_mem, NULL, NULL);
298 }
else if (m_type == DENSE + JAC) {
300 }
else if (m_type == DIAG) {
302 }
else if (m_type == GMRES) {
303 CVSpgmr(m_cvode_mem, NONE, MODIFIED_GS, 0, 0.0,
306 throw CVodeErr(
"unsupported option");
314 flag = CVode(m_cvode_mem, tout, nv(m_y), &t, NORMAL);
315 if (flag != SUCCESS) {
316 throw CVodeErr(
" CVode error encountered. Error code: " +
int2str(flag));
324 flag = CVode(m_cvode_mem, tout, nv(m_y), &t, ONE_STEP);
325 if (flag != SUCCESS) {
326 throw CVodeErr(
" CVode error encountered. Error code: " +
int2str(flag));