Actual source code: sundials.c

  1: /*
  2:     Provides a PETSc interface to version 2.5 of SUNDIALS/CVODE solver (a very old version)
  3:     The interface to PVODE (old version of CVODE) was originally contributed
  4:     by Liyang Xu. It has been redone by Hong Zhang and Dinesh Kaushik.

  6:     Reference: sundials-2.4.0/examples/cvode/parallel/cvDiurnal_kry_p.c
  7: */
  8: #include <../src/ts/impls/implicit/sundials/sundials.h>

 10: /*
 11:       TSPrecond_Sundials - function that we provide to SUNDIALS to
 12:                         evaluate the preconditioner.
 13: */
 14: static PetscErrorCode TSPrecond_Sundials_Petsc(realtype tn, N_Vector y, N_Vector fy, booleantype jok, booleantype *jcurPtr, realtype _gamma, void *P_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3)
 15: {
 16:   TS           ts    = (TS)P_data;
 17:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
 18:   PC           pc;
 19:   Mat          J, P;
 20:   Vec          yy = cvode->w1, yydot = cvode->ydot;
 21:   PetscReal    gm = (PetscReal)_gamma;
 22:   PetscScalar *y_data;

 24:   PetscFunctionBegin;
 25:   PetscCall(TSGetIJacobian(ts, &J, &P, NULL, NULL));
 26:   y_data = (PetscScalar *)N_VGetArrayPointer(y);
 27:   PetscCall(VecPlaceArray(yy, y_data));
 28:   PetscCall(VecZeroEntries(yydot)); /* The Jacobian is independent of Ydot for ODE which is all that CVode works for */
 29:   /* compute the shifted Jacobian   (1/gm)*I + Jrest */
 30:   PetscCall(TSComputeIJacobian(ts, ts->ptime, yy, yydot, 1 / gm, J, P, PETSC_FALSE));
 31:   PetscCall(VecResetArray(yy));
 32:   PetscCall(MatScale(P, gm)); /* turn into I-gm*Jrest, J is not used by SUNDIALS  */
 33:   *jcurPtr = TRUE;
 34:   PetscCall(TSSundialsGetPC(ts, &pc));
 35:   PetscCall(PCSetOperators(pc, J, P));
 36:   PetscFunctionReturn(PETSC_SUCCESS);
 37: }

 39: /* Sundial expects an int (*)(args...) but PetscErrorCode is an enum. Instead of switching out
 40:    all the PetscCalls in TSPrecond_Sundials_Petsc we just wrap it */
 41: static int TSPrecond_Sundials_Private(realtype tn, N_Vector y, N_Vector fy, booleantype jok, booleantype *jcurPtr, realtype _gamma, void *P_data, N_Vector vtemp1, N_Vector vtemp2, N_Vector vtemp3)
 42: {
 43:   return (int)TSPrecond_Sundials_Petsc(tn, y, fy, jok, jcurPtr, _gamma, P_data, vtemp1, vtemp2, vtemp3);
 44: }

 46: /*
 47:      TSPSolve_Sundials -  routine that we provide to SUNDIALS that applies the preconditioner.
 48: */
 49: static PetscErrorCode TSPSolve_Sundials_Petsc(realtype tn, N_Vector y, N_Vector fy, N_Vector r, N_Vector z, realtype _gamma, realtype delta, int lr, void *P_data, N_Vector vtemp)
 50: {
 51:   TS           ts    = (TS)P_data;
 52:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
 53:   PC           pc;
 54:   Vec          rr = cvode->w1, zz = cvode->w2;
 55:   PetscScalar *r_data, *z_data;

 57:   PetscFunctionBegin;
 58:   /* Make the PETSc work vectors rr and zz point to the arrays in the SUNDIALS vectors r and z respectively*/
 59:   r_data = (PetscScalar *)N_VGetArrayPointer(r);
 60:   z_data = (PetscScalar *)N_VGetArrayPointer(z);
 61:   PetscCall(VecPlaceArray(rr, r_data));
 62:   PetscCall(VecPlaceArray(zz, z_data));

 64:   /* Solve the Px=r and put the result in zz */
 65:   PetscCall(TSSundialsGetPC(ts, &pc));
 66:   PetscCall(PCApply(pc, rr, zz));
 67:   PetscCall(VecResetArray(rr));
 68:   PetscCall(VecResetArray(zz));
 69:   PetscFunctionReturn(PETSC_SUCCESS);
 70: }

 72: /* See TSPrecond_Sundials_Private() */
 73: static int TSPSolve_Sundials_Private(realtype tn, N_Vector y, N_Vector fy, N_Vector r, N_Vector z, realtype _gamma, realtype delta, int lr, void *P_data, N_Vector vtemp)
 74: {
 75:   return (int)TSPSolve_Sundials_Petsc(tn, y, fy, r, z, _gamma, delta, lr, P_data, vtemp);
 76: }

 78: /*
 79:         TSFunction_Sundials - routine that we provide to SUNDIALS that applies the right-hand side.
 80: */
 81: static int TSFunction_Sundials(realtype t, N_Vector y, N_Vector ydot, PetscCtx ctx)
 82: {
 83:   TS             ts = (TS)ctx;
 84:   DM             dm;
 85:   DMTS           tsdm;
 86:   TSIFunctionFn *ifunction;
 87:   MPI_Comm       comm;
 88:   TS_Sundials   *cvode = (TS_Sundials *)ts->data;
 89:   Vec            yy = cvode->w1, yyd = cvode->w2, yydot = cvode->ydot;
 90:   PetscScalar   *y_data, *ydot_data;

 92:   PetscFunctionBegin;
 93:   PetscCall(PetscObjectGetComm((PetscObject)ts, &comm));
 94:   /* Make the PETSc work vectors yy and yyd point to the arrays in the SUNDIALS vectors y and ydot respectively*/
 95:   y_data    = (PetscScalar *)N_VGetArrayPointer(y);
 96:   ydot_data = (PetscScalar *)N_VGetArrayPointer(ydot);
 97:   PetscCallAbort(comm, VecPlaceArray(yy, y_data));
 98:   PetscCallAbort(comm, VecPlaceArray(yyd, ydot_data));

100:   /* Now compute the right-hand side function, via IFunction unless only the more efficient RHSFunction is set */
101:   PetscCall(TSGetDM(ts, &dm));
102:   PetscCall(DMGetDMTS(dm, &tsdm));
103:   PetscCall(DMTSGetIFunction(dm, &ifunction, NULL));
104:   if (!ifunction) PetscCall(TSComputeRHSFunction(ts, t, yy, yyd));
105:   else {
106:     /* If rhsfunction is also set, this computes both parts and shifts them to the right */
107:     PetscCall(VecZeroEntries(yydot));
108:     PetscCallAbort(comm, TSComputeIFunction(ts, t, yy, yydot, yyd, PETSC_FALSE));
109:     PetscCall(VecScale(yyd, -1.));
110:   }
111:   PetscCallAbort(comm, VecResetArray(yy));
112:   PetscCallAbort(comm, VecResetArray(yyd));
113:   PetscFunctionReturn(PETSC_SUCCESS);
114: }

