Actual source code: radau5.c
1: /*
2: Provides a PETSc interface to RADAU5 solver.
4: */
5: #include <petsc/private/tsimpl.h>
7: typedef struct {
8: Vec work, workf;
9: } TS_Radau5;
11: static void FVPOL(int *N, double *X, double *Y, double *F, double *RPAR, void *IPAR)
12: {
13: TS ts = (TS)IPAR;
14: TS_Radau5 *cvode = (TS_Radau5 *)ts->data;
15: DM dm;
16: DMTS tsdm;
17: TSIFunctionFn *ifunction;
19: PetscCallAbort(PETSC_COMM_SELF, VecPlaceArray(cvode->work, Y));
20: PetscCallAbort(PETSC_COMM_SELF, VecPlaceArray(cvode->workf, F));
22: /* Now compute the right-hand side function, via IFunction unless only the more efficient RHSFunction is set */
23: PetscCallAbort(PETSC_COMM_SELF, TSGetDM(ts, &dm));
24: PetscCallAbort(PETSC_COMM_SELF, DMGetDMTS(dm, &tsdm));
25: PetscCallAbort(PETSC_COMM_SELF, DMTSGetIFunction(dm, &ifunction, NULL));
26: if (!ifunction) {
27: PetscCallAbort(PETSC_COMM_SELF, TSComputeRHSFunction(ts, *X, cvode->work, cvode->workf));
28: } else { /* If rhsfunction is also set, this computes both parts and scale them to the right-hand side */
29: Vec yydot;
31: PetscCallAbort(PETSC_COMM_SELF, VecDuplicate(cvode->work, &yydot));
32: PetscCallAbort(PETSC_COMM_SELF, TSComputeIFunction(ts, *X, cvode->work, yydot, cvode->workf, PETSC_FALSE));
33: PetscCallAbort(PETSC_COMM_SELF, VecScale(cvode->workf, -1.));
34: PetscCallAbort(PETSC_COMM_SELF, VecDestroy(&yydot));
35: }
37: PetscCallAbort(PETSC_COMM_SELF, VecResetArray(cvode->work));
38: PetscCallAbort(PETSC_COMM_SELF, VecResetArray(cvode->workf));
39: }
41: static void JVPOL(PetscInt *N, PetscScalar *X, PetscScalar *Y, PetscScalar *DFY, int *LDFY, PetscScalar *RPAR, void *IPAR)
42: {
43: TS ts = (TS)IPAR;
44: TS_Radau5 *cvode = (TS_Radau5 *)ts->data;
45: Vec yydot;
46: Mat mat;
47: PetscInt n;
49: PetscCallAbort(PETSC_COMM_SELF, VecPlaceArray(cvode->work, Y));
50: PetscCallAbort(PETSC_COMM_SELF, VecDuplicate(cvode->work, &yydot));
51: PetscCallAbort(PETSC_COMM_SELF, VecGetSize(yydot, &n));
52: PetscCallAbort(PETSC_COMM_SELF, MatCreateSeqDense(PETSC_COMM_SELF, n, n, DFY, &mat));
53: PetscCallAbort(PETSC_COMM_SELF, TSComputeIJacobian(ts, *X, cvode->work, yydot, 0, mat, mat, PETSC_FALSE));
54: PetscCallAbort(PETSC_COMM_SELF, MatScale(mat, -1.0));
55: PetscCallAbort(PETSC_COMM_SELF, MatDestroy(&mat));
56: PetscCallAbort(PETSC_COMM_SELF, VecDestroy(&yydot));
57: PetscCallAbort(PETSC_COMM_SELF, VecResetArray(cvode->work));
58: }
60: static void SOLOUT(int *NR, double *XOLD, double *X, double *Y, double *CONT, double *LRC, int *N, double *RPAR, void *IPAR, int *IRTRN)
61: {
62: TS ts = (TS)IPAR;
63: TS_Radau5 *cvode = (TS_Radau5 *)ts->data;
65: PetscCallAbort(PETSC_COMM_SELF, VecPlaceArray(cvode->work, Y));
66: ts->time_step = *X - *XOLD;
67: PetscCallAbort(PETSC_COMM_SELF, TSMonitor(ts, *NR - 1, *X, cvode->work));
68: PetscCallAbort(PETSC_COMM_SELF, VecResetArray(cvode->work));
69: }
71: extern void radau5_(int *, void *, double *, double *, double *, double *, double *, double *, int *, void *, int *, int *, int *, void *, int *, int *, int *, void *, int *, double *, int *, int *, int *, double *, void *, int *);
73: static PetscErrorCode TSSolve_Radau5(TS ts)
74: {
75: TS_Radau5 *cvode = (TS_Radau5 *)ts->data;
76: PetscScalar *Y, *WORK, X, XEND, RTOL, ATOL, H, RPAR;
77: PetscInt ND, *IWORK, LWORK, LIWORK, MUJAC, MLMAS, MUMAS, IDID, ITOL;
78: int IJAC, MLJAC, IMAS, IOUT;
80: PetscFunctionBegin;
81: PetscCall(VecGetArray(ts->vec_sol, &Y));
82: PetscCall(VecGetSize(ts->vec_sol, &ND));
83: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, ND, NULL, &cvode->work));
84: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, ND, NULL, &cvode->workf));
86: LWORK = 4 * ND * ND + 12 * ND + 20;
87: LIWORK = 3 * ND + 20;
89: PetscCall(PetscCalloc2(LWORK, &WORK, LIWORK, &IWORK));
91: /* C --- PARAMETER IN THE DIFFERENTIAL EQUATION */
92: RPAR = 1.0e-6;
93: /* C --- COMPUTE THE JACOBIAN ANALYTICALLY */
94: IJAC = 1;
95: /* C --- JACOBIAN IS A FULL MATRIX */
96: MLJAC = ND;
97: /* C --- DIFFERENTIAL EQUATION IS IN EXPLICIT FORM*/
98: IMAS = 0;
99: /* C --- OUTPUT ROUTINE IS USED DURING INTEGRATION*/
100: IOUT = 1;
101: /* C --- INITIAL VALUES*/
102: X = ts->ptime;
103: /* C --- ENDPOINT OF INTEGRATION */
104: XEND = ts->max_time;
105: /* C --- REQUIRED TOLERANCE */
106: RTOL = ts->rtol;
107: ATOL = ts->atol;
108: ITOL = 0;
109: /* C --- INITIAL STEP SIZE */
110: H = ts->time_step;
112: /* output MUJAC MLMAS IDID; currently all ignored */
114: radau5_(&ND, FVPOL, &X, Y, &XEND, &H, &RTOL, &ATOL, &ITOL, JVPOL, &IJAC, &MLJAC, &MUJAC, FVPOL, &IMAS, &MLMAS, &MUMAS, SOLOUT, &IOUT, WORK, &LWORK, IWORK, &LIWORK, &RPAR, (void *)ts, &IDID);
116: PetscCall(PetscFree2(WORK, IWORK));
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: static PetscErrorCode TSDestroy_Radau5(TS ts)
121: {
122: TS_Radau5 *cvode = (TS_Radau5 *)ts->data;
124: PetscFunctionBegin;
125: PetscCall(VecDestroy(&cvode->work));
126: PetscCall(VecDestroy(&cvode->workf));
127: PetscCall(PetscFree(ts->data));
128: PetscFunctionReturn(PETSC_SUCCESS);
129: }
131: /*MC
132: TSRADAU5 - ODE solver using the external RADAU5 package, requires ./configure --download-radau5
134: Level: beginner
136: Notes:
137: This uses its own nonlinear solver and dense matrix direct solver so PETSc `SNES` and `KSP` options do not apply.
139: Uses its own time-step adaptivity (but uses the TS rtol and atol, and initial timestep)
141: Uses its own memory for the dense matrix storage and factorization
143: Can only handle ODEs of the form $ \dot{u} = -F(t,u) + G(t,u) $
145: .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSSetType()`, `TSType`
146: M*/
147: PETSC_EXTERN PetscErrorCode TSCreate_Radau5(TS ts)
148: {
149: TS_Radau5 *cvode;
151: PetscFunctionBegin;
152: ts->ops->destroy = TSDestroy_Radau5;
153: ts->ops->solve = TSSolve_Radau5;
154: ts->default_adapt_type = TSADAPTNONE;
156: PetscCall(PetscNew(&cvode));
157: ts->data = (void *)cvode;
158: PetscFunctionReturn(PETSC_SUCCESS);
159: }