Actual source code: ex37.c
1: static char help[] = "Tests repeated TSSolve() calls, optionally each followed by TSAdjointSolve(), with a saved TSTrajectory.\n\n";
3: /*
4: The problem is
6: u_t = -u, u(0) = 1
8: with one component of u on each MPI process. It is solved -num_solves times from t = 0 and step 0
9: with the same TS, reusing the TSTrajectory created by TSSetSaveTrajectory(). Solve i runs to the final
10: time T = 1 + i * max_time_increment, so a positive -max_time_increment makes each solve take more
11: steps than the previous one. With -adjoint, each forward solve is followed by an adjoint solve for the
12: cost J = sum_j u_j(T), whose gradient with respect to each u_j(0) is approximately exp(-T). With
13: -adjoint_last, only the last one is. Every component of u(T) and of the gradient is the same, so the
14: output does not depend on the number of processes or on the checkpointing schedule the trajectory uses.
15: */
17: #include <petscts.h>
19: static PetscErrorCode RHSFunction(TS ts, PetscReal t, Vec U, Vec F, void *ctx)
20: {
21: PetscFunctionBeginUser;
22: PetscCall(VecCopy(U, F));
23: PetscCall(VecScale(F, -1.0));
24: PetscFunctionReturn(PETSC_SUCCESS);
25: }
27: static PetscErrorCode RHSJacobian(TS ts, PetscReal t, Vec U, Mat J, Mat P, void *ctx)
28: {
29: PetscInt rstart;
31: PetscFunctionBeginUser;
32: PetscCall(MatGetOwnershipRange(P, &rstart, NULL));
33: PetscCall(MatSetValue(P, rstart, rstart, -1.0, INSERT_VALUES));
34: PetscCall(MatAssemblyBegin(P, MAT_FINAL_ASSEMBLY));
35: PetscCall(MatAssemblyEnd(P, MAT_FINAL_ASSEMBLY));
36: PetscFunctionReturn(PETSC_SUCCESS);
37: }
39: int main(int argc, char **argv)
40: {
41: TS ts;
42: Vec U, lambda;
43: Mat J;
44: PetscInt num_solves = 2;
45: PetscReal max_time_increment = 0.0;
46: PetscBool adjoint = PETSC_FALSE, adjoint_last = PETSC_FALSE;
48: PetscFunctionBeginUser;
49: PetscCall(PetscInitialize(&argc, &argv, NULL, help));
50: PetscOptionsBegin(PETSC_COMM_WORLD, NULL, "Repeated TSSolve() with trajectory options", NULL);
51: PetscCall(PetscOptionsInt("-num_solves", "Number of forward solves", NULL, num_solves, &num_solves, NULL));
52: PetscCall(PetscOptionsReal("-max_time_increment", "Increase of the final time from one solve to the next", NULL, max_time_increment, &max_time_increment, NULL));
53: PetscCall(PetscOptionsBool("-adjoint", "Run TSAdjointSolve() after each TSSolve()", NULL, adjoint, &adjoint, NULL));
54: PetscCall(PetscOptionsBool("-adjoint_last", "Run TSAdjointSolve() only after the last TSSolve()", NULL, adjoint_last, &adjoint_last, NULL));
55: PetscOptionsEnd();
57: PetscCall(VecCreateMPI(PETSC_COMM_WORLD, 1, PETSC_DETERMINE, &U));
58: PetscCall(VecDuplicate(U, &lambda));
59: PetscCall(MatCreateAIJ(PETSC_COMM_WORLD, 1, 1, PETSC_DETERMINE, PETSC_DETERMINE, 1, NULL, 0, NULL, &J));
61: PetscCall(TSCreate(PETSC_COMM_WORLD, &ts));
62: PetscCall(TSSetType(ts, TSRK));
63: PetscCall(TSSetRHSFunction(ts, NULL, RHSFunction, NULL));
64: PetscCall(TSSetRHSJacobian(ts, J, J, RHSJacobian, NULL));
65: PetscCall(TSSetMaxTime(ts, 1.0));
66: PetscCall(TSSetTimeStep(ts, 0.1));
67: PetscCall(TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP));
68: PetscCall(TSSetSaveTrajectory(ts));
69: if (adjoint || adjoint_last) PetscCall(TSSetCostGradients(ts, 1, &lambda, NULL));
70: PetscCall(TSSetFromOptions(ts));
72: for (PetscInt i = 0; i < num_solves; i++) {
73: PetscReal u;
75: PetscCall(TSSetTime(ts, 0.0));
76: PetscCall(TSSetStepNumber(ts, 0));
77: PetscCall(TSSetTimeStep(ts, 0.1));
78: PetscCall(TSSetMaxTime(ts, 1.0 + i * max_time_increment));
79: PetscCall(VecSet(U, 1.0));
80: PetscCall(TSSolve(ts, U));
81: PetscCall(VecMax(U, NULL, &u));
82: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Solve %" PetscInt_FMT ": u(T) = %.6f\n", i, (double)u));
83: if (adjoint || (adjoint_last && i == num_solves - 1)) {
84: PetscReal l;
86: PetscCall(VecSet(lambda, 1.0));
87: PetscCall(TSAdjointSolve(ts));
88: PetscCall(VecMax(lambda, NULL, &l));
89: PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Solve %" PetscInt_FMT ": dJ/du(0) = %.6f\n", i, (double)l));
90: }
91: }
93: PetscCall(TSDestroy(&ts));
94: PetscCall(MatDestroy(&J));
95: PetscCall(VecDestroy(&lambda));
96: PetscCall(VecDestroy(&U));
97: PetscCall(PetscFinalize());
98: return 0;
99: }
101: /*TEST
103: testset:
104: requires: !single !complex
105: nsize: {{1 2}}
106: output_file: output/ex37.out
108: test:
109: suffix: memory
110: args: -ts_trajectory_type memory
112: test:
113: suffix: basic
114: args: -ts_trajectory_type basic
116: # without TSHistory, the second solve reaches the checkpoints left on the memory stack by the first
117: test:
118: suffix: memory_no_history
119: args: -ts_trajectory_type memory -ts_trajectory_use_history 0
121: testset:
122: requires: !single !complex
123: nsize: {{1 2}}
124: output_file: output/ex37_adjoint.out
125: args: -adjoint
127: test:
128: suffix: memory_adjoint
129: args: -ts_trajectory_type memory
131: test:
132: suffix: basic_adjoint
133: args: -ts_trajectory_type basic
135: # checkpointing schedules that store only some steps, which require a fixed time step
136: testset:
137: requires: !single !complex
138: nsize: {{1 2}}
139: output_file: output/ex37_schedule_adjoint.out
140: args: -ts_trajectory_type memory -ts_adapt_type none -adjoint
142: test:
143: suffix: two_level_norevolve_adjoint
144: args: -ts_trajectory_stride 5
146: test:
147: suffix: revolve_offline_adjoint
148: requires: revolve
149: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_memory_type revolve
151: test:
152: suffix: revolve_online_adjoint
153: requires: revolve
154: args: -ts_trajectory_max_cps_ram 4 -ts_trajectory_revolve_online
156: test:
157: suffix: revolve_multistage_adjoint
158: requires: revolve
159: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_max_cps_disk 8 -ts_trajectory_memory_type revolve
161: test:
162: suffix: two_level_revolve_adjoint
163: requires: revolve
164: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_stride 5
166: test:
167: suffix: two_level_two_revolve_adjoint
168: requires: revolve
169: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_max_cps_disk 8 -ts_trajectory_stride 5
171: test:
172: suffix: cams_adjoint
173: requires: cams
174: args: -ts_trajectory_max_units_ram 5 -ts_trajectory_memory_type cams
176: # the second forward solve must discard the checkpoints and schedule of the first
177: testset:
178: requires: !single !complex
179: nsize: {{1 2}}
180: output_file: output/ex37_schedule_adjoint_last.out
181: args: -ts_trajectory_type memory -ts_adapt_type none -adjoint_last
183: test:
184: suffix: two_level_norevolve_adjoint_last
185: args: -ts_trajectory_stride 5
187: test:
188: suffix: revolve_offline_adjoint_last
189: requires: revolve
190: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_memory_type revolve -ts_trajectory_solution_only
192: test:
193: suffix: revolve_online_adjoint_last
194: requires: revolve
195: args: -ts_trajectory_max_cps_ram 4 -ts_trajectory_revolve_online
197: test:
198: suffix: revolve_multistage_adjoint_last
199: requires: revolve
200: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_max_cps_disk 8 -ts_trajectory_memory_type revolve
202: test:
203: suffix: two_level_revolve_adjoint_last
204: requires: revolve
205: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_stride 5
207: test:
208: suffix: two_level_two_revolve_adjoint_last
209: requires: revolve
210: args: -ts_trajectory_max_cps_ram 3 -ts_trajectory_max_cps_disk 8 -ts_trajectory_stride 5
212: test:
213: suffix: cams_adjoint_last
214: requires: cams
215: args: -ts_trajectory_max_units_ram 5 -ts_trajectory_memory_type cams
217: # the second solve takes more steps than the first, so it needs a larger checkpoint stack
218: testset:
219: requires: !single !complex
220: nsize: {{1 2}}
221: args: -ts_trajectory_type memory -ts_adapt_type none -max_time_increment 1
223: test:
224: suffix: grow_adjoint
225: output_file: output/ex37_grow_adjoint.out
226: args: -adjoint
228: test:
229: suffix: grow_adjoint_last
230: output_file: output/ex37_grow_adjoint_last.out
231: args: -adjoint_last
233: TEST*/