116: /*
117:        TSStep_Sundials - Calls SUNDIALS to integrate the ODE.
118: */
119: static PetscErrorCode TSStep_Sundials(TS ts)
120: {
121:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
122:   PetscInt     flag;
123:   long int     nits, lits, nsteps;
124:   realtype     t, tout;
125:   PetscScalar *y_data;
126:   void        *mem;

128:   PetscFunctionBegin;
129:   mem  = cvode->mem;
130:   tout = ts->max_time;
131:   PetscCall(VecGetArray(ts->vec_sol, &y_data));
132:   N_VSetArrayPointer((realtype *)y_data, cvode->y);
133:   PetscCall(VecRestoreArray(ts->vec_sol, NULL));

135:   /* We would like to TSPreStage() and TSPostStage()
136:    * before each stage solve but CVode does not appear to support this. */
137:   if (cvode->monitorstep) flag = CVode(mem, tout, cvode->y, &t, CV_ONE_STEP);
138:   else flag = CVode(mem, tout, cvode->y, &t, CV_NORMAL);

140:   if (flag) { /* display error message */
141:     switch (flag) {
142:     case CV_ILL_INPUT:
143:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_ILL_INPUT");
144:       break;
145:     case CV_TOO_CLOSE:
146:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_TOO_CLOSE");
147:       break;
148:     case CV_TOO_MUCH_WORK: {
149:       PetscReal tcur;
150:       PetscCallExternal(CVodeGetNumSteps, mem, &nsteps);
151:       PetscCallExternal(CVodeGetCurrentTime, mem, &tcur);
152:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_TOO_MUCH_WORK. At t=%g, nsteps %ld exceeds maxstep %" PetscInt_FMT ". Increase '-ts_max_steps <>' or modify TSSetMaxSteps()", (double)tcur, nsteps, ts->max_steps);
153:     } break;
154:     case CV_TOO_MUCH_ACC:
155:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_TOO_MUCH_ACC");
156:       break;
157:     case CV_ERR_FAILURE:
158:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_ERR_FAILURE");
159:       break;
160:     case CV_CONV_FAILURE:
161:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_CONV_FAILURE");
162:       break;
163:     case CV_LINIT_FAIL:
164:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_LINIT_FAIL");
165:       break;
166:     case CV_LSETUP_FAIL:
167:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_LSETUP_FAIL");
168:       break;
169:     case CV_LSOLVE_FAIL:
170:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_LSOLVE_FAIL");
171:       break;
172:     case CV_RHSFUNC_FAIL:
173:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_RHSFUNC_FAIL");
174:       break;
175:     case CV_FIRST_RHSFUNC_ERR:
176:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_FIRST_RHSFUNC_ERR");
177:       break;
178:     case CV_REPTD_RHSFUNC_ERR:
179:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_REPTD_RHSFUNC_ERR");
180:       break;
181:     case CV_UNREC_RHSFUNC_ERR:
182:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_UNREC_RHSFUNC_ERR");
183:       break;
184:     case CV_RTFUNC_FAIL:
185:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, CV_RTFUNC_FAIL");
186:       break;
187:     default:
188:       SETERRQ(PETSC_COMM_SELF, PETSC_ERR_LIB, "CVode() fails, flag %d", flag);
189:     }
190:   }

192:   /* log inner nonlinear and linear iterations */
193:   PetscCallExternal(CVodeGetNumNonlinSolvIters, mem, &nits);
194:   PetscCallExternal(CVSpilsGetNumLinIters, mem, &lits);
195:   ts->snes_its += nits;
196:   ts->ksp_its = lits;

198:   /* copy the solution from cvode->y to cvode->update and sol */
199:   PetscCall(VecPlaceArray(cvode->w1, y_data));
200:   PetscCall(VecCopy(cvode->w1, cvode->update));
201:   PetscCall(VecResetArray(cvode->w1));
202:   PetscCall(VecCopy(cvode->update, ts->vec_sol));

204:   ts->time_step = t - ts->ptime;
205:   ts->ptime     = t;

207:   PetscCallExternal(CVodeGetNumSteps, mem, &nsteps);
208:   if (!cvode->monitorstep) ts->steps += nsteps - 1; /* TSStep() increments the step counter by one */
209:   PetscFunctionReturn(PETSC_SUCCESS);
210: }

