Actual source code: ex82.c
1: static char help[] = "Order check for TSARKIMEX started at a nonzero time with an implicit-only problem.\n\
2: Solves u' = cos(t), u(t0) = sin(t0), as an IFunction only, from t0 to t0 + 1 with two time step sizes.\n";
4: #include <petscts.h>
6: static PetscErrorCode IFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, PetscCtx ctx)
7: {
8: const PetscScalar *udot;
9: PetscScalar *f;
11: PetscFunctionBeginUser;
12: PetscCall(VecGetArrayRead(Udot, &udot));
13: PetscCall(VecGetArrayWrite(F, &f));
14: f[0] = udot[0] - PetscCosReal(t);
15: PetscCall(VecRestoreArrayRead(Udot, &udot));
16: PetscCall(VecRestoreArrayWrite(F, &f));
17: PetscFunctionReturn(PETSC_SUCCESS);
18: }
20: static PetscErrorCode IJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscCtx ctx)
21: {
22: PetscFunctionBeginUser;
23: PetscCall(MatSetValue(B, 0, 0, shift, INSERT_VALUES));
24: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
25: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
26: PetscFunctionReturn(PETSC_SUCCESS);
27: }
29: static PetscErrorCode SolveError(PetscReal t0, PetscReal dt, PetscReal *err)
30: {
31: TS ts;
32: TSAdapt adapt;
33: Vec U;
34: Mat J;
35: PetscScalar u;
36: PetscInt row = 0;
38: PetscFunctionBeginUser;
39: PetscCall(VecCreateSeq(PETSC_COMM_SELF, 1, &U));
40: PetscCall(VecSet(U, PetscSinReal(t0)));
41: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, 1, 1, NULL, &J));
42: PetscCall(TSCreate(PETSC_COMM_SELF, &ts));
43: PetscCall(TSSetType(ts, TSARKIMEX));
44: PetscCall(TSSetIFunction(ts, NULL, IFunction, NULL));
45: PetscCall(TSSetIJacobian(ts, J, J, IJacobian, NULL));
46: PetscCall(TSSetTime(ts, t0));
47: PetscCall(TSSetTimeStep(ts, dt));
48: PetscCall(TSSetMaxTime(ts, t0 + 1.0));
49: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP));
50: PetscCall(TSSetTolerances(ts, 1e-12, NULL, 1e-12, NULL));
51: PetscCall(TSSetFromOptions(ts));
52: PetscCall(TSGetAdapt(ts, &adapt));
53: PetscCall(TSAdaptSetType(adapt, TSADAPTNONE));
54: PetscCall(TSSolve(ts, U));
55: PetscCall(VecGetValues(U, 1, &row, &u));
56: *err = PetscAbsScalar(u - PetscSinReal(t0 + 1.0));
57: PetscCall(MatDestroy(&J));
58: PetscCall(VecDestroy(&U));
59: PetscCall(TSDestroy(&ts));
60: PetscFunctionReturn(PETSC_SUCCESS);
61: }
63: int main(int argc, char **argv)
64: {
65: PetscReal t0 = 1.0, dt = 0.01, e1, e2, order, minorder = 2.5;
67: PetscFunctionBeginUser;
68: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
69: PetscCall(PetscOptionsGetReal(NULL, NULL, "-t0", &t0, NULL));
70: PetscCall(PetscOptionsGetReal(NULL, NULL, "-dt", &dt, NULL));
71: PetscCall(PetscOptionsGetReal(NULL, NULL, "-min_order", &minorder, NULL));
72: PetscCall(SolveError(t0, dt, &e1));
73: PetscCall(SolveError(t0, dt / 2, &e2));
74: order = PetscLog2Real(e1 / e2);
75: PetscCall(PetscInfo(NULL, "errors %g %g observed order %g\n", (double)e1, (double)e2, (double)order));
76: PetscCall(PetscPrintf(PETSC_COMM_SELF, "Observed order >= %g: %s\n", (double)minorder, order >= minorder ? "yes" : "no"));
77: PetscCall(PetscFinalize());
78: return 0;
79: }
81: /*TEST
83: testset:
84: requires: !single
85: output_file: output/ex82.out
86: test:
87: suffix: 3
88: args: -ts_arkimex_type 3
89: test:
90: suffix: 4
91: args: -ts_arkimex_type 4
92: test:
93: suffix: 3_t0
94: args: -ts_arkimex_type 3 -t0 0
95: test:
96: suffix: 4_fully_implicit
97: args: -ts_arkimex_type 4 -ts_arkimex_fully_implicit
99: TEST*/