1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 21e25c274SJed Brown #include <petscdmda.h> 360e16b1bSMatthew G. Knepley #include <petscdmshell.h> 460e16b1bSMatthew G. Knepley #include <petscdmplex.h> // For TSSetFromOptions() 560e16b1bSMatthew G. Knepley #include <petscdmswarm.h> // For TSSetFromOptions() 62d5ee99bSBarry Smith #include <petscviewer.h> 72d5ee99bSBarry Smith #include <petscdraw.h> 8900f6b5bSMatthew G. Knepley #include <petscconvest.h> 9d763cef2SBarry Smith 10d5ba7fb7SMatthew Knepley /* Logging support */ 11d74926cbSBarry Smith PetscClassId TS_CLASSID, DMTS_CLASSID; 12a05bf03eSHong Zhang PetscLogEvent TS_Step, TS_PseudoComputeTimeStep, TS_FunctionEval, TS_JacobianEval; 13d405a339SMatthew Knepley 14c793f718SLisandro Dalcin const char *const TSExactFinalTimeOptions[] = {"UNSPECIFIED", "STEPOVER", "INTERPOLATE", "MATCHSTEP", "TSExactFinalTimeOption", "TS_EXACTFINALTIME_", NULL}; 1549354f04SShri Abhyankar 16d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSAdaptSetDefaultType(TSAdapt adapt, TSAdaptType default_type) 17d71ae5a4SJacob Faibussowitsch { 182ffb9264SLisandro Dalcin PetscFunctionBegin; 19b92453a8SLisandro Dalcin PetscValidHeaderSpecific(adapt, TSADAPT_CLASSID, 1); 204f572ea9SToby Isaac PetscAssertPointer(default_type, 2); 211e66621cSBarry Smith if (!((PetscObject)adapt)->type_name) PetscCall(TSAdaptSetType(adapt, default_type)); 223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 232ffb9264SLisandro Dalcin } 242ffb9264SLisandro Dalcin 25bdad233fSMatthew Knepley /*@ 26195e9b02SBarry Smith TSSetFromOptions - Sets various `TS` parameters from the options database 27bdad233fSMatthew Knepley 28c3339decSBarry Smith Collective 29bdad233fSMatthew Knepley 30bdad233fSMatthew Knepley Input Parameter: 31bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 32bdad233fSMatthew Knepley 33bdad233fSMatthew Knepley Options Database Keys: 34195e9b02SBarry Smith + -ts_type <type> - EULER, BEULER, SUNDIALS, PSEUDO, CN, RK, THETA, ALPHA, GLLE, SSP, GLEE, BSYMP, IRK, see `TSType` 35ef222394SBarry Smith . -ts_save_trajectory - checkpoint the solution at each time-step 36ef85077eSLisandro Dalcin . -ts_max_time <time> - maximum time to compute to 378343f784SJames Wright . -ts_time_span <t0,...tf> - sets the time span, solutions are computed and stored for each indicated time, init_time and max_time are set 388343f784SJames Wright . -ts_eval_times <t0,...tn> - time points where solutions are computed and stored for each indicated time 398e562f8dSJames Wright . -ts_max_steps <steps> - maximum time-step number to execute until (possibly with nonzero starting value) 408e562f8dSJames Wright . -ts_run_steps <steps> - maximum number of time steps for TSSolve to take on each call 41ef85077eSLisandro Dalcin . -ts_init_time <time> - initial time to start computation 42195e9b02SBarry Smith . -ts_final_time <time> - final time to compute to (deprecated: use `-ts_max_time`) 433e4cdcaaSBarry Smith . -ts_dt <dt> - initial time step 441628793fSSatish Balay . -ts_exact_final_time <stepover,interpolate,matchstep> - whether to stop at the exact given final time and how to compute the solution at that time 45a3cdaa26SBarry Smith . -ts_max_snes_failures <maxfailures> - Maximum number of nonlinear solve failures allowed 46a3cdaa26SBarry Smith . -ts_max_reject <maxrejects> - Maximum number of step rejections before step fails 47a3cdaa26SBarry Smith . -ts_error_if_step_fails <true,false> - Error if no step succeeds 48a3cdaa26SBarry Smith . -ts_rtol <rtol> - relative tolerance for local truncation error 4967b8a455SSatish Balay . -ts_atol <atol> - Absolute tolerance for local truncation error 50f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - test the Jacobian at each iteration against finite difference with RHS function 51b43aa488SJacob Faibussowitsch . -ts_rhs_jacobian_test_mult_transpose - test the Jacobian at each iteration against finite difference with RHS function 52195e9b02SBarry Smith . -ts_adjoint_solve <yes,no> - After solving the ODE/DAE solve the adjoint problem (requires `-ts_save_trajectory`) 53847ff0e1SMatthew G. Knepley . -ts_fd_color - Use finite differences with coloring to compute IJacobian 54bdad233fSMatthew Knepley . -ts_monitor - print information at each timestep 55aee7a9fbSMatthew G. Knepley . -ts_monitor_cancel - Cancel all monitors 56de06c3feSJed Brown . -ts_monitor_lg_solution - Monitor solution graphically 57de06c3feSJed Brown . -ts_monitor_lg_error - Monitor error graphically 587cf37e64SBarry Smith . -ts_monitor_error - Monitors norm of error 596934998bSLisandro Dalcin . -ts_monitor_lg_timestep - Monitor timestep size graphically 608b668821SLisandro Dalcin . -ts_monitor_lg_timestep_log - Monitor log timestep size graphically 61de06c3feSJed Brown . -ts_monitor_lg_snes_iterations - Monitor number nonlinear iterations for each timestep graphically 62de06c3feSJed Brown . -ts_monitor_lg_ksp_iterations - Monitor number nonlinear iterations for each timestep graphically 63de06c3feSJed Brown . -ts_monitor_sp_eig - Monitor eigenvalues of linearized operator graphically 64de06c3feSJed Brown . -ts_monitor_draw_solution - Monitor solution graphically 653e4cdcaaSBarry Smith . -ts_monitor_draw_solution_phase <xleft,yleft,xright,yright> - Monitor solution graphically with phase diagram, requires problem with exactly 2 degrees of freedom 663e4cdcaaSBarry Smith . -ts_monitor_draw_error - Monitor error graphically, requires use to have provided TSSetSolutionFunction() 67fde5950dSBarry Smith . -ts_monitor_solution [ascii binary draw][:filename][:viewerformat] - monitors the solution at each timestep 68c17ba870SStefano Zampini . -ts_monitor_solution_interval <interval> - output once every interval (default=1) time steps. Use -1 to only output at the end of the simulation 698e562f8dSJames Wright . -ts_monitor_solution_skip_initial - skip writing of initial condition 7063a3b9bcSJacob Faibussowitsch . -ts_monitor_solution_vtk <filename.vts,filename.vtu> - Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts (filename-%%03" PetscInt_FMT ".vtu) 71c17ba870SStefano Zampini . -ts_monitor_solution_vtk_interval <interval> - output once every interval (default=1) time steps. Use -1 to only output at the end of the simulation 729e336e28SPatrick Sanan - -ts_monitor_envelope - determine maximum and minimum value of each component of the solution over the solution time 7353ea634cSHong Zhang 74bcf0153eSBarry Smith Level: beginner 753d5a8a6aSBarry Smith 76bcf0153eSBarry Smith Notes: 77bcf0153eSBarry Smith See `SNESSetFromOptions()` and `KSPSetFromOptions()` for how to control the nonlinear and linear solves used by the time-stepper. 78bcf0153eSBarry Smith 79195e9b02SBarry Smith Certain `SNES` options get reset for each new nonlinear solver, for example `-snes_lag_jacobian its` and `-snes_lag_preconditioner its`, in order 800d56bcf9SIlya Fursov to retain them over the multiple nonlinear solves that `TS` uses you must also provide `-snes_lag_jacobian_persists true` and 81195e9b02SBarry Smith `-snes_lag_preconditioner_persists true` 823d5a8a6aSBarry Smith 83b43aa488SJacob Faibussowitsch Developer Notes: 849e336e28SPatrick Sanan We should unify all the -ts_monitor options in the way that -xxx_view has been unified 85bdad233fSMatthew Knepley 861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetType()` 87bdad233fSMatthew Knepley @*/ 88d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetFromOptions(TS ts) 89d71ae5a4SJacob Faibussowitsch { 90bc952696SBarry Smith PetscBool opt, flg, tflg; 91eabae89aSBarry Smith char monfilename[PETSC_MAX_PATH_LEN]; 92136cf249SJames Wright PetscReal time_step, eval_times[100]; 93136cf249SJames Wright PetscInt num_eval_times = PETSC_STATIC_ARRAY_LENGTH(eval_times); 9449354f04SShri Abhyankar TSExactFinalTimeOption eftopt; 95d1212d36SBarry Smith char dir[16]; 968434afd1SBarry Smith TSIFunctionFn *ifun; 976991f827SBarry Smith const char *defaultType; 986991f827SBarry Smith char typeName[256]; 99bdad233fSMatthew Knepley 100bdad233fSMatthew Knepley PetscFunctionBegin; 1010700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1026991f827SBarry Smith 1039566063dSJacob Faibussowitsch PetscCall(TSRegisterAll()); 1049566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 105cd11d68dSLisandro Dalcin 106d0609cedSBarry Smith PetscObjectOptionsBegin((PetscObject)ts); 1071ef27442SStefano Zampini if (((PetscObject)ts)->type_name) defaultType = ((PetscObject)ts)->type_name; 1081ef27442SStefano Zampini else defaultType = ifun ? TSBEULER : TSEULER; 1099566063dSJacob Faibussowitsch PetscCall(PetscOptionsFList("-ts_type", "TS method", "TSSetType", TSList, defaultType, typeName, 256, &opt)); 1101e66621cSBarry Smith if (opt) PetscCall(TSSetType(ts, typeName)); 1111e66621cSBarry Smith else PetscCall(TSSetType(ts, defaultType)); 112bdad233fSMatthew Knepley 113bdad233fSMatthew Knepley /* Handle generic TS options */ 1149566063dSJacob Faibussowitsch PetscCall(PetscOptionsDeprecated("-ts_final_time", "-ts_max_time", "3.10", NULL)); 1159566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_max_time", "Maximum time to run to", "TSSetMaxTime", ts->max_time, &ts->max_time, NULL)); 116136cf249SJames Wright PetscCall(PetscOptionsRealArray("-ts_time_span", "Time span", "TSSetTimeSpan", eval_times, &num_eval_times, &flg)); 117136cf249SJames Wright if (flg) PetscCall(TSSetTimeSpan(ts, num_eval_times, eval_times)); 118136cf249SJames Wright num_eval_times = PETSC_STATIC_ARRAY_LENGTH(eval_times); 119136cf249SJames Wright PetscCall(PetscOptionsRealArray("-ts_eval_times", "Evaluation time points", "TSSetEvaluationTimes", eval_times, &num_eval_times, &opt)); 1208343f784SJames Wright PetscCheck(flg != opt || (!flg && !opt), PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "May not provide -ts_time_span and -ts_eval_times simultaneously"); 121136cf249SJames Wright if (opt) PetscCall(TSSetEvaluationTimes(ts, num_eval_times, eval_times)); 1228e562f8dSJames Wright PetscCall(PetscOptionsInt("-ts_max_steps", "Maximum time step number to execute to (possibly with non-zero starting value)", "TSSetMaxSteps", ts->max_steps, &ts->max_steps, NULL)); 1238e562f8dSJames Wright PetscCall(PetscOptionsInt("-ts_run_steps", "Maximum number of time steps to take on each call to TSSolve()", "TSSetRunSteps", ts->run_steps, &ts->run_steps, NULL)); 1249566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_init_time", "Initial time", "TSSetTime", ts->ptime, &ts->ptime, NULL)); 1259566063dSJacob Faibussowitsch PetscCall(PetscOptionsReal("-ts_dt", "Initial time step", "TSSetTimeStep", ts->time_step, &time_step, &flg)); 1269566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetTimeStep(ts, time_step)); 1279566063dSJacob Faibussowitsch PetscCall(PetscOptionsEnum("-ts_exact_final_time", "Option for handling of final time step", "TSSetExactFinalTime", TSExactFinalTimeOptions, (PetscEnum)ts->exact_final_time, (PetscEnum *)&eftopt, &flg)); 1289566063dSJacob Faibussowitsch if (flg) PetscCall(TSSetExactFinalTime(ts, eftopt)); 12909cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_snes_failures", "Maximum number of nonlinear solve failures", "TSSetMaxSNESFailures", ts->max_snes_failures, &ts->max_snes_failures, &flg)); 13009cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxSNESFailures(ts, ts->max_snes_failures)); 13109cb0f53SBarry Smith PetscCall(PetscOptionsInt("-ts_max_reject", "Maximum number of step rejections before step fails", "TSSetMaxStepRejections", ts->max_reject, &ts->max_reject, &flg)); 13209cb0f53SBarry Smith if (flg) PetscCall(TSSetMaxStepRejections(ts, ts->max_reject)); 1339566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_error_if_step_fails", "Error if no step succeeds", "TSSetErrorIfStepFails", ts->errorifstepfailed, &ts->errorifstepfailed, NULL)); 13409cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_rtol", "Relative tolerance for local truncation error", "TSSetTolerances", ts->rtol, &ts->rtol, NULL, 0)); 13509cb0f53SBarry Smith PetscCall(PetscOptionsBoundedReal("-ts_atol", "Absolute tolerance for local truncation error", "TSSetTolerances", ts->atol, &ts->atol, NULL, 0)); 136bdad233fSMatthew Knepley 1379566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_rhs_jacobian_test_mult", "Test the RHS Jacobian for consistency with RHS at each solve ", "None", ts->testjacobian, &ts->testjacobian, NULL)); 1389566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_rhs_jacobian_test_mult_transpose", "Test the RHS Jacobian transpose for consistency with RHS at each solve ", "None", ts->testjacobiantranspose, &ts->testjacobiantranspose, NULL)); 1399566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_use_splitrhsfunction", "Use the split RHS function for multirate solvers ", "TSSetUseSplitRHSFunction", ts->use_splitrhsfunction, &ts->use_splitrhsfunction, NULL)); 14056f85f32SBarry Smith #if defined(PETSC_HAVE_SAWS) 14156f85f32SBarry Smith { 14256f85f32SBarry Smith PetscBool set; 14356f85f32SBarry Smith flg = PETSC_FALSE; 1449566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_saws_block", "Block for SAWs memory snooper at end of TSSolve", "PetscObjectSAWsBlock", ((PetscObject)ts)->amspublishblock, &flg, &set)); 1451baa6e33SBarry Smith if (set) PetscCall(PetscObjectSAWsSetBlock((PetscObject)ts, flg)); 14656f85f32SBarry Smith } 14756f85f32SBarry Smith #endif 14856f85f32SBarry Smith 149bdad233fSMatthew Knepley /* Monitor options */ 15041d17464SJames Wright PetscCall(PetscOptionsDeprecated("-ts_monitor_frequency", "-ts_dmswarm_monitor_moments_interval", "3.24", "Retired in favor of monitor-specific intervals (ts_dmswarm_monitor_moments was the only monitor to use ts_monitor_frequency)")); 1519566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor", "Monitor time and timestep size", "TSMonitorDefault", TSMonitorDefault, NULL)); 1529566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_extreme", "Monitor extreme values of the solution", "TSMonitorExtreme", TSMonitorExtreme, NULL)); 1538e562f8dSJames Wright PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_solution", "View the solution at each timestep", "TSMonitorSolution", TSMonitorSolution, TSMonitorSolutionSetup)); 1549566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_dmswarm_monitor_moments", "Monitor moments of particle distribution", "TSDMSwarmMonitorMoments", TSDMSwarmMonitorMoments, NULL)); 1559566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_python", "Use Python function", "TSMonitorSet", NULL, monfilename, sizeof(monfilename), &flg)); 1569566063dSJacob Faibussowitsch if (flg) PetscCall(PetscPythonMonitorSet((PetscObject)ts, monfilename)); 1575180491cSLisandro Dalcin 1589566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", &opt)); 159b3603a34SBarry Smith if (opt) { 1603923b477SBarry Smith PetscInt howoften = 1; 161e669de00SBarry Smith DM dm; 162e669de00SBarry Smith PetscBool net; 163b3603a34SBarry Smith 1649566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_solution", "Monitor solution graphically", "TSMonitorLGSolution", howoften, &howoften, NULL)); 1659566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 1669566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)dm, DMNETWORK, &net)); 167e669de00SBarry Smith if (net) { 168e669de00SBarry Smith TSMonitorLGCtxNetwork ctx; 1699566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxNetworkCreate(ts, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &ctx)); 17049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGCtxNetworkSolution, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxNetworkDestroy)); 1719566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_lg_solution_semilogy", "Plot the solution with a semi-log axis", "", ctx->semilogy, &ctx->semilogy, NULL)); 172e669de00SBarry Smith } else { 173e669de00SBarry Smith TSMonitorLGCtx ctx; 1749566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 17549abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGSolution, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 176bdad233fSMatthew Knepley } 177e669de00SBarry Smith } 1786ba87a44SLisandro Dalcin 1799566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", &opt)); 180ef20d060SBarry Smith if (opt) { 1810b039ecaSBarry Smith TSMonitorLGCtx ctx; 1823923b477SBarry Smith PetscInt howoften = 1; 183ef20d060SBarry Smith 1849566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_error", "Monitor error graphically", "TSMonitorLGError", howoften, &howoften, NULL)); 1859566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 18649abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGError, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 187ef20d060SBarry Smith } 1889566063dSJacob Faibussowitsch PetscCall(TSMonitorSetFromOptions(ts, "-ts_monitor_error", "View the error at each timestep", "TSMonitorError", TSMonitorError, NULL)); 1897cf37e64SBarry Smith 1909566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", &opt)); 1916934998bSLisandro Dalcin if (opt) { 1926934998bSLisandro Dalcin TSMonitorLGCtx ctx; 1936934998bSLisandro Dalcin PetscInt howoften = 1; 1946934998bSLisandro Dalcin 1959566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep", "Monitor timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 1969566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 19749abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 1986934998bSLisandro Dalcin } 1999566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", &opt)); 2008b668821SLisandro Dalcin if (opt) { 2018b668821SLisandro Dalcin TSMonitorLGCtx ctx; 2028b668821SLisandro Dalcin PetscInt howoften = 1; 2038b668821SLisandro Dalcin 2049566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_timestep_log", "Monitor log timestep size graphically", "TSMonitorLGTimeStep", howoften, &howoften, NULL)); 2059566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 20649abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGTimeStep, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 2078b668821SLisandro Dalcin ctx->semilogy = PETSC_TRUE; 2088b668821SLisandro Dalcin } 2098b668821SLisandro Dalcin 2109566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", &opt)); 211201da799SBarry Smith if (opt) { 212201da799SBarry Smith TSMonitorLGCtx ctx; 213201da799SBarry Smith PetscInt howoften = 1; 214201da799SBarry Smith 2159566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_snes_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGSNESIterations", howoften, &howoften, NULL)); 2169566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 21749abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGSNESIterations, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 218201da799SBarry Smith } 2199566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", &opt)); 220201da799SBarry Smith if (opt) { 221201da799SBarry Smith TSMonitorLGCtx ctx; 222201da799SBarry Smith PetscInt howoften = 1; 223201da799SBarry Smith 2249566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_lg_ksp_iterations", "Monitor number nonlinear iterations for each timestep graphically", "TSMonitorLGKSPIterations", howoften, &howoften, NULL)); 2259566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 400, 300, howoften, &ctx)); 22649abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorLGKSPIterations, ctx, (PetscCtxDestroyFn *)TSMonitorLGCtxDestroy)); 227201da799SBarry Smith } 2289566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", &opt)); 2298189c53fSBarry Smith if (opt) { 2308189c53fSBarry Smith TSMonitorSPEigCtx ctx; 2318189c53fSBarry Smith PetscInt howoften = 1; 2328189c53fSBarry Smith 2339566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_eig", "Monitor eigenvalues of linearized operator graphically", "TSMonitorSPEig", howoften, &howoften, NULL)); 2349566063dSJacob Faibussowitsch PetscCall(TSMonitorSPEigCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 23549abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorSPEig, ctx, (PetscCtxDestroyFn *)TSMonitorSPEigCtxDestroy)); 2368189c53fSBarry Smith } 23760e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_sp_swarm", "Display particle phase space from the DMSwarm", "TSMonitorSPSwarm", &opt)); 2381b575b74SJoseph Pusztay if (opt) { 2391b575b74SJoseph Pusztay TSMonitorSPCtx ctx; 240d7462660SMatthew Knepley PetscInt howoften = 1, retain = 0; 24160e16b1bSMatthew G. Knepley PetscBool phase = PETSC_TRUE, create = PETSC_TRUE, multispecies = PETSC_FALSE; 242d7462660SMatthew Knepley 2439371c9d4SSatish Balay for (PetscInt i = 0; i < ts->numbermonitors; ++i) 2449371c9d4SSatish Balay if (ts->monitor[i] == TSMonitorSPSwarmSolution) { 2459371c9d4SSatish Balay create = PETSC_FALSE; 2469371c9d4SSatish Balay break; 2479371c9d4SSatish Balay } 2486a5217c0SMatthew G. Knepley if (create) { 24960e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm", "Display particles phase space from the DMSwarm", "TSMonitorSPSwarm", howoften, &howoften, NULL)); 2509566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_sp_swarm_retain", "Retain n points plotted to show trajectory, -1 for all points", "TSMonitorSPSwarm", retain, &retain, NULL)); 2519566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_phase", "Plot in phase space rather than coordinate space", "TSMonitorSPSwarm", phase, &phase, NULL)); 25260e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_sp_swarm_multi_species", "Color particles by particle species", "TSMonitorSPSwarm", multispecies, &multispecies, NULL)); 25360e16b1bSMatthew G. Knepley PetscCall(TSMonitorSPCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, retain, phase, multispecies, &ctx)); 25449abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorSPSwarmSolution, ctx, (PetscCtxDestroyFn *)TSMonitorSPCtxDestroy)); 2551b575b74SJoseph Pusztay } 2566a5217c0SMatthew G. Knepley } 25760e16b1bSMatthew G. Knepley PetscCall(PetscOptionsName("-ts_monitor_hg_swarm", "Display particle histogram from the DMSwarm", "TSMonitorHGSwarm", &opt)); 25860e16b1bSMatthew G. Knepley if (opt) { 25960e16b1bSMatthew G. Knepley TSMonitorHGCtx ctx; 26060e16b1bSMatthew G. Knepley PetscInt howoften = 1, Ns = 1; 26160e16b1bSMatthew G. Knepley PetscBool velocity = PETSC_FALSE, create = PETSC_TRUE; 26260e16b1bSMatthew G. Knepley 26360e16b1bSMatthew G. Knepley for (PetscInt i = 0; i < ts->numbermonitors; ++i) 26460e16b1bSMatthew G. Knepley if (ts->monitor[i] == TSMonitorHGSwarmSolution) { 26560e16b1bSMatthew G. Knepley create = PETSC_FALSE; 26660e16b1bSMatthew G. Knepley break; 26760e16b1bSMatthew G. Knepley } 26860e16b1bSMatthew G. Knepley if (create) { 26960e16b1bSMatthew G. Knepley DM sw, dm; 27060e16b1bSMatthew G. Knepley PetscInt Nc, Nb; 27160e16b1bSMatthew G. Knepley 27260e16b1bSMatthew G. Knepley PetscCall(TSGetDM(ts, &sw)); 27360e16b1bSMatthew G. Knepley PetscCall(DMSwarmGetCellDM(sw, &dm)); 27460e16b1bSMatthew G. Knepley PetscCall(DMPlexGetHeightStratum(dm, 0, NULL, &Nc)); 27560e16b1bSMatthew G. Knepley Nb = PetscMin(20, PetscMax(10, Nc)); 27660e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm", "Display particles histogram from the DMSwarm", "TSMonitorHGSwarm", howoften, &howoften, NULL)); 27760e16b1bSMatthew G. Knepley PetscCall(PetscOptionsBool("-ts_monitor_hg_swarm_velocity", "Plot in velocity space rather than coordinate space", "TSMonitorHGSwarm", velocity, &velocity, NULL)); 27860e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_species", "Number of species to histogram", "TSMonitorHGSwarm", Ns, &Ns, NULL)); 27960e16b1bSMatthew G. Knepley PetscCall(PetscOptionsInt("-ts_monitor_hg_swarm_bins", "Number of histogram bins", "TSMonitorHGSwarm", Nb, &Nb, NULL)); 28060e16b1bSMatthew G. Knepley PetscCall(TSMonitorHGCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, Ns, Nb, velocity, &ctx)); 28149abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorHGSwarmSolution, ctx, (PetscCtxDestroyFn *)TSMonitorHGCtxDestroy)); 28260e16b1bSMatthew G. Knepley } 28360e16b1bSMatthew G. Knepley } 284ef20d060SBarry Smith opt = PETSC_FALSE; 2859566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", &opt)); 286a7cc72afSBarry Smith if (opt) { 28783a4ac43SBarry Smith TSMonitorDrawCtx ctx; 28883a4ac43SBarry Smith PetscInt howoften = 1; 289a80ad3e0SBarry Smith 2909566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution", "Monitor solution graphically", "TSMonitorDrawSolution", howoften, &howoften, NULL)); 2919566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Computed Solution", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 29249abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolution, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 293bdad233fSMatthew Knepley } 294fb1732b5SBarry Smith opt = PETSC_FALSE; 2959566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", &opt)); 2962d5ee99bSBarry Smith if (opt) { 2972d5ee99bSBarry Smith TSMonitorDrawCtx ctx; 2982d5ee99bSBarry Smith PetscReal bounds[4]; 2992d5ee99bSBarry Smith PetscInt n = 4; 3002d5ee99bSBarry Smith PetscDraw draw; 3016934998bSLisandro Dalcin PetscDrawAxis axis; 3022d5ee99bSBarry Smith 3039566063dSJacob Faibussowitsch PetscCall(PetscOptionsRealArray("-ts_monitor_draw_solution_phase", "Monitor solution graphically", "TSMonitorDrawSolutionPhase", bounds, &n, NULL)); 3043c633725SBarry Smith PetscCheck(n == 4, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Must provide bounding box of phase field"); 3059566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 300, 300, 1, &ctx)); 3069566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(ctx->viewer, 0, &draw)); 3079566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDrawAxis(ctx->viewer, 0, &axis)); 3089566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLimits(axis, bounds[0], bounds[2], bounds[1], bounds[3])); 3099566063dSJacob Faibussowitsch PetscCall(PetscDrawAxisSetLabels(axis, "Phase Diagram", "Variable 1", "Variable 2")); 31049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionPhase, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3112d5ee99bSBarry Smith } 3122d5ee99bSBarry Smith opt = PETSC_FALSE; 3139566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", &opt)); 3143a471f94SBarry Smith if (opt) { 31583a4ac43SBarry Smith TSMonitorDrawCtx ctx; 31683a4ac43SBarry Smith PetscInt howoften = 1; 3173a471f94SBarry Smith 3189566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_error", "Monitor error graphically", "TSMonitorDrawError", howoften, &howoften, NULL)); 3199566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Error", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 32049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawError, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3213a471f94SBarry Smith } 3220ed3bfb6SBarry Smith opt = PETSC_FALSE; 3239566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", &opt)); 3240ed3bfb6SBarry Smith if (opt) { 3250ed3bfb6SBarry Smith TSMonitorDrawCtx ctx; 3260ed3bfb6SBarry Smith PetscInt howoften = 1; 3270ed3bfb6SBarry Smith 3289566063dSJacob Faibussowitsch PetscCall(PetscOptionsInt("-ts_monitor_draw_solution_function", "Monitor solution provided by TSMonitorSetSolutionFunction() graphically", "TSMonitorDrawSolutionFunction", howoften, &howoften, NULL)); 3299566063dSJacob Faibussowitsch PetscCall(TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts), NULL, "Solution provided by user function", PETSC_DECIDE, PETSC_DECIDE, 300, 300, howoften, &ctx)); 33049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorDrawSolutionFunction, ctx, (PetscCtxDestroyFn *)TSMonitorDrawCtxDestroy)); 3310ed3bfb6SBarry Smith } 332fde5950dSBarry Smith 333ed81e22dSJed Brown opt = PETSC_FALSE; 33463a3b9bcSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_solution_vtk", "Save each time step to a binary file, use filename-%%03" PetscInt_FMT ".vts", "TSMonitorSolutionVTK", NULL, monfilename, sizeof(monfilename), &flg)); 335ed81e22dSJed Brown if (flg) { 3367f27e910SStefano Zampini TSMonitorVTKCtx ctx; 3377f27e910SStefano Zampini 3387f27e910SStefano Zampini PetscCall(TSMonitorSolutionVTKCtxCreate(monfilename, &ctx)); 339c17ba870SStefano Zampini PetscCall(PetscOptionsInt("-ts_monitor_solution_vtk_interval", "Save every interval time step (-1 for last step only)", NULL, ctx->interval, &ctx->interval, NULL)); 34049abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, (PetscErrorCode (*)(TS, PetscInt, PetscReal, Vec, void *))TSMonitorSolutionVTK, ctx, (PetscCtxDestroyFn *)TSMonitorSolutionVTKDestroy)); 341ed81e22dSJed Brown } 342bdad233fSMatthew Knepley 3439566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_dmda_ray", "Display a ray of the solution", "None", "y=0", dir, sizeof(dir), &flg)); 344d1212d36SBarry Smith if (flg) { 345d1212d36SBarry Smith TSMonitorDMDARayCtx *rayctx; 346d1212d36SBarry Smith int ray = 0; 3473ee9839eSMatthew G. Knepley DMDirection ddir; 348d1212d36SBarry Smith DM da; 349d1212d36SBarry Smith PetscMPIInt rank; 350d1212d36SBarry Smith 3513c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 3523ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3533ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 35498921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray %s", dir); 355d1212d36SBarry Smith sscanf(dir + 2, "%d", &ray); 356d1212d36SBarry Smith 357835f2295SStefano Zampini PetscCall(PetscInfo(ts, "Displaying DMDA ray %c = %d\n", dir[0], ray)); 3589566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3599566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3609566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3619566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)ts), &rank)); 3621e66621cSBarry Smith if (rank == 0) PetscCall(PetscViewerDrawOpen(PETSC_COMM_SELF, NULL, NULL, 0, 0, 600, 300, &rayctx->viewer)); 36351b4a12fSMatthew G. Knepley rayctx->lgctx = NULL; 3649566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorDMDARay, rayctx, TSMonitorDMDARayDestroy)); 365d1212d36SBarry Smith } 3669566063dSJacob Faibussowitsch PetscCall(PetscOptionsString("-ts_monitor_lg_dmda_ray", "Display a ray of the solution", "None", "x=0", dir, sizeof(dir), &flg)); 36751b4a12fSMatthew G. Knepley if (flg) { 36851b4a12fSMatthew G. Knepley TSMonitorDMDARayCtx *rayctx; 36951b4a12fSMatthew G. Knepley int ray = 0; 3703ee9839eSMatthew G. Knepley DMDirection ddir; 37151b4a12fSMatthew G. Knepley DM da; 37251b4a12fSMatthew G. Knepley PetscInt howoften = 1; 373d1212d36SBarry Smith 3743c633725SBarry Smith PetscCheck(dir[1] == '=', PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Malformed ray %s", dir); 3753ee9839eSMatthew G. Knepley if (dir[0] == 'x') ddir = DM_X; 3763ee9839eSMatthew G. Knepley else if (dir[0] == 'y') ddir = DM_Y; 37798921bdaSJacob Faibussowitsch else SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Unknown ray direction %s", dir); 37851b4a12fSMatthew G. Knepley sscanf(dir + 2, "%d", &ray); 3791c3436cfSJed Brown 380835f2295SStefano Zampini PetscCall(PetscInfo(ts, "Displaying LG DMDA ray %c = %d\n", dir[0], ray)); 3819566063dSJacob Faibussowitsch PetscCall(PetscNew(&rayctx)); 3829566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &da)); 3839566063dSJacob Faibussowitsch PetscCall(DMDAGetRay(da, ddir, ray, &rayctx->ray, &rayctx->scatter)); 3849566063dSJacob Faibussowitsch PetscCall(TSMonitorLGCtxCreate(PETSC_COMM_SELF, NULL, NULL, PETSC_DECIDE, PETSC_DECIDE, 600, 400, howoften, &rayctx->lgctx)); 3859566063dSJacob Faibussowitsch PetscCall(TSMonitorSet(ts, TSMonitorLGDMDARay, rayctx, TSMonitorDMDARayDestroy)); 38651b4a12fSMatthew G. Knepley } 387a7a1495cSBarry Smith 3889566063dSJacob Faibussowitsch PetscCall(PetscOptionsName("-ts_monitor_envelope", "Monitor maximum and minimum value of each component of the solution", "TSMonitorEnvelope", &opt)); 389b3d3934dSBarry Smith if (opt) { 390b3d3934dSBarry Smith TSMonitorEnvelopeCtx ctx; 391b3d3934dSBarry Smith 3929566063dSJacob Faibussowitsch PetscCall(TSMonitorEnvelopeCtxCreate(ts, &ctx)); 39349abdd8aSBarry Smith PetscCall(TSMonitorSet(ts, TSMonitorEnvelope, ctx, (PetscCtxDestroyFn *)TSMonitorEnvelopeCtxDestroy)); 394b3d3934dSBarry Smith } 395aee7a9fbSMatthew G. Knepley flg = PETSC_FALSE; 3969566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_monitor_cancel", "Remove all monitors", "TSMonitorCancel", flg, &flg, &opt)); 3979566063dSJacob Faibussowitsch if (opt && flg) PetscCall(TSMonitorCancel(ts)); 398b3d3934dSBarry Smith 399847ff0e1SMatthew G. Knepley flg = PETSC_FALSE; 400d7cfae9bSHong Zhang PetscCall(PetscOptionsBool("-ts_fd_color", "Use finite differences with coloring to compute IJacobian", "TSComputeIJacobianDefaultColor", flg, &flg, NULL)); 401847ff0e1SMatthew G. Knepley if (flg) { 402847ff0e1SMatthew G. Knepley DM dm; 403847ff0e1SMatthew G. Knepley 4049371c9d4SSatish Balay PetscCall(TSGetDM(ts, &dm)); 4059371c9d4SSatish Balay PetscCall(DMTSUnsetIJacobianContext_Internal(dm)); 4069566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, NULL, NULL, TSComputeIJacobianDefaultColor, NULL)); 4079566063dSJacob Faibussowitsch PetscCall(PetscInfo(ts, "Setting default finite difference coloring Jacobian matrix\n")); 408847ff0e1SMatthew G. Knepley } 409847ff0e1SMatthew G. Knepley 410d763cef2SBarry Smith /* Handle specific TS options */ 411dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setfromoptions, PetscOptionsObject); 412fbc52257SHong Zhang 413a7bdc993SLisandro Dalcin /* Handle TSAdapt options */ 4149566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 4159566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 416dbbe0bcdSBarry Smith PetscCall(TSAdaptSetFromOptions(ts->adapt, PetscOptionsObject)); 417a7bdc993SLisandro Dalcin 41868bece0bSHong Zhang /* TS trajectory must be set after TS, since it may use some TS options above */ 4194f122a70SLisandro Dalcin tflg = ts->trajectory ? PETSC_TRUE : PETSC_FALSE; 4209566063dSJacob Faibussowitsch PetscCall(PetscOptionsBool("-ts_save_trajectory", "Save the solution at each timestep", "TSSetSaveTrajectory", tflg, &tflg, NULL)); 4211baa6e33SBarry Smith if (tflg) PetscCall(TSSetSaveTrajectory(ts)); 422a05bf03eSHong Zhang 423dbbe0bcdSBarry Smith PetscCall(TSAdjointSetFromOptions(ts, PetscOptionsObject)); 424d763cef2SBarry Smith 425d763cef2SBarry Smith /* process any options handlers added with PetscObjectAddOptionsHandler() */ 426dbbe0bcdSBarry Smith PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)ts, PetscOptionsObject)); 427d0609cedSBarry Smith PetscOptionsEnd(); 428d763cef2SBarry Smith 4291baa6e33SBarry Smith if (ts->trajectory) PetscCall(TSTrajectorySetFromOptions(ts->trajectory, ts)); 43068bece0bSHong Zhang 4311ef27442SStefano Zampini /* why do we have to do this here and not during TSSetUp? */ 4329566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &ts->snes)); 4331ef27442SStefano Zampini if (ts->problem_type == TS_LINEAR) { 4349566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &flg, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 4359566063dSJacob Faibussowitsch if (!flg) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 4361ef27442SStefano Zampini } 4379566063dSJacob Faibussowitsch PetscCall(SNESSetFromOptions(ts->snes)); 4383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 439d763cef2SBarry Smith } 440d763cef2SBarry Smith 441d2daff3dSHong Zhang /*@ 442bcf0153eSBarry Smith TSGetTrajectory - Gets the trajectory from a `TS` if it exists 44378fbdcc8SBarry Smith 444c3339decSBarry Smith Collective 44578fbdcc8SBarry Smith 4462fe279fdSBarry Smith Input Parameter: 447bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 44878fbdcc8SBarry Smith 4492fe279fdSBarry Smith Output Parameter: 450bcf0153eSBarry Smith . tr - the `TSTrajectory` object, if it exists 45178fbdcc8SBarry Smith 45278fbdcc8SBarry Smith Level: advanced 45378fbdcc8SBarry Smith 454bcf0153eSBarry Smith Note: 455bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 45678fbdcc8SBarry Smith 457b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSAdjointSolve()`, `TSTrajectoryCreate()` 45878fbdcc8SBarry Smith @*/ 459d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTrajectory(TS ts, TSTrajectory *tr) 460d71ae5a4SJacob Faibussowitsch { 46178fbdcc8SBarry Smith PetscFunctionBegin; 46278fbdcc8SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 46378fbdcc8SBarry Smith *tr = ts->trajectory; 4643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46578fbdcc8SBarry Smith } 46678fbdcc8SBarry Smith 46778fbdcc8SBarry Smith /*@ 468bcf0153eSBarry Smith TSSetSaveTrajectory - Causes the `TS` to save its solutions as it iterates forward in time in a `TSTrajectory` object 469d2daff3dSHong Zhang 470c3339decSBarry Smith Collective 471d2daff3dSHong Zhang 472f899ff85SJose E. Roman Input Parameter: 473bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 474bc952696SBarry Smith 475bcf0153eSBarry Smith Options Database Keys: 47678fbdcc8SBarry Smith + -ts_save_trajectory - saves the trajectory to a file 47767b8a455SSatish Balay - -ts_trajectory_type type - set trajectory type 47878fbdcc8SBarry Smith 479d2daff3dSHong Zhang Level: intermediate 480d2daff3dSHong Zhang 481bcf0153eSBarry Smith Notes: 482bcf0153eSBarry Smith This routine should be called after all `TS` options have been set 483d2daff3dSHong Zhang 484bcf0153eSBarry Smith The `TSTRAJECTORYVISUALIZATION` files can be loaded into Python with $PETSC_DIR/lib/petsc/bin/PetscBinaryIOTrajectory.py and 485bcf0153eSBarry Smith MATLAB with $PETSC_DIR/share/petsc/matlab/PetscReadBinaryTrajectory.m 486bcf0153eSBarry Smith 4871cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()` 488d2daff3dSHong Zhang @*/ 489d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSaveTrajectory(TS ts) 490d71ae5a4SJacob Faibussowitsch { 491d2daff3dSHong Zhang PetscFunctionBegin; 492d2daff3dSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49363a3b9bcSJacob Faibussowitsch if (!ts->trajectory) PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 4943ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 495d2daff3dSHong Zhang } 496d2daff3dSHong Zhang 497a7a1495cSBarry Smith /*@ 498bcf0153eSBarry Smith TSResetTrajectory - Destroys and recreates the internal `TSTrajectory` object 4992d29f1f2SStefano Zampini 500c3339decSBarry Smith Collective 5012d29f1f2SStefano Zampini 5022fe279fdSBarry Smith Input Parameter: 503bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5042d29f1f2SStefano Zampini 5052d29f1f2SStefano Zampini Level: intermediate 5062d29f1f2SStefano Zampini 5071cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSGetTrajectory()`, `TSAdjointSolve()`, `TSRemoveTrajectory()` 5082d29f1f2SStefano Zampini @*/ 509d71ae5a4SJacob Faibussowitsch PetscErrorCode TSResetTrajectory(TS ts) 510d71ae5a4SJacob Faibussowitsch { 5112d29f1f2SStefano Zampini PetscFunctionBegin; 5122d29f1f2SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5132d29f1f2SStefano Zampini if (ts->trajectory) { 5149566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5159566063dSJacob Faibussowitsch PetscCall(TSTrajectoryCreate(PetscObjectComm((PetscObject)ts), &ts->trajectory)); 5162d29f1f2SStefano Zampini } 5173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5182d29f1f2SStefano Zampini } 5192d29f1f2SStefano Zampini 5202d29f1f2SStefano Zampini /*@ 521195e9b02SBarry Smith TSRemoveTrajectory - Destroys and removes the internal `TSTrajectory` object from a `TS` 52267a3cfb0SHong Zhang 523c3339decSBarry Smith Collective 52467a3cfb0SHong Zhang 5252fe279fdSBarry Smith Input Parameter: 526bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 52767a3cfb0SHong Zhang 52867a3cfb0SHong Zhang Level: intermediate 52967a3cfb0SHong Zhang 5301cc06b55SBarry Smith .seealso: [](ch_ts), `TSTrajectory`, `TSResetTrajectory()`, `TSAdjointSolve()` 53167a3cfb0SHong Zhang @*/ 532d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRemoveTrajectory(TS ts) 533d71ae5a4SJacob Faibussowitsch { 53467a3cfb0SHong Zhang PetscFunctionBegin; 53567a3cfb0SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5361e66621cSBarry Smith if (ts->trajectory) PetscCall(TSTrajectoryDestroy(&ts->trajectory)); 5373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53867a3cfb0SHong Zhang } 53967a3cfb0SHong Zhang 54067a3cfb0SHong Zhang /*@ 541a7a1495cSBarry Smith TSComputeRHSJacobian - Computes the Jacobian matrix that has been 542bcf0153eSBarry Smith set with `TSSetRHSJacobian()`. 543a7a1495cSBarry Smith 544c3339decSBarry Smith Collective 545a7a1495cSBarry Smith 546a7a1495cSBarry Smith Input Parameters: 547bcf0153eSBarry Smith + ts - the `TS` context 548a7a1495cSBarry Smith . t - current timestep 5490910c330SBarry Smith - U - input vector 550a7a1495cSBarry Smith 551a7a1495cSBarry Smith Output Parameters: 552a7a1495cSBarry Smith + A - Jacobian matrix 5537addb90fSBarry Smith - B - optional matrix used to compute the preconditioner, often the same as `A` 554a7a1495cSBarry Smith 555bcf0153eSBarry Smith Level: developer 556bcf0153eSBarry Smith 557bcf0153eSBarry Smith Note: 558a7a1495cSBarry Smith Most users should not need to explicitly call this routine, as it 5597addb90fSBarry Smith is used internally within the ODE integrators. 560a7a1495cSBarry Smith 5611cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `KSPSetOperators()` 562a7a1495cSBarry Smith @*/ 563d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobian(TS ts, PetscReal t, Vec U, Mat A, Mat B) 564d71ae5a4SJacob Faibussowitsch { 565270bf2e7SJed Brown PetscObjectState Ustate; 5666c1e1eecSBarry Smith PetscObjectId Uid; 56724989b8cSPeter Brune DM dm; 568942e3340SBarry Smith DMTS tsdm; 5698434afd1SBarry Smith TSRHSJacobianFn *rhsjacobianfunc; 57024989b8cSPeter Brune void *ctx; 5718434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 572a7a1495cSBarry Smith 573a7a1495cSBarry Smith PetscFunctionBegin; 5740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5750910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 5760910c330SBarry Smith PetscCheckSameComm(ts, 1, U, 3); 5779566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 5789566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &tsdm)); 5799566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 5809566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobianfunc, &ctx)); 5819566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 5829566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 583971015bcSStefano Zampini 5843ba16761SJacob Faibussowitsch if (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && (rhsfunction != TSComputeRHSFunctionLinear)) PetscFunctionReturn(PETSC_SUCCESS); 585d90be118SSean Farley 58663a3b9bcSJacob Faibussowitsch PetscCheck(ts->rhsjacobian.shift == 0.0 || !ts->rhsjacobian.reuse, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Should not call TSComputeRHSJacobian() on a shifted matrix (shift=%lf) when RHSJacobian is reusable.", (double)ts->rhsjacobian.shift); 58724989b8cSPeter Brune if (rhsjacobianfunc) { 588da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, A, B)); 589792fecdfSBarry Smith PetscCallBack("TS callback Jacobian", (*rhsjacobianfunc)(ts, t, U, A, B, ctx)); 590a6ab3590SBarry Smith ts->rhsjacs++; 591da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, A, B)); 592ef66eb69SBarry Smith } else { 5939566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 5949566063dSJacob Faibussowitsch if (B && A != B) PetscCall(MatZeroEntries(B)); 595ef66eb69SBarry Smith } 5960e4ef248SJed Brown ts->rhsjacobian.time = t; 597971015bcSStefano Zampini ts->rhsjacobian.shift = 0; 598971015bcSStefano Zampini ts->rhsjacobian.scale = 1.; 5999566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &ts->rhsjacobian.Xid)); 6009566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &ts->rhsjacobian.Xstate)); 6013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 602a7a1495cSBarry Smith } 603a7a1495cSBarry Smith 604316643e7SJed Brown /*@ 605bcf0153eSBarry Smith TSComputeRHSFunction - Evaluates the right-hand-side function for a `TS` 606d763cef2SBarry Smith 607c3339decSBarry Smith Collective 608316643e7SJed Brown 609316643e7SJed Brown Input Parameters: 610bcf0153eSBarry Smith + ts - the `TS` context 611316643e7SJed Brown . t - current time 6120910c330SBarry Smith - U - state vector 613316643e7SJed Brown 614316643e7SJed Brown Output Parameter: 615dd8e379bSPierre Jolivet . y - right-hand side 616316643e7SJed Brown 617bcf0153eSBarry Smith Level: developer 618bcf0153eSBarry Smith 619316643e7SJed Brown Note: 620316643e7SJed Brown Most users should not need to explicitly call this routine, as it 621316643e7SJed Brown is used internally within the nonlinear solvers. 622316643e7SJed Brown 6231cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 624316643e7SJed Brown @*/ 625d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunction(TS ts, PetscReal t, Vec U, Vec y) 626d71ae5a4SJacob Faibussowitsch { 6278434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 6288434afd1SBarry Smith TSIFunctionFn *ifunction; 62924989b8cSPeter Brune void *ctx; 63024989b8cSPeter Brune DM dm; 63124989b8cSPeter Brune 632d763cef2SBarry Smith PetscFunctionBegin; 6330700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6340910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6350700a824SBarry Smith PetscValidHeaderSpecific(y, VEC_CLASSID, 4); 6369566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6379566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, &ctx)); 6389566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, NULL)); 639d763cef2SBarry Smith 6403c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 641d763cef2SBarry Smith 64224989b8cSPeter Brune if (rhsfunction) { 643da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, y, 0)); 6449566063dSJacob Faibussowitsch PetscCall(VecLockReadPush(U)); 645792fecdfSBarry Smith PetscCallBack("TS callback right-hand-side", (*rhsfunction)(ts, t, U, y, ctx)); 6469566063dSJacob Faibussowitsch PetscCall(VecLockReadPop(U)); 647a6ab3590SBarry Smith ts->rhsfuncs++; 648da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, y, 0)); 6491e66621cSBarry Smith } else PetscCall(VecZeroEntries(y)); 6503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 651d763cef2SBarry Smith } 652d763cef2SBarry Smith 653ef20d060SBarry Smith /*@ 654ef20d060SBarry Smith TSComputeSolutionFunction - Evaluates the solution function. 655ef20d060SBarry Smith 656c3339decSBarry Smith Collective 657ef20d060SBarry Smith 658ef20d060SBarry Smith Input Parameters: 659bcf0153eSBarry Smith + ts - the `TS` context 660ef20d060SBarry Smith - t - current time 661ef20d060SBarry Smith 662ef20d060SBarry Smith Output Parameter: 6630910c330SBarry Smith . U - the solution 664ef20d060SBarry Smith 665ef20d060SBarry Smith Level: developer 666ef20d060SBarry Smith 6671cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 668ef20d060SBarry Smith @*/ 669d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeSolutionFunction(TS ts, PetscReal t, Vec U) 670d71ae5a4SJacob Faibussowitsch { 6718434afd1SBarry Smith TSSolutionFn *solutionfunction; 672ef20d060SBarry Smith void *ctx; 673ef20d060SBarry Smith DM dm; 674ef20d060SBarry Smith 675ef20d060SBarry Smith PetscFunctionBegin; 676ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 6770910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 6789566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 6799566063dSJacob Faibussowitsch PetscCall(DMTSGetSolutionFunction(dm, &solutionfunction, &ctx)); 680792fecdfSBarry Smith if (solutionfunction) PetscCallBack("TS callback solution", (*solutionfunction)(ts, t, U, ctx)); 6813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 682ef20d060SBarry Smith } 6839b7cd975SBarry Smith /*@ 6849b7cd975SBarry Smith TSComputeForcingFunction - Evaluates the forcing function. 6859b7cd975SBarry Smith 686c3339decSBarry Smith Collective 6879b7cd975SBarry Smith 6889b7cd975SBarry Smith Input Parameters: 689bcf0153eSBarry Smith + ts - the `TS` context 6909b7cd975SBarry Smith - t - current time 6919b7cd975SBarry Smith 6929b7cd975SBarry Smith Output Parameter: 6939b7cd975SBarry Smith . U - the function value 6949b7cd975SBarry Smith 6959b7cd975SBarry Smith Level: developer 6969b7cd975SBarry Smith 6971cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSSetRHSFunction()`, `TSComputeIFunction()` 6989b7cd975SBarry Smith @*/ 699d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeForcingFunction(TS ts, PetscReal t, Vec U) 700d71ae5a4SJacob Faibussowitsch { 7019b7cd975SBarry Smith void *ctx; 7029b7cd975SBarry Smith DM dm; 7038434afd1SBarry Smith TSForcingFn *forcing; 7049b7cd975SBarry Smith 7059b7cd975SBarry Smith PetscFunctionBegin; 7069b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7079b7cd975SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 7089566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 7099566063dSJacob Faibussowitsch PetscCall(DMTSGetForcingFunction(dm, &forcing, &ctx)); 7109b7cd975SBarry Smith 711792fecdfSBarry Smith if (forcing) PetscCallBack("TS callback forcing function", (*forcing)(ts, t, U, ctx)); 7123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 7139b7cd975SBarry Smith } 714ef20d060SBarry Smith 715d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetRHSMats_Private(TS ts, Mat *Arhs, Mat *Brhs) 716d71ae5a4SJacob Faibussowitsch { 717214bc6a2SJed Brown Mat A, B; 7188434afd1SBarry Smith TSIJacobianFn *ijacobian; 719214bc6a2SJed Brown 720214bc6a2SJed Brown PetscFunctionBegin; 721c0cd0301SJed Brown if (Arhs) *Arhs = NULL; 722c0cd0301SJed Brown if (Brhs) *Brhs = NULL; 7239566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, &ijacobian, NULL)); 724214bc6a2SJed Brown if (Arhs) { 725214bc6a2SJed Brown if (!ts->Arhs) { 72641a1d4d2SBarry Smith if (ijacobian) { 7279566063dSJacob Faibussowitsch PetscCall(MatDuplicate(A, MAT_DO_NOT_COPY_VALUES, &ts->Arhs)); 7289566063dSJacob Faibussowitsch PetscCall(TSSetMatStructure(ts, SAME_NONZERO_PATTERN)); 72941a1d4d2SBarry Smith } else { 73041a1d4d2SBarry Smith ts->Arhs = A; 7319566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 73241a1d4d2SBarry Smith } 7333565c898SBarry Smith } else { 7343565c898SBarry Smith PetscBool flg; 7359566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 7363565c898SBarry Smith /* Handle case where user provided only RHSJacobian and used -snes_mf_operator */ 7373565c898SBarry Smith if (flg && !ijacobian && ts->Arhs == ts->Brhs) { 7389566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)ts->Arhs)); 7393565c898SBarry Smith ts->Arhs = A; 7409566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)A)); 7413565c898SBarry Smith } 742214bc6a2SJed Brown } 743214bc6a2SJed Brown *Arhs = ts->Arhs; 744214bc6a2SJed Brown } 745214bc6a2SJed Brown if (Brhs) { 746214bc6a2SJed Brown if (!ts->Brhs) { 747bdb70873SJed Brown if (A != B) { 74841a1d4d2SBarry Smith if (ijacobian) { 7499566063dSJacob Faibussowitsch PetscCall(MatDuplicate(B, MAT_DO_NOT_COPY_VALUES, &ts->Brhs)); 750bdb70873SJed Brown } else { 75141a1d4d2SBarry Smith ts->Brhs = B; 7529566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)B)); 75341a1d4d2SBarry Smith } 75441a1d4d2SBarry Smith } else { 7559566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Arhs)); 75651699248SLisandro Dalcin ts->Brhs = ts->Arhs; 757bdb70873SJed Brown } 758214bc6a2SJed Brown } 759214bc6a2SJed Brown *Brhs = ts->Brhs; 760214bc6a2SJed Brown } 7613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 762214bc6a2SJed Brown } 763214bc6a2SJed Brown 764316643e7SJed Brown /*@ 765195e9b02SBarry Smith TSComputeIFunction - Evaluates the DAE residual written in the implicit form F(t,U,Udot)=0 766316643e7SJed Brown 767c3339decSBarry Smith Collective 768316643e7SJed Brown 769316643e7SJed Brown Input Parameters: 770bcf0153eSBarry Smith + ts - the `TS` context 771316643e7SJed Brown . t - current time 7720910c330SBarry Smith . U - state vector 7730910c330SBarry Smith . Udot - time derivative of state vector 7743dddbd81SStefano Zampini - imex - flag indicates if the method is `TSARKIMEX` so that the RHSFunction should be kept separate 775316643e7SJed Brown 776316643e7SJed Brown Output Parameter: 777dd8e379bSPierre Jolivet . Y - right-hand side 778316643e7SJed Brown 779bcf0153eSBarry Smith Level: developer 780bcf0153eSBarry Smith 781316643e7SJed Brown Note: 782316643e7SJed Brown Most users should not need to explicitly call this routine, as it 783316643e7SJed Brown is used internally within the nonlinear solvers. 784316643e7SJed Brown 78515229ffcSPierre Jolivet If the user did not write their equations in implicit form, this 786316643e7SJed Brown function recasts them in implicit form. 787316643e7SJed Brown 7881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSComputeRHSFunction()` 789316643e7SJed Brown @*/ 790d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunction(TS ts, PetscReal t, Vec U, Vec Udot, Vec Y, PetscBool imex) 791d71ae5a4SJacob Faibussowitsch { 7928434afd1SBarry Smith TSIFunctionFn *ifunction; 7938434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 79424989b8cSPeter Brune void *ctx; 79524989b8cSPeter Brune DM dm; 796316643e7SJed Brown 797316643e7SJed Brown PetscFunctionBegin; 7980700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 7990910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 8000910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 8010700a824SBarry Smith PetscValidHeaderSpecific(Y, VEC_CLASSID, 5); 802316643e7SJed Brown 8039566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 8049566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, &ifunction, &ctx)); 8059566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 80624989b8cSPeter Brune 8073c633725SBarry Smith PetscCheck(rhsfunction || ifunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSFunction() and / or TSSetIFunction()"); 808d90be118SSean Farley 809da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, Udot, Y)); 81024989b8cSPeter Brune if (ifunction) { 811792fecdfSBarry Smith PetscCallBack("TS callback implicit function", (*ifunction)(ts, t, U, Udot, Y, ctx)); 812a6ab3590SBarry Smith ts->ifuncs++; 813214bc6a2SJed Brown } 814214bc6a2SJed Brown if (imex) { 8151e66621cSBarry Smith if (!ifunction) PetscCall(VecCopy(Udot, Y)); 81624989b8cSPeter Brune } else if (rhsfunction) { 81724989b8cSPeter Brune if (ifunction) { 818214bc6a2SJed Brown Vec Frhs; 8198af0501fSStefano Zampini 8208af0501fSStefano Zampini PetscCall(DMGetGlobalVector(dm, &Frhs)); 8219566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 8229566063dSJacob Faibussowitsch PetscCall(VecAXPY(Y, -1, Frhs)); 8238af0501fSStefano Zampini PetscCall(DMRestoreGlobalVector(dm, &Frhs)); 8242dd45cf8SJed Brown } else { 8259566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Y)); 8269566063dSJacob Faibussowitsch PetscCall(VecAYPX(Y, -1, Udot)); 827316643e7SJed Brown } 8284a6899ffSJed Brown } 829da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, Udot, Y)); 8303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 831316643e7SJed Brown } 832316643e7SJed Brown 833cfa8a9a2SHong Zhang /* 834195e9b02SBarry Smith TSRecoverRHSJacobian - Recover the Jacobian matrix so that one can call `TSComputeRHSJacobian()` on it. 835cfa8a9a2SHong Zhang 836cfa8a9a2SHong Zhang Note: 837195e9b02SBarry Smith This routine is needed when one switches from `TSComputeIJacobian()` to `TSComputeRHSJacobian()` because the Jacobian matrix may be shifted or scaled in `TSComputeIJacobian()`. 838cfa8a9a2SHong Zhang 839cfa8a9a2SHong Zhang */ 840d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSRecoverRHSJacobian(TS ts, Mat A, Mat B) 841d71ae5a4SJacob Faibussowitsch { 842cfa8a9a2SHong Zhang PetscFunctionBegin; 843cfa8a9a2SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8443c633725SBarry Smith PetscCheck(A == ts->Arhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Amat"); 8453c633725SBarry Smith PetscCheck(B == ts->Brhs, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Invalid Bmat"); 846cfa8a9a2SHong Zhang 8471baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(A, -ts->rhsjacobian.shift)); 84848a46eb9SPierre Jolivet if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(A, -1)); 849cfa8a9a2SHong Zhang if (B && B == ts->Brhs && A != B) { 8501baa6e33SBarry Smith if (ts->rhsjacobian.shift) PetscCall(MatShift(B, -ts->rhsjacobian.shift)); 8511e66621cSBarry Smith if (ts->rhsjacobian.scale == -1.) PetscCall(MatScale(B, -1)); 852cfa8a9a2SHong Zhang } 853cfa8a9a2SHong Zhang ts->rhsjacobian.shift = 0; 854cfa8a9a2SHong Zhang ts->rhsjacobian.scale = 1.; 8553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 856cfa8a9a2SHong Zhang } 857cfa8a9a2SHong Zhang 858316643e7SJed Brown /*@ 859316643e7SJed Brown TSComputeIJacobian - Evaluates the Jacobian of the DAE 860316643e7SJed Brown 861c3339decSBarry Smith Collective 862316643e7SJed Brown 863316643e7SJed Brown Input Parameters: 864bcf0153eSBarry Smith + ts - the `TS` context 865316643e7SJed Brown . t - current timestep 8660910c330SBarry Smith . U - state vector 8670910c330SBarry Smith . Udot - time derivative of state vector 868214bc6a2SJed Brown . shift - shift to apply, see note below 8693dddbd81SStefano Zampini - imex - flag indicates if the method is `TSARKIMEX` so that the RHSJacobian should be kept separate 870316643e7SJed Brown 871316643e7SJed Brown Output Parameters: 872316643e7SJed Brown + A - Jacobian matrix 873195e9b02SBarry Smith - B - matrix from which the preconditioner is constructed; often the same as `A` 874316643e7SJed Brown 875bcf0153eSBarry Smith Level: developer 876bcf0153eSBarry Smith 877316643e7SJed Brown Notes: 8780910c330SBarry Smith If F(t,U,Udot)=0 is the DAE, the required Jacobian is 879195e9b02SBarry Smith .vb 8800910c330SBarry Smith dF/dU + shift*dF/dUdot 881195e9b02SBarry Smith .ve 882316643e7SJed Brown Most users should not need to explicitly call this routine, as it 883316643e7SJed Brown is used internally within the nonlinear solvers. 884316643e7SJed Brown 8851cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()` 88663495f91SJed Brown @*/ 887d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobian(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, PetscBool imex) 888d71ae5a4SJacob Faibussowitsch { 8898434afd1SBarry Smith TSIJacobianFn *ijacobian; 8908434afd1SBarry Smith TSRHSJacobianFn *rhsjacobian; 89124989b8cSPeter Brune DM dm; 89224989b8cSPeter Brune void *ctx; 893316643e7SJed Brown 894316643e7SJed Brown PetscFunctionBegin; 8950700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 8960910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 8970910c330SBarry Smith PetscValidHeaderSpecific(Udot, VEC_CLASSID, 4); 89894ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 6); 89994ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 7); 90024989b8cSPeter Brune 9019566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 9029566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, &ctx)); 9039566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 90424989b8cSPeter Brune 9053c633725SBarry Smith PetscCheck(rhsjacobian || ijacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_USER, "Must call TSSetRHSJacobian() and / or TSSetIJacobian()"); 906316643e7SJed Brown 907da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, A, B)); 90824989b8cSPeter Brune if (ijacobian) { 909792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", (*ijacobian)(ts, t, U, Udot, shift, A, B, ctx)); 910a6ab3590SBarry Smith ts->ijacs++; 9114a6899ffSJed Brown } 912214bc6a2SJed Brown if (imex) { 913b5abc632SBarry Smith if (!ijacobian) { /* system was written as Udot = G(t,U) */ 9144c26be97Sstefano_zampini PetscBool assembled; 915971015bcSStefano Zampini if (rhsjacobian) { 916971015bcSStefano Zampini Mat Arhs = NULL; 9179566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, NULL)); 918971015bcSStefano Zampini if (A == Arhs) { 9193c633725SBarry Smith PetscCheck(rhsjacobian != TSComputeRHSJacobianConstant, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Unsupported operation! cannot use TSComputeRHSJacobianConstant"); /* there is no way to reconstruct shift*M-J since J cannot be reevaluated */ 920971015bcSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 921971015bcSStefano Zampini } 922971015bcSStefano Zampini } 9239566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9249566063dSJacob Faibussowitsch PetscCall(MatAssembled(A, &assembled)); 9254c26be97Sstefano_zampini if (!assembled) { 9269566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY)); 9279566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY)); 9284c26be97Sstefano_zampini } 9299566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 93094ab13aaSBarry Smith if (A != B) { 9319566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9329566063dSJacob Faibussowitsch PetscCall(MatAssembled(B, &assembled)); 9334c26be97Sstefano_zampini if (!assembled) { 9349566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY)); 9359566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY)); 9364c26be97Sstefano_zampini } 9379566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 938214bc6a2SJed Brown } 939214bc6a2SJed Brown } 940214bc6a2SJed Brown } else { 941e1244c69SJed Brown Mat Arhs = NULL, Brhs = NULL; 9421e66621cSBarry Smith 9431e66621cSBarry Smith /* RHSJacobian needs to be converted to part of IJacobian if exists */ 9441e66621cSBarry Smith if (rhsjacobian) PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 945e8b1e424SHong Zhang if (Arhs == A) { /* No IJacobian matrix, so we only have the RHS matrix */ 946e8b1e424SHong Zhang PetscObjectState Ustate; 947e8b1e424SHong Zhang PetscObjectId Uid; 9488434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 949e8b1e424SHong Zhang 9509566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 9519566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &Ustate)); 9529566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &Uid)); 9539371c9d4SSatish Balay if ((rhsjacobian == TSComputeRHSJacobianConstant || (ts->rhsjacobian.time == t && (ts->problem_type == TS_LINEAR || (ts->rhsjacobian.Xid == Uid && ts->rhsjacobian.Xstate == Ustate)) && rhsfunction != TSComputeRHSFunctionLinear)) && 9549371c9d4SSatish Balay ts->rhsjacobian.scale == -1.) { /* No need to recompute RHSJacobian */ 9559566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift - ts->rhsjacobian.shift)); /* revert the old shift and add the new shift with a single call to MatShift */ 9561e66621cSBarry Smith if (A != B) PetscCall(MatShift(B, shift - ts->rhsjacobian.shift)); 957e8b1e424SHong Zhang } else { 9583565c898SBarry Smith PetscBool flg; 959e8b1e424SHong Zhang 960e8b1e424SHong Zhang if (ts->rhsjacobian.reuse) { /* Undo the damage */ 961e8b1e424SHong Zhang /* MatScale has a short path for this case. 962e8b1e424SHong Zhang However, this code path is taken the first time TSComputeRHSJacobian is called 963e8b1e424SHong Zhang and the matrices have not been assembled yet */ 9649566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, A, B)); 965e8b1e424SHong Zhang } 9669566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, A, B)); 9679566063dSJacob Faibussowitsch PetscCall(SNESGetUseMatrixFree(ts->snes, NULL, &flg)); 9683565c898SBarry Smith /* since -snes_mf_operator uses the full SNES function it does not need to be shifted or scaled here */ 9693565c898SBarry Smith if (!flg) { 9709566063dSJacob Faibussowitsch PetscCall(MatScale(A, -1)); 9719566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 9723565c898SBarry Smith } 97394ab13aaSBarry Smith if (A != B) { 9749566063dSJacob Faibussowitsch PetscCall(MatScale(B, -1)); 9759566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 976316643e7SJed Brown } 977e8b1e424SHong Zhang } 978e8b1e424SHong Zhang ts->rhsjacobian.scale = -1; 979e8b1e424SHong Zhang ts->rhsjacobian.shift = shift; 980d60b7d5cSBarry Smith } else if (Arhs) { /* Both IJacobian and RHSJacobian */ 981e1244c69SJed Brown if (!ijacobian) { /* No IJacobian provided, but we have a separate RHS matrix */ 9829566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(A)); 9839566063dSJacob Faibussowitsch PetscCall(MatShift(A, shift)); 98494ab13aaSBarry Smith if (A != B) { 9859566063dSJacob Faibussowitsch PetscCall(MatZeroEntries(B)); 9869566063dSJacob Faibussowitsch PetscCall(MatShift(B, shift)); 987214bc6a2SJed Brown } 988316643e7SJed Brown } 9899566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 9909566063dSJacob Faibussowitsch PetscCall(MatAXPY(A, -1, Arhs, ts->axpy_pattern)); 9911e66621cSBarry Smith if (A != B) PetscCall(MatAXPY(B, -1, Brhs, ts->axpy_pattern)); 992316643e7SJed Brown } 993316643e7SJed Brown } 994da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, A, B)); 9953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 996316643e7SJed Brown } 997316643e7SJed Brown 998d763cef2SBarry Smith /*@C 999d763cef2SBarry Smith TSSetRHSFunction - Sets the routine for evaluating the function, 1000b5abc632SBarry Smith where U_t = G(t,u). 1001d763cef2SBarry Smith 1002c3339decSBarry Smith Logically Collective 1003d763cef2SBarry Smith 1004d763cef2SBarry Smith Input Parameters: 1005bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1006dd8e379bSPierre Jolivet . r - vector to put the computed right-hand side (or `NULL` to have it created) 1007d763cef2SBarry Smith . f - routine for evaluating the right-hand-side function 1008195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the function evaluation routine (may be `NULL`) 1009d763cef2SBarry Smith 1010d763cef2SBarry Smith Level: beginner 1011d763cef2SBarry Smith 1012bcf0153eSBarry Smith Note: 1013bcf0153eSBarry Smith You must call this function or `TSSetIFunction()` to define your ODE. You cannot use this function when solving a DAE. 10142bbac0d3SBarry Smith 10158434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSRHSFunctionFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSSetIFunction()` 1016d763cef2SBarry Smith @*/ 10178434afd1SBarry Smith PetscErrorCode TSSetRHSFunction(TS ts, Vec r, TSRHSFunctionFn *f, void *ctx) 1018d71ae5a4SJacob Faibussowitsch { 1019089b2837SJed Brown SNES snes; 10200298fd71SBarry Smith Vec ralloc = NULL; 102124989b8cSPeter Brune DM dm; 1022d763cef2SBarry Smith 1023089b2837SJed Brown PetscFunctionBegin; 10240700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1025ca94891dSJed Brown if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 102624989b8cSPeter Brune 10279566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10289566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSFunction(dm, f, ctx)); 10299566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 1030e856ceecSJed Brown if (!r && !ts->dm && ts->vec_sol) { 10319566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 1032e856ceecSJed Brown r = ralloc; 1033e856ceecSJed Brown } 10349566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 10359566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 10363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1037d763cef2SBarry Smith } 1038d763cef2SBarry Smith 1039ef20d060SBarry Smith /*@C 1040abd5a294SJed Brown TSSetSolutionFunction - Provide a function that computes the solution of the ODE or DAE 1041ef20d060SBarry Smith 1042c3339decSBarry Smith Logically Collective 1043ef20d060SBarry Smith 1044ef20d060SBarry Smith Input Parameters: 1045bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1046ef20d060SBarry Smith . f - routine for evaluating the solution 1047ef20d060SBarry Smith - ctx - [optional] user-defined context for private data for the 1048195e9b02SBarry Smith function evaluation routine (may be `NULL`) 1049ef20d060SBarry Smith 1050bcf0153eSBarry Smith Options Database Keys: 1051bcf0153eSBarry Smith + -ts_monitor_lg_error - create a graphical monitor of error history, requires user to have provided `TSSetSolutionFunction()` 1052bcf0153eSBarry Smith - -ts_monitor_draw_error - Monitor error graphically, requires user to have provided `TSSetSolutionFunction()` 1053bcf0153eSBarry Smith 1054bcf0153eSBarry Smith Level: intermediate 10550ed3bfb6SBarry Smith 1056abd5a294SJed Brown Notes: 1057abd5a294SJed Brown This routine is used for testing accuracy of time integration schemes when you already know the solution. 1058abd5a294SJed Brown If analytic solutions are not known for your system, consider using the Method of Manufactured Solutions to 1059abd5a294SJed Brown create closed-form solutions with non-physical forcing terms. 1060abd5a294SJed Brown 1061bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 1062abd5a294SJed Brown 10638434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolutionFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, `TSComputeSolutionFunction()`, `TSSetForcingFunction()`, `TSSetSolution()`, `TSGetSolution()`, `TSMonitorLGError()`, `TSMonitorDrawError()` 1064ef20d060SBarry Smith @*/ 10658434afd1SBarry Smith PetscErrorCode TSSetSolutionFunction(TS ts, TSSolutionFn *f, void *ctx) 1066d71ae5a4SJacob Faibussowitsch { 1067ef20d060SBarry Smith DM dm; 1068ef20d060SBarry Smith 1069ef20d060SBarry Smith PetscFunctionBegin; 1070ef20d060SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 10719566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 10729566063dSJacob Faibussowitsch PetscCall(DMTSSetSolutionFunction(dm, f, ctx)); 10733ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1074ef20d060SBarry Smith } 1075ef20d060SBarry Smith 10769b7cd975SBarry Smith /*@C 10779b7cd975SBarry Smith TSSetForcingFunction - Provide a function that computes a forcing term for a ODE or PDE 10789b7cd975SBarry Smith 1079c3339decSBarry Smith Logically Collective 10809b7cd975SBarry Smith 10819b7cd975SBarry Smith Input Parameters: 1082bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1083e162b725SBarry Smith . func - routine for evaluating the forcing function 108414d0ab18SJacob Faibussowitsch - ctx - [optional] user-defined context for private data for the function evaluation routine 108514d0ab18SJacob Faibussowitsch (may be `NULL`) 10869b7cd975SBarry Smith 1087bcf0153eSBarry Smith Level: intermediate 1088bcf0153eSBarry Smith 10899b7cd975SBarry Smith Notes: 10909b7cd975SBarry Smith This routine is useful for testing accuracy of time integration schemes when using the Method of Manufactured Solutions to 1091e162b725SBarry Smith create closed-form solutions with a non-physical forcing term. It allows you to use the Method of Manufactored Solution without directly editing the 1092e162b725SBarry Smith definition of the problem you are solving and hence possibly introducing bugs. 1093e162b725SBarry Smith 10948847d985SBarry Smith This replaces the ODE F(u,u_t,t) = 0 the `TS` is solving with F(u,u_t,t) - func(t) = 0 1095e162b725SBarry Smith 1096e162b725SBarry Smith This forcing function does not depend on the solution to the equations, it can only depend on spatial location, time, and possibly parameters, the 1097e162b725SBarry Smith parameters can be passed in the ctx variable. 10989b7cd975SBarry Smith 1099bcf0153eSBarry Smith For low-dimensional problems solved in serial, such as small discrete systems, `TSMonitorLGError()` can be used to monitor the error history. 11009b7cd975SBarry Smith 11018434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSForcingFn`, `TSSetRHSJacobian()`, `TSSetIJacobian()`, 110214d0ab18SJacob Faibussowitsch `TSComputeSolutionFunction()`, `TSSetSolutionFunction()` 11039b7cd975SBarry Smith @*/ 11048434afd1SBarry Smith PetscErrorCode TSSetForcingFunction(TS ts, TSForcingFn *func, void *ctx) 1105d71ae5a4SJacob Faibussowitsch { 11069b7cd975SBarry Smith DM dm; 11079b7cd975SBarry Smith 11089b7cd975SBarry Smith PetscFunctionBegin; 11099b7cd975SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 11109566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11119566063dSJacob Faibussowitsch PetscCall(DMTSSetForcingFunction(dm, func, ctx)); 11123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 11139b7cd975SBarry Smith } 11149b7cd975SBarry Smith 1115d763cef2SBarry Smith /*@C 1116f7ab8db6SBarry Smith TSSetRHSJacobian - Sets the function to compute the Jacobian of G, 1117b5abc632SBarry Smith where U_t = G(U,t), as well as the location to store the matrix. 1118d763cef2SBarry Smith 1119c3339decSBarry Smith Logically Collective 1120d763cef2SBarry Smith 1121d763cef2SBarry Smith Input Parameters: 1122bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1123195e9b02SBarry Smith . Amat - (approximate) location to store Jacobian matrix entries computed by `f` 1124195e9b02SBarry Smith . Pmat - matrix from which preconditioner is to be constructed (usually the same as `Amat`) 1125d763cef2SBarry Smith . f - the Jacobian evaluation routine 1126195e9b02SBarry Smith - ctx - [optional] user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1127d763cef2SBarry Smith 1128bcf0153eSBarry Smith Level: beginner 1129bcf0153eSBarry Smith 11306cd88445SBarry Smith Notes: 11316cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 11326cd88445SBarry Smith 113314d0ab18SJacob Faibussowitsch The `TS` solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f()` 1134ca5f011dSBarry Smith You should not assume the values are the same in the next call to f() as you set them in the previous call. 1135d763cef2SBarry Smith 11368434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSRHSJacobianFn`, `SNESComputeJacobianDefaultColor()`, 11378434afd1SBarry Smith `TSSetRHSFunction()`, `TSRHSJacobianSetReuse()`, `TSSetIJacobian()`, `TSRHSFunctionFn`, `TSIFunctionFn` 1138d763cef2SBarry Smith @*/ 11398434afd1SBarry Smith PetscErrorCode TSSetRHSJacobian(TS ts, Mat Amat, Mat Pmat, TSRHSJacobianFn *f, void *ctx) 1140d71ae5a4SJacob Faibussowitsch { 1141089b2837SJed Brown SNES snes; 114224989b8cSPeter Brune DM dm; 11438434afd1SBarry Smith TSIJacobianFn *ijacobian; 1144277b19d0SLisandro Dalcin 1145d763cef2SBarry Smith PetscFunctionBegin; 11460700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1147e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1148e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1149e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1150e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 1151d763cef2SBarry Smith 11529566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 11539566063dSJacob Faibussowitsch PetscCall(DMTSSetRHSJacobian(dm, f, ctx)); 11549566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijacobian, NULL)); 11559566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 11561e66621cSBarry Smith if (!ijacobian) PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 1157e5d3d808SBarry Smith if (Amat) { 11589566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Amat)); 11599566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 1160e5d3d808SBarry Smith ts->Arhs = Amat; 11610e4ef248SJed Brown } 1162e5d3d808SBarry Smith if (Pmat) { 11639566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)Pmat)); 11649566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 1165e5d3d808SBarry Smith ts->Brhs = Pmat; 11660e4ef248SJed Brown } 11673ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1168d763cef2SBarry Smith } 1169d763cef2SBarry Smith 1170316643e7SJed Brown /*@C 1171b5abc632SBarry Smith TSSetIFunction - Set the function to compute F(t,U,U_t) where F() = 0 is the DAE to be solved. 1172316643e7SJed Brown 1173c3339decSBarry Smith Logically Collective 1174316643e7SJed Brown 1175316643e7SJed Brown Input Parameters: 1176bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1177195e9b02SBarry Smith . r - vector to hold the residual (or `NULL` to have it created internally) 1178316643e7SJed Brown . f - the function evaluation routine 1179195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1180316643e7SJed Brown 1181316643e7SJed Brown Level: beginner 1182316643e7SJed Brown 1183bcf0153eSBarry Smith Note: 1184bcf0153eSBarry Smith The user MUST call either this routine or `TSSetRHSFunction()` to define the ODE. When solving DAEs you must use this function. 1185bcf0153eSBarry Smith 11868434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSIFunctionFn`, `TSSetRHSJacobian()`, `TSSetRHSFunction()`, 118714d0ab18SJacob Faibussowitsch `TSSetIJacobian()` 1188316643e7SJed Brown @*/ 11898434afd1SBarry Smith PetscErrorCode TSSetIFunction(TS ts, Vec r, TSIFunctionFn *f, void *ctx) 1190d71ae5a4SJacob Faibussowitsch { 1191089b2837SJed Brown SNES snes; 119251699248SLisandro Dalcin Vec ralloc = NULL; 119324989b8cSPeter Brune DM dm; 1194316643e7SJed Brown 1195316643e7SJed Brown PetscFunctionBegin; 11960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 119751699248SLisandro Dalcin if (r) PetscValidHeaderSpecific(r, VEC_CLASSID, 2); 119824989b8cSPeter Brune 11999566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12009566063dSJacob Faibussowitsch PetscCall(DMTSSetIFunction(dm, f, ctx)); 120124989b8cSPeter Brune 12029566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 120351699248SLisandro Dalcin if (!r && !ts->dm && ts->vec_sol) { 12049566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->vec_sol, &ralloc)); 120551699248SLisandro Dalcin r = ralloc; 1206e856ceecSJed Brown } 12079566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(snes, r, SNESTSFormFunction, ts)); 12089566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ralloc)); 12093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1210089b2837SJed Brown } 1211089b2837SJed Brown 1212089b2837SJed Brown /*@C 1213a5b23f4aSJose E. Roman TSGetIFunction - Returns the vector where the implicit residual is stored and the function/context to compute it. 1214089b2837SJed Brown 1215089b2837SJed Brown Not Collective 1216089b2837SJed Brown 1217089b2837SJed Brown Input Parameter: 1218bcf0153eSBarry Smith . ts - the `TS` context 1219089b2837SJed Brown 1220d8d19677SJose E. Roman Output Parameters: 1221195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1222195e9b02SBarry Smith . func - the function to compute residual (or `NULL`) 1223195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1224089b2837SJed Brown 1225089b2837SJed Brown Level: advanced 1226089b2837SJed Brown 12271cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()` 1228089b2837SJed Brown @*/ 12298434afd1SBarry Smith PetscErrorCode TSGetIFunction(TS ts, Vec *r, TSIFunctionFn **func, void **ctx) 1230d71ae5a4SJacob Faibussowitsch { 1231089b2837SJed Brown SNES snes; 123224989b8cSPeter Brune DM dm; 1233089b2837SJed Brown 1234089b2837SJed Brown PetscFunctionBegin; 1235089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12369566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12379566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12389566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12399566063dSJacob Faibussowitsch PetscCall(DMTSGetIFunction(dm, func, ctx)); 12403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1241089b2837SJed Brown } 1242089b2837SJed Brown 1243089b2837SJed Brown /*@C 1244dd8e379bSPierre Jolivet TSGetRHSFunction - Returns the vector where the right-hand side is stored and the function/context to compute it. 1245089b2837SJed Brown 1246089b2837SJed Brown Not Collective 1247089b2837SJed Brown 1248089b2837SJed Brown Input Parameter: 1249bcf0153eSBarry Smith . ts - the `TS` context 1250089b2837SJed Brown 1251d8d19677SJose E. Roman Output Parameters: 1252dd8e379bSPierre Jolivet + r - vector to hold computed right-hand side (or `NULL`) 1253dd8e379bSPierre Jolivet . func - the function to compute right-hand side (or `NULL`) 1254195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1255089b2837SJed Brown 1256089b2837SJed Brown Level: advanced 1257089b2837SJed Brown 12581cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `SNESGetFunction()` 1259089b2837SJed Brown @*/ 12608434afd1SBarry Smith PetscErrorCode TSGetRHSFunction(TS ts, Vec *r, TSRHSFunctionFn **func, void **ctx) 1261d71ae5a4SJacob Faibussowitsch { 1262089b2837SJed Brown SNES snes; 126324989b8cSPeter Brune DM dm; 1264089b2837SJed Brown 1265089b2837SJed Brown PetscFunctionBegin; 1266089b2837SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 12679566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 12689566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 12699566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 12709566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, func, ctx)); 12713ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1272316643e7SJed Brown } 1273316643e7SJed Brown 1274316643e7SJed Brown /*@C 1275a4f0a591SBarry Smith TSSetIJacobian - Set the function to compute the matrix dF/dU + a*dF/dU_t where F(t,U,U_t) is the function 1276bcf0153eSBarry Smith provided with `TSSetIFunction()`. 1277316643e7SJed Brown 1278c3339decSBarry Smith Logically Collective 1279316643e7SJed Brown 1280316643e7SJed Brown Input Parameters: 1281bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1282aaa8cc7dSPierre Jolivet . Amat - (approximate) matrix to store Jacobian entries computed by `f` 1283195e9b02SBarry Smith . Pmat - matrix used to compute preconditioner (usually the same as `Amat`) 1284316643e7SJed Brown . f - the Jacobian evaluation routine 1285195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1286316643e7SJed Brown 1287bcf0153eSBarry Smith Level: beginner 1288316643e7SJed Brown 1289bcf0153eSBarry Smith Notes: 1290195e9b02SBarry Smith The matrices `Amat` and `Pmat` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1291bcf0153eSBarry Smith 1292bcf0153eSBarry Smith If you know the operator Amat has a null space you can use `MatSetNullSpace()` and `MatSetTransposeNullSpace()` to supply the null 1293195e9b02SBarry Smith space to `Amat` and the `KSP` solvers will automatically use that null space as needed during the solution process. 1294895c21f2SBarry Smith 1295a4f0a591SBarry Smith The matrix dF/dU + a*dF/dU_t you provide turns out to be 1296b5abc632SBarry Smith the Jacobian of F(t,U,W+a*U) where F(t,U,U_t) = 0 is the DAE to be solved. 1297a4f0a591SBarry Smith The time integrator internally approximates U_t by W+a*U where the positive "shift" 1298a4f0a591SBarry Smith a and vector W depend on the integration method, step size, and past states. For example with 1299a4f0a591SBarry Smith the backward Euler method a = 1/dt and W = -a*U(previous timestep) so 1300a4f0a591SBarry Smith W + a*U = a*(U - U(previous timestep)) = (U - U(previous timestep))/dt 1301a4f0a591SBarry Smith 13026cd88445SBarry Smith You must set all the diagonal entries of the matrices, if they are zero you must still set them with a zero value 13036cd88445SBarry Smith 1304195e9b02SBarry Smith The TS solver may modify the nonzero structure and the entries of the matrices `Amat` and `Pmat` between the calls to `f` 1305195e9b02SBarry Smith You should not assume the values are the same in the next call to `f` as you set them in the previous call. 1306ca5f011dSBarry Smith 1307bc7fdbb5SPierre Jolivet In case `TSSetRHSJacobian()` is also used in conjunction with a fully-implicit solver, 1308d469ad21SStefano Zampini multilevel linear solvers, e.g. `PCMG`, will likely not work due to the way `TS` handles rhs matrices. 1309d469ad21SStefano Zampini 13108434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSIJacobianFn`, `TSSetIFunction()`, `TSSetRHSJacobian()`, 131114d0ab18SJacob Faibussowitsch `SNESComputeJacobianDefaultColor()`, `SNESComputeJacobianDefault()`, `TSSetRHSFunction()` 1312316643e7SJed Brown @*/ 13138434afd1SBarry Smith PetscErrorCode TSSetIJacobian(TS ts, Mat Amat, Mat Pmat, TSIJacobianFn *f, void *ctx) 1314d71ae5a4SJacob Faibussowitsch { 1315089b2837SJed Brown SNES snes; 131624989b8cSPeter Brune DM dm; 1317316643e7SJed Brown 1318316643e7SJed Brown PetscFunctionBegin; 13190700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1320e5d3d808SBarry Smith if (Amat) PetscValidHeaderSpecific(Amat, MAT_CLASSID, 2); 1321e5d3d808SBarry Smith if (Pmat) PetscValidHeaderSpecific(Pmat, MAT_CLASSID, 3); 1322e5d3d808SBarry Smith if (Amat) PetscCheckSameComm(ts, 1, Amat, 2); 1323e5d3d808SBarry Smith if (Pmat) PetscCheckSameComm(ts, 1, Pmat, 3); 132424989b8cSPeter Brune 13259566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13269566063dSJacob Faibussowitsch PetscCall(DMTSSetIJacobian(dm, f, ctx)); 132724989b8cSPeter Brune 13289566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 13299566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, Pmat, SNESTSFormJacobian, ts)); 13303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1331316643e7SJed Brown } 1332316643e7SJed Brown 1333e1244c69SJed Brown /*@ 13348434afd1SBarry Smith TSRHSJacobianSetReuse - restore the RHS Jacobian before calling the user-provided `TSRHSJacobianFn` function again 1335e1244c69SJed Brown 1336e1244c69SJed Brown Logically Collective 1337e1244c69SJed Brown 13384165533cSJose E. Roman Input Parameters: 1339bcf0153eSBarry Smith + ts - `TS` context obtained from `TSCreate()` 1340bcf0153eSBarry Smith - reuse - `PETSC_TRUE` if the RHS Jacobian 1341e1244c69SJed Brown 1342e1244c69SJed Brown Level: intermediate 1343e1244c69SJed Brown 134414d0ab18SJacob Faibussowitsch Notes: 134514d0ab18SJacob Faibussowitsch Without this flag, `TS` will change the sign and shift the RHS Jacobian for a 134614d0ab18SJacob Faibussowitsch finite-time-step implicit solve, in which case the user function will need to recompute the 134714d0ab18SJacob Faibussowitsch entire Jacobian. The `reuse `flag must be set if the evaluation function assumes that the 134814d0ab18SJacob Faibussowitsch matrix entries have not been changed by the `TS`. 134914d0ab18SJacob Faibussowitsch 13501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 1351e1244c69SJed Brown @*/ 1352d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianSetReuse(TS ts, PetscBool reuse) 1353d71ae5a4SJacob Faibussowitsch { 1354e1244c69SJed Brown PetscFunctionBegin; 1355e1244c69SJed Brown ts->rhsjacobian.reuse = reuse; 13563ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1357e1244c69SJed Brown } 1358e1244c69SJed Brown 1359efe9872eSLisandro Dalcin /*@C 1360efe9872eSLisandro Dalcin TSSetI2Function - Set the function to compute F(t,U,U_t,U_tt) where F = 0 is the DAE to be solved. 1361efe9872eSLisandro Dalcin 1362c3339decSBarry Smith Logically Collective 1363efe9872eSLisandro Dalcin 1364efe9872eSLisandro Dalcin Input Parameters: 1365bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1366195e9b02SBarry Smith . F - vector to hold the residual (or `NULL` to have it created internally) 1367efe9872eSLisandro Dalcin . fun - the function evaluation routine 1368195e9b02SBarry Smith - ctx - user-defined context for private data for the function evaluation routine (may be `NULL`) 1369efe9872eSLisandro Dalcin 1370efe9872eSLisandro Dalcin Level: beginner 1371efe9872eSLisandro Dalcin 13728434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSI2FunctionFn`, `TSSetI2Jacobian()`, `TSSetIFunction()`, 137314d0ab18SJacob Faibussowitsch `TSCreate()`, `TSSetRHSFunction()` 1374efe9872eSLisandro Dalcin @*/ 13758434afd1SBarry Smith PetscErrorCode TSSetI2Function(TS ts, Vec F, TSI2FunctionFn *fun, void *ctx) 1376d71ae5a4SJacob Faibussowitsch { 1377efe9872eSLisandro Dalcin DM dm; 1378efe9872eSLisandro Dalcin 1379efe9872eSLisandro Dalcin PetscFunctionBegin; 1380efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1381efe9872eSLisandro Dalcin if (F) PetscValidHeaderSpecific(F, VEC_CLASSID, 2); 13829566063dSJacob Faibussowitsch PetscCall(TSSetIFunction(ts, F, NULL, NULL)); 13839566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 13849566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Function(dm, fun, ctx)); 13853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1386efe9872eSLisandro Dalcin } 1387efe9872eSLisandro Dalcin 1388efe9872eSLisandro Dalcin /*@C 1389a5b23f4aSJose E. Roman TSGetI2Function - Returns the vector where the implicit residual is stored and the function/context to compute it. 1390efe9872eSLisandro Dalcin 1391efe9872eSLisandro Dalcin Not Collective 1392efe9872eSLisandro Dalcin 1393efe9872eSLisandro Dalcin Input Parameter: 1394bcf0153eSBarry Smith . ts - the `TS` context 1395efe9872eSLisandro Dalcin 1396d8d19677SJose E. Roman Output Parameters: 1397195e9b02SBarry Smith + r - vector to hold residual (or `NULL`) 1398195e9b02SBarry Smith . fun - the function to compute residual (or `NULL`) 1399195e9b02SBarry Smith - ctx - the function context (or `NULL`) 1400efe9872eSLisandro Dalcin 1401efe9872eSLisandro Dalcin Level: advanced 1402efe9872eSLisandro Dalcin 14031cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `SNESGetFunction()`, `TSCreate()` 1404efe9872eSLisandro Dalcin @*/ 14058434afd1SBarry Smith PetscErrorCode TSGetI2Function(TS ts, Vec *r, TSI2FunctionFn **fun, void **ctx) 1406d71ae5a4SJacob Faibussowitsch { 1407efe9872eSLisandro Dalcin SNES snes; 1408efe9872eSLisandro Dalcin DM dm; 1409efe9872eSLisandro Dalcin 1410efe9872eSLisandro Dalcin PetscFunctionBegin; 1411efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 14129566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14139566063dSJacob Faibussowitsch PetscCall(SNESGetFunction(snes, r, NULL, NULL)); 14149566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14159566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, fun, ctx)); 14163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1417efe9872eSLisandro Dalcin } 1418efe9872eSLisandro Dalcin 1419efe9872eSLisandro Dalcin /*@C 1420bc77d74cSLisandro Dalcin TSSetI2Jacobian - Set the function to compute the matrix dF/dU + v*dF/dU_t + a*dF/dU_tt 1421bcf0153eSBarry Smith where F(t,U,U_t,U_tt) is the function you provided with `TSSetI2Function()`. 1422efe9872eSLisandro Dalcin 1423c3339decSBarry Smith Logically Collective 1424efe9872eSLisandro Dalcin 1425efe9872eSLisandro Dalcin Input Parameters: 1426bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1427195e9b02SBarry Smith . J - matrix to hold the Jacobian values 1428195e9b02SBarry Smith . P - matrix for constructing the preconditioner (may be same as `J`) 14298434afd1SBarry Smith . jac - the Jacobian evaluation routine, see `TSI2JacobianFn` for the calling sequence 1430195e9b02SBarry Smith - ctx - user-defined context for private data for the Jacobian evaluation routine (may be `NULL`) 1431efe9872eSLisandro Dalcin 1432bcf0153eSBarry Smith Level: beginner 1433bcf0153eSBarry Smith 1434efe9872eSLisandro Dalcin Notes: 1435195e9b02SBarry Smith The matrices `J` and `P` are exactly the matrices that are used by `SNES` for the nonlinear solve. 1436efe9872eSLisandro Dalcin 1437efe9872eSLisandro Dalcin The matrix dF/dU + v*dF/dU_t + a*dF/dU_tt you provide turns out to be 1438efe9872eSLisandro Dalcin the Jacobian of G(U) = F(t,U,W+v*U,W'+a*U) where F(t,U,U_t,U_tt) = 0 is the DAE to be solved. 1439efe9872eSLisandro Dalcin The time integrator internally approximates U_t by W+v*U and U_tt by W'+a*U where the positive "shift" 1440bc77d74cSLisandro Dalcin parameters 'v' and 'a' and vectors W, W' depend on the integration method, step size, and past states. 1441efe9872eSLisandro Dalcin 14428434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSI2JacobianFn`, `TSSetI2Function()`, `TSGetI2Jacobian()` 1443efe9872eSLisandro Dalcin @*/ 14448434afd1SBarry Smith PetscErrorCode TSSetI2Jacobian(TS ts, Mat J, Mat P, TSI2JacobianFn *jac, void *ctx) 1445d71ae5a4SJacob Faibussowitsch { 1446efe9872eSLisandro Dalcin DM dm; 1447efe9872eSLisandro Dalcin 1448efe9872eSLisandro Dalcin PetscFunctionBegin; 1449efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1450efe9872eSLisandro Dalcin if (J) PetscValidHeaderSpecific(J, MAT_CLASSID, 2); 1451efe9872eSLisandro Dalcin if (P) PetscValidHeaderSpecific(P, MAT_CLASSID, 3); 14529566063dSJacob Faibussowitsch PetscCall(TSSetIJacobian(ts, J, P, NULL, NULL)); 14539566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14549566063dSJacob Faibussowitsch PetscCall(DMTSSetI2Jacobian(dm, jac, ctx)); 14553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1456efe9872eSLisandro Dalcin } 1457efe9872eSLisandro Dalcin 1458efe9872eSLisandro Dalcin /*@C 1459efe9872eSLisandro Dalcin TSGetI2Jacobian - Returns the implicit Jacobian at the present timestep. 1460efe9872eSLisandro Dalcin 1461bcf0153eSBarry Smith Not Collective, but parallel objects are returned if `TS` is parallel 1462efe9872eSLisandro Dalcin 1463efe9872eSLisandro Dalcin Input Parameter: 1464bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 1465efe9872eSLisandro Dalcin 1466efe9872eSLisandro Dalcin Output Parameters: 1467efe9872eSLisandro Dalcin + J - The (approximate) Jacobian of F(t,U,U_t,U_tt) 1468195e9b02SBarry Smith . P - The matrix from which the preconditioner is constructed, often the same as `J` 1469efe9872eSLisandro Dalcin . jac - The function to compute the Jacobian matrices 1470efe9872eSLisandro Dalcin - ctx - User-defined context for Jacobian evaluation routine 1471efe9872eSLisandro Dalcin 1472bcf0153eSBarry Smith Level: advanced 1473bcf0153eSBarry Smith 1474195e9b02SBarry Smith Note: 1475195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 1476efe9872eSLisandro Dalcin 14771cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()`, `TSSetI2Jacobian()`, `TSGetI2Function()`, `TSCreate()` 1478efe9872eSLisandro Dalcin @*/ 14798434afd1SBarry Smith PetscErrorCode TSGetI2Jacobian(TS ts, Mat *J, Mat *P, TSI2JacobianFn **jac, void **ctx) 1480d71ae5a4SJacob Faibussowitsch { 1481efe9872eSLisandro Dalcin SNES snes; 1482efe9872eSLisandro Dalcin DM dm; 1483efe9872eSLisandro Dalcin 1484efe9872eSLisandro Dalcin PetscFunctionBegin; 14859566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 14869566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 14879566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, J, P, NULL, NULL)); 14889566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 14899566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, jac, ctx)); 14903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1491efe9872eSLisandro Dalcin } 1492efe9872eSLisandro Dalcin 1493efe9872eSLisandro Dalcin /*@ 1494efe9872eSLisandro Dalcin TSComputeI2Function - Evaluates the DAE residual written in implicit form F(t,U,U_t,U_tt) = 0 1495efe9872eSLisandro Dalcin 1496c3339decSBarry Smith Collective 1497efe9872eSLisandro Dalcin 1498efe9872eSLisandro Dalcin Input Parameters: 1499bcf0153eSBarry Smith + ts - the `TS` context 1500efe9872eSLisandro Dalcin . t - current time 1501efe9872eSLisandro Dalcin . U - state vector 1502efe9872eSLisandro Dalcin . V - time derivative of state vector (U_t) 1503efe9872eSLisandro Dalcin - A - second time derivative of state vector (U_tt) 1504efe9872eSLisandro Dalcin 1505efe9872eSLisandro Dalcin Output Parameter: 1506efe9872eSLisandro Dalcin . F - the residual vector 1507efe9872eSLisandro Dalcin 1508bcf0153eSBarry Smith Level: developer 1509bcf0153eSBarry Smith 1510efe9872eSLisandro Dalcin Note: 1511efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 1512efe9872eSLisandro Dalcin is used internally within the nonlinear solvers. 1513efe9872eSLisandro Dalcin 15141cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Function()`, `TSGetI2Function()` 1515efe9872eSLisandro Dalcin @*/ 1516d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Function(TS ts, PetscReal t, Vec U, Vec V, Vec A, Vec F) 1517d71ae5a4SJacob Faibussowitsch { 1518efe9872eSLisandro Dalcin DM dm; 15198434afd1SBarry Smith TSI2FunctionFn *I2Function; 1520efe9872eSLisandro Dalcin void *ctx; 15218434afd1SBarry Smith TSRHSFunctionFn *rhsfunction; 1522efe9872eSLisandro Dalcin 1523efe9872eSLisandro Dalcin PetscFunctionBegin; 1524efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1525efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1526efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1527efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1528efe9872eSLisandro Dalcin PetscValidHeaderSpecific(F, VEC_CLASSID, 6); 1529efe9872eSLisandro Dalcin 15309566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 15319566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Function(dm, &I2Function, &ctx)); 15329566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSFunction(dm, &rhsfunction, NULL)); 1533efe9872eSLisandro Dalcin 1534efe9872eSLisandro Dalcin if (!I2Function) { 15359566063dSJacob Faibussowitsch PetscCall(TSComputeIFunction(ts, t, U, A, F, PETSC_FALSE)); 15363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1537efe9872eSLisandro Dalcin } 1538efe9872eSLisandro Dalcin 1539da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_FunctionEval, U, ts, V, F)); 1540efe9872eSLisandro Dalcin 1541792fecdfSBarry Smith PetscCallBack("TS callback implicit function", I2Function(ts, t, U, V, A, F, ctx)); 1542efe9872eSLisandro Dalcin 1543efe9872eSLisandro Dalcin if (rhsfunction) { 1544efe9872eSLisandro Dalcin Vec Frhs; 15458af0501fSStefano Zampini 15468af0501fSStefano Zampini PetscCall(DMGetGlobalVector(dm, &Frhs)); 15479566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, t, U, Frhs)); 15489566063dSJacob Faibussowitsch PetscCall(VecAXPY(F, -1, Frhs)); 15498af0501fSStefano Zampini PetscCall(DMRestoreGlobalVector(dm, &Frhs)); 1550efe9872eSLisandro Dalcin } 1551efe9872eSLisandro Dalcin 1552da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_FunctionEval, U, ts, V, F)); 15533ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1554efe9872eSLisandro Dalcin } 1555efe9872eSLisandro Dalcin 1556efe9872eSLisandro Dalcin /*@ 1557efe9872eSLisandro Dalcin TSComputeI2Jacobian - Evaluates the Jacobian of the DAE 1558efe9872eSLisandro Dalcin 1559c3339decSBarry Smith Collective 1560efe9872eSLisandro Dalcin 1561efe9872eSLisandro Dalcin Input Parameters: 1562bcf0153eSBarry Smith + ts - the `TS` context 1563efe9872eSLisandro Dalcin . t - current timestep 1564efe9872eSLisandro Dalcin . U - state vector 1565efe9872eSLisandro Dalcin . V - time derivative of state vector 1566efe9872eSLisandro Dalcin . A - second time derivative of state vector 1567efe9872eSLisandro Dalcin . shiftV - shift to apply, see note below 1568efe9872eSLisandro Dalcin - shiftA - shift to apply, see note below 1569efe9872eSLisandro Dalcin 1570efe9872eSLisandro Dalcin Output Parameters: 1571efe9872eSLisandro Dalcin + J - Jacobian matrix 15727addb90fSBarry Smith - P - optional matrix used to construct the preconditioner 1573efe9872eSLisandro Dalcin 1574bcf0153eSBarry Smith Level: developer 1575bcf0153eSBarry Smith 1576efe9872eSLisandro Dalcin Notes: 15777addb90fSBarry Smith If $F(t,U,V,A) = 0$ is the DAE, the required Jacobian is 1578efe9872eSLisandro Dalcin 15797addb90fSBarry Smith $$ 1580efe9872eSLisandro Dalcin dF/dU + shiftV*dF/dV + shiftA*dF/dA 15817addb90fSBarry Smith $$ 1582efe9872eSLisandro Dalcin 1583efe9872eSLisandro Dalcin Most users should not need to explicitly call this routine, as it 15847addb90fSBarry Smith is used internally within the ODE integrators. 1585efe9872eSLisandro Dalcin 15861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetI2Jacobian()` 1587efe9872eSLisandro Dalcin @*/ 1588d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeI2Jacobian(TS ts, PetscReal t, Vec U, Vec V, Vec A, PetscReal shiftV, PetscReal shiftA, Mat J, Mat P) 1589d71ae5a4SJacob Faibussowitsch { 1590efe9872eSLisandro Dalcin DM dm; 15918434afd1SBarry Smith TSI2JacobianFn *I2Jacobian; 1592efe9872eSLisandro Dalcin void *ctx; 15938434afd1SBarry Smith TSRHSJacobianFn *rhsjacobian; 1594efe9872eSLisandro Dalcin 1595efe9872eSLisandro Dalcin PetscFunctionBegin; 1596efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1597efe9872eSLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 1598efe9872eSLisandro Dalcin PetscValidHeaderSpecific(V, VEC_CLASSID, 4); 1599efe9872eSLisandro Dalcin PetscValidHeaderSpecific(A, VEC_CLASSID, 5); 1600efe9872eSLisandro Dalcin PetscValidHeaderSpecific(J, MAT_CLASSID, 8); 1601efe9872eSLisandro Dalcin PetscValidHeaderSpecific(P, MAT_CLASSID, 9); 1602efe9872eSLisandro Dalcin 16039566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16049566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &I2Jacobian, &ctx)); 16059566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjacobian, NULL)); 1606efe9872eSLisandro Dalcin 1607efe9872eSLisandro Dalcin if (!I2Jacobian) { 16089566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, A, shiftA, J, P, PETSC_FALSE)); 16093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1610efe9872eSLisandro Dalcin } 1611efe9872eSLisandro Dalcin 1612da023ba9SStefano Zampini PetscCall(PetscLogEventBegin(TS_JacobianEval, U, ts, J, P)); 1613792fecdfSBarry Smith PetscCallBack("TS callback implicit Jacobian", I2Jacobian(ts, t, U, V, A, shiftV, shiftA, J, P, ctx)); 1614efe9872eSLisandro Dalcin if (rhsjacobian) { 1615d60b7d5cSBarry Smith Mat Jrhs, Prhs; 16169566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Jrhs, &Prhs)); 16179566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Jrhs, Prhs)); 16189566063dSJacob Faibussowitsch PetscCall(MatAXPY(J, -1, Jrhs, ts->axpy_pattern)); 16199566063dSJacob Faibussowitsch if (P != J) PetscCall(MatAXPY(P, -1, Prhs, ts->axpy_pattern)); 1620efe9872eSLisandro Dalcin } 1621efe9872eSLisandro Dalcin 1622da023ba9SStefano Zampini PetscCall(PetscLogEventEnd(TS_JacobianEval, U, ts, J, P)); 16233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1624efe9872eSLisandro Dalcin } 1625efe9872eSLisandro Dalcin 1626438f35afSJed Brown /*@C 1627438f35afSJed Brown TSSetTransientVariable - sets function to transform from state to transient variables 1628438f35afSJed Brown 1629438f35afSJed Brown Logically Collective 1630438f35afSJed Brown 16314165533cSJose E. Roman Input Parameters: 1632438f35afSJed Brown + ts - time stepping context on which to change the transient variable 16338434afd1SBarry Smith . tvar - a function that transforms to transient variables, see `TSTransientVariableFn` for the calling sequence 1634438f35afSJed Brown - ctx - a context for tvar 1635438f35afSJed Brown 1636438f35afSJed Brown Level: advanced 1637438f35afSJed Brown 1638438f35afSJed Brown Notes: 1639bcf0153eSBarry Smith This is typically used to transform from primitive to conservative variables so that a time integrator (e.g., `TSBDF`) 1640438f35afSJed Brown can be conservative. In this context, primitive variables P are used to model the state (e.g., because they lead to 1641438f35afSJed Brown well-conditioned formulations even in limiting cases such as low-Mach or zero porosity). The transient variable is 1642438f35afSJed Brown C(P), specified by calling this function. An IFunction thus receives arguments (P, Cdot) and the IJacobian must be 1643438f35afSJed Brown evaluated via the chain rule, as in 1644195e9b02SBarry Smith .vb 1645438f35afSJed Brown dF/dP + shift * dF/dCdot dC/dP. 1646195e9b02SBarry Smith .ve 1647438f35afSJed Brown 16488434afd1SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSTransientVariableFn`, `DMTSSetTransientVariable()`, `DMTSGetTransientVariable()`, `TSSetIFunction()`, `TSSetIJacobian()` 1649438f35afSJed Brown @*/ 16508434afd1SBarry Smith PetscErrorCode TSSetTransientVariable(TS ts, TSTransientVariableFn *tvar, void *ctx) 1651d71ae5a4SJacob Faibussowitsch { 1652438f35afSJed Brown DM dm; 1653438f35afSJed Brown 1654438f35afSJed Brown PetscFunctionBegin; 1655438f35afSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 16569566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16579566063dSJacob Faibussowitsch PetscCall(DMTSSetTransientVariable(dm, tvar, ctx)); 16583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1659438f35afSJed Brown } 1660438f35afSJed Brown 1661efe9872eSLisandro Dalcin /*@ 1662e3c11fc1SJed Brown TSComputeTransientVariable - transforms state (primitive) variables to transient (conservative) variables 1663e3c11fc1SJed Brown 1664e3c11fc1SJed Brown Logically Collective 1665e3c11fc1SJed Brown 1666e3c11fc1SJed Brown Input Parameters: 1667e3c11fc1SJed Brown + ts - TS on which to compute 1668e3c11fc1SJed Brown - U - state vector to be transformed to transient variables 1669e3c11fc1SJed Brown 16702fe279fdSBarry Smith Output Parameter: 1671e3c11fc1SJed Brown . C - transient (conservative) variable 1672e3c11fc1SJed Brown 1673e3c11fc1SJed Brown Level: developer 1674e3c11fc1SJed Brown 1675b43aa488SJacob Faibussowitsch Developer Notes: 1676195e9b02SBarry Smith If `DMTSSetTransientVariable()` has not been called, then C is not modified in this routine and C = `NULL` is allowed. 1677bcf0153eSBarry Smith This makes it safe to call without a guard. One can use `TSHasTransientVariable()` to check if transient variables are 1678bcf0153eSBarry Smith being used. 1679bcf0153eSBarry Smith 16801cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeIFunction()`, `TSComputeIJacobian()` 1681e3c11fc1SJed Brown @*/ 1682d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeTransientVariable(TS ts, Vec U, Vec C) 1683d71ae5a4SJacob Faibussowitsch { 1684e3c11fc1SJed Brown DM dm; 1685e3c11fc1SJed Brown DMTS dmts; 1686e3c11fc1SJed Brown 1687e3c11fc1SJed Brown PetscFunctionBegin; 1688e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1689e3c11fc1SJed Brown PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 16909566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 16919566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1692e3c11fc1SJed Brown if (dmts->ops->transientvar) { 1693e3c11fc1SJed Brown PetscValidHeaderSpecific(C, VEC_CLASSID, 3); 16949566063dSJacob Faibussowitsch PetscCall((*dmts->ops->transientvar)(ts, U, C, dmts->transientvarctx)); 1695e3c11fc1SJed Brown } 16963ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1697e3c11fc1SJed Brown } 1698e3c11fc1SJed Brown 1699e3c11fc1SJed Brown /*@ 1700e3c11fc1SJed Brown TSHasTransientVariable - determine whether transient variables have been set 1701e3c11fc1SJed Brown 1702e3c11fc1SJed Brown Logically Collective 1703e3c11fc1SJed Brown 17042fe279fdSBarry Smith Input Parameter: 1705195e9b02SBarry Smith . ts - `TS` on which to compute 1706e3c11fc1SJed Brown 17072fe279fdSBarry Smith Output Parameter: 1708bcf0153eSBarry Smith . has - `PETSC_TRUE` if transient variables have been set 1709e3c11fc1SJed Brown 1710e3c11fc1SJed Brown Level: developer 1711e3c11fc1SJed Brown 17121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `DMTSSetTransientVariable()`, `TSComputeTransientVariable()` 1713e3c11fc1SJed Brown @*/ 1714d71ae5a4SJacob Faibussowitsch PetscErrorCode TSHasTransientVariable(TS ts, PetscBool *has) 1715d71ae5a4SJacob Faibussowitsch { 1716e3c11fc1SJed Brown DM dm; 1717e3c11fc1SJed Brown DMTS dmts; 1718e3c11fc1SJed Brown 1719e3c11fc1SJed Brown PetscFunctionBegin; 1720e3c11fc1SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17219566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 17229566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(dm, &dmts)); 1723e3c11fc1SJed Brown *has = dmts->ops->transientvar ? PETSC_TRUE : PETSC_FALSE; 17243ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1725e3c11fc1SJed Brown } 1726e3c11fc1SJed Brown 1727e3c11fc1SJed Brown /*@ 1728efe9872eSLisandro Dalcin TS2SetSolution - Sets the initial solution and time derivative vectors 1729bcf0153eSBarry Smith for use by the `TS` routines handling second order equations. 1730efe9872eSLisandro Dalcin 1731c3339decSBarry Smith Logically Collective 1732efe9872eSLisandro Dalcin 1733efe9872eSLisandro Dalcin Input Parameters: 1734bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1735efe9872eSLisandro Dalcin . u - the solution vector 1736efe9872eSLisandro Dalcin - v - the time derivative vector 1737efe9872eSLisandro Dalcin 1738efe9872eSLisandro Dalcin Level: beginner 1739efe9872eSLisandro Dalcin 17401cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 1741efe9872eSLisandro Dalcin @*/ 1742d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2SetSolution(TS ts, Vec u, Vec v) 1743d71ae5a4SJacob Faibussowitsch { 1744efe9872eSLisandro Dalcin PetscFunctionBegin; 1745efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1746efe9872eSLisandro Dalcin PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 1747efe9872eSLisandro Dalcin PetscValidHeaderSpecific(v, VEC_CLASSID, 3); 17489566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, u)); 17499566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)v)); 17509566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 1751efe9872eSLisandro Dalcin ts->vec_dot = v; 17523ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1753efe9872eSLisandro Dalcin } 1754efe9872eSLisandro Dalcin 1755efe9872eSLisandro Dalcin /*@ 1756efe9872eSLisandro Dalcin TS2GetSolution - Returns the solution and time derivative at the present timestep 175714d0ab18SJacob Faibussowitsch for second order equations. 1758efe9872eSLisandro Dalcin 175914d0ab18SJacob Faibussowitsch Not Collective 1760efe9872eSLisandro Dalcin 1761efe9872eSLisandro Dalcin Input Parameter: 1762bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 1763efe9872eSLisandro Dalcin 1764d8d19677SJose E. Roman Output Parameters: 1765efe9872eSLisandro Dalcin + u - the vector containing the solution 1766efe9872eSLisandro Dalcin - v - the vector containing the time derivative 1767efe9872eSLisandro Dalcin 1768efe9872eSLisandro Dalcin Level: intermediate 1769efe9872eSLisandro Dalcin 177014d0ab18SJacob Faibussowitsch Notes: 177114d0ab18SJacob Faibussowitsch It is valid to call this routine inside the function 177214d0ab18SJacob Faibussowitsch that you are evaluating in order to move to the new timestep. This vector not 177314d0ab18SJacob Faibussowitsch changed until the solution at the next timestep has been calculated. 177414d0ab18SJacob Faibussowitsch 17751cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TS2SetSolution()`, `TSGetTimeStep()`, `TSGetTime()` 1776efe9872eSLisandro Dalcin @*/ 1777d71ae5a4SJacob Faibussowitsch PetscErrorCode TS2GetSolution(TS ts, Vec *u, Vec *v) 1778d71ae5a4SJacob Faibussowitsch { 1779efe9872eSLisandro Dalcin PetscFunctionBegin; 1780efe9872eSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 17814f572ea9SToby Isaac if (u) PetscAssertPointer(u, 2); 17824f572ea9SToby Isaac if (v) PetscAssertPointer(v, 3); 1783efe9872eSLisandro Dalcin if (u) *u = ts->vec_sol; 1784efe9872eSLisandro Dalcin if (v) *v = ts->vec_dot; 17853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1786efe9872eSLisandro Dalcin } 1787efe9872eSLisandro Dalcin 1788ffeef943SBarry Smith /*@ 1789bcf0153eSBarry Smith TSLoad - Loads a `TS` that has been stored in binary with `TSView()`. 179055849f57SBarry Smith 1791c3339decSBarry Smith Collective 179255849f57SBarry Smith 179355849f57SBarry Smith Input Parameters: 1794b43aa488SJacob Faibussowitsch + ts - the newly loaded `TS`, this needs to have been created with `TSCreate()` or 1795bcf0153eSBarry Smith some related function before a call to `TSLoad()`. 1796bcf0153eSBarry Smith - viewer - binary file viewer, obtained from `PetscViewerBinaryOpen()` 179755849f57SBarry Smith 179855849f57SBarry Smith Level: intermediate 179955849f57SBarry Smith 1800bcf0153eSBarry Smith Note: 1801bcf0153eSBarry Smith The type is determined by the data in the file, any type set into the `TS` before this call is ignored. 180255849f57SBarry Smith 18031cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerBinaryOpen()`, `TSView()`, `MatLoad()`, `VecLoad()` 180455849f57SBarry Smith @*/ 1805d71ae5a4SJacob Faibussowitsch PetscErrorCode TSLoad(TS ts, PetscViewer viewer) 1806d71ae5a4SJacob Faibussowitsch { 180755849f57SBarry Smith PetscBool isbinary; 180855849f57SBarry Smith PetscInt classid; 180955849f57SBarry Smith char type[256]; 18102d53ad75SBarry Smith DMTS sdm; 1811ad6bc421SBarry Smith DM dm; 181255849f57SBarry Smith 181355849f57SBarry Smith PetscFunctionBegin; 1814f2c2a1b9SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 181555849f57SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 18169566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 18173c633725SBarry Smith PetscCheck(isbinary, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerBinaryOpen()"); 181855849f57SBarry Smith 18199566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, &classid, 1, NULL, PETSC_INT)); 18203c633725SBarry Smith PetscCheck(classid == TS_FILE_CLASSID, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Not TS next in file"); 18219566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryRead(viewer, type, 256, NULL, PETSC_CHAR)); 18229566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, type)); 1823dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, load, viewer); 18249566063dSJacob Faibussowitsch PetscCall(DMCreate(PetscObjectComm((PetscObject)ts), &dm)); 18259566063dSJacob Faibussowitsch PetscCall(DMLoad(dm, viewer)); 18269566063dSJacob Faibussowitsch PetscCall(TSSetDM(ts, dm)); 18279566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 18289566063dSJacob Faibussowitsch PetscCall(VecLoad(ts->vec_sol, viewer)); 18299566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 18309566063dSJacob Faibussowitsch PetscCall(DMTSLoad(sdm, viewer)); 18313ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 183255849f57SBarry Smith } 183355849f57SBarry Smith 18349804daf3SBarry Smith #include <petscdraw.h> 1835e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1836e04113cfSBarry Smith #include <petscviewersaws.h> 1837f05ece33SBarry Smith #endif 1838fe2efc57SMark 1839ffeef943SBarry Smith /*@ 1840bcf0153eSBarry Smith TSViewFromOptions - View a `TS` based on values in the options database 1841fe2efc57SMark 1842c3339decSBarry Smith Collective 1843fe2efc57SMark 1844fe2efc57SMark Input Parameters: 1845bcf0153eSBarry Smith + ts - the `TS` context 1846bcf0153eSBarry Smith . obj - Optional object that provides the prefix for the options database keys 1847bcf0153eSBarry Smith - name - command line option string to be passed by user 1848fe2efc57SMark 1849fe2efc57SMark Level: intermediate 1850bcf0153eSBarry Smith 18511cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSView`, `PetscObjectViewFromOptions()`, `TSCreate()` 1852fe2efc57SMark @*/ 1853bcf0153eSBarry Smith PetscErrorCode TSViewFromOptions(TS ts, PetscObject obj, const char name[]) 1854d71ae5a4SJacob Faibussowitsch { 1855fe2efc57SMark PetscFunctionBegin; 1856bcf0153eSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 1857bcf0153eSBarry Smith PetscCall(PetscObjectViewFromOptions((PetscObject)ts, obj, name)); 18583ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 1859fe2efc57SMark } 1860fe2efc57SMark 1861ffeef943SBarry Smith /*@ 1862bcf0153eSBarry Smith TSView - Prints the `TS` data structure. 1863d763cef2SBarry Smith 1864c3339decSBarry Smith Collective 1865d763cef2SBarry Smith 1866d763cef2SBarry Smith Input Parameters: 1867bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 1868d763cef2SBarry Smith - viewer - visualization context 1869d763cef2SBarry Smith 1870d763cef2SBarry Smith Options Database Key: 1871bcf0153eSBarry Smith . -ts_view - calls `TSView()` at end of `TSStep()` 1872bcf0153eSBarry Smith 1873bcf0153eSBarry Smith Level: beginner 1874d763cef2SBarry Smith 1875d763cef2SBarry Smith Notes: 1876d763cef2SBarry Smith The available visualization contexts include 1877bcf0153eSBarry Smith + `PETSC_VIEWER_STDOUT_SELF` - standard output (default) 1878bcf0153eSBarry Smith - `PETSC_VIEWER_STDOUT_WORLD` - synchronized standard 1879d763cef2SBarry Smith output where only the first processor opens 1880d763cef2SBarry Smith the file. All other processors send their 1881d763cef2SBarry Smith data to the first processor to print. 1882d763cef2SBarry Smith 1883d763cef2SBarry Smith The user can open an alternative visualization context with 1884bcf0153eSBarry Smith `PetscViewerASCIIOpen()` - output to a specified file. 1885d763cef2SBarry Smith 1886bcf0153eSBarry Smith In the debugger you can do call `TSView`(ts,0) to display the `TS` solver. (The same holds for any PETSc object viewer). 1887595c91d4SBarry Smith 18881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `PetscViewer`, `PetscViewerASCIIOpen()` 1889d763cef2SBarry Smith @*/ 1890d71ae5a4SJacob Faibussowitsch PetscErrorCode TSView(TS ts, PetscViewer viewer) 1891d71ae5a4SJacob Faibussowitsch { 189219fd82e9SBarry Smith TSType type; 18932b0a91c0SBarry Smith PetscBool iascii, isstring, isundials, isbinary, isdraw; 18942d53ad75SBarry Smith DMTS sdm; 1895e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1896536b137fSBarry Smith PetscBool issaws; 1897f05ece33SBarry Smith #endif 1898d763cef2SBarry Smith 1899d763cef2SBarry Smith PetscFunctionBegin; 19000700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 19011e66621cSBarry Smith if (!viewer) PetscCall(PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)ts), &viewer)); 19020700a824SBarry Smith PetscValidHeaderSpecific(viewer, PETSC_VIEWER_CLASSID, 2); 1903c9780b6fSBarry Smith PetscCheckSameComm(ts, 1, viewer, 2); 1904fd16b177SBarry Smith 19059566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii)); 19069566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSTRING, &isstring)); 19079566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary)); 19089566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw)); 1909e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 19109566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSAWS, &issaws)); 1911f05ece33SBarry Smith #endif 191232077d6dSBarry Smith if (iascii) { 19139566063dSJacob Faibussowitsch PetscCall(PetscObjectPrintClassNamePrefixType((PetscObject)ts, viewer)); 1914efd4aadfSBarry Smith if (ts->ops->view) { 19159566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 1916dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, view, viewer); 19179566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 1918efd4aadfSBarry Smith } 19191690c2aeSBarry Smith if (ts->max_steps < PETSC_INT_MAX) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum steps=%" PetscInt_FMT "\n", ts->max_steps)); 19208e562f8dSJames Wright if (ts->run_steps < PETSC_INT_MAX) PetscCall(PetscViewerASCIIPrintf(viewer, " run steps=%" PetscInt_FMT "\n", ts->run_steps)); 19211e66621cSBarry Smith if (ts->max_time < PETSC_MAX_REAL) PetscCall(PetscViewerASCIIPrintf(viewer, " maximum time=%g\n", (double)ts->max_time)); 19221e66621cSBarry Smith if (ts->ifuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I function evaluations=%" PetscInt_FMT "\n", ts->ifuncs)); 19231e66621cSBarry Smith if (ts->ijacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of I Jacobian evaluations=%" PetscInt_FMT "\n", ts->ijacs)); 19241e66621cSBarry Smith if (ts->rhsfuncs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS function evaluations=%" PetscInt_FMT "\n", ts->rhsfuncs)); 19251e66621cSBarry Smith if (ts->rhsjacs) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of RHS Jacobian evaluations=%" PetscInt_FMT "\n", ts->rhsjacs)); 1926efd4aadfSBarry Smith if (ts->usessnes) { 1927efd4aadfSBarry Smith PetscBool lin; 19281e66621cSBarry Smith if (ts->problem_type == TS_NONLINEAR) PetscCall(PetscViewerASCIIPrintf(viewer, " total number of nonlinear solver iterations=%" PetscInt_FMT "\n", ts->snes_its)); 192963a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of linear solver iterations=%" PetscInt_FMT "\n", ts->ksp_its)); 19309566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompareAny((PetscObject)ts->snes, &lin, SNESKSPONLY, SNESKSPTRANSPOSEONLY, "")); 193163a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of %slinear solve failures=%" PetscInt_FMT "\n", lin ? "" : "non", ts->num_snes_failures)); 1932efd4aadfSBarry Smith } 193363a3b9bcSJacob Faibussowitsch PetscCall(PetscViewerASCIIPrintf(viewer, " total number of rejected steps=%" PetscInt_FMT "\n", ts->reject)); 19341e66621cSBarry Smith if (ts->vrtol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of relative error tolerances, ")); 19351e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using relative error tolerance of %g, ", (double)ts->rtol)); 19361e66621cSBarry Smith if (ts->vatol) PetscCall(PetscViewerASCIIPrintf(viewer, " using vector of absolute error tolerances\n")); 19371e66621cSBarry Smith else PetscCall(PetscViewerASCIIPrintf(viewer, " using absolute error tolerance of %g\n", (double)ts->atol)); 19389566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 19399566063dSJacob Faibussowitsch PetscCall(TSAdaptView(ts->adapt, viewer)); 19409566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 19410f5bd95cSBarry Smith } else if (isstring) { 19429566063dSJacob Faibussowitsch PetscCall(TSGetType(ts, &type)); 19439566063dSJacob Faibussowitsch PetscCall(PetscViewerStringSPrintf(viewer, " TSType: %-7.7s", type)); 1944dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 194555849f57SBarry Smith } else if (isbinary) { 194655849f57SBarry Smith PetscInt classid = TS_FILE_CLASSID; 194755849f57SBarry Smith MPI_Comm comm; 194855849f57SBarry Smith PetscMPIInt rank; 194955849f57SBarry Smith char type[256]; 195055849f57SBarry Smith 19519566063dSJacob Faibussowitsch PetscCall(PetscObjectGetComm((PetscObject)ts, &comm)); 19529566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(comm, &rank)); 1953dd400576SPatrick Sanan if (rank == 0) { 19549566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, &classid, 1, PETSC_INT)); 19559566063dSJacob Faibussowitsch PetscCall(PetscStrncpy(type, ((PetscObject)ts)->type_name, 256)); 19569566063dSJacob Faibussowitsch PetscCall(PetscViewerBinaryWrite(viewer, type, 256, PETSC_CHAR)); 195755849f57SBarry Smith } 1958dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19599566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19609566063dSJacob Faibussowitsch PetscCall(DMView(ts->dm, viewer)); 19619566063dSJacob Faibussowitsch PetscCall(VecView(ts->vec_sol, viewer)); 19629566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 19639566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 19642b0a91c0SBarry Smith } else if (isdraw) { 19652b0a91c0SBarry Smith PetscDraw draw; 19662b0a91c0SBarry Smith char str[36]; 196789fd9fafSBarry Smith PetscReal x, y, bottom, h; 19682b0a91c0SBarry Smith 19699566063dSJacob Faibussowitsch PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw)); 19709566063dSJacob Faibussowitsch PetscCall(PetscDrawGetCurrentPoint(draw, &x, &y)); 1971c6a7a370SJeremy L Thompson PetscCall(PetscStrncpy(str, "TS: ", sizeof(str))); 1972c6a7a370SJeremy L Thompson PetscCall(PetscStrlcat(str, ((PetscObject)ts)->type_name, sizeof(str))); 19739566063dSJacob Faibussowitsch PetscCall(PetscDrawStringBoxed(draw, x, y, PETSC_DRAW_BLACK, PETSC_DRAW_BLACK, str, NULL, &h)); 197489fd9fafSBarry Smith bottom = y - h; 19759566063dSJacob Faibussowitsch PetscCall(PetscDrawPushCurrentPoint(draw, x, bottom)); 1976dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 19779566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptView(ts->adapt, viewer)); 19789566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESView(ts->snes, viewer)); 19799566063dSJacob Faibussowitsch PetscCall(PetscDrawPopCurrentPoint(draw)); 1980e04113cfSBarry Smith #if defined(PETSC_HAVE_SAWS) 1981536b137fSBarry Smith } else if (issaws) { 1982d45a07a7SBarry Smith PetscMPIInt rank; 19832657e9d9SBarry Smith const char *name; 19842657e9d9SBarry Smith 19859566063dSJacob Faibussowitsch PetscCall(PetscObjectGetName((PetscObject)ts, &name)); 19869566063dSJacob Faibussowitsch PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD, &rank)); 1987dd400576SPatrick Sanan if (!((PetscObject)ts)->amsmem && rank == 0) { 1988d45a07a7SBarry Smith char dir[1024]; 1989d45a07a7SBarry Smith 19909566063dSJacob Faibussowitsch PetscCall(PetscObjectViewSAWs((PetscObject)ts, viewer)); 19919566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time_step", name)); 1992792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->steps, 1, SAWs_READ, SAWs_INT)); 19939566063dSJacob Faibussowitsch PetscCall(PetscSNPrintf(dir, 1024, "/PETSc/Objects/%s/time", name)); 1994792fecdfSBarry Smith PetscCallSAWs(SAWs_Register, (dir, &ts->ptime, 1, SAWs_READ, SAWs_DOUBLE)); 1995d763cef2SBarry Smith } 1996dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, view, viewer); 1997f05ece33SBarry Smith #endif 1998f05ece33SBarry Smith } 199936a9e3b9SBarry Smith if (ts->snes && ts->usessnes) { 20009566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20019566063dSJacob Faibussowitsch PetscCall(SNESView(ts->snes, viewer)); 20029566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 200336a9e3b9SBarry Smith } 20049566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &sdm)); 20059566063dSJacob Faibussowitsch PetscCall(DMTSView(sdm, viewer)); 2006f05ece33SBarry Smith 20079566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPushTab(viewer)); 20089566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts, TSSUNDIALS, &isundials)); 20099566063dSJacob Faibussowitsch PetscCall(PetscViewerASCIIPopTab(viewer)); 20103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2011d763cef2SBarry Smith } 2012d763cef2SBarry Smith 2013b07ff414SBarry Smith /*@ 2014ce78bad3SBarry Smith TSSetApplicationContext - Sets an optional user-defined context for the timesteppers that may be accessed, for example inside the user provided 2015ce78bad3SBarry Smith `TS` callbacks with `TSGetApplicationContext()` 2016d763cef2SBarry Smith 2017c3339decSBarry Smith Logically Collective 2018d763cef2SBarry Smith 2019d763cef2SBarry Smith Input Parameters: 2020bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 202149abdd8aSBarry Smith - ctx - user context 2022daf670e6SBarry Smith 2023d763cef2SBarry Smith Level: intermediate 2024d763cef2SBarry Smith 2025ce78bad3SBarry Smith Fortran Note: 2026ce78bad3SBarry Smith This only works when `ctx` is a Fortran derived type (it cannot be a `PetscObject`), we recommend writing a Fortran interface definition for this 2027ce78bad3SBarry Smith function that tells the Fortran compiler the derived data type that is passed in as the `ctx` argument. See `TSGetApplicationContext()` for 2028ce78bad3SBarry Smith an example. 2029bcf0153eSBarry Smith 20301cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetApplicationContext()` 2031d763cef2SBarry Smith @*/ 2032ce78bad3SBarry Smith PetscErrorCode TSSetApplicationContext(TS ts, PeCtx ctx) 2033d71ae5a4SJacob Faibussowitsch { 2034d763cef2SBarry Smith PetscFunctionBegin; 20350700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 203649abdd8aSBarry Smith ts->ctx = ctx; 20373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2038d763cef2SBarry Smith } 2039d763cef2SBarry Smith 2040b07ff414SBarry Smith /*@ 2041d763cef2SBarry Smith TSGetApplicationContext - Gets the user-defined context for the 2042bcf0153eSBarry Smith timestepper that was set with `TSSetApplicationContext()` 2043d763cef2SBarry Smith 2044d763cef2SBarry Smith Not Collective 2045d763cef2SBarry Smith 2046d763cef2SBarry Smith Input Parameter: 2047bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2048d763cef2SBarry Smith 2049d763cef2SBarry Smith Output Parameter: 2050ce78bad3SBarry Smith . ctx - a pointer to the user context 2051d763cef2SBarry Smith 2052bcf0153eSBarry Smith Level: intermediate 2053bcf0153eSBarry Smith 2054b43aa488SJacob Faibussowitsch Fortran Notes: 2055ce78bad3SBarry Smith This only works when the context is a Fortran derived type (it cannot be a `PetscObject`) and you **must** write a Fortran interface definition for this 2056ce78bad3SBarry Smith function that tells the Fortran compiler the derived data type that is returned as the `ctx` argument. For example, 2057ce78bad3SBarry Smith .vb 2058ce78bad3SBarry Smith Interface TSGetApplicationContext 2059ce78bad3SBarry Smith Subroutine TSGetApplicationContext(ts,ctx,ierr) 2060ce78bad3SBarry Smith #include <petsc/finclude/petscts.h> 2061ce78bad3SBarry Smith use petscts 2062ce78bad3SBarry Smith TS ts 2063ce78bad3SBarry Smith type(tUsertype), pointer :: ctx 2064ce78bad3SBarry Smith PetscErrorCode ierr 2065ce78bad3SBarry Smith End Subroutine 2066ce78bad3SBarry Smith End Interface TSGetApplicationContext 2067ce78bad3SBarry Smith .ve 2068ce78bad3SBarry Smith 2069bfe80ac4SPierre Jolivet The prototype for `ctx` must be 2070ce78bad3SBarry Smith .vb 2071ce78bad3SBarry Smith type(tUsertype), pointer :: ctx 2072ce78bad3SBarry Smith .ve 2073daf670e6SBarry Smith 20741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetApplicationContext()` 2075d763cef2SBarry Smith @*/ 207649abdd8aSBarry Smith PetscErrorCode TSGetApplicationContext(TS ts, void *ctx) 2077d71ae5a4SJacob Faibussowitsch { 2078d763cef2SBarry Smith PetscFunctionBegin; 20790700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 208049abdd8aSBarry Smith *(void **)ctx = ts->ctx; 20813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2082d763cef2SBarry Smith } 2083d763cef2SBarry Smith 2084d763cef2SBarry Smith /*@ 2085bcf0153eSBarry Smith TSGetStepNumber - Gets the number of time steps completed. 2086d763cef2SBarry Smith 2087d763cef2SBarry Smith Not Collective 2088d763cef2SBarry Smith 2089d763cef2SBarry Smith Input Parameter: 2090bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2091d763cef2SBarry Smith 2092d763cef2SBarry Smith Output Parameter: 209380275a0aSLisandro Dalcin . steps - number of steps completed so far 2094d763cef2SBarry Smith 2095d763cef2SBarry Smith Level: intermediate 2096d763cef2SBarry Smith 20971cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetTimeStep()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()` 2098d763cef2SBarry Smith @*/ 2099d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepNumber(TS ts, PetscInt *steps) 2100d71ae5a4SJacob Faibussowitsch { 2101d763cef2SBarry Smith PetscFunctionBegin; 21020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 21034f572ea9SToby Isaac PetscAssertPointer(steps, 2); 210480275a0aSLisandro Dalcin *steps = ts->steps; 21053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 210680275a0aSLisandro Dalcin } 210780275a0aSLisandro Dalcin 210880275a0aSLisandro Dalcin /*@ 210980275a0aSLisandro Dalcin TSSetStepNumber - Sets the number of steps completed. 211080275a0aSLisandro Dalcin 2111c3339decSBarry Smith Logically Collective 211280275a0aSLisandro Dalcin 211380275a0aSLisandro Dalcin Input Parameters: 2114bcf0153eSBarry Smith + ts - the `TS` context 211580275a0aSLisandro Dalcin - steps - number of steps completed so far 211680275a0aSLisandro Dalcin 2117bcf0153eSBarry Smith Level: developer 2118bcf0153eSBarry Smith 2119bcf0153eSBarry Smith Note: 2120bcf0153eSBarry Smith For most uses of the `TS` solvers the user need not explicitly call 2121bcf0153eSBarry Smith `TSSetStepNumber()`, as the step counter is appropriately updated in 2122bcf0153eSBarry Smith `TSSolve()`/`TSStep()`/`TSRollBack()`. Power users may call this routine to 212380275a0aSLisandro Dalcin reinitialize timestepping by setting the step counter to zero (and time 212480275a0aSLisandro Dalcin to the initial time) to solve a similar problem with different initial 212580275a0aSLisandro Dalcin conditions or parameters. Other possible use case is to continue 2126195e9b02SBarry Smith timestepping from a previously interrupted run in such a way that `TS` 212780275a0aSLisandro Dalcin monitors will be called with a initial nonzero step counter. 212880275a0aSLisandro Dalcin 21291cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetStepNumber()`, `TSSetTime()`, `TSSetTimeStep()`, `TSSetSolution()` 213080275a0aSLisandro Dalcin @*/ 2131d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetStepNumber(TS ts, PetscInt steps) 2132d71ae5a4SJacob Faibussowitsch { 213380275a0aSLisandro Dalcin PetscFunctionBegin; 213480275a0aSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 213580275a0aSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, steps, 2); 21363c633725SBarry Smith PetscCheck(steps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Step number must be non-negative"); 213780275a0aSLisandro Dalcin ts->steps = steps; 21383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2139d763cef2SBarry Smith } 2140d763cef2SBarry Smith 2141d763cef2SBarry Smith /*@ 2142d763cef2SBarry Smith TSSetTimeStep - Allows one to reset the timestep at any time, 2143d763cef2SBarry Smith useful for simple pseudo-timestepping codes. 2144d763cef2SBarry Smith 2145c3339decSBarry Smith Logically Collective 2146d763cef2SBarry Smith 2147d763cef2SBarry Smith Input Parameters: 2148bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2149d763cef2SBarry Smith - time_step - the size of the timestep 2150d763cef2SBarry Smith 2151d763cef2SBarry Smith Level: intermediate 2152d763cef2SBarry Smith 21531cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPSEUDO`, `TSGetTimeStep()`, `TSSetTime()` 2154d763cef2SBarry Smith @*/ 2155d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeStep(TS ts, PetscReal time_step) 2156d71ae5a4SJacob Faibussowitsch { 2157d763cef2SBarry Smith PetscFunctionBegin; 21580700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2159c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, time_step, 2); 2160d763cef2SBarry Smith ts->time_step = time_step; 21613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2162d763cef2SBarry Smith } 2163d763cef2SBarry Smith 2164a43b19c4SJed Brown /*@ 216549354f04SShri Abhyankar TSSetExactFinalTime - Determines whether to adapt the final time step to 21660b4b7b1cSBarry Smith match the exact final time, to interpolate the solution to the exact final time, 21670b4b7b1cSBarry Smith or to just return at the final time `TS` computed (which may be slightly larger 21680b4b7b1cSBarry Smith than the requested final time). 2169a43b19c4SJed Brown 2170c3339decSBarry Smith Logically Collective 2171a43b19c4SJed Brown 2172d8d19677SJose E. Roman Input Parameters: 2173a43b19c4SJed Brown + ts - the time-step context 217449354f04SShri Abhyankar - eftopt - exact final time option 2175bcf0153eSBarry Smith .vb 21760b4b7b1cSBarry Smith TS_EXACTFINALTIME_STEPOVER - Don't do anything if final time is exceeded, just use it 21770b4b7b1cSBarry Smith TS_EXACTFINALTIME_INTERPOLATE - Interpolate back to final time if the final time is exceeded 21780b4b7b1cSBarry Smith TS_EXACTFINALTIME_MATCHSTEP - Adapt final time step to ensure the computed final time exactly equals the requested final time 2179bcf0153eSBarry Smith .ve 2180a43b19c4SJed Brown 2181bcf0153eSBarry Smith Options Database Key: 21820b4b7b1cSBarry Smith . -ts_exact_final_time <stepover,interpolate,matchstep> - select the final step approach at runtime 2183feed9e9dSBarry Smith 2184a43b19c4SJed Brown Level: beginner 2185a43b19c4SJed Brown 2186bcf0153eSBarry Smith Note: 2187bcf0153eSBarry Smith If you use the option `TS_EXACTFINALTIME_STEPOVER` the solution may be at a very different time 2188bcf0153eSBarry Smith then the final time you selected. 2189bcf0153eSBarry Smith 21901cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSGetExactFinalTime()` 2191a43b19c4SJed Brown @*/ 2192d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetExactFinalTime(TS ts, TSExactFinalTimeOption eftopt) 2193d71ae5a4SJacob Faibussowitsch { 2194a43b19c4SJed Brown PetscFunctionBegin; 2195a43b19c4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 219649354f04SShri Abhyankar PetscValidLogicalCollectiveEnum(ts, eftopt, 2); 219749354f04SShri Abhyankar ts->exact_final_time = eftopt; 21983ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2199a43b19c4SJed Brown } 2200a43b19c4SJed Brown 2201d763cef2SBarry Smith /*@ 2202bcf0153eSBarry Smith TSGetExactFinalTime - Gets the exact final time option set with `TSSetExactFinalTime()` 2203f6953c82SLisandro Dalcin 2204f6953c82SLisandro Dalcin Not Collective 2205f6953c82SLisandro Dalcin 2206f6953c82SLisandro Dalcin Input Parameter: 2207bcf0153eSBarry Smith . ts - the `TS` context 2208f6953c82SLisandro Dalcin 2209f6953c82SLisandro Dalcin Output Parameter: 2210f6953c82SLisandro Dalcin . eftopt - exact final time option 2211f6953c82SLisandro Dalcin 2212f6953c82SLisandro Dalcin Level: beginner 2213f6953c82SLisandro Dalcin 22141cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSExactFinalTimeOption`, `TSSetExactFinalTime()` 2215f6953c82SLisandro Dalcin @*/ 2216d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetExactFinalTime(TS ts, TSExactFinalTimeOption *eftopt) 2217d71ae5a4SJacob Faibussowitsch { 2218f6953c82SLisandro Dalcin PetscFunctionBegin; 2219f6953c82SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22204f572ea9SToby Isaac PetscAssertPointer(eftopt, 2); 2221f6953c82SLisandro Dalcin *eftopt = ts->exact_final_time; 22223ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2223f6953c82SLisandro Dalcin } 2224f6953c82SLisandro Dalcin 2225f6953c82SLisandro Dalcin /*@ 2226d763cef2SBarry Smith TSGetTimeStep - Gets the current timestep size. 2227d763cef2SBarry Smith 2228d763cef2SBarry Smith Not Collective 2229d763cef2SBarry Smith 2230d763cef2SBarry Smith Input Parameter: 2231bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2232d763cef2SBarry Smith 2233d763cef2SBarry Smith Output Parameter: 2234d763cef2SBarry Smith . dt - the current timestep size 2235d763cef2SBarry Smith 2236d763cef2SBarry Smith Level: intermediate 2237d763cef2SBarry Smith 22381cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetTimeStep()`, `TSGetTime()` 2239d763cef2SBarry Smith @*/ 2240d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStep(TS ts, PetscReal *dt) 2241d71ae5a4SJacob Faibussowitsch { 2242d763cef2SBarry Smith PetscFunctionBegin; 22430700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22444f572ea9SToby Isaac PetscAssertPointer(dt, 2); 2245d763cef2SBarry Smith *dt = ts->time_step; 22463ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2247d763cef2SBarry Smith } 2248d763cef2SBarry Smith 2249d8e5e3e6SSatish Balay /*@ 2250d763cef2SBarry Smith TSGetSolution - Returns the solution at the present timestep. It 2251d763cef2SBarry Smith is valid to call this routine inside the function that you are evaluating 2252d763cef2SBarry Smith in order to move to the new timestep. This vector not changed until 2253d763cef2SBarry Smith the solution at the next timestep has been calculated. 2254d763cef2SBarry Smith 2255bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 2256d763cef2SBarry Smith 2257d763cef2SBarry Smith Input Parameter: 2258bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2259d763cef2SBarry Smith 2260d763cef2SBarry Smith Output Parameter: 2261d763cef2SBarry Smith . v - the vector containing the solution 2262d763cef2SBarry Smith 2263d763cef2SBarry Smith Level: intermediate 2264d763cef2SBarry Smith 2265bcf0153eSBarry Smith Note: 2266bcf0153eSBarry Smith If you used `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`); this does not return the solution at the requested 2267bcf0153eSBarry Smith final time. It returns the solution at the next timestep. 2268d763cef2SBarry Smith 22691cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetSolutionComponents()`, `TSSetSolutionFunction()` 2270d763cef2SBarry Smith @*/ 2271d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolution(TS ts, Vec *v) 2272d71ae5a4SJacob Faibussowitsch { 2273d763cef2SBarry Smith PetscFunctionBegin; 22740700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 22754f572ea9SToby Isaac PetscAssertPointer(v, 2); 22768737fe31SLisandro Dalcin *v = ts->vec_sol; 22773ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2278d763cef2SBarry Smith } 2279d763cef2SBarry Smith 228003fe5f5eSDebojyoti Ghosh /*@ 2281b2bf4f3aSDebojyoti Ghosh TSGetSolutionComponents - Returns any solution components at the present 228203fe5f5eSDebojyoti Ghosh timestep, if available for the time integration method being used. 2283b2bf4f3aSDebojyoti Ghosh Solution components are quantities that share the same size and 228403fe5f5eSDebojyoti Ghosh structure as the solution vector. 228503fe5f5eSDebojyoti Ghosh 2286bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 228703fe5f5eSDebojyoti Ghosh 2288b43aa488SJacob Faibussowitsch Input Parameters: 2289bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2290195e9b02SBarry Smith . n - If v is `NULL`, then the number of solution components is 2291b2bf4f3aSDebojyoti Ghosh returned through n, else the n-th solution component is 229203fe5f5eSDebojyoti Ghosh returned in v. 2293a2b725a8SWilliam Gropp - v - the vector containing the n-th solution component 2294195e9b02SBarry Smith (may be `NULL` to use this function to find out 2295b2bf4f3aSDebojyoti Ghosh the number of solutions components). 229603fe5f5eSDebojyoti Ghosh 22974cdd57e5SDebojyoti Ghosh Level: advanced 229803fe5f5eSDebojyoti Ghosh 22991cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 230003fe5f5eSDebojyoti Ghosh @*/ 2301d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolutionComponents(TS ts, PetscInt *n, Vec *v) 2302d71ae5a4SJacob Faibussowitsch { 230303fe5f5eSDebojyoti Ghosh PetscFunctionBegin; 230403fe5f5eSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2305b2bf4f3aSDebojyoti Ghosh if (!ts->ops->getsolutioncomponents) *n = 0; 2306dbbe0bcdSBarry Smith else PetscUseTypeMethod(ts, getsolutioncomponents, n, v); 23073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 230803fe5f5eSDebojyoti Ghosh } 230903fe5f5eSDebojyoti Ghosh 23104cdd57e5SDebojyoti Ghosh /*@ 23114cdd57e5SDebojyoti Ghosh TSGetAuxSolution - Returns an auxiliary solution at the present 23124cdd57e5SDebojyoti Ghosh timestep, if available for the time integration method being used. 23134cdd57e5SDebojyoti Ghosh 2314bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23154cdd57e5SDebojyoti Ghosh 2316b43aa488SJacob Faibussowitsch Input Parameters: 2317bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2318a2b725a8SWilliam Gropp - v - the vector containing the auxiliary solution 23194cdd57e5SDebojyoti Ghosh 23204cdd57e5SDebojyoti Ghosh Level: intermediate 23214cdd57e5SDebojyoti Ghosh 23221cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetSolution()` 23234cdd57e5SDebojyoti Ghosh @*/ 2324d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAuxSolution(TS ts, Vec *v) 2325d71ae5a4SJacob Faibussowitsch { 23264cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23274cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2328dbbe0bcdSBarry Smith if (ts->ops->getauxsolution) PetscUseTypeMethod(ts, getauxsolution, v); 23291e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23303ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23314cdd57e5SDebojyoti Ghosh } 23324cdd57e5SDebojyoti Ghosh 23334cdd57e5SDebojyoti Ghosh /*@ 23344cdd57e5SDebojyoti Ghosh TSGetTimeError - Returns the estimated error vector, if the chosen 2335bcf0153eSBarry Smith `TSType` has an error estimation functionality and `TSSetTimeError()` was called 23364cdd57e5SDebojyoti Ghosh 2337bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 23389657682dSDebojyoti Ghosh 2339b43aa488SJacob Faibussowitsch Input Parameters: 2340bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2341657c1e31SEmil Constantinescu . n - current estimate (n=0) or previous one (n=-1) 2342a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 23434cdd57e5SDebojyoti Ghosh 23444cdd57e5SDebojyoti Ghosh Level: intermediate 23454cdd57e5SDebojyoti Ghosh 2346bcf0153eSBarry Smith Note: 2347bcf0153eSBarry Smith MUST call after `TSSetUp()` 23484cdd57e5SDebojyoti Ghosh 23491cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetSolution()`, `TSSetTimeError()` 23504cdd57e5SDebojyoti Ghosh @*/ 2351d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeError(TS ts, PetscInt n, Vec *v) 2352d71ae5a4SJacob Faibussowitsch { 23534cdd57e5SDebojyoti Ghosh PetscFunctionBegin; 23544cdd57e5SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2355dbbe0bcdSBarry Smith if (ts->ops->gettimeerror) PetscUseTypeMethod(ts, gettimeerror, n, v); 23561e66621cSBarry Smith else PetscCall(VecZeroEntries(*v)); 23573ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 23584cdd57e5SDebojyoti Ghosh } 23594cdd57e5SDebojyoti Ghosh 236057df6a1bSDebojyoti Ghosh /*@ 236157df6a1bSDebojyoti Ghosh TSSetTimeError - Sets the estimated error vector, if the chosen 2362bcf0153eSBarry Smith `TSType` has an error estimation functionality. This can be used 236357df6a1bSDebojyoti Ghosh to restart such a time integrator with a given error vector. 236457df6a1bSDebojyoti Ghosh 2365bcf0153eSBarry Smith Not Collective, but v returned is parallel if ts is parallel 236657df6a1bSDebojyoti Ghosh 2367b43aa488SJacob Faibussowitsch Input Parameters: 2368bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` (input parameter). 2369a2b725a8SWilliam Gropp - v - the vector containing the error (same size as the solution). 237057df6a1bSDebojyoti Ghosh 237157df6a1bSDebojyoti Ghosh Level: intermediate 237257df6a1bSDebojyoti Ghosh 2373b43aa488SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSSetSolution()`, `TSGetTimeError()` 237457df6a1bSDebojyoti Ghosh @*/ 2375d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTimeError(TS ts, Vec v) 2376d71ae5a4SJacob Faibussowitsch { 237757df6a1bSDebojyoti Ghosh PetscFunctionBegin; 237857df6a1bSDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 23793c633725SBarry Smith PetscCheck(ts->setupcalled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetUp() first"); 2380dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, settimeerror, v); 23813ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 238257df6a1bSDebojyoti Ghosh } 238357df6a1bSDebojyoti Ghosh 2384bdad233fSMatthew Knepley /* ----- Routines to initialize and destroy a timestepper ---- */ 2385d8e5e3e6SSatish Balay /*@ 2386bdad233fSMatthew Knepley TSSetProblemType - Sets the type of problem to be solved. 2387d763cef2SBarry Smith 2388bdad233fSMatthew Knepley Not collective 2389d763cef2SBarry Smith 2390bdad233fSMatthew Knepley Input Parameters: 2391bcf0153eSBarry Smith + ts - The `TS` 2392bcf0153eSBarry Smith - type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2393d763cef2SBarry Smith .vb 23940910c330SBarry Smith U_t - A U = 0 (linear) 23950910c330SBarry Smith U_t - A(t) U = 0 (linear) 23960910c330SBarry Smith F(t,U,U_t) = 0 (nonlinear) 2397d763cef2SBarry Smith .ve 2398d763cef2SBarry Smith 2399d763cef2SBarry Smith Level: beginner 2400d763cef2SBarry Smith 24011cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2402d763cef2SBarry Smith @*/ 2403d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetProblemType(TS ts, TSProblemType type) 2404d71ae5a4SJacob Faibussowitsch { 2405d763cef2SBarry Smith PetscFunctionBegin; 24060700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2407bdad233fSMatthew Knepley ts->problem_type = type; 24089e2a6581SJed Brown if (type == TS_LINEAR) { 24099e2a6581SJed Brown SNES snes; 24109566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 24119566063dSJacob Faibussowitsch PetscCall(SNESSetType(snes, SNESKSPONLY)); 24129e2a6581SJed Brown } 24133ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2414d763cef2SBarry Smith } 2415d763cef2SBarry Smith 2416cc4c1da9SBarry Smith /*@ 2417bdad233fSMatthew Knepley TSGetProblemType - Gets the type of problem to be solved. 2418bdad233fSMatthew Knepley 2419bdad233fSMatthew Knepley Not collective 2420bdad233fSMatthew Knepley 2421bdad233fSMatthew Knepley Input Parameter: 2422bcf0153eSBarry Smith . ts - The `TS` 2423bdad233fSMatthew Knepley 2424bdad233fSMatthew Knepley Output Parameter: 2425bcf0153eSBarry Smith . type - One of `TS_LINEAR`, `TS_NONLINEAR` where these types refer to problems of the forms 2426bdad233fSMatthew Knepley .vb 2427089b2837SJed Brown M U_t = A U 2428089b2837SJed Brown M(t) U_t = A(t) U 2429b5abc632SBarry Smith F(t,U,U_t) 2430bdad233fSMatthew Knepley .ve 2431bdad233fSMatthew Knepley 2432bdad233fSMatthew Knepley Level: beginner 2433bdad233fSMatthew Knepley 24341cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetUp()`, `TSProblemType`, `TS` 2435bdad233fSMatthew Knepley @*/ 2436d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetProblemType(TS ts, TSProblemType *type) 2437d71ae5a4SJacob Faibussowitsch { 2438bdad233fSMatthew Knepley PetscFunctionBegin; 24390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24404f572ea9SToby Isaac PetscAssertPointer(type, 2); 2441bdad233fSMatthew Knepley *type = ts->problem_type; 24423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2443bdad233fSMatthew Knepley } 2444d763cef2SBarry Smith 2445303a5415SBarry Smith /* 2446303a5415SBarry Smith Attempt to check/preset a default value for the exact final time option. This is needed at the beginning of TSSolve() and in TSSetUp() 2447303a5415SBarry Smith */ 2448d71ae5a4SJacob Faibussowitsch static PetscErrorCode TSSetExactFinalTimeDefault(TS ts) 2449d71ae5a4SJacob Faibussowitsch { 2450303a5415SBarry Smith PetscBool isnone; 2451303a5415SBarry Smith 2452303a5415SBarry Smith PetscFunctionBegin; 24539566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 24549566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 2455303a5415SBarry Smith 24569566063dSJacob Faibussowitsch PetscCall(PetscObjectTypeCompare((PetscObject)ts->adapt, TSADAPTNONE, &isnone)); 24571e66621cSBarry Smith if (!isnone && ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_MATCHSTEP; 24581e66621cSBarry Smith else if (ts->exact_final_time == TS_EXACTFINALTIME_UNSPECIFIED) ts->exact_final_time = TS_EXACTFINALTIME_INTERPOLATE; 24593ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2460303a5415SBarry Smith } 2461303a5415SBarry Smith 2462d763cef2SBarry Smith /*@ 2463303a5415SBarry Smith TSSetUp - Sets up the internal data structures for the later use of a timestepper. 2464d763cef2SBarry Smith 2465c3339decSBarry Smith Collective 2466d763cef2SBarry Smith 2467d763cef2SBarry Smith Input Parameter: 2468bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2469d763cef2SBarry Smith 2470d763cef2SBarry Smith Level: advanced 2471d763cef2SBarry Smith 2472bcf0153eSBarry Smith Note: 2473bcf0153eSBarry Smith For basic use of the `TS` solvers the user need not explicitly call 2474bcf0153eSBarry Smith `TSSetUp()`, since these actions will automatically occur during 2475bcf0153eSBarry Smith the call to `TSStep()` or `TSSolve()`. However, if one wishes to control this 2476bcf0153eSBarry Smith phase separately, `TSSetUp()` should be called after `TSCreate()` 2477bcf0153eSBarry Smith and optional routines of the form TSSetXXX(), but before `TSStep()` and `TSSolve()`. 2478bcf0153eSBarry Smith 24791cc06b55SBarry Smith .seealso: [](ch_ts), `TSCreate()`, `TS`, `TSStep()`, `TSDestroy()`, `TSSolve()` 2480d763cef2SBarry Smith @*/ 2481d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUp(TS ts) 2482d71ae5a4SJacob Faibussowitsch { 24836c6b9e74SPeter Brune DM dm; 24846c6b9e74SPeter Brune PetscErrorCode (*func)(SNES, Vec, Vec, void *); 2485d1e9a80fSBarry Smith PetscErrorCode (*jac)(SNES, Vec, Mat, Mat, void *); 24868434afd1SBarry Smith TSIFunctionFn *ifun; 24878434afd1SBarry Smith TSIJacobianFn *ijac; 24888434afd1SBarry Smith TSI2JacobianFn *i2jac; 24898434afd1SBarry Smith TSRHSJacobianFn *rhsjac; 2490d763cef2SBarry Smith 2491d763cef2SBarry Smith PetscFunctionBegin; 24920700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 24933ba16761SJacob Faibussowitsch if (ts->setupcalled) PetscFunctionReturn(PETSC_SUCCESS); 2494277b19d0SLisandro Dalcin 24957adad957SLisandro Dalcin if (!((PetscObject)ts)->type_name) { 24969566063dSJacob Faibussowitsch PetscCall(TSGetIFunction(ts, NULL, &ifun, NULL)); 24979566063dSJacob Faibussowitsch PetscCall(TSSetType(ts, ifun ? TSBEULER : TSEULER)); 2498d763cef2SBarry Smith } 2499277b19d0SLisandro Dalcin 25001a638600SStefano Zampini if (!ts->vec_sol) { 25011e66621cSBarry Smith PetscCheck(ts->dm, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call TSSetSolution() first"); 25029566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(ts->dm, &ts->vec_sol)); 25031a638600SStefano Zampini } 2504277b19d0SLisandro Dalcin 2505136cf249SJames Wright if (ts->eval_times) { 2506136cf249SJames Wright if (!ts->eval_times->sol_vecs) PetscCall(VecDuplicateVecs(ts->vec_sol, ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 2507136cf249SJames Wright if (!ts->eval_times->sol_times) PetscCall(PetscMalloc1(ts->eval_times->num_time_points, &ts->eval_times->sol_times)); 25084a658b32SHong Zhang } 2509298bade4SHong Zhang if (!ts->Jacp && ts->Jacprhs) { /* IJacobianP shares the same matrix with RHSJacobianP if only RHSJacobianP is provided */ 25109566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)ts->Jacprhs)); 2511298bade4SHong Zhang ts->Jacp = ts->Jacprhs; 2512298bade4SHong Zhang } 2513298bade4SHong Zhang 2514cd4cee2dSHong Zhang if (ts->quadraturets) { 25159566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts->quadraturets)); 25169566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 25179566063dSJacob Faibussowitsch PetscCall(VecDuplicate(ts->quadraturets->vec_sol, &ts->vec_costintegrand)); 2518cd4cee2dSHong Zhang } 2519cd4cee2dSHong Zhang 25209566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, NULL, NULL, &rhsjac, NULL)); 2521f23ba4b3SHong Zhang if (rhsjac == TSComputeRHSJacobianConstant) { 2522e1244c69SJed Brown Mat Amat, Pmat; 2523e1244c69SJed Brown SNES snes; 25249566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 25259566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, &Amat, &Pmat, NULL, NULL)); 2526e1244c69SJed Brown /* Matching matrices implies that an IJacobian is NOT set, because if it had been set, the IJacobian's matrix would 2527e1244c69SJed Brown * have displaced the RHS matrix */ 2528971015bcSStefano Zampini if (Amat && Amat == ts->Arhs) { 2529abc0d4abSBarry Smith /* we need to copy the values of the matrix because for the constant Jacobian case the user will never set the numerical values in this new location */ 25309566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Arhs, MAT_COPY_VALUES, &Amat)); 25319566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, Amat, NULL, NULL, NULL)); 25329566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Amat)); 2533e1244c69SJed Brown } 2534971015bcSStefano Zampini if (Pmat && Pmat == ts->Brhs) { 25359566063dSJacob Faibussowitsch PetscCall(MatDuplicate(ts->Brhs, MAT_COPY_VALUES, &Pmat)); 25369566063dSJacob Faibussowitsch PetscCall(SNESSetJacobian(snes, NULL, Pmat, NULL, NULL)); 25379566063dSJacob Faibussowitsch PetscCall(MatDestroy(&Pmat)); 2538e1244c69SJed Brown } 2539e1244c69SJed Brown } 25402ffb9264SLisandro Dalcin 25419566063dSJacob Faibussowitsch PetscCall(TSGetAdapt(ts, &ts->adapt)); 25429566063dSJacob Faibussowitsch PetscCall(TSAdaptSetDefaultType(ts->adapt, ts->default_adapt_type)); 25432ffb9264SLisandro Dalcin 2544dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, setup); 2545277b19d0SLisandro Dalcin 25469566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 25472ffb9264SLisandro Dalcin 2548a6772fa2SLisandro Dalcin /* In the case where we've set a DMTSFunction or what have you, we need the default SNESFunction 25496c6b9e74SPeter Brune to be set right but can't do it elsewhere due to the overreliance on ctx=ts. 25506c6b9e74SPeter Brune */ 25519566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 25529566063dSJacob Faibussowitsch PetscCall(DMSNESGetFunction(dm, &func, NULL)); 25531e66621cSBarry Smith if (!func) PetscCall(DMSNESSetFunction(dm, SNESTSFormFunction, ts)); 25541e66621cSBarry Smith 2555a6772fa2SLisandro Dalcin /* If the SNES doesn't have a jacobian set and the TS has an ijacobian or rhsjacobian set, set the SNES to use it. 25566c6b9e74SPeter Brune Otherwise, the SNES will use coloring internally to form the Jacobian. 25576c6b9e74SPeter Brune */ 25589566063dSJacob Faibussowitsch PetscCall(DMSNESGetJacobian(dm, &jac, NULL)); 25599566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, &ijac, NULL)); 25609566063dSJacob Faibussowitsch PetscCall(DMTSGetI2Jacobian(dm, &i2jac, NULL)); 25619566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, &rhsjac, NULL)); 25621e66621cSBarry Smith if (!jac && (ijac || i2jac || rhsjac)) PetscCall(DMSNESSetJacobian(dm, SNESTSFormJacobian, ts)); 2563c0517034SDebojyoti Ghosh 2564c0517034SDebojyoti Ghosh /* if time integration scheme has a starting method, call it */ 2565dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, startingmethod); 2566c0517034SDebojyoti Ghosh 2567277b19d0SLisandro Dalcin ts->setupcalled = PETSC_TRUE; 25683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2569277b19d0SLisandro Dalcin } 2570277b19d0SLisandro Dalcin 2571f6a906c0SBarry Smith /*@ 25720b4b7b1cSBarry Smith TSReset - Resets a `TS` context to the state it was in before `TSSetUp()` was called and removes any allocated `Vec` and `Mat` from its data structures 2573277b19d0SLisandro Dalcin 2574c3339decSBarry Smith Collective 2575277b19d0SLisandro Dalcin 2576277b19d0SLisandro Dalcin Input Parameter: 2577bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2578277b19d0SLisandro Dalcin 25790b4b7b1cSBarry Smith Level: developer 2580277b19d0SLisandro Dalcin 25810b4b7b1cSBarry Smith Notes: 25820b4b7b1cSBarry Smith Any options set on the `TS` object, including those set with `TSSetFromOptions()` remain. 25830b4b7b1cSBarry Smith 25840b4b7b1cSBarry Smith See also `TSSetResize()` to change the size of the system being integrated (for example by adaptive mesh refinement) during the time integration. 25850b4b7b1cSBarry Smith 2586bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSetResize()` 2587277b19d0SLisandro Dalcin @*/ 2588d71ae5a4SJacob Faibussowitsch PetscErrorCode TSReset(TS ts) 2589d71ae5a4SJacob Faibussowitsch { 25901d06f6b3SHong Zhang TS_RHSSplitLink ilink = ts->tsrhssplit, next; 2591277b19d0SLisandro Dalcin 2592277b19d0SLisandro Dalcin PetscFunctionBegin; 2593277b19d0SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2594b18ea86cSHong Zhang 2595dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, reset); 25969566063dSJacob Faibussowitsch if (ts->snes) PetscCall(SNESReset(ts->snes)); 25979566063dSJacob Faibussowitsch if (ts->adapt) PetscCall(TSAdaptReset(ts->adapt)); 2598bbd56ea5SKarl Rupp 25999566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Arhs)); 26009566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Brhs)); 26019566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->Frhs)); 26029566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 2603c61711c8SStefano Zampini PetscCall(VecDestroy(&ts->vec_sol0)); 26049566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_dot)); 26059566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 26069566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 26079566063dSJacob Faibussowitsch PetscCall(VecDestroyVecs(ts->nwork, &ts->work)); 2608bbd56ea5SKarl Rupp 26099566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacprhs)); 26109566063dSJacob Faibussowitsch PetscCall(MatDestroy(&ts->Jacp)); 26111baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardReset(ts)); 2612cd4cee2dSHong Zhang if (ts->quadraturets) { 26139566063dSJacob Faibussowitsch PetscCall(TSReset(ts->quadraturets)); 26149566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_costintegrand)); 2615cd4cee2dSHong Zhang } 26161d06f6b3SHong Zhang while (ilink) { 26171d06f6b3SHong Zhang next = ilink->next; 26189566063dSJacob Faibussowitsch PetscCall(TSDestroy(&ilink->ts)); 26199566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink->splitname)); 26209566063dSJacob Faibussowitsch PetscCall(ISDestroy(&ilink->is)); 26219566063dSJacob Faibussowitsch PetscCall(PetscFree(ilink)); 26221d06f6b3SHong Zhang ilink = next; 262387f4e208SHong Zhang } 26246bf673ebSJoe Pusztay ts->tsrhssplit = NULL; 2625545aaa6fSHong Zhang ts->num_rhs_splits = 0; 2626136cf249SJames Wright if (ts->eval_times) { 2627136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->time_points)); 2628136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->sol_times)); 2629136cf249SJames Wright PetscCall(VecDestroyVecs(ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 2630136cf249SJames Wright PetscCall(PetscFree(ts->eval_times)); 26314a658b32SHong Zhang } 2632b3a72457SStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 2633b3a72457SStefano Zampini ts->rhsjacobian.scale = 1.0; 2634b3a72457SStefano Zampini ts->ijacobian.shift = 1.0; 2635277b19d0SLisandro Dalcin ts->setupcalled = PETSC_FALSE; 26363ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2637d763cef2SBarry Smith } 2638d763cef2SBarry Smith 263914d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS); 264014d0ab18SJacob Faibussowitsch 26410764c050SBarry Smith /*@ 2642d763cef2SBarry Smith TSDestroy - Destroys the timestepper context that was created 2643bcf0153eSBarry Smith with `TSCreate()`. 2644d763cef2SBarry Smith 2645c3339decSBarry Smith Collective 2646d763cef2SBarry Smith 2647d763cef2SBarry Smith Input Parameter: 2648bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2649d763cef2SBarry Smith 2650d763cef2SBarry Smith Level: beginner 2651d763cef2SBarry Smith 26521cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2653d763cef2SBarry Smith @*/ 2654d71ae5a4SJacob Faibussowitsch PetscErrorCode TSDestroy(TS *ts) 2655d71ae5a4SJacob Faibussowitsch { 2656d763cef2SBarry Smith PetscFunctionBegin; 26573ba16761SJacob Faibussowitsch if (!*ts) PetscFunctionReturn(PETSC_SUCCESS); 2658ecf68647SHong Zhang PetscValidHeaderSpecific(*ts, TS_CLASSID, 1); 2659f4f49eeaSPierre Jolivet if (--((PetscObject)*ts)->refct > 0) { 26609371c9d4SSatish Balay *ts = NULL; 26613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 26629371c9d4SSatish Balay } 2663d763cef2SBarry Smith 26649566063dSJacob Faibussowitsch PetscCall(TSReset(*ts)); 26659566063dSJacob Faibussowitsch PetscCall(TSAdjointReset(*ts)); 26661e66621cSBarry Smith if ((*ts)->forward_solve) PetscCall(TSForwardReset(*ts)); 26676bd3a4fdSStefano Zampini PetscCall(TSResizeReset(*ts)); 26681e66621cSBarry Smith 2669e04113cfSBarry Smith /* if memory was published with SAWs then destroy it */ 26709566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsViewOff((PetscObject)*ts)); 2671f4f49eeaSPierre Jolivet PetscTryTypeMethod(*ts, destroy); 26726d4c513bSLisandro Dalcin 26739566063dSJacob Faibussowitsch PetscCall(TSTrajectoryDestroy(&(*ts)->trajectory)); 2674bc952696SBarry Smith 26759566063dSJacob Faibussowitsch PetscCall(TSAdaptDestroy(&(*ts)->adapt)); 26769566063dSJacob Faibussowitsch PetscCall(TSEventDestroy(&(*ts)->event)); 26776427ac75SLisandro Dalcin 26789566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&(*ts)->snes)); 26794bd3aaa3SHong Zhang PetscCall(SNESDestroy(&(*ts)->snesrhssplit)); 26809566063dSJacob Faibussowitsch PetscCall(DMDestroy(&(*ts)->dm)); 2681f4f49eeaSPierre Jolivet PetscCall(TSMonitorCancel(*ts)); 2682f4f49eeaSPierre Jolivet PetscCall(TSAdjointMonitorCancel(*ts)); 26836d4c513bSLisandro Dalcin 26849566063dSJacob Faibussowitsch PetscCall(TSDestroy(&(*ts)->quadraturets)); 26859566063dSJacob Faibussowitsch PetscCall(PetscHeaderDestroy(ts)); 26863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2687d763cef2SBarry Smith } 2688d763cef2SBarry Smith 2689d8e5e3e6SSatish Balay /*@ 2690bcf0153eSBarry Smith TSGetSNES - Returns the `SNES` (nonlinear solver) associated with 2691bcf0153eSBarry Smith a `TS` (timestepper) context. Valid only for nonlinear problems. 2692d763cef2SBarry Smith 2693bcf0153eSBarry Smith Not Collective, but snes is parallel if ts is parallel 2694d763cef2SBarry Smith 2695d763cef2SBarry Smith Input Parameter: 2696bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2697d763cef2SBarry Smith 2698d763cef2SBarry Smith Output Parameter: 2699d763cef2SBarry Smith . snes - the nonlinear solver context 2700d763cef2SBarry Smith 2701d763cef2SBarry Smith Level: beginner 2702d763cef2SBarry Smith 2703bcf0153eSBarry Smith Notes: 2704bcf0153eSBarry Smith The user can then directly manipulate the `SNES` context to set various 2705bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2706bcf0153eSBarry Smith `KSP`, and `PC` contexts as well. 2707bcf0153eSBarry Smith 2708bcf0153eSBarry Smith `TSGetSNES()` does not work for integrators that do not use `SNES`; in 2709195e9b02SBarry Smith this case `TSGetSNES()` returns `NULL` in `snes`. 2710bcf0153eSBarry Smith 27111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()` 2712d763cef2SBarry Smith @*/ 2713d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNES(TS ts, SNES *snes) 2714d71ae5a4SJacob Faibussowitsch { 2715d763cef2SBarry Smith PetscFunctionBegin; 27160700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27174f572ea9SToby Isaac PetscAssertPointer(snes, 2); 2718d372ba47SLisandro Dalcin if (!ts->snes) { 27199566063dSJacob Faibussowitsch PetscCall(SNESCreate(PetscObjectComm((PetscObject)ts), &ts->snes)); 27209566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptions((PetscObject)ts->snes, ((PetscObject)ts)->options)); 27219566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27229566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->snes, (PetscObject)ts, 1)); 27234bd3aaa3SHong Zhang if (ts->dm) PetscCall(SNESSetDM(ts->snes, ts->dm)); 27241e66621cSBarry Smith if (ts->problem_type == TS_LINEAR) PetscCall(SNESSetType(ts->snes, SNESKSPONLY)); 2725d372ba47SLisandro Dalcin } 2726d763cef2SBarry Smith *snes = ts->snes; 27273ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2728d763cef2SBarry Smith } 2729d763cef2SBarry Smith 2730deb2cd25SJed Brown /*@ 27310b4b7b1cSBarry Smith TSSetSNES - Set the `SNES` (nonlinear solver) to be used by the `TS` timestepping context 2732deb2cd25SJed Brown 2733deb2cd25SJed Brown Collective 2734deb2cd25SJed Brown 2735d8d19677SJose E. Roman Input Parameters: 2736bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2737deb2cd25SJed Brown - snes - the nonlinear solver context 2738deb2cd25SJed Brown 2739deb2cd25SJed Brown Level: developer 2740deb2cd25SJed Brown 2741bcf0153eSBarry Smith Note: 2742bcf0153eSBarry Smith Most users should have the `TS` created by calling `TSGetSNES()` 2743bcf0153eSBarry Smith 27441cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2745deb2cd25SJed Brown @*/ 2746d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSNES(TS ts, SNES snes) 2747d71ae5a4SJacob Faibussowitsch { 2748d1e9a80fSBarry Smith PetscErrorCode (*func)(SNES, Vec, Mat, Mat, void *); 2749deb2cd25SJed Brown 2750deb2cd25SJed Brown PetscFunctionBegin; 2751deb2cd25SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2752deb2cd25SJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 2); 27539566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)snes)); 27549566063dSJacob Faibussowitsch PetscCall(SNESDestroy(&ts->snes)); 2755bbd56ea5SKarl Rupp 2756deb2cd25SJed Brown ts->snes = snes; 2757bbd56ea5SKarl Rupp 27589566063dSJacob Faibussowitsch PetscCall(SNESSetFunction(ts->snes, NULL, SNESTSFormFunction, ts)); 27599566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(ts->snes, NULL, NULL, &func, NULL)); 27601e66621cSBarry Smith if (func == SNESTSFormJacobian) PetscCall(SNESSetJacobian(ts->snes, NULL, NULL, SNESTSFormJacobian, ts)); 27613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2762deb2cd25SJed Brown } 2763deb2cd25SJed Brown 2764d8e5e3e6SSatish Balay /*@ 2765bcf0153eSBarry Smith TSGetKSP - Returns the `KSP` (linear solver) associated with 2766bcf0153eSBarry Smith a `TS` (timestepper) context. 2767d763cef2SBarry Smith 2768195e9b02SBarry Smith Not Collective, but `ksp` is parallel if `ts` is parallel 2769d763cef2SBarry Smith 2770d763cef2SBarry Smith Input Parameter: 2771bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2772d763cef2SBarry Smith 2773d763cef2SBarry Smith Output Parameter: 277494b7f48cSBarry Smith . ksp - the nonlinear solver context 2775d763cef2SBarry Smith 2776d763cef2SBarry Smith Level: beginner 2777d763cef2SBarry Smith 2778bcf0153eSBarry Smith Notes: 2779bcf0153eSBarry Smith The user can then directly manipulate the `KSP` context to set various 2780bcf0153eSBarry Smith options, etc. Likewise, the user can then extract and manipulate the 2781bcf0153eSBarry Smith `PC` context as well. 2782bcf0153eSBarry Smith 2783bcf0153eSBarry Smith `TSGetKSP()` does not work for integrators that do not use `KSP`; 2784195e9b02SBarry Smith in this case `TSGetKSP()` returns `NULL` in `ksp`. 2785bcf0153eSBarry Smith 27861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `KSP`, `TSCreate()`, `TSSetUp()`, `TSSolve()`, `TSGetSNES()` 2787d763cef2SBarry Smith @*/ 2788d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSP(TS ts, KSP *ksp) 2789d71ae5a4SJacob Faibussowitsch { 2790089b2837SJed Brown SNES snes; 2791d372ba47SLisandro Dalcin 2792d763cef2SBarry Smith PetscFunctionBegin; 27930700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 27944f572ea9SToby Isaac PetscAssertPointer(ksp, 2); 27953c633725SBarry Smith PetscCheck(((PetscObject)ts)->type_name, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "KSP is not created yet. Call TSSetType() first"); 27963c633725SBarry Smith PetscCheck(ts->problem_type == TS_LINEAR, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Linear only; use TSGetSNES()"); 27979566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 27989566063dSJacob Faibussowitsch PetscCall(SNESGetKSP(snes, ksp)); 27993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2800d763cef2SBarry Smith } 2801d763cef2SBarry Smith 2802d763cef2SBarry Smith /* ----------- Routines to set solver parameters ---------- */ 2803d763cef2SBarry Smith 2804adb62b0dSMatthew Knepley /*@ 2805618ce8baSLisandro Dalcin TSSetMaxSteps - Sets the maximum number of steps to use. 2806618ce8baSLisandro Dalcin 2807c3339decSBarry Smith Logically Collective 2808618ce8baSLisandro Dalcin 2809618ce8baSLisandro Dalcin Input Parameters: 2810bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2811618ce8baSLisandro Dalcin - maxsteps - maximum number of steps to use 2812618ce8baSLisandro Dalcin 2813bcf0153eSBarry Smith Options Database Key: 2814618ce8baSLisandro Dalcin . -ts_max_steps <maxsteps> - Sets maxsteps 2815618ce8baSLisandro Dalcin 2816618ce8baSLisandro Dalcin Level: intermediate 2817618ce8baSLisandro Dalcin 2818bcf0153eSBarry Smith Note: 281909cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum number of steps to the default from when the object's type was set 282009cb0f53SBarry Smith 282109cb0f53SBarry Smith The default maximum number of steps is 5,000 282209cb0f53SBarry Smith 282309cb0f53SBarry Smith Fortran Note: 282409cb0f53SBarry Smith Use `PETSC_DETERMINE_INTEGER` 2825bcf0153eSBarry Smith 28261cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()` 2827618ce8baSLisandro Dalcin @*/ 2828d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSteps(TS ts, PetscInt maxsteps) 2829d71ae5a4SJacob Faibussowitsch { 2830618ce8baSLisandro Dalcin PetscFunctionBegin; 2831618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2832618ce8baSLisandro Dalcin PetscValidLogicalCollectiveInt(ts, maxsteps, 2); 283309cb0f53SBarry Smith if (maxsteps == PETSC_DETERMINE) { 283409cb0f53SBarry Smith ts->max_steps = ts->default_max_steps; 283509cb0f53SBarry Smith } else { 28363c633725SBarry Smith PetscCheck(maxsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Maximum number of steps must be non-negative"); 2837618ce8baSLisandro Dalcin ts->max_steps = maxsteps; 283809cb0f53SBarry Smith } 28393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2840618ce8baSLisandro Dalcin } 2841618ce8baSLisandro Dalcin 2842618ce8baSLisandro Dalcin /*@ 2843618ce8baSLisandro Dalcin TSGetMaxSteps - Gets the maximum number of steps to use. 2844618ce8baSLisandro Dalcin 2845618ce8baSLisandro Dalcin Not Collective 2846618ce8baSLisandro Dalcin 28472fe279fdSBarry Smith Input Parameter: 2848bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2849618ce8baSLisandro Dalcin 2850618ce8baSLisandro Dalcin Output Parameter: 2851618ce8baSLisandro Dalcin . maxsteps - maximum number of steps to use 2852618ce8baSLisandro Dalcin 2853618ce8baSLisandro Dalcin Level: advanced 2854618ce8baSLisandro Dalcin 28551cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()` 2856618ce8baSLisandro Dalcin @*/ 2857d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxSteps(TS ts, PetscInt *maxsteps) 2858d71ae5a4SJacob Faibussowitsch { 2859618ce8baSLisandro Dalcin PetscFunctionBegin; 2860618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28614f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 2862618ce8baSLisandro Dalcin *maxsteps = ts->max_steps; 28633ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2864618ce8baSLisandro Dalcin } 2865618ce8baSLisandro Dalcin 2866618ce8baSLisandro Dalcin /*@ 28678e562f8dSJames Wright TSSetRunSteps - Sets the maximum number of steps to take in each call to `TSSolve()`. 28688e562f8dSJames Wright 28698e562f8dSJames Wright If the step count when `TSSolve()` is `start_step`, this will stop the simulation once `current_step - start_step >= run_steps`. 28708e562f8dSJames Wright Comparatively, `TSSetMaxSteps()` will stop if `current_step >= max_steps`. 28718e562f8dSJames Wright The simulation will stop when either condition is reached. 28728e562f8dSJames Wright 28738e562f8dSJames Wright Logically Collective 28748e562f8dSJames Wright 28758e562f8dSJames Wright Input Parameters: 28768e562f8dSJames Wright + ts - the `TS` context obtained from `TSCreate()` 28778e562f8dSJames Wright - runsteps - maximum number of steps to take in each call to `TSSolve()`; 28788e562f8dSJames Wright 28798e562f8dSJames Wright Options Database Key: 28808e562f8dSJames Wright . -ts_run_steps <runsteps> - Sets runsteps 28818e562f8dSJames Wright 28828e562f8dSJames Wright Level: intermediate 28838e562f8dSJames Wright 28848e562f8dSJames Wright Note: 28858e562f8dSJames Wright The default is `PETSC_UNLIMITED` 28868e562f8dSJames Wright 28878e562f8dSJames Wright .seealso: [](ch_ts), `TS`, `TSGetRunSteps()`, `TSSetMaxTime()`, `TSSetExactFinalTime()`, `TSSetMaxSteps()` 28888e562f8dSJames Wright @*/ 28898e562f8dSJames Wright PetscErrorCode TSSetRunSteps(TS ts, PetscInt runsteps) 28908e562f8dSJames Wright { 28918e562f8dSJames Wright PetscFunctionBegin; 28928e562f8dSJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 28938e562f8dSJames Wright PetscValidLogicalCollectiveInt(ts, runsteps, 2); 28948e562f8dSJames Wright if (runsteps == PETSC_DETERMINE) { 28958e562f8dSJames Wright ts->run_steps = PETSC_UNLIMITED; 28968e562f8dSJames Wright } else { 28978e562f8dSJames Wright PetscCheck(runsteps >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Max number of steps to take in each call to TSSolve must be non-negative"); 28988e562f8dSJames Wright ts->run_steps = runsteps; 28998e562f8dSJames Wright } 29008e562f8dSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29018e562f8dSJames Wright } 29028e562f8dSJames Wright 29038e562f8dSJames Wright /*@ 29048e562f8dSJames Wright TSGetRunSteps - Gets the maximum number of steps to take in each call to `TSSolve()`. 29058e562f8dSJames Wright 29068e562f8dSJames Wright Not Collective 29078e562f8dSJames Wright 29088e562f8dSJames Wright Input Parameter: 29098e562f8dSJames Wright . ts - the `TS` context obtained from `TSCreate()` 29108e562f8dSJames Wright 29118e562f8dSJames Wright Output Parameter: 29128e562f8dSJames Wright . runsteps - maximum number of steps to take in each call to `TSSolve`. 29138e562f8dSJames Wright 29148e562f8dSJames Wright Level: advanced 29158e562f8dSJames Wright 29168e562f8dSJames Wright .seealso: [](ch_ts), `TS`, `TSSetRunSteps()`, `TSGetMaxTime()`, `TSSetMaxTime()`, `TSGetMaxSteps()` 29178e562f8dSJames Wright @*/ 29188e562f8dSJames Wright PetscErrorCode TSGetRunSteps(TS ts, PetscInt *runsteps) 29198e562f8dSJames Wright { 29208e562f8dSJames Wright PetscFunctionBegin; 29218e562f8dSJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29228e562f8dSJames Wright PetscAssertPointer(runsteps, 2); 29238e562f8dSJames Wright *runsteps = ts->run_steps; 29248e562f8dSJames Wright PetscFunctionReturn(PETSC_SUCCESS); 29258e562f8dSJames Wright } 29268e562f8dSJames Wright 29278e562f8dSJames Wright /*@ 2928618ce8baSLisandro Dalcin TSSetMaxTime - Sets the maximum (or final) time for timestepping. 2929618ce8baSLisandro Dalcin 2930c3339decSBarry Smith Logically Collective 2931618ce8baSLisandro Dalcin 2932618ce8baSLisandro Dalcin Input Parameters: 2933bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 2934618ce8baSLisandro Dalcin - maxtime - final time to step to 2935618ce8baSLisandro Dalcin 2936bcf0153eSBarry Smith Options Database Key: 2937ef85077eSLisandro Dalcin . -ts_max_time <maxtime> - Sets maxtime 2938618ce8baSLisandro Dalcin 2939bcf0153eSBarry Smith Level: intermediate 2940bcf0153eSBarry Smith 2941618ce8baSLisandro Dalcin Notes: 294209cb0f53SBarry Smith Use `PETSC_DETERMINE` to reset the maximum time to the default from when the object's type was set 294309cb0f53SBarry Smith 2944618ce8baSLisandro Dalcin The default maximum time is 5.0 2945618ce8baSLisandro Dalcin 294609cb0f53SBarry Smith Fortran Note: 294709cb0f53SBarry Smith Use `PETSC_DETERMINE_REAL` 294809cb0f53SBarry Smith 29491cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetMaxTime()`, `TSSetMaxSteps()`, `TSSetExactFinalTime()` 2950618ce8baSLisandro Dalcin @*/ 2951d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxTime(TS ts, PetscReal maxtime) 2952d71ae5a4SJacob Faibussowitsch { 2953618ce8baSLisandro Dalcin PetscFunctionBegin; 2954618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 2955618ce8baSLisandro Dalcin PetscValidLogicalCollectiveReal(ts, maxtime, 2); 295609cb0f53SBarry Smith if (maxtime == PETSC_DETERMINE) { 295709cb0f53SBarry Smith ts->max_time = ts->default_max_time; 295809cb0f53SBarry Smith } else { 2959618ce8baSLisandro Dalcin ts->max_time = maxtime; 296009cb0f53SBarry Smith } 29613ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2962618ce8baSLisandro Dalcin } 2963618ce8baSLisandro Dalcin 2964618ce8baSLisandro Dalcin /*@ 2965618ce8baSLisandro Dalcin TSGetMaxTime - Gets the maximum (or final) time for timestepping. 2966618ce8baSLisandro Dalcin 2967618ce8baSLisandro Dalcin Not Collective 2968618ce8baSLisandro Dalcin 29692fe279fdSBarry Smith Input Parameter: 2970bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 2971618ce8baSLisandro Dalcin 2972618ce8baSLisandro Dalcin Output Parameter: 2973618ce8baSLisandro Dalcin . maxtime - final time to step to 2974618ce8baSLisandro Dalcin 2975618ce8baSLisandro Dalcin Level: advanced 2976618ce8baSLisandro Dalcin 29771cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetMaxTime()`, `TSGetMaxSteps()`, `TSSetMaxSteps()` 2978618ce8baSLisandro Dalcin @*/ 2979d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetMaxTime(TS ts, PetscReal *maxtime) 2980d71ae5a4SJacob Faibussowitsch { 2981618ce8baSLisandro Dalcin PetscFunctionBegin; 2982618ce8baSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29834f572ea9SToby Isaac PetscAssertPointer(maxtime, 2); 2984618ce8baSLisandro Dalcin *maxtime = ts->max_time; 29853ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 2986618ce8baSLisandro Dalcin } 2987618ce8baSLisandro Dalcin 298814d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 2989618ce8baSLisandro Dalcin /*@ 2990bcf0153eSBarry Smith TSSetInitialTimeStep - Deprecated, use `TSSetTime()` and `TSSetTimeStep()`. 2991edc382c3SSatish Balay 2992edc382c3SSatish Balay Level: deprecated 2993edc382c3SSatish Balay 2994aaa6c58dSLisandro Dalcin @*/ 2995d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetInitialTimeStep(TS ts, PetscReal initial_time, PetscReal time_step) 2996d71ae5a4SJacob Faibussowitsch { 2997aaa6c58dSLisandro Dalcin PetscFunctionBegin; 2998aaa6c58dSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 29999566063dSJacob Faibussowitsch PetscCall(TSSetTime(ts, initial_time)); 30009566063dSJacob Faibussowitsch PetscCall(TSSetTimeStep(ts, time_step)); 30013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3002aaa6c58dSLisandro Dalcin } 3003aaa6c58dSLisandro Dalcin 300414d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 3005aaa6c58dSLisandro Dalcin /*@ 3006bcf0153eSBarry Smith TSGetDuration - Deprecated, use `TSGetMaxSteps()` and `TSGetMaxTime()`. 3007edc382c3SSatish Balay 3008edc382c3SSatish Balay Level: deprecated 3009edc382c3SSatish Balay 3010adb62b0dSMatthew Knepley @*/ 3011d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDuration(TS ts, PetscInt *maxsteps, PetscReal *maxtime) 3012d71ae5a4SJacob Faibussowitsch { 3013adb62b0dSMatthew Knepley PetscFunctionBegin; 30140700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3015abc0a331SBarry Smith if (maxsteps) { 30164f572ea9SToby Isaac PetscAssertPointer(maxsteps, 2); 3017adb62b0dSMatthew Knepley *maxsteps = ts->max_steps; 3018adb62b0dSMatthew Knepley } 3019abc0a331SBarry Smith if (maxtime) { 30204f572ea9SToby Isaac PetscAssertPointer(maxtime, 3); 3021adb62b0dSMatthew Knepley *maxtime = ts->max_time; 3022adb62b0dSMatthew Knepley } 30233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3024adb62b0dSMatthew Knepley } 3025adb62b0dSMatthew Knepley 302614d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 3027d763cef2SBarry Smith /*@ 3028bcf0153eSBarry Smith TSSetDuration - Deprecated, use `TSSetMaxSteps()` and `TSSetMaxTime()`. 3029edc382c3SSatish Balay 3030edc382c3SSatish Balay Level: deprecated 3031edc382c3SSatish Balay 3032d763cef2SBarry Smith @*/ 3033d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDuration(TS ts, PetscInt maxsteps, PetscReal maxtime) 3034d71ae5a4SJacob Faibussowitsch { 3035d763cef2SBarry Smith PetscFunctionBegin; 3036835f2295SStefano Zampini if (maxsteps != PETSC_CURRENT) PetscCall(TSSetMaxSteps(ts, maxsteps)); 303709cb0f53SBarry Smith if (maxtime != (PetscReal)PETSC_CURRENT) PetscCall(TSSetMaxTime(ts, maxtime)); 30383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3039d763cef2SBarry Smith } 3040d763cef2SBarry Smith 304114d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 3042d763cef2SBarry Smith /*@ 3043bcf0153eSBarry Smith TSGetTimeStepNumber - Deprecated, use `TSGetStepNumber()`. 3044edc382c3SSatish Balay 3045edc382c3SSatish Balay Level: deprecated 3046edc382c3SSatish Balay 30475c5f5948SLisandro Dalcin @*/ 3048d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTimeStepNumber(TS ts, PetscInt *steps) 3049d71ae5a4SJacob Faibussowitsch { 30509371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 30519371c9d4SSatish Balay } 30525c5f5948SLisandro Dalcin 305314d0ab18SJacob Faibussowitsch // PetscClangLinter pragma disable: -fdoc-* 305419eac22cSLisandro Dalcin /*@ 3055bcf0153eSBarry Smith TSGetTotalSteps - Deprecated, use `TSGetStepNumber()`. 3056edc382c3SSatish Balay 3057edc382c3SSatish Balay Level: deprecated 3058edc382c3SSatish Balay 30594f4e0956SLisandro Dalcin @*/ 3060d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTotalSteps(TS ts, PetscInt *steps) 3061d71ae5a4SJacob Faibussowitsch { 30629371c9d4SSatish Balay return TSGetStepNumber(ts, steps); 30639371c9d4SSatish Balay } 30644f4e0956SLisandro Dalcin 30654f4e0956SLisandro Dalcin /*@ 3066d763cef2SBarry Smith TSSetSolution - Sets the initial solution vector 3067bcf0153eSBarry Smith for use by the `TS` routines. 3068d763cef2SBarry Smith 3069c3339decSBarry Smith Logically Collective 3070d763cef2SBarry Smith 3071d763cef2SBarry Smith Input Parameters: 3072bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 30730910c330SBarry Smith - u - the solution vector 3074d763cef2SBarry Smith 3075d763cef2SBarry Smith Level: beginner 3076d763cef2SBarry Smith 30771cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetSolutionFunction()`, `TSGetSolution()`, `TSCreate()` 3078d763cef2SBarry Smith @*/ 3079d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetSolution(TS ts, Vec u) 3080d71ae5a4SJacob Faibussowitsch { 3081496e6a7aSJed Brown DM dm; 30828737fe31SLisandro Dalcin 3083d763cef2SBarry Smith PetscFunctionBegin; 30840700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 30850910c330SBarry Smith PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 30869566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)u)); 30879566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vec_sol)); 30880910c330SBarry Smith ts->vec_sol = u; 3089bbd56ea5SKarl Rupp 30909566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 30919566063dSJacob Faibussowitsch PetscCall(DMShellSetGlobalVector(dm, u)); 30923ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3093d763cef2SBarry Smith } 3094d763cef2SBarry Smith 3095ac226902SBarry Smith /*@C 3096000e7ae3SMatthew Knepley TSSetPreStep - Sets the general-purpose function 30973f2090d5SJed Brown called once at the beginning of each time step. 3098000e7ae3SMatthew Knepley 3099c3339decSBarry Smith Logically Collective 3100000e7ae3SMatthew Knepley 3101000e7ae3SMatthew Knepley Input Parameters: 3102bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3103000e7ae3SMatthew Knepley - func - The function 3104000e7ae3SMatthew Knepley 310520f4b53cSBarry Smith Calling sequence of `func`: 310614d0ab18SJacob Faibussowitsch . ts - the `TS` context 3107000e7ae3SMatthew Knepley 3108000e7ae3SMatthew Knepley Level: intermediate 3109000e7ae3SMatthew Knepley 31101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPostStage()`, `TSSetPostStep()`, `TSStep()`, `TSRestartStep()` 3111000e7ae3SMatthew Knepley @*/ 311214d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStep(TS ts, PetscErrorCode (*func)(TS ts)) 3113d71ae5a4SJacob Faibussowitsch { 3114000e7ae3SMatthew Knepley PetscFunctionBegin; 31150700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3116ae60f76fSBarry Smith ts->prestep = func; 31173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3118000e7ae3SMatthew Knepley } 3119000e7ae3SMatthew Knepley 312009ee8438SJed Brown /*@ 3121bcf0153eSBarry Smith TSPreStep - Runs the user-defined pre-step function provided with `TSSetPreStep()` 31223f2090d5SJed Brown 3123c3339decSBarry Smith Collective 31243f2090d5SJed Brown 31252fe279fdSBarry Smith Input Parameter: 3126bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 31273f2090d5SJed Brown 31283f2090d5SJed Brown Level: developer 31293f2090d5SJed Brown 3130bcf0153eSBarry Smith Note: 3131bcf0153eSBarry Smith `TSPreStep()` is typically used within time stepping implementations, 3132bcf0153eSBarry Smith so most users would not generally call this routine themselves. 3133bcf0153eSBarry Smith 31341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSPreStage()`, `TSPostStage()`, `TSPostStep()` 31353f2090d5SJed Brown @*/ 3136d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStep(TS ts) 3137d71ae5a4SJacob Faibussowitsch { 31383f2090d5SJed Brown PetscFunctionBegin; 31390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3140ae60f76fSBarry Smith if (ts->prestep) { 31415efd42a4SStefano Zampini Vec U; 3142ef8d1ce0SJohann Rudi PetscObjectId idprev; 3143ef8d1ce0SJohann Rudi PetscBool sameObject; 31445efd42a4SStefano Zampini PetscObjectState sprev, spost; 31455efd42a4SStefano Zampini 31469566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 31479566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 31489566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 314925e27a38SBarry Smith PetscCallBack("TS callback preset", (*ts->prestep)(ts)); 31509566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 31519566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 31529566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 31539566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 3154312ce896SJed Brown } 31553ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 31563f2090d5SJed Brown } 31573f2090d5SJed Brown 3158b8123daeSJed Brown /*@C 3159b8123daeSJed Brown TSSetPreStage - Sets the general-purpose function 3160b8123daeSJed Brown called once at the beginning of each stage. 3161b8123daeSJed Brown 3162c3339decSBarry Smith Logically Collective 3163b8123daeSJed Brown 3164b8123daeSJed Brown Input Parameters: 3165bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3166b8123daeSJed Brown - func - The function 3167b8123daeSJed Brown 316820f4b53cSBarry Smith Calling sequence of `func`: 316914d0ab18SJacob Faibussowitsch + ts - the `TS` context 317014d0ab18SJacob Faibussowitsch - stagetime - the stage time 3171b8123daeSJed Brown 3172b8123daeSJed Brown Level: intermediate 3173b8123daeSJed Brown 3174b8123daeSJed Brown Note: 3175b8123daeSJed Brown There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3176bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3177bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 3178b8123daeSJed Brown 31791cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3180b8123daeSJed Brown @*/ 318114d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPreStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime)) 3182d71ae5a4SJacob Faibussowitsch { 3183b8123daeSJed Brown PetscFunctionBegin; 3184b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3185ae60f76fSBarry Smith ts->prestage = func; 31863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3187b8123daeSJed Brown } 3188b8123daeSJed Brown 31899be3e283SDebojyoti Ghosh /*@C 31900d56bcf9SIlya Fursov TSSetPostStage - Sets the general-purpose function 31919be3e283SDebojyoti Ghosh called once at the end of each stage. 31929be3e283SDebojyoti Ghosh 3193c3339decSBarry Smith Logically Collective 31949be3e283SDebojyoti Ghosh 31959be3e283SDebojyoti Ghosh Input Parameters: 3196bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 31979be3e283SDebojyoti Ghosh - func - The function 31989be3e283SDebojyoti Ghosh 319920f4b53cSBarry Smith Calling sequence of `func`: 320014d0ab18SJacob Faibussowitsch + ts - the `TS` context 320114d0ab18SJacob Faibussowitsch . stagetime - the stage time 320214d0ab18SJacob Faibussowitsch . stageindex - the stage index 320314d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 32049be3e283SDebojyoti Ghosh 32059be3e283SDebojyoti Ghosh Level: intermediate 32069be3e283SDebojyoti Ghosh 32079be3e283SDebojyoti Ghosh Note: 32089be3e283SDebojyoti Ghosh There may be several stages per time step. If the solve for a given stage fails, the step may be rejected and retried. 3209bcf0153eSBarry Smith The time step number being computed can be queried using `TSGetStepNumber()` and the total size of the step being 3210bcf0153eSBarry Smith attempted can be obtained using `TSGetTimeStep()`. The time at the start of the step is available via `TSGetTime()`. 32119be3e283SDebojyoti Ghosh 32121cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 32139be3e283SDebojyoti Ghosh @*/ 321414d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStage(TS ts, PetscErrorCode (*func)(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y)) 3215d71ae5a4SJacob Faibussowitsch { 32169be3e283SDebojyoti Ghosh PetscFunctionBegin; 32179be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32189be3e283SDebojyoti Ghosh ts->poststage = func; 32193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 32209be3e283SDebojyoti Ghosh } 32219be3e283SDebojyoti Ghosh 3222c688d042SShri Abhyankar /*@C 3223c688d042SShri Abhyankar TSSetPostEvaluate - Sets the general-purpose function 32240d56bcf9SIlya Fursov called at the end of each step evaluation. 3225c688d042SShri Abhyankar 3226c3339decSBarry Smith Logically Collective 3227c688d042SShri Abhyankar 3228c688d042SShri Abhyankar Input Parameters: 3229bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3230c688d042SShri Abhyankar - func - The function 3231c688d042SShri Abhyankar 323220f4b53cSBarry Smith Calling sequence of `func`: 323314d0ab18SJacob Faibussowitsch . ts - the `TS` context 3234c688d042SShri Abhyankar 3235c688d042SShri Abhyankar Level: intermediate 3236c688d042SShri Abhyankar 3237c688d042SShri Abhyankar Note: 32380d56bcf9SIlya Fursov The function set by `TSSetPostEvaluate()` is called after the solution is evaluated, or after the step rollback. 32390d56bcf9SIlya Fursov Inside the `func` callback, the solution vector can be obtained with `TSGetSolution()`, and modified, if need be. 32400d56bcf9SIlya Fursov The time step can be obtained with `TSGetTimeStep()`, and the time at the start of the step - via `TSGetTime()`. 32410d56bcf9SIlya Fursov The potential changes to the solution vector introduced by event handling (`postevent()`) are not relevant for `TSSetPostEvaluate()`, 32420d56bcf9SIlya Fursov but are relevant for `TSSetPostStep()`, according to the function call scheme in `TSSolve()`, as shown below 32430d56bcf9SIlya Fursov .vb 32440d56bcf9SIlya Fursov ... 32450d56bcf9SIlya Fursov Step() 32460d56bcf9SIlya Fursov PostEvaluate() 32470d56bcf9SIlya Fursov EventHandling() 32480d56bcf9SIlya Fursov step_rollback ? PostEvaluate() : PostStep() 32490d56bcf9SIlya Fursov ... 32500d56bcf9SIlya Fursov .ve 32510d56bcf9SIlya Fursov where EventHandling() may result in one of the following three outcomes 32520d56bcf9SIlya Fursov .vb 32530d56bcf9SIlya Fursov (1) | successful step | solution intact 32540d56bcf9SIlya Fursov (2) | successful step | solution modified by `postevent()` 32550d56bcf9SIlya Fursov (3) | step_rollback | solution rolled back 32560d56bcf9SIlya Fursov .ve 3257c688d042SShri Abhyankar 32581cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStage()`, `TSSetPreStep()`, `TSSetPostStep()`, `TSGetApplicationContext()` 3259c688d042SShri Abhyankar @*/ 326014d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostEvaluate(TS ts, PetscErrorCode (*func)(TS ts)) 3261d71ae5a4SJacob Faibussowitsch { 3262c688d042SShri Abhyankar PetscFunctionBegin; 3263c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3264c688d042SShri Abhyankar ts->postevaluate = func; 32653ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3266c688d042SShri Abhyankar } 3267c688d042SShri Abhyankar 3268b8123daeSJed Brown /*@ 3269bcf0153eSBarry Smith TSPreStage - Runs the user-defined pre-stage function set using `TSSetPreStage()` 3270b8123daeSJed Brown 3271c3339decSBarry Smith Collective 3272b8123daeSJed Brown 3273b8123daeSJed Brown Input Parameters: 327414d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 327514d0ab18SJacob Faibussowitsch - stagetime - The absolute time of the current stage 3276b8123daeSJed Brown 3277b8123daeSJed Brown Level: developer 3278b8123daeSJed Brown 3279bcf0153eSBarry Smith Note: 3280bcf0153eSBarry Smith `TSPreStage()` is typically used within time stepping implementations, 3281bcf0153eSBarry Smith most users would not generally call this routine themselves. 3282bcf0153eSBarry Smith 32831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPostStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3284b8123daeSJed Brown @*/ 3285d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPreStage(TS ts, PetscReal stagetime) 3286d71ae5a4SJacob Faibussowitsch { 3287b8123daeSJed Brown PetscFunctionBegin; 3288b8123daeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 32891e66621cSBarry Smith if (ts->prestage) PetscCallBack("TS callback prestage", (*ts->prestage)(ts, stagetime)); 32903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3291b8123daeSJed Brown } 3292b8123daeSJed Brown 32939be3e283SDebojyoti Ghosh /*@ 3294bcf0153eSBarry Smith TSPostStage - Runs the user-defined post-stage function set using `TSSetPostStage()` 32959be3e283SDebojyoti Ghosh 3296c3339decSBarry Smith Collective 32979be3e283SDebojyoti Ghosh 32989be3e283SDebojyoti Ghosh Input Parameters: 329914d0ab18SJacob Faibussowitsch + ts - The `TS` context obtained from `TSCreate()` 330014d0ab18SJacob Faibussowitsch . stagetime - The absolute time of the current stage 330114d0ab18SJacob Faibussowitsch . stageindex - Stage number 330214d0ab18SJacob Faibussowitsch - Y - Array of vectors (of size = total number of stages) with the stage solutions 33039be3e283SDebojyoti Ghosh 33049be3e283SDebojyoti Ghosh Level: developer 33059be3e283SDebojyoti Ghosh 3306bcf0153eSBarry Smith Note: 3307bcf0153eSBarry Smith `TSPostStage()` is typically used within time stepping implementations, 3308bcf0153eSBarry Smith most users would not generally call this routine themselves. 3309bcf0153eSBarry Smith 33101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSPreStage()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 33119be3e283SDebojyoti Ghosh @*/ 3312d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStage(TS ts, PetscReal stagetime, PetscInt stageindex, Vec *Y) 3313d71ae5a4SJacob Faibussowitsch { 33149be3e283SDebojyoti Ghosh PetscFunctionBegin; 33159be3e283SDebojyoti Ghosh PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 33161e66621cSBarry Smith if (ts->poststage) PetscCallBack("TS callback poststage", (*ts->poststage)(ts, stagetime, stageindex, Y)); 33173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 33189be3e283SDebojyoti Ghosh } 33199be3e283SDebojyoti Ghosh 3320c688d042SShri Abhyankar /*@ 3321bcf0153eSBarry Smith TSPostEvaluate - Runs the user-defined post-evaluate function set using `TSSetPostEvaluate()` 3322c688d042SShri Abhyankar 3323c3339decSBarry Smith Collective 3324c688d042SShri Abhyankar 33252fe279fdSBarry Smith Input Parameter: 3326bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 3327c688d042SShri Abhyankar 3328c688d042SShri Abhyankar Level: developer 3329c688d042SShri Abhyankar 3330bcf0153eSBarry Smith Note: 3331bcf0153eSBarry Smith `TSPostEvaluate()` is typically used within time stepping implementations, 3332bcf0153eSBarry Smith most users would not generally call this routine themselves. 3333bcf0153eSBarry Smith 33341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPostEvaluate()`, `TSSetPreStep()`, `TSPreStep()`, `TSPostStep()` 3335c688d042SShri Abhyankar @*/ 3336d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostEvaluate(TS ts) 3337d71ae5a4SJacob Faibussowitsch { 3338c688d042SShri Abhyankar PetscFunctionBegin; 3339c688d042SShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3340c688d042SShri Abhyankar if (ts->postevaluate) { 3341dcb233daSLisandro Dalcin Vec U; 3342dcb233daSLisandro Dalcin PetscObjectState sprev, spost; 3343dcb233daSLisandro Dalcin 33449566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 33459566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 334625e27a38SBarry Smith PetscCallBack("TS callback postevaluate", (*ts->postevaluate)(ts)); 33479566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 33489566063dSJacob Faibussowitsch if (sprev != spost) PetscCall(TSRestartStep(ts)); 3349c688d042SShri Abhyankar } 33503ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3351c688d042SShri Abhyankar } 3352c688d042SShri Abhyankar 3353ac226902SBarry Smith /*@C 3354000e7ae3SMatthew Knepley TSSetPostStep - Sets the general-purpose function 33550d56bcf9SIlya Fursov called once at the end of each successful time step. 3356000e7ae3SMatthew Knepley 3357c3339decSBarry Smith Logically Collective 3358000e7ae3SMatthew Knepley 3359000e7ae3SMatthew Knepley Input Parameters: 3360bcf0153eSBarry Smith + ts - The `TS` context obtained from `TSCreate()` 3361000e7ae3SMatthew Knepley - func - The function 3362000e7ae3SMatthew Knepley 336320f4b53cSBarry Smith Calling sequence of `func`: 336414d0ab18SJacob Faibussowitsch . ts - the `TS` context 3365000e7ae3SMatthew Knepley 3366000e7ae3SMatthew Knepley Level: intermediate 3367000e7ae3SMatthew Knepley 3368bcf0153eSBarry Smith Note: 33690d56bcf9SIlya Fursov The function set by `TSSetPostStep()` is called after each successful step. If the event handler locates an event at the 33700d56bcf9SIlya Fursov given step, and `postevent()` modifies the solution vector, the solution vector obtained by `TSGetSolution()` inside `func` will 33710d56bcf9SIlya Fursov contain the changes. To get the solution without these changes, use `TSSetPostEvaluate()` to set the appropriate callback. 33720d56bcf9SIlya Fursov The scheme of the relevant function calls in `TSSolve()` is shown below 33730d56bcf9SIlya Fursov .vb 33740d56bcf9SIlya Fursov ... 33750d56bcf9SIlya Fursov Step() 33760d56bcf9SIlya Fursov PostEvaluate() 33770d56bcf9SIlya Fursov EventHandling() 33780d56bcf9SIlya Fursov step_rollback ? PostEvaluate() : PostStep() 33790d56bcf9SIlya Fursov ... 33800d56bcf9SIlya Fursov .ve 33810d56bcf9SIlya Fursov where EventHandling() may result in one of the following three outcomes 33820d56bcf9SIlya Fursov .vb 33830d56bcf9SIlya Fursov (1) | successful step | solution intact 33840d56bcf9SIlya Fursov (2) | successful step | solution modified by `postevent()` 33850d56bcf9SIlya Fursov (3) | step_rollback | solution rolled back 33860d56bcf9SIlya Fursov .ve 3387bcf0153eSBarry Smith 33881cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSRestartStep()` 3389000e7ae3SMatthew Knepley @*/ 339014d0ab18SJacob Faibussowitsch PetscErrorCode TSSetPostStep(TS ts, PetscErrorCode (*func)(TS ts)) 3391d71ae5a4SJacob Faibussowitsch { 3392000e7ae3SMatthew Knepley PetscFunctionBegin; 33930700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3394ae60f76fSBarry Smith ts->poststep = func; 33953ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3396000e7ae3SMatthew Knepley } 3397000e7ae3SMatthew Knepley 339809ee8438SJed Brown /*@ 3399195e9b02SBarry Smith TSPostStep - Runs the user-defined post-step function that was set with `TSSetPostStep()` 34003f2090d5SJed Brown 3401c3339decSBarry Smith Collective 34023f2090d5SJed Brown 34032fe279fdSBarry Smith Input Parameter: 3404bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 34053f2090d5SJed Brown 3406bcf0153eSBarry Smith Note: 3407bcf0153eSBarry Smith `TSPostStep()` is typically used within time stepping implementations, 34083f2090d5SJed Brown so most users would not generally call this routine themselves. 34093f2090d5SJed Brown 34103f2090d5SJed Brown Level: developer 34113f2090d5SJed Brown 3412bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostEvaluate()`, `TSGetTimeStep()`, `TSGetStepNumber()`, `TSGetTime()`, `TSSetPostStep()` 34133f2090d5SJed Brown @*/ 3414d71ae5a4SJacob Faibussowitsch PetscErrorCode TSPostStep(TS ts) 3415d71ae5a4SJacob Faibussowitsch { 34163f2090d5SJed Brown PetscFunctionBegin; 34170700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3418ae60f76fSBarry Smith if (ts->poststep) { 34195efd42a4SStefano Zampini Vec U; 3420ef8d1ce0SJohann Rudi PetscObjectId idprev; 3421ef8d1ce0SJohann Rudi PetscBool sameObject; 34225efd42a4SStefano Zampini PetscObjectState sprev, spost; 34235efd42a4SStefano Zampini 34249566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 34259566063dSJacob Faibussowitsch PetscCall(PetscObjectGetId((PetscObject)U, &idprev)); 34269566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &sprev)); 342725e27a38SBarry Smith PetscCallBack("TS callback poststep", (*ts->poststep)(ts)); 34289566063dSJacob Faibussowitsch PetscCall(TSGetSolution(ts, &U)); 34299566063dSJacob Faibussowitsch PetscCall(PetscObjectCompareId((PetscObject)U, idprev, &sameObject)); 34309566063dSJacob Faibussowitsch PetscCall(PetscObjectStateGet((PetscObject)U, &spost)); 34319566063dSJacob Faibussowitsch if (!sameObject || sprev != spost) PetscCall(TSRestartStep(ts)); 343272ac3e02SJed Brown } 34333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 34343f2090d5SJed Brown } 34353f2090d5SJed Brown 3436cd652676SJed Brown /*@ 3437cd652676SJed Brown TSInterpolate - Interpolate the solution computed during the previous step to an arbitrary location in the interval 3438cd652676SJed Brown 3439c3339decSBarry Smith Collective 3440cd652676SJed Brown 34414165533cSJose E. Roman Input Parameters: 3442cd652676SJed Brown + ts - time stepping context 3443cd652676SJed Brown - t - time to interpolate to 3444cd652676SJed Brown 34454165533cSJose E. Roman Output Parameter: 34460910c330SBarry Smith . U - state at given time 3447cd652676SJed Brown 3448cd652676SJed Brown Level: intermediate 3449cd652676SJed Brown 3450b43aa488SJacob Faibussowitsch Developer Notes: 3451bcf0153eSBarry Smith `TSInterpolate()` and the storing of previous steps/stages should be generalized to support delay differential equations and continuous adjoints. 3452cd652676SJed Brown 34531cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetExactFinalTime()`, `TSSolve()` 3454cd652676SJed Brown @*/ 3455d71ae5a4SJacob Faibussowitsch PetscErrorCode TSInterpolate(TS ts, PetscReal t, Vec U) 3456d71ae5a4SJacob Faibussowitsch { 3457cd652676SJed Brown PetscFunctionBegin; 3458cd652676SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3459b06615a5SLisandro Dalcin PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 346063a3b9bcSJacob Faibussowitsch PetscCheck(t >= ts->ptime_prev && t <= ts->ptime, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Requested time %g not in last time steps [%g,%g]", (double)t, (double)ts->ptime_prev, (double)ts->ptime); 3461dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, interpolate, t, U); 34623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3463cd652676SJed Brown } 3464cd652676SJed Brown 3465d763cef2SBarry Smith /*@ 34666d9e5789SSean Farley TSStep - Steps one time step 3467d763cef2SBarry Smith 3468c3339decSBarry Smith Collective 3469d763cef2SBarry Smith 3470d763cef2SBarry Smith Input Parameter: 3471bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 3472d763cef2SBarry Smith 347327829d71SBarry Smith Level: developer 3474d763cef2SBarry Smith 3475b8123daeSJed Brown Notes: 3476bcf0153eSBarry Smith The public interface for the ODE/DAE solvers is `TSSolve()`, you should almost for sure be using that routine and not this routine. 347727829d71SBarry Smith 3478bcf0153eSBarry Smith The hook set using `TSSetPreStep()` is called before each attempt to take the step. In general, the time step size may 3479b8123daeSJed Brown be changed due to adaptive error controller or solve failures. Note that steps may contain multiple stages. 3480b8123daeSJed Brown 3481bcf0153eSBarry Smith This may over-step the final time provided in `TSSetMaxTime()` depending on the time-step used. `TSSolve()` interpolates to exactly the 3482bcf0153eSBarry Smith time provided in `TSSetMaxTime()`. One can use `TSInterpolate()` to determine an interpolated solution within the final timestep. 348325cb2221SBarry Smith 34841cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSSetPostStage()`, `TSInterpolate()` 3485d763cef2SBarry Smith @*/ 3486d71ae5a4SJacob Faibussowitsch PetscErrorCode TSStep(TS ts) 3487d71ae5a4SJacob Faibussowitsch { 3488fffbeea8SBarry Smith static PetscBool cite = PETSC_FALSE; 3489be5899b3SLisandro Dalcin PetscReal ptime; 3490d763cef2SBarry Smith 3491d763cef2SBarry Smith PetscFunctionBegin; 34920700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3493d0609cedSBarry Smith PetscCall(PetscCitationsRegister("@article{tspaper,\n" 3494fffbeea8SBarry Smith " title = {{PETSc/TS}: A Modern Scalable {DAE/ODE} Solver Library},\n" 3495f1d62c27SHong Zhang " author = {Abhyankar, Shrirang and Brown, Jed and Constantinescu, Emil and Ghosh, Debojyoti and Smith, Barry F. and Zhang, Hong},\n" 3496f1d62c27SHong Zhang " journal = {arXiv e-preprints},\n" 3497f1d62c27SHong Zhang " eprint = {1806.01437},\n" 3498f1d62c27SHong Zhang " archivePrefix = {arXiv},\n" 34999371c9d4SSatish Balay " year = {2018}\n}\n", 35009371c9d4SSatish Balay &cite)); 35019566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 35029566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 3503136cf249SJames Wright if (ts->eval_times) 3504136cf249SJames Wright ts->eval_times->worktol = 0; /* In each step of TSSolve() 'eval_times->worktol' will be meaningfully defined (later) only once: 3505ca4445c7SIlya Fursov in TSAdaptChoose() or TSEvent_dt_cap(), and then reused till the end of the step */ 3506d405a339SMatthew Knepley 35078e562f8dSJames Wright PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->run_steps != PETSC_INT_MAX || ts->max_steps != PETSC_INT_MAX, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime(), TSSetMaxSteps(), or TSSetRunSteps() or use -ts_max_time <time>, -ts_max_steps <steps>, -ts_run_steps <steps>"); 35083c633725SBarry Smith PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_UNSPECIFIED, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSStep()"); 35093c633725SBarry Smith PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP || ts->adapt, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 3510a6772fa2SLisandro Dalcin 3511c61711c8SStefano Zampini if (!ts->vec_sol0) PetscCall(VecDuplicate(ts->vec_sol, &ts->vec_sol0)); 3512c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol, ts->vec_sol0)); 3513c61711c8SStefano Zampini ts->time_step0 = ts->time_step; 3514c61711c8SStefano Zampini 3515be5899b3SLisandro Dalcin if (!ts->steps) ts->ptime_prev = ts->ptime; 35169371c9d4SSatish Balay ptime = ts->ptime; 3517c61711c8SStefano Zampini 35189371c9d4SSatish Balay ts->ptime_prev_rollback = ts->ptime_prev; 35192808aa04SLisandro Dalcin ts->reason = TS_CONVERGED_ITERATING; 3520fc8dbba5SLisandro Dalcin 35219566063dSJacob Faibussowitsch PetscCall(PetscLogEventBegin(TS_Step, ts, 0, 0, 0)); 3522dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, step); 35239566063dSJacob Faibussowitsch PetscCall(PetscLogEventEnd(TS_Step, ts, 0, 0, 0)); 3524fc8dbba5SLisandro Dalcin 3525fc8dbba5SLisandro Dalcin if (ts->reason >= 0) { 3526be5899b3SLisandro Dalcin ts->ptime_prev = ptime; 35272808aa04SLisandro Dalcin ts->steps++; 3528be5899b3SLisandro Dalcin ts->steprollback = PETSC_FALSE; 352928d5b5d6SLisandro Dalcin ts->steprestart = PETSC_FALSE; 3530c6bf8827SStefano Zampini ts->stepresize = PETSC_FALSE; 3531d2daff3dSHong Zhang } 3532fc8dbba5SLisandro Dalcin 35335c9bbc89SJed Brown if (ts->reason < 0 && ts->errorifstepfailed) { 35345c9bbc89SJed Brown PetscCall(TSMonitorCancel(ts)); 353509cb0f53SBarry Smith PetscCheck(ts->reason != TS_DIVERGED_NONLINEAR_SOLVE, PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s, increase -ts_max_snes_failures or use unlimited to attempt recovery", TSConvergedReasons[ts->reason]); 35365c9bbc89SJed Brown SETERRQ(PetscObjectComm((PetscObject)ts), PETSC_ERR_NOT_CONVERGED, "TSStep has failed due to %s", TSConvergedReasons[ts->reason]); 35375c9bbc89SJed Brown } 35383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 353908c7845fSBarry Smith } 354008c7845fSBarry Smith 354108c7845fSBarry Smith /*@ 35427cbde773SLisandro Dalcin TSEvaluateWLTE - Evaluate the weighted local truncation error norm 35437cbde773SLisandro Dalcin at the end of a time step with a given order of accuracy. 35447cbde773SLisandro Dalcin 3545c3339decSBarry Smith Collective 35467cbde773SLisandro Dalcin 35474165533cSJose E. Roman Input Parameters: 35487cbde773SLisandro Dalcin + ts - time stepping context 3549bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 35507cbde773SLisandro Dalcin 355197bb3fdcSJose E. Roman Input/Output Parameter: 3552bcf0153eSBarry Smith . order - optional, desired order for the error evaluation or `PETSC_DECIDE`; 355397bb3fdcSJose E. Roman on output, the actual order of the error evaluation 355497bb3fdcSJose E. Roman 355597bb3fdcSJose E. Roman Output Parameter: 355697bb3fdcSJose E. Roman . wlte - the weighted local truncation error norm 35577cbde773SLisandro Dalcin 35587cbde773SLisandro Dalcin Level: advanced 35597cbde773SLisandro Dalcin 3560bcf0153eSBarry Smith Note: 35617cbde773SLisandro Dalcin If the timestepper cannot evaluate the error in a particular step 35627cbde773SLisandro Dalcin (eg. in the first step or restart steps after event handling), 35637cbde773SLisandro Dalcin this routine returns wlte=-1.0 . 35647cbde773SLisandro Dalcin 35651cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt`, `TSErrorWeightedNorm()` 35667cbde773SLisandro Dalcin @*/ 3567d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateWLTE(TS ts, NormType wnormtype, PetscInt *order, PetscReal *wlte) 3568d71ae5a4SJacob Faibussowitsch { 35697cbde773SLisandro Dalcin PetscFunctionBegin; 35707cbde773SLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 35717cbde773SLisandro Dalcin PetscValidType(ts, 1); 3572064a246eSJacob Faibussowitsch PetscValidLogicalCollectiveEnum(ts, wnormtype, 2); 35734f572ea9SToby Isaac if (order) PetscAssertPointer(order, 3); 35747cbde773SLisandro Dalcin if (order) PetscValidLogicalCollectiveInt(ts, *order, 3); 35754f572ea9SToby Isaac PetscAssertPointer(wlte, 4); 35763c633725SBarry Smith PetscCheck(wnormtype == NORM_2 || wnormtype == NORM_INFINITY, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No support for norm type %s", NormTypes[wnormtype]); 3577dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatewlte, wnormtype, order, wlte); 35783ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 35797cbde773SLisandro Dalcin } 35807cbde773SLisandro Dalcin 358105175c85SJed Brown /*@ 358205175c85SJed Brown TSEvaluateStep - Evaluate the solution at the end of a time step with a given order of accuracy. 358305175c85SJed Brown 3584c3339decSBarry Smith Collective 358505175c85SJed Brown 35864165533cSJose E. Roman Input Parameters: 35871c3436cfSJed Brown + ts - time stepping context 35881c3436cfSJed Brown . order - desired order of accuracy 3589195e9b02SBarry Smith - done - whether the step was evaluated at this order (pass `NULL` to generate an error if not available) 359005175c85SJed Brown 35914165533cSJose E. Roman Output Parameter: 35920910c330SBarry Smith . U - state at the end of the current step 359305175c85SJed Brown 359405175c85SJed Brown Level: advanced 359505175c85SJed Brown 3596108c343cSJed Brown Notes: 3597108c343cSJed Brown This function cannot be called until all stages have been evaluated. 3598108c343cSJed Brown 3599bcf0153eSBarry Smith It is normally called by adaptive controllers before a step has been accepted and may also be called by the user after `TSStep()` has returned. 3600bcf0153eSBarry Smith 36011cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSStep()`, `TSAdapt` 360205175c85SJed Brown @*/ 3603d71ae5a4SJacob Faibussowitsch PetscErrorCode TSEvaluateStep(TS ts, PetscInt order, Vec U, PetscBool *done) 3604d71ae5a4SJacob Faibussowitsch { 360505175c85SJed Brown PetscFunctionBegin; 360605175c85SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 360705175c85SJed Brown PetscValidType(ts, 1); 36080910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 3); 3609dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, evaluatestep, order, U, done); 36103ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 361105175c85SJed Brown } 361205175c85SJed Brown 3613aad739acSMatthew G. Knepley /*@C 36142e61be88SMatthew G. Knepley TSGetComputeInitialCondition - Get the function used to automatically compute an initial condition for the timestepping. 3615aad739acSMatthew G. Knepley 3616aad739acSMatthew G. Knepley Not collective 3617aad739acSMatthew G. Knepley 36184165533cSJose E. Roman Input Parameter: 3619aad739acSMatthew G. Knepley . ts - time stepping context 3620aad739acSMatthew G. Knepley 36214165533cSJose E. Roman Output Parameter: 3622b43aa488SJacob Faibussowitsch . initCondition - The function which computes an initial condition 3623aad739acSMatthew G. Knepley 362420f4b53cSBarry Smith Calling sequence of `initCondition`: 362520f4b53cSBarry Smith + ts - The timestepping context 362620f4b53cSBarry Smith - u - The input vector in which the initial condition is stored 3627bcf0153eSBarry Smith 3628aad739acSMatthew G. Knepley Level: advanced 3629aad739acSMatthew G. Knepley 36301cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetComputeInitialCondition()`, `TSComputeInitialCondition()` 3631aad739acSMatthew G. Knepley @*/ 363214d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeInitialCondition(TS ts, PetscErrorCode (**initCondition)(TS ts, Vec u)) 3633d71ae5a4SJacob Faibussowitsch { 3634aad739acSMatthew G. Knepley PetscFunctionBegin; 3635aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36364f572ea9SToby Isaac PetscAssertPointer(initCondition, 2); 36372e61be88SMatthew G. Knepley *initCondition = ts->ops->initcondition; 36383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3639aad739acSMatthew G. Knepley } 3640aad739acSMatthew G. Knepley 3641aad739acSMatthew G. Knepley /*@C 36422e61be88SMatthew G. Knepley TSSetComputeInitialCondition - Set the function used to automatically compute an initial condition for the timestepping. 3643aad739acSMatthew G. Knepley 3644c3339decSBarry Smith Logically collective 3645aad739acSMatthew G. Knepley 36464165533cSJose E. Roman Input Parameters: 3647aad739acSMatthew G. Knepley + ts - time stepping context 36482e61be88SMatthew G. Knepley - initCondition - The function which computes an initial condition 3649aad739acSMatthew G. Knepley 365020f4b53cSBarry Smith Calling sequence of `initCondition`: 3651a96d6ef6SBarry Smith + ts - The timestepping context 365214d0ab18SJacob Faibussowitsch - e - The input vector in which the initial condition is to be stored 3653aad739acSMatthew G. Knepley 3654bcf0153eSBarry Smith Level: advanced 3655bcf0153eSBarry Smith 36561cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSComputeInitialCondition()` 3657aad739acSMatthew G. Knepley @*/ 365814d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeInitialCondition(TS ts, PetscErrorCode (*initCondition)(TS ts, Vec e)) 3659d71ae5a4SJacob Faibussowitsch { 3660aad739acSMatthew G. Knepley PetscFunctionBegin; 3661aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 36622e61be88SMatthew G. Knepley PetscValidFunction(initCondition, 2); 36632e61be88SMatthew G. Knepley ts->ops->initcondition = initCondition; 36643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3665aad739acSMatthew G. Knepley } 3666aad739acSMatthew G. Knepley 3667aad739acSMatthew G. Knepley /*@ 3668bcf0153eSBarry Smith TSComputeInitialCondition - Compute an initial condition for the timestepping using the function previously set with `TSSetComputeInitialCondition()` 3669aad739acSMatthew G. Knepley 3670c3339decSBarry Smith Collective 3671aad739acSMatthew G. Knepley 36724165533cSJose E. Roman Input Parameters: 3673aad739acSMatthew G. Knepley + ts - time stepping context 3674bcf0153eSBarry Smith - u - The `Vec` to store the condition in which will be used in `TSSolve()` 3675aad739acSMatthew G. Knepley 3676aad739acSMatthew G. Knepley Level: advanced 3677aad739acSMatthew G. Knepley 36781cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3679aad739acSMatthew G. Knepley @*/ 3680d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeInitialCondition(TS ts, Vec u) 3681d71ae5a4SJacob Faibussowitsch { 3682aad739acSMatthew G. Knepley PetscFunctionBegin; 3683aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3684aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3685dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, initcondition, u); 36863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3687aad739acSMatthew G. Knepley } 3688aad739acSMatthew G. Knepley 3689aad739acSMatthew G. Knepley /*@C 3690aad739acSMatthew G. Knepley TSGetComputeExactError - Get the function used to automatically compute the exact error for the timestepping. 3691aad739acSMatthew G. Knepley 3692aad739acSMatthew G. Knepley Not collective 3693aad739acSMatthew G. Knepley 36944165533cSJose E. Roman Input Parameter: 3695aad739acSMatthew G. Knepley . ts - time stepping context 3696aad739acSMatthew G. Knepley 36974165533cSJose E. Roman Output Parameter: 3698aad739acSMatthew G. Knepley . exactError - The function which computes the solution error 3699aad739acSMatthew G. Knepley 370020f4b53cSBarry Smith Calling sequence of `exactError`: 3701a96d6ef6SBarry Smith + ts - The timestepping context 3702a96d6ef6SBarry Smith . u - The approximate solution vector 370320f4b53cSBarry Smith - e - The vector in which the error is stored 3704aad739acSMatthew G. Knepley 3705bcf0153eSBarry Smith Level: advanced 3706bcf0153eSBarry Smith 370742747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSComputeExactError()` 3708aad739acSMatthew G. Knepley @*/ 370914d0ab18SJacob Faibussowitsch PetscErrorCode TSGetComputeExactError(TS ts, PetscErrorCode (**exactError)(TS ts, Vec u, Vec e)) 3710d71ae5a4SJacob Faibussowitsch { 3711aad739acSMatthew G. Knepley PetscFunctionBegin; 3712aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 37134f572ea9SToby Isaac PetscAssertPointer(exactError, 2); 3714aad739acSMatthew G. Knepley *exactError = ts->ops->exacterror; 37153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3716aad739acSMatthew G. Knepley } 3717aad739acSMatthew G. Knepley 3718aad739acSMatthew G. Knepley /*@C 3719aad739acSMatthew G. Knepley TSSetComputeExactError - Set the function used to automatically compute the exact error for the timestepping. 3720aad739acSMatthew G. Knepley 3721c3339decSBarry Smith Logically collective 3722aad739acSMatthew G. Knepley 37234165533cSJose E. Roman Input Parameters: 3724aad739acSMatthew G. Knepley + ts - time stepping context 3725aad739acSMatthew G. Knepley - exactError - The function which computes the solution error 3726aad739acSMatthew G. Knepley 372720f4b53cSBarry Smith Calling sequence of `exactError`: 3728a96d6ef6SBarry Smith + ts - The timestepping context 3729a96d6ef6SBarry Smith . u - The approximate solution vector 373020f4b53cSBarry Smith - e - The vector in which the error is stored 3731aad739acSMatthew G. Knepley 3732bcf0153eSBarry Smith Level: advanced 3733bcf0153eSBarry Smith 37341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeExactError()`, `TSComputeExactError()` 3735aad739acSMatthew G. Knepley @*/ 373614d0ab18SJacob Faibussowitsch PetscErrorCode TSSetComputeExactError(TS ts, PetscErrorCode (*exactError)(TS ts, Vec u, Vec e)) 3737d71ae5a4SJacob Faibussowitsch { 3738aad739acSMatthew G. Knepley PetscFunctionBegin; 3739aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3740f907fdbfSMatthew G. Knepley PetscValidFunction(exactError, 2); 3741aad739acSMatthew G. Knepley ts->ops->exacterror = exactError; 37423ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3743aad739acSMatthew G. Knepley } 3744aad739acSMatthew G. Knepley 3745aad739acSMatthew G. Knepley /*@ 3746bcf0153eSBarry Smith TSComputeExactError - Compute the solution error for the timestepping using the function previously set with `TSSetComputeExactError()` 3747aad739acSMatthew G. Knepley 3748c3339decSBarry Smith Collective 3749aad739acSMatthew G. Knepley 37504165533cSJose E. Roman Input Parameters: 3751aad739acSMatthew G. Knepley + ts - time stepping context 3752aad739acSMatthew G. Knepley . u - The approximate solution 3753bcf0153eSBarry Smith - e - The `Vec` used to store the error 3754aad739acSMatthew G. Knepley 3755aad739acSMatthew G. Knepley Level: advanced 3756aad739acSMatthew G. Knepley 37571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetComputeInitialCondition()`, `TSSetComputeInitialCondition()`, `TSSolve()` 3758aad739acSMatthew G. Knepley @*/ 3759d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeExactError(TS ts, Vec u, Vec e) 3760d71ae5a4SJacob Faibussowitsch { 3761aad739acSMatthew G. Knepley PetscFunctionBegin; 3762aad739acSMatthew G. Knepley PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3763aad739acSMatthew G. Knepley PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 3764aad739acSMatthew G. Knepley PetscValidHeaderSpecific(e, VEC_CLASSID, 3); 3765dbbe0bcdSBarry Smith PetscTryTypeMethod(ts, exacterror, u, e); 37663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 3767aad739acSMatthew G. Knepley } 3768aad739acSMatthew G. Knepley 37696bd3a4fdSStefano Zampini /*@C 37706bd3a4fdSStefano Zampini TSSetResize - Sets the resize callbacks. 37716bd3a4fdSStefano Zampini 37726bd3a4fdSStefano Zampini Logically Collective 37736bd3a4fdSStefano Zampini 37746bd3a4fdSStefano Zampini Input Parameters: 37756bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3776ecc87898SStefano Zampini . rollback - Whether a resize will restart the step 37776bd3a4fdSStefano Zampini . setup - The setup function 377814d0ab18SJacob Faibussowitsch . transfer - The transfer function 377914d0ab18SJacob Faibussowitsch - ctx - [optional] The user-defined context 37806bd3a4fdSStefano Zampini 37816bd3a4fdSStefano Zampini Calling sequence of `setup`: 37828847d985SBarry Smith + ts - the `TS` context 37836bd3a4fdSStefano Zampini . step - the current step 37846bd3a4fdSStefano Zampini . time - the current time 37856bd3a4fdSStefano Zampini . state - the current vector of state 37866bd3a4fdSStefano Zampini . resize - (output parameter) `PETSC_TRUE` if need resizing, `PETSC_FALSE` otherwise 37876bd3a4fdSStefano Zampini - ctx - user defined context 37886bd3a4fdSStefano Zampini 37896bd3a4fdSStefano Zampini Calling sequence of `transfer`: 37908847d985SBarry Smith + ts - the `TS` context 37916bd3a4fdSStefano Zampini . nv - the number of vectors to be transferred 37926bd3a4fdSStefano Zampini . vecsin - array of vectors to be transferred 37936bd3a4fdSStefano Zampini . vecsout - array of transferred vectors 37946bd3a4fdSStefano Zampini - ctx - user defined context 37956bd3a4fdSStefano Zampini 37966bd3a4fdSStefano Zampini Notes: 3797ecc87898SStefano Zampini The `setup` function is called inside `TSSolve()` after `TSEventHandler()` or after `TSPostStep()` 3798ecc87898SStefano Zampini depending on the `rollback` value: if `rollback` is true, then these callbacks behave as error indicators 3799ecc87898SStefano Zampini and will flag the need to remesh and restart the current step. Otherwise, they will simply flag the solver 3800ecc87898SStefano Zampini that the size of the discrete problem has changed. 3801ecc87898SStefano Zampini In both cases, the solver will collect the needed vectors that will be 3802ecc87898SStefano Zampini transferred from the old to the new sizes using the `transfer` callback. These vectors will include the 3803ecc87898SStefano Zampini current solution vector, and other vectors needed by the specific solver used. 38046bd3a4fdSStefano Zampini For example, `TSBDF` uses previous solutions vectors to solve for the next time step. 38056bd3a4fdSStefano Zampini Other application specific objects associated with the solver, i.e. Jacobian matrices and `DM`, 38066bd3a4fdSStefano Zampini will be automatically reset if the sizes are changed and they must be specified again by the user 38076bd3a4fdSStefano Zampini inside the `transfer` function. 38086bd3a4fdSStefano Zampini The input and output arrays passed to `transfer` are allocated by PETSc. 38096bd3a4fdSStefano Zampini Vectors in `vecsout` must be created by the user. 38106bd3a4fdSStefano Zampini Ownership of vectors in `vecsout` is transferred to PETSc. 38116bd3a4fdSStefano Zampini 38126bd3a4fdSStefano Zampini Level: advanced 38136bd3a4fdSStefano Zampini 38146bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetDM()`, `TSSetIJacobian()`, `TSSetRHSJacobian()` 38156bd3a4fdSStefano Zampini @*/ 3816ecc87898SStefano Zampini PetscErrorCode TSSetResize(TS ts, PetscBool rollback, PetscErrorCode (*setup)(TS ts, PetscInt step, PetscReal time, Vec state, PetscBool *resize, void *ctx), PetscErrorCode (*transfer)(TS ts, PetscInt nv, Vec vecsin[], Vec vecsout[], void *ctx), void *ctx) 38176bd3a4fdSStefano Zampini { 38186bd3a4fdSStefano Zampini PetscFunctionBegin; 38196bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3820ecc87898SStefano Zampini ts->resizerollback = rollback; 38216bd3a4fdSStefano Zampini ts->resizesetup = setup; 38226bd3a4fdSStefano Zampini ts->resizetransfer = transfer; 38236bd3a4fdSStefano Zampini ts->resizectx = ctx; 38246bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38256bd3a4fdSStefano Zampini } 38266bd3a4fdSStefano Zampini 382714d0ab18SJacob Faibussowitsch /* 38286bd3a4fdSStefano Zampini TSResizeRegisterOrRetrieve - Register or import vectors transferred with `TSResize()`. 38296bd3a4fdSStefano Zampini 38306bd3a4fdSStefano Zampini Collective 38316bd3a4fdSStefano Zampini 38326bd3a4fdSStefano Zampini Input Parameters: 38336bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 38346bd3a4fdSStefano Zampini - flg - If `PETSC_TRUE` each TS implementation (e.g. `TSBDF`) will register vectors to be transferred, if `PETSC_FALSE` vectors will be imported from transferred vectors. 38356bd3a4fdSStefano Zampini 38366bd3a4fdSStefano Zampini Level: developer 38376bd3a4fdSStefano Zampini 38386bd3a4fdSStefano Zampini Note: 38396bd3a4fdSStefano Zampini `TSResizeRegisterOrRetrieve()` is declared PETSC_INTERN since it is 38406bd3a4fdSStefano Zampini used within time stepping implementations, 38416bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 38426bd3a4fdSStefano Zampini 38436bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 38446bd3a4fdSStefano Zampini @*/ 384514d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeRegisterOrRetrieve(TS ts, PetscBool flg) 38466bd3a4fdSStefano Zampini { 38476bd3a4fdSStefano Zampini PetscFunctionBegin; 38486bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38496bd3a4fdSStefano Zampini PetscTryTypeMethod(ts, resizeregister, flg); 38506bd3a4fdSStefano Zampini /* PetscTryTypeMethod(adapt, resizeregister, flg); */ 38516bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38526bd3a4fdSStefano Zampini } 38536bd3a4fdSStefano Zampini 385414d0ab18SJacob Faibussowitsch static PetscErrorCode TSResizeReset(TS ts) 38556bd3a4fdSStefano Zampini { 38566bd3a4fdSStefano Zampini PetscFunctionBegin; 38576bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38586bd3a4fdSStefano Zampini PetscCall(PetscObjectListDestroy(&ts->resizetransferobjs)); 38596bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38606bd3a4fdSStefano Zampini } 38616bd3a4fdSStefano Zampini 38626bd3a4fdSStefano Zampini static PetscErrorCode TSResizeTransferVecs(TS ts, PetscInt cnt, Vec vecsin[], Vec vecsout[]) 38636bd3a4fdSStefano Zampini { 38646bd3a4fdSStefano Zampini PetscFunctionBegin; 38656bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38666bd3a4fdSStefano Zampini PetscValidLogicalCollectiveInt(ts, cnt, 2); 38676bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPush(vecsin[i])); 38686bd3a4fdSStefano Zampini if (ts->resizetransfer) { 38696bd3a4fdSStefano Zampini PetscCall(PetscInfo(ts, "Transferring %" PetscInt_FMT " vectors\n", cnt)); 38706bd3a4fdSStefano Zampini PetscCallBack("TS callback resize transfer", (*ts->resizetransfer)(ts, cnt, vecsin, vecsout, ts->resizectx)); 38716bd3a4fdSStefano Zampini } 38726bd3a4fdSStefano Zampini for (PetscInt i = 0; i < cnt; i++) PetscCall(VecLockReadPop(vecsin[i])); 38736bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 38746bd3a4fdSStefano Zampini } 38756bd3a4fdSStefano Zampini 38766bd3a4fdSStefano Zampini /*@C 38776bd3a4fdSStefano Zampini TSResizeRegisterVec - Register a vector to be transferred with `TSResize()`. 38786bd3a4fdSStefano Zampini 38796bd3a4fdSStefano Zampini Collective 38806bd3a4fdSStefano Zampini 38816bd3a4fdSStefano Zampini Input Parameters: 38826bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3883baca6076SPierre Jolivet . name - A string identifying the vector 38846bd3a4fdSStefano Zampini - vec - The vector 38856bd3a4fdSStefano Zampini 38866bd3a4fdSStefano Zampini Level: developer 38876bd3a4fdSStefano Zampini 38886bd3a4fdSStefano Zampini Note: 38896bd3a4fdSStefano Zampini `TSResizeRegisterVec()` is typically used within time stepping implementations, 38906bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 38916bd3a4fdSStefano Zampini 38926bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRetrieveVec()` 38936bd3a4fdSStefano Zampini @*/ 3894cc4c1da9SBarry Smith PetscErrorCode TSResizeRegisterVec(TS ts, const char name[], Vec vec) 38956bd3a4fdSStefano Zampini { 38966bd3a4fdSStefano Zampini PetscFunctionBegin; 38976bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 38984f572ea9SToby Isaac PetscAssertPointer(name, 2); 38996bd3a4fdSStefano Zampini if (vec) PetscValidHeaderSpecific(vec, VEC_CLASSID, 3); 39006bd3a4fdSStefano Zampini PetscCall(PetscObjectListAdd(&ts->resizetransferobjs, name, (PetscObject)vec)); 39016bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39026bd3a4fdSStefano Zampini } 39036bd3a4fdSStefano Zampini 39046bd3a4fdSStefano Zampini /*@C 39056bd3a4fdSStefano Zampini TSResizeRetrieveVec - Retrieve a vector registered with `TSResizeRegisterVec()`. 39066bd3a4fdSStefano Zampini 39076bd3a4fdSStefano Zampini Collective 39086bd3a4fdSStefano Zampini 39096bd3a4fdSStefano Zampini Input Parameters: 39106bd3a4fdSStefano Zampini + ts - The `TS` context obtained from `TSCreate()` 3911baca6076SPierre Jolivet . name - A string identifying the vector 39126bd3a4fdSStefano Zampini - vec - The vector 39136bd3a4fdSStefano Zampini 39146bd3a4fdSStefano Zampini Level: developer 39156bd3a4fdSStefano Zampini 39166bd3a4fdSStefano Zampini Note: 39176bd3a4fdSStefano Zampini `TSResizeRetrieveVec()` is typically used within time stepping implementations, 39186bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 39196bd3a4fdSStefano Zampini 39206bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()`, `TSResize()`, `TSResizeRegisterVec()` 39216bd3a4fdSStefano Zampini @*/ 3922cc4c1da9SBarry Smith PetscErrorCode TSResizeRetrieveVec(TS ts, const char name[], Vec *vec) 39236bd3a4fdSStefano Zampini { 39246bd3a4fdSStefano Zampini PetscFunctionBegin; 39256bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 39264f572ea9SToby Isaac PetscAssertPointer(name, 2); 39274f572ea9SToby Isaac PetscAssertPointer(vec, 3); 39286bd3a4fdSStefano Zampini PetscCall(PetscObjectListFind(ts->resizetransferobjs, name, (PetscObject *)vec)); 39296bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39306bd3a4fdSStefano Zampini } 39316bd3a4fdSStefano Zampini 39326bd3a4fdSStefano Zampini static PetscErrorCode TSResizeGetVecArray(TS ts, PetscInt *nv, const char **names[], Vec *vecs[]) 39336bd3a4fdSStefano Zampini { 39346bd3a4fdSStefano Zampini PetscInt cnt; 39356bd3a4fdSStefano Zampini PetscObjectList tmp; 39366bd3a4fdSStefano Zampini Vec *vecsin = NULL; 39376bd3a4fdSStefano Zampini const char **namesin = NULL; 39386bd3a4fdSStefano Zampini 39396bd3a4fdSStefano Zampini PetscFunctionBegin; 39406bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) 39416bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) cnt++; 394203ba6ac9SBarry Smith if (names) PetscCall(PetscMalloc1(cnt, &namesin)); 394303ba6ac9SBarry Smith if (vecs) PetscCall(PetscMalloc1(cnt, &vecsin)); 39446bd3a4fdSStefano Zampini for (tmp = ts->resizetransferobjs, cnt = 0; tmp; tmp = tmp->next) { 39456bd3a4fdSStefano Zampini if (tmp->obj && tmp->obj->classid == VEC_CLASSID) { 39466bd3a4fdSStefano Zampini if (vecs) vecsin[cnt] = (Vec)tmp->obj; 39476bd3a4fdSStefano Zampini if (names) namesin[cnt] = tmp->name; 39486bd3a4fdSStefano Zampini cnt++; 39496bd3a4fdSStefano Zampini } 39506bd3a4fdSStefano Zampini } 39516bd3a4fdSStefano Zampini if (nv) *nv = cnt; 39526bd3a4fdSStefano Zampini if (names) *names = namesin; 39536bd3a4fdSStefano Zampini if (vecs) *vecs = vecsin; 39546bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 39556bd3a4fdSStefano Zampini } 39566bd3a4fdSStefano Zampini 39576bd3a4fdSStefano Zampini /*@ 39586bd3a4fdSStefano Zampini TSResize - Runs the user-defined transfer functions provided with `TSSetResize()` 39596bd3a4fdSStefano Zampini 39606bd3a4fdSStefano Zampini Collective 39616bd3a4fdSStefano Zampini 39626bd3a4fdSStefano Zampini Input Parameter: 39636bd3a4fdSStefano Zampini . ts - The `TS` context obtained from `TSCreate()` 39646bd3a4fdSStefano Zampini 39656bd3a4fdSStefano Zampini Level: developer 39666bd3a4fdSStefano Zampini 39676bd3a4fdSStefano Zampini Note: 39686bd3a4fdSStefano Zampini `TSResize()` is typically used within time stepping implementations, 39696bd3a4fdSStefano Zampini so most users would not generally call this routine themselves. 39706bd3a4fdSStefano Zampini 39716bd3a4fdSStefano Zampini .seealso: [](ch_ts), `TS`, `TSSetResize()` 39726bd3a4fdSStefano Zampini @*/ 39736bd3a4fdSStefano Zampini PetscErrorCode TSResize(TS ts) 39746bd3a4fdSStefano Zampini { 39756bd3a4fdSStefano Zampini PetscInt nv = 0; 39766bd3a4fdSStefano Zampini const char **names = NULL; 39776bd3a4fdSStefano Zampini Vec *vecsin = NULL; 39786bd3a4fdSStefano Zampini const char *solname = "ts:vec_sol"; 39796bd3a4fdSStefano Zampini 39806bd3a4fdSStefano Zampini PetscFunctionBegin; 39816bd3a4fdSStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 3982ecc87898SStefano Zampini if (!ts->resizesetup) PetscFunctionReturn(PETSC_SUCCESS); 39836bd3a4fdSStefano Zampini if (ts->resizesetup) { 39846bd3a4fdSStefano Zampini PetscCall(VecLockReadPush(ts->vec_sol)); 3985c6bf8827SStefano Zampini PetscCallBack("TS callback resize setup", (*ts->resizesetup)(ts, ts->steps, ts->ptime, ts->vec_sol, &ts->stepresize, ts->resizectx)); 39866bd3a4fdSStefano Zampini PetscCall(VecLockReadPop(ts->vec_sol)); 3987c6bf8827SStefano Zampini if (ts->stepresize) { 3988ecc87898SStefano Zampini if (ts->resizerollback) { 3989ecc87898SStefano Zampini PetscCall(TSRollBack(ts)); 3990ecc87898SStefano Zampini ts->time_step = ts->time_step0; 3991ecc87898SStefano Zampini } 39926bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, solname, ts->vec_sol)); 39936bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_TRUE)); /* specific impls register their own objects */ 39946bd3a4fdSStefano Zampini } 39956bd3a4fdSStefano Zampini } 39966bd3a4fdSStefano Zampini 39976bd3a4fdSStefano Zampini PetscCall(TSResizeGetVecArray(ts, &nv, &names, &vecsin)); 39986bd3a4fdSStefano Zampini if (nv) { 39996bd3a4fdSStefano Zampini Vec *vecsout, vecsol; 40006bd3a4fdSStefano Zampini 40016bd3a4fdSStefano Zampini /* Reset internal objects */ 40026bd3a4fdSStefano Zampini PetscCall(TSReset(ts)); 40036bd3a4fdSStefano Zampini 4004ecc87898SStefano Zampini /* Transfer needed vectors (users can call SetJacobian, SetDM, etc. here) */ 40056bd3a4fdSStefano Zampini PetscCall(PetscCalloc1(nv, &vecsout)); 40066bd3a4fdSStefano Zampini PetscCall(TSResizeTransferVecs(ts, nv, vecsin, vecsout)); 40076bd3a4fdSStefano Zampini for (PetscInt i = 0; i < nv; i++) { 4008ecc87898SStefano Zampini const char *name; 4009ecc87898SStefano Zampini char *oname; 4010ecc87898SStefano Zampini 4011ecc87898SStefano Zampini PetscCall(PetscObjectGetName((PetscObject)vecsin[i], &name)); 4012ecc87898SStefano Zampini PetscCall(PetscStrallocpy(name, &oname)); 40136bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterVec(ts, names[i], vecsout[i])); 4014ecc87898SStefano Zampini if (vecsout[i]) PetscCall(PetscObjectSetName((PetscObject)vecsout[i], oname)); 4015ecc87898SStefano Zampini PetscCall(PetscFree(oname)); 40166bd3a4fdSStefano Zampini PetscCall(VecDestroy(&vecsout[i])); 40176bd3a4fdSStefano Zampini } 40186bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsout)); 40196bd3a4fdSStefano Zampini PetscCall(TSResizeRegisterOrRetrieve(ts, PETSC_FALSE)); /* specific impls import the transferred objects */ 40206bd3a4fdSStefano Zampini 40216bd3a4fdSStefano Zampini PetscCall(TSResizeRetrieveVec(ts, solname, &vecsol)); 40226bd3a4fdSStefano Zampini if (vecsol) PetscCall(TSSetSolution(ts, vecsol)); 40236bd3a4fdSStefano Zampini PetscAssert(ts->vec_sol, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_NULL, "Missing TS solution"); 40246bd3a4fdSStefano Zampini } 40256bd3a4fdSStefano Zampini 40266bd3a4fdSStefano Zampini PetscCall(PetscFree(names)); 40276bd3a4fdSStefano Zampini PetscCall(PetscFree(vecsin)); 40286bd3a4fdSStefano Zampini PetscCall(TSResizeReset(ts)); 40296bd3a4fdSStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 40306bd3a4fdSStefano Zampini } 40316bd3a4fdSStefano Zampini 4032b1cb36f3SHong Zhang /*@ 40336a4d4014SLisandro Dalcin TSSolve - Steps the requested number of timesteps. 40346a4d4014SLisandro Dalcin 4035c3339decSBarry Smith Collective 40366a4d4014SLisandro Dalcin 4037d8d19677SJose E. Roman Input Parameters: 4038bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 4039bcf0153eSBarry Smith - u - the solution vector (can be null if `TSSetSolution()` was used and `TSSetExactFinalTime`(ts,`TS_EXACTFINALTIME_MATCHSTEP`) was not used, 40400b4b7b1cSBarry Smith otherwise it must contain the initial conditions and will contain the solution at the final requested time 40415a3a76d0SJed Brown 40426a4d4014SLisandro Dalcin Level: beginner 40436a4d4014SLisandro Dalcin 40445a3a76d0SJed Brown Notes: 40455a3a76d0SJed Brown The final time returned by this function may be different from the time of the internally 4046bcf0153eSBarry Smith held state accessible by `TSGetSolution()` and `TSGetTime()` because the method may have 40475a3a76d0SJed Brown stepped over the final time. 40485a3a76d0SJed Brown 40491cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetSolution()`, `TSStep()`, `TSGetTime()`, `TSGetSolveTime()` 40506a4d4014SLisandro Dalcin @*/ 4051d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSolve(TS ts, Vec u) 4052d71ae5a4SJacob Faibussowitsch { 4053b06615a5SLisandro Dalcin Vec solution; 4054f22f69f0SBarry Smith 40556a4d4014SLisandro Dalcin PetscFunctionBegin; 40560700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4057f2c2a1b9SBarry Smith if (u) PetscValidHeaderSpecific(u, VEC_CLASSID, 2); 4058303a5415SBarry Smith 40599566063dSJacob Faibussowitsch PetscCall(TSSetExactFinalTimeDefault(ts)); 4060ee41a567SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && u) { /* Need ts->vec_sol to be distinct so it is not overwritten when we interpolate at the end */ 40610910c330SBarry Smith if (!ts->vec_sol || u == ts->vec_sol) { 40629566063dSJacob Faibussowitsch PetscCall(VecDuplicate(u, &solution)); 40639566063dSJacob Faibussowitsch PetscCall(TSSetSolution(ts, solution)); 40649566063dSJacob Faibussowitsch PetscCall(VecDestroy(&solution)); /* grant ownership */ 40655a3a76d0SJed Brown } 40669566063dSJacob Faibussowitsch PetscCall(VecCopy(u, ts->vec_sol)); 40673c633725SBarry Smith PetscCheck(!ts->forward_solve, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Sensitivity analysis does not support the mode TS_EXACTFINALTIME_INTERPOLATE"); 40681baa6e33SBarry Smith } else if (u) PetscCall(TSSetSolution(ts, u)); 40699566063dSJacob Faibussowitsch PetscCall(TSSetUp(ts)); 40709566063dSJacob Faibussowitsch PetscCall(TSTrajectorySetUp(ts->trajectory, ts)); 4071a6772fa2SLisandro Dalcin 40728e562f8dSJames Wright PetscCheck(ts->max_time < PETSC_MAX_REAL || ts->run_steps != PETSC_INT_MAX || ts->max_steps != PETSC_INT_MAX, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetMaxTime(), TSSetMaxSteps(), or TSSetRunSteps() or use -ts_max_time <time>, -ts_max_steps <steps>, -ts_run_steps <steps>"); 40733c633725SBarry Smith PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_UNSPECIFIED, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "You must call TSSetExactFinalTime() or use -ts_exact_final_time <stepover,interpolate,matchstep> before calling TSSolve()"); 40743c633725SBarry Smith PetscCheck(ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP || ts->adapt, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "Since TS is not adaptive you cannot use TS_EXACTFINALTIME_MATCHSTEP, suggest TS_EXACTFINALTIME_INTERPOLATE"); 4075136cf249SJames Wright PetscCheck(!(ts->eval_times && ts->exact_final_time != TS_EXACTFINALTIME_MATCHSTEP), PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "You must use TS_EXACTFINALTIME_MATCHSTEP when using time span or evaluation times"); 40764a658b32SHong Zhang 4077136cf249SJames Wright if (ts->eval_times) { 4078136cf249SJames Wright for (PetscInt i = 0; i < ts->eval_times->num_time_points; i++) { 4079136cf249SJames Wright PetscBool is_close = PetscIsCloseAtTol(ts->ptime, ts->eval_times->time_points[i], ts->eval_times->reltol * ts->time_step + ts->eval_times->abstol, 0); 4080136cf249SJames Wright if (ts->ptime <= ts->eval_times->time_points[i] || is_close) { 4081136cf249SJames Wright ts->eval_times->time_point_idx = i; 4082136cf249SJames Wright if (is_close) { /* starting point in evaluation times */ 4083136cf249SJames Wright PetscCall(VecCopy(ts->vec_sol, ts->eval_times->sol_vecs[ts->eval_times->sol_ctr])); 4084136cf249SJames Wright ts->eval_times->sol_times[ts->eval_times->sol_ctr] = ts->ptime; 4085136cf249SJames Wright ts->eval_times->sol_ctr++; 4086136cf249SJames Wright ts->eval_times->time_point_idx++; 40878343f784SJames Wright } 40888343f784SJames Wright break; 40898343f784SJames Wright } 40908343f784SJames Wright } 40914a658b32SHong Zhang } 4092a6772fa2SLisandro Dalcin 40931baa6e33SBarry Smith if (ts->forward_solve) PetscCall(TSForwardSetUp(ts)); 4094715f1b00SHong Zhang 4095e7069c78SShri /* reset number of steps only when the step is not restarted. ARKIMEX 4096715f1b00SHong Zhang restarts the step after an event. Resetting these counters in such case causes 4097e7069c78SShri TSTrajectory to incorrectly save the output files 4098e7069c78SShri */ 4099715f1b00SHong Zhang /* reset time step and iteration counters */ 41002808aa04SLisandro Dalcin if (!ts->steps) { 41015ef26d82SJed Brown ts->ksp_its = 0; 41025ef26d82SJed Brown ts->snes_its = 0; 4103c610991cSLisandro Dalcin ts->num_snes_failures = 0; 4104c610991cSLisandro Dalcin ts->reject = 0; 41052808aa04SLisandro Dalcin ts->steprestart = PETSC_TRUE; 41062808aa04SLisandro Dalcin ts->steprollback = PETSC_FALSE; 4107c6bf8827SStefano Zampini ts->stepresize = PETSC_FALSE; 41087d51462cSStefano Zampini ts->rhsjacobian.time = PETSC_MIN_REAL; 41092808aa04SLisandro Dalcin } 4110e97c63d7SStefano Zampini 4111136cf249SJames Wright /* make sure initial time step does not overshoot final time or the next point in evaluation times */ 4112e97c63d7SStefano Zampini if (ts->exact_final_time == TS_EXACTFINALTIME_MATCHSTEP) { 41134a658b32SHong Zhang PetscReal maxdt; 4114e97c63d7SStefano Zampini PetscReal dt = ts->time_step; 4115e97c63d7SStefano Zampini 4116136cf249SJames Wright if (ts->eval_times) maxdt = ts->eval_times->time_points[ts->eval_times->time_point_idx] - ts->ptime; 41174a658b32SHong Zhang else maxdt = ts->max_time - ts->ptime; 4118e97c63d7SStefano Zampini ts->time_step = dt >= maxdt ? maxdt : (PetscIsCloseAtTol(dt, maxdt, 10 * PETSC_MACHINE_EPSILON, 0) ? maxdt : dt); 4119e97c63d7SStefano Zampini } 4120193ac0bcSJed Brown ts->reason = TS_CONVERGED_ITERATING; 4121193ac0bcSJed Brown 4122900f6b5bSMatthew G. Knepley { 4123900f6b5bSMatthew G. Knepley PetscViewer viewer; 4124900f6b5bSMatthew G. Knepley PetscViewerFormat format; 4125900f6b5bSMatthew G. Knepley PetscBool flg; 4126900f6b5bSMatthew G. Knepley static PetscBool incall = PETSC_FALSE; 4127900f6b5bSMatthew G. Knepley 4128900f6b5bSMatthew G. Knepley if (!incall) { 4129900f6b5bSMatthew G. Knepley /* Estimate the convergence rate of the time discretization */ 4130648c30bcSBarry Smith PetscCall(PetscOptionsCreateViewer(PetscObjectComm((PetscObject)ts), ((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_estimate", &viewer, &format, &flg)); 4131900f6b5bSMatthew G. Knepley if (flg) { 4132900f6b5bSMatthew G. Knepley PetscConvEst conv; 4133900f6b5bSMatthew G. Knepley DM dm; 4134900f6b5bSMatthew G. Knepley PetscReal *alpha; /* Convergence rate of the solution error for each field in the L_2 norm */ 4135900f6b5bSMatthew G. Knepley PetscInt Nf; 4136f2ed2dc7SMatthew G. Knepley PetscBool checkTemporal = PETSC_TRUE; 4137900f6b5bSMatthew G. Knepley 4138900f6b5bSMatthew G. Knepley incall = PETSC_TRUE; 41399566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_convergence_temporal", &checkTemporal, &flg)); 41409566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 41419566063dSJacob Faibussowitsch PetscCall(DMGetNumFields(dm, &Nf)); 41429566063dSJacob Faibussowitsch PetscCall(PetscCalloc1(PetscMax(Nf, 1), &alpha)); 41439566063dSJacob Faibussowitsch PetscCall(PetscConvEstCreate(PetscObjectComm((PetscObject)ts), &conv)); 41449566063dSJacob Faibussowitsch PetscCall(PetscConvEstUseTS(conv, checkTemporal)); 41459566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetSolver(conv, (PetscObject)ts)); 41469566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetFromOptions(conv)); 41479566063dSJacob Faibussowitsch PetscCall(PetscConvEstSetUp(conv)); 41489566063dSJacob Faibussowitsch PetscCall(PetscConvEstGetConvRate(conv, alpha)); 41499566063dSJacob Faibussowitsch PetscCall(PetscViewerPushFormat(viewer, format)); 41509566063dSJacob Faibussowitsch PetscCall(PetscConvEstRateView(conv, alpha, viewer)); 41519566063dSJacob Faibussowitsch PetscCall(PetscViewerPopFormat(viewer)); 4152648c30bcSBarry Smith PetscCall(PetscViewerDestroy(&viewer)); 41539566063dSJacob Faibussowitsch PetscCall(PetscConvEstDestroy(&conv)); 41549566063dSJacob Faibussowitsch PetscCall(PetscFree(alpha)); 4155900f6b5bSMatthew G. Knepley incall = PETSC_FALSE; 4156900f6b5bSMatthew G. Knepley } 4157900f6b5bSMatthew G. Knepley } 4158900f6b5bSMatthew G. Knepley } 4159900f6b5bSMatthew G. Knepley 41609566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view_pre")); 4161f05ece33SBarry Smith 4162193ac0bcSJed Brown if (ts->ops->solve) { /* This private interface is transitional and should be removed when all implementations are updated. */ 4163dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, solve); 41649566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4165cc708dedSBarry Smith ts->solvetime = ts->ptime; 4166a6772fa2SLisandro Dalcin solution = ts->vec_sol; 4167be5899b3SLisandro Dalcin } else { /* Step the requested number of timesteps. */ 4168db4deed7SKarl Rupp if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 4169db4deed7SKarl Rupp else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 4170e7069c78SShri 41712808aa04SLisandro Dalcin if (!ts->steps) { 41729566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 41739566063dSJacob Faibussowitsch PetscCall(TSEventInitialize(ts->event, ts, ts->ptime, ts->vec_sol)); 41742808aa04SLisandro Dalcin } 41756427ac75SLisandro Dalcin 41768e562f8dSJames Wright ts->start_step = ts->steps; // records starting step 4177e1a7a14fSJed Brown while (!ts->reason) { 41789566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 4179c6bf8827SStefano Zampini if (!ts->steprollback || (ts->stepresize && ts->resizerollback)) PetscCall(TSPreStep(ts)); 41809566063dSJacob Faibussowitsch PetscCall(TSStep(ts)); 41811baa6e33SBarry Smith if (ts->testjacobian) PetscCall(TSRHSJacobianTest(ts, NULL)); 41821baa6e33SBarry Smith if (ts->testjacobiantranspose) PetscCall(TSRHSJacobianTestTranspose(ts, NULL)); 4183cd4cee2dSHong Zhang if (ts->quadraturets && ts->costintegralfwd) { /* Must evaluate the cost integral before event is handled. The cost integral value can also be rolled back. */ 41847b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 41859566063dSJacob Faibussowitsch PetscCall(TSForwardCostIntegral(ts)); 41867b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4187b1cb36f3SHong Zhang } 418858818c2dSLisandro Dalcin if (ts->forward_solve) { /* compute forward sensitivities before event handling because postevent() may change RHS and jump conditions may have to be applied */ 41897b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps--; /* Revert the step number changed by TSStep() */ 41909566063dSJacob Faibussowitsch PetscCall(TSForwardStep(ts)); 41917b0e2f17SHong Zhang if (ts->reason >= 0) ts->steps++; 4192715f1b00SHong Zhang } 41939566063dSJacob Faibussowitsch PetscCall(TSPostEvaluate(ts)); 41949566063dSJacob Faibussowitsch PetscCall(TSEventHandler(ts)); /* The right-hand side may be changed due to event. Be careful with Any computation using the RHS information after this point. */ 41951baa6e33SBarry Smith if (ts->steprollback) PetscCall(TSPostEvaluate(ts)); 4196ecc87898SStefano Zampini if (!ts->steprollback && ts->resizerollback) PetscCall(TSResize(ts)); 419790d719a4SStefano Zampini /* check convergence */ 419890d719a4SStefano Zampini if (!ts->reason) { 41998e562f8dSJames Wright if ((ts->steps - ts->start_step) >= ts->run_steps) ts->reason = TS_CONVERGED_ITS; 42008e562f8dSJames Wright else if (ts->steps >= ts->max_steps) ts->reason = TS_CONVERGED_ITS; 420190d719a4SStefano Zampini else if (ts->ptime >= ts->max_time) ts->reason = TS_CONVERGED_TIME; 420290d719a4SStefano Zampini } 4203e783b05fSHong Zhang if (!ts->steprollback) { 42049566063dSJacob Faibussowitsch PetscCall(TSTrajectorySet(ts->trajectory, ts, ts->steps, ts->ptime, ts->vec_sol)); 42059566063dSJacob Faibussowitsch PetscCall(TSPostStep(ts)); 4206ecc87898SStefano Zampini if (!ts->resizerollback) PetscCall(TSResize(ts)); 4207ca4445c7SIlya Fursov 420826077dccSJames Wright if (ts->eval_times && ts->eval_times->time_point_idx < ts->eval_times->num_time_points && ts->reason >= 0) { 4209136cf249SJames Wright PetscCheck(ts->eval_times->worktol > 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_PLIB, "Unexpected state !(eval_times->worktol > 0) in TSSolve()"); 4210136cf249SJames Wright if (PetscIsCloseAtTol(ts->ptime, ts->eval_times->time_points[ts->eval_times->time_point_idx], ts->eval_times->worktol, 0)) { 4211136cf249SJames Wright ts->eval_times->sol_times[ts->eval_times->sol_ctr] = ts->ptime; 4212136cf249SJames Wright PetscCall(VecCopy(ts->vec_sol, ts->eval_times->sol_vecs[ts->eval_times->sol_ctr])); 4213136cf249SJames Wright ts->eval_times->sol_ctr++; 4214136cf249SJames Wright ts->eval_times->time_point_idx++; 42158343f784SJames Wright } 4216ca4445c7SIlya Fursov } 4217aeb4809dSShri Abhyankar } 4218193ac0bcSJed Brown } 42199566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, ts->steps, ts->ptime, ts->vec_sol)); 42206427ac75SLisandro Dalcin 422149354f04SShri Abhyankar if (ts->exact_final_time == TS_EXACTFINALTIME_INTERPOLATE && ts->ptime > ts->max_time) { 42225dbbf88eSStefano Zampini if (!u) u = ts->vec_sol; 42239566063dSJacob Faibussowitsch PetscCall(TSInterpolate(ts, ts->max_time, u)); 4224cc708dedSBarry Smith ts->solvetime = ts->max_time; 4225b06615a5SLisandro Dalcin solution = u; 42269566063dSJacob Faibussowitsch PetscCall(TSMonitor(ts, -1, ts->solvetime, solution)); 42270574a7fbSJed Brown } else { 42289566063dSJacob Faibussowitsch if (u) PetscCall(VecCopy(ts->vec_sol, u)); 4229cc708dedSBarry Smith ts->solvetime = ts->ptime; 4230b06615a5SLisandro Dalcin solution = ts->vec_sol; 42310574a7fbSJed Brown } 4232193ac0bcSJed Brown } 4233d2daff3dSHong Zhang 42349566063dSJacob Faibussowitsch PetscCall(TSViewFromOptions(ts, NULL, "-ts_view")); 42359566063dSJacob Faibussowitsch PetscCall(VecViewFromOptions(solution, (PetscObject)ts, "-ts_view_solution")); 42369566063dSJacob Faibussowitsch PetscCall(PetscObjectSAWsBlock((PetscObject)ts)); 42371baa6e33SBarry Smith if (ts->adjoint_solve) PetscCall(TSAdjointSolve(ts)); 42383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 42396a4d4014SLisandro Dalcin } 42406a4d4014SLisandro Dalcin 4241d763cef2SBarry Smith /*@ 4242b8123daeSJed Brown TSGetTime - Gets the time of the most recently completed step. 4243d763cef2SBarry Smith 4244d763cef2SBarry Smith Not Collective 4245d763cef2SBarry Smith 4246d763cef2SBarry Smith Input Parameter: 4247bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4248d763cef2SBarry Smith 4249d763cef2SBarry Smith Output Parameter: 4250bcf0153eSBarry Smith . t - the current time. This time may not corresponds to the final time set with `TSSetMaxTime()`, use `TSGetSolveTime()`. 4251d763cef2SBarry Smith 4252d763cef2SBarry Smith Level: beginner 4253d763cef2SBarry Smith 4254b8123daeSJed Brown Note: 4255bcf0153eSBarry Smith When called during time step evaluation (e.g. during residual evaluation or via hooks set using `TSSetPreStep()`, 4256bcf0153eSBarry Smith `TSSetPreStage()`, `TSSetPostStage()`, or `TSSetPostStep()`), the time is the time at the start of the step being evaluated. 4257b8123daeSJed Brown 4258a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetSolveTime()`, `TSSetTime()`, `TSGetTimeStep()`, `TSGetStepNumber()` 4259d763cef2SBarry Smith @*/ 4260d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTime(TS ts, PetscReal *t) 4261d71ae5a4SJacob Faibussowitsch { 4262d763cef2SBarry Smith PetscFunctionBegin; 42630700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42644f572ea9SToby Isaac PetscAssertPointer(t, 2); 4265d763cef2SBarry Smith *t = ts->ptime; 42663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4267d763cef2SBarry Smith } 4268d763cef2SBarry Smith 4269e5e524a1SHong Zhang /*@ 4270e5e524a1SHong Zhang TSGetPrevTime - Gets the starting time of the previously completed step. 4271e5e524a1SHong Zhang 4272e5e524a1SHong Zhang Not Collective 4273e5e524a1SHong Zhang 4274e5e524a1SHong Zhang Input Parameter: 4275bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 4276e5e524a1SHong Zhang 4277e5e524a1SHong Zhang Output Parameter: 4278e5e524a1SHong Zhang . t - the previous time 4279e5e524a1SHong Zhang 4280e5e524a1SHong Zhang Level: beginner 4281e5e524a1SHong Zhang 4282a94f484eSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSGetSolveTime()`, `TSGetTimeStep()` 4283e5e524a1SHong Zhang @*/ 4284d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetPrevTime(TS ts, PetscReal *t) 4285d71ae5a4SJacob Faibussowitsch { 4286e5e524a1SHong Zhang PetscFunctionBegin; 4287e5e524a1SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 42884f572ea9SToby Isaac PetscAssertPointer(t, 2); 4289e5e524a1SHong Zhang *t = ts->ptime_prev; 42903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4291e5e524a1SHong Zhang } 4292e5e524a1SHong Zhang 42936a4d4014SLisandro Dalcin /*@ 42946a4d4014SLisandro Dalcin TSSetTime - Allows one to reset the time. 42956a4d4014SLisandro Dalcin 4296c3339decSBarry Smith Logically Collective 42976a4d4014SLisandro Dalcin 42986a4d4014SLisandro Dalcin Input Parameters: 4299bcf0153eSBarry Smith + ts - the `TS` context obtained from `TSCreate()` 4300b43aa488SJacob Faibussowitsch - t - the time 43016a4d4014SLisandro Dalcin 43026a4d4014SLisandro Dalcin Level: intermediate 43036a4d4014SLisandro Dalcin 43041cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTime()`, `TSSetMaxSteps()` 43056a4d4014SLisandro Dalcin @*/ 4306d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTime(TS ts, PetscReal t) 4307d71ae5a4SJacob Faibussowitsch { 43086a4d4014SLisandro Dalcin PetscFunctionBegin; 43090700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4310c5eb9154SBarry Smith PetscValidLogicalCollectiveReal(ts, t, 2); 43116a4d4014SLisandro Dalcin ts->ptime = t; 43123ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 43136a4d4014SLisandro Dalcin } 43146a4d4014SLisandro Dalcin 4315cc4c1da9SBarry Smith /*@ 4316d763cef2SBarry Smith TSSetOptionsPrefix - Sets the prefix used for searching for all 4317d763cef2SBarry Smith TS options in the database. 4318d763cef2SBarry Smith 4319c3339decSBarry Smith Logically Collective 4320d763cef2SBarry Smith 4321d8d19677SJose E. Roman Input Parameters: 4322bcf0153eSBarry Smith + ts - The `TS` context 4323d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4324d763cef2SBarry Smith 4325bcf0153eSBarry Smith Level: advanced 4326bcf0153eSBarry Smith 4327bcf0153eSBarry Smith Note: 4328d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4329d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4330d763cef2SBarry Smith hyphen. 4331d763cef2SBarry Smith 43321cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFromOptions()`, `TSAppendOptionsPrefix()` 4333d763cef2SBarry Smith @*/ 4334d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetOptionsPrefix(TS ts, const char prefix[]) 4335d71ae5a4SJacob Faibussowitsch { 4336089b2837SJed Brown SNES snes; 4337d763cef2SBarry Smith 4338d763cef2SBarry Smith PetscFunctionBegin; 43390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43409566063dSJacob Faibussowitsch PetscCall(PetscObjectSetOptionsPrefix((PetscObject)ts, prefix)); 43419566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43429566063dSJacob Faibussowitsch PetscCall(SNESSetOptionsPrefix(snes, prefix)); 43433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4344d763cef2SBarry Smith } 4345d763cef2SBarry Smith 4346cc4c1da9SBarry Smith /*@ 4347d763cef2SBarry Smith TSAppendOptionsPrefix - Appends to the prefix used for searching for all 4348d763cef2SBarry Smith TS options in the database. 4349d763cef2SBarry Smith 4350c3339decSBarry Smith Logically Collective 4351d763cef2SBarry Smith 4352d8d19677SJose E. Roman Input Parameters: 4353bcf0153eSBarry Smith + ts - The `TS` context 4354d763cef2SBarry Smith - prefix - The prefix to prepend to all option names 4355d763cef2SBarry Smith 4356bcf0153eSBarry Smith Level: advanced 4357bcf0153eSBarry Smith 4358bcf0153eSBarry Smith Note: 4359d763cef2SBarry Smith A hyphen (-) must NOT be given at the beginning of the prefix name. 4360d763cef2SBarry Smith The first character of all runtime options is AUTOMATICALLY the 4361d763cef2SBarry Smith hyphen. 4362d763cef2SBarry Smith 43631cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetOptionsPrefix()`, `TSSetOptionsPrefix()`, `TSSetFromOptions()` 4364d763cef2SBarry Smith @*/ 4365d71ae5a4SJacob Faibussowitsch PetscErrorCode TSAppendOptionsPrefix(TS ts, const char prefix[]) 4366d71ae5a4SJacob Faibussowitsch { 4367089b2837SJed Brown SNES snes; 4368d763cef2SBarry Smith 4369d763cef2SBarry Smith PetscFunctionBegin; 43700700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43719566063dSJacob Faibussowitsch PetscCall(PetscObjectAppendOptionsPrefix((PetscObject)ts, prefix)); 43729566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 43739566063dSJacob Faibussowitsch PetscCall(SNESAppendOptionsPrefix(snes, prefix)); 43743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4375d763cef2SBarry Smith } 4376d763cef2SBarry Smith 4377cc4c1da9SBarry Smith /*@ 4378d763cef2SBarry Smith TSGetOptionsPrefix - Sets the prefix used for searching for all 4379bcf0153eSBarry Smith `TS` options in the database. 4380d763cef2SBarry Smith 4381d763cef2SBarry Smith Not Collective 4382d763cef2SBarry Smith 4383d763cef2SBarry Smith Input Parameter: 4384bcf0153eSBarry Smith . ts - The `TS` context 4385d763cef2SBarry Smith 4386d763cef2SBarry Smith Output Parameter: 4387d763cef2SBarry Smith . prefix - A pointer to the prefix string used 4388d763cef2SBarry Smith 4389d763cef2SBarry Smith Level: intermediate 4390d763cef2SBarry Smith 43911cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAppendOptionsPrefix()`, `TSSetFromOptions()` 4392d763cef2SBarry Smith @*/ 4393d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetOptionsPrefix(TS ts, const char *prefix[]) 4394d71ae5a4SJacob Faibussowitsch { 4395d763cef2SBarry Smith PetscFunctionBegin; 43960700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 43974f572ea9SToby Isaac PetscAssertPointer(prefix, 2); 43989566063dSJacob Faibussowitsch PetscCall(PetscObjectGetOptionsPrefix((PetscObject)ts, prefix)); 43993ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4400d763cef2SBarry Smith } 4401d763cef2SBarry Smith 4402d763cef2SBarry Smith /*@C 4403d763cef2SBarry Smith TSGetRHSJacobian - Returns the Jacobian J at the present timestep. 4404d763cef2SBarry Smith 4405bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 4406d763cef2SBarry Smith 4407d763cef2SBarry Smith Input Parameter: 4408bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 4409d763cef2SBarry Smith 4410d763cef2SBarry Smith Output Parameters: 4411195e9b02SBarry Smith + Amat - The (approximate) Jacobian J of G, where U_t = G(U,t) (or `NULL`) 4412195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, usually the same as `Amat` (or `NULL`) 4413195e9b02SBarry Smith . func - Function to compute the Jacobian of the RHS (or `NULL`) 4414195e9b02SBarry Smith - ctx - User-defined context for Jacobian evaluation routine (or `NULL`) 4415d763cef2SBarry Smith 4416d763cef2SBarry Smith Level: intermediate 4417d763cef2SBarry Smith 4418bcf0153eSBarry Smith Note: 4419195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4420bcf0153eSBarry Smith 44211cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 4422d763cef2SBarry Smith 4423d763cef2SBarry Smith @*/ 44248434afd1SBarry Smith PetscErrorCode TSGetRHSJacobian(TS ts, Mat *Amat, Mat *Pmat, TSRHSJacobianFn **func, void **ctx) 4425d71ae5a4SJacob Faibussowitsch { 442624989b8cSPeter Brune DM dm; 4427089b2837SJed Brown 4428d763cef2SBarry Smith PetscFunctionBegin; 442923a57915SBarry Smith if (Amat || Pmat) { 443023a57915SBarry Smith SNES snes; 44319566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 44329566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 44339566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 443423a57915SBarry Smith } 44359566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 44369566063dSJacob Faibussowitsch PetscCall(DMTSGetRHSJacobian(dm, func, ctx)); 44373ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4438d763cef2SBarry Smith } 4439d763cef2SBarry Smith 44402eca1d9cSJed Brown /*@C 44412eca1d9cSJed Brown TSGetIJacobian - Returns the implicit Jacobian at the present timestep. 44422eca1d9cSJed Brown 4443bcf0153eSBarry Smith Not Collective, but parallel objects are returned if ts is parallel 44442eca1d9cSJed Brown 44452eca1d9cSJed Brown Input Parameter: 4446bcf0153eSBarry Smith . ts - The `TS` context obtained from `TSCreate()` 44472eca1d9cSJed Brown 44482eca1d9cSJed Brown Output Parameters: 4449e4357dc4SBarry Smith + Amat - The (approximate) Jacobian of F(t,U,U_t) 4450195e9b02SBarry Smith . Pmat - The matrix from which the preconditioner is constructed, often the same as `Amat` 44512eca1d9cSJed Brown . f - The function to compute the matrices 44522eca1d9cSJed Brown - ctx - User-defined context for Jacobian evaluation routine 44532eca1d9cSJed Brown 44542eca1d9cSJed Brown Level: advanced 44552eca1d9cSJed Brown 4456bcf0153eSBarry Smith Note: 4457195e9b02SBarry Smith You can pass in `NULL` for any return argument you do not need. 4458bcf0153eSBarry Smith 44591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetTimeStep()`, `TSGetRHSJacobian()`, `TSGetMatrices()`, `TSGetTime()`, `TSGetStepNumber()` 44602eca1d9cSJed Brown @*/ 44618434afd1SBarry Smith PetscErrorCode TSGetIJacobian(TS ts, Mat *Amat, Mat *Pmat, TSIJacobianFn **f, void **ctx) 4462d71ae5a4SJacob Faibussowitsch { 446324989b8cSPeter Brune DM dm; 44640910c330SBarry Smith 44652eca1d9cSJed Brown PetscFunctionBegin; 4466c0aab802Sstefano_zampini if (Amat || Pmat) { 4467c0aab802Sstefano_zampini SNES snes; 44689566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 44699566063dSJacob Faibussowitsch PetscCall(SNESSetUpMatrices(snes)); 44709566063dSJacob Faibussowitsch PetscCall(SNESGetJacobian(snes, Amat, Pmat, NULL, NULL)); 4471c0aab802Sstefano_zampini } 44729566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 44739566063dSJacob Faibussowitsch PetscCall(DMTSGetIJacobian(dm, f, ctx)); 44743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 44752eca1d9cSJed Brown } 44762eca1d9cSJed Brown 4477af0996ceSBarry Smith #include <petsc/private/dmimpl.h> 44786c699258SBarry Smith /*@ 4479bcf0153eSBarry Smith TSSetDM - Sets the `DM` that may be used by some nonlinear solvers or preconditioners under the `TS` 44806c699258SBarry Smith 4481c3339decSBarry Smith Logically Collective 44826c699258SBarry Smith 44836c699258SBarry Smith Input Parameters: 4484bcf0153eSBarry Smith + ts - the `TS` integrator object 4485195e9b02SBarry Smith - dm - the dm, cannot be `NULL` 44866c699258SBarry Smith 44876c699258SBarry Smith Level: intermediate 44886c699258SBarry Smith 4489bcf0153eSBarry Smith Notes: 4490bcf0153eSBarry Smith A `DM` can only be used for solving one problem at a time because information about the problem is stored on the `DM`, 4491bcf0153eSBarry Smith even when not using interfaces like `DMTSSetIFunction()`. Use `DMClone()` to get a distinct `DM` when solving 4492bcf0153eSBarry Smith different problems using the same function space. 4493bcf0153eSBarry Smith 44941cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSGetDM()`, `SNESSetDM()`, `SNESGetDM()` 44956c699258SBarry Smith @*/ 4496d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetDM(TS ts, DM dm) 4497d71ae5a4SJacob Faibussowitsch { 4498089b2837SJed Brown SNES snes; 4499942e3340SBarry Smith DMTS tsdm; 45006c699258SBarry Smith 45016c699258SBarry Smith PetscFunctionBegin; 45020700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 45032a808120SBarry Smith PetscValidHeaderSpecific(dm, DM_CLASSID, 2); 45049566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)dm)); 4505942e3340SBarry Smith if (ts->dm) { /* Move the DMTS context over to the new DM unless the new DM already has one */ 45062a34c10cSBarry Smith if (ts->dm->dmts && !dm->dmts) { 45079566063dSJacob Faibussowitsch PetscCall(DMCopyDMTS(ts->dm, dm)); 45089566063dSJacob Faibussowitsch PetscCall(DMGetDMTS(ts->dm, &tsdm)); 45091e66621cSBarry Smith /* Grant write privileges to the replacement DM */ 45101e66621cSBarry Smith if (tsdm->originaldm == ts->dm) tsdm->originaldm = dm; 451124989b8cSPeter Brune } 45129566063dSJacob Faibussowitsch PetscCall(DMDestroy(&ts->dm)); 451324989b8cSPeter Brune } 45146c699258SBarry Smith ts->dm = dm; 4515bbd56ea5SKarl Rupp 45169566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 45179566063dSJacob Faibussowitsch PetscCall(SNESSetDM(snes, dm)); 45183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45196c699258SBarry Smith } 45206c699258SBarry Smith 45216c699258SBarry Smith /*@ 4522bcf0153eSBarry Smith TSGetDM - Gets the `DM` that may be used by some preconditioners 45236c699258SBarry Smith 45243f9fe445SBarry Smith Not Collective 45256c699258SBarry Smith 45266c699258SBarry Smith Input Parameter: 4527bcf0153eSBarry Smith . ts - the `TS` 45286c699258SBarry Smith 45296c699258SBarry Smith Output Parameter: 4530195e9b02SBarry Smith . dm - the `DM` 45316c699258SBarry Smith 45326c699258SBarry Smith Level: intermediate 45336c699258SBarry Smith 45341cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `DM`, `TSSetDM()`, `SNESSetDM()`, `SNESGetDM()` 45356c699258SBarry Smith @*/ 4536d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetDM(TS ts, DM *dm) 4537d71ae5a4SJacob Faibussowitsch { 45386c699258SBarry Smith PetscFunctionBegin; 45390700a824SBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4540496e6a7aSJed Brown if (!ts->dm) { 45419566063dSJacob Faibussowitsch PetscCall(DMShellCreate(PetscObjectComm((PetscObject)ts), &ts->dm)); 45424bd3aaa3SHong Zhang if (ts->snes) PetscCall(SNESSetDM(ts->snes, ts->dm)); 4543496e6a7aSJed Brown } 45446c699258SBarry Smith *dm = ts->dm; 45453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45466c699258SBarry Smith } 45471713a123SBarry Smith 45480f5c6efeSJed Brown /*@ 45492ba42892SBarry Smith SNESTSFormFunction - Function to evaluate nonlinear residual defined by an ODE solver algorithm implemented within `TS` 45500f5c6efeSJed Brown 4551c3339decSBarry Smith Logically Collective 45520f5c6efeSJed Brown 4553d8d19677SJose E. Roman Input Parameters: 4554d42a1c89SJed Brown + snes - nonlinear solver 45550910c330SBarry Smith . U - the current state at which to evaluate the residual 45562ba42892SBarry Smith - ctx - user context, must be a `TS` 45570f5c6efeSJed Brown 45580f5c6efeSJed Brown Output Parameter: 45590f5c6efeSJed Brown . F - the nonlinear residual 45600f5c6efeSJed Brown 45612ba42892SBarry Smith Level: developer 45620f5c6efeSJed Brown 4563bcf0153eSBarry Smith Note: 4564bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4565bcf0153eSBarry Smith It is most frequently passed to `MatFDColoringSetFunction()`. 4566bcf0153eSBarry Smith 45671cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetFunction()`, `MatFDColoringSetFunction()` 45680f5c6efeSJed Brown @*/ 4569d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormFunction(SNES snes, Vec U, Vec F, void *ctx) 4570d71ae5a4SJacob Faibussowitsch { 45710f5c6efeSJed Brown TS ts = (TS)ctx; 45720f5c6efeSJed Brown 45730f5c6efeSJed Brown PetscFunctionBegin; 45740f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 45750910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 45760f5c6efeSJed Brown PetscValidHeaderSpecific(F, VEC_CLASSID, 3); 45770f5c6efeSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 4); 4578346ce620SStefano Zampini PetscCheck(ts->ops->snesfunction, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesfunction for TS of type %s", ((PetscObject)ts)->type_name); 4579346ce620SStefano Zampini PetscCall((*ts->ops->snesfunction)(snes, U, F, ts)); 45803ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 45810f5c6efeSJed Brown } 45820f5c6efeSJed Brown 45830f5c6efeSJed Brown /*@ 45842ba42892SBarry Smith SNESTSFormJacobian - Function to evaluate the Jacobian defined by an ODE solver algorithm implemented within `TS` 45850f5c6efeSJed Brown 4586c3339decSBarry Smith Collective 45870f5c6efeSJed Brown 4588d8d19677SJose E. Roman Input Parameters: 45890f5c6efeSJed Brown + snes - nonlinear solver 45900910c330SBarry Smith . U - the current state at which to evaluate the residual 4591bcf0153eSBarry Smith - ctx - user context, must be a `TS` 45920f5c6efeSJed Brown 4593d8d19677SJose E. Roman Output Parameters: 45940f5c6efeSJed Brown + A - the Jacobian 45957addb90fSBarry Smith - B - the matrix used to construct the preconditioner (often the same as `A`) 45960f5c6efeSJed Brown 45970f5c6efeSJed Brown Level: developer 45980f5c6efeSJed Brown 4599bcf0153eSBarry Smith Note: 4600bcf0153eSBarry Smith This function is not normally called by users and is automatically registered with the `SNES` used by `TS`. 4601bcf0153eSBarry Smith 46021cc06b55SBarry Smith .seealso: [](ch_ts), `SNESSetJacobian()` 46030f5c6efeSJed Brown @*/ 4604d71ae5a4SJacob Faibussowitsch PetscErrorCode SNESTSFormJacobian(SNES snes, Vec U, Mat A, Mat B, void *ctx) 4605d71ae5a4SJacob Faibussowitsch { 46060f5c6efeSJed Brown TS ts = (TS)ctx; 46070f5c6efeSJed Brown 46080f5c6efeSJed Brown PetscFunctionBegin; 46090f5c6efeSJed Brown PetscValidHeaderSpecific(snes, SNES_CLASSID, 1); 46100910c330SBarry Smith PetscValidHeaderSpecific(U, VEC_CLASSID, 2); 461194ab13aaSBarry Smith PetscValidHeaderSpecific(A, MAT_CLASSID, 3); 461294ab13aaSBarry Smith PetscValidHeaderSpecific(B, MAT_CLASSID, 4); 4613064a246eSJacob Faibussowitsch PetscValidHeaderSpecific(ts, TS_CLASSID, 5); 4614346ce620SStefano Zampini PetscCheck(ts->ops->snesjacobian, PetscObjectComm((PetscObject)ts), PETSC_ERR_SUP, "No method snesjacobian for TS of type %s", ((PetscObject)ts)->type_name); 4615346ce620SStefano Zampini PetscCall((*ts->ops->snesjacobian)(snes, U, A, B, ts)); 46163ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46170f5c6efeSJed Brown } 4618325fc9f4SBarry Smith 46190e4ef248SJed Brown /*@C 4620dd8e379bSPierre Jolivet TSComputeRHSFunctionLinear - Evaluate the right-hand side via the user-provided Jacobian, for linear problems Udot = A U only 46210e4ef248SJed Brown 4622c3339decSBarry Smith Collective 46230e4ef248SJed Brown 46244165533cSJose E. Roman Input Parameters: 46250e4ef248SJed Brown + ts - time stepping context 46260e4ef248SJed Brown . t - time at which to evaluate 46270910c330SBarry Smith . U - state at which to evaluate 46280e4ef248SJed Brown - ctx - context 46290e4ef248SJed Brown 46304165533cSJose E. Roman Output Parameter: 4631dd8e379bSPierre Jolivet . F - right-hand side 46320e4ef248SJed Brown 46330e4ef248SJed Brown Level: intermediate 46340e4ef248SJed Brown 4635bcf0153eSBarry Smith Note: 4636dd8e379bSPierre Jolivet This function is intended to be passed to `TSSetRHSFunction()` to evaluate the right-hand side for linear problems. 4637bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetRHSJacobian()`. 46380e4ef248SJed Brown 46391cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSJacobianConstant()` 46400e4ef248SJed Brown @*/ 4641d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSFunctionLinear(TS ts, PetscReal t, Vec U, Vec F, void *ctx) 4642d71ae5a4SJacob Faibussowitsch { 46430e4ef248SJed Brown Mat Arhs, Brhs; 46440e4ef248SJed Brown 46450e4ef248SJed Brown PetscFunctionBegin; 46469566063dSJacob Faibussowitsch PetscCall(TSGetRHSMats_Private(ts, &Arhs, &Brhs)); 46472663174eSHong Zhang /* undo the damage caused by shifting */ 46489566063dSJacob Faibussowitsch PetscCall(TSRecoverRHSJacobian(ts, Arhs, Brhs)); 46499566063dSJacob Faibussowitsch PetscCall(TSComputeRHSJacobian(ts, t, U, Arhs, Brhs)); 46509566063dSJacob Faibussowitsch PetscCall(MatMult(Arhs, U, F)); 46513ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46520e4ef248SJed Brown } 46530e4ef248SJed Brown 46540e4ef248SJed Brown /*@C 46550e4ef248SJed Brown TSComputeRHSJacobianConstant - Reuses a Jacobian that is time-independent. 46560e4ef248SJed Brown 4657c3339decSBarry Smith Collective 46580e4ef248SJed Brown 46594165533cSJose E. Roman Input Parameters: 46600e4ef248SJed Brown + ts - time stepping context 46610e4ef248SJed Brown . t - time at which to evaluate 46620910c330SBarry Smith . U - state at which to evaluate 46630e4ef248SJed Brown - ctx - context 46640e4ef248SJed Brown 46654165533cSJose E. Roman Output Parameters: 46667addb90fSBarry Smith + A - Jacobian 46677addb90fSBarry Smith - B - matrix used to construct the preconditioner, often the same as `A` 46680e4ef248SJed Brown 46690e4ef248SJed Brown Level: intermediate 46700e4ef248SJed Brown 4671bcf0153eSBarry Smith Note: 4672bcf0153eSBarry Smith This function is intended to be passed to `TSSetRHSJacobian()` to evaluate the Jacobian for linear time-independent problems. 46730e4ef248SJed Brown 46741cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSSetRHSJacobian()`, `TSComputeRHSFunctionLinear()` 46750e4ef248SJed Brown @*/ 4676d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeRHSJacobianConstant(TS ts, PetscReal t, Vec U, Mat A, Mat B, void *ctx) 4677d71ae5a4SJacob Faibussowitsch { 46780e4ef248SJed Brown PetscFunctionBegin; 46793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 46800e4ef248SJed Brown } 46810e4ef248SJed Brown 46820026cea9SSean Farley /*@C 46830026cea9SSean Farley TSComputeIFunctionLinear - Evaluate the left hand side via the user-provided Jacobian, for linear problems only 46840026cea9SSean Farley 4685c3339decSBarry Smith Collective 46860026cea9SSean Farley 46874165533cSJose E. Roman Input Parameters: 46880026cea9SSean Farley + ts - time stepping context 46890026cea9SSean Farley . t - time at which to evaluate 46900910c330SBarry Smith . U - state at which to evaluate 46910910c330SBarry Smith . Udot - time derivative of state vector 46920026cea9SSean Farley - ctx - context 46930026cea9SSean Farley 46944165533cSJose E. Roman Output Parameter: 46950026cea9SSean Farley . F - left hand side 46960026cea9SSean Farley 46970026cea9SSean Farley Level: intermediate 46980026cea9SSean Farley 46990026cea9SSean Farley Notes: 47000910c330SBarry Smith The assumption here is that the left hand side is of the form A*Udot (and not A*Udot + B*U). For other cases, the 4701bcf0153eSBarry Smith user is required to write their own `TSComputeIFunction()`. 4702bcf0153eSBarry Smith This function is intended to be passed to `TSSetIFunction()` to evaluate the left hand side for linear problems. 4703bcf0153eSBarry Smith The matrix (and optionally the evaluation context) should be passed to `TSSetIJacobian()`. 47040026cea9SSean Farley 4705bcf0153eSBarry Smith Note that using this function is NOT equivalent to using `TSComputeRHSFunctionLinear()` since that solves Udot = A U 47069ae8fd06SBarry Smith 47071cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIJacobianConstant()`, `TSComputeRHSFunctionLinear()` 47080026cea9SSean Farley @*/ 4709d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIFunctionLinear(TS ts, PetscReal t, Vec U, Vec Udot, Vec F, void *ctx) 4710d71ae5a4SJacob Faibussowitsch { 47110026cea9SSean Farley Mat A, B; 47120026cea9SSean Farley 47130026cea9SSean Farley PetscFunctionBegin; 47149566063dSJacob Faibussowitsch PetscCall(TSGetIJacobian(ts, &A, &B, NULL, NULL)); 47159566063dSJacob Faibussowitsch PetscCall(TSComputeIJacobian(ts, t, U, Udot, 1.0, A, B, PETSC_TRUE)); 47169566063dSJacob Faibussowitsch PetscCall(MatMult(A, Udot, F)); 47173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47180026cea9SSean Farley } 47190026cea9SSean Farley 47200026cea9SSean Farley /*@C 47218434afd1SBarry Smith TSComputeIJacobianConstant - Reuses the matrix previously computed with the provided `TSIJacobianFn` for a semi-implicit DAE or ODE 47220026cea9SSean Farley 4723c3339decSBarry Smith Collective 47240026cea9SSean Farley 47254165533cSJose E. Roman Input Parameters: 47260026cea9SSean Farley + ts - time stepping context 47270026cea9SSean Farley . t - time at which to evaluate 47280910c330SBarry Smith . U - state at which to evaluate 47290910c330SBarry Smith . Udot - time derivative of state vector 47300026cea9SSean Farley . shift - shift to apply 47310026cea9SSean Farley - ctx - context 47320026cea9SSean Farley 47334165533cSJose E. Roman Output Parameters: 47340026cea9SSean Farley + A - pointer to operator 4735d1c5d1fcSBarry Smith - B - pointer to matrix from which the preconditioner is built (often `A`) 47360026cea9SSean Farley 4737b41af12eSJed Brown Level: advanced 47380026cea9SSean Farley 47390026cea9SSean Farley Notes: 4740bcf0153eSBarry Smith This function is intended to be passed to `TSSetIJacobian()` to evaluate the Jacobian for linear time-independent problems. 47410026cea9SSean Farley 4742b41af12eSJed Brown It is only appropriate for problems of the form 4743b41af12eSJed Brown 4744d1c5d1fcSBarry Smith $$ 4745d1c5d1fcSBarry Smith M \dot{U} = F(U,t) 4746d1c5d1fcSBarry Smith $$ 4747b41af12eSJed Brown 4748bcf0153eSBarry Smith where M is constant and F is non-stiff. The user must pass M to `TSSetIJacobian()`. The current implementation only 4749bcf0153eSBarry Smith works with IMEX time integration methods such as `TSROSW` and `TSARKIMEX`, since there is no support for de-constructing 4750b41af12eSJed Brown an implicit operator of the form 4751b41af12eSJed Brown 4752d1c5d1fcSBarry Smith $$ 4753d1c5d1fcSBarry Smith shift*M + J 4754d1c5d1fcSBarry Smith $$ 4755b41af12eSJed Brown 4756b41af12eSJed Brown where J is the Jacobian of -F(U). Support may be added in a future version of PETSc, but for now, the user must store 4757b41af12eSJed Brown a copy of M or reassemble it when requested. 4758b41af12eSJed Brown 47591cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSROSW`, `TSARKIMEX`, `TSSetIFunction()`, `TSSetIJacobian()`, `TSComputeIFunctionLinear()` 47600026cea9SSean Farley @*/ 4761d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianConstant(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat A, Mat B, void *ctx) 4762d71ae5a4SJacob Faibussowitsch { 47630026cea9SSean Farley PetscFunctionBegin; 47649566063dSJacob Faibussowitsch PetscCall(MatScale(A, shift / ts->ijacobian.shift)); 4765b41af12eSJed Brown ts->ijacobian.shift = shift; 47663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 47670026cea9SSean Farley } 4768b41af12eSJed Brown 4769e817cc15SEmil Constantinescu /*@ 4770bcf0153eSBarry Smith TSGetEquationType - Gets the type of the equation that `TS` is solving. 4771e817cc15SEmil Constantinescu 4772e817cc15SEmil Constantinescu Not Collective 4773e817cc15SEmil Constantinescu 4774e817cc15SEmil Constantinescu Input Parameter: 4775bcf0153eSBarry Smith . ts - the `TS` context 4776e817cc15SEmil Constantinescu 4777e817cc15SEmil Constantinescu Output Parameter: 4778bcf0153eSBarry Smith . equation_type - see `TSEquationType` 4779e817cc15SEmil Constantinescu 4780e817cc15SEmil Constantinescu Level: beginner 4781e817cc15SEmil Constantinescu 47821cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetEquationType()`, `TSEquationType` 4783e817cc15SEmil Constantinescu @*/ 4784d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetEquationType(TS ts, TSEquationType *equation_type) 4785d71ae5a4SJacob Faibussowitsch { 4786e817cc15SEmil Constantinescu PetscFunctionBegin; 4787e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 47884f572ea9SToby Isaac PetscAssertPointer(equation_type, 2); 4789e817cc15SEmil Constantinescu *equation_type = ts->equation_type; 47903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4791e817cc15SEmil Constantinescu } 4792e817cc15SEmil Constantinescu 4793e817cc15SEmil Constantinescu /*@ 4794bcf0153eSBarry Smith TSSetEquationType - Sets the type of the equation that `TS` is solving. 4795e817cc15SEmil Constantinescu 4796e817cc15SEmil Constantinescu Not Collective 4797e817cc15SEmil Constantinescu 4798d8d19677SJose E. Roman Input Parameters: 4799bcf0153eSBarry Smith + ts - the `TS` context 4800bcf0153eSBarry Smith - equation_type - see `TSEquationType` 4801e817cc15SEmil Constantinescu 4802e817cc15SEmil Constantinescu Level: advanced 4803e817cc15SEmil Constantinescu 48041cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetEquationType()`, `TSEquationType` 4805e817cc15SEmil Constantinescu @*/ 4806d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetEquationType(TS ts, TSEquationType equation_type) 4807d71ae5a4SJacob Faibussowitsch { 4808e817cc15SEmil Constantinescu PetscFunctionBegin; 4809e817cc15SEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4810e817cc15SEmil Constantinescu ts->equation_type = equation_type; 48113ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4812e817cc15SEmil Constantinescu } 48130026cea9SSean Farley 48144af1b03aSJed Brown /*@ 4815bcf0153eSBarry Smith TSGetConvergedReason - Gets the reason the `TS` iteration was stopped. 48164af1b03aSJed Brown 48174af1b03aSJed Brown Not Collective 48184af1b03aSJed Brown 48194af1b03aSJed Brown Input Parameter: 4820bcf0153eSBarry Smith . ts - the `TS` context 48214af1b03aSJed Brown 48224af1b03aSJed Brown Output Parameter: 4823bcf0153eSBarry Smith . reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 48244af1b03aSJed Brown manual pages for the individual convergence tests for complete lists 48254af1b03aSJed Brown 4826487e0bb9SJed Brown Level: beginner 48274af1b03aSJed Brown 4828bcf0153eSBarry Smith Note: 4829bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 48304af1b03aSJed Brown 48317d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 48324af1b03aSJed Brown @*/ 4833d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetConvergedReason(TS ts, TSConvergedReason *reason) 4834d71ae5a4SJacob Faibussowitsch { 48354af1b03aSJed Brown PetscFunctionBegin; 48364af1b03aSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48374f572ea9SToby Isaac PetscAssertPointer(reason, 2); 48384af1b03aSJed Brown *reason = ts->reason; 48393ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 48404af1b03aSJed Brown } 48414af1b03aSJed Brown 4842d6ad946cSShri Abhyankar /*@ 4843bcf0153eSBarry Smith TSSetConvergedReason - Sets the reason for handling the convergence of `TSSolve()`. 4844d6ad946cSShri Abhyankar 48456b221cbeSPatrick Sanan Logically Collective; reason must contain common value 4846d6ad946cSShri Abhyankar 48476b221cbeSPatrick Sanan Input Parameters: 4848bcf0153eSBarry Smith + ts - the `TS` context 4849bcf0153eSBarry Smith - reason - negative value indicates diverged, positive value converged, see `TSConvergedReason` or the 4850d6ad946cSShri Abhyankar manual pages for the individual convergence tests for complete lists 4851d6ad946cSShri Abhyankar 4852f5abba47SShri Abhyankar Level: advanced 4853d6ad946cSShri Abhyankar 4854bcf0153eSBarry Smith Note: 4855bcf0153eSBarry Smith Can only be called while `TSSolve()` is active. 4856d6ad946cSShri Abhyankar 48571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4858d6ad946cSShri Abhyankar @*/ 4859d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetConvergedReason(TS ts, TSConvergedReason reason) 4860d71ae5a4SJacob Faibussowitsch { 4861d6ad946cSShri Abhyankar PetscFunctionBegin; 4862d6ad946cSShri Abhyankar PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 4863d6ad946cSShri Abhyankar ts->reason = reason; 48643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4865d6ad946cSShri Abhyankar } 4866d6ad946cSShri Abhyankar 4867cc708dedSBarry Smith /*@ 4868bcf0153eSBarry Smith TSGetSolveTime - Gets the time after a call to `TSSolve()` 4869cc708dedSBarry Smith 4870cc708dedSBarry Smith Not Collective 4871cc708dedSBarry Smith 4872cc708dedSBarry Smith Input Parameter: 4873bcf0153eSBarry Smith . ts - the `TS` context 4874cc708dedSBarry Smith 4875cc708dedSBarry Smith Output Parameter: 4876bcf0153eSBarry Smith . ftime - the final time. This time corresponds to the final time set with `TSSetMaxTime()` 4877cc708dedSBarry Smith 4878487e0bb9SJed Brown Level: beginner 4879cc708dedSBarry Smith 4880bcf0153eSBarry Smith Note: 4881bcf0153eSBarry Smith Can only be called after the call to `TSSolve()` is complete. 4882cc708dedSBarry Smith 48837d66147cSPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSConvergedReason` 4884cc708dedSBarry Smith @*/ 4885d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSolveTime(TS ts, PetscReal *ftime) 4886d71ae5a4SJacob Faibussowitsch { 4887cc708dedSBarry Smith PetscFunctionBegin; 4888cc708dedSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 48894f572ea9SToby Isaac PetscAssertPointer(ftime, 2); 4890cc708dedSBarry Smith *ftime = ts->solvetime; 48913ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4892cc708dedSBarry Smith } 4893cc708dedSBarry Smith 48942c18e0fdSBarry Smith /*@ 48955ef26d82SJed Brown TSGetSNESIterations - Gets the total number of nonlinear iterations 48969f67acb7SJed Brown used by the time integrator. 48979f67acb7SJed Brown 48989f67acb7SJed Brown Not Collective 48999f67acb7SJed Brown 49009f67acb7SJed Brown Input Parameter: 4901bcf0153eSBarry Smith . ts - `TS` context 49029f67acb7SJed Brown 49039f67acb7SJed Brown Output Parameter: 49049f67acb7SJed Brown . nits - number of nonlinear iterations 49059f67acb7SJed Brown 49069f67acb7SJed Brown Level: intermediate 49079f67acb7SJed Brown 4908195e9b02SBarry Smith Note: 4909bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4910bcf0153eSBarry Smith 49111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetKSPIterations()` 49129f67acb7SJed Brown @*/ 4913d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESIterations(TS ts, PetscInt *nits) 4914d71ae5a4SJacob Faibussowitsch { 49159f67acb7SJed Brown PetscFunctionBegin; 49169f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49174f572ea9SToby Isaac PetscAssertPointer(nits, 2); 49185ef26d82SJed Brown *nits = ts->snes_its; 49193ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49209f67acb7SJed Brown } 49219f67acb7SJed Brown 49229f67acb7SJed Brown /*@ 49235ef26d82SJed Brown TSGetKSPIterations - Gets the total number of linear iterations 49249f67acb7SJed Brown used by the time integrator. 49259f67acb7SJed Brown 49269f67acb7SJed Brown Not Collective 49279f67acb7SJed Brown 49289f67acb7SJed Brown Input Parameter: 4929bcf0153eSBarry Smith . ts - `TS` context 49309f67acb7SJed Brown 49319f67acb7SJed Brown Output Parameter: 49329f67acb7SJed Brown . lits - number of linear iterations 49339f67acb7SJed Brown 49349f67acb7SJed Brown Level: intermediate 49359f67acb7SJed Brown 4936bcf0153eSBarry Smith Note: 4937bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4938bcf0153eSBarry Smith 4939bfe80ac4SPierre Jolivet .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()` 49409f67acb7SJed Brown @*/ 4941d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetKSPIterations(TS ts, PetscInt *lits) 4942d71ae5a4SJacob Faibussowitsch { 49439f67acb7SJed Brown PetscFunctionBegin; 49449f67acb7SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49454f572ea9SToby Isaac PetscAssertPointer(lits, 2); 49465ef26d82SJed Brown *lits = ts->ksp_its; 49473ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 49489f67acb7SJed Brown } 49499f67acb7SJed Brown 4950cef5090cSJed Brown /*@ 4951cef5090cSJed Brown TSGetStepRejections - Gets the total number of rejected steps. 4952cef5090cSJed Brown 4953cef5090cSJed Brown Not Collective 4954cef5090cSJed Brown 4955cef5090cSJed Brown Input Parameter: 4956bcf0153eSBarry Smith . ts - `TS` context 4957cef5090cSJed Brown 4958cef5090cSJed Brown Output Parameter: 4959cef5090cSJed Brown . rejects - number of steps rejected 4960cef5090cSJed Brown 4961cef5090cSJed Brown Level: intermediate 4962cef5090cSJed Brown 4963bcf0153eSBarry Smith Note: 4964bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4965bcf0153eSBarry Smith 49661cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetSNESFailures()`, `TSSetMaxSNESFailures()`, `TSSetErrorIfStepFails()` 4967cef5090cSJed Brown @*/ 4968d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStepRejections(TS ts, PetscInt *rejects) 4969d71ae5a4SJacob Faibussowitsch { 4970cef5090cSJed Brown PetscFunctionBegin; 4971cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49724f572ea9SToby Isaac PetscAssertPointer(rejects, 2); 4973cef5090cSJed Brown *rejects = ts->reject; 49743ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 4975cef5090cSJed Brown } 4976cef5090cSJed Brown 4977cef5090cSJed Brown /*@ 4978bcf0153eSBarry Smith TSGetSNESFailures - Gets the total number of failed `SNES` solves in a `TS` 4979cef5090cSJed Brown 4980cef5090cSJed Brown Not Collective 4981cef5090cSJed Brown 4982cef5090cSJed Brown Input Parameter: 4983bcf0153eSBarry Smith . ts - `TS` context 4984cef5090cSJed Brown 4985cef5090cSJed Brown Output Parameter: 4986cef5090cSJed Brown . fails - number of failed nonlinear solves 4987cef5090cSJed Brown 4988cef5090cSJed Brown Level: intermediate 4989cef5090cSJed Brown 4990bcf0153eSBarry Smith Note: 4991bcf0153eSBarry Smith This counter is reset to zero for each successive call to `TSSolve()`. 4992bcf0153eSBarry Smith 49931cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSSetMaxSNESFailures()` 4994cef5090cSJed Brown @*/ 4995d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetSNESFailures(TS ts, PetscInt *fails) 4996d71ae5a4SJacob Faibussowitsch { 4997cef5090cSJed Brown PetscFunctionBegin; 4998cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 49994f572ea9SToby Isaac PetscAssertPointer(fails, 2); 5000cef5090cSJed Brown *fails = ts->num_snes_failures; 50013ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5002cef5090cSJed Brown } 5003cef5090cSJed Brown 5004cef5090cSJed Brown /*@ 5005bcf0153eSBarry Smith TSSetMaxStepRejections - Sets the maximum number of step rejections before a time step fails 5006cef5090cSJed Brown 5007cef5090cSJed Brown Not Collective 5008cef5090cSJed Brown 5009d8d19677SJose E. Roman Input Parameters: 5010bcf0153eSBarry Smith + ts - `TS` context 501109cb0f53SBarry Smith - rejects - maximum number of rejected steps, pass `PETSC_UNLIMITED` for unlimited 5012cef5090cSJed Brown 5013cef5090cSJed Brown Options Database Key: 5014cef5090cSJed Brown . -ts_max_reject - Maximum number of step rejections before a step fails 5015cef5090cSJed Brown 5016cef5090cSJed Brown Level: intermediate 5017cef5090cSJed Brown 501809cb0f53SBarry Smith Developer Note: 501909cb0f53SBarry Smith The options database name is incorrect. 502009cb0f53SBarry Smith 50211cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxSNESFailures()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSSetErrorIfStepFails()`, `TSGetConvergedReason()` 5022cef5090cSJed Brown @*/ 5023d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxStepRejections(TS ts, PetscInt rejects) 5024d71ae5a4SJacob Faibussowitsch { 5025cef5090cSJed Brown PetscFunctionBegin; 5026cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 502709cb0f53SBarry Smith if (rejects == PETSC_UNLIMITED || rejects == -1) { 502809cb0f53SBarry Smith ts->max_reject = PETSC_UNLIMITED; 502909cb0f53SBarry Smith } else { 503009cb0f53SBarry Smith PetscCheck(rejects >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of rejections"); 5031cef5090cSJed Brown ts->max_reject = rejects; 503209cb0f53SBarry Smith } 50333ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5034cef5090cSJed Brown } 5035cef5090cSJed Brown 5036cef5090cSJed Brown /*@ 5037bcf0153eSBarry Smith TSSetMaxSNESFailures - Sets the maximum number of failed `SNES` solves 5038cef5090cSJed Brown 5039cef5090cSJed Brown Not Collective 5040cef5090cSJed Brown 5041d8d19677SJose E. Roman Input Parameters: 5042bcf0153eSBarry Smith + ts - `TS` context 504309cb0f53SBarry Smith - fails - maximum number of failed nonlinear solves, pass `PETSC_UNLIMITED` to allow any number of failures. 5044cef5090cSJed Brown 5045cef5090cSJed Brown Options Database Key: 5046cef5090cSJed Brown . -ts_max_snes_failures - Maximum number of nonlinear solve failures 5047cef5090cSJed Brown 5048cef5090cSJed Brown Level: intermediate 5049cef5090cSJed Brown 50501cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `SNESGetConvergedReason()`, `TSGetConvergedReason()` 5051cef5090cSJed Brown @*/ 5052d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMaxSNESFailures(TS ts, PetscInt fails) 5053d71ae5a4SJacob Faibussowitsch { 5054cef5090cSJed Brown PetscFunctionBegin; 5055cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 505609cb0f53SBarry Smith if (fails == PETSC_UNLIMITED || fails == -1) { 505709cb0f53SBarry Smith ts->max_snes_failures = PETSC_UNLIMITED; 505809cb0f53SBarry Smith } else { 505909cb0f53SBarry Smith PetscCheck(fails >= 0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Cannot have a negative maximum number of failures"); 5060cef5090cSJed Brown ts->max_snes_failures = fails; 506109cb0f53SBarry Smith } 50623ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5063cef5090cSJed Brown } 5064cef5090cSJed Brown 5065cef5090cSJed Brown /*@ 5066195e9b02SBarry Smith TSSetErrorIfStepFails - Immediately error if no step succeeds during `TSSolve()` 5067cef5090cSJed Brown 5068cef5090cSJed Brown Not Collective 5069cef5090cSJed Brown 5070d8d19677SJose E. Roman Input Parameters: 5071bcf0153eSBarry Smith + ts - `TS` context 5072bcf0153eSBarry Smith - err - `PETSC_TRUE` to error if no step succeeds, `PETSC_FALSE` to return without failure 5073cef5090cSJed Brown 5074cef5090cSJed Brown Options Database Key: 5075cef5090cSJed Brown . -ts_error_if_step_fails - Error if no step succeeds 5076cef5090cSJed Brown 5077cef5090cSJed Brown Level: intermediate 5078cef5090cSJed Brown 507942747ad1SJacob Faibussowitsch .seealso: [](ch_ts), `TS`, `TSGetSNESIterations()`, `TSGetKSPIterations()`, `TSSetMaxStepRejections()`, `TSGetStepRejections()`, `TSGetSNESFailures()`, `TSGetConvergedReason()` 5080cef5090cSJed Brown @*/ 5081d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetErrorIfStepFails(TS ts, PetscBool err) 5082d71ae5a4SJacob Faibussowitsch { 5083cef5090cSJed Brown PetscFunctionBegin; 5084cef5090cSJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5085cef5090cSJed Brown ts->errorifstepfailed = err; 50863ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5087cef5090cSJed Brown } 5088cef5090cSJed Brown 508984df9cb4SJed Brown /*@ 5090552698daSJed Brown TSGetAdapt - Get the adaptive controller context for the current method 509184df9cb4SJed Brown 50928f14a041SBarry Smith Collective if controller has not yet been created 509384df9cb4SJed Brown 50944165533cSJose E. Roman Input Parameter: 5095ed81e22dSJed Brown . ts - time stepping context 509684df9cb4SJed Brown 50974165533cSJose E. Roman Output Parameter: 5098ed81e22dSJed Brown . adapt - adaptive controller 509984df9cb4SJed Brown 510084df9cb4SJed Brown Level: intermediate 510184df9cb4SJed Brown 51021cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSAdaptSetType()`, `TSAdaptChoose()` 510384df9cb4SJed Brown @*/ 5104d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetAdapt(TS ts, TSAdapt *adapt) 5105d71ae5a4SJacob Faibussowitsch { 510684df9cb4SJed Brown PetscFunctionBegin; 510784df9cb4SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 51084f572ea9SToby Isaac PetscAssertPointer(adapt, 2); 510984df9cb4SJed Brown if (!ts->adapt) { 51109566063dSJacob Faibussowitsch PetscCall(TSAdaptCreate(PetscObjectComm((PetscObject)ts), &ts->adapt)); 51119566063dSJacob Faibussowitsch PetscCall(PetscObjectIncrementTabLevel((PetscObject)ts->adapt, (PetscObject)ts, 1)); 511284df9cb4SJed Brown } 5113bec58848SLisandro Dalcin *adapt = ts->adapt; 51143ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 511584df9cb4SJed Brown } 5116d6ebe24aSShri Abhyankar 51171c3436cfSJed Brown /*@ 5118195e9b02SBarry Smith TSSetTolerances - Set tolerances for local truncation error when using an adaptive controller 51191c3436cfSJed Brown 51201c3436cfSJed Brown Logically Collective 51211c3436cfSJed Brown 51224165533cSJose E. Roman Input Parameters: 51231c3436cfSJed Brown + ts - time integration context 512409cb0f53SBarry Smith . atol - scalar absolute tolerances 512509cb0f53SBarry Smith . vatol - vector of absolute tolerances or `NULL`, used in preference to `atol` if present 512609cb0f53SBarry Smith . rtol - scalar relative tolerances 512709cb0f53SBarry Smith - vrtol - vector of relative tolerances or `NULL`, used in preference to `rtol` if present 51281c3436cfSJed Brown 5129195e9b02SBarry Smith Options Database Keys: 5130a3cdaa26SBarry Smith + -ts_rtol <rtol> - relative tolerance for local truncation error 513167b8a455SSatish Balay - -ts_atol <atol> - Absolute tolerance for local truncation error 5132a3cdaa26SBarry Smith 5133bcf0153eSBarry Smith Level: beginner 5134bcf0153eSBarry Smith 51353ff766beSShri Abhyankar Notes: 513609cb0f53SBarry Smith `PETSC_CURRENT` or `PETSC_DETERMINE` may be used for `atol` or `rtol` to indicate the current value 513709cb0f53SBarry Smith or the default value from when the object's type was set. 513809cb0f53SBarry Smith 51393ff766beSShri Abhyankar With PETSc's implicit schemes for DAE problems, the calculation of the local truncation error 51403ff766beSShri Abhyankar (LTE) includes both the differential and the algebraic variables. If one wants the LTE to be 51413ff766beSShri Abhyankar computed only for the differential or the algebraic part then this can be done using the vector of 51423ff766beSShri Abhyankar tolerances vatol. For example, by setting the tolerance vector with the desired tolerance for the 51433ff766beSShri Abhyankar differential part and infinity for the algebraic part, the LTE calculation will include only the 51443ff766beSShri Abhyankar differential variables. 51453ff766beSShri Abhyankar 514609cb0f53SBarry Smith Fortran Note: 514709cb0f53SBarry Smith Use `PETSC_CURRENT_INTEGER` or `PETSC_DETERMINE_INTEGER`. 514809cb0f53SBarry Smith 51491cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSGetTolerances()` 51501c3436cfSJed Brown @*/ 5151d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetTolerances(TS ts, PetscReal atol, Vec vatol, PetscReal rtol, Vec vrtol) 5152d71ae5a4SJacob Faibussowitsch { 51531c3436cfSJed Brown PetscFunctionBegin; 515409cb0f53SBarry Smith if (atol == (PetscReal)PETSC_DETERMINE) { 5155dd460d27SBarry Smith ts->atol = ts->default_atol; 515609cb0f53SBarry Smith } else if (atol != (PetscReal)PETSC_CURRENT) { 515709cb0f53SBarry Smith PetscCheck(atol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Absolute tolerance %g must be non-negative", (double)atol); 515809cb0f53SBarry Smith ts->atol = atol; 515909cb0f53SBarry Smith } 516009cb0f53SBarry Smith 51611c3436cfSJed Brown if (vatol) { 51629566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vatol)); 51639566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vatol)); 51641c3436cfSJed Brown ts->vatol = vatol; 51651c3436cfSJed Brown } 516609cb0f53SBarry Smith 516709cb0f53SBarry Smith if (rtol == (PetscReal)PETSC_DETERMINE) { 5168dd460d27SBarry Smith ts->rtol = ts->default_rtol; 516909cb0f53SBarry Smith } else if (rtol != (PetscReal)PETSC_CURRENT) { 517009cb0f53SBarry Smith PetscCheck(rtol >= 0.0, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_OUTOFRANGE, "Relative tolerance %g must be non-negative", (double)rtol); 517109cb0f53SBarry Smith ts->rtol = rtol; 517209cb0f53SBarry Smith } 517309cb0f53SBarry Smith 51741c3436cfSJed Brown if (vrtol) { 51759566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)vrtol)); 51769566063dSJacob Faibussowitsch PetscCall(VecDestroy(&ts->vrtol)); 51771c3436cfSJed Brown ts->vrtol = vrtol; 51781c3436cfSJed Brown } 51793ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 51801c3436cfSJed Brown } 51811c3436cfSJed Brown 5182c5033834SJed Brown /*@ 5183c5033834SJed Brown TSGetTolerances - Get tolerances for local truncation error when using adaptive controller 5184c5033834SJed Brown 5185c5033834SJed Brown Logically Collective 5186c5033834SJed Brown 51874165533cSJose E. Roman Input Parameter: 5188c5033834SJed Brown . ts - time integration context 5189c5033834SJed Brown 51904165533cSJose E. Roman Output Parameters: 5191195e9b02SBarry Smith + atol - scalar absolute tolerances, `NULL` to ignore 5192195e9b02SBarry Smith . vatol - vector of absolute tolerances, `NULL` to ignore 5193195e9b02SBarry Smith . rtol - scalar relative tolerances, `NULL` to ignore 5194195e9b02SBarry Smith - vrtol - vector of relative tolerances, `NULL` to ignore 5195c5033834SJed Brown 5196c5033834SJed Brown Level: beginner 5197c5033834SJed Brown 51981cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSAdapt`, `TSErrorWeightedNorm()`, `TSSetTolerances()` 5199c5033834SJed Brown @*/ 5200d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetTolerances(TS ts, PetscReal *atol, Vec *vatol, PetscReal *rtol, Vec *vrtol) 5201d71ae5a4SJacob Faibussowitsch { 5202c5033834SJed Brown PetscFunctionBegin; 5203c5033834SJed Brown if (atol) *atol = ts->atol; 5204c5033834SJed Brown if (vatol) *vatol = ts->vatol; 5205c5033834SJed Brown if (rtol) *rtol = ts->rtol; 5206c5033834SJed Brown if (vrtol) *vrtol = ts->vrtol; 52073ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5208c5033834SJed Brown } 5209c5033834SJed Brown 52109c6b16b5SShri Abhyankar /*@ 52118a175baeSEmil Constantinescu TSErrorWeightedNorm - compute a weighted norm of the difference between two state vectors based on supplied absolute and relative tolerances 52121c3436cfSJed Brown 5213c3339decSBarry Smith Collective 52141c3436cfSJed Brown 52154165533cSJose E. Roman Input Parameters: 52161c3436cfSJed Brown + ts - time stepping context 5217a4868fbcSLisandro Dalcin . U - state vector, usually ts->vec_sol 5218a4868fbcSLisandro Dalcin . Y - state vector to be compared to U 5219bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 52207619abb3SShri 52214165533cSJose E. Roman Output Parameters: 5222a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 52238a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5224a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 5225a4868fbcSLisandro Dalcin 5226bcf0153eSBarry Smith Options Database Key: 5227a4868fbcSLisandro Dalcin . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 5228a4868fbcSLisandro Dalcin 52291c3436cfSJed Brown Level: developer 52301c3436cfSJed Brown 52318c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedENorm()` 52321c3436cfSJed Brown @*/ 5233d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedNorm(TS ts, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5234d71ae5a4SJacob Faibussowitsch { 5235037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 52368a175baeSEmil Constantinescu 52378a175baeSEmil Constantinescu PetscFunctionBegin; 52388a175baeSEmil Constantinescu PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5239037d63e4SStefano Zampini PetscCall(VecErrorWeightedNorms(U, Y, NULL, wnormtype, ts->atol, ts->vatol, ts->rtol, ts->vrtol, ts->adapt->ignore_max, norm, &norm_loc, norma, &norma_loc, normr, &normr_loc)); 5240037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5241037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5242037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5243037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 52448a175baeSEmil Constantinescu } 52453c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 52463c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 52473c633725SBarry Smith PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 52483ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52498a175baeSEmil Constantinescu } 52508a175baeSEmil Constantinescu 52518a175baeSEmil Constantinescu /*@ 52528a175baeSEmil Constantinescu TSErrorWeightedENorm - compute a weighted error norm based on supplied absolute and relative tolerances 52538a175baeSEmil Constantinescu 5254c3339decSBarry Smith Collective 52558a175baeSEmil Constantinescu 52564165533cSJose E. Roman Input Parameters: 52578a175baeSEmil Constantinescu + ts - time stepping context 52588a175baeSEmil Constantinescu . E - error vector 52598a175baeSEmil Constantinescu . U - state vector, usually ts->vec_sol 52608a175baeSEmil Constantinescu . Y - state vector, previous time step 5261bcf0153eSBarry Smith - wnormtype - norm type, either `NORM_2` or `NORM_INFINITY` 52628a175baeSEmil Constantinescu 52634165533cSJose E. Roman Output Parameters: 5264a2b725a8SWilliam Gropp + norm - weighted norm, a value of 1.0 achieves a balance between absolute and relative tolerances 52658a175baeSEmil Constantinescu . norma - weighted norm, a value of 1.0 means that the error meets the absolute tolerance set by the user 5266a2b725a8SWilliam Gropp - normr - weighted norm, a value of 1.0 means that the error meets the relative tolerance set by the user 52678a175baeSEmil Constantinescu 5268bcf0153eSBarry Smith Options Database Key: 52698a175baeSEmil Constantinescu . -ts_adapt_wnormtype <wnormtype> - 2, INFINITY 52708a175baeSEmil Constantinescu 52718a175baeSEmil Constantinescu Level: developer 52728a175baeSEmil Constantinescu 52738c38e02aSSatish Balay .seealso: [](ch_ts), `TS`, `VecErrorWeightedNorms()`, `TSErrorWeightedNorm()` 52748a175baeSEmil Constantinescu @*/ 5275d71ae5a4SJacob Faibussowitsch PetscErrorCode TSErrorWeightedENorm(TS ts, Vec E, Vec U, Vec Y, NormType wnormtype, PetscReal *norm, PetscReal *norma, PetscReal *normr) 5276d71ae5a4SJacob Faibussowitsch { 5277037d63e4SStefano Zampini PetscInt norma_loc, norm_loc, normr_loc; 5278037d63e4SStefano Zampini 52798a175baeSEmil Constantinescu PetscFunctionBegin; 5280037d63e4SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5281037d63e4SStefano Zampini PetscCall(VecErrorWeightedNorms(U, Y, E, wnormtype, ts->atol, ts->vatol, ts->rtol, ts->vrtol, ts->adapt->ignore_max, norm, &norm_loc, norma, &norma_loc, normr, &normr_loc)); 5282037d63e4SStefano Zampini if (wnormtype == NORM_2) { 5283037d63e4SStefano Zampini if (norm_loc) *norm = PetscSqrtReal(PetscSqr(*norm) / norm_loc); 5284037d63e4SStefano Zampini if (norma_loc) *norma = PetscSqrtReal(PetscSqr(*norma) / norma_loc); 5285037d63e4SStefano Zampini if (normr_loc) *normr = PetscSqrtReal(PetscSqr(*normr) / normr_loc); 5286037d63e4SStefano Zampini } 5287037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norm), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 5288037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*norma), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in norma"); 5289037d63e4SStefano Zampini PetscCheck(!PetscIsInfOrNanScalar(*normr), PetscObjectComm((PetscObject)ts), PETSC_ERR_FP, "Infinite or not-a-number generated in normr"); 52903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 52918a175baeSEmil Constantinescu } 52928a175baeSEmil Constantinescu 52938d59e960SJed Brown /*@ 52948d59e960SJed Brown TSSetCFLTimeLocal - Set the local CFL constraint relative to forward Euler 52958d59e960SJed Brown 5296c3339decSBarry Smith Logically Collective 52978d59e960SJed Brown 52984165533cSJose E. Roman Input Parameters: 52998d59e960SJed Brown + ts - time stepping context 53008d59e960SJed Brown - cfltime - maximum stable time step if using forward Euler (value can be different on each process) 53018d59e960SJed Brown 53028d59e960SJed Brown Note: 53038d59e960SJed Brown After calling this function, the global CFL time can be obtained by calling TSGetCFLTime() 53048d59e960SJed Brown 53058d59e960SJed Brown Level: intermediate 53068d59e960SJed Brown 53071cc06b55SBarry Smith .seealso: [](ch_ts), `TSGetCFLTime()`, `TSADAPTCFL` 53088d59e960SJed Brown @*/ 5309d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetCFLTimeLocal(TS ts, PetscReal cfltime) 5310d71ae5a4SJacob Faibussowitsch { 53118d59e960SJed Brown PetscFunctionBegin; 53128d59e960SJed Brown PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 53138d59e960SJed Brown ts->cfltime_local = cfltime; 53148d59e960SJed Brown ts->cfltime = -1.; 53153ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53168d59e960SJed Brown } 53178d59e960SJed Brown 53188d59e960SJed Brown /*@ 53198d59e960SJed Brown TSGetCFLTime - Get the maximum stable time step according to CFL criteria applied to forward Euler 53208d59e960SJed Brown 5321c3339decSBarry Smith Collective 53228d59e960SJed Brown 53234165533cSJose E. Roman Input Parameter: 53248d59e960SJed Brown . ts - time stepping context 53258d59e960SJed Brown 53264165533cSJose E. Roman Output Parameter: 53278d59e960SJed Brown . cfltime - maximum stable time step for forward Euler 53288d59e960SJed Brown 53298d59e960SJed Brown Level: advanced 53308d59e960SJed Brown 53311cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetCFLTimeLocal()` 53328d59e960SJed Brown @*/ 5333d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetCFLTime(TS ts, PetscReal *cfltime) 5334d71ae5a4SJacob Faibussowitsch { 53358d59e960SJed Brown PetscFunctionBegin; 5336462c564dSBarry Smith if (ts->cfltime < 0) PetscCallMPI(MPIU_Allreduce(&ts->cfltime_local, &ts->cfltime, 1, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject)ts))); 53378d59e960SJed Brown *cfltime = ts->cfltime; 53383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 53398d59e960SJed Brown } 53408d59e960SJed Brown 5341d6ebe24aSShri Abhyankar /*@ 5342d6ebe24aSShri Abhyankar TSVISetVariableBounds - Sets the lower and upper bounds for the solution vector. xl <= x <= xu 5343d6ebe24aSShri Abhyankar 5344d6ebe24aSShri Abhyankar Input Parameters: 5345bcf0153eSBarry Smith + ts - the `TS` context. 5346d6ebe24aSShri Abhyankar . xl - lower bound. 5347a2b725a8SWilliam Gropp - xu - upper bound. 5348d6ebe24aSShri Abhyankar 53492bd2b0e6SSatish Balay Level: advanced 53502bd2b0e6SSatish Balay 5351bcf0153eSBarry Smith Note: 5352bcf0153eSBarry Smith If this routine is not called then the lower and upper bounds are set to 5353bcf0153eSBarry Smith `PETSC_NINFINITY` and `PETSC_INFINITY` respectively during `SNESSetUp()`. 5354bcf0153eSBarry Smith 53551cc06b55SBarry Smith .seealso: [](ch_ts), `TS` 5356d6ebe24aSShri Abhyankar @*/ 5357d71ae5a4SJacob Faibussowitsch PetscErrorCode TSVISetVariableBounds(TS ts, Vec xl, Vec xu) 5358d71ae5a4SJacob Faibussowitsch { 5359d6ebe24aSShri Abhyankar SNES snes; 5360d6ebe24aSShri Abhyankar 5361d6ebe24aSShri Abhyankar PetscFunctionBegin; 53629566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 53639566063dSJacob Faibussowitsch PetscCall(SNESVISetVariableBounds(snes, xl, xu)); 53643ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5365d6ebe24aSShri Abhyankar } 5366d6ebe24aSShri Abhyankar 5367f9c1d6abSBarry Smith /*@ 5368f9c1d6abSBarry Smith TSComputeLinearStability - computes the linear stability function at a point 5369f9c1d6abSBarry Smith 5370c3339decSBarry Smith Collective 5371f9c1d6abSBarry Smith 5372f9c1d6abSBarry Smith Input Parameters: 5373bcf0153eSBarry Smith + ts - the `TS` context 53742fe279fdSBarry Smith . xr - real part of input argument 53752fe279fdSBarry Smith - xi - imaginary part of input argument 5376f9c1d6abSBarry Smith 5377f9c1d6abSBarry Smith Output Parameters: 53782fe279fdSBarry Smith + yr - real part of function value 53792fe279fdSBarry Smith - yi - imaginary part of function value 5380f9c1d6abSBarry Smith 5381f9c1d6abSBarry Smith Level: developer 5382f9c1d6abSBarry Smith 53831cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetRHSFunction()`, `TSComputeIFunction()` 5384f9c1d6abSBarry Smith @*/ 5385d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeLinearStability(TS ts, PetscReal xr, PetscReal xi, PetscReal *yr, PetscReal *yi) 5386d71ae5a4SJacob Faibussowitsch { 5387f9c1d6abSBarry Smith PetscFunctionBegin; 5388f9c1d6abSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5389dbbe0bcdSBarry Smith PetscUseTypeMethod(ts, linearstability, xr, xi, yr, yi); 53903ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5391f9c1d6abSBarry Smith } 539224655328SShri 539324655328SShri /*@ 5394dcb233daSLisandro Dalcin TSRestartStep - Flags the solver to restart the next step 5395dcb233daSLisandro Dalcin 5396c3339decSBarry Smith Collective 5397dcb233daSLisandro Dalcin 5398dcb233daSLisandro Dalcin Input Parameter: 5399bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5400dcb233daSLisandro Dalcin 5401dcb233daSLisandro Dalcin Level: advanced 5402dcb233daSLisandro Dalcin 5403dcb233daSLisandro Dalcin Notes: 5404bcf0153eSBarry Smith Multistep methods like `TSBDF` or Runge-Kutta methods with FSAL property require restarting the solver in the event of 5405dcb233daSLisandro Dalcin discontinuities. These discontinuities may be introduced as a consequence of explicitly modifications to the solution 5406dcb233daSLisandro Dalcin vector (which PETSc attempts to detect and handle) or problem coefficients (which PETSc is not able to detect). For 5407bcf0153eSBarry Smith the sake of correctness and maximum safety, users are expected to call `TSRestart()` whenever they introduce 5408dcb233daSLisandro Dalcin discontinuities in callback routines (e.g. prestep and poststep routines, or implicit/rhs function routines with 5409dcb233daSLisandro Dalcin discontinuous source terms). 5410dcb233daSLisandro Dalcin 54111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSBDF`, `TSSolve()`, `TSSetPreStep()`, `TSSetPostStep()` 5412dcb233daSLisandro Dalcin @*/ 5413d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRestartStep(TS ts) 5414d71ae5a4SJacob Faibussowitsch { 5415dcb233daSLisandro Dalcin PetscFunctionBegin; 5416dcb233daSLisandro Dalcin PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5417dcb233daSLisandro Dalcin ts->steprestart = PETSC_TRUE; 54183ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5419dcb233daSLisandro Dalcin } 5420dcb233daSLisandro Dalcin 5421dcb233daSLisandro Dalcin /*@ 542224655328SShri TSRollBack - Rolls back one time step 542324655328SShri 5424c3339decSBarry Smith Collective 542524655328SShri 542624655328SShri Input Parameter: 5427bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 542824655328SShri 542924655328SShri Level: advanced 543024655328SShri 54319dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSGetStepRollBack()`, `TSCreate()`, `TSSetUp()`, `TSDestroy()`, `TSSolve()`, `TSSetPreStep()`, `TSSetPreStage()`, `TSInterpolate()` 543224655328SShri @*/ 5433d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRollBack(TS ts) 5434d71ae5a4SJacob Faibussowitsch { 543524655328SShri PetscFunctionBegin; 543624655328SShri PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54373c633725SBarry Smith PetscCheck(!ts->steprollback, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONGSTATE, "TSRollBack already called"); 5438c61711c8SStefano Zampini PetscTryTypeMethod(ts, rollback); 5439c61711c8SStefano Zampini PetscCall(VecCopy(ts->vec_sol0, ts->vec_sol)); 544024655328SShri ts->time_step = ts->ptime - ts->ptime_prev; 544124655328SShri ts->ptime = ts->ptime_prev; 5442be5899b3SLisandro Dalcin ts->ptime_prev = ts->ptime_prev_rollback; 54432808aa04SLisandro Dalcin ts->steps--; 5444b3de5cdeSLisandro Dalcin ts->steprollback = PETSC_TRUE; 54453ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 544624655328SShri } 5447aeb4809dSShri Abhyankar 5448ff22ae23SHong Zhang /*@ 54499dc50cb5SStefano Zampini TSGetStepRollBack - Get the internal flag indicating if you are rolling back a step 54509dc50cb5SStefano Zampini 54519dc50cb5SStefano Zampini Not collective 54529dc50cb5SStefano Zampini 54539dc50cb5SStefano Zampini Input Parameter: 54549dc50cb5SStefano Zampini . ts - the `TS` context obtained from `TSCreate()` 54559dc50cb5SStefano Zampini 54569dc50cb5SStefano Zampini Output Parameter: 54579dc50cb5SStefano Zampini . flg - the rollback flag 54589dc50cb5SStefano Zampini 54599dc50cb5SStefano Zampini Level: advanced 54609dc50cb5SStefano Zampini 54619dc50cb5SStefano Zampini .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSRollBack()` 54629dc50cb5SStefano Zampini @*/ 54639dc50cb5SStefano Zampini PetscErrorCode TSGetStepRollBack(TS ts, PetscBool *flg) 54649dc50cb5SStefano Zampini { 54659dc50cb5SStefano Zampini PetscFunctionBegin; 54669dc50cb5SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 54679dc50cb5SStefano Zampini PetscAssertPointer(flg, 2); 54689dc50cb5SStefano Zampini *flg = ts->steprollback; 54699dc50cb5SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 54709dc50cb5SStefano Zampini } 54719dc50cb5SStefano Zampini 54729dc50cb5SStefano Zampini /*@ 5473c6bf8827SStefano Zampini TSGetStepResize - Get the internal flag indicating if the current step is after a resize. 5474c6bf8827SStefano Zampini 5475c6bf8827SStefano Zampini Not collective 5476c6bf8827SStefano Zampini 5477c6bf8827SStefano Zampini Input Parameter: 5478c6bf8827SStefano Zampini . ts - the `TS` context obtained from `TSCreate()` 5479c6bf8827SStefano Zampini 5480c6bf8827SStefano Zampini Output Parameter: 5481c6bf8827SStefano Zampini . flg - the resize flag 5482c6bf8827SStefano Zampini 5483c6bf8827SStefano Zampini Level: advanced 5484c6bf8827SStefano Zampini 5485c6bf8827SStefano Zampini .seealso: [](ch_ts), `TS`, `TSCreate()`, `TSSetResize()` 5486c6bf8827SStefano Zampini @*/ 5487c6bf8827SStefano Zampini PetscErrorCode TSGetStepResize(TS ts, PetscBool *flg) 5488c6bf8827SStefano Zampini { 5489c6bf8827SStefano Zampini PetscFunctionBegin; 5490c6bf8827SStefano Zampini PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5491c6bf8827SStefano Zampini PetscAssertPointer(flg, 2); 5492c6bf8827SStefano Zampini *flg = ts->stepresize; 5493c6bf8827SStefano Zampini PetscFunctionReturn(PETSC_SUCCESS); 5494c6bf8827SStefano Zampini } 5495c6bf8827SStefano Zampini 5496c6bf8827SStefano Zampini /*@ 5497ff22ae23SHong Zhang TSGetStages - Get the number of stages and stage values 5498ff22ae23SHong Zhang 5499ff22ae23SHong Zhang Input Parameter: 5500bcf0153eSBarry Smith . ts - the `TS` context obtained from `TSCreate()` 5501ff22ae23SHong Zhang 55020429704eSStefano Zampini Output Parameters: 55030429704eSStefano Zampini + ns - the number of stages 55040429704eSStefano Zampini - Y - the current stage vectors 55050429704eSStefano Zampini 5506ff22ae23SHong Zhang Level: advanced 5507ff22ae23SHong Zhang 5508bcf0153eSBarry Smith Note: 5509195e9b02SBarry Smith Both `ns` and `Y` can be `NULL`. 55100429704eSStefano Zampini 55111cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSCreate()` 5512ff22ae23SHong Zhang @*/ 5513d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetStages(TS ts, PetscInt *ns, Vec **Y) 5514d71ae5a4SJacob Faibussowitsch { 5515ff22ae23SHong Zhang PetscFunctionBegin; 5516ff22ae23SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 55174f572ea9SToby Isaac if (ns) PetscAssertPointer(ns, 2); 55184f572ea9SToby Isaac if (Y) PetscAssertPointer(Y, 3); 55190429704eSStefano Zampini if (!ts->ops->getstages) { 55200429704eSStefano Zampini if (ns) *ns = 0; 55210429704eSStefano Zampini if (Y) *Y = NULL; 5522dbbe0bcdSBarry Smith } else PetscUseTypeMethod(ts, getstages, ns, Y); 55233ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5524ff22ae23SHong Zhang } 5525ff22ae23SHong Zhang 5526847ff0e1SMatthew G. Knepley /*@C 5527847ff0e1SMatthew G. Knepley TSComputeIJacobianDefaultColor - Computes the Jacobian using finite differences and coloring to exploit matrix sparsity. 5528847ff0e1SMatthew G. Knepley 5529c3339decSBarry Smith Collective 5530847ff0e1SMatthew G. Knepley 5531847ff0e1SMatthew G. Knepley Input Parameters: 5532bcf0153eSBarry Smith + ts - the `TS` context 5533847ff0e1SMatthew G. Knepley . t - current timestep 5534847ff0e1SMatthew G. Knepley . U - state vector 5535847ff0e1SMatthew G. Knepley . Udot - time derivative of state vector 5536847ff0e1SMatthew G. Knepley . shift - shift to apply, see note below 5537847ff0e1SMatthew G. Knepley - ctx - an optional user context 5538847ff0e1SMatthew G. Knepley 5539847ff0e1SMatthew G. Knepley Output Parameters: 5540847ff0e1SMatthew G. Knepley + J - Jacobian matrix (not altered in this routine) 5541195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner (generally the same as `J`) 5542847ff0e1SMatthew G. Knepley 5543847ff0e1SMatthew G. Knepley Level: intermediate 5544847ff0e1SMatthew G. Knepley 5545847ff0e1SMatthew G. Knepley Notes: 5546847ff0e1SMatthew G. Knepley If F(t,U,Udot)=0 is the DAE, the required Jacobian is 5547847ff0e1SMatthew G. Knepley 5548847ff0e1SMatthew G. Knepley dF/dU + shift*dF/dUdot 5549847ff0e1SMatthew G. Knepley 5550847ff0e1SMatthew G. Knepley Most users should not need to explicitly call this routine, as it 5551847ff0e1SMatthew G. Knepley is used internally within the nonlinear solvers. 5552847ff0e1SMatthew G. Knepley 5553bcf0153eSBarry Smith This will first try to get the coloring from the `DM`. If the `DM` type has no coloring 5554847ff0e1SMatthew G. Knepley routine, then it will try to get the coloring from the matrix. This requires that the 5555847ff0e1SMatthew G. Knepley matrix have nonzero entries precomputed. 5556847ff0e1SMatthew G. Knepley 55571cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetIJacobian()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 5558847ff0e1SMatthew G. Knepley @*/ 5559d71ae5a4SJacob Faibussowitsch PetscErrorCode TSComputeIJacobianDefaultColor(TS ts, PetscReal t, Vec U, Vec Udot, PetscReal shift, Mat J, Mat B, void *ctx) 5560d71ae5a4SJacob Faibussowitsch { 5561847ff0e1SMatthew G. Knepley SNES snes; 5562847ff0e1SMatthew G. Knepley MatFDColoring color; 5563847ff0e1SMatthew G. Knepley PetscBool hascolor, matcolor = PETSC_FALSE; 5564847ff0e1SMatthew G. Knepley 5565847ff0e1SMatthew G. Knepley PetscFunctionBegin; 55669566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetBool(((PetscObject)ts)->options, ((PetscObject)ts)->prefix, "-ts_fd_color_use_mat", &matcolor, NULL)); 55679566063dSJacob Faibussowitsch PetscCall(PetscObjectQuery((PetscObject)B, "TSMatFDColoring", (PetscObject *)&color)); 5568847ff0e1SMatthew G. Knepley if (!color) { 5569847ff0e1SMatthew G. Knepley DM dm; 5570847ff0e1SMatthew G. Knepley ISColoring iscoloring; 5571847ff0e1SMatthew G. Knepley 55729566063dSJacob Faibussowitsch PetscCall(TSGetDM(ts, &dm)); 55739566063dSJacob Faibussowitsch PetscCall(DMHasColoring(dm, &hascolor)); 5574847ff0e1SMatthew G. Knepley if (hascolor && !matcolor) { 55759566063dSJacob Faibussowitsch PetscCall(DMCreateColoring(dm, IS_COLORING_GLOBAL, &iscoloring)); 55769566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 55772ba42892SBarry Smith PetscCall(MatFDColoringSetFunction(color, (MatFDColoringFn *)SNESTSFormFunction, (void *)ts)); 55789566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 55799566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 55809566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5581847ff0e1SMatthew G. Knepley } else { 5582847ff0e1SMatthew G. Knepley MatColoring mc; 5583847ff0e1SMatthew G. Knepley 55849566063dSJacob Faibussowitsch PetscCall(MatColoringCreate(B, &mc)); 55859566063dSJacob Faibussowitsch PetscCall(MatColoringSetDistance(mc, 2)); 55869566063dSJacob Faibussowitsch PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 55879566063dSJacob Faibussowitsch PetscCall(MatColoringSetFromOptions(mc)); 55889566063dSJacob Faibussowitsch PetscCall(MatColoringApply(mc, &iscoloring)); 55899566063dSJacob Faibussowitsch PetscCall(MatColoringDestroy(&mc)); 55909566063dSJacob Faibussowitsch PetscCall(MatFDColoringCreate(B, iscoloring, &color)); 55912ba42892SBarry Smith PetscCall(MatFDColoringSetFunction(color, (MatFDColoringFn *)SNESTSFormFunction, (void *)ts)); 55929566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetFromOptions(color)); 55939566063dSJacob Faibussowitsch PetscCall(MatFDColoringSetUp(B, iscoloring, color)); 55949566063dSJacob Faibussowitsch PetscCall(ISColoringDestroy(&iscoloring)); 5595847ff0e1SMatthew G. Knepley } 55969566063dSJacob Faibussowitsch PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)color)); 55979566063dSJacob Faibussowitsch PetscCall(PetscObjectDereference((PetscObject)color)); 5598847ff0e1SMatthew G. Knepley } 55999566063dSJacob Faibussowitsch PetscCall(TSGetSNES(ts, &snes)); 56009566063dSJacob Faibussowitsch PetscCall(MatFDColoringApply(B, color, U, snes)); 5601847ff0e1SMatthew G. Knepley if (J != B) { 56029566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J, MAT_FINAL_ASSEMBLY)); 56039566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J, MAT_FINAL_ASSEMBLY)); 5604847ff0e1SMatthew G. Knepley } 56053ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5606847ff0e1SMatthew G. Knepley } 560793b34091SDebojyoti Ghosh 5608b43aa488SJacob Faibussowitsch /*@C 56096bc98fa9SBarry Smith TSSetFunctionDomainError - Set a function that tests if the current state vector is valid 5610cb9d8021SPierre Barbier de Reuille 5611cb9d8021SPierre Barbier de Reuille Input Parameters: 5612bcf0153eSBarry Smith + ts - the `TS` context 5613bcf0153eSBarry Smith - func - function called within `TSFunctionDomainError()` 56146bc98fa9SBarry Smith 561520f4b53cSBarry Smith Calling sequence of `func`: 56168847d985SBarry Smith + ts - the `TS` context 56176bc98fa9SBarry Smith . time - the current time (of the stage) 56186bc98fa9SBarry Smith . state - the state to check if it is valid 5619a144a63fSStefano Zampini - accept - (output parameter) `PETSC_FALSE` if the state is not acceptable, `PETSC_TRUE` if acceptable 5620cb9d8021SPierre Barbier de Reuille 5621cb9d8021SPierre Barbier de Reuille Level: intermediate 5622cb9d8021SPierre Barbier de Reuille 56236bc98fa9SBarry Smith Notes: 56246bc98fa9SBarry Smith If an implicit ODE solver is being used then, in addition to providing this routine, the 5625bcf0153eSBarry Smith user's code should call `SNESSetFunctionDomainError()` when domain errors occur during 5626bcf0153eSBarry Smith function evaluations where the functions are provided by `TSSetIFunction()` or `TSSetRHSFunction()`. 5627bcf0153eSBarry Smith Use `TSGetSNES()` to obtain the `SNES` object 56286bc98fa9SBarry Smith 5629b43aa488SJacob Faibussowitsch Developer Notes: 5630bcf0153eSBarry Smith The naming of this function is inconsistent with the `SNESSetFunctionDomainError()` 56316bc98fa9SBarry Smith since one takes a function pointer and the other does not. 56326bc98fa9SBarry Smith 56331cc06b55SBarry Smith .seealso: [](ch_ts), `TSAdaptCheckStage()`, `TSFunctionDomainError()`, `SNESSetFunctionDomainError()`, `TSGetSNES()` 5634cb9d8021SPierre Barbier de Reuille @*/ 5635a144a63fSStefano Zampini PetscErrorCode TSSetFunctionDomainError(TS ts, PetscErrorCode (*func)(TS ts, PetscReal time, Vec state, PetscBool *accept)) 5636d71ae5a4SJacob Faibussowitsch { 5637cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5638cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5639cb9d8021SPierre Barbier de Reuille ts->functiondomainerror = func; 56403ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5641cb9d8021SPierre Barbier de Reuille } 5642cb9d8021SPierre Barbier de Reuille 5643cb9d8021SPierre Barbier de Reuille /*@ 56446bc98fa9SBarry Smith TSFunctionDomainError - Checks if the current state is valid 5645cb9d8021SPierre Barbier de Reuille 5646cb9d8021SPierre Barbier de Reuille Input Parameters: 5647bcf0153eSBarry Smith + ts - the `TS` context 56486bc98fa9SBarry Smith . stagetime - time of the simulation 56496bc98fa9SBarry Smith - Y - state vector to check. 5650cb9d8021SPierre Barbier de Reuille 5651cb9d8021SPierre Barbier de Reuille Output Parameter: 5652bcf0153eSBarry Smith . accept - Set to `PETSC_FALSE` if the current state vector is valid. 565396a0c994SBarry Smith 56546bc98fa9SBarry Smith Level: developer 56556bc98fa9SBarry Smith 5656bcf0153eSBarry Smith Note: 5657bcf0153eSBarry Smith This function is called by the `TS` integration routines and calls the user provided function (set with `TSSetFunctionDomainError()`) 5658bcf0153eSBarry Smith to check if the current state is valid. 5659bcf0153eSBarry Smith 56601cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetFunctionDomainError()` 5661cb9d8021SPierre Barbier de Reuille @*/ 5662d71ae5a4SJacob Faibussowitsch PetscErrorCode TSFunctionDomainError(TS ts, PetscReal stagetime, Vec Y, PetscBool *accept) 5663d71ae5a4SJacob Faibussowitsch { 5664cb9d8021SPierre Barbier de Reuille PetscFunctionBegin; 5665cb9d8021SPierre Barbier de Reuille PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5666cb9d8021SPierre Barbier de Reuille *accept = PETSC_TRUE; 56671e66621cSBarry Smith if (ts->functiondomainerror) PetscCall((*ts->functiondomainerror)(ts, stagetime, Y, accept)); 56683ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5669cb9d8021SPierre Barbier de Reuille } 56701ceb14c0SBarry Smith 5671cc4c1da9SBarry Smith /*@ 5672195e9b02SBarry Smith TSClone - This function clones a time step `TS` object. 567393b34091SDebojyoti Ghosh 5674d083f849SBarry Smith Collective 567593b34091SDebojyoti Ghosh 567693b34091SDebojyoti Ghosh Input Parameter: 5677bcf0153eSBarry Smith . tsin - The input `TS` 567893b34091SDebojyoti Ghosh 567993b34091SDebojyoti Ghosh Output Parameter: 5680bcf0153eSBarry Smith . tsout - The output `TS` (cloned) 56815eca1a21SEmil Constantinescu 56825eca1a21SEmil Constantinescu Level: developer 568393b34091SDebojyoti Ghosh 5684bcf0153eSBarry Smith Notes: 5685bcf0153eSBarry Smith This function is used to create a clone of a `TS` object. It is used in `TSARKIMEX` for initializing the slope for first stage explicit methods. 5686bcf0153eSBarry Smith It will likely be replaced in the future with a mechanism of switching methods on the fly. 5687bcf0153eSBarry Smith 5688195e9b02SBarry Smith When using `TSDestroy()` on a clone the user has to first reset the correct `TS` reference in the embedded `SNES` object: e.g., by running 5689195e9b02SBarry Smith .vb 5690195e9b02SBarry Smith SNES snes_dup = NULL; 5691195e9b02SBarry Smith TSGetSNES(ts,&snes_dup); 5692195e9b02SBarry Smith TSSetSNES(ts,snes_dup); 5693195e9b02SBarry Smith .ve 5694bcf0153eSBarry Smith 56951cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `SNES`, `TSCreate()`, `TSSetType()`, `TSSetUp()`, `TSDestroy()`, `TSSetProblemType()` 569693b34091SDebojyoti Ghosh @*/ 5697d71ae5a4SJacob Faibussowitsch PetscErrorCode TSClone(TS tsin, TS *tsout) 5698d71ae5a4SJacob Faibussowitsch { 569993b34091SDebojyoti Ghosh TS t; 5700dc846ba4SSatish Balay SNES snes_start; 5701dc846ba4SSatish Balay DM dm; 5702dc846ba4SSatish Balay TSType type; 570393b34091SDebojyoti Ghosh 570493b34091SDebojyoti Ghosh PetscFunctionBegin; 57054f572ea9SToby Isaac PetscAssertPointer(tsin, 1); 570693b34091SDebojyoti Ghosh *tsout = NULL; 570793b34091SDebojyoti Ghosh 57089566063dSJacob Faibussowitsch PetscCall(PetscHeaderCreate(t, TS_CLASSID, "TS", "Time stepping", "TS", PetscObjectComm((PetscObject)tsin), TSDestroy, TSView)); 570993b34091SDebojyoti Ghosh 571093b34091SDebojyoti Ghosh /* General TS description */ 571193b34091SDebojyoti Ghosh t->numbermonitors = 0; 5712*371d2eb7SMartin Diehl t->setupcalled = PETSC_FALSE; 571393b34091SDebojyoti Ghosh t->ksp_its = 0; 571493b34091SDebojyoti Ghosh t->snes_its = 0; 571593b34091SDebojyoti Ghosh t->nwork = 0; 57167d51462cSStefano Zampini t->rhsjacobian.time = PETSC_MIN_REAL; 571793b34091SDebojyoti Ghosh t->rhsjacobian.scale = 1.; 571893b34091SDebojyoti Ghosh t->ijacobian.shift = 1.; 571993b34091SDebojyoti Ghosh 57209566063dSJacob Faibussowitsch PetscCall(TSGetSNES(tsin, &snes_start)); 57219566063dSJacob Faibussowitsch PetscCall(TSSetSNES(t, snes_start)); 5722d15a3a53SEmil Constantinescu 57239566063dSJacob Faibussowitsch PetscCall(TSGetDM(tsin, &dm)); 57249566063dSJacob Faibussowitsch PetscCall(TSSetDM(t, dm)); 572593b34091SDebojyoti Ghosh 572693b34091SDebojyoti Ghosh t->adapt = tsin->adapt; 57279566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->adapt)); 572893b34091SDebojyoti Ghosh 5729e7069c78SShri t->trajectory = tsin->trajectory; 57309566063dSJacob Faibussowitsch PetscCall(PetscObjectReference((PetscObject)t->trajectory)); 5731e7069c78SShri 5732e7069c78SShri t->event = tsin->event; 57336b10a48eSSatish Balay if (t->event) t->event->refct++; 5734e7069c78SShri 573593b34091SDebojyoti Ghosh t->problem_type = tsin->problem_type; 573693b34091SDebojyoti Ghosh t->ptime = tsin->ptime; 5737e7069c78SShri t->ptime_prev = tsin->ptime_prev; 573893b34091SDebojyoti Ghosh t->time_step = tsin->time_step; 573993b34091SDebojyoti Ghosh t->max_time = tsin->max_time; 574093b34091SDebojyoti Ghosh t->steps = tsin->steps; 574193b34091SDebojyoti Ghosh t->max_steps = tsin->max_steps; 574293b34091SDebojyoti Ghosh t->equation_type = tsin->equation_type; 574393b34091SDebojyoti Ghosh t->atol = tsin->atol; 574493b34091SDebojyoti Ghosh t->rtol = tsin->rtol; 574593b34091SDebojyoti Ghosh t->max_snes_failures = tsin->max_snes_failures; 574693b34091SDebojyoti Ghosh t->max_reject = tsin->max_reject; 574793b34091SDebojyoti Ghosh t->errorifstepfailed = tsin->errorifstepfailed; 574893b34091SDebojyoti Ghosh 57499566063dSJacob Faibussowitsch PetscCall(TSGetType(tsin, &type)); 57509566063dSJacob Faibussowitsch PetscCall(TSSetType(t, type)); 575193b34091SDebojyoti Ghosh 575293b34091SDebojyoti Ghosh t->vec_sol = NULL; 575393b34091SDebojyoti Ghosh 575493b34091SDebojyoti Ghosh t->cfltime = tsin->cfltime; 575593b34091SDebojyoti Ghosh t->cfltime_local = tsin->cfltime_local; 575693b34091SDebojyoti Ghosh t->exact_final_time = tsin->exact_final_time; 575793b34091SDebojyoti Ghosh 5758aea10558SJacob Faibussowitsch t->ops[0] = tsin->ops[0]; 575993b34091SDebojyoti Ghosh 57600d4fed19SBarry Smith if (((PetscObject)tsin)->fortran_func_pointers) { 57610d4fed19SBarry Smith PetscInt i; 57629566063dSJacob Faibussowitsch PetscCall(PetscMalloc((10) * sizeof(void (*)(void)), &((PetscObject)t)->fortran_func_pointers)); 5763ad540459SPierre Jolivet for (i = 0; i < 10; i++) ((PetscObject)t)->fortran_func_pointers[i] = ((PetscObject)tsin)->fortran_func_pointers[i]; 57640d4fed19SBarry Smith } 576593b34091SDebojyoti Ghosh *tsout = t; 57663ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 576793b34091SDebojyoti Ghosh } 5768f3b1f45cSBarry Smith 5769d71ae5a4SJacob Faibussowitsch static PetscErrorCode RHSWrapperFunction_TSRHSJacobianTest(void *ctx, Vec x, Vec y) 5770d71ae5a4SJacob Faibussowitsch { 5771f3b1f45cSBarry Smith TS ts = (TS)ctx; 5772f3b1f45cSBarry Smith 5773f3b1f45cSBarry Smith PetscFunctionBegin; 57749566063dSJacob Faibussowitsch PetscCall(TSComputeRHSFunction(ts, 0, x, y)); 57753ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5776f3b1f45cSBarry Smith } 5777f3b1f45cSBarry Smith 5778f3b1f45cSBarry Smith /*@ 5779bcf0153eSBarry Smith TSRHSJacobianTest - Compares the multiply routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5780f3b1f45cSBarry Smith 5781c3339decSBarry Smith Logically Collective 5782f3b1f45cSBarry Smith 57832fe279fdSBarry Smith Input Parameter: 5784b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5785f3b1f45cSBarry Smith 5786f3b1f45cSBarry Smith Output Parameter: 5787bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5788f3b1f45cSBarry Smith 5789bcf0153eSBarry Smith Options Database Key: 5790f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult -mat_shell_test_mult_view - run the test at each timestep of the integrator 5791f3b1f45cSBarry Smith 5792f3b1f45cSBarry Smith Level: advanced 5793f3b1f45cSBarry Smith 5794bcf0153eSBarry Smith Note: 5795195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5796f3b1f45cSBarry Smith 57971cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MATSHELL`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTestTranspose()` 5798f3b1f45cSBarry Smith @*/ 5799d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTest(TS ts, PetscBool *flg) 5800d71ae5a4SJacob Faibussowitsch { 5801f3b1f45cSBarry Smith Mat J, B; 58028434afd1SBarry Smith TSRHSJacobianFn *func; 5803f3b1f45cSBarry Smith void *ctx; 5804f3b1f45cSBarry Smith 5805f3b1f45cSBarry Smith PetscFunctionBegin; 58069566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 58079566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 58089566063dSJacob Faibussowitsch PetscCall(MatShellTestMult(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 58093ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5810f3b1f45cSBarry Smith } 5811f3b1f45cSBarry Smith 5812cc4c1da9SBarry Smith /*@ 5813bcf0153eSBarry Smith TSRHSJacobianTestTranspose - Compares the multiply transpose routine provided to the `MATSHELL` with differencing on the `TS` given RHS function. 5814f3b1f45cSBarry Smith 5815c3339decSBarry Smith Logically Collective 5816f3b1f45cSBarry Smith 58172fe279fdSBarry Smith Input Parameter: 5818b43aa488SJacob Faibussowitsch . ts - the time stepping routine 5819f3b1f45cSBarry Smith 5820f3b1f45cSBarry Smith Output Parameter: 5821bcf0153eSBarry Smith . flg - `PETSC_TRUE` if the multiply is likely correct 5822f3b1f45cSBarry Smith 5823bcf0153eSBarry Smith Options Database Key: 5824f3b1f45cSBarry Smith . -ts_rhs_jacobian_test_mult_transpose -mat_shell_test_mult_transpose_view - run the test at each timestep of the integrator 5825f3b1f45cSBarry Smith 5826bcf0153eSBarry Smith Level: advanced 5827bcf0153eSBarry Smith 582895452b02SPatrick Sanan Notes: 5829195e9b02SBarry Smith This only works for problems defined using `TSSetRHSFunction()` and Jacobian NOT `TSSetIFunction()` and Jacobian 5830f3b1f45cSBarry Smith 58311cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `Mat`, `MatCreateShell()`, `MatShellGetContext()`, `MatShellGetOperation()`, `MatShellTestMultTranspose()`, `TSRHSJacobianTest()` 5832f3b1f45cSBarry Smith @*/ 5833d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRHSJacobianTestTranspose(TS ts, PetscBool *flg) 5834d71ae5a4SJacob Faibussowitsch { 5835f3b1f45cSBarry Smith Mat J, B; 5836f3b1f45cSBarry Smith void *ctx; 58378434afd1SBarry Smith TSRHSJacobianFn *func; 5838f3b1f45cSBarry Smith 5839f3b1f45cSBarry Smith PetscFunctionBegin; 58409566063dSJacob Faibussowitsch PetscCall(TSGetRHSJacobian(ts, &J, &B, &func, &ctx)); 58419566063dSJacob Faibussowitsch PetscCall((*func)(ts, 0.0, ts->vec_sol, J, B, ctx)); 58429566063dSJacob Faibussowitsch PetscCall(MatShellTestMultTranspose(J, RHSWrapperFunction_TSRHSJacobianTest, ts->vec_sol, ts, flg)); 58433ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5844f3b1f45cSBarry Smith } 58450fe4d17eSHong Zhang 58460fe4d17eSHong Zhang /*@ 58470fe4d17eSHong Zhang TSSetUseSplitRHSFunction - Use the split RHSFunction when a multirate method is used. 58480fe4d17eSHong Zhang 584920f4b53cSBarry Smith Logically Collective 58500fe4d17eSHong Zhang 5851d8d19677SJose E. Roman Input Parameters: 58520fe4d17eSHong Zhang + ts - timestepping context 5853bcf0153eSBarry Smith - use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 58540fe4d17eSHong Zhang 5855bcf0153eSBarry Smith Options Database Key: 58560fe4d17eSHong Zhang . -ts_use_splitrhsfunction - <true,false> 58570fe4d17eSHong Zhang 58580fe4d17eSHong Zhang Level: intermediate 58590fe4d17eSHong Zhang 5860bcf0153eSBarry Smith Note: 5861195e9b02SBarry Smith This is only for multirate methods 5862bcf0153eSBarry Smith 58631cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSGetUseSplitRHSFunction()` 58640fe4d17eSHong Zhang @*/ 5865d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetUseSplitRHSFunction(TS ts, PetscBool use_splitrhsfunction) 5866d71ae5a4SJacob Faibussowitsch { 58670fe4d17eSHong Zhang PetscFunctionBegin; 58680fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58690fe4d17eSHong Zhang ts->use_splitrhsfunction = use_splitrhsfunction; 58703ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58710fe4d17eSHong Zhang } 58720fe4d17eSHong Zhang 58730fe4d17eSHong Zhang /*@ 58740fe4d17eSHong Zhang TSGetUseSplitRHSFunction - Gets whether to use the split RHSFunction when a multirate method is used. 58750fe4d17eSHong Zhang 587620f4b53cSBarry Smith Not Collective 58770fe4d17eSHong Zhang 58780fe4d17eSHong Zhang Input Parameter: 58790fe4d17eSHong Zhang . ts - timestepping context 58800fe4d17eSHong Zhang 58810fe4d17eSHong Zhang Output Parameter: 5882bcf0153eSBarry Smith . use_splitrhsfunction - `PETSC_TRUE` indicates that the split RHSFunction will be used 58830fe4d17eSHong Zhang 58840fe4d17eSHong Zhang Level: intermediate 58850fe4d17eSHong Zhang 58861cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSetUseSplitRHSFunction()` 58870fe4d17eSHong Zhang @*/ 5888d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetUseSplitRHSFunction(TS ts, PetscBool *use_splitrhsfunction) 5889d71ae5a4SJacob Faibussowitsch { 58900fe4d17eSHong Zhang PetscFunctionBegin; 58910fe4d17eSHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 58920fe4d17eSHong Zhang *use_splitrhsfunction = ts->use_splitrhsfunction; 58933ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 58940fe4d17eSHong Zhang } 5895d60b7d5cSBarry Smith 5896d60b7d5cSBarry Smith /*@ 5897d60b7d5cSBarry Smith TSSetMatStructure - sets the relationship between the nonzero structure of the RHS Jacobian matrix to the IJacobian matrix. 5898d60b7d5cSBarry Smith 5899c3339decSBarry Smith Logically Collective 5900d60b7d5cSBarry Smith 5901d60b7d5cSBarry Smith Input Parameters: 5902d60b7d5cSBarry Smith + ts - the time-stepper 5903bcf0153eSBarry Smith - str - the structure (the default is `UNKNOWN_NONZERO_PATTERN`) 5904d60b7d5cSBarry Smith 5905d60b7d5cSBarry Smith Level: intermediate 5906d60b7d5cSBarry Smith 5907bcf0153eSBarry Smith Note: 5908d60b7d5cSBarry Smith When the relationship between the nonzero structures is known and supplied the solution process can be much faster 5909d60b7d5cSBarry Smith 59101cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatAXPY()`, `MatStructure` 5911d60b7d5cSBarry Smith @*/ 5912d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetMatStructure(TS ts, MatStructure str) 5913d71ae5a4SJacob Faibussowitsch { 5914d60b7d5cSBarry Smith PetscFunctionBegin; 5915d60b7d5cSBarry Smith PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5916d60b7d5cSBarry Smith ts->axpy_pattern = str; 59173ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 5918d60b7d5cSBarry Smith } 59194a658b32SHong Zhang 59204a658b32SHong Zhang /*@ 59218343f784SJames Wright TSSetEvaluationTimes - sets the evaluation points. The solution will be computed and stored for each time requested 59224a658b32SHong Zhang 5923c3339decSBarry Smith Collective 59244a658b32SHong Zhang 59254a658b32SHong Zhang Input Parameters: 59264a658b32SHong Zhang + ts - the time-stepper 59278343f784SJames Wright . n - number of the time points 59288343f784SJames Wright - time_points - array of the time points 59294a658b32SHong Zhang 5930bcf0153eSBarry Smith Options Database Key: 59318343f784SJames Wright . -ts_eval_times <t0,...tn> - Sets the evaluation times 59324a658b32SHong Zhang 5933bcf0153eSBarry Smith Level: intermediate 59344a658b32SHong Zhang 59354a658b32SHong Zhang Notes: 59368343f784SJames Wright The elements in `time_points` must be all increasing. They correspond to the intermediate points for time integration. 59374a658b32SHong Zhang 59388343f784SJames Wright `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 59398343f784SJames Wright 5940c80d56d9SJames Wright The intermediate solutions are saved in a vector array that can be accessed with `TSGetEvaluationSolutions()`. Thus using evaluation times may 59418343f784SJames Wright pressure the memory system when using a large number of time points. 59428343f784SJames Wright 5943c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSGetEvaluationTimes()`, `TSGetEvaluationSolutions()`, `TSSetTimeSpan()` 59444a658b32SHong Zhang @*/ 59458343f784SJames Wright PetscErrorCode TSSetEvaluationTimes(TS ts, PetscInt n, PetscReal *time_points) 5946d71ae5a4SJacob Faibussowitsch { 59478343f784SJames Wright PetscBool is_sorted; 59488343f784SJames Wright 59494a658b32SHong Zhang PetscFunctionBegin; 59504a658b32SHong Zhang PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 5951136cf249SJames Wright if (ts->eval_times && n != ts->eval_times->num_time_points) { 5952136cf249SJames Wright PetscCall(PetscFree(ts->eval_times->time_points)); 5953136cf249SJames Wright PetscCall(VecDestroyVecs(ts->eval_times->num_time_points, &ts->eval_times->sol_vecs)); 5954136cf249SJames Wright PetscCall(PetscMalloc1(n, &ts->eval_times->time_points)); 59554a658b32SHong Zhang } 5956136cf249SJames Wright if (!ts->eval_times) { 5957136cf249SJames Wright TSEvaluationTimes eval_times; 5958136cf249SJames Wright PetscCall(PetscNew(&eval_times)); 5959136cf249SJames Wright PetscCall(PetscMalloc1(n, &eval_times->time_points)); 5960136cf249SJames Wright eval_times->reltol = 1e-6; 5961136cf249SJames Wright eval_times->abstol = 10 * PETSC_MACHINE_EPSILON; 5962136cf249SJames Wright eval_times->worktol = 0; 5963136cf249SJames Wright ts->eval_times = eval_times; 59644a658b32SHong Zhang } 5965136cf249SJames Wright ts->eval_times->num_time_points = n; 59668343f784SJames Wright PetscCall(PetscSortedReal(n, time_points, &is_sorted)); 59678343f784SJames Wright PetscCheck(is_sorted, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "time_points array must be sorted"); 5968136cf249SJames Wright PetscCall(PetscArraycpy(ts->eval_times->time_points, time_points, n)); 59698343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 59708343f784SJames Wright } 59718343f784SJames Wright 59728343f784SJames Wright /*@C 59738343f784SJames Wright TSGetEvaluationTimes - gets the evaluation times set with `TSSetEvaluationTimes()` 59748343f784SJames Wright 59758343f784SJames Wright Not Collective 59768343f784SJames Wright 59778343f784SJames Wright Input Parameter: 59788343f784SJames Wright . ts - the time-stepper 59798343f784SJames Wright 59808343f784SJames Wright Output Parameters: 59818343f784SJames Wright + n - number of the time points 59828343f784SJames Wright - time_points - array of the time points 59838343f784SJames Wright 59848343f784SJames Wright Level: beginner 59858343f784SJames Wright 59868343f784SJames Wright Note: 59878343f784SJames Wright The values obtained are valid until the `TS` object is destroyed. 59888343f784SJames Wright 59898343f784SJames Wright Both `n` and `time_points` can be `NULL`. 59908343f784SJames Wright 59918343f784SJames Wright Also used to see time points set by `TSSetTimeSpan()`. 59928343f784SJames Wright 5993c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationSolutions()` 59948343f784SJames Wright @*/ 59958343f784SJames Wright PetscErrorCode TSGetEvaluationTimes(TS ts, PetscInt *n, const PetscReal *time_points[]) 59968343f784SJames Wright { 59978343f784SJames Wright PetscFunctionBegin; 59988343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 59998343f784SJames Wright if (n) PetscAssertPointer(n, 2); 60008343f784SJames Wright if (time_points) PetscAssertPointer(time_points, 3); 6001136cf249SJames Wright if (!ts->eval_times) { 60028343f784SJames Wright if (n) *n = 0; 60038343f784SJames Wright if (time_points) *time_points = NULL; 60048343f784SJames Wright } else { 6005136cf249SJames Wright if (n) *n = ts->eval_times->num_time_points; 6006136cf249SJames Wright if (time_points) *time_points = ts->eval_times->time_points; 60078343f784SJames Wright } 60088343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 60098343f784SJames Wright } 60108343f784SJames Wright 60118343f784SJames Wright /*@C 6012c80d56d9SJames Wright TSGetEvaluationSolutions - Get the number of solutions and the solutions at the evaluation time points specified 60138343f784SJames Wright 60148343f784SJames Wright Input Parameter: 60158343f784SJames Wright . ts - the `TS` context obtained from `TSCreate()` 60168343f784SJames Wright 60178343f784SJames Wright Output Parameters: 60188343f784SJames Wright + nsol - the number of solutions 60198343f784SJames Wright . sol_times - array of solution times corresponding to the solution vectors. See note below 60208343f784SJames Wright - Sols - the solution vectors 60218343f784SJames Wright 60228343f784SJames Wright Level: intermediate 60238343f784SJames Wright 60248343f784SJames Wright Notes: 60258343f784SJames Wright Both `nsol` and `Sols` can be `NULL`. 60268343f784SJames Wright 60278343f784SJames Wright Some time points in the evaluation points may be skipped by `TS` so that `nsol` is less than the number of points specified by `TSSetEvaluationTimes()`. 60288343f784SJames Wright For example, manipulating the step size, especially with a reduced precision, may cause `TS` to step over certain evaluation times. 60298343f784SJames Wright 60308343f784SJames Wright Also used to see view solutions requested by `TSSetTimeSpan()`. 60318343f784SJames Wright 60328343f784SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationTimes()` 60338343f784SJames Wright @*/ 6034c80d56d9SJames Wright PetscErrorCode TSGetEvaluationSolutions(TS ts, PetscInt *nsol, const PetscReal *sol_times[], Vec **Sols) 60358343f784SJames Wright { 60368343f784SJames Wright PetscFunctionBegin; 60378343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60388343f784SJames Wright if (nsol) PetscAssertPointer(nsol, 2); 60398343f784SJames Wright if (sol_times) PetscAssertPointer(sol_times, 3); 60408343f784SJames Wright if (Sols) PetscAssertPointer(Sols, 4); 6041136cf249SJames Wright if (!ts->eval_times) { 60428343f784SJames Wright if (nsol) *nsol = 0; 60438343f784SJames Wright if (sol_times) *sol_times = NULL; 60448343f784SJames Wright if (Sols) *Sols = NULL; 60458343f784SJames Wright } else { 6046136cf249SJames Wright if (nsol) *nsol = ts->eval_times->sol_ctr; 6047136cf249SJames Wright if (sol_times) *sol_times = ts->eval_times->sol_times; 6048136cf249SJames Wright if (Sols) *Sols = ts->eval_times->sol_vecs; 60498343f784SJames Wright } 60508343f784SJames Wright PetscFunctionReturn(PETSC_SUCCESS); 60518343f784SJames Wright } 60528343f784SJames Wright 60538343f784SJames Wright /*@ 60548343f784SJames Wright TSSetTimeSpan - sets the time span. The solution will be computed and stored for each time requested in the span 60558343f784SJames Wright 60568343f784SJames Wright Collective 60578343f784SJames Wright 60588343f784SJames Wright Input Parameters: 60598343f784SJames Wright + ts - the time-stepper 60608343f784SJames Wright . n - number of the time points (>=2) 60618343f784SJames Wright - span_times - array of the time points. The first element and the last element are the initial time and the final time respectively. 60628343f784SJames Wright 60638343f784SJames Wright Options Database Key: 60648343f784SJames Wright . -ts_time_span <t0,...tf> - Sets the time span 60658343f784SJames Wright 60668343f784SJames Wright Level: intermediate 60678343f784SJames Wright 60688343f784SJames Wright Notes: 6069c80d56d9SJames Wright This function is identical to `TSSetEvaluationTimes()`, except that it also sets the initial time and final time for the `ts` to the first and last `span_times` entries. 60708343f784SJames Wright 60718343f784SJames Wright The elements in tspan must be all increasing. They correspond to the intermediate points for time integration. 60728343f784SJames Wright 60738343f784SJames Wright `TS_EXACTFINALTIME_MATCHSTEP` must be used to make the last time step in each sub-interval match the intermediate points specified. 60748343f784SJames Wright 6075c80d56d9SJames Wright The intermediate solutions are saved in a vector array that can be accessed with `TSGetEvaluationSolutions()`. Thus using time span may 60768343f784SJames Wright pressure the memory system when using a large number of span points. 60778343f784SJames Wright 6078c80d56d9SJames Wright .seealso: [](ch_ts), `TS`, `TSSetEvaluationTimes()`, `TSGetEvaluationTimes()`, `TSGetEvaluationSolutions()` 60798343f784SJames Wright @*/ 60808343f784SJames Wright PetscErrorCode TSSetTimeSpan(TS ts, PetscInt n, PetscReal *span_times) 60818343f784SJames Wright { 60828343f784SJames Wright PetscFunctionBegin; 60838343f784SJames Wright PetscValidHeaderSpecific(ts, TS_CLASSID, 1); 60848343f784SJames Wright PetscCheck(n >= 2, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_WRONG, "Minimum time span size is 2 but %" PetscInt_FMT " is provided", n); 60858343f784SJames Wright PetscCall(TSSetEvaluationTimes(ts, n, span_times)); 60868343f784SJames Wright PetscCall(TSSetTime(ts, span_times[0])); 60878343f784SJames Wright PetscCall(TSSetMaxTime(ts, span_times[n - 1])); 60883ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 60894a658b32SHong Zhang } 60904a658b32SHong Zhang 6091cc4c1da9SBarry Smith /*@ 60927b2dc123SHong Zhang TSPruneIJacobianColor - Remove nondiagonal zeros in the Jacobian matrix and update the `MatMFFD` coloring information. 6093d7cfae9bSHong Zhang 6094195e9b02SBarry Smith Collective 6095d7cfae9bSHong Zhang 6096d7cfae9bSHong Zhang Input Parameters: 6097195e9b02SBarry Smith + ts - the `TS` context 6098d7cfae9bSHong Zhang . J - Jacobian matrix (not altered in this routine) 6099195e9b02SBarry Smith - B - newly computed Jacobian matrix to use with preconditioner 6100d7cfae9bSHong Zhang 6101d7cfae9bSHong Zhang Level: intermediate 6102d7cfae9bSHong Zhang 6103d7cfae9bSHong Zhang Notes: 6104195e9b02SBarry Smith This function improves the `MatFDColoring` performance when the Jacobian matrix was over-allocated or contains 6105195e9b02SBarry Smith many constant zeros entries, which is typically the case when the matrix is generated by a `DM` 6106d7cfae9bSHong Zhang and multiple fields are involved. 6107d7cfae9bSHong Zhang 6108d7cfae9bSHong Zhang Users need to make sure that the Jacobian matrix is properly filled to reflect the sparsity 6109195e9b02SBarry Smith structure. For `MatFDColoring`, the values of nonzero entries are not important. So one can 61107b2dc123SHong Zhang usually call `TSComputeIJacobian()` with randomized input vectors to generate a dummy Jacobian. 61117b2dc123SHong Zhang `TSComputeIJacobian()` should be called before `TSSolve()` but after `TSSetUp()`. 6112d7cfae9bSHong Zhang 61131cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `MatFDColoring`, `TSComputeIJacobianDefaultColor()`, `MatEliminateZeros()`, `MatFDColoringCreate()`, `MatFDColoringSetFunction()` 6114d7cfae9bSHong Zhang @*/ 6115d7cfae9bSHong Zhang PetscErrorCode TSPruneIJacobianColor(TS ts, Mat J, Mat B) 6116d7cfae9bSHong Zhang { 6117d7cfae9bSHong Zhang MatColoring mc = NULL; 6118d7cfae9bSHong Zhang ISColoring iscoloring = NULL; 6119d7cfae9bSHong Zhang MatFDColoring matfdcoloring = NULL; 6120d7cfae9bSHong Zhang 6121d7cfae9bSHong Zhang PetscFunctionBegin; 6122d7cfae9bSHong Zhang /* Generate new coloring after eliminating zeros in the matrix */ 612358c11ad4SPierre Jolivet PetscCall(MatEliminateZeros(B, PETSC_TRUE)); 6124d7cfae9bSHong Zhang PetscCall(MatColoringCreate(B, &mc)); 6125d7cfae9bSHong Zhang PetscCall(MatColoringSetDistance(mc, 2)); 6126d7cfae9bSHong Zhang PetscCall(MatColoringSetType(mc, MATCOLORINGSL)); 6127d7cfae9bSHong Zhang PetscCall(MatColoringSetFromOptions(mc)); 6128d7cfae9bSHong Zhang PetscCall(MatColoringApply(mc, &iscoloring)); 6129d7cfae9bSHong Zhang PetscCall(MatColoringDestroy(&mc)); 6130d7cfae9bSHong Zhang /* Replace the old coloring with the new one */ 6131d7cfae9bSHong Zhang PetscCall(MatFDColoringCreate(B, iscoloring, &matfdcoloring)); 61322ba42892SBarry Smith PetscCall(MatFDColoringSetFunction(matfdcoloring, (MatFDColoringFn *)SNESTSFormFunction, (void *)ts)); 6133d7cfae9bSHong Zhang PetscCall(MatFDColoringSetFromOptions(matfdcoloring)); 6134d7cfae9bSHong Zhang PetscCall(MatFDColoringSetUp(B, iscoloring, matfdcoloring)); 6135d7cfae9bSHong Zhang PetscCall(PetscObjectCompose((PetscObject)B, "TSMatFDColoring", (PetscObject)matfdcoloring)); 6136d7cfae9bSHong Zhang PetscCall(PetscObjectDereference((PetscObject)matfdcoloring)); 6137d7cfae9bSHong Zhang PetscCall(ISColoringDestroy(&iscoloring)); 61383ba16761SJacob Faibussowitsch PetscFunctionReturn(PETSC_SUCCESS); 6139d7cfae9bSHong Zhang } 6140