212: static PetscErrorCode TSInterpolate_Sundials(TS ts, PetscReal t, Vec X)
213: {
214:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
215:   N_Vector     y;
216:   PetscScalar *x_data;
217:   PetscInt     glosize, locsize;

219:   PetscFunctionBegin;
220:   /* get the vector size */
221:   PetscCall(VecGetSize(X, &glosize));
222:   PetscCall(VecGetLocalSize(X, &locsize));
223:   PetscCall(VecGetArray(X, &x_data));

225:   /* Initialize N_Vec y with x_data */
226:   if (cvode->use_dense) {
227:     PetscMPIInt size;

229:     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
230:     PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "TSSUNDIALS only supports a dense solve in the serial case");
231:     y = N_VMake_Serial(locsize, (realtype *)x_data);
232:   } else {
233:     y = N_VMake_Parallel(cvode->comm_sundials, locsize, glosize, (realtype *)x_data);
234:   }

236:   PetscCheck(y, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Interpolated y is not allocated");

238:   PetscCallExternal(CVodeGetDky, cvode->mem, t, 0, y);
239:   PetscCall(VecRestoreArray(X, &x_data));
240:   PetscFunctionReturn(PETSC_SUCCESS);
241: }

243: static PetscErrorCode TSReset_Sundials(TS ts)
244: {
245:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

247:   PetscFunctionBegin;
248:   PetscCall(VecDestroy(&cvode->update));
249:   PetscCall(VecDestroy(&cvode->ydot));
250:   PetscCall(VecDestroy(&cvode->w1));
251:   PetscCall(VecDestroy(&cvode->w2));
252:   if (cvode->mem) CVodeFree(&cvode->mem);
253:   PetscFunctionReturn(PETSC_SUCCESS);
254: }

256: static PetscErrorCode TSDestroy_Sundials(TS ts)
257: {
258:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

260:   PetscFunctionBegin;
261:   PetscCall(TSReset_Sundials(ts));
262:   PetscCallMPI(MPI_Comm_free(&cvode->comm_sundials));
263:   PetscCall(PetscFree(ts->data));
264:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetType_C", NULL));
265:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMaxl_C", NULL));
266:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetLinearTolerance_C", NULL));
267:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetGramSchmidtType_C", NULL));
268:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetTolerance_C", NULL));
269:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMinTimeStep_C", NULL));
270:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMaxTimeStep_C", NULL));
271:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsGetPC_C", NULL));
272:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsGetIterations_C", NULL));
273:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsMonitorInternalSteps_C", NULL));
274:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetUseDense_C", NULL));
275:   PetscFunctionReturn(PETSC_SUCCESS);
276: }

278: static PetscErrorCode TSSetUp_Sundials(TS ts)
279: {
280:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
281:   PetscInt     glosize, locsize, i;
282:   PetscScalar *y_data, *parray;
283:   PC           pc;
284:   PCType       pctype;
285:   PetscBool    pcnone;

287:   PetscFunctionBegin;
288:   PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for exact final time option 'MATCHSTEP' when using SUNDIALS");

290:   /* get the vector size */
291:   PetscCall(VecGetSize(ts->vec_sol, &glosize));
292:   PetscCall(VecGetLocalSize(ts->vec_sol, &locsize));

294:   /* allocate the memory for N_Vec y */
295:   if (cvode->use_dense) {
296:     PetscMPIInt size;

298:     PetscCallMPI(MPI_Comm_size(PETSC_COMM_WORLD, &size));
299:     PetscCheck(size == 1, PETSC_COMM_WORLD, PETSC_ERR_WRONG_MPI_SIZE, "TSSUNDIALS only supports a dense solve in the serial case");
300:     cvode->y = N_VNew_Serial(locsize);
301:   } else {
302:     cvode->y = N_VNew_Parallel(cvode->comm_sundials, locsize, glosize);
303:   }
304:   PetscCheck(cvode->y, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "cvode->y is not allocated");

306:   /* initialize N_Vec y: copy ts->vec_sol to cvode->y */
307:   PetscCall(VecGetArray(ts->vec_sol, &parray));
308:   y_data = (PetscScalar *)N_VGetArrayPointer(cvode->y);
309:   for (i = 0; i < locsize; i++) y_data[i] = parray[i];
310:   PetscCall(VecRestoreArray(ts->vec_sol, NULL));

312:   PetscCall(VecDuplicate(ts->vec_sol, &cvode->update));
313:   PetscCall(VecDuplicate(ts->vec_sol, &cvode->ydot));

315:   /*
316:     Create work vectors for the TSPSolve_Sundials() routine. Note these are
317:     allocated with zero space arrays because the actual array space is provided
318:     by SUNDIALS and set using VecPlaceArray().
319:   */
320:   PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)ts), 1, locsize, PETSC_DECIDE, NULL, &cvode->w1));
321:   PetscCall(VecCreateMPIWithArray(PetscObjectComm((PetscObject)ts), 1, locsize, PETSC_DECIDE, NULL, &cvode->w2));

323:   /* Call CVodeCreate to create the solver memory and the use of a Newton iteration */
324:   cvode->mem = CVodeCreate(cvode->cvode_type, CV_NEWTON);
325:   PetscCheck(cvode->mem, PETSC_COMM_SELF, PETSC_ERR_MEM, "CVodeCreate() fails");

327:   /* Set the pointer to user-defined data */
328:   PetscCallExternal(CVodeSetUserData, cvode->mem, ts);

330:   /* SUNDIALS may choose to use a smaller initial step, but will never use a larger step. */
331:   PetscCallExternal(CVodeSetInitStep, cvode->mem, (realtype)ts->time_step);
332:   if (cvode->mindt > 0) {
333:     int flag = CVodeSetMinStep(cvode->mem, (realtype)cvode->mindt);
334:     if (flag) {
335:       PetscCheck(flag != CV_MEM_NULL, PetscObjectComm((PetscObject)ts), PETSC_ERR_LIB, "CVodeSetMinStep() failed, cvode_mem pointer is NULL");
336:       PetscCheck(flag != CV_ILL_INPUT, PetscObjectComm((PetscObject)ts), PETSC_ERR_LIB, "CVodeSetMinStep() failed, hmin is nonpositive or it exceeds the maximum allowable step size");
337:       SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_LIB, "CVodeSetMinStep() failed");
338:     }
339:   }
340:   if (cvode->maxdt > 0) PetscCallExternal(CVodeSetMaxStep, cvode->mem, (realtype)cvode->maxdt);

342:   /* Call CVodeInit to initialize the integrator memory and specify the
343:    * user's right-hand side function in u'=f(t,u), the initial time T0, and
344:    * the initial dependent variable vector cvode->y */
345:   PetscCallExternal(CVodeInit, cvode->mem, TSFunction_Sundials, ts->ptime, cvode->y);

347:   /* specifies scalar relative and absolute tolerances */
348:   PetscCallExternal(CVodeSStolerances, cvode->mem, cvode->reltol, cvode->abstol);

350:   /* Specify max order of BDF / ADAMS method */
351:   if (cvode->maxord != PETSC_DEFAULT) PetscCallExternal(CVodeSetMaxOrd, cvode->mem, cvode->maxord);

353:   /* Specify max num of steps to be taken by cvode in its attempt to reach the next output time */
354:   PetscCallExternal(CVodeSetMaxNumSteps, cvode->mem, ts->max_steps);

356:   if (cvode->use_dense) PetscCallExternal(CVDense, cvode->mem, locsize);
357:   else {
358:     /* call CVSpgmr to use GMRES as the linear solver.        */
359:     /* setup the ode integrator with the given preconditioner */
360:     PetscCall(TSSundialsGetPC(ts, &pc));
361:     PetscCall(PCGetType(pc, &pctype));
362:     PetscCall(PetscObjectTypeCompare((PetscObject)pc, PCNONE, &pcnone));
363:     if (pcnone) {
364:       PetscCallExternal(CVSpgmr, cvode->mem, PREC_NONE, 0);
365:     } else {
366:       PetscCallExternal(CVSpgmr, cvode->mem, PREC_LEFT, cvode->maxl);

368:       /* Set preconditioner and solve routines Precond and PSolve,
369:          and the pointer to the user-defined block data */
370:       PetscCallExternal(CVSpilsSetPreconditioner, cvode->mem, TSPrecond_Sundials_Private, TSPSolve_Sundials_Private);
371:     }
372:   }
373:   PetscFunctionReturn(PETSC_SUCCESS);
374: }

376: /* type of CVODE linear multistep method */
377: const char *const TSSundialsLmmTypes[] = {"", "ADAMS", "BDF", "TSSundialsLmmType", "SUNDIALS_", NULL};
378: /* type of G-S orthogonalization used by CVODE linear solver */
379: const char *const TSSundialsGramSchmidtTypes[] = {"", "MODIFIED", "CLASSICAL", "TSSundialsGramSchmidtType", "SUNDIALS_", NULL};

381: static PetscErrorCode TSSetFromOptions_Sundials(TS ts, PetscOptionItems PetscOptionsObject)
382: {
383:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
384:   int          indx;
385:   PetscBool    flag;
386:   PC           pc;

388:   PetscFunctionBegin;
389:   PetscOptionsHeadBegin(PetscOptionsObject, "SUNDIALS ODE solver options");
390:   PetscCall(PetscOptionsEList("-ts_sundials_type", "Scheme", "TSSundialsSetType", TSSundialsLmmTypes, 3, TSSundialsLmmTypes[cvode->cvode_type], &indx, &flag));
391:   if (flag) PetscCall(TSSundialsSetType(ts, (TSSundialsLmmType)indx));
392:   PetscCall(PetscOptionsEList("-ts_sundials_gramschmidt_type", "Type of orthogonalization", "TSSundialsSetGramSchmidtType", TSSundialsGramSchmidtTypes, 3, TSSundialsGramSchmidtTypes[cvode->gtype], &indx, &flag));
393:   if (flag) PetscCall(TSSundialsSetGramSchmidtType(ts, (TSSundialsGramSchmidtType)indx));
394:   PetscCall(PetscOptionsReal("-ts_sundials_atol", "Absolute tolerance for convergence", "TSSundialsSetTolerance", cvode->abstol, &cvode->abstol, NULL));
395:   PetscCall(PetscOptionsReal("-ts_sundials_rtol", "Relative tolerance for convergence", "TSSundialsSetTolerance", cvode->reltol, &cvode->reltol, NULL));
396:   PetscCall(PetscOptionsReal("-ts_sundials_mindt", "Minimum step size", "TSSundialsSetMinTimeStep", cvode->mindt, &cvode->mindt, NULL));
397:   PetscCall(PetscOptionsReal("-ts_sundials_maxdt", "Maximum step size", "TSSundialsSetMaxTimeStep", cvode->maxdt, &cvode->maxdt, NULL));
398:   PetscCall(PetscOptionsReal("-ts_sundials_linear_tolerance", "Convergence tolerance for linear solve", "TSSundialsSetLinearTolerance", cvode->linear_tol, &cvode->linear_tol, NULL));
399:   PetscCall(PetscOptionsInt("-ts_sundials_maxord", "Max Order for BDF/Adams method", "TSSundialsSetMaxOrd", cvode->maxord, &cvode->maxord, NULL));
400:   PetscCall(PetscOptionsInt("-ts_sundials_maxl", "Max dimension of the Krylov subspace", "TSSundialsSetMaxl", cvode->maxl, &cvode->maxl, NULL));
401:   PetscCall(PetscOptionsBool("-ts_sundials_monitor_steps", "Monitor SUNDIALS internal steps", "TSSundialsMonitorInternalSteps", cvode->monitorstep, &cvode->monitorstep, NULL));
402:   PetscCall(PetscOptionsBool("-ts_sundials_use_dense", "Use dense internal solver in SUNDIALS (serial only)", "TSSundialsSetUseDense", cvode->use_dense, &cvode->use_dense, NULL));
403:   PetscOptionsHeadEnd();
404:   PetscCall(TSSundialsGetPC(ts, &pc));
405:   PetscCall(PCSetFromOptions(pc));
406:   PetscFunctionReturn(PETSC_SUCCESS);
407: }

409: static PetscErrorCode TSView_Sundials(TS ts, PetscViewer viewer)
410: {
411:   TS_Sundials *cvode = (TS_Sundials *)ts->data;
412:   char        *type;
413:   char         atype[] = "Adams";
414:   char         btype[] = "BDF: backward differentiation formula";
415:   PetscBool    isascii, isstring;
416:   long int     nsteps, its, nfevals, nlinsetups, nfails, itmp;
417:   PetscInt     qlast, qcur;
418:   PetscReal    hinused, hlast, hcur, tcur, tolsfac;
419:   PC           pc;

421:   PetscFunctionBegin;
422:   if (cvode->cvode_type == SUNDIALS_ADAMS) type = atype;
423:   else type = btype;

425:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
426:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring));
427:   if (isascii) {
428:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS integrator does not use SNES!\n"));
429:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS integrator type %s\n", type));
430:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS maxord %" PetscInt_FMT "\n", cvode->maxord));
431:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS abs tol %g rel tol %g\n", (double)cvode->abstol, (double)cvode->reltol));
432:     if (cvode->use_dense) {
433:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS integrator using a dense linear solve\n"));
434:     } else {
435:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS linear solver tolerance factor %g\n", (double)cvode->linear_tol));
436:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS max dimension of Krylov subspace %" PetscInt_FMT "\n", cvode->maxl));
437:       if (cvode->gtype == SUNDIALS_MODIFIED_GS) {
438:         PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS using modified Gram-Schmidt for orthogonalization in GMRES\n"));
439:       } else {
440:         PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS using unmodified (classical) Gram-Schmidt for orthogonalization in GMRES\n"));
441:       }
442:     }
443:     if (cvode->mindt > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS minimum time step %g\n", (double)cvode->mindt));
444:     if (cvode->maxdt > 0) PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS maximum time step %g\n", (double)cvode->maxdt));

446:     /* Outputs from CVODE, CVSPILS */
447:     PetscCallExternal(CVodeGetTolScaleFactor, cvode->mem, &tolsfac);
448:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS suggested factor for tolerance scaling %g\n", tolsfac));
449:     PetscCallExternal(CVodeGetIntegratorStats, cvode->mem, &nsteps, &nfevals, &nlinsetups, &nfails, &qlast, &qcur, &hinused, &hlast, &hcur, &tcur);
450:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS cumulative number of internal steps %ld\n", nsteps));
451:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of calls to rhs function %ld\n", nfevals));
452:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of calls to linear solver setup function %ld\n", nlinsetups));
453:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of error test failures %ld\n", nfails));

455:     PetscCallExternal(CVodeGetNonlinSolvStats, cvode->mem, &its, &nfails);
456:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of nonlinear solver iterations %ld\n", its));
457:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of nonlinear convergence failure %ld\n", nfails));
458:     if (!cvode->use_dense) {
459:       PetscCallExternal(CVSpilsGetNumLinIters, cvode->mem, &its); /* its = no. of calls to TSPrecond_Sundials() */
460:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of linear iterations %ld\n", its));
461:       PetscCallExternal(CVSpilsGetNumConvFails, cvode->mem, &itmp);
462:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of linear convergence failures %ld\n", itmp));

464:       PetscCall(TSSundialsGetPC(ts, &pc));
465:       PetscCall(PCView(pc, viewer));
466:       PetscCallExternal(CVSpilsGetNumPrecEvals, cvode->mem, &itmp);
467:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of preconditioner evaluations %ld\n", itmp));
468:       PetscCallExternal(CVSpilsGetNumPrecSolves, cvode->mem, &itmp);
469:       PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of preconditioner solves %ld\n", itmp));
470:     }
471:     PetscCallExternal(CVSpilsGetNumJtimesEvals, cvode->mem, &itmp);
472:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of Jacobian-vector product evaluations %ld\n", itmp));
473:     PetscCallExternal(CVSpilsGetNumRhsEvals, cvode->mem, &itmp);
474:     PetscCall(PetscViewerASCIIPrintf(viewer, "SUNDIALS no. of rhs calls for finite diff. Jacobian-vector evals %ld\n", itmp));
475:   } else if (isstring) {
476:     PetscCall(PetscViewerStringSPrintf(viewer, "SUNDIALS type %s", type));
477:   }
478:   PetscFunctionReturn(PETSC_SUCCESS);
479: }

481: static PetscErrorCode TSSundialsSetType_Sundials(TS ts, TSSundialsLmmType type)
482: {
483:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

485:   PetscFunctionBegin;
486:   cvode->cvode_type = type;
487:   PetscFunctionReturn(PETSC_SUCCESS);
488: }

490: static PetscErrorCode TSSundialsSetMaxl_Sundials(TS ts, PetscInt maxl)
491: {
492:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

494:   PetscFunctionBegin;
495:   cvode->maxl = maxl;
496:   PetscFunctionReturn(PETSC_SUCCESS);
497: }

499: static PetscErrorCode TSSundialsSetLinearTolerance_Sundials(TS ts, PetscReal tol)
500: {
501:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

503:   PetscFunctionBegin;
504:   cvode->linear_tol = tol;
505:   PetscFunctionReturn(PETSC_SUCCESS);
506: }

508: static PetscErrorCode TSSundialsSetGramSchmidtType_Sundials(TS ts, TSSundialsGramSchmidtType type)
509: {
510:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

512:   PetscFunctionBegin;
513:   cvode->gtype = type;
514:   PetscFunctionReturn(PETSC_SUCCESS);
515: }

517: static PetscErrorCode TSSundialsSetTolerance_Sundials(TS ts, PetscReal aabs, PetscReal rel)
518: {
519:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

521:   PetscFunctionBegin;
522:   if (aabs != PETSC_DECIDE) cvode->abstol = aabs;
523:   if (rel != PETSC_DECIDE) cvode->reltol = rel;
524:   PetscFunctionReturn(PETSC_SUCCESS);
525: }

527: static PetscErrorCode TSSundialsSetMinTimeStep_Sundials(TS ts, PetscReal mindt)
528: {
529:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

531:   PetscFunctionBegin;
532:   cvode->mindt = mindt;
533:   PetscFunctionReturn(PETSC_SUCCESS);
534: }

536: static PetscErrorCode TSSundialsSetMaxTimeStep_Sundials(TS ts, PetscReal maxdt)
537: {
538:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

540:   PetscFunctionBegin;
541:   cvode->maxdt = maxdt;
542:   PetscFunctionReturn(PETSC_SUCCESS);
543: }

545: static PetscErrorCode TSSundialsSetUseDense_Sundials(TS ts, PetscBool use_dense)
546: {
547:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

549:   PetscFunctionBegin;
550:   cvode->use_dense = use_dense;
551:   PetscFunctionReturn(PETSC_SUCCESS);
552: }

554: static PetscErrorCode TSSundialsGetPC_Sundials(TS ts, PC *pc)
555: {
556:   SNES snes;
557:   KSP  ksp;

559:   PetscFunctionBegin;
560:   PetscCall(TSGetSNES(ts, &snes));
561:   PetscCall(SNESGetKSP(snes, &ksp));
562:   PetscCall(KSPGetPC(ksp, pc));
563:   PetscFunctionReturn(PETSC_SUCCESS);
564: }

566: static PetscErrorCode TSSundialsGetIterations_Sundials(TS ts, int *nonlin, int *lin)
567: {
568:   PetscFunctionBegin;
569:   if (nonlin) *nonlin = ts->snes_its;
570:   if (lin) *lin = ts->ksp_its;
571:   PetscFunctionReturn(PETSC_SUCCESS);
572: }

574: static PetscErrorCode TSSundialsMonitorInternalSteps_Sundials(TS ts, PetscBool s)
575: {
576:   TS_Sundials *cvode = (TS_Sundials *)ts->data;

578:   PetscFunctionBegin;
579:   cvode->monitorstep = s;
580:   PetscFunctionReturn(PETSC_SUCCESS);
581: }

583: /*@
584:   TSSundialsGetIterations - Gets the number of nonlinear and linear iterations used so far by `TSSUNDIALS`.

586:   Not Collective

588:   Input Parameter:
589: . ts - the time-step context

591:   Output Parameters:
592: + nonlin - number of nonlinear iterations
593: - lin    - number of linear iterations

595:   Level: advanced

597:   Note:
598:   These return the number since the creation of the `TS` object

600: .seealso: [](ch_ts), `TSSundialsSetType()`, `TSSundialsSetMaxl()`,
601:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`,
602:           `TSSundialsGetPC()`, `TSSetExactFinalTime()`
603: @*/
604: PetscErrorCode TSSundialsGetIterations(TS ts, int *nonlin, int *lin)
605: {
606:   PetscFunctionBegin;
607:   PetscUseMethod(ts, "TSSundialsGetIterations_C", (TS, int *, int *), (ts, nonlin, lin));
608:   PetscFunctionReturn(PETSC_SUCCESS);
609: }

611: /*@
612:   TSSundialsSetType - Sets the method that `TSSUNDIALS` will use for integration.

614:   Logically Collective

616:   Input Parameters:
617: + ts   - the time-step context
618: - type - one of  `SUNDIALS_ADAMS` or `SUNDIALS_BDF`

620:   Level: intermediate

622: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetMaxl()`,
623:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`,
624:           `TSSundialsSetTolerance()`, `TSSundialsGetPC()`, `TSSetExactFinalTime()`
625: @*/
626: PetscErrorCode TSSundialsSetType(TS ts, TSSundialsLmmType type)
627: {
628:   PetscFunctionBegin;
629:   PetscTryMethod(ts, "TSSundialsSetType_C", (TS, TSSundialsLmmType), (ts, type));
630:   PetscFunctionReturn(PETSC_SUCCESS);
631: }

633: /*@
634:   TSSundialsSetMaxord - Sets the maximum order for BDF/Adams method used by `TSSUNDIALS`.

636:   Logically Collective

638:   Input Parameters:
639: + ts     - the time-step context
640: - maxord - maximum order of BDF / Adams method

642:   Level: advanced

644: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`,
645:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`,
646:           `TSSundialsSetTolerance()`, `TSSundialsGetPC()`, `TSSetExactFinalTime()`
647: @*/
648: PetscErrorCode TSSundialsSetMaxord(TS ts, PetscInt maxord)
649: {
650:   PetscFunctionBegin;
652:   PetscTryMethod(ts, "TSSundialsSetMaxOrd_C", (TS, PetscInt), (ts, maxord));
653:   PetscFunctionReturn(PETSC_SUCCESS);
654: }

656: /*@
657:   TSSundialsSetMaxl - Sets the dimension of the Krylov space used by
658:   GMRES in the linear solver in `TSSUNDIALS`. `TSSUNDIALS` DOES NOT use restarted GMRES so
659:   this is the maximum number of GMRES steps that will be used.

661:   Logically Collective

663:   Input Parameters:
664: + ts   - the time-step context
665: - maxl - number of direction vectors (the dimension of Krylov subspace).

667:   Level: advanced

669: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`,
670:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`,
671:           `TSSundialsGetPC()`, `TSSetExactFinalTime()`
672: @*/
673: PetscErrorCode TSSundialsSetMaxl(TS ts, PetscInt maxl)
674: {
675:   PetscFunctionBegin;
677:   PetscTryMethod(ts, "TSSundialsSetMaxl_C", (TS, PetscInt), (ts, maxl));
678:   PetscFunctionReturn(PETSC_SUCCESS);
679: }

681: /*@
682:   TSSundialsSetLinearTolerance - Sets the tolerance used to solve the linear
683:   system by `TSSUNDIALS`.

685:   Logically Collective

687:   Input Parameters:
688: + ts  - the time-step context
689: - tol - the factor by which the tolerance on the nonlinear solver is
690:              multiplied to get the tolerance on the linear solver, .05 by default.

692:   Level: advanced

694: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`, `TSSundialsSetMaxl()`,
695:           `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`,
696:           `TSSundialsGetPC()`,
697:           `TSSetExactFinalTime()`
698: @*/
699: PetscErrorCode TSSundialsSetLinearTolerance(TS ts, PetscReal tol)
700: {
701:   PetscFunctionBegin;
703:   PetscTryMethod(ts, "TSSundialsSetLinearTolerance_C", (TS, PetscReal), (ts, tol));
704:   PetscFunctionReturn(PETSC_SUCCESS);
705: }

707: /*@
708:   TSSundialsSetGramSchmidtType - Sets type of orthogonalization used
709:   in GMRES method by `TSSUNDIALS` linear solver.

711:   Logically Collective

713:   Input Parameters:
714: + ts   - the time-step context
715: - type - either `SUNDIALS_MODIFIED_GS` or `SUNDIALS_CLASSICAL_GS`

717:   Level: advanced

719: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`, `TSSundialsSetMaxl()`,
720:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetTolerance()`,
721:           `TSSundialsGetPC()`,
722:           `TSSetExactFinalTime()`
723: @*/
724: PetscErrorCode TSSundialsSetGramSchmidtType(TS ts, TSSundialsGramSchmidtType type)
725: {
726:   PetscFunctionBegin;
727:   PetscTryMethod(ts, "TSSundialsSetGramSchmidtType_C", (TS, TSSundialsGramSchmidtType), (ts, type));
728:   PetscFunctionReturn(PETSC_SUCCESS);
729: }

731: /*@
732:   TSSundialsSetTolerance - Sets the absolute and relative tolerance used by
733:   `TSSUNDIALS` for error control.

735:   Logically Collective

737:   Input Parameters:
738: + ts   - the time-step context
739: . aabs - the absolute tolerance
740: - rel  - the relative tolerance

742:      See the CVODE/SUNDIALS users manual for exact details on these parameters. Essentially
743:     these regulate the size of the error for a SINGLE timestep.

745:   Level: intermediate

747: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`, `TSSundialsSetGMRESMaxl()`,
748:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`,
749:           `TSSundialsGetPC()`,
750:           `TSSetExactFinalTime()`
751: @*/
752: PetscErrorCode TSSundialsSetTolerance(TS ts, PetscReal aabs, PetscReal rel)
753: {
754:   PetscFunctionBegin;
755:   PetscTryMethod(ts, "TSSundialsSetTolerance_C", (TS, PetscReal, PetscReal), (ts, aabs, rel));
756:   PetscFunctionReturn(PETSC_SUCCESS);
757: }

759: /*@
760:   TSSundialsGetPC - Extract the PC context from a time-step context for `TSSUNDIALS`.

762:   Input Parameter:
763: . ts - the time-step context

765:   Output Parameter:
766: . pc - the preconditioner context

768:   Level: advanced

770: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`, `TSSundialsSetMaxl()`,
771:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`
772: @*/
773: PetscErrorCode TSSundialsGetPC(TS ts, PC *pc)
774: {
775:   PetscFunctionBegin;
776:   PetscUseMethod(ts, "TSSundialsGetPC_C", (TS, PC *), (ts, pc));
777:   PetscFunctionReturn(PETSC_SUCCESS);
778: }

780: /*@
781:   TSSundialsSetMinTimeStep - Smallest time step to be chosen by the adaptive controller.

783:   Input Parameters:
784: + ts    - the time-step context
785: - mindt - lowest time step if positive, negative to deactivate

787:   Note:
788:   `TSSUNDIALS` will error if it is not possible to keep the estimated truncation error below
789:   the tolerance set with `TSSundialsSetTolerance()` without going below this step size.

791:   Level: beginner

793: .seealso: [](ch_ts), `TSSundialsSetType()`, `TSSundialsSetTolerance()`
794: @*/
795: PetscErrorCode TSSundialsSetMinTimeStep(TS ts, PetscReal mindt)
796: {
797:   PetscFunctionBegin;
798:   PetscTryMethod(ts, "TSSundialsSetMinTimeStep_C", (TS, PetscReal), (ts, mindt));
799:   PetscFunctionReturn(PETSC_SUCCESS);
800: }

802: /*@
803:   TSSundialsSetMaxTimeStep - Largest time step to be chosen by the adaptive controller.

805:   Input Parameters:
806: + ts    - the time-step context
807: - maxdt - lowest time step if positive, negative to deactivate

809:   Level: beginner

811: .seealso: [](ch_ts), `TSSundialsSetType()`, `TSSundialsSetTolerance()`
812: @*/
813: PetscErrorCode TSSundialsSetMaxTimeStep(TS ts, PetscReal maxdt)
814: {
815:   PetscFunctionBegin;
816:   PetscTryMethod(ts, "TSSundialsSetMaxTimeStep_C", (TS, PetscReal), (ts, maxdt));
817:   PetscFunctionReturn(PETSC_SUCCESS);
818: }

820: /*@
821:   TSSundialsMonitorInternalSteps - Monitor `TSSUNDIALS` internal steps (Defaults to false).

823:   Input Parameters:
824: + ts - the time-step context
825: - ft - `PETSC_TRUE` if monitor, else `PETSC_FALSE`

827:   Level: beginner

829: .seealso: [](ch_ts), `TSSundialsGetIterations()`, `TSSundialsSetType()`, `TSSundialsSetMaxl()`,
830:           `TSSundialsSetLinearTolerance()`, `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`,
831:           `TSSundialsGetPC()`
832: @*/
833: PetscErrorCode TSSundialsMonitorInternalSteps(TS ts, PetscBool ft)
834: {
835:   PetscFunctionBegin;
836:   PetscTryMethod(ts, "TSSundialsMonitorInternalSteps_C", (TS, PetscBool), (ts, ft));
837:   PetscFunctionReturn(PETSC_SUCCESS);
838: }

840: /*@
841:   TSSundialsSetUseDense - Set a flag to use a dense linear solver in `TSSUNDIALS` (serial only)

843:   Logically Collective

845:   Input Parameters:
846: + ts        - the time-step context
847: - use_dense - `PETSC_TRUE` to use the dense solver

849:   Level: advanced

851: .seealso: [](ch_ts), `TSSUNDIALS`
852: @*/
853: PetscErrorCode TSSundialsSetUseDense(TS ts, PetscBool use_dense)
854: {
855:   PetscFunctionBegin;
857:   PetscTryMethod(ts, "TSSundialsSetUseDense_C", (TS, PetscBool), (ts, use_dense));
858:   PetscFunctionReturn(PETSC_SUCCESS);
859: }

861: /*MC
862:       TSSUNDIALS - ODE solver using a very old version of the LLNL CVODE/SUNDIALS package, version 2.5 (now called SUNDIALS). Requires ./configure --download-sundials

864:    Options Database Keys:
865: +    -ts_sundials_type (bdf|adams)                      - integrator type
866: .    -ts_sundials_gramschmidt_type (modified|classical) - type of orthogonalization inside GMRES
867: .    -ts_sundials_atol atol                             - Absolute tolerance for convergence
868: .    -ts_sundials_rtol rtol                             - Relative tolerance for convergence
869: .    -ts_sundials_linear_tolerance ltol                 - convergence tolerance for linear solver
870: .    -ts_sundials_maxl maxl                             - Max dimension of the Krylov subspace
871: .    -ts_sundials_monitor_steps                         - Monitor SUNDIALS internal steps
872: -    -ts_sundials_use_dense                             - Use a dense linear solver within CVODE (serial only)

874:     Level: beginner

876:     Note:
877:     This uses its own nonlinear solver and Krylov method so PETSc `SNES` and `KSP` options do not apply,
878:     only PETSc `PC` options.

880: .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSSetType()`, `TSSundialsSetType()`, `TSSundialsSetMaxl()`, `TSSundialsSetLinearTolerance()`, `TSType`,
881:           `TSSundialsSetGramSchmidtType()`, `TSSundialsSetTolerance()`, `TSSundialsGetPC()`, `TSSundialsGetIterations()`, `TSSetExactFinalTime()`
882: M*/
883: PETSC_EXTERN PetscErrorCode TSCreate_Sundials(TS ts)
884: {
885:   TS_Sundials *cvode;
886:   PC           pc;

888:   PetscFunctionBegin;
889:   ts->ops->reset          = TSReset_Sundials;
890:   ts->ops->destroy        = TSDestroy_Sundials;
891:   ts->ops->view           = TSView_Sundials;
892:   ts->ops->setup          = TSSetUp_Sundials;
893:   ts->ops->step           = TSStep_Sundials;
894:   ts->ops->interpolate    = TSInterpolate_Sundials;
895:   ts->ops->setfromoptions = TSSetFromOptions_Sundials;
896:   ts->default_adapt_type  = TSADAPTNONE;

898:   PetscCall(PetscNew(&cvode));

900:   ts->usessnes = PETSC_TRUE;

902:   ts->data           = (void *)cvode;
903:   cvode->cvode_type  = SUNDIALS_BDF;
904:   cvode->gtype       = SUNDIALS_CLASSICAL_GS;
905:   cvode->maxl        = 5;
906:   cvode->maxord      = PETSC_DEFAULT;
907:   cvode->linear_tol  = .05;
908:   cvode->monitorstep = PETSC_TRUE;
909:   cvode->use_dense   = PETSC_FALSE;

911:   PetscCallMPI(MPI_Comm_dup(PetscObjectComm((PetscObject)ts), &cvode->comm_sundials));

913:   cvode->mindt = -1.;
914:   cvode->maxdt = -1.;

916:   /* set tolerance for SUNDIALS */
917:   cvode->reltol = 1e-6;
918:   cvode->abstol = 1e-6;

920:   /* set PCNONE as default pctype */
921:   PetscCall(TSSundialsGetPC_Sundials(ts, &pc));
922:   PetscCall(PCSetType(pc, PCNONE));

924:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetType_C", TSSundialsSetType_Sundials));
925:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMaxl_C", TSSundialsSetMaxl_Sundials));
926:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetLinearTolerance_C", TSSundialsSetLinearTolerance_Sundials));
927:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetGramSchmidtType_C", TSSundialsSetGramSchmidtType_Sundials));
928:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetTolerance_C", TSSundialsSetTolerance_Sundials));
929:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMinTimeStep_C", TSSundialsSetMinTimeStep_Sundials));
930:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetMaxTimeStep_C", TSSundialsSetMaxTimeStep_Sundials));
931:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsGetPC_C", TSSundialsGetPC_Sundials));
932:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsGetIterations_C", TSSundialsGetIterations_Sundials));
933:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsMonitorInternalSteps_C", TSSundialsMonitorInternalSteps_Sundials));
934:   PetscCall(PetscObjectComposeFunction((PetscObject)ts, "TSSundialsSetUseDense_C", TSSundialsSetUseDense_Sundials));
935:   PetscFunctionReturn(PETSC_SUCCESS);
936: }